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
, struct prefix
*p
, struct route_entry
*re
)
94 struct sockaddr_in
*mask
= NULL
;
95 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
97 struct sockaddr_mpls smpls
;
99 union sockunion
*smplsp
= NULL
;
100 struct nexthop
*nexthop
;
102 ifindex_t ifindex
= 0;
105 char prefix_buf
[PREFIX_STRLEN
];
106 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
108 if (IS_ZEBRA_DEBUG_RIB
)
109 prefix2str(p
, prefix_buf
, sizeof(prefix_buf
));
110 memset(&sin_dest
, 0, sizeof(struct sockaddr_in
));
111 sin_dest
.sin_family
= AF_INET
;
112 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
113 sin_dest
.sin_len
= sizeof(struct sockaddr_in
);
114 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
115 sin_dest
.sin_addr
= p
->u
.prefix4
;
117 memset(&sin_mask
, 0, sizeof(struct sockaddr_in
));
119 memset(&sin_gate
, 0, sizeof(struct sockaddr_in
));
120 sin_gate
.sin_family
= AF_INET
;
121 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
122 sin_gate
.sin_len
= sizeof(struct sockaddr_in
);
123 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
126 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
127 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
131 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
134 * XXX We need to refrain from kernel operations in some cases,
135 * but this if statement seems overly cautious - what about
136 * other than ADD and DELETE?
138 if ((cmd
== RTM_ADD
&& NEXTHOP_IS_ACTIVE(nexthop
->flags
))
139 || (cmd
== RTM_DELETE
140 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))) {
141 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
142 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
143 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
146 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
147 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
148 ifindex
= nexthop
->ifindex
;
149 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
150 struct in_addr loopback
;
151 loopback
.s_addr
= htonl(INADDR_LOOPBACK
);
152 sin_gate
.sin_addr
= loopback
;
153 bh_type
= nexthop
->bh_type
;
157 if (gate
&& p
->prefixlen
== 32)
160 masklen2ip(p
->prefixlen
, &sin_mask
.sin_addr
);
161 sin_mask
.sin_family
= AF_INET
;
162 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
164 sin_masklen(sin_mask
.sin_addr
);
165 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
170 if (nexthop
->nh_label
171 && !kernel_rtm_add_labels(nexthop
->nh_label
,
174 smplsp
= (union sockunion
*)&smpls
;
177 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
178 (union sockunion
*)mask
,
179 gate
? (union sockunion
*)&sin_gate
181 smplsp
, ifindex
, bh_type
, re
->metric
);
183 if (IS_ZEBRA_DEBUG_RIB
) {
186 "%s: %s: attention! gate not found for re %p",
187 __func__
, prefix_buf
, re
);
188 route_entry_dump(p
, NULL
, re
);
190 inet_ntop(AF_INET
, &sin_gate
.sin_addr
,
191 gate_buf
, INET_ADDRSTRLEN
);
195 /* We only flag nexthops as being in FIB if rtm_write()
197 case ZEBRA_ERR_NOERROR
:
199 if (IS_ZEBRA_DEBUG_RIB
)
201 "%s: %s: successfully did NH %s",
202 __func__
, prefix_buf
, gate_buf
);
205 /* The only valid case for this error is kernel's
207 * a multipath route, which is common for FreeBSD. This
209 * ignored silently, but logged as an error otherwise.
211 case ZEBRA_ERR_RTEXIST
:
214 "%s: rtm_write() returned %d for command %d",
215 __func__
, error
, cmd
);
219 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
221 * normal to get any other messages in ANY case.
223 case ZEBRA_ERR_RTNOEXIST
:
224 case ZEBRA_ERR_RTUNREACH
:
227 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
229 prefix2str(p
, prefix_buf
,
232 lookup_msg(rtm_type_str
, cmd
, NULL
));
235 } /* if (cmd and flags make sense) */
236 else if (IS_ZEBRA_DEBUG_RIB
)
237 zlog_debug("%s: odd command %s for flags %d", __func__
,
238 lookup_msg(rtm_type_str
, cmd
, NULL
),
240 } /* for (ALL_NEXTHOPS(...))*/
242 /* If there was no useful nexthop, then complain. */
243 if (nexthop_num
== 0 && IS_ZEBRA_DEBUG_KERNEL
)
244 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
251 /* Calculate sin6_len value for netmask socket value. */
252 static int sin6_masklen(struct in6_addr mask
)
254 struct sockaddr_in6 sin6
;
258 if (IN6_IS_ADDR_UNSPECIFIED(&mask
))
261 sin6
.sin6_addr
= mask
;
262 len
= sizeof(struct sockaddr_in6
);
264 lim
= (char *)&sin6
.sin6_addr
;
265 p
= lim
+ sizeof(sin6
.sin6_addr
);
267 while (*--p
== 0 && p
>= lim
)
272 #endif /* SIN6_LEN */
274 /* Interface between zebra message and rtm message. */
275 static int kernel_rtm_ipv6(int cmd
, struct prefix
*p
, struct route_entry
*re
)
277 struct sockaddr_in6
*mask
;
278 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
280 struct sockaddr_mpls smpls
;
282 union sockunion
*smplsp
= NULL
;
283 struct nexthop
*nexthop
;
285 ifindex_t ifindex
= 0;
288 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
290 memset(&sin_dest
, 0, sizeof(struct sockaddr_in6
));
291 sin_dest
.sin6_family
= AF_INET6
;
293 sin_dest
.sin6_len
= sizeof(struct sockaddr_in6
);
294 #endif /* SIN6_LEN */
295 sin_dest
.sin6_addr
= p
->u
.prefix6
;
297 memset(&sin_mask
, 0, sizeof(struct sockaddr_in6
));
299 memset(&sin_gate
, 0, sizeof(struct sockaddr_in6
));
300 sin_gate
.sin6_family
= AF_INET6
;
301 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
302 sin_gate
.sin6_len
= sizeof(struct sockaddr_in6
);
303 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
306 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
307 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
312 if ((cmd
== RTM_ADD
&& NEXTHOP_IS_ACTIVE(nexthop
->flags
))
313 || (cmd
== RTM_DELETE
)) {
314 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
315 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
316 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
319 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
320 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
321 ifindex
= nexthop
->ifindex
;
323 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
)
324 bh_type
= nexthop
->bh_type
;
327 /* Under kame set interface index to link local address. */
330 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
332 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
333 (a).s6_addr[3] = (i)&0xff; \
336 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
337 SET_IN6_LINKLOCAL_IFINDEX(sin_gate
.sin6_addr
, ifindex
);
340 if (gate
&& p
->prefixlen
== 128)
343 masklen2ip6(p
->prefixlen
, &sin_mask
.sin6_addr
);
344 sin_mask
.sin6_family
= AF_INET6
;
346 sin_mask
.sin6_len
= sin6_masklen(sin_mask
.sin6_addr
);
347 #endif /* SIN6_LEN */
352 if (nexthop
->nh_label
353 && !kernel_rtm_add_labels(nexthop
->nh_label
, &smpls
))
355 smplsp
= (union sockunion
*)&smpls
;
358 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
359 (union sockunion
*)mask
,
360 gate
? (union sockunion
*)&sin_gate
: NULL
,
361 smplsp
, ifindex
, bh_type
, re
->metric
);
367 /* If there is no useful nexthop then return. */
368 if (nexthop_num
== 0) {
369 if (IS_ZEBRA_DEBUG_KERNEL
)
370 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
377 static int kernel_rtm(int cmd
, struct prefix
*p
, struct route_entry
*re
)
379 switch (PREFIX_FAMILY(p
)) {
381 return kernel_rtm_ipv4(cmd
, p
, re
);
383 return kernel_rtm_ipv6(cmd
, p
, re
);
388 enum dp_req_result
kernel_route_rib(struct route_node
*rn
,
389 const struct prefix
*p
,
390 const struct prefix
*src_p
,
391 struct route_entry
*old
,
392 struct route_entry
*new)
396 if (src_p
&& src_p
->prefixlen
) {
397 zlog_err("route add: IPv6 sourcedest routes unsupported!");
398 return DP_REQUEST_FAILURE
;
401 if (zserv_privs
.change(ZPRIVS_RAISE
))
402 zlog_err("Can't raise privileges");
405 route
|= kernel_rtm(RTM_DELETE
, p
, old
);
408 route
|= kernel_rtm(RTM_ADD
, p
, new);
410 if (zserv_privs
.change(ZPRIVS_LOWER
))
411 zlog_err("Can't lower privileges");
414 kernel_route_rib_pass_fail(
416 (!route
) ? DP_INSTALL_SUCCESS
417 : DP_INSTALL_FAILURE
);
419 kernel_route_rib_pass_fail(rn
, p
, old
,
422 : DP_DELETE_FAILURE
);
425 return DP_REQUEST_SUCCESS
;
428 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
429 int llalen
, ns_id_t ns_id
)
435 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *mroute
)
440 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
445 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
450 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
451 struct in_addr vtep_ip
, uint8_t sticky
)
456 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
457 struct in_addr vtep_ip
, int local
)
462 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
468 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
473 extern int kernel_interface_set_master(struct interface
*master
,
474 struct interface
*slave
)
479 uint32_t kernel_get_speed(struct interface
*ifp
)
484 #endif /* !HAVE_NETLINK */