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