]>
Commit | Line | Data |
---|---|---|
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 RW |
21 | #include <zebra.h> |
22 | #include <netmpls/mpls.h> | |
23 | #include "zebra/rt.h" | |
24 | #include "zebra/zebra_mpls.h" | |
25 | #include "zebra/debug.h" | |
26 | ||
27 | #include "privs.h" | |
28 | #include "prefix.h" | |
29 | #include "interface.h" | |
30 | #include "log.h" | |
31 | ||
32 | extern struct zebra_privs_t zserv_privs; | |
33 | ||
34 | struct { | |
35 | u_int32_t rtseq; | |
36 | int fd; | |
37 | } kr_state; | |
38 | ||
39 | static int | |
e07486ce | 40 | kernel_send_rtmsg_v4 (int action, mpls_label_t in_label, zebra_nhlfe_t *nhlfe) |
d3e2c74a RW |
41 | { |
42 | struct iovec iov[5]; | |
43 | struct rt_msghdr hdr; | |
44 | struct sockaddr_mpls sa_label_in, sa_label_out; | |
45 | struct sockaddr_in nexthop; | |
46 | int iovcnt = 0; | |
47 | int ret; | |
48 | ||
49 | if (IS_ZEBRA_DEBUG_KERNEL) | |
e07486ce | 50 | zlog_debug ("%s: 0x%x, label=%u", __func__, action, in_label); |
d3e2c74a RW |
51 | |
52 | /* initialize header */ | |
283a3654 | 53 | memset (&hdr, 0, sizeof (hdr)); |
d3e2c74a RW |
54 | hdr.rtm_version = RTM_VERSION; |
55 | ||
56 | hdr.rtm_type = action; | |
57 | hdr.rtm_flags = RTF_UP; | |
58 | hdr.rtm_fmask = RTF_MPLS; | |
59 | hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */ | |
60 | hdr.rtm_msglen = sizeof (hdr); | |
61 | hdr.rtm_hdrlen = sizeof (struct rt_msghdr); | |
62 | hdr.rtm_priority = 0; | |
63 | /* adjust iovec */ | |
64 | iov[iovcnt].iov_base = &hdr; | |
65 | iov[iovcnt++].iov_len = sizeof (hdr); | |
66 | ||
67 | /* in label */ | |
283a3654 | 68 | memset (&sa_label_in, 0, sizeof (sa_label_in)); |
d3e2c74a RW |
69 | sa_label_in.smpls_len = sizeof (sa_label_in); |
70 | sa_label_in.smpls_family = AF_MPLS; | |
71 | sa_label_in.smpls_label = htonl(in_label << MPLS_LABEL_OFFSET); | |
72 | /* adjust header */ | |
73 | hdr.rtm_flags |= RTF_MPLS | RTF_MPATH; | |
74 | hdr.rtm_addrs |= RTA_DST; | |
75 | hdr.rtm_msglen += sizeof (sa_label_in); | |
76 | /* adjust iovec */ | |
77 | iov[iovcnt].iov_base = &sa_label_in; | |
78 | iov[iovcnt++].iov_len = sizeof (sa_label_in); | |
79 | ||
80 | /* nexthop */ | |
283a3654 | 81 | memset (&nexthop, 0, sizeof (nexthop)); |
d3e2c74a RW |
82 | nexthop.sin_len = sizeof (nexthop); |
83 | nexthop.sin_family = AF_INET; | |
84 | nexthop.sin_addr = nhlfe->nexthop->gate.ipv4; | |
85 | /* adjust header */ | |
86 | hdr.rtm_flags |= RTF_GATEWAY; | |
87 | hdr.rtm_addrs |= RTA_GATEWAY; | |
88 | hdr.rtm_msglen += sizeof (nexthop); | |
89 | /* adjust iovec */ | |
90 | iov[iovcnt].iov_base = &nexthop; | |
91 | iov[iovcnt++].iov_len = sizeof (nexthop); | |
92 | ||
93 | /* If action is RTM_DELETE we have to get rid of MPLS infos */ | |
94 | if (action != RTM_DELETE) | |
95 | { | |
283a3654 | 96 | memset (&sa_label_out, 0, sizeof (sa_label_out)); |
d3e2c74a RW |
97 | sa_label_out.smpls_len = sizeof (sa_label_out); |
98 | sa_label_out.smpls_family = AF_MPLS; | |
99 | sa_label_out.smpls_label = | |
100 | htonl(nhlfe->nexthop->nh_label->label[0] << MPLS_LABEL_OFFSET); | |
101 | /* adjust header */ | |
102 | hdr.rtm_addrs |= RTA_SRC; | |
103 | hdr.rtm_flags |= RTF_MPLS; | |
104 | hdr.rtm_msglen += sizeof (sa_label_out); | |
105 | /* adjust iovec */ | |
106 | iov[iovcnt].iov_base = &sa_label_out; | |
107 | iov[iovcnt++].iov_len = sizeof (sa_label_out); | |
108 | ||
109 | if (nhlfe->nexthop->nh_label->label[0] == MPLS_LABEL_IMPLNULL) | |
110 | hdr.rtm_mpls = MPLS_OP_POP; | |
111 | else | |
112 | hdr.rtm_mpls = MPLS_OP_SWAP; | |
113 | } | |
114 | ||
115 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
116 | zlog_err ("Can't raise privileges"); | |
117 | ret = writev (kr_state.fd, iov, iovcnt); | |
118 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
119 | zlog_err ("Can't lower privileges"); | |
120 | ||
121 | if (ret == -1) | |
e07486ce RW |
122 | zlog_err ("%s: %s", __func__, safe_strerror (errno)); |
123 | ||
124 | return ret; | |
125 | } | |
126 | ||
127 | #if !defined(ROUNDUP) | |
128 | #define ROUNDUP(a) \ | |
129 | (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a)) | |
130 | #endif | |
131 | ||
132 | static int | |
133 | kernel_send_rtmsg_v6 (int action, mpls_label_t in_label, zebra_nhlfe_t *nhlfe) | |
134 | { | |
135 | struct iovec iov[5]; | |
136 | struct rt_msghdr hdr; | |
137 | struct sockaddr_mpls sa_label_in, sa_label_out; | |
138 | struct pad { | |
139 | struct sockaddr_in6 addr; | |
140 | char pad[sizeof(long)]; /* thank you IPv6 */ | |
141 | } nexthop; | |
142 | int iovcnt = 0; | |
143 | int ret; | |
144 | ||
145 | if (IS_ZEBRA_DEBUG_KERNEL) | |
146 | zlog_debug ("%s: 0x%x, label=%u", __func__, action, in_label); | |
147 | ||
148 | /* initialize header */ | |
283a3654 | 149 | memset (&hdr, 0, sizeof (hdr)); |
e07486ce RW |
150 | hdr.rtm_version = RTM_VERSION; |
151 | ||
152 | hdr.rtm_type = action; | |
153 | hdr.rtm_flags = RTF_UP; | |
154 | hdr.rtm_fmask = RTF_MPLS; | |
155 | hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */ | |
156 | hdr.rtm_msglen = sizeof (hdr); | |
157 | hdr.rtm_hdrlen = sizeof (struct rt_msghdr); | |
158 | hdr.rtm_priority = 0; | |
159 | /* adjust iovec */ | |
160 | iov[iovcnt].iov_base = &hdr; | |
161 | iov[iovcnt++].iov_len = sizeof (hdr); | |
162 | ||
163 | /* in label */ | |
283a3654 | 164 | memset (&sa_label_in, 0, sizeof (sa_label_in)); |
e07486ce RW |
165 | sa_label_in.smpls_len = sizeof (sa_label_in); |
166 | sa_label_in.smpls_family = AF_MPLS; | |
167 | sa_label_in.smpls_label = htonl(in_label << MPLS_LABEL_OFFSET); | |
168 | /* adjust header */ | |
169 | hdr.rtm_flags |= RTF_MPLS | RTF_MPATH; | |
170 | hdr.rtm_addrs |= RTA_DST; | |
171 | hdr.rtm_msglen += sizeof (sa_label_in); | |
172 | /* adjust iovec */ | |
173 | iov[iovcnt].iov_base = &sa_label_in; | |
174 | iov[iovcnt++].iov_len = sizeof (sa_label_in); | |
175 | ||
176 | /* nexthop */ | |
177 | memset (&nexthop, 0, sizeof (nexthop)); | |
178 | nexthop.addr.sin6_len = sizeof (struct sockaddr_in6); | |
179 | nexthop.addr.sin6_family = AF_INET6; | |
180 | nexthop.addr.sin6_addr = nhlfe->nexthop->gate.ipv6; | |
181 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop.addr.sin6_addr)) | |
182 | { | |
183 | uint16_t tmp16; | |
184 | struct sockaddr_in6 *sin6 = &nexthop.addr; | |
185 | ||
186 | nexthop.addr.sin6_scope_id = nhlfe->nexthop->ifindex; | |
187 | ||
188 | memcpy (&tmp16, &sin6->sin6_addr.s6_addr[2], sizeof (tmp16)); | |
189 | tmp16 = htons (sin6->sin6_scope_id); | |
190 | memcpy (&sin6->sin6_addr.s6_addr[2], &tmp16, sizeof (tmp16)); | |
191 | sin6->sin6_scope_id = 0; | |
192 | } | |
193 | ||
194 | /* adjust header */ | |
195 | hdr.rtm_flags |= RTF_GATEWAY; | |
196 | hdr.rtm_addrs |= RTA_GATEWAY; | |
197 | hdr.rtm_msglen += ROUNDUP (sizeof (struct sockaddr_in6)); | |
198 | /* adjust iovec */ | |
199 | iov[iovcnt].iov_base = &nexthop; | |
200 | iov[iovcnt++].iov_len = ROUNDUP (sizeof (struct sockaddr_in6)); | |
201 | ||
202 | /* If action is RTM_DELETE we have to get rid of MPLS infos */ | |
203 | if (action != RTM_DELETE) | |
204 | { | |
283a3654 | 205 | memset (&sa_label_out, 0, sizeof (sa_label_out)); |
e07486ce RW |
206 | sa_label_out.smpls_len = sizeof (sa_label_out); |
207 | sa_label_out.smpls_family = AF_MPLS; | |
208 | sa_label_out.smpls_label = | |
209 | htonl(nhlfe->nexthop->nh_label->label[0] << MPLS_LABEL_OFFSET); | |
210 | /* adjust header */ | |
211 | hdr.rtm_addrs |= RTA_SRC; | |
212 | hdr.rtm_flags |= RTF_MPLS; | |
213 | hdr.rtm_msglen += sizeof (sa_label_out); | |
214 | /* adjust iovec */ | |
215 | iov[iovcnt].iov_base = &sa_label_out; | |
216 | iov[iovcnt++].iov_len = sizeof (sa_label_out); | |
217 | ||
218 | if (nhlfe->nexthop->nh_label->label[0] == MPLS_LABEL_IMPLNULL) | |
219 | hdr.rtm_mpls = MPLS_OP_POP; | |
220 | else | |
221 | hdr.rtm_mpls = MPLS_OP_SWAP; | |
222 | } | |
223 | ||
224 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
225 | zlog_err ("Can't raise privileges"); | |
226 | ret = writev (kr_state.fd, iov, iovcnt); | |
227 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
228 | zlog_err ("Can't lower privileges"); | |
229 | ||
230 | if (ret == -1) | |
231 | zlog_err ("%s: %s", __func__, safe_strerror (errno)); | |
d3e2c74a RW |
232 | |
233 | return ret; | |
234 | } | |
235 | ||
236 | static int | |
237 | kernel_lsp_cmd (int action, zebra_lsp_t *lsp) | |
238 | { | |
239 | zebra_nhlfe_t *nhlfe; | |
240 | struct nexthop *nexthop = NULL; | |
c4c8dec0 | 241 | unsigned int nexthop_num = 0; |
d3e2c74a RW |
242 | |
243 | for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) | |
244 | { | |
245 | nexthop = nhlfe->nexthop; | |
246 | if (!nexthop) | |
247 | continue; | |
248 | ||
37fe7731 | 249 | if (nexthop_num >= multipath_num) |
d3e2c74a RW |
250 | break; |
251 | ||
d3e2c74a RW |
252 | if (((action == RTM_ADD || action == RTM_CHANGE) && |
253 | (CHECK_FLAG (nhlfe->flags, NHLFE_FLAG_SELECTED) && | |
254 | CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))) || | |
255 | (action == RTM_DELETE && | |
256 | (CHECK_FLAG (nhlfe->flags, NHLFE_FLAG_INSTALLED) && | |
257 | CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))) | |
258 | { | |
259 | nexthop_num++; | |
260 | ||
e07486ce RW |
261 | switch (NHLFE_FAMILY(nhlfe)) |
262 | { | |
263 | case AF_INET: | |
264 | kernel_send_rtmsg_v4 (action, lsp->ile.in_label, nhlfe); | |
265 | break; | |
266 | case AF_INET6: | |
267 | kernel_send_rtmsg_v6 (action, lsp->ile.in_label, nhlfe); | |
268 | break; | |
269 | default: | |
270 | break; | |
271 | } | |
d3e2c74a RW |
272 | if (action == RTM_ADD || action == RTM_CHANGE) |
273 | { | |
274 | SET_FLAG (nhlfe->flags, NHLFE_FLAG_INSTALLED); | |
275 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
276 | } | |
277 | else | |
278 | { | |
279 | UNSET_FLAG (nhlfe->flags, NHLFE_FLAG_INSTALLED); | |
280 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | return (0); | |
286 | } | |
287 | ||
288 | int | |
289 | kernel_add_lsp (zebra_lsp_t *lsp) | |
290 | { | |
c4c8dec0 RW |
291 | int ret; |
292 | ||
d3e2c74a RW |
293 | if (!lsp || !lsp->best_nhlfe) // unexpected |
294 | return -1; | |
295 | ||
c4c8dec0 RW |
296 | UNSET_FLAG (lsp->flags, LSP_FLAG_CHANGED); |
297 | ret = kernel_lsp_cmd (RTM_ADD, lsp); | |
298 | if (!ret) | |
299 | SET_FLAG (lsp->flags, LSP_FLAG_INSTALLED); | |
300 | ||
301 | return ret; | |
d3e2c74a RW |
302 | } |
303 | ||
304 | int | |
305 | kernel_upd_lsp (zebra_lsp_t *lsp) | |
306 | { | |
c4c8dec0 RW |
307 | int ret; |
308 | ||
d3e2c74a RW |
309 | if (!lsp || !lsp->best_nhlfe) // unexpected |
310 | return -1; | |
311 | ||
c4c8dec0 RW |
312 | UNSET_FLAG (lsp->flags, LSP_FLAG_CHANGED); |
313 | UNSET_FLAG (lsp->flags, LSP_FLAG_INSTALLED); | |
314 | ret = kernel_lsp_cmd (RTM_CHANGE, lsp); | |
315 | if (!ret) | |
316 | SET_FLAG (lsp->flags, LSP_FLAG_INSTALLED); | |
317 | ||
318 | return ret; | |
d3e2c74a RW |
319 | } |
320 | ||
321 | int | |
322 | kernel_del_lsp (zebra_lsp_t *lsp) | |
323 | { | |
c4c8dec0 RW |
324 | int ret; |
325 | ||
d3e2c74a RW |
326 | if (!lsp) // unexpected |
327 | return -1; | |
328 | ||
c4c8dec0 RW |
329 | if (! CHECK_FLAG (lsp->flags, LSP_FLAG_INSTALLED)) |
330 | return -1; | |
331 | ||
332 | ret = kernel_lsp_cmd (RTM_DELETE, lsp); | |
333 | if (!ret) | |
334 | UNSET_FLAG (lsp->flags, LSP_FLAG_INSTALLED); | |
335 | ||
336 | return ret; | |
d3e2c74a RW |
337 | } |
338 | ||
339 | #define MAX_RTSOCK_BUF 128 * 1024 | |
fe6c7157 | 340 | int |
d3e2c74a RW |
341 | mpls_kernel_init (void) |
342 | { | |
343 | int rcvbuf, default_rcvbuf; | |
344 | socklen_t optlen; | |
345 | ||
346 | if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) { | |
fe6c7157 RW |
347 | zlog_warn("%s: socket", __func__); |
348 | return -1; | |
d3e2c74a RW |
349 | } |
350 | ||
351 | /* grow receive buffer, don't wanna miss messages */ | |
352 | optlen = sizeof (default_rcvbuf); | |
353 | if (getsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, | |
354 | &default_rcvbuf, &optlen) == -1) | |
355 | zlog_warn("kr_init getsockopt SOL_SOCKET SO_RCVBUF"); | |
356 | else | |
357 | for (rcvbuf = MAX_RTSOCK_BUF; | |
358 | rcvbuf > default_rcvbuf && | |
359 | setsockopt(kr_state.fd, SOL_SOCKET, SO_RCVBUF, | |
360 | &rcvbuf, sizeof (rcvbuf)) == -1 && errno == ENOBUFS; | |
361 | rcvbuf /= 2) | |
362 | ; /* nothing */ | |
363 | ||
364 | kr_state.rtseq = 1; | |
fe6c7157 RW |
365 | |
366 | return 0; | |
d3e2c74a | 367 | } |