proto: add an option for specifying external addresses using proto_apply_ip_settings()
[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, bool ext)
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
134 if (ext)
135 addr->flags |= DEVADDR_EXTERNAL;
136
137 vlist_add(&iface->proto_ip.addr, &addr->node);
138 return true;
139 }
140
141 static int
142 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6, int netmask, bool ext)
143 {
144 struct blob_attr *cur;
145 int n_addr = 0;
146 int rem;
147
148 blobmsg_for_each_attr(cur, attr, rem) {
149 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
150 return -1;
151
152 n_addr++;
153 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext))
154 return -1;
155 }
156
157 return n_addr;
158 }
159
160
161 static bool
162 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
163 {
164 struct device_route *route;
165 const char *str = blobmsg_data(attr);
166 int af = v6 ? AF_INET6 : AF_INET;
167
168 route = calloc(1, sizeof(*route));
169 if (!inet_pton(af, str, &route->nexthop)) {
170 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
171 free(route);
172 return false;
173 }
174
175 route->mask = 0;
176 route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
177 vlist_add(&iface->proto_ip.route, &route->node);
178
179 return true;
180 }
181
182 int
183 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
184 {
185 struct blob_attr *tb[__OPT_MAX];
186 const char *error;
187 unsigned int netmask = 32;
188 int n_v4 = 0, n_v6 = 0;
189
190 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
191
192 if (tb[OPT_NETMASK]) {
193 netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false);
194 if (netmask > 32) {
195 error = "INVALID_NETMASK";
196 goto error;
197 }
198 }
199
200 if (tb[OPT_IPADDR])
201 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask, ext);
202
203 if (tb[OPT_IP6ADDR])
204 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask, ext);
205
206 if (!n_v4 && !n_v6) {
207 error = "NO_ADDRESS";
208 goto error;
209 }
210
211 if (n_v4 < 0 || n_v6 < 0)
212 goto out;
213
214 if (n_v4 && tb[OPT_GATEWAY]) {
215 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
216 goto out;
217 }
218
219 if (n_v6 && tb[OPT_IP6GW]) {
220 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
221 goto out;
222 }
223
224 if (tb[OPT_DNS])
225 interface_add_dns_server_list(&iface->proto_ip, tb[OPT_DNS]);
226
227 return 0;
228
229 error:
230 interface_add_error(iface, "proto", error, NULL, 0);
231 out:
232 return -1;
233 }
234
235 void add_proto_handler(struct proto_handler *p)
236 {
237 if (!handlers.comp)
238 avl_init(&handlers, avl_strcmp, false, NULL);
239
240 if (p->avl.key)
241 return;
242
243 p->avl.key = p->name;
244 avl_insert(&handlers, &p->avl);
245 }
246
247 static void
248 default_proto_free(struct interface_proto_state *proto)
249 {
250 free(proto);
251 }
252
253 static int
254 invalid_proto_handler(struct interface_proto_state *proto,
255 enum interface_proto_cmd cmd, bool force)
256 {
257 return -1;
258 }
259
260 static int
261 no_proto_handler(struct interface_proto_state *proto,
262 enum interface_proto_cmd cmd, bool force)
263 {
264 return 0;
265 }
266
267 static struct interface_proto_state *
268 default_proto_attach(const struct proto_handler *h,
269 struct interface *iface, struct blob_attr *attr)
270 {
271 struct interface_proto_state *proto;
272
273 proto = calloc(1, sizeof(*proto));
274 proto->free = default_proto_free;
275 proto->cb = no_proto_handler;
276
277 return proto;
278 }
279
280 static const struct proto_handler no_proto = {
281 .name = "none",
282 .flags = PROTO_FLAG_IMMEDIATE,
283 .attach = default_proto_attach,
284 };
285
286 static const struct proto_handler *
287 get_proto_handler(const char *name)
288 {
289 struct proto_handler *proto;
290
291 if (!strcmp(name, "none"))
292 return &no_proto;
293
294 if (!handlers.comp)
295 return NULL;
296
297 return avl_find_element(&handlers, name, proto, avl);
298 }
299
300 void
301 proto_init_interface(struct interface *iface, struct blob_attr *attr)
302 {
303 const struct proto_handler *proto = iface->proto_handler;
304 struct interface_proto_state *state = NULL;
305
306 if (!proto)
307 proto = &no_proto;
308
309 state = proto->attach(proto, iface, attr);
310 if (!state) {
311 state = no_proto.attach(&no_proto, iface, attr);
312 state->cb = invalid_proto_handler;
313 }
314
315 state->handler = proto;
316 interface_set_proto_state(iface, state);
317 }
318
319 void
320 proto_attach_interface(struct interface *iface, const char *proto_name)
321 {
322 const struct proto_handler *proto = &no_proto;
323
324 if (proto_name) {
325 proto = get_proto_handler(proto_name);
326 if (!proto) {
327 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
328 proto = &no_proto;
329 }
330 }
331
332 iface->proto_handler = proto;
333 }
334
335 int
336 interface_proto_event(struct interface_proto_state *proto,
337 enum interface_proto_cmd cmd, bool force)
338 {
339 enum interface_proto_event ev;
340 int ret;
341
342 ret = proto->cb(proto, cmd, force);
343 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
344 goto out;
345
346 switch(cmd) {
347 case PROTO_CMD_SETUP:
348 ev = IFPEV_UP;
349 break;
350 case PROTO_CMD_TEARDOWN:
351 ev = IFPEV_DOWN;
352 break;
353 default:
354 return -EINVAL;
355 }
356 proto->proto_event(proto, ev);
357
358 out:
359 return ret;
360 }