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