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