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