97b2b588673e6358f2b67516079cf42d88fa02e4
[project/netifd.git] / proto-static.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 "proto.h"
11 #include "system.h"
12
13 struct v4_addr {
14         unsigned int prefix;
15         struct in_addr addr;
16 };
17
18 struct v6_addr {
19         unsigned int prefix;
20         struct in6_addr addr;
21 };
22
23 struct static_proto_state {
24     struct interface_proto_state proto;
25         struct interface *iface;
26
27         int n_v4;
28         struct v4_addr *v4;
29
30         int n_v6;
31         struct v4_addr *v6;
32 };
33
34 static int
35 static_handler(struct interface_proto_state *proto,
36                enum interface_proto_cmd cmd, bool force)
37 {
38         struct static_proto_state *state;
39         struct device *dev;
40         int ret = 0;
41         int i;
42
43         state = container_of(proto, struct static_proto_state, proto);
44         dev = state->iface->main_dev.dev;
45
46         switch (cmd) {
47         case PROTO_CMD_SETUP:
48                 for (i = 0; i < state->n_v4; i++) {
49                         if (ret)
50                                 break;
51                         ret = system_add_address(dev, AF_INET,
52                                 &state->v4[i].addr, state->v4[i].prefix);
53                 }
54                 for (i = 0; i < state->n_v6; i++) {
55                         if (ret)
56                                 break;
57                         ret = system_add_address(dev, AF_INET6,
58                                 &state->v6[i].addr, state->v6[i].prefix);
59                 }
60
61                 if (!ret)
62                         return 0;
63
64                 interface_add_error(state->iface, "proto-static",
65                         "SET_ADDRESS_FAILED", NULL, 0);
66                 /* fall through */
67
68         case PROTO_CMD_TEARDOWN:
69                 for (i = 0; i < state->n_v4; i++)
70                         system_del_address(dev, AF_INET, &state->v4[i].addr);
71                 for (i = 0; i < state->n_v6; i++)
72                         system_del_address(dev, AF_INET6, &state->v6[i].addr);
73                 break;
74         }
75         return ret;
76 }
77
78 static void
79 static_free(struct interface_proto_state *proto)
80 {
81         struct static_proto_state *state;
82
83         state = container_of(proto, struct static_proto_state, proto);
84         free(state);
85 }
86
87 struct interface_proto_state *
88 static_create_state(struct interface *iface, struct v4_addr *v4, int n_v4, struct v6_addr *v6, int n_v6)
89 {
90         struct static_proto_state *state;
91         int v4_len = sizeof(struct v4_addr) * n_v4;
92         int v6_len = sizeof(struct v6_addr) * n_v6;
93         void *next;
94
95         state = calloc(1, sizeof(*state) + v4_len + v6_len);
96         state->iface = iface;
97         state->proto.free = static_free;
98         state->proto.handler = static_handler;
99         state->proto.flags = PROTO_FLAG_IMMEDIATE;
100         next = (void *) (state + 1);
101
102         if (n_v4) {
103                 state->n_v4 = n_v4;
104                 state->v4 = next;
105                 memcpy(state->v4, v4, sizeof(*v4) * n_v4);
106                 next = state->v4 + n_v4;
107         }
108
109         if (n_v6) {
110                 state->n_v6 = n_v6;
111                 state->v6 = next;
112                 memcpy(state->v6, v6, sizeof(*v6) * n_v6);
113         }
114
115         return &state->proto;
116 }
117
118 static bool
119 split_netmask(char *str, unsigned int *netmask)
120 {
121         char *delim, *err = NULL;
122
123         delim = strchr(str, '/');
124         if (delim) {
125                 *(delim++) = 0;
126
127                 *netmask = strtoul(delim, &err, 10);
128                 if (err && *err)
129                         return false;
130         }
131         return true;
132 }
133
134 static int
135 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
136 {
137         char *astr = alloca(strlen(str) + 1);
138
139         strcpy(astr, str);
140         if (!split_netmask(astr, netmask))
141                 return 0;
142
143         if (af == AF_INET6) {
144                 if (*netmask > 128)
145                         return 0;
146         } else {
147                 if (*netmask > 32)
148                         return 0;
149         }
150
151         return inet_pton(af, str, addr);
152 }
153
154 static int
155 parse_v4(const char *str, struct v4_addr *v4, int netmask)
156 {
157         v4->prefix = netmask;
158         return parse_ip_and_netmask(AF_INET, str, &v4->addr, &v4->prefix);
159 }
160
161 static int
162 parse_v6(const char *str, struct v6_addr *v6, int netmask)
163 {
164         v6->prefix = netmask;
165         return parse_ip_and_netmask(AF_INET6, str, &v6->addr, &v6->prefix);
166 }
167
168 static int
169 count_list_entries(struct uci_option *o)
170 {
171         struct uci_element *e;
172         int n = 0;
173
174         uci_foreach_element(&o->v.list, e)
175                 n++;
176
177         return n;
178 }
179
180 enum {
181         OPT_IPADDR,
182         OPT_IP6ADDR,
183         OPT_NETMASK,
184         OPT_GATEWAY,
185         OPT_DNS,
186         __OPT_MAX,
187 };
188
189 static const struct uci_parse_option opts[__OPT_MAX] = {
190         [OPT_IPADDR] = { .name = "ipaddr" },
191         [OPT_IP6ADDR] = { .name = "ip6addr" },
192         [OPT_NETMASK] = { .name = "netmask", .type = UCI_TYPE_STRING },
193         [OPT_GATEWAY] = { .name = "gateway", .type = UCI_TYPE_STRING },
194         [OPT_DNS] = { .name = "dns" },
195 };
196
197 struct interface_proto_state *
198 static_attach(struct proto_handler *h, struct interface *iface,
199               struct uci_section *s)
200 {
201         struct uci_option *tb[__OPT_MAX];
202         struct uci_element *e;
203         struct v4_addr *v4 = NULL;
204         struct v6_addr *v6 = NULL;
205         int n_v4 = 0, n_v6 = 0;
206         struct in_addr ina = {};
207         const char *error = NULL;
208         int netmask = 32;
209         int i;
210
211         uci_parse_section(s, opts, __OPT_MAX, tb);
212
213         if (tb[OPT_NETMASK]) {
214                 if (!inet_aton(tb[OPT_NETMASK]->v.string, &ina)) {
215                         error = "INVALID_NETMASK";
216                         goto error;
217                 }
218
219                 netmask = 32 - fls(~(ntohl(ina.s_addr)));
220         }
221
222         if (tb[OPT_IPADDR]) {
223                 if (tb[OPT_IPADDR]->type == UCI_TYPE_STRING) {
224                         n_v4 = 1;
225                         v4 = alloca(sizeof(*v4));
226                         if (!parse_v4(tb[OPT_IPADDR]->v.string, v4, netmask))
227                                 goto invalid_addr;
228                 } else {
229                         i = 0;
230                         n_v4 = count_list_entries(tb[OPT_IPADDR]);
231                         v4 = alloca(sizeof(*v4) * n_v4);
232                         uci_foreach_element(&tb[OPT_IPADDR]->v.list, e) {
233                                 if (!parse_v4(e->name, &v4[i++], netmask))
234                                         goto invalid_addr;
235                         }
236                 }
237         }
238
239         if (tb[OPT_IP6ADDR]) {
240                 if (tb[OPT_IP6ADDR]->type == UCI_TYPE_STRING) {
241                         n_v6 = 1;
242                         v6 = alloca(sizeof(*v6));
243                         v6->prefix = netmask;
244                         if (!parse_v6(tb[OPT_IP6ADDR]->v.string, v6, netmask))
245                                 goto invalid_addr;
246                 } else {
247                         i = 0;
248                         n_v6 = count_list_entries(tb[OPT_IP6ADDR]);
249                         v6 = alloca(sizeof(*v6) * n_v6);
250                         uci_foreach_element(&tb[OPT_IP6ADDR]->v.list, e) {
251                                 if (!parse_v6(e->name, &v6[i++], netmask))
252                                         goto invalid_addr;
253                         }
254                 }
255         }
256
257
258         if (!n_v4 && !n_v6) {
259                 error = "NO_ADDRESS";
260                 goto error;
261         }
262
263         return static_create_state(iface, v4, n_v4, v6, n_v6);
264
265 invalid_addr:
266         error = "INVALID_ADDRESS";
267
268 error:
269         interface_add_error(iface, "proto-static", error, NULL, 0);
270         return NULL;
271 }
272
273 static struct proto_handler static_proto = {
274         .name = "static",
275         .attach = static_attach,
276 };
277
278 static void __init
279 static_proto_init(void)
280 {
281         add_proto_handler(&static_proto);
282 }