]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rt_socket.c
Merge pull request #2608 from pacovn/PVS-Studio_dead_code_1
[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
37 #include "zebra/debug.h"
38 #include "zebra/rib.h"
39 #include "zebra/rt.h"
40 #include "zebra/kernel_socket.h"
41 #include "zebra/zebra_mpls.h"
42
43 extern struct zebra_privs_t zserv_privs;
44
45 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
46 /* Adjust netmask socket length. Return value is a adjusted sin_len
47 value. */
48 static int sin_masklen(struct in_addr mask)
49 {
50 char *p, *lim;
51 int len;
52 struct sockaddr_in sin;
53
54 if (mask.s_addr == 0)
55 return sizeof(long);
56
57 sin.sin_addr = mask;
58 len = sizeof(struct sockaddr_in);
59
60 lim = (char *)&sin.sin_addr;
61 p = lim + sizeof(sin.sin_addr);
62
63 while (*--p == 0 && p >= lim)
64 len--;
65 return len;
66 }
67 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
68
69 #ifdef __OpenBSD__
70 static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
71 struct sockaddr_mpls *smpls)
72 {
73 if (nh_label->num_labels > 1) {
74 zlog_warn(
75 "%s: can't push %u labels at "
76 "once (maximum is 1)",
77 __func__, nh_label->num_labels);
78 return -1;
79 }
80
81 memset(smpls, 0, sizeof(*smpls));
82 smpls->smpls_len = sizeof(*smpls);
83 smpls->smpls_family = AF_MPLS;
84 smpls->smpls_label = htonl(nh_label->label[0] << MPLS_LABEL_OFFSET);
85
86 return 0;
87 }
88 #endif
89
90 /* Interface between zebra message and rtm message. */
91 static int kernel_rtm_ipv4(int cmd, struct prefix *p, struct route_entry *re)
92
93 {
94 struct sockaddr_in *mask = NULL;
95 struct sockaddr_in sin_dest, sin_mask, sin_gate;
96 #ifdef __OpenBSD__
97 struct sockaddr_mpls smpls;
98 #endif
99 union sockunion *smplsp = NULL;
100 struct nexthop *nexthop;
101 int nexthop_num = 0;
102 ifindex_t ifindex = 0;
103 int gate = 0;
104 int error;
105 char prefix_buf[PREFIX_STRLEN];
106 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
107
108 if (IS_ZEBRA_DEBUG_RIB)
109 prefix2str(p, prefix_buf, sizeof(prefix_buf));
110 memset(&sin_dest, 0, sizeof(struct sockaddr_in));
111 sin_dest.sin_family = AF_INET;
112 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
113 sin_dest.sin_len = sizeof(struct sockaddr_in);
114 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
115 sin_dest.sin_addr = p->u.prefix4;
116
117 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
118
119 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
120 sin_gate.sin_family = AF_INET;
121 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
122 sin_gate.sin_len = sizeof(struct sockaddr_in);
123 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
124
125 /* Make gateway. */
126 for (ALL_NEXTHOPS(re->ng, nexthop)) {
127 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
128 continue;
129
130 gate = 0;
131 char gate_buf[INET_ADDRSTRLEN] = "NULL";
132
133 /*
134 * XXX We need to refrain from kernel operations in some cases,
135 * but this if statement seems overly cautious - what about
136 * other than ADD and DELETE?
137 */
138 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
139 || (cmd == RTM_DELETE
140 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))) {
141 if (nexthop->type == NEXTHOP_TYPE_IPV4
142 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
143 sin_gate.sin_addr = nexthop->gate.ipv4;
144 gate = 1;
145 }
146 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
147 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
148 ifindex = nexthop->ifindex;
149 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
150 struct in_addr loopback;
151 loopback.s_addr = htonl(INADDR_LOOPBACK);
152 sin_gate.sin_addr = loopback;
153 bh_type = nexthop->bh_type;
154 gate = 1;
155 }
156
157 if (gate && p->prefixlen == 32)
158 mask = NULL;
159 else {
160 masklen2ip(p->prefixlen, &sin_mask.sin_addr);
161 sin_mask.sin_family = AF_INET;
162 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
163 sin_mask.sin_len =
164 sin_masklen(sin_mask.sin_addr);
165 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
166 mask = &sin_mask;
167 }
168
169 #ifdef __OpenBSD__
170 if (nexthop->nh_label
171 && !kernel_rtm_add_labels(nexthop->nh_label,
172 &smpls))
173 continue;
174 smplsp = (union sockunion *)&smpls;
175 #endif
176
177 error = rtm_write(cmd, (union sockunion *)&sin_dest,
178 (union sockunion *)mask,
179 gate ? (union sockunion *)&sin_gate
180 : NULL,
181 smplsp, ifindex, bh_type, re->metric);
182
183 if (IS_ZEBRA_DEBUG_RIB) {
184 if (!gate) {
185 zlog_debug(
186 "%s: %s: attention! gate not found for re %p",
187 __func__, prefix_buf, re);
188 route_entry_dump(p, NULL, re);
189 } else
190 inet_ntop(AF_INET, &sin_gate.sin_addr,
191 gate_buf, INET_ADDRSTRLEN);
192 }
193
194 switch (error) {
195 /* We only flag nexthops as being in FIB if rtm_write()
196 * did its work. */
197 case ZEBRA_ERR_NOERROR:
198 nexthop_num++;
199 if (IS_ZEBRA_DEBUG_RIB)
200 zlog_debug(
201 "%s: %s: successfully did NH %s",
202 __func__, prefix_buf, gate_buf);
203 break;
204
205 /* The only valid case for this error is kernel's
206 * failure to install
207 * a multipath route, which is common for FreeBSD. This
208 * should be
209 * ignored silently, but logged as an error otherwise.
210 */
211 case ZEBRA_ERR_RTEXIST:
212 if (cmd != RTM_ADD)
213 zlog_err(
214 "%s: rtm_write() returned %d for command %d",
215 __func__, error, cmd);
216 continue;
217 break;
218
219 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
220 * FIB, it isn't
221 * normal to get any other messages in ANY case.
222 */
223 case ZEBRA_ERR_RTNOEXIST:
224 case ZEBRA_ERR_RTUNREACH:
225 default:
226 zlog_err(
227 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
228 __func__,
229 prefix2str(p, prefix_buf,
230 sizeof(prefix_buf)),
231 error,
232 lookup_msg(rtm_type_str, cmd, NULL));
233 break;
234 }
235 } /* if (cmd and flags make sense) */
236 else if (IS_ZEBRA_DEBUG_RIB)
237 zlog_debug("%s: odd command %s for flags %d", __func__,
238 lookup_msg(rtm_type_str, cmd, NULL),
239 nexthop->flags);
240 } /* for (ALL_NEXTHOPS(...))*/
241
242 /* If there was no useful nexthop, then complain. */
243 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
244 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
245 __func__, re);
246
247 return 0; /*XXX*/
248 }
249
250 #ifdef SIN6_LEN
251 /* Calculate sin6_len value for netmask socket value. */
252 static int sin6_masklen(struct in6_addr mask)
253 {
254 struct sockaddr_in6 sin6;
255 char *p, *lim;
256 int len;
257
258 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
259 return sizeof(long);
260
261 sin6.sin6_addr = mask;
262 len = sizeof(struct sockaddr_in6);
263
264 lim = (char *)&sin6.sin6_addr;
265 p = lim + sizeof(sin6.sin6_addr);
266
267 while (*--p == 0 && p >= lim)
268 len--;
269
270 return len;
271 }
272 #endif /* SIN6_LEN */
273
274 /* Interface between zebra message and rtm message. */
275 static int kernel_rtm_ipv6(int cmd, struct prefix *p, struct route_entry *re)
276 {
277 struct sockaddr_in6 *mask;
278 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
279 #ifdef __OpenBSD__
280 struct sockaddr_mpls smpls;
281 #endif
282 union sockunion *smplsp = NULL;
283 struct nexthop *nexthop;
284 int nexthop_num = 0;
285 ifindex_t ifindex = 0;
286 int gate = 0;
287 int error;
288 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
289
290 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
291 sin_dest.sin6_family = AF_INET6;
292 #ifdef SIN6_LEN
293 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
294 #endif /* SIN6_LEN */
295 sin_dest.sin6_addr = p->u.prefix6;
296
297 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
298
299 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
300 sin_gate.sin6_family = AF_INET6;
301 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
302 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
303 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
304
305 /* Make gateway. */
306 for (ALL_NEXTHOPS(re->ng, nexthop)) {
307 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
308 continue;
309
310 gate = 0;
311
312 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
313 || (cmd == RTM_DELETE)) {
314 if (nexthop->type == NEXTHOP_TYPE_IPV6
315 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
316 sin_gate.sin6_addr = nexthop->gate.ipv6;
317 gate = 1;
318 }
319 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
320 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
321 ifindex = nexthop->ifindex;
322
323 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
324 bh_type = nexthop->bh_type;
325 }
326
327 /* Under kame set interface index to link local address. */
328 #ifdef KAME
329
330 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
331 do { \
332 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
333 (a).s6_addr[3] = (i)&0xff; \
334 } while (0)
335
336 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
337 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
338 #endif /* KAME */
339
340 if (gate && p->prefixlen == 128)
341 mask = NULL;
342 else {
343 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
344 sin_mask.sin6_family = AF_INET6;
345 #ifdef SIN6_LEN
346 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
347 #endif /* SIN6_LEN */
348 mask = &sin_mask;
349 }
350
351 #ifdef __OpenBSD__
352 if (nexthop->nh_label
353 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
354 continue;
355 smplsp = (union sockunion *)&smpls;
356 #endif
357
358 error = rtm_write(cmd, (union sockunion *)&sin_dest,
359 (union sockunion *)mask,
360 gate ? (union sockunion *)&sin_gate : NULL,
361 smplsp, ifindex, bh_type, re->metric);
362 (void)error;
363
364 nexthop_num++;
365 }
366
367 /* If there is no useful nexthop then return. */
368 if (nexthop_num == 0) {
369 if (IS_ZEBRA_DEBUG_KERNEL)
370 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
371 return 0;
372 }
373
374 return 0; /*XXX*/
375 }
376
377 static int kernel_rtm(int cmd, struct prefix *p, struct route_entry *re)
378 {
379 switch (PREFIX_FAMILY(p)) {
380 case AF_INET:
381 return kernel_rtm_ipv4(cmd, p, re);
382 case AF_INET6:
383 return kernel_rtm_ipv6(cmd, p, re);
384 }
385 return 0;
386 }
387
388 enum dp_req_result kernel_route_rib(struct route_node *rn,
389 const struct prefix *p,
390 const struct prefix *src_p,
391 struct route_entry *old,
392 struct route_entry *new)
393 {
394 int route = 0;
395
396 if (src_p && src_p->prefixlen) {
397 zlog_err("route add: IPv6 sourcedest routes unsupported!");
398 return DP_REQUEST_FAILURE;
399 }
400
401 if (zserv_privs.change(ZPRIVS_RAISE))
402 zlog_err("Can't raise privileges");
403
404 if (old)
405 route |= kernel_rtm(RTM_DELETE, p, old);
406
407 if (new)
408 route |= kernel_rtm(RTM_ADD, p, new);
409
410 if (zserv_privs.change(ZPRIVS_LOWER))
411 zlog_err("Can't lower privileges");
412
413 if (new) {
414 kernel_route_rib_pass_fail(
415 rn, p, new,
416 (!route) ? DP_INSTALL_SUCCESS
417 : DP_INSTALL_FAILURE);
418 } else {
419 kernel_route_rib_pass_fail(rn, p, old,
420 (!route)
421 ? DP_DELETE_SUCCESS
422 : DP_DELETE_FAILURE);
423 }
424
425 return DP_REQUEST_SUCCESS;
426 }
427
428 int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
429 int llalen, ns_id_t ns_id)
430 {
431 /* TODO */
432 return 0;
433 }
434
435 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
436 {
437 return 0;
438 }
439
440 int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
441 {
442 return 0;
443 }
444
445 int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
446 {
447 return 0;
448 }
449
450 int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
451 struct in_addr vtep_ip, uint8_t sticky)
452 {
453 return 0;
454 }
455
456 int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
457 struct in_addr vtep_ip, int local)
458 {
459 return 0;
460 }
461
462 int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
463 struct ethaddr *mac)
464 {
465 return 0;
466 }
467
468 int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
469 {
470 return 0;
471 }
472
473 extern int kernel_interface_set_master(struct interface *master,
474 struct interface *slave)
475 {
476 return 0;
477 }
478
479 uint32_t kernel_get_speed(struct interface *ifp)
480 {
481 return ifp->speed;
482 }
483
484 #endif /* !HAVE_NETLINK */