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