]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
zebra: Fixup spaces/tabs issue found by CI in rt_socket.c
[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
9ba0e570
DS
92#ifdef SIN6_LEN
93/* Calculate sin6_len value for netmask socket value. */
94static int sin6_masklen(struct in6_addr mask)
95{
96 struct sockaddr_in6 sin6;
97 char *p, *lim;
98 int len;
99
100 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
101 return sizeof(long);
102
103 sin6.sin6_addr = mask;
104 len = sizeof(struct sockaddr_in6);
105
106 lim = (char *)&sin6.sin6_addr;
107 p = lim + sizeof(sin6.sin6_addr);
108
109 while (*--p == 0 && p >= lim)
110 len--;
111
112 return len;
113}
114#endif /* SIN6_LEN */
115
718e3744 116/* Interface between zebra message and rtm message. */
08ea27d1
DS
117static int kernel_rtm(int cmd, const struct prefix *p,
118 const struct nexthop_group *ng, uint32_t metric)
718e3744 119
120{
ca2c70bd 121 union sockunion sin_dest, sin_mask, sin_gate;
fe6c7157 122#ifdef __OpenBSD__
d62a17ae 123 struct sockaddr_mpls smpls;
d3e2c74a 124#endif
d62a17ae 125 union sockunion *smplsp = NULL;
126 struct nexthop *nexthop;
127 int nexthop_num = 0;
128 ifindex_t ifindex = 0;
4dd39a0e 129 bool gate = false;
d62a17ae 130 int error;
131 char prefix_buf[PREFIX_STRLEN];
a8309422 132 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 133
134 if (IS_ZEBRA_DEBUG_RIB)
135 prefix2str(p, prefix_buf, sizeof(prefix_buf));
718e3744 136
86afd529
DS
137 /*
138 * We only have the ability to ADD or DELETE at this point
139 * in time.
140 */
141 if (cmd != RTM_ADD && cmd != RTM_DELETE) {
142 if (IS_ZEBRA_DEBUG_KERNEL)
143 zlog_debug("%s: %s odd command %s for flags %d",
144 __func__, prefix_buf,
145 lookup_msg(rtm_type_str, cmd, NULL),
146 nexthop->flags);
147 return 0;
148 }
149
08ea27d1
DS
150 memset(&sin_dest, 0, sizeof(sin_dest));
151 memset(&sin_gate, 0, sizeof(sin_gate));
ca2c70bd 152 memset(&sin_mask, 0, sizeof(sin_mask));
718e3744 153
08ea27d1
DS
154 switch (p->family) {
155 case AF_INET:
156 sin_dest.sin.sin_family = AF_INET;
157#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
158 sin_dest.sin.sin_len = sizeof(sin_dest);
159 sin_gate.sin.sin_len = sizeof(sin_gate);
160#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
161 sin_dest.sin.sin_addr = p->u.prefix4;
162 sin_gate.sin.sin_family = AF_INET;
163 break;
164 case AF_INET6:
165 sin_dest.sin6.sin6_family = AF_INET6;
166#ifdef SIN6_LEN
167 sin_dest.sin6.sin6_len = sizeof(sin_dest);
168#endif /* SIN6_LEN */
169 sin_dest.sin6.sin6_addr = p->u.prefix6;
170 sin_gate.sin6.sin6_family = AF_INET6;
6f0e3f6e 171#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
08ea27d1 172 sin_gate.sin6.sin6_len = sizeof(sin_gate);
6f0e3f6e 173#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
08ea27d1
DS
174 break;
175 }
718e3744 176
d62a17ae 177 /* Make gateway. */
01ce7cba 178 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
86afd529
DS
179 /*
180 * We only want to use the actual good nexthops
181 */
182 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ||
183 !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
d62a17ae 184 continue;
185
c2519893 186 smplsp = NULL;
4dd39a0e 187 gate = false;
d62a17ae 188 char gate_buf[INET_ADDRSTRLEN] = "NULL";
189
86afd529
DS
190 switch (nexthop->type) {
191 case NEXTHOP_TYPE_IPV4:
192 case NEXTHOP_TYPE_IPV4_IFINDEX:
193 sin_gate.sin.sin_addr = nexthop->gate.ipv4;
194 sin_gate.sin.sin_family = AF_INET;
195 ifindex = nexthop->ifindex;
4dd39a0e 196 gate = true;
86afd529
DS
197 break;
198 case NEXTHOP_TYPE_IPV6:
199 case NEXTHOP_TYPE_IPV6_IFINDEX:
200 sin_gate.sin6.sin6_addr = nexthop->gate.ipv6;
201 sin_gate.sin6.sin6_family = AF_INET6;
202 ifindex = nexthop->ifindex;
08ea27d1
DS
203/* Under kame set interface index to link local address */
204#ifdef KAME
205
206#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
c9277ebb
DS
207 do { \
208 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
209 (a).s6_addr[3] = (i)&0xff; \
210 } while (0)
08ea27d1 211
86afd529
DS
212 if (IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6.sin6_addr))
213 SET_IN6_LINKLOCAL_IFINDEX(
214 sin_gate.sin6.sin6_addr,
215 ifindex);
08ea27d1
DS
216#endif /* KAME */
217
4dd39a0e 218 gate = true;
86afd529
DS
219 break;
220 case NEXTHOP_TYPE_IFINDEX:
221 ifindex = nexthop->ifindex;
222 break;
223 case NEXTHOP_TYPE_BLACKHOLE:
224 bh_type = nexthop->bh_type;
225 switch (p->family) {
226 case AFI_IP: {
227 struct in_addr loopback;
228 loopback.s_addr = htonl(INADDR_LOOPBACK);
229 sin_gate.sin.sin_addr = loopback;
4dd39a0e 230 gate = true;
86afd529 231 }
08ea27d1 232 break;
86afd529 233 case AFI_IP6:
08ea27d1 234 break;
d62a17ae 235 }
86afd529
DS
236 }
237
238 switch (p->family) {
239 case AF_INET:
18d10d88
DS
240 masklen2ip(p->prefixlen, &sin_mask.sin.sin_addr);
241 sin_mask.sin.sin_family = AF_INET;
6f0e3f6e 242#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
18d10d88
DS
243 sin_mask.sin.sin_len = sin_masklen(
244 sin_mask.sin.sin_addr);
6f0e3f6e 245#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
86afd529
DS
246 break;
247 case AF_INET6:
18d10d88
DS
248 masklen2ip6(p->prefixlen, &sin_mask.sin6.sin6_addr);
249 sin_mask.sin6.sin6_family = AF_INET6;
08ea27d1 250#ifdef SIN6_LEN
18d10d88
DS
251 sin_mask.sin6.sin6_len = sin6_masklen(
252 sin_mask.sin6.sin6_addr);
08ea27d1 253#endif /* SIN6_LEN */
86afd529
DS
254 break;
255 }
718e3744 256
fe6c7157 257#ifdef __OpenBSD__
86afd529
DS
258 if (nexthop->nh_label
259 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
260 continue;
261 smplsp = (union sockunion *)&smpls;
d3e2c74a 262#endif
18d10d88 263 error = rtm_write(cmd, &sin_dest, &sin_mask,
86afd529
DS
264 gate ? &sin_gate : NULL, smplsp,
265 ifindex, bh_type, metric);
266
267 if (IS_ZEBRA_DEBUG_KERNEL) {
268 if (!gate) {
269 zlog_debug("%s: %s: attention! gate not found for re",
270 __func__, prefix_buf);
271 } else
272 inet_ntop(p->family == AFI_IP ? AF_INET
273 : AF_INET6,
274 &sin_gate.sin.sin_addr,
275 gate_buf, INET_ADDRSTRLEN);
276 }
277 switch (error) {
278 /* We only flag nexthops as being in FIB if
279 * rtm_write() did its work. */
280 case ZEBRA_ERR_NOERROR:
281 nexthop_num++;
282 if (IS_ZEBRA_DEBUG_KERNEL)
283 zlog_debug("%s: %s: successfully did NH %s",
284 __func__, prefix_buf, gate_buf);
285 if (cmd == RTM_ADD)
286 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
287 break;
288
289 /* The only valid case for this error is
290 * kernel's failure to install a multipath
291 * route, which is common for FreeBSD. This
292 * should be ignored silently, but logged as an error
293 * otherwise.
294 */
295 case ZEBRA_ERR_RTEXIST:
296 if (cmd != RTM_ADD)
297 flog_err(EC_LIB_SYSTEM_CALL,
298 "%s: rtm_write() returned %d for command %d",
299 __func__, error, cmd);
300 continue;
d62a17ae 301
86afd529
DS
302 /* Note any unexpected status returns */
303 default:
304 flog_err(EC_LIB_SYSTEM_CALL,
305 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
306 __func__,
307 prefix2str(p, prefix_buf,
308 sizeof(prefix_buf)),
309 error, lookup_msg(rtm_type_str, cmd, NULL));
310 break;
311 }
d62a17ae 312 } /* for (ALL_NEXTHOPS(...))*/
313
314 /* If there was no useful nexthop, then complain. */
560e3136
DS
315 if (nexthop_num == 0) {
316 if (IS_ZEBRA_DEBUG_KERNEL)
01ce7cba
MS
317 zlog_debug("%s: No useful nexthops were found in RIB prefix %s",
318 __func__, prefix2str(p, prefix_buf,
319 sizeof(prefix_buf)));
560e3136
DS
320 return 1;
321 }
d62a17ae 322
323 return 0; /*XXX*/
718e3744 324}
325
01ce7cba
MS
326/*
327 * Update or delete a prefix from the kernel,
328 * using info from a dataplane context struct.
329 */
25779064 330enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
01ce7cba
MS
331{
332 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
333
334 if (dplane_ctx_get_src(ctx) != NULL) {
335 zlog_err("route add: IPv6 sourcedest routes unsupported!");
336 res = ZEBRA_DPLANE_REQUEST_FAILURE;
337 goto done;
338 }
339
49ddb66a 340 frr_elevate_privs(&zserv_privs) {
f183e380
MS
341
342 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE)
343 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
344 dplane_ctx_get_ng(ctx),
345 dplane_ctx_get_metric(ctx));
346 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL)
347 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
348 dplane_ctx_get_ng(ctx),
349 dplane_ctx_get_metric(ctx));
350 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
351 /* Must do delete and add separately -
352 * no update available
353 */
354 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
355 dplane_ctx_get_old_ng(ctx),
356 dplane_ctx_get_old_metric(ctx));
357
358 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
359 dplane_ctx_get_ng(ctx),
360 dplane_ctx_get_metric(ctx));
361 } else {
362 zlog_err("Invalid routing socket update op %s (%u)",
363 dplane_op2str(dplane_ctx_get_op(ctx)),
364 dplane_ctx_get_op(ctx));
365 res = ZEBRA_DPLANE_REQUEST_FAILURE;
366 }
367 } /* Elevated privs */
01ce7cba
MS
368
369done:
370
371 return res;
372}
373
d62a17ae 374int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 375 int llalen, ns_id_t ns_id)
6b8a5694 376{
d62a17ae 377 /* TODO */
378 return 0;
6b8a5694 379}
1498c059 380
43b5cc5e 381extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 382{
d62a17ae 383 return 0;
1498c059 384}
13d60d35 385
d62a17ae 386int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 387{
d62a17ae 388 return 0;
13d60d35 389}
390
d62a17ae 391int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 392{
d62a17ae 393 return 0;
13d60d35 394}
2232a77c 395
d62a17ae 396int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
a37f4598 397 struct in_addr vtep_ip, bool sticky)
2232a77c 398{
d62a17ae 399 return 0;
2232a77c 400}
401
d62a17ae 402int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
d63c1b18 403 struct in_addr vtep_ip)
2232a77c 404{
d62a17ae 405 return 0;
2232a77c 406}
407
d62a17ae 408int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
68e33151 409 struct ethaddr *mac, uint8_t flags)
2232a77c 410{
d62a17ae 411 return 0;
2232a77c 412}
413
d62a17ae 414int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 415{
d62a17ae 416 return 0;
2232a77c 417}
e0ae31b8
DS
418
419extern int kernel_interface_set_master(struct interface *master,
420 struct interface *slave)
421{
422 return 0;
423}
0ecfe5bf 424
dc7b3cae
DS
425uint32_t kernel_get_speed(struct interface *ifp)
426{
427 return ifp->speed;
428}
429
ddfeb486 430#endif /* !HAVE_NETLINK */