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