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