1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Susant Sahani <susant@redhat.com>
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>
29 #include "networkd-netdev-tunnel.h"
30 #include "network-internal.h"
33 #include "conf-parser.h"
35 static int netdev_ipip_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
36 Tunnel *t = IPIP(netdev);
43 assert(t->family == AF_INET);
45 r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
47 log_error_netdev(netdev,
48 "Could not append IFLA_IPTUN_LINK attribute: %s",
53 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &t->local.in);
55 log_error_netdev(netdev,
56 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
61 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in);
63 log_error_netdev(netdev,
64 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
69 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
71 log_error_netdev(netdev,
72 "Could not append IFLA_IPTUN_TTL attribute: %s",
80 static int netdev_sit_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
81 Tunnel *t = SIT(netdev);
88 assert(t->family == AF_INET);
90 r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
92 log_error_netdev(netdev,
93 "Could not append IFLA_IPTUN_LINK attribute: %s",
98 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &t->local.in);
100 log_error_netdev(netdev,
101 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
106 r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in);
108 log_error_netdev(netdev,
109 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
114 r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
116 log_error_netdev(netdev,
117 "Could not append IFLA_IPTUN_TTL attribute: %s",
125 static int netdev_gre_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
126 Tunnel *t = GRE(netdev);
133 assert(t->family == AF_INET);
135 r = sd_rtnl_message_append_u32(m, IFLA_GRE_LINK, link->ifindex);
137 log_error_netdev(netdev,
138 "Could not append IFLA_GRE_LINK attribute: %s",
143 r = sd_rtnl_message_append_in_addr(m, IFLA_GRE_LOCAL, &t->local.in);
145 log_error_netdev(netdev,
146 "Could not append IFLA_GRE_LOCAL attribute: %s",
151 r = sd_rtnl_message_append_in_addr(m, IFLA_GRE_REMOTE, &t->remote.in);
153 log_error_netdev(netdev,
154 "Could not append IFLA_GRE_REMOTE attribute: %s",
159 r = sd_rtnl_message_append_u8(m, IFLA_GRE_TTL, t->ttl);
161 log_error_netdev(netdev,
162 "Could not append IFLA_GRE_TTL attribute: %s",
167 r = sd_rtnl_message_append_u8(m, IFLA_GRE_TOS, t->tos);
169 log_error_netdev(netdev,
170 "Could not append IFLA_GRE_TOS attribute: %s",
178 static int netdev_vti_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
179 Tunnel *t = VTI(netdev);
186 assert(t->family == AF_INET);
188 r = sd_rtnl_message_append_u32(m, IFLA_VTI_LINK, link->ifindex);
190 log_error_netdev(netdev,
191 "Could not append IFLA_IPTUN_LINK attribute: %s",
196 r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_LOCAL, &t->local.in);
198 log_error_netdev(netdev,
199 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
204 r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_REMOTE, &t->remote.in);
206 log_error_netdev(netdev,
207 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
215 static int netdev_tunnel_verify(NetDev *netdev, const char *filename) {
221 switch (netdev->kind) {
222 case NETDEV_KIND_IPIP:
225 case NETDEV_KIND_SIT:
228 case NETDEV_KIND_GRE:
231 case NETDEV_KIND_VTI:
235 assert_not_reached("Invalid tunnel kind");
240 if (t->local.in.s_addr == INADDR_ANY) {
241 log_warning("Tunnel without local address configured in %s. Ignoring", filename);
245 if (t->remote.in.s_addr == INADDR_ANY) {
246 log_warning("Tunnel without remote address configured in %s. Ignoring", filename);
250 if (t->family != AF_INET) {
251 log_warning("Tunnel with invalid address family configured in %s. Ignoring", filename);
258 int config_parse_tunnel_address(const char *unit,
259 const char *filename,
262 unsigned section_line,
268 Tunnel *t = userdata;
269 union in_addr_union *addr = data;
277 r = net_parse_inaddr(rvalue, &t->family, addr);
279 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
280 "Tunnel address is invalid, ignoring assignment: %s", rvalue);
287 static void ipip_init(NetDev *n) {
296 static void sit_init(NetDev *n) {
305 static void vti_init(NetDev *n) {
314 static void gre_init(NetDev *n) {
323 const NetDevVTable ipip_vtable = {
324 .object_size = sizeof(Tunnel),
326 .sections = "Match\0NetDev\0Tunnel\0",
327 .fill_message_create = netdev_ipip_fill_message_create,
328 .create_type = NETDEV_CREATE_STACKED,
329 .config_verify = netdev_tunnel_verify,
332 const NetDevVTable sit_vtable = {
333 .object_size = sizeof(Tunnel),
335 .sections = "Match\0NetDev\0Tunnel\0",
336 .fill_message_create = netdev_sit_fill_message_create,
337 .create_type = NETDEV_CREATE_STACKED,
338 .config_verify = netdev_tunnel_verify,
341 const NetDevVTable vti_vtable = {
342 .object_size = sizeof(Tunnel),
344 .sections = "Match\0NetDev\0Tunnel\0",
345 .fill_message_create = netdev_vti_fill_message_create,
346 .create_type = NETDEV_CREATE_STACKED,
347 .config_verify = netdev_tunnel_verify,
350 const NetDevVTable gre_vtable = {
351 .object_size = sizeof(Tunnel),
353 .sections = "Match\0NetDev\0Tunnel\0",
354 .fill_message_create = netdev_gre_fill_message_create,
355 .create_type = NETDEV_CREATE_STACKED,
356 .config_verify = netdev_tunnel_verify,