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