]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_mpls_openbsd.c
bgpd: Validate large-community-list against UINT_MAX
[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,
81793ac1 46 const 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,
81793ac1 138 const 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
fc608372 239static int kernel_lsp_cmd(struct zebra_dplane_ctx *ctx)
d3e2c74a 240{
81793ac1 241 const zebra_nhlfe_t *nhlfe;
d62a17ae 242 struct nexthop *nexthop = NULL;
243 unsigned int nexthop_num = 0;
fc608372
MS
244 int action;
245
246 switch (dplane_ctx_get_op(ctx)) {
247 case DPLANE_OP_LSP_DELETE:
248 action = RTM_DELETE;
249 break;
250 case DPLANE_OP_LSP_INSTALL:
251 action = RTM_ADD;
252 break;
253 case DPLANE_OP_LSP_UPDATE:
254 action = RTM_CHANGE;
255 break;
256 default:
257 return -1;
258 }
d62a17ae 259
fc608372 260 for (nhlfe = dplane_ctx_get_nhlfe(ctx); nhlfe; nhlfe = nhlfe->next) {
d62a17ae 261 nexthop = nhlfe->nexthop;
262 if (!nexthop)
263 continue;
264
265 if (nexthop_num >= multipath_num)
266 break;
267
268 if (((action == RTM_ADD || action == RTM_CHANGE)
269 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
270 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)))
271 || (action == RTM_DELETE
272 && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)
273 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)))) {
5e8c8947 274 if (nhlfe->nexthop->nh_label->num_labels > 1) {
e914ccbe 275 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
9df414fe
QY
276 "%s: can't push %u labels at once "
277 "(maximum is 1)",
278 __func__,
279 nhlfe->nexthop->nh_label->num_labels);
5e8c8947
RW
280 continue;
281 }
282
d62a17ae 283 nexthop_num++;
284
285 switch (NHLFE_FAMILY(nhlfe)) {
286 case AF_INET:
fc608372
MS
287 kernel_send_rtmsg_v4(
288 action,
289 dplane_ctx_get_in_label(ctx),
290 nhlfe);
d62a17ae 291 break;
292 case AF_INET6:
fc608372
MS
293 kernel_send_rtmsg_v6(
294 action,
295 dplane_ctx_get_in_label(ctx),
296 nhlfe);
d62a17ae 297 break;
298 default:
299 break;
300 }
d62a17ae 301 }
302 }
303
304 return (0);
d3e2c74a
RW
305}
306
fc608372 307enum zebra_dplane_result kernel_lsp_update(struct zebra_dplane_ctx *ctx)
d3e2c74a 308{
d62a17ae 309 int ret;
c4c8dec0 310
fc608372 311 ret = kernel_lsp_cmd(ctx);
c4c8dec0 312
fc608372
MS
313 return (ret == 0 ?
314 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
16c628de
MS
315}
316
97d8d05a 317static enum zebra_dplane_result kmpw_install(struct zebra_dplane_ctx *ctx)
d42127da
RW
318{
319 struct ifreq ifr;
320 struct ifmpwreq imr;
321 struct sockaddr_storage ss;
322 struct sockaddr_in *sa_in = (struct sockaddr_in *)&ss;
323 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&ss;
97d8d05a 324 const union g_addr *gaddr;
d42127da
RW
325
326 memset(&imr, 0, sizeof(imr));
97d8d05a 327 switch (dplane_ctx_get_pw_type(ctx)) {
d42127da
RW
328 case PW_TYPE_ETHERNET:
329 imr.imr_type = IMR_TYPE_ETHERNET;
330 break;
331 case PW_TYPE_ETHERNET_TAGGED:
332 imr.imr_type = IMR_TYPE_ETHERNET_TAGGED;
333 break;
334 default:
9df414fe 335 zlog_debug("%s: unhandled pseudowire type (%#X)", __func__,
97d8d05a
MS
336 dplane_ctx_get_pw_type(ctx));
337 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
338 }
339
97d8d05a 340 if (dplane_ctx_get_pw_flags(ctx) & F_PSEUDOWIRE_CWORD)
d42127da
RW
341 imr.imr_flags |= IMR_FLAG_CONTROLWORD;
342
343 /* pseudowire nexthop */
344 memset(&ss, 0, sizeof(ss));
16d69787 345 gaddr = dplane_ctx_get_pw_dest(ctx);
97d8d05a 346 switch (dplane_ctx_get_pw_af(ctx)) {
d42127da
RW
347 case AF_INET:
348 sa_in->sin_family = AF_INET;
349 sa_in->sin_len = sizeof(struct sockaddr_in);
97d8d05a 350 sa_in->sin_addr = gaddr->ipv4;
d42127da
RW
351 break;
352 case AF_INET6:
353 sa_in6->sin6_family = AF_INET6;
354 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
97d8d05a 355 sa_in6->sin6_addr = gaddr->ipv6;
d42127da
RW
356 break;
357 default:
9df414fe 358 zlog_debug("%s: unhandled pseudowire address-family (%u)",
97d8d05a
MS
359 __func__, dplane_ctx_get_pw_af(ctx));
360 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
361 }
362 memcpy(&imr.imr_nexthop, (struct sockaddr *)&ss,
363 sizeof(imr.imr_nexthop));
364
365 /* pseudowire local/remote labels */
97d8d05a
MS
366 imr.imr_lshim.shim_label = dplane_ctx_get_pw_local_label(ctx);
367 imr.imr_rshim.shim_label = dplane_ctx_get_pw_remote_label(ctx);
d42127da
RW
368
369 /* ioctl */
370 memset(&ifr, 0, sizeof(ifr));
97d8d05a
MS
371 strlcpy(ifr.ifr_name, dplane_ctx_get_pw_ifname(ctx),
372 sizeof(ifr.ifr_name));
d42127da
RW
373 ifr.ifr_data = (caddr_t)&imr;
374 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
450971aa 375 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
09c866e3 376 safe_strerror(errno));
97d8d05a 377 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
378 }
379
97d8d05a 380 return ZEBRA_DPLANE_REQUEST_SUCCESS;
d42127da
RW
381}
382
97d8d05a 383static enum zebra_dplane_result kmpw_uninstall(struct zebra_dplane_ctx *ctx)
d42127da
RW
384{
385 struct ifreq ifr;
386 struct ifmpwreq imr;
387
388 memset(&ifr, 0, sizeof(ifr));
389 memset(&imr, 0, sizeof(imr));
97d8d05a
MS
390 strlcpy(ifr.ifr_name, dplane_ctx_get_pw_ifname(ctx),
391 sizeof(ifr.ifr_name));
d42127da
RW
392 ifr.ifr_data = (caddr_t)&imr;
393 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
450971aa 394 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
09c866e3 395 safe_strerror(errno));
97d8d05a 396 return ZEBRA_DPLANE_REQUEST_FAILURE;
d42127da
RW
397 }
398
97d8d05a
MS
399 return ZEBRA_DPLANE_REQUEST_SUCCESS;
400}
401
402/*
403 * Pseudowire update api for openbsd.
404 */
405enum zebra_dplane_result kernel_pw_update(struct zebra_dplane_ctx *ctx)
406{
407 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
408
409 switch (dplane_ctx_get_op(ctx)) {
410 case DPLANE_OP_PW_INSTALL:
411 result = kmpw_install(ctx);
412 break;
413 case DPLANE_OP_PW_UNINSTALL:
414 result = kmpw_uninstall(ctx);
415 break;
416 default:
417 break;
5b94ec50 418 }
97d8d05a
MS
419
420 return result;
d42127da
RW
421}
422
d3e2c74a 423#define MAX_RTSOCK_BUF 128 * 1024
d62a17ae 424int mpls_kernel_init(void)
d3e2c74a 425{
d62a17ae 426 int rcvbuf, default_rcvbuf;
427 socklen_t optlen;
428
429 if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
450971aa 430 flog_err_sys(EC_LIB_SOCKET, "%s: socket", __func__);
d62a17ae 431 return -1;
432 }
433
d42127da
RW
434 if ((kr_state.ioctl_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0))
435 == -1) {
450971aa 436 flog_err_sys(EC_LIB_SOCKET, "%s: ioctl socket", __func__);
d42127da
RW
437 return -1;
438 }
439
d62a17ae 440 /* grow receive buffer, don't wanna miss messages */
441 optlen = sizeof(default_rcvbuf);
442 if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf,
443 &optlen)
444 == -1)
450971aa 445 flog_err_sys(EC_LIB_SOCKET,
9df414fe 446 "kr_init getsockopt SOL_SOCKET SO_RCVBUF");
d62a17ae 447 else
448 for (rcvbuf = MAX_RTSOCK_BUF;
449 rcvbuf > default_rcvbuf
450 && setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
451 sizeof(rcvbuf))
452 == -1
453 && errno == ENOBUFS;
454 rcvbuf /= 2)
455 ; /* nothing */
456
457 kr_state.rtseq = 1;
458
459 return 0;
d3e2c74a 460}
ddfeb486
DL
461
462#endif /* OPEN_BSD */