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.
22 #include "interface.h"
23 #include "interface-ip.h"
29 bool config_init
= false;
31 static struct uci_context
*uci_ctx
;
32 static struct uci_package
*uci_network
;
33 static struct uci_package
*uci_wireless
;
34 static struct blob_buf b
;
37 config_section_idx(struct uci_section
*s
)
39 struct uci_element
*e
;
42 uci_foreach_element(&uci_wireless
->sections
, e
) {
43 struct uci_section
*cur
= uci_to_section(e
);
48 if (!strcmp(cur
->type
, s
->type
))
56 config_bridge_has_vlans(const char *br_name
)
58 struct uci_element
*e
;
60 uci_foreach_element(&uci_network
->sections
, e
) {
61 struct uci_section
*s
= uci_to_section(e
);
64 if (strcmp(s
->type
, "bridge-vlan") != 0)
67 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
71 if (!strcmp(name
, br_name
))
79 config_fixup_bridge_var(struct uci_section
*s
, const char *name
, const char *val
)
81 struct uci_ptr ptr
= {
88 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
92 uci_set(uci_ctx
, &ptr
);
96 config_fixup_bridge_vlan_filtering(struct uci_section
*s
, const char *name
)
98 bool has_vlans
= config_bridge_has_vlans(name
);
100 config_fixup_bridge_var(s
, "__has_vlans", has_vlans
? "1" : "0");
105 config_fixup_bridge_var(s
, "vlan_filtering", "1");
109 config_parse_bridge_interface(struct uci_section
*s
, struct device_type
*devtype
)
113 name
= alloca(strlen(s
->e
.name
) + strlen(devtype
->name_prefix
) + 2);
114 sprintf(name
, "%s-%s", devtype
->name_prefix
, s
->e
.name
);
115 blobmsg_add_string(&b
, "name", name
);
117 config_fixup_bridge_vlan_filtering(s
, name
);
118 uci_to_blob(&b
, s
, devtype
->config_params
);
119 if (!device_create(name
, devtype
, b
.head
)) {
120 D(INTERFACE
, "Failed to create '%s' device for interface '%s'\n",
121 devtype
->name
, s
->e
.name
);
124 blob_buf_init(&b
, 0);
125 blobmsg_add_string(&b
, "ifname", name
);
130 config_parse_interface(struct uci_section
*s
, bool alias
)
132 struct interface
*iface
;
133 const char *type
= NULL
, *disabled
;
134 struct blob_attr
*config
;
136 struct device_type
*devtype
= NULL
;
138 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
139 if (disabled
&& !strcmp(disabled
, "1"))
142 blob_buf_init(&b
, 0);
145 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
148 devtype
= device_type_get(type
);
150 if (devtype
&& devtype
->bridge_capability
) {
151 if (config_parse_bridge_interface(s
, devtype
))
157 uci_to_blob(&b
, s
, &interface_attr_list
);
159 iface
= interface_alloc(s
->e
.name
, b
.head
, false);
163 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
164 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
166 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
167 iface
->device_config
= true;
169 config
= blob_memdup(b
.head
);
174 if (!interface_add_alias(iface
, config
))
175 goto error_free_config
;
177 if (!interface_add(iface
, config
))
178 goto error_free_config
;
189 config_parse_route(struct uci_section
*s
, bool v6
)
193 blob_buf_init(&b
, 0);
194 route
= blobmsg_open_array(&b
, "route");
195 uci_to_blob(&b
, s
, &route_attr_list
);
196 blobmsg_close_array(&b
, route
);
197 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
201 config_parse_neighbor(struct uci_section
*s
, bool v6
)
205 neighbor
= blobmsg_open_array(&b
, "neighbor");
206 uci_to_blob(&b
,s
, &neighbor_attr_list
);
207 blobmsg_close_array(&b
, neighbor
);
208 interface_ip_add_neighbor(NULL
, blob_data(b
.head
), v6
);
212 config_parse_rule(struct uci_section
*s
, bool v6
)
216 blob_buf_init(&b
, 0);
217 rule
= blobmsg_open_array(&b
, "rule");
218 uci_to_blob(&b
, s
, &rule_attr_list
);
219 blobmsg_close_array(&b
, rule
);
220 iprule_add(blob_data(b
.head
), v6
);
224 config_init_devices(bool bridge
)
226 struct uci_element
*e
;
228 uci_foreach_element(&uci_network
->sections
, e
) {
229 const struct uci_blob_param_list
*params
= NULL
;
230 struct uci_section
*s
= uci_to_section(e
);
231 struct device_type
*devtype
= NULL
;
233 const char *type
, *name
;
235 if (strcmp(s
->type
, "device") != 0)
238 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
242 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
244 devtype
= device_type_get(type
);
246 if (bridge
!= (devtype
&& devtype
->bridge_capability
))
250 params
= devtype
->config_params
;
252 params
= simple_device_type
.config_params
;
254 if (devtype
&& devtype
->bridge_capability
)
255 config_fixup_bridge_vlan_filtering(s
, name
);
257 blob_buf_init(&b
, 0);
258 uci_to_blob(&b
, s
, params
);
260 dev
= device_create(name
, devtype
, b
.head
);
264 dev
= device_get(name
, 1);
268 dev
->current_config
= true;
269 device_apply_config(dev
, dev
->type
, b
.head
);
271 dev
->default_config
= false;
276 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
284 static const struct blobmsg_policy vlan_attrs
[__BRVLAN_ATTR_MAX
] = {
285 [BRVLAN_ATTR_VID
] = { "vlan", BLOBMSG_TYPE_INT32
},
286 [BRVLAN_ATTR_LOCAL
] = { "local", BLOBMSG_TYPE_BOOL
},
287 [BRVLAN_ATTR_PORTS
] = { "ports", BLOBMSG_TYPE_ARRAY
},
289 static const struct uci_blob_param_info vlan_attr_info
[__BRVLAN_ATTR_MAX
] = {
290 [BRVLAN_ATTR_PORTS
] = { .type
= BLOBMSG_TYPE_STRING
},
292 static const struct uci_blob_param_list vlan_attr_list
= {
293 .n_params
= __BRVLAN_ATTR_MAX
,
294 .params
= vlan_attrs
,
295 .info
= vlan_attr_info
,
297 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
298 struct blob_attr
*cur
;
299 struct bridge_vlan_port
*port
;
300 struct bridge_vlan
*vlan
;
308 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
312 blob_buf_init(&b
, 0);
313 uci_to_blob(&b
, s
, &vlan_attr_list
);
314 blobmsg_parse(vlan_attrs
, __BRVLAN_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
316 if (!tb
[BRVLAN_ATTR_VID
])
319 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
320 if (!vid
|| vid
> 4095)
323 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
324 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
328 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
334 if (tb
[BRVLAN_ATTR_LOCAL
])
335 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
337 vlan
->n_ports
= n_ports
;
338 vlan
->ports
= port
= (struct bridge_vlan_port
*)&vlan
[1];
339 INIT_LIST_HEAD(&vlan
->hotplug_ports
);
340 name_buf
= (char *)&port
[n_ports
];
342 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
345 port
->ifname
= name_buf
;
346 port
->flags
= BRVLAN_F_UNTAGGED
;
347 strcpy(name_buf
, blobmsg_get_string(cur
));
349 sep
= strchr(name_buf
, ':');
351 for (*sep
= 0, sep
++; *sep
; sep
++)
354 port
->flags
|= BRVLAN_F_PVID
;
357 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
362 name_buf
+= strlen(name_buf
) + 1;
366 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
371 config_init_vlans(void)
373 struct uci_element
*e
;
376 device_vlan_update(false);
377 uci_foreach_element(&uci_network
->sections
, e
) {
378 struct uci_section
*s
= uci_to_section(e
);
381 if (strcmp(s
->type
, "bridge-vlan") != 0)
384 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
388 dev
= device_get(name
, 0);
389 if (!dev
|| !dev
->vlans
.update
)
392 config_parse_vlan(dev
, s
);
394 device_vlan_update(true);
397 static struct uci_package
*
398 config_init_package(const char *config
)
400 struct uci_context
*ctx
= uci_ctx
;
401 struct uci_package
*p
= NULL
;
404 ctx
= uci_alloc_context();
407 ctx
->flags
&= ~UCI_FLAG_STRICT
;
409 uci_set_confdir(ctx
, config_path
);
412 uci_set_savedir(ctx
, "./tmp");
415 p
= uci_lookup_package(ctx
, config
);
420 if (uci_load(ctx
, config
, &p
))
427 config_init_interfaces(void)
429 struct uci_element
*e
;
431 uci_foreach_element(&uci_network
->sections
, e
) {
432 struct uci_section
*s
= uci_to_section(e
);
434 if (!strcmp(s
->type
, "interface"))
435 config_parse_interface(s
, false);
438 uci_foreach_element(&uci_network
->sections
, e
) {
439 struct uci_section
*s
= uci_to_section(e
);
441 if (!strcmp(s
->type
, "alias"))
442 config_parse_interface(s
, true);
449 struct interface
*iface
;
450 struct uci_element
*e
;
452 vlist_for_each_element(&interfaces
, iface
, node
)
453 interface_ip_update_start(&iface
->config_ip
);
455 uci_foreach_element(&uci_network
->sections
, e
) {
456 struct uci_section
*s
= uci_to_section(e
);
458 if (!strcmp(s
->type
, "route"))
459 config_parse_route(s
, false);
460 else if (!strcmp(s
->type
, "route6"))
461 config_parse_route(s
, true);
462 if (!strcmp(s
->type
, "neighbor"))
463 config_parse_neighbor(s
, false);
464 else if (!strcmp(s
->type
, "neighbor6"))
465 config_parse_neighbor(s
, true);
468 vlist_for_each_element(&interfaces
, iface
, node
)
469 interface_ip_update_complete(&iface
->config_ip
);
473 config_init_rules(void)
475 struct uci_element
*e
;
477 iprule_update_start();
479 uci_foreach_element(&uci_network
->sections
, e
) {
480 struct uci_section
*s
= uci_to_section(e
);
482 if (!strcmp(s
->type
, "rule"))
483 config_parse_rule(s
, false);
484 else if (!strcmp(s
->type
, "rule6"))
485 config_parse_rule(s
, true);
488 iprule_update_complete();
492 config_init_globals(void)
494 struct uci_section
*globals
= uci_lookup_section(
495 uci_ctx
, uci_network
, "globals");
499 const char *ula_prefix
= uci_lookup_option_string(
500 uci_ctx
, globals
, "ula_prefix");
501 interface_ip_set_ula_prefix(ula_prefix
);
505 config_parse_wireless_device(struct uci_section
*s
)
507 struct wireless_driver
*drv
;
508 const char *driver_name
;
510 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
514 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
518 blob_buf_init(&b
, 0);
519 uci_to_blob(&b
, s
, drv
->device
.config
);
520 wireless_device_create(drv
, s
->e
.name
, b
.head
);
523 static struct wireless_interface
*
524 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
528 name
= alloca(strlen(s
->type
) + 16);
529 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
531 blob_buf_init(&b
, 0);
532 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
533 return wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
537 config_parse_wireless_vlan(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
541 name
= alloca(strlen(s
->type
) + 16);
542 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
544 blob_buf_init(&b
, 0);
545 uci_to_blob(&b
, s
, wdev
->drv
->vlan
.config
);
546 wireless_vlan_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
550 config_parse_wireless_station(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
554 name
= alloca(strlen(s
->type
) + 16);
555 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
557 blob_buf_init(&b
, 0);
558 uci_to_blob(&b
, s
, wdev
->drv
->station
.config
);
559 wireless_station_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
563 config_init_wireless(void)
565 struct wireless_device
*wdev
;
566 struct uci_element
*e
;
567 const char *dev_name
;
570 DPRINTF("No wireless configuration found\n");
574 vlist_update(&wireless_devices
);
576 uci_foreach_element(&uci_wireless
->sections
, e
) {
577 struct uci_section
*s
= uci_to_section(e
);
578 if (strcmp(s
->type
, "wifi-device") != 0)
581 config_parse_wireless_device(s
);
584 vlist_flush(&wireless_devices
);
586 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
588 vlist_update(&wdev
->interfaces
);
590 vlist_update(&wdev
->vlans
);
592 vlist_update(&wdev
->stations
);
595 uci_foreach_element(&uci_wireless
->sections
, e
) {
596 struct uci_section
*s
= uci_to_section(e
);
597 struct wireless_interface
*vif
;
598 struct uci_element
*f
;
600 if (strcmp(s
->type
, "wifi-iface") != 0)
603 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
607 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
609 DPRINTF("device %s not found!\n", dev_name
);
613 vif
= config_parse_wireless_interface(wdev
, s
);
615 if (!vif
|| s
->anonymous
)
617 uci_foreach_element(&uci_wireless
->sections
, f
) {
618 struct uci_section
*s
= uci_to_section(f
);
619 const char *vif_name
;
621 if (strcmp(s
->type
, "wifi-vlan") != 0)
624 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
625 if (vif_name
&& strcmp(e
->name
, vif_name
))
627 config_parse_wireless_vlan(wdev
, vif
->name
, s
);
630 uci_foreach_element(&uci_wireless
->sections
, f
) {
631 struct uci_section
*s
= uci_to_section(f
);
632 const char *vif_name
;
634 if (strcmp(s
->type
, "wifi-station") != 0)
637 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
638 if (vif_name
&& strcmp(e
->name
, vif_name
))
640 config_parse_wireless_station(wdev
, vif
->name
, s
);
644 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
645 vlist_flush(&wdev
->interfaces
);
646 vlist_flush(&wdev
->vlans
);
647 vlist_flush(&wdev
->stations
);
652 config_init_all(void)
657 uci_network
= config_init_package("network");
659 uci_get_errorstr(uci_ctx
, &err
, NULL
);
660 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
665 uci_wireless
= config_init_package("wireless");
666 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
667 uci_get_errorstr(uci_ctx
, &err
, NULL
);
668 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
673 vlist_update(&interfaces
);
677 device_reset_config();
678 config_init_devices(true);
680 config_init_devices(false);
681 config_init_interfaces();
684 config_init_globals();
685 config_init_wireless();
691 device_init_pending();
692 vlist_flush(&interfaces
);
693 device_free_unused(NULL
);
694 interface_refresh_assignments(false);
695 interface_start_pending();
696 wireless_start_pending();