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.
21 #include "interface.h"
22 #include "interface-ip.h"
26 bool config_init
= false;
28 static struct uci_context
*uci_ctx
;
29 static struct uci_package
*uci_network
;
30 static struct blob_buf b
;
32 static void uci_attr_to_blob(struct blob_buf
*b
, const char *str
,
33 const char *name
, enum blobmsg_type type
)
39 case BLOBMSG_TYPE_STRING
:
40 blobmsg_add_string(b
, name
, str
);
42 case BLOBMSG_TYPE_BOOL
:
43 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
45 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
50 blobmsg_add_u8(b
, name
, intval
);
52 case BLOBMSG_TYPE_INT32
:
53 intval
= strtol(str
, &err
, 0);
57 blobmsg_add_u32(b
, name
, intval
);
64 static void uci_array_to_blob(struct blob_buf
*b
, struct uci_option
*o
,
65 enum blobmsg_type type
)
67 struct uci_element
*e
;
68 char *str
, *next
, *word
;
70 if (o
->type
== UCI_TYPE_LIST
) {
71 uci_foreach_element(&o
->v
.list
, e
) {
72 uci_attr_to_blob(b
, e
->name
, NULL
, type
);
77 str
= strdup(o
->v
.string
);
80 while ((word
= strsep(&next
, " \t")) != NULL
) {
84 uci_attr_to_blob(b
, word
, NULL
, type
);
90 static void __uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
91 const struct config_param_list
*p
)
93 const struct blobmsg_policy
*attr
= NULL
;
94 struct uci_element
*e
;
99 uci_foreach_element(&s
->options
, e
) {
100 for (i
= 0; i
< p
->n_params
; i
++) {
101 attr
= &p
->params
[i
];
102 if (!strcmp(attr
->name
, e
->name
))
106 if (i
== p
->n_params
)
109 o
= uci_to_option(e
);
111 if (attr
->type
== BLOBMSG_TYPE_ARRAY
) {
115 array
= blobmsg_open_array(b
, attr
->name
);
116 uci_array_to_blob(b
, o
, p
->info
[i
].type
);
117 blobmsg_close_array(b
, array
);
121 if (o
->type
== UCI_TYPE_LIST
)
124 uci_attr_to_blob(b
, o
->v
.string
, attr
->name
, attr
->type
);
128 static void uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
129 const struct config_param_list
*p
)
133 __uci_to_blob(b
, s
, p
);
134 for (i
= 0; i
< p
->n_next
; i
++)
135 uci_to_blob(b
, s
, p
->next
[i
]);
139 config_parse_bridge_interface(struct uci_section
*s
)
143 name
= alloca(strlen(s
->e
.name
) + 4);
144 sprintf(name
, "br-%s", s
->e
.name
);
145 blobmsg_add_string(&b
, "name", name
);
147 uci_to_blob(&b
, s
, bridge_device_type
.config_params
);
148 if (!device_create(name
, &bridge_device_type
, b
.head
)) {
149 D(INTERFACE
, "Failed to create bridge for interface '%s'\n", s
->e
.name
);
153 blob_buf_init(&b
, 0);
154 blobmsg_add_string(&b
, "ifname", name
);
159 config_parse_interface(struct uci_section
*s
)
161 struct interface
*iface
;
163 struct blob_attr
*config
;
166 blob_buf_init(&b
, 0);
168 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
169 if (type
&& !strcmp(type
, "bridge"))
170 if (config_parse_bridge_interface(s
))
173 uci_to_blob(&b
, s
, &interface_attr_list
);
174 iface
= calloc(1, sizeof(*iface
));
178 interface_init(iface
, s
->e
.name
, b
.head
);
180 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
181 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
183 config
= malloc(blob_pad_len(b
.head
));
189 memcpy(config
, b
.head
, blob_pad_len(b
.head
));
190 interface_add(iface
, config
);
193 * need to look up the interface name again, in case of config update,
194 * the pointer will have changed
196 iface
= vlist_find(&interfaces
, s
->e
.name
, iface
, node
);
200 dev
= iface
->main_dev
.dev
;
201 if (!dev
|| !dev
->default_config
)
204 blob_buf_init(&b
, 0);
205 uci_to_blob(&b
, s
, dev
->type
->config_params
);
206 if (blob_len(b
.head
) == 0)
209 device_set_config(dev
, dev
->type
, b
.head
);
213 config_parse_route(struct uci_section
*s
, bool v6
)
217 blob_buf_init(&b
, 0);
218 route
= blobmsg_open_array(&b
, "route");
219 uci_to_blob(&b
, s
, &route_attr_list
);
220 blobmsg_close_array(&b
, route
);
221 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
225 config_init_devices(void)
227 struct uci_element
*e
;
229 uci_foreach_element(&uci_network
->sections
, e
) {
230 struct uci_section
*s
= uci_to_section(e
);
231 const struct device_type
*devtype
= NULL
;
232 const char *type
, *name
;
234 if (strcmp(s
->type
, "device") != 0)
237 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
241 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
243 if (!strcmp(type
, "bridge"))
244 devtype
= &bridge_device_type
;
245 else if (!strcmp(type
, "tunnel"))
246 devtype
= &tunnel_device_type
;
250 devtype
= &simple_device_type
;
252 blob_buf_init(&b
, 0);
253 uci_to_blob(&b
, s
, devtype
->config_params
);
254 device_create(name
, devtype
, b
.head
);
259 config_diff(struct blob_attr
**tb1
, struct blob_attr
**tb2
,
260 const struct config_param_list
*config
, unsigned long *diff
)
265 for (i
= 0; i
< config
->n_params
; i
++) {
266 if (!tb1
[i
] && !tb2
[i
])
269 if (!!tb1
[i
] != !!tb2
[i
])
272 if (blob_len(tb1
[i
]) != blob_len(tb2
[i
]))
275 if (memcmp(tb1
[i
], tb2
[i
], blob_raw_len(tb1
[i
])) != 0)
293 __config_check_equal(struct blob_attr
*c1
, struct blob_attr
*c2
,
294 const struct config_param_list
*config
)
296 struct blob_attr
**tb1
, **tb2
;
304 tb1
= alloca(config
->n_params
* sizeof(struct blob_attr
*));
305 blobmsg_parse(config
->params
, config
->n_params
, tb1
,
306 blob_data(c1
), blob_len(c1
));
308 tb2
= alloca(config
->n_params
* sizeof(struct blob_attr
*));
309 blobmsg_parse(config
->params
, config
->n_params
, tb2
,
310 blob_data(c2
), blob_len(c2
));
312 return !config_diff(tb1
, tb2
, config
, NULL
);
316 config_check_equal(struct blob_attr
*c1
, struct blob_attr
*c2
,
317 const struct config_param_list
*config
)
321 if (!__config_check_equal(c1
, c2
, config
))
324 for (i
= 0; i
< config
->n_next
; i
++) {
325 if (!__config_check_equal(c1
, c2
, config
->next
[i
]))
333 config_memdup(struct blob_attr
*attr
)
335 struct blob_attr
*ret
;
336 int size
= blob_pad_len(attr
);
342 memcpy(ret
, attr
, size
);
346 static struct uci_package
*
347 config_init_package(const char *config
)
349 struct uci_context
*ctx
= uci_ctx
;
350 struct uci_package
*p
= NULL
;
353 ctx
= uci_alloc_context();
357 uci_set_confdir(ctx
, "./config");
358 uci_set_savedir(ctx
, "./tmp");
361 p
= uci_lookup_package(ctx
, config
);
366 if (uci_load(ctx
, config
, &p
))
373 config_init_interfaces(void)
375 struct uci_element
*e
;
377 uci_foreach_element(&uci_network
->sections
, e
) {
378 struct uci_section
*s
= uci_to_section(e
);
380 if (!strcmp(s
->type
, "interface"))
381 config_parse_interface(s
);
386 config_init_routes(void)
388 struct interface
*iface
;
389 struct uci_element
*e
;
391 vlist_for_each_element(&interfaces
, iface
, node
)
392 interface_ip_update_start(&iface
->config_ip
);
394 uci_foreach_element(&uci_network
->sections
, e
) {
395 struct uci_section
*s
= uci_to_section(e
);
397 if (!strcmp(s
->type
, "route"))
398 config_parse_route(s
, false);
399 else if (!strcmp(s
->type
, "route6"))
400 config_parse_route(s
, true);
403 vlist_for_each_element(&interfaces
, iface
, node
)
404 interface_ip_update_complete(&iface
->config_ip
);
408 config_init_all(void)
410 uci_network
= config_init_package("network");
412 fprintf(stderr
, "Failed to load network config\n");
416 vlist_update(&interfaces
);
420 device_reset_config();
421 config_init_devices();
422 config_init_interfaces();
423 config_init_routes();
429 device_init_pending();
430 device_free_unused(NULL
);
431 vlist_flush(&interfaces
);
432 interface_start_pending();