]>
Commit | Line | Data |
---|---|---|
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(int action, zebra_lsp_t *lsp) | |
240 | { | |
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)))) { | |
259 | if (nhlfe->nexthop->nh_label->num_labels > 1) { | |
260 | flog_warn(EC_ZEBRA_MAX_LABELS_PUSH, | |
261 | "%s: can't push %u labels at once " | |
262 | "(maximum is 1)", | |
263 | __func__, | |
264 | nhlfe->nexthop->nh_label->num_labels); | |
265 | continue; | |
266 | } | |
267 | ||
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 | } | |
282 | } | |
283 | } | |
284 | ||
285 | return (0); | |
286 | } | |
287 | ||
288 | enum zebra_dplane_result kernel_add_lsp(zebra_lsp_t *lsp) | |
289 | { | |
290 | int ret; | |
291 | ||
292 | if (!lsp || !lsp->best_nhlfe) { // unexpected | |
293 | kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_INSTALL_FAILURE); | |
294 | return ZEBRA_DPLANE_REQUEST_FAILURE; | |
295 | } | |
296 | ||
297 | ret = kernel_lsp_cmd(RTM_ADD, lsp); | |
298 | ||
299 | kernel_lsp_pass_fail(lsp, | |
300 | (!ret) ? ZEBRA_DPLANE_INSTALL_SUCCESS | |
301 | : ZEBRA_DPLANE_INSTALL_FAILURE); | |
302 | ||
303 | return ZEBRA_DPLANE_REQUEST_SUCCESS; | |
304 | } | |
305 | ||
306 | enum zebra_dplane_result kernel_upd_lsp(zebra_lsp_t *lsp) | |
307 | { | |
308 | int ret; | |
309 | ||
310 | if (!lsp || !lsp->best_nhlfe) { // unexpected | |
311 | kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_INSTALL_FAILURE); | |
312 | return ZEBRA_DPLANE_REQUEST_FAILURE; | |
313 | } | |
314 | ||
315 | ret = kernel_lsp_cmd(RTM_CHANGE, lsp); | |
316 | ||
317 | kernel_lsp_pass_fail(lsp, | |
318 | (!ret) ? ZEBRA_DPLANE_INSTALL_SUCCESS | |
319 | : ZEBRA_DPLANE_INSTALL_FAILURE); | |
320 | return ZEBRA_DPLANE_REQUEST_SUCCESS; | |
321 | } | |
322 | ||
323 | enum zebra_dplane_result kernel_del_lsp(zebra_lsp_t *lsp) | |
324 | { | |
325 | int ret; | |
326 | ||
327 | if (!lsp) { // unexpected | |
328 | kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_DELETE_FAILURE); | |
329 | return ZEBRA_DPLANE_REQUEST_FAILURE; | |
330 | } | |
331 | ||
332 | if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) { | |
333 | kernel_lsp_pass_fail(lsp, ZEBRA_DPLANE_DELETE_FAILURE); | |
334 | return ZEBRA_DPLANE_REQUEST_FAILURE; | |
335 | } | |
336 | ||
337 | ret = kernel_lsp_cmd(RTM_DELETE, lsp); | |
338 | ||
339 | kernel_lsp_pass_fail(lsp, | |
340 | (!ret) ? ZEBRA_DPLANE_DELETE_SUCCESS | |
341 | : ZEBRA_DPLANE_DELETE_FAILURE); | |
342 | ||
343 | return ZEBRA_DPLANE_REQUEST_SUCCESS; | |
344 | } | |
345 | ||
346 | static 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: | |
363 | zlog_debug("%s: unhandled pseudowire type (%#X)", __func__, | |
364 | pw->type); | |
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: | |
385 | zlog_debug("%s: unhandled pseudowire address-family (%u)", | |
386 | __func__, pw->af); | |
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) { | |
401 | flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s", | |
402 | safe_strerror(errno)); | |
403 | return -1; | |
404 | } | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | static 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) { | |
419 | flog_err_sys(EC_LIB_SYSTEM_CALL, "ioctl SIOCSETMPWCFG: %s", | |
420 | safe_strerror(errno)); | |
421 | return -1; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
427 | #define MAX_RTSOCK_BUF 128 * 1024 | |
428 | int mpls_kernel_init(void) | |
429 | { | |
430 | int rcvbuf, default_rcvbuf; | |
431 | socklen_t optlen; | |
432 | ||
433 | if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) { | |
434 | flog_err_sys(EC_LIB_SOCKET, "%s: socket", __func__); | |
435 | return -1; | |
436 | } | |
437 | ||
438 | if ((kr_state.ioctl_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0)) | |
439 | == -1) { | |
440 | flog_err_sys(EC_LIB_SOCKET, "%s: ioctl socket", __func__); | |
441 | return -1; | |
442 | } | |
443 | ||
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) | |
449 | flog_err_sys(EC_LIB_SOCKET, | |
450 | "kr_init getsockopt SOL_SOCKET SO_RCVBUF"); | |
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 | ||
463 | /* register hook to install/uninstall pseudowires */ | |
464 | hook_register(pw_install, kmpw_install); | |
465 | hook_register(pw_uninstall, kmpw_uninstall); | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | #endif /* OPEN_BSD */ |