]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_mpls_openbsd.c
isisd: implement the 'lsp-too-large' notification
[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"
d3e2c74a
RW
30
31#include "privs.h"
32#include "prefix.h"
33#include "interface.h"
34#include "log.h"
174482ef 35#include "lib_errors.h"
d3e2c74a
RW
36
37extern struct zebra_privs_t zserv_privs;
38
39struct {
d7c0a89a 40 uint32_t rtseq;
d62a17ae 41 int fd;
d42127da 42 int ioctl_fd;
d3e2c74a
RW
43} kr_state;
44
d62a17ae 45static int kernel_send_rtmsg_v4(int action, mpls_label_t in_label,
46 zebra_nhlfe_t *nhlfe)
d3e2c74a 47{
d62a17ae 48 struct iovec iov[5];
49 struct rt_msghdr hdr;
50 struct sockaddr_mpls sa_label_in, sa_label_out;
51 struct sockaddr_in nexthop;
52 int iovcnt = 0;
53 int ret;
54
55 if (IS_ZEBRA_DEBUG_KERNEL)
56 zlog_debug("%s: 0x%x, label=%u", __func__, action, in_label);
57
58 /* initialize header */
59 memset(&hdr, 0, sizeof(hdr));
60 hdr.rtm_version = RTM_VERSION;
61
62 hdr.rtm_type = action;
63 hdr.rtm_flags = RTF_UP;
64 hdr.rtm_fmask = RTF_MPLS;
65 hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */
66 hdr.rtm_msglen = sizeof(hdr);
67 hdr.rtm_hdrlen = sizeof(struct rt_msghdr);
68 hdr.rtm_priority = 0;
69 /* adjust iovec */
70 iov[iovcnt].iov_base = &hdr;
71 iov[iovcnt++].iov_len = sizeof(hdr);
72
73 /* in label */
74 memset(&sa_label_in, 0, sizeof(sa_label_in));
75 sa_label_in.smpls_len = sizeof(sa_label_in);
76 sa_label_in.smpls_family = AF_MPLS;
77 sa_label_in.smpls_label = htonl(in_label << MPLS_LABEL_OFFSET);
78 /* adjust header */
79 hdr.rtm_flags |= RTF_MPLS | RTF_MPATH;
80 hdr.rtm_addrs |= RTA_DST;
81 hdr.rtm_msglen += sizeof(sa_label_in);
82 /* adjust iovec */
83 iov[iovcnt].iov_base = &sa_label_in;
84 iov[iovcnt++].iov_len = sizeof(sa_label_in);
85
86 /* nexthop */
87 memset(&nexthop, 0, sizeof(nexthop));
88 nexthop.sin_len = sizeof(nexthop);
89 nexthop.sin_family = AF_INET;
90 nexthop.sin_addr = nhlfe->nexthop->gate.ipv4;
91 /* adjust header */
92 hdr.rtm_flags |= RTF_GATEWAY;
93 hdr.rtm_addrs |= RTA_GATEWAY;
94 hdr.rtm_msglen += sizeof(nexthop);
95 /* adjust iovec */
96 iov[iovcnt].iov_base = &nexthop;
97 iov[iovcnt++].iov_len = sizeof(nexthop);
98
99 /* If action is RTM_DELETE we have to get rid of MPLS infos */
100 if (action != RTM_DELETE) {
101 memset(&sa_label_out, 0, sizeof(sa_label_out));
102 sa_label_out.smpls_len = sizeof(sa_label_out);
103 sa_label_out.smpls_family = AF_MPLS;
104 sa_label_out.smpls_label =
105 htonl(nhlfe->nexthop->nh_label->label[0]
106 << MPLS_LABEL_OFFSET);
107 /* adjust header */
108 hdr.rtm_addrs |= RTA_SRC;
109 hdr.rtm_flags |= RTF_MPLS;
110 hdr.rtm_msglen += sizeof(sa_label_out);
111 /* adjust iovec */
112 iov[iovcnt].iov_base = &sa_label_out;
113 iov[iovcnt++].iov_len = sizeof(sa_label_out);
114
115 if (nhlfe->nexthop->nh_label->label[0] == MPLS_LABEL_IMPLNULL)
116 hdr.rtm_mpls = MPLS_OP_POP;
117 else
118 hdr.rtm_mpls = MPLS_OP_SWAP;
119 }
120
01b9e3fd
DL
121 frr_elevate_privs(&zserv_privs) {
122 ret = writev(kr_state.fd, iov, iovcnt);
123 }
d62a17ae 124
125 if (ret == -1)
450971aa 126 flog_err_sys(EC_LIB_SOCKET, "%s: %s", __func__,
09c866e3 127 safe_strerror(errno));
d62a17ae 128
129 return ret;
e07486ce
RW
130}
131
132#if !defined(ROUNDUP)
d62a17ae 133#define ROUNDUP(a) \
134 (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a))
e07486ce
RW
135#endif
136
d62a17ae 137static int kernel_send_rtmsg_v6(int action, mpls_label_t in_label,
138 zebra_nhlfe_t *nhlfe)
e07486ce 139{
d62a17ae 140 struct iovec iov[5];
141 struct rt_msghdr hdr;
142 struct sockaddr_mpls sa_label_in, sa_label_out;
143 struct pad {
144 struct sockaddr_in6 addr;
145 char pad[sizeof(long)]; /* thank you IPv6 */
146 } nexthop;
147 int iovcnt = 0;
148 int ret;
149
150 if (IS_ZEBRA_DEBUG_KERNEL)
151 zlog_debug("%s: 0x%x, label=%u", __func__, action, in_label);
152
153 /* initialize header */
154 memset(&hdr, 0, sizeof(hdr));
155 hdr.rtm_version = RTM_VERSION;
156
157 hdr.rtm_type = action;
158 hdr.rtm_flags = RTF_UP;
159 hdr.rtm_fmask = RTF_MPLS;
160 hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */
161 hdr.rtm_msglen = sizeof(hdr);
162 hdr.rtm_hdrlen = sizeof(struct rt_msghdr);
163 hdr.rtm_priority = 0;
164 /* adjust iovec */
165 iov[iovcnt].iov_base = &hdr;
166 iov[iovcnt++].iov_len = sizeof(hdr);
167
168 /* in label */
169 memset(&sa_label_in, 0, sizeof(sa_label_in));
170 sa_label_in.smpls_len = sizeof(sa_label_in);
171 sa_label_in.smpls_family = AF_MPLS;
172 sa_label_in.smpls_label = htonl(in_label << MPLS_LABEL_OFFSET);
173 /* adjust header */
174 hdr.rtm_flags |= RTF_MPLS | RTF_MPATH;
175 hdr.rtm_addrs |= RTA_DST;
176 hdr.rtm_msglen += sizeof(sa_label_in);
177 /* adjust iovec */
178 iov[iovcnt].iov_base = &sa_label_in;
179 iov[iovcnt++].iov_len = sizeof(sa_label_in);
180
181 /* nexthop */
182 memset(&nexthop, 0, sizeof(nexthop));
183 nexthop.addr.sin6_len = sizeof(struct sockaddr_in6);
184 nexthop.addr.sin6_family = AF_INET6;
185 nexthop.addr.sin6_addr = nhlfe->nexthop->gate.ipv6;
186 if (IN6_IS_ADDR_LINKLOCAL(&nexthop.addr.sin6_addr)) {
187 uint16_t tmp16;
188 struct sockaddr_in6 *sin6 = &nexthop.addr;
189
190 nexthop.addr.sin6_scope_id = nhlfe->nexthop->ifindex;
191
192 memcpy(&tmp16, &sin6->sin6_addr.s6_addr[2], sizeof(tmp16));
193 tmp16 = htons(sin6->sin6_scope_id);
194 memcpy(&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof(tmp16));
195 sin6->sin6_scope_id = 0;
196 }
197
198 /* adjust header */
199 hdr.rtm_flags |= RTF_GATEWAY;
200 hdr.rtm_addrs |= RTA_GATEWAY;
201 hdr.rtm_msglen += ROUNDUP(sizeof(struct sockaddr_in6));
202 /* adjust iovec */
203 iov[iovcnt].iov_base = &nexthop;
204 iov[iovcnt++].iov_len = ROUNDUP(sizeof(struct sockaddr_in6));
205
206 /* If action is RTM_DELETE we have to get rid of MPLS infos */
207 if (action != RTM_DELETE) {
208 memset(&sa_label_out, 0, sizeof(sa_label_out));
209 sa_label_out.smpls_len = sizeof(sa_label_out);
210 sa_label_out.smpls_family = AF_MPLS;
211 sa_label_out.smpls_label =
212 htonl(nhlfe->nexthop->nh_label->label[0]
213 << MPLS_LABEL_OFFSET);
214 /* adjust header */
215 hdr.rtm_addrs |= RTA_SRC;
216 hdr.rtm_flags |= RTF_MPLS;
217 hdr.rtm_msglen += sizeof(sa_label_out);
218 /* adjust iovec */
219 iov[iovcnt].iov_base = &sa_label_out;
220 iov[iovcnt++].iov_len = sizeof(sa_label_out);
221
222 if (nhlfe->nexthop->nh_label->label[0] == MPLS_LABEL_IMPLNULL)
223 hdr.rtm_mpls = MPLS_OP_POP;
224 else
225 hdr.rtm_mpls = MPLS_OP_SWAP;
226 }
227
01b9e3fd
DL
228 frr_elevate_privs(&zserv_privs) {
229 ret = writev(kr_state.fd, iov, iovcnt);
230 }
d62a17ae 231
232 if (ret == -1)
450971aa 233 flog_err_sys(EC_LIB_SOCKET, "%s: %s", __func__,
09c866e3 234 safe_strerror(errno));
d62a17ae 235
236 return ret;
d3e2c74a
RW
237}
238
d62a17ae 239static int kernel_lsp_cmd(int action, zebra_lsp_t *lsp)
d3e2c74a 240{
d62a17ae 241 zebra_nhlfe_t *nhlfe;
242 struct nexthop *nexthop = NULL;
243 unsigned int nexthop_num = 0;
244
245 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
246 nexthop = nhlfe->nexthop;
247 if (!nexthop)
248 continue;
249
250 if (nexthop_num >= multipath_num)
251 break;
252
253 if (((action == RTM_ADD || action == RTM_CHANGE)
254 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
255 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)))
256 || (action == RTM_DELETE
257 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
258 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)))) {
5e8c8947 259 if (nhlfe->nexthop->nh_label->num_labels > 1) {
e914ccbe 260 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
9df414fe
QY
261 "%s: can't push %u labels at once "
262 "(maximum is 1)",
263 __func__,
264 nhlfe->nexthop->nh_label->num_labels);
5e8c8947
RW
265 continue;
266 }
267
d62a17ae 268 nexthop_num++;
269
270 switch (NHLFE_FAMILY(nhlfe)) {
271 case AF_INET:
272 kernel_send_rtmsg_v4(action, lsp->ile.in_label,
273 nhlfe);
274 break;
275 case AF_INET6:
276 kernel_send_rtmsg_v6(action, lsp->ile.in_label,
277 nhlfe);
278 break;
279 default:
280 break;
281 }
d62a17ae 282 }
283 }
284
285 return (0);
d3e2c74a
RW
286}
287
ea1c14f6 288enum zebra_dplane_result kernel_add_lsp(zebra_lsp_t *lsp)
d3e2c74a 289{
d62a17ae 290 int ret;
c4c8dec0 291
4a83e7a0 292 if (!lsp || !lsp->best_nhlfe) { // unexpected
ea1c14f6
MS
293 kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_INSTALL_FAILURE);
294 return ZEBRA_DPLANE_REQUEST_FAILURE;
4a83e7a0 295 }
d3e2c74a 296
d62a17ae 297 ret = kernel_lsp_cmd(RTM_ADD, lsp);
c4c8dec0 298
4a83e7a0 299 kernel_lsp_pass_fail(lsp,
ea1c14f6
MS
300 (!ret) ? ZEBRA_DPLANE_INSTALL_SUCCESS
301 : ZEBRA_DPLANE_INSTALL_FAILURE);
7c5d0e18 302
ea1c14f6 303 return ZEBRA_DPLANE_REQUEST_SUCCESS;
d3e2c74a
RW
304}
305
ea1c14f6 306enum zebra_dplane_result kernel_upd_lsp(zebra_lsp_t *lsp)
d3e2c74a 307{
d62a17ae 308 int ret;
c4c8dec0 309
4a83e7a0 310 if (!lsp || !lsp->best_nhlfe) { // unexpected
ea1c14f6
MS
311 kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_INSTALL_FAILURE);
312 return ZEBRA_DPLANE_REQUEST_FAILURE;
4a83e7a0 313 }
d3e2c74a 314
d62a17ae 315 ret = kernel_lsp_cmd(RTM_CHANGE, lsp);
c4c8dec0 316
4a83e7a0 317 kernel_lsp_pass_fail(lsp,
ea1c14f6
MS
318 (!ret) ? ZEBRA_DPLANE_INSTALL_SUCCESS
319 : ZEBRA_DPLANE_INSTALL_FAILURE);
320 return ZEBRA_DPLANE_REQUEST_SUCCESS;
d3e2c74a
RW
321}
322
ea1c14f6 323enum zebra_dplane_result kernel_del_lsp(zebra_lsp_t *lsp)
d3e2c74a 324{
d62a17ae 325 int ret;
c4c8dec0 326
4a83e7a0 327 if (!lsp) { // unexpected
ea1c14f6
MS
328 kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_DELETE_FAILURE);
329 return ZEBRA_DPLANE_REQUEST_FAILURE;
4a83e7a0 330 }
d3e2c74a 331
4a83e7a0 332 if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
ea1c14f6
MS
333 kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_DELETE_FAILURE);
334 return ZEBRA_DPLANE_REQUEST_FAILURE;
4a83e7a0 335 }
c4c8dec0 336
d62a17ae 337 ret = kernel_lsp_cmd(RTM_DELETE, lsp);
c4c8dec0 338
4a83e7a0 339 kernel_lsp_pass_fail(lsp,
ea1c14f6
MS
340 (!ret) ? ZEBRA_DPLANE_DELETE_SUCCESS
341 : ZEBRA_DPLANE_DELETE_FAILURE);
7c5d0e18 342
ea1c14f6 343 return ZEBRA_DPLANE_REQUEST_SUCCESS;
d3e2c74a
RW
344}
345
d42127da
RW
346static int kmpw_install(struct zebra_pw *pw)
347{
348 struct ifreq ifr;
349 struct ifmpwreq imr;
350 struct sockaddr_storage ss;
351 struct sockaddr_in *sa_in = (struct sockaddr_in *)&ss;
352 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&ss;
353
354 memset(&imr, 0, sizeof(imr));
355 switch (pw->type) {
356 case PW_TYPE_ETHERNET:
357 imr.imr_type = IMR_TYPE_ETHERNET;
358 break;
359 case PW_TYPE_ETHERNET_TAGGED:
360 imr.imr_type = IMR_TYPE_ETHERNET_TAGGED;
361 break;
362 default:
9df414fe
QY
363 zlog_debug("%s: unhandled pseudowire type (%#X)", __func__,
364 pw->type);
d42127da
RW
365 return -1;
366 }
367
368 if (pw->flags & F_PSEUDOWIRE_CWORD)
369 imr.imr_flags |= IMR_FLAG_CONTROLWORD;
370
371 /* pseudowire nexthop */
372 memset(&ss, 0, sizeof(ss));
373 switch (pw->af) {
374 case AF_INET:
375 sa_in->sin_family = AF_INET;
376 sa_in->sin_len = sizeof(struct sockaddr_in);
377 sa_in->sin_addr = pw->nexthop.ipv4;
378 break;
379 case AF_INET6:
380 sa_in6->sin6_family = AF_INET6;
381 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
382 sa_in6->sin6_addr = pw->nexthop.ipv6;
383 break;
384 default:
9df414fe
QY
385 zlog_debug("%s: unhandled pseudowire address-family (%u)",
386 __func__, pw->af);
d42127da
RW
387 return -1;
388 }
389 memcpy(&imr.imr_nexthop, (struct sockaddr *)&ss,
390 sizeof(imr.imr_nexthop));
391
392 /* pseudowire local/remote labels */
393 imr.imr_lshim.shim_label = pw->local_label;
394 imr.imr_rshim.shim_label = pw->remote_label;
395
396 /* ioctl */
397 memset(&ifr, 0, sizeof(ifr));
398 strlcpy(ifr.ifr_name, pw->ifname, sizeof(ifr.ifr_name));
399 ifr.ifr_data = (caddr_t)&imr;
400 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
450971aa 401 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
09c866e3 402 safe_strerror(errno));
d42127da
RW
403 return -1;
404 }
405
406 return 0;
407}
408
409static int kmpw_uninstall(struct zebra_pw *pw)
410{
411 struct ifreq ifr;
412 struct ifmpwreq imr;
413
414 memset(&ifr, 0, sizeof(ifr));
415 memset(&imr, 0, sizeof(imr));
416 strlcpy(ifr.ifr_name, pw->ifname, sizeof(ifr.ifr_name));
417 ifr.ifr_data = (caddr_t)&imr;
418 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
450971aa 419 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
09c866e3 420 safe_strerror(errno));
d42127da
RW
421 return -1;
422 }
423
424 return 0;
425}
426
d3e2c74a 427#define MAX_RTSOCK_BUF 128 * 1024
d62a17ae 428int mpls_kernel_init(void)
d3e2c74a 429{
d62a17ae 430 int rcvbuf, default_rcvbuf;
431 socklen_t optlen;
432
433 if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
450971aa 434 flog_err_sys(EC_LIB_SOCKET, "%s: socket", __func__);
d62a17ae 435 return -1;
436 }
437
d42127da
RW
438 if ((kr_state.ioctl_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0))
439 == -1) {
450971aa 440 flog_err_sys(EC_LIB_SOCKET, "%s: ioctl socket", __func__);
d42127da
RW
441 return -1;
442 }
443
d62a17ae 444 /* grow receive buffer, don't wanna miss messages */
445 optlen = sizeof(default_rcvbuf);
446 if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf,
447 &optlen)
448 == -1)
450971aa 449 flog_err_sys(EC_LIB_SOCKET,
9df414fe 450 "kr_init getsockopt SOL_SOCKET SO_RCVBUF");
d62a17ae 451 else
452 for (rcvbuf = MAX_RTSOCK_BUF;
453 rcvbuf > default_rcvbuf
454 && setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
455 sizeof(rcvbuf))
456 == -1
457 && errno == ENOBUFS;
458 rcvbuf /= 2)
459 ; /* nothing */
460
461 kr_state.rtseq = 1;
462
d42127da
RW
463 /* register hook to install/uninstall pseudowires */
464 hook_register(pw_install, kmpw_install);
465 hook_register(pw_uninstall, kmpw_uninstall);
466
d62a17ae 467 return 0;
d3e2c74a 468}
ddfeb486
DL
469
470#endif /* OPEN_BSD */