paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Kernel communication using routing socket. |
| 2 | * Copyright (C) 1999 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 6 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "if.h" |
| 25 | #include "prefix.h" |
| 26 | #include "sockunion.h" |
| 27 | #include "connected.h" |
| 28 | #include "memory.h" |
| 29 | #include "ioctl.h" |
| 30 | #include "log.h" |
| 31 | #include "str.h" |
| 32 | #include "table.h" |
| 33 | #include "rib.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 34 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "zebra/interface.h" |
| 37 | #include "zebra/zserv.h" |
| 38 | #include "zebra/debug.h" |
| 39 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 40 | extern struct zebra_privs_t zserv_privs; |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 41 | extern struct zebra_t zebrad; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 42 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Given a sockaddr length, round it up to include pad bytes following |
| 45 | * it. Assumes the kernel pads to sizeof(long). |
| 46 | * |
| 47 | * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr |
| 48 | * length anyway (< sizeof (struct sockaddr)), so this shouldn't |
| 49 | * matter. |
| 50 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | #define ROUNDUP(a) \ |
| 52 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 53 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 54 | /* |
| 55 | * Given a pointer (sockaddr or void *), return the number of bytes |
| 56 | * taken up by the sockaddr and any padding needed for alignment. |
| 57 | */ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 58 | #if defined(HAVE_SA_LEN) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 59 | #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 60 | #elif defined(HAVE_IPV6) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 61 | /* |
| 62 | * One would hope all fixed-size structure definitions are aligned, |
| 63 | * but round them up nonetheless. |
| 64 | */ |
| 65 | #define SAROUNDUP(X) \ |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 66 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 67 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 68 | (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \ |
| 69 | ROUNDUP(sizeof(struct sockaddr_in6)) : \ |
| 70 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
paul | c50ae8b | 2004-05-11 11:31:07 +0000 | [diff] [blame] | 71 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 72 | #else /* HAVE_IPV6 */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 73 | #define SAROUNDUP(X) \ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 74 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 75 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 76 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
| 77 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | #endif /* HAVE_SA_LEN */ |
| 79 | |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 80 | #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 81 | if ((RTMADDRS) & (RTA)) \ |
| 82 | { \ |
| 83 | int len = SAROUNDUP ((PNT)); \ |
paul | ea6f82b | 2005-06-28 17:20:26 +0000 | [diff] [blame] | 84 | if ( ((DEST) != NULL) && \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 85 | af_check (((struct sockaddr *)(PNT))->sa_family)) \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 86 | memcpy ((DEST), (PNT), len); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 87 | (PNT) += len; \ |
| 88 | } |
| 89 | #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 90 | if ((RTMADDRS) & (RTA)) \ |
| 91 | { \ |
| 92 | int len = SAROUNDUP ((PNT)); \ |
paul | ea6f82b | 2005-06-28 17:20:26 +0000 | [diff] [blame] | 93 | if ( ((DEST) != NULL) ) \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 94 | memcpy ((DEST), (PNT), len); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 95 | (PNT) += len; \ |
| 96 | } |
| 97 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 98 | #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \ |
| 99 | if ((RTMADDRS) & (RTA)) \ |
| 100 | { \ |
| 101 | int len = SAROUNDUP ((PNT)); \ |
| 102 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \ |
| 103 | if (IS_ZEBRA_DEBUG_KERNEL) \ |
| 104 | zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \ |
| 105 | __func__, sdl->sdl_nlen, sdl->sdl_alen); \ |
| 106 | if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \ |
| 107 | && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \ |
| 108 | { \ |
| 109 | memcpy ((DEST), sdl->sdl_data, sdl->sdl_nlen); \ |
| 110 | (DEST)[sdl->sdl_nlen] = '\0'; \ |
| 111 | (LEN) = sdl->sdl_nlen; \ |
| 112 | } \ |
| 113 | (PNT) += len; \ |
| 114 | } \ |
| 115 | else \ |
| 116 | { \ |
| 117 | (LEN) = 0; \ |
| 118 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | /* Routing socket message types. */ |
| 120 | struct message rtm_type_str[] = |
| 121 | { |
| 122 | {RTM_ADD, "RTM_ADD"}, |
| 123 | {RTM_DELETE, "RTM_DELETE"}, |
| 124 | {RTM_CHANGE, "RTM_CHANGE"}, |
| 125 | {RTM_GET, "RTM_GET"}, |
| 126 | {RTM_LOSING, "RTM_LOSING"}, |
| 127 | {RTM_REDIRECT, "RTM_REDIRECT"}, |
| 128 | {RTM_MISS, "RTM_MISS"}, |
| 129 | {RTM_LOCK, "RTM_LOCK"}, |
| 130 | {RTM_OLDADD, "RTM_OLDADD"}, |
| 131 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
| 132 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
| 133 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 134 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 135 | {RTM_IFINFO, "RTM_IFINFO"}, |
| 136 | #ifdef RTM_OIFINFO |
| 137 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
| 138 | #endif /* RTM_OIFINFO */ |
| 139 | #ifdef RTM_NEWMADDR |
| 140 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
| 141 | #endif /* RTM_NEWMADDR */ |
| 142 | #ifdef RTM_DELMADDR |
| 143 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
| 144 | #endif /* RTM_DELMADDR */ |
| 145 | #ifdef RTM_IFANNOUNCE |
| 146 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
| 147 | #endif /* RTM_IFANNOUNCE */ |
| 148 | {0, NULL} |
| 149 | }; |
| 150 | |
| 151 | struct message rtm_flag_str[] = |
| 152 | { |
| 153 | {RTF_UP, "UP"}, |
| 154 | {RTF_GATEWAY, "GATEWAY"}, |
| 155 | {RTF_HOST, "HOST"}, |
| 156 | {RTF_REJECT, "REJECT"}, |
| 157 | {RTF_DYNAMIC, "DYNAMIC"}, |
| 158 | {RTF_MODIFIED, "MODIFIED"}, |
| 159 | {RTF_DONE, "DONE"}, |
| 160 | #ifdef RTF_MASK |
| 161 | {RTF_MASK, "MASK"}, |
| 162 | #endif /* RTF_MASK */ |
| 163 | {RTF_CLONING, "CLONING"}, |
| 164 | {RTF_XRESOLVE, "XRESOLVE"}, |
| 165 | {RTF_LLINFO, "LLINFO"}, |
| 166 | {RTF_STATIC, "STATIC"}, |
| 167 | {RTF_BLACKHOLE, "BLACKHOLE"}, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 168 | #ifdef RTF_PRIVATE |
| 169 | {RTF_PRIVATE, "PRIVATE"}, |
| 170 | #endif /* RTF_PRIVATE */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | {RTF_PROTO1, "PROTO1"}, |
| 172 | {RTF_PROTO2, "PROTO2"}, |
| 173 | #ifdef RTF_PRCLONING |
| 174 | {RTF_PRCLONING, "PRCLONING"}, |
| 175 | #endif /* RTF_PRCLONING */ |
| 176 | #ifdef RTF_WASCLONED |
| 177 | {RTF_WASCLONED, "WASCLONED"}, |
| 178 | #endif /* RTF_WASCLONED */ |
| 179 | #ifdef RTF_PROTO3 |
| 180 | {RTF_PROTO3, "PROTO3"}, |
| 181 | #endif /* RTF_PROTO3 */ |
| 182 | #ifdef RTF_PINNED |
| 183 | {RTF_PINNED, "PINNED"}, |
| 184 | #endif /* RTF_PINNED */ |
| 185 | #ifdef RTF_LOCAL |
| 186 | {RTF_LOCAL, "LOCAL"}, |
| 187 | #endif /* RTF_LOCAL */ |
| 188 | #ifdef RTF_BROADCAST |
| 189 | {RTF_BROADCAST, "BROADCAST"}, |
| 190 | #endif /* RTF_BROADCAST */ |
| 191 | #ifdef RTF_MULTICAST |
| 192 | {RTF_MULTICAST, "MULTICAST"}, |
| 193 | #endif /* RTF_MULTICAST */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 194 | #ifdef RTF_MULTIRT |
| 195 | {RTF_MULTIRT, "MULTIRT"}, |
| 196 | #endif /* RTF_MULTIRT */ |
| 197 | #ifdef RTF_SETSRC |
| 198 | {RTF_SETSRC, "SETSRC"}, |
| 199 | #endif /* RTF_SETSRC */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | {0, NULL} |
| 201 | }; |
| 202 | |
| 203 | /* Kernel routing update socket. */ |
| 204 | int routing_sock = -1; |
| 205 | |
| 206 | /* Yes I'm checking ugly routing socket behavior. */ |
| 207 | /* #define DEBUG */ |
| 208 | |
| 209 | /* Supported address family check. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 210 | static int inline |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | af_check (int family) |
| 212 | { |
| 213 | if (family == AF_INET) |
| 214 | return 1; |
| 215 | #ifdef HAVE_IPV6 |
| 216 | if (family == AF_INET6) |
| 217 | return 1; |
| 218 | #endif /* HAVE_IPV6 */ |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Dump routing table flag for debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 223 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | rtm_flag_dump (int flag) |
| 225 | { |
| 226 | struct message *mes; |
| 227 | static char buf[BUFSIZ]; |
| 228 | |
gdt | cced60d | 2004-07-13 16:45:54 +0000 | [diff] [blame] | 229 | buf[0] = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
| 231 | { |
| 232 | if (mes->key & flag) |
| 233 | { |
| 234 | strlcat (buf, mes->str, BUFSIZ); |
| 235 | strlcat (buf, " ", BUFSIZ); |
| 236 | } |
| 237 | } |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 238 | zlog_debug ("Kernel: %s", buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | #ifdef RTM_IFANNOUNCE |
| 242 | /* Interface adding function */ |
| 243 | int |
| 244 | ifan_read (struct if_announcemsghdr *ifan) |
| 245 | { |
| 246 | struct interface *ifp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 247 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | ifp = if_lookup_by_index (ifan->ifan_index); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 249 | |
| 250 | if (ifp) |
| 251 | assert ( (ifp->ifindex == ifan->ifan_index) |
| 252 | || (ifp->ifindex == IFINDEX_INTERNAL) ); |
| 253 | |
| 254 | if ( (ifp == NULL || (ifp->ifindex == IFINDEX_INTERNAL) |
| 255 | && ifan->ifan_what == IFAN_ARRIVAL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 256 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 257 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 258 | zlog_debug ("%s: creating interface for ifindex %d, name %s", |
| 259 | __func__, ifan->ifan_index, ifan->ifan_name); |
| 260 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 261 | /* Create Interface */ |
ajs | 08dbfb6 | 2005-04-03 03:40:52 +0000 | [diff] [blame] | 262 | ifp = if_get_by_name_len(ifan->ifan_name, |
| 263 | strnlen(ifan->ifan_name, |
| 264 | sizeof(ifan->ifan_name))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 265 | ifp->ifindex = ifan->ifan_index; |
| 266 | |
| 267 | if_add_update (ifp); |
| 268 | } |
| 269 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 270 | if_delete_update (ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | |
| 272 | if_get_flags (ifp); |
| 273 | if_get_mtu (ifp); |
| 274 | if_get_metric (ifp); |
| 275 | |
| 276 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 277 | zlog_debug ("%s: interface %s index %d", |
| 278 | __func__, ifan->ifan_name, ifan->ifan_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | #endif /* RTM_IFANNOUNCE */ |
| 283 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 284 | /* |
| 285 | * Handle struct if_msghdr obtained from reading routing socket or |
| 286 | * sysctl (from interface_list). There may or may not be sockaddrs |
| 287 | * present after the header. |
| 288 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | int |
| 290 | ifm_read (struct if_msghdr *ifm) |
| 291 | { |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 292 | struct interface *ifp = NULL; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 293 | char ifname[IFNAMSIZ]; |
| 294 | short ifnlen = 0; |
paul | 0994c3a | 2005-11-11 09:52:40 +0000 | [diff] [blame] | 295 | caddr_t *cp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 296 | |
| 297 | /* terminate ifname at head (for strnlen) and tail (for safety) */ |
| 298 | ifname[IFNAMSIZ - 1] = '\0'; |
| 299 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 300 | /* paranoia: sanity check structure */ |
| 301 | if (ifm->ifm_msglen < sizeof(struct if_msghdr)) |
| 302 | { |
| 303 | zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n", |
| 304 | ifm->ifm_msglen); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 309 | * Check for a sockaddr_dl following the message. First, point to |
| 310 | * where a socakddr might be if one follows the message. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 311 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 312 | cp = (void *)(ifm + 1); |
| 313 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 314 | #ifdef SUNOS_5 |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 315 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 316 | * XXX This behavior should be narrowed to only the kernel versions |
| 317 | * for which the structures returned do not match the headers. |
| 318 | * |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 319 | * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 320 | * is 12 bytes larger than the 32 bit version. |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 321 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 322 | if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC) |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 323 | cp = cp + 12; |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 324 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 325 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 326 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp); |
| 327 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp); |
| 328 | RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp); |
| 329 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp); |
| 330 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen); |
| 331 | RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp); |
| 332 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp); |
| 333 | RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp); |
| 334 | |
| 335 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 336 | zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)")); |
| 337 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 338 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 339 | * Look up on ifindex first, because ifindices are the primary handle for |
| 340 | * interfaces across the user/kernel boundary, for most systems. (Some |
| 341 | * messages, such as up/down status changes on NetBSD, do not include a |
| 342 | * sockaddr_dl). |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 343 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 344 | if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL ) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 345 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 346 | /* we have an ifp, verify that the name matches as some systems, |
| 347 | * eg Solaris, have a 1:many association of ifindex:ifname |
| 348 | * if they dont match, we dont have the correct ifp and should |
| 349 | * set it back to NULL to let next check do lookup by name |
| 350 | */ |
| 351 | if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) ) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 352 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 353 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 354 | zlog_debug ("%s: ifp name %s doesnt match sdl name %s", |
| 355 | __func__, ifp->name, ifname); |
| 356 | ifp = NULL; |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 359 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 360 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 361 | * If we dont have an ifp, try looking up by name. Particularly as some |
| 362 | * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname |
| 363 | * is therefore our unique handle to that interface. |
| 364 | * |
| 365 | * Interfaces specified in the configuration file for which the ifindex |
| 366 | * has not been determined will have ifindex == IFINDEX_INTERNAL, and such |
| 367 | * interfaces are found by this search, and then their ifindex values can |
| 368 | * be filled in. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 369 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 370 | if ( (ifp == NULL) && ifnlen) |
| 371 | ifp = if_lookup_by_name (ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 372 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 373 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 374 | * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL), |
| 375 | * create or fill in an interface. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 376 | */ |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 377 | if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | { |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 379 | /* |
| 380 | * To create or fill in an interface, a sockaddr_dl (via |
| 381 | * RTA_IFP) is required. |
| 382 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 383 | if (!ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 384 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 385 | zlog_warn ("Interface index %d (new) missing ifname\n", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | ifm->ifm_index); |
| 387 | return -1; |
| 388 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 389 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 390 | if (ifp == NULL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 391 | { |
| 392 | /* Interface that zebra was not previously aware of, so create. */ |
| 393 | ifp = if_create (ifname, ifnlen); |
| 394 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 395 | zlog_debug ("%s: creating ifp for ifindex %d", |
| 396 | __func__, ifm->ifm_index); |
| 397 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 399 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 400 | zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d", |
| 401 | __func__, ifp->name, ifp->ifindex); |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 402 | /* |
| 403 | * Fill in newly created interface structure, or larval |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 404 | * structure with ifindex IFINDEX_INTERNAL. |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 405 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | ifp->ifindex = ifm->ifm_index; |
| 407 | ifp->flags = ifm->ifm_flags; |
| 408 | #if defined(__bsdi__) |
| 409 | if_kvm_get_mtu (ifp); |
| 410 | #else |
| 411 | if_get_mtu (ifp); |
| 412 | #endif /* __bsdi__ */ |
| 413 | if_get_metric (ifp); |
| 414 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 415 | if_add_update (ifp); |
| 416 | } |
| 417 | else |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 418 | /* |
| 419 | * Interface structure exists. Adjust stored flags from |
| 420 | * notification. If interface has up->down or down->up |
| 421 | * transition, call state change routines (to adjust routes, |
| 422 | * notify routing daemons, etc.). (Other flag changes are stored |
| 423 | * but apparently do not trigger action.) |
| 424 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 425 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 426 | if (ifp->ifindex != ifm->ifm_index) |
| 427 | { |
| 428 | zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, " |
| 429 | "ifm index %d", |
| 430 | __func__, ifp->name, ifp->ifindex, ifm->ifm_index); |
| 431 | return -1; |
| 432 | } |
| 433 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 434 | if (if_is_up (ifp)) |
| 435 | { |
| 436 | ifp->flags = ifm->ifm_flags; |
| 437 | if (! if_is_up (ifp)) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 438 | { |
| 439 | if_down (ifp); |
| 440 | #ifndef RTM_IFANNOUNCE |
| 441 | /* No RTM_IFANNOUNCE on this platform, so we can never |
| 442 | * distinguish between down and delete. We must presume |
| 443 | * it has been deleted. |
| 444 | * Eg, Solaris will not notify us of unplumb. |
| 445 | * |
| 446 | * XXX: Fixme - this should be runtime detected |
| 447 | * So that a binary compiled on a system with IFANNOUNCE |
| 448 | * will still behave correctly if run on a platform without |
| 449 | */ |
| 450 | if_delete_update (ifp); |
| 451 | #endif /* RTM_IFANNOUNCE */ |
| 452 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 453 | } |
| 454 | else |
| 455 | { |
| 456 | ifp->flags = ifm->ifm_flags; |
| 457 | if (if_is_up (ifp)) |
| 458 | if_up (ifp); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | #ifdef HAVE_NET_RT_IFLIST |
| 463 | ifp->stats = ifm->ifm_data; |
| 464 | #endif /* HAVE_NET_RT_IFLIST */ |
| 465 | |
| 466 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 467 | zlog_debug ("%s: interface %s index %d", |
| 468 | __func__, ifp->name, ifp->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /* Address read from struct ifa_msghdr. */ |
| 474 | void |
| 475 | ifam_read_mesg (struct ifa_msghdr *ifm, |
| 476 | union sockunion *addr, |
| 477 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 478 | union sockunion *brd, |
| 479 | char *ifname, |
| 480 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | { |
| 482 | caddr_t pnt, end; |
| 483 | |
| 484 | pnt = (caddr_t)(ifm + 1); |
| 485 | end = ((caddr_t)ifm) + ifm->ifam_msglen; |
| 486 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | /* Be sure structure is cleared */ |
| 488 | memset (mask, 0, sizeof (union sockunion)); |
| 489 | memset (addr, 0, sizeof (union sockunion)); |
| 490 | memset (dest, 0, sizeof (union sockunion)); |
| 491 | |
| 492 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 493 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifam_addrs, pnt); |
| 494 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifam_addrs, pnt); |
| 495 | RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt); |
| 496 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 497 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 498 | RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt); |
| 499 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 500 | RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 502 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 503 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x", |
| 504 | __func__, ifm->ifam_index, |
| 505 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs); |
| 506 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | /* Assert read up end point matches to end point */ |
| 508 | if (pnt != end) |
| 509 | zlog_warn ("ifam_read() does't read all socket data"); |
| 510 | } |
| 511 | |
| 512 | /* Interface's address information get. */ |
| 513 | int |
| 514 | ifam_read (struct ifa_msghdr *ifam) |
| 515 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 516 | struct interface *ifp = NULL; |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 517 | union sockunion addr, mask, brd; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 518 | char ifname[INTERFACE_NAMSIZ]; |
| 519 | short ifnlen = 0; |
| 520 | char isalias = 0; |
| 521 | |
| 522 | ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0'; |
| 523 | |
| 524 | /* Allocate and read address information. */ |
| 525 | ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen); |
| 526 | |
| 527 | if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 529 | zlog_warn ("%s: no interface for ifname %s, index %d", |
| 530 | __func__, ifname, ifam->ifam_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | return -1; |
| 532 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 533 | |
| 534 | if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ)) |
| 535 | isalias = 1; |
| 536 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | /* Check interface flag for implicit up of the interface. */ |
| 538 | if_refresh (ifp); |
| 539 | |
| 540 | /* Add connected address. */ |
| 541 | switch (sockunion_family (&addr)) |
| 542 | { |
| 543 | case AF_INET: |
| 544 | if (ifam->ifam_type == RTM_NEWADDR) |
| 545 | connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 546 | ip_masklen (mask.sin.sin_addr), |
paul | 0994c3a | 2005-11-11 09:52:40 +0000 | [diff] [blame] | 547 | &brd.sin.sin_addr, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 548 | else |
| 549 | connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 550 | ip_masklen (mask.sin.sin_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 551 | &brd.sin.sin_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 552 | break; |
| 553 | #ifdef HAVE_IPV6 |
| 554 | case AF_INET6: |
| 555 | /* Unset interface index from link-local address when IPv6 stack |
| 556 | is KAME. */ |
| 557 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
| 558 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 559 | |
| 560 | if (ifam->ifam_type == RTM_NEWADDR) |
| 561 | connected_add_ipv6 (ifp, |
| 562 | &addr.sin6.sin6_addr, |
| 563 | ip6_masklen (mask.sin6.sin6_addr), |
paul | 0994c3a | 2005-11-11 09:52:40 +0000 | [diff] [blame] | 564 | &brd.sin6.sin6_addr, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 565 | else |
| 566 | connected_delete_ipv6 (ifp, |
| 567 | &addr.sin6.sin6_addr, |
| 568 | ip6_masklen (mask.sin6.sin6_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 569 | &brd.sin6.sin6_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | break; |
| 571 | #endif /* HAVE_IPV6 */ |
| 572 | default: |
| 573 | /* Unsupported family silently ignore... */ |
| 574 | break; |
| 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* Interface function for reading kernel routing table information. */ |
| 580 | int |
| 581 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 582 | union sockunion *dest, |
| 583 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 584 | union sockunion *gate, |
| 585 | char *ifname, |
| 586 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 587 | { |
| 588 | caddr_t pnt, end; |
| 589 | |
| 590 | /* Pnt points out socket data start point. */ |
| 591 | pnt = (caddr_t)(rtm + 1); |
| 592 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 593 | |
| 594 | /* rt_msghdr version check. */ |
| 595 | if (rtm->rtm_version != RTM_VERSION) |
| 596 | zlog (NULL, LOG_WARNING, |
| 597 | "Routing message version different %d should be %d." |
| 598 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 599 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 600 | /* Be sure structure is cleared */ |
| 601 | memset (dest, 0, sizeof (union sockunion)); |
| 602 | memset (gate, 0, sizeof (union sockunion)); |
| 603 | memset (mask, 0, sizeof (union sockunion)); |
| 604 | |
| 605 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 606 | RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt); |
| 607 | RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt); |
| 608 | RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt); |
| 609 | RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 610 | RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 611 | RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt); |
| 612 | RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt); |
| 613 | RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 614 | |
| 615 | /* If there is netmask information set it's family same as |
| 616 | destination family*/ |
| 617 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 618 | mask->sa.sa_family = dest->sa.sa_family; |
| 619 | |
| 620 | /* Assert read up to the end of pointer. */ |
| 621 | if (pnt != end) |
| 622 | zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data."); |
| 623 | |
| 624 | return rtm->rtm_flags; |
| 625 | } |
| 626 | |
| 627 | void |
| 628 | rtm_read (struct rt_msghdr *rtm) |
| 629 | { |
| 630 | int flags; |
| 631 | u_char zebra_flags; |
| 632 | union sockunion dest, mask, gate; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 633 | char ifname[INTERFACE_NAMSIZ + 1]; |
| 634 | short ifnlen = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 635 | |
| 636 | zebra_flags = 0; |
| 637 | |
| 638 | /* Discard self send message. */ |
| 639 | if (rtm->rtm_type != RTM_GET |
| 640 | && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid)) |
| 641 | return; |
| 642 | |
| 643 | /* Read destination and netmask and gateway from rtm message |
| 644 | structure. */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 645 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 646 | |
| 647 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 648 | if (flags & RTF_CLONED) |
| 649 | return; |
| 650 | #endif |
| 651 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 652 | if (flags & RTF_WASCLONED) |
| 653 | return; |
| 654 | #endif |
| 655 | |
| 656 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) |
| 657 | return; |
| 658 | |
| 659 | /* This is connected route. */ |
| 660 | if (! (flags & RTF_GATEWAY)) |
| 661 | return; |
| 662 | |
| 663 | if (flags & RTF_PROTO1) |
| 664 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 665 | |
| 666 | /* This is persistent route. */ |
| 667 | if (flags & RTF_STATIC) |
| 668 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 669 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 670 | /* This is a reject or blackhole route */ |
| 671 | if (flags & RTF_REJECT) |
| 672 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 673 | if (flags & RTF_BLACKHOLE) |
| 674 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 675 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 676 | if (dest.sa.sa_family == AF_INET) |
| 677 | { |
| 678 | struct prefix_ipv4 p; |
| 679 | |
| 680 | p.family = AF_INET; |
| 681 | p.prefix = dest.sin.sin_addr; |
| 682 | if (flags & RTF_HOST) |
| 683 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 684 | else |
| 685 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 686 | |
| 687 | /* Change, delete the old prefix, we have no further information |
| 688 | * to specify the route really |
| 689 | */ |
| 690 | if (rtm->rtm_type == RTM_CHANGE) |
| 691 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
| 692 | NULL, 0, 0); |
| 693 | |
| 694 | if (rtm->rtm_type == RTM_GET |
| 695 | || rtm->rtm_type == RTM_ADD |
| 696 | || rtm->rtm_type == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 697 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 698 | &p, &gate.sin.sin_addr, 0, 0, 0, 0); |
| 699 | else |
| 700 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 701 | &p, &gate.sin.sin_addr, 0, 0); |
| 702 | } |
| 703 | #ifdef HAVE_IPV6 |
| 704 | if (dest.sa.sa_family == AF_INET6) |
| 705 | { |
| 706 | struct prefix_ipv6 p; |
| 707 | unsigned int ifindex = 0; |
| 708 | |
| 709 | p.family = AF_INET6; |
| 710 | p.prefix = dest.sin6.sin6_addr; |
| 711 | if (flags & RTF_HOST) |
| 712 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 713 | else |
| 714 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 715 | |
| 716 | #ifdef KAME |
| 717 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 718 | { |
| 719 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 720 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 721 | } |
| 722 | #endif /* KAME */ |
| 723 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 724 | /* CHANGE: delete the old prefix, we have no further information |
| 725 | * to specify the route really |
| 726 | */ |
| 727 | if (rtm->rtm_type == RTM_CHANGE) |
| 728 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
| 729 | NULL, 0, 0); |
| 730 | |
| 731 | if (rtm->rtm_type == RTM_GET |
| 732 | || rtm->rtm_type == RTM_ADD |
| 733 | || rtm->rtm_type == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 734 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 735 | &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 736 | else |
| 737 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 738 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 739 | } |
| 740 | #endif /* HAVE_IPV6 */ |
| 741 | } |
| 742 | |
| 743 | /* Interface function for the kernel routing table updates. Support |
| 744 | for RTM_CHANGE will be needed. */ |
| 745 | int |
| 746 | rtm_write (int message, |
| 747 | union sockunion *dest, |
| 748 | union sockunion *mask, |
| 749 | union sockunion *gate, |
| 750 | unsigned int index, |
| 751 | int zebra_flags, |
| 752 | int metric) |
| 753 | { |
| 754 | int ret; |
| 755 | caddr_t pnt; |
| 756 | struct interface *ifp; |
| 757 | struct sockaddr_in tmp_gate; |
| 758 | #ifdef HAVE_IPV6 |
| 759 | struct sockaddr_in6 tmp_gate6; |
| 760 | #endif /* HAVE_IPV6 */ |
| 761 | |
| 762 | /* Sequencial number of routing message. */ |
| 763 | static int msg_seq = 0; |
| 764 | |
| 765 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 766 | struct |
| 767 | { |
| 768 | struct rt_msghdr rtm; |
| 769 | char buf[512]; |
| 770 | } msg; |
| 771 | |
| 772 | memset (&tmp_gate, 0, sizeof (struct sockaddr_in)); |
| 773 | tmp_gate.sin_family = AF_INET; |
| 774 | #ifdef HAVE_SIN_LEN |
| 775 | tmp_gate.sin_len = sizeof (struct sockaddr_in); |
| 776 | #endif /* HAVE_SIN_LEN */ |
| 777 | |
| 778 | #ifdef HAVE_IPV6 |
| 779 | memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6)); |
| 780 | tmp_gate6.sin6_family = AF_INET6; |
| 781 | #ifdef SIN6_LEN |
| 782 | tmp_gate6.sin6_len = sizeof (struct sockaddr_in6); |
| 783 | #endif /* SIN6_LEN */ |
| 784 | #endif /* HAVE_IPV6 */ |
| 785 | |
| 786 | if (routing_sock < 0) |
| 787 | return ZEBRA_ERR_EPERM; |
| 788 | |
| 789 | /* Clear and set rt_msghdr values */ |
| 790 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 791 | msg.rtm.rtm_version = RTM_VERSION; |
| 792 | msg.rtm.rtm_type = message; |
| 793 | msg.rtm.rtm_seq = msg_seq++; |
| 794 | msg.rtm.rtm_addrs = RTA_DST; |
| 795 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 796 | msg.rtm.rtm_flags = RTF_UP; |
| 797 | msg.rtm.rtm_index = index; |
| 798 | |
| 799 | if (metric != 0) |
| 800 | { |
| 801 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 802 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 803 | } |
| 804 | |
| 805 | ifp = if_lookup_by_index (index); |
| 806 | |
| 807 | if (gate && message == RTM_ADD) |
| 808 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 809 | |
| 810 | if (! gate && message == RTM_ADD && ifp && |
| 811 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 812 | msg.rtm.rtm_flags |= RTF_CLONING; |
| 813 | |
| 814 | /* If no protocol specific gateway is specified, use link |
| 815 | address for gateway. */ |
| 816 | if (! gate) |
| 817 | { |
| 818 | if (!ifp) |
| 819 | { |
| 820 | zlog_warn ("no gateway found for interface index %d", index); |
| 821 | return -1; |
| 822 | } |
| 823 | gate = (union sockunion *) & ifp->sdl; |
| 824 | } |
| 825 | |
| 826 | if (mask) |
| 827 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
| 828 | else if (message == RTM_ADD) |
| 829 | msg.rtm.rtm_flags |= RTF_HOST; |
| 830 | |
| 831 | /* Tagging route with flags */ |
| 832 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 833 | |
| 834 | /* Additional flags. */ |
| 835 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 836 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 837 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 838 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 839 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 840 | |
| 841 | #ifdef HAVE_SIN_LEN |
| 842 | #define SOCKADDRSET(X,R) \ |
| 843 | if (msg.rtm.rtm_addrs & (R)) \ |
| 844 | { \ |
| 845 | int len = ROUNDUP ((X)->sa.sa_len); \ |
| 846 | memcpy (pnt, (caddr_t)(X), len); \ |
| 847 | pnt += len; \ |
| 848 | } |
| 849 | #else |
| 850 | #define SOCKADDRSET(X,R) \ |
| 851 | if (msg.rtm.rtm_addrs & (R)) \ |
| 852 | { \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 853 | int len = SAROUNDUP (X); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 854 | memcpy (pnt, (caddr_t)(X), len); \ |
| 855 | pnt += len; \ |
| 856 | } |
| 857 | #endif /* HAVE_SIN_LEN */ |
| 858 | |
| 859 | pnt = (caddr_t) msg.buf; |
| 860 | |
| 861 | /* Write each socket data into rtm message buffer */ |
| 862 | SOCKADDRSET (dest, RTA_DST); |
| 863 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 864 | SOCKADDRSET (mask, RTA_NETMASK); |
| 865 | |
| 866 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 867 | |
| 868 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 869 | |
| 870 | if (ret != msg.rtm.rtm_msglen) |
| 871 | { |
| 872 | if (errno == EEXIST) |
| 873 | return ZEBRA_ERR_RTEXIST; |
| 874 | if (errno == ENETUNREACH) |
| 875 | return ZEBRA_ERR_RTUNREACH; |
| 876 | |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 877 | zlog_warn ("write : %s (%d)", safe_strerror (errno), errno); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 878 | return -1; |
| 879 | } |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | #include "thread.h" |
| 885 | #include "zebra/zserv.h" |
| 886 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 887 | /* For debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 888 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 889 | rtmsg_debug (struct rt_msghdr *rtm) |
| 890 | { |
gdt | 6a250b0 | 2004-12-09 14:48:12 +0000 | [diff] [blame] | 891 | const char *type = "Unknown"; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 892 | struct message *mes; |
| 893 | |
| 894 | for (mes = rtm_type_str; mes->str; mes++) |
| 895 | if (mes->key == rtm->rtm_type) |
| 896 | { |
| 897 | type = mes->str; |
| 898 | break; |
| 899 | } |
| 900 | |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 901 | zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 902 | rtm_flag_dump (rtm->rtm_flags); |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 903 | zlog_debug ("Kernel: message seq %d", rtm->rtm_seq); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 904 | zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 908 | #ifndef RTAX_MAX |
| 909 | #ifdef RTA_NUMBITS |
| 910 | #define RTAX_MAX RTA_NUMBITS |
| 911 | #else |
| 912 | #define RTAX_MAX 8 |
| 913 | #endif /* RTA_NUMBITS */ |
| 914 | #endif /* RTAX_MAX */ |
| 915 | |
| 916 | /* Kernel routing table and interface updates via routing socket. */ |
| 917 | int |
| 918 | kernel_read (struct thread *thread) |
| 919 | { |
| 920 | int sock; |
| 921 | int nbytes; |
| 922 | struct rt_msghdr *rtm; |
| 923 | |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 924 | /* |
| 925 | * This must be big enough for any message the kernel might send. |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 926 | * Rather than determining how many sockaddrs of what size might be |
| 927 | * in each particular message, just use RTAX_MAX of sockaddr_storage |
| 928 | * for each. Note that the sockaddrs must be after each message |
| 929 | * definition, or rather after whichever happens to be the largest, |
| 930 | * since the buffer needs to be big enough for a message and the |
| 931 | * sockaddrs together. |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 932 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 933 | union |
| 934 | { |
| 935 | /* Routing information. */ |
| 936 | struct |
| 937 | { |
| 938 | struct rt_msghdr rtm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 939 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 940 | } r; |
| 941 | |
| 942 | /* Interface information. */ |
| 943 | struct |
| 944 | { |
| 945 | struct if_msghdr ifm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 946 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 947 | } im; |
| 948 | |
| 949 | /* Interface address information. */ |
| 950 | struct |
| 951 | { |
| 952 | struct ifa_msghdr ifa; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 953 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 954 | } ia; |
| 955 | |
| 956 | #ifdef RTM_IFANNOUNCE |
| 957 | /* Interface arrival/departure */ |
| 958 | struct |
| 959 | { |
| 960 | struct if_announcemsghdr ifan; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 961 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 962 | } ian; |
| 963 | #endif /* RTM_IFANNOUNCE */ |
| 964 | |
| 965 | } buf; |
| 966 | |
| 967 | /* Fetch routing socket. */ |
| 968 | sock = THREAD_FD (thread); |
| 969 | |
| 970 | nbytes= read (sock, &buf, sizeof buf); |
| 971 | |
| 972 | if (nbytes <= 0) |
| 973 | { |
| 974 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 975 | zlog_warn ("routing socket error: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 976 | return 0; |
| 977 | } |
| 978 | |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 979 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 980 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 981 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 982 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | |
| 984 | rtm = &buf.r.rtm; |
| 985 | |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 986 | /* |
| 987 | * Ensure that we didn't drop any data, so that processing routines |
| 988 | * can assume they have the whole message. |
| 989 | */ |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 990 | if (rtm->rtm_msglen != nbytes) |
| 991 | { |
| 992 | zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n", |
| 993 | rtm->rtm_msglen, nbytes, rtm->rtm_type); |
| 994 | return -1; |
| 995 | } |
| 996 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 997 | switch (rtm->rtm_type) |
| 998 | { |
| 999 | case RTM_ADD: |
| 1000 | case RTM_DELETE: |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1001 | case RTM_CHANGE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1002 | rtm_read (rtm); |
| 1003 | break; |
| 1004 | case RTM_IFINFO: |
| 1005 | ifm_read (&buf.im.ifm); |
| 1006 | break; |
| 1007 | case RTM_NEWADDR: |
| 1008 | case RTM_DELADDR: |
| 1009 | ifam_read (&buf.ia.ifa); |
| 1010 | break; |
| 1011 | #ifdef RTM_IFANNOUNCE |
| 1012 | case RTM_IFANNOUNCE: |
| 1013 | ifan_read (&buf.ian.ifan); |
| 1014 | break; |
| 1015 | #endif /* RTM_IFANNOUNCE */ |
| 1016 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1017 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1018 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | /* Make routing socket. */ |
| 1025 | void |
| 1026 | routing_socket () |
| 1027 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1028 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 1029 | zlog_err ("routing_socket: Can't raise privileges"); |
| 1030 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 1032 | |
| 1033 | if (routing_sock < 0) |
| 1034 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1035 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1036 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1037 | zlog_warn ("Can't init kernel routing socket"); |
| 1038 | return; |
| 1039 | } |
| 1040 | |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1041 | /* XXX: Socket should be NONBLOCK, however as we currently |
| 1042 | * discard failed writes, this will lead to inconsistencies. |
| 1043 | * For now, socket must be blocking. |
| 1044 | */ |
| 1045 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 1046 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ |
| 1047 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1048 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1049 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1050 | |
| 1051 | /* kernel_read needs rewrite. */ |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 1052 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* Exported interface function. This function simply calls |
| 1056 | routing_socket (). */ |
| 1057 | void |
| 1058 | kernel_init () |
| 1059 | { |
| 1060 | routing_socket (); |
| 1061 | } |