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