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
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
25 #include <netmpls/mpls.h>
30 #include "sockunion.h"
34 #include "zebra/debug.h"
35 #include "zebra/rib.h"
37 #include "zebra/kernel_socket.h"
38 #include "zebra/zebra_mpls.h"
40 extern struct zebra_privs_t zserv_privs
;
42 /* kernel socket export */
43 extern int rtm_write (int message
, union sockunion
*dest
,
44 union sockunion
*mask
, union sockunion
*gate
,
45 union sockunion
*mpls
, unsigned int index
,
46 int zebra_flags
, int metric
);
48 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
49 /* Adjust netmask socket length. Return value is a adjusted sin_len
52 sin_masklen (struct in_addr mask
)
56 struct sockaddr_in sin
;
62 len
= sizeof (struct sockaddr_in
);
64 lim
= (char *) &sin
.sin_addr
;
65 p
= lim
+ sizeof (sin
.sin_addr
);
67 while (*--p
== 0 && p
>= lim
)
71 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
73 /* Interface between zebra message and rtm message. */
75 kernel_rtm_ipv4 (int cmd
, struct prefix
*p
, struct rib
*rib
)
78 struct sockaddr_in
*mask
= NULL
;
79 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
81 struct sockaddr_mpls smpls
;
83 union sockunion
*smplsp
= NULL
;
84 struct nexthop
*nexthop
, *tnexthop
;
87 ifindex_t ifindex
= 0;
90 char prefix_buf
[PREFIX_STRLEN
];
92 if (IS_ZEBRA_DEBUG_RIB
)
93 prefix2str (p
, prefix_buf
, sizeof(prefix_buf
));
94 memset (&sin_dest
, 0, sizeof (struct sockaddr_in
));
95 sin_dest
.sin_family
= AF_INET
;
96 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
97 sin_dest
.sin_len
= sizeof (struct sockaddr_in
);
98 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
99 sin_dest
.sin_addr
= p
->u
.prefix4
;
101 memset (&sin_mask
, 0, sizeof (struct sockaddr_in
));
103 memset (&sin_gate
, 0, sizeof (struct sockaddr_in
));
104 sin_gate
.sin_family
= AF_INET
;
105 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
106 sin_gate
.sin_len
= sizeof (struct sockaddr_in
);
107 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
110 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
112 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
116 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
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?
124 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
125 || (cmd
== RTM_DELETE
126 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)
129 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
130 nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
132 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
135 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
136 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
137 ifindex
= nexthop
->ifindex
;
138 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
)
140 struct in_addr loopback
;
141 loopback
.s_addr
= htonl (INADDR_LOOPBACK
);
142 sin_gate
.sin_addr
= loopback
;
146 if (gate
&& p
->prefixlen
== 32)
150 masklen2ip (p
->prefixlen
, &sin_mask
.sin_addr
);
151 sin_mask
.sin_family
= AF_INET
;
152 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
153 sin_mask
.sin_len
= sin_masklen (sin_mask
.sin_addr
);
154 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
159 if (nexthop
->nh_label
)
161 memset (&smpls
, 0, sizeof (smpls
));
162 smpls
.smpls_len
= sizeof (smpls
);
163 smpls
.smpls_family
= AF_MPLS
;
165 htonl (nexthop
->nh_label
->label
[0] << MPLS_LABEL_OFFSET
);
166 smplsp
= (union sockunion
*)&smpls
;
170 error
= rtm_write (cmd
,
171 (union sockunion
*)&sin_dest
,
172 (union sockunion
*)mask
,
173 gate
? (union sockunion
*)&sin_gate
: NULL
,
179 if (IS_ZEBRA_DEBUG_RIB
)
183 zlog_debug ("%s: %s: attention! gate not found for rib %p",
184 __func__
, prefix_buf
, rib
);
188 inet_ntop (AF_INET
, &sin_gate
.sin_addr
, gate_buf
, INET_ADDRSTRLEN
);
193 /* We only flag nexthops as being in FIB if rtm_write() did its work. */
194 case ZEBRA_ERR_NOERROR
:
196 if (IS_ZEBRA_DEBUG_RIB
)
197 zlog_debug ("%s: %s: successfully did NH %s",
198 __func__
, prefix_buf
, gate_buf
);
200 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
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.
207 case ZEBRA_ERR_RTEXIST
:
209 zlog_err ("%s: rtm_write() returned %d for command %d",
210 __func__
, error
, cmd
);
214 /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
215 * normal to get any other messages in ANY case.
217 case ZEBRA_ERR_RTNOEXIST
:
218 case ZEBRA_ERR_RTUNREACH
:
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
));
225 } /* if (cmd and flags make sense) */
227 if (IS_ZEBRA_DEBUG_RIB
)
228 zlog_debug ("%s: odd command %s for flags %d",
229 __func__
, lookup (rtm_type_str
, cmd
), nexthop
->flags
);
230 } /* for (ALL_NEXTHOPS_RO(...))*/
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
);
240 /* Calculate sin6_len value for netmask socket value. */
242 sin6_masklen (struct in6_addr mask
)
244 struct sockaddr_in6 sin6
;
248 if (IN6_IS_ADDR_UNSPECIFIED (&mask
))
249 return sizeof (long);
251 sin6
.sin6_addr
= mask
;
252 len
= sizeof (struct sockaddr_in6
);
254 lim
= (char *) & sin6
.sin6_addr
;
255 p
= lim
+ sizeof (sin6
.sin6_addr
);
257 while (*--p
== 0 && p
>= lim
)
262 #endif /* SIN6_LEN */
264 /* Interface between zebra message and rtm message. */
266 kernel_rtm_ipv6 (int cmd
, struct prefix
*p
, struct rib
*rib
)
268 struct sockaddr_in6
*mask
;
269 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
270 struct nexthop
*nexthop
, *tnexthop
;
273 ifindex_t ifindex
= 0;
277 memset (&sin_dest
, 0, sizeof (struct sockaddr_in6
));
278 sin_dest
.sin6_family
= AF_INET6
;
280 sin_dest
.sin6_len
= sizeof (struct sockaddr_in6
);
281 #endif /* SIN6_LEN */
282 sin_dest
.sin6_addr
= p
->u
.prefix6
;
284 memset (&sin_mask
, 0, sizeof (struct sockaddr_in6
));
286 memset (&sin_gate
, 0, sizeof (struct sockaddr_in6
));
287 sin_gate
.sin6_family
= AF_INET6
;
288 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
289 sin_gate
.sin6_len
= sizeof (struct sockaddr_in6
);
290 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
293 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
295 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
301 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
302 || (cmd
== RTM_DELETE
304 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)
308 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
309 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
311 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
314 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
315 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
316 ifindex
= nexthop
->ifindex
;
319 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
322 /* Under kame set interface index to link local address. */
325 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
327 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
328 (a).s6_addr[3] = (i) & 0xff; \
331 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
332 SET_IN6_LINKLOCAL_IFINDEX (sin_gate
.sin6_addr
, ifindex
);
335 if (gate
&& p
->prefixlen
== 128)
339 masklen2ip6 (p
->prefixlen
, &sin_mask
.sin6_addr
);
340 sin_mask
.sin6_family
= AF_INET6
;
342 sin_mask
.sin6_len
= sin6_masklen (sin_mask
.sin6_addr
);
343 #endif /* SIN6_LEN */
347 error
= rtm_write (cmd
,
348 (union sockunion
*) &sin_dest
,
349 (union sockunion
*) mask
,
350 gate
? (union sockunion
*)&sin_gate
: NULL
,
359 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
369 /* If there is no useful nexthop then return. */
370 if (nexthop_num
== 0)
372 if (IS_ZEBRA_DEBUG_KERNEL
)
373 zlog_debug ("kernel_rtm_ipv6(): No useful nexthop.");
381 kernel_rtm (int cmd
, struct prefix
*p
, struct rib
*rib
)
383 switch (PREFIX_FAMILY(p
))
386 return kernel_rtm_ipv4 (cmd
, p
, rib
);
388 return kernel_rtm_ipv6 (cmd
, p
, rib
);
394 kernel_route_rib (struct prefix
*p
, struct rib
*old
, struct rib
*new)
398 if (zserv_privs
.change(ZPRIVS_RAISE
))
399 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
402 route
|= kernel_rtm (RTM_DELETE
, p
, old
);
405 route
|= kernel_rtm (RTM_ADD
, p
, new);
407 if (zserv_privs
.change(ZPRIVS_LOWER
))
408 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
414 kernel_neigh_update (int add
, int ifindex
, uint32_t addr
, char *lla
, int llalen
)
421 kernel_get_ipmr_sg_stats (void *mroute
)