proto-shell: add support for appending configuration data over multiple update_link...
[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, &addr->flags);
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 = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
187 vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
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 struct blob_attr *cur;
197 const char *error;
198 unsigned int netmask = 32;
199 int n_v4 = 0, n_v6 = 0;
200 struct in_addr bcast = {};
201
202 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
203
204 if ((cur = tb[OPT_NETMASK])) {
205 netmask = parse_netmask_string(blobmsg_data(cur), false);
206 if (netmask > 32) {
207 error = "INVALID_NETMASK";
208 goto error;
209 }
210 }
211
212 if ((cur = tb[OPT_BROADCAST])) {
213 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
214 error = "INVALID_BROADCAST";
215 goto error;
216 }
217 }
218
219 if ((cur = tb[OPT_IPADDR]))
220 n_v4 = parse_address_option(iface, cur, false,
221 netmask, ext, bcast.s_addr);
222
223 if ((cur = tb[OPT_IP6ADDR]))
224 n_v6 = parse_address_option(iface, cur, true,
225 netmask, ext, 0);
226
227 if (!n_v4 && !n_v6) {
228 error = "NO_ADDRESS";
229 goto error;
230 }
231
232 if (n_v4 < 0 || n_v6 < 0)
233 goto out;
234
235 if ((cur = tb[OPT_GATEWAY])) {
236 if (n_v4 && !parse_gateway_option(iface, cur, false))
237 goto out;
238 }
239
240 if ((cur = tb[OPT_IP6GW])) {
241 if (n_v6 && !parse_gateway_option(iface, cur, true))
242 goto out;
243 }
244
245 if ((cur = tb[OPT_DNS]))
246 interface_add_dns_server_list(&iface->proto_ip, cur);
247
248 if ((cur = tb[OPT_DNS_SEARCH]))
249 interface_add_dns_search_list(&iface->proto_ip, cur);
250
251 return 0;
252
253 error:
254 interface_add_error(iface, "proto", error, NULL, 0);
255 out:
256 return -1;
257 }
258
259 void add_proto_handler(struct proto_handler *p)
260 {
261 if (!handlers.comp)
262 avl_init(&handlers, avl_strcmp, false, NULL);
263
264 if (p->avl.key)
265 return;
266
267 p->avl.key = p->name;
268 avl_insert(&handlers, &p->avl);
269 }
270
271 static void
272 default_proto_free(struct interface_proto_state *proto)
273 {
274 free(proto);
275 }
276
277 static int
278 invalid_proto_handler(struct interface_proto_state *proto,
279 enum interface_proto_cmd cmd, bool force)
280 {
281 return -1;
282 }
283
284 static int
285 no_proto_handler(struct interface_proto_state *proto,
286 enum interface_proto_cmd cmd, bool force)
287 {
288 return 0;
289 }
290
291 static struct interface_proto_state *
292 default_proto_attach(const struct proto_handler *h,
293 struct interface *iface, struct blob_attr *attr)
294 {
295 struct interface_proto_state *proto;
296
297 proto = calloc(1, sizeof(*proto));
298 proto->free = default_proto_free;
299 proto->cb = no_proto_handler;
300
301 return proto;
302 }
303
304 static const struct proto_handler no_proto = {
305 .name = "none",
306 .flags = PROTO_FLAG_IMMEDIATE,
307 .attach = default_proto_attach,
308 };
309
310 static const struct proto_handler *
311 get_proto_handler(const char *name)
312 {
313 struct proto_handler *proto;
314
315 if (!strcmp(name, "none"))
316 return &no_proto;
317
318 if (!handlers.comp)
319 return NULL;
320
321 return avl_find_element(&handlers, name, proto, avl);
322 }
323
324 void
325 proto_init_interface(struct interface *iface, struct blob_attr *attr)
326 {
327 const struct proto_handler *proto = iface->proto_handler;
328 struct interface_proto_state *state = NULL;
329
330 if (!proto)
331 proto = &no_proto;
332
333 state = proto->attach(proto, iface, attr);
334 if (!state) {
335 state = no_proto.attach(&no_proto, iface, attr);
336 state->cb = invalid_proto_handler;
337 }
338
339 state->handler = proto;
340 interface_set_proto_state(iface, state);
341 }
342
343 void
344 proto_attach_interface(struct interface *iface, const char *proto_name)
345 {
346 const struct proto_handler *proto = &no_proto;
347
348 if (proto_name) {
349 proto = get_proto_handler(proto_name);
350 if (!proto) {
351 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
352 proto = &no_proto;
353 }
354 }
355
356 iface->proto_handler = proto;
357 }
358
359 int
360 interface_proto_event(struct interface_proto_state *proto,
361 enum interface_proto_cmd cmd, bool force)
362 {
363 enum interface_proto_event ev;
364 int ret;
365
366 ret = proto->cb(proto, cmd, force);
367 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
368 goto out;
369
370 switch(cmd) {
371 case PROTO_CMD_SETUP:
372 ev = IFPEV_UP;
373 break;
374 case PROTO_CMD_TEARDOWN:
375 ev = IFPEV_DOWN;
376 break;
377 default:
378 return -EINVAL;
379 }
380 proto->proto_event(proto, ev);
381
382 out:
383 return ret;
384 }