1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Susant Sahani
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
23 #include <arpa/inet.h>
26 #include <linux/if_tunnel.h>
27 #include <linux/ip6_tunnel.h>
30 #include "networkd-netdev-tunnel.h"
31 #include "networkd-link.h"
32 #include "network-internal.h"
35 #include "conf-parser.h"
37 #define DEFAULT_TNL_HOP_LIMIT 64
39 static const char* const ip6tnl_mode_table[_NETDEV_IP6_TNL_MODE_MAX] = {
40 [NETDEV_IP6_TNL_MODE_IP6IP6] = "ip6ip6",
41 [NETDEV_IP6_TNL_MODE_IPIP6] = "ip4ipv6",
42 [NETDEV_IP6_TNL_MODE_ANYIP6] = "any",
45 DEFINE_STRING_TABLE_LOOKUP(ip6tnl_mode, Ip6TnlMode);
46 DEFINE_CONFIG_PARSE_ENUM(config_parse_ip6tnl_mode, ip6tnl_mode, Ip6TnlMode, "Failed to parse ip6 tunnel Mode");
48 static int netdev_ipip_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
49 Tunnel *t = IPIP(netdev);
56 assert(t->family == AF_INET);
58 r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
60 log_netdev_error(netdev,
61 "Could not append IFLA_IPTUN_LINK attribute: %s",
66 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &t->local.in);
68 log_netdev_error(netdev,
69 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
74 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in);
76 log_netdev_error(netdev,
77 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
82 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
84 log_netdev_error(netdev,
85 "Could not append IFLA_IPTUN_TTL attribute: %s",
90 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_PMTUDISC, t->pmtudisc);
92 log_netdev_error(netdev,
93 "Could not append IFLA_IPTUN_PMTUDISC attribute: %s",
101 static int netdev_sit_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
102 Tunnel *t = SIT(netdev);
109 assert(t->family == AF_INET);
111 r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
113 log_netdev_error(netdev,
114 "Could not append IFLA_IPTUN_LINK attribute: %s",
119 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &t->local.in);
121 log_netdev_error(netdev,
122 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
127 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in);
129 log_netdev_error(netdev,
130 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
135 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
137 log_netdev_error(netdev,
138 "Could not append IFLA_IPTUN_TTL attribute: %s",
143 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_PMTUDISC, t->pmtudisc);
145 log_netdev_error(netdev,
146 "Could not append IFLA_IPTUN_PMTUDISC attribute: %s",
154 static int netdev_gre_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
160 if (netdev->kind == NETDEV_KIND_GRE)
166 assert(t->family == AF_INET);
170 r = sd_rtnl_message_append_u32(m, IFLA_GRE_LINK, link->ifindex);
172 log_netdev_error(netdev,
173 "Could not append IFLA_GRE_LINK attribute: %s",
178 r = sd_rtnl_message_append_in_addr(m, IFLA_GRE_LOCAL, &t->local.in);
180 log_netdev_error(netdev,
181 "Could not append IFLA_GRE_LOCAL attribute: %s",
186 r = sd_rtnl_message_append_in_addr(m, IFLA_GRE_REMOTE, &t->remote.in);
188 log_netdev_error(netdev,
189 "Could not append IFLA_GRE_REMOTE attribute: %s",
194 r = sd_rtnl_message_append_u8(m, IFLA_GRE_TTL, t->ttl);
196 log_netdev_error(netdev,
197 "Could not append IFLA_GRE_TTL attribute: %s",
202 r = sd_rtnl_message_append_u8(m, IFLA_GRE_TOS, t->tos);
204 log_netdev_error(netdev,
205 "Could not append IFLA_GRE_TOS attribute: %s",
210 r = sd_rtnl_message_append_u8(m, IFLA_GRE_PMTUDISC, t->pmtudisc);
212 log_netdev_error(netdev,
213 "Could not append IFLA_GRE_PMTUDISC attribute: %s",
221 static int netdev_vti_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
222 Tunnel *t = VTI(netdev);
229 assert(t->family == AF_INET);
231 r = sd_rtnl_message_append_u32(m, IFLA_VTI_LINK, link->ifindex);
233 log_netdev_error(netdev,
234 "Could not append IFLA_IPTUN_LINK attribute: %s",
239 r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_LOCAL, &t->local.in);
241 log_netdev_error(netdev,
242 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
247 r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_REMOTE, &t->remote.in);
249 log_netdev_error(netdev,
250 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
258 static int netdev_ip6tnl_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
259 Tunnel *t = IP6TNL(netdev);
267 assert(t->family == AF_INET6);
269 r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
271 log_netdev_error(netdev,
272 "Could not append IFLA_IPTUN_LINK attribute: %s",
277 r = sd_rtnl_message_append_in6_addr(m, IFLA_IPTUN_LOCAL, &t->local.in6);
279 log_netdev_error(netdev,
280 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
285 r = sd_rtnl_message_append_in6_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in6);
287 log_netdev_error(netdev,
288 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
293 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
295 log_netdev_error(netdev,
296 "Could not append IFLA_IPTUN_TTL attribute: %s",
301 switch (t->ip6tnl_mode) {
302 case NETDEV_IP6_TNL_MODE_IP6IP6:
303 proto = IPPROTO_IPV6;
305 case NETDEV_IP6_TNL_MODE_IPIP6:
306 proto = IPPROTO_IPIP;
308 case NETDEV_IP6_TNL_MODE_ANYIP6:
314 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_PROTO, proto);
316 log_netdev_error(netdev,
317 "Could not append IFLA_IPTUN_MODE attribute: %s",
325 static int netdev_tunnel_verify(NetDev *netdev, const char *filename) {
331 switch (netdev->kind) {
332 case NETDEV_KIND_IPIP:
335 case NETDEV_KIND_SIT:
338 case NETDEV_KIND_GRE:
341 case NETDEV_KIND_GRETAP:
344 case NETDEV_KIND_VTI:
347 case NETDEV_KIND_IP6TNL:
351 assert_not_reached("Invalid tunnel kind");
356 if (t->remote.in.s_addr == INADDR_ANY) {
357 log_warning("Tunnel without remote address configured in %s. Ignoring", filename);
361 if (t->family != AF_INET && t->family != AF_INET6) {
362 log_warning("Tunnel with invalid address family configured in %s. Ignoring", filename);
366 if (netdev->kind == NETDEV_KIND_IP6TNL) {
367 if (t->ip6tnl_mode == _NETDEV_IP6_TNL_MODE_INVALID) {
368 log_warning("IP6 Tunnel without mode configured in %s. Ignoring", filename);
376 int config_parse_tunnel_address(const char *unit,
377 const char *filename,
380 unsigned section_line,
386 Tunnel *t = userdata;
387 union in_addr_union *addr = data, buffer;
395 r = in_addr_from_string_auto(rvalue, &f, &buffer);
397 log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Tunnel address is invalid, ignoring assignment: %s", rvalue);
401 if (t->family != AF_UNSPEC && t->family != f) {
402 log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Tunnel addresses incompatible, ignoring assignment: %s", rvalue);
412 static void ipip_init(NetDev *n) {
421 static void sit_init(NetDev *n) {
430 static void vti_init(NetDev *n) {
439 static void gre_init(NetDev *n) {
444 if (n->kind == NETDEV_KIND_GRE)
454 static void ip6tnl_init(NetDev *n) {
455 Tunnel *t = IP6TNL(n);
460 t->ttl = DEFAULT_TNL_HOP_LIMIT;
461 t->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
462 t->ip6tnl_mode = _NETDEV_IP6_TNL_MODE_INVALID;
465 const NetDevVTable ipip_vtable = {
466 .object_size = sizeof(Tunnel),
468 .sections = "Match\0NetDev\0Tunnel\0",
469 .fill_message_create = netdev_ipip_fill_message_create,
470 .create_type = NETDEV_CREATE_STACKED,
471 .config_verify = netdev_tunnel_verify,
474 const NetDevVTable sit_vtable = {
475 .object_size = sizeof(Tunnel),
477 .sections = "Match\0NetDev\0Tunnel\0",
478 .fill_message_create = netdev_sit_fill_message_create,
479 .create_type = NETDEV_CREATE_STACKED,
480 .config_verify = netdev_tunnel_verify,
483 const NetDevVTable vti_vtable = {
484 .object_size = sizeof(Tunnel),
486 .sections = "Match\0NetDev\0Tunnel\0",
487 .fill_message_create = netdev_vti_fill_message_create,
488 .create_type = NETDEV_CREATE_STACKED,
489 .config_verify = netdev_tunnel_verify,
492 const NetDevVTable gre_vtable = {
493 .object_size = sizeof(Tunnel),
495 .sections = "Match\0NetDev\0Tunnel\0",
496 .fill_message_create = netdev_gre_fill_message_create,
497 .create_type = NETDEV_CREATE_STACKED,
498 .config_verify = netdev_tunnel_verify,
501 const NetDevVTable gretap_vtable = {
502 .object_size = sizeof(Tunnel),
504 .sections = "Match\0NetDev\0Tunnel\0",
505 .fill_message_create = netdev_gre_fill_message_create,
506 .create_type = NETDEV_CREATE_STACKED,
507 .config_verify = netdev_tunnel_verify,
510 const NetDevVTable ip6tnl_vtable = {
511 .object_size = sizeof(Tunnel),
513 .sections = "Match\0NetDev\0Tunnel\0",
514 .fill_message_create = netdev_ip6tnl_fill_message_create,
515 .create_type = NETDEV_CREATE_STACKED,
516 .config_verify = netdev_tunnel_verify,