]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mpls_openbsd.c
Merge pull request #3288 from nitinsoniism/show_intf_brief
[mirror_frr.git] / zebra / zebra_mpls_openbsd.c
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #ifdef OPEN_BSD
24
25 #include <netmpls/mpls.h>
26 #include "zebra/rt.h"
27 #include "zebra/zebra_mpls.h"
28 #include "zebra/debug.h"
29 #include "zebra/zebra_errors.h"
30
31 #include "privs.h"
32 #include "prefix.h"
33 #include "interface.h"
34 #include "log.h"
35 #include "lib_errors.h"
36
37 extern struct zebra_privs_t zserv_privs;
38
39 struct {
40 uint32_t rtseq;
41 int fd;
42 int ioctl_fd;
43 } kr_state;
44
45 static int kernel_send_rtmsg_v4(int action, mpls_label_t in_label,
46 zebra_nhlfe_t *nhlfe)
47 {
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
121 frr_elevate_privs(&zserv_privs) {
122 ret = writev(kr_state.fd, iov, iovcnt);
123 }
124
125 if (ret == -1)
126 flog_err_sys(EC_LIB_SOCKET, "%s: %s", __func__,
127 safe_strerror(errno));
128
129 return ret;
130 }
131
132 #if !defined(ROUNDUP)
133 #define ROUNDUP(a) \
134 (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a))
135 #endif
136
137 static int kernel_send_rtmsg_v6(int action, mpls_label_t in_label,
138 zebra_nhlfe_t *nhlfe)
139 {
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
228 frr_elevate_privs(&zserv_privs) {
229 ret = writev(kr_state.fd, iov, iovcnt);
230 }
231
232 if (ret == -1)
233 flog_err_sys(EC_LIB_SOCKET, "%s: %s", __func__,
234 safe_strerror(errno));
235
236 return ret;
237 }
238
239 static int kernel_lsp_cmd(struct zebra_dplane_ctx *ctx)
240 {
241 zebra_nhlfe_t *nhlfe;
242 struct nexthop *nexthop = NULL;
243 unsigned int nexthop_num = 0;
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 }
259
260 for (nhlfe = dplane_ctx_get_nhlfe(ctx); nhlfe; nhlfe = nhlfe->next) {
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)))) {
274 if (nhlfe->nexthop->nh_label->num_labels > 1) {
275 flog_warn(EC_ZEBRA_MAX_LABELS_PUSH,
276 "%s: can't push %u labels at once "
277 "(maximum is 1)",
278 __func__,
279 nhlfe->nexthop->nh_label->num_labels);
280 continue;
281 }
282
283 nexthop_num++;
284
285 switch (NHLFE_FAMILY(nhlfe)) {
286 case AF_INET:
287 kernel_send_rtmsg_v4(
288 action,
289 dplane_ctx_get_in_label(ctx),
290 nhlfe);
291 break;
292 case AF_INET6:
293 kernel_send_rtmsg_v6(
294 action,
295 dplane_ctx_get_in_label(ctx),
296 nhlfe);
297 break;
298 default:
299 break;
300 }
301 }
302 }
303
304 return (0);
305 }
306
307 enum zebra_dplane_result kernel_lsp_update(struct zebra_dplane_ctx *ctx)
308 {
309 int ret;
310
311 ret = kernel_lsp_cmd(ctx);
312
313 return (ret == 0 ?
314 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
315 }
316
317 static int kmpw_install(struct zebra_pw *pw)
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;
324
325 memset(&imr, 0, sizeof(imr));
326 switch (pw->type) {
327 case PW_TYPE_ETHERNET:
328 imr.imr_type = IMR_TYPE_ETHERNET;
329 break;
330 case PW_TYPE_ETHERNET_TAGGED:
331 imr.imr_type = IMR_TYPE_ETHERNET_TAGGED;
332 break;
333 default:
334 zlog_debug("%s: unhandled pseudowire type (%#X)", __func__,
335 pw->type);
336 return -1;
337 }
338
339 if (pw->flags & F_PSEUDOWIRE_CWORD)
340 imr.imr_flags |= IMR_FLAG_CONTROLWORD;
341
342 /* pseudowire nexthop */
343 memset(&ss, 0, sizeof(ss));
344 switch (pw->af) {
345 case AF_INET:
346 sa_in->sin_family = AF_INET;
347 sa_in->sin_len = sizeof(struct sockaddr_in);
348 sa_in->sin_addr = pw->nexthop.ipv4;
349 break;
350 case AF_INET6:
351 sa_in6->sin6_family = AF_INET6;
352 sa_in6->sin6_len = sizeof(struct sockaddr_in6);
353 sa_in6->sin6_addr = pw->nexthop.ipv6;
354 break;
355 default:
356 zlog_debug("%s: unhandled pseudowire address-family (%u)",
357 __func__, pw->af);
358 return -1;
359 }
360 memcpy(&imr.imr_nexthop, (struct sockaddr *)&ss,
361 sizeof(imr.imr_nexthop));
362
363 /* pseudowire local/remote labels */
364 imr.imr_lshim.shim_label = pw->local_label;
365 imr.imr_rshim.shim_label = pw->remote_label;
366
367 /* ioctl */
368 memset(&ifr, 0, sizeof(ifr));
369 strlcpy(ifr.ifr_name, pw->ifname, sizeof(ifr.ifr_name));
370 ifr.ifr_data = (caddr_t)&imr;
371 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
372 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
373 safe_strerror(errno));
374 return -1;
375 }
376
377 return 0;
378 }
379
380 static int kmpw_uninstall(struct zebra_pw *pw)
381 {
382 struct ifreq ifr;
383 struct ifmpwreq imr;
384
385 memset(&ifr, 0, sizeof(ifr));
386 memset(&imr, 0, sizeof(imr));
387 strlcpy(ifr.ifr_name, pw->ifname, sizeof(ifr.ifr_name));
388 ifr.ifr_data = (caddr_t)&imr;
389 if (ioctl(kr_state.ioctl_fd, SIOCSETMPWCFG, &ifr) == -1) {
390 flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s",
391 safe_strerror(errno));
392 return -1;
393 }
394
395 return 0;
396 }
397
398 #define MAX_RTSOCK_BUF 128 * 1024
399 int mpls_kernel_init(void)
400 {
401 int rcvbuf, default_rcvbuf;
402 socklen_t optlen;
403
404 if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
405 flog_err_sys(EC_LIB_SOCKET, "%s: socket", __func__);
406 return -1;
407 }
408
409 if ((kr_state.ioctl_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0))
410 == -1) {
411 flog_err_sys(EC_LIB_SOCKET, "%s: ioctl socket", __func__);
412 return -1;
413 }
414
415 /* grow receive buffer, don't wanna miss messages */
416 optlen = sizeof(default_rcvbuf);
417 if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &default_rcvbuf,
418 &optlen)
419 == -1)
420 flog_err_sys(EC_LIB_SOCKET,
421 "kr_init getsockopt SOL_SOCKET SO_RCVBUF");
422 else
423 for (rcvbuf = MAX_RTSOCK_BUF;
424 rcvbuf > default_rcvbuf
425 && setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
426 sizeof(rcvbuf))
427 == -1
428 && errno == ENOBUFS;
429 rcvbuf /= 2)
430 ; /* nothing */
431
432 kr_state.rtseq = 1;
433
434 /* register hook to install/uninstall pseudowires */
435 hook_register(pw_install, kmpw_install);
436 hook_register(pw_uninstall, kmpw_uninstall);
437
438 return 0;
439 }
440
441 #endif /* OPEN_BSD */