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"
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
, union sockunion
*mask
,
44 union sockunion
*gate
, union sockunion
*mpls
,
45 unsigned int index
, 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
50 static int sin_masklen(struct in_addr mask
)
54 struct sockaddr_in sin
;
60 len
= sizeof(struct sockaddr_in
);
62 lim
= (char *)&sin
.sin_addr
;
63 p
= lim
+ sizeof(sin
.sin_addr
);
65 while (*--p
== 0 && p
>= lim
)
69 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
71 /* Interface between zebra message and rtm message. */
72 static int kernel_rtm_ipv4(int cmd
, struct prefix
*p
, struct route_entry
*re
)
75 struct sockaddr_in
*mask
= NULL
;
76 struct sockaddr_in sin_dest
, sin_mask
, sin_gate
;
78 struct sockaddr_mpls smpls
;
80 union sockunion
*smplsp
= NULL
;
81 struct nexthop
*nexthop
;
83 ifindex_t ifindex
= 0;
86 char prefix_buf
[PREFIX_STRLEN
];
88 if (IS_ZEBRA_DEBUG_RIB
)
89 prefix2str(p
, prefix_buf
, sizeof(prefix_buf
));
90 memset(&sin_dest
, 0, sizeof(struct sockaddr_in
));
91 sin_dest
.sin_family
= AF_INET
;
92 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
93 sin_dest
.sin_len
= sizeof(struct sockaddr_in
);
94 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
95 sin_dest
.sin_addr
= p
->u
.prefix4
;
97 memset(&sin_mask
, 0, sizeof(struct sockaddr_in
));
99 memset(&sin_gate
, 0, sizeof(struct sockaddr_in
));
100 sin_gate
.sin_family
= AF_INET
;
101 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
102 sin_gate
.sin_len
= sizeof(struct sockaddr_in
);
103 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
106 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
107 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
111 char gate_buf
[INET_ADDRSTRLEN
] = "NULL";
114 * XXX We need to refrain from kernel operations in some cases,
115 * but this if statement seems overly cautious - what about
116 * other than ADD and DELETE?
119 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
120 || (cmd
== RTM_DELETE
121 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))) {
122 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
123 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
124 sin_gate
.sin_addr
= nexthop
->gate
.ipv4
;
127 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
128 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
129 ifindex
= nexthop
->ifindex
;
130 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
131 struct in_addr loopback
;
132 loopback
.s_addr
= htonl(INADDR_LOOPBACK
);
133 sin_gate
.sin_addr
= loopback
;
137 if (gate
&& p
->prefixlen
== 32)
140 masklen2ip(p
->prefixlen
, &sin_mask
.sin_addr
);
141 sin_mask
.sin_family
= AF_INET
;
142 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
144 sin_masklen(sin_mask
.sin_addr
);
145 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
150 if (nexthop
->nh_label
) {
151 memset(&smpls
, 0, sizeof(smpls
));
152 smpls
.smpls_len
= sizeof(smpls
);
153 smpls
.smpls_family
= AF_MPLS
;
155 htonl(nexthop
->nh_label
->label
[0]
156 << MPLS_LABEL_OFFSET
);
157 smplsp
= (union sockunion
*)&smpls
;
162 cmd
, (union sockunion
*)&sin_dest
,
163 (union sockunion
*)mask
,
164 gate
? (union sockunion
*)&sin_gate
: NULL
,
165 smplsp
, ifindex
, re
->flags
, re
->metric
);
167 if (IS_ZEBRA_DEBUG_RIB
) {
170 "%s: %s: attention! gate not found for re %p",
171 __func__
, prefix_buf
, re
);
172 route_entry_dump(p
, NULL
, re
);
174 inet_ntop(AF_INET
, &sin_gate
.sin_addr
,
175 gate_buf
, INET_ADDRSTRLEN
);
179 /* We only flag nexthops as being in FIB if rtm_write()
181 case ZEBRA_ERR_NOERROR
:
183 if (IS_ZEBRA_DEBUG_RIB
)
185 "%s: %s: successfully did NH %s",
186 __func__
, prefix_buf
, gate_buf
);
188 SET_FLAG(nexthop
->flags
,
192 /* The only valid case for this error is kernel's
194 * a multipath route, which is common for FreeBSD. This
196 * ignored silently, but logged as an error otherwise.
198 case ZEBRA_ERR_RTEXIST
:
201 "%s: rtm_write() returned %d for command %d",
202 __func__
, error
, cmd
);
206 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
208 * normal to get any other messages in ANY case.
210 case ZEBRA_ERR_RTNOEXIST
:
211 case ZEBRA_ERR_RTUNREACH
:
214 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
216 prefix2str(p
, prefix_buf
,
219 lookup_msg(rtm_type_str
, cmd
, NULL
));
222 } /* if (cmd and flags make sense) */
223 else if (IS_ZEBRA_DEBUG_RIB
)
224 zlog_debug("%s: odd command %s for flags %d", __func__
,
225 lookup_msg(rtm_type_str
, cmd
, NULL
),
227 } /* for (ALL_NEXTHOPS(...))*/
229 /* If there was no useful nexthop, then complain. */
230 if (nexthop_num
== 0 && IS_ZEBRA_DEBUG_KERNEL
)
231 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
238 /* Calculate sin6_len value for netmask socket value. */
239 static int sin6_masklen(struct in6_addr mask
)
241 struct sockaddr_in6 sin6
;
245 if (IN6_IS_ADDR_UNSPECIFIED(&mask
))
248 sin6
.sin6_addr
= mask
;
249 len
= sizeof(struct sockaddr_in6
);
251 lim
= (char *)&sin6
.sin6_addr
;
252 p
= lim
+ sizeof(sin6
.sin6_addr
);
254 while (*--p
== 0 && p
>= lim
)
259 #endif /* SIN6_LEN */
261 /* Interface between zebra message and rtm message. */
262 static int kernel_rtm_ipv6(int cmd
, struct prefix
*p
, struct route_entry
*re
)
264 struct sockaddr_in6
*mask
;
265 struct sockaddr_in6 sin_dest
, sin_mask
, sin_gate
;
266 struct nexthop
*nexthop
;
268 ifindex_t ifindex
= 0;
272 memset(&sin_dest
, 0, sizeof(struct sockaddr_in6
));
273 sin_dest
.sin6_family
= AF_INET6
;
275 sin_dest
.sin6_len
= sizeof(struct sockaddr_in6
);
276 #endif /* SIN6_LEN */
277 sin_dest
.sin6_addr
= p
->u
.prefix6
;
279 memset(&sin_mask
, 0, sizeof(struct sockaddr_in6
));
281 memset(&sin_gate
, 0, sizeof(struct sockaddr_in6
));
282 sin_gate
.sin6_family
= AF_INET6
;
283 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
284 sin_gate
.sin6_len
= sizeof(struct sockaddr_in6
);
285 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
288 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
289 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
295 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
296 || (cmd
== RTM_DELETE
298 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)
301 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
302 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
303 sin_gate
.sin6_addr
= nexthop
->gate
.ipv6
;
306 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
307 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
308 ifindex
= nexthop
->ifindex
;
311 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
314 /* Under kame set interface index to link local address. */
317 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
319 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
320 (a).s6_addr[3] = (i)&0xff; \
323 if (gate
&& IN6_IS_ADDR_LINKLOCAL(&sin_gate
.sin6_addr
))
324 SET_IN6_LINKLOCAL_IFINDEX(sin_gate
.sin6_addr
, ifindex
);
327 if (gate
&& p
->prefixlen
== 128)
330 masklen2ip6(p
->prefixlen
, &sin_mask
.sin6_addr
);
331 sin_mask
.sin6_family
= AF_INET6
;
333 sin_mask
.sin6_len
= sin6_masklen(sin_mask
.sin6_addr
);
334 #endif /* SIN6_LEN */
338 error
= rtm_write(cmd
, (union sockunion
*)&sin_dest
,
339 (union sockunion
*)mask
,
340 gate
? (union sockunion
*)&sin_gate
: NULL
,
341 NULL
, ifindex
, re
->flags
, re
->metric
);
346 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
356 /* If there is no useful nexthop then return. */
357 if (nexthop_num
== 0) {
358 if (IS_ZEBRA_DEBUG_KERNEL
)
359 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
366 static int kernel_rtm(int cmd
, struct prefix
*p
, struct route_entry
*re
)
368 switch (PREFIX_FAMILY(p
)) {
370 return kernel_rtm_ipv4(cmd
, p
, re
);
372 return kernel_rtm_ipv6(cmd
, p
, re
);
377 int kernel_route_rib(struct prefix
*p
, struct prefix
*src_p
,
378 struct route_entry
*old
, struct route_entry
*new)
382 if (src_p
&& src_p
->prefixlen
) {
383 zlog_err("route add: IPv6 sourcedest routes unsupported!");
387 if (zserv_privs
.change(ZPRIVS_RAISE
))
388 zlog_err("Can't raise privileges");
391 route
|= kernel_rtm(RTM_DELETE
, p
, old
);
394 route
|= kernel_rtm(RTM_ADD
, p
, new);
396 if (zserv_privs
.change(ZPRIVS_LOWER
))
397 zlog_err("Can't lower privileges");
402 int kernel_neigh_update(int add
, int ifindex
, uint32_t addr
, char *lla
,
409 extern int kernel_get_ipmr_sg_stats(struct zebra_vrf
*zvrf
, void *mroute
)
414 int kernel_add_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
419 int kernel_del_vtep(vni_t vni
, struct interface
*ifp
, struct in_addr
*vtep_ip
)
424 int kernel_add_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
425 struct in_addr vtep_ip
, u_char sticky
)
430 int kernel_del_mac(struct interface
*ifp
, vlanid_t vid
, struct ethaddr
*mac
,
431 struct in_addr vtep_ip
, int local
)
436 int kernel_add_neigh(struct interface
*ifp
, struct ipaddr
*ip
,
442 int kernel_del_neigh(struct interface
*ifp
, struct ipaddr
*ip
)
447 extern int kernel_interface_set_master(struct interface
*master
,
448 struct interface
*slave
)