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.
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
25 #include "interface.h"
26 #include "interface-ip.h"
29 static struct avl_tree handlers
;
42 static const struct blobmsg_policy proto_ip_attributes
[__OPT_MAX
] = {
43 [OPT_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
44 [OPT_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
45 [OPT_NETMASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
46 [OPT_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
47 [OPT_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
48 [OPT_IP6GW
] = { .name
= "ip6gw", .type
= BLOBMSG_TYPE_STRING
},
49 [OPT_IP6PREFIX
] = { .name
= "ip6prefix", .type
= BLOBMSG_TYPE_ARRAY
},
52 static const struct uci_blob_param_info proto_ip_attr_info
[__OPT_MAX
] = {
53 [OPT_IPADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
54 [OPT_IP6ADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
55 [OPT_IP6PREFIX
] = { .type
= BLOBMSG_TYPE_STRING
},
58 const struct uci_blob_param_list proto_ip_attr
= {
59 .n_params
= __OPT_MAX
,
60 .params
= proto_ip_attributes
,
61 .info
= proto_ip_attr_info
,
75 static const struct blobmsg_policy proto_ip_addr
[__ADDR_MAX
] = {
76 [ADDR_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
77 [ADDR_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
78 [ADDR_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
79 [ADDR_PTP
] = { .name
= "ptp", .type
= BLOBMSG_TYPE_STRING
},
80 [ADDR_PREFERRED
] = { .name
= "preferred", .type
= BLOBMSG_TYPE_INT32
},
81 [ADDR_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
82 [ADDR_OFFLINK
] = { .name
= "offlink", .type
= BLOBMSG_TYPE_BOOL
},
85 static struct device_addr
*
86 alloc_device_addr(bool v6
, bool ext
)
88 struct device_addr
*addr
;
90 addr
= calloc(1, sizeof(*addr
));
91 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
93 addr
->flags
|= DEVADDR_EXTERNAL
;
99 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
100 bool ext
, uint32_t broadcast
)
102 struct device_addr
*addr
;
103 int af
= v6
? AF_INET6
: AF_INET
;
105 addr
= alloc_device_addr(v6
, ext
);
110 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
)) {
111 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
117 addr
->broadcast
= broadcast
;
119 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
124 parse_static_address_option(struct interface
*iface
, struct blob_attr
*attr
,
125 bool v6
, int netmask
, bool ext
, uint32_t broadcast
)
127 struct blob_attr
*cur
;
131 blobmsg_for_each_attr(cur
, attr
, rem
) {
132 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
136 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
144 static struct device_addr
*
145 parse_address_item(struct blob_attr
*attr
, bool v6
, bool ext
)
147 struct device_addr
*addr
;
148 struct blob_attr
*tb
[__ADDR_MAX
];
149 struct blob_attr
*cur
;
151 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
154 addr
= alloc_device_addr(v6
, ext
);
158 blobmsg_parse(proto_ip_addr
, __ADDR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
160 addr
->mask
= v6
? 128 : 32;
161 if ((cur
= tb
[ADDR_MASK
])) {
162 unsigned int new_mask
;
164 new_mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
165 if (new_mask
> addr
->mask
)
168 addr
->mask
= new_mask
;
171 cur
= tb
[ADDR_IPADDR
];
175 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(cur
), &addr
->addr
))
178 if ((cur
= tb
[ADDR_OFFLINK
]) && blobmsg_get_bool(cur
))
179 addr
->flags
|= DEVADDR_OFFLINK
;
182 if ((cur
= tb
[ADDR_BROADCAST
]) &&
183 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->broadcast
))
185 if ((cur
= tb
[ADDR_PTP
]) &&
186 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->point_to_point
))
189 time_t now
= system_get_rtime();
190 if ((cur
= tb
[ADDR_PREFERRED
])) {
191 int64_t preferred
= blobmsg_get_u32(cur
);
192 int64_t preferred_until
= preferred
+ (int64_t)now
;
193 if (preferred_until
<= LONG_MAX
&& preferred
!= 0xffffffffLL
)
194 addr
->preferred_until
= preferred_until
;
197 if ((cur
= tb
[ADDR_VALID
])) {
198 int64_t valid
= blobmsg_get_u32(cur
);
199 int64_t valid_until
= valid
+ (int64_t)now
;
200 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
)
201 addr
->valid_until
= valid_until
;
205 if (addr
->valid_until
) {
206 if (!addr
->preferred_until
)
207 addr
->preferred_until
= addr
->valid_until
;
208 else if (addr
->preferred_until
> addr
->valid_until
)
221 parse_address_list(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
224 struct device_addr
*addr
;
225 struct blob_attr
*cur
;
229 blobmsg_for_each_attr(cur
, attr
, rem
) {
230 addr
= parse_address_item(cur
, v6
, ext
);
235 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
242 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
244 struct device_route
*route
;
245 const char *str
= blobmsg_data(attr
);
246 int af
= v6
? AF_INET6
: AF_INET
;
248 route
= calloc(1, sizeof(*route
));
249 if (!inet_pton(af
, str
, &route
->nexthop
)) {
250 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
256 route
->flags
= (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
258 unsigned int table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
260 route
->table
= table
;
261 route
->flags
|= DEVROUTE_SRCTABLE
;
264 vlist_add(&iface
->proto_ip
.route
, &route
->node
, route
);
270 parse_prefix_option(struct interface
*iface
, const char *str
, size_t len
)
272 char buf
[128] = {0}, *saveptr
;
273 if (len
> sizeof(buf
))
276 memcpy(buf
, str
, len
);
277 char *addrstr
= strtok_r(buf
, "/", &saveptr
);
281 char *lengthstr
= strtok_r(NULL
, ",", &saveptr
);
285 char *prefstr
= strtok_r(NULL
, ",", &saveptr
);
286 char *validstr
= (!prefstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
287 char *addstr
= (!validstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
288 const char *pclass
= NULL
;
290 int64_t pref
= (!prefstr
) ? 0 : strtoul(prefstr
, NULL
, 10);
291 int64_t valid
= (!validstr
) ? 0 : strtoul(validstr
, NULL
, 10);
293 uint8_t length
= strtoul(lengthstr
, NULL
, 10), excl_length
= 0;
294 if (length
< 1 || length
> 64)
297 struct in6_addr addr
, excluded
, *excludedp
= NULL
;
298 if (inet_pton(AF_INET6
, addrstr
, &addr
) < 1)
301 for (; addstr
; addstr
= strtok_r(NULL
, ",", &saveptr
)) {
302 char *key
= NULL
, *val
= NULL
, *addsaveptr
;
303 if (!(key
= strtok_r(addstr
, "=", &addsaveptr
)) ||
304 !(val
= strtok_r(NULL
, ",", &addsaveptr
)))
307 if (!strcmp(key
, "excluded")) {
308 char *sep
= strchr(val
, '/');
313 excl_length
= atoi(sep
+ 1);
315 if (inet_pton(AF_INET6
, val
, &excluded
) < 1)
318 excludedp
= &excluded
;
319 } else if (!strcmp(key
, "class")) {
328 int64_t now
= system_get_rtime();
329 time_t preferred_until
= 0;
330 if (prefstr
&& pref
!= 0xffffffffLL
&& pref
+ now
<= LONG_MAX
)
331 preferred_until
= pref
+ now
;
333 time_t valid_until
= 0;
334 if (validstr
&& valid
!= 0xffffffffLL
&& valid
+ now
<= LONG_MAX
)
335 valid_until
= valid
+ now
;
337 interface_ip_add_device_prefix(iface
, &addr
, length
,
338 valid_until
, preferred_until
,
339 excludedp
, excl_length
, pclass
);
344 parse_prefix_list(struct interface
*iface
, struct blob_attr
*attr
)
346 struct blob_attr
*cur
;
350 blobmsg_for_each_attr(cur
, attr
, rem
) {
351 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
355 if (!parse_prefix_option(iface
, blobmsg_data(cur
),
356 blobmsg_data_len(cur
)))
364 proto_apply_static_ip_settings(struct interface
*iface
, struct blob_attr
*attr
)
366 struct blob_attr
*tb
[__OPT_MAX
];
367 struct blob_attr
*cur
;
369 unsigned int netmask
= 32;
370 int n_v4
= 0, n_v6
= 0;
371 struct in_addr bcast
= {};
373 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
375 if ((cur
= tb
[OPT_NETMASK
])) {
376 netmask
= parse_netmask_string(blobmsg_data(cur
), false);
378 error
= "INVALID_NETMASK";
383 if ((cur
= tb
[OPT_BROADCAST
])) {
384 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &bcast
)) {
385 error
= "INVALID_BROADCAST";
390 if ((cur
= tb
[OPT_IPADDR
]))
391 n_v4
= parse_static_address_option(iface
, cur
, false,
392 netmask
, false, bcast
.s_addr
);
394 if ((cur
= tb
[OPT_IP6ADDR
]))
395 n_v6
= parse_static_address_option(iface
, cur
, true,
398 if ((cur
= tb
[OPT_IP6PREFIX
]))
399 if (parse_prefix_list(iface
, cur
) < 0)
402 if (n_v4
< 0 || n_v6
< 0)
405 if ((cur
= tb
[OPT_GATEWAY
])) {
406 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
410 if ((cur
= tb
[OPT_IP6GW
])) {
411 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
418 interface_add_error(iface
, "proto", error
, NULL
, 0);
424 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
426 struct blob_attr
*tb
[__OPT_MAX
];
427 struct blob_attr
*cur
;
428 int n_v4
= 0, n_v6
= 0;
430 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
432 if ((cur
= tb
[OPT_IPADDR
]))
433 n_v4
= parse_address_list(iface
, cur
, false, ext
);
435 if ((cur
= tb
[OPT_IP6ADDR
]))
436 n_v6
= parse_address_list(iface
, cur
, true, ext
);
438 if ((cur
= tb
[OPT_IP6PREFIX
]))
439 if (parse_prefix_list(iface
, cur
) < 0)
442 if (n_v4
< 0 || n_v6
< 0)
445 if ((cur
= tb
[OPT_GATEWAY
])) {
446 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
450 if ((cur
= tb
[OPT_IP6GW
])) {
451 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
461 void add_proto_handler(struct proto_handler
*p
)
464 avl_init(&handlers
, avl_strcmp
, false, NULL
);
469 p
->avl
.key
= p
->name
;
470 avl_insert(&handlers
, &p
->avl
);
474 default_proto_free(struct interface_proto_state
*proto
)
480 invalid_proto_handler(struct interface_proto_state
*proto
,
481 enum interface_proto_cmd cmd
, bool force
)
487 no_proto_handler(struct interface_proto_state
*proto
,
488 enum interface_proto_cmd cmd
, bool force
)
493 static struct interface_proto_state
*
494 default_proto_attach(const struct proto_handler
*h
,
495 struct interface
*iface
, struct blob_attr
*attr
)
497 struct interface_proto_state
*proto
;
499 proto
= calloc(1, sizeof(*proto
));
500 proto
->free
= default_proto_free
;
501 proto
->cb
= no_proto_handler
;
506 static const struct proto_handler no_proto
= {
508 .flags
= PROTO_FLAG_IMMEDIATE
,
509 .attach
= default_proto_attach
,
512 static const struct proto_handler
*
513 get_proto_handler(const char *name
)
515 struct proto_handler
*proto
;
517 if (!strcmp(name
, "none"))
523 return avl_find_element(&handlers
, name
, proto
, avl
);
527 proto_dump_handlers(struct blob_buf
*b
)
529 struct proto_handler
*p
;
532 avl_for_each_element(&handlers
, p
, avl
) {
533 c
= blobmsg_open_table(b
, p
->name
);
534 blobmsg_add_u8(b
, "no_device", !!(p
->flags
& PROTO_FLAG_NODEV
));
535 blobmsg_close_table(b
, c
);
540 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
542 const struct proto_handler
*proto
= iface
->proto_handler
;
543 struct interface_proto_state
*state
= NULL
;
548 state
= proto
->attach(proto
, iface
, attr
);
550 state
= no_proto
.attach(&no_proto
, iface
, attr
);
551 state
->cb
= invalid_proto_handler
;
554 state
->handler
= proto
;
555 interface_set_proto_state(iface
, state
);
559 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
561 const struct proto_handler
*proto
= &no_proto
;
564 proto
= get_proto_handler(proto_name
);
566 interface_add_error(iface
, "proto", "INVALID_PROTO", NULL
, 0);
571 iface
->proto_handler
= proto
;
575 interface_proto_event(struct interface_proto_state
*proto
,
576 enum interface_proto_cmd cmd
, bool force
)
578 enum interface_proto_event ev
;
581 ret
= proto
->cb(proto
, cmd
, force
);
582 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
586 case PROTO_CMD_SETUP
:
589 case PROTO_CMD_TEARDOWN
:
595 proto
->proto_event(proto
, ev
);