2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
24 #include "interface.h"
25 #include "interface-ip.h"
28 static struct avl_tree handlers
;
41 static const struct blobmsg_policy proto_ip_attributes
[__OPT_MAX
] = {
42 [OPT_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
43 [OPT_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
44 [OPT_NETMASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
45 [OPT_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
46 [OPT_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
47 [OPT_IP6GW
] = { .name
= "ip6gw", .type
= BLOBMSG_TYPE_STRING
},
48 [OPT_IP6PREFIX
] = { .name
= "ip6prefix", .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_IP6PREFIX
] = { .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
,
74 static const struct blobmsg_policy proto_ip_addr
[__ADDR_MAX
] = {
75 [ADDR_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
76 [ADDR_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
77 [ADDR_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
78 [ADDR_PTP
] = { .name
= "ptp", .type
= BLOBMSG_TYPE_STRING
},
79 [ADDR_PREFERRED
] = { .name
= "preferred", .type
= BLOBMSG_TYPE_INT32
},
80 [ADDR_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
81 [ADDR_OFFLINK
] = { .name
= "offlink", .type
= BLOBMSG_TYPE_BOOL
},
84 static struct device_addr
*
85 alloc_device_addr(bool v6
, bool ext
)
87 struct device_addr
*addr
;
89 addr
= calloc(1, sizeof(*addr
));
90 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
92 addr
->flags
|= DEVADDR_EXTERNAL
;
98 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
99 bool ext
, uint32_t broadcast
)
101 struct device_addr
*addr
;
102 int af
= v6
? AF_INET6
: AF_INET
;
104 addr
= alloc_device_addr(v6
, ext
);
109 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
)) {
110 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
116 addr
->broadcast
= broadcast
;
118 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
123 parse_static_address_option(struct interface
*iface
, struct blob_attr
*attr
,
124 bool v6
, int netmask
, bool ext
, uint32_t broadcast
)
126 struct blob_attr
*cur
;
130 blobmsg_for_each_attr(cur
, attr
, rem
) {
131 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
135 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
143 static struct device_addr
*
144 parse_address_item(struct blob_attr
*attr
, bool v6
, bool ext
)
146 struct device_addr
*addr
;
147 struct blob_attr
*tb
[__ADDR_MAX
];
148 struct blob_attr
*cur
;
150 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
153 addr
= alloc_device_addr(v6
, ext
);
157 blobmsg_parse(proto_ip_addr
, __ADDR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
159 addr
->mask
= v6
? 128 : 32;
160 if ((cur
= tb
[ADDR_MASK
])) {
161 unsigned int new_mask
;
163 new_mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
164 if (new_mask
> addr
->mask
)
167 addr
->mask
= new_mask
;
170 cur
= tb
[ADDR_IPADDR
];
174 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(cur
), &addr
->addr
))
177 if ((cur
= tb
[ADDR_OFFLINK
]) && blobmsg_get_bool(cur
))
178 addr
->flags
|= DEVADDR_OFFLINK
;
181 if ((cur
= tb
[ADDR_BROADCAST
]) &&
182 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->broadcast
))
184 if ((cur
= tb
[ADDR_PTP
]) &&
185 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->point_to_point
))
188 time_t now
= system_get_rtime();
189 if ((cur
= tb
[ADDR_PREFERRED
])) {
190 uint32_t preferred
= blobmsg_get_u32(cur
);
191 if (preferred
< UINT32_MAX
)
192 addr
->preferred_until
= now
+ preferred
;
195 if ((cur
= tb
[ADDR_VALID
])) {
196 uint32_t valid
= blobmsg_get_u32(cur
);
197 if (valid
< UINT32_MAX
)
198 addr
->valid_until
= now
+ valid
;
202 if (addr
->valid_until
) {
203 if (!addr
->preferred_until
)
204 addr
->preferred_until
= addr
->valid_until
;
205 else if (addr
->preferred_until
> addr
->valid_until
)
218 parse_address_list(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
221 struct device_addr
*addr
;
222 struct blob_attr
*cur
;
226 blobmsg_for_each_attr(cur
, attr
, rem
) {
227 addr
= parse_address_item(cur
, v6
, ext
);
232 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
239 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
241 struct device_route
*route
;
242 const char *str
= blobmsg_data(attr
);
243 int af
= v6
? AF_INET6
: AF_INET
;
245 route
= calloc(1, sizeof(*route
));
246 if (!inet_pton(af
, str
, &route
->nexthop
)) {
247 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
253 route
->flags
= (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
255 unsigned int table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
257 route
->table
= table
;
258 route
->flags
|= DEVROUTE_SRCTABLE
;
261 vlist_add(&iface
->proto_ip
.route
, &route
->node
, route
);
267 parse_prefix_option(struct interface
*iface
, const char *str
, size_t len
)
269 char buf
[128] = {0}, *saveptr
;
270 if (len
> sizeof(buf
))
273 memcpy(buf
, str
, len
);
274 char *addrstr
= strtok_r(buf
, "/", &saveptr
);
278 char *lengthstr
= strtok_r(NULL
, ",", &saveptr
);
282 char *prefstr
= strtok_r(NULL
, ",", &saveptr
);
283 char *validstr
= (!prefstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
284 char *addstr
= (!validstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
286 uint32_t pref
= (!prefstr
) ? 0 : strtoul(prefstr
, NULL
, 10);
287 uint32_t valid
= (!validstr
) ? 0 : strtoul(validstr
, NULL
, 10);
289 uint8_t length
= strtoul(lengthstr
, NULL
, 10), excl_length
= 0;
290 if (length
< 1 || length
> 64)
293 struct in6_addr addr
, excluded
, *excludedp
= NULL
;
294 if (inet_pton(AF_INET6
, addrstr
, &addr
) < 1)
297 for (; addstr
; addstr
= strtok_r(NULL
, ",", &saveptr
)) {
298 char *key
= NULL
, *val
= NULL
, *addsaveptr
;
299 if (!(key
= strtok_r(addstr
, "=", &addsaveptr
)) ||
300 !(val
= strtok_r(NULL
, ",", &addsaveptr
)))
303 if (!strcmp(key
, "excluded")) {
304 char *sep
= strchr(val
, '/');
309 excl_length
= atoi(sep
+ 1);
311 if (inet_pton(AF_INET6
, val
, &excluded
) < 1)
314 excludedp
= &excluded
;
322 time_t now
= system_get_rtime();
323 time_t preferred_until
= 0;
324 if (prefstr
&& pref
!= 0xffffffffU
)
325 preferred_until
= pref
+ now
;
327 time_t valid_until
= 0;
328 if (validstr
&& valid
!= 0xffffffffU
)
329 valid_until
= valid
+ now
;
331 interface_ip_add_device_prefix(iface
, &addr
, length
,
332 valid_until
, preferred_until
,
333 excludedp
, excl_length
);
338 parse_prefix_list(struct interface
*iface
, struct blob_attr
*attr
)
340 struct blob_attr
*cur
;
344 blobmsg_for_each_attr(cur
, attr
, rem
) {
345 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
349 if (!parse_prefix_option(iface
, blobmsg_data(cur
),
350 blobmsg_data_len(cur
)))
358 proto_apply_static_ip_settings(struct interface
*iface
, struct blob_attr
*attr
)
360 struct blob_attr
*tb
[__OPT_MAX
];
361 struct blob_attr
*cur
;
363 unsigned int netmask
= 32;
364 int n_v4
= 0, n_v6
= 0;
365 struct in_addr bcast
= {};
367 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
369 if ((cur
= tb
[OPT_NETMASK
])) {
370 netmask
= parse_netmask_string(blobmsg_data(cur
), false);
372 error
= "INVALID_NETMASK";
377 if ((cur
= tb
[OPT_BROADCAST
])) {
378 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &bcast
)) {
379 error
= "INVALID_BROADCAST";
384 if ((cur
= tb
[OPT_IPADDR
]))
385 n_v4
= parse_static_address_option(iface
, cur
, false,
386 netmask
, false, bcast
.s_addr
);
388 if ((cur
= tb
[OPT_IP6ADDR
]))
389 n_v6
= parse_static_address_option(iface
, cur
, true,
392 if ((cur
= tb
[OPT_IP6PREFIX
]))
393 if (parse_prefix_list(iface
, cur
) < 0)
396 if (n_v4
< 0 || n_v6
< 0)
399 if ((cur
= tb
[OPT_GATEWAY
])) {
400 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
404 if ((cur
= tb
[OPT_IP6GW
])) {
405 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
412 interface_add_error(iface
, "proto", error
, NULL
, 0);
418 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
420 struct blob_attr
*tb
[__OPT_MAX
];
421 struct blob_attr
*cur
;
422 int n_v4
= 0, n_v6
= 0;
424 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
426 if ((cur
= tb
[OPT_IPADDR
]))
427 n_v4
= parse_address_list(iface
, cur
, false, ext
);
429 if ((cur
= tb
[OPT_IP6ADDR
]))
430 n_v6
= parse_address_list(iface
, cur
, true, ext
);
432 if ((cur
= tb
[OPT_IP6PREFIX
]))
433 if (parse_prefix_list(iface
, cur
) < 0)
436 if (n_v4
< 0 || n_v6
< 0)
439 if ((cur
= tb
[OPT_GATEWAY
])) {
440 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
444 if ((cur
= tb
[OPT_IP6GW
])) {
445 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
455 void add_proto_handler(struct proto_handler
*p
)
458 avl_init(&handlers
, avl_strcmp
, false, NULL
);
463 p
->avl
.key
= p
->name
;
464 avl_insert(&handlers
, &p
->avl
);
468 default_proto_free(struct interface_proto_state
*proto
)
474 invalid_proto_handler(struct interface_proto_state
*proto
,
475 enum interface_proto_cmd cmd
, bool force
)
481 no_proto_handler(struct interface_proto_state
*proto
,
482 enum interface_proto_cmd cmd
, bool force
)
487 static struct interface_proto_state
*
488 default_proto_attach(const struct proto_handler
*h
,
489 struct interface
*iface
, struct blob_attr
*attr
)
491 struct interface_proto_state
*proto
;
493 proto
= calloc(1, sizeof(*proto
));
494 proto
->free
= default_proto_free
;
495 proto
->cb
= no_proto_handler
;
500 static const struct proto_handler no_proto
= {
502 .flags
= PROTO_FLAG_IMMEDIATE
,
503 .attach
= default_proto_attach
,
506 static const struct proto_handler
*
507 get_proto_handler(const char *name
)
509 struct proto_handler
*proto
;
511 if (!strcmp(name
, "none"))
517 return avl_find_element(&handlers
, name
, proto
, avl
);
521 proto_dump_handlers(struct blob_buf
*b
)
523 struct proto_handler
*p
;
526 avl_for_each_element(&handlers
, p
, avl
) {
527 c
= blobmsg_open_table(b
, p
->name
);
528 blobmsg_add_u8(b
, "no_device", !!(p
->flags
& PROTO_FLAG_NODEV
));
529 blobmsg_close_table(b
, c
);
534 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
536 const struct proto_handler
*proto
= iface
->proto_handler
;
537 struct interface_proto_state
*state
= NULL
;
542 state
= proto
->attach(proto
, iface
, attr
);
544 state
= no_proto
.attach(&no_proto
, iface
, attr
);
545 state
->cb
= invalid_proto_handler
;
548 state
->handler
= proto
;
549 interface_set_proto_state(iface
, state
);
553 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
555 const struct proto_handler
*proto
= &no_proto
;
558 proto
= get_proto_handler(proto_name
);
560 interface_add_error(iface
, "proto", "INVALID_PROTO", NULL
, 0);
565 iface
->proto_handler
= proto
;
569 interface_proto_event(struct interface_proto_state
*proto
,
570 enum interface_proto_cmd cmd
, bool force
)
572 enum interface_proto_event ev
;
575 ret
= proto
->cb(proto
, cmd
, force
);
576 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
580 case PROTO_CMD_SETUP
:
583 case PROTO_CMD_TEARDOWN
:
589 proto
->proto_event(proto
, ev
);