]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | * | |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
ddfeb486 DL |
23 | |
24 | #ifndef HAVE_NETLINK | |
25 | ||
fe6c7157 | 26 | #ifdef __OpenBSD__ |
d3e2c74a RW |
27 | #include <netmpls/mpls.h> |
28 | #endif | |
718e3744 | 29 | |
30 | #include "if.h" | |
31 | #include "prefix.h" | |
32 | #include "sockunion.h" | |
33 | #include "log.h" | |
edd7c245 | 34 | #include "privs.h" |
13d60d35 | 35 | #include "vxlan.h" |
174482ef | 36 | #include "lib_errors.h" |
718e3744 | 37 | |
38 | #include "zebra/debug.h" | |
39 | #include "zebra/rib.h" | |
6621ca86 | 40 | #include "zebra/rt.h" |
dc95824a | 41 | #include "zebra/kernel_socket.h" |
d3e2c74a | 42 | #include "zebra/zebra_mpls.h" |
364fed6b | 43 | #include "zebra/zebra_errors.h" |
718e3744 | 44 | |
edd7c245 | 45 | extern struct zebra_privs_t zserv_privs; |
46 | ||
5e8c8947 | 47 | #ifdef __OpenBSD__ |
8ecdb26e | 48 | static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label, |
5e8c8947 RW |
49 | struct sockaddr_mpls *smpls) |
50 | { | |
51 | if (nh_label->num_labels > 1) { | |
e914ccbe | 52 | flog_warn(EC_ZEBRA_MAX_LABELS_PUSH, |
9df414fe QY |
53 | "%s: can't push %u labels at " |
54 | "once (maximum is 1)", | |
55 | __func__, nh_label->num_labels); | |
5e8c8947 RW |
56 | return -1; |
57 | } | |
58 | ||
59 | memset(smpls, 0, sizeof(*smpls)); | |
60 | smpls->smpls_len = sizeof(*smpls); | |
61 | smpls->smpls_family = AF_MPLS; | |
62 | smpls->smpls_label = htonl(nh_label->label[0] << MPLS_LABEL_OFFSET); | |
63 | ||
64 | return 0; | |
65 | } | |
66 | #endif | |
67 | ||
718e3744 | 68 | /* Interface between zebra message and rtm message. */ |
08ea27d1 DS |
69 | static int kernel_rtm(int cmd, const struct prefix *p, |
70 | const struct nexthop_group *ng, uint32_t metric) | |
718e3744 | 71 | |
72 | { | |
ca2c70bd | 73 | union sockunion sin_dest, sin_mask, sin_gate; |
fe6c7157 | 74 | #ifdef __OpenBSD__ |
d62a17ae | 75 | struct sockaddr_mpls smpls; |
d3e2c74a | 76 | #endif |
d62a17ae | 77 | union sockunion *smplsp = NULL; |
78 | struct nexthop *nexthop; | |
79 | int nexthop_num = 0; | |
80 | ifindex_t ifindex = 0; | |
4dd39a0e | 81 | bool gate = false; |
d62a17ae | 82 | int error; |
981dc13f | 83 | char gate_buf[INET6_BUFSIZ]; |
d62a17ae | 84 | char prefix_buf[PREFIX_STRLEN]; |
a8309422 | 85 | enum blackhole_type bh_type = BLACKHOLE_UNSPEC; |
d62a17ae | 86 | |
683c06ad | 87 | prefix2str(p, prefix_buf, sizeof(prefix_buf)); |
718e3744 | 88 | |
86afd529 DS |
89 | /* |
90 | * We only have the ability to ADD or DELETE at this point | |
91 | * in time. | |
92 | */ | |
93 | if (cmd != RTM_ADD && cmd != RTM_DELETE) { | |
94 | if (IS_ZEBRA_DEBUG_KERNEL) | |
5ea8213f | 95 | zlog_debug("%s: %s odd command %s", |
86afd529 | 96 | __func__, prefix_buf, |
5ea8213f | 97 | lookup_msg(rtm_type_str, cmd, NULL)); |
86afd529 DS |
98 | return 0; |
99 | } | |
100 | ||
08ea27d1 DS |
101 | memset(&sin_dest, 0, sizeof(sin_dest)); |
102 | memset(&sin_gate, 0, sizeof(sin_gate)); | |
ca2c70bd | 103 | memset(&sin_mask, 0, sizeof(sin_mask)); |
718e3744 | 104 | |
08ea27d1 DS |
105 | switch (p->family) { |
106 | case AF_INET: | |
107 | sin_dest.sin.sin_family = AF_INET; | |
08ea27d1 DS |
108 | sin_dest.sin.sin_addr = p->u.prefix4; |
109 | sin_gate.sin.sin_family = AF_INET; | |
0413dd75 RZ |
110 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
111 | sin_dest.sin.sin_len = sizeof(struct sockaddr_in); | |
112 | sin_gate.sin.sin_len = sizeof(struct sockaddr_in); | |
113 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
08ea27d1 DS |
114 | break; |
115 | case AF_INET6: | |
116 | sin_dest.sin6.sin6_family = AF_INET6; | |
08ea27d1 DS |
117 | sin_dest.sin6.sin6_addr = p->u.prefix6; |
118 | sin_gate.sin6.sin6_family = AF_INET6; | |
0413dd75 RZ |
119 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
120 | sin_dest.sin6.sin6_len = sizeof(struct sockaddr_in6); | |
121 | sin_gate.sin6.sin6_len = sizeof(struct sockaddr_in6); | |
122 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
08ea27d1 DS |
123 | break; |
124 | } | |
718e3744 | 125 | |
d62a17ae | 126 | /* Make gateway. */ |
01ce7cba | 127 | for (ALL_NEXTHOPS_PTR(ng, nexthop)) { |
86afd529 DS |
128 | /* |
129 | * We only want to use the actual good nexthops | |
130 | */ | |
131 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE) || | |
132 | !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
d62a17ae | 133 | continue; |
134 | ||
c2519893 | 135 | smplsp = NULL; |
4dd39a0e | 136 | gate = false; |
981dc13f | 137 | snprintf(gate_buf, sizeof(gate_buf), "NULL"); |
d62a17ae | 138 | |
86afd529 DS |
139 | switch (nexthop->type) { |
140 | case NEXTHOP_TYPE_IPV4: | |
141 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
142 | sin_gate.sin.sin_addr = nexthop->gate.ipv4; | |
143 | sin_gate.sin.sin_family = AF_INET; | |
0413dd75 RZ |
144 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
145 | sin_gate.sin.sin_len = sizeof(struct sockaddr_in); | |
146 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
86afd529 | 147 | ifindex = nexthop->ifindex; |
4dd39a0e | 148 | gate = true; |
86afd529 DS |
149 | break; |
150 | case NEXTHOP_TYPE_IPV6: | |
151 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
152 | sin_gate.sin6.sin6_addr = nexthop->gate.ipv6; | |
153 | sin_gate.sin6.sin6_family = AF_INET6; | |
0413dd75 RZ |
154 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
155 | sin_gate.sin6.sin6_len = sizeof(struct sockaddr_in6); | |
156 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
86afd529 | 157 | ifindex = nexthop->ifindex; |
08ea27d1 DS |
158 | /* Under kame set interface index to link local address */ |
159 | #ifdef KAME | |
160 | ||
161 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ | |
c9277ebb DS |
162 | do { \ |
163 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ | |
164 | (a).s6_addr[3] = (i)&0xff; \ | |
165 | } while (0) | |
08ea27d1 | 166 | |
86afd529 DS |
167 | if (IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6.sin6_addr)) |
168 | SET_IN6_LINKLOCAL_IFINDEX( | |
169 | sin_gate.sin6.sin6_addr, | |
170 | ifindex); | |
08ea27d1 DS |
171 | #endif /* KAME */ |
172 | ||
4dd39a0e | 173 | gate = true; |
86afd529 DS |
174 | break; |
175 | case NEXTHOP_TYPE_IFINDEX: | |
176 | ifindex = nexthop->ifindex; | |
177 | break; | |
178 | case NEXTHOP_TYPE_BLACKHOLE: | |
179 | bh_type = nexthop->bh_type; | |
180 | switch (p->family) { | |
c3d6b386 | 181 | case AF_INET: { |
86afd529 DS |
182 | struct in_addr loopback; |
183 | loopback.s_addr = htonl(INADDR_LOOPBACK); | |
184 | sin_gate.sin.sin_addr = loopback; | |
0413dd75 RZ |
185 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
186 | sin_gate.sin.sin_len = | |
187 | sizeof(struct sockaddr_in); | |
188 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
4dd39a0e | 189 | gate = true; |
86afd529 | 190 | } |
08ea27d1 | 191 | break; |
c3d6b386 DS |
192 | case AF_INET6: |
193 | zlog_warn("v6 blackhole routes have not been programmed yet"); | |
08ea27d1 | 194 | break; |
d62a17ae | 195 | } |
86afd529 DS |
196 | } |
197 | ||
198 | switch (p->family) { | |
199 | case AF_INET: | |
18d10d88 DS |
200 | masklen2ip(p->prefixlen, &sin_mask.sin.sin_addr); |
201 | sin_mask.sin.sin_family = AF_INET; | |
0413dd75 RZ |
202 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
203 | sin_mask.sin.sin_len = sizeof(struct sockaddr_in); | |
204 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
86afd529 DS |
205 | break; |
206 | case AF_INET6: | |
18d10d88 DS |
207 | masklen2ip6(p->prefixlen, &sin_mask.sin6.sin6_addr); |
208 | sin_mask.sin6.sin6_family = AF_INET6; | |
0413dd75 RZ |
209 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
210 | sin_mask.sin6.sin6_len = sizeof(struct sockaddr_in6); | |
211 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
86afd529 DS |
212 | break; |
213 | } | |
718e3744 | 214 | |
fe6c7157 | 215 | #ifdef __OpenBSD__ |
b24204be MS |
216 | if (nexthop->nh_label) { |
217 | if (kernel_rtm_add_labels(nexthop->nh_label, | |
218 | &smpls) != 0) | |
219 | continue; | |
220 | smplsp = (union sockunion *)&smpls; | |
221 | } | |
d3e2c74a | 222 | #endif |
18d10d88 | 223 | error = rtm_write(cmd, &sin_dest, &sin_mask, |
86afd529 DS |
224 | gate ? &sin_gate : NULL, smplsp, |
225 | ifindex, bh_type, metric); | |
226 | ||
227 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
228 | if (!gate) { | |
981dc13f RZ |
229 | zlog_debug( |
230 | "%s: %s: attention! gate not found for re", | |
231 | __func__, prefix_buf); | |
232 | } else { | |
233 | switch (p->family) { | |
c3d6b386 | 234 | case AF_INET: |
981dc13f RZ |
235 | inet_ntop(AF_INET, |
236 | &sin_gate.sin.sin_addr, | |
237 | gate_buf, sizeof(gate_buf)); | |
238 | break; | |
239 | ||
c3d6b386 | 240 | case AF_INET6: |
981dc13f RZ |
241 | inet_ntop(AF_INET6, |
242 | &sin_gate.sin6.sin6_addr, | |
243 | gate_buf, sizeof(gate_buf)); | |
244 | break; | |
245 | ||
246 | default: | |
247 | snprintf(gate_buf, sizeof(gate_buf), | |
248 | "(invalid-af)"); | |
249 | break; | |
250 | } | |
251 | } | |
86afd529 DS |
252 | } |
253 | switch (error) { | |
254 | /* We only flag nexthops as being in FIB if | |
255 | * rtm_write() did its work. */ | |
256 | case ZEBRA_ERR_NOERROR: | |
257 | nexthop_num++; | |
258 | if (IS_ZEBRA_DEBUG_KERNEL) | |
259 | zlog_debug("%s: %s: successfully did NH %s", | |
260 | __func__, prefix_buf, gate_buf); | |
261 | if (cmd == RTM_ADD) | |
262 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); | |
263 | break; | |
264 | ||
265 | /* The only valid case for this error is | |
266 | * kernel's failure to install a multipath | |
267 | * route, which is common for FreeBSD. This | |
268 | * should be ignored silently, but logged as an error | |
269 | * otherwise. | |
270 | */ | |
271 | case ZEBRA_ERR_RTEXIST: | |
272 | if (cmd != RTM_ADD) | |
273 | flog_err(EC_LIB_SYSTEM_CALL, | |
274 | "%s: rtm_write() returned %d for command %d", | |
275 | __func__, error, cmd); | |
276 | continue; | |
d62a17ae | 277 | |
86afd529 DS |
278 | /* Note any unexpected status returns */ |
279 | default: | |
31be7dbb RZ |
280 | flog_err( |
281 | EC_LIB_SYSTEM_CALL, | |
282 | "%s: %s: rtm_write() unexpectedly returned %d for command %s", | |
283 | __func__, prefix_buf, error, | |
284 | lookup_msg(rtm_type_str, cmd, NULL)); | |
86afd529 DS |
285 | break; |
286 | } | |
d62a17ae | 287 | } /* for (ALL_NEXTHOPS(...))*/ |
288 | ||
289 | /* If there was no useful nexthop, then complain. */ | |
560e3136 DS |
290 | if (nexthop_num == 0) { |
291 | if (IS_ZEBRA_DEBUG_KERNEL) | |
31be7dbb RZ |
292 | zlog_debug( |
293 | "%s: No useful nexthops were found in RIB prefix %s", | |
294 | __func__, prefix_buf); | |
560e3136 DS |
295 | return 1; |
296 | } | |
d62a17ae | 297 | |
298 | return 0; /*XXX*/ | |
718e3744 | 299 | } |
300 | ||
01ce7cba MS |
301 | /* |
302 | * Update or delete a prefix from the kernel, | |
303 | * using info from a dataplane context struct. | |
304 | */ | |
25779064 | 305 | enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx) |
01ce7cba MS |
306 | { |
307 | enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS; | |
3cdba47a | 308 | uint32_t type, old_type; |
01ce7cba MS |
309 | |
310 | if (dplane_ctx_get_src(ctx) != NULL) { | |
311 | zlog_err("route add: IPv6 sourcedest routes unsupported!"); | |
e677d109 | 312 | return ZEBRA_DPLANE_REQUEST_FAILURE; |
01ce7cba MS |
313 | } |
314 | ||
3cdba47a DS |
315 | type = dplane_ctx_get_type(ctx); |
316 | old_type = dplane_ctx_get_old_type(ctx); | |
317 | ||
0cf6db21 | 318 | frr_with_privs(&zserv_privs) { |
f183e380 | 319 | |
3cdba47a DS |
320 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) { |
321 | if (!RSYSTEM_ROUTE(type)) | |
322 | kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx), | |
323 | dplane_ctx_get_ng(ctx), | |
324 | dplane_ctx_get_metric(ctx)); | |
325 | } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) { | |
326 | if (!RSYSTEM_ROUTE(type)) | |
327 | kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx), | |
328 | dplane_ctx_get_ng(ctx), | |
329 | dplane_ctx_get_metric(ctx)); | |
330 | } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) { | |
f183e380 MS |
331 | /* Must do delete and add separately - |
332 | * no update available | |
333 | */ | |
3cdba47a DS |
334 | if (!RSYSTEM_ROUTE(old_type)) |
335 | kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx), | |
336 | dplane_ctx_get_old_ng(ctx), | |
337 | dplane_ctx_get_old_metric(ctx)); | |
338 | ||
339 | if (!RSYSTEM_ROUTE(type)) | |
340 | kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx), | |
341 | dplane_ctx_get_ng(ctx), | |
342 | dplane_ctx_get_metric(ctx)); | |
f183e380 MS |
343 | } else { |
344 | zlog_err("Invalid routing socket update op %s (%u)", | |
345 | dplane_op2str(dplane_ctx_get_op(ctx)), | |
346 | dplane_ctx_get_op(ctx)); | |
347 | res = ZEBRA_DPLANE_REQUEST_FAILURE; | |
348 | } | |
349 | } /* Elevated privs */ | |
01ce7cba | 350 | |
3cdba47a DS |
351 | if (RSYSTEM_ROUTE(type) |
352 | && dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) { | |
353 | struct nexthop *nexthop; | |
354 | ||
355 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) { | |
356 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
357 | continue; | |
358 | ||
359 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) { | |
360 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); | |
361 | } | |
362 | } | |
363 | } | |
364 | ||
01ce7cba MS |
365 | return res; |
366 | } | |
367 | ||
07cc1745 SW |
368 | enum zebra_dplane_result kernel_nexthop_update(struct zebra_dplane_ctx *ctx) |
369 | { | |
370 | return ZEBRA_DPLANE_REQUEST_SUCCESS; | |
371 | } | |
372 | ||
d62a17ae | 373 | int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla, |
5895d33f | 374 | int llalen, ns_id_t ns_id) |
6b8a5694 | 375 | { |
d62a17ae | 376 | /* TODO */ |
377 | return 0; | |
6b8a5694 | 378 | } |
1498c059 | 379 | |
931fa60c MS |
380 | /* NYI on routing-socket platforms, but we've always returned 'success'... */ |
381 | enum zebra_dplane_result kernel_neigh_update_ctx(struct zebra_dplane_ctx *ctx) | |
382 | { | |
383 | return ZEBRA_DPLANE_REQUEST_SUCCESS; | |
384 | } | |
385 | ||
43b5cc5e | 386 | extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute) |
1498c059 | 387 | { |
d62a17ae | 388 | return 0; |
1498c059 | 389 | } |
13d60d35 | 390 | |
036d93c0 MS |
391 | /* |
392 | * Update MAC, using dataplane context object. No-op here for now. | |
393 | */ | |
394 | enum zebra_dplane_result kernel_mac_update_ctx(struct zebra_dplane_ctx *ctx) | |
2232a77c | 395 | { |
036d93c0 | 396 | return ZEBRA_DPLANE_REQUEST_SUCCESS; |
2232a77c | 397 | } |
398 | ||
e0ae31b8 DS |
399 | extern int kernel_interface_set_master(struct interface *master, |
400 | struct interface *slave) | |
401 | { | |
402 | return 0; | |
403 | } | |
0ecfe5bf | 404 | |
594c2878 | 405 | uint32_t kernel_get_speed(struct interface *ifp, int *error) |
dc7b3cae DS |
406 | { |
407 | return ifp->speed; | |
408 | } | |
409 | ||
ddfeb486 | 410 | #endif /* !HAVE_NETLINK */ |