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