1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Susant Sahani
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <arpa/inet.h>
24 #include <linux/if_tunnel.h>
25 #include <linux/ip6_tunnel.h>
27 #include "sd-netlink.h"
29 #include "conf-parser.h"
31 #include "networkd-link.h"
32 #include "netdev/tunnel.h"
33 #include "parse-util.h"
34 #include "string-table.h"
35 #include "string-util.h"
38 #define DEFAULT_TNL_HOP_LIMIT 64
39 #define IP6_FLOWINFO_FLOWLABEL htobe32(0x000FFFFF)
40 #define IP6_TNL_F_ALLOW_LOCAL_REMOTE 0x40
42 static const char* const ip6tnl_mode_table
[_NETDEV_IP6_TNL_MODE_MAX
] = {
43 [NETDEV_IP6_TNL_MODE_IP6IP6
] = "ip6ip6",
44 [NETDEV_IP6_TNL_MODE_IPIP6
] = "ipip6",
45 [NETDEV_IP6_TNL_MODE_ANYIP6
] = "any",
48 DEFINE_STRING_TABLE_LOOKUP(ip6tnl_mode
, Ip6TnlMode
);
49 DEFINE_CONFIG_PARSE_ENUM(config_parse_ip6tnl_mode
, ip6tnl_mode
, Ip6TnlMode
, "Failed to parse ip6 tunnel Mode");
51 static int netdev_ipip_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
52 Tunnel
*t
= IPIP(netdev
);
58 assert(IN_SET(t
->family
, AF_INET
, AF_UNSPEC
));
61 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
63 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
67 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in
);
69 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
71 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in
);
73 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
75 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
77 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
79 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PMTUDISC
, t
->pmtudisc
);
81 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m");
86 static int netdev_sit_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
87 Tunnel
*t
= SIT(netdev
);
93 assert(IN_SET(t
->family
, AF_INET
, AF_UNSPEC
));
96 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
98 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
102 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in
);
104 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
106 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in
);
108 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
110 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
112 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
114 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PMTUDISC
, t
->pmtudisc
);
116 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m");
121 static int netdev_gre_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
127 if (netdev
->kind
== NETDEV_KIND_GRE
)
133 assert(IN_SET(t
->family
, AF_INET
, AF_UNSPEC
));
137 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_LINK
, link
->ifindex
);
139 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LINK attribute: %m");
142 r
= sd_netlink_message_append_in_addr(m
, IFLA_GRE_LOCAL
, &t
->local
.in
);
144 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LOCAL attribute: %m");
146 r
= sd_netlink_message_append_in_addr(m
, IFLA_GRE_REMOTE
, &t
->remote
.in
);
148 log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_REMOTE attribute: %m");
150 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TTL
, t
->ttl
);
152 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TTL attribute: %m");
154 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TOS
, t
->tos
);
156 log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TOS attribute: %m");
158 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_PMTUDISC
, t
->pmtudisc
);
160 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_PMTUDISC attribute: %m");
165 static int netdev_ip6gre_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
171 if (netdev
->kind
== NETDEV_KIND_IP6GRE
)
174 t
= IP6GRETAP(netdev
);
177 assert(t
->family
== AF_INET6
);
181 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_LINK
, link
->ifindex
);
183 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LINK attribute: %m");
186 r
= sd_netlink_message_append_in6_addr(m
, IFLA_GRE_LOCAL
, &t
->local
.in6
);
188 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LOCAL attribute: %m");
190 r
= sd_netlink_message_append_in6_addr(m
, IFLA_GRE_REMOTE
, &t
->remote
.in6
);
192 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_REMOTE attribute: %m");
194 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TTL
, t
->ttl
);
196 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TTL attribute: %m");
198 if (t
->ipv6_flowlabel
!= _NETDEV_IPV6_FLOWLABEL_INVALID
) {
199 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_FLOWINFO
, t
->ipv6_flowlabel
);
201 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_FLOWINFO attribute: %m");
204 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_FLAGS
, t
->flags
);
206 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_FLAGS attribute: %m");
211 static int netdev_vti_fill_message_key(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
218 if (netdev
->kind
== NETDEV_KIND_VTI
)
226 ikey
= okey
= htobe32(t
->key
);
228 ikey
= htobe32(t
->ikey
);
229 okey
= htobe32(t
->okey
);
232 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_IKEY
, ikey
);
234 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_VTI_IKEY attribute: %m");
236 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_OKEY
, okey
);
238 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_VTI_OKEY attribute: %m");
243 static int netdev_vti_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
244 Tunnel
*t
= VTI(netdev
);
250 assert(t
->family
== AF_INET
);
253 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_LINK
, link
->ifindex
);
255 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
258 r
= netdev_vti_fill_message_key(netdev
, link
, m
);
262 r
= sd_netlink_message_append_in_addr(m
, IFLA_VTI_LOCAL
, &t
->local
.in
);
264 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
266 r
= sd_netlink_message_append_in_addr(m
, IFLA_VTI_REMOTE
, &t
->remote
.in
);
268 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
273 static int netdev_vti6_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
274 Tunnel
*t
= VTI6(netdev
);
280 assert(t
->family
== AF_INET6
);
283 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_LINK
, link
->ifindex
);
285 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
288 r
= netdev_vti_fill_message_key(netdev
, link
, m
);
292 r
= sd_netlink_message_append_in6_addr(m
, IFLA_VTI_LOCAL
, &t
->local
.in6
);
294 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
296 r
= sd_netlink_message_append_in6_addr(m
, IFLA_VTI_REMOTE
, &t
->remote
.in6
);
298 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
303 static int netdev_ip6tnl_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
304 Tunnel
*t
= IP6TNL(netdev
);
311 assert(t
->family
== AF_INET6
);
314 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
316 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
319 r
= sd_netlink_message_append_in6_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in6
);
321 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
323 r
= sd_netlink_message_append_in6_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in6
);
325 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
327 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
329 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
331 if (t
->ipv6_flowlabel
!= _NETDEV_IPV6_FLOWLABEL_INVALID
) {
332 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_FLOWINFO
, t
->ipv6_flowlabel
);
334 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_FLOWINFO attribute: %m");
338 t
->flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
340 if (t
->allow_localremote
!= -1)
341 SET_FLAG(t
->flags
, IP6_TNL_F_ALLOW_LOCAL_REMOTE
, t
->allow_localremote
);
343 if (t
->encap_limit
!= IPV6_DEFAULT_TNL_ENCAP_LIMIT
) {
344 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_ENCAP_LIMIT
, t
->encap_limit
);
346 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_ENCAP_LIMIT attribute: %m");
349 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_FLAGS
, t
->flags
);
351 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_FLAGS attribute: %m");
353 switch (t
->ip6tnl_mode
) {
354 case NETDEV_IP6_TNL_MODE_IP6IP6
:
355 proto
= IPPROTO_IPV6
;
357 case NETDEV_IP6_TNL_MODE_IPIP6
:
358 proto
= IPPROTO_IPIP
;
360 case NETDEV_IP6_TNL_MODE_ANYIP6
:
366 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PROTO
, proto
);
368 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_MODE attribute: %m");
373 static int netdev_tunnel_verify(NetDev
*netdev
, const char *filename
) {
379 switch (netdev
->kind
) {
380 case NETDEV_KIND_IPIP
:
383 case NETDEV_KIND_SIT
:
386 case NETDEV_KIND_GRE
:
389 case NETDEV_KIND_GRETAP
:
392 case NETDEV_KIND_IP6GRE
:
395 case NETDEV_KIND_IP6GRETAP
:
396 t
= IP6GRETAP(netdev
);
398 case NETDEV_KIND_VTI
:
401 case NETDEV_KIND_VTI6
:
404 case NETDEV_KIND_IP6TNL
:
408 assert_not_reached("Invalid tunnel kind");
413 if (!IN_SET(t
->family
, AF_INET
, AF_INET6
, AF_UNSPEC
)) {
414 log_netdev_error(netdev
,
415 "Tunnel with invalid address family configured in %s. Ignoring", filename
);
419 if (netdev
->kind
== NETDEV_KIND_VTI
&&
420 (t
->family
!= AF_INET
|| in_addr_is_null(t
->family
, &t
->local
))) {
421 log_netdev_error(netdev
,
422 "vti tunnel without a local IPv4 address configured in %s. Ignoring", filename
);
426 if (IN_SET(netdev
->kind
, NETDEV_KIND_VTI6
, NETDEV_KIND_IP6TNL
, NETDEV_KIND_IP6GRE
) &&
427 (t
->family
!= AF_INET6
|| in_addr_is_null(t
->family
, &t
->local
))) {
428 log_netdev_error(netdev
,
429 "vti6/ip6tnl/ip6gre tunnel without a local IPv6 address configured in %s. Ignoring", filename
);
433 if (netdev
->kind
== NETDEV_KIND_IP6TNL
&&
434 t
->ip6tnl_mode
== _NETDEV_IP6_TNL_MODE_INVALID
) {
435 log_netdev_error(netdev
,
436 "ip6tnl without mode configured in %s. Ignoring", filename
);
443 int config_parse_tunnel_address(const char *unit
,
444 const char *filename
,
447 unsigned section_line
,
453 Tunnel
*t
= userdata
;
454 union in_addr_union
*addr
= data
, buffer
;
462 /* This is used to parse addresses on both local and remote ends of the tunnel.
463 * Address families must match.
465 * "any" is a special value which means that the address is unspecified.
468 if (streq(rvalue
, "any")) {
469 *addr
= IN_ADDR_NULL
;
471 /* As a special case, if both the local and remote addresses are
472 * unspecified, also clear the address family.
474 if (t
->family
!= AF_UNSPEC
&&
475 in_addr_is_null(t
->family
, &t
->local
) &&
476 in_addr_is_null(t
->family
, &t
->remote
))
477 t
->family
= AF_UNSPEC
;
481 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
483 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
484 "Tunnel address \"%s\" invalid, ignoring assignment: %m", rvalue
);
488 if (t
->family
!= AF_UNSPEC
&& t
->family
!= f
) {
489 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
490 "Tunnel addresses incompatible, ignoring assignment: %s", rvalue
);
499 int config_parse_tunnel_key(const char *unit
,
500 const char *filename
,
503 unsigned section_line
,
509 union in_addr_union buffer
;
510 Tunnel
*t
= userdata
;
519 r
= in_addr_from_string(AF_INET
, rvalue
, &buffer
);
521 r
= safe_atou32(rvalue
, &k
);
523 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse tunnel key ignoring assignment: %s", rvalue
);
527 k
= be32toh(buffer
.in
.s_addr
);
529 if (streq(lvalue
, "Key"))
531 else if (streq(lvalue
, "InputKey"))
539 int config_parse_ipv6_flowlabel(const char* unit
,
540 const char *filename
,
543 unsigned section_line
,
549 IPv6FlowLabel
*ipv6_flowlabel
= data
;
550 Tunnel
*t
= userdata
;
557 assert(ipv6_flowlabel
);
559 if (streq(rvalue
, "inherit")) {
560 *ipv6_flowlabel
= IP6_FLOWINFO_FLOWLABEL
;
561 t
->flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
563 r
= config_parse_int(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &k
, userdata
);
568 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IPv6 flowlabel option, ignoring: %s", rvalue
);
570 *ipv6_flowlabel
= htobe32(k
) & IP6_FLOWINFO_FLOWLABEL
;
571 t
->flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
578 int config_parse_encap_limit(const char* unit
,
579 const char *filename
,
582 unsigned section_line
,
588 Tunnel
*t
= userdata
;
596 if (streq(rvalue
, "none"))
597 t
->flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
599 r
= safe_atoi(rvalue
, &k
);
601 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse Tunnel Encapsulation Limit option, ignoring: %s", rvalue
);
605 if (k
> 255 || k
< 0)
606 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid Tunnel Encapsulation value, ignoring: %d", k
);
609 t
->flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
616 static void ipip_init(NetDev
*n
) {
625 static void sit_init(NetDev
*n
) {
634 static void vti_init(NetDev
*n
) {
639 if (n
->kind
== NETDEV_KIND_VTI
)
649 static void gre_init(NetDev
*n
) {
654 if (n
->kind
== NETDEV_KIND_GRE
)
664 static void ip6gre_init(NetDev
*n
) {
669 if (n
->kind
== NETDEV_KIND_IP6GRE
)
676 t
->ttl
= DEFAULT_TNL_HOP_LIMIT
;
679 static void ip6tnl_init(NetDev
*n
) {
680 Tunnel
*t
= IP6TNL(n
);
685 t
->ttl
= DEFAULT_TNL_HOP_LIMIT
;
686 t
->encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
687 t
->ip6tnl_mode
= _NETDEV_IP6_TNL_MODE_INVALID
;
688 t
->ipv6_flowlabel
= _NETDEV_IPV6_FLOWLABEL_INVALID
;
689 t
->allow_localremote
= -1;
692 const NetDevVTable ipip_vtable
= {
693 .object_size
= sizeof(Tunnel
),
695 .sections
= "Match\0NetDev\0Tunnel\0",
696 .fill_message_create
= netdev_ipip_fill_message_create
,
697 .create_type
= NETDEV_CREATE_STACKED
,
698 .config_verify
= netdev_tunnel_verify
,
701 const NetDevVTable sit_vtable
= {
702 .object_size
= sizeof(Tunnel
),
704 .sections
= "Match\0NetDev\0Tunnel\0",
705 .fill_message_create
= netdev_sit_fill_message_create
,
706 .create_type
= NETDEV_CREATE_STACKED
,
707 .config_verify
= netdev_tunnel_verify
,
710 const NetDevVTable vti_vtable
= {
711 .object_size
= sizeof(Tunnel
),
713 .sections
= "Match\0NetDev\0Tunnel\0",
714 .fill_message_create
= netdev_vti_fill_message_create
,
715 .create_type
= NETDEV_CREATE_STACKED
,
716 .config_verify
= netdev_tunnel_verify
,
719 const NetDevVTable vti6_vtable
= {
720 .object_size
= sizeof(Tunnel
),
722 .sections
= "Match\0NetDev\0Tunnel\0",
723 .fill_message_create
= netdev_vti6_fill_message_create
,
724 .create_type
= NETDEV_CREATE_STACKED
,
725 .config_verify
= netdev_tunnel_verify
,
728 const NetDevVTable gre_vtable
= {
729 .object_size
= sizeof(Tunnel
),
731 .sections
= "Match\0NetDev\0Tunnel\0",
732 .fill_message_create
= netdev_gre_fill_message_create
,
733 .create_type
= NETDEV_CREATE_STACKED
,
734 .config_verify
= netdev_tunnel_verify
,
737 const NetDevVTable gretap_vtable
= {
738 .object_size
= sizeof(Tunnel
),
740 .sections
= "Match\0NetDev\0Tunnel\0",
741 .fill_message_create
= netdev_gre_fill_message_create
,
742 .create_type
= NETDEV_CREATE_STACKED
,
743 .config_verify
= netdev_tunnel_verify
,
746 const NetDevVTable ip6gre_vtable
= {
747 .object_size
= sizeof(Tunnel
),
749 .sections
= "Match\0NetDev\0Tunnel\0",
750 .fill_message_create
= netdev_ip6gre_fill_message_create
,
751 .create_type
= NETDEV_CREATE_STACKED
,
752 .config_verify
= netdev_tunnel_verify
,
755 const NetDevVTable ip6gretap_vtable
= {
756 .object_size
= sizeof(Tunnel
),
758 .sections
= "Match\0NetDev\0Tunnel\0",
759 .fill_message_create
= netdev_ip6gre_fill_message_create
,
760 .create_type
= NETDEV_CREATE_STACKED
,
761 .config_verify
= netdev_tunnel_verify
,
764 const NetDevVTable ip6tnl_vtable
= {
765 .object_size
= sizeof(Tunnel
),
767 .sections
= "Match\0NetDev\0Tunnel\0",
768 .fill_message_create
= netdev_ip6tnl_fill_message_create
,
769 .create_type
= NETDEV_CREATE_STACKED
,
770 .config_verify
= netdev_tunnel_verify
,