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
;
38 static const struct blobmsg_policy proto_ip_attributes
[__OPT_MAX
] = {
39 [OPT_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
40 [OPT_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
41 [OPT_NETMASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
42 [OPT_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
43 [OPT_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
44 [OPT_IP6GW
] = { .name
= "ip6gw", .type
= BLOBMSG_TYPE_STRING
},
47 static const union config_param_info proto_ip_attr_info
[__OPT_MAX
] = {
48 [OPT_IPADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
49 [OPT_IP6ADDR
] = { .type
= BLOBMSG_TYPE_STRING
},
52 const struct config_param_list proto_ip_attr
= {
53 .n_params
= __OPT_MAX
,
54 .params
= proto_ip_attributes
,
55 .info
= proto_ip_attr_info
,
66 static const struct blobmsg_policy proto_ip_addr
[__ADDR_MAX
] = {
67 [ADDR_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
68 [ADDR_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
69 [ADDR_BROADCAST
] = { .name
= "broadcast", .type
= BLOBMSG_TYPE_STRING
},
70 [ADDR_PTP
] = { .name
= "ptp", .type
= BLOBMSG_TYPE_STRING
},
74 parse_netmask_string(const char *str
, bool v6
)
80 if (!strchr(str
, '.')) {
81 ret
= strtoul(str
, &err
, 0);
91 if (inet_aton(str
, &addr
) != 1)
94 return 32 - fls(~(ntohl(addr
.s_addr
)));
101 split_netmask(char *str
, unsigned int *netmask
, bool v6
)
103 char *delim
= strchr(str
, '/');
108 *netmask
= parse_netmask_string(delim
, v6
);
114 parse_ip_and_netmask(int af
, const char *str
, void *addr
, unsigned int *netmask
)
116 char *astr
= alloca(strlen(str
) + 1);
119 if (!split_netmask(astr
, netmask
, af
== AF_INET6
))
122 if (af
== AF_INET6
) {
130 return inet_pton(af
, astr
, addr
);
133 static struct device_addr
*
134 alloc_device_addr(bool v6
, bool ext
)
136 struct device_addr
*addr
;
138 addr
= calloc(1, sizeof(*addr
));
139 addr
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
141 addr
->flags
|= DEVADDR_EXTERNAL
;
147 parse_addr(struct interface
*iface
, const char *str
, bool v6
, int mask
,
148 bool ext
, uint32_t broadcast
)
150 struct device_addr
*addr
;
151 int af
= v6
? AF_INET6
: AF_INET
;
153 addr
= alloc_device_addr(v6
, ext
);
158 if (!parse_ip_and_netmask(af
, str
, &addr
->addr
, &addr
->mask
)) {
159 interface_add_error(iface
, "proto", "INVALID_ADDRESS", &str
, 1);
165 addr
->broadcast
= broadcast
;
167 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
172 parse_static_address_option(struct interface
*iface
, struct blob_attr
*attr
,
173 bool v6
, int netmask
, bool ext
, uint32_t broadcast
)
175 struct blob_attr
*cur
;
179 blobmsg_for_each_attr(cur
, attr
, rem
) {
180 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
184 if (!parse_addr(iface
, blobmsg_data(cur
), v6
, netmask
, ext
,
192 static struct device_addr
*
193 parse_address_item(struct blob_attr
*attr
, bool v6
, bool ext
)
195 struct device_addr
*addr
;
196 struct blob_attr
*tb
[__ADDR_MAX
];
197 struct blob_attr
*cur
;
199 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
202 addr
= alloc_device_addr(v6
, ext
);
206 blobmsg_parse(proto_ip_addr
, __ADDR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
208 addr
->mask
= v6
? 128 : 32;
209 if ((cur
= tb
[ADDR_MASK
])) {
210 unsigned int new_mask
;
212 new_mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
213 if (new_mask
> addr
->mask
)
216 addr
->mask
= new_mask
;
219 cur
= tb
[ADDR_IPADDR
];
223 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(cur
), &addr
->addr
))
227 if ((cur
= tb
[ADDR_BROADCAST
]) &&
228 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->broadcast
))
230 if ((cur
= tb
[ADDR_PTP
]) &&
231 !inet_pton(AF_INET
, blobmsg_data(cur
), &addr
->point_to_point
))
243 parse_address_list(struct interface
*iface
, struct blob_attr
*attr
, bool v6
,
246 struct device_addr
*addr
;
247 struct blob_attr
*cur
;
251 blobmsg_for_each_attr(cur
, attr
, rem
) {
252 addr
= parse_address_item(cur
, v6
, ext
);
257 vlist_add(&iface
->proto_ip
.addr
, &addr
->node
, &addr
->flags
);
264 parse_gateway_option(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
266 struct device_route
*route
;
267 const char *str
= blobmsg_data(attr
);
268 int af
= v6
? AF_INET6
: AF_INET
;
270 route
= calloc(1, sizeof(*route
));
271 if (!inet_pton(af
, str
, &route
->nexthop
)) {
272 interface_add_error(iface
, "proto", "INVALID_GATEWAY", &str
, 1);
278 route
->flags
= (v6
? DEVADDR_INET6
: DEVADDR_INET4
);
279 vlist_add(&iface
->proto_ip
.route
, &route
->node
, &route
->flags
);
285 proto_apply_static_ip_settings(struct interface
*iface
, struct blob_attr
*attr
)
287 struct blob_attr
*tb
[__OPT_MAX
];
288 struct blob_attr
*cur
;
290 unsigned int netmask
= 32;
291 int n_v4
= 0, n_v6
= 0;
292 struct in_addr bcast
= {};
294 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
296 if ((cur
= tb
[OPT_NETMASK
])) {
297 netmask
= parse_netmask_string(blobmsg_data(cur
), false);
299 error
= "INVALID_NETMASK";
304 if ((cur
= tb
[OPT_BROADCAST
])) {
305 if (!inet_pton(AF_INET
, blobmsg_data(cur
), &bcast
)) {
306 error
= "INVALID_BROADCAST";
311 if ((cur
= tb
[OPT_IPADDR
]))
312 n_v4
= parse_static_address_option(iface
, cur
, false,
313 netmask
, false, bcast
.s_addr
);
315 if ((cur
= tb
[OPT_IP6ADDR
]))
316 n_v6
= parse_static_address_option(iface
, cur
, true,
319 if (!n_v4
&& !n_v6
) {
320 error
= "NO_ADDRESS";
324 if (n_v4
< 0 || n_v6
< 0)
327 if ((cur
= tb
[OPT_GATEWAY
])) {
328 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
332 if ((cur
= tb
[OPT_IP6GW
])) {
333 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
340 interface_add_error(iface
, "proto", error
, NULL
, 0);
346 proto_apply_ip_settings(struct interface
*iface
, struct blob_attr
*attr
, bool ext
)
348 struct blob_attr
*tb
[__OPT_MAX
];
349 struct blob_attr
*cur
;
351 int n_v4
= 0, n_v6
= 0;
353 blobmsg_parse(proto_ip_attributes
, __OPT_MAX
, tb
, blob_data(attr
), blob_len(attr
));
355 if ((cur
= tb
[OPT_IPADDR
]))
356 n_v4
= parse_address_list(iface
, cur
, false, ext
);
358 if ((cur
= tb
[OPT_IP6ADDR
]))
359 n_v6
= parse_address_list(iface
, cur
, true, ext
);
361 if (!n_v4
&& !n_v6
) {
362 error
= "NO_ADDRESS";
366 if (n_v4
< 0 || n_v6
< 0)
369 if ((cur
= tb
[OPT_GATEWAY
])) {
370 if (n_v4
&& !parse_gateway_option(iface
, cur
, false))
374 if ((cur
= tb
[OPT_IP6GW
])) {
375 if (n_v6
&& !parse_gateway_option(iface
, cur
, true))
382 interface_add_error(iface
, "proto", error
, NULL
, 0);
387 void add_proto_handler(struct proto_handler
*p
)
390 avl_init(&handlers
, avl_strcmp
, false, NULL
);
395 p
->avl
.key
= p
->name
;
396 avl_insert(&handlers
, &p
->avl
);
400 default_proto_free(struct interface_proto_state
*proto
)
406 invalid_proto_handler(struct interface_proto_state
*proto
,
407 enum interface_proto_cmd cmd
, bool force
)
413 no_proto_handler(struct interface_proto_state
*proto
,
414 enum interface_proto_cmd cmd
, bool force
)
419 static struct interface_proto_state
*
420 default_proto_attach(const struct proto_handler
*h
,
421 struct interface
*iface
, struct blob_attr
*attr
)
423 struct interface_proto_state
*proto
;
425 proto
= calloc(1, sizeof(*proto
));
426 proto
->free
= default_proto_free
;
427 proto
->cb
= no_proto_handler
;
432 static const struct proto_handler no_proto
= {
434 .flags
= PROTO_FLAG_IMMEDIATE
,
435 .attach
= default_proto_attach
,
438 static const struct proto_handler
*
439 get_proto_handler(const char *name
)
441 struct proto_handler
*proto
;
443 if (!strcmp(name
, "none"))
449 return avl_find_element(&handlers
, name
, proto
, avl
);
453 proto_init_interface(struct interface
*iface
, struct blob_attr
*attr
)
455 const struct proto_handler
*proto
= iface
->proto_handler
;
456 struct interface_proto_state
*state
= NULL
;
461 state
= proto
->attach(proto
, iface
, attr
);
463 state
= no_proto
.attach(&no_proto
, iface
, attr
);
464 state
->cb
= invalid_proto_handler
;
467 state
->handler
= proto
;
468 interface_set_proto_state(iface
, state
);
472 proto_attach_interface(struct interface
*iface
, const char *proto_name
)
474 const struct proto_handler
*proto
= &no_proto
;
477 proto
= get_proto_handler(proto_name
);
479 interface_add_error(iface
, "proto", "INVALID_PROTO", NULL
, 0);
484 iface
->proto_handler
= proto
;
488 interface_proto_event(struct interface_proto_state
*proto
,
489 enum interface_proto_cmd cmd
, bool force
)
491 enum interface_proto_event ev
;
494 ret
= proto
->cb(proto
, cmd
, force
);
495 if (ret
|| !(proto
->handler
->flags
& PROTO_FLAG_IMMEDIATE
))
499 case PROTO_CMD_SETUP
:
502 case PROTO_CMD_TEARDOWN
:
508 proto
->proto_event(proto
, ev
);