2 * Kernel routing table updates by routing socket.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
24 #include <netmpls/mpls.h>
29 #include "sockunion.h"
33 #include "zebra/debug.h"
34 #include "zebra/rib.h"
36 #include "zebra/kernel_socket.h"
37 #include "zebra/zebra_mpls.h"
39 extern struct zebra_privs_t zserv_privs
;
41 /* kernel socket export */
42 extern int rtm_write (int message
, union sockunion
*dest
,
43 union sockunion
*mask
, union sockunion
*gate
,
44 union sockunion
*mpls
, unsigned int index
,
45 int zebra_flags
, int metric
);
47 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
48 /* Adjust netmask socket length. Return value is a adjusted sin_len
51 sin_masklen (struct in_addr mask
)
55 struct sockaddr_in sin
;
61 len
= sizeof (struct sockaddr_in
);
63 lim
= (char *) &sin
.sin_addr
;
64 p
= lim
+ sizeof (sin
.sin_addr
);
66 while (*--p
== 0 && p
>= lim
)
70 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
72 /* Interface between zebra message and rtm message. */
74 kernel_rtm_ipv4 (int cmd
, struct prefix
*p
, struct route_entry
*re
)
77 struct sockaddr_in
*mask
= NULL
;
78 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
80 struct sockaddr_mpls smpls
;
82 union sockunion
*smplsp
= NULL
;
83 struct nexthop
*nexthop
, *tnexthop
;
86 ifindex_t ifindex
= 0;
89 char prefix_buf
[PREFIX_STRLEN
];
91 if (IS_ZEBRA_DEBUG_RIB
)
92 prefix2str (p
, prefix_buf
, sizeof(prefix_buf
));
93 memset (&sin_dest
, 0, sizeof (struct sockaddr_in
));
94 sin_dest
.sin_family
= AF_INET
;
95 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
96 sin_dest
.sin_len
= sizeof (struct sockaddr_in
);
97 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
98 sin_dest
.sin_addr
= p
->u
.prefix4
;
100 memset (&sin_mask
, 0, sizeof (struct sockaddr_in
));
102 memset (&sin_gate
, 0, sizeof (struct sockaddr_in
));
103 sin_gate
.sin_family
= AF_INET
;
104 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
105 sin_gate
.sin_len
= sizeof (struct sockaddr_in
);
106 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
109 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
111 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
115 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
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?
123 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
124 || (cmd
== RTM_DELETE
125 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)
128 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
129 nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
131 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
134 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
135 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
136 ifindex
= nexthop
->ifindex
;
137 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
)
139 struct in_addr loopback
;
140 loopback
.s_addr
= htonl (INADDR_LOOPBACK
);
141 sin_gate
.sin_addr
= loopback
;
145 if (gate
&& p
->prefixlen
== 32)
149 masklen2ip (p
->prefixlen
, &sin_mask
.sin_addr
);
150 sin_mask
.sin_family
= AF_INET
;
151 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
152 sin_mask
.sin_len
= sin_masklen (sin_mask
.sin_addr
);
153 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
158 if (nexthop
->nh_label
)
160 memset (&smpls
, 0, sizeof (smpls
));
161 smpls
.smpls_len
= sizeof (smpls
);
162 smpls
.smpls_family
= AF_MPLS
;
164 htonl (nexthop
->nh_label
->label
[0] << MPLS_LABEL_OFFSET
);
165 smplsp
= (union sockunion
*)&smpls
;
169 error
= rtm_write (cmd
,
170 (union sockunion
*)&sin_dest
,
171 (union sockunion
*)mask
,
172 gate
? (union sockunion
*)&sin_gate
: NULL
,
178 if (IS_ZEBRA_DEBUG_RIB
)
182 zlog_debug ("%s: %s: attention! gate not found for re %p",
183 __func__
, prefix_buf
, re
);
184 route_entry_dump (p
, NULL
, re
);
187 inet_ntop (AF_INET
, &sin_gate
.sin_addr
, gate_buf
, INET_ADDRSTRLEN
);
192 /* We only flag nexthops as being in FIB if rtm_write() did its work. */
193 case ZEBRA_ERR_NOERROR
:
195 if (IS_ZEBRA_DEBUG_RIB
)
196 zlog_debug ("%s: %s: successfully did NH %s",
197 __func__
, prefix_buf
, gate_buf
);
199 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
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.
206 case ZEBRA_ERR_RTEXIST
:
208 zlog_err ("%s: rtm_write() returned %d for command %d",
209 __func__
, error
, cmd
);
213 /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
214 * normal to get any other messages in ANY case.
216 case ZEBRA_ERR_RTNOEXIST
:
217 case ZEBRA_ERR_RTUNREACH
:
219 zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s",
220 __func__
, prefix2str(p
, prefix_buf
, sizeof(prefix_buf
)),
221 error
, lookup (rtm_type_str
, cmd
));
224 } /* if (cmd and flags make sense) */
226 if (IS_ZEBRA_DEBUG_RIB
)
227 zlog_debug ("%s: odd command %s for flags %d",
228 __func__
, lookup (rtm_type_str
, cmd
), nexthop
->flags
);
229 } /* for (ALL_NEXTHOPS_RO(...))*/
231 /* If there was no useful nexthop, then complain. */
232 if (nexthop_num
== 0 && IS_ZEBRA_DEBUG_KERNEL
)
233 zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__
, re
);
239 /* Calculate sin6_len value for netmask socket value. */
241 sin6_masklen (struct in6_addr mask
)
243 struct sockaddr_in6 sin6
;
247 if (IN6_IS_ADDR_UNSPECIFIED (&mask
))
248 return sizeof (long);
250 sin6
.sin6_addr
= mask
;
251 len
= sizeof (struct sockaddr_in6
);
253 lim
= (char *) & sin6
.sin6_addr
;
254 p
= lim
+ sizeof (sin6
.sin6_addr
);
256 while (*--p
== 0 && p
>= lim
)
261 #endif /* SIN6_LEN */
263 /* Interface between zebra message and rtm message. */
265 kernel_rtm_ipv6 (int cmd
, struct prefix
*p
, struct route_entry
*re
)
267 struct sockaddr_in6
*mask
;
268 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
269 struct nexthop
*nexthop
, *tnexthop
;
272 ifindex_t ifindex
= 0;
276 memset (&sin_dest
, 0, sizeof (struct sockaddr_in6
));
277 sin_dest
.sin6_family
= AF_INET6
;
279 sin_dest
.sin6_len
= sizeof (struct sockaddr_in6
);
280 #endif /* SIN6_LEN */
281 sin_dest
.sin6_addr
= p
->u
.prefix6
;
283 memset (&sin_mask
, 0, sizeof (struct sockaddr_in6
));
285 memset (&sin_gate
, 0, sizeof (struct sockaddr_in6
));
286 sin_gate
.sin6_family
= AF_INET6
;
287 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
288 sin_gate
.sin6_len
= sizeof (struct sockaddr_in6
);
289 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
292 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
294 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
300 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
301 || (cmd
== RTM_DELETE
303 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)
307 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
308 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
310 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
313 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
314 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
315 ifindex
= nexthop
->ifindex
;
318 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
321 /* Under kame set interface index to link local address. */
324 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
326 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
327 (a).s6_addr[3] = (i) & 0xff; \
330 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
331 SET_IN6_LINKLOCAL_IFINDEX (sin_gate
.sin6_addr
, ifindex
);
334 if (gate
&& p
->prefixlen
== 128)
338 masklen2ip6 (p
->prefixlen
, &sin_mask
.sin6_addr
);
339 sin_mask
.sin6_family
= AF_INET6
;
341 sin_mask
.sin6_len
= sin6_masklen (sin_mask
.sin6_addr
);
342 #endif /* SIN6_LEN */
346 error
= rtm_write (cmd
,
347 (union sockunion
*) &sin_dest
,
348 (union sockunion
*) mask
,
349 gate
? (union sockunion
*)&sin_gate
: NULL
,
358 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
368 /* If there is no useful nexthop then return. */
369 if (nexthop_num
== 0)
371 if (IS_ZEBRA_DEBUG_KERNEL
)
372 zlog_debug ("kernel_rtm_ipv6(): No useful nexthop.");
380 kernel_rtm (int cmd
, struct prefix
*p
, struct route_entry
*re
)
382 switch (PREFIX_FAMILY(p
))
385 return kernel_rtm_ipv4 (cmd
, p
, re
);
387 return kernel_rtm_ipv6 (cmd
, p
, re
);
393 kernel_route_rib (struct prefix
*p
, struct prefix
*src_p
,
394 struct route_entry
*old
, struct route_entry
*new)
398 if (src_p
&& src_p
->prefixlen
)
400 zlog_err ("route add: IPv6 sourcedest routes unsupported!");
404 if (zserv_privs
.change(ZPRIVS_RAISE
))
405 zlog_err("Can't raise privileges");
408 route
|= kernel_rtm (RTM_DELETE
, p
, old
);
411 route
|= kernel_rtm (RTM_ADD
, p
, new);
413 if (zserv_privs
.change(ZPRIVS_LOWER
))
414 zlog_err("Can't lower privileges");
420 kernel_neigh_update (int add
, int ifindex
, uint32_t addr
, char *lla
, int llalen
)
427 kernel_get_ipmr_sg_stats (void *mroute
)