blob: b06c586a319cf57b070c9af077ba9158c9391145 [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"
paul718e3742002-12-13 20:15:29 +000035
pauledd7c242003-06-04 13:59:38 +000036extern struct zebra_privs_t zserv_privs;
37
paul6621ca82005-11-23 13:02:08 +000038/* kernel socket export */
39extern int rtm_write (int message, union sockunion *dest,
40 union sockunion *mask, union sockunion *gate,
41 unsigned int index, int zebra_flags, int metric);
paul718e3742002-12-13 20:15:29 +000042
43/* Adjust netmask socket length. Return value is a adjusted sin_len
44 value. */
paul6621ca82005-11-23 13:02:08 +000045static int
paul718e3742002-12-13 20:15:29 +000046sin_masklen (struct in_addr mask)
47{
48 char *p, *lim;
49 int len;
50 struct sockaddr_in sin;
51
52 if (mask.s_addr == 0)
53 return sizeof (long);
54
55 sin.sin_addr = mask;
56 len = sizeof (struct sockaddr_in);
57
58 lim = (char *) &sin.sin_addr;
59 p = lim + sizeof (sin.sin_addr);
60
61 while (*--p == 0 && p >= lim)
62 len--;
63 return len;
64}
65
66/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +000067static int
paul718e3742002-12-13 20:15:29 +000068kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family)
69
70{
hassofa2b17e2004-03-04 17:45:00 +000071 struct sockaddr_in *mask = NULL;
paul718e3742002-12-13 20:15:29 +000072 struct sockaddr_in sin_dest, sin_mask, sin_gate;
73 struct nexthop *nexthop;
74 int nexthop_num = 0;
75 unsigned int ifindex = 0;
76 int gate = 0;
77 int error;
78
79 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
80 sin_dest.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000081#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000082 sin_dest.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000083#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000084 sin_dest.sin_addr = p->u.prefix4;
85
86 memset (&sin_mask, 0, sizeof (struct sockaddr_in));
87
88 memset (&sin_gate, 0, sizeof (struct sockaddr_in));
89 sin_gate.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000090#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000091 sin_gate.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000092#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000093
94 /* Make gateway. */
95 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
96 {
97 gate = 0;
98
Greg Troxeldfdb8f12007-08-02 14:13:56 +000099 /*
100 * XXX We need to refrain from kernel operations in some cases,
101 * but this if statement seems overly cautious - what about
102 * other than ADD and DELETE?
103 */
paul718e3742002-12-13 20:15:29 +0000104 if ((cmd == RTM_ADD
105 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
106 || (cmd == RTM_DELETE
paul718e3742002-12-13 20:15:29 +0000107 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
paul718e3742002-12-13 20:15:29 +0000108 ))
109 {
110 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
111 {
112 if (nexthop->rtype == NEXTHOP_TYPE_IPV4 ||
113 nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
114 {
115 sin_gate.sin_addr = nexthop->rgate.ipv4;
116 gate = 1;
117 }
118 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
119 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
120 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
121 ifindex = nexthop->rifindex;
122 }
123 else
124 {
125 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
126 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
127 {
128 sin_gate.sin_addr = nexthop->gate.ipv4;
129 gate = 1;
130 }
131 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
132 || nexthop->type == NEXTHOP_TYPE_IFNAME
133 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
134 ifindex = nexthop->ifindex;
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000135 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
136 {
137 struct in_addr loopback;
138 loopback.s_addr = htonl (INADDR_LOOPBACK);
139 sin_gate.sin_addr = loopback;
140 gate = 1;
141 }
142 }
paul718e3742002-12-13 20:15:29 +0000143
paul718e3742002-12-13 20:15:29 +0000144 if (gate && p->prefixlen == 32)
145 mask = NULL;
146 else
147 {
148 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
gdt6083e1f2005-12-29 15:59:57 +0000149 sin_mask.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000150#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000151 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000152#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000153 mask = &sin_mask;
154 }
paul718e3742002-12-13 20:15:29 +0000155
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000156 error = rtm_write (cmd,
157 (union sockunion *)&sin_dest,
158 (union sockunion *)mask,
159 gate ? (union sockunion *)&sin_gate : NULL,
160 ifindex,
161 rib->flags,
162 rib->metric);
paul718e3742002-12-13 20:15:29 +0000163
164#if 0
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000165 if (error)
166 {
167 zlog_info ("kernel_rtm_ipv4(): nexthop %d add error=%d.",
168 nexthop_num, error);
169 }
paul718e3742002-12-13 20:15:29 +0000170#endif
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000171 if (error == 0)
172 {
173 nexthop_num++;
174 if (cmd == RTM_ADD)
175 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
176 }
Greg Troxelf76594a2007-08-02 14:07:07 +0000177 }
paul718e3742002-12-13 20:15:29 +0000178 }
179
180 /* If there is no useful nexthop then return. */
181 if (nexthop_num == 0)
182 {
183 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000184 zlog_debug ("kernel_rtm_ipv4(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +0000185 return 0;
186 }
187
188 return 0; /*XXX*/
189}
190
191int
192kernel_add_ipv4 (struct prefix *p, struct rib *rib)
193{
pauledd7c242003-06-04 13:59:38 +0000194 int route;
195
196 if (zserv_privs.change(ZPRIVS_RAISE))
197 zlog (NULL, LOG_ERR, "Can't raise privileges");
198 route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET);
199 if (zserv_privs.change(ZPRIVS_LOWER))
200 zlog (NULL, LOG_ERR, "Can't lower privileges");
201
202 return route;
paul718e3742002-12-13 20:15:29 +0000203}
204
205int
206kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
207{
pauledd7c242003-06-04 13:59:38 +0000208 int route;
209
210 if (zserv_privs.change(ZPRIVS_RAISE))
211 zlog (NULL, LOG_ERR, "Can't raise privileges");
212 route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET);
213 if (zserv_privs.change(ZPRIVS_LOWER))
214 zlog (NULL, LOG_ERR, "Can't lower privileges");
215
216 return route;
paul718e3742002-12-13 20:15:29 +0000217}
218
219#ifdef HAVE_IPV6
220
221/* Calculate sin6_len value for netmask socket value. */
paul6621ca82005-11-23 13:02:08 +0000222static int
paul718e3742002-12-13 20:15:29 +0000223sin6_masklen (struct in6_addr mask)
224{
225 struct sockaddr_in6 sin6;
226 char *p, *lim;
227 int len;
228
229#if defined (INRIA)
230 if (IN_ANYADDR6 (mask))
231 return sizeof (long);
232#else /* ! INRIA */
233 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
234 return sizeof (long);
235#endif /* ! INRIA */
236
237 sin6.sin6_addr = mask;
238 len = sizeof (struct sockaddr_in6);
239
240 lim = (char *) & sin6.sin6_addr;
241 p = lim + sizeof (sin6.sin6_addr);
242
243 while (*--p == 0 && p >= lim)
244 len--;
245
246 return len;
247}
248
249/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +0000250static int
paul718e3742002-12-13 20:15:29 +0000251kernel_rtm_ipv6 (int message, struct prefix_ipv6 *dest,
252 struct in6_addr *gate, int index, int flags)
253{
254 struct sockaddr_in6 *mask;
255 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
256
257 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
258 sin_dest.sin6_family = AF_INET6;
259#ifdef SIN6_LEN
260 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
261#endif /* SIN6_LEN */
262
263 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
264
265 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
266 sin_gate.sin6_family = AF_INET6;
267#ifdef SIN6_LEN
268 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
269#endif /* SIN6_LEN */
270
271 sin_dest.sin6_addr = dest->prefix;
272
273 if (gate)
274 memcpy (&sin_gate.sin6_addr, gate, sizeof (struct in6_addr));
275
276 /* Under kame set interface index to link local address. */
277#ifdef KAME
278
279#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
280 do { \
281 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
282 (a).s6_addr[3] = (i) & 0xff; \
283 } while (0)
284
285 if (gate && IN6_IS_ADDR_LINKLOCAL(gate))
286 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, index);
287#endif /* KAME */
288
289 if (gate && dest->prefixlen == 128)
290 mask = NULL;
291 else
292 {
293 masklen2ip6 (dest->prefixlen, &sin_mask.sin6_addr);
paul6fe70d12005-11-12 22:55:10 +0000294 sin_mask.sin6_family = AF_INET6;
paul718e3742002-12-13 20:15:29 +0000295#ifdef SIN6_LEN
296 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
297#endif /* SIN6_LEN */
298 mask = &sin_mask;
299 }
300
301 return rtm_write (message,
302 (union sockunion *) &sin_dest,
303 (union sockunion *) mask,
304 gate ? (union sockunion *)&sin_gate : NULL,
305 index,
306 flags,
307 0);
308}
309
310/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +0000311static int
paul718e3742002-12-13 20:15:29 +0000312kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
313 int family)
314{
315 struct sockaddr_in6 *mask;
316 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
317 struct nexthop *nexthop;
318 int nexthop_num = 0;
319 unsigned int ifindex = 0;
320 int gate = 0;
321 int error;
322
323 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
324 sin_dest.sin6_family = AF_INET6;
325#ifdef SIN6_LEN
326 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
327#endif /* SIN6_LEN */
328 sin_dest.sin6_addr = p->u.prefix6;
329
330 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
331
332 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
333 sin_gate.sin6_family = AF_INET6;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000334#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000335 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000336#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000337
338 /* Make gateway. */
339 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
340 {
341 gate = 0;
342
343 if ((cmd == RTM_ADD
344 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
345 || (cmd == RTM_DELETE
346#if 0
347 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
348#endif
349 ))
350 {
351 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
352 {
353 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
354 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
355 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
356 {
357 sin_gate.sin6_addr = nexthop->rgate.ipv6;
358 gate = 1;
359 }
360 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
361 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
362 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
363 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
364 ifindex = nexthop->rifindex;
365 }
366 else
367 {
368 if (nexthop->type == NEXTHOP_TYPE_IPV6
369 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
370 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
371 {
372 sin_gate.sin6_addr = nexthop->gate.ipv6;
373 gate = 1;
374 }
375 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
376 || nexthop->type == NEXTHOP_TYPE_IFNAME
377 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
378 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
379 ifindex = nexthop->ifindex;
380 }
381
382 if (cmd == RTM_ADD)
383 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
384 }
385
386 /* Under kame set interface index to link local address. */
387#ifdef KAME
388
389#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
390 do { \
391 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
392 (a).s6_addr[3] = (i) & 0xff; \
393 } while (0)
394
395 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
396 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
397#endif /* KAME */
398
399 if (gate && p->prefixlen == 128)
400 mask = NULL;
401 else
402 {
403 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
paul6fe70d12005-11-12 22:55:10 +0000404 sin_mask.sin6_family = AF_INET6;
paul718e3742002-12-13 20:15:29 +0000405#ifdef SIN6_LEN
406 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
407#endif /* SIN6_LEN */
408 mask = &sin_mask;
409 }
410
411 error = rtm_write (cmd,
412 (union sockunion *) &sin_dest,
413 (union sockunion *) mask,
414 gate ? (union sockunion *)&sin_gate : NULL,
415 ifindex,
416 rib->flags,
417 rib->metric);
418
419#if 0
420 if (error)
421 {
422 zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
423 nexthop_num, error);
424 }
425#endif
426
427 nexthop_num++;
428 }
429
430 /* If there is no useful nexthop then return. */
431 if (nexthop_num == 0)
432 {
433 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000434 zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +0000435 return 0;
436 }
437
438 return 0; /*XXX*/
439}
440
441int
442kernel_add_ipv6 (struct prefix *p, struct rib *rib)
443{
pauledd7c242003-06-04 13:59:38 +0000444 int route;
445
446 if (zserv_privs.change(ZPRIVS_RAISE))
447 zlog (NULL, LOG_ERR, "Can't raise privileges");
448 route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6);
449 if (zserv_privs.change(ZPRIVS_LOWER))
450 zlog (NULL, LOG_ERR, "Can't lower privileges");
451
452 return route;
paul718e3742002-12-13 20:15:29 +0000453}
454
455int
456kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
457{
pauledd7c242003-06-04 13:59:38 +0000458 int route;
459
460 if (zserv_privs.change(ZPRIVS_RAISE))
461 zlog (NULL, LOG_ERR, "Can't raise privileges");
462 route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
463 if (zserv_privs.change(ZPRIVS_LOWER))
464 zlog (NULL, LOG_ERR, "Can't lower privileges");
465
466 return route;
paul718e3742002-12-13 20:15:29 +0000467}
468
469/* Delete IPv6 route from the kernel. */
470int
471kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
paul6621ca82005-11-23 13:02:08 +0000472 unsigned int index, int flags, int table)
paul718e3742002-12-13 20:15:29 +0000473{
pauledd7c242003-06-04 13:59:38 +0000474 int route;
475
476 if (zserv_privs.change(ZPRIVS_RAISE))
477 zlog (NULL, LOG_ERR, "Can't raise privileges");
478 route = kernel_rtm_ipv6 (RTM_DELETE, dest, gate, index, flags);
479 if (zserv_privs.change(ZPRIVS_LOWER))
480 zlog (NULL, LOG_ERR, "Can't lower privileges");
481
482 return route;
paul718e3742002-12-13 20:15:29 +0000483}
484#endif /* HAVE_IPV6 */