]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Kernel communication using routing socket. |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
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 | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
ddfeb486 DL |
22 | |
23 | #ifndef HAVE_NETLINK | |
24 | ||
8ccc7e80 | 25 | #include <net/if_types.h> |
fe6c7157 | 26 | #ifdef __OpenBSD__ |
d3e2c74a RW |
27 | #include <netmpls/mpls.h> |
28 | #endif | |
718e3744 | 29 | |
30 | #include "if.h" | |
31 | #include "prefix.h" | |
32 | #include "sockunion.h" | |
33 | #include "connected.h" | |
34 | #include "memory.h" | |
35 | #include "ioctl.h" | |
36 | #include "log.h" | |
718e3744 | 37 | #include "table.h" |
38 | #include "rib.h" | |
edd7c245 | 39 | #include "privs.h" |
78104b9b | 40 | #include "vrf.h" |
174482ef | 41 | #include "lib_errors.h" |
718e3744 | 42 | |
a37bd5e0 | 43 | #include "zebra/rt.h" |
718e3744 | 44 | #include "zebra/interface.h" |
3801e764 | 45 | #include "zebra/zebra_router.h" |
718e3744 | 46 | #include "zebra/debug.h" |
ec1a4283 | 47 | #include "zebra/kernel_socket.h" |
8f7d9fc0 | 48 | #include "zebra/rib.h" |
98c15db6 | 49 | #include "zebra/zebra_errors.h" |
01ce7cba | 50 | #include "zebra/zebra_ptm.h" |
718e3744 | 51 | |
edd7c245 | 52 | extern struct zebra_privs_t zserv_privs; |
53 | ||
4bfbea8c | 54 | /* |
cfa0ed09 GT |
55 | * Historically, the BSD routing socket has aligned data following a |
56 | * struct sockaddr to sizeof(long), which was 4 bytes on some | |
57 | * platforms, and 8 bytes on others. NetBSD 6 changed the routing | |
58 | * socket to align to sizeof(uint64_t), which is 8 bytes. OS X | |
59 | * appears to align to sizeof(int), which is 4 bytes. | |
4bfbea8c | 60 | * |
cfa0ed09 GT |
61 | * Alignment of zero-sized sockaddrs is nonsensical, but historically |
62 | * BSD defines RT_ROUNDUP(0) to be the alignment interval (rather than | |
63 | * 0). We follow this practice without questioning it, but it is a | |
b72aae2e | 64 | * bug if frr calls ROUNDUP with 0. |
4bfbea8c | 65 | */ |
cb800e35 RZ |
66 | #ifdef __APPLE__ |
67 | #define ROUNDUP_TYPE int | |
68 | #else | |
69 | #define ROUNDUP_TYPE long | |
70 | #endif | |
cfa0ed09 GT |
71 | |
72 | /* | |
73 | * Because of these varying conventions, the only sane approach is for | |
74 | * the <net/route.h> header to define some flavor of ROUNDUP macro. | |
75 | */ | |
e7f0e649 | 76 | |
cb800e35 | 77 | /* OS X (Xcode as of 2014-12) is known not to define RT_ROUNDUP */ |
cfa0ed09 GT |
78 | #if defined(RT_ROUNDUP) |
79 | #define ROUNDUP(a) RT_ROUNDUP(a) | |
80 | #endif /* defined(RT_ROUNDUP) */ | |
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 | /* | |
adf580f7 DL |
89 | * If you're porting to a platform that changed RT_ROUNDUP but doesn't |
90 | * have it in its headers, this will break rather obviously and you'll | |
91 | * have to fix it here. | |
cfa0ed09 | 92 | */ |
d62a17ae | 93 | #define ROUNDUP(a) \ |
94 | ((a) > 0 ? (1 + (((a)-1) | (sizeof(ROUNDUP_TYPE) - 1))) \ | |
95 | : sizeof(ROUNDUP_TYPE)) | |
cfa0ed09 GT |
96 | |
97 | #endif /* defined(ROUNDUP) */ | |
98 | ||
cb800e35 RZ |
99 | |
100 | #if defined(SA_SIZE) | |
101 | /* SAROUNDUP is the only thing we need, and SA_SIZE provides that */ | |
102 | #define SAROUNDUP(a) SA_SIZE(a) | |
103 | #else /* !SA_SIZE */ | |
4bfbea8c | 104 | /* |
105 | * Given a pointer (sockaddr or void *), return the number of bytes | |
106 | * taken up by the sockaddr and any padding needed for alignment. | |
107 | */ | |
6f0e3f6e | 108 | #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN) |
4bfbea8c | 109 | #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
56c1f7d8 | 110 | #else |
4bfbea8c | 111 | /* |
112 | * One would hope all fixed-size structure definitions are aligned, | |
113 | * but round them up nonetheless. | |
114 | */ | |
d62a17ae | 115 | #define SAROUNDUP(X) \ |
116 | (((struct sockaddr *)(X))->sa_family == AF_INET \ | |
117 | ? ROUNDUP(sizeof(struct sockaddr_in)) \ | |
118 | : (((struct sockaddr *)(X))->sa_family == AF_INET6 \ | |
119 | ? ROUNDUP(sizeof(struct sockaddr_in6)) \ | |
120 | : (((struct sockaddr *)(X))->sa_family == AF_LINK \ | |
121 | ? ROUNDUP(sizeof(struct sockaddr_dl)) \ | |
122 | : sizeof(struct sockaddr)))) | |
6f0e3f6e | 123 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ |
718e3744 | 124 | |
e7f0e649 DL |
125 | #endif /* !SA_SIZE */ |
126 | ||
718e3744 | 127 | /* Routing socket message types. */ |
d62a17ae | 128 | const struct message rtm_type_str[] = {{RTM_ADD, "RTM_ADD"}, |
129 | {RTM_DELETE, "RTM_DELETE"}, | |
130 | {RTM_CHANGE, "RTM_CHANGE"}, | |
131 | {RTM_GET, "RTM_GET"}, | |
132 | {RTM_LOSING, "RTM_LOSING"}, | |
133 | {RTM_REDIRECT, "RTM_REDIRECT"}, | |
134 | {RTM_MISS, "RTM_MISS"}, | |
7f7651eb | 135 | #ifdef RTM_LOCK |
d62a17ae | 136 | {RTM_LOCK, "RTM_LOCK"}, |
7f7651eb | 137 | #endif /* RTM_LOCK */ |
9458b819 | 138 | #ifdef OLDADD |
d62a17ae | 139 | {RTM_OLDADD, "RTM_OLDADD"}, |
9458b819 GT |
140 | #endif /* RTM_OLDADD */ |
141 | #ifdef RTM_OLDDEL | |
d62a17ae | 142 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
9458b819 | 143 | #endif /* RTM_OLDDEL */ |
5f11807b | 144 | #ifdef RTM_RESOLVE |
d62a17ae | 145 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
5f11807b | 146 | #endif /* RTM_RESOLVE */ |
d62a17ae | 147 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
148 | {RTM_DELADDR, "RTM_DELADDR"}, | |
149 | {RTM_IFINFO, "RTM_IFINFO"}, | |
718e3744 | 150 | #ifdef RTM_OIFINFO |
d62a17ae | 151 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
718e3744 | 152 | #endif /* RTM_OIFINFO */ |
153 | #ifdef RTM_NEWMADDR | |
d62a17ae | 154 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
718e3744 | 155 | #endif /* RTM_NEWMADDR */ |
156 | #ifdef RTM_DELMADDR | |
d62a17ae | 157 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
718e3744 | 158 | #endif /* RTM_DELMADDR */ |
159 | #ifdef RTM_IFANNOUNCE | |
d62a17ae | 160 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
718e3744 | 161 | #endif /* RTM_IFANNOUNCE */ |
d62a17ae | 162 | {0}}; |
163 | ||
164 | static const struct message rtm_flag_str[] = {{RTF_UP, "UP"}, | |
165 | {RTF_GATEWAY, "GATEWAY"}, | |
166 | {RTF_HOST, "HOST"}, | |
167 | {RTF_REJECT, "REJECT"}, | |
168 | {RTF_DYNAMIC, "DYNAMIC"}, | |
169 | {RTF_MODIFIED, "MODIFIED"}, | |
170 | {RTF_DONE, "DONE"}, | |
718e3744 | 171 | #ifdef RTF_MASK |
d62a17ae | 172 | {RTF_MASK, "MASK"}, |
718e3744 | 173 | #endif /* RTF_MASK */ |
e6f148e6 | 174 | #ifdef RTF_CLONING |
d62a17ae | 175 | {RTF_CLONING, "CLONING"}, |
e6f148e6 | 176 | #endif /* RTF_CLONING */ |
23b1f400 | 177 | #ifdef RTF_XRESOLVE |
d62a17ae | 178 | {RTF_XRESOLVE, "XRESOLVE"}, |
23b1f400 | 179 | #endif /* RTF_XRESOLVE */ |
c54632ec | 180 | #ifdef RTF_LLINFO |
d62a17ae | 181 | {RTF_LLINFO, "LLINFO"}, |
c54632ec | 182 | #endif /* RTF_LLINFO */ |
d62a17ae | 183 | {RTF_STATIC, "STATIC"}, |
184 | {RTF_BLACKHOLE, "BLACKHOLE"}, | |
6fe70d1b | 185 | #ifdef RTF_PRIVATE |
d62a17ae | 186 | {RTF_PRIVATE, "PRIVATE"}, |
6fe70d1b | 187 | #endif /* RTF_PRIVATE */ |
d62a17ae | 188 | {RTF_PROTO1, "PROTO1"}, |
189 | {RTF_PROTO2, "PROTO2"}, | |
718e3744 | 190 | #ifdef RTF_PRCLONING |
d62a17ae | 191 | {RTF_PRCLONING, "PRCLONING"}, |
718e3744 | 192 | #endif /* RTF_PRCLONING */ |
193 | #ifdef RTF_WASCLONED | |
d62a17ae | 194 | {RTF_WASCLONED, "WASCLONED"}, |
718e3744 | 195 | #endif /* RTF_WASCLONED */ |
196 | #ifdef RTF_PROTO3 | |
d62a17ae | 197 | {RTF_PROTO3, "PROTO3"}, |
718e3744 | 198 | #endif /* RTF_PROTO3 */ |
199 | #ifdef RTF_PINNED | |
d62a17ae | 200 | {RTF_PINNED, "PINNED"}, |
718e3744 | 201 | #endif /* RTF_PINNED */ |
202 | #ifdef RTF_LOCAL | |
d62a17ae | 203 | {RTF_LOCAL, "LOCAL"}, |
718e3744 | 204 | #endif /* RTF_LOCAL */ |
205 | #ifdef RTF_BROADCAST | |
d62a17ae | 206 | {RTF_BROADCAST, "BROADCAST"}, |
718e3744 | 207 | #endif /* RTF_BROADCAST */ |
208 | #ifdef RTF_MULTICAST | |
d62a17ae | 209 | {RTF_MULTICAST, "MULTICAST"}, |
718e3744 | 210 | #endif /* RTF_MULTICAST */ |
6fe70d1b | 211 | #ifdef RTF_MULTIRT |
d62a17ae | 212 | {RTF_MULTIRT, "MULTIRT"}, |
6fe70d1b | 213 | #endif /* RTF_MULTIRT */ |
214 | #ifdef RTF_SETSRC | |
d62a17ae | 215 | {RTF_SETSRC, "SETSRC"}, |
6fe70d1b | 216 | #endif /* RTF_SETSRC */ |
d62a17ae | 217 | {0}}; |
718e3744 | 218 | |
219 | /* Kernel routing update socket. */ | |
220 | int routing_sock = -1; | |
221 | ||
80776aec MS |
222 | /* Kernel dataplane routing update socket, used in the dataplane pthread |
223 | * context. | |
224 | */ | |
225 | int dplane_routing_sock = -1; | |
226 | ||
718e3744 | 227 | /* Yes I'm checking ugly routing socket behavior. */ |
228 | /* #define DEBUG */ | |
229 | ||
19fb538d | 230 | size_t _rta_get(caddr_t sap, void *destp, size_t destlen, bool checkaf); |
86c57af5 | 231 | size_t rta_get(caddr_t sap, void *dest, size_t destlen); |
19fb538d | 232 | size_t rta_getattr(caddr_t sap, void *destp, size_t destlen); |
86c57af5 | 233 | size_t rta_getsdlname(caddr_t sap, void *dest, short *destlen); |
5b62211d | 234 | const char *rtatostr(unsigned int flags, char *buf, size_t buflen); |
86c57af5 | 235 | |
718e3744 | 236 | /* Supported address family check. */ |
d62a17ae | 237 | static inline int af_check(int family) |
718e3744 | 238 | { |
d62a17ae | 239 | if (family == AF_INET) |
240 | return 1; | |
241 | if (family == AF_INET6) | |
242 | return 1; | |
243 | return 0; | |
718e3744 | 244 | } |
6b0655a2 | 245 | |
19fb538d | 246 | size_t _rta_get(caddr_t sap, void *destp, size_t destlen, bool checkaf) |
86c57af5 RZ |
247 | { |
248 | struct sockaddr *sa = (struct sockaddr *)sap; | |
75e710df | 249 | struct sockaddr_dl *sdl; |
86c57af5 RZ |
250 | uint8_t *dest = destp; |
251 | size_t tlen, copylen; | |
252 | ||
253 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
254 | copylen = sa->sa_len; | |
255 | tlen = (copylen == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(copylen); | |
256 | #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
257 | copylen = tlen = SAROUNDUP(sap); | |
258 | #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
259 | ||
19fb538d RZ |
260 | if (copylen > 0 && dest != NULL) { |
261 | if (checkaf && af_check(sa->sa_family) == 0) | |
262 | return tlen; | |
75e710df RZ |
263 | /* |
264 | * Handle sockaddr_dl corner case: | |
265 | * RTA_NETMASK might be AF_LINK, but it doesn't anything | |
266 | * relevant (e.g. zeroed out fields). Check for this | |
267 | * case and avoid warning log message. | |
268 | */ | |
269 | if (sa->sa_family == AF_LINK) { | |
270 | sdl = (struct sockaddr_dl *)sa; | |
271 | if (sdl->sdl_index == 0 || sdl->sdl_nlen == 0) | |
e0f741f4 | 272 | copylen = destlen; |
75e710df | 273 | } |
19fb538d | 274 | |
86c57af5 | 275 | if (copylen > destlen) { |
fa0069c6 DS |
276 | zlog_warn( |
277 | "%s: destination buffer too small (%zu vs %zu)", | |
278 | __func__, copylen, destlen); | |
86c57af5 RZ |
279 | memcpy(dest, sap, destlen); |
280 | } else | |
281 | memcpy(dest, sap, copylen); | |
282 | } | |
283 | ||
284 | return tlen; | |
285 | } | |
286 | ||
19fb538d RZ |
287 | size_t rta_get(caddr_t sap, void *destp, size_t destlen) |
288 | { | |
289 | return _rta_get(sap, destp, destlen, true); | |
290 | } | |
291 | ||
292 | size_t rta_getattr(caddr_t sap, void *destp, size_t destlen) | |
293 | { | |
294 | return _rta_get(sap, destp, destlen, false); | |
295 | } | |
296 | ||
86c57af5 RZ |
297 | size_t rta_getsdlname(caddr_t sap, void *destp, short *destlen) |
298 | { | |
299 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)sap; | |
86c57af5 RZ |
300 | uint8_t *dest = destp; |
301 | size_t tlen, copylen; | |
302 | ||
303 | copylen = sdl->sdl_nlen; | |
304 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
8054cf97 QY |
305 | struct sockaddr *sa = (struct sockaddr *)sap; |
306 | ||
86c57af5 RZ |
307 | tlen = (sa->sa_len == 0) ? sizeof(ROUNDUP_TYPE) : ROUNDUP(sa->sa_len); |
308 | #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
309 | tlen = SAROUNDUP(sap); | |
310 | #endif /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
311 | ||
312 | if (copylen > 0 && dest != NULL && sdl->sdl_family == AF_LINK) { | |
313 | if (copylen > IFNAMSIZ) { | |
fa0069c6 DS |
314 | zlog_warn( |
315 | "%s: destination buffer too small (%zu vs %d)", | |
316 | __func__, copylen, IFNAMSIZ); | |
86c57af5 RZ |
317 | memcpy(dest, sdl->sdl_data, IFNAMSIZ); |
318 | dest[IFNAMSIZ] = 0; | |
319 | *destlen = IFNAMSIZ; | |
320 | } else { | |
321 | memcpy(dest, sdl->sdl_data, copylen); | |
322 | dest[copylen] = 0; | |
323 | *destlen = copylen; | |
324 | } | |
325 | } else | |
326 | *destlen = 0; | |
327 | ||
328 | return tlen; | |
329 | } | |
330 | ||
5b62211d RZ |
331 | const char *rtatostr(unsigned int flags, char *buf, size_t buflen) |
332 | { | |
333 | const char *flagstr, *bufstart; | |
334 | int bit, wlen; | |
335 | char ustr[32]; | |
336 | ||
337 | /* Hold the pointer to the buffer beginning. */ | |
338 | bufstart = buf; | |
339 | ||
340 | for (bit = 1; bit; bit <<= 1) { | |
341 | if ((flags & bit) == 0) | |
342 | continue; | |
343 | ||
344 | switch (bit) { | |
345 | case RTA_DST: | |
346 | flagstr = "DST"; | |
347 | break; | |
348 | case RTA_GATEWAY: | |
349 | flagstr = "GATEWAY"; | |
350 | break; | |
351 | case RTA_NETMASK: | |
352 | flagstr = "NETMASK"; | |
353 | break; | |
354 | #ifdef RTA_GENMASK | |
355 | case RTA_GENMASK: | |
356 | flagstr = "GENMASK"; | |
357 | break; | |
358 | #endif /* RTA_GENMASK */ | |
359 | case RTA_IFP: | |
360 | flagstr = "IFP"; | |
361 | break; | |
362 | case RTA_IFA: | |
363 | flagstr = "IFA"; | |
364 | break; | |
365 | #ifdef RTA_AUTHOR | |
366 | case RTA_AUTHOR: | |
367 | flagstr = "AUTHOR"; | |
368 | break; | |
369 | #endif /* RTA_AUTHOR */ | |
370 | case RTA_BRD: | |
371 | flagstr = "BRD"; | |
372 | break; | |
373 | #ifdef RTA_SRC | |
374 | case RTA_SRC: | |
375 | flagstr = "SRC"; | |
376 | break; | |
377 | #endif /* RTA_SRC */ | |
378 | #ifdef RTA_SRCMASK | |
379 | case RTA_SRCMASK: | |
380 | flagstr = "SRCMASK"; | |
381 | break; | |
382 | #endif /* RTA_SRCMASK */ | |
383 | #ifdef RTA_LABEL | |
384 | case RTA_LABEL: | |
385 | flagstr = "LABEL"; | |
386 | break; | |
387 | #endif /* RTA_LABEL */ | |
388 | ||
389 | default: | |
390 | snprintf(ustr, sizeof(ustr), "0x%x", bit); | |
391 | flagstr = ustr; | |
392 | break; | |
393 | } | |
394 | ||
395 | wlen = snprintf(buf, buflen, "%s,", flagstr); | |
396 | buf += wlen; | |
397 | buflen -= wlen; | |
398 | } | |
399 | ||
400 | /* Check for empty buffer. */ | |
401 | if (bufstart != buf) | |
402 | buf--; | |
403 | ||
404 | /* Remove the last comma. */ | |
405 | *buf = 0; | |
406 | ||
407 | return bufstart; | |
408 | } | |
409 | ||
718e3744 | 410 | /* Dump routing table flag for debug purpose. */ |
d62a17ae | 411 | static void rtm_flag_dump(int flag) |
718e3744 | 412 | { |
d62a17ae | 413 | const struct message *mes; |
414 | static char buf[BUFSIZ]; | |
415 | ||
416 | buf[0] = '\0'; | |
417 | for (mes = rtm_flag_str; mes->key != 0; mes++) { | |
418 | if (mes->key & flag) { | |
419 | strlcat(buf, mes->str, BUFSIZ); | |
420 | strlcat(buf, " ", BUFSIZ); | |
421 | } | |
718e3744 | 422 | } |
d62a17ae | 423 | zlog_debug("Kernel: %s", buf); |
718e3744 | 424 | } |
425 | ||
426 | #ifdef RTM_IFANNOUNCE | |
427 | /* Interface adding function */ | |
d62a17ae | 428 | static int ifan_read(struct if_announcemsghdr *ifan) |
718e3744 | 429 | { |
d62a17ae | 430 | struct interface *ifp; |
431 | ||
432 | ifp = if_lookup_by_index(ifan->ifan_index, VRF_DEFAULT); | |
433 | ||
434 | if (ifp) | |
435 | assert((ifp->ifindex == ifan->ifan_index) | |
436 | || (ifp->ifindex == IFINDEX_INTERNAL)); | |
437 | ||
9d303b37 DL |
438 | if ((ifp == NULL) || ((ifp->ifindex == IFINDEX_INTERNAL) |
439 | && (ifan->ifan_what == IFAN_ARRIVAL))) { | |
d62a17ae | 440 | if (IS_ZEBRA_DEBUG_KERNEL) |
441 | zlog_debug( | |
442 | "%s: creating interface for ifindex %d, name %s", | |
443 | __func__, ifan->ifan_index, ifan->ifan_name); | |
444 | ||
445 | /* Create Interface */ | |
f60a1188 IR |
446 | ifp = if_get_by_name(ifan->ifan_name, VRF_DEFAULT, |
447 | VRF_DEFAULT_NAME); | |
ff880b78 | 448 | if_set_index(ifp, ifan->ifan_index); |
d62a17ae | 449 | |
450 | if_get_metric(ifp); | |
451 | if_add_update(ifp); | |
452 | } else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) | |
d0438da6 | 453 | if_delete_update(&ifp); |
d62a17ae | 454 | |
d0438da6 DS |
455 | if (ifp) { |
456 | if_get_flags(ifp); | |
457 | if_get_mtu(ifp); | |
458 | if_get_metric(ifp); | |
459 | } | |
d62a17ae | 460 | if (IS_ZEBRA_DEBUG_KERNEL) |
461 | zlog_debug("%s: interface %s index %d", __func__, | |
462 | ifan->ifan_name, ifan->ifan_index); | |
463 | ||
464 | return 0; | |
718e3744 | 465 | } |
466 | #endif /* RTM_IFANNOUNCE */ | |
467 | ||
9234b382 | 468 | #ifdef HAVE_BSD_IFI_LINK_STATE |
c543a173 | 469 | /* BSD link detect translation */ |
d62a17ae | 470 | static void bsd_linkdetect_translate(struct if_msghdr *ifm) |
c543a173 | 471 | { |
d62a17ae | 472 | if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) |
473 | || (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN)) | |
474 | SET_FLAG(ifm->ifm_flags, IFF_RUNNING); | |
475 | else | |
476 | UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING); | |
c543a173 | 477 | } |
9234b382 | 478 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
c543a173 | 479 | |
d62a17ae | 480 | static enum zebra_link_type sdl_to_zebra_link_type(unsigned int sdlt) |
8ccc7e80 | 481 | { |
d62a17ae | 482 | switch (sdlt) { |
483 | case IFT_ETHER: | |
484 | return ZEBRA_LLT_ETHER; | |
485 | case IFT_X25: | |
486 | return ZEBRA_LLT_X25; | |
487 | case IFT_FDDI: | |
488 | return ZEBRA_LLT_FDDI; | |
489 | case IFT_PPP: | |
490 | return ZEBRA_LLT_PPP; | |
491 | case IFT_LOOP: | |
492 | return ZEBRA_LLT_LOOPBACK; | |
493 | case IFT_SLIP: | |
494 | return ZEBRA_LLT_SLIP; | |
495 | case IFT_ARCNET: | |
496 | return ZEBRA_LLT_ARCNET; | |
497 | case IFT_ATM: | |
498 | return ZEBRA_LLT_ATM; | |
499 | case IFT_LOCALTALK: | |
500 | return ZEBRA_LLT_LOCALTLK; | |
501 | case IFT_HIPPI: | |
502 | return ZEBRA_LLT_HIPPI; | |
8ccc7e80 | 503 | #ifdef IFT_IEEE1394 |
d62a17ae | 504 | case IFT_IEEE1394: |
505 | return ZEBRA_LLT_IEEE1394; | |
8ccc7e80 TT |
506 | #endif |
507 | ||
d62a17ae | 508 | default: |
509 | return ZEBRA_LLT_UNKNOWN; | |
510 | } | |
8ccc7e80 TT |
511 | } |
512 | ||
da26e3b6 | 513 | /* |
514 | * Handle struct if_msghdr obtained from reading routing socket or | |
515 | * sysctl (from interface_list). There may or may not be sockaddrs | |
516 | * present after the header. | |
517 | */ | |
d62a17ae | 518 | int ifm_read(struct if_msghdr *ifm) |
718e3744 | 519 | { |
d62a17ae | 520 | struct interface *ifp = NULL; |
ee74220b | 521 | struct sockaddr_dl *sdl = NULL; |
d62a17ae | 522 | char ifname[IFNAMSIZ]; |
523 | short ifnlen = 0; | |
86c57af5 | 524 | int maskbit; |
d62a17ae | 525 | caddr_t cp; |
5b62211d | 526 | char fbuf[64]; |
d62a17ae | 527 | |
528 | /* terminate ifname at head (for strnlen) and tail (for safety) */ | |
529 | ifname[IFNAMSIZ - 1] = '\0'; | |
530 | ||
531 | /* paranoia: sanity check structure */ | |
532 | if (ifm->ifm_msglen < sizeof(struct if_msghdr)) { | |
e914ccbe | 533 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, |
bcfce7ad | 534 | "ifm_read: ifm->ifm_msglen %d too short", |
1c50c1c0 | 535 | ifm->ifm_msglen); |
d62a17ae | 536 | return -1; |
537 | } | |
538 | ||
539 | /* | |
540 | * Check for a sockaddr_dl following the message. First, point to | |
541 | * where a socakddr might be if one follows the message. | |
542 | */ | |
543 | cp = (void *)(ifm + 1); | |
718e3744 | 544 | |
86c57af5 RZ |
545 | /* Look up for RTA_IFP and skip others. */ |
546 | for (maskbit = 1; maskbit; maskbit <<= 1) { | |
547 | if ((maskbit & ifm->ifm_addrs) == 0) | |
548 | continue; | |
549 | if (maskbit != RTA_IFP) { | |
550 | cp += rta_get(cp, NULL, 0); | |
551 | continue; | |
552 | } | |
553 | ||
554 | /* Save the pointer to the structure. */ | |
555 | sdl = (struct sockaddr_dl *)cp; | |
556 | cp += rta_getsdlname(cp, ifname, &ifnlen); | |
557 | } | |
d62a17ae | 558 | |
559 | if (IS_ZEBRA_DEBUG_KERNEL) | |
5b62211d RZ |
560 | zlog_debug("%s: sdl ifname %s addrs {%s}", __func__, |
561 | (ifnlen ? ifname : "(nil)"), | |
562 | rtatostr(ifm->ifm_addrs, fbuf, sizeof(fbuf))); | |
d62a17ae | 563 | |
564 | /* | |
565 | * Look up on ifindex first, because ifindices are the primary handle | |
566 | * for | |
567 | * interfaces across the user/kernel boundary, for most systems. (Some | |
568 | * messages, such as up/down status changes on NetBSD, do not include a | |
569 | * sockaddr_dl). | |
570 | */ | |
571 | if ((ifp = if_lookup_by_index(ifm->ifm_index, VRF_DEFAULT)) != NULL) { | |
572 | /* we have an ifp, verify that the name matches as some systems, | |
573 | * eg Solaris, have a 1:many association of ifindex:ifname | |
574 | * if they dont match, we dont have the correct ifp and should | |
575 | * set it back to NULL to let next check do lookup by name | |
576 | */ | |
577 | if (ifnlen && (strncmp(ifp->name, ifname, IFNAMSIZ) != 0)) { | |
578 | if (IS_ZEBRA_DEBUG_KERNEL) | |
579 | zlog_debug( | |
0437e105 | 580 | "%s: ifp name %s doesn't match sdl name %s", |
d62a17ae | 581 | __func__, ifp->name, ifname); |
582 | ifp = NULL; | |
583 | } | |
da26e3b6 | 584 | } |
5c78b3d0 | 585 | |
d62a17ae | 586 | /* |
587 | * If we dont have an ifp, try looking up by name. Particularly as some | |
588 | * systems (Solaris) have a 1:many mapping of ifindex:ifname - the | |
589 | * ifname | |
590 | * is therefore our unique handle to that interface. | |
591 | * | |
592 | * Interfaces specified in the configuration file for which the ifindex | |
593 | * has not been determined will have ifindex == IFINDEX_INTERNAL, and | |
594 | * such | |
595 | * interfaces are found by this search, and then their ifindex values | |
596 | * can | |
597 | * be filled in. | |
598 | */ | |
599 | if ((ifp == NULL) && ifnlen) | |
a36898e7 | 600 | ifp = if_lookup_by_name(ifname, VRF_DEFAULT); |
d62a17ae | 601 | |
602 | /* | |
603 | * If ifp still does not exist or has an invalid index | |
604 | * (IFINDEX_INTERNAL), | |
605 | * create or fill in an interface. | |
606 | */ | |
607 | if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) { | |
608 | /* | |
609 | * To create or fill in an interface, a sockaddr_dl (via | |
610 | * RTA_IFP) is required. | |
611 | */ | |
612 | if (!ifnlen) { | |
9165c5f5 | 613 | zlog_debug("Interface index %d (new) missing ifname", |
9df414fe | 614 | ifm->ifm_index); |
d62a17ae | 615 | return -1; |
616 | } | |
617 | ||
5c78b3d0 | 618 | #ifndef RTM_IFANNOUNCE |
d62a17ae | 619 | /* Down->Down interface should be ignored here. |
620 | * See further comment below. | |
621 | */ | |
622 | if (!CHECK_FLAG(ifm->ifm_flags, IFF_UP)) | |
623 | return 0; | |
5c78b3d0 | 624 | #endif /* !RTM_IFANNOUNCE */ |
d62a17ae | 625 | |
626 | if (ifp == NULL) { | |
627 | /* Interface that zebra was not previously aware of, so | |
628 | * create. */ | |
f60a1188 IR |
629 | ifp = if_get_by_name(ifname, VRF_DEFAULT, |
630 | VRF_DEFAULT_NAME); | |
d62a17ae | 631 | if (IS_ZEBRA_DEBUG_KERNEL) |
632 | zlog_debug("%s: creating ifp for ifindex %d", | |
633 | __func__, ifm->ifm_index); | |
634 | } | |
635 | ||
636 | if (IS_ZEBRA_DEBUG_KERNEL) | |
637 | zlog_debug( | |
638 | "%s: updated/created ifp, ifname %s, ifindex %d", | |
639 | __func__, ifp->name, ifp->ifindex); | |
640 | /* | |
641 | * Fill in newly created interface structure, or larval | |
642 | * structure with ifindex IFINDEX_INTERNAL. | |
643 | */ | |
ff880b78 | 644 | if_set_index(ifp, ifm->ifm_index); |
d62a17ae | 645 | |
9234b382 | 646 | #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */ |
d62a17ae | 647 | bsd_linkdetect_translate(ifm); |
9234b382 | 648 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
c543a173 | 649 | |
d62a17ae | 650 | if_flags_update(ifp, ifm->ifm_flags); |
718e3744 | 651 | #if defined(__bsdi__) |
d62a17ae | 652 | if_kvm_get_mtu(ifp); |
718e3744 | 653 | #else |
d62a17ae | 654 | if_get_mtu(ifp); |
718e3744 | 655 | #endif /* __bsdi__ */ |
d62a17ae | 656 | if_get_metric(ifp); |
657 | ||
658 | /* | |
659 | * XXX sockaddr_dl contents can be larger than the structure | |
660 | * definition. There are 2 big families here: | |
661 | * - BSD has sdl_len + sdl_data[16] + overruns sdl_data | |
662 | * we MUST use sdl_len here or we'll truncate data. | |
663 | * - Solaris has no sdl_len, but sdl_data[244] | |
664 | * presumably, it's not going to run past that, so sizeof() | |
665 | * is fine here. | |
86c57af5 | 666 | * a nonzero ifnlen from rta_getsdlname() means sdl is valid |
d62a17ae | 667 | */ |
668 | ifp->ll_type = ZEBRA_LLT_UNKNOWN; | |
669 | ifp->hw_addr_len = 0; | |
670 | if (ifnlen) { | |
ca3ccd87 | 671 | #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN |
d62a17ae | 672 | memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl, |
673 | sdl->sdl_len); | |
ca3ccd87 | 674 | #else |
d62a17ae | 675 | memcpy(&((struct zebra_if *)ifp->info)->sdl, sdl, |
676 | sizeof(struct sockaddr_dl)); | |
ca3ccd87 | 677 | #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */ |
8ccc7e80 | 678 | |
d62a17ae | 679 | ifp->ll_type = sdl_to_zebra_link_type(sdl->sdl_type); |
680 | if (sdl->sdl_alen <= sizeof(ifp->hw_addr)) { | |
681 | memcpy(ifp->hw_addr, LLADDR(sdl), | |
682 | sdl->sdl_alen); | |
683 | ifp->hw_addr_len = sdl->sdl_alen; | |
684 | } | |
685 | } | |
686 | ||
687 | if_add_update(ifp); | |
688 | } else | |
689 | /* | |
690 | * Interface structure exists. Adjust stored flags from | |
691 | * notification. If interface has up->down or down->up | |
692 | * transition, call state change routines (to adjust routes, | |
693 | * notify routing daemons, etc.). (Other flag changes are stored | |
694 | * but apparently do not trigger action.) | |
695 | */ | |
696 | { | |
697 | if (ifp->ifindex != ifm->ifm_index) { | |
9df414fe | 698 | zlog_debug( |
3efd0893 | 699 | "%s: index mismatch, ifname %s, ifp index %d, ifm index %d", |
d62a17ae | 700 | __func__, ifp->name, ifp->ifindex, |
701 | ifm->ifm_index); | |
702 | return -1; | |
703 | } | |
704 | ||
9234b382 | 705 | #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */ |
d62a17ae | 706 | bsd_linkdetect_translate(ifm); |
9234b382 | 707 | #endif /* HAVE_BSD_IFI_LINK_STATE */ |
c543a173 | 708 | |
d62a17ae | 709 | /* update flags and handle operative->inoperative transition, if |
710 | * any */ | |
711 | if_flags_update(ifp, ifm->ifm_flags); | |
712 | ||
6eb8827d | 713 | #ifndef RTM_IFANNOUNCE |
d62a17ae | 714 | if (!if_is_up(ifp)) { |
715 | /* No RTM_IFANNOUNCE on this platform, so we can never | |
716 | * distinguish between ~IFF_UP and delete. We must | |
717 | * presume | |
718 | * it has been deleted. | |
719 | * Eg, Solaris will not notify us of unplumb. | |
720 | * | |
721 | * XXX: Fixme - this should be runtime detected | |
722 | * So that a binary compiled on a system with IFANNOUNCE | |
723 | * will still behave correctly if run on a platform | |
724 | * without | |
725 | */ | |
d0438da6 | 726 | if_delete_update(&ifp); |
d62a17ae | 727 | } |
6eb8827d | 728 | #endif /* RTM_IFANNOUNCE */ |
d0438da6 | 729 | if (ifp && if_is_up(ifp)) { |
1ba27564 | 730 | #if defined(__bsdi__) |
d62a17ae | 731 | if_kvm_get_mtu(ifp); |
1ba27564 | 732 | #else |
d62a17ae | 733 | if_get_mtu(ifp); |
1ba27564 | 734 | #endif /* __bsdi__ */ |
d62a17ae | 735 | if_get_metric(ifp); |
736 | } | |
737 | } | |
5c78b3d0 | 738 | |
d0438da6 | 739 | if (ifp) { |
718e3744 | 740 | #ifdef HAVE_NET_RT_IFLIST |
d0438da6 | 741 | ifp->stats = ifm->ifm_data; |
718e3744 | 742 | #endif /* HAVE_NET_RT_IFLIST */ |
d0438da6 | 743 | ifp->speed = ifm->ifm_data.ifi_baudrate / 1000000; |
718e3744 | 744 | |
d0438da6 DS |
745 | if (IS_ZEBRA_DEBUG_KERNEL) |
746 | zlog_debug("%s: interface %s index %d", __func__, | |
747 | ifp->name, ifp->ifindex); | |
748 | } | |
718e3744 | 749 | |
d62a17ae | 750 | return 0; |
718e3744 | 751 | } |
6b0655a2 | 752 | |
718e3744 | 753 | /* Address read from struct ifa_msghdr. */ |
d62a17ae | 754 | static void ifam_read_mesg(struct ifa_msghdr *ifm, union sockunion *addr, |
755 | union sockunion *mask, union sockunion *brd, | |
756 | char *ifname, short *ifnlen) | |
718e3744 | 757 | { |
d62a17ae | 758 | caddr_t pnt, end; |
759 | union sockunion dst; | |
760 | union sockunion gateway; | |
86c57af5 | 761 | int maskbit; |
5b62211d | 762 | char fbuf[64]; |
d62a17ae | 763 | |
764 | pnt = (caddr_t)(ifm + 1); | |
765 | end = ((caddr_t)ifm) + ifm->ifam_msglen; | |
766 | ||
767 | /* Be sure structure is cleared */ | |
768 | memset(mask, 0, sizeof(union sockunion)); | |
769 | memset(addr, 0, sizeof(union sockunion)); | |
770 | memset(brd, 0, sizeof(union sockunion)); | |
771 | memset(&dst, 0, sizeof(union sockunion)); | |
772 | memset(&gateway, 0, sizeof(union sockunion)); | |
773 | ||
774 | /* We fetch each socket variable into sockunion. */ | |
86c57af5 RZ |
775 | for (maskbit = 1; maskbit; maskbit <<= 1) { |
776 | if ((maskbit & ifm->ifam_addrs) == 0) | |
777 | continue; | |
778 | ||
779 | switch (maskbit) { | |
780 | case RTA_DST: | |
781 | pnt += rta_get(pnt, &dst, sizeof(dst)); | |
782 | break; | |
783 | case RTA_GATEWAY: | |
784 | pnt += rta_get(pnt, &gateway, sizeof(gateway)); | |
785 | break; | |
786 | case RTA_NETMASK: | |
19fb538d | 787 | pnt += rta_getattr(pnt, mask, sizeof(*mask)); |
86c57af5 RZ |
788 | break; |
789 | case RTA_IFP: | |
790 | pnt += rta_getsdlname(pnt, ifname, ifnlen); | |
791 | break; | |
792 | case RTA_IFA: | |
793 | pnt += rta_get(pnt, addr, sizeof(*addr)); | |
794 | break; | |
795 | case RTA_BRD: | |
796 | pnt += rta_get(pnt, brd, sizeof(*brd)); | |
797 | break; | |
798 | ||
799 | default: | |
800 | pnt += rta_get(pnt, NULL, 0); | |
801 | break; | |
802 | } | |
803 | ||
804 | if (pnt > end) { | |
805 | zlog_warn("%s: overflow detected (pnt:%p end:%p)", | |
806 | __func__, pnt, end); | |
807 | break; | |
808 | } | |
809 | } | |
d62a17ae | 810 | |
811 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
7053cae4 | 812 | switch (sockunion_family(addr)) { |
d62a17ae | 813 | case AF_INET: |
814 | case AF_INET6: { | |
7053cae4 RZ |
815 | int masklen = |
816 | (sockunion_family(addr) == AF_INET) | |
817 | ? ip_masklen(mask->sin.sin_addr) | |
818 | : ip6_masklen(mask->sin6.sin6_addr); | |
d62a17ae | 819 | zlog_debug( |
bcfce7ad | 820 | "%s: ifindex %d, ifname %s, ifam_addrs {%s}, ifam_flags 0x%x, addr %pSU/%d broad %pSU dst %pSU gateway %pSU", |
d62a17ae | 821 | __func__, ifm->ifam_index, |
5b62211d RZ |
822 | (ifnlen ? ifname : "(nil)"), |
823 | rtatostr(ifm->ifam_addrs, fbuf, sizeof(fbuf)), | |
bcfce7ad MS |
824 | ifm->ifam_flags, addr, masklen, brd, &dst, |
825 | &gateway); | |
d62a17ae | 826 | } break; |
827 | default: | |
5b62211d | 828 | zlog_debug("%s: ifindex %d, ifname %s, ifam_addrs {%s}", |
d62a17ae | 829 | __func__, ifm->ifam_index, |
830 | (ifnlen ? ifname : "(nil)"), | |
5b62211d RZ |
831 | rtatostr(ifm->ifam_addrs, fbuf, |
832 | sizeof(fbuf))); | |
d62a17ae | 833 | break; |
834 | } | |
835 | } | |
836 | ||
837 | /* Assert read up end point matches to end point */ | |
86c57af5 | 838 | pnt = (caddr_t)ROUNDUP((size_t)pnt); |
2d100aa5 | 839 | if (pnt != (caddr_t)ROUNDUP((size_t)end)) |
9df414fe | 840 | zlog_debug("ifam_read() doesn't read all socket data"); |
718e3744 | 841 | } |
842 | ||
843 | /* Interface's address information get. */ | |
d62a17ae | 844 | int ifam_read(struct ifa_msghdr *ifam) |
718e3744 | 845 | { |
d62a17ae | 846 | struct interface *ifp = NULL; |
847 | union sockunion addr, mask, brd; | |
8c36640b | 848 | bool dest_same = false; |
d62a17ae | 849 | char ifname[INTERFACE_NAMSIZ]; |
850 | short ifnlen = 0; | |
ecff5258 DS |
851 | bool isalias = false; |
852 | uint32_t flags = 0; | |
d62a17ae | 853 | |
854 | ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0'; | |
855 | ||
856 | /* Allocate and read address information. */ | |
857 | ifam_read_mesg(ifam, &addr, &mask, &brd, ifname, &ifnlen); | |
858 | ||
859 | if ((ifp = if_lookup_by_index(ifam->ifam_index, VRF_DEFAULT)) == NULL) { | |
e914ccbe | 860 | flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE, |
9df414fe | 861 | "%s: no interface for ifname %s, index %d", __func__, |
d62a17ae | 862 | ifname, ifam->ifam_index); |
863 | return -1; | |
864 | } | |
865 | ||
866 | if (ifnlen && strncmp(ifp->name, ifname, INTERFACE_NAMSIZ)) | |
ecff5258 DS |
867 | isalias = true; |
868 | ||
869 | /* | |
870 | * Mark the alias prefixes as secondary | |
871 | */ | |
872 | if (isalias) | |
873 | SET_FLAG(flags, ZEBRA_IFA_SECONDARY); | |
d62a17ae | 874 | |
875 | /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD | |
876 | field contains a broadcast address or a peer address, so we are | |
877 | forced to | |
878 | rely upon the interface type. */ | |
879 | if (if_is_pointopoint(ifp)) | |
880 | SET_FLAG(flags, ZEBRA_IFA_PEER); | |
8c36640b DS |
881 | else { |
882 | if (memcmp(&addr, &brd, sizeof(addr)) == 0) | |
883 | dest_same = true; | |
884 | } | |
7ab62c53 | 885 | |
6502208c PJ |
886 | #if 0 |
887 | /* it might seem cute to grab the interface metric here, however | |
888 | * we're processing an address update message, and so some systems | |
889 | * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left | |
890 | * in deliberately, as comment. | |
891 | */ | |
d34b8991 | 892 | ifp->metric = ifam->ifam_metric; |
6502208c PJ |
893 | #endif |
894 | ||
d62a17ae | 895 | /* Add connected address. */ |
896 | switch (sockunion_family(&addr)) { | |
897 | case AF_INET: | |
898 | if (ifam->ifam_type == RTM_NEWADDR) | |
899 | connected_add_ipv4(ifp, flags, &addr.sin.sin_addr, | |
900 | ip_masklen(mask.sin.sin_addr), | |
8c36640b | 901 | dest_same ? NULL : &brd.sin.sin_addr, |
cde1af84 AK |
902 | (isalias ? ifname : NULL), |
903 | METRIC_MAX); | |
d62a17ae | 904 | else |
905 | connected_delete_ipv4(ifp, flags, &addr.sin.sin_addr, | |
906 | ip_masklen(mask.sin.sin_addr), | |
8c36640b DS |
907 | dest_same ? NULL |
908 | : &brd.sin.sin_addr); | |
d62a17ae | 909 | break; |
910 | case AF_INET6: | |
911 | /* Unset interface index from link-local address when IPv6 stack | |
912 | is KAME. */ | |
913 | if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6.sin6_addr)) { | |
914 | SET_IN6_LINKLOCAL_IFINDEX(addr.sin6.sin6_addr, 0); | |
915 | } | |
916 | ||
917 | if (ifam->ifam_type == RTM_NEWADDR) | |
918 | connected_add_ipv6(ifp, flags, &addr.sin6.sin6_addr, | |
60c0687a | 919 | NULL, |
d62a17ae | 920 | ip6_masklen(mask.sin6.sin6_addr), |
cde1af84 AK |
921 | (isalias ? ifname : NULL), |
922 | METRIC_MAX); | |
d62a17ae | 923 | else |
60c0687a | 924 | connected_delete_ipv6(ifp, &addr.sin6.sin6_addr, NULL, |
608105a7 | 925 | ip6_masklen(mask.sin6.sin6_addr)); |
d62a17ae | 926 | break; |
927 | default: | |
928 | /* Unsupported family silently ignore... */ | |
929 | break; | |
930 | } | |
931 | ||
932 | /* Check interface flag for implicit up of the interface. */ | |
933 | if_refresh(ifp); | |
5c78b3d0 | 934 | |
d62a17ae | 935 | return 0; |
718e3744 | 936 | } |
6b0655a2 | 937 | |
718e3744 | 938 | /* Interface function for reading kernel routing table information. */ |
d62a17ae | 939 | static int rtm_read_mesg(struct rt_msghdr *rtm, union sockunion *dest, |
940 | union sockunion *mask, union sockunion *gate, | |
941 | char *ifname, short *ifnlen) | |
718e3744 | 942 | { |
d62a17ae | 943 | caddr_t pnt, end; |
86c57af5 | 944 | int maskbit; |
d62a17ae | 945 | |
946 | /* Pnt points out socket data start point. */ | |
947 | pnt = (caddr_t)(rtm + 1); | |
948 | end = ((caddr_t)rtm) + rtm->rtm_msglen; | |
949 | ||
950 | /* rt_msghdr version check. */ | |
951 | if (rtm->rtm_version != RTM_VERSION) | |
e914ccbe | 952 | flog_warn(EC_ZEBRA_RTM_VERSION_MISMATCH, |
bcfce7ad | 953 | "Routing message version different %d should be %d.This may cause problem", |
9df414fe | 954 | rtm->rtm_version, RTM_VERSION); |
d62a17ae | 955 | |
956 | /* Be sure structure is cleared */ | |
957 | memset(dest, 0, sizeof(union sockunion)); | |
958 | memset(gate, 0, sizeof(union sockunion)); | |
959 | memset(mask, 0, sizeof(union sockunion)); | |
960 | ||
961 | /* We fetch each socket variable into sockunion. */ | |
86c57af5 RZ |
962 | /* We fetch each socket variable into sockunion. */ |
963 | for (maskbit = 1; maskbit; maskbit <<= 1) { | |
964 | if ((maskbit & rtm->rtm_addrs) == 0) | |
965 | continue; | |
966 | ||
967 | switch (maskbit) { | |
968 | case RTA_DST: | |
969 | pnt += rta_get(pnt, dest, sizeof(*dest)); | |
970 | break; | |
971 | case RTA_GATEWAY: | |
972 | pnt += rta_get(pnt, gate, sizeof(*gate)); | |
973 | break; | |
974 | case RTA_NETMASK: | |
1583f8f5 | 975 | pnt += rta_getattr(pnt, mask, sizeof(*mask)); |
86c57af5 RZ |
976 | break; |
977 | case RTA_IFP: | |
978 | pnt += rta_getsdlname(pnt, ifname, ifnlen); | |
979 | break; | |
980 | ||
981 | default: | |
982 | pnt += rta_get(pnt, NULL, 0); | |
983 | break; | |
984 | } | |
985 | ||
986 | if (pnt > end) { | |
987 | zlog_warn("%s: overflow detected (pnt:%p end:%p)", | |
988 | __func__, pnt, end); | |
989 | break; | |
990 | } | |
991 | } | |
d62a17ae | 992 | |
993 | /* If there is netmask information set it's family same as | |
994 | destination family*/ | |
995 | if (rtm->rtm_addrs & RTA_NETMASK) | |
996 | mask->sa.sa_family = dest->sa.sa_family; | |
997 | ||
998 | /* Assert read up to the end of pointer. */ | |
999 | if (pnt != end) | |
9df414fe | 1000 | zlog_debug("rtm_read() doesn't read all socket data."); |
d62a17ae | 1001 | |
1002 | return rtm->rtm_flags; | |
718e3744 | 1003 | } |
1004 | ||
d62a17ae | 1005 | void rtm_read(struct rt_msghdr *rtm) |
718e3744 | 1006 | { |
d62a17ae | 1007 | int flags; |
acde7f6b | 1008 | uint32_t zebra_flags; |
d62a17ae | 1009 | union sockunion dest, mask, gate; |
1010 | char ifname[INTERFACE_NAMSIZ + 1]; | |
1011 | short ifnlen = 0; | |
fd36be7e | 1012 | struct nexthop nh; |
026745a2 DS |
1013 | struct prefix p; |
1014 | ifindex_t ifindex = 0; | |
1015 | afi_t afi; | |
5b62211d | 1016 | char fbuf[64]; |
73d3197c DS |
1017 | int32_t proto = ZEBRA_ROUTE_KERNEL; |
1018 | uint8_t distance = 0; | |
d62a17ae | 1019 | |
1020 | zebra_flags = 0; | |
1021 | ||
1022 | /* Read destination and netmask and gateway from rtm message | |
1023 | structure. */ | |
1024 | flags = rtm_read_mesg(rtm, &dest, &mask, &gate, ifname, &ifnlen); | |
1025 | if (!(flags & RTF_DONE)) | |
1026 | return; | |
1027 | if (IS_ZEBRA_DEBUG_KERNEL) | |
5b62211d | 1028 | zlog_debug("%s: got rtm of type %d (%s) addrs {%s}", __func__, |
d62a17ae | 1029 | rtm->rtm_type, |
5b62211d RZ |
1030 | lookup_msg(rtm_type_str, rtm->rtm_type, NULL), |
1031 | rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf))); | |
d62a17ae | 1032 | |
1033 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ | |
1034 | if (flags & RTF_CLONED) | |
1035 | return; | |
718e3744 | 1036 | #endif |
d62a17ae | 1037 | #ifdef RTF_WASCLONED /*freebsd*/ |
1038 | if (flags & RTF_WASCLONED) | |
1039 | return; | |
718e3744 | 1040 | #endif |
1041 | ||
d62a17ae | 1042 | if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE) |
1043 | && !(flags & RTF_UP)) | |
1044 | return; | |
1045 | ||
1046 | /* This is connected route. */ | |
1047 | if (!(flags & RTF_GATEWAY)) | |
1048 | return; | |
1049 | ||
73d3197c | 1050 | if (flags & RTF_PROTO1) { |
d62a17ae | 1051 | SET_FLAG(zebra_flags, ZEBRA_FLAG_SELFROUTE); |
73d3197c DS |
1052 | proto = ZEBRA_ROUTE_STATIC; |
1053 | distance = 255; | |
1054 | } | |
d62a17ae | 1055 | |
a8309422 | 1056 | memset(&nh, 0, sizeof(nh)); |
4a7371e9 DS |
1057 | |
1058 | nh.vrf_id = VRF_DEFAULT; | |
d62a17ae | 1059 | /* This is a reject or blackhole route */ |
a8309422 DL |
1060 | if (flags & RTF_REJECT) { |
1061 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
1062 | nh.bh_type = BLACKHOLE_REJECT; | |
1063 | } else if (flags & RTF_BLACKHOLE) { | |
1064 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
1065 | nh.bh_type = BLACKHOLE_NULL; | |
1066 | } | |
d62a17ae | 1067 | |
c626d369 DS |
1068 | /* |
1069 | * Ignore our own messages. | |
1070 | */ | |
1071 | if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid) | |
1072 | return; | |
d62a17ae | 1073 | |
d62a17ae | 1074 | if (dest.sa.sa_family == AF_INET) { |
026745a2 | 1075 | afi = AFI_IP; |
d62a17ae | 1076 | p.family = AF_INET; |
1077 | p.u.prefix4 = dest.sin.sin_addr; | |
1078 | if (flags & RTF_HOST) | |
936fbaef | 1079 | p.prefixlen = IPV4_MAX_BITLEN; |
d62a17ae | 1080 | else |
1081 | p.prefixlen = ip_masklen(mask.sin.sin_addr); | |
1082 | ||
a8309422 DL |
1083 | if (!nh.type) { |
1084 | nh.type = NEXTHOP_TYPE_IPV4; | |
1085 | nh.gate.ipv4 = gate.sin.sin_addr; | |
1086 | } | |
026745a2 DS |
1087 | } else if (dest.sa.sa_family == AF_INET6) { |
1088 | afi = AFI_IP6; | |
d62a17ae | 1089 | p.family = AF_INET6; |
1090 | p.u.prefix6 = dest.sin6.sin6_addr; | |
1091 | if (flags & RTF_HOST) | |
f4d81e55 | 1092 | p.prefixlen = IPV6_MAX_BITLEN; |
d62a17ae | 1093 | else |
1094 | p.prefixlen = ip6_masklen(mask.sin6.sin6_addr); | |
718e3744 | 1095 | |
1096 | #ifdef KAME | |
d62a17ae | 1097 | if (IN6_IS_ADDR_LINKLOCAL(&gate.sin6.sin6_addr)) { |
1098 | ifindex = IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr); | |
1099 | SET_IN6_LINKLOCAL_IFINDEX(gate.sin6.sin6_addr, 0); | |
1100 | } | |
718e3744 | 1101 | #endif /* KAME */ |
1102 | ||
a8309422 DL |
1103 | if (!nh.type) { |
1104 | nh.type = ifindex ? NEXTHOP_TYPE_IPV6_IFINDEX | |
1105 | : NEXTHOP_TYPE_IPV6; | |
1106 | nh.gate.ipv6 = gate.sin6.sin6_addr; | |
1107 | nh.ifindex = ifindex; | |
1108 | } | |
026745a2 DS |
1109 | } else |
1110 | return; | |
d62a17ae | 1111 | |
026745a2 DS |
1112 | /* |
1113 | * CHANGE: delete the old prefix, we have no further information | |
1114 | * to specify the route really | |
1115 | */ | |
1116 | if (rtm->rtm_type == RTM_CHANGE) | |
1117 | rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, ZEBRA_ROUTE_KERNEL, | |
bc541126 | 1118 | 0, zebra_flags, &p, NULL, NULL, 0, RT_TABLE_MAIN, 0, |
3ceae22b | 1119 | 0, true); |
026745a2 DS |
1120 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD |
1121 | || rtm->rtm_type == RTM_CHANGE) | |
73d3197c DS |
1122 | rib_add(afi, SAFI_UNICAST, VRF_DEFAULT, proto, 0, zebra_flags, |
1123 | &p, NULL, &nh, 0, RT_TABLE_MAIN, 0, 0, distance, 0, | |
1124 | false); | |
026745a2 | 1125 | else |
73d3197c DS |
1126 | rib_delete(afi, SAFI_UNICAST, VRF_DEFAULT, proto, 0, |
1127 | zebra_flags, &p, NULL, &nh, 0, RT_TABLE_MAIN, 0, | |
1128 | distance, true); | |
718e3744 | 1129 | } |
1130 | ||
1131 | /* Interface function for the kernel routing table updates. Support | |
6621ca86 | 1132 | * for RTM_CHANGE will be needed. |
1133 | * Exported only for rt_socket.c | |
1134 | */ | |
d62a17ae | 1135 | int rtm_write(int message, union sockunion *dest, union sockunion *mask, |
1136 | union sockunion *gate, union sockunion *mpls, unsigned int index, | |
a8309422 | 1137 | enum blackhole_type bh_type, int metric) |
718e3744 | 1138 | { |
d62a17ae | 1139 | int ret; |
1140 | caddr_t pnt; | |
1141 | struct interface *ifp; | |
1142 | ||
1143 | /* Sequencial number of routing message. */ | |
1144 | static int msg_seq = 0; | |
1145 | ||
1146 | /* Struct of rt_msghdr and buffer for storing socket's data. */ | |
1147 | struct { | |
1148 | struct rt_msghdr rtm; | |
1149 | char buf[512]; | |
1150 | } msg; | |
1151 | ||
80776aec | 1152 | if (dplane_routing_sock < 0) |
d62a17ae | 1153 | return ZEBRA_ERR_EPERM; |
1154 | ||
1155 | /* Clear and set rt_msghdr values */ | |
1156 | memset(&msg, 0, sizeof(struct rt_msghdr)); | |
1157 | msg.rtm.rtm_version = RTM_VERSION; | |
1158 | msg.rtm.rtm_type = message; | |
1159 | msg.rtm.rtm_seq = msg_seq++; | |
1160 | msg.rtm.rtm_addrs = RTA_DST; | |
1161 | msg.rtm.rtm_addrs |= RTA_GATEWAY; | |
1162 | msg.rtm.rtm_flags = RTF_UP; | |
fe6c7157 | 1163 | #ifdef __OpenBSD__ |
d62a17ae | 1164 | msg.rtm.rtm_flags |= RTF_MPATH; |
1165 | msg.rtm.rtm_fmask = RTF_MPLS; | |
d3e2c74a | 1166 | #endif |
d62a17ae | 1167 | msg.rtm.rtm_index = index; |
718e3744 | 1168 | |
d62a17ae | 1169 | if (metric != 0) { |
1170 | msg.rtm.rtm_rmx.rmx_hopcount = metric; | |
1171 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; | |
1172 | } | |
718e3744 | 1173 | |
d62a17ae | 1174 | ifp = if_lookup_by_index(index, VRF_DEFAULT); |
718e3744 | 1175 | |
d62a17ae | 1176 | if (gate && (message == RTM_ADD || message == RTM_CHANGE)) |
1177 | msg.rtm.rtm_flags |= RTF_GATEWAY; | |
718e3744 | 1178 | |
d62a17ae | 1179 | /* When RTF_CLONING is unavailable on BSD, should we set some |
1180 | * other flag instead? | |
1181 | */ | |
e6f148e6 | 1182 | #ifdef RTF_CLONING |
d62a17ae | 1183 | if (!gate && (message == RTM_ADD || message == RTM_CHANGE) && ifp |
1184 | && (ifp->flags & IFF_POINTOPOINT) == 0) | |
1185 | msg.rtm.rtm_flags |= RTF_CLONING; | |
e6f148e6 | 1186 | #endif /* RTF_CLONING */ |
718e3744 | 1187 | |
d62a17ae | 1188 | /* If no protocol specific gateway is specified, use link |
1189 | address for gateway. */ | |
1190 | if (!gate) { | |
1191 | if (!ifp) { | |
1192 | char dest_buf[INET_ADDRSTRLEN] = "NULL", | |
1193 | mask_buf[INET_ADDRSTRLEN] = "255.255.255.255"; | |
1194 | if (dest) | |
1195 | inet_ntop(AF_INET, &dest->sin.sin_addr, | |
1196 | dest_buf, INET_ADDRSTRLEN); | |
1197 | if (mask) | |
1198 | inet_ntop(AF_INET, &mask->sin.sin_addr, | |
1199 | mask_buf, INET_ADDRSTRLEN); | |
9df414fe | 1200 | flog_warn( |
e914ccbe | 1201 | EC_ZEBRA_RTM_NO_GATEWAY, |
d62a17ae | 1202 | "%s: %s/%s: gate == NULL and no gateway found for ifindex %d", |
1203 | __func__, dest_buf, mask_buf, index); | |
1204 | return -1; | |
1205 | } | |
1206 | gate = (union sockunion *)&((struct zebra_if *)ifp->info)->sdl; | |
1207 | } | |
1208 | ||
1209 | if (mask) | |
1210 | msg.rtm.rtm_addrs |= RTA_NETMASK; | |
1211 | else if (message == RTM_ADD || message == RTM_CHANGE) | |
1212 | msg.rtm.rtm_flags |= RTF_HOST; | |
718e3744 | 1213 | |
fe6c7157 | 1214 | #ifdef __OpenBSD__ |
d62a17ae | 1215 | if (mpls) { |
1216 | msg.rtm.rtm_addrs |= RTA_SRC; | |
1217 | msg.rtm.rtm_flags |= RTF_MPLS; | |
1218 | ||
1219 | if (mpls->smpls.smpls_label | |
70e98a7f | 1220 | != htonl(MPLS_LABEL_IMPLICIT_NULL << MPLS_LABEL_OFFSET)) |
d62a17ae | 1221 | msg.rtm.rtm_mpls = MPLS_OP_PUSH; |
1222 | } | |
d3e2c74a RW |
1223 | #endif |
1224 | ||
d62a17ae | 1225 | /* Tagging route with flags */ |
1226 | msg.rtm.rtm_flags |= (RTF_PROTO1); | |
718e3744 | 1227 | |
a8309422 DL |
1228 | switch (bh_type) { |
1229 | case BLACKHOLE_UNSPEC: | |
1230 | break; | |
1231 | case BLACKHOLE_REJECT: | |
d62a17ae | 1232 | msg.rtm.rtm_flags |= RTF_REJECT; |
a8309422 DL |
1233 | break; |
1234 | default: | |
1235 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; | |
1236 | break; | |
1237 | } | |
81dfcaa2 | 1238 | |
718e3744 | 1239 | |
d62a17ae | 1240 | #define SOCKADDRSET(X, R) \ |
1241 | if (msg.rtm.rtm_addrs & (R)) { \ | |
1242 | int len = SAROUNDUP(X); \ | |
1243 | memcpy(pnt, (caddr_t)(X), len); \ | |
1244 | pnt += len; \ | |
1245 | } | |
718e3744 | 1246 | |
d62a17ae | 1247 | pnt = (caddr_t)msg.buf; |
718e3744 | 1248 | |
d62a17ae | 1249 | /* Write each socket data into rtm message buffer */ |
1250 | SOCKADDRSET(dest, RTA_DST); | |
1251 | SOCKADDRSET(gate, RTA_GATEWAY); | |
1252 | SOCKADDRSET(mask, RTA_NETMASK); | |
fe6c7157 | 1253 | #ifdef __OpenBSD__ |
d62a17ae | 1254 | SOCKADDRSET(mpls, RTA_SRC); |
d3e2c74a | 1255 | #endif |
718e3744 | 1256 | |
d62a17ae | 1257 | msg.rtm.rtm_msglen = pnt - (caddr_t)&msg; |
1258 | ||
80776aec | 1259 | ret = write(dplane_routing_sock, &msg, msg.rtm.rtm_msglen); |
d62a17ae | 1260 | |
1261 | if (ret != msg.rtm.rtm_msglen) { | |
1262 | if (errno == EEXIST) | |
1263 | return ZEBRA_ERR_RTEXIST; | |
1264 | if (errno == ENETUNREACH) | |
1265 | return ZEBRA_ERR_RTUNREACH; | |
1266 | if (errno == ESRCH) | |
1267 | return ZEBRA_ERR_RTNOEXIST; | |
1268 | ||
450971aa | 1269 | flog_err_sys(EC_LIB_SOCKET, "%s: write : %s (%d)", __func__, |
9df414fe | 1270 | safe_strerror(errno), errno); |
d62a17ae | 1271 | return ZEBRA_ERR_KERNEL; |
1272 | } | |
1273 | return ZEBRA_ERR_NOERROR; | |
718e3744 | 1274 | } |
1275 | ||
6b0655a2 | 1276 | |
718e3744 | 1277 | #include "thread.h" |
1278 | #include "zebra/zserv.h" | |
1279 | ||
718e3744 | 1280 | /* For debug purpose. */ |
d62a17ae | 1281 | static void rtmsg_debug(struct rt_msghdr *rtm) |
718e3744 | 1282 | { |
5b62211d RZ |
1283 | char fbuf[64]; |
1284 | ||
d62a17ae | 1285 | zlog_debug("Kernel: Len: %d Type: %s", rtm->rtm_msglen, |
1286 | lookup_msg(rtm_type_str, rtm->rtm_type, NULL)); | |
1287 | rtm_flag_dump(rtm->rtm_flags); | |
1288 | zlog_debug("Kernel: message seq %d", rtm->rtm_seq); | |
5b62211d RZ |
1289 | zlog_debug("Kernel: pid %lld, rtm_addrs {%s}", (long long)rtm->rtm_pid, |
1290 | rtatostr(rtm->rtm_addrs, fbuf, sizeof(fbuf))); | |
718e3744 | 1291 | } |
1292 | ||
1293 | /* This is pretty gross, better suggestions welcome -- mhandler */ | |
1294 | #ifndef RTAX_MAX | |
1295 | #ifdef RTA_NUMBITS | |
1296 | #define RTAX_MAX RTA_NUMBITS | |
1297 | #else | |
1298 | #define RTAX_MAX 8 | |
1299 | #endif /* RTA_NUMBITS */ | |
1300 | #endif /* RTAX_MAX */ | |
1301 | ||
1302 | /* Kernel routing table and interface updates via routing socket. */ | |
cc9f21da | 1303 | static void kernel_read(struct thread *thread) |
718e3744 | 1304 | { |
d62a17ae | 1305 | int sock; |
1306 | int nbytes; | |
1307 | struct rt_msghdr *rtm; | |
1308 | ||
1309 | /* | |
1310 | * This must be big enough for any message the kernel might send. | |
1311 | * Rather than determining how many sockaddrs of what size might be | |
1312 | * in each particular message, just use RTAX_MAX of sockaddr_storage | |
1313 | * for each. Note that the sockaddrs must be after each message | |
1314 | * definition, or rather after whichever happens to be the largest, | |
1315 | * since the buffer needs to be big enough for a message and the | |
1316 | * sockaddrs together. | |
1317 | */ | |
1318 | union { | |
1319 | /* Routing information. */ | |
1320 | struct { | |
1321 | struct rt_msghdr rtm; | |
1322 | struct sockaddr_storage addr[RTAX_MAX]; | |
1323 | } r; | |
1324 | ||
1325 | /* Interface information. */ | |
1326 | struct { | |
1327 | struct if_msghdr ifm; | |
1328 | struct sockaddr_storage addr[RTAX_MAX]; | |
1329 | } im; | |
1330 | ||
1331 | /* Interface address information. */ | |
1332 | struct { | |
1333 | struct ifa_msghdr ifa; | |
1334 | struct sockaddr_storage addr[RTAX_MAX]; | |
1335 | } ia; | |
718e3744 | 1336 | |
1337 | #ifdef RTM_IFANNOUNCE | |
d62a17ae | 1338 | /* Interface arrival/departure */ |
1339 | struct { | |
1340 | struct if_announcemsghdr ifan; | |
1341 | struct sockaddr_storage addr[RTAX_MAX]; | |
1342 | } ian; | |
718e3744 | 1343 | #endif /* RTM_IFANNOUNCE */ |
1344 | ||
d62a17ae | 1345 | } buf; |
718e3744 | 1346 | |
d62a17ae | 1347 | /* Fetch routing socket. */ |
1348 | sock = THREAD_FD(thread); | |
718e3744 | 1349 | |
0d6f7fd6 | 1350 | nbytes = read(sock, &buf, sizeof(buf)); |
718e3744 | 1351 | |
68cd699d RM |
1352 | if (nbytes < 0) { |
1353 | if (errno == ENOBUFS) { | |
1354 | flog_err(EC_ZEBRA_RECVMSG_OVERRUN, | |
1355 | "routing socket overrun: %s", | |
1356 | safe_strerror(errno)); | |
1357 | /* | |
1358 | * In this case we are screwed. | |
1359 | * There is no good way to | |
1360 | * recover zebra at this point. | |
1361 | */ | |
1362 | exit(-1); | |
1363 | } | |
1364 | if (errno != EAGAIN && errno != EWOULDBLOCK) | |
450971aa | 1365 | flog_err_sys(EC_LIB_SOCKET, "routing socket error: %s", |
9df414fe | 1366 | safe_strerror(errno)); |
cc9f21da | 1367 | return; |
d62a17ae | 1368 | } |
718e3744 | 1369 | |
68cd699d | 1370 | if (nbytes == 0) |
cc9f21da | 1371 | return; |
68cd699d | 1372 | |
3801e764 | 1373 | thread_add_read(zrouter.master, kernel_read, NULL, sock, NULL); |
718e3744 | 1374 | |
d62a17ae | 1375 | if (IS_ZEBRA_DEBUG_KERNEL) |
1376 | rtmsg_debug(&buf.r.rtm); | |
718e3744 | 1377 | |
d62a17ae | 1378 | rtm = &buf.r.rtm; |
718e3744 | 1379 | |
d62a17ae | 1380 | /* |
1381 | * Ensure that we didn't drop any data, so that processing routines | |
1382 | * can assume they have the whole message. | |
1383 | */ | |
1384 | if (rtm->rtm_msglen != nbytes) { | |
9df414fe | 1385 | zlog_debug( |
63efca0e | 1386 | "kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d", |
d62a17ae | 1387 | rtm->rtm_msglen, nbytes, rtm->rtm_type); |
cc9f21da | 1388 | return; |
d62a17ae | 1389 | } |
1390 | ||
1391 | switch (rtm->rtm_type) { | |
1392 | case RTM_ADD: | |
1393 | case RTM_DELETE: | |
1394 | case RTM_CHANGE: | |
1395 | rtm_read(rtm); | |
1396 | break; | |
1397 | case RTM_IFINFO: | |
1398 | ifm_read(&buf.im.ifm); | |
1399 | break; | |
1400 | case RTM_NEWADDR: | |
1401 | case RTM_DELADDR: | |
1402 | ifam_read(&buf.ia.ifa); | |
1403 | break; | |
718e3744 | 1404 | #ifdef RTM_IFANNOUNCE |
d62a17ae | 1405 | case RTM_IFANNOUNCE: |
1406 | ifan_read(&buf.ian.ifan); | |
1407 | break; | |
718e3744 | 1408 | #endif /* RTM_IFANNOUNCE */ |
d62a17ae | 1409 | default: |
1410 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1411 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); | |
1412 | break; | |
1413 | } | |
718e3744 | 1414 | } |
1415 | ||
1416 | /* Make routing socket. */ | |
d62a17ae | 1417 | static void routing_socket(struct zebra_ns *zns) |
718e3744 | 1418 | { |
9fb83b55 DS |
1419 | uint32_t default_rcvbuf; |
1420 | socklen_t optlen; | |
1421 | ||
0cf6db21 | 1422 | frr_with_privs(&zserv_privs) { |
6bb30c2c | 1423 | routing_sock = ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id); |
80776aec MS |
1424 | |
1425 | dplane_routing_sock = | |
1426 | ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id); | |
6bb30c2c | 1427 | } |
d62a17ae | 1428 | |
1429 | if (routing_sock < 0) { | |
1c50c1c0 | 1430 | flog_err_sys(EC_LIB_SOCKET, "Can't init kernel routing socket"); |
d62a17ae | 1431 | return; |
1432 | } | |
1433 | ||
80776aec MS |
1434 | if (dplane_routing_sock < 0) { |
1435 | flog_err_sys(EC_LIB_SOCKET, | |
1436 | "Can't init kernel dataplane routing socket"); | |
1437 | return; | |
1438 | } | |
1439 | ||
68cd699d RM |
1440 | #ifdef SO_RERROR |
1441 | /* Allow reporting of route(4) buffer overflow errors */ | |
1442 | int n = 1; | |
355c74b7 | 1443 | |
68cd699d RM |
1444 | if (setsockopt(routing_sock, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) < 0) |
1445 | flog_err_sys(EC_LIB_SOCKET, | |
1446 | "Can't set SO_RERROR on routing socket"); | |
1447 | #endif | |
1448 | ||
d62a17ae | 1449 | /* XXX: Socket should be NONBLOCK, however as we currently |
1450 | * discard failed writes, this will lead to inconsistencies. | |
1451 | * For now, socket must be blocking. | |
1452 | */ | |
1453 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) | |
1454 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ | |
1455 | ||
9fb83b55 DS |
1456 | /* |
1457 | * Attempt to set a more useful receive buffer size | |
1458 | */ | |
1459 | optlen = sizeof(default_rcvbuf); | |
1460 | if (getsockopt(routing_sock, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf, | |
1461 | &optlen) == -1) | |
1462 | flog_err_sys(EC_LIB_SOCKET, | |
1463 | "routing_sock sockopt SOL_SOCKET SO_RCVBUF"); | |
1464 | else { | |
1465 | for (; rcvbufsize > default_rcvbuf && | |
1466 | setsockopt(routing_sock, SOL_SOCKET, SO_RCVBUF, | |
1467 | &rcvbufsize, sizeof(rcvbufsize)) == -1 && | |
1468 | errno == ENOBUFS; | |
1469 | rcvbufsize /= 2) | |
1470 | ; | |
1471 | } | |
1472 | ||
d62a17ae | 1473 | /* kernel_read needs rewrite. */ |
3801e764 | 1474 | thread_add_read(zrouter.master, kernel_read, NULL, routing_sock, NULL); |
718e3744 | 1475 | } |
1476 | ||
1477 | /* Exported interface function. This function simply calls | |
1478 | routing_socket (). */ | |
d62a17ae | 1479 | void kernel_init(struct zebra_ns *zns) |
8f7d9fc0 | 1480 | { |
d62a17ae | 1481 | routing_socket(zns); |
8f7d9fc0 FL |
1482 | } |
1483 | ||
62b8bb7a | 1484 | void kernel_terminate(struct zebra_ns *zns, bool complete) |
718e3744 | 1485 | { |
d62a17ae | 1486 | return; |
718e3744 | 1487 | } |
ddfeb486 | 1488 | |
34869809 MS |
1489 | /* |
1490 | * Global init for platform-/OS-specific things | |
1491 | */ | |
1492 | void kernel_router_init(void) | |
1493 | { | |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | * Global deinit for platform-/OS-specific things | |
1498 | */ | |
1499 | void kernel_router_terminate(void) | |
1500 | { | |
1501 | } | |
1502 | ||
d166308b MS |
1503 | /* |
1504 | * Called by the dplane pthread to read incoming OS messages and dispatch them. | |
1505 | */ | |
1506 | int kernel_dplane_read(struct zebra_dplane_info *info) | |
1507 | { | |
1508 | return 0; | |
1509 | } | |
1510 | ||
fef24b03 JU |
1511 | void kernel_update_multi(struct dplane_ctx_q *ctx_list) |
1512 | { | |
18f60fe9 JU |
1513 | struct zebra_dplane_ctx *ctx; |
1514 | struct dplane_ctx_q handled_list; | |
1515 | enum zebra_dplane_result res; | |
fef24b03 | 1516 | |
18f60fe9 JU |
1517 | TAILQ_INIT(&handled_list); |
1518 | ||
1519 | while (true) { | |
1520 | ctx = dplane_ctx_dequeue(ctx_list); | |
1521 | if (ctx == NULL) | |
1522 | break; | |
1523 | ||
1524 | /* | |
1525 | * A previous provider plugin may have asked to skip the | |
1526 | * kernel update. | |
1527 | */ | |
1528 | if (dplane_ctx_is_skip_kernel(ctx)) { | |
1529 | res = ZEBRA_DPLANE_REQUEST_SUCCESS; | |
1530 | goto skip_one; | |
1531 | } | |
1532 | ||
1533 | switch (dplane_ctx_get_op(ctx)) { | |
1534 | ||
1535 | case DPLANE_OP_ROUTE_INSTALL: | |
1536 | case DPLANE_OP_ROUTE_UPDATE: | |
1537 | case DPLANE_OP_ROUTE_DELETE: | |
1538 | res = kernel_route_update(ctx); | |
1539 | break; | |
1540 | ||
1541 | case DPLANE_OP_NH_INSTALL: | |
1542 | case DPLANE_OP_NH_UPDATE: | |
1543 | case DPLANE_OP_NH_DELETE: | |
1544 | res = kernel_nexthop_update(ctx); | |
1545 | break; | |
1546 | ||
1547 | case DPLANE_OP_LSP_INSTALL: | |
1548 | case DPLANE_OP_LSP_UPDATE: | |
1549 | case DPLANE_OP_LSP_DELETE: | |
1550 | res = kernel_lsp_update(ctx); | |
1551 | break; | |
1552 | ||
1553 | case DPLANE_OP_PW_INSTALL: | |
1554 | case DPLANE_OP_PW_UNINSTALL: | |
1555 | res = kernel_pw_update(ctx); | |
1556 | break; | |
1557 | ||
1558 | case DPLANE_OP_ADDR_INSTALL: | |
1559 | case DPLANE_OP_ADDR_UNINSTALL: | |
1560 | res = kernel_address_update_ctx(ctx); | |
1561 | break; | |
1562 | ||
1563 | case DPLANE_OP_MAC_INSTALL: | |
1564 | case DPLANE_OP_MAC_DELETE: | |
1565 | res = kernel_mac_update_ctx(ctx); | |
1566 | break; | |
1567 | ||
1568 | case DPLANE_OP_NEIGH_INSTALL: | |
1569 | case DPLANE_OP_NEIGH_UPDATE: | |
1570 | case DPLANE_OP_NEIGH_DELETE: | |
1571 | case DPLANE_OP_VTEP_ADD: | |
1572 | case DPLANE_OP_VTEP_DELETE: | |
d68e74b4 | 1573 | case DPLANE_OP_NEIGH_DISCOVER: |
18f60fe9 JU |
1574 | res = kernel_neigh_update_ctx(ctx); |
1575 | break; | |
1576 | ||
1577 | case DPLANE_OP_RULE_ADD: | |
1578 | case DPLANE_OP_RULE_DELETE: | |
1579 | case DPLANE_OP_RULE_UPDATE: | |
1580 | res = kernel_pbr_rule_update(ctx); | |
1581 | break; | |
1582 | ||
97c72633 SW |
1583 | case DPLANE_OP_INTF_INSTALL: |
1584 | case DPLANE_OP_INTF_UPDATE: | |
1585 | case DPLANE_OP_INTF_DELETE: | |
1586 | res = kernel_intf_update(ctx); | |
1587 | break; | |
1588 | ||
18f60fe9 JU |
1589 | /* Ignore 'notifications' - no-op */ |
1590 | case DPLANE_OP_SYS_ROUTE_ADD: | |
1591 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
1592 | case DPLANE_OP_ROUTE_NOTIFY: | |
1593 | case DPLANE_OP_LSP_NOTIFY: | |
1594 | res = ZEBRA_DPLANE_REQUEST_SUCCESS; | |
1595 | break; | |
1596 | ||
1597 | default: | |
1598 | res = ZEBRA_DPLANE_REQUEST_FAILURE; | |
1599 | break; | |
1600 | } | |
1601 | ||
1602 | skip_one: | |
1603 | dplane_ctx_set_status(ctx, res); | |
1604 | ||
1605 | dplane_ctx_enqueue_tail(&handled_list, ctx); | |
1606 | } | |
1607 | ||
1608 | TAILQ_INIT(ctx_list); | |
1609 | dplane_ctx_list_append(ctx_list, &handled_list); | |
fef24b03 JU |
1610 | } |
1611 | ||
ddfeb486 | 1612 | #endif /* !HAVE_NETLINK */ |