add some code for parsing static ipaddr/ip6addr/netmask
[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 enum {
127         OPT_IPADDR,
128         OPT_IP6ADDR,
129         OPT_NETMASK,
130         OPT_GATEWAY,
131         OPT_DNS,
132         __OPT_MAX,
133 };
134
135 static const struct uci_parse_option opts[__OPT_MAX] = {
136         [OPT_IPADDR] = { .name = "ipaddr" },
137         [OPT_IP6ADDR] = { .name = "ip6addr" },
138         [OPT_NETMASK] = { .name = "netmask", .type = UCI_TYPE_STRING },
139         [OPT_GATEWAY] = { .name = "gateway", .type = UCI_TYPE_STRING },
140         [OPT_DNS] = { .name = "dns" },
141 };
142
143 struct interface_proto_state *
144 static_attach(struct proto_handler *h, struct interface *iface,
145               struct uci_section *s)
146 {
147         struct uci_option *tb[__OPT_MAX];
148         struct uci_element *e;
149         struct v4_addr *v4 = NULL;
150         struct v6_addr *v6 = NULL;
151         int n_v4 = 0, n_v6 = 0;
152         struct in_addr ina = {};
153         const char *error = NULL;
154         int netmask = 32;
155         int i;
156
157         uci_parse_section(s, opts, __OPT_MAX, tb);
158
159         if (tb[OPT_NETMASK]) {
160                 if (!inet_aton(tb[OPT_NETMASK]->v.string, &ina)) {
161                         error = "INVALID_NETMASK";
162                         goto error;
163                 }
164
165                 netmask = 32 - fls(~(ntohl(ina.s_addr)));
166         }
167
168         if (tb[OPT_IPADDR]) {
169                 if (tb[OPT_IPADDR]->type == UCI_TYPE_STRING) {
170                         n_v4 = 1;
171                         v4 = alloca(sizeof(*v4));
172                         if (!parse_v4(tb[OPT_IPADDR]->v.string, v4, netmask))
173                                 goto invalid_addr;
174                 } else {
175                         uci_foreach_element(&tb[OPT_IPADDR]->v.list, e)
176                                 n_v4++;
177
178                         i = 0;
179                         v4 = alloca(sizeof(*v4) * n_v4);
180                         uci_foreach_element(&tb[OPT_IPADDR]->v.list, e) {
181                                 if (!parse_v4(e->name, &v4[i++], netmask))
182                                         goto invalid_addr;
183                         }
184                 }
185         }
186
187         if (tb[OPT_IP6ADDR]) {
188                 if (tb[OPT_IP6ADDR]->type == UCI_TYPE_STRING) {
189                         n_v6 = 1;
190                         v6 = alloca(sizeof(*v6));
191                         v6->prefix = netmask;
192                         if (!parse_v6(tb[OPT_IP6ADDR]->v.string, v6, netmask))
193                                 goto invalid_addr;
194                 } else {
195                         uci_foreach_element(&tb[OPT_IP6ADDR]->v.list, e)
196                                 n_v6++;
197
198                         i = 0;
199                         v6 = alloca(sizeof(*v6) * n_v6);
200                         uci_foreach_element(&tb[OPT_IP6ADDR]->v.list, e) {
201                                 if (!parse_v6(e->name, &v6[i++], netmask))
202                                         goto invalid_addr;
203                         }
204                 }
205         }
206
207
208         if (!n_v4 && !n_v6) {
209                 error = "NO_ADDRESS";
210                 goto error;
211         }
212
213         return static_create_state(v4, n_v4, v6, n_v6);
214
215 invalid_addr:
216         error = "INVALID_ADDRESS";
217
218 error:
219         interface_add_error(iface, "proto-static", error, NULL, 0);
220         return NULL;
221 }
222
223 static struct proto_handler static_proto = {
224         .name = "static",
225         .attach = static_attach,
226 };
227
228 static void __init
229 static_proto_init(void)
230 {
231         add_proto_handler(&static_proto);
232 }