]>
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> | |
fe6c7157 | 24 | #ifdef __OpenBSD__ |
d3e2c74a RW |
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" | |
edd7c245 | 32 | #include "privs.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 |
62ccf1e5 | 75 | kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib) |
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; | |
fa713d9e CF |
84 | struct nexthop *nexthop, *tnexthop; |
85 | int recursing; | |
718e3744 | 86 | int nexthop_num = 0; |
b892f1dd | 87 | ifindex_t ifindex = 0; |
718e3744 | 88 | int gate = 0; |
89 | int error; | |
35d921cc | 90 | char prefix_buf[PREFIX_STRLEN]; |
718e3744 | 91 | |
dc95824a | 92 | if (IS_ZEBRA_DEBUG_RIB) |
35d921cc | 93 | prefix2str (p, prefix_buf, sizeof(prefix_buf)); |
718e3744 | 94 | memset (&sin_dest, 0, sizeof (struct sockaddr_in)); |
95 | sin_dest.sin_family = AF_INET; | |
6f0e3f6e | 96 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 97 | sin_dest.sin_len = sizeof (struct sockaddr_in); |
6f0e3f6e | 98 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 99 | sin_dest.sin_addr = p->u.prefix4; |
100 | ||
101 | memset (&sin_mask, 0, sizeof (struct sockaddr_in)); | |
102 | ||
103 | memset (&sin_gate, 0, sizeof (struct sockaddr_in)); | |
104 | sin_gate.sin_family = AF_INET; | |
6f0e3f6e | 105 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 106 | sin_gate.sin_len = sizeof (struct sockaddr_in); |
6f0e3f6e | 107 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 108 | |
109 | /* Make gateway. */ | |
fa713d9e | 110 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
718e3744 | 111 | { |
fa713d9e CF |
112 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
113 | continue; | |
114 | ||
718e3744 | 115 | gate = 0; |
dc95824a | 116 | char gate_buf[INET_ADDRSTRLEN] = "NULL"; |
718e3744 | 117 | |
dfdb8f18 GT |
118 | /* |
119 | * XXX We need to refrain from kernel operations in some cases, | |
120 | * but this if statement seems overly cautious - what about | |
121 | * other than ADD and DELETE? | |
122 | */ | |
718e3744 | 123 | if ((cmd == RTM_ADD |
124 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
125 | || (cmd == RTM_DELETE | |
718e3744 | 126 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) |
718e3744 | 127 | )) |
128 | { | |
fa713d9e CF |
129 | if (nexthop->type == NEXTHOP_TYPE_IPV4 || |
130 | nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) | |
718e3744 | 131 | { |
fa713d9e CF |
132 | sin_gate.sin_addr = nexthop->gate.ipv4; |
133 | gate = 1; | |
718e3744 | 134 | } |
fa713d9e | 135 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
fa713d9e CF |
136 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
137 | ifindex = nexthop->ifindex; | |
138 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) | |
718e3744 | 139 | { |
fa713d9e CF |
140 | struct in_addr loopback; |
141 | loopback.s_addr = htonl (INADDR_LOOPBACK); | |
142 | sin_gate.sin_addr = loopback; | |
143 | gate = 1; | |
dfdb8f18 | 144 | } |
718e3744 | 145 | |
718e3744 | 146 | if (gate && p->prefixlen == 32) |
147 | mask = NULL; | |
148 | else | |
149 | { | |
150 | masklen2ip (p->prefixlen, &sin_mask.sin_addr); | |
6083e1f8 | 151 | sin_mask.sin_family = AF_INET; |
6f0e3f6e | 152 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 153 | sin_mask.sin_len = sin_masklen (sin_mask.sin_addr); |
6f0e3f6e | 154 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 155 | mask = &sin_mask; |
156 | } | |
718e3744 | 157 | |
fe6c7157 | 158 | #ifdef __OpenBSD__ |
d3e2c74a RW |
159 | if (nexthop->nh_label) |
160 | { | |
161 | memset (&smpls, 0, sizeof (smpls)); | |
162 | smpls.smpls_len = sizeof (smpls); | |
163 | smpls.smpls_family = AF_MPLS; | |
164 | smpls.smpls_label = | |
165 | htonl (nexthop->nh_label->label[0] << MPLS_LABEL_OFFSET); | |
166 | smplsp = (union sockunion *)&smpls; | |
167 | } | |
168 | #endif | |
169 | ||
dfdb8f18 GT |
170 | error = rtm_write (cmd, |
171 | (union sockunion *)&sin_dest, | |
172 | (union sockunion *)mask, | |
173 | gate ? (union sockunion *)&sin_gate : NULL, | |
d3e2c74a | 174 | smplsp, |
dfdb8f18 GT |
175 | ifindex, |
176 | rib->flags, | |
177 | rib->metric); | |
718e3744 | 178 | |
dc95824a DO |
179 | if (IS_ZEBRA_DEBUG_RIB) |
180 | { | |
181 | if (!gate) | |
182 | { | |
35d921cc TT |
183 | zlog_debug ("%s: %s: attention! gate not found for rib %p", |
184 | __func__, prefix_buf, rib); | |
f7bf4153 | 185 | rib_dump (p, rib); |
dc95824a DO |
186 | } |
187 | else | |
188 | inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN); | |
189 | } | |
190 | ||
191 | switch (error) | |
192 | { | |
193 | /* We only flag nexthops as being in FIB if rtm_write() did its work. */ | |
194 | case ZEBRA_ERR_NOERROR: | |
195 | nexthop_num++; | |
196 | if (IS_ZEBRA_DEBUG_RIB) | |
35d921cc TT |
197 | zlog_debug ("%s: %s: successfully did NH %s", |
198 | __func__, prefix_buf, gate_buf); | |
dc95824a DO |
199 | if (cmd == RTM_ADD) |
200 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
201 | break; | |
202 | ||
203 | /* The only valid case for this error is kernel's failure to install | |
204 | * a multipath route, which is common for FreeBSD. This should be | |
205 | * ignored silently, but logged as an error otherwise. | |
206 | */ | |
207 | case ZEBRA_ERR_RTEXIST: | |
208 | if (cmd != RTM_ADD) | |
209 | zlog_err ("%s: rtm_write() returned %d for command %d", | |
210 | __func__, error, cmd); | |
211 | continue; | |
212 | break; | |
213 | ||
214 | /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't | |
215 | * normal to get any other messages in ANY case. | |
216 | */ | |
217 | case ZEBRA_ERR_RTNOEXIST: | |
218 | case ZEBRA_ERR_RTUNREACH: | |
219 | default: | |
35d921cc TT |
220 | zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s", |
221 | __func__, prefix2str(p, prefix_buf, sizeof(prefix_buf)), | |
222 | error, lookup (rtm_type_str, cmd)); | |
dc95824a DO |
223 | break; |
224 | } | |
225 | } /* if (cmd and flags make sense) */ | |
226 | else | |
227 | if (IS_ZEBRA_DEBUG_RIB) | |
228 | zlog_debug ("%s: odd command %s for flags %d", | |
2d844524 | 229 | __func__, lookup (rtm_type_str, cmd), nexthop->flags); |
fa713d9e | 230 | } /* for (ALL_NEXTHOPS_RO(...))*/ |
dc95824a DO |
231 | |
232 | /* If there was no useful nexthop, then complain. */ | |
233 | if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL) | |
234 | zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib); | |
718e3744 | 235 | |
236 | return 0; /*XXX*/ | |
237 | } | |
238 | ||
746c4f02 | 239 | #ifdef SIN6_LEN |
718e3744 | 240 | /* Calculate sin6_len value for netmask socket value. */ |
6621ca86 | 241 | static int |
718e3744 | 242 | sin6_masklen (struct in6_addr mask) |
243 | { | |
244 | struct sockaddr_in6 sin6; | |
245 | char *p, *lim; | |
246 | int len; | |
247 | ||
718e3744 | 248 | if (IN6_IS_ADDR_UNSPECIFIED (&mask)) |
249 | return sizeof (long); | |
718e3744 | 250 | |
251 | sin6.sin6_addr = mask; | |
252 | len = sizeof (struct sockaddr_in6); | |
253 | ||
254 | lim = (char *) & sin6.sin6_addr; | |
255 | p = lim + sizeof (sin6.sin6_addr); | |
256 | ||
257 | while (*--p == 0 && p >= lim) | |
258 | len--; | |
259 | ||
260 | return len; | |
261 | } | |
746c4f02 | 262 | #endif /* SIN6_LEN */ |
718e3744 | 263 | |
718e3744 | 264 | /* Interface between zebra message and rtm message. */ |
6621ca86 | 265 | static int |
62ccf1e5 | 266 | kernel_rtm_ipv6 (int cmd, struct prefix *p, struct rib *rib) |
718e3744 | 267 | { |
268 | struct sockaddr_in6 *mask; | |
269 | struct sockaddr_in6 sin_dest, sin_mask, sin_gate; | |
fa713d9e CF |
270 | struct nexthop *nexthop, *tnexthop; |
271 | int recursing; | |
718e3744 | 272 | int nexthop_num = 0; |
b892f1dd | 273 | ifindex_t ifindex = 0; |
718e3744 | 274 | int gate = 0; |
275 | int error; | |
276 | ||
277 | memset (&sin_dest, 0, sizeof (struct sockaddr_in6)); | |
278 | sin_dest.sin6_family = AF_INET6; | |
279 | #ifdef SIN6_LEN | |
280 | sin_dest.sin6_len = sizeof (struct sockaddr_in6); | |
281 | #endif /* SIN6_LEN */ | |
282 | sin_dest.sin6_addr = p->u.prefix6; | |
283 | ||
284 | memset (&sin_mask, 0, sizeof (struct sockaddr_in6)); | |
285 | ||
286 | memset (&sin_gate, 0, sizeof (struct sockaddr_in6)); | |
287 | sin_gate.sin6_family = AF_INET6; | |
6f0e3f6e | 288 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 289 | sin_gate.sin6_len = sizeof (struct sockaddr_in6); |
6f0e3f6e | 290 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 291 | |
292 | /* Make gateway. */ | |
fa713d9e | 293 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
718e3744 | 294 | { |
fa713d9e CF |
295 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
296 | continue; | |
297 | ||
718e3744 | 298 | gate = 0; |
299 | ||
300 | if ((cmd == RTM_ADD | |
301 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
302 | || (cmd == RTM_DELETE | |
303 | #if 0 | |
304 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) | |
305 | #endif | |
306 | )) | |
307 | { | |
fa713d9e | 308 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
fa713d9e | 309 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
718e3744 | 310 | { |
fa713d9e CF |
311 | sin_gate.sin6_addr = nexthop->gate.ipv6; |
312 | gate = 1; | |
718e3744 | 313 | } |
fa713d9e | 314 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
fa713d9e CF |
315 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
316 | ifindex = nexthop->ifindex; | |
718e3744 | 317 | |
318 | if (cmd == RTM_ADD) | |
319 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
320 | } | |
321 | ||
322 | /* Under kame set interface index to link local address. */ | |
323 | #ifdef KAME | |
324 | ||
325 | #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \ | |
326 | do { \ | |
327 | (a).s6_addr[2] = ((i) >> 8) & 0xff; \ | |
328 | (a).s6_addr[3] = (i) & 0xff; \ | |
329 | } while (0) | |
330 | ||
331 | if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr)) | |
332 | SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex); | |
333 | #endif /* KAME */ | |
334 | ||
335 | if (gate && p->prefixlen == 128) | |
336 | mask = NULL; | |
337 | else | |
338 | { | |
339 | masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr); | |
6fe70d1b | 340 | sin_mask.sin6_family = AF_INET6; |
718e3744 | 341 | #ifdef SIN6_LEN |
342 | sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr); | |
343 | #endif /* SIN6_LEN */ | |
344 | mask = &sin_mask; | |
345 | } | |
346 | ||
347 | error = rtm_write (cmd, | |
348 | (union sockunion *) &sin_dest, | |
349 | (union sockunion *) mask, | |
350 | gate ? (union sockunion *)&sin_gate : NULL, | |
d3e2c74a | 351 | NULL, |
718e3744 | 352 | ifindex, |
353 | rib->flags, | |
354 | rib->metric); | |
355 | ||
356 | #if 0 | |
357 | if (error) | |
358 | { | |
be717a0a | 359 | zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.", |
718e3744 | 360 | nexthop_num, error); |
361 | } | |
746c4f02 DL |
362 | #else |
363 | (void)error; | |
718e3744 | 364 | #endif |
365 | ||
366 | nexthop_num++; | |
367 | } | |
368 | ||
369 | /* If there is no useful nexthop then return. */ | |
370 | if (nexthop_num == 0) | |
371 | { | |
372 | if (IS_ZEBRA_DEBUG_KERNEL) | |
be717a0a | 373 | zlog_debug ("kernel_rtm_ipv6(): No useful nexthop."); |
718e3744 | 374 | return 0; |
375 | } | |
376 | ||
377 | return 0; /*XXX*/ | |
378 | } | |
379 | ||
62ccf1e5 TT |
380 | static int |
381 | kernel_rtm (int cmd, struct prefix *p, struct rib *rib) | |
382 | { | |
383 | switch (PREFIX_FAMILY(p)) | |
384 | { | |
385 | case AF_INET: | |
386 | return kernel_rtm_ipv4 (cmd, p, rib); | |
387 | case AF_INET6: | |
388 | return kernel_rtm_ipv6 (cmd, p, rib); | |
389 | } | |
390 | return 0; | |
391 | } | |
392 | ||
718e3744 | 393 | int |
be717a0a | 394 | kernel_route_rib (struct prefix *p, struct rib *old, struct rib *new) |
718e3744 | 395 | { |
62ccf1e5 | 396 | int route = 0; |
edd7c245 | 397 | |
398 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
399 | zlog (NULL, LOG_ERR, "Can't raise privileges"); | |
be717a0a | 400 | |
62ccf1e5 TT |
401 | if (old) |
402 | route |= kernel_rtm (RTM_DELETE, p, old); | |
403 | ||
404 | if (new) | |
405 | route |= kernel_rtm (RTM_ADD, p, new); | |
be717a0a | 406 | |
edd7c245 | 407 | if (zserv_privs.change(ZPRIVS_LOWER)) |
408 | zlog (NULL, LOG_ERR, "Can't lower privileges"); | |
409 | ||
410 | return route; | |
718e3744 | 411 | } |
6b8a5694 RW |
412 | |
413 | int | |
414 | kernel_neigh_update (int add, int ifindex, uint32_t addr, char *lla, int llalen) | |
415 | { | |
416 | /* TODO */ | |
417 | return 0; | |
418 | } | |
1498c059 | 419 | |
a7d3a72e | 420 | extern int |
1498c059 DS |
421 | kernel_get_ipmr_sg_stats (void *mroute) |
422 | { | |
6f1d88a7 | 423 | return 0; |
1498c059 | 424 | } |