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