add -Wmissing-declarations to cflags
[project/netifd.git] / interface-ip.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include <arpa/inet.h>
7
8 #include "netifd.h"
9 #include "device.h"
10 #include "interface.h"
11 #include "interface-ip.h"
12 #include "proto.h"
13 #include "ubus.h"
14 #include "system.h"
15
16 enum {
17         ROUTE_INTERFACE,
18         ROUTE_TARGET,
19         ROUTE_MASK,
20         ROUTE_GATEWAY,
21         ROUTE_DEVICE,
22         ROUTE_METRIC,
23         ROUTE_MTU,
24         __ROUTE_LAST
25 };
26
27 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
28         [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
29         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
30         [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
31         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
32         [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
33         [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
34         [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
35 };
36
37 void
38 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
39 {
40         struct interface_ip_settings *ip;
41         struct blob_attr *tb[__ROUTE_LAST], *cur;
42         struct device_route *route;
43         int af = v6 ? AF_INET6 : AF_INET;
44         bool config = false;
45
46         blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
47
48         if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
49                 return;
50
51         if (!iface) {
52                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
53                         return;
54
55                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
56                 if (!iface)
57                         return;
58
59                 ip = &iface->config_ip;
60                 config = true;
61         } else {
62                 ip = &iface->proto_ip;
63         }
64
65         route = calloc(1, sizeof(*route));
66         if (!route)
67                 return;
68
69         route->mask = v6 ? 128 : 32;
70         if ((cur = tb[ROUTE_MASK]) != NULL) {
71                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
72                 if (route->mask > (v6 ? 128 : 32))
73                         goto error;
74         }
75
76         if ((cur = tb[ROUTE_TARGET]) != NULL) {
77                 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
78                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
79                         goto error;
80                 }
81         }
82
83         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
84                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
85                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
86                         goto error;
87                 }
88         }
89
90         if ((cur = tb[ROUTE_METRIC]) != NULL)
91                 route->metric = blobmsg_get_u32(cur);
92
93         if ((cur = tb[ROUTE_MTU]) != NULL)
94                 route->mtu = blobmsg_get_u32(cur);
95
96         if (!config && (cur = tb[ROUTE_DEVICE]) != NULL)
97                 route->device = device_get(blobmsg_data(cur), true);
98
99         vlist_add(&ip->route, &route->node);
100         return;
101
102 error:
103         free(route);
104 }
105
106 static int
107 addr_cmp(const void *k1, const void *k2, void *ptr)
108 {
109         return memcmp(k1, k2, sizeof(struct device_addr) -
110                       offsetof(struct device_addr, mask));
111 }
112
113 static int
114 route_cmp(const void *k1, const void *k2, void *ptr)
115 {
116         return memcmp(k1, k2, sizeof(struct device_route) -
117                       offsetof(struct device_route, mask));
118 }
119
120 static void
121 interface_update_proto_addr(struct vlist_tree *tree,
122                             struct vlist_node *node_new,
123                             struct vlist_node *node_old)
124 {
125         struct interface_ip_settings *ip;
126         struct interface *iface;
127         struct device *dev;
128         struct device_addr *addr;
129
130         ip = container_of(tree, struct interface_ip_settings, addr);
131         iface = ip->iface;
132         dev = iface->l3_dev->dev;
133
134         if (node_old) {
135                 addr = container_of(node_old, struct device_addr, node);
136                 if (!(addr->flags & DEVADDR_EXTERNAL) && addr->enabled)
137                         system_del_address(dev, addr);
138                 free(addr);
139         }
140
141         if (node_new) {
142                 addr = container_of(node_new, struct device_addr, node);
143                 if (!(addr->flags & DEVADDR_EXTERNAL))
144                         system_add_address(dev, addr);
145                 addr->enabled = true;
146         }
147 }
148
149 static void
150 interface_update_proto_route(struct vlist_tree *tree,
151                              struct vlist_node *node_new,
152                              struct vlist_node *node_old)
153 {
154         struct interface_ip_settings *ip;
155         struct interface *iface;
156         struct device *dev;
157         struct device_route *route;
158
159         ip = container_of(tree, struct interface_ip_settings, route);
160         iface = ip->iface;
161         dev = iface->l3_dev->dev;
162
163         if (node_old) {
164                 route = container_of(node_old, struct device_route, node);
165                 if (!(route->flags & DEVADDR_EXTERNAL) && route->enabled)
166                         system_del_route(dev, route);
167                 free(route);
168         }
169
170         if (node_new) {
171                 route = container_of(node_new, struct device_route, node);
172                 if (!(route->flags & DEVADDR_EXTERNAL))
173                         system_add_route(dev, route);
174                 route->enabled = true;
175         }
176 }
177
178 void
179 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
180 {
181         struct dns_server *s;
182
183         s = calloc(1, sizeof(*s));
184         s->af = AF_INET;
185         if (inet_pton(s->af, str, &s->addr.in))
186                 goto add;
187
188         s->af = AF_INET6;
189         if (inet_pton(s->af, str, &s->addr.in))
190                 goto add;
191
192         free(s);
193         return;
194
195 add:
196         D(INTERFACE, "Add IPv%c DNS server: %s\n",
197           s->af == AF_INET6 ? '6' : '4', str);
198         list_add_tail(&s->list, &ip->dns_servers);
199 }
200
201 void
202 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
203 {
204         struct blob_attr *cur;
205         int rem;
206
207         blobmsg_for_each_attr(cur, list, rem) {
208                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
209                         continue;
210
211                 if (!blobmsg_check_attr(cur, NULL))
212                         continue;
213
214                 interface_add_dns_server(ip, blobmsg_data(cur));
215         }
216 }
217
218 static void
219 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
220 {
221         struct dns_search_domain *s;
222         int len = strlen(str);
223
224         s = calloc(1, sizeof(*s) + len + 1);
225         if (!s)
226                 return;
227
228         D(INTERFACE, "Add DNS search domain: %s\n", str);
229         memcpy(s->name, str, len);
230         list_add_tail(&s->list, &ip->dns_search);
231 }
232
233 void
234 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
235 {
236         struct blob_attr *cur;
237         int rem;
238
239         blobmsg_for_each_attr(cur, list, rem) {
240                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
241                         continue;
242
243                 if (!blobmsg_check_attr(cur, NULL))
244                         continue;
245
246                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
247         }
248 }
249
250 static void
251 interface_clear_dns_servers(struct interface_ip_settings *ip)
252 {
253         struct dns_server *s, *tmp;
254
255         list_for_each_entry_safe(s, tmp, &ip->dns_servers, list) {
256                 list_del(&s->list);
257                 free(s);
258         }
259 }
260
261 static void
262 interface_clear_dns_search(struct interface_ip_settings *ip)
263 {
264         struct dns_search_domain *s, *tmp;
265
266         list_for_each_entry_safe(s, tmp, &ip->dns_search, list) {
267                 list_del(&s->list);
268                 free(s);
269         }
270 }
271
272 void
273 interface_write_resolv_conf(void)
274 {
275         struct interface *iface;
276         struct dns_server *s;
277         struct dns_search_domain *d;
278         char *path = alloca(strlen(resolv_conf) + 5);
279         const char *str;
280         char buf[32];
281         FILE *f;
282
283         sprintf(path, "%s.tmp", resolv_conf);
284         unlink(path);
285         f = fopen(path, "w");
286         if (!f) {
287                 D(INTERFACE, "Failed to open %s for writing\n", path);
288                 return;
289         }
290
291         vlist_for_each_element(&interfaces, iface, node) {
292                 if (iface->state != IFS_UP)
293                         continue;
294
295                 if (list_empty(&iface->proto_ip.dns_search) &&
296                     list_empty(&iface->proto_ip.dns_servers))
297                         continue;
298
299                 fprintf(f, "# Interface %s\n", iface->name);
300                 list_for_each_entry(s, &iface->proto_ip.dns_servers, list) {
301                         str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
302                         if (!str)
303                                 continue;
304
305                         fprintf(f, "nameserver %s\n", str);
306                 }
307
308                 list_for_each_entry(d, &iface->proto_ip.dns_search, list) {
309                         fprintf(f, "search %s\n", d->name);
310                 }
311         }
312         fclose(f);
313         if (rename(path, resolv_conf) < 0) {
314                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
315                 unlink(path);
316         }
317 }
318
319 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
320 {
321         struct device_addr *addr;
322         struct device_route *route;
323         struct device *dev;
324
325         ip->enabled = enabled;
326         dev = ip->iface->l3_dev->dev;
327         if (!dev)
328                 return;
329
330         vlist_for_each_element(&ip->addr, addr, node) {
331                 if (addr->enabled == enabled)
332                         continue;
333
334                 if (enabled)
335                         system_add_address(dev, addr);
336                 else
337                         system_del_address(dev, addr);
338                 addr->enabled = enabled;
339         }
340
341         vlist_for_each_element(&ip->route, route, node) {
342                 if (route->enabled == enabled)
343                         continue;
344
345                 if (enabled)
346                         system_add_route(dev, route);
347                 else
348                         system_del_route(dev, route);
349                 route->enabled = enabled;
350         }
351 }
352
353 void
354 interface_ip_update_start(struct interface_ip_settings *ip)
355 {
356         interface_clear_dns_servers(ip);
357         interface_clear_dns_search(ip);
358         vlist_update(&ip->route);
359         vlist_update(&ip->addr);
360 }
361
362 void
363 interface_ip_update_complete(struct interface_ip_settings *ip)
364 {
365         vlist_flush(&ip->route);
366         vlist_flush(&ip->addr);
367 }
368
369 void
370 interface_ip_flush(struct interface_ip_settings *ip)
371 {
372         interface_clear_dns_servers(ip);
373         interface_clear_dns_search(ip);
374         vlist_flush_all(&ip->route);
375         vlist_flush_all(&ip->addr);
376 }
377
378 void
379 interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
380 {
381         ip->iface = iface;
382         ip->enabled = true;
383         INIT_LIST_HEAD(&ip->dns_search);
384         INIT_LIST_HEAD(&ip->dns_servers);
385         vlist_init(&ip->route, route_cmp, interface_update_proto_route,
386                    struct device_route, node, mask);
387         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr,
388                    struct device_addr, node, mask);
389 }