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