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