]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
*: use an ifindex_t type, defined in lib/if.h, for ifindex values
[mirror_frr.git] / zebra / rt_socket.c
CommitLineData
718e3744 1/*
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"
edd7c245 30#include "privs.h"
718e3744 31
32#include "zebra/debug.h"
33#include "zebra/rib.h"
6621ca86 34#include "zebra/rt.h"
dc95824a 35#include "zebra/kernel_socket.h"
718e3744 36
edd7c245 37extern struct zebra_privs_t zserv_privs;
38
6621ca86 39/* 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);
718e3744 43
746c4f02 44#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 45/* Adjust netmask socket length. Return value is a adjusted sin_len
46 value. */
6621ca86 47static int
718e3744 48sin_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}
746c4f02 67#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 68
69/* Interface between zebra message and rtm message. */
6621ca86 70static int
718e3744 71kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family)
72
73{
fa2b17e3 74 struct sockaddr_in *mask = NULL;
718e3744 75 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fa713d9e
CF
76 struct nexthop *nexthop, *tnexthop;
77 int recursing;
718e3744 78 int nexthop_num = 0;
b892f1dd 79 ifindex_t ifindex = 0;
718e3744 80 int gate = 0;
81 int error;
35d921cc 82 char prefix_buf[PREFIX_STRLEN];
718e3744 83
dc95824a 84 if (IS_ZEBRA_DEBUG_RIB)
35d921cc 85 prefix2str (p, prefix_buf, sizeof(prefix_buf));
718e3744 86 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
87 sin_dest.sin_family = AF_INET;
6f0e3f6e 88#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 89 sin_dest.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 90#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 91 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;
6f0e3f6e 97#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 98 sin_gate.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 99#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 100
101 /* Make gateway. */
fa713d9e 102 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
718e3744 103 {
fa713d9e
CF
104 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
105 continue;
106
718e3744 107 gate = 0;
dc95824a 108 char gate_buf[INET_ADDRSTRLEN] = "NULL";
718e3744 109
dfdb8f18
GT
110 /*
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 */
718e3744 115 if ((cmd == RTM_ADD
116 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
117 || (cmd == RTM_DELETE
718e3744 118 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
718e3744 119 ))
120 {
fa713d9e
CF
121 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
122 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
718e3744 123 {
fa713d9e
CF
124 sin_gate.sin_addr = nexthop->gate.ipv4;
125 gate = 1;
718e3744 126 }
fa713d9e 127 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
fa713d9e
CF
128 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
129 ifindex = nexthop->ifindex;
130 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
718e3744 131 {
fa713d9e
CF
132 struct in_addr loopback;
133 loopback.s_addr = htonl (INADDR_LOOPBACK);
134 sin_gate.sin_addr = loopback;
135 gate = 1;
dfdb8f18 136 }
718e3744 137
718e3744 138 if (gate && p->prefixlen == 32)
139 mask = NULL;
140 else
141 {
142 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
6083e1f8 143 sin_mask.sin_family = AF_INET;
6f0e3f6e 144#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 145 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
6f0e3f6e 146#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 147 mask = &sin_mask;
148 }
718e3744 149
dfdb8f18
GT
150 error = rtm_write (cmd,
151 (union sockunion *)&sin_dest,
152 (union sockunion *)mask,
153 gate ? (union sockunion *)&sin_gate : NULL,
154 ifindex,
155 rib->flags,
156 rib->metric);
718e3744 157
dc95824a
DO
158 if (IS_ZEBRA_DEBUG_RIB)
159 {
160 if (!gate)
161 {
35d921cc
TT
162 zlog_debug ("%s: %s: attention! gate not found for rib %p",
163 __func__, prefix_buf, rib);
f7bf4153 164 rib_dump (p, rib);
dc95824a
DO
165 }
166 else
167 inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN);
168 }
169
170 switch (error)
171 {
172 /* We only flag nexthops as being in FIB if rtm_write() did its work. */
173 case ZEBRA_ERR_NOERROR:
174 nexthop_num++;
175 if (IS_ZEBRA_DEBUG_RIB)
35d921cc
TT
176 zlog_debug ("%s: %s: successfully did NH %s",
177 __func__, prefix_buf, gate_buf);
dc95824a
DO
178 if (cmd == RTM_ADD)
179 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
180 break;
181
182 /* The only valid case for this error is kernel's failure to install
183 * a multipath route, which is common for FreeBSD. This should be
184 * ignored silently, but logged as an error otherwise.
185 */
186 case ZEBRA_ERR_RTEXIST:
187 if (cmd != RTM_ADD)
188 zlog_err ("%s: rtm_write() returned %d for command %d",
189 __func__, error, cmd);
190 continue;
191 break;
192
193 /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
194 * normal to get any other messages in ANY case.
195 */
196 case ZEBRA_ERR_RTNOEXIST:
197 case ZEBRA_ERR_RTUNREACH:
198 default:
35d921cc
TT
199 zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s",
200 __func__, prefix2str(p, prefix_buf, sizeof(prefix_buf)),
201 error, lookup (rtm_type_str, cmd));
dc95824a
DO
202 break;
203 }
204 } /* if (cmd and flags make sense) */
205 else
206 if (IS_ZEBRA_DEBUG_RIB)
207 zlog_debug ("%s: odd command %s for flags %d",
2d844524 208 __func__, lookup (rtm_type_str, cmd), nexthop->flags);
fa713d9e 209 } /* for (ALL_NEXTHOPS_RO(...))*/
dc95824a
DO
210
211 /* If there was no useful nexthop, then complain. */
212 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
213 zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib);
718e3744 214
215 return 0; /*XXX*/
216}
217
218int
219kernel_add_ipv4 (struct prefix *p, struct rib *rib)
220{
edd7c245 221 int route;
222
223 if (zserv_privs.change(ZPRIVS_RAISE))
224 zlog (NULL, LOG_ERR, "Can't raise privileges");
225 route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET);
226 if (zserv_privs.change(ZPRIVS_LOWER))
227 zlog (NULL, LOG_ERR, "Can't lower privileges");
228
229 return route;
718e3744 230}
231
6ae24471
DS
232int
233kernel_update_ipv4 (struct prefix *p, struct rib *rib)
234{
235 kernel_delete_ipv4 (p, rib);
236 return kernel_add_ipv4 (p, rib);
237}
238
718e3744 239int
240kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
241{
edd7c245 242 int route;
243
244 if (zserv_privs.change(ZPRIVS_RAISE))
245 zlog (NULL, LOG_ERR, "Can't raise privileges");
246 route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET);
247 if (zserv_privs.change(ZPRIVS_LOWER))
248 zlog (NULL, LOG_ERR, "Can't lower privileges");
249
250 return route;
718e3744 251}
252
253#ifdef HAVE_IPV6
254
746c4f02 255#ifdef SIN6_LEN
718e3744 256/* Calculate sin6_len value for netmask socket value. */
6621ca86 257static int
718e3744 258sin6_masklen (struct in6_addr mask)
259{
260 struct sockaddr_in6 sin6;
261 char *p, *lim;
262 int len;
263
718e3744 264 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
265 return sizeof (long);
718e3744 266
267 sin6.sin6_addr = mask;
268 len = sizeof (struct sockaddr_in6);
269
270 lim = (char *) & sin6.sin6_addr;
271 p = lim + sizeof (sin6.sin6_addr);
272
273 while (*--p == 0 && p >= lim)
274 len--;
275
276 return len;
277}
746c4f02 278#endif /* SIN6_LEN */
718e3744 279
718e3744 280/* Interface between zebra message and rtm message. */
6621ca86 281static int
718e3744 282kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
283 int family)
284{
285 struct sockaddr_in6 *mask;
286 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
fa713d9e
CF
287 struct nexthop *nexthop, *tnexthop;
288 int recursing;
718e3744 289 int nexthop_num = 0;
b892f1dd 290 ifindex_t ifindex = 0;
718e3744 291 int gate = 0;
292 int error;
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 sin_dest.sin6_addr = p->u.prefix6;
300
301 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
302
303 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
304 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 305#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 306 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
6f0e3f6e 307#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 308
309 /* Make gateway. */
fa713d9e 310 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
718e3744 311 {
fa713d9e
CF
312 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
313 continue;
314
718e3744 315 gate = 0;
316
317 if ((cmd == RTM_ADD
318 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
319 || (cmd == RTM_DELETE
320#if 0
321 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
322#endif
323 ))
324 {
fa713d9e 325 if (nexthop->type == NEXTHOP_TYPE_IPV6
fa713d9e 326 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
718e3744 327 {
fa713d9e
CF
328 sin_gate.sin6_addr = nexthop->gate.ipv6;
329 gate = 1;
718e3744 330 }
fa713d9e 331 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
fa713d9e
CF
332 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
333 ifindex = nexthop->ifindex;
718e3744 334
335 if (cmd == RTM_ADD)
336 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
337 }
338
339 /* Under kame set interface index to link local address. */
340#ifdef KAME
341
342#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
343 do { \
344 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
345 (a).s6_addr[3] = (i) & 0xff; \
346 } while (0)
347
348 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
349 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
350#endif /* KAME */
351
352 if (gate && p->prefixlen == 128)
353 mask = NULL;
354 else
355 {
356 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
6fe70d1b 357 sin_mask.sin6_family = AF_INET6;
718e3744 358#ifdef SIN6_LEN
359 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
360#endif /* SIN6_LEN */
361 mask = &sin_mask;
362 }
363
364 error = rtm_write (cmd,
365 (union sockunion *) &sin_dest,
366 (union sockunion *) mask,
367 gate ? (union sockunion *)&sin_gate : NULL,
368 ifindex,
369 rib->flags,
370 rib->metric);
371
372#if 0
373 if (error)
374 {
375 zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
376 nexthop_num, error);
377 }
746c4f02
DL
378#else
379 (void)error;
718e3744 380#endif
381
382 nexthop_num++;
383 }
384
385 /* If there is no useful nexthop then return. */
386 if (nexthop_num == 0)
387 {
388 if (IS_ZEBRA_DEBUG_KERNEL)
b6178002 389 zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
718e3744 390 return 0;
391 }
392
393 return 0; /*XXX*/
394}
395
396int
397kernel_add_ipv6 (struct prefix *p, struct rib *rib)
398{
edd7c245 399 int route;
400
401 if (zserv_privs.change(ZPRIVS_RAISE))
402 zlog (NULL, LOG_ERR, "Can't raise privileges");
403 route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6);
404 if (zserv_privs.change(ZPRIVS_LOWER))
405 zlog (NULL, LOG_ERR, "Can't lower privileges");
406
407 return route;
718e3744 408}
409
dccc5225 410int
411kernel_update_ipv6 (struct prefix *p, struct rib *rib)
412{
413 kernel_delete_ipv6 (p, rib);
414 return kernel_add_ipv6 (p, rib);
415}
416
718e3744 417int
418kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
419{
edd7c245 420 int route;
421
422 if (zserv_privs.change(ZPRIVS_RAISE))
423 zlog (NULL, LOG_ERR, "Can't raise privileges");
424 route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
425 if (zserv_privs.change(ZPRIVS_LOWER))
426 zlog (NULL, LOG_ERR, "Can't lower privileges");
427
428 return route;
718e3744 429}
718e3744 430#endif /* HAVE_IPV6 */
6b8a5694
RW
431
432int
433kernel_neigh_update (int add, int ifindex, uint32_t addr, char *lla, int llalen)
434{
435 /* TODO */
436 return 0;
437}