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_vlan_filtering(struct uci_section
*s
, const char *name
)
81 struct uci_ptr ptr
= {
84 .option
= "vlan_filtering",
88 if (!config_bridge_has_vlans(name
))
91 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
95 uci_set(uci_ctx
, &ptr
);
99 config_parse_bridge_interface(struct uci_section
*s
, struct device_type
*devtype
)
103 name
= alloca(strlen(s
->e
.name
) + strlen(devtype
->name_prefix
) + 2);
104 sprintf(name
, "%s-%s", devtype
->name_prefix
, s
->e
.name
);
105 blobmsg_add_string(&b
, "name", name
);
107 config_fixup_bridge_vlan_filtering(s
, name
);
108 uci_to_blob(&b
, s
, devtype
->config_params
);
109 if (!device_create(name
, devtype
, b
.head
)) {
110 D(INTERFACE
, "Failed to create '%s' device for interface '%s'\n",
111 devtype
->name
, s
->e
.name
);
114 blob_buf_init(&b
, 0);
115 blobmsg_add_string(&b
, "ifname", name
);
120 config_parse_interface(struct uci_section
*s
, bool alias
)
122 struct interface
*iface
;
123 const char *type
= NULL
, *disabled
;
124 struct blob_attr
*config
;
126 struct device_type
*devtype
= NULL
;
128 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
129 if (disabled
&& !strcmp(disabled
, "1"))
132 blob_buf_init(&b
, 0);
135 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
138 devtype
= device_type_get(type
);
140 if (devtype
&& devtype
->bridge_capability
) {
141 if (config_parse_bridge_interface(s
, devtype
))
147 uci_to_blob(&b
, s
, &interface_attr_list
);
149 iface
= interface_alloc(s
->e
.name
, b
.head
, false);
153 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
154 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
156 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
157 iface
->device_config
= true;
159 config
= blob_memdup(b
.head
);
164 if (!interface_add_alias(iface
, config
))
165 goto error_free_config
;
167 if (!interface_add(iface
, config
))
168 goto error_free_config
;
179 config_parse_route(struct uci_section
*s
, bool v6
)
183 blob_buf_init(&b
, 0);
184 route
= blobmsg_open_array(&b
, "route");
185 uci_to_blob(&b
, s
, &route_attr_list
);
186 blobmsg_close_array(&b
, route
);
187 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
191 config_parse_neighbor(struct uci_section
*s
, bool v6
)
195 neighbor
= blobmsg_open_array(&b
, "neighbor");
196 uci_to_blob(&b
,s
, &neighbor_attr_list
);
197 blobmsg_close_array(&b
, neighbor
);
198 interface_ip_add_neighbor(NULL
, blob_data(b
.head
), v6
);
202 config_parse_rule(struct uci_section
*s
, bool v6
)
206 blob_buf_init(&b
, 0);
207 rule
= blobmsg_open_array(&b
, "rule");
208 uci_to_blob(&b
, s
, &rule_attr_list
);
209 blobmsg_close_array(&b
, rule
);
210 iprule_add(blob_data(b
.head
), v6
);
214 config_init_devices(void)
216 struct uci_element
*e
;
218 uci_foreach_element(&uci_network
->sections
, e
) {
219 const struct uci_blob_param_list
*params
= NULL
;
220 struct uci_section
*s
= uci_to_section(e
);
221 struct device_type
*devtype
= NULL
;
223 const char *type
, *name
;
225 if (strcmp(s
->type
, "device") != 0)
228 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
232 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
234 devtype
= device_type_get(type
);
237 params
= devtype
->config_params
;
239 params
= simple_device_type
.config_params
;
241 if (devtype
&& devtype
->bridge_capability
)
242 config_fixup_bridge_vlan_filtering(s
, name
);
244 blob_buf_init(&b
, 0);
245 uci_to_blob(&b
, s
, params
);
247 dev
= device_create(name
, devtype
, b
.head
);
251 dev
= device_get(name
, 1);
255 dev
->current_config
= true;
256 device_apply_config(dev
, dev
->type
, b
.head
);
258 dev
->default_config
= false;
263 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
271 static const struct blobmsg_policy vlan_attrs
[__BRVLAN_ATTR_MAX
] = {
272 [BRVLAN_ATTR_VID
] = { "vlan", BLOBMSG_TYPE_INT32
},
273 [BRVLAN_ATTR_LOCAL
] = { "local", BLOBMSG_TYPE_BOOL
},
274 [BRVLAN_ATTR_PORTS
] = { "ports", BLOBMSG_TYPE_ARRAY
},
276 static const struct uci_blob_param_info vlan_attr_info
[__BRVLAN_ATTR_MAX
] = {
277 [BRVLAN_ATTR_PORTS
] = { .type
= BLOBMSG_TYPE_STRING
},
279 static const struct uci_blob_param_list vlan_attr_list
= {
280 .n_params
= __BRVLAN_ATTR_MAX
,
281 .params
= vlan_attrs
,
282 .info
= vlan_attr_info
,
284 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
285 struct blob_attr
*cur
;
286 struct bridge_vlan_port
*port
;
287 struct bridge_vlan
*vlan
;
295 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
299 blob_buf_init(&b
, 0);
300 uci_to_blob(&b
, s
, &vlan_attr_list
);
301 blobmsg_parse(vlan_attrs
, __BRVLAN_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
303 if (!tb
[BRVLAN_ATTR_VID
])
306 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
307 if (!vid
|| vid
> 4095)
310 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
311 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
315 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
321 if (tb
[BRVLAN_ATTR_LOCAL
])
322 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
324 vlan
->n_ports
= n_ports
;
325 vlan
->ports
= port
= (struct bridge_vlan_port
*)&vlan
[1];
326 name_buf
= (char *)&port
[n_ports
];
328 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
331 port
->ifname
= name_buf
;
332 port
->flags
= BRVLAN_F_UNTAGGED
;
333 strcpy(name_buf
, blobmsg_get_string(cur
));
335 sep
= strchr(name_buf
, ':');
337 for (*sep
= 0, sep
++; *sep
; sep
++)
340 port
->flags
|= BRVLAN_F_PVID
;
343 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
348 name_buf
+= strlen(name_buf
) + 1;
352 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
357 config_init_vlans(void)
359 struct uci_element
*e
;
362 device_vlan_update(false);
363 uci_foreach_element(&uci_network
->sections
, e
) {
364 struct uci_section
*s
= uci_to_section(e
);
367 if (strcmp(s
->type
, "bridge-vlan") != 0)
370 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
374 dev
= device_get(name
, 0);
375 if (!dev
|| !dev
->vlans
.update
)
378 config_parse_vlan(dev
, s
);
380 device_vlan_update(true);
383 static struct uci_package
*
384 config_init_package(const char *config
)
386 struct uci_context
*ctx
= uci_ctx
;
387 struct uci_package
*p
= NULL
;
390 ctx
= uci_alloc_context();
393 ctx
->flags
&= ~UCI_FLAG_STRICT
;
395 uci_set_confdir(ctx
, config_path
);
398 uci_set_savedir(ctx
, "./tmp");
401 p
= uci_lookup_package(ctx
, config
);
406 if (uci_load(ctx
, config
, &p
))
413 config_init_interfaces(void)
415 struct uci_element
*e
;
417 uci_foreach_element(&uci_network
->sections
, e
) {
418 struct uci_section
*s
= uci_to_section(e
);
420 if (!strcmp(s
->type
, "interface"))
421 config_parse_interface(s
, false);
424 uci_foreach_element(&uci_network
->sections
, e
) {
425 struct uci_section
*s
= uci_to_section(e
);
427 if (!strcmp(s
->type
, "alias"))
428 config_parse_interface(s
, true);
435 struct interface
*iface
;
436 struct uci_element
*e
;
438 vlist_for_each_element(&interfaces
, iface
, node
)
439 interface_ip_update_start(&iface
->config_ip
);
441 uci_foreach_element(&uci_network
->sections
, e
) {
442 struct uci_section
*s
= uci_to_section(e
);
444 if (!strcmp(s
->type
, "route"))
445 config_parse_route(s
, false);
446 else if (!strcmp(s
->type
, "route6"))
447 config_parse_route(s
, true);
448 if (!strcmp(s
->type
, "neighbor"))
449 config_parse_neighbor(s
, false);
450 else if (!strcmp(s
->type
, "neighbor6"))
451 config_parse_neighbor(s
, true);
454 vlist_for_each_element(&interfaces
, iface
, node
)
455 interface_ip_update_complete(&iface
->config_ip
);
459 config_init_rules(void)
461 struct uci_element
*e
;
463 iprule_update_start();
465 uci_foreach_element(&uci_network
->sections
, e
) {
466 struct uci_section
*s
= uci_to_section(e
);
468 if (!strcmp(s
->type
, "rule"))
469 config_parse_rule(s
, false);
470 else if (!strcmp(s
->type
, "rule6"))
471 config_parse_rule(s
, true);
474 iprule_update_complete();
478 config_init_globals(void)
480 struct uci_section
*globals
= uci_lookup_section(
481 uci_ctx
, uci_network
, "globals");
485 const char *ula_prefix
= uci_lookup_option_string(
486 uci_ctx
, globals
, "ula_prefix");
487 interface_ip_set_ula_prefix(ula_prefix
);
491 config_parse_wireless_device(struct uci_section
*s
)
493 struct wireless_driver
*drv
;
494 const char *driver_name
;
496 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
500 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
504 blob_buf_init(&b
, 0);
505 uci_to_blob(&b
, s
, drv
->device
.config
);
506 wireless_device_create(drv
, s
->e
.name
, b
.head
);
509 static struct wireless_interface
*
510 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
514 name
= alloca(strlen(s
->type
) + 16);
515 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
517 blob_buf_init(&b
, 0);
518 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
519 return wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
523 config_parse_wireless_vlan(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
527 name
= alloca(strlen(s
->type
) + 16);
528 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
530 blob_buf_init(&b
, 0);
531 uci_to_blob(&b
, s
, wdev
->drv
->vlan
.config
);
532 wireless_vlan_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
536 config_parse_wireless_station(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
540 name
= alloca(strlen(s
->type
) + 16);
541 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
543 blob_buf_init(&b
, 0);
544 uci_to_blob(&b
, s
, wdev
->drv
->station
.config
);
545 wireless_station_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
549 config_init_wireless(void)
551 struct wireless_device
*wdev
;
552 struct uci_element
*e
;
553 const char *dev_name
;
556 DPRINTF("No wireless configuration found\n");
560 vlist_update(&wireless_devices
);
562 uci_foreach_element(&uci_wireless
->sections
, e
) {
563 struct uci_section
*s
= uci_to_section(e
);
564 if (strcmp(s
->type
, "wifi-device") != 0)
567 config_parse_wireless_device(s
);
570 vlist_flush(&wireless_devices
);
572 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
574 vlist_update(&wdev
->interfaces
);
576 vlist_update(&wdev
->vlans
);
578 vlist_update(&wdev
->stations
);
581 uci_foreach_element(&uci_wireless
->sections
, e
) {
582 struct uci_section
*s
= uci_to_section(e
);
583 struct wireless_interface
*vif
;
584 struct uci_element
*f
;
586 if (strcmp(s
->type
, "wifi-iface") != 0)
589 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
593 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
595 DPRINTF("device %s not found!\n", dev_name
);
599 vif
= config_parse_wireless_interface(wdev
, s
);
601 if (!vif
|| s
->anonymous
)
603 uci_foreach_element(&uci_wireless
->sections
, f
) {
604 struct uci_section
*s
= uci_to_section(f
);
605 const char *vif_name
;
607 if (strcmp(s
->type
, "wifi-vlan") != 0)
610 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
611 if (vif_name
&& strcmp(e
->name
, vif_name
))
613 config_parse_wireless_vlan(wdev
, vif
->name
, s
);
616 uci_foreach_element(&uci_wireless
->sections
, f
) {
617 struct uci_section
*s
= uci_to_section(f
);
618 const char *vif_name
;
620 if (strcmp(s
->type
, "wifi-station") != 0)
623 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
624 if (vif_name
&& strcmp(e
->name
, vif_name
))
626 config_parse_wireless_station(wdev
, vif
->name
, s
);
630 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
631 vlist_flush(&wdev
->interfaces
);
632 vlist_flush(&wdev
->vlans
);
633 vlist_flush(&wdev
->stations
);
638 config_init_all(void)
643 uci_network
= config_init_package("network");
645 uci_get_errorstr(uci_ctx
, &err
, NULL
);
646 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
651 uci_wireless
= config_init_package("wireless");
652 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
653 uci_get_errorstr(uci_ctx
, &err
, NULL
);
654 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
659 vlist_update(&interfaces
);
663 device_reset_config();
664 config_init_devices();
665 config_init_interfaces();
669 config_init_globals();
670 config_init_wireless();
676 device_init_pending();
677 vlist_flush(&interfaces
);
678 device_free_unused(NULL
);
679 interface_refresh_assignments(false);
680 interface_start_pending();
681 wireless_start_pending();