dfdedcb028fb4a613ddc3d6ef919fac2bdfd7f96
[project/netifd.git] / proto.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7
8 #include "netifd.h"
9 #include "interface.h"
10 #include "interface-ip.h"
11 #include "proto.h"
12
13 static struct avl_tree handlers;
14
15 enum {
16         OPT_IPADDR,
17         OPT_IP6ADDR,
18         OPT_NETMASK,
19         OPT_GATEWAY,
20         OPT_IP6GW,
21         OPT_DNS,
22         __OPT_MAX,
23 };
24
25 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
26         [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
27         [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
28         [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
29         [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
30         [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
31         [OPT_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
32 };
33
34 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
35         [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
36         [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
37         [OPT_DNS] = { .type = BLOBMSG_TYPE_STRING },
38 };
39
40 const struct config_param_list proto_ip_attr = {
41         .n_params = __OPT_MAX,
42         .params = proto_ip_attributes,
43         .info = proto_ip_attr_info,
44 };
45
46
47 unsigned int
48 parse_netmask_string(const char *str, bool v6)
49 {
50         struct in_addr addr;
51         unsigned int ret;
52         char *err = NULL;
53
54         if (!strchr(str, '.')) {
55                 ret = strtoul(str, &err, 0);
56                 if (err && *err)
57                         goto error;
58
59                 return ret;
60         }
61
62         if (v6)
63                 goto error;
64
65         if (inet_aton(str, &addr) != 1)
66                 goto error;
67
68         return 32 - fls(~(ntohl(addr.s_addr)));
69
70 error:
71         return ~0;
72 }
73
74 static bool
75 split_netmask(char *str, unsigned int *netmask, bool v6)
76 {
77         char *delim = strchr(str, '/');
78
79         if (delim) {
80                 *(delim++) = 0;
81
82                 *netmask = parse_netmask_string(delim, v6);
83         }
84         return true;
85 }
86
87 static int
88 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
89 {
90         char *astr = alloca(strlen(str) + 1);
91
92         strcpy(astr, str);
93         if (!split_netmask(astr, netmask, af == AF_INET6))
94                 return 0;
95
96         if (af == AF_INET6) {
97                 if (*netmask > 128)
98                         return 0;
99         } else {
100                 if (*netmask > 32)
101                         return 0;
102         }
103
104         return inet_pton(af, astr, addr);
105 }
106
107 struct device_addr *
108 proto_parse_ip_addr_string(const char *str, bool v6, int mask)
109 {
110         struct device_addr *addr;
111         int af = v6 ? AF_INET6 : AF_INET;
112
113         addr = calloc(1, sizeof(*addr));
114         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
115         addr->mask = mask;
116         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
117                 free(addr);
118                 return NULL;
119         }
120         return addr;
121 }
122
123 static bool
124 parse_addr(struct interface *iface, const char *str, bool v6, int mask)
125 {
126         struct device_addr *addr;
127
128         addr = proto_parse_ip_addr_string(str, v6, mask);
129         if (!addr) {
130                 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
131                 return false;
132         }
133         vlist_add(&iface->proto_ip.addr, &addr->node);
134         return true;
135 }
136
137 static int
138 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6, int netmask)
139 {
140         struct blob_attr *cur;
141         int n_addr = 0;
142         int rem;
143
144         blobmsg_for_each_attr(cur, attr, rem) {
145                 n_addr++;
146                 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask))
147                         return -1;
148         }
149
150         return n_addr;
151 }
152
153
154 static bool
155 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
156 {
157         struct device_route *route;
158         const char *str = blobmsg_data(attr);
159         int af = v6 ? AF_INET6 : AF_INET;
160
161         route = calloc(1, sizeof(*route));
162         if (!inet_pton(af, str, &route->nexthop)) {
163                 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
164                 free(route);
165                 return false;
166         }
167
168         route->mask = 0;
169         route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
170         vlist_add(&iface->proto_ip.route, &route->node);
171
172         return true;
173 }
174
175 int
176 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr)
177 {
178         struct blob_attr *tb[__OPT_MAX];
179         const char *error;
180         unsigned int netmask = 32;
181         int n_v4 = 0, n_v6 = 0;
182
183         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
184
185         if (tb[OPT_NETMASK]) {
186                 netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false);
187                 if (netmask > 32) {
188                         error = "INVALID_NETMASK";
189                         goto error;
190                 }
191         }
192
193         if (tb[OPT_IPADDR])
194                 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask);
195
196         if (tb[OPT_IP6ADDR])
197                 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask);
198
199         if (!n_v4 && !n_v6) {
200                 error = "NO_ADDRESS";
201                 goto error;
202         }
203
204         if (n_v4 < 0 || n_v6 < 0)
205                 goto out;
206
207         if (n_v4 && tb[OPT_GATEWAY]) {
208                 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
209                         goto out;
210         }
211
212         if (n_v6 && tb[OPT_IP6GW]) {
213                 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
214                         goto out;
215         }
216
217         if (tb[OPT_DNS])
218                 interface_add_dns_server_list(&iface->proto_ip, tb[OPT_DNS]);
219
220         return 0;
221
222 error:
223         interface_add_error(iface, "proto", error, NULL, 0);
224 out:
225         return -1;
226 }
227
228 void add_proto_handler(struct proto_handler *p)
229 {
230         if (!handlers.comp)
231                 avl_init(&handlers, avl_strcmp, false, NULL);
232
233         if (p->avl.key)
234                 return;
235
236         p->avl.key = p->name;
237         avl_insert(&handlers, &p->avl);
238 }
239
240 static void
241 default_proto_free(struct interface_proto_state *proto)
242 {
243         free(proto);
244 }
245
246 static int
247 invalid_proto_handler(struct interface_proto_state *proto,
248                       enum interface_proto_cmd cmd, bool force)
249 {
250         return -1;
251 }
252
253 static int
254 no_proto_handler(struct interface_proto_state *proto,
255                  enum interface_proto_cmd cmd, bool force)
256 {
257         return 0;
258 }
259
260 static struct interface_proto_state *
261 default_proto_attach(const struct proto_handler *h,
262                      struct interface *iface, struct blob_attr *attr)
263 {
264         struct interface_proto_state *proto;
265
266         proto = calloc(1, sizeof(*proto));
267         proto->free = default_proto_free;
268         proto->cb = no_proto_handler;
269
270         return proto;
271 }
272
273 static const struct proto_handler no_proto = {
274         .name = "none",
275         .flags = PROTO_FLAG_IMMEDIATE,
276         .attach = default_proto_attach,
277 };
278
279 static const struct proto_handler *
280 get_proto_handler(const char *name)
281 {
282         struct proto_handler *proto;
283
284         if (!strcmp(name, "none"))
285             return &no_proto;
286
287         if (!handlers.comp)
288                 return NULL;
289
290         return avl_find_element(&handlers, name, proto, avl);
291 }
292
293 void
294 proto_init_interface(struct interface *iface, struct blob_attr *attr)
295 {
296         const struct proto_handler *proto = iface->proto_handler;
297         struct interface_proto_state *state = NULL;
298
299         if (!proto)
300                 proto = &no_proto;
301
302         state = proto->attach(proto, iface, attr);
303         if (!state) {
304                 state = no_proto.attach(&no_proto, iface, attr);
305                 state->cb = invalid_proto_handler;
306         }
307
308         state->handler = proto;
309         interface_set_proto_state(iface, state);
310 }
311
312 void
313 proto_attach_interface(struct interface *iface, const char *proto_name)
314 {
315         const struct proto_handler *proto = &no_proto;
316
317         if (proto_name) {
318                 proto = get_proto_handler(proto_name);
319                 if (!proto) {
320                         interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
321                         proto = &no_proto;
322                 }
323         }
324
325         iface->proto_handler = proto;
326 }
327
328 int
329 interface_proto_event(struct interface_proto_state *proto,
330                       enum interface_proto_cmd cmd, bool force)
331 {
332         enum interface_proto_event ev;
333         int ret;
334
335         ret = proto->cb(proto, cmd, force);
336         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
337                 goto out;
338
339         switch(cmd) {
340         case PROTO_CMD_SETUP:
341                 ev = IFPEV_UP;
342                 break;
343         case PROTO_CMD_TEARDOWN:
344                 ev = IFPEV_DOWN;
345                 break;
346         default:
347                 return -EINVAL;
348         }
349         proto->proto_event(proto, ev);
350
351 out:
352         return ret;
353 }