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>
23 #include "interface.h"
24 #include "interface-ip.h"
27 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_IP6PREFIX
] = { .name
= "ip6prefix", .type
= BLOBMSG_TYPE_ARRAY
},
50 static const union config_param_info proto_ip_attr_info
[__OPT_MAX
] = {
51 [OPT_IPADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
52 [OPT_IP6ADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
53 [OPT_IP6PREFIX
] = { .type
= BLOBMSG_TYPE_STRING
},
56 const struct config_param_list proto_ip_attr
= {
57 .n_params
= __OPT_MAX
,
58 .params
= proto_ip_attributes
,
59 .info
= proto_ip_attr_info
,
73 static const struct blobmsg_policy proto_ip_addr
[__ADDR_MAX
] = {
74 [ADDR_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
75 [ADDR_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
76 [ADDR_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
77 [ADDR_PTP
] = { .name
= "ptp", .type
= BLOBMSG_TYPE_STRING
},
78 [ADDR_PREFERRED
] = { .name
= "preferred", .type
= BLOBMSG_TYPE_INT32
},
79 [ADDR_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
80 [ADDR_OFFLINK
] = { .name
= "offlink", .type
= BLOBMSG_TYPE_BOOL
},
83 static struct device_addr
*
84 alloc_device_addr(bool v6
, bool ext
)
86 struct device_addr
*addr
;
88 addr
= calloc(1, sizeof(*addr
));
89 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
91 addr
->flags
|= DEVADDR_EXTERNAL
;
97 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
98 bool ext
, uint32_t broadcast
)
100 struct device_addr
*addr
;
101 int af
= v6
? AF_INET6
: AF_INET
;
103 addr
= alloc_device_addr(v6
, ext
);
108 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
)) {
109 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
115 addr
->broadcast
= broadcast
;
117 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
122 parse_static_address_option(struct interface
*iface
, struct blob_attr
*attr
,
123 bool v6
, int netmask
, bool ext
, uint32_t broadcast
)
125 struct blob_attr
*cur
;
129 blobmsg_for_each_attr(cur
, attr
, rem
) {
130 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
134 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
142 static struct device_addr
*
143 parse_address_item(struct blob_attr
*attr
, bool v6
, bool ext
)
145 struct device_addr
*addr
;
146 struct blob_attr
*tb
[__ADDR_MAX
];
147 struct blob_attr
*cur
;
149 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
152 addr
= alloc_device_addr(v6
, ext
);
156 blobmsg_parse(proto_ip_addr
, __ADDR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
158 addr
->mask
= v6
? 128 : 32;
159 if ((cur
= tb
[ADDR_MASK
])) {
160 unsigned int new_mask
;
162 new_mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
163 if (new_mask
> addr
->mask
)
166 addr
->mask
= new_mask
;
169 cur
= tb
[ADDR_IPADDR
];
173 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(cur
), &addr
->addr
))
176 if ((cur
= tb
[ADDR_OFFLINK
]) && blobmsg_get_bool(cur
))
177 addr
->flags
|= DEVADDR_OFFLINK
;
180 if ((cur
= tb
[ADDR_BROADCAST
]) &&
181 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->broadcast
))
183 if ((cur
= tb
[ADDR_PTP
]) &&
184 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->point_to_point
))
187 time_t now
= system_get_rtime();
188 if ((cur
= tb
[ADDR_PREFERRED
])) {
189 uint32_t preferred
= blobmsg_get_u32(cur
);
190 if (preferred
< UINT32_MAX
)
191 addr
->preferred_until
= now
+ preferred
;
194 if ((cur
= tb
[ADDR_VALID
])) {
195 uint32_t valid
= blobmsg_get_u32(cur
);
196 if (valid
< UINT32_MAX
)
197 addr
->valid_until
= now
+ valid
;
201 if (addr
->valid_until
) {
202 if (!addr
->preferred_until
)
203 addr
->preferred_until
= addr
->valid_until
;
204 else if (addr
->preferred_until
> addr
->valid_until
)
217 parse_address_list(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
220 struct device_addr
*addr
;
221 struct blob_attr
*cur
;
225 blobmsg_for_each_attr(cur
, attr
, rem
) {
226 addr
= parse_address_item(cur
, v6
, ext
);
231 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
238 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
240 struct device_route
*route
;
241 const char *str
= blobmsg_data(attr
);
242 int af
= v6
? AF_INET6
: AF_INET
;
244 route
= calloc(1, sizeof(*route
));
245 if (!inet_pton(af
, str
, &route
->nexthop
)) {
246 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
252 route
->flags
= (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
253 vlist_add(&iface
->proto_ip
.route
, &route
->node
, &route
->flags
);
259 parse_prefix_option(struct interface
*iface
, const char *str
, size_t len
)
261 char buf
[128] = {0}, *saveptr
;
262 if (len
> sizeof(buf
))
265 memcpy(buf
, str
, len
);
266 char *addrstr
= strtok_r(buf
, "/", &saveptr
);
270 char *lengthstr
= strtok_r(NULL
, ",", &saveptr
);
274 char *prefstr
= strtok_r(NULL
, ",", &saveptr
);
275 char *validstr
= (!prefstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
276 char *excludestr
= (!validstr
) ? NULL
: strtok_r(NULL
, ",", &saveptr
);
278 uint32_t pref
= (!prefstr
) ? 0 : strtoul(prefstr
, NULL
, 10);
279 uint32_t valid
= (!validstr
) ? 0 : strtoul(validstr
, NULL
, 10);
281 uint8_t length
= strtoul(lengthstr
, NULL
, 10), excl_length
= 0;
282 if (length
< 1 || length
> 64)
285 struct in6_addr addr
, excluded
, *excludedp
= NULL
;
286 if (inet_pton(AF_INET6
, addrstr
, &addr
) < 1)
290 char *sep
= strchr(excludestr
, '/');
295 excl_length
= atoi(sep
+ 1);
297 if (inet_pton(AF_INET6
, excludestr
, &excluded
) < 1)
300 excludedp
= &excluded
;
303 time_t now
= system_get_rtime();
304 time_t preferred_until
= 0;
305 if (prefstr
&& pref
!= 0xffffffffU
)
306 preferred_until
= pref
+ now
;
308 time_t valid_until
= 0;
309 if (validstr
&& valid
!= 0xffffffffU
)
310 valid_until
= valid
+ now
;
312 interface_ip_add_device_prefix(iface
, &addr
, length
,
313 valid_until
, preferred_until
,
314 excludedp
, excl_length
);
319 parse_prefix_list(struct interface
*iface
, struct blob_attr
*attr
)
321 struct blob_attr
*cur
;
325 blobmsg_for_each_attr(cur
, attr
, rem
) {
326 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
330 if (!parse_prefix_option(iface
, blobmsg_data(cur
),
331 blobmsg_data_len(cur
)))
339 proto_apply_static_ip_settings(struct interface
*iface
, struct blob_attr
*attr
)
341 struct blob_attr
*tb
[__OPT_MAX
];
342 struct blob_attr
*cur
;
344 unsigned int netmask
= 32;
345 int n_v4
= 0, n_v6
= 0;
346 struct in_addr bcast
= {};
348 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
350 if ((cur
= tb
[OPT_NETMASK
])) {
351 netmask
= parse_netmask_string(blobmsg_data(cur
), false);
353 error
= "INVALID_NETMASK";
358 if ((cur
= tb
[OPT_BROADCAST
])) {
359 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &bcast
)) {
360 error
= "INVALID_BROADCAST";
365 if ((cur
= tb
[OPT_IPADDR
]))
366 n_v4
= parse_static_address_option(iface
, cur
, false,
367 netmask
, false, bcast
.s_addr
);
369 if ((cur
= tb
[OPT_IP6ADDR
]))
370 n_v6
= parse_static_address_option(iface
, cur
, true,
373 if ((cur
= tb
[OPT_IP6PREFIX
]))
374 if (parse_prefix_list(iface
, cur
) < 0)
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))
393 interface_add_error(iface
, "proto", error
, NULL
, 0);
399 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
401 struct blob_attr
*tb
[__OPT_MAX
];
402 struct blob_attr
*cur
;
403 int n_v4
= 0, n_v6
= 0;
405 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
407 if ((cur
= tb
[OPT_IPADDR
]))
408 n_v4
= parse_address_list(iface
, cur
, false, ext
);
410 if ((cur
= tb
[OPT_IP6ADDR
]))
411 n_v6
= parse_address_list(iface
, cur
, true, ext
);
413 if ((cur
= tb
[OPT_IP6PREFIX
]))
414 if (parse_prefix_list(iface
, cur
) < 0)
417 if (n_v4
< 0 || n_v6
< 0)
420 if ((cur
= tb
[OPT_GATEWAY
])) {
421 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
425 if ((cur
= tb
[OPT_IP6GW
])) {
426 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
436 void add_proto_handler(struct proto_handler
*p
)
439 avl_init(&handlers
, avl_strcmp
, false, NULL
);
444 p
->avl
.key
= p
->name
;
445 avl_insert(&handlers
, &p
->avl
);
449 default_proto_free(struct interface_proto_state
*proto
)
455 invalid_proto_handler(struct interface_proto_state
*proto
,
456 enum interface_proto_cmd cmd
, bool force
)
462 no_proto_handler(struct interface_proto_state
*proto
,
463 enum interface_proto_cmd cmd
, bool force
)
468 static struct interface_proto_state
*
469 default_proto_attach(const struct proto_handler
*h
,
470 struct interface
*iface
, struct blob_attr
*attr
)
472 struct interface_proto_state
*proto
;
474 proto
= calloc(1, sizeof(*proto
));
475 proto
->free
= default_proto_free
;
476 proto
->cb
= no_proto_handler
;
481 static const struct proto_handler no_proto
= {
483 .flags
= PROTO_FLAG_IMMEDIATE
,
484 .attach
= default_proto_attach
,
487 static const struct proto_handler
*
488 get_proto_handler(const char *name
)
490 struct proto_handler
*proto
;
492 if (!strcmp(name
, "none"))
498 return avl_find_element(&handlers
, name
, proto
, avl
);
502 proto_dump_handlers(struct blob_buf
*b
)
504 struct proto_handler
*p
;
507 avl_for_each_element(&handlers
, p
, avl
) {
508 c
= blobmsg_open_table(b
, p
->name
);
509 blobmsg_add_u8(b
, "no_device", !!(p
->flags
& PROTO_FLAG_NODEV
));
510 blobmsg_close_table(b
, c
);
515 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
517 const struct proto_handler
*proto
= iface
->proto_handler
;
518 struct interface_proto_state
*state
= NULL
;
523 state
= proto
->attach(proto
, iface
, attr
);
525 state
= no_proto
.attach(&no_proto
, iface
, attr
);
526 state
->cb
= invalid_proto_handler
;
529 state
->handler
= proto
;
530 interface_set_proto_state(iface
, state
);
534 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
536 const struct proto_handler
*proto
= &no_proto
;
539 proto
= get_proto_handler(proto_name
);
541 interface_add_error(iface
, "proto", "INVALID_PROTO", NULL
, 0);
546 iface
->proto_handler
= proto
;
550 interface_proto_event(struct interface_proto_state
*proto
,
551 enum interface_proto_cmd cmd
, bool force
)
553 enum interface_proto_event ev
;
556 ret
= proto
->cb(proto
, cmd
, force
);
557 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
561 case PROTO_CMD_SETUP
:
564 case PROTO_CMD_TEARDOWN
:
570 proto
->proto_event(proto
, ev
);