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
},
68 const struct uci_blob_param_list device_attr_list
= {
69 .n_params
= __DEV_ATTR_MAX
,
73 static int __devlock
= 0;
75 int device_type_add(struct device_type
*devtype
)
77 if (device_type_get(devtype
->name
)) {
78 netifd_log_message(L_WARNING
, "Device handler '%s' already exists\n",
83 netifd_log_message(L_NOTICE
, "Added device handler type: %s\n",
86 list_add(&devtype
->list
, &devtypes
);
91 device_type_get(const char *tname
)
93 struct device_type
*cur
;
95 list_for_each_entry(cur
, &devtypes
, list
)
96 if (!strcmp(cur
->name
, tname
))
102 void device_lock(void)
107 void device_unlock(void)
111 device_free_unused(NULL
);
114 static int device_vlan_len(struct kvlist
*kv
, const void *data
)
116 return sizeof(unsigned int);
119 void device_vlan_update(bool done
)
123 avl_for_each_element(&devices
, dev
, avl
) {
124 if (!dev
->vlans
.update
)
128 if (dev
->vlan_aliases
.get_len
)
129 kvlist_free(&dev
->vlan_aliases
);
131 kvlist_init(&dev
->vlan_aliases
, device_vlan_len
);
132 vlist_update(&dev
->vlans
);
134 vlist_flush(&dev
->vlans
);
136 if (dev
->type
->vlan_update
)
137 dev
->type
->vlan_update(dev
);
142 void device_stp_init(void)
146 avl_for_each_element(&devices
, dev
, avl
) {
147 if (!dev
->type
->stp_init
)
150 dev
->type
->stp_init(dev
);
154 static int set_device_state(struct device
*dev
, bool state
)
157 /* Get ifindex for all devices being enabled so a valid */
158 /* ifindex is in place avoiding possible race conditions */
159 device_set_ifindex(dev
, system_if_resolve(dev
));
163 system_if_get_settings(dev
, &dev
->orig_settings
);
164 /* Only keep orig settings based on what needs to be set */
165 dev
->orig_settings
.valid_flags
= dev
->orig_settings
.flags
;
166 dev
->orig_settings
.flags
&= dev
->settings
.flags
;
167 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
172 system_if_apply_settings(dev
, &dev
->orig_settings
, dev
->orig_settings
.flags
);
179 simple_device_set_state(struct device
*dev
, bool state
)
184 pdev
= dev
->parent
.dev
;
185 if (state
&& !pdev
) {
186 pdev
= system_if_get_parent(dev
);
188 device_add_user(&dev
->parent
, pdev
);
193 ret
= device_claim(&dev
->parent
);
195 device_release(&dev
->parent
);
200 return set_device_state(dev
, state
);
203 static struct device
*
204 simple_device_create(const char *name
, struct device_type
*devtype
,
205 struct blob_attr
*attr
)
207 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
208 struct device
*dev
= NULL
;
210 /* device type is unused for simple devices */
213 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
214 dev
= device_get(name
, true);
218 dev
->set_state
= simple_device_set_state
;
219 device_init_settings(dev
, tb
);
224 static void simple_device_free(struct device
*dev
)
227 device_remove_user(&dev
->parent
);
231 struct device_type simple_device_type
= {
232 .name
= "Network device",
233 .config_params
= &device_attr_list
,
235 .create
= simple_device_create
,
236 .check_state
= system_if_check
,
237 .free
= simple_device_free
,
241 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
243 struct device_settings
*os
= &dev
->orig_settings
;
244 struct device_settings
*s
= &dev
->settings
;
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
;
252 (s
->flags
& (DEV_OPT_MACADDR
|DEV_OPT_DEFAULT_MACADDR
) ? s
->macaddr
: os
->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
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
299 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
301 struct device_settings
*s
= &dev
->settings
;
302 struct blob_attr
*cur
;
303 struct ether_addr
*ea
;
304 bool disabled
= false;
307 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
308 disabled
= !blobmsg_get_bool(cur
);
310 if ((cur
= tb
[DEV_ATTR_MTU
]) && blobmsg_get_u32(cur
) >= 68) {
311 s
->mtu
= blobmsg_get_u32(cur
);
312 s
->flags
|= DEV_OPT_MTU
;
315 if ((cur
= tb
[DEV_ATTR_MTU6
]) && blobmsg_get_u32(cur
) >= 1280) {
316 s
->mtu6
= blobmsg_get_u32(cur
);
317 s
->flags
|= DEV_OPT_MTU6
;
320 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
321 s
->txqueuelen
= blobmsg_get_u32(cur
);
322 s
->flags
|= DEV_OPT_TXQUEUELEN
;
325 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
326 ea
= ether_aton(blobmsg_data(cur
));
328 memcpy(s
->macaddr
, ea
, 6);
329 s
->flags
|= DEV_OPT_MACADDR
;
333 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
334 s
->ipv6
= blobmsg_get_bool(cur
);
335 s
->flags
|= DEV_OPT_IPV6
;
338 if ((cur
= tb
[DEV_ATTR_IP6SEGMENTROUTING
])) {
339 s
->ip6segmentrouting
= blobmsg_get_bool(cur
);
340 s
->flags
|= DEV_OPT_IP6SEGMENTROUTING
;
343 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
344 s
->promisc
= blobmsg_get_bool(cur
);
345 s
->flags
|= DEV_OPT_PROMISC
;
348 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
349 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
350 s
->flags
|= DEV_OPT_RPFILTER
;
352 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
355 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
356 s
->acceptlocal
= blobmsg_get_bool(cur
);
357 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
360 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
361 s
->igmpversion
= blobmsg_get_u32(cur
);
362 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
363 s
->flags
|= DEV_OPT_IGMPVERSION
;
365 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
368 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
369 s
->mldversion
= blobmsg_get_u32(cur
);
370 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
371 s
->flags
|= DEV_OPT_MLDVERSION
;
373 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
376 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
377 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
378 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
381 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
382 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
383 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
386 if ((cur
= tb
[DEV_ATTR_NEIGHLOCKTIME
])) {
387 s
->neigh4locktime
= blobmsg_get_u32(cur
);
388 s
->flags
|= DEV_OPT_NEIGHLOCKTIME
;
391 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
392 s
->dadtransmits
= blobmsg_get_u32(cur
);
393 s
->flags
|= DEV_OPT_DADTRANSMITS
;
396 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
397 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
398 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
401 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
402 s
->multicast_router
= blobmsg_get_u32(cur
);
403 if (s
->multicast_router
<= 2)
404 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
406 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
409 if ((cur
= tb
[DEV_ATTR_MULTICAST_FAST_LEAVE
])) {
410 s
->multicast_fast_leave
= blobmsg_get_bool(cur
);
411 s
->flags
|= DEV_OPT_MULTICAST_FAST_LEAVE
;
414 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
415 s
->multicast
= blobmsg_get_bool(cur
);
416 s
->flags
|= DEV_OPT_MULTICAST
;
419 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
420 s
->learning
= blobmsg_get_bool(cur
);
421 s
->flags
|= DEV_OPT_LEARNING
;
424 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
425 s
->unicast_flood
= blobmsg_get_bool(cur
);
426 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
429 if ((cur
= tb
[DEV_ATTR_SENDREDIRECTS
])) {
430 s
->sendredirects
= blobmsg_get_bool(cur
);
431 s
->flags
|= DEV_OPT_SENDREDIRECTS
;
434 if ((cur
= tb
[DEV_ATTR_ISOLATE
])) {
435 s
->isolate
= blobmsg_get_bool(cur
);
436 s
->flags
|= DEV_OPT_ISOLATE
;
439 if ((cur
= tb
[DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST
])) {
440 s
->drop_v4_unicast_in_l2_multicast
= blobmsg_get_bool(cur
);
441 s
->flags
|= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST
;
444 if ((cur
= tb
[DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST
])) {
445 s
->drop_v6_unicast_in_l2_multicast
= blobmsg_get_bool(cur
);
446 s
->flags
|= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST
;
449 if ((cur
= tb
[DEV_ATTR_DROP_GRATUITOUS_ARP
])) {
450 s
->drop_gratuitous_arp
= blobmsg_get_bool(cur
);
451 s
->flags
|= DEV_OPT_DROP_GRATUITOUS_ARP
;
454 if ((cur
= tb
[DEV_ATTR_DROP_UNSOLICITED_NA
])) {
455 s
->drop_unsolicited_na
= blobmsg_get_bool(cur
);
456 s
->flags
|= DEV_OPT_DROP_UNSOLICITED_NA
;
459 if ((cur
= tb
[DEV_ATTR_ARP_ACCEPT
])) {
460 s
->arp_accept
= blobmsg_get_bool(cur
);
461 s
->flags
|= DEV_OPT_ARP_ACCEPT
;
464 if ((cur
= tb
[DEV_ATTR_AUTH
])) {
465 s
->auth
= blobmsg_get_bool(cur
);
466 s
->flags
|= DEV_OPT_AUTH
;
469 if ((cur
= tb
[DEV_ATTR_SPEED
])) {
470 s
->speed
= blobmsg_get_u32(cur
);
471 s
->flags
|= DEV_OPT_SPEED
;
474 if ((cur
= tb
[DEV_ATTR_DUPLEX
])) {
475 s
->duplex
= blobmsg_get_bool(cur
);
476 s
->flags
|= DEV_OPT_DUPLEX
;
479 device_set_disabled(dev
, disabled
);
482 static void __init
dev_init(void)
484 avl_init(&devices
, avl_strcmp
, true, NULL
);
487 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
489 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
492 /* device might have been removed by an earlier callback */
501 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
505 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
506 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
510 device_fill_default_settings(struct device
*dev
)
512 struct device_settings
*s
= &dev
->settings
;
513 struct ether_addr
*ea
;
515 if (!(s
->flags
& DEV_OPT_MACADDR
)) {
516 ea
= config_get_default_macaddr(dev
->ifname
);
518 memcpy(s
->macaddr
, ea
, 6);
519 s
->flags
|= DEV_OPT_DEFAULT_MACADDR
;
524 int device_claim(struct device_user
*dep
)
526 struct device
*dev
= dep
->dev
;
536 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
537 if (++dev
->active
!= 1)
540 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
541 device_fill_default_settings(dev
);
543 /* Get ifindex for external claimed devices so a valid */
544 /* ifindex is in place avoiding possible race conditions */
545 device_set_ifindex(dev
, system_if_resolve(dev
));
549 system_if_get_settings(dev
, &dev
->orig_settings
);
551 ret
= dev
->set_state(dev
, true);
554 device_broadcast_event(dev
, DEV_EVENT_UP
);
556 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
558 dep
->claimed
= false;
564 void device_release(struct device_user
*dep
)
566 struct device
*dev
= dep
->dev
;
571 dep
->claimed
= false;
573 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
574 assert(dev
->active
>= 0);
579 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
581 dev
->set_state(dev
, false);
586 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
589 int device_check_state(struct device
*dev
)
591 if (!dev
->type
->check_state
)
592 return simple_device_type
.check_state(dev
);
594 return dev
->type
->check_state(dev
);
597 int device_init_virtual(struct device
*dev
, struct device_type
*type
, const char *name
)
602 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
603 INIT_SAFE_LIST(&dev
->users
);
604 INIT_SAFE_LIST(&dev
->aliases
);
610 ret
= device_set_ifname(dev
, name
);
616 dev
->set_state
= set_device_state
;
621 int device_init(struct device
*dev
, struct device_type
*type
, const char *ifname
)
625 ret
= device_init_virtual(dev
, type
, ifname
);
629 dev
->avl
.key
= dev
->ifname
;
631 ret
= avl_insert(&devices
, &dev
->avl
);
635 system_if_clear_state(dev
);
640 static struct device
*
641 device_create_default(const char *name
, bool external
)
645 if (!external
&& system_if_force_external(name
))
648 D(DEVICE
, "Create simple device '%s'\n", name
);
649 dev
= calloc(1, sizeof(*dev
));
653 dev
->external
= external
;
654 dev
->set_state
= simple_device_set_state
;
656 if (device_init(dev
, &simple_device_type
, name
) < 0) {
662 dev
->default_config
= true;
664 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
666 device_check_state(dev
);
672 device_find(const char *name
)
676 return avl_find_element(&devices
, name
, dev
, avl
);
680 __device_get(const char *name
, int create
, bool check_vlan
)
684 dev
= avl_find_element(&devices
, name
, dev
, avl
);
686 if (!dev
&& check_vlan
&& strchr(name
, '.'))
687 return get_vlan_device_chain(name
, create
);
690 return device_alias_get(name
+ 1);
693 if (create
> 1 && !dev
->external
) {
694 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
695 dev
->external
= true;
696 device_set_present(dev
, true);
704 return device_create_default(name
, create
> 1);
708 device_delete(struct device
*dev
)
713 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
714 avl_delete(&devices
, &dev
->avl
);
718 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
720 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
722 dep
->cb(dep
, DEV_EVENT_REMOVE
);
728 void device_cleanup(struct device
*dev
)
730 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
731 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
732 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
736 static void __device_set_present(struct device
*dev
, bool state
)
738 if (dev
->present
== state
)
741 dev
->present
= state
;
742 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
746 device_refresh_present(struct device
*dev
)
748 bool state
= dev
->sys_present
;
750 if (dev
->disabled
|| dev
->deferred
)
753 __device_set_present(dev
, state
);
757 device_set_auth_status(struct device
*dev
, bool value
)
759 if (dev
->auth_status
== value
)
762 dev
->auth_status
= value
;
766 if (dev
->auth_status
) {
767 device_broadcast_event(dev
, DEV_EVENT_AUTH_UP
);
771 device_broadcast_event(dev
, DEV_EVENT_LINK_DOWN
);
772 if (!dev
->link_active
)
775 device_broadcast_event(dev
, DEV_EVENT_LINK_UP
);
778 void device_set_present(struct device
*dev
, bool state
)
780 if (dev
->sys_present
== state
)
783 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
784 dev
->sys_present
= state
;
785 device_refresh_present(dev
);
788 void device_set_link(struct device
*dev
, bool state
)
790 if (dev
->link_active
== state
)
793 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
795 dev
->link_active
= state
;
797 dev
->auth_status
= false;
798 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
801 void device_set_ifindex(struct device
*dev
, int ifindex
)
803 if (dev
->ifindex
== ifindex
)
806 dev
->ifindex
= ifindex
;
807 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
810 int device_set_ifname(struct device
*dev
, const char *name
)
814 if (!strcmp(dev
->ifname
, name
))
817 if (strlen(name
) > sizeof(dev
->ifname
) - 1)
821 avl_delete(&devices
, &dev
->avl
);
823 strcpy(dev
->ifname
, name
);
826 ret
= avl_insert(&devices
, &dev
->avl
);
829 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
834 static int device_refcount(struct device
*dev
)
836 struct list_head
*list
;
839 list_for_each(list
, &dev
->users
.list
)
842 list_for_each(list
, &dev
->aliases
.list
)
849 __device_add_user(struct device_user
*dep
, struct device
*dev
)
851 struct safe_list
*head
;
856 head
= &dev
->aliases
;
860 safe_list_add(&dep
->list
, head
);
861 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
863 if (dep
->cb
&& dev
->present
) {
864 dep
->cb(dep
, DEV_EVENT_ADD
);
866 dep
->cb(dep
, DEV_EVENT_UP
);
868 if (dev
->link_active
)
869 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
873 void device_add_user(struct device_user
*dep
, struct device
*dev
)
879 device_remove_user(dep
);
884 __device_add_user(dep
, dev
);
888 device_free(struct device
*dev
)
893 dev
->type
->free(dev
);
898 __device_free_unused(struct device
*dev
)
900 if (!safe_list_empty(&dev
->users
) ||
901 !safe_list_empty(&dev
->aliases
) ||
902 dev
->current_config
|| __devlock
)
908 void device_remove_user(struct device_user
*dep
)
910 struct device
*dev
= dep
->dev
;
915 dep
->hotplug
= false;
919 safe_list_del(&dep
->list
);
921 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
922 __device_free_unused(dev
);
926 device_free_unused(struct device
*dev
)
931 return __device_free_unused(dev
);
933 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
934 __device_free_unused(dev
);
938 device_init_pending(void)
940 struct device
*dev
, *tmp
;
942 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
943 if (!dev
->config_pending
)
946 dev
->type
->config_init(dev
);
947 dev
->config_pending
= false;
948 device_check_state(dev
);
953 device_check_ip6segmentrouting(void)
956 bool ip6segmentrouting
= false;
958 avl_for_each_element(&devices
, dev
, avl
)
959 ip6segmentrouting
|= dev
->settings
.ip6segmentrouting
;
961 return ip6segmentrouting
;
964 static enum dev_change_type
965 device_set_config(struct device
*dev
, struct device_type
*type
,
966 struct blob_attr
*attr
)
968 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
969 const struct uci_blob_param_list
*cfg
= type
->config_params
;
971 if (type
!= dev
->type
)
972 return DEV_CONFIG_RECREATE
;
974 if (dev
->type
->reload
)
975 return dev
->type
->reload(dev
, attr
);
977 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
978 return DEV_CONFIG_NO_CHANGE
;
980 if (cfg
== &device_attr_list
) {
981 memset(tb
, 0, sizeof(tb
));
984 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
985 blob_data(attr
), blob_len(attr
));
987 device_init_settings(dev
, tb
);
988 return DEV_CONFIG_RESTART
;
990 return DEV_CONFIG_RECREATE
;
994 device_apply_config(struct device
*dev
, struct device_type
*type
,
995 struct blob_attr
*config
)
997 enum dev_change_type change
;
999 change
= device_set_config(dev
, type
, config
);
1000 if (dev
->external
) {
1001 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
1002 change
= DEV_CONFIG_APPLIED
;
1006 case DEV_CONFIG_RESTART
:
1007 case DEV_CONFIG_APPLIED
:
1008 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
1009 config
= blob_memdup(config
);
1011 dev
->config
= config
;
1012 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
1015 device_set_present(dev
, false);
1016 if (dev
->active
&& !dev
->external
) {
1017 ret
= dev
->set_state(dev
, false);
1019 ret
= dev
->set_state(dev
, true);
1022 device_set_present(dev
, true);
1025 case DEV_CONFIG_NO_CHANGE
:
1026 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
1028 case DEV_CONFIG_RECREATE
:
1036 device_replace(struct device
*dev
, struct device
*odev
)
1038 struct device_user
*dep
, *tmp
;
1042 device_set_present(odev
, false);
1044 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
1045 device_release(dep
);
1046 safe_list_del(&dep
->list
);
1047 __device_add_user(dep
, dev
);
1055 device_reset_config(void)
1059 avl_for_each_element(&devices
, dev
, avl
)
1060 dev
->current_config
= false;
1064 device_reset_old(void)
1066 struct device
*dev
, *tmp
, *ndev
;
1068 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
1069 if (dev
->current_config
|| dev
->default_config
)
1072 if (dev
->type
!= &simple_device_type
)
1075 ndev
= device_create_default(dev
->ifname
, dev
->external
);
1079 device_replace(ndev
, dev
);
1084 device_create(const char *name
, struct device_type
*type
,
1085 struct blob_attr
*config
)
1087 struct device
*odev
= NULL
, *dev
;
1088 enum dev_change_type change
;
1090 odev
= device_find(name
);
1092 odev
->current_config
= true;
1093 change
= device_apply_config(odev
, type
, config
);
1095 case DEV_CONFIG_RECREATE
:
1096 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
1097 device_delete(odev
);
1103 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
1105 config
= blob_memdup(config
);
1109 dev
= type
->create(name
, type
, config
);
1113 dev
->current_config
= true;
1114 dev
->config
= config
;
1116 device_replace(dev
, odev
);
1118 if (!config_init
&& dev
->config_pending
) {
1119 type
->config_init(dev
);
1120 dev
->config_pending
= false;
1123 device_check_state(dev
);
1129 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
1131 struct device_settings st
;
1135 avl_for_each_element(&devices
, dev
, avl
) {
1138 c
= blobmsg_open_table(b
, dev
->ifname
);
1139 device_dump_status(b
, dev
);
1140 blobmsg_close_table(b
, c
);
1146 blobmsg_add_u8(b
, "external", dev
->external
);
1147 blobmsg_add_u8(b
, "present", dev
->present
);
1148 blobmsg_add_string(b
, "type", dev
->type
->name
);
1153 blobmsg_add_u8(b
, "up", !!dev
->active
);
1154 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
1155 blobmsg_add_u8(b
, "auth_status", !!dev
->auth_status
);
1157 if (dev
->type
->dump_info
)
1158 dev
->type
->dump_info(dev
, b
);
1160 system_if_dump_info(dev
, b
);
1163 device_merge_settings(dev
, &st
);
1164 if (st
.flags
& DEV_OPT_MTU
)
1165 blobmsg_add_u32(b
, "mtu", st
.mtu
);
1166 if (st
.flags
& DEV_OPT_MTU6
)
1167 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
1168 if (st
.flags
& DEV_OPT_MACADDR
)
1169 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
1170 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
1171 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
1172 if (st
.flags
& DEV_OPT_IPV6
)
1173 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
1174 if (st
.flags
& DEV_OPT_IP6SEGMENTROUTING
)
1175 blobmsg_add_u8(b
, "ip6segmentrouting", st
.ip6segmentrouting
);
1176 if (st
.flags
& DEV_OPT_PROMISC
)
1177 blobmsg_add_u8(b
, "promisc", st
.promisc
);
1178 if (st
.flags
& DEV_OPT_RPFILTER
)
1179 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
1180 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
1181 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
1182 if (st
.flags
& DEV_OPT_IGMPVERSION
)
1183 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
1184 if (st
.flags
& DEV_OPT_MLDVERSION
)
1185 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
1186 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
1187 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
1188 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
1190 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
1191 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
1192 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
1194 if (st
.flags
& DEV_OPT_NEIGHLOCKTIME
)
1195 blobmsg_add_u32(b
, "neigh4locktime", st
.neigh4locktime
);
1196 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
1197 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
1198 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
1199 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
1200 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
1201 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
1202 if (st
.flags
& DEV_OPT_MULTICAST_FAST_LEAVE
)
1203 blobmsg_add_u8(b
, "multicast_fast_leave", st
.multicast_fast_leave
);
1204 if (st
.flags
& DEV_OPT_MULTICAST
)
1205 blobmsg_add_u8(b
, "multicast", st
.multicast
);
1206 if (st
.flags
& DEV_OPT_LEARNING
)
1207 blobmsg_add_u8(b
, "learning", st
.learning
);
1208 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
1209 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
1210 if (st
.flags
& DEV_OPT_SENDREDIRECTS
)
1211 blobmsg_add_u8(b
, "sendredirects", st
.sendredirects
);
1212 if (st
.flags
& DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST
)
1213 blobmsg_add_u8(b
, "drop_v4_unicast_in_l2_multicast", st
.drop_v4_unicast_in_l2_multicast
);
1214 if (st
.flags
& DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST
)
1215 blobmsg_add_u8(b
, "drop_v6_unicast_in_l2_multicast", st
.drop_v6_unicast_in_l2_multicast
);
1216 if (st
.flags
& DEV_OPT_DROP_GRATUITOUS_ARP
)
1217 blobmsg_add_u8(b
, "drop_gratuitous_arp", st
.drop_gratuitous_arp
);
1218 if (st
.flags
& DEV_OPT_DROP_UNSOLICITED_NA
)
1219 blobmsg_add_u8(b
, "drop_unsolicited_na", st
.drop_unsolicited_na
);
1220 if (st
.flags
& DEV_OPT_ARP_ACCEPT
)
1221 blobmsg_add_u8(b
, "arp_accept", st
.arp_accept
);
1222 if (st
.flags
& DEV_OPT_AUTH
)
1223 blobmsg_add_u8(b
, "auth", st
.auth
);
1226 s
= blobmsg_open_table(b
, "statistics");
1227 if (dev
->type
->dump_stats
)
1228 dev
->type
->dump_stats(dev
, b
);
1230 system_if_dump_stats(dev
, b
);
1231 blobmsg_close_table(b
, s
);
1234 static void __init
simple_device_type_init(void)
1236 device_type_add(&simple_device_type
);
1239 void device_hotplug_event(const char *name
, bool add
)
1243 wireless_device_hotplug_event(name
, add
);
1245 dev
= device_find(name
);
1246 if (!dev
|| dev
->type
!= &simple_device_type
)
1249 device_set_present(dev
, add
);