]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rt_socket.c
zebra: Move sin6_masklen to earlier in the file
[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 HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
48 /* Adjust netmask socket length. Return value is a adjusted sin_len
49 value. */
50 static int sin_masklen(struct in_addr mask)
51 {
52 char *p, *lim;
53 int len;
54 struct sockaddr_in sin;
55
56 if (mask.s_addr == 0)
57 return sizeof(long);
58
59 sin.sin_addr = mask;
60 len = sizeof(struct sockaddr_in);
61
62 lim = (char *)&sin.sin_addr;
63 p = lim + sizeof(sin.sin_addr);
64
65 while (*--p == 0 && p >= lim)
66 len--;
67 return len;
68 }
69 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
70
71 #ifdef __OpenBSD__
72 static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
73 struct sockaddr_mpls *smpls)
74 {
75 if (nh_label->num_labels > 1) {
76 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
77 "%s: can't push %u labels at "
78 "once (maximum is 1)",
79 __func__, nh_label->num_labels);
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
92 #ifdef SIN6_LEN
93 /* Calculate sin6_len value for netmask socket value. */
94 static 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
116 /* Interface between zebra message and rtm message. */
117 static int kernel_rtm_ipv4(int cmd, const struct prefix *p,
118 const struct nexthop_group *ng, uint32_t metric)
119
120 {
121 union sockunion *mask = NULL;
122 union sockunion sin_dest, sin_mask, sin_gate;
123 #ifdef __OpenBSD__
124 struct sockaddr_mpls smpls;
125 #endif
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];
133 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
134
135 if (IS_ZEBRA_DEBUG_RIB)
136 prefix2str(p, prefix_buf, sizeof(prefix_buf));
137 memset(&sin_dest, 0, sizeof(sin_dest));
138 sin_dest.sin.sin_family = AF_INET;
139 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
140 sin_dest.sin.sin_len = sizeof(sin_dest);
141 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
142 sin_dest.sin.sin_addr = p->u.prefix4;
143
144 memset(&sin_mask, 0, sizeof(sin_mask));
145
146 memset(&sin_gate, 0, sizeof(sin_gate));
147 sin_gate.sin.sin_family = AF_INET;
148 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
149 sin_gate.sin.sin_len = sizeof(sin_gate);
150 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
151
152 /* Make gateway. */
153 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
154 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
155 continue;
156
157 gate = 0;
158 char gate_buf[INET_ADDRSTRLEN] = "NULL";
159
160 /*
161 * XXX We need to refrain from kernel operations in some cases,
162 * but this if statement seems overly cautious - what about
163 * other than ADD and DELETE?
164 */
165 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
166 || (cmd == RTM_DELETE)) {
167 if (nexthop->type == NEXTHOP_TYPE_IPV4
168 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
169 sin_gate.sin.sin_addr = nexthop->gate.ipv4;
170 gate = 1;
171 }
172 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
173 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
174 ifindex = nexthop->ifindex;
175 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
176 struct in_addr loopback;
177 loopback.s_addr = htonl(INADDR_LOOPBACK);
178 sin_gate.sin.sin_addr = loopback;
179 bh_type = nexthop->bh_type;
180 gate = 1;
181 }
182
183 if (gate && p->prefixlen == 32)
184 mask = NULL;
185 else {
186 masklen2ip(p->prefixlen,
187 &sin_mask.sin.sin_addr);
188 sin_mask.sin.sin_family = AF_INET;
189 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
190 sin_mask.sin.sin_len =
191 sin_masklen(sin_mask.sin.sin_addr);
192 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
193 mask = &sin_mask;
194 }
195
196 #ifdef __OpenBSD__
197 if (nexthop->nh_label
198 && !kernel_rtm_add_labels(nexthop->nh_label,
199 &smpls))
200 continue;
201 smplsp = (union sockunion *)&smpls;
202 #endif
203
204 error = rtm_write(cmd, &sin_dest, mask,
205 gate ? &sin_gate : NULL, smplsp,
206 ifindex, bh_type, metric);
207
208 if (IS_ZEBRA_DEBUG_KERNEL) {
209 if (!gate) {
210 zlog_debug(
211 "%s: %s: attention! gate not found for re",
212 __func__, prefix_buf);
213 } else
214 inet_ntop(AF_INET,
215 &sin_gate.sin.sin_addr,
216 gate_buf, INET_ADDRSTRLEN);
217 }
218
219 switch (error) {
220 /* We only flag nexthops as being in FIB if rtm_write()
221 * did its work. */
222 case ZEBRA_ERR_NOERROR:
223 nexthop_num++;
224 if (IS_ZEBRA_DEBUG_KERNEL)
225 zlog_debug(
226 "%s: %s: successfully did NH %s",
227 __func__, prefix_buf, gate_buf);
228
229 if (cmd == RTM_ADD)
230 SET_FLAG(nexthop->flags,
231 NEXTHOP_FLAG_FIB);
232
233 break;
234
235 /* The only valid case for this error is kernel's
236 * failure to install
237 * a multipath route, which is common for FreeBSD. This
238 * should be
239 * ignored silently, but logged as an error otherwise.
240 */
241 case ZEBRA_ERR_RTEXIST:
242 if (cmd != RTM_ADD)
243 flog_err(
244 EC_LIB_SYSTEM_CALL,
245 "%s: rtm_write() returned %d for command %d",
246 __func__, error, cmd);
247 continue;
248
249 /* Note any unexpected status returns */
250 default:
251 flog_err(
252 EC_LIB_SYSTEM_CALL,
253 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
254 __func__,
255 prefix2str(p, prefix_buf,
256 sizeof(prefix_buf)),
257 error,
258 lookup_msg(rtm_type_str, cmd, NULL));
259 break;
260 }
261 } /* if (cmd and flags make sense) */
262 else if (IS_ZEBRA_DEBUG_KERNEL)
263 zlog_debug("%s: odd command %s for flags %d", __func__,
264 lookup_msg(rtm_type_str, cmd, NULL),
265 nexthop->flags);
266 } /* for (ALL_NEXTHOPS(...))*/
267
268 /* If there was no useful nexthop, then complain. */
269 if (nexthop_num == 0) {
270 if (IS_ZEBRA_DEBUG_KERNEL)
271 zlog_debug("%s: No useful nexthops were found in RIB prefix %s",
272 __func__, prefix2str(p, prefix_buf,
273 sizeof(prefix_buf)));
274 return 1;
275 }
276
277 return 0; /*XXX*/
278 }
279
280 /* Interface between zebra message and rtm message. */
281 static int kernel_rtm_ipv6(int cmd, const struct prefix *p,
282 const struct nexthop_group *ng, uint32_t metric)
283 {
284 union sockunion *mask;
285 union sockunion sin_dest, sin_mask, sin_gate;
286 #ifdef __OpenBSD__
287 struct sockaddr_mpls smpls;
288 #endif
289 union sockunion *smplsp = NULL;
290 struct nexthop *nexthop;
291 int nexthop_num = 0;
292 ifindex_t ifindex = 0;
293 int gate = 0;
294 int error;
295 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
296
297 memset(&sin_dest, 0, sizeof(sin_dest));
298 sin_dest.sin6.sin6_family = AF_INET6;
299 #ifdef SIN6_LEN
300 sin_dest.sin6.sin6_len = sizeof(sin_dest);
301 #endif /* SIN6_LEN */
302 sin_dest.sin6.sin6_addr = p->u.prefix6;
303
304 memset(&sin_mask, 0, sizeof(sin_mask));
305
306 memset(&sin_gate, 0, sizeof(sin_gate));
307 sin_gate.sin6.sin6_family = AF_INET6;
308 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
309 sin_gate.sin6.sin6_len = sizeof(sin_gate);
310 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
311
312 /* Make gateway. */
313 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
314 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
315 continue;
316
317 gate = 0;
318
319 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
320 || (cmd == RTM_DELETE)) {
321 if (nexthop->type == NEXTHOP_TYPE_IPV6
322 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
323 sin_gate.sin6.sin6_addr = nexthop->gate.ipv6;
324 gate = 1;
325 }
326 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
327 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
328 ifindex = nexthop->ifindex;
329
330 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
331 bh_type = nexthop->bh_type;
332 }
333
334 /* Under kame set interface index to link local address. */
335 #ifdef KAME
336
337 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
338 do { \
339 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
340 (a).s6_addr[3] = (i)&0xff; \
341 } while (0)
342
343 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6.sin6_addr))
344 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6.sin6_addr,
345 ifindex);
346 #endif /* KAME */
347
348 if (gate && p->prefixlen == 128)
349 mask = NULL;
350 else {
351 masklen2ip6(p->prefixlen, &sin_mask.sin6.sin6_addr);
352 sin_mask.sin6.sin6_family = AF_INET6;
353 #ifdef SIN6_LEN
354 sin_mask.sin6.sin6_len =
355 sin6_masklen(sin_mask.sin6.sin6_addr);
356 #endif /* SIN6_LEN */
357 mask = &sin_mask;
358 }
359
360 #ifdef __OpenBSD__
361 if (nexthop->nh_label
362 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
363 continue;
364 smplsp = (union sockunion *)&smpls;
365 #endif
366
367 error = rtm_write(cmd, &sin_dest, mask, gate ? &sin_gate : NULL,
368 smplsp, ifindex, bh_type, metric);
369
370 /* Update installed nexthop info on success */
371 if ((cmd == RTM_ADD) && (error == ZEBRA_ERR_NOERROR))
372 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
373
374 nexthop_num++;
375 }
376
377 /* If there is no useful nexthop then return. */
378 if (nexthop_num == 0) {
379 if (IS_ZEBRA_DEBUG_KERNEL)
380 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
381 return 1;
382 }
383
384 return 0; /*XXX*/
385 }
386
387 static int kernel_rtm(int cmd, const struct prefix *p,
388 const struct nexthop_group *ng, uint32_t metric)
389 {
390 switch (PREFIX_FAMILY(p)) {
391 case AF_INET:
392 return kernel_rtm_ipv4(cmd, p, ng, metric);
393 case AF_INET6:
394 return kernel_rtm_ipv6(cmd, p, ng, metric);
395 }
396 return 0;
397 }
398
399 /*
400 * Update or delete a prefix from the kernel,
401 * using info from a dataplane context struct.
402 */
403 enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
404 {
405 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
406
407 if (dplane_ctx_get_src(ctx) != NULL) {
408 zlog_err("route add: IPv6 sourcedest routes unsupported!");
409 res = ZEBRA_DPLANE_REQUEST_FAILURE;
410 goto done;
411 }
412
413 frr_elevate_privs(&zserv_privs) {
414
415 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE)
416 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
417 dplane_ctx_get_ng(ctx),
418 dplane_ctx_get_metric(ctx));
419 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL)
420 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
421 dplane_ctx_get_ng(ctx),
422 dplane_ctx_get_metric(ctx));
423 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
424 /* Must do delete and add separately -
425 * no update available
426 */
427 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
428 dplane_ctx_get_old_ng(ctx),
429 dplane_ctx_get_old_metric(ctx));
430
431 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
432 dplane_ctx_get_ng(ctx),
433 dplane_ctx_get_metric(ctx));
434 } else {
435 zlog_err("Invalid routing socket update op %s (%u)",
436 dplane_op2str(dplane_ctx_get_op(ctx)),
437 dplane_ctx_get_op(ctx));
438 res = ZEBRA_DPLANE_REQUEST_FAILURE;
439 }
440 } /* Elevated privs */
441
442 done:
443
444 return res;
445 }
446
447 int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
448 int llalen, ns_id_t ns_id)
449 {
450 /* TODO */
451 return 0;
452 }
453
454 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
455 {
456 return 0;
457 }
458
459 int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
460 {
461 return 0;
462 }
463
464 int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
465 {
466 return 0;
467 }
468
469 int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
470 struct in_addr vtep_ip, bool sticky)
471 {
472 return 0;
473 }
474
475 int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
476 struct in_addr vtep_ip)
477 {
478 return 0;
479 }
480
481 int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
482 struct ethaddr *mac, uint8_t flags)
483 {
484 return 0;
485 }
486
487 int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
488 {
489 return 0;
490 }
491
492 extern int kernel_interface_set_master(struct interface *master,
493 struct interface *slave)
494 {
495 return 0;
496 }
497
498 uint32_t kernel_get_speed(struct interface *ifp)
499 {
500 return ifp->speed;
501 }
502
503 #endif /* !HAVE_NETLINK */