2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <libubox/list.h>
29 static struct list_head devtypes
= LIST_HEAD_INIT(devtypes
);
30 static struct avl_tree devices
;
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
},
74 const struct uci_blob_param_list device_attr_list
= {
75 .n_params
= __DEV_ATTR_MAX
,
79 static int __devlock
= 0;
81 int device_type_add(struct device_type
*devtype
)
83 if (device_type_get(devtype
->name
)) {
84 netifd_log_message(L_WARNING
, "Device handler '%s' already exists\n",
89 netifd_log_message(L_NOTICE
, "Added device handler type: %s\n",
92 list_add(&devtype
->list
, &devtypes
);
97 device_type_get(const char *tname
)
99 struct device_type
*cur
;
101 list_for_each_entry(cur
, &devtypes
, list
)
102 if (!strcmp(cur
->name
, tname
))
108 static int device_vlan_len(struct kvlist
*kv
, const void *data
)
110 return sizeof(unsigned int);
113 void device_vlan_update(bool done
)
117 avl_for_each_element(&devices
, dev
, avl
) {
118 if (!dev
->vlans
.update
)
122 if (dev
->vlan_aliases
.get_len
)
123 kvlist_free(&dev
->vlan_aliases
);
125 kvlist_init(&dev
->vlan_aliases
, device_vlan_len
);
126 vlist_update(&dev
->vlans
);
128 vlist_flush(&dev
->vlans
);
130 if (dev
->type
->vlan_update
)
131 dev
->type
->vlan_update(dev
);
136 void device_stp_init(void)
140 avl_for_each_element(&devices
, dev
, avl
) {
141 if (!dev
->type
->stp_init
)
144 dev
->type
->stp_init(dev
);
148 static int set_device_state(struct device
*dev
, bool 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
));
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
);
166 system_if_apply_settings(dev
, &dev
->orig_settings
, dev
->orig_settings
.flags
);
173 simple_device_set_state(struct device
*dev
, bool state
)
178 pdev
= dev
->parent
.dev
;
179 if (state
&& !pdev
) {
180 pdev
= system_if_get_parent(dev
);
182 device_add_user(&dev
->parent
, pdev
);
187 ret
= device_claim(&dev
->parent
);
189 device_release(&dev
->parent
);
194 return set_device_state(dev
, state
);
197 static struct device
*
198 simple_device_create(const char *name
, struct device_type
*devtype
,
199 struct blob_attr
*attr
)
201 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
202 struct device
*dev
= NULL
;
204 /* device type is unused for simple devices */
207 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
208 dev
= device_get(name
, true);
212 dev
->set_state
= simple_device_set_state
;
213 device_init_settings(dev
, tb
);
218 static void simple_device_free(struct device
*dev
)
221 device_remove_user(&dev
->parent
);
225 struct device_type simple_device_type
= {
226 .name
= "Network device",
227 .config_params
= &device_attr_list
,
229 .create
= simple_device_create
,
230 .check_state
= system_if_check
,
231 .free
= simple_device_free
,
235 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
237 struct device_settings
*os
= &dev
->orig_settings
;
238 struct device_settings
*s
= &dev
->settings
;
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
;
246 (s
->flags
& (DEV_OPT_MACADDR
|DEV_OPT_DEFAULT_MACADDR
) ? s
->macaddr
: os
->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
;
297 static bool device_fill_vlan_range(struct device_vlan_range
*r
, const char *val
)
299 unsigned long cur_start
, cur_end
;
302 cur_start
= strtoul(val
, &sep
, 0);
306 cur_end
= strtoul(sep
+ 1, &sep
, 0);
307 if (*sep
|| cur_end
< cur_start
)
310 r
->start
= cur_start
;
317 device_set_extra_vlans(struct device
*dev
, struct blob_attr
*data
)
319 struct blob_attr
*cur
;
323 dev
->n_extra_vlan
= 0;
327 n_vlans
= blobmsg_check_array(data
, BLOBMSG_TYPE_STRING
);
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
)))
339 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
341 struct device_settings
*s
= &dev
->settings
;
342 struct blob_attr
*cur
;
343 struct ether_addr
*ea
;
344 bool disabled
= false;
347 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
348 disabled
= !blobmsg_get_bool(cur
);
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
;
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
;
360 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
361 s
->txqueuelen
= blobmsg_get_u32(cur
);
362 s
->flags
|= DEV_OPT_TXQUEUELEN
;
365 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
366 ea
= ether_aton(blobmsg_data(cur
));
368 memcpy(s
->macaddr
, ea
, 6);
369 s
->flags
|= DEV_OPT_MACADDR
;
373 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
374 s
->ipv6
= blobmsg_get_bool(cur
);
375 s
->flags
|= DEV_OPT_IPV6
;
378 if ((cur
= tb
[DEV_ATTR_IP6SEGMENTROUTING
])) {
379 s
->ip6segmentrouting
= blobmsg_get_bool(cur
);
380 s
->flags
|= DEV_OPT_IP6SEGMENTROUTING
;
383 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
384 s
->promisc
= blobmsg_get_bool(cur
);
385 s
->flags
|= DEV_OPT_PROMISC
;
388 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
389 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
390 s
->flags
|= DEV_OPT_RPFILTER
;
392 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
395 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
396 s
->acceptlocal
= blobmsg_get_bool(cur
);
397 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
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
;
405 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
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
;
413 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
416 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
417 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
418 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
421 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
422 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
423 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
426 if ((cur
= tb
[DEV_ATTR_NEIGHLOCKTIME
])) {
427 s
->neigh4locktime
= blobmsg_get_u32(cur
);
428 s
->flags
|= DEV_OPT_NEIGHLOCKTIME
;
431 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
432 s
->dadtransmits
= blobmsg_get_u32(cur
);
433 s
->flags
|= DEV_OPT_DADTRANSMITS
;
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
;
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
;
446 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
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
;
454 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
455 s
->multicast
= blobmsg_get_bool(cur
);
456 s
->flags
|= DEV_OPT_MULTICAST
;
459 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
460 s
->learning
= blobmsg_get_bool(cur
);
461 s
->flags
|= DEV_OPT_LEARNING
;
464 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
465 s
->unicast_flood
= blobmsg_get_bool(cur
);
466 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
469 if ((cur
= tb
[DEV_ATTR_SENDREDIRECTS
])) {
470 s
->sendredirects
= blobmsg_get_bool(cur
);
471 s
->flags
|= DEV_OPT_SENDREDIRECTS
;
474 if ((cur
= tb
[DEV_ATTR_ISOLATE
])) {
475 s
->isolate
= blobmsg_get_bool(cur
);
476 s
->flags
|= DEV_OPT_ISOLATE
;
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
;
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
;
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
;
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
;
499 if ((cur
= tb
[DEV_ATTR_ARP_ACCEPT
])) {
500 s
->arp_accept
= blobmsg_get_bool(cur
);
501 s
->flags
|= DEV_OPT_ARP_ACCEPT
;
504 if ((cur
= tb
[DEV_ATTR_AUTH
])) {
505 s
->auth
= blobmsg_get_bool(cur
);
506 s
->flags
|= DEV_OPT_AUTH
;
509 if ((cur
= tb
[DEV_ATTR_SPEED
])) {
510 s
->speed
= blobmsg_get_u32(cur
);
511 s
->flags
|= DEV_OPT_SPEED
;
514 if ((cur
= tb
[DEV_ATTR_DUPLEX
])) {
515 s
->duplex
= blobmsg_get_bool(cur
);
516 s
->flags
|= DEV_OPT_DUPLEX
;
519 if ((cur
= tb
[DEV_ATTR_PAUSE
])) {
520 s
->pause
= blobmsg_get_bool(cur
);
521 s
->flags
|= DEV_OPT_PAUSE
;
524 if ((cur
= tb
[DEV_ATTR_ASYM_PAUSE
])) {
525 s
->asym_pause
= blobmsg_get_bool(cur
);
526 s
->flags
|= DEV_OPT_ASYM_PAUSE
;
529 if ((cur
= tb
[DEV_ATTR_RXPAUSE
])) {
530 s
->rxpause
= blobmsg_get_bool(cur
);
531 s
->flags
|= DEV_OPT_RXPAUSE
;
534 if ((cur
= tb
[DEV_ATTR_TXPAUSE
])) {
535 s
->txpause
= blobmsg_get_bool(cur
);
536 s
->flags
|= DEV_OPT_TXPAUSE
;
539 if ((cur
= tb
[DEV_ATTR_AUTONEG
])) {
540 s
->autoneg
= blobmsg_get_bool(cur
);
541 s
->flags
|= DEV_OPT_AUTONEG
;
543 device_set_extra_vlans(dev
, tb
[DEV_ATTR_VLAN
]);
544 device_set_disabled(dev
, disabled
);
547 static void __init
dev_init(void)
549 avl_init(&devices
, avl_strcmp
, true, NULL
);
552 static int device_release_cb(void *ctx
, struct safe_list
*list
)
554 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
556 if (!dep
->dev
|| !dep
->claimed
)
563 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
565 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
568 /* device might have been removed by an earlier callback */
577 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
581 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
582 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
586 device_fill_default_settings(struct device
*dev
)
588 struct device_settings
*s
= &dev
->settings
;
589 struct ether_addr
*ea
;
591 if (!(s
->flags
& DEV_OPT_MACADDR
)) {
592 ea
= config_get_default_macaddr(dev
->ifname
);
594 memcpy(s
->macaddr
, ea
, 6);
595 s
->flags
|= DEV_OPT_DEFAULT_MACADDR
;
600 int device_claim(struct device_user
*dep
)
602 struct device
*dev
= dep
->dev
;
612 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
613 if (++dev
->active
!= 1)
616 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
617 device_fill_default_settings(dev
);
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
));
625 system_if_get_settings(dev
, &dev
->orig_settings
);
627 ret
= dev
->set_state(dev
, true);
630 device_broadcast_event(dev
, DEV_EVENT_UP
);
632 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
634 dep
->claimed
= false;
640 void device_release(struct device_user
*dep
)
642 struct device
*dev
= dep
->dev
;
647 dep
->claimed
= false;
649 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
650 assert(dev
->active
>= 0);
655 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
657 dev
->set_state(dev
, false);
662 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
665 int device_check_state(struct device
*dev
)
667 if (!dev
->type
->check_state
)
668 return simple_device_type
.check_state(dev
);
670 return dev
->type
->check_state(dev
);
673 int device_init_virtual(struct device
*dev
, struct device_type
*type
, const char *name
)
678 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
679 INIT_SAFE_LIST(&dev
->users
);
680 INIT_SAFE_LIST(&dev
->aliases
);
686 ret
= device_set_ifname(dev
, name
);
692 dev
->set_state
= set_device_state
;
697 int device_init(struct device
*dev
, struct device_type
*type
, const char *ifname
)
701 ret
= device_init_virtual(dev
, type
, ifname
);
705 dev
->avl
.key
= dev
->ifname
;
707 ret
= avl_insert(&devices
, &dev
->avl
);
711 system_if_clear_state(dev
);
716 static struct device
*
717 device_create_default(const char *name
, bool external
)
721 if (!external
&& system_if_force_external(name
))
724 D(DEVICE
, "Create simple device '%s'\n", name
);
725 dev
= calloc(1, sizeof(*dev
));
729 dev
->external
= external
;
730 dev
->set_state
= simple_device_set_state
;
732 if (device_init(dev
, &simple_device_type
, name
) < 0) {
738 dev
->default_config
= true;
740 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
742 device_check_state(dev
);
748 device_find(const char *name
)
752 return avl_find_element(&devices
, name
, dev
, avl
);
756 __device_get(const char *name
, int create
, bool check_vlan
)
760 dev
= avl_find_element(&devices
, name
, dev
, avl
);
762 if (!dev
&& check_vlan
&& strchr(name
, '.'))
763 return get_vlan_device_chain(name
, create
);
766 return device_alias_get(name
+ 1);
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);
780 return device_create_default(name
, create
> 1);
784 device_delete(struct device
*dev
)
789 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
790 avl_delete(&devices
, &dev
->avl
);
794 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
796 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
798 dep
->cb(dep
, DEV_EVENT_REMOVE
);
804 void device_cleanup(struct device
*dev
)
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
);
812 static void __device_set_present(struct device
*dev
, bool state
)
814 if (dev
->present
== state
)
817 dev
->present
= state
;
818 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
822 device_refresh_present(struct device
*dev
)
824 bool state
= dev
->sys_present
;
826 if (dev
->disabled
|| dev
->deferred
)
829 __device_set_present(dev
, state
);
833 device_set_auth_status(struct device
*dev
, bool value
)
835 if (dev
->auth_status
== value
)
838 dev
->auth_status
= value
;
842 if (dev
->auth_status
) {
843 device_broadcast_event(dev
, DEV_EVENT_AUTH_UP
);
847 device_broadcast_event(dev
, DEV_EVENT_LINK_DOWN
);
848 if (!dev
->link_active
)
851 device_broadcast_event(dev
, DEV_EVENT_LINK_UP
);
854 void device_set_present(struct device
*dev
, bool state
)
856 if (dev
->sys_present
== state
)
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
);
863 safe_list_for_each(&dev
->users
, device_release_cb
, NULL
);
866 void device_set_link(struct device
*dev
, bool state
)
868 if (dev
->link_active
== state
)
871 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
873 dev
->link_active
= state
;
875 dev
->auth_status
= false;
876 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
879 void device_set_ifindex(struct device
*dev
, int ifindex
)
881 if (dev
->ifindex
== ifindex
)
884 dev
->ifindex
= ifindex
;
885 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
888 int device_set_ifname(struct device
*dev
, const char *name
)
892 if (!strcmp(dev
->ifname
, name
))
895 if (strlen(name
) > sizeof(dev
->ifname
) - 1)
899 avl_delete(&devices
, &dev
->avl
);
901 strcpy(dev
->ifname
, name
);
904 ret
= avl_insert(&devices
, &dev
->avl
);
907 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
912 static int device_refcount(struct device
*dev
)
914 struct list_head
*list
;
917 list_for_each(list
, &dev
->users
.list
)
920 list_for_each(list
, &dev
->aliases
.list
)
927 __device_add_user(struct device_user
*dep
, struct device
*dev
)
929 struct safe_list
*head
;
934 head
= &dev
->aliases
;
938 safe_list_add(&dep
->list
, head
);
939 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
941 if (dep
->cb
&& dev
->present
) {
942 dep
->cb(dep
, DEV_EVENT_ADD
);
944 dep
->cb(dep
, DEV_EVENT_UP
);
946 if (dev
->link_active
)
947 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
951 void device_add_user(struct device_user
*dep
, struct device
*dev
)
957 device_remove_user(dep
);
962 __device_add_user(dep
, dev
);
966 device_free(struct device
*dev
)
971 free(dev
->extra_vlan
);
972 dev
->type
->free(dev
);
977 __device_free_unused(struct uloop_timeout
*timeout
)
979 struct device
*dev
, *tmp
;
981 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
982 if (!safe_list_empty(&dev
->users
) ||
983 !safe_list_empty(&dev
->aliases
) ||
991 void device_free_unused(void)
993 static struct uloop_timeout free_timer
= {
994 .cb
= __device_free_unused
,
997 uloop_timeout_set(&free_timer
, 1);
1000 void device_remove_user(struct device_user
*dep
)
1002 struct device
*dev
= dep
->dev
;
1007 dep
->hotplug
= false;
1009 device_release(dep
);
1011 safe_list_del(&dep
->list
);
1013 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
1014 device_free_unused();
1018 device_init_pending(void)
1020 struct device
*dev
, *tmp
;
1022 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
1023 if (!dev
->config_pending
)
1026 dev
->type
->config_init(dev
);
1027 dev
->config_pending
= false;
1028 device_check_state(dev
);
1033 device_check_ip6segmentrouting(void)
1036 bool ip6segmentrouting
= false;
1038 avl_for_each_element(&devices
, dev
, avl
)
1039 ip6segmentrouting
|= dev
->settings
.ip6segmentrouting
;
1041 return ip6segmentrouting
;
1044 static enum dev_change_type
1045 device_set_config(struct device
*dev
, struct device_type
*type
,
1046 struct blob_attr
*attr
)
1048 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
1049 const struct uci_blob_param_list
*cfg
= type
->config_params
;
1051 if (type
!= dev
->type
)
1052 return DEV_CONFIG_RECREATE
;
1054 if (dev
->type
->reload
)
1055 return dev
->type
->reload(dev
, attr
);
1057 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
1058 return DEV_CONFIG_NO_CHANGE
;
1060 if (cfg
== &device_attr_list
) {
1061 memset(tb
, 0, sizeof(tb
));
1064 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
1065 blob_data(attr
), blob_len(attr
));
1067 device_init_settings(dev
, tb
);
1068 return DEV_CONFIG_RESTART
;
1070 return DEV_CONFIG_RECREATE
;
1073 enum dev_change_type
1074 device_apply_config(struct device
*dev
, struct device_type
*type
,
1075 struct blob_attr
*config
)
1077 enum dev_change_type change
;
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
;
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
);
1091 dev
->config
= config
;
1092 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
1095 device_set_present(dev
, false);
1096 if (dev
->active
&& !dev
->external
) {
1097 ret
= dev
->set_state(dev
, false);
1099 ret
= dev
->set_state(dev
, true);
1102 device_set_present(dev
, true);
1105 case DEV_CONFIG_NO_CHANGE
:
1106 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
1108 case DEV_CONFIG_RECREATE
:
1116 device_replace(struct device
*dev
, struct device
*odev
)
1118 struct device_user
*dep
;
1122 device_set_present(odev
, false);
1124 while (!list_empty(&odev
->users
.list
)) {
1125 dep
= list_first_entry(&odev
->users
.list
, struct device_user
, list
.list
);
1126 device_release(dep
);
1130 safe_list_del(&dep
->list
);
1131 __device_add_user(dep
, dev
);
1139 device_reset_config(void)
1143 avl_for_each_element(&devices
, dev
, avl
)
1144 dev
->current_config
= false;
1148 device_reset_old(void)
1150 struct device
*dev
, *tmp
, *ndev
;
1152 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
1153 if (dev
->current_config
|| dev
->default_config
)
1156 if (dev
->type
!= &simple_device_type
)
1159 ndev
= device_create_default(dev
->ifname
, dev
->external
);
1163 device_replace(ndev
, dev
);
1168 device_create(const char *name
, struct device_type
*type
,
1169 struct blob_attr
*config
)
1171 struct device
*odev
= NULL
, *dev
;
1172 enum dev_change_type change
;
1174 odev
= device_find(name
);
1176 odev
->current_config
= true;
1177 change
= device_apply_config(odev
, type
, config
);
1179 case DEV_CONFIG_RECREATE
:
1180 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
1181 device_delete(odev
);
1187 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
1189 config
= blob_memdup(config
);
1193 dev
= type
->create(name
, type
, config
);
1197 dev
->current_config
= true;
1198 dev
->config
= config
;
1200 device_replace(dev
, odev
);
1202 if (!config_init
&& dev
->config_pending
) {
1203 type
->config_init(dev
);
1204 dev
->config_pending
= false;
1207 device_check_state(dev
);
1213 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
1215 struct device_settings st
;
1219 avl_for_each_element(&devices
, dev
, avl
) {
1222 c
= blobmsg_open_table(b
, dev
->ifname
);
1223 device_dump_status(b
, dev
);
1224 blobmsg_close_table(b
, c
);
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
);
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
);
1241 if (dev
->type
->dump_info
)
1242 dev
->type
->dump_info(dev
, b
);
1244 system_if_dump_info(dev
, b
);
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
);
1274 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
1275 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
1276 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
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
);
1310 s
= blobmsg_open_table(b
, "statistics");
1311 if (dev
->type
->dump_stats
)
1312 dev
->type
->dump_stats(dev
, b
);
1314 system_if_dump_stats(dev
, b
);
1315 blobmsg_close_table(b
, s
);
1318 static void __init
simple_device_type_init(void)
1320 device_type_add(&simple_device_type
);
1323 void device_hotplug_event(const char *name
, bool add
)
1327 wireless_device_hotplug_event(name
, add
);
1329 dev
= device_find(name
);
1330 if (!dev
|| dev
->type
!= &simple_device_type
)
1333 device_set_present(dev
, add
);