]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_mpls_openbsd.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / zebra / zebra_mpls_openbsd.c
CommitLineData
7fc02572
RW
1/*
2 * Copyright (C) 2016 by Open Source Routing.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
7fc02572
RW
19 */
20
d3e2c74a 21#include <zebra.h>
ddfeb486
DL
22
23#ifdef OPEN_BSD
24
d3e2c74a
RW
25#include <netmpls/mpls.h>
26#include "zebra/rt.h"
27#include "zebra/zebra_mpls.h"
28#include "zebra/debug.h"
364fed6b 29#include "zebra/zebra_errors.h"
b3f2b590 30#include "zebra/zebra_router.h"
d3e2c74a
RW
31
32#include "privs.h"
33#include "prefix.h"
34#include "interface.h"
35#include "log.h"
174482ef 36#include "lib_errors.h"
d3e2c74a
RW
37
38extern struct zebra_privs_t zserv_privs;
39
40struct {
d7c0a89a 41 uint32_t rtseq;
d62a17ae 42 int fd;
d42127da 43 int ioctl_fd;
d3e2c74a
RW
44} kr_state;
45
d62a17ae 46static int kernel_send_rtmsg_v4(int action, mpls_label_t in_label,
81793ac1 47 const zebra_nhlfe_t *nhlfe)
d3e2c74a 48{
d62a17ae 49 struct iovec iov[5];
50 struct rt_msghdr hdr;
51 struct sockaddr_mpls sa_label_in, sa_label_out;
52 struct sockaddr_in nexthop;
53 int iovcnt = 0;
54 int ret;
55
56 if (IS_ZEBRA_DEBUG_KERNEL)
57 zlog_debug("%s: 0x%x, label=%u", __func__, action, in_label);
58
59 /* initialize header */
60 memset(&hdr, 0, sizeof(hdr));
61 hdr.rtm_version = RTM_VERSION;
62
63 hdr.rtm_type = action;
64 hdr.rtm_flags = RTF_UP;
65 hdr.rtm_fmask = RTF_MPLS;
66 hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */
67 hdr.rtm_msglen = sizeof(hdr);
68 hdr.rtm_hdrlen = sizeof(struct rt_msghdr);
69 hdr.rtm_priority = 0;
70 /* adjust iovec */
71 iov[iovcnt].iov_base = &hdr;
72 iov[iovcnt++].iov_len = sizeof(hdr);
73
74 /* in label */
75 memset(&sa_label_in, 0, sizeof(sa_label_in));
76 sa_label_in.smpls_len = sizeof(sa_label_in);
77 sa_label_in.smpls_family = AF_MPLS;
78 sa_label_in.smpls_label = htonl(in_label << MPLS_LABEL_OFFSET);
79 /* adjust header */
80 hdr.rtm_flags |= RTF_MPLS | RTF_MPATH;
81 hdr.rtm_addrs |= RTA_DST;
82 hdr.rtm_msglen += sizeof(sa_label_in);
83 /* adjust iovec */
84 iov[iovcnt].iov_base = &sa_label_in;
85 iov[iovcnt++].iov_len = sizeof(sa_label_in);
86
87 /* nexthop */
88 memset(&nexthop, 0, sizeof(nexthop));
89 nexthop.sin_len = sizeof(nexthop);
90 nexthop.sin_family = AF_INET;
91 nexthop.sin_addr = nhlfe->nexthop->gate.ipv4;
92 /* adjust header */
93 hdr.rtm_flags |= RTF_GATEWAY;
94 hdr.rtm_addrs |= RTA_GATEWAY;
95 hdr.rtm_msglen += sizeof(nexthop);
96 /* adjust iovec */
97 iov[iovcnt].iov_base = &nexthop;
98 iov[iovcnt++].iov_len = sizeof(nexthop);
99
100 /* If action is RTM_DELETE we have to get rid of MPLS infos */
101 if (action != RTM_DELETE) {
102 memset(&sa_label_out, 0, sizeof(sa_label_out));
103 sa_label_out.smpls_len = sizeof(sa_label_out);
104 sa_label_out.smpls_family = AF_MPLS;
105 sa_label_out.smpls_label =
106 htonl(nhlfe->nexthop->nh_label->label[0]
107 << MPLS_LABEL_OFFSET);
108 /* adjust header */
109 hdr.rtm_addrs |= RTA_SRC;
110 hdr.rtm_flags |= RTF_MPLS;
111 hdr.rtm_msglen += sizeof(sa_label_out);
112 /* adjust iovec */
113 iov[iovcnt].iov_base = &sa_label_out;
114 iov[iovcnt++].iov_len = sizeof(sa_label_out);
115
116 if (nhlfe->nexthop->nh_label->label[0] == MPLS_LABEL_IMPLNULL)
117 hdr.rtm_mpls = MPLS_OP_POP;
118 else
119 hdr.rtm_mpls = MPLS_OP_SWAP;
120 }
121
0cf6db21 122 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
123 ret = writev(kr_state.fd, iov, iovcnt);
124 }
d62a17ae 125
126 if (ret == -1)
450971aa 127 flog_err_sys(EC_LIB_SOCKET, "%s: %s", __func__,
09c866e3 128 safe_strerror(errno));
d62a17ae 129
130 return ret;
e07486ce
RW
131}
132
133#if !defined(ROUNDUP)
d62a17ae 134#define ROUNDUP(a) \
135 (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a))
e07486ce
RW
136#endif
137
d62a17ae 138static int kernel_send_rtmsg_v6(int action, mpls_label_t in_label,
81793ac1 139 const zebra_nhlfe_t *nhlfe)
e07486ce 140{
d62a17ae 141 struct iovec iov[5];
142 struct rt_msghdr hdr;
143 struct sockaddr_mpls sa_label_in, sa_label_out;
144 struct pad {
145 struct sockaddr_in6 addr;
146 char pad[sizeof(long)]; /* thank you IPv6 */
147 } nexthop;
148 int iovcnt = 0;
149 int ret;
150
151 if (IS_ZEBRA_DEBUG_KERNEL)
152 zlog_debug("%s: 0x%x, label=%u", __func__, action, in_label);
153
154 /* initialize header */
155 memset(&hdr, 0, sizeof(hdr));
156 hdr.rtm_version = RTM_VERSION;
157
158 hdr.rtm_type = action;
159 hdr.rtm_flags = RTF_UP;
160 hdr.rtm_fmask = RTF_MPLS;
161 hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */
162 hdr.rtm_msglen = sizeof(hdr);
163 hdr.rtm_hdrlen = sizeof(struct rt_msghdr);
164 hdr.rtm_priority = 0;
165 /* adjust iovec */
166 iov[iovcnt].iov_base = &hdr;
167 iov[iovcnt++].iov_len = sizeof(hdr);
168
169 /* in label */
170 memset(&sa_label_in, 0, sizeof(sa_label_in));
171 sa_label_in.smpls_len = sizeof(sa_label_in);
172 sa_label_in.smpls_family = AF_MPLS;
173 sa_label_in.smpls_label = htonl(in_label << MPLS_LABEL_OFFSET);
174 /* adjust header */
175 hdr.rtm_flags |= RTF_MPLS | RTF_MPATH;
176 hdr.rtm_addrs |= RTA_DST;
177 hdr.rtm_msglen += sizeof(sa_label_in);
178 /* adjust iovec */
179 iov[iovcnt].iov_base = &sa_label_in;
180 iov[iovcnt++].iov_len = sizeof(sa_label_in);
181
182 /* nexthop */
183 memset(&nexthop, 0, sizeof(nexthop));
184 nexthop.addr.sin6_len = sizeof(struct sockaddr_in6);
185 nexthop.addr.sin6_family = AF_INET6;
186 nexthop.addr.sin6_addr = nhlfe->nexthop->gate.ipv6;
187 if (IN6_IS_ADDR_LINKLOCAL(&nexthop.addr.sin6_addr)) {
188 uint16_t tmp16;
189 struct sockaddr_in6 *sin6 = &nexthop.addr;
190
191 nexthop.addr.sin6_scope_id = nhlfe->nexthop->ifindex;
192
193 memcpy(&tmp16, &sin6->sin6_addr.s6_addr[2], sizeof(tmp16));
194 tmp16 = htons(sin6->sin6_scope_id);
195 memcpy(&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof(tmp16));
196 sin6->sin6_scope_id = 0;
197 }
198
199 /* adjust header */
200 hdr.rtm_flags |= RTF_GATEWAY;
201 hdr.rtm_addrs |= RTA_GATEWAY;
202 hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
203 /* adjust iovec */
204 iov[iovcnt].iov_base = &nexthop;
205 iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
206
207 /* If action is RTM_DELETE we have to get rid of MPLS infos */
208 if (action != RTM_DELETE) {
209 memset(&sa_label_out, 0, sizeof(sa_label_out));
210 sa_label_out.smpls_len = sizeof(sa_label_out);
211 sa_label_out.smpls_family = AF_MPLS;
212 sa_label_out.smpls_label =
213 htonl(nhlfe->nexthop->nh_label->label[0]
214 << MPLS_LABEL_OFFSET);
215 /* adjust header */
216 hdr.rtm_addrs |= RTA_SRC;
217 hdr.rtm_flags |= RTF_MPLS;
218 hdr.rtm_msglen += sizeof(sa_label_out);
219 /* adjust iovec */
220 iov[iovcnt].iov_base = &sa_label_out;
221 iov[iovcnt++].iov_len = sizeof(sa_label_out);
222
223 if (nhlfe->nexthop->nh_label->label[0] == MPLS_LABEL_IMPLNULL)
224 hdr.rtm_mpls = MPLS_OP_POP;
225 else
226 hdr.rtm_mpls = MPLS_OP_SWAP;
227 }
228
0cf6db21 229 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
230 ret = writev(kr_state.fd, iov, iovcnt);
231 }
d62a17ae 232
233 if (ret == -1)
450971aa 234 flog_err_sys(EC_LIB_SOCKET, "%s: %s", __func__,
09c866e3 235 safe_strerror(errno));
d62a17ae 236
237 return ret;
d3e2c74a
RW
238}
239
fc608372 240static int kernel_lsp_cmd(struct zebra_dplane_ctx *ctx)
d3e2c74a 241{
81793ac1 242 const zebra_nhlfe_t *nhlfe;
d62a17ae 243 struct nexthop *nexthop = NULL;
244 unsigned int nexthop_num = 0;
fc608372
MS
245 int action;
246
247 switch (dplane_ctx_get_op(ctx)) {
248 case DPLANE_OP_LSP_DELETE:
249 action = RTM_DELETE;
250 break;
251 case DPLANE_OP_LSP_INSTALL:
252 action = RTM_ADD;
253 break;
254 case DPLANE_OP_LSP_UPDATE:
255 action = RTM_CHANGE;
256 break;
257 default:
258 return -1;
259 }
d62a17ae 260
fc608372 261 for (nhlfe = dplane_ctx_get_nhlfe(ctx); nhlfe; nhlfe = nhlfe->next) {
d62a17ae 262 nexthop = nhlfe->nexthop;
263 if (!nexthop)
264 continue;
265
b3f2b590 266 if (nexthop_num >= zrouter.multipath_num)
d62a17ae 267 break;
268
269 if (((action == RTM_ADD || action == RTM_CHANGE)
270 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
271 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)))
272 || (action == RTM_DELETE
273 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
274 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)))) {
5e8c8947 275 if (nhlfe->nexthop->nh_label->num_labels > 1) {
e914ccbe 276 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
9df414fe
QY
277 "%s: can't push %u labels at once "
278 "(maximum is 1)",
279 __func__,
280 nhlfe->nexthop->nh_label->num_labels);
5e8c8947
RW
281 continue;
282 }
283
d62a17ae 284 nexthop_num++;
285
286 switch (NHLFE_FAMILY(nhlfe)) {
287 case AF_INET:
fc608372
MS
288 kernel_send_rtmsg_v4(
289 action,
290 dplane_ctx_get_in_label(ctx),
291 nhlfe);
d62a17ae 292 break;
293 case AF_INET6:
fc608372
MS
294 kernel_send_rtmsg_v6(
295 action,
296 dplane_ctx_get_in_label(ctx),
297 nhlfe);
d62a17ae 298 break;
299 default:
300 break;
301 }
d62a17ae 302 }
303 }
304
95f7965d 305 return 0;
d3e2c74a
RW
306}
307
fc608372 308enum zebra_dplane_result kernel_lsp_update(struct zebra_dplane_ctx *ctx)
d3e2c74a 309{
d62a17ae 310 int ret;
c4c8dec0 311
fc608372 312 ret = kernel_lsp_cmd(ctx);
c4c8dec0 313
fc608372
MS
314 return (ret == 0 ?
315 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
16c628de
MS
316}
317
97d8d05a 318static enum zebra_dplane_result kmpw_install(struct zebra_dplane_ctx *ctx)
d42127da
RW
319{
320 struct ifreq ifr;
321 struct ifmpwreq imr;
322 struct sockaddr_storage ss;
323 struct sockaddr_in *sa_in = (struct sockaddr_in *)&ss;
324 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&ss;
97d8d05a 325 const union g_addr *gaddr;
d42127da
RW
326
327 memset(&imr, 0, sizeof(imr));
97d8d05a 328 switch (dplane_ctx_get_pw_type(ctx)) {
d42127da
RW
329 case PW_TYPE_ETHERNET:
330 imr.imr_type = IMR_TYPE_ETHERNET;
331 break;
332 case PW_TYPE_ETHERNET_TAGGED:
333 imr.imr_type = IMR_TYPE_ETHERNET_TAGGED;
334 break;
335 default:
9df414fe 336 zlog_debug("%s: unhandled pseudowire type (%#X)", __func__,
97d8d05a
MS
337 dplane_ctx_get_pw_type(ctx));
338 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
339 }
340
97d8d05a 341 if (dplane_ctx_get_pw_flags(ctx) & F_PSEUDOWIRE_CWORD)
d42127da
RW
342 imr.imr_flags |= IMR_FLAG_CONTROLWORD;
343
344 /* pseudowire nexthop */
345 memset(&ss, 0, sizeof(ss));
16d69787 346 gaddr = dplane_ctx_get_pw_dest(ctx);
97d8d05a 347 switch (dplane_ctx_get_pw_af(ctx)) {
d42127da
RW
348 case AF_INET:
349 sa_in->sin_family = AF_INET;
350 sa_in->sin_len = sizeof(struct sockaddr_in);
97d8d05a 351 sa_in->sin_addr = gaddr->ipv4;
d42127da
RW
352 break;
353 case AF_INET6:
354 sa_in6->sin6_family = AF_INET6;
355 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
97d8d05a 356 sa_in6->sin6_addr = gaddr->ipv6;
d42127da
RW
357 break;
358 default:
9df414fe 359 zlog_debug("%s: unhandled pseudowire address-family (%u)",
97d8d05a
MS
360 __func__, dplane_ctx_get_pw_af(ctx));
361 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
362 }
363 memcpy(&imr.imr_nexthop, (struct sockaddr *)&ss,
364 sizeof(imr.imr_nexthop));
365
366 /* pseudowire local/remote labels */
97d8d05a
MS
367 imr.imr_lshim.shim_label = dplane_ctx_get_pw_local_label(ctx);
368 imr.imr_rshim.shim_label = dplane_ctx_get_pw_remote_label(ctx);
d42127da
RW
369
370 /* ioctl */
371 memset(&ifr, 0, sizeof(ifr));
7c7ef4a8 372 strlcpy(ifr.ifr_name, dplane_ctx_get_ifname(ctx),
97d8d05a 373 sizeof(ifr.ifr_name));
d42127da
RW
374 ifr.ifr_data = (caddr_t)&imr;
375 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
450971aa 376 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
09c866e3 377 safe_strerror(errno));
97d8d05a 378 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
379 }
380
97d8d05a 381 return ZEBRA_DPLANE_REQUEST_SUCCESS;
d42127da
RW
382}
383
97d8d05a 384static enum zebra_dplane_result kmpw_uninstall(struct zebra_dplane_ctx *ctx)
d42127da
RW
385{
386 struct ifreq ifr;
387 struct ifmpwreq imr;
388
389 memset(&ifr, 0, sizeof(ifr));
390 memset(&imr, 0, sizeof(imr));
7c7ef4a8 391 strlcpy(ifr.ifr_name, dplane_ctx_get_ifname(ctx),
97d8d05a 392 sizeof(ifr.ifr_name));
d42127da
RW
393 ifr.ifr_data = (caddr_t)&imr;
394 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
450971aa 395 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
09c866e3 396 safe_strerror(errno));
97d8d05a 397 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
398 }
399
97d8d05a
MS
400 return ZEBRA_DPLANE_REQUEST_SUCCESS;
401}
402
403/*
404 * Pseudowire update api for openbsd.
405 */
406enum zebra_dplane_result kernel_pw_update(struct zebra_dplane_ctx *ctx)
407{
408 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
409
410 switch (dplane_ctx_get_op(ctx)) {
411 case DPLANE_OP_PW_INSTALL:
412 result = kmpw_install(ctx);
413 break;
414 case DPLANE_OP_PW_UNINSTALL:
415 result = kmpw_uninstall(ctx);
416 break;
417 default:
418 break;
5b94ec50 419 }
97d8d05a
MS
420
421 return result;
d42127da
RW
422}
423
d3e2c74a 424#define MAX_RTSOCK_BUF 128 * 1024
d62a17ae 425int mpls_kernel_init(void)
d3e2c74a 426{
d62a17ae 427 int rcvbuf, default_rcvbuf;
428 socklen_t optlen;
429
430 if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
450971aa 431 flog_err_sys(EC_LIB_SOCKET, "%s: socket", __func__);
d62a17ae 432 return -1;
433 }
434
d42127da
RW
435 if ((kr_state.ioctl_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0))
436 == -1) {
450971aa 437 flog_err_sys(EC_LIB_SOCKET, "%s: ioctl socket", __func__);
d42127da
RW
438 return -1;
439 }
440
d62a17ae 441 /* grow receive buffer, don't wanna miss messages */
442 optlen = sizeof(default_rcvbuf);
443 if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf,
444 &optlen)
445 == -1)
450971aa 446 flog_err_sys(EC_LIB_SOCKET,
9df414fe 447 "kr_init getsockopt SOL_SOCKET SO_RCVBUF");
d62a17ae 448 else
449 for (rcvbuf = MAX_RTSOCK_BUF;
450 rcvbuf > default_rcvbuf
451 && setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
452 sizeof(rcvbuf))
453 == -1
454 && errno == ENOBUFS;
455 rcvbuf /= 2)
456 ; /* nothing */
457
458 kr_state.rtseq = 1;
459
460 return 0;
d3e2c74a 461}
ddfeb486
DL
462
463#endif /* OPEN_BSD */