blob: 19b2fc2f75d79aa3cba8112b64b036d9b389066c [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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>
24
25#include "if.h"
26#include "prefix.h"
27#include "sockunion.h"
28#include "log.h"
29#include "str.h"
30
31#include "zebra/debug.h"
32#include "zebra/rib.h"
33
34int
35rtm_write (int message,
36 union sockunion *dest,
37 union sockunion *mask,
38 union sockunion *gate,
39 unsigned int index,
40 int zebra_flags,
41 int metric);
42
43/* Adjust netmask socket length. Return value is a adjusted sin_len
44 value. */
45int
46sin_masklen (struct in_addr mask)
47{
48 char *p, *lim;
49 int len;
50 struct sockaddr_in sin;
51
52 if (mask.s_addr == 0)
53 return sizeof (long);
54
55 sin.sin_addr = mask;
56 len = sizeof (struct sockaddr_in);
57
58 lim = (char *) &sin.sin_addr;
59 p = lim + sizeof (sin.sin_addr);
60
61 while (*--p == 0 && p >= lim)
62 len--;
63 return len;
64}
65
66/* Interface between zebra message and rtm message. */
67int
68kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family)
69
70{
71 struct sockaddr_in *mask;
72 struct sockaddr_in sin_dest, sin_mask, sin_gate;
73 struct nexthop *nexthop;
74 int nexthop_num = 0;
75 unsigned int ifindex = 0;
76 int gate = 0;
77 int error;
78
79 memset (&sin_dest, 0, sizeof (struct sockaddr_in));
80 sin_dest.sin_family = AF_INET;
81#ifdef HAVE_SIN_LEN
82 sin_dest.sin_len = sizeof (struct sockaddr_in);
83#endif /* HAVE_SIN_LEN */
84 sin_dest.sin_addr = p->u.prefix4;
85
86 memset (&sin_mask, 0, sizeof (struct sockaddr_in));
87
88 memset (&sin_gate, 0, sizeof (struct sockaddr_in));
89 sin_gate.sin_family = AF_INET;
90#ifdef HAVE_SIN_LEN
91 sin_gate.sin_len = sizeof (struct sockaddr_in);
92#endif /* HAVE_SIN_LEN */
93
94 /* Make gateway. */
95 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
96 {
97 gate = 0;
98
99 if ((cmd == RTM_ADD
100 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
101 || (cmd == RTM_DELETE
102#if 0
103 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
104#endif
105 ))
106 {
107 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
108 {
109 if (nexthop->rtype == NEXTHOP_TYPE_IPV4 ||
110 nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
111 {
112 sin_gate.sin_addr = nexthop->rgate.ipv4;
113 gate = 1;
114 }
115 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
116 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
117 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
118 ifindex = nexthop->rifindex;
119 }
120 else
121 {
122 if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
123 nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
124 {
125 sin_gate.sin_addr = nexthop->gate.ipv4;
126 gate = 1;
127 }
128 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
129 || nexthop->type == NEXTHOP_TYPE_IFNAME
130 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
131 ifindex = nexthop->ifindex;
paul595db7f2003-05-25 21:35:06 +0000132 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
133 {
134 struct in_addr loopback;
135 loopback.s_addr = htonl (INADDR_LOOPBACK);
136 sin_gate.sin_addr = loopback;
137 gate = 1;
138 }
139 }
paul718e3742002-12-13 20:15:29 +0000140
141 if (cmd == RTM_ADD)
142 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
143
144 if (gate && p->prefixlen == 32)
145 mask = NULL;
146 else
147 {
148 masklen2ip (p->prefixlen, &sin_mask.sin_addr);
149 sin_mask.sin_family = AF_UNSPEC;
150#ifdef HAVE_SIN_LEN
151 sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
152#endif /* HAVE_SIN_LEN */
153 mask = &sin_mask;
154 }
155 }
156
157 error = rtm_write (cmd,
158 (union sockunion *)&sin_dest,
159 (union sockunion *)mask,
160 gate ? (union sockunion *)&sin_gate : NULL,
161 ifindex,
162 rib->flags,
163 rib->metric);
164
165#if 0
166 if (error)
167 {
168 zlog_info ("kernel_rtm_ipv4(): nexthop %d add error=%d.",
169 nexthop_num, error);
170 }
171#endif
172
173 nexthop_num++;
174 }
175
176 /* If there is no useful nexthop then return. */
177 if (nexthop_num == 0)
178 {
179 if (IS_ZEBRA_DEBUG_KERNEL)
180 zlog_info ("kernel_rtm_ipv4(): No useful nexthop.");
181 return 0;
182 }
183
184 return 0; /*XXX*/
185}
186
187int
188kernel_add_ipv4 (struct prefix *p, struct rib *rib)
189{
190 return kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET);
191}
192
193int
194kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
195{
196 return kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET);
197}
198
199#ifdef HAVE_IPV6
200
201/* Calculate sin6_len value for netmask socket value. */
202int
203sin6_masklen (struct in6_addr mask)
204{
205 struct sockaddr_in6 sin6;
206 char *p, *lim;
207 int len;
208
209#if defined (INRIA)
210 if (IN_ANYADDR6 (mask))
211 return sizeof (long);
212#else /* ! INRIA */
213 if (IN6_IS_ADDR_UNSPECIFIED (&mask))
214 return sizeof (long);
215#endif /* ! INRIA */
216
217 sin6.sin6_addr = mask;
218 len = sizeof (struct sockaddr_in6);
219
220 lim = (char *) & sin6.sin6_addr;
221 p = lim + sizeof (sin6.sin6_addr);
222
223 while (*--p == 0 && p >= lim)
224 len--;
225
226 return len;
227}
228
229/* Interface between zebra message and rtm message. */
230int
231kernel_rtm_ipv6 (int message, struct prefix_ipv6 *dest,
232 struct in6_addr *gate, int index, int flags)
233{
234 struct sockaddr_in6 *mask;
235 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
236
237 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
238 sin_dest.sin6_family = AF_INET6;
239#ifdef SIN6_LEN
240 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
241#endif /* SIN6_LEN */
242
243 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
244
245 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
246 sin_gate.sin6_family = AF_INET6;
247#ifdef SIN6_LEN
248 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
249#endif /* SIN6_LEN */
250
251 sin_dest.sin6_addr = dest->prefix;
252
253 if (gate)
254 memcpy (&sin_gate.sin6_addr, gate, sizeof (struct in6_addr));
255
256 /* Under kame set interface index to link local address. */
257#ifdef KAME
258
259#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
260 do { \
261 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
262 (a).s6_addr[3] = (i) & 0xff; \
263 } while (0)
264
265 if (gate && IN6_IS_ADDR_LINKLOCAL(gate))
266 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, index);
267#endif /* KAME */
268
269 if (gate && dest->prefixlen == 128)
270 mask = NULL;
271 else
272 {
273 masklen2ip6 (dest->prefixlen, &sin_mask.sin6_addr);
274 sin_mask.sin6_family = AF_UNSPEC;
275#ifdef SIN6_LEN
276 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
277#endif /* SIN6_LEN */
278 mask = &sin_mask;
279 }
280
281 return rtm_write (message,
282 (union sockunion *) &sin_dest,
283 (union sockunion *) mask,
284 gate ? (union sockunion *)&sin_gate : NULL,
285 index,
286 flags,
287 0);
288}
289
290/* Interface between zebra message and rtm message. */
291int
292kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
293 int family)
294{
295 struct sockaddr_in6 *mask;
296 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
297 struct nexthop *nexthop;
298 int nexthop_num = 0;
299 unsigned int ifindex = 0;
300 int gate = 0;
301 int error;
302
303 memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
304 sin_dest.sin6_family = AF_INET6;
305#ifdef SIN6_LEN
306 sin_dest.sin6_len = sizeof (struct sockaddr_in6);
307#endif /* SIN6_LEN */
308 sin_dest.sin6_addr = p->u.prefix6;
309
310 memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
311
312 memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
313 sin_gate.sin6_family = AF_INET6;
314#ifdef HAVE_SIN_LEN
315 sin_gate.sin6_len = sizeof (struct sockaddr_in6);
316#endif /* HAVE_SIN_LEN */
317
318 /* Make gateway. */
319 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
320 {
321 gate = 0;
322
323 if ((cmd == RTM_ADD
324 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
325 || (cmd == RTM_DELETE
326#if 0
327 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
328#endif
329 ))
330 {
331 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
332 {
333 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
334 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
335 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
336 {
337 sin_gate.sin6_addr = nexthop->rgate.ipv6;
338 gate = 1;
339 }
340 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
341 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
342 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
343 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
344 ifindex = nexthop->rifindex;
345 }
346 else
347 {
348 if (nexthop->type == NEXTHOP_TYPE_IPV6
349 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
350 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
351 {
352 sin_gate.sin6_addr = nexthop->gate.ipv6;
353 gate = 1;
354 }
355 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
356 || nexthop->type == NEXTHOP_TYPE_IFNAME
357 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
358 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
359 ifindex = nexthop->ifindex;
360 }
361
362 if (cmd == RTM_ADD)
363 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
364 }
365
366 /* Under kame set interface index to link local address. */
367#ifdef KAME
368
369#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
370 do { \
371 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
372 (a).s6_addr[3] = (i) & 0xff; \
373 } while (0)
374
375 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
376 SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
377#endif /* KAME */
378
379 if (gate && p->prefixlen == 128)
380 mask = NULL;
381 else
382 {
383 masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
384 sin_mask.sin6_family = AF_UNSPEC;
385#ifdef SIN6_LEN
386 sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
387#endif /* SIN6_LEN */
388 mask = &sin_mask;
389 }
390
391 error = rtm_write (cmd,
392 (union sockunion *) &sin_dest,
393 (union sockunion *) mask,
394 gate ? (union sockunion *)&sin_gate : NULL,
395 ifindex,
396 rib->flags,
397 rib->metric);
398
399#if 0
400 if (error)
401 {
402 zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
403 nexthop_num, error);
404 }
405#endif
406
407 nexthop_num++;
408 }
409
410 /* If there is no useful nexthop then return. */
411 if (nexthop_num == 0)
412 {
413 if (IS_ZEBRA_DEBUG_KERNEL)
414 zlog_info ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
415 return 0;
416 }
417
418 return 0; /*XXX*/
419}
420
421int
422kernel_add_ipv6 (struct prefix *p, struct rib *rib)
423{
424 return kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6);
425}
426
427int
428kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
429{
430 return kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
431}
432
433/* Delete IPv6 route from the kernel. */
434int
435kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
436 int index, int flags, int table)
437{
438 return kernel_rtm_ipv6 (RTM_DELETE, dest, gate, index, flags);
439}
440#endif /* HAVE_IPV6 */