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