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