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
27 #include <netmpls/mpls.h>
32 #include "sockunion.h"
37 #include "zebra/debug.h"
38 #include "zebra/rib.h"
40 #include "zebra/kernel_socket.h"
41 #include "zebra/zebra_mpls.h"
43 extern struct zebra_privs_t zserv_privs
;
45 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
46 /* Adjust netmask socket length. Return value is a adjusted sin_len
48 static int sin_masklen(struct in_addr mask
)
52 struct sockaddr_in sin
;
58 len
= sizeof(struct sockaddr_in
);
60 lim
= (char *)&sin
.sin_addr
;
61 p
= lim
+ sizeof(sin
.sin_addr
);
63 while (*--p
== 0 && p
>= lim
)
67 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
70 static int kernel_rtm_add_labels(struct mpls_label_stack
*nh_label
,
71 struct sockaddr_mpls
*smpls
)
73 if (nh_label
->num_labels
> 1) {
75 "%s: can't push %u labels at "
76 "once (maximum is 1)",
77 __func__
, nh_label
->num_labels
);
81 memset(smpls
, 0, sizeof(*smpls
));
82 smpls
->smpls_len
= sizeof(*smpls
);
83 smpls
->smpls_family
= AF_MPLS
;
84 smpls
->smpls_label
= htonl(nh_label
->label
[0] << MPLS_LABEL_OFFSET
);
90 /* Interface between zebra message and rtm message. */
91 static int kernel_rtm_ipv4(int cmd
, const struct prefix
*p
,
92 struct route_entry
*re
)
95 struct sockaddr_in
*mask
= NULL
;
96 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
98 struct sockaddr_mpls smpls
;
100 union sockunion
*smplsp
= NULL
;
101 struct nexthop
*nexthop
;
103 ifindex_t ifindex
= 0;
106 char prefix_buf
[PREFIX_STRLEN
];
107 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
109 if (IS_ZEBRA_DEBUG_RIB
)
110 prefix2str(p
, prefix_buf
, sizeof(prefix_buf
));
111 memset(&sin_dest
, 0, sizeof(struct sockaddr_in
));
112 sin_dest
.sin_family
= AF_INET
;
113 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
114 sin_dest
.sin_len
= sizeof(struct sockaddr_in
);
115 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
116 sin_dest
.sin_addr
= p
->u
.prefix4
;
118 memset(&sin_mask
, 0, sizeof(struct sockaddr_in
));
120 memset(&sin_gate
, 0, sizeof(struct sockaddr_in
));
121 sin_gate
.sin_family
= AF_INET
;
122 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
123 sin_gate
.sin_len
= sizeof(struct sockaddr_in
);
124 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
127 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
128 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
132 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
135 * XXX We need to refrain from kernel operations in some cases,
136 * but this if statement seems overly cautious - what about
137 * other than ADD and DELETE?
139 if ((cmd
== RTM_ADD
&& NEXTHOP_IS_ACTIVE(nexthop
->flags
))
140 || (cmd
== RTM_DELETE
141 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))) {
142 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
143 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
144 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
147 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
148 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
149 ifindex
= nexthop
->ifindex
;
150 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
151 struct in_addr loopback
;
152 loopback
.s_addr
= htonl(INADDR_LOOPBACK
);
153 sin_gate
.sin_addr
= loopback
;
154 bh_type
= nexthop
->bh_type
;
158 if (gate
&& p
->prefixlen
== 32)
161 masklen2ip(p
->prefixlen
, &sin_mask
.sin_addr
);
162 sin_mask
.sin_family
= AF_INET
;
163 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
165 sin_masklen(sin_mask
.sin_addr
);
166 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
171 if (nexthop
->nh_label
172 && !kernel_rtm_add_labels(nexthop
->nh_label
,
175 smplsp
= (union sockunion
*)&smpls
;
178 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
179 (union sockunion
*)mask
,
180 gate
? (union sockunion
*)&sin_gate
182 smplsp
, ifindex
, bh_type
, re
->metric
);
184 if (IS_ZEBRA_DEBUG_RIB
) {
187 "%s: %s: attention! gate not found for re %p",
188 __func__
, prefix_buf
, re
);
189 route_entry_dump(p
, NULL
, re
);
191 inet_ntop(AF_INET
, &sin_gate
.sin_addr
,
192 gate_buf
, INET_ADDRSTRLEN
);
196 /* We only flag nexthops as being in FIB if rtm_write()
198 case ZEBRA_ERR_NOERROR
:
200 if (IS_ZEBRA_DEBUG_RIB
)
202 "%s: %s: successfully did NH %s",
203 __func__
, prefix_buf
, gate_buf
);
206 /* The only valid case for this error is kernel's
208 * a multipath route, which is common for FreeBSD. This
210 * ignored silently, but logged as an error otherwise.
212 case ZEBRA_ERR_RTEXIST
:
215 "%s: rtm_write() returned %d for command %d",
216 __func__
, error
, cmd
);
220 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
222 * normal to get any other messages in ANY case.
224 case ZEBRA_ERR_RTNOEXIST
:
225 case ZEBRA_ERR_RTUNREACH
:
228 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
230 prefix2str(p
, prefix_buf
,
233 lookup_msg(rtm_type_str
, cmd
, NULL
));
236 } /* if (cmd and flags make sense) */
237 else if (IS_ZEBRA_DEBUG_RIB
)
238 zlog_debug("%s: odd command %s for flags %d", __func__
,
239 lookup_msg(rtm_type_str
, cmd
, NULL
),
241 } /* for (ALL_NEXTHOPS(...))*/
243 /* If there was no useful nexthop, then complain. */
244 if (nexthop_num
== 0 && IS_ZEBRA_DEBUG_KERNEL
)
245 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
252 /* Calculate sin6_len value for netmask socket value. */
253 static int sin6_masklen(struct in6_addr mask
)
255 struct sockaddr_in6 sin6
;
259 if (IN6_IS_ADDR_UNSPECIFIED(&mask
))
262 sin6
.sin6_addr
= mask
;
263 len
= sizeof(struct sockaddr_in6
);
265 lim
= (char *)&sin6
.sin6_addr
;
266 p
= lim
+ sizeof(sin6
.sin6_addr
);
268 while (*--p
== 0 && p
>= lim
)
273 #endif /* SIN6_LEN */
275 /* Interface between zebra message and rtm message. */
276 static int kernel_rtm_ipv6(int cmd
, const struct prefix
*p
,
277 struct route_entry
*re
)
279 struct sockaddr_in6
*mask
;
280 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
282 struct sockaddr_mpls smpls
;
284 union sockunion
*smplsp
= NULL
;
285 struct nexthop
*nexthop
;
287 ifindex_t ifindex
= 0;
290 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
292 memset(&sin_dest
, 0, sizeof(struct sockaddr_in6
));
293 sin_dest
.sin6_family
= AF_INET6
;
295 sin_dest
.sin6_len
= sizeof(struct sockaddr_in6
);
296 #endif /* SIN6_LEN */
297 sin_dest
.sin6_addr
= p
->u
.prefix6
;
299 memset(&sin_mask
, 0, sizeof(struct sockaddr_in6
));
301 memset(&sin_gate
, 0, sizeof(struct sockaddr_in6
));
302 sin_gate
.sin6_family
= AF_INET6
;
303 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
304 sin_gate
.sin6_len
= sizeof(struct sockaddr_in6
);
305 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
308 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
309 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
314 if ((cmd
== RTM_ADD
&& NEXTHOP_IS_ACTIVE(nexthop
->flags
))
315 || (cmd
== RTM_DELETE
)) {
316 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
317 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
318 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
321 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
322 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
323 ifindex
= nexthop
->ifindex
;
325 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
)
326 bh_type
= nexthop
->bh_type
;
329 /* Under kame set interface index to link local address. */
332 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
334 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
335 (a).s6_addr[3] = (i)&0xff; \
338 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
339 SET_IN6_LINKLOCAL_IFINDEX(sin_gate
.sin6_addr
, ifindex
);
342 if (gate
&& p
->prefixlen
== 128)
345 masklen2ip6(p
->prefixlen
, &sin_mask
.sin6_addr
);
346 sin_mask
.sin6_family
= AF_INET6
;
348 sin_mask
.sin6_len
= sin6_masklen(sin_mask
.sin6_addr
);
349 #endif /* SIN6_LEN */
354 if (nexthop
->nh_label
355 && !kernel_rtm_add_labels(nexthop
->nh_label
, &smpls
))
357 smplsp
= (union sockunion
*)&smpls
;
360 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
361 (union sockunion
*)mask
,
362 gate
? (union sockunion
*)&sin_gate
: NULL
,
363 smplsp
, ifindex
, bh_type
, re
->metric
);
369 /* If there is no useful nexthop then return. */
370 if (nexthop_num
== 0) {
371 if (IS_ZEBRA_DEBUG_KERNEL
)
372 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
379 static int kernel_rtm(int cmd
, const struct prefix
*p
, struct route_entry
*re
)
381 switch (PREFIX_FAMILY(p
)) {
383 return kernel_rtm_ipv4(cmd
, p
, re
);
385 return kernel_rtm_ipv6(cmd
, p
, re
);
390 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
391 const struct prefix
*p
,
392 const struct prefix
*src_p
,
393 struct route_entry
*old
,
394 struct route_entry
*new)
398 if (src_p
&& src_p
->prefixlen
) {
399 zlog_err("route add: IPv6 sourcedest routes unsupported!");
400 return DP_REQUEST_FAILURE
;
403 if (zserv_privs
.change(ZPRIVS_RAISE
))
404 zlog_err("Can't raise privileges");
407 route
|= kernel_rtm(RTM_DELETE
, p
, old
);
410 route
|= kernel_rtm(RTM_ADD
, p
, new);
412 if (zserv_privs
.change(ZPRIVS_LOWER
))
413 zlog_err("Can't lower privileges");
416 kernel_route_rib_pass_fail(
418 (!route
) ? DP_INSTALL_SUCCESS
419 : DP_INSTALL_FAILURE
);
421 kernel_route_rib_pass_fail(rn
, p
, old
,
424 : DP_DELETE_FAILURE
);
427 return DP_REQUEST_SUCCESS
;
430 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
431 int llalen
, ns_id_t ns_id
)
437 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *mroute
)
442 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
447 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
452 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
453 struct in_addr vtep_ip
, uint8_t sticky
)
458 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
459 struct in_addr vtep_ip
, int local
)
464 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
470 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
475 extern int kernel_interface_set_master(struct interface
*master
,
476 struct interface
*slave
)
481 uint32_t kernel_get_speed(struct interface
*ifp
)
486 #endif /* !HAVE_NETLINK */