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