d2e1461cfa8cca291bd8a785775a3ed745496163
[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 static void
83 interface_clear_dns_servers(struct interface *iface)
84 {
85         struct dns_server *s, *tmp;
86
87         list_for_each_entry_safe(s, tmp, &iface->proto_dns_servers, list) {
88                 list_del(&s->list);
89                 free(s);
90         }
91 }
92
93 static void
94 interface_clear_dns_search(struct interface *iface)
95 {
96         struct dns_search_domain *s, *tmp;
97
98         list_for_each_entry_safe(s, tmp, &iface->proto_dns_search, list) {
99                 list_del(&s->list);
100                 free(s);
101         }
102 }
103
104 void
105 interface_clear_dns(struct interface *iface)
106 {
107         interface_clear_dns_servers(iface);
108         interface_clear_dns_search(iface);
109 }
110
111 void
112 interface_write_resolv_conf(void)
113 {
114         struct interface *iface;
115         struct dns_server *s;
116         struct dns_search_domain *d;
117         char *path = alloca(strlen(resolv_conf) + 5);
118         const char *str;
119         char buf[32];
120         FILE *f;
121
122         sprintf(path, "%s.tmp", resolv_conf);
123         unlink(path);
124         f = fopen(path, "w");
125         if (!f) {
126                 D(INTERFACE, "Failed to open %s for writing\n", path);
127                 return;
128         }
129
130         vlist_for_each_element(&interfaces, iface, node) {
131                 if (iface->state != IFS_UP)
132                         continue;
133
134                 if (list_empty(&iface->proto_dns_search) &&
135                     list_empty(&iface->proto_dns_servers))
136                         continue;
137
138                 fprintf(f, "# Interface %s\n", iface->name);
139                 list_for_each_entry(s, &iface->proto_dns_servers, list) {
140                         str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
141                         if (!str)
142                                 continue;
143
144                         fprintf(f, "nameserver %s\n", str);
145                 }
146
147                 list_for_each_entry(d, &iface->proto_dns_search, list) {
148                         fprintf(f, "search %s\n", d->name);
149                 }
150         }
151         fclose(f);
152         if (rename(path, resolv_conf) < 0) {
153                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
154                 unlink(path);
155         }
156 }
157
158 void
159 interface_ip_init(struct interface *iface)
160 {
161         vlist_init(&iface->proto_route, route_cmp, interface_update_proto_route,
162                    struct device_route, node, mask);
163         vlist_init(&iface->proto_addr, addr_cmp, interface_update_proto_addr,
164                    struct device_addr, node, mask);
165 }