]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
zebra: clean up const use in bsd build
[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. */
c886868f
MS
91static int kernel_rtm_ipv4(int cmd, const struct prefix *p,
92 struct route_entry *re)
718e3744 93
94{
d62a17ae 95 struct sockaddr_in *mask = NULL;
96 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fe6c7157 97#ifdef __OpenBSD__
d62a17ae 98 struct sockaddr_mpls smpls;
d3e2c74a 99#endif
d62a17ae 100 union sockunion *smplsp = NULL;
101 struct nexthop *nexthop;
102 int nexthop_num = 0;
103 ifindex_t ifindex = 0;
104 int gate = 0;
105 int error;
106 char prefix_buf[PREFIX_STRLEN];
a8309422 107 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 108
109 if (IS_ZEBRA_DEBUG_RIB)
110 prefix2str(p, prefix_buf, sizeof(prefix_buf));
111 memset(&sin_dest, 0, sizeof(struct sockaddr_in));
112 sin_dest.sin_family = AF_INET;
6f0e3f6e 113#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 114 sin_dest.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 115#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 116 sin_dest.sin_addr = p->u.prefix4;
718e3744 117
d62a17ae 118 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
718e3744 119
d62a17ae 120 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
121 sin_gate.sin_family = AF_INET;
6f0e3f6e 122#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 123 sin_gate.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 124#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 125
d62a17ae 126 /* Make gateway. */
7ee30f28 127 for (ALL_NEXTHOPS(re->ng, nexthop)) {
d62a17ae 128 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
129 continue;
130
131 gate = 0;
132 char gate_buf[INET_ADDRSTRLEN] = "NULL";
133
134 /*
135 * XXX We need to refrain from kernel operations in some cases,
136 * but this if statement seems overly cautious - what about
137 * other than ADD and DELETE?
138 */
996c9314 139 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
d62a17ae 140 || (cmd == RTM_DELETE
141 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))) {
142 if (nexthop->type == NEXTHOP_TYPE_IPV4
143 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
144 sin_gate.sin_addr = nexthop->gate.ipv4;
145 gate = 1;
146 }
147 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
148 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
149 ifindex = nexthop->ifindex;
150 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
151 struct in_addr loopback;
152 loopback.s_addr = htonl(INADDR_LOOPBACK);
153 sin_gate.sin_addr = loopback;
a8309422 154 bh_type = nexthop->bh_type;
d62a17ae 155 gate = 1;
156 }
157
158 if (gate && p->prefixlen == 32)
159 mask = NULL;
160 else {
161 masklen2ip(p->prefixlen, &sin_mask.sin_addr);
162 sin_mask.sin_family = AF_INET;
6f0e3f6e 163#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 164 sin_mask.sin_len =
165 sin_masklen(sin_mask.sin_addr);
6f0e3f6e 166#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 167 mask = &sin_mask;
168 }
718e3744 169
fe6c7157 170#ifdef __OpenBSD__
5e8c8947
RW
171 if (nexthop->nh_label
172 && !kernel_rtm_add_labels(nexthop->nh_label,
173 &smpls))
174 continue;
175 smplsp = (union sockunion *)&smpls;
d3e2c74a
RW
176#endif
177
60466a63
QY
178 error = rtm_write(cmd, (union sockunion *)&sin_dest,
179 (union sockunion *)mask,
180 gate ? (union sockunion *)&sin_gate
181 : NULL,
182 smplsp, ifindex, bh_type, re->metric);
d62a17ae 183
184 if (IS_ZEBRA_DEBUG_RIB) {
185 if (!gate) {
186 zlog_debug(
187 "%s: %s: attention! gate not found for re %p",
188 __func__, prefix_buf, re);
189 route_entry_dump(p, NULL, re);
190 } else
191 inet_ntop(AF_INET, &sin_gate.sin_addr,
192 gate_buf, INET_ADDRSTRLEN);
193 }
194
195 switch (error) {
196 /* We only flag nexthops as being in FIB if rtm_write()
197 * did its work. */
198 case ZEBRA_ERR_NOERROR:
199 nexthop_num++;
200 if (IS_ZEBRA_DEBUG_RIB)
201 zlog_debug(
202 "%s: %s: successfully did NH %s",
203 __func__, prefix_buf, gate_buf);
d62a17ae 204 break;
205
206 /* The only valid case for this error is kernel's
207 * failure to install
208 * a multipath route, which is common for FreeBSD. This
209 * should be
210 * ignored silently, but logged as an error otherwise.
211 */
212 case ZEBRA_ERR_RTEXIST:
213 if (cmd != RTM_ADD)
214 zlog_err(
215 "%s: rtm_write() returned %d for command %d",
216 __func__, error, cmd);
217 continue;
218 break;
219
220 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
221 * FIB, it isn't
222 * normal to get any other messages in ANY case.
223 */
224 case ZEBRA_ERR_RTNOEXIST:
225 case ZEBRA_ERR_RTUNREACH:
226 default:
227 zlog_err(
228 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
229 __func__,
230 prefix2str(p, prefix_buf,
231 sizeof(prefix_buf)),
232 error,
233 lookup_msg(rtm_type_str, cmd, NULL));
234 break;
235 }
236 } /* if (cmd and flags make sense) */
237 else if (IS_ZEBRA_DEBUG_RIB)
238 zlog_debug("%s: odd command %s for flags %d", __func__,
239 lookup_msg(rtm_type_str, cmd, NULL),
240 nexthop->flags);
241 } /* for (ALL_NEXTHOPS(...))*/
242
243 /* If there was no useful nexthop, then complain. */
244 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
245 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
246 __func__, re);
247
248 return 0; /*XXX*/
718e3744 249}
250
746c4f02 251#ifdef SIN6_LEN
718e3744 252/* Calculate sin6_len value for netmask socket value. */
d62a17ae 253static int sin6_masklen(struct in6_addr mask)
718e3744 254{
d62a17ae 255 struct sockaddr_in6 sin6;
256 char *p, *lim;
257 int len;
718e3744 258
d62a17ae 259 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
260 return sizeof(long);
718e3744 261
d62a17ae 262 sin6.sin6_addr = mask;
263 len = sizeof(struct sockaddr_in6);
718e3744 264
d62a17ae 265 lim = (char *)&sin6.sin6_addr;
266 p = lim + sizeof(sin6.sin6_addr);
718e3744 267
d62a17ae 268 while (*--p == 0 && p >= lim)
269 len--;
718e3744 270
d62a17ae 271 return len;
718e3744 272}
746c4f02 273#endif /* SIN6_LEN */
718e3744 274
718e3744 275/* Interface between zebra message and rtm message. */
c886868f
MS
276static int kernel_rtm_ipv6(int cmd, const struct prefix *p,
277 struct route_entry *re)
718e3744 278{
d62a17ae 279 struct sockaddr_in6 *mask;
280 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
5e8c8947
RW
281#ifdef __OpenBSD__
282 struct sockaddr_mpls smpls;
283#endif
284 union sockunion *smplsp = NULL;
d62a17ae 285 struct nexthop *nexthop;
286 int nexthop_num = 0;
287 ifindex_t ifindex = 0;
288 int gate = 0;
289 int error;
a8309422 290 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 291
292 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
293 sin_dest.sin6_family = AF_INET6;
718e3744 294#ifdef SIN6_LEN
d62a17ae 295 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
718e3744 296#endif /* SIN6_LEN */
d62a17ae 297 sin_dest.sin6_addr = p->u.prefix6;
718e3744 298
d62a17ae 299 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
718e3744 300
d62a17ae 301 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
302 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 303#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 304 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
6f0e3f6e 305#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 306
d62a17ae 307 /* Make gateway. */
7ee30f28 308 for (ALL_NEXTHOPS(re->ng, nexthop)) {
d62a17ae 309 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
310 continue;
fa713d9e 311
d62a17ae 312 gate = 0;
718e3744 313
996c9314 314 if ((cmd == RTM_ADD && NEXTHOP_IS_ACTIVE(nexthop->flags))
2d74d637 315 || (cmd == RTM_DELETE)) {
d62a17ae 316 if (nexthop->type == NEXTHOP_TYPE_IPV6
317 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
318 sin_gate.sin6_addr = nexthop->gate.ipv6;
319 gate = 1;
320 }
321 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
322 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
323 ifindex = nexthop->ifindex;
324
a8309422
DL
325 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
326 bh_type = nexthop->bh_type;
d62a17ae 327 }
328
329/* Under kame set interface index to link local address. */
718e3744 330#ifdef KAME
331
d62a17ae 332#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
333 do { \
334 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
335 (a).s6_addr[3] = (i)&0xff; \
336 } while (0)
718e3744 337
d62a17ae 338 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
339 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
718e3744 340#endif /* KAME */
341
d62a17ae 342 if (gate && p->prefixlen == 128)
343 mask = NULL;
344 else {
345 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
346 sin_mask.sin6_family = AF_INET6;
718e3744 347#ifdef SIN6_LEN
d62a17ae 348 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
718e3744 349#endif /* SIN6_LEN */
d62a17ae 350 mask = &sin_mask;
351 }
718e3744 352
5e8c8947
RW
353#ifdef __OpenBSD__
354 if (nexthop->nh_label
355 && !kernel_rtm_add_labels(nexthop->nh_label, &smpls))
356 continue;
357 smplsp = (union sockunion *)&smpls;
358#endif
359
d62a17ae 360 error = rtm_write(cmd, (union sockunion *)&sin_dest,
361 (union sockunion *)mask,
362 gate ? (union sockunion *)&sin_gate : NULL,
a8309422 363 smplsp, ifindex, bh_type, re->metric);
d62a17ae 364 (void)error;
718e3744 365
d62a17ae 366 nexthop_num++;
367 }
718e3744 368
d62a17ae 369 /* If there is no useful nexthop then return. */
370 if (nexthop_num == 0) {
371 if (IS_ZEBRA_DEBUG_KERNEL)
372 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
373 return 0;
374 }
718e3744 375
d62a17ae 376 return 0; /*XXX*/
718e3744 377}
378
c886868f 379static int kernel_rtm(int cmd, const struct prefix *p, struct route_entry *re)
62ccf1e5 380{
d62a17ae 381 switch (PREFIX_FAMILY(p)) {
382 case AF_INET:
383 return kernel_rtm_ipv4(cmd, p, re);
384 case AF_INET6:
385 return kernel_rtm_ipv6(cmd, p, re);
386 }
387 return 0;
62ccf1e5
TT
388}
389
1e885672 390enum dp_req_result kernel_route_rib(struct route_node *rn,
86391e56
MS
391 const struct prefix *p,
392 const struct prefix *src_p,
1e885672
DS
393 struct route_entry *old,
394 struct route_entry *new)
718e3744 395{
d62a17ae 396 int route = 0;
edd7c245 397
d62a17ae 398 if (src_p && src_p->prefixlen) {
399 zlog_err("route add: IPv6 sourcedest routes unsupported!");
f47598b0 400 return DP_REQUEST_FAILURE;
d62a17ae 401 }
05737783 402
d62a17ae 403 if (zserv_privs.change(ZPRIVS_RAISE))
404 zlog_err("Can't raise privileges");
be717a0a 405
d62a17ae 406 if (old)
407 route |= kernel_rtm(RTM_DELETE, p, old);
62ccf1e5 408
d62a17ae 409 if (new)
410 route |= kernel_rtm(RTM_ADD, p, new);
be717a0a 411
d62a17ae 412 if (zserv_privs.change(ZPRIVS_LOWER))
413 zlog_err("Can't lower privileges");
edd7c245 414
0c555cc6 415 if (new) {
996c9314
LB
416 kernel_route_rib_pass_fail(
417 rn, p, new,
215181cb
DS
418 (!route) ? DP_INSTALL_SUCCESS
419 : DP_INSTALL_FAILURE);
0c555cc6 420 } else {
7d974ba3 421 kernel_route_rib_pass_fail(rn, p, old,
996c9314 422 (!route)
215181cb
DS
423 ? DP_DELETE_SUCCESS
424 : DP_DELETE_FAILURE);
0c555cc6 425 }
1e885672
DS
426
427 return DP_REQUEST_SUCCESS;
718e3744 428}
6b8a5694 429
d62a17ae 430int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 431 int llalen, ns_id_t ns_id)
6b8a5694 432{
d62a17ae 433 /* TODO */
434 return 0;
6b8a5694 435}
1498c059 436
43b5cc5e 437extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 438{
d62a17ae 439 return 0;
1498c059 440}
13d60d35 441
d62a17ae 442int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 443{
d62a17ae 444 return 0;
13d60d35 445}
446
d62a17ae 447int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 448{
d62a17ae 449 return 0;
13d60d35 450}
2232a77c 451
d62a17ae 452int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
d7c0a89a 453 struct in_addr vtep_ip, uint8_t sticky)
2232a77c 454{
d62a17ae 455 return 0;
2232a77c 456}
457
d62a17ae 458int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
459 struct in_addr vtep_ip, int local)
2232a77c 460{
d62a17ae 461 return 0;
2232a77c 462}
463
d62a17ae 464int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
465 struct ethaddr *mac)
2232a77c 466{
d62a17ae 467 return 0;
2232a77c 468}
469
d62a17ae 470int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 471{
d62a17ae 472 return 0;
2232a77c 473}
e0ae31b8
DS
474
475extern int kernel_interface_set_master(struct interface *master,
476 struct interface *slave)
477{
478 return 0;
479}
0ecfe5bf 480
dc7b3cae
DS
481uint32_t kernel_get_speed(struct interface *ifp)
482{
483 return ifp->speed;
484}
485
ddfeb486 486#endif /* !HAVE_NETLINK */