]>
Commit | Line | Data |
---|---|---|
718e3744 | 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" | |
edd7c245 | 34 | #include "privs.h" |
718e3744 | 35 | |
36 | #include "zebra/interface.h" | |
37 | #include "zebra/zserv.h" | |
38 | #include "zebra/debug.h" | |
ec1a4283 | 39 | #include "zebra/kernel_socket.h" |
718e3744 | 40 | |
edd7c245 | 41 | extern struct zebra_privs_t zserv_privs; |
9bcdb638 | 42 | extern struct zebra_t zebrad; |
edd7c245 | 43 | |
4bfbea8c | 44 | /* |
45 | * Given a sockaddr length, round it up to include pad bytes following | |
46 | * it. Assumes the kernel pads to sizeof(long). | |
47 | * | |
48 | * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr | |
49 | * length anyway (< sizeof (struct sockaddr)), so this shouldn't | |
50 | * matter. | |
3b33de67 | 51 | * On OS X, both 32, 64bit syatems align on 4 byte boundary |
4bfbea8c | 52 | */ |
3b33de67 DV |
53 | #ifdef __APPLE__ |
54 | #define ROUNDUP(a) \ | |
55 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(int) - 1))) : sizeof(int)) | |
56 | #else | |
718e3744 | 57 | #define ROUNDUP(a) \ |
58 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | |
3b33de67 | 59 | #endif |
718e3744 | 60 | |
4bfbea8c | 61 | /* |
62 | * Given a pointer (sockaddr or void *), return the number of bytes | |
63 | * taken up by the sockaddr and any padding needed for alignment. | |
64 | */ | |
6f0e3f6e | 65 | #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN) |
4bfbea8c | 66 | #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
30be8028 | 67 | #elif defined(HAVE_IPV6) |
4bfbea8c | 68 | /* |
69 | * One would hope all fixed-size structure definitions are aligned, | |
70 | * but round them up nonetheless. | |
71 | */ | |
72 | #define SAROUNDUP(X) \ | |
3e95a074 | 73 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
74 | ROUNDUP(sizeof(struct sockaddr_in)):\ | |
75 | (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \ | |
76 | ROUNDUP(sizeof(struct sockaddr_in6)) : \ | |
77 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ | |
c50ae8ba | 78 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))) |
30be8028 | 79 | #else /* HAVE_IPV6 */ |
4bfbea8c | 80 | #define SAROUNDUP(X) \ |
30be8028 | 81 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
82 | ROUNDUP(sizeof(struct sockaddr_in)):\ | |
83 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ | |
84 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))) | |
6f0e3f6e | 85 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ |
718e3744 | 86 | |
a05df8fd DV |
87 | /* |
88 | * We use a call to an inline function to copy (PNT) to (DEST) | |
89 | * 1. Calculating the length of the copy requires an #ifdef to determine | |
90 | * if sa_len is a field and can't be used directly inside a #define | |
91 | * 2. So the compiler doesn't complain when DEST is NULL, which is only true | |
92 | * when we are skipping the copy and incrementing to the next SA | |
ec1a4283 | 93 | */ |
a05df8fd DV |
94 | static void inline |
95 | rta_copy (union sockunion *dest, caddr_t src) { | |
96 | int len; | |
97 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
98 | len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ? | |
99 | sizeof (*dest) : ((struct sockaddr *)src)->sa_len ; | |
100 | #else | |
101 | len = (SAROUNDUP (src) > sizeof (*dest)) ? | |
102 | sizeof (*dest) : SAROUNDUP (src) ; | |
103 | #endif | |
104 | memcpy (dest, src, len); | |
105 | } | |
106 | ||
62debbbe | 107 | #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \ |
108 | if ((RTMADDRS) & (RTA)) \ | |
109 | { \ | |
110 | int len = SAROUNDUP ((PNT)); \ | |
ea6f82b9 | 111 | if ( ((DEST) != NULL) && \ |
62debbbe | 112 | af_check (((struct sockaddr *)(PNT))->sa_family)) \ |
a05df8fd | 113 | rta_copy((DEST), (PNT)); \ |
62debbbe | 114 | (PNT) += len; \ |
115 | } | |
116 | #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \ | |
117 | if ((RTMADDRS) & (RTA)) \ | |
118 | { \ | |
119 | int len = SAROUNDUP ((PNT)); \ | |
ec1a4283 | 120 | if ((DEST) != NULL) \ |
a05df8fd | 121 | rta_copy((DEST), (PNT)); \ |
62debbbe | 122 | (PNT) += len; \ |
123 | } | |
124 | ||
6fe70d1b | 125 | #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \ |
126 | if ((RTMADDRS) & (RTA)) \ | |
127 | { \ | |
ec1a4283 | 128 | u_char *pdest = (u_char *) (DEST); \ |
6fe70d1b | 129 | int len = SAROUNDUP ((PNT)); \ |
130 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \ | |
131 | if (IS_ZEBRA_DEBUG_KERNEL) \ | |
132 | zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \ | |
133 | __func__, sdl->sdl_nlen, sdl->sdl_alen); \ | |
134 | if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \ | |
135 | && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \ | |
136 | { \ | |
ec1a4283 | 137 | memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \ |
138 | pdest[sdl->sdl_nlen] = '\0'; \ | |
6fe70d1b | 139 | (LEN) = sdl->sdl_nlen; \ |
140 | } \ | |
141 | (PNT) += len; \ | |
142 | } \ | |
143 | else \ | |
144 | { \ | |
145 | (LEN) = 0; \ | |
146 | } | |
718e3744 | 147 | /* Routing socket message types. */ |
1423c809 | 148 | const struct message rtm_type_str[] = |
718e3744 | 149 | { |
150 | {RTM_ADD, "RTM_ADD"}, | |
151 | {RTM_DELETE, "RTM_DELETE"}, | |
152 | {RTM_CHANGE, "RTM_CHANGE"}, | |
153 | {RTM_GET, "RTM_GET"}, | |
154 | {RTM_LOSING, "RTM_LOSING"}, | |
155 | {RTM_REDIRECT, "RTM_REDIRECT"}, | |
156 | {RTM_MISS, "RTM_MISS"}, | |
157 | {RTM_LOCK, "RTM_LOCK"}, | |
9458b819 | 158 | #ifdef OLDADD |
718e3744 | 159 | {RTM_OLDADD, "RTM_OLDADD"}, |
9458b819 GT |
160 | #endif /* RTM_OLDADD */ |
161 | #ifdef RTM_OLDDEL | |
718e3744 | 162 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
9458b819 | 163 | #endif /* RTM_OLDDEL */ |
718e3744 | 164 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
165 | {RTM_NEWADDR, "RTM_NEWADDR"}, | |
166 | {RTM_DELADDR, "RTM_DELADDR"}, | |
167 | {RTM_IFINFO, "RTM_IFINFO"}, | |
168 | #ifdef RTM_OIFINFO | |
169 | {RTM_OIFINFO, "RTM_OIFINFO"}, | |
170 | #endif /* RTM_OIFINFO */ | |
171 | #ifdef RTM_NEWMADDR | |
172 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, | |
173 | #endif /* RTM_NEWMADDR */ | |
174 | #ifdef RTM_DELMADDR | |
175 | {RTM_DELMADDR, "RTM_DELMADDR"}, | |
176 | #endif /* RTM_DELMADDR */ | |
177 | #ifdef RTM_IFANNOUNCE | |
178 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, | |
179 | #endif /* RTM_IFANNOUNCE */ | |
180 | {0, NULL} | |
181 | }; | |
182 | ||
ce0db9cb | 183 | static const struct message rtm_flag_str[] = |
718e3744 | 184 | { |
185 | {RTF_UP, "UP"}, | |
186 | {RTF_GATEWAY, "GATEWAY"}, | |
187 | {RTF_HOST, "HOST"}, | |
188 | {RTF_REJECT, "REJECT"}, | |
189 | {RTF_DYNAMIC, "DYNAMIC"}, | |
190 | {RTF_MODIFIED, "MODIFIED"}, | |
191 | {RTF_DONE, "DONE"}, | |
192 | #ifdef RTF_MASK | |
193 | {RTF_MASK, "MASK"}, | |
194 | #endif /* RTF_MASK */ | |
e6f148e6 | 195 | #ifdef RTF_CLONING |
718e3744 | 196 | {RTF_CLONING, "CLONING"}, |
e6f148e6 | 197 | #endif /* RTF_CLONING */ |
718e3744 | 198 | {RTF_XRESOLVE, "XRESOLVE"}, |
199 | {RTF_LLINFO, "LLINFO"}, | |
200 | {RTF_STATIC, "STATIC"}, | |
201 | {RTF_BLACKHOLE, "BLACKHOLE"}, | |
6fe70d1b | 202 | #ifdef RTF_PRIVATE |
203 | {RTF_PRIVATE, "PRIVATE"}, | |
204 | #endif /* RTF_PRIVATE */ | |
718e3744 | 205 | {RTF_PROTO1, "PROTO1"}, |
206 | {RTF_PROTO2, "PROTO2"}, | |
207 | #ifdef RTF_PRCLONING | |
208 | {RTF_PRCLONING, "PRCLONING"}, | |
209 | #endif /* RTF_PRCLONING */ | |
210 | #ifdef RTF_WASCLONED | |
211 | {RTF_WASCLONED, "WASCLONED"}, | |
212 | #endif /* RTF_WASCLONED */ | |
213 | #ifdef RTF_PROTO3 | |
214 | {RTF_PROTO3, "PROTO3"}, | |
215 | #endif /* RTF_PROTO3 */ | |
216 | #ifdef RTF_PINNED | |
217 | {RTF_PINNED, "PINNED"}, | |
218 | #endif /* RTF_PINNED */ | |
219 | #ifdef RTF_LOCAL | |
220 | {RTF_LOCAL, "LOCAL"}, | |
221 | #endif /* RTF_LOCAL */ | |
222 | #ifdef RTF_BROADCAST | |
223 | {RTF_BROADCAST, "BROADCAST"}, | |
224 | #endif /* RTF_BROADCAST */ | |
225 | #ifdef RTF_MULTICAST | |
226 | {RTF_MULTICAST, "MULTICAST"}, | |
227 | #endif /* RTF_MULTICAST */ | |
6fe70d1b | 228 | #ifdef RTF_MULTIRT |
229 | {RTF_MULTIRT, "MULTIRT"}, | |
230 | #endif /* RTF_MULTIRT */ | |
231 | #ifdef RTF_SETSRC | |
232 | {RTF_SETSRC, "SETSRC"}, | |
233 | #endif /* RTF_SETSRC */ | |
718e3744 | 234 | {0, NULL} |
235 | }; | |
236 | ||
237 | /* Kernel routing update socket. */ | |
238 | int routing_sock = -1; | |
239 | ||
240 | /* Yes I'm checking ugly routing socket behavior. */ | |
241 | /* #define DEBUG */ | |
242 | ||
243 | /* Supported address family check. */ | |
62debbbe | 244 | static int inline |
718e3744 | 245 | af_check (int family) |
246 | { | |
247 | if (family == AF_INET) | |
248 | return 1; | |
249 | #ifdef HAVE_IPV6 | |
250 | if (family == AF_INET6) | |
251 | return 1; | |
252 | #endif /* HAVE_IPV6 */ | |
253 | return 0; | |
254 | } | |
6b0655a2 | 255 | |
718e3744 | 256 | /* Dump routing table flag for debug purpose. */ |
b6178002 | 257 | static void |
718e3744 | 258 | rtm_flag_dump (int flag) |
259 | { | |
80b2a941 | 260 | const struct message *mes; |
718e3744 | 261 | static char buf[BUFSIZ]; |
262 | ||
cced60dd | 263 | buf[0] = '\0'; |
718e3744 | 264 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
265 | { | |
266 | if (mes->key & flag) | |
267 | { | |
268 | strlcat (buf, mes->str, BUFSIZ); | |
269 | strlcat (buf, " ", BUFSIZ); | |
270 | } | |
271 | } | |
b6178002 | 272 | zlog_debug ("Kernel: %s", buf); |
718e3744 | 273 | } |
274 | ||
275 | #ifdef RTM_IFANNOUNCE | |
276 | /* Interface adding function */ | |
6621ca86 | 277 | static int |
718e3744 | 278 | ifan_read (struct if_announcemsghdr *ifan) |
279 | { | |
280 | struct interface *ifp; | |
6fe70d1b | 281 | |
718e3744 | 282 | ifp = if_lookup_by_index (ifan->ifan_index); |
6fe70d1b | 283 | |
284 | if (ifp) | |
285 | assert ( (ifp->ifindex == ifan->ifan_index) | |
286 | || (ifp->ifindex == IFINDEX_INTERNAL) ); | |
287 | ||
ec1a4283 | 288 | if ( (ifp == NULL) |
289 | || ((ifp->ifindex == IFINDEX_INTERNAL) | |
290 | && (ifan->ifan_what == IFAN_ARRIVAL)) ) | |
718e3744 | 291 | { |
6fe70d1b | 292 | if (IS_ZEBRA_DEBUG_KERNEL) |
293 | zlog_debug ("%s: creating interface for ifindex %d, name %s", | |
294 | __func__, ifan->ifan_index, ifan->ifan_name); | |
295 | ||
718e3744 | 296 | /* Create Interface */ |
08dbfb69 | 297 | ifp = if_get_by_name_len(ifan->ifan_name, |
298 | strnlen(ifan->ifan_name, | |
299 | sizeof(ifan->ifan_name))); | |
718e3744 | 300 | ifp->ifindex = ifan->ifan_index; |
301 | ||
1db65fad | 302 | if_get_metric (ifp); |
718e3744 | 303 | if_add_update (ifp); |
304 | } | |
305 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) | |
6eb8827d | 306 | if_delete_update (ifp); |
718e3744 | 307 | |
308 | if_get_flags (ifp); | |
309 | if_get_mtu (ifp); | |
310 | if_get_metric (ifp); | |
311 | ||
312 | if (IS_ZEBRA_DEBUG_KERNEL) | |
6fe70d1b | 313 | zlog_debug ("%s: interface %s index %d", |
314 | __func__, ifan->ifan_name, ifan->ifan_index); | |
718e3744 | 315 | |
316 | return 0; | |
317 | } | |
318 | #endif /* RTM_IFANNOUNCE */ | |
319 | ||
9234b382 | 320 | #ifdef HAVE_BSD_IFI_LINK_STATE |
c543a173 AS |
321 | /* BSD link detect translation */ |
322 | static void | |
323 | bsd_linkdetect_translate (struct if_msghdr *ifm) | |
324 | { | |
55edb0d4 AS |
325 | if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) || |
326 | (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN)) | |
c543a173 AS |
327 | SET_FLAG(ifm->ifm_flags, IFF_RUNNING); |
328 | else | |
329 | UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING); | |
330 | } | |
9234b382 | 331 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
c543a173 | 332 | |
da26e3b6 | 333 | /* |
334 | * Handle struct if_msghdr obtained from reading routing socket or | |
335 | * sysctl (from interface_list). There may or may not be sockaddrs | |
336 | * present after the header. | |
337 | */ | |
ec1a4283 | 338 | int |
718e3744 | 339 | ifm_read (struct if_msghdr *ifm) |
340 | { | |
3e95a074 | 341 | struct interface *ifp = NULL; |
a34eb368 | 342 | struct sockaddr_dl *sdl; |
6fe70d1b | 343 | char ifname[IFNAMSIZ]; |
344 | short ifnlen = 0; | |
a05df8fd | 345 | caddr_t cp; |
6fe70d1b | 346 | |
347 | /* terminate ifname at head (for strnlen) and tail (for safety) */ | |
348 | ifname[IFNAMSIZ - 1] = '\0'; | |
349 | ||
da26e3b6 | 350 | /* paranoia: sanity check structure */ |
351 | if (ifm->ifm_msglen < sizeof(struct if_msghdr)) | |
352 | { | |
353 | zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n", | |
354 | ifm->ifm_msglen); | |
355 | return -1; | |
356 | } | |
357 | ||
358 | /* | |
4bfbea8c | 359 | * Check for a sockaddr_dl following the message. First, point to |
360 | * where a socakddr might be if one follows the message. | |
da26e3b6 | 361 | */ |
4bfbea8c | 362 | cp = (void *)(ifm + 1); |
718e3744 | 363 | |
4bfbea8c | 364 | #ifdef SUNOS_5 |
3e95a074 | 365 | /* |
4bfbea8c | 366 | * XXX This behavior should be narrowed to only the kernel versions |
367 | * for which the structures returned do not match the headers. | |
368 | * | |
3e95a074 | 369 | * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions |
4bfbea8c | 370 | * is 12 bytes larger than the 32 bit version. |
3e95a074 | 371 | */ |
4bfbea8c | 372 | if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC) |
3e95a074 | 373 | cp = cp + 12; |
4bfbea8c | 374 | #endif |
3e95a074 | 375 | |
6fe70d1b | 376 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp); |
377 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp); | |
378 | RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp); | |
379 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp); | |
a34eb368 | 380 | sdl = (struct sockaddr_dl *)cp; |
6fe70d1b | 381 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen); |
382 | RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp); | |
383 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp); | |
384 | RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp); | |
385 | ||
386 | if (IS_ZEBRA_DEBUG_KERNEL) | |
387 | zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)")); | |
388 | ||
4bfbea8c | 389 | /* |
6fe70d1b | 390 | * Look up on ifindex first, because ifindices are the primary handle for |
391 | * interfaces across the user/kernel boundary, for most systems. (Some | |
392 | * messages, such as up/down status changes on NetBSD, do not include a | |
393 | * sockaddr_dl). | |
4bfbea8c | 394 | */ |
6fe70d1b | 395 | if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL ) |
3e95a074 | 396 | { |
6fe70d1b | 397 | /* we have an ifp, verify that the name matches as some systems, |
398 | * eg Solaris, have a 1:many association of ifindex:ifname | |
399 | * if they dont match, we dont have the correct ifp and should | |
400 | * set it back to NULL to let next check do lookup by name | |
401 | */ | |
402 | if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) ) | |
3e95a074 | 403 | { |
6fe70d1b | 404 | if (IS_ZEBRA_DEBUG_KERNEL) |
405 | zlog_debug ("%s: ifp name %s doesnt match sdl name %s", | |
406 | __func__, ifp->name, ifname); | |
407 | ifp = NULL; | |
3e95a074 | 408 | } |
409 | } | |
6fe70d1b | 410 | |
3e95a074 | 411 | /* |
6fe70d1b | 412 | * If we dont have an ifp, try looking up by name. Particularly as some |
413 | * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname | |
414 | * is therefore our unique handle to that interface. | |
415 | * | |
416 | * Interfaces specified in the configuration file for which the ifindex | |
417 | * has not been determined will have ifindex == IFINDEX_INTERNAL, and such | |
418 | * interfaces are found by this search, and then their ifindex values can | |
419 | * be filled in. | |
3e95a074 | 420 | */ |
6fe70d1b | 421 | if ( (ifp == NULL) && ifnlen) |
422 | ifp = if_lookup_by_name (ifname); | |
718e3744 | 423 | |
da26e3b6 | 424 | /* |
6fe70d1b | 425 | * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL), |
426 | * create or fill in an interface. | |
da26e3b6 | 427 | */ |
d2fc8896 | 428 | if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) |
718e3744 | 429 | { |
da26e3b6 | 430 | /* |
4bfbea8c | 431 | * To create or fill in an interface, a sockaddr_dl (via |
432 | * RTA_IFP) is required. | |
da26e3b6 | 433 | */ |
6fe70d1b | 434 | if (!ifnlen) |
da26e3b6 | 435 | { |
6fe70d1b | 436 | zlog_warn ("Interface index %d (new) missing ifname\n", |
4bfbea8c | 437 | ifm->ifm_index); |
da26e3b6 | 438 | return -1; |
439 | } | |
5c78b3d0 | 440 | |
441 | #ifndef RTM_IFANNOUNCE | |
442 | /* Down->Down interface should be ignored here. | |
443 | * See further comment below. | |
444 | */ | |
445 | if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP)) | |
446 | return 0; | |
447 | #endif /* !RTM_IFANNOUNCE */ | |
6fe70d1b | 448 | |
3e95a074 | 449 | if (ifp == NULL) |
6fe70d1b | 450 | { |
451 | /* Interface that zebra was not previously aware of, so create. */ | |
452 | ifp = if_create (ifname, ifnlen); | |
453 | if (IS_ZEBRA_DEBUG_KERNEL) | |
454 | zlog_debug ("%s: creating ifp for ifindex %d", | |
455 | __func__, ifm->ifm_index); | |
456 | } | |
718e3744 | 457 | |
6fe70d1b | 458 | if (IS_ZEBRA_DEBUG_KERNEL) |
459 | zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d", | |
460 | __func__, ifp->name, ifp->ifindex); | |
4bfbea8c | 461 | /* |
462 | * Fill in newly created interface structure, or larval | |
d2fc8896 | 463 | * structure with ifindex IFINDEX_INTERNAL. |
4bfbea8c | 464 | */ |
718e3744 | 465 | ifp->ifindex = ifm->ifm_index; |
c543a173 | 466 | |
9234b382 | 467 | #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */ |
c543a173 | 468 | bsd_linkdetect_translate(ifm); |
9234b382 | 469 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
c543a173 | 470 | |
5c78b3d0 | 471 | if_flags_update (ifp, ifm->ifm_flags); |
718e3744 | 472 | #if defined(__bsdi__) |
473 | if_kvm_get_mtu (ifp); | |
474 | #else | |
475 | if_get_mtu (ifp); | |
476 | #endif /* __bsdi__ */ | |
477 | if_get_metric (ifp); | |
478 | ||
a34eb368 TG |
479 | /* |
480 | * XXX sockaddr_dl contents can be larger than the structure | |
ca3ccd87 DL |
481 | * definition. There are 2 big families here: |
482 | * - BSD has sdl_len + sdl_data[16] + overruns sdl_data | |
483 | * we MUST use sdl_len here or we'll truncate data. | |
484 | * - Solaris has no sdl_len, but sdl_data[244] | |
485 | * presumably, it's not going to run past that, so sizeof() | |
486 | * is fine here. | |
a34eb368 TG |
487 | * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid |
488 | */ | |
489 | if (ifnlen) | |
ca3ccd87 DL |
490 | { |
491 | #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN | |
492 | memcpy (&ifp->sdl, sdl, sdl->sdl_len); | |
493 | #else | |
a34eb368 | 494 | memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl)); |
ca3ccd87 DL |
495 | #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */ |
496 | } | |
a34eb368 | 497 | |
718e3744 | 498 | if_add_update (ifp); |
499 | } | |
500 | else | |
da26e3b6 | 501 | /* |
502 | * Interface structure exists. Adjust stored flags from | |
503 | * notification. If interface has up->down or down->up | |
504 | * transition, call state change routines (to adjust routes, | |
505 | * notify routing daemons, etc.). (Other flag changes are stored | |
506 | * but apparently do not trigger action.) | |
507 | */ | |
718e3744 | 508 | { |
6fe70d1b | 509 | if (ifp->ifindex != ifm->ifm_index) |
510 | { | |
511 | zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, " | |
512 | "ifm index %d", | |
513 | __func__, ifp->name, ifp->ifindex, ifm->ifm_index); | |
514 | return -1; | |
515 | } | |
516 | ||
9234b382 | 517 | #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */ |
c543a173 | 518 | bsd_linkdetect_translate(ifm); |
9234b382 | 519 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
c543a173 | 520 | |
5c78b3d0 | 521 | /* update flags and handle operative->inoperative transition, if any */ |
522 | if_flags_update (ifp, ifm->ifm_flags); | |
523 | ||
6eb8827d | 524 | #ifndef RTM_IFANNOUNCE |
5c78b3d0 | 525 | if (!if_is_up (ifp)) |
526 | { | |
527 | /* No RTM_IFANNOUNCE on this platform, so we can never | |
528 | * distinguish between ~IFF_UP and delete. We must presume | |
529 | * it has been deleted. | |
530 | * Eg, Solaris will not notify us of unplumb. | |
531 | * | |
532 | * XXX: Fixme - this should be runtime detected | |
533 | * So that a binary compiled on a system with IFANNOUNCE | |
534 | * will still behave correctly if run on a platform without | |
535 | */ | |
536 | if_delete_update (ifp); | |
537 | } | |
6eb8827d | 538 | #endif /* RTM_IFANNOUNCE */ |
1ba27564 DO |
539 | if (if_is_up (ifp)) |
540 | { | |
541 | #if defined(__bsdi__) | |
542 | if_kvm_get_mtu (ifp); | |
543 | #else | |
544 | if_get_mtu (ifp); | |
545 | #endif /* __bsdi__ */ | |
546 | if_get_metric (ifp); | |
547 | } | |
718e3744 | 548 | } |
5c78b3d0 | 549 | |
718e3744 | 550 | #ifdef HAVE_NET_RT_IFLIST |
551 | ifp->stats = ifm->ifm_data; | |
552 | #endif /* HAVE_NET_RT_IFLIST */ | |
553 | ||
554 | if (IS_ZEBRA_DEBUG_KERNEL) | |
6fe70d1b | 555 | zlog_debug ("%s: interface %s index %d", |
556 | __func__, ifp->name, ifp->ifindex); | |
718e3744 | 557 | |
558 | return 0; | |
559 | } | |
6b0655a2 | 560 | |
718e3744 | 561 | /* Address read from struct ifa_msghdr. */ |
6621ca86 | 562 | static void |
718e3744 | 563 | ifam_read_mesg (struct ifa_msghdr *ifm, |
564 | union sockunion *addr, | |
565 | union sockunion *mask, | |
6fe70d1b | 566 | union sockunion *brd, |
567 | char *ifname, | |
568 | short *ifnlen) | |
718e3744 | 569 | { |
570 | caddr_t pnt, end; | |
7ab62c53 AS |
571 | union sockunion dst; |
572 | union sockunion gateway; | |
718e3744 | 573 | |
574 | pnt = (caddr_t)(ifm + 1); | |
575 | end = ((caddr_t)ifm) + ifm->ifam_msglen; | |
576 | ||
718e3744 | 577 | /* Be sure structure is cleared */ |
578 | memset (mask, 0, sizeof (union sockunion)); | |
579 | memset (addr, 0, sizeof (union sockunion)); | |
6621ca86 | 580 | memset (brd, 0, sizeof (union sockunion)); |
7ab62c53 AS |
581 | memset (&dst, 0, sizeof (union sockunion)); |
582 | memset (&gateway, 0, sizeof (union sockunion)); | |
718e3744 | 583 | |
584 | /* We fetch each socket variable into sockunion. */ | |
7ab62c53 AS |
585 | RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt); |
586 | RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt); | |
62debbbe | 587 | RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt); |
588 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt); | |
6fe70d1b | 589 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen); |
62debbbe | 590 | RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt); |
591 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt); | |
6fe70d1b | 592 | RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt); |
718e3744 | 593 | |
6fe70d1b | 594 | if (IS_ZEBRA_DEBUG_KERNEL) |
55196042 AS |
595 | { |
596 | switch (sockunion_family(addr)) | |
597 | { | |
598 | case AF_INET: | |
599 | { | |
7ab62c53 | 600 | char buf[4][INET_ADDRSTRLEN]; |
55196042 | 601 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, " |
7ab62c53 AS |
602 | "ifam_flags 0x%x, addr %s/%d broad %s dst %s " |
603 | "gateway %s", | |
604 | __func__, ifm->ifam_index, | |
55196042 | 605 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs, |
7ab62c53 | 606 | ifm->ifam_flags, |
55196042 AS |
607 | inet_ntop(AF_INET,&addr->sin.sin_addr, |
608 | buf[0],sizeof(buf[0])), | |
609 | ip_masklen(mask->sin.sin_addr), | |
610 | inet_ntop(AF_INET,&brd->sin.sin_addr, | |
7ab62c53 AS |
611 | buf[1],sizeof(buf[1])), |
612 | inet_ntop(AF_INET,&dst.sin.sin_addr, | |
613 | buf[2],sizeof(buf[2])), | |
614 | inet_ntop(AF_INET,&gateway.sin.sin_addr, | |
615 | buf[3],sizeof(buf[3]))); | |
55196042 AS |
616 | } |
617 | break; | |
618 | #ifdef HAVE_IPV6 | |
619 | case AF_INET6: | |
620 | { | |
7ab62c53 | 621 | char buf[4][INET6_ADDRSTRLEN]; |
55196042 | 622 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, " |
7ab62c53 AS |
623 | "ifam_flags 0x%x, addr %s/%d broad %s dst %s " |
624 | "gateway %s", | |
55196042 AS |
625 | __func__, ifm->ifam_index, |
626 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs, | |
7ab62c53 | 627 | ifm->ifam_flags, |
55196042 AS |
628 | inet_ntop(AF_INET6,&addr->sin6.sin6_addr, |
629 | buf[0],sizeof(buf[0])), | |
630 | ip6_masklen(mask->sin6.sin6_addr), | |
631 | inet_ntop(AF_INET6,&brd->sin6.sin6_addr, | |
7ab62c53 AS |
632 | buf[1],sizeof(buf[1])), |
633 | inet_ntop(AF_INET6,&dst.sin6.sin6_addr, | |
634 | buf[2],sizeof(buf[2])), | |
635 | inet_ntop(AF_INET6,&gateway.sin6.sin6_addr, | |
636 | buf[3],sizeof(buf[3]))); | |
55196042 AS |
637 | } |
638 | break; | |
639 | #endif /* HAVE_IPV6 */ | |
640 | default: | |
641 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x", | |
642 | __func__, ifm->ifam_index, | |
643 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs); | |
644 | break; | |
645 | } | |
646 | } | |
7ab62c53 | 647 | |
718e3744 | 648 | /* Assert read up end point matches to end point */ |
649 | if (pnt != end) | |
85a2ebf2 | 650 | zlog_warn ("ifam_read() doesn't read all socket data"); |
718e3744 | 651 | } |
652 | ||
653 | /* Interface's address information get. */ | |
ec1a4283 | 654 | int |
718e3744 | 655 | ifam_read (struct ifa_msghdr *ifam) |
656 | { | |
6fe70d1b | 657 | struct interface *ifp = NULL; |
0752ef0b | 658 | union sockunion addr, mask, brd; |
6fe70d1b | 659 | char ifname[INTERFACE_NAMSIZ]; |
660 | short ifnlen = 0; | |
661 | char isalias = 0; | |
7ab62c53 | 662 | int flags = 0; |
6fe70d1b | 663 | |
664 | ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0'; | |
665 | ||
666 | /* Allocate and read address information. */ | |
667 | ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen); | |
668 | ||
669 | if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL) | |
718e3744 | 670 | { |
6fe70d1b | 671 | zlog_warn ("%s: no interface for ifname %s, index %d", |
672 | __func__, ifname, ifam->ifam_index); | |
718e3744 | 673 | return -1; |
674 | } | |
6fe70d1b | 675 | |
676 | if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ)) | |
677 | isalias = 1; | |
678 | ||
7ab62c53 AS |
679 | /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD |
680 | field contains a broadcast address or a peer address, so we are forced to | |
681 | rely upon the interface type. */ | |
682 | if (if_is_pointopoint(ifp)) | |
683 | SET_FLAG(flags, ZEBRA_IFA_PEER); | |
684 | ||
6502208c PJ |
685 | #if 0 |
686 | /* it might seem cute to grab the interface metric here, however | |
687 | * we're processing an address update message, and so some systems | |
688 | * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left | |
689 | * in deliberately, as comment. | |
690 | */ | |
d34b8991 | 691 | ifp->metric = ifam->ifam_metric; |
6502208c PJ |
692 | #endif |
693 | ||
718e3744 | 694 | /* Add connected address. */ |
695 | switch (sockunion_family (&addr)) | |
696 | { | |
697 | case AF_INET: | |
698 | if (ifam->ifam_type == RTM_NEWADDR) | |
7ab62c53 | 699 | connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr, |
718e3744 | 700 | ip_masklen (mask.sin.sin_addr), |
d34b8991 | 701 | &brd.sin.sin_addr, |
702 | (isalias ? ifname : NULL)); | |
718e3744 | 703 | else |
7ab62c53 | 704 | connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr, |
718e3744 | 705 | ip_masklen (mask.sin.sin_addr), |
0752ef0b | 706 | &brd.sin.sin_addr); |
718e3744 | 707 | break; |
708 | #ifdef HAVE_IPV6 | |
709 | case AF_INET6: | |
710 | /* Unset interface index from link-local address when IPv6 stack | |
711 | is KAME. */ | |
712 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) | |
713 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); | |
714 | ||
715 | if (ifam->ifam_type == RTM_NEWADDR) | |
7ab62c53 | 716 | connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr, |
718e3744 | 717 | ip6_masklen (mask.sin6.sin6_addr), |
d34b8991 | 718 | &brd.sin6.sin6_addr, |
719 | (isalias ? ifname : NULL)); | |
718e3744 | 720 | else |
721 | connected_delete_ipv6 (ifp, | |
722 | &addr.sin6.sin6_addr, | |
723 | ip6_masklen (mask.sin6.sin6_addr), | |
0752ef0b | 724 | &brd.sin6.sin6_addr); |
718e3744 | 725 | break; |
726 | #endif /* HAVE_IPV6 */ | |
727 | default: | |
728 | /* Unsupported family silently ignore... */ | |
729 | break; | |
730 | } | |
5c78b3d0 | 731 | |
732 | /* Check interface flag for implicit up of the interface. */ | |
733 | if_refresh (ifp); | |
734 | ||
735 | #ifdef SUNOS_5 | |
736 | /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange. | |
737 | * See comments for SUNOS_5 in interface.c::if_flags_mangle. | |
738 | * | |
739 | * Here we take care of case where the real IFF_UP was previously | |
740 | * unset (as kept in struct zebra_if.primary_state) and the mangled | |
741 | * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned | |
742 | * to unset due to the lost non-primary address having DELADDR'd. | |
743 | * | |
744 | * we must delete the interface, because in between here and next | |
745 | * event for this interface-name the administrator could unplumb | |
746 | * and replumb the interface. | |
747 | */ | |
748 | if (!if_is_up (ifp)) | |
749 | if_delete_update (ifp); | |
750 | #endif /* SUNOS_5 */ | |
751 | ||
718e3744 | 752 | return 0; |
753 | } | |
6b0655a2 | 754 | |
718e3744 | 755 | /* Interface function for reading kernel routing table information. */ |
6621ca86 | 756 | static int |
718e3744 | 757 | rtm_read_mesg (struct rt_msghdr *rtm, |
758 | union sockunion *dest, | |
759 | union sockunion *mask, | |
6fe70d1b | 760 | union sockunion *gate, |
761 | char *ifname, | |
762 | short *ifnlen) | |
718e3744 | 763 | { |
764 | caddr_t pnt, end; | |
765 | ||
766 | /* Pnt points out socket data start point. */ | |
767 | pnt = (caddr_t)(rtm + 1); | |
768 | end = ((caddr_t)rtm) + rtm->rtm_msglen; | |
769 | ||
770 | /* rt_msghdr version check. */ | |
771 | if (rtm->rtm_version != RTM_VERSION) | |
772 | zlog (NULL, LOG_WARNING, | |
773 | "Routing message version different %d should be %d." | |
774 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); | |
62debbbe | 775 | |
718e3744 | 776 | /* Be sure structure is cleared */ |
777 | memset (dest, 0, sizeof (union sockunion)); | |
778 | memset (gate, 0, sizeof (union sockunion)); | |
779 | memset (mask, 0, sizeof (union sockunion)); | |
780 | ||
781 | /* We fetch each socket variable into sockunion. */ | |
62debbbe | 782 | RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt); |
783 | RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt); | |
784 | RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt); | |
785 | RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt); | |
6fe70d1b | 786 | RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen); |
62debbbe | 787 | RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt); |
788 | RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt); | |
789 | RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt); | |
718e3744 | 790 | |
791 | /* If there is netmask information set it's family same as | |
792 | destination family*/ | |
793 | if (rtm->rtm_addrs & RTA_NETMASK) | |
794 | mask->sa.sa_family = dest->sa.sa_family; | |
795 | ||
796 | /* Assert read up to the end of pointer. */ | |
797 | if (pnt != end) | |
85a2ebf2 | 798 | zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data."); |
718e3744 | 799 | |
800 | return rtm->rtm_flags; | |
801 | } | |
802 | ||
ec1a4283 | 803 | void |
718e3744 | 804 | rtm_read (struct rt_msghdr *rtm) |
805 | { | |
806 | int flags; | |
807 | u_char zebra_flags; | |
808 | union sockunion dest, mask, gate; | |
6fe70d1b | 809 | char ifname[INTERFACE_NAMSIZ + 1]; |
810 | short ifnlen = 0; | |
718e3744 | 811 | |
812 | zebra_flags = 0; | |
813 | ||
718e3744 | 814 | /* Read destination and netmask and gateway from rtm message |
815 | structure. */ | |
6fe70d1b | 816 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen); |
6da59801 DO |
817 | if (!(flags & RTF_DONE)) |
818 | return; | |
dc95824a DO |
819 | if (IS_ZEBRA_DEBUG_KERNEL) |
820 | zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type, | |
2d844524 | 821 | lookup (rtm_type_str, rtm->rtm_type)); |
718e3744 | 822 | |
823 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ | |
824 | if (flags & RTF_CLONED) | |
825 | return; | |
826 | #endif | |
827 | #ifdef RTF_WASCLONED /*freebsd*/ | |
828 | if (flags & RTF_WASCLONED) | |
829 | return; | |
830 | #endif | |
831 | ||
832 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) | |
833 | return; | |
834 | ||
835 | /* This is connected route. */ | |
836 | if (! (flags & RTF_GATEWAY)) | |
837 | return; | |
838 | ||
839 | if (flags & RTF_PROTO1) | |
840 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); | |
841 | ||
842 | /* This is persistent route. */ | |
843 | if (flags & RTF_STATIC) | |
844 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); | |
845 | ||
81dfcaa2 | 846 | /* This is a reject or blackhole route */ |
847 | if (flags & RTF_REJECT) | |
848 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); | |
849 | if (flags & RTF_BLACKHOLE) | |
850 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); | |
851 | ||
718e3744 | 852 | if (dest.sa.sa_family == AF_INET) |
853 | { | |
854 | struct prefix_ipv4 p; | |
855 | ||
856 | p.family = AF_INET; | |
857 | p.prefix = dest.sin.sin_addr; | |
858 | if (flags & RTF_HOST) | |
859 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
860 | else | |
861 | p.prefixlen = ip_masklen (mask.sin.sin_addr); | |
ca16218d | 862 | |
dc95824a DO |
863 | /* Catch self originated messages and match them against our current RIB. |
864 | * At the same time, ignore unconfirmed messages, they should be tracked | |
865 | * by rtm_write() and kernel_rtm_ipv4(). | |
866 | */ | |
96934e6a | 867 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) |
dc95824a DO |
868 | { |
869 | char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN]; | |
870 | int ret; | |
dc95824a DO |
871 | if (! IS_ZEBRA_DEBUG_RIB) |
872 | return; | |
873 | ret = rib_lookup_ipv4_route (&p, &gate); | |
874 | inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN); | |
875 | switch (rtm->rtm_type) | |
876 | { | |
877 | case RTM_ADD: | |
878 | case RTM_GET: | |
879 | case RTM_CHANGE: | |
880 | /* The kernel notifies us about a new route in FIB created by us. | |
881 | Do we have a correspondent entry in our RIB? */ | |
882 | switch (ret) | |
883 | { | |
884 | case ZEBRA_RIB_NOTFOUND: | |
885 | zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB", | |
2d844524 | 886 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
dc95824a DO |
887 | break; |
888 | case ZEBRA_RIB_FOUND_CONNECTED: | |
889 | case ZEBRA_RIB_FOUND_NOGATE: | |
890 | inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN); | |
891 | zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)", | |
2d844524 | 892 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf); |
dc95824a DO |
893 | break; |
894 | case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */ | |
895 | zlog_debug ("%s: %s %s/%d: done Ok", | |
2d844524 | 896 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
dc95824a DO |
897 | rib_lookup_and_dump (&p); |
898 | return; | |
899 | break; | |
900 | } | |
901 | break; | |
902 | case RTM_DELETE: | |
903 | /* The kernel notifies us about a route deleted by us. Do we still | |
904 | have it in the RIB? Do we have anything instead? */ | |
905 | switch (ret) | |
906 | { | |
907 | case ZEBRA_RIB_FOUND_EXACT: | |
908 | zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB", | |
2d844524 | 909 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
dc95824a DO |
910 | rib_lookup_and_dump (&p); |
911 | break; | |
912 | case ZEBRA_RIB_FOUND_CONNECTED: | |
913 | case ZEBRA_RIB_FOUND_NOGATE: | |
914 | zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs", | |
2d844524 | 915 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
dc95824a DO |
916 | rib_lookup_and_dump (&p); |
917 | break; | |
918 | case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */ | |
919 | zlog_debug ("%s: %s %s/%d: done Ok", | |
2d844524 | 920 | __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen); |
dc95824a DO |
921 | rib_lookup_and_dump (&p); |
922 | return; | |
923 | break; | |
924 | } | |
925 | break; | |
926 | default: | |
927 | zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received", | |
2d844524 | 928 | __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type)); |
dc95824a DO |
929 | } |
930 | return; | |
931 | } | |
932 | ||
ca16218d | 933 | /* Change, delete the old prefix, we have no further information |
934 | * to specify the route really | |
935 | */ | |
936 | if (rtm->rtm_type == RTM_CHANGE) | |
7c8ff89e | 937 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, &p, |
6eac79a6 | 938 | NULL, 0, 0, SAFI_UNICAST); |
ca16218d | 939 | |
940 | if (rtm->rtm_type == RTM_GET | |
941 | || rtm->rtm_type == RTM_ADD | |
942 | || rtm->rtm_type == RTM_CHANGE) | |
7c8ff89e | 943 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, |
cddf391b | 944 | &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST); |
718e3744 | 945 | else |
7c8ff89e | 946 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0 zebra_flags, |
6eac79a6 | 947 | &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST); |
718e3744 | 948 | } |
949 | #ifdef HAVE_IPV6 | |
950 | if (dest.sa.sa_family == AF_INET6) | |
951 | { | |
5619f56b DO |
952 | /* One day we might have a debug section here like one in the |
953 | * IPv4 case above. Just ignore own messages at the moment. | |
954 | */ | |
955 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) | |
956 | return; | |
718e3744 | 957 | struct prefix_ipv6 p; |
958 | unsigned int ifindex = 0; | |
959 | ||
960 | p.family = AF_INET6; | |
961 | p.prefix = dest.sin6.sin6_addr; | |
962 | if (flags & RTF_HOST) | |
963 | p.prefixlen = IPV6_MAX_PREFIXLEN; | |
964 | else | |
965 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); | |
966 | ||
967 | #ifdef KAME | |
968 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) | |
969 | { | |
970 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); | |
971 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); | |
972 | } | |
973 | #endif /* KAME */ | |
974 | ||
ca16218d | 975 | /* CHANGE: delete the old prefix, we have no further information |
976 | * to specify the route really | |
977 | */ | |
978 | if (rtm->rtm_type == RTM_CHANGE) | |
7c8ff89e | 979 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, &p, |
6eac79a6 | 980 | NULL, 0, 0, SAFI_UNICAST); |
ca16218d | 981 | |
982 | if (rtm->rtm_type == RTM_GET | |
983 | || rtm->rtm_type == RTM_ADD | |
984 | || rtm->rtm_type == RTM_CHANGE) | |
7c8ff89e | 985 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, |
6eac79a6 | 986 | &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST); |
718e3744 | 987 | else |
7c8ff89e | 988 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, zebra_flags, |
6eac79a6 | 989 | &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST); |
718e3744 | 990 | } |
991 | #endif /* HAVE_IPV6 */ | |
992 | } | |
993 | ||
994 | /* Interface function for the kernel routing table updates. Support | |
6621ca86 | 995 | * for RTM_CHANGE will be needed. |
996 | * Exported only for rt_socket.c | |
997 | */ | |
718e3744 | 998 | int |
999 | rtm_write (int message, | |
1000 | union sockunion *dest, | |
1001 | union sockunion *mask, | |
1002 | union sockunion *gate, | |
1003 | unsigned int index, | |
1004 | int zebra_flags, | |
1005 | int metric) | |
1006 | { | |
1007 | int ret; | |
1008 | caddr_t pnt; | |
1009 | struct interface *ifp; | |
718e3744 | 1010 | |
1011 | /* Sequencial number of routing message. */ | |
1012 | static int msg_seq = 0; | |
1013 | ||
1014 | /* Struct of rt_msghdr and buffer for storing socket's data. */ | |
1015 | struct | |
1016 | { | |
1017 | struct rt_msghdr rtm; | |
1018 | char buf[512]; | |
1019 | } msg; | |
1020 | ||
718e3744 | 1021 | if (routing_sock < 0) |
1022 | return ZEBRA_ERR_EPERM; | |
1023 | ||
1024 | /* Clear and set rt_msghdr values */ | |
1025 | memset (&msg, 0, sizeof (struct rt_msghdr)); | |
1026 | msg.rtm.rtm_version = RTM_VERSION; | |
1027 | msg.rtm.rtm_type = message; | |
1028 | msg.rtm.rtm_seq = msg_seq++; | |
1029 | msg.rtm.rtm_addrs = RTA_DST; | |
1030 | msg.rtm.rtm_addrs |= RTA_GATEWAY; | |
1031 | msg.rtm.rtm_flags = RTF_UP; | |
1032 | msg.rtm.rtm_index = index; | |
1033 | ||
1034 | if (metric != 0) | |
1035 | { | |
1036 | msg.rtm.rtm_rmx.rmx_hopcount = metric; | |
1037 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; | |
1038 | } | |
1039 | ||
1040 | ifp = if_lookup_by_index (index); | |
1041 | ||
1042 | if (gate && message == RTM_ADD) | |
1043 | msg.rtm.rtm_flags |= RTF_GATEWAY; | |
1044 | ||
e6f148e6 DW |
1045 | /* When RTF_CLONING is unavailable on BSD, should we set some |
1046 | * other flag instead? | |
1047 | */ | |
1048 | #ifdef RTF_CLONING | |
718e3744 | 1049 | if (! gate && message == RTM_ADD && ifp && |
1050 | (ifp->flags & IFF_POINTOPOINT) == 0) | |
1051 | msg.rtm.rtm_flags |= RTF_CLONING; | |
e6f148e6 | 1052 | #endif /* RTF_CLONING */ |
718e3744 | 1053 | |
1054 | /* If no protocol specific gateway is specified, use link | |
1055 | address for gateway. */ | |
1056 | if (! gate) | |
1057 | { | |
1058 | if (!ifp) | |
1059 | { | |
dc95824a DO |
1060 | char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255"; |
1061 | if (dest) | |
1062 | inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN); | |
1063 | if (mask) | |
1064 | inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN); | |
1065 | zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d", | |
1066 | __func__, dest_buf, mask_buf, index); | |
718e3744 | 1067 | return -1; |
1068 | } | |
1069 | gate = (union sockunion *) & ifp->sdl; | |
1070 | } | |
1071 | ||
1072 | if (mask) | |
1073 | msg.rtm.rtm_addrs |= RTA_NETMASK; | |
1074 | else if (message == RTM_ADD) | |
1075 | msg.rtm.rtm_flags |= RTF_HOST; | |
1076 | ||
1077 | /* Tagging route with flags */ | |
1078 | msg.rtm.rtm_flags |= (RTF_PROTO1); | |
1079 | ||
1080 | /* Additional flags. */ | |
1081 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) | |
1082 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; | |
81dfcaa2 | 1083 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
1084 | msg.rtm.rtm_flags |= RTF_REJECT; | |
1085 | ||
718e3744 | 1086 | |
6f0e3f6e | 1087 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 1088 | #define SOCKADDRSET(X,R) \ |
1089 | if (msg.rtm.rtm_addrs & (R)) \ | |
1090 | { \ | |
1091 | int len = ROUNDUP ((X)->sa.sa_len); \ | |
1092 | memcpy (pnt, (caddr_t)(X), len); \ | |
1093 | pnt += len; \ | |
1094 | } | |
1095 | #else | |
1096 | #define SOCKADDRSET(X,R) \ | |
1097 | if (msg.rtm.rtm_addrs & (R)) \ | |
1098 | { \ | |
6fe70d1b | 1099 | int len = SAROUNDUP (X); \ |
718e3744 | 1100 | memcpy (pnt, (caddr_t)(X), len); \ |
1101 | pnt += len; \ | |
1102 | } | |
6f0e3f6e | 1103 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 1104 | |
1105 | pnt = (caddr_t) msg.buf; | |
1106 | ||
1107 | /* Write each socket data into rtm message buffer */ | |
1108 | SOCKADDRSET (dest, RTA_DST); | |
1109 | SOCKADDRSET (gate, RTA_GATEWAY); | |
1110 | SOCKADDRSET (mask, RTA_NETMASK); | |
1111 | ||
1112 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; | |
1113 | ||
1114 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); | |
1115 | ||
1116 | if (ret != msg.rtm.rtm_msglen) | |
1117 | { | |
1118 | if (errno == EEXIST) | |
1119 | return ZEBRA_ERR_RTEXIST; | |
1120 | if (errno == ENETUNREACH) | |
1121 | return ZEBRA_ERR_RTUNREACH; | |
dc95824a DO |
1122 | if (errno == ESRCH) |
1123 | return ZEBRA_ERR_RTNOEXIST; | |
718e3744 | 1124 | |
dc95824a DO |
1125 | zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno); |
1126 | return ZEBRA_ERR_KERNEL; | |
718e3744 | 1127 | } |
dc95824a | 1128 | return ZEBRA_ERR_NOERROR; |
718e3744 | 1129 | } |
1130 | ||
6b0655a2 | 1131 | |
718e3744 | 1132 | #include "thread.h" |
1133 | #include "zebra/zserv.h" | |
1134 | ||
718e3744 | 1135 | /* For debug purpose. */ |
b6178002 | 1136 | static void |
718e3744 | 1137 | rtmsg_debug (struct rt_msghdr *rtm) |
1138 | { | |
2d844524 | 1139 | zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type)); |
718e3744 | 1140 | rtm_flag_dump (rtm->rtm_flags); |
b6178002 | 1141 | zlog_debug ("Kernel: message seq %d", rtm->rtm_seq); |
6fe70d1b | 1142 | zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs); |
718e3744 | 1143 | } |
1144 | ||
1145 | /* This is pretty gross, better suggestions welcome -- mhandler */ | |
1146 | #ifndef RTAX_MAX | |
1147 | #ifdef RTA_NUMBITS | |
1148 | #define RTAX_MAX RTA_NUMBITS | |
1149 | #else | |
1150 | #define RTAX_MAX 8 | |
1151 | #endif /* RTA_NUMBITS */ | |
1152 | #endif /* RTAX_MAX */ | |
1153 | ||
1154 | /* Kernel routing table and interface updates via routing socket. */ | |
6621ca86 | 1155 | static int |
718e3744 | 1156 | kernel_read (struct thread *thread) |
1157 | { | |
1158 | int sock; | |
1159 | int nbytes; | |
1160 | struct rt_msghdr *rtm; | |
1161 | ||
dbee01fe | 1162 | /* |
1163 | * This must be big enough for any message the kernel might send. | |
b27900b7 | 1164 | * Rather than determining how many sockaddrs of what size might be |
1165 | * in each particular message, just use RTAX_MAX of sockaddr_storage | |
1166 | * for each. Note that the sockaddrs must be after each message | |
1167 | * definition, or rather after whichever happens to be the largest, | |
1168 | * since the buffer needs to be big enough for a message and the | |
1169 | * sockaddrs together. | |
dbee01fe | 1170 | */ |
718e3744 | 1171 | union |
1172 | { | |
1173 | /* Routing information. */ | |
1174 | struct | |
1175 | { | |
1176 | struct rt_msghdr rtm; | |
b27900b7 | 1177 | struct sockaddr_storage addr[RTAX_MAX]; |
718e3744 | 1178 | } r; |
1179 | ||
1180 | /* Interface information. */ | |
1181 | struct | |
1182 | { | |
1183 | struct if_msghdr ifm; | |
b27900b7 | 1184 | struct sockaddr_storage addr[RTAX_MAX]; |
718e3744 | 1185 | } im; |
1186 | ||
1187 | /* Interface address information. */ | |
1188 | struct | |
1189 | { | |
1190 | struct ifa_msghdr ifa; | |
b27900b7 | 1191 | struct sockaddr_storage addr[RTAX_MAX]; |
718e3744 | 1192 | } ia; |
1193 | ||
1194 | #ifdef RTM_IFANNOUNCE | |
1195 | /* Interface arrival/departure */ | |
1196 | struct | |
1197 | { | |
1198 | struct if_announcemsghdr ifan; | |
b27900b7 | 1199 | struct sockaddr_storage addr[RTAX_MAX]; |
718e3744 | 1200 | } ian; |
1201 | #endif /* RTM_IFANNOUNCE */ | |
1202 | ||
1203 | } buf; | |
1204 | ||
1205 | /* Fetch routing socket. */ | |
1206 | sock = THREAD_FD (thread); | |
1207 | ||
1208 | nbytes= read (sock, &buf, sizeof buf); | |
1209 | ||
1210 | if (nbytes <= 0) | |
1211 | { | |
1212 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) | |
6099b3b5 | 1213 | zlog_warn ("routing socket error: %s", safe_strerror (errno)); |
718e3744 | 1214 | return 0; |
1215 | } | |
1216 | ||
9bcdb638 | 1217 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
718e3744 | 1218 | |
726f9b2b | 1219 | if (IS_ZEBRA_DEBUG_KERNEL) |
1220 | rtmsg_debug (&buf.r.rtm); | |
718e3744 | 1221 | |
1222 | rtm = &buf.r.rtm; | |
1223 | ||
b27900b7 | 1224 | /* |
1225 | * Ensure that we didn't drop any data, so that processing routines | |
1226 | * can assume they have the whole message. | |
1227 | */ | |
da26e3b6 | 1228 | if (rtm->rtm_msglen != nbytes) |
1229 | { | |
1230 | zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n", | |
1231 | rtm->rtm_msglen, nbytes, rtm->rtm_type); | |
1232 | return -1; | |
1233 | } | |
1234 | ||
718e3744 | 1235 | switch (rtm->rtm_type) |
1236 | { | |
1237 | case RTM_ADD: | |
1238 | case RTM_DELETE: | |
ca16218d | 1239 | case RTM_CHANGE: |
718e3744 | 1240 | rtm_read (rtm); |
1241 | break; | |
1242 | case RTM_IFINFO: | |
1243 | ifm_read (&buf.im.ifm); | |
1244 | break; | |
1245 | case RTM_NEWADDR: | |
1246 | case RTM_DELADDR: | |
1247 | ifam_read (&buf.ia.ifa); | |
1248 | break; | |
1249 | #ifdef RTM_IFANNOUNCE | |
1250 | case RTM_IFANNOUNCE: | |
1251 | ifan_read (&buf.ian.ifan); | |
1252 | break; | |
1253 | #endif /* RTM_IFANNOUNCE */ | |
1254 | default: | |
726f9b2b | 1255 | if (IS_ZEBRA_DEBUG_KERNEL) |
b6178002 | 1256 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); |
718e3744 | 1257 | break; |
1258 | } | |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | /* Make routing socket. */ | |
6621ca86 | 1263 | static void |
1264 | routing_socket (void) | |
718e3744 | 1265 | { |
edd7c245 | 1266 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
1267 | zlog_err ("routing_socket: Can't raise privileges"); | |
1268 | ||
718e3744 | 1269 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
1270 | ||
1271 | if (routing_sock < 0) | |
1272 | { | |
edd7c245 | 1273 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
1274 | zlog_err ("routing_socket: Can't lower privileges"); | |
718e3744 | 1275 | zlog_warn ("Can't init kernel routing socket"); |
1276 | return; | |
1277 | } | |
1278 | ||
865b852c | 1279 | /* XXX: Socket should be NONBLOCK, however as we currently |
1280 | * discard failed writes, this will lead to inconsistencies. | |
1281 | * For now, socket must be blocking. | |
1282 | */ | |
1283 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) | |
1284 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ | |
1285 | ||
edd7c245 | 1286 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
1287 | zlog_err ("routing_socket: Can't lower privileges"); | |
718e3744 | 1288 | |
1289 | /* kernel_read needs rewrite. */ | |
9bcdb638 | 1290 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
718e3744 | 1291 | } |
1292 | ||
1293 | /* Exported interface function. This function simply calls | |
1294 | routing_socket (). */ | |
1295 | void | |
6621ca86 | 1296 | kernel_init (void) |
718e3744 | 1297 | { |
1298 | routing_socket (); | |
1299 | } |