2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
22 #include "interface.h"
23 #include "interface-ip.h"
26 static struct avl_tree handlers
;
40 static const struct blobmsg_policy proto_ip_attributes
[__OPT_MAX
] = {
41 [OPT_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
42 [OPT_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
43 [OPT_NETMASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
44 [OPT_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
45 [OPT_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
46 [OPT_IP6GW
] = { .name
= "ip6gw", .type
= BLOBMSG_TYPE_STRING
},
47 [OPT_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
48 [OPT_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
51 static const union config_param_info proto_ip_attr_info
[__OPT_MAX
] = {
52 [OPT_IPADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
53 [OPT_IP6ADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
54 [OPT_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
57 const struct config_param_list proto_ip_attr
= {
58 .n_params
= __OPT_MAX
,
59 .params
= proto_ip_attributes
,
60 .info
= proto_ip_attr_info
,
71 static const struct blobmsg_policy proto_ip_addr
[__ADDR_MAX
] = {
72 [ADDR_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
73 [ADDR_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
74 [ADDR_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
75 [ADDR_PTP
] = { .name
= "ptp", .type
= BLOBMSG_TYPE_STRING
},
79 parse_netmask_string(const char *str
, bool v6
)
85 if (!strchr(str
, '.')) {
86 ret
= strtoul(str
, &err
, 0);
96 if (inet_aton(str
, &addr
) != 1)
99 return 32 - fls(~(ntohl(addr
.s_addr
)));
106 split_netmask(char *str
, unsigned int *netmask
, bool v6
)
108 char *delim
= strchr(str
, '/');
113 *netmask
= parse_netmask_string(delim
, v6
);
119 parse_ip_and_netmask(int af
, const char *str
, void *addr
, unsigned int *netmask
)
121 char *astr
= alloca(strlen(str
) + 1);
124 if (!split_netmask(astr
, netmask
, af
== AF_INET6
))
127 if (af
== AF_INET6
) {
135 return inet_pton(af
, astr
, addr
);
138 static struct device_addr
*
139 alloc_device_addr(bool v6
, bool ext
)
141 struct device_addr
*addr
;
143 addr
= calloc(1, sizeof(*addr
));
144 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
146 addr
->flags
|= DEVADDR_EXTERNAL
;
152 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
153 bool ext
, uint32_t broadcast
)
155 struct device_addr
*addr
;
156 int af
= v6
? AF_INET6
: AF_INET
;
158 addr
= alloc_device_addr(v6
, ext
);
163 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
)) {
164 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
170 addr
->broadcast
= broadcast
;
172 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
177 parse_static_address_option(struct interface
*iface
, struct blob_attr
*attr
,
178 bool v6
, int netmask
, bool ext
, uint32_t broadcast
)
180 struct blob_attr
*cur
;
184 blobmsg_for_each_attr(cur
, attr
, rem
) {
185 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
189 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
197 static struct device_addr
*
198 parse_address_item(struct blob_attr
*attr
, bool v6
, bool ext
)
200 struct device_addr
*addr
;
201 struct blob_attr
*tb
[__ADDR_MAX
];
202 struct blob_attr
*cur
;
204 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
207 addr
= alloc_device_addr(v6
, ext
);
211 blobmsg_parse(proto_ip_addr
, __ADDR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
213 addr
->mask
= v6
? 128 : 32;
214 if ((cur
= tb
[ADDR_MASK
])) {
215 unsigned int new_mask
;
217 new_mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
218 if (new_mask
> addr
->mask
)
221 addr
->mask
= new_mask
;
224 cur
= tb
[ADDR_IPADDR
];
228 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(cur
), &addr
->addr
))
232 if ((cur
= tb
[ADDR_BROADCAST
]) &&
233 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->broadcast
))
235 if ((cur
= tb
[ADDR_PTP
]) &&
236 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->point_to_point
))
248 parse_address_list(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
251 struct device_addr
*addr
;
252 struct blob_attr
*cur
;
256 blobmsg_for_each_attr(cur
, attr
, rem
) {
257 addr
= parse_address_item(cur
, v6
, ext
);
262 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
269 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
271 struct device_route
*route
;
272 const char *str
= blobmsg_data(attr
);
273 int af
= v6
? AF_INET6
: AF_INET
;
275 route
= calloc(1, sizeof(*route
));
276 if (!inet_pton(af
, str
, &route
->nexthop
)) {
277 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
283 route
->flags
= (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
284 vlist_add(&iface
->proto_ip
.route
, &route
->node
, &route
->flags
);
290 proto_apply_static_ip_settings(struct interface
*iface
, struct blob_attr
*attr
)
292 struct blob_attr
*tb
[__OPT_MAX
];
293 struct blob_attr
*cur
;
295 unsigned int netmask
= 32;
296 int n_v4
= 0, n_v6
= 0;
297 struct in_addr bcast
= {};
299 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
301 if ((cur
= tb
[OPT_NETMASK
])) {
302 netmask
= parse_netmask_string(blobmsg_data(cur
), false);
304 error
= "INVALID_NETMASK";
309 if ((cur
= tb
[OPT_BROADCAST
])) {
310 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &bcast
)) {
311 error
= "INVALID_BROADCAST";
316 if ((cur
= tb
[OPT_IPADDR
]))
317 n_v4
= parse_static_address_option(iface
, cur
, false,
318 netmask
, false, bcast
.s_addr
);
320 if ((cur
= tb
[OPT_IP6ADDR
]))
321 n_v6
= parse_static_address_option(iface
, cur
, true,
324 if (!n_v4
&& !n_v6
) {
325 error
= "NO_ADDRESS";
329 if (n_v4
< 0 || n_v6
< 0)
332 if ((cur
= tb
[OPT_GATEWAY
])) {
333 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
337 if ((cur
= tb
[OPT_IP6GW
])) {
338 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
342 if ((cur
= tb
[OPT_DNS
]))
343 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
345 if ((cur
= tb
[OPT_DNS_SEARCH
]))
346 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
351 interface_add_error(iface
, "proto", error
, NULL
, 0);
357 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
359 struct blob_attr
*tb
[__OPT_MAX
];
360 struct blob_attr
*cur
;
362 int n_v4
= 0, n_v6
= 0;
364 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
366 if ((cur
= tb
[OPT_IPADDR
]))
367 n_v4
= parse_address_list(iface
, cur
, false, ext
);
369 if ((cur
= tb
[OPT_IP6ADDR
]))
370 n_v6
= parse_address_list(iface
, cur
, true, ext
);
372 if (!n_v4
&& !n_v6
) {
373 error
= "NO_ADDRESS";
377 if (n_v4
< 0 || n_v6
< 0)
380 if ((cur
= tb
[OPT_GATEWAY
])) {
381 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
385 if ((cur
= tb
[OPT_IP6GW
])) {
386 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
390 if ((cur
= tb
[OPT_DNS
]))
391 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
393 if ((cur
= tb
[OPT_DNS_SEARCH
]))
394 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
399 interface_add_error(iface
, "proto", error
, NULL
, 0);
404 void add_proto_handler(struct proto_handler
*p
)
407 avl_init(&handlers
, avl_strcmp
, false, NULL
);
412 p
->avl
.key
= p
->name
;
413 avl_insert(&handlers
, &p
->avl
);
417 default_proto_free(struct interface_proto_state
*proto
)
423 invalid_proto_handler(struct interface_proto_state
*proto
,
424 enum interface_proto_cmd cmd
, bool force
)
430 no_proto_handler(struct interface_proto_state
*proto
,
431 enum interface_proto_cmd cmd
, bool force
)
436 static struct interface_proto_state
*
437 default_proto_attach(const struct proto_handler
*h
,
438 struct interface
*iface
, struct blob_attr
*attr
)
440 struct interface_proto_state
*proto
;
442 proto
= calloc(1, sizeof(*proto
));
443 proto
->free
= default_proto_free
;
444 proto
->cb
= no_proto_handler
;
449 static const struct proto_handler no_proto
= {
451 .flags
= PROTO_FLAG_IMMEDIATE
,
452 .attach
= default_proto_attach
,
455 static const struct proto_handler
*
456 get_proto_handler(const char *name
)
458 struct proto_handler
*proto
;
460 if (!strcmp(name
, "none"))
466 return avl_find_element(&handlers
, name
, proto
, avl
);
470 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
472 const struct proto_handler
*proto
= iface
->proto_handler
;
473 struct interface_proto_state
*state
= NULL
;
478 state
= proto
->attach(proto
, iface
, attr
);
480 state
= no_proto
.attach(&no_proto
, iface
, attr
);
481 state
->cb
= invalid_proto_handler
;
484 state
->handler
= proto
;
485 interface_set_proto_state(iface
, state
);
489 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
491 const struct proto_handler
*proto
= &no_proto
;
494 proto
= get_proto_handler(proto_name
);
496 interface_add_error(iface
, "proto", "INVALID_PROTO", NULL
, 0);
501 iface
->proto_handler
= proto
;
505 interface_proto_event(struct interface_proto_state
*proto
,
506 enum interface_proto_cmd cmd
, bool force
)
508 enum interface_proto_event ev
;
511 ret
= proto
->cb(proto
, cmd
, force
);
512 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
516 case PROTO_CMD_SETUP
:
519 case PROTO_CMD_TEARDOWN
:
525 proto
->proto_event(proto
, ev
);