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