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