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"
29 #include "networkd-netdev-tunnel.h"
30 #include "networkd-link.h"
33 #include "conf-parser.h"
35 #define DEFAULT_TNL_HOP_LIMIT 64
36 #define IP6_FLOWINFO_FLOWLABEL htonl(0x000FFFFF)
38 static const char* const ip6tnl_mode_table
[_NETDEV_IP6_TNL_MODE_MAX
] = {
39 [NETDEV_IP6_TNL_MODE_IP6IP6
] = "ip6ip6",
40 [NETDEV_IP6_TNL_MODE_IPIP6
] = "ipip6",
41 [NETDEV_IP6_TNL_MODE_ANYIP6
] = "any",
44 DEFINE_STRING_TABLE_LOOKUP(ip6tnl_mode
, Ip6TnlMode
);
45 DEFINE_CONFIG_PARSE_ENUM(config_parse_ip6tnl_mode
, ip6tnl_mode
, Ip6TnlMode
, "Failed to parse ip6 tunnel Mode");
47 static int netdev_ipip_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
48 Tunnel
*t
= IPIP(netdev
);
55 assert(t
->family
== AF_INET
);
57 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
59 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
61 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in
);
63 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
65 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in
);
67 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
69 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
71 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
73 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PMTUDISC
, t
->pmtudisc
);
75 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m");
80 static int netdev_sit_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
81 Tunnel
*t
= SIT(netdev
);
88 assert(t
->family
== AF_INET
);
90 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
92 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
94 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in
);
96 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
98 r
= sd_netlink_message_append_in_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in
);
100 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
102 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
104 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
106 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PMTUDISC
, t
->pmtudisc
);
108 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_PMTUDISC attribute: %m");
113 static int netdev_gre_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
119 if (netdev
->kind
== NETDEV_KIND_GRE
)
125 assert(t
->family
== AF_INET
);
129 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_LINK
, link
->ifindex
);
131 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LINK attribute: %m");
133 r
= sd_netlink_message_append_in_addr(m
, IFLA_GRE_LOCAL
, &t
->local
.in
);
135 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LOCAL attribute: %m");
137 r
= sd_netlink_message_append_in_addr(m
, IFLA_GRE_REMOTE
, &t
->remote
.in
);
139 log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_REMOTE attribute: %m");
141 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TTL
, t
->ttl
);
143 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TTL attribute: %m");
145 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TOS
, t
->tos
);
147 log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TOS attribute: %m");
149 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_PMTUDISC
, t
->pmtudisc
);
151 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_PMTUDISC attribute: %m");
156 static int netdev_ip6gre_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
162 if (netdev
->kind
== NETDEV_KIND_IP6GRE
)
165 t
= IP6GRETAP(netdev
);
168 assert(t
->family
== AF_INET6
);
172 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_LINK
, link
->ifindex
);
174 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LINK attribute: %m");
176 r
= sd_netlink_message_append_in6_addr(m
, IFLA_GRE_LOCAL
, &t
->local
.in6
);
178 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_LOCAL attribute: %m");
180 r
= sd_netlink_message_append_in6_addr(m
, IFLA_GRE_REMOTE
, &t
->remote
.in6
);
182 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_REMOTE attribute: %m");
184 r
= sd_netlink_message_append_u8(m
, IFLA_GRE_TTL
, t
->ttl
);
186 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_TTL attribute: %m");
188 if (t
->ipv6_flowlabel
!= _NETDEV_IPV6_FLOWLABEL_INVALID
) {
189 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_FLOWINFO
, t
->ipv6_flowlabel
);
191 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_FLOWINFO attribute: %m");
194 r
= sd_netlink_message_append_u32(m
, IFLA_GRE_FLAGS
, t
->flags
);
196 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_GRE_FLAGS attribute: %m");
201 static int netdev_vti_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
202 Tunnel
*t
= VTI(netdev
);
209 assert(t
->family
== AF_INET
);
211 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_LINK
, link
->ifindex
);
213 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
215 r
= sd_netlink_message_append_in_addr(m
, IFLA_VTI_LOCAL
, &t
->local
.in
);
217 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
219 r
= sd_netlink_message_append_in_addr(m
, IFLA_VTI_REMOTE
, &t
->remote
.in
);
221 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
226 static int netdev_vti6_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
227 Tunnel
*t
= VTI6(netdev
);
234 assert(t
->family
== AF_INET6
);
236 r
= sd_netlink_message_append_u32(m
, IFLA_VTI_LINK
, link
->ifindex
);
238 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
240 r
= sd_netlink_message_append_in6_addr(m
, IFLA_VTI_LOCAL
, &t
->local
.in6
);
242 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
244 r
= sd_netlink_message_append_in6_addr(m
, IFLA_VTI_REMOTE
, &t
->remote
.in6
);
246 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
251 static int netdev_ip6tnl_fill_message_create(NetDev
*netdev
, Link
*link
, sd_netlink_message
*m
) {
252 Tunnel
*t
= IP6TNL(netdev
);
260 assert(t
->family
== AF_INET6
);
262 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_LINK
, link
->ifindex
);
264 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LINK attribute: %m");
266 r
= sd_netlink_message_append_in6_addr(m
, IFLA_IPTUN_LOCAL
, &t
->local
.in6
);
268 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_LOCAL attribute: %m");
270 r
= sd_netlink_message_append_in6_addr(m
, IFLA_IPTUN_REMOTE
, &t
->remote
.in6
);
272 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_REMOTE attribute: %m");
274 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_TTL
, t
->ttl
);
276 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_TTL attribute: %m");
278 if (t
->ipv6_flowlabel
!= _NETDEV_IPV6_FLOWLABEL_INVALID
) {
279 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_FLOWINFO
, t
->ipv6_flowlabel
);
281 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_FLOWINFO attribute: %m");
285 t
->flags
|= IP6_TNL_F_RCV_DSCP_COPY
;
287 if (t
->encap_limit
!= IPV6_DEFAULT_TNL_ENCAP_LIMIT
) {
288 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_ENCAP_LIMIT
, t
->encap_limit
);
290 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_ENCAP_LIMIT attribute: %m");
293 r
= sd_netlink_message_append_u32(m
, IFLA_IPTUN_FLAGS
, t
->flags
);
295 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_FLAGS attribute: %m");
297 switch (t
->ip6tnl_mode
) {
298 case NETDEV_IP6_TNL_MODE_IP6IP6
:
299 proto
= IPPROTO_IPV6
;
301 case NETDEV_IP6_TNL_MODE_IPIP6
:
302 proto
= IPPROTO_IPIP
;
304 case NETDEV_IP6_TNL_MODE_ANYIP6
:
310 r
= sd_netlink_message_append_u8(m
, IFLA_IPTUN_PROTO
, proto
);
312 return log_netdev_error_errno(netdev
, r
, "Could not append IFLA_IPTUN_MODE attribute: %m");
317 static int netdev_tunnel_verify(NetDev
*netdev
, const char *filename
) {
323 switch (netdev
->kind
) {
324 case NETDEV_KIND_IPIP
:
327 case NETDEV_KIND_SIT
:
330 case NETDEV_KIND_GRE
:
333 case NETDEV_KIND_GRETAP
:
336 case NETDEV_KIND_IP6GRE
:
339 case NETDEV_KIND_IP6GRETAP
:
340 t
= IP6GRETAP(netdev
);
342 case NETDEV_KIND_VTI
:
345 case NETDEV_KIND_VTI6
:
348 case NETDEV_KIND_IP6TNL
:
352 assert_not_reached("Invalid tunnel kind");
357 if (t
->remote
.in
.s_addr
== INADDR_ANY
) {
358 log_warning("Tunnel without remote address configured in %s. Ignoring", filename
);
362 if (t
->family
!= AF_INET
&& t
->family
!= AF_INET6
) {
363 log_warning("Tunnel with invalid address family configured in %s. Ignoring", filename
);
367 if (netdev
->kind
== NETDEV_KIND_IP6TNL
) {
368 if (t
->ip6tnl_mode
== _NETDEV_IP6_TNL_MODE_INVALID
) {
369 log_warning("IP6 Tunnel without mode configured in %s. Ignoring", filename
);
377 int config_parse_tunnel_address(const char *unit
,
378 const char *filename
,
381 unsigned section_line
,
387 Tunnel
*t
= userdata
;
388 union in_addr_union
*addr
= data
, buffer
;
396 r
= in_addr_from_string_auto(rvalue
, &f
, &buffer
);
398 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Tunnel address is invalid, ignoring assignment: %s", rvalue
);
402 if (t
->family
!= AF_UNSPEC
&& t
->family
!= f
) {
403 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Tunnel addresses incompatible, ignoring assignment: %s", rvalue
);
413 int config_parse_ipv6_flowlabel(const char* unit
,
414 const char *filename
,
417 unsigned section_line
,
423 IPv6FlowLabel
*ipv6_flowlabel
= data
;
424 Tunnel
*t
= userdata
;
431 assert(ipv6_flowlabel
);
433 if (streq(rvalue
, "inherit")) {
434 *ipv6_flowlabel
= IP6_FLOWINFO_FLOWLABEL
;
435 t
->flags
|= IP6_TNL_F_USE_ORIG_FLOWLABEL
;
437 r
= config_parse_int(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &k
, userdata
);
442 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IPv6 flowlabel option, ignoring: %s", rvalue
);
444 *ipv6_flowlabel
= htonl(k
) & IP6_FLOWINFO_FLOWLABEL
;
445 t
->flags
&= ~IP6_TNL_F_USE_ORIG_FLOWLABEL
;
452 int config_parse_encap_limit(const char* unit
,
453 const char *filename
,
456 unsigned section_line
,
462 Tunnel
*t
= userdata
;
470 if (streq(rvalue
, "none"))
471 t
->flags
|= IP6_TNL_F_IGN_ENCAP_LIMIT
;
473 r
= safe_atoi(rvalue
, &k
);
475 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse Tunnel Encapsulation Limit option, ignoring: %s", rvalue
);
479 if (k
> 255 || k
< 0)
480 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid Tunnel Encapsulation value, ignoring: %d", k
);
483 t
->flags
&= ~IP6_TNL_F_IGN_ENCAP_LIMIT
;
490 static void ipip_init(NetDev
*n
) {
499 static void sit_init(NetDev
*n
) {
508 static void vti_init(NetDev
*n
) {
513 if (n
->kind
== NETDEV_KIND_VTI
)
523 static void gre_init(NetDev
*n
) {
528 if (n
->kind
== NETDEV_KIND_GRE
)
538 static void ip6gre_init(NetDev
*n
) {
543 if (n
->kind
== NETDEV_KIND_IP6GRE
)
550 t
->ttl
= DEFAULT_TNL_HOP_LIMIT
;
553 static void ip6tnl_init(NetDev
*n
) {
554 Tunnel
*t
= IP6TNL(n
);
559 t
->ttl
= DEFAULT_TNL_HOP_LIMIT
;
560 t
->encap_limit
= IPV6_DEFAULT_TNL_ENCAP_LIMIT
;
561 t
->ip6tnl_mode
= _NETDEV_IP6_TNL_MODE_INVALID
;
562 t
->ipv6_flowlabel
= _NETDEV_IPV6_FLOWLABEL_INVALID
;
565 const NetDevVTable ipip_vtable
= {
566 .object_size
= sizeof(Tunnel
),
568 .sections
= "Match\0NetDev\0Tunnel\0",
569 .fill_message_create
= netdev_ipip_fill_message_create
,
570 .create_type
= NETDEV_CREATE_STACKED
,
571 .config_verify
= netdev_tunnel_verify
,
574 const NetDevVTable sit_vtable
= {
575 .object_size
= sizeof(Tunnel
),
577 .sections
= "Match\0NetDev\0Tunnel\0",
578 .fill_message_create
= netdev_sit_fill_message_create
,
579 .create_type
= NETDEV_CREATE_STACKED
,
580 .config_verify
= netdev_tunnel_verify
,
583 const NetDevVTable vti_vtable
= {
584 .object_size
= sizeof(Tunnel
),
586 .sections
= "Match\0NetDev\0Tunnel\0",
587 .fill_message_create
= netdev_vti_fill_message_create
,
588 .create_type
= NETDEV_CREATE_STACKED
,
589 .config_verify
= netdev_tunnel_verify
,
592 const NetDevVTable vti6_vtable
= {
593 .object_size
= sizeof(Tunnel
),
595 .sections
= "Match\0NetDev\0Tunnel\0",
596 .fill_message_create
= netdev_vti6_fill_message_create
,
597 .create_type
= NETDEV_CREATE_STACKED
,
598 .config_verify
= netdev_tunnel_verify
,
601 const NetDevVTable gre_vtable
= {
602 .object_size
= sizeof(Tunnel
),
604 .sections
= "Match\0NetDev\0Tunnel\0",
605 .fill_message_create
= netdev_gre_fill_message_create
,
606 .create_type
= NETDEV_CREATE_STACKED
,
607 .config_verify
= netdev_tunnel_verify
,
610 const NetDevVTable gretap_vtable
= {
611 .object_size
= sizeof(Tunnel
),
613 .sections
= "Match\0NetDev\0Tunnel\0",
614 .fill_message_create
= netdev_gre_fill_message_create
,
615 .create_type
= NETDEV_CREATE_STACKED
,
616 .config_verify
= netdev_tunnel_verify
,
619 const NetDevVTable ip6gre_vtable
= {
620 .object_size
= sizeof(Tunnel
),
622 .sections
= "Match\0NetDev\0Tunnel\0",
623 .fill_message_create
= netdev_ip6gre_fill_message_create
,
624 .create_type
= NETDEV_CREATE_STACKED
,
625 .config_verify
= netdev_tunnel_verify
,
628 const NetDevVTable ip6gretap_vtable
= {
629 .object_size
= sizeof(Tunnel
),
631 .sections
= "Match\0NetDev\0Tunnel\0",
632 .fill_message_create
= netdev_ip6gre_fill_message_create
,
633 .create_type
= NETDEV_CREATE_STACKED
,
634 .config_verify
= netdev_tunnel_verify
,
637 const NetDevVTable ip6tnl_vtable
= {
638 .object_size
= sizeof(Tunnel
),
640 .sections
= "Match\0NetDev\0Tunnel\0",
641 .fill_message_create
= netdev_ip6tnl_fill_message_create
,
642 .create_type
= NETDEV_CREATE_STACKED
,
643 .config_verify
= netdev_tunnel_verify
,