• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/device.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt