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