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