chiark / gitweb /
918a1d0191790bf5e6960116d4dc5a16ffdda20c
[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(Manager *manager, Route *route, Link *link) {
58         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
59         int r;
60
61         assert(manager);
62         assert(link);
63         assert(link->ifindex > 0);
64         assert(route->family == AF_INET || route->family == AF_INET6);
65
66         r = sd_rtnl_message_route_new(RTM_NEWROUTE, route->family, 0, 0, 0,
67                                       RT_TABLE_MAIN, RT_SCOPE_UNIVERSE, RTPROT_BOOT,
68                                       RTN_UNICAST, 0, &req);
69         if (r < 0) {
70                 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
71                 return r;
72         }
73
74         r = sd_rtnl_message_append(req, RTA_GATEWAY, &route->in_addr);
75         if (r < 0) {
76                 log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
77                 return r;
78         }
79
80         r = sd_rtnl_message_append(req, RTA_OIF, &link->ifindex);
81         if (r < 0) {
82                 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
83                 return r;
84         }
85
86         r = sd_rtnl_send_with_reply_and_block(manager->rtnl, req, 0, NULL);
87         if (r < 0) {
88                 log_error("Could not configure route: %s", strerror(-r));
89                 return r;
90         }
91
92         log_info("Configured route");
93
94         return 0;
95 }
96
97 int config_parse_gateway(const char *unit,
98                 const char *filename,
99                 unsigned line,
100                 const char *section,
101                 const char *lvalue,
102                 int ltype,
103                 const char *rvalue,
104                 void *data,
105                 void *userdata) {
106         _cleanup_route_free_ Route *n = NULL;
107         _cleanup_free_ char *route = NULL;
108         int r;
109
110         assert(filename);
111         assert(lvalue);
112         assert(rvalue);
113         assert(data);
114
115         r = route_new(userdata, &n);
116         if (r < 0)
117                 return r;
118
119         r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
120         if (r < 0) {
121                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
122                            "Route is invalid, ignoring assignment: %s", route);
123                 return 0;
124         }
125
126         n = NULL;
127
128         return 0;
129 }