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