15db33081988b8a7e7075b50cc32aad419067597
[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 enum {
14         OPT_IPADDR,
15         OPT_IP6ADDR,
16         OPT_NETMASK,
17         OPT_GATEWAY,
18         OPT_IP6GW,
19         __OPT_MAX,
20 };
21
22 static const struct blobmsg_policy static_attrs[__OPT_MAX] = {
23         [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
24         [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
25         [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
26         [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
27         [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
28 };
29
30 static const union config_param_info static_attr_info[__OPT_MAX] = {
31         [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
32         [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
33 };
34
35 static const struct config_param_list static_attr_list = {
36         .n_params = __OPT_MAX,
37         .params = static_attrs,
38         .info = static_attr_info,
39 };
40
41 struct static_proto_state {
42         struct interface_proto_state proto;
43
44         struct blob_attr *config;
45         struct interface *iface;
46 };
47
48 static bool
49 split_netmask(char *str, unsigned int *netmask)
50 {
51         char *delim, *err = NULL;
52
53         delim = strchr(str, '/');
54         if (delim) {
55                 *(delim++) = 0;
56
57                 *netmask = strtoul(delim, &err, 10);
58                 if (err && *err)
59                         return false;
60         }
61         return true;
62 }
63
64 static int
65 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
66 {
67         char *astr = alloca(strlen(str) + 1);
68
69         strcpy(astr, str);
70         if (!split_netmask(astr, netmask))
71                 return 0;
72
73         if (af == AF_INET6) {
74                 if (*netmask > 128)
75                         return 0;
76         } else {
77                 if (*netmask > 32)
78                         return 0;
79         }
80
81         return inet_pton(af, str, addr);
82 }
83
84 static bool
85 parse_addr(struct static_proto_state *state, const char *str, bool v6, int mask)
86 {
87         struct device_addr *addr;
88         int af = v6 ? AF_INET6 : AF_INET;
89
90         addr = calloc(1, sizeof(*addr));
91         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
92         addr->ctx = state;
93         addr->mask = mask;
94         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
95                 interface_add_error(state->iface, "proto-static", "INVALID_ADDRESS", &str, 1);
96                 free(addr);
97                 return false;
98         }
99         interface_add_address(state->iface, addr);
100         return true;
101 }
102
103 static int
104 parse_address_option(struct static_proto_state *state, struct blob_attr *attr, bool v6, int netmask)
105 {
106         struct blob_attr *cur;
107         int n_addr = 0;
108         int rem;
109
110         blobmsg_for_each_attr(cur, attr, rem) {
111                 n_addr++;
112                 if (!parse_addr(state, blobmsg_data(cur), v6, netmask))
113                         return -1;
114         }
115
116         return n_addr;
117 }
118
119 static bool
120 parse_gateway_option(struct static_proto_state *state, struct blob_attr *attr, bool v6)
121 {
122         struct device_route *route;
123         const char *str = blobmsg_data(attr);
124         int af = v6 ? AF_INET6 : AF_INET;
125
126         route = calloc(1, sizeof(*route));
127         if (!inet_pton(af, str, &route->nexthop)) {
128                 interface_add_error(state->iface, "proto-static",
129                                 "INVALID_GATEWAY", &str, 1);
130                 free(route);
131                 return false;
132         }
133         route->mask = 0;
134         route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
135         interface_add_route(state->iface, route);
136
137         return true;
138 }
139
140 static bool
141 static_proto_setup(struct static_proto_state *state)
142 {
143         struct blob_attr *tb[__OPT_MAX];
144         struct in_addr ina;
145         const char *error;
146         int netmask = 32;
147         int n_v4 = 0, n_v6 = 0;
148
149         blobmsg_parse(static_attrs, __OPT_MAX, tb, blob_data(state->config),
150                 blob_len(state->config));
151
152         if (tb[OPT_NETMASK]) {
153                 if (!inet_aton(blobmsg_data(tb[OPT_NETMASK]), &ina)) {
154                         error = "INVALID_NETMASK";
155                         goto error;
156                 }
157
158                 netmask = 32 - fls(~(ntohl(ina.s_addr)));
159         }
160
161         if (tb[OPT_IPADDR])
162                 n_v4 = parse_address_option(state, tb[OPT_IPADDR], false, netmask);
163
164         if (tb[OPT_IP6ADDR])
165                 n_v6 = parse_address_option(state, tb[OPT_IP6ADDR], true, netmask);
166
167         if (!n_v4 && !n_v6) {
168                 error = "NO_ADDRESS";
169                 goto error;
170         }
171
172         if (n_v4 < 0 || n_v6 < 0)
173                 goto out;
174
175         if (n_v4 && tb[OPT_GATEWAY]) {
176                 if (!parse_gateway_option(state, tb[OPT_GATEWAY], false))
177                         goto out;
178         }
179
180         if (n_v6 && tb[OPT_IP6GW]) {
181                 if (!parse_gateway_option(state, tb[OPT_IP6GW], true))
182                         goto out;
183         }
184
185         return true;
186
187 error:
188         interface_add_error(state->iface, "proto-static", error, NULL, 0);
189 out:
190         return false;
191 }
192
193 static int
194 static_handler(struct interface_proto_state *proto,
195                enum interface_proto_cmd cmd, bool force)
196 {
197         struct static_proto_state *state;
198         int ret = 0;
199
200         state = container_of(proto, struct static_proto_state, proto);
201
202         switch (cmd) {
203         case PROTO_CMD_SETUP:
204                 if (static_proto_setup(state))
205                         break;
206
207                 /* fall through */
208         case PROTO_CMD_TEARDOWN:
209                 interface_del_ctx_addr(state->iface, state);
210                 break;
211         }
212         return ret;
213 }
214
215 static void
216 static_free(struct interface_proto_state *proto)
217 {
218         struct static_proto_state *state;
219
220         state = container_of(proto, struct static_proto_state, proto);
221         free(state->config);
222         free(state);
223 }
224
225 struct interface_proto_state *
226 static_attach(const struct proto_handler *h, struct interface *iface,
227               struct blob_attr *attr)
228 {
229         struct static_proto_state *state;
230
231         state = calloc(1, sizeof(*state));
232         if (!state)
233                 return NULL;
234
235         state->iface = iface;
236         state->config = malloc(blob_pad_len(attr));
237         if (!state->config)
238                 goto error;
239
240         memcpy(state->config, attr, blob_pad_len(attr));
241         state->proto.free = static_free;
242         state->proto.handler = static_handler;
243         state->proto.flags = PROTO_FLAG_IMMEDIATE;
244
245         return &state->proto;
246
247 error:
248         free(state);
249         return NULL;
250 }
251
252 static struct proto_handler static_proto = {
253         .name = "static",
254         .config_params = &static_attr_list,
255         .attach = static_attach,
256 };
257
258 static void __init
259 static_proto_init(void)
260 {
261         add_proto_handler(&static_proto);
262 }