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