blob: 9dd45823fdbd5e638709b2c276e07152aa91ab01 [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
David Lamparter8fa1d022015-09-15 21:55:38 -070044#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000045/* Adjust netmask socket length. Return value is a adjusted sin_len
46 value. */
paul6621ca82005-11-23 13:02:08 +000047static int
paul718e3742002-12-13 20:15:29 +000048sin_masklen (struct in_addr mask)
49{
50 char *p, *lim;
51 int len;
52 struct sockaddr_in sin;
53
54 if (mask.s_addr == 0)
55 return sizeof (long);
56
57 sin.sin_addr = mask;
58 len = sizeof (struct sockaddr_in);
59
60 lim = (char *) &sin.sin_addr;
61 p = lim + sizeof (sin.sin_addr);
62
63 while (*--p == 0 && p >= lim)
64 len--;
65 return len;
66}
David Lamparter8fa1d022015-09-15 21:55:38 -070067#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000068
69/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +000070static int
Timo Teräsd849e232016-02-18 18:19:54 -080071kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib)
paul718e3742002-12-13 20:15:29 +000072
73{
hassofa2b17e2004-03-04 17:45:00 +000074 struct sockaddr_in *mask = NULL;
paul718e3742002-12-13 20:15:29 +000075 struct sockaddr_in sin_dest, sin_mask, sin_gate;
Christian Frankefa713d92013-07-05 15:35:37 +000076 struct nexthop *nexthop, *tnexthop;
77 int recursing;
paul718e3742002-12-13 20:15:29 +000078 int nexthop_num = 0;
Paul Jakma9099f9b2016-01-18 10:12:10 +000079 ifindex_t ifindex = 0;
paul718e3742002-12-13 20:15:29 +000080 int gate = 0;
81 int error;
Timo Teräsbe6335d2015-05-23 11:08:41 +030082 char prefix_buf[PREFIX_STRLEN];
paul718e3742002-12-13 20:15:29 +000083
Denis Ovsienkodc958242007-08-13 16:03:06 +000084 if (IS_ZEBRA_DEBUG_RIB)
Timo Teräsbe6335d2015-05-23 11:08:41 +030085 prefix2str (p, prefix_buf, sizeof(prefix_buf));
paul718e3742002-12-13 20:15:29 +000086 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
87 sin_dest.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000088#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000089 sin_dest.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000090#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +000091 sin_dest.sin_addr = p->u.prefix4;
92
93 memset (&sin_mask, 0, sizeof (struct sockaddr_in));
94
95 memset (&sin_gate, 0, sizeof (struct sockaddr_in));
96 sin_gate.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +000097#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +000098 sin_gate.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +000099#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000100
101 /* Make gateway. */
Christian Frankefa713d92013-07-05 15:35:37 +0000102 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +0000103 {
Christian Frankefa713d92013-07-05 15:35:37 +0000104 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
105 continue;
106
paul718e3742002-12-13 20:15:29 +0000107 gate = 0;
Denis Ovsienkodc958242007-08-13 16:03:06 +0000108 char gate_buf[INET_ADDRSTRLEN] = "NULL";
paul718e3742002-12-13 20:15:29 +0000109
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000110 /*
111 * XXX We need to refrain from kernel operations in some cases,
112 * but this if statement seems overly cautious - what about
113 * other than ADD and DELETE?
114 */
paul718e3742002-12-13 20:15:29 +0000115 if ((cmd == RTM_ADD
116 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
117 || (cmd == RTM_DELETE
paul718e3742002-12-13 20:15:29 +0000118 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
paul718e3742002-12-13 20:15:29 +0000119 ))
120 {
Christian Frankefa713d92013-07-05 15:35:37 +0000121 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
122 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
paul718e3742002-12-13 20:15:29 +0000123 {
Christian Frankefa713d92013-07-05 15:35:37 +0000124 sin_gate.sin_addr = nexthop->gate.ipv4;
125 gate = 1;
paul718e3742002-12-13 20:15:29 +0000126 }
Christian Frankefa713d92013-07-05 15:35:37 +0000127 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
128 || nexthop->type == NEXTHOP_TYPE_IFNAME
129 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
130 ifindex = nexthop->ifindex;
131 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
paul718e3742002-12-13 20:15:29 +0000132 {
Christian Frankefa713d92013-07-05 15:35:37 +0000133 struct in_addr loopback;
134 loopback.s_addr = htonl (INADDR_LOOPBACK);
135 sin_gate.sin_addr = loopback;
136 gate = 1;
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000137 }
paul718e3742002-12-13 20:15:29 +0000138
paul718e3742002-12-13 20:15:29 +0000139 if (gate && p->prefixlen == 32)
140 mask = NULL;
141 else
142 {
143 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
gdt6083e1f2005-12-29 15:59:57 +0000144 sin_mask.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000145#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000146 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000147#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000148 mask = &sin_mask;
149 }
paul718e3742002-12-13 20:15:29 +0000150
Greg Troxeldfdb8f12007-08-02 14:13:56 +0000151 error = rtm_write (cmd,
152 (union sockunion *)&sin_dest,
153 (union sockunion *)mask,
154 gate ? (union sockunion *)&sin_gate : NULL,
155 ifindex,
156 rib->flags,
157 rib->metric);
paul718e3742002-12-13 20:15:29 +0000158
Denis Ovsienkodc958242007-08-13 16:03:06 +0000159 if (IS_ZEBRA_DEBUG_RIB)
160 {
161 if (!gate)
162 {
Timo Teräsbe6335d2015-05-23 11:08:41 +0300163 zlog_debug ("%s: %s: attention! gate not found for rib %p",
164 __func__, prefix_buf, rib);
David Lamparterf7bf4152013-10-22 17:10:21 +0000165 rib_dump (p, rib);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000166 }
167 else
168 inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN);
169 }
170
171 switch (error)
172 {
173 /* We only flag nexthops as being in FIB if rtm_write() did its work. */
174 case ZEBRA_ERR_NOERROR:
175 nexthop_num++;
176 if (IS_ZEBRA_DEBUG_RIB)
Timo Teräsbe6335d2015-05-23 11:08:41 +0300177 zlog_debug ("%s: %s: successfully did NH %s",
178 __func__, prefix_buf, gate_buf);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000179 if (cmd == RTM_ADD)
180 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
181 break;
182
183 /* The only valid case for this error is kernel's failure to install
184 * a multipath route, which is common for FreeBSD. This should be
185 * ignored silently, but logged as an error otherwise.
186 */
187 case ZEBRA_ERR_RTEXIST:
188 if (cmd != RTM_ADD)
189 zlog_err ("%s: rtm_write() returned %d for command %d",
190 __func__, error, cmd);
191 continue;
192 break;
193
194 /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
195 * normal to get any other messages in ANY case.
196 */
197 case ZEBRA_ERR_RTNOEXIST:
198 case ZEBRA_ERR_RTUNREACH:
199 default:
Timo Teräsbe6335d2015-05-23 11:08:41 +0300200 zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s",
201 __func__, prefix2str(p, prefix_buf, sizeof(prefix_buf)),
202 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
paul718e3742002-12-13 20:15:29 +0000219#ifdef HAVE_IPV6
220
David Lamparter8fa1d022015-09-15 21:55:38 -0700221#ifdef SIN6_LEN
paul718e3742002-12-13 20:15:29 +0000222/* Calculate sin6_len value for netmask socket value. */
paul6621ca82005-11-23 13:02:08 +0000223static int
paul718e3742002-12-13 20:15:29 +0000224sin6_masklen (struct in6_addr mask)
225{
226 struct sockaddr_in6 sin6;
227 char *p, *lim;
228 int len;
229
paul718e3742002-12-13 20:15:29 +0000230 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
231 return sizeof (long);
paul718e3742002-12-13 20:15:29 +0000232
233 sin6.sin6_addr = mask;
234 len = sizeof (struct sockaddr_in6);
235
236 lim = (char *) & sin6.sin6_addr;
237 p = lim + sizeof (sin6.sin6_addr);
238
239 while (*--p == 0 && p >= lim)
240 len--;
241
242 return len;
243}
David Lamparter8fa1d022015-09-15 21:55:38 -0700244#endif /* SIN6_LEN */
paul718e3742002-12-13 20:15:29 +0000245
246/* Interface between zebra message and rtm message. */
paul6621ca82005-11-23 13:02:08 +0000247static int
Timo Teräsd849e232016-02-18 18:19:54 -0800248kernel_rtm_ipv6 (int cmd, struct prefix *p, struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000249{
250 struct sockaddr_in6 *mask;
251 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
Christian Frankefa713d92013-07-05 15:35:37 +0000252 struct nexthop *nexthop, *tnexthop;
253 int recursing;
paul718e3742002-12-13 20:15:29 +0000254 int nexthop_num = 0;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000255 ifindex_t ifindex = 0;
paul718e3742002-12-13 20:15:29 +0000256 int gate = 0;
257 int error;
258
259 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
260 sin_dest.sin6_family = AF_INET6;
261#ifdef SIN6_LEN
262 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
263#endif /* SIN6_LEN */
264 sin_dest.sin6_addr = p->u.prefix6;
265
266 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
267
268 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
269 sin_gate.sin6_family = AF_INET6;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000270#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000271 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000272#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000273
274 /* Make gateway. */
Christian Frankefa713d92013-07-05 15:35:37 +0000275 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
paul718e3742002-12-13 20:15:29 +0000276 {
Christian Frankefa713d92013-07-05 15:35:37 +0000277 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
278 continue;
279
paul718e3742002-12-13 20:15:29 +0000280 gate = 0;
281
282 if ((cmd == RTM_ADD
283 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
284 || (cmd == RTM_DELETE
285#if 0
286 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
287#endif
288 ))
289 {
Christian Frankefa713d92013-07-05 15:35:37 +0000290 if (nexthop->type == NEXTHOP_TYPE_IPV6
291 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
292 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
paul718e3742002-12-13 20:15:29 +0000293 {
Christian Frankefa713d92013-07-05 15:35:37 +0000294 sin_gate.sin6_addr = nexthop->gate.ipv6;
295 gate = 1;
paul718e3742002-12-13 20:15:29 +0000296 }
Christian Frankefa713d92013-07-05 15:35:37 +0000297 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
298 || nexthop->type == NEXTHOP_TYPE_IFNAME
299 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
300 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
301 ifindex = nexthop->ifindex;
paul718e3742002-12-13 20:15:29 +0000302
303 if (cmd == RTM_ADD)
304 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
305 }
306
307 /* Under kame set interface index to link local address. */
308#ifdef KAME
309
310#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
311 do { \
312 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
313 (a).s6_addr[3] = (i) & 0xff; \
314 } while (0)
315
316 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
317 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
318#endif /* KAME */
319
320 if (gate && p->prefixlen == 128)
321 mask = NULL;
322 else
323 {
324 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
paul6fe70d12005-11-12 22:55:10 +0000325 sin_mask.sin6_family = AF_INET6;
paul718e3742002-12-13 20:15:29 +0000326#ifdef SIN6_LEN
327 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
328#endif /* SIN6_LEN */
329 mask = &sin_mask;
330 }
331
332 error = rtm_write (cmd,
333 (union sockunion *) &sin_dest,
334 (union sockunion *) mask,
335 gate ? (union sockunion *)&sin_gate : NULL,
336 ifindex,
337 rib->flags,
338 rib->metric);
339
340#if 0
341 if (error)
342 {
Timo Teräs0abf6792016-01-15 17:36:29 +0200343 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
paul718e3742002-12-13 20:15:29 +0000344 nexthop_num, error);
345 }
David Lamparter8fa1d022015-09-15 21:55:38 -0700346#else
347 (void)error;
paul718e3742002-12-13 20:15:29 +0000348#endif
349
350 nexthop_num++;
351 }
352
353 /* If there is no useful nexthop then return. */
354 if (nexthop_num == 0)
355 {
356 if (IS_ZEBRA_DEBUG_KERNEL)
Timo Teräs0abf6792016-01-15 17:36:29 +0200357 zlog_debug ("kernel_rtm_ipv6(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +0000358 return 0;
359 }
360
361 return 0; /*XXX*/
362}
363
Timo Teräs0abf6792016-01-15 17:36:29 +0200364#endif
365
Timo Teräsd849e232016-02-18 18:19:54 -0800366static int
367kernel_rtm (int cmd, struct prefix *p, struct rib *rib)
368{
369 switch (PREFIX_FAMILY(p))
370 {
371 case AF_INET:
372 return kernel_rtm_ipv4 (cmd, p, rib);
373 case AF_INET6:
374 return kernel_rtm_ipv6 (cmd, p, rib);
375 }
376 return 0;
377}
378
paul718e3742002-12-13 20:15:29 +0000379int
Timo Teräs0abf6792016-01-15 17:36:29 +0200380kernel_route_rib (struct prefix *p, struct rib *old, struct rib *new)
paul718e3742002-12-13 20:15:29 +0000381{
Timo Teräsd849e232016-02-18 18:19:54 -0800382 int route = 0;
pauledd7c242003-06-04 13:59:38 +0000383
384 if (zserv_privs.change(ZPRIVS_RAISE))
385 zlog (NULL, LOG_ERR, "Can't raise privileges");
Timo Teräs0abf6792016-01-15 17:36:29 +0200386
Timo Teräsd849e232016-02-18 18:19:54 -0800387 if (old)
388 route |= kernel_rtm (RTM_DELETE, p, old);
389
390 if (new)
391 route |= kernel_rtm (RTM_ADD, p, new);
Timo Teräs0abf6792016-01-15 17:36:29 +0200392
pauledd7c242003-06-04 13:59:38 +0000393 if (zserv_privs.change(ZPRIVS_LOWER))
394 zlog (NULL, LOG_ERR, "Can't lower privileges");
395
396 return route;
paul718e3742002-12-13 20:15:29 +0000397}