add support for alias devices, which are activated based on hotplug events containing...
[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 static int
17 addr_cmp(const void *k1, const void *k2, void *ptr)
18 {
19 return memcmp(k1, k2, sizeof(struct device_addr) -
20 offsetof(struct device_addr, mask));
21 }
22
23 static int
24 route_cmp(const void *k1, const void *k2, void *ptr)
25 {
26 return memcmp(k1, k2, sizeof(struct device_route) -
27 offsetof(struct device_route, mask));
28 }
29
30 static void
31 interface_update_proto_addr(struct vlist_tree *tree,
32 struct vlist_node *node_new,
33 struct vlist_node *node_old)
34 {
35 struct interface *iface;
36 struct device *dev;
37 struct device_addr *addr;
38
39 iface = container_of(tree, struct interface, proto_addr);
40 dev = iface->l3_dev->dev;
41
42 if (node_old) {
43 addr = container_of(node_old, struct device_addr, node);
44 if (!(addr->flags & DEVADDR_EXTERNAL))
45 system_del_address(dev, addr);
46 free(addr);
47 }
48
49 if (node_new) {
50 addr = container_of(node_new, struct device_addr, node);
51 if (!(addr->flags & DEVADDR_EXTERNAL))
52 system_add_address(dev, addr);
53 }
54 }
55
56 static void
57 interface_update_proto_route(struct vlist_tree *tree,
58 struct vlist_node *node_new,
59 struct vlist_node *node_old)
60 {
61 struct interface *iface;
62 struct device *dev;
63 struct device_route *route;
64
65 iface = container_of(tree, struct interface, proto_route);
66 dev = iface->l3_dev->dev;
67
68 if (node_old) {
69 route = container_of(node_old, struct device_route, node);
70 if (!(route->flags & DEVADDR_EXTERNAL))
71 system_del_route(dev, route);
72 free(route);
73 }
74
75 if (node_new) {
76 route = container_of(node_new, struct device_route, node);
77 if (!(route->flags & DEVADDR_EXTERNAL))
78 system_add_route(dev, route);
79 }
80 }
81
82 void
83 interface_add_dns_server(struct interface *iface, const char *str)
84 {
85 struct dns_server *s;
86
87 s = calloc(1, sizeof(*s));
88 s->af = AF_INET;
89 if (inet_pton(s->af, str, &s->addr.in))
90 goto add;
91
92 s->af = AF_INET6;
93 if (inet_pton(s->af, str, &s->addr.in))
94 goto add;
95
96 free(s);
97 return;
98
99 add:
100 D(INTERFACE, "Add IPv%c DNS server: %s\n",
101 s->af == AF_INET6 ? '6' : '4', str);
102 list_add_tail(&s->list, &iface->proto_dns_servers);
103 }
104
105 void
106 interface_add_dns_server_list(struct interface *iface, struct blob_attr *list)
107 {
108 struct blob_attr *cur;
109 int rem;
110
111 blobmsg_for_each_attr(cur, list, rem) {
112 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
113 continue;
114
115 if (!blobmsg_check_attr(cur, NULL))
116 continue;
117
118 interface_add_dns_server(iface, blobmsg_data(cur));
119 }
120 }
121
122 void
123 interface_add_dns_search_domain(struct interface *iface, const char *str)
124 {
125 struct dns_search_domain *s;
126 int len = strlen(str);
127
128 s = calloc(1, sizeof(*s) + len + 1);
129 if (!s)
130 return;
131
132 D(INTERFACE, "Add DNS search domain: %s\n", str);
133 memcpy(s->name, str, len);
134 list_add_tail(&s->list, &iface->proto_dns_search);
135 }
136
137 void
138 interface_add_dns_search_list(struct interface *iface, struct blob_attr *list)
139 {
140 struct blob_attr *cur;
141 int rem;
142
143 blobmsg_for_each_attr(cur, list, rem) {
144 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
145 continue;
146
147 if (!blobmsg_check_attr(cur, NULL))
148 continue;
149
150 interface_add_dns_server(iface, blobmsg_data(cur));
151 }
152 }
153
154 static void
155 interface_clear_dns_servers(struct interface *iface)
156 {
157 struct dns_server *s, *tmp;
158
159 list_for_each_entry_safe(s, tmp, &iface->proto_dns_servers, list) {
160 list_del(&s->list);
161 free(s);
162 }
163 }
164
165 static void
166 interface_clear_dns_search(struct interface *iface)
167 {
168 struct dns_search_domain *s, *tmp;
169
170 list_for_each_entry_safe(s, tmp, &iface->proto_dns_search, list) {
171 list_del(&s->list);
172 free(s);
173 }
174 }
175
176 void
177 interface_clear_dns(struct interface *iface)
178 {
179 interface_clear_dns_servers(iface);
180 interface_clear_dns_search(iface);
181 }
182
183 void
184 interface_write_resolv_conf(void)
185 {
186 struct interface *iface;
187 struct dns_server *s;
188 struct dns_search_domain *d;
189 char *path = alloca(strlen(resolv_conf) + 5);
190 const char *str;
191 char buf[32];
192 FILE *f;
193
194 sprintf(path, "%s.tmp", resolv_conf);
195 unlink(path);
196 f = fopen(path, "w");
197 if (!f) {
198 D(INTERFACE, "Failed to open %s for writing\n", path);
199 return;
200 }
201
202 vlist_for_each_element(&interfaces, iface, node) {
203 if (iface->state != IFS_UP)
204 continue;
205
206 if (list_empty(&iface->proto_dns_search) &&
207 list_empty(&iface->proto_dns_servers))
208 continue;
209
210 fprintf(f, "# Interface %s\n", iface->name);
211 list_for_each_entry(s, &iface->proto_dns_servers, list) {
212 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
213 if (!str)
214 continue;
215
216 fprintf(f, "nameserver %s\n", str);
217 }
218
219 list_for_each_entry(d, &iface->proto_dns_search, list) {
220 fprintf(f, "search %s\n", d->name);
221 }
222 }
223 fclose(f);
224 if (rename(path, resolv_conf) < 0) {
225 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
226 unlink(path);
227 }
228 }
229
230 void
231 interface_ip_update_start(struct interface *iface)
232 {
233 interface_clear_dns(iface);
234 vlist_update(&iface->proto_route);
235 vlist_update(&iface->proto_addr);
236 }
237
238 void
239 interface_ip_update_complete(struct interface *iface)
240 {
241 vlist_flush(&iface->proto_route);
242 vlist_flush(&iface->proto_addr);
243 }
244
245 void
246 interface_ip_init(struct interface *iface)
247 {
248 vlist_init(&iface->proto_route, route_cmp, interface_update_proto_route,
249 struct device_route, node, mask);
250 vlist_init(&iface->proto_addr, addr_cmp, interface_update_proto_addr,
251 struct device_addr, node, mask);
252 }