99adda1a0a66943e668aafdd87313c7fe4c8f85f
[project/netifd.git] / config.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <net/ethernet.h>
8
9 #include "netifd.h"
10 #include "interface.h"
11
12 struct uci_context *uci_ctx;
13 struct uci_package *uci_network;
14 bool config_init = false;
15
16 static void
17 config_parse_interface(struct uci_section *s)
18 {
19         struct interface *iface;
20         const char *type;
21
22         DPRINTF("Create interface '%s'\n", s->e.name);
23
24         iface = alloc_interface(s->e.name);
25         type = uci_lookup_option_string(uci_ctx, s, "type");
26
27         if (!type)
28                 type = "";
29
30         if (!strcmp(type, "bridge"))
31                 interface_attach_bridge(iface, s);
32 }
33
34 enum {
35         SDEV_NAME,
36         SDEV_TYPE,
37         SDEV_MTU,
38         SDEV_MACADDR,
39         SDEV_TXQUEUELEN,
40         __SDEV_MAX,
41 };
42
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 },
49 };
50
51 static bool
52 add_int_option(struct uci_option *o, unsigned int *dest)
53 {
54         char *error = NULL;
55         int val;
56
57         if (!o)
58                 return false;
59
60         val = strtoul(o->v.string, &error, 0);
61         if (error && *error)
62                 return false;
63
64         *dest = val;
65         return true;
66 }
67
68 static void
69 config_init_device_settings(struct device *dev, struct uci_option **opts)
70 {
71         struct ether_addr *ea;
72
73         dev->flags = 0;
74
75         if (add_int_option(opts[SDEV_MTU], &dev->mtu))
76                 dev->flags |= DEV_OPT_MTU;
77
78         if (add_int_option(opts[SDEV_TXQUEUELEN], &dev->txqueuelen))
79                 dev->flags |= DEV_OPT_TXQUEUELEN;
80
81         if (opts[SDEV_MACADDR]) {
82                 ea = ether_aton(opts[SDEV_MACADDR]->v.string);
83                 if (ea) {
84                         memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
85                         dev->flags |= DEV_OPT_MACADDR;
86                 }
87         }
88 }
89
90 void
91 config_init_devices(void)
92 {
93         struct uci_element *e;
94         struct device *dev;
95         struct uci_option *opts[__SDEV_MAX];
96
97         uci_foreach_element(&uci_network->sections, e) {
98                 struct uci_section *s = uci_to_section(e);
99
100                 if (strcmp(s->type, "device") != 0)
101                         continue;
102
103                 uci_parse_section(s, dev_opts, __SDEV_MAX, opts);
104                 if (!opts[SDEV_NAME])
105                         continue;
106
107                 dev = NULL;
108                 if (opts[SDEV_TYPE]) {
109                         const char *type = opts[SDEV_TYPE]->v.string;
110
111                         if (!strcmp(type, "bridge"))
112                                 dev = bridge_create(opts[SDEV_NAME]->v.string, s);
113                 } else {
114                         dev = get_device(opts[SDEV_NAME]->v.string, true);
115                 }
116
117                 if (!dev)
118                         continue;
119
120                 config_init_device_settings(dev, opts);
121                 dev->config_hash = uci_hash_options(opts, __SDEV_MAX);
122         }
123 }
124
125 void
126 config_init_interfaces(const char *name)
127 {
128         struct uci_context *ctx;
129         struct uci_package *p = NULL;
130         struct uci_element *e;
131
132         ctx = uci_alloc_context();
133         uci_ctx = ctx;
134
135         uci_set_confdir(ctx, "./config");
136
137         if (uci_load(ctx, "network", &p)) {
138                 fprintf(stderr, "Failed to load network config\n");
139                 return;
140         }
141
142         uci_network = p;
143         config_init = true;
144
145         config_init_devices();
146
147         uci_foreach_element(&p->sections, e) {
148                 struct uci_section *s = uci_to_section(e);
149
150                 if (name && strcmp(s->e.name, name) != 0)
151                         continue;
152
153                 if (!strcmp(s->type, "interface"))
154                         config_parse_interface(s);
155         }
156         config_init = false;
157
158         start_pending_interfaces();
159 }