wireless: rework and fix vlan/station config reload handling
[project/netifd.git] / device.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <assert.h>
18
19 #include <sys/types.h>
20 #include <sys/socket.h>
21
22 #include <libubox/list.h>
23
24 #include "netifd.h"
25 #include "system.h"
26 #include "config.h"
27 #include "wireless.h"
28
29 static struct list_head devtypes = LIST_HEAD_INIT(devtypes);
30 static struct avl_tree devices;
31
32 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
33 [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
34 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
35 [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
36 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
37 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
38 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
39 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
40 [DEV_ATTR_IP6SEGMENTROUTING] = { .name = "ip6segmentrouting", .type = BLOBMSG_TYPE_BOOL },
41 [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
42 [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
43 [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
44 [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
45 [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
46 [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
47 [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 },
48 [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
49 [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
50 [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
51 [DEV_ATTR_MULTICAST_FAST_LEAVE] = { .name = "multicast_fast_leave", . type = BLOBMSG_TYPE_BOOL },
52 [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL },
53 [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL },
54 [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL },
55 [DEV_ATTR_SENDREDIRECTS] = { .name = "sendredirects", .type = BLOBMSG_TYPE_BOOL },
56 [DEV_ATTR_NEIGHLOCKTIME] = { .name = "neighlocktime", .type = BLOBMSG_TYPE_INT32 },
57 [DEV_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
58 [DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST] = { .name = "drop_v4_unicast_in_l2_multicast", .type = BLOBMSG_TYPE_BOOL },
59 [DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST] = { .name = "drop_v6_unicast_in_l2_multicast", .type = BLOBMSG_TYPE_BOOL },
60 [DEV_ATTR_DROP_GRATUITOUS_ARP] = { .name = "drop_gratuitous_arp", .type = BLOBMSG_TYPE_BOOL },
61 [DEV_ATTR_DROP_UNSOLICITED_NA] = { .name = "drop_unsolicited_na", .type = BLOBMSG_TYPE_BOOL },
62 [DEV_ATTR_ARP_ACCEPT] = { .name = "arp_accept", .type = BLOBMSG_TYPE_BOOL },
63 [DEV_ATTR_AUTH] = { .name = "auth", .type = BLOBMSG_TYPE_BOOL },
64 [DEV_ATTR_SPEED] = { .name = "speed", .type = BLOBMSG_TYPE_INT32 },
65 [DEV_ATTR_DUPLEX] = { .name = "duplex", .type = BLOBMSG_TYPE_BOOL },
66 [DEV_ATTR_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_ARRAY },
67 [DEV_ATTR_PAUSE] = { .name = "pause", .type = BLOBMSG_TYPE_BOOL },
68 [DEV_ATTR_ASYM_PAUSE] = { .name = "asym_pause", .type = BLOBMSG_TYPE_BOOL },
69 [DEV_ATTR_RXPAUSE] = { .name = "rxpause", .type = BLOBMSG_TYPE_BOOL },
70 [DEV_ATTR_TXPAUSE] = { .name = "txpause", .type = BLOBMSG_TYPE_BOOL },
71 [DEV_ATTR_AUTONEG] = { .name = "autoneg", .type = BLOBMSG_TYPE_BOOL },
72 };
73
74 const struct uci_blob_param_list device_attr_list = {
75 .n_params = __DEV_ATTR_MAX,
76 .params = dev_attrs,
77 };
78
79 static int __devlock = 0;
80
81 int device_type_add(struct device_type *devtype)
82 {
83 if (device_type_get(devtype->name)) {
84 netifd_log_message(L_WARNING, "Device handler '%s' already exists\n",
85 devtype->name);
86 return 1;
87 }
88
89 netifd_log_message(L_NOTICE, "Added device handler type: %s\n",
90 devtype->name);
91
92 list_add(&devtype->list, &devtypes);
93 return 0;
94 }
95
96 struct device_type *
97 device_type_get(const char *tname)
98 {
99 struct device_type *cur;
100
101 list_for_each_entry(cur, &devtypes, list)
102 if (!strcmp(cur->name, tname))
103 return cur;
104
105 return NULL;
106 }
107
108 static int device_vlan_len(struct kvlist *kv, const void *data)
109 {
110 return sizeof(unsigned int);
111 }
112
113 void device_vlan_update(bool done)
114 {
115 struct device *dev;
116
117 avl_for_each_element(&devices, dev, avl) {
118 if (!dev->vlans.update)
119 continue;
120
121 if (!done) {
122 if (dev->vlan_aliases.get_len)
123 kvlist_free(&dev->vlan_aliases);
124 else
125 kvlist_init(&dev->vlan_aliases, device_vlan_len);
126 vlist_update(&dev->vlans);
127 } else {
128 vlist_flush(&dev->vlans);
129
130 if (dev->type->vlan_update)
131 dev->type->vlan_update(dev);
132 }
133 }
134 }
135
136 void device_stp_init(void)
137 {
138 struct device *dev;
139
140 avl_for_each_element(&devices, dev, avl) {
141 if (!dev->type->stp_init)
142 continue;
143
144 dev->type->stp_init(dev);
145 }
146 }
147
148 static int set_device_state(struct device *dev, bool state)
149 {
150 if (state) {
151 /* Get ifindex for all devices being enabled so a valid */
152 /* ifindex is in place avoiding possible race conditions */
153 device_set_ifindex(dev, system_if_resolve(dev));
154 if (!dev->ifindex)
155 return -1;
156
157 system_if_get_settings(dev, &dev->orig_settings);
158 /* Only keep orig settings based on what needs to be set */
159 dev->orig_settings.valid_flags = dev->orig_settings.flags;
160 dev->orig_settings.flags &= dev->settings.flags;
161 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
162
163 system_if_up(dev);
164 } else {
165 system_if_down(dev);
166 system_if_apply_settings(dev, &dev->orig_settings, dev->orig_settings.flags);
167 }
168
169 return 0;
170 }
171
172 static int
173 simple_device_set_state(struct device *dev, bool state)
174 {
175 struct device *pdev;
176 int ret = 0;
177
178 pdev = dev->parent.dev;
179 if (state && !pdev) {
180 pdev = system_if_get_parent(dev);
181 if (pdev)
182 device_add_user(&dev->parent, pdev);
183 }
184
185 if (pdev) {
186 if (state)
187 ret = device_claim(&dev->parent);
188 else
189 device_release(&dev->parent);
190
191 if (ret < 0)
192 return ret;
193 }
194 return set_device_state(dev, state);
195 }
196
197 static struct device *
198 simple_device_create(const char *name, struct device_type *devtype,
199 struct blob_attr *attr)
200 {
201 struct blob_attr *tb[__DEV_ATTR_MAX];
202 struct device *dev = NULL;
203
204 /* device type is unused for simple devices */
205 devtype = NULL;
206
207 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
208 dev = device_get(name, true);
209 if (!dev)
210 return NULL;
211
212 dev->set_state = simple_device_set_state;
213 device_init_settings(dev, tb);
214
215 return dev;
216 }
217
218 static void simple_device_free(struct device *dev)
219 {
220 if (dev->parent.dev)
221 device_remove_user(&dev->parent);
222 free(dev);
223 }
224
225 struct device_type simple_device_type = {
226 .name = "Network device",
227 .config_params = &device_attr_list,
228
229 .create = simple_device_create,
230 .check_state = system_if_check,
231 .free = simple_device_free,
232 };
233
234 void
235 device_merge_settings(struct device *dev, struct device_settings *n)
236 {
237 struct device_settings *os = &dev->orig_settings;
238 struct device_settings *s = &dev->settings;
239
240 memset(n, 0, sizeof(*n));
241 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
242 n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
243 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
244 s->txqueuelen : os->txqueuelen;
245 memcpy(n->macaddr,
246 (s->flags & (DEV_OPT_MACADDR|DEV_OPT_DEFAULT_MACADDR) ? s->macaddr : os->macaddr),
247 sizeof(n->macaddr));
248 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
249 n->ip6segmentrouting = s->flags & DEV_OPT_IP6SEGMENTROUTING ? s->ip6segmentrouting : os->ip6segmentrouting;
250 n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
251 n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
252 n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
253 n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
254 n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
255 n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
256 s->neigh4reachabletime : os->neigh4reachabletime;
257 n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
258 s->neigh6reachabletime : os->neigh6reachabletime;
259 n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
260 s->neigh4gcstaletime : os->neigh4gcstaletime;
261 n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
262 s->neigh6gcstaletime : os->neigh6gcstaletime;
263 n->neigh4locktime = s->flags & DEV_OPT_NEIGHLOCKTIME ?
264 s->neigh4locktime : os->neigh4locktime;
265 n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
266 s->dadtransmits : os->dadtransmits;
267 n->multicast = s->flags & DEV_OPT_MULTICAST ?
268 s->multicast : os->multicast;
269 n->multicast_to_unicast = s->multicast_to_unicast;
270 n->multicast_router = s->multicast_router;
271 n->multicast_fast_leave = s->multicast_fast_leave;
272 n->learning = s->learning;
273 n->unicast_flood = s->unicast_flood;
274 n->sendredirects = s->flags & DEV_OPT_SENDREDIRECTS ?
275 s->sendredirects : os->sendredirects;
276 n->drop_v4_unicast_in_l2_multicast = s->flags & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST ?
277 s->drop_v4_unicast_in_l2_multicast : os->drop_v4_unicast_in_l2_multicast;
278 n->drop_v6_unicast_in_l2_multicast = s->flags & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST ?
279 s->drop_v6_unicast_in_l2_multicast : os->drop_v6_unicast_in_l2_multicast;
280 n->drop_gratuitous_arp = s->flags & DEV_OPT_DROP_GRATUITOUS_ARP ?
281 s->drop_gratuitous_arp : os->drop_gratuitous_arp;
282 n->drop_unsolicited_na = s->flags & DEV_OPT_DROP_UNSOLICITED_NA ?
283 s->drop_unsolicited_na : os->drop_unsolicited_na;
284 n->arp_accept = s->flags & DEV_OPT_ARP_ACCEPT ?
285 s->arp_accept : os->arp_accept;
286 n->auth = s->flags & DEV_OPT_AUTH ? s->auth : os->auth;
287 n->speed = s->flags & DEV_OPT_SPEED ? s->speed : os->speed;
288 n->duplex = s->flags & DEV_OPT_DUPLEX ? s->duplex : os->duplex;
289 n->pause = s->flags & DEV_OPT_PAUSE ? s->pause : os->pause;
290 n->asym_pause = s->flags & DEV_OPT_ASYM_PAUSE ? s->asym_pause : os->asym_pause;
291 n->rxpause = s->flags & DEV_OPT_RXPAUSE ? s->rxpause : os->rxpause;
292 n->txpause = s->flags & DEV_OPT_TXPAUSE ? s->txpause : os->txpause;
293 n->autoneg = s->flags & DEV_OPT_AUTONEG ? s->autoneg : os->autoneg;
294 n->flags = s->flags | os->flags | os->valid_flags;
295 }
296
297 static bool device_fill_vlan_range(struct device_vlan_range *r, const char *val)
298 {
299 unsigned long cur_start, cur_end;
300 char *sep;
301
302 cur_start = strtoul(val, &sep, 0);
303 cur_end = cur_start;
304
305 if (*sep == '-')
306 cur_end = strtoul(sep + 1, &sep, 0);
307 if (*sep || cur_end < cur_start)
308 return false;
309
310 r->start = cur_start;
311 r->end = cur_end;
312
313 return true;
314 }
315
316 static void
317 device_set_extra_vlans(struct device *dev, struct blob_attr *data)
318 {
319 struct blob_attr *cur;
320 int n_vlans;
321 size_t rem;
322
323 dev->n_extra_vlan = 0;
324 if (!data)
325 return;
326
327 n_vlans = blobmsg_check_array(data, BLOBMSG_TYPE_STRING);
328 if (n_vlans < 1)
329 return;
330
331 dev->extra_vlan = realloc(dev->extra_vlan, n_vlans * sizeof(*dev->extra_vlan));
332 blobmsg_for_each_attr(cur, data, rem)
333 if (device_fill_vlan_range(&dev->extra_vlan[dev->n_extra_vlan],
334 blobmsg_get_string(cur)))
335 dev->n_extra_vlan++;
336 }
337
338 void
339 device_init_settings(struct device *dev, struct blob_attr **tb)
340 {
341 struct device_settings *s = &dev->settings;
342 struct blob_attr *cur;
343 struct ether_addr *ea;
344 bool disabled = false;
345
346 s->flags = 0;
347 if ((cur = tb[DEV_ATTR_ENABLED]))
348 disabled = !blobmsg_get_bool(cur);
349
350 if ((cur = tb[DEV_ATTR_MTU]) && blobmsg_get_u32(cur) >= 68) {
351 s->mtu = blobmsg_get_u32(cur);
352 s->flags |= DEV_OPT_MTU;
353 }
354
355 if ((cur = tb[DEV_ATTR_MTU6]) && blobmsg_get_u32(cur) >= 1280) {
356 s->mtu6 = blobmsg_get_u32(cur);
357 s->flags |= DEV_OPT_MTU6;
358 }
359
360 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
361 s->txqueuelen = blobmsg_get_u32(cur);
362 s->flags |= DEV_OPT_TXQUEUELEN;
363 }
364
365 if ((cur = tb[DEV_ATTR_MACADDR])) {
366 ea = ether_aton(blobmsg_data(cur));
367 if (ea) {
368 memcpy(s->macaddr, ea, 6);
369 s->flags |= DEV_OPT_MACADDR;
370 }
371 }
372
373 if ((cur = tb[DEV_ATTR_IPV6])) {
374 s->ipv6 = blobmsg_get_bool(cur);
375 s->flags |= DEV_OPT_IPV6;
376 }
377
378 if ((cur = tb[DEV_ATTR_IP6SEGMENTROUTING])) {
379 s->ip6segmentrouting = blobmsg_get_bool(cur);
380 s->flags |= DEV_OPT_IP6SEGMENTROUTING;
381 }
382
383 if ((cur = tb[DEV_ATTR_PROMISC])) {
384 s->promisc = blobmsg_get_bool(cur);
385 s->flags |= DEV_OPT_PROMISC;
386 }
387
388 if ((cur = tb[DEV_ATTR_RPFILTER])) {
389 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
390 s->flags |= DEV_OPT_RPFILTER;
391 else
392 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
393 }
394
395 if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
396 s->acceptlocal = blobmsg_get_bool(cur);
397 s->flags |= DEV_OPT_ACCEPTLOCAL;
398 }
399
400 if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
401 s->igmpversion = blobmsg_get_u32(cur);
402 if (s->igmpversion >= 1 && s->igmpversion <= 3)
403 s->flags |= DEV_OPT_IGMPVERSION;
404 else
405 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
406 }
407
408 if ((cur = tb[DEV_ATTR_MLDVERSION])) {
409 s->mldversion = blobmsg_get_u32(cur);
410 if (s->mldversion >= 1 && s->mldversion <= 2)
411 s->flags |= DEV_OPT_MLDVERSION;
412 else
413 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
414 }
415
416 if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
417 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
418 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
419 }
420
421 if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) {
422 s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur);
423 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
424 }
425
426 if ((cur = tb[DEV_ATTR_NEIGHLOCKTIME])) {
427 s->neigh4locktime = blobmsg_get_u32(cur);
428 s->flags |= DEV_OPT_NEIGHLOCKTIME;
429 }
430
431 if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
432 s->dadtransmits = blobmsg_get_u32(cur);
433 s->flags |= DEV_OPT_DADTRANSMITS;
434 }
435
436 if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
437 s->multicast_to_unicast = blobmsg_get_bool(cur);
438 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
439 }
440
441 if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
442 s->multicast_router = blobmsg_get_u32(cur);
443 if (s->multicast_router <= 2)
444 s->flags |= DEV_OPT_MULTICAST_ROUTER;
445 else
446 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
447 }
448
449 if ((cur = tb[DEV_ATTR_MULTICAST_FAST_LEAVE])) {
450 s->multicast_fast_leave = blobmsg_get_bool(cur);
451 s->flags |= DEV_OPT_MULTICAST_FAST_LEAVE;
452 }
453
454 if ((cur = tb[DEV_ATTR_MULTICAST])) {
455 s->multicast = blobmsg_get_bool(cur);
456 s->flags |= DEV_OPT_MULTICAST;
457 }
458
459 if ((cur = tb[DEV_ATTR_LEARNING])) {
460 s->learning = blobmsg_get_bool(cur);
461 s->flags |= DEV_OPT_LEARNING;
462 }
463
464 if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) {
465 s->unicast_flood = blobmsg_get_bool(cur);
466 s->flags |= DEV_OPT_UNICAST_FLOOD;
467 }
468
469 if ((cur = tb[DEV_ATTR_SENDREDIRECTS])) {
470 s->sendredirects = blobmsg_get_bool(cur);
471 s->flags |= DEV_OPT_SENDREDIRECTS;
472 }
473
474 if ((cur = tb[DEV_ATTR_ISOLATE])) {
475 s->isolate = blobmsg_get_bool(cur);
476 s->flags |= DEV_OPT_ISOLATE;
477 }
478
479 if ((cur = tb[DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST])) {
480 s->drop_v4_unicast_in_l2_multicast = blobmsg_get_bool(cur);
481 s->flags |= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST;
482 }
483
484 if ((cur = tb[DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST])) {
485 s->drop_v6_unicast_in_l2_multicast = blobmsg_get_bool(cur);
486 s->flags |= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST;
487 }
488
489 if ((cur = tb[DEV_ATTR_DROP_GRATUITOUS_ARP])) {
490 s->drop_gratuitous_arp = blobmsg_get_bool(cur);
491 s->flags |= DEV_OPT_DROP_GRATUITOUS_ARP;
492 }
493
494 if ((cur = tb[DEV_ATTR_DROP_UNSOLICITED_NA])) {
495 s->drop_unsolicited_na = blobmsg_get_bool(cur);
496 s->flags |= DEV_OPT_DROP_UNSOLICITED_NA;
497 }
498
499 if ((cur = tb[DEV_ATTR_ARP_ACCEPT])) {
500 s->arp_accept = blobmsg_get_bool(cur);
501 s->flags |= DEV_OPT_ARP_ACCEPT;
502 }
503
504 if ((cur = tb[DEV_ATTR_AUTH])) {
505 s->auth = blobmsg_get_bool(cur);
506 s->flags |= DEV_OPT_AUTH;
507 }
508
509 if ((cur = tb[DEV_ATTR_SPEED])) {
510 s->speed = blobmsg_get_u32(cur);
511 s->flags |= DEV_OPT_SPEED;
512 }
513
514 if ((cur = tb[DEV_ATTR_DUPLEX])) {
515 s->duplex = blobmsg_get_bool(cur);
516 s->flags |= DEV_OPT_DUPLEX;
517 }
518
519 if ((cur = tb[DEV_ATTR_PAUSE])) {
520 s->pause = blobmsg_get_bool(cur);
521 s->flags |= DEV_OPT_PAUSE;
522 }
523
524 if ((cur = tb[DEV_ATTR_ASYM_PAUSE])) {
525 s->asym_pause = blobmsg_get_bool(cur);
526 s->flags |= DEV_OPT_ASYM_PAUSE;
527 }
528
529 if ((cur = tb[DEV_ATTR_RXPAUSE])) {
530 s->rxpause = blobmsg_get_bool(cur);
531 s->flags |= DEV_OPT_RXPAUSE;
532 }
533
534 if ((cur = tb[DEV_ATTR_TXPAUSE])) {
535 s->txpause = blobmsg_get_bool(cur);
536 s->flags |= DEV_OPT_TXPAUSE;
537 }
538
539 if ((cur = tb[DEV_ATTR_AUTONEG])) {
540 s->autoneg = blobmsg_get_bool(cur);
541 s->flags |= DEV_OPT_AUTONEG;
542 }
543 device_set_extra_vlans(dev, tb[DEV_ATTR_VLAN]);
544 device_set_disabled(dev, disabled);
545 }
546
547 static void __init dev_init(void)
548 {
549 avl_init(&devices, avl_strcmp, true, NULL);
550 }
551
552 static int device_release_cb(void *ctx, struct safe_list *list)
553 {
554 struct device_user *dep = container_of(list, struct device_user, list);
555
556 if (!dep->dev || !dep->claimed)
557 return 0;
558
559 device_release(dep);
560 return 0;
561 }
562
563 static int device_broadcast_cb(void *ctx, struct safe_list *list)
564 {
565 struct device_user *dep = container_of(list, struct device_user, list);
566 int *ev = ctx;
567
568 /* device might have been removed by an earlier callback */
569 if (!dep->dev)
570 return 0;
571
572 if (dep->cb)
573 dep->cb(dep, *ev);
574 return 0;
575 }
576
577 void device_broadcast_event(struct device *dev, enum device_event ev)
578 {
579 int dev_ev = ev;
580
581 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
582 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
583 }
584
585 static void
586 device_fill_default_settings(struct device *dev)
587 {
588 struct device_settings *s = &dev->settings;
589 struct ether_addr *ea;
590
591 if (!(s->flags & DEV_OPT_MACADDR)) {
592 ea = config_get_default_macaddr(dev->ifname);
593 if (ea) {
594 memcpy(s->macaddr, ea, 6);
595 s->flags |= DEV_OPT_DEFAULT_MACADDR;
596 }
597 }
598 }
599
600 int device_claim(struct device_user *dep)
601 {
602 struct device *dev = dep->dev;
603 int ret = 0;
604
605 if (dep->claimed)
606 return 0;
607
608 if (!dev)
609 return -1;
610
611 dep->claimed = true;
612 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
613 if (++dev->active != 1)
614 return 0;
615
616 device_broadcast_event(dev, DEV_EVENT_SETUP);
617 device_fill_default_settings(dev);
618 if (dev->external) {
619 /* Get ifindex for external claimed devices so a valid */
620 /* ifindex is in place avoiding possible race conditions */
621 device_set_ifindex(dev, system_if_resolve(dev));
622 if (!dev->ifindex)
623 ret = -1;
624
625 system_if_get_settings(dev, &dev->orig_settings);
626 } else
627 ret = dev->set_state(dev, true);
628
629 if (ret == 0)
630 device_broadcast_event(dev, DEV_EVENT_UP);
631 else {
632 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
633 dev->active = 0;
634 dep->claimed = false;
635 }
636
637 return ret;
638 }
639
640 void device_release(struct device_user *dep)
641 {
642 struct device *dev = dep->dev;
643
644 if (!dep->claimed)
645 return;
646
647 dep->claimed = false;
648 dev->active--;
649 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
650 assert(dev->active >= 0);
651
652 if (dev->active)
653 return;
654
655 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
656 if (!dev->external)
657 dev->set_state(dev, false);
658
659 if (dev->active)
660 return;
661
662 device_broadcast_event(dev, DEV_EVENT_DOWN);
663 }
664
665 int device_check_state(struct device *dev)
666 {
667 if (!dev->type->check_state)
668 return simple_device_type.check_state(dev);
669
670 return dev->type->check_state(dev);
671 }
672
673 int device_init_virtual(struct device *dev, struct device_type *type, const char *name)
674 {
675 assert(dev);
676 assert(type);
677
678 D(DEVICE, "Initialize device '%s'\n", name ? name : "");
679 INIT_SAFE_LIST(&dev->users);
680 INIT_SAFE_LIST(&dev->aliases);
681 dev->type = type;
682
683 if (name) {
684 int ret;
685
686 ret = device_set_ifname(dev, name);
687 if (ret < 0)
688 return ret;
689 }
690
691 if (!dev->set_state)
692 dev->set_state = set_device_state;
693
694 return 0;
695 }
696
697 int device_init(struct device *dev, struct device_type *type, const char *ifname)
698 {
699 int ret;
700
701 ret = device_init_virtual(dev, type, ifname);
702 if (ret < 0)
703 return ret;
704
705 dev->avl.key = dev->ifname;
706
707 ret = avl_insert(&devices, &dev->avl);
708 if (ret < 0)
709 return ret;
710
711 system_if_clear_state(dev);
712
713 return 0;
714 }
715
716 static struct device *
717 device_create_default(const char *name, bool external)
718 {
719 struct device *dev;
720
721 if (!external && system_if_force_external(name))
722 return NULL;
723
724 D(DEVICE, "Create simple device '%s'\n", name);
725 dev = calloc(1, sizeof(*dev));
726 if (!dev)
727 return NULL;
728
729 dev->external = external;
730 dev->set_state = simple_device_set_state;
731
732 if (device_init(dev, &simple_device_type, name) < 0) {
733 device_cleanup(dev);
734 free(dev);
735 return NULL;
736 }
737
738 dev->default_config = true;
739 if (external)
740 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
741
742 device_check_state(dev);
743
744 return dev;
745 }
746
747 struct device *
748 device_find(const char *name)
749 {
750 struct device *dev;
751
752 return avl_find_element(&devices, name, dev, avl);
753 }
754
755 struct device *
756 __device_get(const char *name, int create, bool check_vlan)
757 {
758 struct device *dev;
759
760 dev = avl_find_element(&devices, name, dev, avl);
761
762 if (!dev && check_vlan && strchr(name, '.'))
763 return get_vlan_device_chain(name, create);
764
765 if (name[0] == '@')
766 return device_alias_get(name + 1);
767
768 if (dev) {
769 if (create > 1 && !dev->external) {
770 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
771 dev->external = true;
772 device_set_present(dev, true);
773 }
774 return dev;
775 }
776
777 if (!create)
778 return NULL;
779
780 return device_create_default(name, create > 1);
781 }
782
783 static void
784 device_delete(struct device *dev)
785 {
786 if (!dev->avl.key)
787 return;
788
789 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
790 avl_delete(&devices, &dev->avl);
791 dev->avl.key = NULL;
792 }
793
794 static int device_cleanup_cb(void *ctx, struct safe_list *list)
795 {
796 struct device_user *dep = container_of(list, struct device_user, list);
797 if (dep->cb)
798 dep->cb(dep, DEV_EVENT_REMOVE);
799
800 device_release(dep);
801 return 0;
802 }
803
804 void device_cleanup(struct device *dev)
805 {
806 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
807 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
808 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
809 device_delete(dev);
810 }
811
812 static void __device_set_present(struct device *dev, bool state)
813 {
814 if (dev->present == state)
815 return;
816
817 dev->present = state;
818 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
819 }
820
821 void
822 device_refresh_present(struct device *dev)
823 {
824 bool state = dev->sys_present;
825
826 if (dev->disabled || dev->deferred)
827 state = false;
828
829 __device_set_present(dev, state);
830 }
831
832 void
833 device_set_auth_status(struct device *dev, bool value)
834 {
835 if (dev->auth_status == value)
836 return;
837
838 dev->auth_status = value;
839 if (!dev->present)
840 return;
841
842 if (dev->auth_status) {
843 device_broadcast_event(dev, DEV_EVENT_AUTH_UP);
844 return;
845 }
846
847 device_broadcast_event(dev, DEV_EVENT_LINK_DOWN);
848 if (!dev->link_active)
849 return;
850
851 device_broadcast_event(dev, DEV_EVENT_LINK_UP);
852 }
853
854 void device_set_present(struct device *dev, bool state)
855 {
856 if (dev->sys_present == state)
857 return;
858
859 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
860 dev->sys_present = state;
861 device_refresh_present(dev);
862 if (!state)
863 safe_list_for_each(&dev->users, device_release_cb, NULL);
864 }
865
866 void device_set_link(struct device *dev, bool state)
867 {
868 if (dev->link_active == state)
869 return;
870
871 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
872
873 dev->link_active = state;
874 if (!state)
875 dev->auth_status = false;
876 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
877 }
878
879 void device_set_ifindex(struct device *dev, int ifindex)
880 {
881 if (dev->ifindex == ifindex)
882 return;
883
884 dev->ifindex = ifindex;
885 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
886 }
887
888 int device_set_ifname(struct device *dev, const char *name)
889 {
890 int ret = 0;
891
892 if (!strcmp(dev->ifname, name))
893 return 0;
894
895 if (strlen(name) > sizeof(dev->ifname) - 1)
896 return -1;
897
898 if (dev->avl.key)
899 avl_delete(&devices, &dev->avl);
900
901 strcpy(dev->ifname, name);
902
903 if (dev->avl.key)
904 ret = avl_insert(&devices, &dev->avl);
905
906 if (ret == 0)
907 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
908
909 return ret;
910 }
911
912 static int device_refcount(struct device *dev)
913 {
914 struct list_head *list;
915 int count = 0;
916
917 list_for_each(list, &dev->users.list)
918 count++;
919
920 list_for_each(list, &dev->aliases.list)
921 count++;
922
923 return count;
924 }
925
926 static void
927 __device_add_user(struct device_user *dep, struct device *dev)
928 {
929 struct safe_list *head;
930
931 dep->dev = dev;
932
933 if (dep->alias)
934 head = &dev->aliases;
935 else
936 head = &dev->users;
937
938 safe_list_add(&dep->list, head);
939 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
940
941 if (dep->cb && dev->present) {
942 dep->cb(dep, DEV_EVENT_ADD);
943 if (dev->active)
944 dep->cb(dep, DEV_EVENT_UP);
945
946 if (dev->link_active)
947 dep->cb(dep, DEV_EVENT_LINK_UP);
948 }
949 }
950
951 void device_add_user(struct device_user *dep, struct device *dev)
952 {
953 if (dep->dev == dev)
954 return;
955
956 if (dep->dev)
957 device_remove_user(dep);
958
959 if (!dev)
960 return;
961
962 __device_add_user(dep, dev);
963 }
964
965 static void
966 device_free(struct device *dev)
967 {
968 __devlock++;
969 free(dev->config);
970 device_cleanup(dev);
971 free(dev->extra_vlan);
972 dev->type->free(dev);
973 __devlock--;
974 }
975
976 static void
977 __device_free_unused(struct uloop_timeout *timeout)
978 {
979 struct device *dev, *tmp;
980
981 avl_for_each_element_safe(&devices, dev, avl, tmp) {
982 if (!safe_list_empty(&dev->users) ||
983 !safe_list_empty(&dev->aliases) ||
984 dev->current_config)
985 continue;
986
987 device_free(dev);
988 }
989 }
990
991 void device_free_unused(void)
992 {
993 static struct uloop_timeout free_timer = {
994 .cb = __device_free_unused,
995 };
996
997 uloop_timeout_set(&free_timer, 1);
998 }
999
1000 void device_remove_user(struct device_user *dep)
1001 {
1002 struct device *dev = dep->dev;
1003
1004 if (!dep->dev)
1005 return;
1006
1007 dep->hotplug = false;
1008 if (dep->claimed)
1009 device_release(dep);
1010
1011 safe_list_del(&dep->list);
1012 dep->dev = NULL;
1013 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
1014 device_free_unused();
1015 }
1016
1017 void
1018 device_init_pending(void)
1019 {
1020 struct device *dev, *tmp;
1021
1022 avl_for_each_element_safe(&devices, dev, avl, tmp) {
1023 if (!dev->config_pending)
1024 continue;
1025
1026 dev->type->config_init(dev);
1027 dev->config_pending = false;
1028 device_check_state(dev);
1029 }
1030 }
1031
1032 bool
1033 device_check_ip6segmentrouting(void)
1034 {
1035 struct device *dev;
1036 bool ip6segmentrouting = false;
1037
1038 avl_for_each_element(&devices, dev, avl)
1039 ip6segmentrouting |= dev->settings.ip6segmentrouting;
1040
1041 return ip6segmentrouting;
1042 }
1043
1044 static enum dev_change_type
1045 device_set_config(struct device *dev, struct device_type *type,
1046 struct blob_attr *attr)
1047 {
1048 struct blob_attr *tb[__DEV_ATTR_MAX];
1049 const struct uci_blob_param_list *cfg = type->config_params;
1050
1051 if (type != dev->type)
1052 return DEV_CONFIG_RECREATE;
1053
1054 if (dev->type->reload)
1055 return dev->type->reload(dev, attr);
1056
1057 if (uci_blob_check_equal(dev->config, attr, cfg))
1058 return DEV_CONFIG_NO_CHANGE;
1059
1060 if (cfg == &device_attr_list) {
1061 memset(tb, 0, sizeof(tb));
1062
1063 if (attr)
1064 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
1065 blob_data(attr), blob_len(attr));
1066
1067 device_init_settings(dev, tb);
1068 return DEV_CONFIG_RESTART;
1069 } else
1070 return DEV_CONFIG_RECREATE;
1071 }
1072
1073 enum dev_change_type
1074 device_apply_config(struct device *dev, struct device_type *type,
1075 struct blob_attr *config)
1076 {
1077 enum dev_change_type change;
1078
1079 change = device_set_config(dev, type, config);
1080 if (dev->external) {
1081 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
1082 change = DEV_CONFIG_APPLIED;
1083 }
1084
1085 switch (change) {
1086 case DEV_CONFIG_RESTART:
1087 case DEV_CONFIG_APPLIED:
1088 D(DEVICE, "Device '%s': config applied\n", dev->ifname);
1089 config = blob_memdup(config);
1090 free(dev->config);
1091 dev->config = config;
1092 if (change == DEV_CONFIG_RESTART && dev->present) {
1093 int ret = 0;
1094
1095 device_set_present(dev, false);
1096 if (dev->active && !dev->external) {
1097 ret = dev->set_state(dev, false);
1098 if (!ret)
1099 ret = dev->set_state(dev, true);
1100 }
1101 if (!ret)
1102 device_set_present(dev, true);
1103 }
1104 break;
1105 case DEV_CONFIG_NO_CHANGE:
1106 D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
1107 break;
1108 case DEV_CONFIG_RECREATE:
1109 break;
1110 }
1111
1112 return change;
1113 }
1114
1115 static void
1116 device_replace(struct device *dev, struct device *odev)
1117 {
1118 struct device_user *dep;
1119
1120 __devlock++;
1121 if (odev->present)
1122 device_set_present(odev, false);
1123
1124 while (!list_empty(&odev->users.list)) {
1125 dep = list_first_entry(&odev->users.list, struct device_user, list.list);
1126 device_release(dep);
1127 if (!dep->dev)
1128 continue;
1129
1130 safe_list_del(&dep->list);
1131 __device_add_user(dep, dev);
1132 }
1133 __devlock--;
1134
1135 device_free(odev);
1136 }
1137
1138 void
1139 device_reset_config(void)
1140 {
1141 struct device *dev;
1142
1143 avl_for_each_element(&devices, dev, avl)
1144 dev->current_config = false;
1145 }
1146
1147 void
1148 device_reset_old(void)
1149 {
1150 struct device *dev, *tmp, *ndev;
1151
1152 avl_for_each_element_safe(&devices, dev, avl, tmp) {
1153 if (dev->current_config || dev->default_config)
1154 continue;
1155
1156 if (dev->type != &simple_device_type)
1157 continue;
1158
1159 ndev = device_create_default(dev->ifname, dev->external);
1160 if (!ndev)
1161 continue;
1162
1163 device_replace(ndev, dev);
1164 }
1165 }
1166
1167 struct device *
1168 device_create(const char *name, struct device_type *type,
1169 struct blob_attr *config)
1170 {
1171 struct device *odev = NULL, *dev;
1172 enum dev_change_type change;
1173
1174 odev = device_find(name);
1175 if (odev) {
1176 odev->current_config = true;
1177 change = device_apply_config(odev, type, config);
1178 switch (change) {
1179 case DEV_CONFIG_RECREATE:
1180 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
1181 device_delete(odev);
1182 break;
1183 default:
1184 return odev;
1185 }
1186 } else
1187 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
1188
1189 config = blob_memdup(config);
1190 if (!config)
1191 return NULL;
1192
1193 dev = type->create(name, type, config);
1194 if (!dev)
1195 return NULL;
1196
1197 dev->current_config = true;
1198 dev->config = config;
1199 if (odev)
1200 device_replace(dev, odev);
1201
1202 if (!config_init && dev->config_pending) {
1203 type->config_init(dev);
1204 dev->config_pending = false;
1205 }
1206
1207 device_check_state(dev);
1208
1209 return dev;
1210 }
1211
1212 void
1213 device_dump_status(struct blob_buf *b, struct device *dev)
1214 {
1215 struct device_settings st;
1216 void *c, *s;
1217
1218 if (!dev) {
1219 avl_for_each_element(&devices, dev, avl) {
1220 if (!dev->present)
1221 continue;
1222 c = blobmsg_open_table(b, dev->ifname);
1223 device_dump_status(b, dev);
1224 blobmsg_close_table(b, c);
1225 }
1226
1227 return;
1228 }
1229
1230 blobmsg_add_u8(b, "external", dev->external);
1231 blobmsg_add_u8(b, "present", dev->present);
1232 blobmsg_add_string(b, "type", dev->type->name);
1233
1234 if (!dev->present)
1235 return;
1236
1237 blobmsg_add_u8(b, "up", !!dev->active);
1238 blobmsg_add_u8(b, "carrier", !!dev->link_active);
1239 blobmsg_add_u8(b, "auth_status", !!dev->auth_status);
1240
1241 if (dev->type->dump_info)
1242 dev->type->dump_info(dev, b);
1243 else
1244 system_if_dump_info(dev, b);
1245
1246 if (dev->active) {
1247 device_merge_settings(dev, &st);
1248 if (st.flags & DEV_OPT_MTU)
1249 blobmsg_add_u32(b, "mtu", st.mtu);
1250 if (st.flags & DEV_OPT_MTU6)
1251 blobmsg_add_u32(b, "mtu6", st.mtu6);
1252 if (st.flags & DEV_OPT_MACADDR)
1253 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
1254 if (st.flags & DEV_OPT_TXQUEUELEN)
1255 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
1256 if (st.flags & DEV_OPT_IPV6)
1257 blobmsg_add_u8(b, "ipv6", st.ipv6);
1258 if (st.flags & DEV_OPT_IP6SEGMENTROUTING)
1259 blobmsg_add_u8(b, "ip6segmentrouting", st.ip6segmentrouting);
1260 if (st.flags & DEV_OPT_PROMISC)
1261 blobmsg_add_u8(b, "promisc", st.promisc);
1262 if (st.flags & DEV_OPT_RPFILTER)
1263 blobmsg_add_u32(b, "rpfilter", st.rpfilter);
1264 if (st.flags & DEV_OPT_ACCEPTLOCAL)
1265 blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
1266 if (st.flags & DEV_OPT_IGMPVERSION)
1267 blobmsg_add_u32(b, "igmpversion", st.igmpversion);
1268 if (st.flags & DEV_OPT_MLDVERSION)
1269 blobmsg_add_u32(b, "mldversion", st.mldversion);
1270 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
1271 blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
1272 blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
1273 }
1274 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
1275 blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
1276 blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
1277 }
1278 if (st.flags & DEV_OPT_NEIGHLOCKTIME)
1279 blobmsg_add_u32(b, "neigh4locktime", st.neigh4locktime);
1280 if (st.flags & DEV_OPT_DADTRANSMITS)
1281 blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
1282 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
1283 blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
1284 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
1285 blobmsg_add_u32(b, "multicast_router", st.multicast_router);
1286 if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
1287 blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
1288 if (st.flags & DEV_OPT_MULTICAST)
1289 blobmsg_add_u8(b, "multicast", st.multicast);
1290 if (st.flags & DEV_OPT_LEARNING)
1291 blobmsg_add_u8(b, "learning", st.learning);
1292 if (st.flags & DEV_OPT_UNICAST_FLOOD)
1293 blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
1294 if (st.flags & DEV_OPT_SENDREDIRECTS)
1295 blobmsg_add_u8(b, "sendredirects", st.sendredirects);
1296 if (st.flags & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
1297 blobmsg_add_u8(b, "drop_v4_unicast_in_l2_multicast", st.drop_v4_unicast_in_l2_multicast);
1298 if (st.flags & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
1299 blobmsg_add_u8(b, "drop_v6_unicast_in_l2_multicast", st.drop_v6_unicast_in_l2_multicast);
1300 if (st.flags & DEV_OPT_DROP_GRATUITOUS_ARP)
1301 blobmsg_add_u8(b, "drop_gratuitous_arp", st.drop_gratuitous_arp);
1302 if (st.flags & DEV_OPT_DROP_UNSOLICITED_NA)
1303 blobmsg_add_u8(b, "drop_unsolicited_na", st.drop_unsolicited_na);
1304 if (st.flags & DEV_OPT_ARP_ACCEPT)
1305 blobmsg_add_u8(b, "arp_accept", st.arp_accept);
1306 if (st.flags & DEV_OPT_AUTH)
1307 blobmsg_add_u8(b, "auth", st.auth);
1308 }
1309
1310 s = blobmsg_open_table(b, "statistics");
1311 if (dev->type->dump_stats)
1312 dev->type->dump_stats(dev, b);
1313 else
1314 system_if_dump_stats(dev, b);
1315 blobmsg_close_table(b, s);
1316 }
1317
1318 static void __init simple_device_type_init(void)
1319 {
1320 device_type_add(&simple_device_type);
1321 }
1322
1323 void device_hotplug_event(const char *name, bool add)
1324 {
1325 struct device *dev;
1326
1327 wireless_device_hotplug_event(name, add);
1328
1329 dev = device_find(name);
1330 if (!dev || dev->type != &simple_device_type)
1331 return;
1332
1333 device_set_present(dev, add);
1334 }