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