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