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 <arpa/inet.h>
25 #include <linux/if_tunnel.h>
26 #include <linux/ip6_tunnel.h>
28 #include "sd-netlink.h"
30 #include "conf-parser.h"
32 #include "networkd-link.h"
33 #include "string-util.h"
35 #include "networkd-netdev-tunnel.h"
37 #define DEFAULT_TNL_HOP_LIMIT 64
38 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
40 static const char* const ip6tnl_mode_table
[_NETDEV_IP6_TNL_MODE_MAX
] = {
41 [NETDEV_IP6_TNL_MODE_IP6IP6
] = "ip6ip6",
42 [NETDEV_IP6_TNL_MODE_IPIP6
] = "ipip6",
43 [NETDEV_IP6_TNL_MODE_ANYIP6
] = "any",
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");
49 static int netdev_ipip_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
50 Tunnel
*t
= IPIP(netdev
);
57 assert(t
->family
== AF_INET
);
59 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
61 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
63 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in
);
65 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
67 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in
);
69 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
71 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
73 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
75 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PMTUDISC
, t
->pmtudisc
);
77 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m");
82 static int netdev_sit_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
83 Tunnel
*t
= SIT(netdev
);
90 assert(t
->family
== AF_INET
);
92 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
94 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
96 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in
);
98 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
100 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in
);
102 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
104 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
106 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
108 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PMTUDISC
, t
->pmtudisc
);
110 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m");
115 static int netdev_gre_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
121 if (netdev
->kind
== NETDEV_KIND_GRE
)
127 assert(t
->family
== AF_INET
);
131 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_LINK
, link
->ifindex
);
133 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LINK attribute: %m");
135 r
= sd_netlink_message_append_in_addr(m
, IFLA_GRE_LOCAL
, &t
->local
.in
);
137 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LOCAL attribute: %m");
139 r
= sd_netlink_message_append_in_addr(m
, IFLA_GRE_REMOTE
, &t
->remote
.in
);
141 log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_REMOTE attribute: %m");
143 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TTL
, t
->ttl
);
145 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TTL attribute: %m");
147 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TOS
, t
->tos
);
149 log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TOS attribute: %m");
151 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_PMTUDISC
, t
->pmtudisc
);
153 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_PMTUDISC attribute: %m");
158 static int netdev_ip6gre_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
164 if (netdev
->kind
== NETDEV_KIND_IP6GRE
)
167 t
= IP6GRETAP(netdev
);
170 assert(t
->family
== AF_INET6
);
174 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_LINK
, link
->ifindex
);
176 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LINK attribute: %m");
178 r
= sd_netlink_message_append_in6_addr(m
, IFLA_GRE_LOCAL
, &t
->local
.in6
);
180 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LOCAL attribute: %m");
182 r
= sd_netlink_message_append_in6_addr(m
, IFLA_GRE_REMOTE
, &t
->remote
.in6
);
184 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_REMOTE attribute: %m");
186 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TTL
, t
->ttl
);
188 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TTL attribute: %m");
190 if (t
->ipv6_flowlabel
!= _NETDEV_IPV6_FLOWLABEL_INVALID
) {
191 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_FLOWINFO
, t
->ipv6_flowlabel
);
193 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_FLOWINFO attribute: %m");
196 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_FLAGS
, t
->flags
);
198 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_FLAGS attribute: %m");
203 static int netdev_vti_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
204 Tunnel
*t
= VTI(netdev
);
211 assert(t
->family
== AF_INET
);
213 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_LINK
, link
->ifindex
);
215 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
217 r
= sd_netlink_message_append_in_addr(m
, IFLA_VTI_LOCAL
, &t
->local
.in
);
219 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
221 r
= sd_netlink_message_append_in_addr(m
, IFLA_VTI_REMOTE
, &t
->remote
.in
);
223 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
228 static int netdev_vti6_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
229 Tunnel
*t
= VTI6(netdev
);
236 assert(t
->family
== AF_INET6
);
238 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_LINK
, link
->ifindex
);
240 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
242 r
= sd_netlink_message_append_in6_addr(m
, IFLA_VTI_LOCAL
, &t
->local
.in6
);
244 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
246 r
= sd_netlink_message_append_in6_addr(m
, IFLA_VTI_REMOTE
, &t
->remote
.in6
);
248 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
253 static int netdev_ip6tnl_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
254 Tunnel
*t
= IP6TNL(netdev
);
262 assert(t
->family
== AF_INET6
);
264 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
266 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
268 r
= sd_netlink_message_append_in6_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in6
);
270 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
272 r
= sd_netlink_message_append_in6_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in6
);
274 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
276 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
278 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
280 if (t
->ipv6_flowlabel
!= _NETDEV_IPV6_FLOWLABEL_INVALID
) {
281 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_FLOWINFO
, t
->ipv6_flowlabel
);
283 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_FLOWINFO attribute: %m");
287 t
->flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
289 if (t
->encap_limit
!= IPV6_DEFAULT_TNL_ENCAP_LIMIT
) {
290 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_ENCAP_LIMIT
, t
->encap_limit
);
292 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_ENCAP_LIMIT attribute: %m");
295 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_FLAGS
, t
->flags
);
297 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_FLAGS attribute: %m");
299 switch (t
->ip6tnl_mode
) {
300 case NETDEV_IP6_TNL_MODE_IP6IP6
:
301 proto
= IPPROTO_IPV6
;
303 case NETDEV_IP6_TNL_MODE_IPIP6
:
304 proto
= IPPROTO_IPIP
;
306 case NETDEV_IP6_TNL_MODE_ANYIP6
:
312 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PROTO
, proto
);
314 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_MODE attribute: %m");
319 static int netdev_tunnel_verify(NetDev
*netdev
, const char *filename
) {
325 switch (netdev
->kind
) {
326 case NETDEV_KIND_IPIP
:
329 case NETDEV_KIND_SIT
:
332 case NETDEV_KIND_GRE
:
335 case NETDEV_KIND_GRETAP
:
338 case NETDEV_KIND_IP6GRE
:
341 case NETDEV_KIND_IP6GRETAP
:
342 t
= IP6GRETAP(netdev
);
344 case NETDEV_KIND_VTI
:
347 case NETDEV_KIND_VTI6
:
350 case NETDEV_KIND_IP6TNL
:
354 assert_not_reached("Invalid tunnel kind");
359 if (t
->remote
.in
.s_addr
== INADDR_ANY
) {
360 log_warning("Tunnel without remote address configured in %s. Ignoring", filename
);
364 if (t
->family
!= AF_INET
&& t
->family
!= AF_INET6
) {
365 log_warning("Tunnel with invalid address family configured in %s. Ignoring", filename
);
369 if (netdev
->kind
== NETDEV_KIND_IP6TNL
) {
370 if (t
->ip6tnl_mode
== _NETDEV_IP6_TNL_MODE_INVALID
) {
371 log_warning("IP6 Tunnel without mode configured in %s. Ignoring", filename
);
379 int config_parse_tunnel_address(const char *unit
,
380 const char *filename
,
383 unsigned section_line
,
389 Tunnel
*t
= userdata
;
390 union in_addr_union
*addr
= data
, buffer
;
398 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
400 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Tunnel address is invalid, ignoring assignment: %s", rvalue
);
404 if (t
->family
!= AF_UNSPEC
&& t
->family
!= f
) {
405 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Tunnel addresses incompatible, ignoring assignment: %s", rvalue
);
415 int config_parse_ipv6_flowlabel(const char* unit
,
416 const char *filename
,
419 unsigned section_line
,
425 IPv6FlowLabel
*ipv6_flowlabel
= data
;
426 Tunnel
*t
= userdata
;
433 assert(ipv6_flowlabel
);
435 if (streq(rvalue
, "inherit")) {
436 *ipv6_flowlabel
= IP6_FLOWINFO_FLOWLABEL
;
437 t
->flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
439 r
= config_parse_int(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &k
, userdata
);
444 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IPv6 flowlabel option, ignoring: %s", rvalue
);
446 *ipv6_flowlabel
= htonl(k
) & IP6_FLOWINFO_FLOWLABEL
;
447 t
->flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
454 int config_parse_encap_limit(const char* unit
,
455 const char *filename
,
458 unsigned section_line
,
464 Tunnel
*t
= userdata
;
472 if (streq(rvalue
, "none"))
473 t
->flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
475 r
= safe_atoi(rvalue
, &k
);
477 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse Tunnel Encapsulation Limit option, ignoring: %s", rvalue
);
481 if (k
> 255 || k
< 0)
482 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid Tunnel Encapsulation value, ignoring: %d", k
);
485 t
->flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
492 static void ipip_init(NetDev
*n
) {
501 static void sit_init(NetDev
*n
) {
510 static void vti_init(NetDev
*n
) {
515 if (n
->kind
== NETDEV_KIND_VTI
)
525 static void gre_init(NetDev
*n
) {
530 if (n
->kind
== NETDEV_KIND_GRE
)
540 static void ip6gre_init(NetDev
*n
) {
545 if (n
->kind
== NETDEV_KIND_IP6GRE
)
552 t
->ttl
= DEFAULT_TNL_HOP_LIMIT
;
555 static void ip6tnl_init(NetDev
*n
) {
556 Tunnel
*t
= IP6TNL(n
);
561 t
->ttl
= DEFAULT_TNL_HOP_LIMIT
;
562 t
->encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
563 t
->ip6tnl_mode
= _NETDEV_IP6_TNL_MODE_INVALID
;
564 t
->ipv6_flowlabel
= _NETDEV_IPV6_FLOWLABEL_INVALID
;
567 const NetDevVTable ipip_vtable
= {
568 .object_size
= sizeof(Tunnel
),
570 .sections
= "Match\0NetDev\0Tunnel\0",
571 .fill_message_create
= netdev_ipip_fill_message_create
,
572 .create_type
= NETDEV_CREATE_STACKED
,
573 .config_verify
= netdev_tunnel_verify
,
576 const NetDevVTable sit_vtable
= {
577 .object_size
= sizeof(Tunnel
),
579 .sections
= "Match\0NetDev\0Tunnel\0",
580 .fill_message_create
= netdev_sit_fill_message_create
,
581 .create_type
= NETDEV_CREATE_STACKED
,
582 .config_verify
= netdev_tunnel_verify
,
585 const NetDevVTable vti_vtable
= {
586 .object_size
= sizeof(Tunnel
),
588 .sections
= "Match\0NetDev\0Tunnel\0",
589 .fill_message_create
= netdev_vti_fill_message_create
,
590 .create_type
= NETDEV_CREATE_STACKED
,
591 .config_verify
= netdev_tunnel_verify
,
594 const NetDevVTable vti6_vtable
= {
595 .object_size
= sizeof(Tunnel
),
597 .sections
= "Match\0NetDev\0Tunnel\0",
598 .fill_message_create
= netdev_vti6_fill_message_create
,
599 .create_type
= NETDEV_CREATE_STACKED
,
600 .config_verify
= netdev_tunnel_verify
,
603 const NetDevVTable gre_vtable
= {
604 .object_size
= sizeof(Tunnel
),
606 .sections
= "Match\0NetDev\0Tunnel\0",
607 .fill_message_create
= netdev_gre_fill_message_create
,
608 .create_type
= NETDEV_CREATE_STACKED
,
609 .config_verify
= netdev_tunnel_verify
,
612 const NetDevVTable gretap_vtable
= {
613 .object_size
= sizeof(Tunnel
),
615 .sections
= "Match\0NetDev\0Tunnel\0",
616 .fill_message_create
= netdev_gre_fill_message_create
,
617 .create_type
= NETDEV_CREATE_STACKED
,
618 .config_verify
= netdev_tunnel_verify
,
621 const NetDevVTable ip6gre_vtable
= {
622 .object_size
= sizeof(Tunnel
),
624 .sections
= "Match\0NetDev\0Tunnel\0",
625 .fill_message_create
= netdev_ip6gre_fill_message_create
,
626 .create_type
= NETDEV_CREATE_STACKED
,
627 .config_verify
= netdev_tunnel_verify
,
630 const NetDevVTable ip6gretap_vtable
= {
631 .object_size
= sizeof(Tunnel
),
633 .sections
= "Match\0NetDev\0Tunnel\0",
634 .fill_message_create
= netdev_ip6gre_fill_message_create
,
635 .create_type
= NETDEV_CREATE_STACKED
,
636 .config_verify
= netdev_tunnel_verify
,
639 const NetDevVTable ip6tnl_vtable
= {
640 .object_size
= sizeof(Tunnel
),
642 .sections
= "Match\0NetDev\0Tunnel\0",
643 .fill_message_create
= netdev_ip6tnl_fill_message_create
,
644 .create_type
= NETDEV_CREATE_STACKED
,
645 .config_verify
= netdev_tunnel_verify
,