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