]> git.proxmox.com Git - mirror_frr.git/blob - zebra/rt_socket.c
Merge pull request #893 from opensourcerouting/minor-issues
[mirror_frr.git] / zebra / rt_socket.c
1 /*
2 * Kernel routing table updates by routing socket.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
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
20 */
21
22 #include <zebra.h>
23
24 #ifndef HAVE_NETLINK
25
26 #ifdef __OpenBSD__
27 #include <netmpls/mpls.h>
28 #endif
29
30 #include "if.h"
31 #include "prefix.h"
32 #include "sockunion.h"
33 #include "log.h"
34 #include "privs.h"
35 #include "vxlan.h"
36
37 #include "zebra/debug.h"
38 #include "zebra/rib.h"
39 #include "zebra/rt.h"
40 #include "zebra/kernel_socket.h"
41 #include "zebra/zebra_mpls.h"
42
43 extern struct zebra_privs_t zserv_privs;
44
45 /* kernel socket export */
46 extern int rtm_write(int message, union sockunion *dest, union sockunion *mask,
47 union sockunion *gate, union sockunion *mpls,
48 unsigned int index, int zebra_flags, int metric);
49
50 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
51 /* Adjust netmask socket length. Return value is a adjusted sin_len
52 value. */
53 static int sin_masklen(struct in_addr mask)
54 {
55 char *p, *lim;
56 int len;
57 struct sockaddr_in sin;
58
59 if (mask.s_addr == 0)
60 return sizeof(long);
61
62 sin.sin_addr = mask;
63 len = sizeof(struct sockaddr_in);
64
65 lim = (char *)&sin.sin_addr;
66 p = lim + sizeof(sin.sin_addr);
67
68 while (*--p == 0 && p >= lim)
69 len--;
70 return len;
71 }
72 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
73
74 /* Interface between zebra message and rtm message. */
75 static int kernel_rtm_ipv4(int cmd, struct prefix *p, struct route_entry *re)
76
77 {
78 struct sockaddr_in *mask = NULL;
79 struct sockaddr_in sin_dest, sin_mask, sin_gate;
80 #ifdef __OpenBSD__
81 struct sockaddr_mpls smpls;
82 #endif
83 union sockunion *smplsp = NULL;
84 struct nexthop *nexthop;
85 int nexthop_num = 0;
86 ifindex_t ifindex = 0;
87 int gate = 0;
88 int error;
89 char prefix_buf[PREFIX_STRLEN];
90
91 if (IS_ZEBRA_DEBUG_RIB)
92 prefix2str(p, prefix_buf, sizeof(prefix_buf));
93 memset(&sin_dest, 0, sizeof(struct sockaddr_in));
94 sin_dest.sin_family = AF_INET;
95 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
96 sin_dest.sin_len = sizeof(struct sockaddr_in);
97 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
98 sin_dest.sin_addr = p->u.prefix4;
99
100 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
101
102 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
103 sin_gate.sin_family = AF_INET;
104 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
105 sin_gate.sin_len = sizeof(struct sockaddr_in);
106 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
107
108 /* Make gateway. */
109 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
110 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
111 continue;
112
113 gate = 0;
114 char gate_buf[INET_ADDRSTRLEN] = "NULL";
115
116 /*
117 * XXX We need to refrain from kernel operations in some cases,
118 * but this if statement seems overly cautious - what about
119 * other than ADD and DELETE?
120 */
121 if ((cmd == RTM_ADD
122 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
123 || (cmd == RTM_DELETE
124 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))) {
125 if (nexthop->type == NEXTHOP_TYPE_IPV4
126 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
127 sin_gate.sin_addr = nexthop->gate.ipv4;
128 gate = 1;
129 }
130 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
131 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
132 ifindex = nexthop->ifindex;
133 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
134 struct in_addr loopback;
135 loopback.s_addr = htonl(INADDR_LOOPBACK);
136 sin_gate.sin_addr = loopback;
137 gate = 1;
138 }
139
140 if (gate && p->prefixlen == 32)
141 mask = NULL;
142 else {
143 masklen2ip(p->prefixlen, &sin_mask.sin_addr);
144 sin_mask.sin_family = AF_INET;
145 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
146 sin_mask.sin_len =
147 sin_masklen(sin_mask.sin_addr);
148 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
149 mask = &sin_mask;
150 }
151
152 #ifdef __OpenBSD__
153 if (nexthop->nh_label) {
154 memset(&smpls, 0, sizeof(smpls));
155 smpls.smpls_len = sizeof(smpls);
156 smpls.smpls_family = AF_MPLS;
157 smpls.smpls_label =
158 htonl(nexthop->nh_label->label[0]
159 << MPLS_LABEL_OFFSET);
160 smplsp = (union sockunion *)&smpls;
161 }
162 #endif
163
164 error = rtm_write(
165 cmd, (union sockunion *)&sin_dest,
166 (union sockunion *)mask,
167 gate ? (union sockunion *)&sin_gate : NULL,
168 smplsp, ifindex, re->flags, re->metric);
169
170 if (IS_ZEBRA_DEBUG_RIB) {
171 if (!gate) {
172 zlog_debug(
173 "%s: %s: attention! gate not found for re %p",
174 __func__, prefix_buf, re);
175 route_entry_dump(p, NULL, re);
176 } else
177 inet_ntop(AF_INET, &sin_gate.sin_addr,
178 gate_buf, INET_ADDRSTRLEN);
179 }
180
181 switch (error) {
182 /* We only flag nexthops as being in FIB if rtm_write()
183 * did its work. */
184 case ZEBRA_ERR_NOERROR:
185 nexthop_num++;
186 if (IS_ZEBRA_DEBUG_RIB)
187 zlog_debug(
188 "%s: %s: successfully did NH %s",
189 __func__, prefix_buf, gate_buf);
190 if (cmd == RTM_ADD)
191 SET_FLAG(nexthop->flags,
192 NEXTHOP_FLAG_FIB);
193 break;
194
195 /* The only valid case for this error is kernel's
196 * failure to install
197 * a multipath route, which is common for FreeBSD. This
198 * should be
199 * ignored silently, but logged as an error otherwise.
200 */
201 case ZEBRA_ERR_RTEXIST:
202 if (cmd != RTM_ADD)
203 zlog_err(
204 "%s: rtm_write() returned %d for command %d",
205 __func__, error, cmd);
206 continue;
207 break;
208
209 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
210 * FIB, it isn't
211 * normal to get any other messages in ANY case.
212 */
213 case ZEBRA_ERR_RTNOEXIST:
214 case ZEBRA_ERR_RTUNREACH:
215 default:
216 zlog_err(
217 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
218 __func__,
219 prefix2str(p, prefix_buf,
220 sizeof(prefix_buf)),
221 error,
222 lookup_msg(rtm_type_str, cmd, NULL));
223 break;
224 }
225 } /* if (cmd and flags make sense) */
226 else if (IS_ZEBRA_DEBUG_RIB)
227 zlog_debug("%s: odd command %s for flags %d", __func__,
228 lookup_msg(rtm_type_str, cmd, NULL),
229 nexthop->flags);
230 } /* for (ALL_NEXTHOPS(...))*/
231
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",
235 __func__, re);
236
237 return 0; /*XXX*/
238 }
239
240 #ifdef SIN6_LEN
241 /* Calculate sin6_len value for netmask socket value. */
242 static int sin6_masklen(struct in6_addr mask)
243 {
244 struct sockaddr_in6 sin6;
245 char *p, *lim;
246 int len;
247
248 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
249 return sizeof(long);
250
251 sin6.sin6_addr = mask;
252 len = sizeof(struct sockaddr_in6);
253
254 lim = (char *)&sin6.sin6_addr;
255 p = lim + sizeof(sin6.sin6_addr);
256
257 while (*--p == 0 && p >= lim)
258 len--;
259
260 return len;
261 }
262 #endif /* SIN6_LEN */
263
264 /* Interface between zebra message and rtm message. */
265 static int kernel_rtm_ipv6(int cmd, struct prefix *p, struct route_entry *re)
266 {
267 struct sockaddr_in6 *mask;
268 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
269 struct nexthop *nexthop;
270 int nexthop_num = 0;
271 ifindex_t ifindex = 0;
272 int gate = 0;
273 int error;
274
275 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
276 sin_dest.sin6_family = AF_INET6;
277 #ifdef SIN6_LEN
278 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
279 #endif /* SIN6_LEN */
280 sin_dest.sin6_addr = p->u.prefix6;
281
282 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
283
284 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
285 sin_gate.sin6_family = AF_INET6;
286 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
287 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
288 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
289
290 /* Make gateway. */
291 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
292 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
293 continue;
294
295 gate = 0;
296
297 if ((cmd == RTM_ADD
298 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
299 || (cmd == RTM_DELETE
300 #if 0
301 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
302 #endif
303 )) {
304 if (nexthop->type == NEXTHOP_TYPE_IPV6
305 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
306 sin_gate.sin6_addr = nexthop->gate.ipv6;
307 gate = 1;
308 }
309 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
310 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
311 ifindex = nexthop->ifindex;
312
313 if (cmd == RTM_ADD)
314 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
315 }
316
317 /* Under kame set interface index to link local address. */
318 #ifdef KAME
319
320 #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
321 do { \
322 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
323 (a).s6_addr[3] = (i)&0xff; \
324 } while (0)
325
326 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
327 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
328 #endif /* KAME */
329
330 if (gate && p->prefixlen == 128)
331 mask = NULL;
332 else {
333 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
334 sin_mask.sin6_family = AF_INET6;
335 #ifdef SIN6_LEN
336 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
337 #endif /* SIN6_LEN */
338 mask = &sin_mask;
339 }
340
341 error = rtm_write(cmd, (union sockunion *)&sin_dest,
342 (union sockunion *)mask,
343 gate ? (union sockunion *)&sin_gate : NULL,
344 NULL, ifindex, re->flags, re->metric);
345
346 #if 0
347 if (error)
348 {
349 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
350 nexthop_num, error);
351 }
352 #else
353 (void)error;
354 #endif
355
356 nexthop_num++;
357 }
358
359 /* If there is no useful nexthop then return. */
360 if (nexthop_num == 0) {
361 if (IS_ZEBRA_DEBUG_KERNEL)
362 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
363 return 0;
364 }
365
366 return 0; /*XXX*/
367 }
368
369 static int kernel_rtm(int cmd, struct prefix *p, struct route_entry *re)
370 {
371 switch (PREFIX_FAMILY(p)) {
372 case AF_INET:
373 return kernel_rtm_ipv4(cmd, p, re);
374 case AF_INET6:
375 return kernel_rtm_ipv6(cmd, p, re);
376 }
377 return 0;
378 }
379
380 int kernel_route_rib(struct prefix *p, struct prefix *src_p,
381 struct route_entry *old, struct route_entry *new)
382 {
383 int route = 0;
384
385 if (src_p && src_p->prefixlen) {
386 zlog_err("route add: IPv6 sourcedest routes unsupported!");
387 return 1;
388 }
389
390 if (zserv_privs.change(ZPRIVS_RAISE))
391 zlog_err("Can't raise privileges");
392
393 if (old)
394 route |= kernel_rtm(RTM_DELETE, p, old);
395
396 if (new)
397 route |= kernel_rtm(RTM_ADD, p, new);
398
399 if (zserv_privs.change(ZPRIVS_LOWER))
400 zlog_err("Can't lower privileges");
401
402 return route;
403 }
404
405 int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
406 int llalen)
407 {
408 /* TODO */
409 return 0;
410 }
411
412 extern int kernel_get_ipmr_sg_stats(void *mroute)
413 {
414 return 0;
415 }
416
417 int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
418 {
419 return 0;
420 }
421
422 int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
423 {
424 return 0;
425 }
426
427 int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
428 struct in_addr vtep_ip, u_char sticky)
429 {
430 return 0;
431 }
432
433 int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
434 struct in_addr vtep_ip, int local)
435 {
436 return 0;
437 }
438
439 int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
440 struct ethaddr *mac)
441 {
442 return 0;
443 }
444
445 int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
446 {
447 return 0;
448 }
449
450 #endif /* !HAVE_NETLINK */