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] = "ipip6",
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_ip6gre_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
227 if (netdev->kind == NETDEV_KIND_IP6GRE)
230 t = IP6GRETAP(netdev);
233 assert(t->family == AF_INET6);
237 r = sd_rtnl_message_append_u32(m, IFLA_GRE_LINK, link->ifindex);
239 log_netdev_error(netdev,
240 "Could not append IFLA_GRE_LINK attribute: %s",
245 r = sd_rtnl_message_append_in6_addr(m, IFLA_GRE_LOCAL, &t->local.in6);
247 log_netdev_error(netdev,
248 "Could not append IFLA_GRE_LOCAL attribute: %s",
253 r = sd_rtnl_message_append_in6_addr(m, IFLA_GRE_REMOTE, &t->remote.in6);
255 log_netdev_error(netdev,
256 "Could not append IFLA_GRE_REMOTE attribute: %s",
261 r = sd_rtnl_message_append_u8(m, IFLA_GRE_TTL, t->ttl);
263 log_netdev_error(netdev,
264 "Could not append IFLA_GRE_TTL attribute: %s",
272 static int netdev_vti_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
273 Tunnel *t = VTI(netdev);
280 assert(t->family == AF_INET);
282 r = sd_rtnl_message_append_u32(m, IFLA_VTI_LINK, link->ifindex);
284 log_netdev_error(netdev,
285 "Could not append IFLA_IPTUN_LINK attribute: %s",
290 r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_LOCAL, &t->local.in);
292 log_netdev_error(netdev,
293 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
298 r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_REMOTE, &t->remote.in);
300 log_netdev_error(netdev,
301 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
309 static int netdev_ip6tnl_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
310 Tunnel *t = IP6TNL(netdev);
318 assert(t->family == AF_INET6);
320 r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
322 log_netdev_error(netdev,
323 "Could not append IFLA_IPTUN_LINK attribute: %s",
328 r = sd_rtnl_message_append_in6_addr(m, IFLA_IPTUN_LOCAL, &t->local.in6);
330 log_netdev_error(netdev,
331 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
336 r = sd_rtnl_message_append_in6_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in6);
338 log_netdev_error(netdev,
339 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
344 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
346 log_netdev_error(netdev,
347 "Could not append IFLA_IPTUN_TTL attribute: %s",
352 switch (t->ip6tnl_mode) {
353 case NETDEV_IP6_TNL_MODE_IP6IP6:
354 proto = IPPROTO_IPV6;
356 case NETDEV_IP6_TNL_MODE_IPIP6:
357 proto = IPPROTO_IPIP;
359 case NETDEV_IP6_TNL_MODE_ANYIP6:
365 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_PROTO, proto);
367 log_netdev_error(netdev,
368 "Could not append IFLA_IPTUN_MODE attribute: %s",
376 static int netdev_tunnel_verify(NetDev *netdev, const char *filename) {
382 switch (netdev->kind) {
383 case NETDEV_KIND_IPIP:
386 case NETDEV_KIND_SIT:
389 case NETDEV_KIND_GRE:
392 case NETDEV_KIND_GRETAP:
395 case NETDEV_KIND_IP6GRE:
398 case NETDEV_KIND_IP6GRETAP:
399 t = IP6GRETAP(netdev);
401 case NETDEV_KIND_VTI:
404 case NETDEV_KIND_IP6TNL:
408 assert_not_reached("Invalid tunnel kind");
413 if (t->remote.in.s_addr == INADDR_ANY) {
414 log_warning("Tunnel without remote address configured in %s. Ignoring", filename);
418 if (t->family != AF_INET && t->family != AF_INET6) {
419 log_warning("Tunnel with invalid address family configured in %s. Ignoring", filename);
423 if (netdev->kind == NETDEV_KIND_IP6TNL) {
424 if (t->ip6tnl_mode == _NETDEV_IP6_TNL_MODE_INVALID) {
425 log_warning("IP6 Tunnel without mode configured in %s. Ignoring", filename);
433 int config_parse_tunnel_address(const char *unit,
434 const char *filename,
437 unsigned section_line,
443 Tunnel *t = userdata;
444 union in_addr_union *addr = data, buffer;
452 r = in_addr_from_string_auto(rvalue, &f, &buffer);
454 log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Tunnel address is invalid, ignoring assignment: %s", rvalue);
458 if (t->family != AF_UNSPEC && t->family != f) {
459 log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Tunnel addresses incompatible, ignoring assignment: %s", rvalue);
469 static void ipip_init(NetDev *n) {
478 static void sit_init(NetDev *n) {
487 static void vti_init(NetDev *n) {
496 static void gre_init(NetDev *n) {
501 if (n->kind == NETDEV_KIND_GRE)
511 static void ip6gre_init(NetDev *n) {
516 if (n->kind == NETDEV_KIND_IP6GRE)
523 t->ttl = DEFAULT_TNL_HOP_LIMIT;
526 static void ip6tnl_init(NetDev *n) {
527 Tunnel *t = IP6TNL(n);
532 t->ttl = DEFAULT_TNL_HOP_LIMIT;
533 t->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT;
534 t->ip6tnl_mode = _NETDEV_IP6_TNL_MODE_INVALID;
537 const NetDevVTable ipip_vtable = {
538 .object_size = sizeof(Tunnel),
540 .sections = "Match\0NetDev\0Tunnel\0",
541 .fill_message_create = netdev_ipip_fill_message_create,
542 .create_type = NETDEV_CREATE_STACKED,
543 .config_verify = netdev_tunnel_verify,
546 const NetDevVTable sit_vtable = {
547 .object_size = sizeof(Tunnel),
549 .sections = "Match\0NetDev\0Tunnel\0",
550 .fill_message_create = netdev_sit_fill_message_create,
551 .create_type = NETDEV_CREATE_STACKED,
552 .config_verify = netdev_tunnel_verify,
555 const NetDevVTable vti_vtable = {
556 .object_size = sizeof(Tunnel),
558 .sections = "Match\0NetDev\0Tunnel\0",
559 .fill_message_create = netdev_vti_fill_message_create,
560 .create_type = NETDEV_CREATE_STACKED,
561 .config_verify = netdev_tunnel_verify,
564 const NetDevVTable gre_vtable = {
565 .object_size = sizeof(Tunnel),
567 .sections = "Match\0NetDev\0Tunnel\0",
568 .fill_message_create = netdev_gre_fill_message_create,
569 .create_type = NETDEV_CREATE_STACKED,
570 .config_verify = netdev_tunnel_verify,
573 const NetDevVTable gretap_vtable = {
574 .object_size = sizeof(Tunnel),
576 .sections = "Match\0NetDev\0Tunnel\0",
577 .fill_message_create = netdev_gre_fill_message_create,
578 .create_type = NETDEV_CREATE_STACKED,
579 .config_verify = netdev_tunnel_verify,
582 const NetDevVTable ip6gre_vtable = {
583 .object_size = sizeof(Tunnel),
585 .sections = "Match\0NetDev\0Tunnel\0",
586 .fill_message_create = netdev_ip6gre_fill_message_create,
587 .create_type = NETDEV_CREATE_STACKED,
588 .config_verify = netdev_tunnel_verify,
591 const NetDevVTable ip6gretap_vtable = {
592 .object_size = sizeof(Tunnel),
594 .sections = "Match\0NetDev\0Tunnel\0",
595 .fill_message_create = netdev_ip6gre_fill_message_create,
596 .create_type = NETDEV_CREATE_STACKED,
597 .config_verify = netdev_tunnel_verify,
600 const NetDevVTable ip6tnl_vtable = {
601 .object_size = sizeof(Tunnel),
603 .sections = "Match\0NetDev\0Tunnel\0",
604 .fill_message_create = netdev_ip6tnl_fill_message_create,
605 .create_type = NETDEV_CREATE_STACKED,
606 .config_verify = netdev_tunnel_verify,