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