make bridge configuration more dynamic
[project/netifd.git] / config.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "interface.h"
7
8 struct uci_context *uci_ctx;
9 static struct uci_package *uci_network;
10 bool config_init = false;
11 static struct blob_buf b;
12
13
14 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
15                              const char *name, enum blobmsg_type type)
16 {
17         char *err;
18         int intval;
19
20         switch (type) {
21         case BLOBMSG_TYPE_STRING:
22                 blobmsg_add_string(b, name, str);
23                 break;
24         case BLOBMSG_TYPE_BOOL:
25                 if (!strcmp(str, "true") || !strcmp(str, "1"))
26                         intval = 1;
27                 else if (!strcmp(str, "false") || !strcmp(str, "0"))
28                         intval = 0;
29                 else
30                         return;
31
32                 blobmsg_add_u8(b, name, intval);
33                 break;
34         case BLOBMSG_TYPE_INT32:
35                 intval = strtol(str, &err, 0);
36                 if (*err)
37                         return;
38
39                 blobmsg_add_u32(b, name, intval);
40                 break;
41         default:
42                 break;
43         }
44 }
45
46 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
47                               enum blobmsg_type type)
48 {
49         struct uci_element *e;
50         char *str, *next, *word;
51
52         if (o->type == UCI_TYPE_LIST) {
53                 uci_foreach_element(&o->v.list, e) {
54                         uci_attr_to_blob(b, e->name, NULL, type);
55                 }
56                 return;
57         }
58
59         str = strdup(o->v.string);
60         next = str;
61
62         while ((word = strsep(&next, " \t")) != NULL) {
63                 if (!*word)
64                         continue;
65
66                 uci_attr_to_blob(b, word, NULL, type);
67         }
68
69         free(str);
70 }
71
72 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
73                           const struct config_param_list *p)
74 {
75         const struct blobmsg_policy *attr;
76         struct uci_element *e;
77         struct uci_option *o;
78         void *array;
79         int i;
80
81         uci_foreach_element(&s->options, e) {
82                 for (i = 0; i < p->n_params; i++) {
83                         attr = &p->params[i];
84                         if (!strcmp(attr->name, e->name))
85                                 break;
86                 }
87
88                 if (i == p->n_params)
89                         continue;
90
91                 o = uci_to_option(e);
92
93                 if (attr->type == BLOBMSG_TYPE_ARRAY) {
94                         if (!p->info)
95                                 continue;
96
97                         array = blobmsg_open_array(b, attr->name);
98                         uci_array_to_blob(b, o, p->info[i].type);
99                         blobmsg_close_array(b, array);
100                         continue;
101                 }
102
103                 if (o->type == UCI_TYPE_LIST)
104                         continue;
105
106                 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
107         }
108 }
109
110 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
111                         const struct config_param_list *p)
112 {
113         int i;
114
115         __uci_to_blob(b, s, p);
116         for (i = 0; i < p->n_next; i++)
117                 uci_to_blob(b, s, p->next[i]);
118 }
119
120 static int
121 config_parse_bridge_interface(struct uci_section *s)
122 {
123         char *name;
124
125         name = alloca(strlen(s->e.name) + 4);
126         sprintf(name, "br-%s", s->e.name);
127         blobmsg_add_string(&b, "name", name);
128
129         uci_to_blob(&b, s, bridge_device_type.config_params);
130         if (!bridge_device_type.create(b.head)) {
131                 DPRINTF("Failed to create bridge for interface '%s'\n", s->e.name);
132                 return -EINVAL;
133         }
134
135         blob_buf_init(&b, 0);
136         blobmsg_add_string(&b, "ifname", name);
137         return 0;
138 }
139
140 static void
141 config_parse_interface(struct uci_section *s)
142 {
143         const char *type;
144         DPRINTF("Create interface '%s'\n", s->e.name);
145
146         blob_buf_init(&b, 0);
147
148         type = uci_lookup_option_string(uci_ctx, s, "type");
149         if (type && !strcmp(type, "bridge"))
150                 if (config_parse_bridge_interface(s))
151                         return;
152
153         uci_to_blob(&b, s, &interface_attr_list);
154         interface_alloc(s->e.name, s, b.head);
155 }
156
157 void
158 config_init_devices(void)
159 {
160         struct uci_element *e;
161
162         uci_foreach_element(&uci_network->sections, e) {
163                 struct uci_section *s = uci_to_section(e);
164                 const struct device_type *devtype;
165                 const char *type;
166
167                 if (strcmp(s->type, "device") != 0)
168                         continue;
169
170                 blob_buf_init(&b, 0);
171                 type = uci_lookup_option_string(uci_ctx, s, "type");
172                 if (type && !strcmp(type, "bridge"))
173                         devtype = &bridge_device_type;
174                 else
175                         devtype = &simple_device_type;
176
177                 uci_to_blob(&b, s, devtype->config_params);
178                 devtype->create(b.head);
179         }
180 }
181
182 void
183 config_init_interfaces(const char *name)
184 {
185         struct uci_context *ctx;
186         struct uci_package *p = NULL;
187         struct uci_element *e;
188
189         ctx = uci_alloc_context();
190         uci_ctx = ctx;
191
192         uci_set_confdir(ctx, "./config");
193
194         if (uci_load(ctx, "network", &p)) {
195                 fprintf(stderr, "Failed to load network config\n");
196                 return;
197         }
198
199         uci_network = p;
200         config_init = true;
201
202         config_init_devices();
203
204         uci_foreach_element(&p->sections, e) {
205                 struct uci_section *s = uci_to_section(e);
206
207                 if (name && strcmp(s->e.name, name) != 0)
208                         continue;
209
210                 if (!strcmp(s->type, "interface"))
211                         config_parse_interface(s);
212         }
213         device_free_all();
214         config_init = false;
215
216         interface_start_pending();
217 }