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