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