]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
lib, zebra: Allow zapi to send down the tableid
[mirror_frr.git] / zebra / rt_socket.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 20 */
21
22#include <zebra.h>
ddfeb486
DL
23
24#ifndef HAVE_NETLINK
25
fe6c7157 26#ifdef __OpenBSD__
d3e2c74a
RW
27#include <netmpls/mpls.h>
28#endif
718e3744 29
30#include "if.h"
31#include "prefix.h"
32#include "sockunion.h"
33#include "log.h"
edd7c245 34#include "privs.h"
13d60d35 35#include "vxlan.h"
718e3744 36
37#include "zebra/debug.h"
38#include "zebra/rib.h"
6621ca86 39#include "zebra/rt.h"
dc95824a 40#include "zebra/kernel_socket.h"
d3e2c74a 41#include "zebra/zebra_mpls.h"
718e3744 42
edd7c245 43extern struct zebra_privs_t zserv_privs;
44
746c4f02 45#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 46/* Adjust netmask socket length. Return value is a adjusted sin_len
47 value. */
d62a17ae 48static int sin_masklen(struct in_addr mask)
718e3744 49{
d62a17ae 50 char *p, *lim;
51 int len;
52 struct sockaddr_in sin;
718e3744 53
d62a17ae 54 if (mask.s_addr == 0)
55 return sizeof(long);
718e3744 56
d62a17ae 57 sin.sin_addr = mask;
58 len = sizeof(struct sockaddr_in);
718e3744 59
d62a17ae 60 lim = (char *)&sin.sin_addr;
61 p = lim + sizeof(sin.sin_addr);
718e3744 62
d62a17ae 63 while (*--p == 0 && p >= lim)
64 len--;
65 return len;
718e3744 66}
746c4f02 67#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 68
5e8c8947 69#ifdef __OpenBSD__
8ecdb26e 70static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
5e8c8947
RW
71 struct sockaddr_mpls *smpls)
72{
73 if (nh_label->num_labels > 1) {
74 zlog_warn(
75 "%s: can't push %u labels at "
76 "once (maximum is 1)",
77 __func__, nh_label->num_labels);
78 return -1;
79 }
80
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);
85
86 return 0;
87}
88#endif
89
718e3744 90/* Interface between zebra message and rtm message. */
d62a17ae 91static int kernel_rtm_ipv4(int cmd, struct prefix *p, struct route_entry *re)
718e3744 92
93{
d62a17ae 94 struct sockaddr_in *mask = NULL;
95 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fe6c7157 96#ifdef __OpenBSD__
d62a17ae 97 struct sockaddr_mpls smpls;
d3e2c74a 98#endif
d62a17ae 99 union sockunion *smplsp = NULL;
100 struct nexthop *nexthop;
101 int nexthop_num = 0;
102 ifindex_t ifindex = 0;
103 int gate = 0;
104 int error;
105 char prefix_buf[PREFIX_STRLEN];
a8309422 106 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 107
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;
6f0e3f6e 112#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 113 sin_dest.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 114#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 115 sin_dest.sin_addr = p->u.prefix4;
718e3744 116
d62a17ae 117 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
718e3744 118
d62a17ae 119 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
120 sin_gate.sin_family = AF_INET;
6f0e3f6e 121#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 122 sin_gate.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 123#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 124
d62a17ae 125 /* Make gateway. */
126 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
127 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
128 continue;
129
130 gate = 0;
131 char gate_buf[INET_ADDRSTRLEN] = "NULL";
132
133 /*
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?
137 */
996c9314 138 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
d62a17ae 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;
144 gate = 1;
145 }
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;
a8309422 153 bh_type = nexthop->bh_type;
d62a17ae 154 gate = 1;
155 }
156
157 if (gate && p->prefixlen == 32)
158 mask = NULL;
159 else {
160 masklen2ip(p->prefixlen, &sin_mask.sin_addr);
161 sin_mask.sin_family = AF_INET;
6f0e3f6e 162#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 163 sin_mask.sin_len =
164 sin_masklen(sin_mask.sin_addr);
6f0e3f6e 165#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 166 mask = &sin_mask;
167 }
718e3744 168
fe6c7157 169#ifdef __OpenBSD__
5e8c8947
RW
170 if (nexthop->nh_label
171 && !kernel_rtm_add_labels(nexthop->nh_label,
172 &smpls))
173 continue;
174 smplsp = (union sockunion *)&smpls;
d3e2c74a
RW
175#endif
176
60466a63
QY
177 error = rtm_write(cmd, (union sockunion *)&sin_dest,
178 (union sockunion *)mask,
179 gate ? (union sockunion *)&sin_gate
180 : NULL,
181 smplsp, ifindex, bh_type, re->metric);
d62a17ae 182
183 if (IS_ZEBRA_DEBUG_RIB) {
184 if (!gate) {
185 zlog_debug(
186 "%s: %s: attention! gate not found for re %p",
187 __func__, prefix_buf, re);
188 route_entry_dump(p, NULL, re);
189 } else
190 inet_ntop(AF_INET, &sin_gate.sin_addr,
191 gate_buf, INET_ADDRSTRLEN);
192 }
193
194 switch (error) {
195 /* We only flag nexthops as being in FIB if rtm_write()
196 * did its work. */
197 case ZEBRA_ERR_NOERROR:
198 nexthop_num++;
199 if (IS_ZEBRA_DEBUG_RIB)
200 zlog_debug(
201 "%s: %s: successfully did NH %s",
202 __func__, prefix_buf, gate_buf);
d62a17ae 203 break;
204
205 /* The only valid case for this error is kernel's
206 * failure to install
207 * a multipath route, which is common for FreeBSD. This
208 * should be
209 * ignored silently, but logged as an error otherwise.
210 */
211 case ZEBRA_ERR_RTEXIST:
212 if (cmd != RTM_ADD)
213 zlog_err(
214 "%s: rtm_write() returned %d for command %d",
215 __func__, error, cmd);
216 continue;
217 break;
218
219 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
220 * FIB, it isn't
221 * normal to get any other messages in ANY case.
222 */
223 case ZEBRA_ERR_RTNOEXIST:
224 case ZEBRA_ERR_RTUNREACH:
225 default:
226 zlog_err(
227 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
228 __func__,
229 prefix2str(p, prefix_buf,
230 sizeof(prefix_buf)),
231 error,
232 lookup_msg(rtm_type_str, cmd, NULL));
233 break;
234 }
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),
239 nexthop->flags);
240 } /* for (ALL_NEXTHOPS(...))*/
241
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",
245 __func__, re);
246
247 return 0; /*XXX*/
718e3744 248}
249
746c4f02 250#ifdef SIN6_LEN
718e3744 251/* Calculate sin6_len value for netmask socket value. */
d62a17ae 252static int sin6_masklen(struct in6_addr mask)
718e3744 253{
d62a17ae 254 struct sockaddr_in6 sin6;
255 char *p, *lim;
256 int len;
718e3744 257
d62a17ae 258 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
259 return sizeof(long);
718e3744 260
d62a17ae 261 sin6.sin6_addr = mask;
262 len = sizeof(struct sockaddr_in6);
718e3744 263
d62a17ae 264 lim = (char *)&sin6.sin6_addr;
265 p = lim + sizeof(sin6.sin6_addr);
718e3744 266
d62a17ae 267 while (*--p == 0 && p >= lim)
268 len--;
718e3744 269
d62a17ae 270 return len;
718e3744 271}
746c4f02 272#endif /* SIN6_LEN */
718e3744 273
718e3744 274/* Interface between zebra message and rtm message. */
d62a17ae 275static int kernel_rtm_ipv6(int cmd, struct prefix *p, struct route_entry *re)
718e3744 276{
d62a17ae 277 struct sockaddr_in6 *mask;
278 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
5e8c8947
RW
279#ifdef __OpenBSD__
280 struct sockaddr_mpls smpls;
281#endif
282 union sockunion *smplsp = NULL;
d62a17ae 283 struct nexthop *nexthop;
284 int nexthop_num = 0;
285 ifindex_t ifindex = 0;
286 int gate = 0;
287 int error;
a8309422 288 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 289
290 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
291 sin_dest.sin6_family = AF_INET6;
718e3744 292#ifdef SIN6_LEN
d62a17ae 293 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
718e3744 294#endif /* SIN6_LEN */
d62a17ae 295 sin_dest.sin6_addr = p->u.prefix6;
718e3744 296
d62a17ae 297 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
718e3744 298
d62a17ae 299 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
300 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 301#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 302 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
6f0e3f6e 303#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 304
d62a17ae 305 /* Make gateway. */
306 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
307 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
308 continue;
fa713d9e 309
d62a17ae 310 gate = 0;
718e3744 311
996c9314 312 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
2d74d637 313 || (cmd == RTM_DELETE)) {
d62a17ae 314 if (nexthop->type == NEXTHOP_TYPE_IPV6
315 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
316 sin_gate.sin6_addr = nexthop->gate.ipv6;
317 gate = 1;
318 }
319 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
320 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
321 ifindex = nexthop->ifindex;
322
a8309422
DL
323 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
324 bh_type = nexthop->bh_type;
d62a17ae 325 }
326
327/* Under kame set interface index to link local address. */
718e3744 328#ifdef KAME
329
d62a17ae 330#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
331 do { \
332 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
333 (a).s6_addr[3] = (i)&0xff; \
334 } while (0)
718e3744 335
d62a17ae 336 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
337 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
718e3744 338#endif /* KAME */
339
d62a17ae 340 if (gate && p->prefixlen == 128)
341 mask = NULL;
342 else {
343 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
344 sin_mask.sin6_family = AF_INET6;
718e3744 345#ifdef SIN6_LEN
d62a17ae 346 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
718e3744 347#endif /* SIN6_LEN */
d62a17ae 348 mask = &sin_mask;
349 }
718e3744 350
5e8c8947
RW
351#ifdef __OpenBSD__
352 if (nexthop->nh_label
353 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
354 continue;
355 smplsp = (union sockunion *)&smpls;
356#endif
357
d62a17ae 358 error = rtm_write(cmd, (union sockunion *)&sin_dest,
359 (union sockunion *)mask,
360 gate ? (union sockunion *)&sin_gate : NULL,
a8309422 361 smplsp, ifindex, bh_type, re->metric);
d62a17ae 362 (void)error;
718e3744 363
d62a17ae 364 nexthop_num++;
365 }
718e3744 366
d62a17ae 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.");
371 return 0;
372 }
718e3744 373
d62a17ae 374 return 0; /*XXX*/
718e3744 375}
376
d62a17ae 377static int kernel_rtm(int cmd, struct prefix *p, struct route_entry *re)
62ccf1e5 378{
d62a17ae 379 switch (PREFIX_FAMILY(p)) {
380 case AF_INET:
381 return kernel_rtm_ipv4(cmd, p, re);
382 case AF_INET6:
383 return kernel_rtm_ipv6(cmd, p, re);
384 }
385 return 0;
62ccf1e5
TT
386}
387
7d974ba3
DS
388void kernel_route_rib(struct route_node *rn, struct prefix *p,
389 struct prefix *src_p, struct route_entry *old,
390 struct route_entry *new)
718e3744 391{
d62a17ae 392 int route = 0;
edd7c245 393
d62a17ae 394 if (src_p && src_p->prefixlen) {
395 zlog_err("route add: IPv6 sourcedest routes unsupported!");
4a83e7a0 396 return;
d62a17ae 397 }
05737783 398
d62a17ae 399 if (zserv_privs.change(ZPRIVS_RAISE))
400 zlog_err("Can't raise privileges");
be717a0a 401
d62a17ae 402 if (old)
403 route |= kernel_rtm(RTM_DELETE, p, old);
62ccf1e5 404
d62a17ae 405 if (new)
406 route |= kernel_rtm(RTM_ADD, p, new);
be717a0a 407
d62a17ae 408 if (zserv_privs.change(ZPRIVS_LOWER))
409 zlog_err("Can't lower privileges");
edd7c245 410
0c555cc6 411 if (new) {
996c9314
LB
412 kernel_route_rib_pass_fail(
413 rn, p, new,
414 (!route) ? SOUTHBOUND_INSTALL_SUCCESS
415 : SOUTHBOUND_INSTALL_FAILURE);
0c555cc6 416 } else {
7d974ba3 417 kernel_route_rib_pass_fail(rn, p, old,
996c9314
LB
418 (!route)
419 ? SOUTHBOUND_DELETE_SUCCESS
420 : SOUTHBOUND_DELETE_FAILURE);
0c555cc6 421 }
718e3744 422}
6b8a5694 423
d62a17ae 424int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 425 int llalen, ns_id_t ns_id)
6b8a5694 426{
d62a17ae 427 /* TODO */
428 return 0;
6b8a5694 429}
1498c059 430
43b5cc5e 431extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 432{
d62a17ae 433 return 0;
1498c059 434}
13d60d35 435
d62a17ae 436int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 437{
d62a17ae 438 return 0;
13d60d35 439}
440
d62a17ae 441int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 442{
d62a17ae 443 return 0;
13d60d35 444}
2232a77c 445
d62a17ae 446int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
447 struct in_addr vtep_ip, u_char sticky)
2232a77c 448{
d62a17ae 449 return 0;
2232a77c 450}
451
d62a17ae 452int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
453 struct in_addr vtep_ip, int local)
2232a77c 454{
d62a17ae 455 return 0;
2232a77c 456}
457
d62a17ae 458int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
459 struct ethaddr *mac)
2232a77c 460{
d62a17ae 461 return 0;
2232a77c 462}
463
d62a17ae 464int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 465{
d62a17ae 466 return 0;
2232a77c 467}
e0ae31b8
DS
468
469extern int kernel_interface_set_master(struct interface *master,
470 struct interface *slave)
471{
472 return 0;
473}
0ecfe5bf 474
dc7b3cae
DS
475uint32_t kernel_get_speed(struct interface *ifp)
476{
477 return ifp->speed;
478}
479
ddfeb486 480#endif /* !HAVE_NETLINK */