blob: d8c947c67d6cb1bec9e7dea229a6d47330a10df9 [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;
Christian Frankefa713d92013-07-05 15:35:37 +000074 struct nexthop *nexthop, *tnexthop;
75 int recursing;
paul718e3742002-12-13 20:15:29 +000076 int nexthop_num = 0;
77 unsigned int ifindex = 0;
78 int gate = 0;
79 int error;
Denis Ovsienkodc958242007-08-13 16:03:06 +000080 char prefix_buf[INET_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +000081
Denis Ovsienkodc958242007-08-13 16:03:06 +000082 if (IS_ZEBRA_DEBUG_RIB)
83 inet_ntop (AF_INET, &p->u.prefix, prefix_buf, INET_ADDRSTRLEN);
paul718e3742002-12-13 20:15:29 +000084 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
85 sin_dest.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000086#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000087 sin_dest.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000088#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000089 sin_dest.sin_addr = p->u.prefix4;
90
91 memset (&sin_mask, 0, sizeof (struct sockaddr_in));
92
93 memset (&sin_gate, 0, sizeof (struct sockaddr_in));
94 sin_gate.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000095#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000096 sin_gate.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000097#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000098
99 /* Make gateway. */
Christian Frankefa713d92013-07-05 15:35:37 +0000100 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +0000101 {
Christian Frankefa713d92013-07-05 15:35:37 +0000102 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
103 continue;
104
paul718e3742002-12-13 20:15:29 +0000105 gate = 0;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000106 char gate_buf[INET_ADDRSTRLEN] = "NULL";
paul718e3742002-12-13 20:15:29 +0000107
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000108 /*
109 * XXX We need to refrain from kernel operations in some cases,
110 * but this if statement seems overly cautious - what about
111 * other than ADD and DELETE?
112 */
paul718e3742002-12-13 20:15:29 +0000113 if ((cmd == RTM_ADD
114 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
115 || (cmd == RTM_DELETE
paul718e3742002-12-13 20:15:29 +0000116 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
paul718e3742002-12-13 20:15:29 +0000117 ))
118 {
Christian Frankefa713d92013-07-05 15:35:37 +0000119 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
120 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
paul718e3742002-12-13 20:15:29 +0000121 {
Christian Frankefa713d92013-07-05 15:35:37 +0000122 sin_gate.sin_addr = nexthop->gate.ipv4;
123 gate = 1;
paul718e3742002-12-13 20:15:29 +0000124 }
Christian Frankefa713d92013-07-05 15:35:37 +0000125 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
126 || nexthop->type == NEXTHOP_TYPE_IFNAME
127 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
128 ifindex = nexthop->ifindex;
129 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
paul718e3742002-12-13 20:15:29 +0000130 {
Christian Frankefa713d92013-07-05 15:35:37 +0000131 struct in_addr loopback;
132 loopback.s_addr = htonl (INADDR_LOOPBACK);
133 sin_gate.sin_addr = loopback;
134 gate = 1;
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000135 }
paul718e3742002-12-13 20:15:29 +0000136
paul718e3742002-12-13 20:15:29 +0000137 if (gate && p->prefixlen == 32)
138 mask = NULL;
139 else
140 {
141 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
gdt6083e1f2005-12-29 15:59:57 +0000142 sin_mask.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000143#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000144 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000145#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000146 mask = &sin_mask;
147 }
paul718e3742002-12-13 20:15:29 +0000148
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000149 error = rtm_write (cmd,
150 (union sockunion *)&sin_dest,
151 (union sockunion *)mask,
152 gate ? (union sockunion *)&sin_gate : NULL,
153 ifindex,
154 rib->flags,
155 rib->metric);
paul718e3742002-12-13 20:15:29 +0000156
Denis Ovsienkodc958242007-08-13 16:03:06 +0000157 if (IS_ZEBRA_DEBUG_RIB)
158 {
159 if (!gate)
160 {
161 zlog_debug ("%s: %s/%d: attention! gate not found for rib %p",
162 __func__, prefix_buf, p->prefixlen, rib);
David Lamparterf7bf4152013-10-22 17:10:21 +0000163 rib_dump (p, rib);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000164 }
165 else
166 inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN);
167 }
168
169 switch (error)
170 {
171 /* We only flag nexthops as being in FIB if rtm_write() did its work. */
172 case ZEBRA_ERR_NOERROR:
173 nexthop_num++;
174 if (IS_ZEBRA_DEBUG_RIB)
175 zlog_debug ("%s: %s/%d: successfully did NH %s",
176 __func__, prefix_buf, p->prefixlen, gate_buf);
177 if (cmd == RTM_ADD)
178 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
179 break;
180
181 /* The only valid case for this error is kernel's failure to install
182 * a multipath route, which is common for FreeBSD. This should be
183 * ignored silently, but logged as an error otherwise.
184 */
185 case ZEBRA_ERR_RTEXIST:
186 if (cmd != RTM_ADD)
187 zlog_err ("%s: rtm_write() returned %d for command %d",
188 __func__, error, cmd);
189 continue;
190 break;
191
192 /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
193 * normal to get any other messages in ANY case.
194 */
195 case ZEBRA_ERR_RTNOEXIST:
196 case ZEBRA_ERR_RTUNREACH:
197 default:
Denis Ovsienkobd6c86d2007-09-12 15:24:27 +0000198 /* This point is reachable regardless of debugging mode. */
199 if (!IS_ZEBRA_DEBUG_RIB)
200 inet_ntop (AF_INET, &p->u.prefix, prefix_buf, INET_ADDRSTRLEN);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000201 zlog_err ("%s: %s/%d: rtm_write() unexpectedly returned %d for command %s",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000202 __func__, prefix_buf, p->prefixlen, error, lookup (rtm_type_str, cmd));
Denis Ovsienkodc958242007-08-13 16:03:06 +0000203 break;
204 }
205 } /* if (cmd and flags make sense) */
206 else
207 if (IS_ZEBRA_DEBUG_RIB)
208 zlog_debug ("%s: odd command %s for flags %d",
Denis Ovsienko2d844522007-09-14 11:31:55 +0000209 __func__, lookup (rtm_type_str, cmd), nexthop->flags);
Christian Frankefa713d92013-07-05 15:35:37 +0000210 } /* for (ALL_NEXTHOPS_RO(...))*/
Denis Ovsienkodc958242007-08-13 16:03:06 +0000211
212 /* If there was no useful nexthop, then complain. */
213 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
214 zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib);
paul718e3742002-12-13 20:15:29 +0000215
216 return 0; /*XXX*/
217}
218
219int
220kernel_add_ipv4 (struct prefix *p, struct rib *rib)
221{
pauledd7c242003-06-04 13:59:38 +0000222 int route;
223
224 if (zserv_privs.change(ZPRIVS_RAISE))
225 zlog (NULL, LOG_ERR, "Can't raise privileges");
226 route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET);
227 if (zserv_privs.change(ZPRIVS_LOWER))
228 zlog (NULL, LOG_ERR, "Can't lower privileges");
229
230 return route;
paul718e3742002-12-13 20:15:29 +0000231}
232
233int
234kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
235{
pauledd7c242003-06-04 13:59:38 +0000236 int route;
237
238 if (zserv_privs.change(ZPRIVS_RAISE))
239 zlog (NULL, LOG_ERR, "Can't raise privileges");
240 route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET);
241 if (zserv_privs.change(ZPRIVS_LOWER))
242 zlog (NULL, LOG_ERR, "Can't lower privileges");
243
244 return route;
paul718e3742002-12-13 20:15:29 +0000245}
246
247#ifdef HAVE_IPV6
248
249/* Calculate sin6_len value for netmask socket value. */
paul6621ca82005-11-23 13:02:08 +0000250static int
paul718e3742002-12-13 20:15:29 +0000251sin6_masklen (struct in6_addr mask)
252{
253 struct sockaddr_in6 sin6;
254 char *p, *lim;
255 int len;
256
paul718e3742002-12-13 20:15:29 +0000257 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
258 return sizeof (long);
paul718e3742002-12-13 20:15:29 +0000259
260 sin6.sin6_addr = mask;
261 len = sizeof (struct sockaddr_in6);
262
263 lim = (char *) & sin6.sin6_addr;
264 p = lim + sizeof (sin6.sin6_addr);
265
266 while (*--p == 0 && p >= lim)
267 len--;
268
269 return len;
270}
271
272/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +0000273static int
paul718e3742002-12-13 20:15:29 +0000274kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
275 int family)
276{
277 struct sockaddr_in6 *mask;
278 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
Christian Frankefa713d92013-07-05 15:35:37 +0000279 struct nexthop *nexthop, *tnexthop;
280 int recursing;
paul718e3742002-12-13 20:15:29 +0000281 int nexthop_num = 0;
282 unsigned int ifindex = 0;
283 int gate = 0;
284 int error;
285
286 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
287 sin_dest.sin6_family = AF_INET6;
288#ifdef SIN6_LEN
289 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
290#endif /* SIN6_LEN */
291 sin_dest.sin6_addr = p->u.prefix6;
292
293 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
294
295 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
296 sin_gate.sin6_family = AF_INET6;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000297#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000298 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000299#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000300
301 /* Make gateway. */
Christian Frankefa713d92013-07-05 15:35:37 +0000302 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +0000303 {
Christian Frankefa713d92013-07-05 15:35:37 +0000304 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
305 continue;
306
paul718e3742002-12-13 20:15:29 +0000307 gate = 0;
308
309 if ((cmd == RTM_ADD
310 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
311 || (cmd == RTM_DELETE
312#if 0
313 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
314#endif
315 ))
316 {
Christian Frankefa713d92013-07-05 15:35:37 +0000317 if (nexthop->type == NEXTHOP_TYPE_IPV6
318 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
319 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
paul718e3742002-12-13 20:15:29 +0000320 {
Christian Frankefa713d92013-07-05 15:35:37 +0000321 sin_gate.sin6_addr = nexthop->gate.ipv6;
322 gate = 1;
paul718e3742002-12-13 20:15:29 +0000323 }
Christian Frankefa713d92013-07-05 15:35:37 +0000324 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
325 || nexthop->type == NEXTHOP_TYPE_IFNAME
326 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
327 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
328 ifindex = nexthop->ifindex;
paul718e3742002-12-13 20:15:29 +0000329
330 if (cmd == RTM_ADD)
331 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
332 }
333
334 /* Under kame set interface index to link local address. */
335#ifdef KAME
336
337#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
338 do { \
339 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
340 (a).s6_addr[3] = (i) & 0xff; \
341 } while (0)
342
343 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
344 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
345#endif /* KAME */
346
347 if (gate && p->prefixlen == 128)
348 mask = NULL;
349 else
350 {
351 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
paul6fe70d12005-11-12 22:55:10 +0000352 sin_mask.sin6_family = AF_INET6;
paul718e3742002-12-13 20:15:29 +0000353#ifdef SIN6_LEN
354 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
355#endif /* SIN6_LEN */
356 mask = &sin_mask;
357 }
358
359 error = rtm_write (cmd,
360 (union sockunion *) &sin_dest,
361 (union sockunion *) mask,
362 gate ? (union sockunion *)&sin_gate : NULL,
363 ifindex,
364 rib->flags,
365 rib->metric);
366
367#if 0
368 if (error)
369 {
370 zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
371 nexthop_num, error);
372 }
373#endif
374
375 nexthop_num++;
376 }
377
378 /* If there is no useful nexthop then return. */
379 if (nexthop_num == 0)
380 {
381 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000382 zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +0000383 return 0;
384 }
385
386 return 0; /*XXX*/
387}
388
389int
390kernel_add_ipv6 (struct prefix *p, struct rib *rib)
391{
pauledd7c242003-06-04 13:59:38 +0000392 int route;
393
394 if (zserv_privs.change(ZPRIVS_RAISE))
395 zlog (NULL, LOG_ERR, "Can't raise privileges");
396 route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6);
397 if (zserv_privs.change(ZPRIVS_LOWER))
398 zlog (NULL, LOG_ERR, "Can't lower privileges");
399
400 return route;
paul718e3742002-12-13 20:15:29 +0000401}
402
403int
404kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
405{
pauledd7c242003-06-04 13:59:38 +0000406 int route;
407
408 if (zserv_privs.change(ZPRIVS_RAISE))
409 zlog (NULL, LOG_ERR, "Can't raise privileges");
410 route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
411 if (zserv_privs.change(ZPRIVS_LOWER))
412 zlog (NULL, LOG_ERR, "Can't lower privileges");
413
414 return route;
paul718e3742002-12-13 20:15:29 +0000415}
paul718e3742002-12-13 20:15:29 +0000416#endif /* HAVE_IPV6 */