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"
36 #include "lib_errors.h"
38 #include "zebra/debug.h"
39 #include "zebra/rib.h"
41 #include "zebra/kernel_socket.h"
42 #include "zebra/zebra_mpls.h"
43 #include "zebra/zebra_errors.h"
45 extern struct zebra_privs_t zserv_privs
;
48 static int kernel_rtm_add_labels(struct mpls_label_stack
*nh_label
,
49 struct sockaddr_mpls
*smpls
)
51 if (nh_label
->num_labels
> 1) {
52 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH
,
53 "%s: can't push %u labels at "
54 "once (maximum is 1)",
55 __func__
, nh_label
->num_labels
);
59 memset(smpls
, 0, sizeof(*smpls
));
60 smpls
->smpls_len
= sizeof(*smpls
);
61 smpls
->smpls_family
= AF_MPLS
;
62 smpls
->smpls_label
= htonl(nh_label
->label
[0] << MPLS_LABEL_OFFSET
);
68 /* Interface between zebra message and rtm message. */
69 static int kernel_rtm(int cmd
, const struct prefix
*p
,
70 const struct nexthop_group
*ng
, uint32_t metric
)
73 union sockunion sin_dest
, sin_mask
, sin_gate
;
75 struct sockaddr_mpls smpls
;
77 union sockunion
*smplsp
= NULL
;
78 struct nexthop
*nexthop
;
80 ifindex_t ifindex
= 0;
83 char gate_buf
[INET6_BUFSIZ
];
84 char prefix_buf
[PREFIX_STRLEN
];
85 enum blackhole_type bh_type
= BLACKHOLE_UNSPEC
;
87 prefix2str(p
, prefix_buf
, sizeof(prefix_buf
));
90 * We only have the ability to ADD or DELETE at this point
93 if (cmd
!= RTM_ADD
&& cmd
!= RTM_DELETE
) {
94 if (IS_ZEBRA_DEBUG_KERNEL
)
95 zlog_debug("%s: %s odd command %s",
97 lookup_msg(rtm_type_str
, cmd
, NULL
));
101 memset(&sin_dest
, 0, sizeof(sin_dest
));
102 memset(&sin_gate
, 0, sizeof(sin_gate
));
103 memset(&sin_mask
, 0, sizeof(sin_mask
));
107 sin_dest
.sin
.sin_family
= AF_INET
;
108 sin_dest
.sin
.sin_addr
= p
->u
.prefix4
;
109 sin_gate
.sin
.sin_family
= AF_INET
;
110 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
111 sin_dest
.sin
.sin_len
= sizeof(struct sockaddr_in
);
112 sin_gate
.sin
.sin_len
= sizeof(struct sockaddr_in
);
113 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
116 sin_dest
.sin6
.sin6_family
= AF_INET6
;
117 sin_dest
.sin6
.sin6_addr
= p
->u
.prefix6
;
118 sin_gate
.sin6
.sin6_family
= AF_INET6
;
119 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
120 sin_dest
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
121 sin_gate
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
122 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
127 for (ALL_NEXTHOPS_PTR(ng
, nexthop
)) {
129 * We only want to use the actual good nexthops
131 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ||
132 !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
137 snprintf(gate_buf
, sizeof(gate_buf
), "NULL");
139 switch (nexthop
->type
) {
140 case NEXTHOP_TYPE_IPV4
:
141 case NEXTHOP_TYPE_IPV4_IFINDEX
:
142 sin_gate
.sin
.sin_addr
= nexthop
->gate
.ipv4
;
143 sin_gate
.sin
.sin_family
= AF_INET
;
144 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
145 sin_gate
.sin
.sin_len
= sizeof(struct sockaddr_in
);
146 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
147 ifindex
= nexthop
->ifindex
;
150 case NEXTHOP_TYPE_IPV6
:
151 case NEXTHOP_TYPE_IPV6_IFINDEX
:
152 sin_gate
.sin6
.sin6_addr
= nexthop
->gate
.ipv6
;
153 sin_gate
.sin6
.sin6_family
= AF_INET6
;
154 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
155 sin_gate
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
156 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
157 ifindex
= nexthop
->ifindex
;
158 /* Under kame set interface index to link local address */
161 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
163 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
164 (a).s6_addr[3] = (i)&0xff; \
167 if (IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6
.sin6_addr
))
168 SET_IN6_LINKLOCAL_IFINDEX(
169 sin_gate
.sin6
.sin6_addr
,
175 case NEXTHOP_TYPE_IFINDEX
:
176 ifindex
= nexthop
->ifindex
;
178 case NEXTHOP_TYPE_BLACKHOLE
:
179 bh_type
= nexthop
->bh_type
;
182 struct in_addr loopback
;
183 loopback
.s_addr
= htonl(INADDR_LOOPBACK
);
184 sin_gate
.sin
.sin_addr
= loopback
;
185 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
186 sin_gate
.sin
.sin_len
=
187 sizeof(struct sockaddr_in
);
188 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
199 masklen2ip(p
->prefixlen
, &sin_mask
.sin
.sin_addr
);
200 sin_mask
.sin
.sin_family
= AF_INET
;
201 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
202 sin_mask
.sin
.sin_len
= sizeof(struct sockaddr_in
);
203 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
206 masklen2ip6(p
->prefixlen
, &sin_mask
.sin6
.sin6_addr
);
207 sin_mask
.sin6
.sin6_family
= AF_INET6
;
208 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
209 sin_mask
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
210 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
215 if (nexthop
->nh_label
) {
216 if (kernel_rtm_add_labels(nexthop
->nh_label
,
219 smplsp
= (union sockunion
*)&smpls
;
222 error
= rtm_write(cmd
, &sin_dest
, &sin_mask
,
223 gate
? &sin_gate
: NULL
, smplsp
,
224 ifindex
, bh_type
, metric
);
226 if (IS_ZEBRA_DEBUG_KERNEL
) {
229 "%s: %s: attention! gate not found for re",
230 __func__
, prefix_buf
);
235 &sin_gate
.sin
.sin_addr
,
236 gate_buf
, sizeof(gate_buf
));
241 &sin_gate
.sin6
.sin6_addr
,
242 gate_buf
, sizeof(gate_buf
));
246 snprintf(gate_buf
, sizeof(gate_buf
),
253 /* We only flag nexthops as being in FIB if
254 * rtm_write() did its work. */
255 case ZEBRA_ERR_NOERROR
:
257 if (IS_ZEBRA_DEBUG_KERNEL
)
258 zlog_debug("%s: %s: successfully did NH %s",
259 __func__
, prefix_buf
, gate_buf
);
261 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
264 /* The only valid case for this error is
265 * kernel's failure to install a multipath
266 * route, which is common for FreeBSD. This
267 * should be ignored silently, but logged as an error
270 case ZEBRA_ERR_RTEXIST
:
272 flog_err(EC_LIB_SYSTEM_CALL
,
273 "%s: rtm_write() returned %d for command %d",
274 __func__
, error
, cmd
);
277 /* Note any unexpected status returns */
281 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
282 __func__
, prefix_buf
, error
,
283 lookup_msg(rtm_type_str
, cmd
, NULL
));
286 } /* for (ALL_NEXTHOPS(...))*/
288 /* If there was no useful nexthop, then complain. */
289 if (nexthop_num
== 0) {
290 if (IS_ZEBRA_DEBUG_KERNEL
)
292 "%s: No useful nexthops were found in RIB prefix %s",
293 __func__
, prefix_buf
);
301 * Update or delete a prefix from the kernel,
302 * using info from a dataplane context struct.
304 enum zebra_dplane_result
kernel_route_update(struct zebra_dplane_ctx
*ctx
)
306 enum zebra_dplane_result res
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
307 uint32_t type
, old_type
;
309 if (dplane_ctx_get_src(ctx
) != NULL
) {
310 zlog_err("route add: IPv6 sourcedest routes unsupported!");
311 return ZEBRA_DPLANE_REQUEST_FAILURE
;
314 type
= dplane_ctx_get_type(ctx
);
315 old_type
= dplane_ctx_get_old_type(ctx
);
317 frr_with_privs(&zserv_privs
) {
319 if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_DELETE
) {
320 if (!RSYSTEM_ROUTE(type
))
321 kernel_rtm(RTM_DELETE
, dplane_ctx_get_dest(ctx
),
322 dplane_ctx_get_ng(ctx
),
323 dplane_ctx_get_metric(ctx
));
324 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_INSTALL
) {
325 if (!RSYSTEM_ROUTE(type
))
326 kernel_rtm(RTM_ADD
, dplane_ctx_get_dest(ctx
),
327 dplane_ctx_get_ng(ctx
),
328 dplane_ctx_get_metric(ctx
));
329 } else if (dplane_ctx_get_op(ctx
) == DPLANE_OP_ROUTE_UPDATE
) {
330 /* Must do delete and add separately -
331 * no update available
333 if (!RSYSTEM_ROUTE(old_type
))
334 kernel_rtm(RTM_DELETE
, dplane_ctx_get_dest(ctx
),
335 dplane_ctx_get_old_ng(ctx
),
336 dplane_ctx_get_old_metric(ctx
));
338 if (!RSYSTEM_ROUTE(type
))
339 kernel_rtm(RTM_ADD
, dplane_ctx_get_dest(ctx
),
340 dplane_ctx_get_ng(ctx
),
341 dplane_ctx_get_metric(ctx
));
343 zlog_err("Invalid routing socket update op %s (%u)",
344 dplane_op2str(dplane_ctx_get_op(ctx
)),
345 dplane_ctx_get_op(ctx
));
346 res
= ZEBRA_DPLANE_REQUEST_FAILURE
;
348 } /* Elevated privs */
350 if (RSYSTEM_ROUTE(type
)
351 && dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) {
352 struct nexthop
*nexthop
;
354 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
355 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
358 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
359 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
367 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
368 int llalen
, ns_id_t ns_id
)
374 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *mroute
)
379 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
384 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
390 * Update MAC, using dataplane context object. No-op here for now.
392 enum zebra_dplane_result
kernel_mac_update_ctx(struct zebra_dplane_ctx
*ctx
)
394 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
397 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
398 struct ethaddr
*mac
, uint8_t flags
)
403 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
408 extern int kernel_interface_set_master(struct interface
*master
,
409 struct interface
*slave
)
414 uint32_t kernel_get_speed(struct interface
*ifp
)
419 #endif /* !HAVE_NETLINK */