]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
bgpd: bgpd-routemap-match-localpref.patch
[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
44/* Adjust netmask socket length. Return value is a adjusted sin_len
45 value. */
6621ca86 46static int
718e3744 47sin_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. */
6621ca86 68static int
718e3744 69kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family)
70
71{
fa2b17e3 72 struct sockaddr_in *mask = NULL;
718e3744 73 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fa713d9e
CF
74 struct nexthop *nexthop, *tnexthop;
75 int recursing;
718e3744 76 int nexthop_num = 0;
77 unsigned int ifindex = 0;
78 int gate = 0;
79 int error;
dc95824a 80 char prefix_buf[INET_ADDRSTRLEN];
718e3744 81
dc95824a
DO
82 if (IS_ZEBRA_DEBUG_RIB)
83 inet_ntop (AF_INET, &p->u.prefix, prefix_buf, INET_ADDRSTRLEN);
718e3744 84 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
85 sin_dest.sin_family = AF_INET;
6f0e3f6e 86#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 87 sin_dest.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 88#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 89 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;
6f0e3f6e 95#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 96 sin_gate.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 97#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 98
99 /* Make gateway. */
fa713d9e 100 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
718e3744 101 {
fa713d9e
CF
102 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
103 continue;
104
718e3744 105 gate = 0;
dc95824a 106 char gate_buf[INET_ADDRSTRLEN] = "NULL";
718e3744 107
dfdb8f18
GT
108 /*
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 */
718e3744 113 if ((cmd == RTM_ADD
114 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
115 || (cmd == RTM_DELETE
718e3744 116 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
718e3744 117 ))
118 {
fa713d9e
CF
119 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
120 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
718e3744 121 {
fa713d9e
CF
122 sin_gate.sin_addr = nexthop->gate.ipv4;
123 gate = 1;
718e3744 124 }
fa713d9e
CF
125 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)
718e3744 130 {
fa713d9e
CF
131 struct in_addr loopback;
132 loopback.s_addr = htonl (INADDR_LOOPBACK);
133 sin_gate.sin_addr = loopback;
134 gate = 1;
dfdb8f18 135 }
718e3744 136
718e3744 137 if (gate && p->prefixlen == 32)
138 mask = NULL;
139 else
140 {
141 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
6083e1f8 142 sin_mask.sin_family = AF_INET;
6f0e3f6e 143#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 144 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
6f0e3f6e 145#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 146 mask = &sin_mask;
147 }
718e3744 148
dfdb8f18
GT
149 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);
718e3744 156
dc95824a
DO
157 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);
f7bf4153 163 rib_dump (p, rib);
dc95824a
DO
164 }
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:
bd6c86d3
DO
198 /* 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);
dc95824a 201 zlog_err ("%s: %s/%d: rtm_write() unexpectedly returned %d for command %s",
2d844524 202 __func__, prefix_buf, p->prefixlen, error, lookup (rtm_type_str, cmd));
dc95824a
DO
203 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",
2d844524 209 __func__, lookup (rtm_type_str, cmd), nexthop->flags);
fa713d9e 210 } /* for (ALL_NEXTHOPS_RO(...))*/
dc95824a
DO
211
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);
718e3744 215
216 return 0; /*XXX*/
217}
218
219int
220kernel_add_ipv4 (struct prefix *p, struct rib *rib)
221{
edd7c245 222 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;
718e3744 231}
232
233int
234kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
235{
edd7c245 236 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;
718e3744 245}
246
247#ifdef HAVE_IPV6
248
249/* Calculate sin6_len value for netmask socket value. */
6621ca86 250static int
718e3744 251sin6_masklen (struct in6_addr mask)
252{
253 struct sockaddr_in6 sin6;
254 char *p, *lim;
255 int len;
256
257#if defined (INRIA)
258 if (IN_ANYADDR6 (mask))
259 return sizeof (long);
260#else /* ! INRIA */
261 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
262 return sizeof (long);
263#endif /* ! INRIA */
264
265 sin6.sin6_addr = mask;
266 len = sizeof (struct sockaddr_in6);
267
268 lim = (char *) & sin6.sin6_addr;
269 p = lim + sizeof (sin6.sin6_addr);
270
271 while (*--p == 0 && p >= lim)
272 len--;
273
274 return len;
275}
276
277/* Interface between zebra message and rtm message. */
6621ca86 278static int
718e3744 279kernel_rtm_ipv6 (int message, struct prefix_ipv6 *dest,
280 struct in6_addr *gate, int index, int flags)
281{
282 struct sockaddr_in6 *mask;
283 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
284
285 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
286 sin_dest.sin6_family = AF_INET6;
287#ifdef SIN6_LEN
288 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
289#endif /* SIN6_LEN */
290
291 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
292
293 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
294 sin_gate.sin6_family = AF_INET6;
295#ifdef SIN6_LEN
296 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
297#endif /* SIN6_LEN */
298
299 sin_dest.sin6_addr = dest->prefix;
300
301 if (gate)
302 memcpy (&sin_gate.sin6_addr, gate, sizeof (struct in6_addr));
303
304 /* Under kame set interface index to link local address. */
305#ifdef KAME
306
307#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
308 do { \
309 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
310 (a).s6_addr[3] = (i) & 0xff; \
311 } while (0)
312
313 if (gate && IN6_IS_ADDR_LINKLOCAL(gate))
314 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, index);
315#endif /* KAME */
316
317 if (gate && dest->prefixlen == 128)
318 mask = NULL;
319 else
320 {
321 masklen2ip6 (dest->prefixlen, &sin_mask.sin6_addr);
6fe70d1b 322 sin_mask.sin6_family = AF_INET6;
718e3744 323#ifdef SIN6_LEN
324 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
325#endif /* SIN6_LEN */
326 mask = &sin_mask;
327 }
328
329 return rtm_write (message,
330 (union sockunion *) &sin_dest,
331 (union sockunion *) mask,
332 gate ? (union sockunion *)&sin_gate : NULL,
333 index,
334 flags,
335 0);
336}
337
338/* Interface between zebra message and rtm message. */
6621ca86 339static int
718e3744 340kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
341 int family)
342{
343 struct sockaddr_in6 *mask;
344 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
fa713d9e
CF
345 struct nexthop *nexthop, *tnexthop;
346 int recursing;
718e3744 347 int nexthop_num = 0;
348 unsigned int ifindex = 0;
349 int gate = 0;
350 int error;
351
352 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
353 sin_dest.sin6_family = AF_INET6;
354#ifdef SIN6_LEN
355 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
356#endif /* SIN6_LEN */
357 sin_dest.sin6_addr = p->u.prefix6;
358
359 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
360
361 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
362 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 363#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 364 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
6f0e3f6e 365#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 366
367 /* Make gateway. */
fa713d9e 368 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
718e3744 369 {
fa713d9e
CF
370 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
371 continue;
372
718e3744 373 gate = 0;
374
375 if ((cmd == RTM_ADD
376 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
377 || (cmd == RTM_DELETE
378#if 0
379 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
380#endif
381 ))
382 {
fa713d9e
CF
383 if (nexthop->type == NEXTHOP_TYPE_IPV6
384 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
385 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
718e3744 386 {
fa713d9e
CF
387 sin_gate.sin6_addr = nexthop->gate.ipv6;
388 gate = 1;
718e3744 389 }
fa713d9e
CF
390 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
391 || nexthop->type == NEXTHOP_TYPE_IFNAME
392 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
393 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
394 ifindex = nexthop->ifindex;
718e3744 395
396 if (cmd == RTM_ADD)
397 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
398 }
399
400 /* Under kame set interface index to link local address. */
401#ifdef KAME
402
403#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
404 do { \
405 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
406 (a).s6_addr[3] = (i) & 0xff; \
407 } while (0)
408
409 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
410 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
411#endif /* KAME */
412
413 if (gate && p->prefixlen == 128)
414 mask = NULL;
415 else
416 {
417 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
6fe70d1b 418 sin_mask.sin6_family = AF_INET6;
718e3744 419#ifdef SIN6_LEN
420 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
421#endif /* SIN6_LEN */
422 mask = &sin_mask;
423 }
424
425 error = rtm_write (cmd,
426 (union sockunion *) &sin_dest,
427 (union sockunion *) mask,
428 gate ? (union sockunion *)&sin_gate : NULL,
429 ifindex,
430 rib->flags,
431 rib->metric);
432
433#if 0
434 if (error)
435 {
436 zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
437 nexthop_num, error);
438 }
439#endif
440
441 nexthop_num++;
442 }
443
444 /* If there is no useful nexthop then return. */
445 if (nexthop_num == 0)
446 {
447 if (IS_ZEBRA_DEBUG_KERNEL)
b6178002 448 zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
718e3744 449 return 0;
450 }
451
452 return 0; /*XXX*/
453}
454
455int
456kernel_add_ipv6 (struct prefix *p, struct rib *rib)
457{
edd7c245 458 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_ADD, p, rib, AF_INET6);
463 if (zserv_privs.change(ZPRIVS_LOWER))
464 zlog (NULL, LOG_ERR, "Can't lower privileges");
465
466 return route;
718e3744 467}
468
469int
470kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
471{
edd7c245 472 int route;
473
474 if (zserv_privs.change(ZPRIVS_RAISE))
475 zlog (NULL, LOG_ERR, "Can't raise privileges");
476 route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
477 if (zserv_privs.change(ZPRIVS_LOWER))
478 zlog (NULL, LOG_ERR, "Can't lower privileges");
479
480 return route;
718e3744 481}
482
483/* Delete IPv6 route from the kernel. */
484int
485kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
6621ca86 486 unsigned int index, int flags, int table)
718e3744 487{
edd7c245 488 int route;
489
490 if (zserv_privs.change(ZPRIVS_RAISE))
491 zlog (NULL, LOG_ERR, "Can't raise privileges");
492 route = kernel_rtm_ipv6 (RTM_DELETE, dest, gate, index, flags);
493 if (zserv_privs.change(ZPRIVS_LOWER))
494 zlog (NULL, LOG_ERR, "Can't lower privileges");
495
496 return route;
718e3744 497}
498#endif /* HAVE_IPV6 */