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
;
44 static const struct blobmsg_policy proto_ip_attributes
[__OPT_MAX
] = {
45 [OPT_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
46 [OPT_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
47 [OPT_NETMASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
48 [OPT_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
49 [OPT_PTPADDR
] = { .name
= "ptpaddr", .type
= BLOBMSG_TYPE_STRING
},
50 [OPT_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
51 [OPT_IP6GW
] = { .name
= "ip6gw", .type
= BLOBMSG_TYPE_STRING
},
52 [OPT_IP6PREFIX
] = { .name
= "ip6prefix", .type
= BLOBMSG_TYPE_ARRAY
},
53 [OPT_IP6DEPRECATED
] = { .name
= "ip6deprecated", .type
= BLOBMSG_TYPE_BOOL
},
56 static const struct uci_blob_param_info proto_ip_attr_info
[__OPT_MAX
] = {
57 [OPT_IPADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
58 [OPT_IP6ADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
59 [OPT_IP6PREFIX
] = { .type
= BLOBMSG_TYPE_STRING
},
62 static const char * const proto_ip_validate
[__OPT_MAX
] = {
63 [OPT_IPADDR
] = "ip4addr",
64 [OPT_IP6ADDR
] = "ip6addr",
65 [OPT_NETMASK
] = "netmask",
66 [OPT_BROADCAST
] = "ipaddr",
67 [OPT_PTPADDR
] = "ip4addr",
68 [OPT_GATEWAY
] = "ip4addr",
69 [OPT_IP6GW
] = "ip6addr",
70 [OPT_IP6PREFIX
] = "ip6addr",
73 const struct uci_blob_param_list proto_ip_attr
= {
74 .n_params
= __OPT_MAX
,
75 .params
= proto_ip_attributes
,
76 .validate
= proto_ip_validate
,
77 .info
= proto_ip_attr_info
,
92 static const struct blobmsg_policy proto_ip_addr
[__ADDR_MAX
] = {
93 [ADDR_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
94 [ADDR_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
95 [ADDR_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
96 [ADDR_PTP
] = { .name
= "ptp", .type
= BLOBMSG_TYPE_STRING
},
97 [ADDR_PREFERRED
] = { .name
= "preferred", .type
= BLOBMSG_TYPE_INT32
},
98 [ADDR_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
99 [ADDR_OFFLINK
] = { .name
= "offlink", .type
= BLOBMSG_TYPE_BOOL
},
100 [ADDR_CLASS
] = { .name
= "class", .type
= BLOBMSG_TYPE_STRING
},
103 static struct device_addr
*
104 alloc_device_addr(bool v6
, bool ext
)
106 struct device_addr
*addr
;
108 addr
= calloc(1, sizeof(*addr
));
112 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
114 addr
->flags
|= DEVADDR_EXTERNAL
;
120 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
121 bool ext
, uint32_t broadcast
, uint32_t ptp
, bool deprecated
)
123 struct device_addr
*addr
;
124 int af
= v6
? AF_INET6
: AF_INET
;
126 addr
= alloc_device_addr(v6
, ext
);
131 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
))
135 if (IN_EXPERIMENTAL(ntohl(addr
->addr
.in
.s_addr
)))
138 } else if (IN6_IS_ADDR_MULTICAST(&addr
->addr
.in6
))
142 addr
->broadcast
= broadcast
;
145 addr
->point_to_point
= ptp
;
148 addr
->preferred_until
= system_get_rtime();
150 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
154 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
161 parse_static_address_option(struct interface
*iface
, struct blob_attr
*attr
,
162 bool v6
, int netmask
, bool ext
, uint32_t broadcast
,
163 uint32_t ptp
, bool deprecated
)
165 struct blob_attr
*cur
;
169 blobmsg_for_each_attr(cur
, attr
, rem
) {
170 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
174 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
175 broadcast
, ptp
, deprecated
))
182 static struct device_addr
*
183 parse_address_item(struct blob_attr
*attr
, bool v6
, bool ext
)
185 struct device_addr
*addr
;
186 struct blob_attr
*tb
[__ADDR_MAX
];
187 struct blob_attr
*cur
;
189 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
192 addr
= alloc_device_addr(v6
, ext
);
196 blobmsg_parse(proto_ip_addr
, __ADDR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
198 addr
->mask
= v6
? 128 : 32;
199 if ((cur
= tb
[ADDR_MASK
])) {
200 unsigned int new_mask
;
202 new_mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
203 if (new_mask
> addr
->mask
)
206 addr
->mask
= new_mask
;
209 cur
= tb
[ADDR_IPADDR
];
213 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(cur
), &addr
->addr
))
216 if ((cur
= tb
[ADDR_OFFLINK
]) && blobmsg_get_bool(cur
))
217 addr
->flags
|= DEVADDR_OFFLINK
;
220 if ((cur
= tb
[ADDR_BROADCAST
]) &&
221 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->broadcast
))
223 if ((cur
= tb
[ADDR_PTP
]) &&
224 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->point_to_point
))
227 time_t now
= system_get_rtime();
228 if ((cur
= tb
[ADDR_PREFERRED
])) {
229 int64_t preferred
= blobmsg_get_u32(cur
);
230 int64_t preferred_until
= preferred
+ (int64_t)now
;
231 if (preferred_until
<= LONG_MAX
&& preferred
!= 0xffffffffLL
)
232 addr
->preferred_until
= preferred_until
;
235 if ((cur
= tb
[ADDR_VALID
])) {
236 int64_t valid
= blobmsg_get_u32(cur
);
237 int64_t valid_until
= valid
+ (int64_t)now
;
238 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
)
239 addr
->valid_until
= valid_until
;
243 if (addr
->valid_until
) {
244 if (!addr
->preferred_until
)
245 addr
->preferred_until
= addr
->valid_until
;
246 else if (addr
->preferred_until
> addr
->valid_until
)
250 if ((cur
= tb
[ADDR_CLASS
]))
251 addr
->pclass
= strdup(blobmsg_get_string(cur
));
262 parse_address_list(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
265 struct device_addr
*addr
;
266 struct blob_attr
*cur
;
270 blobmsg_for_each_attr(cur
, attr
, rem
) {
271 addr
= parse_address_item(cur
, v6
, ext
);
276 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
283 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
285 struct device_route
*route
;
286 const char *str
= blobmsg_data(attr
);
287 int af
= v6
? AF_INET6
: AF_INET
;
289 route
= calloc(1, sizeof(*route
));
293 if (!inet_pton(af
, str
, &route
->nexthop
)) {
294 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
300 route
->flags
= (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
301 route
->metric
= iface
->metric
;
303 unsigned int table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
305 route
->table
= table
;
306 route
->flags
|= DEVROUTE_SRCTABLE
;
309 vlist_add(&iface
->proto_ip
.route
, &route
->node
, route
);
315 parse_prefix_option(struct interface
*iface
, const char *str
, size_t len
)
317 char buf
[128] = {0}, *saveptr
;
318 if (len
>= sizeof(buf
))
321 memcpy(buf
, str
, len
);
322 char *addrstr
= strtok_r(buf
, "/", &saveptr
);
326 char *lengthstr
= strtok_r(NULL
, ",", &saveptr
);
330 char *prefstr
= strtok_r(NULL
, ",", &saveptr
);
331 char *validstr
= (!prefstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
332 char *addstr
= (!validstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
333 const char *pclass
= NULL
;
335 int64_t pref
= (!prefstr
) ? 0 : strtoul(prefstr
, NULL
, 10);
336 int64_t valid
= (!validstr
) ? 0 : strtoul(validstr
, NULL
, 10);
338 uint8_t length
= strtoul(lengthstr
, NULL
, 10), excl_length
= 0;
339 if (length
< 1 || length
> 64)
342 struct in6_addr addr
, excluded
, *excludedp
= NULL
;
343 if (inet_pton(AF_INET6
, addrstr
, &addr
) < 1)
346 for (; addstr
; addstr
= strtok_r(NULL
, ",", &saveptr
)) {
347 char *key
= NULL
, *val
= NULL
, *addsaveptr
;
348 if (!(key
= strtok_r(addstr
, "=", &addsaveptr
)) ||
349 !(val
= strtok_r(NULL
, ",", &addsaveptr
)))
352 if (!strcmp(key
, "excluded")) {
353 char *sep
= strchr(val
, '/');
358 excl_length
= atoi(sep
+ 1);
360 if (inet_pton(AF_INET6
, val
, &excluded
) < 1)
363 excludedp
= &excluded
;
364 } else if (!strcmp(key
, "class")) {
373 int64_t now
= system_get_rtime();
374 time_t preferred_until
= 0;
375 if (prefstr
&& pref
!= 0xffffffffLL
&& pref
+ now
<= LONG_MAX
)
376 preferred_until
= pref
+ now
;
378 time_t valid_until
= 0;
379 if (validstr
&& valid
!= 0xffffffffLL
&& valid
+ now
<= LONG_MAX
)
380 valid_until
= valid
+ now
;
382 interface_ip_add_device_prefix(iface
, &addr
, length
,
383 valid_until
, preferred_until
,
384 excludedp
, excl_length
, pclass
);
389 parse_prefix_list(struct interface
*iface
, struct blob_attr
*attr
)
391 struct blob_attr
*cur
;
395 blobmsg_for_each_attr(cur
, attr
, rem
) {
396 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
400 if (!parse_prefix_option(iface
, blobmsg_data(cur
),
401 blobmsg_data_len(cur
)))
409 proto_apply_static_ip_settings(struct interface
*iface
, struct blob_attr
*attr
)
411 struct blob_attr
*tb
[__OPT_MAX
];
412 struct blob_attr
*cur
;
414 unsigned int netmask
= 32;
416 int n_v4
= 0, n_v6
= 0;
417 struct in_addr bcast
= {}, ptp
= {};
419 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
421 if ((cur
= tb
[OPT_NETMASK
])) {
422 netmask
= parse_netmask_string(blobmsg_data(cur
), false);
424 error
= "INVALID_NETMASK";
429 if ((cur
= tb
[OPT_BROADCAST
])) {
430 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &bcast
)) {
431 error
= "INVALID_BROADCAST";
436 if ((cur
= tb
[OPT_PTPADDR
])) {
437 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &ptp
)) {
438 error
= "INVALID_PTPADDR";
443 ip6deprecated
= blobmsg_get_bool_default(tb
[OPT_IP6DEPRECATED
], false);
445 if ((cur
= tb
[OPT_IPADDR
]))
446 n_v4
= parse_static_address_option(iface
, cur
, false,
447 netmask
, false, bcast
.s_addr
, ptp
.s_addr
, false);
449 if ((cur
= tb
[OPT_IP6ADDR
]))
450 n_v6
= parse_static_address_option(iface
, cur
, true,
451 128, false, 0, 0, ip6deprecated
);
453 if ((cur
= tb
[OPT_IP6PREFIX
]))
454 if (parse_prefix_list(iface
, cur
) < 0)
457 if (n_v4
< 0 || n_v6
< 0)
460 if ((cur
= tb
[OPT_GATEWAY
])) {
461 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
465 if ((cur
= tb
[OPT_IP6GW
])) {
466 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
473 interface_add_error(iface
, "proto", error
, NULL
, 0);
479 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
481 struct blob_attr
*tb
[__OPT_MAX
];
482 struct blob_attr
*cur
;
483 int n_v4
= 0, n_v6
= 0;
485 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
487 if ((cur
= tb
[OPT_IPADDR
]))
488 n_v4
= parse_address_list(iface
, cur
, false, ext
);
490 if ((cur
= tb
[OPT_IP6ADDR
]))
491 n_v6
= parse_address_list(iface
, cur
, true, ext
);
493 if ((cur
= tb
[OPT_IP6PREFIX
]))
494 if (parse_prefix_list(iface
, cur
) < 0)
497 if (n_v4
< 0 || n_v6
< 0)
500 if ((cur
= tb
[OPT_GATEWAY
])) {
501 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
505 if ((cur
= tb
[OPT_IP6GW
])) {
506 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
516 void add_proto_handler(struct proto_handler
*p
)
519 avl_init(&handlers
, avl_strcmp
, false, NULL
);
524 p
->avl
.key
= p
->name
;
525 avl_insert(&handlers
, &p
->avl
);
529 default_proto_free(struct interface_proto_state
*proto
)
535 invalid_proto_handler(struct interface_proto_state
*proto
,
536 enum interface_proto_cmd cmd
, bool force
)
542 no_proto_handler(struct interface_proto_state
*proto
,
543 enum interface_proto_cmd cmd
, bool force
)
548 static struct interface_proto_state
*
549 default_proto_attach(const struct proto_handler
*h
,
550 struct interface
*iface
, struct blob_attr
*attr
)
552 struct interface_proto_state
*proto
;
554 proto
= calloc(1, sizeof(*proto
));
558 proto
->free
= default_proto_free
;
559 proto
->cb
= no_proto_handler
;
564 static const struct proto_handler no_proto
= {
566 .flags
= PROTO_FLAG_IMMEDIATE
,
567 .attach
= default_proto_attach
,
570 static const struct proto_handler
*
571 get_proto_handler(const char *name
)
573 struct proto_handler
*proto
;
575 if (!strcmp(name
, "none"))
581 return avl_find_element(&handlers
, name
, proto
, avl
);
585 proto_dump_handlers(struct blob_buf
*b
)
587 struct proto_handler
*p
;
590 avl_for_each_element(&handlers
, p
, avl
) {
593 c
= blobmsg_open_table(b
, p
->name
);
594 if (p
->config_params
&& p
->config_params
->validate
) {
597 v
= blobmsg_open_table(b
, "validate");
598 for (i
= 0; i
< p
->config_params
->n_params
; i
++)
599 blobmsg_add_string(b
, p
->config_params
->params
[i
].name
, uci_get_validate_string(p
->config_params
, i
));
600 blobmsg_close_table(b
, v
);
602 blobmsg_add_u8(b
, "immediate", !!(p
->flags
& PROTO_FLAG_IMMEDIATE
));
603 blobmsg_add_u8(b
, "no_device", !!(p
->flags
& PROTO_FLAG_NODEV
));
604 blobmsg_add_u8(b
, "init_available", !!(p
->flags
& PROTO_FLAG_INIT_AVAILABLE
));
605 blobmsg_add_u8(b
, "renew_available", !!(p
->flags
& PROTO_FLAG_RENEW_AVAILABLE
));
606 blobmsg_add_u8(b
, "force_link_default", !!(p
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
));
607 blobmsg_add_u8(b
, "last_error", !!(p
->flags
& PROTO_FLAG_LASTERROR
));
608 blobmsg_add_u8(b
, "teardown_on_l3_link_down", !!(p
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
));
609 blobmsg_add_u8(b
, "no_task", !!(p
->flags
& PROTO_FLAG_NO_TASK
));
610 blobmsg_close_table(b
, c
);
615 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
617 const struct proto_handler
*proto
= iface
->proto_handler
;
618 struct interface_proto_state
*state
= NULL
;
623 state
= proto
->attach(proto
, iface
, attr
);
625 state
= no_proto
.attach(&no_proto
, iface
, attr
);
626 state
->cb
= invalid_proto_handler
;
629 state
->handler
= proto
;
630 interface_set_proto_state(iface
, state
);
634 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
636 const struct proto_handler
*proto
= &no_proto
;
637 const char *error
= NULL
;
640 proto
= get_proto_handler(proto_name
);
642 error
= "INVALID_PROTO";
647 iface
->proto_handler
= proto
;
650 interface_add_error(iface
, "proto", error
, NULL
, 0);
654 interface_proto_event(struct interface_proto_state
*proto
,
655 enum interface_proto_cmd cmd
, bool force
)
657 enum interface_proto_event ev
;
660 ret
= proto
->cb(proto
, cmd
, force
);
661 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
665 case PROTO_CMD_SETUP
:
668 case PROTO_CMD_TEARDOWN
:
671 case PROTO_CMD_RENEW
:
677 proto
->proto_event(proto
, ev
);