6 #include <sys/socket.h>
7 #include <net/ethernet.h>
10 #include "interface.h"
12 struct uci_context *uci_ctx;
13 struct uci_package *uci_network;
14 bool config_init = false;
17 config_parse_interface(struct uci_section *s)
19 struct interface *iface;
22 DPRINTF("Create interface '%s'\n", s->e.name);
24 iface = alloc_interface(s->e.name);
25 type = uci_lookup_option_string(uci_ctx, s, "type");
30 if (!strcmp(type, "bridge"))
31 interface_attach_bridge(iface, s);
43 struct uci_parse_option dev_opts[__SDEV_MAX] = {
44 [SDEV_NAME] = { "name", UCI_TYPE_STRING },
45 [SDEV_TYPE] = { "type", UCI_TYPE_STRING },
46 [SDEV_MTU] = { "mtu", UCI_TYPE_STRING },
47 [SDEV_MACADDR] = { "macaddr", UCI_TYPE_STRING },
48 [SDEV_TXQUEUELEN] = { "txqueuelen", UCI_TYPE_STRING },
52 add_int_option(struct uci_option *o, unsigned int *dest)
60 val = strtoul(o->v.string, &error, 0);
69 config_init_device_settings(struct device *dev, struct uci_option **opts)
71 struct ether_addr *ea;
75 if (add_int_option(opts[SDEV_MTU], &dev->mtu))
76 dev->flags |= DEV_OPT_MTU;
78 if (add_int_option(opts[SDEV_TXQUEUELEN], &dev->txqueuelen))
79 dev->flags |= DEV_OPT_TXQUEUELEN;
81 if (opts[SDEV_MACADDR]) {
82 ea = ether_aton(opts[SDEV_MACADDR]->v.string);
84 memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
85 dev->flags |= DEV_OPT_MACADDR;
91 config_init_devices(void)
93 struct uci_element *e;
95 struct uci_option *opts[__SDEV_MAX];
97 uci_foreach_element(&uci_network->sections, e) {
98 struct uci_section *s = uci_to_section(e);
100 if (strcmp(s->type, "device") != 0)
103 uci_parse_section(s, dev_opts, __SDEV_MAX, opts);
104 if (!opts[SDEV_NAME])
108 if (opts[SDEV_TYPE]) {
109 const char *type = opts[SDEV_TYPE]->v.string;
111 if (!strcmp(type, "bridge"))
112 dev = bridge_create(opts[SDEV_NAME]->v.string, s);
114 dev = get_device(opts[SDEV_NAME]->v.string, true);
120 config_init_device_settings(dev, opts);
121 dev->config_hash = uci_hash_options(opts, __SDEV_MAX);
126 config_init_interfaces(const char *name)
128 struct uci_context *ctx;
129 struct uci_package *p = NULL;
130 struct uci_element *e;
132 ctx = uci_alloc_context();
135 uci_set_confdir(ctx, "./config");
137 if (uci_load(ctx, "network", &p)) {
138 fprintf(stderr, "Failed to load network config\n");
145 config_init_devices();
147 uci_foreach_element(&p->sections, e) {
148 struct uci_section *s = uci_to_section(e);
150 if (name && strcmp(s->e.name, name) != 0)
153 if (!strcmp(s->type, "interface"))
154 config_parse_interface(s);
158 start_pending_interfaces();