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