]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
isisd: implement the 'lsp-too-large' notification
[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 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
ddfeb486
DL
23
24#ifndef HAVE_NETLINK
25
fe6c7157 26#ifdef __OpenBSD__
d3e2c74a
RW
27#include <netmpls/mpls.h>
28#endif
718e3744 29
30#include "if.h"
31#include "prefix.h"
32#include "sockunion.h"
33#include "log.h"
edd7c245 34#include "privs.h"
13d60d35 35#include "vxlan.h"
174482ef 36#include "lib_errors.h"
718e3744 37
38#include "zebra/debug.h"
39#include "zebra/rib.h"
6621ca86 40#include "zebra/rt.h"
dc95824a 41#include "zebra/kernel_socket.h"
d3e2c74a 42#include "zebra/zebra_mpls.h"
364fed6b 43#include "zebra/zebra_errors.h"
718e3744 44
edd7c245 45extern struct zebra_privs_t zserv_privs;
46
746c4f02 47#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 48/* Adjust netmask socket length. Return value is a adjusted sin_len
49 value. */
d62a17ae 50static int sin_masklen(struct in_addr mask)
718e3744 51{
d62a17ae 52 char *p, *lim;
53 int len;
54 struct sockaddr_in sin;
718e3744 55
d62a17ae 56 if (mask.s_addr == 0)
57 return sizeof(long);
718e3744 58
d62a17ae 59 sin.sin_addr = mask;
60 len = sizeof(struct sockaddr_in);
718e3744 61
d62a17ae 62 lim = (char *)&sin.sin_addr;
63 p = lim + sizeof(sin.sin_addr);
718e3744 64
d62a17ae 65 while (*--p == 0 && p >= lim)
66 len--;
67 return len;
718e3744 68}
746c4f02 69#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 70
5e8c8947 71#ifdef __OpenBSD__
8ecdb26e 72static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
5e8c8947
RW
73 struct sockaddr_mpls *smpls)
74{
75 if (nh_label->num_labels > 1) {
e914ccbe 76 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
9df414fe
QY
77 "%s: can't push %u labels at "
78 "once (maximum is 1)",
79 __func__, nh_label->num_labels);
5e8c8947
RW
80 return -1;
81 }
82
83 memset(smpls, 0, sizeof(*smpls));
84 smpls->smpls_len = sizeof(*smpls);
85 smpls->smpls_family = AF_MPLS;
86 smpls->smpls_label = htonl(nh_label->label[0] << MPLS_LABEL_OFFSET);
87
88 return 0;
89}
90#endif
91
718e3744 92/* Interface between zebra message and rtm message. */
c886868f 93static int kernel_rtm_ipv4(int cmd, const struct prefix *p,
01ce7cba 94 const struct nexthop_group *ng, uint32_t metric)
718e3744 95
96{
d62a17ae 97 struct sockaddr_in *mask = NULL;
98 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fe6c7157 99#ifdef __OpenBSD__
d62a17ae 100 struct sockaddr_mpls smpls;
d3e2c74a 101#endif
d62a17ae 102 union sockunion *smplsp = NULL;
103 struct nexthop *nexthop;
104 int nexthop_num = 0;
105 ifindex_t ifindex = 0;
106 int gate = 0;
107 int error;
108 char prefix_buf[PREFIX_STRLEN];
a8309422 109 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 110
111 if (IS_ZEBRA_DEBUG_RIB)
112 prefix2str(p, prefix_buf, sizeof(prefix_buf));
113 memset(&sin_dest, 0, sizeof(struct sockaddr_in));
114 sin_dest.sin_family = AF_INET;
6f0e3f6e 115#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 116 sin_dest.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 117#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 118 sin_dest.sin_addr = p->u.prefix4;
718e3744 119
d62a17ae 120 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
718e3744 121
d62a17ae 122 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
123 sin_gate.sin_family = AF_INET;
6f0e3f6e 124#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 125 sin_gate.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 126#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 127
d62a17ae 128 /* Make gateway. */
01ce7cba 129 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
d62a17ae 130 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
131 continue;
132
133 gate = 0;
134 char gate_buf[INET_ADDRSTRLEN] = "NULL";
135
136 /*
137 * XXX We need to refrain from kernel operations in some cases,
138 * but this if statement seems overly cautious - what about
139 * other than ADD and DELETE?
140 */
996c9314 141 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
01ce7cba 142 || (cmd == RTM_DELETE)) {
d62a17ae 143 if (nexthop->type == NEXTHOP_TYPE_IPV4
144 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
145 sin_gate.sin_addr = nexthop->gate.ipv4;
146 gate = 1;
147 }
148 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
149 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
150 ifindex = nexthop->ifindex;
151 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
152 struct in_addr loopback;
153 loopback.s_addr = htonl(INADDR_LOOPBACK);
154 sin_gate.sin_addr = loopback;
a8309422 155 bh_type = nexthop->bh_type;
d62a17ae 156 gate = 1;
157 }
158
159 if (gate && p->prefixlen == 32)
160 mask = NULL;
161 else {
162 masklen2ip(p->prefixlen, &sin_mask.sin_addr);
163 sin_mask.sin_family = AF_INET;
6f0e3f6e 164#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 165 sin_mask.sin_len =
166 sin_masklen(sin_mask.sin_addr);
6f0e3f6e 167#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 168 mask = &sin_mask;
169 }
718e3744 170
fe6c7157 171#ifdef __OpenBSD__
5e8c8947
RW
172 if (nexthop->nh_label
173 && !kernel_rtm_add_labels(nexthop->nh_label,
174 &smpls))
175 continue;
176 smplsp = (union sockunion *)&smpls;
d3e2c74a
RW
177#endif
178
60466a63
QY
179 error = rtm_write(cmd, (union sockunion *)&sin_dest,
180 (union sockunion *)mask,
181 gate ? (union sockunion *)&sin_gate
182 : NULL,
01ce7cba 183 smplsp, ifindex, bh_type, metric);
d62a17ae 184
f183e380 185 if (IS_ZEBRA_DEBUG_KERNEL) {
d62a17ae 186 if (!gate) {
187 zlog_debug(
01ce7cba
MS
188 "%s: %s: attention! gate not found for re",
189 __func__, prefix_buf);
d62a17ae 190 } else
191 inet_ntop(AF_INET, &sin_gate.sin_addr,
192 gate_buf, INET_ADDRSTRLEN);
193 }
194
195 switch (error) {
196 /* We only flag nexthops as being in FIB if rtm_write()
197 * did its work. */
198 case ZEBRA_ERR_NOERROR:
199 nexthop_num++;
f183e380 200 if (IS_ZEBRA_DEBUG_KERNEL)
d62a17ae 201 zlog_debug(
202 "%s: %s: successfully did NH %s",
203 __func__, prefix_buf, gate_buf);
f183e380
MS
204
205 if (cmd == RTM_ADD)
206 SET_FLAG(nexthop->flags,
207 NEXTHOP_FLAG_FIB);
208
d62a17ae 209 break;
210
211 /* The only valid case for this error is kernel's
212 * failure to install
213 * a multipath route, which is common for FreeBSD. This
214 * should be
215 * ignored silently, but logged as an error otherwise.
216 */
217 case ZEBRA_ERR_RTEXIST:
218 if (cmd != RTM_ADD)
af4c2728 219 flog_err(
450971aa 220 EC_LIB_SYSTEM_CALL,
d62a17ae 221 "%s: rtm_write() returned %d for command %d",
222 __func__, error, cmd);
223 continue;
d62a17ae 224
f183e380 225 /* Note any unexpected status returns */
d62a17ae 226 default:
af4c2728 227 flog_err(
450971aa 228 EC_LIB_SYSTEM_CALL,
d62a17ae 229 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
230 __func__,
231 prefix2str(p, prefix_buf,
232 sizeof(prefix_buf)),
233 error,
234 lookup_msg(rtm_type_str, cmd, NULL));
235 break;
236 }
237 } /* if (cmd and flags make sense) */
f183e380 238 else if (IS_ZEBRA_DEBUG_KERNEL)
d62a17ae 239 zlog_debug("%s: odd command %s for flags %d", __func__,
240 lookup_msg(rtm_type_str, cmd, NULL),
241 nexthop->flags);
242 } /* for (ALL_NEXTHOPS(...))*/
243
244 /* If there was no useful nexthop, then complain. */
560e3136
DS
245 if (nexthop_num == 0) {
246 if (IS_ZEBRA_DEBUG_KERNEL)
01ce7cba
MS
247 zlog_debug("%s: No useful nexthops were found in RIB prefix %s",
248 __func__, prefix2str(p, prefix_buf,
249 sizeof(prefix_buf)));
560e3136
DS
250 return 1;
251 }
d62a17ae 252
253 return 0; /*XXX*/
718e3744 254}
255
746c4f02 256#ifdef SIN6_LEN
718e3744 257/* Calculate sin6_len value for netmask socket value. */
d62a17ae 258static int sin6_masklen(struct in6_addr mask)
718e3744 259{
d62a17ae 260 struct sockaddr_in6 sin6;
261 char *p, *lim;
262 int len;
718e3744 263
d62a17ae 264 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
265 return sizeof(long);
718e3744 266
d62a17ae 267 sin6.sin6_addr = mask;
268 len = sizeof(struct sockaddr_in6);
718e3744 269
d62a17ae 270 lim = (char *)&sin6.sin6_addr;
271 p = lim + sizeof(sin6.sin6_addr);
718e3744 272
d62a17ae 273 while (*--p == 0 && p >= lim)
274 len--;
718e3744 275
d62a17ae 276 return len;
718e3744 277}
746c4f02 278#endif /* SIN6_LEN */
718e3744 279
718e3744 280/* Interface between zebra message and rtm message. */
c886868f 281static int kernel_rtm_ipv6(int cmd, const struct prefix *p,
01ce7cba 282 const struct nexthop_group *ng, uint32_t metric)
718e3744 283{
d62a17ae 284 struct sockaddr_in6 *mask;
285 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
5e8c8947
RW
286#ifdef __OpenBSD__
287 struct sockaddr_mpls smpls;
288#endif
289 union sockunion *smplsp = NULL;
d62a17ae 290 struct nexthop *nexthop;
291 int nexthop_num = 0;
292 ifindex_t ifindex = 0;
293 int gate = 0;
294 int error;
a8309422 295 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 296
297 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
298 sin_dest.sin6_family = AF_INET6;
718e3744 299#ifdef SIN6_LEN
d62a17ae 300 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
718e3744 301#endif /* SIN6_LEN */
d62a17ae 302 sin_dest.sin6_addr = p->u.prefix6;
718e3744 303
d62a17ae 304 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
718e3744 305
d62a17ae 306 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
307 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 308#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 309 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
6f0e3f6e 310#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 311
d62a17ae 312 /* Make gateway. */
01ce7cba 313 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
d62a17ae 314 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
315 continue;
fa713d9e 316
d62a17ae 317 gate = 0;
718e3744 318
996c9314 319 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
2d74d637 320 || (cmd == RTM_DELETE)) {
d62a17ae 321 if (nexthop->type == NEXTHOP_TYPE_IPV6
322 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
323 sin_gate.sin6_addr = nexthop->gate.ipv6;
324 gate = 1;
325 }
326 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
327 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
328 ifindex = nexthop->ifindex;
329
a8309422
DL
330 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
331 bh_type = nexthop->bh_type;
d62a17ae 332 }
333
334/* Under kame set interface index to link local address. */
718e3744 335#ifdef KAME
336
d62a17ae 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)
718e3744 342
d62a17ae 343 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
344 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
718e3744 345#endif /* KAME */
346
d62a17ae 347 if (gate && p->prefixlen == 128)
348 mask = NULL;
349 else {
350 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
351 sin_mask.sin6_family = AF_INET6;
718e3744 352#ifdef SIN6_LEN
d62a17ae 353 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
718e3744 354#endif /* SIN6_LEN */
d62a17ae 355 mask = &sin_mask;
356 }
718e3744 357
5e8c8947
RW
358#ifdef __OpenBSD__
359 if (nexthop->nh_label
360 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
361 continue;
362 smplsp = (union sockunion *)&smpls;
363#endif
364
d62a17ae 365 error = rtm_write(cmd, (union sockunion *)&sin_dest,
366 (union sockunion *)mask,
367 gate ? (union sockunion *)&sin_gate : NULL,
01ce7cba 368 smplsp, ifindex, bh_type, metric);
f183e380
MS
369
370 /* Update installed nexthop info on success */
371 if ((cmd == RTM_ADD) && (error == ZEBRA_ERR_NOERROR))
372 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
718e3744 373
d62a17ae 374 nexthop_num++;
375 }
718e3744 376
d62a17ae 377 /* If there is no useful nexthop then return. */
378 if (nexthop_num == 0) {
379 if (IS_ZEBRA_DEBUG_KERNEL)
380 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
560e3136 381 return 1;
d62a17ae 382 }
718e3744 383
d62a17ae 384 return 0; /*XXX*/
718e3744 385}
386
01ce7cba
MS
387static int kernel_rtm(int cmd, const struct prefix *p,
388 const struct nexthop_group *ng, uint32_t metric)
62ccf1e5 389{
d62a17ae 390 switch (PREFIX_FAMILY(p)) {
391 case AF_INET:
01ce7cba 392 return kernel_rtm_ipv4(cmd, p, ng, metric);
d62a17ae 393 case AF_INET6:
01ce7cba 394 return kernel_rtm_ipv6(cmd, p, ng, metric);
d62a17ae 395 }
396 return 0;
62ccf1e5
TT
397}
398
01ce7cba
MS
399/*
400 * Update or delete a prefix from the kernel,
401 * using info from a dataplane context struct.
402 */
25779064 403enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
01ce7cba
MS
404{
405 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
406
407 if (dplane_ctx_get_src(ctx) != NULL) {
408 zlog_err("route add: IPv6 sourcedest routes unsupported!");
409 res = ZEBRA_DPLANE_REQUEST_FAILURE;
410 goto done;
411 }
412
49ddb66a 413 frr_elevate_privs(&zserv_privs) {
f183e380
MS
414
415 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE)
416 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
417 dplane_ctx_get_ng(ctx),
418 dplane_ctx_get_metric(ctx));
419 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL)
420 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
421 dplane_ctx_get_ng(ctx),
422 dplane_ctx_get_metric(ctx));
423 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
424 /* Must do delete and add separately -
425 * no update available
426 */
427 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
428 dplane_ctx_get_old_ng(ctx),
429 dplane_ctx_get_old_metric(ctx));
430
431 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
432 dplane_ctx_get_ng(ctx),
433 dplane_ctx_get_metric(ctx));
434 } else {
435 zlog_err("Invalid routing socket update op %s (%u)",
436 dplane_op2str(dplane_ctx_get_op(ctx)),
437 dplane_ctx_get_op(ctx));
438 res = ZEBRA_DPLANE_REQUEST_FAILURE;
439 }
440 } /* Elevated privs */
01ce7cba
MS
441
442done:
443
444 return res;
445}
446
d62a17ae 447int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 448 int llalen, ns_id_t ns_id)
6b8a5694 449{
d62a17ae 450 /* TODO */
451 return 0;
6b8a5694 452}
1498c059 453
43b5cc5e 454extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 455{
d62a17ae 456 return 0;
1498c059 457}
13d60d35 458
d62a17ae 459int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 460{
d62a17ae 461 return 0;
13d60d35 462}
463
d62a17ae 464int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 465{
d62a17ae 466 return 0;
13d60d35 467}
2232a77c 468
d62a17ae 469int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
a37f4598 470 struct in_addr vtep_ip, bool sticky)
2232a77c 471{
d62a17ae 472 return 0;
2232a77c 473}
474
d62a17ae 475int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
d63c1b18 476 struct in_addr vtep_ip)
2232a77c 477{
d62a17ae 478 return 0;
2232a77c 479}
480
d62a17ae 481int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
68e33151 482 struct ethaddr *mac, uint8_t flags)
2232a77c 483{
d62a17ae 484 return 0;
2232a77c 485}
486
d62a17ae 487int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 488{
d62a17ae 489 return 0;
2232a77c 490}
e0ae31b8
DS
491
492extern int kernel_interface_set_master(struct interface *master,
493 struct interface *slave)
494{
495 return 0;
496}
0ecfe5bf 497
dc7b3cae
DS
498uint32_t kernel_get_speed(struct interface *ifp)
499{
500 return ifp->speed;
501}
502
ddfeb486 503#endif /* !HAVE_NETLINK */