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