]>
Commit | Line | Data |
---|---|---|
7951dea2 SS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
855ee1a1 | 4 | Copyright 2014 Susant Sahani |
7951dea2 SS |
5 | |
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd 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 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
7951dea2 SS |
20 | #include <arpa/inet.h> |
21 | #include <net/if.h> | |
22 | #include <linux/ip.h> | |
23 | #include <linux/if_tunnel.h> | |
855ee1a1 | 24 | #include <linux/ip6_tunnel.h> |
7951dea2 | 25 | |
1c4baffc | 26 | #include "sd-netlink.h" |
07630cea LP |
27 | |
28 | #include "conf-parser.h" | |
29 | #include "missing.h" | |
0b1831c2 | 30 | #include "networkd-link.h" |
441e9ae4 | 31 | #include "netdev/tunnel.h" |
6bedfcbb | 32 | #include "parse-util.h" |
8b43440b | 33 | #include "string-table.h" |
07630cea | 34 | #include "string-util.h" |
7951dea2 SS |
35 | #include "util.h" |
36 | ||
855ee1a1 | 37 | #define DEFAULT_TNL_HOP_LIMIT 64 |
8e38570e | 38 | #define IP6_FLOWINFO_FLOWLABEL htobe32(0x000FFFFF) |
855ee1a1 SS |
39 | |
40 | static const char* const ip6tnl_mode_table[_NETDEV_IP6_TNL_MODE_MAX] = { | |
41 | [NETDEV_IP6_TNL_MODE_IP6IP6] = "ip6ip6", | |
73b23bea | 42 | [NETDEV_IP6_TNL_MODE_IPIP6] = "ipip6", |
855ee1a1 SS |
43 | [NETDEV_IP6_TNL_MODE_ANYIP6] = "any", |
44 | }; | |
45 | ||
46 | DEFINE_STRING_TABLE_LOOKUP(ip6tnl_mode, Ip6TnlMode); | |
47 | DEFINE_CONFIG_PARSE_ENUM(config_parse_ip6tnl_mode, ip6tnl_mode, Ip6TnlMode, "Failed to parse ip6 tunnel Mode"); | |
48 | ||
1c4baffc | 49 | static int netdev_ipip_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
aa9f1140 | 50 | Tunnel *t = IPIP(netdev); |
7951dea2 SS |
51 | int r; |
52 | ||
3be1d7e0 | 53 | assert(netdev); |
7951dea2 | 54 | assert(link); |
7951dea2 | 55 | assert(m); |
aa9f1140 | 56 | assert(t); |
07dcb085 | 57 | assert(IN_SET(t->family, AF_INET, AF_UNSPEC)); |
7951dea2 | 58 | |
1c4baffc | 59 | r = sd_netlink_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex); |
5289f3ff SS |
60 | if (r < 0) |
61 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LINK attribute: %m"); | |
7951dea2 | 62 | |
1c4baffc | 63 | r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &t->local.in); |
5289f3ff SS |
64 | if (r < 0) |
65 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LOCAL attribute: %m"); | |
7951dea2 | 66 | |
1c4baffc | 67 | r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in); |
5289f3ff SS |
68 | if (r < 0) |
69 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_REMOTE attribute: %m"); | |
7951dea2 | 70 | |
1c4baffc | 71 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl); |
5289f3ff SS |
72 | if (r < 0) |
73 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_TTL attribute: %m"); | |
9ae70211 | 74 | |
1c4baffc | 75 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_PMTUDISC, t->pmtudisc); |
5289f3ff SS |
76 | if (r < 0) |
77 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m"); | |
9243e967 | 78 | |
7951dea2 SS |
79 | return r; |
80 | } | |
81 | ||
1c4baffc | 82 | static int netdev_sit_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
aa9f1140 | 83 | Tunnel *t = SIT(netdev); |
abf446af SS |
84 | int r; |
85 | ||
3be1d7e0 | 86 | assert(netdev); |
abf446af | 87 | assert(link); |
abf446af | 88 | assert(m); |
aa9f1140 | 89 | assert(t); |
07dcb085 | 90 | assert(IN_SET(t->family, AF_INET, AF_UNSPEC)); |
abf446af | 91 | |
1c4baffc | 92 | r = sd_netlink_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex); |
5289f3ff SS |
93 | if (r < 0) |
94 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LINK attribute: %m"); | |
abf446af | 95 | |
1c4baffc | 96 | r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &t->local.in); |
5289f3ff SS |
97 | if (r < 0) |
98 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LOCAL attribute: %m"); | |
abf446af | 99 | |
1c4baffc | 100 | r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in); |
5289f3ff SS |
101 | if (r < 0) |
102 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_REMOTE attribute: %m"); | |
abf446af | 103 | |
1c4baffc | 104 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl); |
5289f3ff SS |
105 | if (r < 0) |
106 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_TTL attribute: %m"); | |
a9f434cf | 107 | |
1c4baffc | 108 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_PMTUDISC, t->pmtudisc); |
5289f3ff SS |
109 | if (r < 0) |
110 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m"); | |
436b910f | 111 | |
abf446af SS |
112 | return r; |
113 | } | |
114 | ||
1c4baffc | 115 | static int netdev_gre_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
1af2536a | 116 | Tunnel *t; |
8bb088c5 SS |
117 | int r; |
118 | ||
3be1d7e0 | 119 | assert(netdev); |
1af2536a SS |
120 | |
121 | if (netdev->kind == NETDEV_KIND_GRE) | |
5289f3ff | 122 | t = GRE(netdev); |
1af2536a | 123 | else |
5289f3ff | 124 | t = GRETAP(netdev); |
1af2536a | 125 | |
aa9f1140 | 126 | assert(t); |
07dcb085 | 127 | assert(IN_SET(t->family, AF_INET, AF_UNSPEC)); |
1af2536a SS |
128 | assert(link); |
129 | assert(m); | |
8bb088c5 | 130 | |
1c4baffc | 131 | r = sd_netlink_message_append_u32(m, IFLA_GRE_LINK, link->ifindex); |
5289f3ff SS |
132 | if (r < 0) |
133 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_LINK attribute: %m"); | |
8bb088c5 | 134 | |
1c4baffc | 135 | r = sd_netlink_message_append_in_addr(m, IFLA_GRE_LOCAL, &t->local.in); |
5289f3ff SS |
136 | if (r < 0) |
137 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_LOCAL attribute: %m"); | |
8bb088c5 | 138 | |
1c4baffc | 139 | r = sd_netlink_message_append_in_addr(m, IFLA_GRE_REMOTE, &t->remote.in); |
5289f3ff SS |
140 | if (r < 0) |
141 | log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_REMOTE attribute: %m"); | |
8bb088c5 | 142 | |
1c4baffc | 143 | r = sd_netlink_message_append_u8(m, IFLA_GRE_TTL, t->ttl); |
5289f3ff SS |
144 | if (r < 0) |
145 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_TTL attribute: %m"); | |
8bb088c5 | 146 | |
1c4baffc | 147 | r = sd_netlink_message_append_u8(m, IFLA_GRE_TOS, t->tos); |
5289f3ff SS |
148 | if (r < 0) |
149 | log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_TOS attribute: %m"); | |
8bb088c5 | 150 | |
1c4baffc | 151 | r = sd_netlink_message_append_u8(m, IFLA_GRE_PMTUDISC, t->pmtudisc); |
5289f3ff SS |
152 | if (r < 0) |
153 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_PMTUDISC attribute: %m"); | |
9243e967 | 154 | |
8bb088c5 SS |
155 | return r; |
156 | } | |
157 | ||
1c4baffc | 158 | static int netdev_ip6gre_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
b16492f8 SS |
159 | Tunnel *t; |
160 | int r; | |
161 | ||
162 | assert(netdev); | |
163 | ||
164 | if (netdev->kind == NETDEV_KIND_IP6GRE) | |
5289f3ff | 165 | t = IP6GRE(netdev); |
b16492f8 | 166 | else |
5289f3ff | 167 | t = IP6GRETAP(netdev); |
b16492f8 SS |
168 | |
169 | assert(t); | |
170 | assert(t->family == AF_INET6); | |
171 | assert(link); | |
172 | assert(m); | |
173 | ||
1c4baffc | 174 | r = sd_netlink_message_append_u32(m, IFLA_GRE_LINK, link->ifindex); |
5289f3ff SS |
175 | if (r < 0) |
176 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_LINK attribute: %m"); | |
b16492f8 | 177 | |
1c4baffc | 178 | r = sd_netlink_message_append_in6_addr(m, IFLA_GRE_LOCAL, &t->local.in6); |
5289f3ff SS |
179 | if (r < 0) |
180 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_LOCAL attribute: %m"); | |
b16492f8 | 181 | |
1c4baffc | 182 | r = sd_netlink_message_append_in6_addr(m, IFLA_GRE_REMOTE, &t->remote.in6); |
5289f3ff SS |
183 | if (r < 0) |
184 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_REMOTE attribute: %m"); | |
b16492f8 | 185 | |
1c4baffc | 186 | r = sd_netlink_message_append_u8(m, IFLA_GRE_TTL, t->ttl); |
5289f3ff SS |
187 | if (r < 0) |
188 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_TTL attribute: %m"); | |
b16492f8 | 189 | |
54a9d20c SS |
190 | if (t->ipv6_flowlabel != _NETDEV_IPV6_FLOWLABEL_INVALID) { |
191 | r = sd_netlink_message_append_u32(m, IFLA_GRE_FLOWINFO, t->ipv6_flowlabel); | |
192 | if (r < 0) | |
193 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_FLOWINFO attribute: %m"); | |
194 | } | |
195 | ||
196 | r = sd_netlink_message_append_u32(m, IFLA_GRE_FLAGS, t->flags); | |
197 | if (r < 0) | |
198 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_GRE_FLAGS attribute: %m"); | |
199 | ||
b16492f8 SS |
200 | return r; |
201 | } | |
202 | ||
1d710029 | 203 | static int netdev_vti_fill_message_key(NetDev *netdev, Link *link, sd_netlink_message *m) { |
1d710029 | 204 | uint32_t ikey, okey; |
59f62519 | 205 | Tunnel *t; |
1d710029 SS |
206 | int r; |
207 | ||
208 | assert(link); | |
209 | assert(m); | |
59f62519 SS |
210 | |
211 | if (netdev->kind == NETDEV_KIND_VTI) | |
212 | t = VTI(netdev); | |
213 | else | |
214 | t = VTI6(netdev); | |
215 | ||
1d710029 SS |
216 | assert(t); |
217 | ||
218 | if (t->key != 0) | |
219 | ikey = okey = htobe32(t->key); | |
220 | else { | |
221 | ikey = htobe32(t->ikey); | |
222 | okey = htobe32(t->okey); | |
223 | } | |
224 | ||
225 | r = sd_netlink_message_append_u32(m, IFLA_VTI_IKEY, ikey); | |
226 | if (r < 0) | |
227 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_VTI_IKEY attribute: %m"); | |
228 | ||
229 | r = sd_netlink_message_append_u32(m, IFLA_VTI_OKEY, okey); | |
230 | if (r < 0) | |
231 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_VTI_OKEY attribute: %m"); | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
1c4baffc | 236 | static int netdev_vti_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
aa9f1140 | 237 | Tunnel *t = VTI(netdev); |
a613382b SS |
238 | int r; |
239 | ||
3be1d7e0 | 240 | assert(netdev); |
a613382b | 241 | assert(link); |
a613382b | 242 | assert(m); |
aa9f1140 TG |
243 | assert(t); |
244 | assert(t->family == AF_INET); | |
a613382b | 245 | |
1c4baffc | 246 | r = sd_netlink_message_append_u32(m, IFLA_VTI_LINK, link->ifindex); |
5289f3ff SS |
247 | if (r < 0) |
248 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LINK attribute: %m"); | |
a613382b | 249 | |
1d710029 SS |
250 | r = netdev_vti_fill_message_key(netdev, link, m); |
251 | if (r < 0) | |
252 | return r; | |
253 | ||
1c4baffc | 254 | r = sd_netlink_message_append_in_addr(m, IFLA_VTI_LOCAL, &t->local.in); |
5289f3ff SS |
255 | if (r < 0) |
256 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LOCAL attribute: %m"); | |
a613382b | 257 | |
1c4baffc | 258 | r = sd_netlink_message_append_in_addr(m, IFLA_VTI_REMOTE, &t->remote.in); |
5289f3ff SS |
259 | if (r < 0) |
260 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_REMOTE attribute: %m"); | |
a613382b | 261 | |
a613382b SS |
262 | return r; |
263 | } | |
8bb088c5 | 264 | |
1c4baffc | 265 | static int netdev_vti6_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
9011ce77 SS |
266 | Tunnel *t = VTI6(netdev); |
267 | int r; | |
268 | ||
269 | assert(netdev); | |
270 | assert(link); | |
271 | assert(m); | |
272 | assert(t); | |
273 | assert(t->family == AF_INET6); | |
274 | ||
1c4baffc | 275 | r = sd_netlink_message_append_u32(m, IFLA_VTI_LINK, link->ifindex); |
9011ce77 SS |
276 | if (r < 0) |
277 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LINK attribute: %m"); | |
278 | ||
1d710029 SS |
279 | r = netdev_vti_fill_message_key(netdev, link, m); |
280 | if (r < 0) | |
281 | return r; | |
282 | ||
1c4baffc | 283 | r = sd_netlink_message_append_in6_addr(m, IFLA_VTI_LOCAL, &t->local.in6); |
9011ce77 SS |
284 | if (r < 0) |
285 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LOCAL attribute: %m"); | |
286 | ||
1c4baffc | 287 | r = sd_netlink_message_append_in6_addr(m, IFLA_VTI_REMOTE, &t->remote.in6); |
9011ce77 SS |
288 | if (r < 0) |
289 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_REMOTE attribute: %m"); | |
290 | ||
291 | return r; | |
292 | } | |
293 | ||
1c4baffc | 294 | static int netdev_ip6tnl_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
855ee1a1 SS |
295 | Tunnel *t = IP6TNL(netdev); |
296 | uint8_t proto; | |
297 | int r; | |
298 | ||
299 | assert(netdev); | |
300 | assert(link); | |
301 | assert(m); | |
302 | assert(t); | |
303 | assert(t->family == AF_INET6); | |
304 | ||
1c4baffc | 305 | r = sd_netlink_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex); |
5289f3ff SS |
306 | if (r < 0) |
307 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LINK attribute: %m"); | |
855ee1a1 | 308 | |
1c4baffc | 309 | r = sd_netlink_message_append_in6_addr(m, IFLA_IPTUN_LOCAL, &t->local.in6); |
5289f3ff SS |
310 | if (r < 0) |
311 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_LOCAL attribute: %m"); | |
855ee1a1 | 312 | |
1c4baffc | 313 | r = sd_netlink_message_append_in6_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in6); |
5289f3ff SS |
314 | if (r < 0) |
315 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_REMOTE attribute: %m"); | |
855ee1a1 | 316 | |
1c4baffc | 317 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl); |
5289f3ff SS |
318 | if (r < 0) |
319 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_TTL attribute: %m"); | |
855ee1a1 | 320 | |
407af9dd SS |
321 | if (t->ipv6_flowlabel != _NETDEV_IPV6_FLOWLABEL_INVALID) { |
322 | r = sd_netlink_message_append_u32(m, IFLA_IPTUN_FLOWINFO, t->ipv6_flowlabel); | |
323 | if (r < 0) | |
324 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_FLOWINFO attribute: %m"); | |
325 | } | |
326 | ||
a9b70f9d | 327 | if (t->copy_dscp) |
ec2a3e3a SS |
328 | t->flags |= IP6_TNL_F_RCV_DSCP_COPY; |
329 | ||
b4828886 SS |
330 | if (t->encap_limit != IPV6_DEFAULT_TNL_ENCAP_LIMIT) { |
331 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_ENCAP_LIMIT, t->encap_limit); | |
332 | if (r < 0) | |
333 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_ENCAP_LIMIT attribute: %m"); | |
334 | } | |
335 | ||
407af9dd SS |
336 | r = sd_netlink_message_append_u32(m, IFLA_IPTUN_FLAGS, t->flags); |
337 | if (r < 0) | |
338 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_FLAGS attribute: %m"); | |
339 | ||
855ee1a1 SS |
340 | switch (t->ip6tnl_mode) { |
341 | case NETDEV_IP6_TNL_MODE_IP6IP6: | |
342 | proto = IPPROTO_IPV6; | |
343 | break; | |
344 | case NETDEV_IP6_TNL_MODE_IPIP6: | |
345 | proto = IPPROTO_IPIP; | |
346 | break; | |
347 | case NETDEV_IP6_TNL_MODE_ANYIP6: | |
348 | default: | |
349 | proto = 0; | |
350 | break; | |
351 | } | |
352 | ||
1c4baffc | 353 | r = sd_netlink_message_append_u8(m, IFLA_IPTUN_PROTO, proto); |
5289f3ff SS |
354 | if (r < 0) |
355 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_IPTUN_MODE attribute: %m"); | |
855ee1a1 SS |
356 | |
357 | return r; | |
358 | } | |
359 | ||
3be1d7e0 | 360 | static int netdev_tunnel_verify(NetDev *netdev, const char *filename) { |
aa9f1140 TG |
361 | Tunnel *t = NULL; |
362 | ||
7951dea2 | 363 | assert(netdev); |
3be1d7e0 | 364 | assert(filename); |
7951dea2 | 365 | |
aa9f1140 TG |
366 | switch (netdev->kind) { |
367 | case NETDEV_KIND_IPIP: | |
368 | t = IPIP(netdev); | |
369 | break; | |
370 | case NETDEV_KIND_SIT: | |
371 | t = SIT(netdev); | |
372 | break; | |
373 | case NETDEV_KIND_GRE: | |
374 | t = GRE(netdev); | |
375 | break; | |
1af2536a SS |
376 | case NETDEV_KIND_GRETAP: |
377 | t = GRETAP(netdev); | |
378 | break; | |
b16492f8 SS |
379 | case NETDEV_KIND_IP6GRE: |
380 | t = IP6GRE(netdev); | |
381 | break; | |
382 | case NETDEV_KIND_IP6GRETAP: | |
383 | t = IP6GRETAP(netdev); | |
384 | break; | |
aa9f1140 TG |
385 | case NETDEV_KIND_VTI: |
386 | t = VTI(netdev); | |
387 | break; | |
9011ce77 SS |
388 | case NETDEV_KIND_VTI6: |
389 | t = VTI6(netdev); | |
390 | break; | |
855ee1a1 SS |
391 | case NETDEV_KIND_IP6TNL: |
392 | t = IP6TNL(netdev); | |
393 | break; | |
aa9f1140 TG |
394 | default: |
395 | assert_not_reached("Invalid tunnel kind"); | |
396 | } | |
397 | ||
398 | assert(t); | |
399 | ||
efd3c897 | 400 | if (t->family != AF_INET && t->family != AF_INET6 && t->family != 0) { |
5289f3ff SS |
401 | log_warning("Tunnel with invalid address family configured in %s. Ignoring", filename); |
402 | return -EINVAL; | |
7951dea2 SS |
403 | } |
404 | ||
855ee1a1 SS |
405 | if (netdev->kind == NETDEV_KIND_IP6TNL) { |
406 | if (t->ip6tnl_mode == _NETDEV_IP6_TNL_MODE_INVALID) { | |
407 | log_warning("IP6 Tunnel without mode configured in %s. Ignoring", filename); | |
408 | return -EINVAL; | |
409 | } | |
410 | } | |
411 | ||
7951dea2 SS |
412 | return 0; |
413 | } | |
6ef892fc TG |
414 | |
415 | int config_parse_tunnel_address(const char *unit, | |
416 | const char *filename, | |
417 | unsigned line, | |
418 | const char *section, | |
419 | unsigned section_line, | |
420 | const char *lvalue, | |
421 | int ltype, | |
422 | const char *rvalue, | |
423 | void *data, | |
424 | void *userdata) { | |
aa9f1140 | 425 | Tunnel *t = userdata; |
44e7b949 LP |
426 | union in_addr_union *addr = data, buffer; |
427 | int r, f; | |
6ef892fc TG |
428 | |
429 | assert(filename); | |
430 | assert(lvalue); | |
431 | assert(rvalue); | |
432 | assert(data); | |
433 | ||
efd3c897 SS |
434 | if (streq(rvalue, "any")) { |
435 | t->family = 0; | |
6ef892fc | 436 | return 0; |
efd3c897 | 437 | } else { |
6ef892fc | 438 | |
efd3c897 SS |
439 | r = in_addr_from_string_auto(rvalue, &f, &buffer); |
440 | if (r < 0) { | |
441 | log_syntax(unit, LOG_ERR, filename, line, r, "Tunnel address is invalid, ignoring assignment: %s", rvalue); | |
442 | return 0; | |
443 | } | |
444 | ||
445 | if (t->family != AF_UNSPEC && t->family != f) { | |
446 | log_syntax(unit, LOG_ERR, filename, line, 0, "Tunnel addresses incompatible, ignoring assignment: %s", rvalue); | |
447 | return 0; | |
448 | } | |
44e7b949 LP |
449 | } |
450 | ||
451 | t->family = f; | |
452 | *addr = buffer; | |
453 | ||
6ef892fc TG |
454 | return 0; |
455 | } | |
3be1d7e0 | 456 | |
1d710029 SS |
457 | int config_parse_tunnel_key(const char *unit, |
458 | const char *filename, | |
459 | unsigned line, | |
460 | const char *section, | |
461 | unsigned section_line, | |
462 | const char *lvalue, | |
463 | int ltype, | |
464 | const char *rvalue, | |
465 | void *data, | |
466 | void *userdata) { | |
467 | union in_addr_union buffer; | |
468 | Tunnel *t = userdata; | |
469 | uint32_t k; | |
470 | int r; | |
471 | ||
472 | assert(filename); | |
473 | assert(lvalue); | |
474 | assert(rvalue); | |
475 | assert(data); | |
476 | ||
477 | r = in_addr_from_string(AF_INET, rvalue, &buffer); | |
478 | if (r < 0) { | |
479 | r = safe_atou32(rvalue, &k); | |
480 | if (r < 0) { | |
481 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse tunnel key ignoring assignment: %s", rvalue); | |
482 | return 0; | |
483 | } | |
484 | } else | |
485 | k = be32toh(buffer.in.s_addr); | |
486 | ||
487 | if (streq(lvalue, "Key")) | |
488 | t->key = k; | |
489 | else if (streq(lvalue, "InputKey")) | |
490 | t->ikey = k; | |
491 | else | |
492 | t->okey = k; | |
493 | ||
494 | return 0; | |
495 | } | |
496 | ||
407af9dd SS |
497 | int config_parse_ipv6_flowlabel(const char* unit, |
498 | const char *filename, | |
499 | unsigned line, | |
500 | const char *section, | |
501 | unsigned section_line, | |
502 | const char *lvalue, | |
503 | int ltype, | |
504 | const char *rvalue, | |
505 | void *data, | |
506 | void *userdata) { | |
507 | IPv6FlowLabel *ipv6_flowlabel = data; | |
508 | Tunnel *t = userdata; | |
407af9dd SS |
509 | int k = 0; |
510 | int r; | |
511 | ||
512 | assert(filename); | |
513 | assert(lvalue); | |
514 | assert(rvalue); | |
515 | assert(ipv6_flowlabel); | |
516 | ||
6870b415 | 517 | if (streq(rvalue, "inherit")) { |
407af9dd SS |
518 | *ipv6_flowlabel = IP6_FLOWINFO_FLOWLABEL; |
519 | t->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; | |
520 | } else { | |
6870b415 | 521 | r = config_parse_int(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &k, userdata); |
12ca818f LP |
522 | if (r < 0) |
523 | return r; | |
524 | ||
525 | if (k > 0xFFFFF) | |
526 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPv6 flowlabel option, ignoring: %s", rvalue); | |
527 | else { | |
8e38570e | 528 | *ipv6_flowlabel = htobe32(k) & IP6_FLOWINFO_FLOWLABEL; |
12ca818f | 529 | t->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; |
407af9dd SS |
530 | } |
531 | } | |
532 | ||
533 | return 0; | |
534 | } | |
535 | ||
b4828886 SS |
536 | int config_parse_encap_limit(const char* unit, |
537 | const char *filename, | |
538 | unsigned line, | |
539 | const char *section, | |
540 | unsigned section_line, | |
541 | const char *lvalue, | |
542 | int ltype, | |
543 | const char *rvalue, | |
544 | void *data, | |
545 | void *userdata) { | |
546 | Tunnel *t = userdata; | |
547 | int k = 0; | |
548 | int r; | |
549 | ||
550 | assert(filename); | |
551 | assert(lvalue); | |
552 | assert(rvalue); | |
553 | ||
554 | if (streq(rvalue, "none")) | |
555 | t->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT; | |
556 | else { | |
557 | r = safe_atoi(rvalue, &k); | |
558 | if (r < 0) { | |
12ca818f | 559 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse Tunnel Encapsulation Limit option, ignoring: %s", rvalue); |
b4828886 SS |
560 | return 0; |
561 | } | |
562 | ||
563 | if (k > 255 || k < 0) | |
12ca818f | 564 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid Tunnel Encapsulation value, ignoring: %d", k); |
b4828886 SS |
565 | else { |
566 | t->encap_limit = k; | |
567 | t->flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT; | |
568 | } | |
569 | } | |
570 | ||
571 | return 0; | |
572 | } | |
573 | ||
aa9f1140 TG |
574 | static void ipip_init(NetDev *n) { |
575 | Tunnel *t = IPIP(n); | |
576 | ||
577 | assert(n); | |
578 | assert(t); | |
579 | ||
580 | t->pmtudisc = true; | |
07dcb085 | 581 | t->family = AF_UNSPEC; |
aa9f1140 TG |
582 | } |
583 | ||
584 | static void sit_init(NetDev *n) { | |
585 | Tunnel *t = SIT(n); | |
586 | ||
587 | assert(n); | |
588 | assert(t); | |
589 | ||
590 | t->pmtudisc = true; | |
07dcb085 | 591 | t->family = AF_UNSPEC; |
aa9f1140 TG |
592 | } |
593 | ||
594 | static void vti_init(NetDev *n) { | |
7185d805 | 595 | Tunnel *t; |
aa9f1140 TG |
596 | |
597 | assert(n); | |
9011ce77 SS |
598 | |
599 | if (n->kind == NETDEV_KIND_VTI) | |
7185d805 | 600 | t = VTI(n); |
9011ce77 SS |
601 | else |
602 | t = VTI6(n); | |
603 | ||
aa9f1140 TG |
604 | assert(t); |
605 | ||
606 | t->pmtudisc = true; | |
607 | } | |
608 | ||
609 | static void gre_init(NetDev *n) { | |
1af2536a | 610 | Tunnel *t; |
aa9f1140 TG |
611 | |
612 | assert(n); | |
1af2536a SS |
613 | |
614 | if (n->kind == NETDEV_KIND_GRE) | |
615 | t = GRE(n); | |
616 | else | |
617 | t = GRETAP(n); | |
618 | ||
aa9f1140 TG |
619 | assert(t); |
620 | ||
621 | t->pmtudisc = true; | |
07dcb085 | 622 | t->family = AF_UNSPEC; |
aa9f1140 TG |
623 | } |
624 | ||
b16492f8 SS |
625 | static void ip6gre_init(NetDev *n) { |
626 | Tunnel *t; | |
627 | ||
628 | assert(n); | |
629 | ||
630 | if (n->kind == NETDEV_KIND_IP6GRE) | |
631 | t = IP6GRE(n); | |
632 | else | |
633 | t = IP6GRETAP(n); | |
634 | ||
635 | assert(t); | |
636 | ||
637 | t->ttl = DEFAULT_TNL_HOP_LIMIT; | |
638 | } | |
639 | ||
855ee1a1 SS |
640 | static void ip6tnl_init(NetDev *n) { |
641 | Tunnel *t = IP6TNL(n); | |
642 | ||
643 | assert(n); | |
644 | assert(t); | |
645 | ||
646 | t->ttl = DEFAULT_TNL_HOP_LIMIT; | |
647 | t->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; | |
648 | t->ip6tnl_mode = _NETDEV_IP6_TNL_MODE_INVALID; | |
407af9dd | 649 | t->ipv6_flowlabel = _NETDEV_IPV6_FLOWLABEL_INVALID; |
855ee1a1 SS |
650 | } |
651 | ||
3be1d7e0 | 652 | const NetDevVTable ipip_vtable = { |
aa9f1140 TG |
653 | .object_size = sizeof(Tunnel), |
654 | .init = ipip_init, | |
655 | .sections = "Match\0NetDev\0Tunnel\0", | |
656 | .fill_message_create = netdev_ipip_fill_message_create, | |
657 | .create_type = NETDEV_CREATE_STACKED, | |
3be1d7e0 TG |
658 | .config_verify = netdev_tunnel_verify, |
659 | }; | |
660 | ||
661 | const NetDevVTable sit_vtable = { | |
aa9f1140 TG |
662 | .object_size = sizeof(Tunnel), |
663 | .init = sit_init, | |
664 | .sections = "Match\0NetDev\0Tunnel\0", | |
665 | .fill_message_create = netdev_sit_fill_message_create, | |
666 | .create_type = NETDEV_CREATE_STACKED, | |
3be1d7e0 TG |
667 | .config_verify = netdev_tunnel_verify, |
668 | }; | |
669 | ||
670 | const NetDevVTable vti_vtable = { | |
aa9f1140 TG |
671 | .object_size = sizeof(Tunnel), |
672 | .init = vti_init, | |
673 | .sections = "Match\0NetDev\0Tunnel\0", | |
674 | .fill_message_create = netdev_vti_fill_message_create, | |
675 | .create_type = NETDEV_CREATE_STACKED, | |
3be1d7e0 TG |
676 | .config_verify = netdev_tunnel_verify, |
677 | }; | |
678 | ||
9011ce77 SS |
679 | const NetDevVTable vti6_vtable = { |
680 | .object_size = sizeof(Tunnel), | |
681 | .init = vti_init, | |
682 | .sections = "Match\0NetDev\0Tunnel\0", | |
683 | .fill_message_create = netdev_vti6_fill_message_create, | |
684 | .create_type = NETDEV_CREATE_STACKED, | |
685 | .config_verify = netdev_tunnel_verify, | |
686 | }; | |
687 | ||
3be1d7e0 | 688 | const NetDevVTable gre_vtable = { |
aa9f1140 TG |
689 | .object_size = sizeof(Tunnel), |
690 | .init = gre_init, | |
691 | .sections = "Match\0NetDev\0Tunnel\0", | |
692 | .fill_message_create = netdev_gre_fill_message_create, | |
693 | .create_type = NETDEV_CREATE_STACKED, | |
3be1d7e0 TG |
694 | .config_verify = netdev_tunnel_verify, |
695 | }; | |
1af2536a SS |
696 | |
697 | const NetDevVTable gretap_vtable = { | |
698 | .object_size = sizeof(Tunnel), | |
699 | .init = gre_init, | |
700 | .sections = "Match\0NetDev\0Tunnel\0", | |
701 | .fill_message_create = netdev_gre_fill_message_create, | |
702 | .create_type = NETDEV_CREATE_STACKED, | |
703 | .config_verify = netdev_tunnel_verify, | |
704 | }; | |
855ee1a1 | 705 | |
b16492f8 SS |
706 | const NetDevVTable ip6gre_vtable = { |
707 | .object_size = sizeof(Tunnel), | |
708 | .init = ip6gre_init, | |
709 | .sections = "Match\0NetDev\0Tunnel\0", | |
710 | .fill_message_create = netdev_ip6gre_fill_message_create, | |
711 | .create_type = NETDEV_CREATE_STACKED, | |
712 | .config_verify = netdev_tunnel_verify, | |
713 | }; | |
714 | ||
715 | const NetDevVTable ip6gretap_vtable = { | |
716 | .object_size = sizeof(Tunnel), | |
717 | .init = ip6gre_init, | |
718 | .sections = "Match\0NetDev\0Tunnel\0", | |
719 | .fill_message_create = netdev_ip6gre_fill_message_create, | |
720 | .create_type = NETDEV_CREATE_STACKED, | |
721 | .config_verify = netdev_tunnel_verify, | |
722 | }; | |
723 | ||
855ee1a1 SS |
724 | const NetDevVTable ip6tnl_vtable = { |
725 | .object_size = sizeof(Tunnel), | |
726 | .init = ip6tnl_init, | |
727 | .sections = "Match\0NetDev\0Tunnel\0", | |
728 | .fill_message_create = netdev_ip6tnl_fill_message_create, | |
729 | .create_type = NETDEV_CREATE_STACKED, | |
730 | .config_verify = netdev_tunnel_verify, | |
731 | }; |