]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
bgpd: don't use BGP_ATTR_VNC(255) unless ENABLE_BGP_VNC_ATTR is defined
[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"
718e3744 43
edd7c245 44extern struct zebra_privs_t zserv_privs;
45
746c4f02 46#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 47/* Adjust netmask socket length. Return value is a adjusted sin_len
48 value. */
d62a17ae 49static int sin_masklen(struct in_addr mask)
718e3744 50{
d62a17ae 51 char *p, *lim;
52 int len;
53 struct sockaddr_in sin;
718e3744 54
d62a17ae 55 if (mask.s_addr == 0)
56 return sizeof(long);
718e3744 57
d62a17ae 58 sin.sin_addr = mask;
59 len = sizeof(struct sockaddr_in);
718e3744 60
d62a17ae 61 lim = (char *)&sin.sin_addr;
62 p = lim + sizeof(sin.sin_addr);
718e3744 63
d62a17ae 64 while (*--p == 0 && p >= lim)
65 len--;
66 return len;
718e3744 67}
746c4f02 68#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 69
5e8c8947 70#ifdef __OpenBSD__
8ecdb26e 71static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
5e8c8947
RW
72 struct sockaddr_mpls *smpls)
73{
74 if (nh_label->num_labels > 1) {
75 zlog_warn(
76 "%s: can't push %u labels at "
77 "once (maximum is 1)",
78 __func__, nh_label->num_labels);
79 return -1;
80 }
81
82 memset(smpls, 0, sizeof(*smpls));
83 smpls->smpls_len = sizeof(*smpls);
84 smpls->smpls_family = AF_MPLS;
85 smpls->smpls_label = htonl(nh_label->label[0] << MPLS_LABEL_OFFSET);
86
87 return 0;
88}
89#endif
90
718e3744 91/* Interface between zebra message and rtm message. */
c886868f
MS
92static int kernel_rtm_ipv4(int cmd, const struct prefix *p,
93 struct route_entry *re)
718e3744 94
95{
d62a17ae 96 struct sockaddr_in *mask = NULL;
97 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fe6c7157 98#ifdef __OpenBSD__
d62a17ae 99 struct sockaddr_mpls smpls;
d3e2c74a 100#endif
d62a17ae 101 union sockunion *smplsp = NULL;
102 struct nexthop *nexthop;
103 int nexthop_num = 0;
104 ifindex_t ifindex = 0;
105 int gate = 0;
106 int error;
107 char prefix_buf[PREFIX_STRLEN];
a8309422 108 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 109
110 if (IS_ZEBRA_DEBUG_RIB)
111 prefix2str(p, prefix_buf, sizeof(prefix_buf));
112 memset(&sin_dest, 0, sizeof(struct sockaddr_in));
113 sin_dest.sin_family = AF_INET;
6f0e3f6e 114#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 115 sin_dest.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 116#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 117 sin_dest.sin_addr = p->u.prefix4;
718e3744 118
d62a17ae 119 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
718e3744 120
d62a17ae 121 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
122 sin_gate.sin_family = AF_INET;
6f0e3f6e 123#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 124 sin_gate.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 125#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 126
d62a17ae 127 /* Make gateway. */
7ee30f28 128 for (ALL_NEXTHOPS(re->ng, nexthop)) {
d62a17ae 129 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
130 continue;
131
132 gate = 0;
133 char gate_buf[INET_ADDRSTRLEN] = "NULL";
134
135 /*
136 * XXX We need to refrain from kernel operations in some cases,
137 * but this if statement seems overly cautious - what about
138 * other than ADD and DELETE?
139 */
996c9314 140 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
d62a17ae 141 || (cmd == RTM_DELETE
142 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))) {
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,
183 smplsp, ifindex, bh_type, re->metric);
d62a17ae 184
185 if (IS_ZEBRA_DEBUG_RIB) {
186 if (!gate) {
187 zlog_debug(
188 "%s: %s: attention! gate not found for re %p",
189 __func__, prefix_buf, re);
190 route_entry_dump(p, NULL, re);
191 } else
192 inet_ntop(AF_INET, &sin_gate.sin_addr,
193 gate_buf, INET_ADDRSTRLEN);
194 }
195
196 switch (error) {
197 /* We only flag nexthops as being in FIB if rtm_write()
198 * did its work. */
199 case ZEBRA_ERR_NOERROR:
200 nexthop_num++;
201 if (IS_ZEBRA_DEBUG_RIB)
202 zlog_debug(
203 "%s: %s: successfully did NH %s",
204 __func__, prefix_buf, gate_buf);
d62a17ae 205 break;
206
207 /* The only valid case for this error is kernel's
208 * failure to install
209 * a multipath route, which is common for FreeBSD. This
210 * should be
211 * ignored silently, but logged as an error otherwise.
212 */
213 case ZEBRA_ERR_RTEXIST:
214 if (cmd != RTM_ADD)
af4c2728 215 flog_err(
43e52561 216 LIB_ERR_SYSTEM_CALL,
d62a17ae 217 "%s: rtm_write() returned %d for command %d",
218 __func__, error, cmd);
219 continue;
220 break;
221
222 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
223 * FIB, it isn't
224 * normal to get any other messages in ANY case.
225 */
226 case ZEBRA_ERR_RTNOEXIST:
227 case ZEBRA_ERR_RTUNREACH:
228 default:
af4c2728 229 flog_err(
43e52561 230 LIB_ERR_SYSTEM_CALL,
d62a17ae 231 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
232 __func__,
233 prefix2str(p, prefix_buf,
234 sizeof(prefix_buf)),
235 error,
236 lookup_msg(rtm_type_str, cmd, NULL));
237 break;
238 }
239 } /* if (cmd and flags make sense) */
240 else if (IS_ZEBRA_DEBUG_RIB)
241 zlog_debug("%s: odd command %s for flags %d", __func__,
242 lookup_msg(rtm_type_str, cmd, NULL),
243 nexthop->flags);
244 } /* for (ALL_NEXTHOPS(...))*/
245
246 /* If there was no useful nexthop, then complain. */
247 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
248 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
249 __func__, re);
250
251 return 0; /*XXX*/
718e3744 252}
253
746c4f02 254#ifdef SIN6_LEN
718e3744 255/* Calculate sin6_len value for netmask socket value. */
d62a17ae 256static int sin6_masklen(struct in6_addr mask)
718e3744 257{
d62a17ae 258 struct sockaddr_in6 sin6;
259 char *p, *lim;
260 int len;
718e3744 261
d62a17ae 262 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
263 return sizeof(long);
718e3744 264
d62a17ae 265 sin6.sin6_addr = mask;
266 len = sizeof(struct sockaddr_in6);
718e3744 267
d62a17ae 268 lim = (char *)&sin6.sin6_addr;
269 p = lim + sizeof(sin6.sin6_addr);
718e3744 270
d62a17ae 271 while (*--p == 0 && p >= lim)
272 len--;
718e3744 273
d62a17ae 274 return len;
718e3744 275}
746c4f02 276#endif /* SIN6_LEN */
718e3744 277
718e3744 278/* Interface between zebra message and rtm message. */
c886868f
MS
279static int kernel_rtm_ipv6(int cmd, const struct prefix *p,
280 struct route_entry *re)
718e3744 281{
d62a17ae 282 struct sockaddr_in6 *mask;
283 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
5e8c8947
RW
284#ifdef __OpenBSD__
285 struct sockaddr_mpls smpls;
286#endif
287 union sockunion *smplsp = NULL;
d62a17ae 288 struct nexthop *nexthop;
289 int nexthop_num = 0;
290 ifindex_t ifindex = 0;
291 int gate = 0;
292 int error;
a8309422 293 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 294
295 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
296 sin_dest.sin6_family = AF_INET6;
718e3744 297#ifdef SIN6_LEN
d62a17ae 298 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
718e3744 299#endif /* SIN6_LEN */
d62a17ae 300 sin_dest.sin6_addr = p->u.prefix6;
718e3744 301
d62a17ae 302 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
718e3744 303
d62a17ae 304 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
305 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 306#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 307 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
6f0e3f6e 308#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 309
d62a17ae 310 /* Make gateway. */
7ee30f28 311 for (ALL_NEXTHOPS(re->ng, nexthop)) {
d62a17ae 312 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
313 continue;
fa713d9e 314
d62a17ae 315 gate = 0;
718e3744 316
996c9314 317 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
2d74d637 318 || (cmd == RTM_DELETE)) {
d62a17ae 319 if (nexthop->type == NEXTHOP_TYPE_IPV6
320 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
321 sin_gate.sin6_addr = nexthop->gate.ipv6;
322 gate = 1;
323 }
324 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
325 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
326 ifindex = nexthop->ifindex;
327
a8309422
DL
328 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
329 bh_type = nexthop->bh_type;
d62a17ae 330 }
331
332/* Under kame set interface index to link local address. */
718e3744 333#ifdef KAME
334
d62a17ae 335#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
336 do { \
337 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
338 (a).s6_addr[3] = (i)&0xff; \
339 } while (0)
718e3744 340
d62a17ae 341 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
342 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
718e3744 343#endif /* KAME */
344
d62a17ae 345 if (gate && p->prefixlen == 128)
346 mask = NULL;
347 else {
348 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
349 sin_mask.sin6_family = AF_INET6;
718e3744 350#ifdef SIN6_LEN
d62a17ae 351 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
718e3744 352#endif /* SIN6_LEN */
d62a17ae 353 mask = &sin_mask;
354 }
718e3744 355
5e8c8947
RW
356#ifdef __OpenBSD__
357 if (nexthop->nh_label
358 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
359 continue;
360 smplsp = (union sockunion *)&smpls;
361#endif
362
d62a17ae 363 error = rtm_write(cmd, (union sockunion *)&sin_dest,
364 (union sockunion *)mask,
365 gate ? (union sockunion *)&sin_gate : NULL,
a8309422 366 smplsp, ifindex, bh_type, re->metric);
d62a17ae 367 (void)error;
718e3744 368
d62a17ae 369 nexthop_num++;
370 }
718e3744 371
d62a17ae 372 /* If there is no useful nexthop then return. */
373 if (nexthop_num == 0) {
374 if (IS_ZEBRA_DEBUG_KERNEL)
375 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
376 return 0;
377 }
718e3744 378
d62a17ae 379 return 0; /*XXX*/
718e3744 380}
381
c886868f 382static int kernel_rtm(int cmd, const struct prefix *p, struct route_entry *re)
62ccf1e5 383{
d62a17ae 384 switch (PREFIX_FAMILY(p)) {
385 case AF_INET:
386 return kernel_rtm_ipv4(cmd, p, re);
387 case AF_INET6:
388 return kernel_rtm_ipv6(cmd, p, re);
389 }
390 return 0;
62ccf1e5
TT
391}
392
1e885672 393enum dp_req_result kernel_route_rib(struct route_node *rn,
86391e56
MS
394 const struct prefix *p,
395 const struct prefix *src_p,
1e885672
DS
396 struct route_entry *old,
397 struct route_entry *new)
718e3744 398{
d62a17ae 399 int route = 0;
edd7c245 400
d62a17ae 401 if (src_p && src_p->prefixlen) {
43e52561 402 zlog_warn("%s: IPv6 sourcedest routes unsupported!", __func__);
f47598b0 403 return DP_REQUEST_FAILURE;
d62a17ae 404 }
05737783 405
01b9e3fd 406 frr_elevate_privs(&zserv_privs) {
be717a0a 407
01b9e3fd
DL
408 if (old)
409 route |= kernel_rtm(RTM_DELETE, p, old);
62ccf1e5 410
01b9e3fd
DL
411 if (new)
412 route |= kernel_rtm(RTM_ADD, p, new);
be717a0a 413
01b9e3fd 414 }
edd7c245 415
0c555cc6 416 if (new) {
996c9314
LB
417 kernel_route_rib_pass_fail(
418 rn, p, new,
215181cb
DS
419 (!route) ? DP_INSTALL_SUCCESS
420 : DP_INSTALL_FAILURE);
0c555cc6 421 } else {
7d974ba3 422 kernel_route_rib_pass_fail(rn, p, old,
996c9314 423 (!route)
215181cb
DS
424 ? DP_DELETE_SUCCESS
425 : DP_DELETE_FAILURE);
0c555cc6 426 }
1e885672
DS
427
428 return DP_REQUEST_SUCCESS;
718e3744 429}
6b8a5694 430
d62a17ae 431int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 432 int llalen, ns_id_t ns_id)
6b8a5694 433{
d62a17ae 434 /* TODO */
435 return 0;
6b8a5694 436}
1498c059 437
43b5cc5e 438extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 439{
d62a17ae 440 return 0;
1498c059 441}
13d60d35 442
d62a17ae 443int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 444{
d62a17ae 445 return 0;
13d60d35 446}
447
d62a17ae 448int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 449{
d62a17ae 450 return 0;
13d60d35 451}
2232a77c 452
d62a17ae 453int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
d7c0a89a 454 struct in_addr vtep_ip, uint8_t sticky)
2232a77c 455{
d62a17ae 456 return 0;
2232a77c 457}
458
d62a17ae 459int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
460 struct in_addr vtep_ip, int local)
2232a77c 461{
d62a17ae 462 return 0;
2232a77c 463}
464
d62a17ae 465int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
68e33151 466 struct ethaddr *mac, uint8_t flags)
2232a77c 467{
d62a17ae 468 return 0;
2232a77c 469}
470
d62a17ae 471int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 472{
d62a17ae 473 return 0;
2232a77c 474}
e0ae31b8
DS
475
476extern int kernel_interface_set_master(struct interface *master,
477 struct interface *slave)
478{
479 return 0;
480}
0ecfe5bf 481
dc7b3cae
DS
482uint32_t kernel_get_speed(struct interface *ifp)
483{
484 return ifp->speed;
485}
486
ddfeb486 487#endif /* !HAVE_NETLINK */