chiark / gitweb /
3eca3cc547310583bbf9d8ffcd69352ca648d4d9
[elogind.git] / src / network / networkd-route.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Tom Gundersen <teg@jklm.no>
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <net/if.h>
23
24 #include "networkd.h"
25
26 #include "utf8.h"
27 #include "util.h"
28 #include "conf-parser.h"
29 #include "net-util.h"
30
31 int route_new(Network *network, Route **ret) {
32         _cleanup_route_free_ Route *route = NULL;
33
34         route = new0(Route, 1);
35         if (!route)
36                 return -ENOMEM;
37
38         route->network = network;
39
40         LIST_PREPEND(routes, network->routes, route);
41
42         *ret = route;
43         route = NULL;
44
45         return 0;
46 }
47
48 void route_free(Route *route) {
49         if (!route)
50                 return;
51
52         LIST_REMOVE(routes, route->network->routes, route);
53
54         free(route);
55 }
56
57 int route_configure(Route *route, Link *link,
58                     sd_rtnl_message_handler_t callback) {
59         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
60         int r;
61
62         assert(link);
63         assert(link->manager);
64         assert(link->manager->rtnl);
65         assert(link->ifindex > 0);
66         assert(route->family == AF_INET || route->family == AF_INET6);
67
68         r = sd_rtnl_message_route_new(RTM_NEWROUTE, route->family, 0, 0, 0,
69                                       RT_TABLE_MAIN, RT_SCOPE_UNIVERSE, RTPROT_BOOT,
70                                       RTN_UNICAST, 0, &req);
71         if (r < 0) {
72                 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
73                 return r;
74         }
75
76         r = sd_rtnl_message_append(req, RTA_GATEWAY, &route->in_addr);
77         if (r < 0) {
78                 log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
79                 return r;
80         }
81
82         r = sd_rtnl_message_append(req, RTA_OIF, &link->ifindex);
83         if (r < 0) {
84                 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
85                 return r;
86         }
87
88         r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
89         if (r < 0) {
90                 log_error("Could not send rtnetlink message: %s", strerror(-r));
91                 return r;
92         }
93
94         link->rtnl_messages ++;
95
96         return 0;
97 }
98
99 int config_parse_gateway(const char *unit,
100                 const char *filename,
101                 unsigned line,
102                 const char *section,
103                 unsigned section_line,
104                 const char *lvalue,
105                 int ltype,
106                 const char *rvalue,
107                 void *data,
108                 void *userdata) {
109         _cleanup_route_free_ Route *n = NULL;
110         _cleanup_free_ char *route = NULL;
111         int r;
112
113         assert(filename);
114         assert(lvalue);
115         assert(rvalue);
116         assert(data);
117
118         r = route_new(userdata, &n);
119         if (r < 0)
120                 return r;
121
122         r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
123         if (r < 0) {
124                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
125                            "Route is invalid, ignoring assignment: %s", route);
126                 return 0;
127         }
128
129         n = NULL;
130
131         return 0;
132 }