6 #include <netinet/in.h>
10 #include "interface-ip.h"
13 static struct avl_tree handlers;
27 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
28 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
29 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
30 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
31 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
32 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
33 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
34 [OPT_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
35 [OPT_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
38 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
39 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
40 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
41 [OPT_DNS] = { .type = BLOBMSG_TYPE_STRING },
44 const struct config_param_list proto_ip_attr = {
45 .n_params = __OPT_MAX,
46 .params = proto_ip_attributes,
47 .info = proto_ip_attr_info,
52 parse_netmask_string(const char *str, bool v6)
58 if (!strchr(str, '.')) {
59 ret = strtoul(str, &err, 0);
69 if (inet_aton(str, &addr) != 1)
72 return 32 - fls(~(ntohl(addr.s_addr)));
79 split_netmask(char *str, unsigned int *netmask, bool v6)
81 char *delim = strchr(str, '/');
86 *netmask = parse_netmask_string(delim, v6);
92 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
94 char *astr = alloca(strlen(str) + 1);
97 if (!split_netmask(astr, netmask, af == AF_INET6))
100 if (af == AF_INET6) {
108 return inet_pton(af, astr, addr);
111 static struct device_addr *
112 proto_parse_ip_addr_string(const char *str, bool v6, int mask)
114 struct device_addr *addr;
115 int af = v6 ? AF_INET6 : AF_INET;
117 addr = calloc(1, sizeof(*addr));
118 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
120 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
128 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
129 bool ext, uint32_t broadcast)
131 struct device_addr *addr;
133 addr = proto_parse_ip_addr_string(str, v6, mask);
135 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
140 addr->broadcast = broadcast;
143 addr->flags |= DEVADDR_EXTERNAL;
145 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->mask);
150 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6,
151 int netmask, bool ext, uint32_t broadcast)
153 struct blob_attr *cur;
157 blobmsg_for_each_attr(cur, attr, rem) {
158 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
162 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
172 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
174 struct device_route *route;
175 const char *str = blobmsg_data(attr);
176 int af = v6 ? AF_INET6 : AF_INET;
178 route = calloc(1, sizeof(*route));
179 if (!inet_pton(af, str, &route->nexthop)) {
180 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
186 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
187 vlist_add(&iface->proto_ip.route, &route->node, &route->mask);
193 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
195 struct blob_attr *tb[__OPT_MAX];
196 struct blob_attr *cur;
198 unsigned int netmask = 32;
199 int n_v4 = 0, n_v6 = 0;
200 struct in_addr bcast = {};
202 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
204 if ((cur = tb[OPT_NETMASK])) {
205 netmask = parse_netmask_string(blobmsg_data(cur), false);
207 error = "INVALID_NETMASK";
212 if ((cur = tb[OPT_BROADCAST])) {
213 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
214 error = "INVALID_BROADCAST";
219 if ((cur = tb[OPT_IPADDR]))
220 n_v4 = parse_address_option(iface, cur, false,
221 netmask, ext, bcast.s_addr);
223 if ((cur = tb[OPT_IP6ADDR]))
224 n_v6 = parse_address_option(iface, cur, true,
227 if (!n_v4 && !n_v6) {
228 error = "NO_ADDRESS";
232 if (n_v4 < 0 || n_v6 < 0)
235 if ((cur = tb[OPT_GATEWAY])) {
236 if (n_v4 && !parse_gateway_option(iface, cur, false))
240 if ((cur = tb[OPT_IP6GW])) {
241 if (n_v6 && !parse_gateway_option(iface, cur, true))
245 if ((cur = tb[OPT_DNS]))
246 interface_add_dns_server_list(&iface->proto_ip, cur);
248 if ((cur = tb[OPT_DNS_SEARCH]))
249 interface_add_dns_search_list(&iface->proto_ip, cur);
254 interface_add_error(iface, "proto", error, NULL, 0);
259 void add_proto_handler(struct proto_handler *p)
262 avl_init(&handlers, avl_strcmp, false, NULL);
267 p->avl.key = p->name;
268 avl_insert(&handlers, &p->avl);
272 default_proto_free(struct interface_proto_state *proto)
278 invalid_proto_handler(struct interface_proto_state *proto,
279 enum interface_proto_cmd cmd, bool force)
285 no_proto_handler(struct interface_proto_state *proto,
286 enum interface_proto_cmd cmd, bool force)
291 static struct interface_proto_state *
292 default_proto_attach(const struct proto_handler *h,
293 struct interface *iface, struct blob_attr *attr)
295 struct interface_proto_state *proto;
297 proto = calloc(1, sizeof(*proto));
298 proto->free = default_proto_free;
299 proto->cb = no_proto_handler;
304 static const struct proto_handler no_proto = {
306 .flags = PROTO_FLAG_IMMEDIATE,
307 .attach = default_proto_attach,
310 static const struct proto_handler *
311 get_proto_handler(const char *name)
313 struct proto_handler *proto;
315 if (!strcmp(name, "none"))
321 return avl_find_element(&handlers, name, proto, avl);
325 proto_init_interface(struct interface *iface, struct blob_attr *attr)
327 const struct proto_handler *proto = iface->proto_handler;
328 struct interface_proto_state *state = NULL;
333 state = proto->attach(proto, iface, attr);
335 state = no_proto.attach(&no_proto, iface, attr);
336 state->cb = invalid_proto_handler;
339 state->handler = proto;
340 interface_set_proto_state(iface, state);
344 proto_attach_interface(struct interface *iface, const char *proto_name)
346 const struct proto_handler *proto = &no_proto;
349 proto = get_proto_handler(proto_name);
351 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
356 iface->proto_handler = proto;
360 interface_proto_event(struct interface_proto_state *proto,
361 enum interface_proto_cmd cmd, bool force)
363 enum interface_proto_event ev;
366 ret = proto->cb(proto, cmd, force);
367 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
371 case PROTO_CMD_SETUP:
374 case PROTO_CMD_TEARDOWN:
380 proto->proto_event(proto, ev);