add code to set ip addresses when an interface with the static proto is brought up
[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 static struct uci_package *uci_network;
14 bool config_init = false;
15
16 enum {
17 SIF_TYPE,
18 SIF_IFNAME,
19 __SIF_MAX,
20 };
21
22 static const struct uci_parse_option if_opts[__SIF_MAX] = {
23 [SIF_TYPE] = { "type", UCI_TYPE_STRING },
24 [SIF_IFNAME] = { "ifname", UCI_TYPE_STRING },
25 };
26
27 static void
28 config_parse_interface(struct uci_section *s)
29 {
30 struct uci_option *opts[__SIF_MAX];
31 struct interface *iface;
32 struct device *dev;
33 const char *type;
34
35 DPRINTF("Create interface '%s'\n", s->e.name);
36
37 iface = alloc_interface(s->e.name, s);
38 if (!iface)
39 return;
40
41 uci_parse_section(s, if_opts, __SIF_MAX, opts);
42
43 if (opts[SIF_TYPE]) {
44 type = opts[SIF_TYPE]->v.string;
45
46 if (!strcmp(type, "bridge")) {
47 interface_attach_bridge(iface, s);
48 return;
49 }
50 }
51
52 if (opts[SIF_IFNAME]) {
53 dev = get_device(opts[SIF_IFNAME]->v.string, true);
54 if (!dev)
55 return;
56
57 add_device_user(&iface->main_dev, dev);
58 }
59 }
60
61 enum {
62 SDEV_NAME,
63 SDEV_TYPE,
64 SDEV_MTU,
65 SDEV_MACADDR,
66 SDEV_TXQUEUELEN,
67 __SDEV_MAX,
68 };
69
70 static const struct uci_parse_option dev_opts[__SDEV_MAX] = {
71 [SDEV_NAME] = { "name", UCI_TYPE_STRING },
72 [SDEV_TYPE] = { "type", UCI_TYPE_STRING },
73 [SDEV_MTU] = { "mtu", UCI_TYPE_STRING },
74 [SDEV_MACADDR] = { "macaddr", UCI_TYPE_STRING },
75 [SDEV_TXQUEUELEN] = { "txqueuelen", UCI_TYPE_STRING },
76 };
77
78 static bool
79 add_int_option(struct uci_option *o, unsigned int *dest)
80 {
81 char *error = NULL;
82 int val;
83
84 if (!o)
85 return false;
86
87 val = strtoul(o->v.string, &error, 0);
88 if (error && *error)
89 return false;
90
91 *dest = val;
92 return true;
93 }
94
95 static void
96 config_init_device_settings(struct device *dev, struct uci_option **opts)
97 {
98 struct ether_addr *ea;
99
100 dev->flags = 0;
101
102 if (add_int_option(opts[SDEV_MTU], &dev->mtu))
103 dev->flags |= DEV_OPT_MTU;
104
105 if (add_int_option(opts[SDEV_TXQUEUELEN], &dev->txqueuelen))
106 dev->flags |= DEV_OPT_TXQUEUELEN;
107
108 if (opts[SDEV_MACADDR]) {
109 ea = ether_aton(opts[SDEV_MACADDR]->v.string);
110 if (ea) {
111 memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
112 dev->flags |= DEV_OPT_MACADDR;
113 }
114 }
115 }
116
117 void
118 config_init_devices(void)
119 {
120 struct uci_element *e;
121 struct device *dev;
122 struct uci_option *opts[__SDEV_MAX];
123
124 uci_foreach_element(&uci_network->sections, e) {
125 struct uci_section *s = uci_to_section(e);
126
127 if (strcmp(s->type, "device") != 0)
128 continue;
129
130 uci_parse_section(s, dev_opts, __SDEV_MAX, opts);
131 if (!opts[SDEV_NAME])
132 continue;
133
134 dev = NULL;
135 if (opts[SDEV_TYPE]) {
136 const char *type = opts[SDEV_TYPE]->v.string;
137
138 if (!strcmp(type, "bridge"))
139 dev = bridge_create(opts[SDEV_NAME]->v.string, s);
140 } else {
141 dev = get_device(opts[SDEV_NAME]->v.string, true);
142 }
143
144 if (!dev)
145 continue;
146
147 config_init_device_settings(dev, opts);
148 dev->config_hash = uci_hash_options(opts, __SDEV_MAX);
149 }
150 }
151
152 void
153 config_init_interfaces(const char *name)
154 {
155 struct uci_context *ctx;
156 struct uci_package *p = NULL;
157 struct uci_element *e;
158
159 ctx = uci_alloc_context();
160 uci_ctx = ctx;
161
162 uci_set_confdir(ctx, "./config");
163
164 if (uci_load(ctx, "network", &p)) {
165 fprintf(stderr, "Failed to load network config\n");
166 return;
167 }
168
169 uci_network = p;
170 config_init = true;
171
172 config_init_devices();
173
174 uci_foreach_element(&p->sections, e) {
175 struct uci_section *s = uci_to_section(e);
176
177 if (name && strcmp(s->e.name, name) != 0)
178 continue;
179
180 if (!strcmp(s->type, "interface"))
181 config_parse_interface(s);
182 }
183 cleanup_devices();
184 config_init = false;
185
186 start_pending_interfaces();
187 }