6 #include <netinet/in.h>
22 struct static_proto_state {
23 struct interface_proto_state proto;
33 static_handler(struct interface_proto_state *proto,
34 enum interface_proto_cmd cmd, bool force)
40 static_free(struct interface_proto_state *proto)
42 struct static_proto_state *state;
44 state = container_of(proto, struct static_proto_state, proto);
48 struct interface_proto_state *
49 static_create_state(struct v4_addr *v4, int n_v4, struct v6_addr *v6, int n_v6)
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;
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;
64 memcpy(state->v4, v4, sizeof(*v4) * n_v4);
65 next = state->v4 + n_v4;
70 memcpy(state->v6, v6, sizeof(*v6) * n_v6);
77 split_netmask(char *str, unsigned int *netmask)
79 char *delim, *err = NULL;
81 delim = strchr(str, '/');
85 *netmask = strtoul(delim, &err, 10);
93 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
95 char *astr = alloca(strlen(str) + 1);
98 if (!split_netmask(astr, netmask))
101 if (af == AF_INET6) {
109 return inet_pton(af, str, addr);
113 parse_v4(const char *str, struct v4_addr *v4, int netmask)
115 v4->prefix = netmask;
116 return parse_ip_and_netmask(AF_INET, str, &v4->addr, &v4->prefix);
120 parse_v6(const char *str, struct v6_addr *v6, int netmask)
122 v6->prefix = netmask;
123 return parse_ip_and_netmask(AF_INET6, str, &v6->addr, &v6->prefix);
127 count_list_entries(struct uci_option *o)
129 struct uci_element *e;
132 uci_foreach_element(&o->v.list, e)
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" },
155 struct interface_proto_state *
156 static_attach(struct proto_handler *h, struct interface *iface,
157 struct uci_section *s)
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;
169 uci_parse_section(s, opts, __OPT_MAX, tb);
171 if (tb[OPT_NETMASK]) {
172 if (!inet_aton(tb[OPT_NETMASK]->v.string, &ina)) {
173 error = "INVALID_NETMASK";
177 netmask = 32 - fls(~(ntohl(ina.s_addr)));
180 if (tb[OPT_IPADDR]) {
181 if (tb[OPT_IPADDR]->type == UCI_TYPE_STRING) {
183 v4 = alloca(sizeof(*v4));
184 if (!parse_v4(tb[OPT_IPADDR]->v.string, v4, netmask))
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))
197 if (tb[OPT_IP6ADDR]) {
198 if (tb[OPT_IP6ADDR]->type == UCI_TYPE_STRING) {
200 v6 = alloca(sizeof(*v6));
201 v6->prefix = netmask;
202 if (!parse_v6(tb[OPT_IP6ADDR]->v.string, v6, netmask))
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))
216 if (!n_v4 && !n_v6) {
217 error = "NO_ADDRESS";
221 return static_create_state(v4, n_v4, v6, n_v6);
224 error = "INVALID_ADDRESS";
227 interface_add_error(iface, "proto-static", error, NULL, 0);
231 static struct proto_handler static_proto = {
233 .attach = static_attach,
237 static_proto_init(void)
239 add_proto_handler(&static_proto);