]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
Merge pull request #11408 from donaldsharp/common_config
[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
5e8c8947 47#ifdef __OpenBSD__
8ecdb26e 48static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
5e8c8947
RW
49 struct sockaddr_mpls *smpls)
50{
51 if (nh_label->num_labels > 1) {
e914ccbe 52 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
3efd0893 53 "%s: can't push %u labels at once (maximum is 1)",
9df414fe 54 __func__, nh_label->num_labels);
5e8c8947
RW
55 return -1;
56 }
57
58 memset(smpls, 0, sizeof(*smpls));
59 smpls->smpls_len = sizeof(*smpls);
60 smpls->smpls_family = AF_MPLS;
61 smpls->smpls_label = htonl(nh_label->label[0] << MPLS_LABEL_OFFSET);
62
63 return 0;
64}
65#endif
66
718e3744 67/* Interface between zebra message and rtm message. */
08ea27d1
DS
68static int kernel_rtm(int cmd, const struct prefix *p,
69 const struct nexthop_group *ng, uint32_t metric)
718e3744 70
71{
ca2c70bd 72 union sockunion sin_dest, sin_mask, sin_gate;
fe6c7157 73#ifdef __OpenBSD__
d62a17ae 74 struct sockaddr_mpls smpls;
d3e2c74a 75#endif
d62a17ae 76 union sockunion *smplsp = NULL;
77 struct nexthop *nexthop;
78 int nexthop_num = 0;
79 ifindex_t ifindex = 0;
4dd39a0e 80 bool gate = false;
d62a17ae 81 int error;
981dc13f 82 char gate_buf[INET6_BUFSIZ];
a8309422 83 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 84
86afd529
DS
85 /*
86 * We only have the ability to ADD or DELETE at this point
87 * in time.
88 */
89 if (cmd != RTM_ADD && cmd != RTM_DELETE) {
90 if (IS_ZEBRA_DEBUG_KERNEL)
2dbe669b 91 zlog_debug("%s: %pFX odd command %s", __func__, p,
5ea8213f 92 lookup_msg(rtm_type_str, cmd, NULL));
86afd529
DS
93 return 0;
94 }
95
08ea27d1
DS
96 memset(&sin_dest, 0, sizeof(sin_dest));
97 memset(&sin_gate, 0, sizeof(sin_gate));
ca2c70bd 98 memset(&sin_mask, 0, sizeof(sin_mask));
718e3744 99
08ea27d1
DS
100 switch (p->family) {
101 case AF_INET:
102 sin_dest.sin.sin_family = AF_INET;
08ea27d1
DS
103 sin_dest.sin.sin_addr = p->u.prefix4;
104 sin_gate.sin.sin_family = AF_INET;
0413dd75
RZ
105#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
106 sin_dest.sin.sin_len = sizeof(struct sockaddr_in);
107 sin_gate.sin.sin_len = sizeof(struct sockaddr_in);
108#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
08ea27d1
DS
109 break;
110 case AF_INET6:
111 sin_dest.sin6.sin6_family = AF_INET6;
08ea27d1
DS
112 sin_dest.sin6.sin6_addr = p->u.prefix6;
113 sin_gate.sin6.sin6_family = AF_INET6;
0413dd75
RZ
114#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
115 sin_dest.sin6.sin6_len = sizeof(struct sockaddr_in6);
116 sin_gate.sin6.sin6_len = sizeof(struct sockaddr_in6);
117#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
08ea27d1
DS
118 break;
119 }
718e3744 120
d62a17ae 121 /* Make gateway. */
01ce7cba 122 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
86afd529
DS
123 /*
124 * We only want to use the actual good nexthops
125 */
126 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ||
127 !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
d62a17ae 128 continue;
129
c2519893 130 smplsp = NULL;
4dd39a0e 131 gate = false;
981dc13f 132 snprintf(gate_buf, sizeof(gate_buf), "NULL");
d62a17ae 133
86afd529
DS
134 switch (nexthop->type) {
135 case NEXTHOP_TYPE_IPV4:
136 case NEXTHOP_TYPE_IPV4_IFINDEX:
137 sin_gate.sin.sin_addr = nexthop->gate.ipv4;
138 sin_gate.sin.sin_family = AF_INET;
0413dd75
RZ
139#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
140 sin_gate.sin.sin_len = sizeof(struct sockaddr_in);
141#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529 142 ifindex = nexthop->ifindex;
4dd39a0e 143 gate = true;
86afd529
DS
144 break;
145 case NEXTHOP_TYPE_IPV6:
146 case NEXTHOP_TYPE_IPV6_IFINDEX:
147 sin_gate.sin6.sin6_addr = nexthop->gate.ipv6;
148 sin_gate.sin6.sin6_family = AF_INET6;
0413dd75
RZ
149#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
150 sin_gate.sin6.sin6_len = sizeof(struct sockaddr_in6);
151#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529 152 ifindex = nexthop->ifindex;
08ea27d1
DS
153/* Under kame set interface index to link local address */
154#ifdef KAME
155
156#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
c9277ebb
DS
157 do { \
158 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
159 (a).s6_addr[3] = (i)&0xff; \
160 } while (0)
08ea27d1 161
86afd529
DS
162 if (IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6.sin6_addr))
163 SET_IN6_LINKLOCAL_IFINDEX(
164 sin_gate.sin6.sin6_addr,
165 ifindex);
08ea27d1
DS
166#endif /* KAME */
167
4dd39a0e 168 gate = true;
86afd529
DS
169 break;
170 case NEXTHOP_TYPE_IFINDEX:
171 ifindex = nexthop->ifindex;
172 break;
173 case NEXTHOP_TYPE_BLACKHOLE:
174 bh_type = nexthop->bh_type;
175 switch (p->family) {
c3d6b386 176 case AF_INET: {
86afd529 177 struct in_addr loopback;
381477e8 178
86afd529
DS
179 loopback.s_addr = htonl(INADDR_LOOPBACK);
180 sin_gate.sin.sin_addr = loopback;
0413dd75
RZ
181#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
182 sin_gate.sin.sin_len =
183 sizeof(struct sockaddr_in);
184#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
4dd39a0e 185 gate = true;
381477e8
DS
186 } break;
187 case AF_INET6: {
188 struct in6_addr loopback;
189
190 inet_pton(AF_INET6, "::1", &loopback);
191
192 sin_gate.sin6.sin6_addr = loopback;
193 sin_gate.sin6.sin6_family = AF_INET6;
194
195#ifdef HAVE_STRUCTSOCKADDR_SA_LEN
196 sin_gate.sin6.sin6_len =
197 sizeof(struct sockaddr_in6);
198#endif /* HAVE_STRUCTSOCKADDR_SA_LEN */
199 gate = true;
200 } break;
d62a17ae 201 }
86afd529
DS
202 }
203
204 switch (p->family) {
205 case AF_INET:
18d10d88
DS
206 masklen2ip(p->prefixlen, &sin_mask.sin.sin_addr);
207 sin_mask.sin.sin_family = AF_INET;
0413dd75
RZ
208#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
209 sin_mask.sin.sin_len = sizeof(struct sockaddr_in);
210#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529
DS
211 break;
212 case AF_INET6:
18d10d88
DS
213 masklen2ip6(p->prefixlen, &sin_mask.sin6.sin6_addr);
214 sin_mask.sin6.sin6_family = AF_INET6;
0413dd75
RZ
215#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
216 sin_mask.sin6.sin6_len = sizeof(struct sockaddr_in6);
217#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529
DS
218 break;
219 }
718e3744 220
fe6c7157 221#ifdef __OpenBSD__
b24204be
MS
222 if (nexthop->nh_label) {
223 if (kernel_rtm_add_labels(nexthop->nh_label,
224 &smpls) != 0)
225 continue;
226 smplsp = (union sockunion *)&smpls;
227 }
d3e2c74a 228#endif
18d10d88 229 error = rtm_write(cmd, &sin_dest, &sin_mask,
86afd529
DS
230 gate ? &sin_gate : NULL, smplsp,
231 ifindex, bh_type, metric);
232
233 if (IS_ZEBRA_DEBUG_KERNEL) {
234 if (!gate) {
981dc13f 235 zlog_debug(
2dbe669b
DA
236 "%s: %pFX: attention! gate not found for re",
237 __func__, p);
981dc13f
RZ
238 } else {
239 switch (p->family) {
c3d6b386 240 case AF_INET:
981dc13f
RZ
241 inet_ntop(AF_INET,
242 &sin_gate.sin.sin_addr,
243 gate_buf, sizeof(gate_buf));
244 break;
245
c3d6b386 246 case AF_INET6:
981dc13f
RZ
247 inet_ntop(AF_INET6,
248 &sin_gate.sin6.sin6_addr,
249 gate_buf, sizeof(gate_buf));
250 break;
251
252 default:
253 snprintf(gate_buf, sizeof(gate_buf),
254 "(invalid-af)");
255 break;
256 }
257 }
86afd529
DS
258 }
259 switch (error) {
260 /* We only flag nexthops as being in FIB if
261 * rtm_write() did its work. */
262 case ZEBRA_ERR_NOERROR:
263 nexthop_num++;
264 if (IS_ZEBRA_DEBUG_KERNEL)
2dbe669b
DA
265 zlog_debug("%s: %pFX: successfully did NH %s",
266 __func__, p, gate_buf);
86afd529
DS
267 if (cmd == RTM_ADD)
268 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
269 break;
270
271 /* The only valid case for this error is
272 * kernel's failure to install a multipath
273 * route, which is common for FreeBSD. This
274 * should be ignored silently, but logged as an error
275 * otherwise.
276 */
277 case ZEBRA_ERR_RTEXIST:
278 if (cmd != RTM_ADD)
279 flog_err(EC_LIB_SYSTEM_CALL,
280 "%s: rtm_write() returned %d for command %d",
281 __func__, error, cmd);
282 continue;
d62a17ae 283
86afd529 284 /* Note any unexpected status returns */
f2f2a16a
DS
285 case ZEBRA_ERR_RTNOEXIST:
286 if (cmd != RTM_DELETE)
287 flog_err(EC_LIB_SYSTEM_CALL,
288 "%s: rtm_write() returned %d for command %d",
289 __func__, error, cmd);
290 break;
86afd529 291 default:
31be7dbb
RZ
292 flog_err(
293 EC_LIB_SYSTEM_CALL,
2dbe669b
DA
294 "%s: %pFX: rtm_write() unexpectedly returned %d for command %s",
295 __func__, p, error,
31be7dbb 296 lookup_msg(rtm_type_str, cmd, NULL));
86afd529
DS
297 break;
298 }
d62a17ae 299 } /* for (ALL_NEXTHOPS(...))*/
300
301 /* If there was no useful nexthop, then complain. */
560e3136
DS
302 if (nexthop_num == 0) {
303 if (IS_ZEBRA_DEBUG_KERNEL)
31be7dbb 304 zlog_debug(
2dbe669b
DA
305 "%s: No useful nexthops were found in RIB prefix %pFX",
306 __func__, p);
560e3136
DS
307 return 1;
308 }
d62a17ae 309
310 return 0; /*XXX*/
718e3744 311}
312
01ce7cba
MS
313/*
314 * Update or delete a prefix from the kernel,
315 * using info from a dataplane context struct.
316 */
25779064 317enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
01ce7cba
MS
318{
319 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3cdba47a 320 uint32_t type, old_type;
01ce7cba
MS
321
322 if (dplane_ctx_get_src(ctx) != NULL) {
323 zlog_err("route add: IPv6 sourcedest routes unsupported!");
e677d109 324 return ZEBRA_DPLANE_REQUEST_FAILURE;
01ce7cba
MS
325 }
326
3cdba47a
DS
327 type = dplane_ctx_get_type(ctx);
328 old_type = dplane_ctx_get_old_type(ctx);
329
0cf6db21 330 frr_with_privs(&zserv_privs) {
f183e380 331
3cdba47a
DS
332 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
333 if (!RSYSTEM_ROUTE(type))
334 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
335 dplane_ctx_get_ng(ctx),
336 dplane_ctx_get_metric(ctx));
337 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) {
338 if (!RSYSTEM_ROUTE(type))
339 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
340 dplane_ctx_get_ng(ctx),
341 dplane_ctx_get_metric(ctx));
342 } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
f183e380
MS
343 /* Must do delete and add separately -
344 * no update available
345 */
3cdba47a
DS
346 if (!RSYSTEM_ROUTE(old_type))
347 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
348 dplane_ctx_get_old_ng(ctx),
349 dplane_ctx_get_old_metric(ctx));
350
351 if (!RSYSTEM_ROUTE(type))
352 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
353 dplane_ctx_get_ng(ctx),
354 dplane_ctx_get_metric(ctx));
f183e380
MS
355 } else {
356 zlog_err("Invalid routing socket update op %s (%u)",
357 dplane_op2str(dplane_ctx_get_op(ctx)),
358 dplane_ctx_get_op(ctx));
359 res = ZEBRA_DPLANE_REQUEST_FAILURE;
360 }
361 } /* Elevated privs */
01ce7cba 362
01ce7cba
MS
363 return res;
364}
365
07cc1745
SW
366enum zebra_dplane_result kernel_nexthop_update(struct zebra_dplane_ctx *ctx)
367{
368 return ZEBRA_DPLANE_REQUEST_SUCCESS;
369}
370
7723e8d3
PG
371int kernel_neigh_register(vrf_id_t vrf_id, struct zserv *client, bool reg)
372{
373 /* TODO */
374 return 0;
375}
376
05657ec2
PG
377int kernel_neigh_update(int add, int ifindex, void *addr, char *lla, int llalen,
378 ns_id_t ns_id, uint8_t family, bool permanent)
6b8a5694 379{
d62a17ae 380 /* TODO */
381 return 0;
6b8a5694 382}
1498c059 383
931fa60c
MS
384/* NYI on routing-socket platforms, but we've always returned 'success'... */
385enum zebra_dplane_result kernel_neigh_update_ctx(struct zebra_dplane_ctx *ctx)
386{
387 return ZEBRA_DPLANE_REQUEST_SUCCESS;
388}
389
43b5cc5e 390extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 391{
d62a17ae 392 return 0;
1498c059 393}
13d60d35 394
036d93c0
MS
395/*
396 * Update MAC, using dataplane context object. No-op here for now.
397 */
398enum zebra_dplane_result kernel_mac_update_ctx(struct zebra_dplane_ctx *ctx)
2232a77c 399{
036d93c0 400 return ZEBRA_DPLANE_REQUEST_SUCCESS;
2232a77c 401}
402
e0ae31b8
DS
403extern int kernel_interface_set_master(struct interface *master,
404 struct interface *slave)
405{
406 return 0;
407}
0ecfe5bf 408
594c2878 409uint32_t kernel_get_speed(struct interface *ifp, int *error)
dc7b3cae
DS
410{
411 return ifp->speed;
412}
413
265fa78d
AK
414int kernel_upd_mac_nh(uint32_t nh_id, struct in_addr vtep_ip)
415{
416 return 0;
417}
418
419int kernel_del_mac_nh(uint32_t nh_id)
420{
421 return 0;
422}
423
424int kernel_upd_mac_nhg(uint32_t nhg_id, uint32_t nh_cnt,
425 struct nh_grp *nh_ids)
426{
427 return 0;
428}
429
430int kernel_del_mac_nhg(uint32_t nhg_id)
431{
432 return 0;
433}
434
ddfeb486 435#endif /* !HAVE_NETLINK */