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