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