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, bool ext)
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);
135 addr->flags |= DEVADDR_EXTERNAL;
137 vlist_add(&iface->proto_ip.addr, &addr->node);
142 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6, int netmask, bool ext)
144 struct blob_attr *cur;
148 blobmsg_for_each_attr(cur, attr, rem) {
149 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
153 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext))
162 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
164 struct device_route *route;
165 const char *str = blobmsg_data(attr);
166 int af = v6 ? AF_INET6 : AF_INET;
168 route = calloc(1, sizeof(*route));
169 if (!inet_pton(af, str, &route->nexthop)) {
170 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
176 route->flags = DEVADDR_DEVICE | (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
177 vlist_add(&iface->proto_ip.route, &route->node);
183 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
185 struct blob_attr *tb[__OPT_MAX];
187 unsigned int netmask = 32;
188 int n_v4 = 0, n_v6 = 0;
190 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
192 if (tb[OPT_NETMASK]) {
193 netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false);
195 error = "INVALID_NETMASK";
201 n_v4 = parse_address_option(iface, tb[OPT_IPADDR], false, netmask, ext);
204 n_v6 = parse_address_option(iface, tb[OPT_IP6ADDR], true, netmask, ext);
206 if (!n_v4 && !n_v6) {
207 error = "NO_ADDRESS";
211 if (n_v4 < 0 || n_v6 < 0)
214 if (n_v4 && tb[OPT_GATEWAY]) {
215 if (!parse_gateway_option(iface, tb[OPT_GATEWAY], false))
219 if (n_v6 && tb[OPT_IP6GW]) {
220 if (!parse_gateway_option(iface, tb[OPT_IP6GW], true))
225 interface_add_dns_server_list(&iface->proto_ip, tb[OPT_DNS]);
230 interface_add_error(iface, "proto", error, NULL, 0);
235 void add_proto_handler(struct proto_handler *p)
238 avl_init(&handlers, avl_strcmp, false, NULL);
243 p->avl.key = p->name;
244 avl_insert(&handlers, &p->avl);
248 default_proto_free(struct interface_proto_state *proto)
254 invalid_proto_handler(struct interface_proto_state *proto,
255 enum interface_proto_cmd cmd, bool force)
261 no_proto_handler(struct interface_proto_state *proto,
262 enum interface_proto_cmd cmd, bool force)
267 static struct interface_proto_state *
268 default_proto_attach(const struct proto_handler *h,
269 struct interface *iface, struct blob_attr *attr)
271 struct interface_proto_state *proto;
273 proto = calloc(1, sizeof(*proto));
274 proto->free = default_proto_free;
275 proto->cb = no_proto_handler;
280 static const struct proto_handler no_proto = {
282 .flags = PROTO_FLAG_IMMEDIATE,
283 .attach = default_proto_attach,
286 static const struct proto_handler *
287 get_proto_handler(const char *name)
289 struct proto_handler *proto;
291 if (!strcmp(name, "none"))
297 return avl_find_element(&handlers, name, proto, avl);
301 proto_init_interface(struct interface *iface, struct blob_attr *attr)
303 const struct proto_handler *proto = iface->proto_handler;
304 struct interface_proto_state *state = NULL;
309 state = proto->attach(proto, iface, attr);
311 state = no_proto.attach(&no_proto, iface, attr);
312 state->cb = invalid_proto_handler;
315 state->handler = proto;
316 interface_set_proto_state(iface, state);
320 proto_attach_interface(struct interface *iface, const char *proto_name)
322 const struct proto_handler *proto = &no_proto;
325 proto = get_proto_handler(proto_name);
327 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
332 iface->proto_handler = proto;
336 interface_proto_event(struct interface_proto_state *proto,
337 enum interface_proto_cmd cmd, bool force)
339 enum interface_proto_event ev;
342 ret = proto->cb(proto, cmd, force);
343 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
347 case PROTO_CMD_SETUP:
350 case PROTO_CMD_TEARDOWN:
356 proto->proto_event(proto, ev);