blob: e8668a1dd47c9306808bca535f856f781896872a [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Kernel routing table updates by routing socket.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "if.h"
26#include "prefix.h"
27#include "sockunion.h"
28#include "log.h"
29#include "str.h"
pauledd7c242003-06-04 13:59:38 +000030#include "privs.h"
paul718e3742002-12-13 20:15:29 +000031
32#include "zebra/debug.h"
33#include "zebra/rib.h"
paul6621ca82005-11-23 13:02:08 +000034#include "zebra/rt.h"
Denis Ovsienkodc958242007-08-13 16:03:06 +000035#include "zebra/kernel_socket.h"
paul718e3742002-12-13 20:15:29 +000036
pauledd7c242003-06-04 13:59:38 +000037extern struct zebra_privs_t zserv_privs;
38
paul6621ca82005-11-23 13:02:08 +000039/* kernel socket export */
40extern int rtm_write (int message, union sockunion *dest,
41 union sockunion *mask, union sockunion *gate,
42 unsigned int index, int zebra_flags, int metric);
paul718e3742002-12-13 20:15:29 +000043
44/* Adjust netmask socket length. Return value is a adjusted sin_len
45 value. */
paul6621ca82005-11-23 13:02:08 +000046static int
paul718e3742002-12-13 20:15:29 +000047sin_masklen (struct in_addr mask)
48{
49 char *p, *lim;
50 int len;
51 struct sockaddr_in sin;
52
53 if (mask.s_addr == 0)
54 return sizeof (long);
55
56 sin.sin_addr = mask;
57 len = sizeof (struct sockaddr_in);
58
59 lim = (char *) &sin.sin_addr;
60 p = lim + sizeof (sin.sin_addr);
61
62 while (*--p == 0 && p >= lim)
63 len--;
64 return len;
65}
66
67/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +000068static int
paul718e3742002-12-13 20:15:29 +000069kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family)
70
71{
hassofa2b17e2004-03-04 17:45:00 +000072 struct sockaddr_in *mask = NULL;
paul718e3742002-12-13 20:15:29 +000073 struct sockaddr_in sin_dest, sin_mask, sin_gate;
74 struct nexthop *nexthop;
75 int nexthop_num = 0;
76 unsigned int ifindex = 0;
77 int gate = 0;
78 int error;
Denis Ovsienkodc958242007-08-13 16:03:06 +000079 char prefix_buf[INET_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +000080
Denis Ovsienkodc958242007-08-13 16:03:06 +000081 if (IS_ZEBRA_DEBUG_RIB)
82 inet_ntop (AF_INET, &p->u.prefix, prefix_buf, INET_ADDRSTRLEN);
paul718e3742002-12-13 20:15:29 +000083 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
84 sin_dest.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000085#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000086 sin_dest.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000087#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000088 sin_dest.sin_addr = p->u.prefix4;
89
90 memset (&sin_mask, 0, sizeof (struct sockaddr_in));
91
92 memset (&sin_gate, 0, sizeof (struct sockaddr_in));
93 sin_gate.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000094#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000095 sin_gate.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000096#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000097
98 /* Make gateway. */
99 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
100 {
101 gate = 0;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000102 char gate_buf[INET_ADDRSTRLEN] = "NULL";
paul718e3742002-12-13 20:15:29 +0000103
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000104 /*
105 * XXX We need to refrain from kernel operations in some cases,
106 * but this if statement seems overly cautious - what about
107 * other than ADD and DELETE?
108 */
paul718e3742002-12-13 20:15:29 +0000109 if ((cmd == RTM_ADD
110 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
111 || (cmd == RTM_DELETE
paul718e3742002-12-13 20:15:29 +0000112 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
paul718e3742002-12-13 20:15:29 +0000113 ))
114 {
115 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
116 {
117 if (nexthop->rtype == NEXTHOP_TYPE_IPV4 ||
118 nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
119 {
120 sin_gate.sin_addr = nexthop->rgate.ipv4;
121 gate = 1;
122 }
123 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
124 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
125 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
126 ifindex = nexthop->rifindex;
127 }
128 else
129 {
130 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
131 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
132 {
133 sin_gate.sin_addr = nexthop->gate.ipv4;
134 gate = 1;
135 }
136 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
137 || nexthop->type == NEXTHOP_TYPE_IFNAME
138 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
139 ifindex = nexthop->ifindex;
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000140 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
141 {
142 struct in_addr loopback;
143 loopback.s_addr = htonl (INADDR_LOOPBACK);
144 sin_gate.sin_addr = loopback;
145 gate = 1;
146 }
147 }
paul718e3742002-12-13 20:15:29 +0000148
paul718e3742002-12-13 20:15:29 +0000149 if (gate && p->prefixlen == 32)
150 mask = NULL;
151 else
152 {
153 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
gdt6083e1f2005-12-29 15:59:57 +0000154 sin_mask.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000155#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000156 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000157#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000158 mask = &sin_mask;
159 }
paul718e3742002-12-13 20:15:29 +0000160
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000161 error = rtm_write (cmd,
162 (union sockunion *)&sin_dest,
163 (union sockunion *)mask,
164 gate ? (union sockunion *)&sin_gate : NULL,
165 ifindex,
166 rib->flags,
167 rib->metric);
paul718e3742002-12-13 20:15:29 +0000168
Denis Ovsienkodc958242007-08-13 16:03:06 +0000169 if (IS_ZEBRA_DEBUG_RIB)
170 {
171 if (!gate)
172 {
173 zlog_debug ("%s: %s/%d: attention! gate not found for rib %p",
174 __func__, prefix_buf, p->prefixlen, rib);
175 rib_dump (__func__, (struct prefix_ipv4 *)p, rib);
176 }
177 else
178 inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN);
179 }
180
181 switch (error)
182 {
183 /* We only flag nexthops as being in FIB if rtm_write() did its work. */
184 case ZEBRA_ERR_NOERROR:
185 nexthop_num++;
186 if (IS_ZEBRA_DEBUG_RIB)
187 zlog_debug ("%s: %s/%d: successfully did NH %s",
188 __func__, prefix_buf, p->prefixlen, gate_buf);
189 if (cmd == RTM_ADD)
190 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
191 break;
192
193 /* The only valid case for this error is kernel's failure to install
194 * a multipath route, which is common for FreeBSD. This should be
195 * ignored silently, but logged as an error otherwise.
196 */
197 case ZEBRA_ERR_RTEXIST:
198 if (cmd != RTM_ADD)
199 zlog_err ("%s: rtm_write() returned %d for command %d",
200 __func__, error, cmd);
201 continue;
202 break;
203
204 /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
205 * normal to get any other messages in ANY case.
206 */
207 case ZEBRA_ERR_RTNOEXIST:
208 case ZEBRA_ERR_RTUNREACH:
209 default:
210 zlog_err ("%s: %s/%d: rtm_write() unexpectedly returned %d for command %s",
211 __func__, prefix_buf, p->prefixlen, error, LOOKUP (rtm_type_str, cmd));
212 break;
213 }
214 } /* if (cmd and flags make sense) */
215 else
216 if (IS_ZEBRA_DEBUG_RIB)
217 zlog_debug ("%s: odd command %s for flags %d",
218 __func__, LOOKUP (rtm_type_str, cmd), nexthop->flags);
219 } /* for (nexthop = ... */
220
221 /* If there was no useful nexthop, then complain. */
222 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
223 zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib);
paul718e3742002-12-13 20:15:29 +0000224
225 return 0; /*XXX*/
226}
227
228int
229kernel_add_ipv4 (struct prefix *p, struct rib *rib)
230{
pauledd7c242003-06-04 13:59:38 +0000231 int route;
232
233 if (zserv_privs.change(ZPRIVS_RAISE))
234 zlog (NULL, LOG_ERR, "Can't raise privileges");
235 route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET);
236 if (zserv_privs.change(ZPRIVS_LOWER))
237 zlog (NULL, LOG_ERR, "Can't lower privileges");
238
239 return route;
paul718e3742002-12-13 20:15:29 +0000240}
241
242int
243kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
244{
pauledd7c242003-06-04 13:59:38 +0000245 int route;
246
247 if (zserv_privs.change(ZPRIVS_RAISE))
248 zlog (NULL, LOG_ERR, "Can't raise privileges");
249 route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET);
250 if (zserv_privs.change(ZPRIVS_LOWER))
251 zlog (NULL, LOG_ERR, "Can't lower privileges");
252
253 return route;
paul718e3742002-12-13 20:15:29 +0000254}
255
256#ifdef HAVE_IPV6
257
258/* Calculate sin6_len value for netmask socket value. */
paul6621ca82005-11-23 13:02:08 +0000259static int
paul718e3742002-12-13 20:15:29 +0000260sin6_masklen (struct in6_addr mask)
261{
262 struct sockaddr_in6 sin6;
263 char *p, *lim;
264 int len;
265
266#if defined (INRIA)
267 if (IN_ANYADDR6 (mask))
268 return sizeof (long);
269#else /* ! INRIA */
270 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
271 return sizeof (long);
272#endif /* ! INRIA */
273
274 sin6.sin6_addr = mask;
275 len = sizeof (struct sockaddr_in6);
276
277 lim = (char *) & sin6.sin6_addr;
278 p = lim + sizeof (sin6.sin6_addr);
279
280 while (*--p == 0 && p >= lim)
281 len--;
282
283 return len;
284}
285
286/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +0000287static int
paul718e3742002-12-13 20:15:29 +0000288kernel_rtm_ipv6 (int message, struct prefix_ipv6 *dest,
289 struct in6_addr *gate, int index, int flags)
290{
291 struct sockaddr_in6 *mask;
292 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
293
294 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
295 sin_dest.sin6_family = AF_INET6;
296#ifdef SIN6_LEN
297 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
298#endif /* SIN6_LEN */
299
300 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
301
302 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
303 sin_gate.sin6_family = AF_INET6;
304#ifdef SIN6_LEN
305 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
306#endif /* SIN6_LEN */
307
308 sin_dest.sin6_addr = dest->prefix;
309
310 if (gate)
311 memcpy (&sin_gate.sin6_addr, gate, sizeof (struct in6_addr));
312
313 /* Under kame set interface index to link local address. */
314#ifdef KAME
315
316#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
317 do { \
318 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
319 (a).s6_addr[3] = (i) & 0xff; \
320 } while (0)
321
322 if (gate && IN6_IS_ADDR_LINKLOCAL(gate))
323 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, index);
324#endif /* KAME */
325
326 if (gate && dest->prefixlen == 128)
327 mask = NULL;
328 else
329 {
330 masklen2ip6 (dest->prefixlen, &sin_mask.sin6_addr);
paul6fe70d12005-11-12 22:55:10 +0000331 sin_mask.sin6_family = AF_INET6;
paul718e3742002-12-13 20:15:29 +0000332#ifdef SIN6_LEN
333 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
334#endif /* SIN6_LEN */
335 mask = &sin_mask;
336 }
337
338 return rtm_write (message,
339 (union sockunion *) &sin_dest,
340 (union sockunion *) mask,
341 gate ? (union sockunion *)&sin_gate : NULL,
342 index,
343 flags,
344 0);
345}
346
347/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +0000348static int
paul718e3742002-12-13 20:15:29 +0000349kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
350 int family)
351{
352 struct sockaddr_in6 *mask;
353 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
354 struct nexthop *nexthop;
355 int nexthop_num = 0;
356 unsigned int ifindex = 0;
357 int gate = 0;
358 int error;
359
360 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
361 sin_dest.sin6_family = AF_INET6;
362#ifdef SIN6_LEN
363 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
364#endif /* SIN6_LEN */
365 sin_dest.sin6_addr = p->u.prefix6;
366
367 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
368
369 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
370 sin_gate.sin6_family = AF_INET6;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000371#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000372 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000373#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000374
375 /* Make gateway. */
376 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
377 {
378 gate = 0;
379
380 if ((cmd == RTM_ADD
381 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
382 || (cmd == RTM_DELETE
383#if 0
384 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
385#endif
386 ))
387 {
388 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
389 {
390 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
391 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
392 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
393 {
394 sin_gate.sin6_addr = nexthop->rgate.ipv6;
395 gate = 1;
396 }
397 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
398 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
399 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
400 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
401 ifindex = nexthop->rifindex;
402 }
403 else
404 {
405 if (nexthop->type == NEXTHOP_TYPE_IPV6
406 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
407 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
408 {
409 sin_gate.sin6_addr = nexthop->gate.ipv6;
410 gate = 1;
411 }
412 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
413 || nexthop->type == NEXTHOP_TYPE_IFNAME
414 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
415 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
416 ifindex = nexthop->ifindex;
417 }
418
419 if (cmd == RTM_ADD)
420 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
421 }
422
423 /* Under kame set interface index to link local address. */
424#ifdef KAME
425
426#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
427 do { \
428 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
429 (a).s6_addr[3] = (i) & 0xff; \
430 } while (0)
431
432 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
433 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
434#endif /* KAME */
435
436 if (gate && p->prefixlen == 128)
437 mask = NULL;
438 else
439 {
440 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
paul6fe70d12005-11-12 22:55:10 +0000441 sin_mask.sin6_family = AF_INET6;
paul718e3742002-12-13 20:15:29 +0000442#ifdef SIN6_LEN
443 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
444#endif /* SIN6_LEN */
445 mask = &sin_mask;
446 }
447
448 error = rtm_write (cmd,
449 (union sockunion *) &sin_dest,
450 (union sockunion *) mask,
451 gate ? (union sockunion *)&sin_gate : NULL,
452 ifindex,
453 rib->flags,
454 rib->metric);
455
456#if 0
457 if (error)
458 {
459 zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
460 nexthop_num, error);
461 }
462#endif
463
464 nexthop_num++;
465 }
466
467 /* If there is no useful nexthop then return. */
468 if (nexthop_num == 0)
469 {
470 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000471 zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +0000472 return 0;
473 }
474
475 return 0; /*XXX*/
476}
477
478int
479kernel_add_ipv6 (struct prefix *p, struct rib *rib)
480{
pauledd7c242003-06-04 13:59:38 +0000481 int route;
482
483 if (zserv_privs.change(ZPRIVS_RAISE))
484 zlog (NULL, LOG_ERR, "Can't raise privileges");
485 route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6);
486 if (zserv_privs.change(ZPRIVS_LOWER))
487 zlog (NULL, LOG_ERR, "Can't lower privileges");
488
489 return route;
paul718e3742002-12-13 20:15:29 +0000490}
491
492int
493kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
494{
pauledd7c242003-06-04 13:59:38 +0000495 int route;
496
497 if (zserv_privs.change(ZPRIVS_RAISE))
498 zlog (NULL, LOG_ERR, "Can't raise privileges");
499 route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
500 if (zserv_privs.change(ZPRIVS_LOWER))
501 zlog (NULL, LOG_ERR, "Can't lower privileges");
502
503 return route;
paul718e3742002-12-13 20:15:29 +0000504}
505
506/* Delete IPv6 route from the kernel. */
507int
508kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
paul6621ca82005-11-23 13:02:08 +0000509 unsigned int index, int flags, int table)
paul718e3742002-12-13 20:15:29 +0000510{
pauledd7c242003-06-04 13:59:38 +0000511 int route;
512
513 if (zserv_privs.change(ZPRIVS_RAISE))
514 zlog (NULL, LOG_ERR, "Can't raise privileges");
515 route = kernel_rtm_ipv6 (RTM_DELETE, dest, gate, index, flags);
516 if (zserv_privs.change(ZPRIVS_LOWER))
517 zlog (NULL, LOG_ERR, "Can't lower privileges");
518
519 return route;
paul718e3742002-12-13 20:15:29 +0000520}
521#endif /* HAVE_IPV6 */