]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
zebra: Having one goto in a function to just return is silly
[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"
174482ef 36#include "lib_errors.h"
718e3744 37
38#include "zebra/debug.h"
39#include "zebra/rib.h"
6621ca86 40#include "zebra/rt.h"
dc95824a 41#include "zebra/kernel_socket.h"
d3e2c74a 42#include "zebra/zebra_mpls.h"
364fed6b 43#include "zebra/zebra_errors.h"
718e3744 44
edd7c245 45extern struct zebra_privs_t zserv_privs;
46
5e8c8947 47#ifdef __OpenBSD__
8ecdb26e 48static int kernel_rtm_add_labels(struct mpls_label_stack *nh_label,
5e8c8947
RW
49 struct sockaddr_mpls *smpls)
50{
51 if (nh_label->num_labels > 1) {
e914ccbe 52 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
9df414fe
QY
53 "%s: can't push %u labels at "
54 "once (maximum is 1)",
55 __func__, nh_label->num_labels);
5e8c8947
RW
56 return -1;
57 }
58
59 memset(smpls, 0, sizeof(*smpls));
60 smpls->smpls_len = sizeof(*smpls);
61 smpls->smpls_family = AF_MPLS;
62 smpls->smpls_label = htonl(nh_label->label[0] << MPLS_LABEL_OFFSET);
63
64 return 0;
65}
66#endif
67
718e3744 68/* Interface between zebra message and rtm message. */
08ea27d1
DS
69static int kernel_rtm(int cmd, const struct prefix *p,
70 const struct nexthop_group *ng, uint32_t metric)
718e3744 71
72{
ca2c70bd 73 union sockunion sin_dest, sin_mask, sin_gate;
fe6c7157 74#ifdef __OpenBSD__
d62a17ae 75 struct sockaddr_mpls smpls;
d3e2c74a 76#endif
d62a17ae 77 union sockunion *smplsp = NULL;
78 struct nexthop *nexthop;
79 int nexthop_num = 0;
80 ifindex_t ifindex = 0;
4dd39a0e 81 bool gate = false;
d62a17ae 82 int error;
981dc13f 83 char gate_buf[INET6_BUFSIZ];
d62a17ae 84 char prefix_buf[PREFIX_STRLEN];
a8309422 85 enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
d62a17ae 86
683c06ad 87 prefix2str(p, prefix_buf, sizeof(prefix_buf));
718e3744 88
86afd529
DS
89 /*
90 * We only have the ability to ADD or DELETE at this point
91 * in time.
92 */
93 if (cmd != RTM_ADD && cmd != RTM_DELETE) {
94 if (IS_ZEBRA_DEBUG_KERNEL)
95 zlog_debug("%s: %s odd command %s for flags %d",
96 __func__, prefix_buf,
97 lookup_msg(rtm_type_str, cmd, NULL),
98 nexthop->flags);
99 return 0;
100 }
101
08ea27d1
DS
102 memset(&sin_dest, 0, sizeof(sin_dest));
103 memset(&sin_gate, 0, sizeof(sin_gate));
ca2c70bd 104 memset(&sin_mask, 0, sizeof(sin_mask));
718e3744 105
08ea27d1
DS
106 switch (p->family) {
107 case AF_INET:
108 sin_dest.sin.sin_family = AF_INET;
08ea27d1
DS
109 sin_dest.sin.sin_addr = p->u.prefix4;
110 sin_gate.sin.sin_family = AF_INET;
0413dd75
RZ
111#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
112 sin_dest.sin.sin_len = sizeof(struct sockaddr_in);
113 sin_gate.sin.sin_len = sizeof(struct sockaddr_in);
114#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
08ea27d1
DS
115 break;
116 case AF_INET6:
117 sin_dest.sin6.sin6_family = AF_INET6;
08ea27d1
DS
118 sin_dest.sin6.sin6_addr = p->u.prefix6;
119 sin_gate.sin6.sin6_family = AF_INET6;
0413dd75
RZ
120#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
121 sin_dest.sin6.sin6_len = sizeof(struct sockaddr_in6);
122 sin_gate.sin6.sin6_len = sizeof(struct sockaddr_in6);
123#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
08ea27d1
DS
124 break;
125 }
718e3744 126
d62a17ae 127 /* Make gateway. */
01ce7cba 128 for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
86afd529
DS
129 /*
130 * We only want to use the actual good nexthops
131 */
132 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ||
133 !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
d62a17ae 134 continue;
135
c2519893 136 smplsp = NULL;
4dd39a0e 137 gate = false;
981dc13f 138 snprintf(gate_buf, sizeof(gate_buf), "NULL");
d62a17ae 139
86afd529
DS
140 switch (nexthop->type) {
141 case NEXTHOP_TYPE_IPV4:
142 case NEXTHOP_TYPE_IPV4_IFINDEX:
143 sin_gate.sin.sin_addr = nexthop->gate.ipv4;
144 sin_gate.sin.sin_family = AF_INET;
0413dd75
RZ
145#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
146 sin_gate.sin.sin_len = sizeof(struct sockaddr_in);
147#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529 148 ifindex = nexthop->ifindex;
4dd39a0e 149 gate = true;
86afd529
DS
150 break;
151 case NEXTHOP_TYPE_IPV6:
152 case NEXTHOP_TYPE_IPV6_IFINDEX:
153 sin_gate.sin6.sin6_addr = nexthop->gate.ipv6;
154 sin_gate.sin6.sin6_family = AF_INET6;
0413dd75
RZ
155#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
156 sin_gate.sin6.sin6_len = sizeof(struct sockaddr_in6);
157#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529 158 ifindex = nexthop->ifindex;
08ea27d1
DS
159/* Under kame set interface index to link local address */
160#ifdef KAME
161
162#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
c9277ebb
DS
163 do { \
164 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
165 (a).s6_addr[3] = (i)&0xff; \
166 } while (0)
08ea27d1 167
86afd529
DS
168 if (IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6.sin6_addr))
169 SET_IN6_LINKLOCAL_IFINDEX(
170 sin_gate.sin6.sin6_addr,
171 ifindex);
08ea27d1
DS
172#endif /* KAME */
173
4dd39a0e 174 gate = true;
86afd529
DS
175 break;
176 case NEXTHOP_TYPE_IFINDEX:
177 ifindex = nexthop->ifindex;
178 break;
179 case NEXTHOP_TYPE_BLACKHOLE:
180 bh_type = nexthop->bh_type;
181 switch (p->family) {
182 case AFI_IP: {
183 struct in_addr loopback;
184 loopback.s_addr = htonl(INADDR_LOOPBACK);
185 sin_gate.sin.sin_addr = loopback;
0413dd75
RZ
186#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
187 sin_gate.sin.sin_len =
188 sizeof(struct sockaddr_in);
189#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
4dd39a0e 190 gate = true;
86afd529 191 }
08ea27d1 192 break;
86afd529 193 case AFI_IP6:
08ea27d1 194 break;
d62a17ae 195 }
86afd529
DS
196 }
197
198 switch (p->family) {
199 case AF_INET:
18d10d88
DS
200 masklen2ip(p->prefixlen, &sin_mask.sin.sin_addr);
201 sin_mask.sin.sin_family = AF_INET;
0413dd75
RZ
202#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
203 sin_mask.sin.sin_len = sizeof(struct sockaddr_in);
204#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529
DS
205 break;
206 case AF_INET6:
18d10d88
DS
207 masklen2ip6(p->prefixlen, &sin_mask.sin6.sin6_addr);
208 sin_mask.sin6.sin6_family = AF_INET6;
0413dd75
RZ
209#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
210 sin_mask.sin6.sin6_len = sizeof(struct sockaddr_in6);
211#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86afd529
DS
212 break;
213 }
718e3744 214
fe6c7157 215#ifdef __OpenBSD__
b24204be
MS
216 if (nexthop->nh_label) {
217 if (kernel_rtm_add_labels(nexthop->nh_label,
218 &smpls) != 0)
219 continue;
220 smplsp = (union sockunion *)&smpls;
221 }
d3e2c74a 222#endif
18d10d88 223 error = rtm_write(cmd, &sin_dest, &sin_mask,
86afd529
DS
224 gate ? &sin_gate : NULL, smplsp,
225 ifindex, bh_type, metric);
226
227 if (IS_ZEBRA_DEBUG_KERNEL) {
228 if (!gate) {
981dc13f
RZ
229 zlog_debug(
230 "%s: %s: attention! gate not found for re",
231 __func__, prefix_buf);
232 } else {
233 switch (p->family) {
234 case AFI_IP:
235 inet_ntop(AF_INET,
236 &sin_gate.sin.sin_addr,
237 gate_buf, sizeof(gate_buf));
238 break;
239
240 case AFI_IP6:
241 inet_ntop(AF_INET6,
242 &sin_gate.sin6.sin6_addr,
243 gate_buf, sizeof(gate_buf));
244 break;
245
246 default:
247 snprintf(gate_buf, sizeof(gate_buf),
248 "(invalid-af)");
249 break;
250 }
251 }
86afd529
DS
252 }
253 switch (error) {
254 /* We only flag nexthops as being in FIB if
255 * rtm_write() did its work. */
256 case ZEBRA_ERR_NOERROR:
257 nexthop_num++;
258 if (IS_ZEBRA_DEBUG_KERNEL)
259 zlog_debug("%s: %s: successfully did NH %s",
260 __func__, prefix_buf, gate_buf);
261 if (cmd == RTM_ADD)
262 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
263 break;
264
265 /* The only valid case for this error is
266 * kernel's failure to install a multipath
267 * route, which is common for FreeBSD. This
268 * should be ignored silently, but logged as an error
269 * otherwise.
270 */
271 case ZEBRA_ERR_RTEXIST:
272 if (cmd != RTM_ADD)
273 flog_err(EC_LIB_SYSTEM_CALL,
274 "%s: rtm_write() returned %d for command %d",
275 __func__, error, cmd);
276 continue;
d62a17ae 277
86afd529
DS
278 /* Note any unexpected status returns */
279 default:
31be7dbb
RZ
280 flog_err(
281 EC_LIB_SYSTEM_CALL,
282 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
283 __func__, prefix_buf, error,
284 lookup_msg(rtm_type_str, cmd, NULL));
86afd529
DS
285 break;
286 }
d62a17ae 287 } /* for (ALL_NEXTHOPS(...))*/
288
289 /* If there was no useful nexthop, then complain. */
560e3136
DS
290 if (nexthop_num == 0) {
291 if (IS_ZEBRA_DEBUG_KERNEL)
31be7dbb
RZ
292 zlog_debug(
293 "%s: No useful nexthops were found in RIB prefix %s",
294 __func__, prefix_buf);
560e3136
DS
295 return 1;
296 }
d62a17ae 297
298 return 0; /*XXX*/
718e3744 299}
300
01ce7cba
MS
301/*
302 * Update or delete a prefix from the kernel,
303 * using info from a dataplane context struct.
304 */
25779064 305enum zebra_dplane_result kernel_route_update(struct zebra_dplane_ctx *ctx)
01ce7cba
MS
306{
307 enum zebra_dplane_result res = ZEBRA_DPLANE_REQUEST_SUCCESS;
308
309 if (dplane_ctx_get_src(ctx) != NULL) {
310 zlog_err("route add: IPv6 sourcedest routes unsupported!");
e677d109 311 return ZEBRA_DPLANE_REQUEST_FAILURE;
01ce7cba
MS
312 }
313
49ddb66a 314 frr_elevate_privs(&zserv_privs) {
f183e380
MS
315
316 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE)
317 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
318 dplane_ctx_get_ng(ctx),
319 dplane_ctx_get_metric(ctx));
320 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL)
321 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
322 dplane_ctx_get_ng(ctx),
323 dplane_ctx_get_metric(ctx));
324 else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) {
325 /* Must do delete and add separately -
326 * no update available
327 */
328 kernel_rtm(RTM_DELETE, dplane_ctx_get_dest(ctx),
329 dplane_ctx_get_old_ng(ctx),
330 dplane_ctx_get_old_metric(ctx));
331
332 kernel_rtm(RTM_ADD, dplane_ctx_get_dest(ctx),
333 dplane_ctx_get_ng(ctx),
334 dplane_ctx_get_metric(ctx));
335 } else {
336 zlog_err("Invalid routing socket update op %s (%u)",
337 dplane_op2str(dplane_ctx_get_op(ctx)),
338 dplane_ctx_get_op(ctx));
339 res = ZEBRA_DPLANE_REQUEST_FAILURE;
340 }
341 } /* Elevated privs */
01ce7cba 342
01ce7cba
MS
343 return res;
344}
345
d62a17ae 346int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
5895d33f 347 int llalen, ns_id_t ns_id)
6b8a5694 348{
d62a17ae 349 /* TODO */
350 return 0;
6b8a5694 351}
1498c059 352
43b5cc5e 353extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 354{
d62a17ae 355 return 0;
1498c059 356}
13d60d35 357
d62a17ae 358int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 359{
d62a17ae 360 return 0;
13d60d35 361}
362
d62a17ae 363int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 364{
d62a17ae 365 return 0;
13d60d35 366}
2232a77c 367
d62a17ae 368int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
a37f4598 369 struct in_addr vtep_ip, bool sticky)
2232a77c 370{
d62a17ae 371 return 0;
2232a77c 372}
373
d62a17ae 374int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
d63c1b18 375 struct in_addr vtep_ip)
2232a77c 376{
d62a17ae 377 return 0;
2232a77c 378}
379
d62a17ae 380int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
68e33151 381 struct ethaddr *mac, uint8_t flags)
2232a77c 382{
d62a17ae 383 return 0;
2232a77c 384}
385
d62a17ae 386int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 387{
d62a17ae 388 return 0;
2232a77c 389}
e0ae31b8
DS
390
391extern int kernel_interface_set_master(struct interface *master,
392 struct interface *slave)
393{
394 return 0;
395}
0ecfe5bf 396
dc7b3cae
DS
397uint32_t kernel_get_speed(struct interface *ifp)
398{
399 return ifp->speed;
400}
401
ddfeb486 402#endif /* !HAVE_NETLINK */