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>
30 static struct list_head devtypes
= LIST_HEAD_INIT(devtypes
);
31 static struct avl_tree devices
;
32 static struct blob_buf b
;
34 static const struct blobmsg_policy dev_attrs
[__DEV_ATTR_MAX
] = {
35 [DEV_ATTR_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
36 [DEV_ATTR_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
37 [DEV_ATTR_MTU6
] = { .name
= "mtu6", .type
= BLOBMSG_TYPE_INT32
},
38 [DEV_ATTR_MACADDR
] = { .name
= "macaddr", .type
= BLOBMSG_TYPE_STRING
},
39 [DEV_ATTR_TXQUEUELEN
] = { .name
= "txqueuelen", .type
= BLOBMSG_TYPE_INT32
},
40 [DEV_ATTR_ENABLED
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_BOOL
},
41 [DEV_ATTR_IPV6
] = { .name
= "ipv6", .type
= BLOBMSG_TYPE_BOOL
},
42 [DEV_ATTR_IP6SEGMENTROUTING
] = { .name
= "ip6segmentrouting", .type
= BLOBMSG_TYPE_BOOL
},
43 [DEV_ATTR_PROMISC
] = { .name
= "promisc", .type
= BLOBMSG_TYPE_BOOL
},
44 [DEV_ATTR_RPFILTER
] = { .name
= "rpfilter", .type
= BLOBMSG_TYPE_STRING
},
45 [DEV_ATTR_ACCEPTLOCAL
] = { .name
= "acceptlocal", .type
= BLOBMSG_TYPE_BOOL
},
46 [DEV_ATTR_IGMPVERSION
] = { .name
= "igmpversion", .type
= BLOBMSG_TYPE_INT32
},
47 [DEV_ATTR_MLDVERSION
] = { .name
= "mldversion", .type
= BLOBMSG_TYPE_INT32
},
48 [DEV_ATTR_NEIGHREACHABLETIME
] = { .name
= "neighreachabletime", .type
= BLOBMSG_TYPE_INT32
},
49 [DEV_ATTR_NEIGHGCSTALETIME
] = { .name
= "neighgcstaletime", .type
= BLOBMSG_TYPE_INT32
},
50 [DEV_ATTR_DADTRANSMITS
] = { .name
= "dadtransmits", .type
= BLOBMSG_TYPE_INT32
},
51 [DEV_ATTR_MULTICAST_TO_UNICAST
] = { .name
= "multicast_to_unicast", .type
= BLOBMSG_TYPE_BOOL
},
52 [DEV_ATTR_MULTICAST_ROUTER
] = { .name
= "multicast_router", .type
= BLOBMSG_TYPE_INT32
},
53 [DEV_ATTR_MULTICAST_FAST_LEAVE
] = { .name
= "multicast_fast_leave", . type
= BLOBMSG_TYPE_BOOL
},
54 [DEV_ATTR_MULTICAST
] = { .name
="multicast", .type
= BLOBMSG_TYPE_BOOL
},
55 [DEV_ATTR_LEARNING
] = { .name
="learning", .type
= BLOBMSG_TYPE_BOOL
},
56 [DEV_ATTR_UNICAST_FLOOD
] = { .name
="unicast_flood", .type
= BLOBMSG_TYPE_BOOL
},
57 [DEV_ATTR_SENDREDIRECTS
] = { .name
= "sendredirects", .type
= BLOBMSG_TYPE_BOOL
},
58 [DEV_ATTR_NEIGHLOCKTIME
] = { .name
= "neighlocktime", .type
= BLOBMSG_TYPE_INT32
},
59 [DEV_ATTR_ISOLATE
] = { .name
= "isolate", .type
= BLOBMSG_TYPE_BOOL
},
60 [DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST
] = { .name
= "drop_v4_unicast_in_l2_multicast", .type
= BLOBMSG_TYPE_BOOL
},
61 [DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST
] = { .name
= "drop_v6_unicast_in_l2_multicast", .type
= BLOBMSG_TYPE_BOOL
},
62 [DEV_ATTR_DROP_GRATUITOUS_ARP
] = { .name
= "drop_gratuitous_arp", .type
= BLOBMSG_TYPE_BOOL
},
63 [DEV_ATTR_DROP_UNSOLICITED_NA
] = { .name
= "drop_unsolicited_na", .type
= BLOBMSG_TYPE_BOOL
},
64 [DEV_ATTR_ARP_ACCEPT
] = { .name
= "arp_accept", .type
= BLOBMSG_TYPE_BOOL
},
65 [DEV_ATTR_AUTH
] = { .name
= "auth", .type
= BLOBMSG_TYPE_BOOL
},
66 [DEV_ATTR_SPEED
] = { .name
= "speed", .type
= BLOBMSG_TYPE_INT32
},
67 [DEV_ATTR_DUPLEX
] = { .name
= "duplex", .type
= BLOBMSG_TYPE_BOOL
},
68 [DEV_ATTR_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
69 [DEV_ATTR_PAUSE
] = { .name
= "pause", .type
= BLOBMSG_TYPE_BOOL
},
70 [DEV_ATTR_ASYM_PAUSE
] = { .name
= "asym_pause", .type
= BLOBMSG_TYPE_BOOL
},
71 [DEV_ATTR_RXPAUSE
] = { .name
= "rxpause", .type
= BLOBMSG_TYPE_BOOL
},
72 [DEV_ATTR_TXPAUSE
] = { .name
= "txpause", .type
= BLOBMSG_TYPE_BOOL
},
73 [DEV_ATTR_AUTONEG
] = { .name
= "autoneg", .type
= BLOBMSG_TYPE_BOOL
},
76 const struct uci_blob_param_list device_attr_list
= {
77 .n_params
= __DEV_ATTR_MAX
,
81 static int __devlock
= 0;
83 int device_type_add(struct device_type
*devtype
)
85 if (device_type_get(devtype
->name
)) {
86 netifd_log_message(L_WARNING
, "Device handler '%s' already exists\n",
91 netifd_log_message(L_NOTICE
, "Added device handler type: %s\n",
94 list_add(&devtype
->list
, &devtypes
);
99 device_type_get(const char *tname
)
101 struct device_type
*cur
;
103 list_for_each_entry(cur
, &devtypes
, list
)
104 if (!strcmp(cur
->name
, tname
))
110 static int device_vlan_len(struct kvlist
*kv
, const void *data
)
112 return sizeof(unsigned int);
115 void device_vlan_update(bool done
)
119 avl_for_each_element(&devices
, dev
, avl
) {
120 if (!dev
->vlans
.update
)
124 if (dev
->vlan_aliases
.get_len
)
125 kvlist_free(&dev
->vlan_aliases
);
127 kvlist_init(&dev
->vlan_aliases
, device_vlan_len
);
128 vlist_update(&dev
->vlans
);
130 vlist_flush(&dev
->vlans
);
132 if (dev
->type
->vlan_update
)
133 dev
->type
->vlan_update(dev
);
138 void device_stp_init(void)
142 avl_for_each_element(&devices
, dev
, avl
) {
143 if (!dev
->type
->stp_init
)
146 dev
->type
->stp_init(dev
);
150 static int set_device_state(struct device
*dev
, bool state
)
153 /* Get ifindex for all devices being enabled so a valid */
154 /* ifindex is in place avoiding possible race conditions */
155 device_set_ifindex(dev
, system_if_resolve(dev
));
159 system_if_get_settings(dev
, &dev
->orig_settings
);
160 /* Only keep orig settings based on what needs to be set */
161 dev
->orig_settings
.valid_flags
= dev
->orig_settings
.flags
;
162 dev
->orig_settings
.flags
&= dev
->settings
.flags
;
163 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
168 system_if_apply_settings(dev
, &dev
->orig_settings
, dev
->orig_settings
.flags
);
175 simple_device_set_state(struct device
*dev
, bool state
)
180 pdev
= dev
->parent
.dev
;
181 if (state
&& !pdev
) {
182 pdev
= system_if_get_parent(dev
);
184 device_add_user(&dev
->parent
, pdev
);
189 ret
= device_claim(&dev
->parent
);
191 device_release(&dev
->parent
);
196 return set_device_state(dev
, state
);
199 static struct device
*
200 simple_device_create(const char *name
, struct device_type
*devtype
,
201 struct blob_attr
*attr
)
203 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
204 struct device
*dev
= NULL
;
206 /* device type is unused for simple devices */
209 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
210 dev
= device_get(name
, true);
214 dev
->set_state
= simple_device_set_state
;
215 device_init_settings(dev
, tb
);
220 static void simple_device_free(struct device
*dev
)
223 device_remove_user(&dev
->parent
);
227 struct device_type simple_device_type
= {
228 .name
= "Network device",
229 .config_params
= &device_attr_list
,
231 .create
= simple_device_create
,
232 .check_state
= system_if_check
,
233 .free
= simple_device_free
,
237 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
239 struct device_settings
*os
= &dev
->orig_settings
;
240 struct device_settings
*s
= &dev
->settings
;
242 memset(n
, 0, sizeof(*n
));
243 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
244 n
->mtu6
= s
->flags
& DEV_OPT_MTU6
? s
->mtu6
: os
->mtu6
;
245 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
246 s
->txqueuelen
: os
->txqueuelen
;
248 (s
->flags
& (DEV_OPT_MACADDR
|DEV_OPT_DEFAULT_MACADDR
) ? s
->macaddr
: os
->macaddr
),
250 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
251 n
->ip6segmentrouting
= s
->flags
& DEV_OPT_IP6SEGMENTROUTING
? s
->ip6segmentrouting
: os
->ip6segmentrouting
;
252 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
253 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
254 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
255 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
256 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
257 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
258 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
259 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
260 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
261 n
->neigh4gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
262 s
->neigh4gcstaletime
: os
->neigh4gcstaletime
;
263 n
->neigh6gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
264 s
->neigh6gcstaletime
: os
->neigh6gcstaletime
;
265 n
->neigh4locktime
= s
->flags
& DEV_OPT_NEIGHLOCKTIME
?
266 s
->neigh4locktime
: os
->neigh4locktime
;
267 n
->dadtransmits
= s
->flags
& DEV_OPT_DADTRANSMITS
?
268 s
->dadtransmits
: os
->dadtransmits
;
269 n
->multicast
= s
->flags
& DEV_OPT_MULTICAST
?
270 s
->multicast
: os
->multicast
;
271 n
->multicast_to_unicast
= s
->multicast_to_unicast
;
272 n
->multicast_router
= s
->multicast_router
;
273 n
->multicast_fast_leave
= s
->multicast_fast_leave
;
274 n
->learning
= s
->learning
;
275 n
->unicast_flood
= s
->unicast_flood
;
276 n
->sendredirects
= s
->flags
& DEV_OPT_SENDREDIRECTS
?
277 s
->sendredirects
: os
->sendredirects
;
278 n
->drop_v4_unicast_in_l2_multicast
= s
->flags
& DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST
?
279 s
->drop_v4_unicast_in_l2_multicast
: os
->drop_v4_unicast_in_l2_multicast
;
280 n
->drop_v6_unicast_in_l2_multicast
= s
->flags
& DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST
?
281 s
->drop_v6_unicast_in_l2_multicast
: os
->drop_v6_unicast_in_l2_multicast
;
282 n
->drop_gratuitous_arp
= s
->flags
& DEV_OPT_DROP_GRATUITOUS_ARP
?
283 s
->drop_gratuitous_arp
: os
->drop_gratuitous_arp
;
284 n
->drop_unsolicited_na
= s
->flags
& DEV_OPT_DROP_UNSOLICITED_NA
?
285 s
->drop_unsolicited_na
: os
->drop_unsolicited_na
;
286 n
->arp_accept
= s
->flags
& DEV_OPT_ARP_ACCEPT
?
287 s
->arp_accept
: os
->arp_accept
;
288 n
->auth
= s
->flags
& DEV_OPT_AUTH
? s
->auth
: os
->auth
;
289 n
->speed
= s
->flags
& DEV_OPT_SPEED
? s
->speed
: os
->speed
;
290 n
->duplex
= s
->flags
& DEV_OPT_DUPLEX
? s
->duplex
: os
->duplex
;
291 n
->pause
= s
->flags
& DEV_OPT_PAUSE
? s
->pause
: os
->pause
;
292 n
->asym_pause
= s
->flags
& DEV_OPT_ASYM_PAUSE
? s
->asym_pause
: os
->asym_pause
;
293 n
->rxpause
= s
->flags
& DEV_OPT_RXPAUSE
? s
->rxpause
: os
->rxpause
;
294 n
->txpause
= s
->flags
& DEV_OPT_TXPAUSE
? s
->txpause
: os
->txpause
;
295 n
->autoneg
= s
->flags
& DEV_OPT_AUTONEG
? s
->autoneg
: os
->autoneg
;
296 n
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
299 static bool device_fill_vlan_range(struct device_vlan_range
*r
, const char *val
)
301 unsigned long cur_start
, cur_end
;
304 cur_start
= strtoul(val
, &sep
, 0);
308 cur_end
= strtoul(sep
+ 1, &sep
, 0);
309 if (*sep
|| cur_end
< cur_start
)
312 r
->start
= cur_start
;
319 device_set_extra_vlans(struct device
*dev
, struct blob_attr
*data
)
321 struct blob_attr
*cur
;
325 dev
->n_extra_vlan
= 0;
329 n_vlans
= blobmsg_check_array(data
, BLOBMSG_TYPE_STRING
);
333 dev
->extra_vlan
= realloc(dev
->extra_vlan
, n_vlans
* sizeof(*dev
->extra_vlan
));
334 blobmsg_for_each_attr(cur
, data
, rem
)
335 if (device_fill_vlan_range(&dev
->extra_vlan
[dev
->n_extra_vlan
],
336 blobmsg_get_string(cur
)))
341 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
343 struct device_settings
*s
= &dev
->settings
;
344 struct blob_attr
*cur
;
345 struct ether_addr
*ea
;
346 bool disabled
= false;
349 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
350 disabled
= !blobmsg_get_bool(cur
);
352 if ((cur
= tb
[DEV_ATTR_MTU
]) && blobmsg_get_u32(cur
) >= 68) {
353 s
->mtu
= blobmsg_get_u32(cur
);
354 s
->flags
|= DEV_OPT_MTU
;
357 if ((cur
= tb
[DEV_ATTR_MTU6
]) && blobmsg_get_u32(cur
) >= 1280) {
358 s
->mtu6
= blobmsg_get_u32(cur
);
359 s
->flags
|= DEV_OPT_MTU6
;
362 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
363 s
->txqueuelen
= blobmsg_get_u32(cur
);
364 s
->flags
|= DEV_OPT_TXQUEUELEN
;
367 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
368 ea
= ether_aton(blobmsg_data(cur
));
370 memcpy(s
->macaddr
, ea
, 6);
371 s
->flags
|= DEV_OPT_MACADDR
;
375 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
376 s
->ipv6
= blobmsg_get_bool(cur
);
377 s
->flags
|= DEV_OPT_IPV6
;
380 if ((cur
= tb
[DEV_ATTR_IP6SEGMENTROUTING
])) {
381 s
->ip6segmentrouting
= blobmsg_get_bool(cur
);
382 s
->flags
|= DEV_OPT_IP6SEGMENTROUTING
;
385 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
386 s
->promisc
= blobmsg_get_bool(cur
);
387 s
->flags
|= DEV_OPT_PROMISC
;
390 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
391 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
392 s
->flags
|= DEV_OPT_RPFILTER
;
394 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
397 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
398 s
->acceptlocal
= blobmsg_get_bool(cur
);
399 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
402 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
403 s
->igmpversion
= blobmsg_get_u32(cur
);
404 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
405 s
->flags
|= DEV_OPT_IGMPVERSION
;
407 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
410 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
411 s
->mldversion
= blobmsg_get_u32(cur
);
412 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
413 s
->flags
|= DEV_OPT_MLDVERSION
;
415 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
418 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
419 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
420 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
423 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
424 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
425 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
428 if ((cur
= tb
[DEV_ATTR_NEIGHLOCKTIME
])) {
429 s
->neigh4locktime
= blobmsg_get_u32(cur
);
430 s
->flags
|= DEV_OPT_NEIGHLOCKTIME
;
433 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
434 s
->dadtransmits
= blobmsg_get_u32(cur
);
435 s
->flags
|= DEV_OPT_DADTRANSMITS
;
438 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
439 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
440 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
443 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
444 s
->multicast_router
= blobmsg_get_u32(cur
);
445 if (s
->multicast_router
<= 2)
446 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
448 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
451 if ((cur
= tb
[DEV_ATTR_MULTICAST_FAST_LEAVE
])) {
452 s
->multicast_fast_leave
= blobmsg_get_bool(cur
);
453 s
->flags
|= DEV_OPT_MULTICAST_FAST_LEAVE
;
456 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
457 s
->multicast
= blobmsg_get_bool(cur
);
458 s
->flags
|= DEV_OPT_MULTICAST
;
461 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
462 s
->learning
= blobmsg_get_bool(cur
);
463 s
->flags
|= DEV_OPT_LEARNING
;
466 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
467 s
->unicast_flood
= blobmsg_get_bool(cur
);
468 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
471 if ((cur
= tb
[DEV_ATTR_SENDREDIRECTS
])) {
472 s
->sendredirects
= blobmsg_get_bool(cur
);
473 s
->flags
|= DEV_OPT_SENDREDIRECTS
;
476 if ((cur
= tb
[DEV_ATTR_ISOLATE
])) {
477 s
->isolate
= blobmsg_get_bool(cur
);
478 s
->flags
|= DEV_OPT_ISOLATE
;
481 if ((cur
= tb
[DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST
])) {
482 s
->drop_v4_unicast_in_l2_multicast
= blobmsg_get_bool(cur
);
483 s
->flags
|= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST
;
486 if ((cur
= tb
[DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST
])) {
487 s
->drop_v6_unicast_in_l2_multicast
= blobmsg_get_bool(cur
);
488 s
->flags
|= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST
;
491 if ((cur
= tb
[DEV_ATTR_DROP_GRATUITOUS_ARP
])) {
492 s
->drop_gratuitous_arp
= blobmsg_get_bool(cur
);
493 s
->flags
|= DEV_OPT_DROP_GRATUITOUS_ARP
;
496 if ((cur
= tb
[DEV_ATTR_DROP_UNSOLICITED_NA
])) {
497 s
->drop_unsolicited_na
= blobmsg_get_bool(cur
);
498 s
->flags
|= DEV_OPT_DROP_UNSOLICITED_NA
;
501 if ((cur
= tb
[DEV_ATTR_ARP_ACCEPT
])) {
502 s
->arp_accept
= blobmsg_get_bool(cur
);
503 s
->flags
|= DEV_OPT_ARP_ACCEPT
;
506 if ((cur
= tb
[DEV_ATTR_AUTH
])) {
507 s
->auth
= blobmsg_get_bool(cur
);
508 s
->flags
|= DEV_OPT_AUTH
;
511 if ((cur
= tb
[DEV_ATTR_SPEED
])) {
512 s
->speed
= blobmsg_get_u32(cur
);
513 s
->flags
|= DEV_OPT_SPEED
;
516 if ((cur
= tb
[DEV_ATTR_DUPLEX
])) {
517 s
->duplex
= blobmsg_get_bool(cur
);
518 s
->flags
|= DEV_OPT_DUPLEX
;
521 if ((cur
= tb
[DEV_ATTR_PAUSE
])) {
522 s
->pause
= blobmsg_get_bool(cur
);
523 s
->flags
|= DEV_OPT_PAUSE
;
526 if ((cur
= tb
[DEV_ATTR_ASYM_PAUSE
])) {
527 s
->asym_pause
= blobmsg_get_bool(cur
);
528 s
->flags
|= DEV_OPT_ASYM_PAUSE
;
531 if ((cur
= tb
[DEV_ATTR_RXPAUSE
])) {
532 s
->rxpause
= blobmsg_get_bool(cur
);
533 s
->flags
|= DEV_OPT_RXPAUSE
;
536 if ((cur
= tb
[DEV_ATTR_TXPAUSE
])) {
537 s
->txpause
= blobmsg_get_bool(cur
);
538 s
->flags
|= DEV_OPT_TXPAUSE
;
541 if ((cur
= tb
[DEV_ATTR_AUTONEG
])) {
542 s
->autoneg
= blobmsg_get_bool(cur
);
543 s
->flags
|= DEV_OPT_AUTONEG
;
545 device_set_extra_vlans(dev
, tb
[DEV_ATTR_VLAN
]);
546 device_set_disabled(dev
, disabled
);
549 static void __init
dev_init(void)
551 avl_init(&devices
, avl_strcmp
, true, NULL
);
554 static int device_release_cb(void *ctx
, struct safe_list
*list
)
556 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
558 if (!dep
->dev
|| !dep
->claimed
)
565 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
567 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
570 /* device might have been removed by an earlier callback */
579 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
581 static const char * const event_names
[] = {
582 [DEV_EVENT_ADD
] = "add",
583 [DEV_EVENT_REMOVE
] = "remove",
584 [DEV_EVENT_UP
] = "up",
585 [DEV_EVENT_DOWN
] = "down",
586 [DEV_EVENT_AUTH_UP
] = "auth_up",
587 [DEV_EVENT_LINK_UP
] = "link_up",
588 [DEV_EVENT_LINK_DOWN
] = "link_down",
589 [DEV_EVENT_TOPO_CHANGE
] = "topo_change",
593 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
594 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
596 if (ev
>= ARRAY_SIZE(event_names
) || !event_names
[ev
] || !dev
->ifname
[0])
599 blob_buf_init(&b
, 0);
600 blobmsg_add_string(&b
, "name", dev
->ifname
);
601 blobmsg_add_u8(&b
, "auth_status", dev
->auth_status
);
602 blobmsg_add_u8(&b
, "present", dev
->present
);
603 blobmsg_add_u8(&b
, "active", dev
->active
);
604 blobmsg_add_u8(&b
, "link_active", dev
->link_active
);
605 netifd_ubus_device_notify(event_names
[ev
], b
.head
, -1);
609 device_fill_default_settings(struct device
*dev
)
611 struct device_settings
*s
= &dev
->settings
;
612 struct ether_addr
*ea
;
614 if (!(s
->flags
& DEV_OPT_MACADDR
)) {
615 ea
= config_get_default_macaddr(dev
->ifname
);
617 memcpy(s
->macaddr
, ea
, 6);
618 s
->flags
|= DEV_OPT_DEFAULT_MACADDR
;
623 int device_claim(struct device_user
*dep
)
625 struct device
*dev
= dep
->dev
;
635 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
636 if (++dev
->active
!= 1)
639 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
640 device_fill_default_settings(dev
);
642 /* Get ifindex for external claimed devices so a valid */
643 /* ifindex is in place avoiding possible race conditions */
644 device_set_ifindex(dev
, system_if_resolve(dev
));
648 system_if_get_settings(dev
, &dev
->orig_settings
);
650 ret
= dev
->set_state(dev
, true);
653 device_broadcast_event(dev
, DEV_EVENT_UP
);
655 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
657 dep
->claimed
= false;
663 void device_release(struct device_user
*dep
)
665 struct device
*dev
= dep
->dev
;
670 dep
->claimed
= false;
672 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
673 assert(dev
->active
>= 0);
678 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
680 dev
->set_state(dev
, false);
685 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
688 int device_check_state(struct device
*dev
)
690 if (!dev
->type
->check_state
)
691 return simple_device_type
.check_state(dev
);
693 return dev
->type
->check_state(dev
);
696 int device_init_virtual(struct device
*dev
, struct device_type
*type
, const char *name
)
701 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
702 INIT_SAFE_LIST(&dev
->users
);
703 INIT_SAFE_LIST(&dev
->aliases
);
709 ret
= device_set_ifname(dev
, name
);
715 dev
->set_state
= set_device_state
;
720 int device_init(struct device
*dev
, struct device_type
*type
, const char *ifname
)
724 ret
= device_init_virtual(dev
, type
, ifname
);
728 dev
->avl
.key
= dev
->ifname
;
730 ret
= avl_insert(&devices
, &dev
->avl
);
734 system_if_clear_state(dev
);
739 static struct device
*
740 device_create_default(const char *name
, bool external
)
744 if (!external
&& system_if_force_external(name
))
747 D(DEVICE
, "Create simple device '%s'\n", name
);
748 dev
= calloc(1, sizeof(*dev
));
752 dev
->external
= external
;
753 dev
->set_state
= simple_device_set_state
;
755 if (device_init(dev
, &simple_device_type
, name
) < 0) {
761 dev
->default_config
= true;
763 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
765 device_check_state(dev
);
771 device_find(const char *name
)
775 return avl_find_element(&devices
, name
, dev
, avl
);
779 __device_get(const char *name
, int create
, bool check_vlan
)
783 dev
= avl_find_element(&devices
, name
, dev
, avl
);
785 if (!dev
&& check_vlan
&& strchr(name
, '.'))
786 return get_vlan_device_chain(name
, create
);
789 return device_alias_get(name
+ 1);
792 if (create
> 1 && !dev
->external
) {
793 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
794 dev
->external
= true;
795 device_set_present(dev
, true);
803 return device_create_default(name
, create
> 1);
807 device_delete(struct device
*dev
)
812 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
813 avl_delete(&devices
, &dev
->avl
);
817 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
819 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
821 dep
->cb(dep
, DEV_EVENT_REMOVE
);
827 void device_cleanup(struct device
*dev
)
829 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
830 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
831 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
835 static void __device_set_present(struct device
*dev
, bool state
)
837 if (dev
->present
== state
)
840 dev
->present
= state
;
841 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
845 device_refresh_present(struct device
*dev
)
847 bool state
= dev
->sys_present
;
849 if (dev
->disabled
|| dev
->deferred
)
852 __device_set_present(dev
, state
);
856 device_set_auth_status(struct device
*dev
, bool value
, struct blob_attr
*vlans
)
860 else if (!blob_attr_equal(vlans
, dev
->auth_vlans
))
861 device_set_auth_status(dev
, false, NULL
);
863 free(dev
->auth_vlans
);
864 dev
->auth_vlans
= vlans
? blob_memdup(vlans
) : NULL
;
866 if (dev
->auth_status
== value
)
869 dev
->auth_status
= value
;
873 if (dev
->auth_status
) {
874 device_broadcast_event(dev
, DEV_EVENT_AUTH_UP
);
878 device_broadcast_event(dev
, DEV_EVENT_LINK_DOWN
);
879 if (!dev
->link_active
)
882 device_broadcast_event(dev
, DEV_EVENT_LINK_UP
);
885 void device_set_present(struct device
*dev
, bool state
)
887 if (dev
->sys_present
== state
)
890 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
891 dev
->sys_present
= state
;
892 device_refresh_present(dev
);
894 safe_list_for_each(&dev
->users
, device_release_cb
, NULL
);
897 void device_set_link(struct device
*dev
, bool state
)
899 if (dev
->link_active
== state
)
902 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
904 dev
->link_active
= state
;
906 dev
->auth_status
= false;
907 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
910 void device_set_ifindex(struct device
*dev
, int ifindex
)
912 if (dev
->ifindex
== ifindex
)
915 dev
->ifindex
= ifindex
;
916 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
919 int device_set_ifname(struct device
*dev
, const char *name
)
923 if (!strcmp(dev
->ifname
, name
))
926 if (strlen(name
) > sizeof(dev
->ifname
) - 1)
930 avl_delete(&devices
, &dev
->avl
);
932 strcpy(dev
->ifname
, name
);
935 ret
= avl_insert(&devices
, &dev
->avl
);
938 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
943 static int device_refcount(struct device
*dev
)
945 struct list_head
*list
;
948 list_for_each(list
, &dev
->users
.list
)
951 list_for_each(list
, &dev
->aliases
.list
)
958 __device_add_user(struct device_user
*dep
, struct device
*dev
)
960 struct safe_list
*head
;
965 head
= &dev
->aliases
;
969 safe_list_add(&dep
->list
, head
);
970 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
972 if (dep
->cb
&& dev
->present
) {
973 dep
->cb(dep
, DEV_EVENT_ADD
);
975 dep
->cb(dep
, DEV_EVENT_UP
);
977 if (dev
->link_active
)
978 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
982 void device_add_user(struct device_user
*dep
, struct device
*dev
)
988 device_remove_user(dep
);
993 __device_add_user(dep
, dev
);
997 device_free(struct device
*dev
)
1000 free(dev
->auth_vlans
);
1002 device_cleanup(dev
);
1003 free(dev
->extra_vlan
);
1004 dev
->type
->free(dev
);
1009 __device_free_unused(struct uloop_timeout
*timeout
)
1011 struct device
*dev
, *tmp
;
1013 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
1014 if (!safe_list_empty(&dev
->users
) ||
1015 !safe_list_empty(&dev
->aliases
) ||
1016 dev
->current_config
)
1023 void device_free_unused(void)
1025 static struct uloop_timeout free_timer
= {
1026 .cb
= __device_free_unused
,
1029 uloop_timeout_set(&free_timer
, 1);
1032 void device_remove_user(struct device_user
*dep
)
1034 struct device
*dev
= dep
->dev
;
1039 dep
->hotplug
= false;
1041 device_release(dep
);
1043 safe_list_del(&dep
->list
);
1045 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
1046 device_free_unused();
1050 device_init_pending(void)
1052 struct device
*dev
, *tmp
;
1054 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
1055 if (!dev
->config_pending
)
1058 dev
->type
->config_init(dev
);
1059 dev
->config_pending
= false;
1060 device_check_state(dev
);
1065 device_check_ip6segmentrouting(void)
1068 bool ip6segmentrouting
= false;
1070 avl_for_each_element(&devices
, dev
, avl
)
1071 ip6segmentrouting
|= dev
->settings
.ip6segmentrouting
;
1073 return ip6segmentrouting
;
1076 static enum dev_change_type
1077 device_set_config(struct device
*dev
, struct device_type
*type
,
1078 struct blob_attr
*attr
)
1080 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
1081 const struct uci_blob_param_list
*cfg
= type
->config_params
;
1083 if (type
!= dev
->type
)
1084 return DEV_CONFIG_RECREATE
;
1086 if (dev
->type
->reload
)
1087 return dev
->type
->reload(dev
, attr
);
1089 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
1090 return DEV_CONFIG_NO_CHANGE
;
1092 if (cfg
== &device_attr_list
) {
1093 memset(tb
, 0, sizeof(tb
));
1096 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
1097 blob_data(attr
), blob_len(attr
));
1099 device_init_settings(dev
, tb
);
1100 return DEV_CONFIG_RESTART
;
1102 return DEV_CONFIG_RECREATE
;
1105 enum dev_change_type
1106 device_apply_config(struct device
*dev
, struct device_type
*type
,
1107 struct blob_attr
*config
)
1109 enum dev_change_type change
;
1111 change
= device_set_config(dev
, type
, config
);
1112 if (dev
->external
) {
1113 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
1114 change
= DEV_CONFIG_APPLIED
;
1118 case DEV_CONFIG_RESTART
:
1119 case DEV_CONFIG_APPLIED
:
1120 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
1121 config
= blob_memdup(config
);
1123 dev
->config
= config
;
1124 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
1127 device_set_present(dev
, false);
1128 if (dev
->active
&& !dev
->external
) {
1129 ret
= dev
->set_state(dev
, false);
1131 ret
= dev
->set_state(dev
, true);
1134 device_set_present(dev
, true);
1137 case DEV_CONFIG_NO_CHANGE
:
1138 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
1140 case DEV_CONFIG_RECREATE
:
1148 device_replace(struct device
*dev
, struct device
*odev
)
1150 struct device_user
*dep
;
1154 device_set_present(odev
, false);
1156 while (!list_empty(&odev
->users
.list
)) {
1157 dep
= list_first_entry(&odev
->users
.list
, struct device_user
, list
.list
);
1158 device_release(dep
);
1162 safe_list_del(&dep
->list
);
1163 __device_add_user(dep
, dev
);
1171 device_reset_config(void)
1175 avl_for_each_element(&devices
, dev
, avl
)
1176 dev
->current_config
= false;
1180 device_reset_old(void)
1182 struct device
*dev
, *tmp
, *ndev
;
1184 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
1185 if (dev
->current_config
|| dev
->default_config
)
1188 if (dev
->type
!= &simple_device_type
)
1191 ndev
= device_create_default(dev
->ifname
, dev
->external
);
1195 device_replace(ndev
, dev
);
1200 device_create(const char *name
, struct device_type
*type
,
1201 struct blob_attr
*config
)
1203 struct device
*odev
= NULL
, *dev
;
1204 enum dev_change_type change
;
1206 odev
= device_find(name
);
1208 odev
->current_config
= true;
1209 change
= device_apply_config(odev
, type
, config
);
1211 case DEV_CONFIG_RECREATE
:
1212 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
1213 device_delete(odev
);
1219 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
1221 config
= blob_memdup(config
);
1225 dev
= type
->create(name
, type
, config
);
1229 dev
->current_config
= true;
1230 dev
->config
= config
;
1232 device_replace(dev
, odev
);
1234 if (!config_init
&& dev
->config_pending
) {
1235 type
->config_init(dev
);
1236 dev
->config_pending
= false;
1239 device_check_state(dev
);
1245 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
1247 struct device_settings st
;
1251 avl_for_each_element(&devices
, dev
, avl
) {
1254 c
= blobmsg_open_table(b
, dev
->ifname
);
1255 device_dump_status(b
, dev
);
1256 blobmsg_close_table(b
, c
);
1262 blobmsg_add_u8(b
, "external", dev
->external
);
1263 blobmsg_add_u8(b
, "present", dev
->present
);
1264 blobmsg_add_string(b
, "type", dev
->type
->name
);
1269 blobmsg_add_u8(b
, "up", !!dev
->active
);
1270 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
1271 blobmsg_add_u8(b
, "auth_status", !!dev
->auth_status
);
1273 if (dev
->type
->dump_info
)
1274 dev
->type
->dump_info(dev
, b
);
1276 system_if_dump_info(dev
, b
);
1279 device_merge_settings(dev
, &st
);
1280 if (st
.flags
& DEV_OPT_MTU
)
1281 blobmsg_add_u32(b
, "mtu", st
.mtu
);
1282 if (st
.flags
& DEV_OPT_MTU6
)
1283 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
1284 if (st
.flags
& DEV_OPT_MACADDR
)
1285 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
1286 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
1287 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
1288 if (st
.flags
& DEV_OPT_IPV6
)
1289 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
1290 if (st
.flags
& DEV_OPT_IP6SEGMENTROUTING
)
1291 blobmsg_add_u8(b
, "ip6segmentrouting", st
.ip6segmentrouting
);
1292 if (st
.flags
& DEV_OPT_PROMISC
)
1293 blobmsg_add_u8(b
, "promisc", st
.promisc
);
1294 if (st
.flags
& DEV_OPT_RPFILTER
)
1295 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
1296 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
1297 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
1298 if (st
.flags
& DEV_OPT_IGMPVERSION
)
1299 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
1300 if (st
.flags
& DEV_OPT_MLDVERSION
)
1301 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
1302 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
1303 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
1304 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
1306 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
1307 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
1308 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
1310 if (st
.flags
& DEV_OPT_NEIGHLOCKTIME
)
1311 blobmsg_add_u32(b
, "neigh4locktime", st
.neigh4locktime
);
1312 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
1313 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
1314 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
1315 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
1316 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
1317 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
1318 if (st
.flags
& DEV_OPT_MULTICAST_FAST_LEAVE
)
1319 blobmsg_add_u8(b
, "multicast_fast_leave", st
.multicast_fast_leave
);
1320 if (st
.flags
& DEV_OPT_MULTICAST
)
1321 blobmsg_add_u8(b
, "multicast", st
.multicast
);
1322 if (st
.flags
& DEV_OPT_LEARNING
)
1323 blobmsg_add_u8(b
, "learning", st
.learning
);
1324 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
1325 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
1326 if (st
.flags
& DEV_OPT_SENDREDIRECTS
)
1327 blobmsg_add_u8(b
, "sendredirects", st
.sendredirects
);
1328 if (st
.flags
& DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST
)
1329 blobmsg_add_u8(b
, "drop_v4_unicast_in_l2_multicast", st
.drop_v4_unicast_in_l2_multicast
);
1330 if (st
.flags
& DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST
)
1331 blobmsg_add_u8(b
, "drop_v6_unicast_in_l2_multicast", st
.drop_v6_unicast_in_l2_multicast
);
1332 if (st
.flags
& DEV_OPT_DROP_GRATUITOUS_ARP
)
1333 blobmsg_add_u8(b
, "drop_gratuitous_arp", st
.drop_gratuitous_arp
);
1334 if (st
.flags
& DEV_OPT_DROP_UNSOLICITED_NA
)
1335 blobmsg_add_u8(b
, "drop_unsolicited_na", st
.drop_unsolicited_na
);
1336 if (st
.flags
& DEV_OPT_ARP_ACCEPT
)
1337 blobmsg_add_u8(b
, "arp_accept", st
.arp_accept
);
1338 if (st
.flags
& DEV_OPT_AUTH
)
1339 blobmsg_add_u8(b
, "auth", st
.auth
);
1342 s
= blobmsg_open_table(b
, "statistics");
1343 if (dev
->type
->dump_stats
)
1344 dev
->type
->dump_stats(dev
, b
);
1346 system_if_dump_stats(dev
, b
);
1347 blobmsg_close_table(b
, s
);
1350 static void __init
simple_device_type_init(void)
1352 device_type_add(&simple_device_type
);
1355 void device_hotplug_event(const char *name
, bool add
)
1359 wireless_device_hotplug_event(name
, add
);
1361 dev
= device_find(name
);
1362 if (!dev
|| dev
->type
!= &simple_device_type
)
1365 device_set_present(dev
, add
);