6 #include <netinet/in.h>
10 #include "interface-ip.h"
13 static struct avl_tree handlers;
25 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
26 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
27 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
28 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
29 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
30 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
31 [OPT_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
34 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
35 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
36 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
37 [OPT_DNS] = { .type = BLOBMSG_TYPE_STRING },
40 const struct config_param_list proto_ip_attr = {
41 .n_params = __OPT_MAX,
42 .params = proto_ip_attributes,
43 .info = proto_ip_attr_info,
48 parse_netmask_string(const char *str, bool v6)
54 if (!strchr(str, '.')) {
55 ret = strtoul(str, &err, 0);
65 if (inet_aton(str, &addr) != 1)
68 return 32 - fls(~(ntohl(addr.s_addr)));
75 split_netmask(char *str, unsigned int *netmask, bool v6)
77 char *delim = strchr(str, '/');
82 *netmask = parse_netmask_string(delim, v6);
88 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
90 char *astr = alloca(strlen(str) + 1);
93 if (!split_netmask(astr, netmask, af == AF_INET6))
104 return inet_pton(af, astr, addr);
108 proto_parse_ip_addr_string(const char *str, bool v6, int mask)
110 struct device_addr *addr;
111 int af = v6 ? AF_INET6 : AF_INET;
113 addr = calloc(1, sizeof(*addr));
114 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
116 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
124 parse_addr(struct interface *iface, const char *str, bool v6, int mask)
126 struct device_addr *addr;
128 addr = proto_parse_ip_addr_string(str, v6, mask);
130 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
133 vlist_add(&iface->proto_ip.addr, &addr->node);
138 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6, int netmask)
140 struct blob_attr *cur;
144 blobmsg_for_each_attr(cur, attr, rem) {
146 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask))
155 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
157 struct device_route *route;
158 const char *str = blobmsg_data(attr);
159 int af = v6 ? AF_INET6 : AF_INET;
161 route = calloc(1, sizeof(*route));
162 if (!inet_pton(af, str, &route->nexthop)) {
163 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
169 route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
170 vlist_add(&iface->proto_ip.route, &route->node);
176 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr)
178 struct blob_attr *tb[__OPT_MAX];
180 unsigned int netmask = 32;
181 int n_v4 = 0, n_v6 = 0;
183 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
185 if (tb[OPT_NETMASK]) {
186 netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false);
188 error = "INVALID_NETMASK";
194 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask);
197 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask);
199 if (!n_v4 && !n_v6) {
200 error = "NO_ADDRESS";
204 if (n_v4 < 0 || n_v6 < 0)
207 if (n_v4 && tb[OPT_GATEWAY]) {
208 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
212 if (n_v6 && tb[OPT_IP6GW]) {
213 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
218 interface_add_dns_server_list(&iface->proto_ip, tb[OPT_DNS]);
223 interface_add_error(iface, "proto", error, NULL, 0);
228 void add_proto_handler(struct proto_handler *p)
231 avl_init(&handlers, avl_strcmp, false, NULL);
236 p->avl.key = p->name;
237 avl_insert(&handlers, &p->avl);
241 default_proto_free(struct interface_proto_state *proto)
247 invalid_proto_handler(struct interface_proto_state *proto,
248 enum interface_proto_cmd cmd, bool force)
254 no_proto_handler(struct interface_proto_state *proto,
255 enum interface_proto_cmd cmd, bool force)
260 static struct interface_proto_state *
261 default_proto_attach(const struct proto_handler *h,
262 struct interface *iface, struct blob_attr *attr)
264 struct interface_proto_state *proto;
266 proto = calloc(1, sizeof(*proto));
267 proto->free = default_proto_free;
268 proto->cb = no_proto_handler;
273 static const struct proto_handler no_proto = {
275 .flags = PROTO_FLAG_IMMEDIATE,
276 .attach = default_proto_attach,
279 static const struct proto_handler *
280 get_proto_handler(const char *name)
282 struct proto_handler *proto;
284 if (!strcmp(name, "none"))
290 return avl_find_element(&handlers, name, proto, avl);
294 proto_init_interface(struct interface *iface, struct blob_attr *attr)
296 const struct proto_handler *proto = iface->proto_handler;
297 struct interface_proto_state *state = NULL;
302 state = proto->attach(proto, iface, attr);
304 state = no_proto.attach(&no_proto, iface, attr);
305 state->cb = invalid_proto_handler;
308 state->handler = proto;
309 interface_set_proto_state(iface, state);
313 proto_attach_interface(struct interface *iface, const char *proto_name)
315 const struct proto_handler *proto = &no_proto;
318 proto = get_proto_handler(proto_name);
320 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
325 iface->proto_handler = proto;
329 interface_proto_event(struct interface_proto_state *proto,
330 enum interface_proto_cmd cmd, bool force)
332 enum interface_proto_event ev;
335 ret = proto->cb(proto, cmd, force);
336 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
340 case PROTO_CMD_SETUP:
343 case PROTO_CMD_TEARDOWN:
349 proto->proto_event(proto, ev);