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