• 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_broadcast_cb(void *ctx, struct safe_list *list)
476 {
477         struct device_user *dep = container_of(list, struct device_user, list);
478         int *ev = ctx;
479 
480         /* device might have been removed by an earlier callback */
481         if (!dep->dev)
482                 return 0;
483 
484         if (dep->cb)
485                 dep->cb(dep, *ev);
486         return 0;
487 }
488 
489 void device_broadcast_event(struct device *dev, enum device_event ev)
490 {
491         int dev_ev = ev;
492 
493         safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
494         safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
495 }
496 
497 static void
498 device_fill_default_settings(struct device *dev)
499 {
500         struct device_settings *s = &dev->settings;
501         struct ether_addr *ea;
502 
503         if (!(s->flags & DEV_OPT_MACADDR)) {
504                 ea = config_get_default_macaddr(dev->ifname);
505                 if (ea) {
506                         memcpy(s->macaddr, ea, 6);
507                         s->flags |= DEV_OPT_DEFAULT_MACADDR;
508                 }
509         }
510 }
511 
512 int device_claim(struct device_user *dep)
513 {
514         struct device *dev = dep->dev;
515         int ret = 0;
516 
517         if (dep->claimed)
518                 return 0;
519 
520         if (!dev)
521                 return -1;
522 
523         dep->claimed = true;
524         D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
525         if (++dev->active != 1)
526                 return 0;
527 
528         device_broadcast_event(dev, DEV_EVENT_SETUP);
529         device_fill_default_settings(dev);
530         if (dev->external) {
531                 /* Get ifindex for external claimed devices so a valid   */
532                 /* ifindex is in place avoiding possible race conditions */
533                 device_set_ifindex(dev, system_if_resolve(dev));
534                 if (!dev->ifindex)
535                         ret = -1;
536 
537                 system_if_get_settings(dev, &dev->orig_settings);
538         } else
539                 ret = dev->set_state(dev, true);
540 
541         if (ret == 0)
542                 device_broadcast_event(dev, DEV_EVENT_UP);
543         else {
544                 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
545                 dev->active = 0;
546                 dep->claimed = false;
547         }
548 
549         return ret;
550 }
551 
552 void device_release(struct device_user *dep)
553 {
554         struct device *dev = dep->dev;
555 
556         if (!dep->claimed)
557                 return;
558 
559         dep->claimed = false;
560         dev->active--;
561         D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
562         assert(dev->active >= 0);
563 
564         if (dev->active)
565                 return;
566 
567         device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
568         if (!dev->external)
569                 dev->set_state(dev, false);
570 
571         if (dev->active)
572                 return;
573 
574         device_broadcast_event(dev, DEV_EVENT_DOWN);
575 }
576 
577 int device_check_state(struct device *dev)
578 {
579         if (!dev->type->check_state)
580                 return simple_device_type.check_state(dev);
581 
582         return dev->type->check_state(dev);
583 }
584 
585 int device_init_virtual(struct device *dev, struct device_type *type, const char *name)
586 {
587         assert(dev);
588         assert(type);
589 
590         D(DEVICE, "Initialize device '%s'\n", name ? name : "");
591         INIT_SAFE_LIST(&dev->users);
592         INIT_SAFE_LIST(&dev->aliases);
593         dev->type = type;
594 
595         if (name) {
596                 int ret;
597 
598                 ret = device_set_ifname(dev, name);
599                 if (ret < 0)
600                         return ret;
601         }
602 
603         if (!dev->set_state)
604                 dev->set_state = set_device_state;
605 
606         return 0;
607 }
608 
609 int device_init(struct device *dev, struct device_type *type, const char *ifname)
610 {
611         int ret;
612 
613         ret = device_init_virtual(dev, type, ifname);
614         if (ret < 0)
615                 return ret;
616 
617         dev->avl.key = dev->ifname;
618 
619         ret = avl_insert(&devices, &dev->avl);
620         if (ret < 0)
621                 return ret;
622 
623         system_if_clear_state(dev);
624 
625         return 0;
626 }
627 
628 static struct device *
629 device_create_default(const char *name, bool external)
630 {
631         struct device *dev;
632 
633         if (!external && system_if_force_external(name))
634                 return NULL;
635 
636         D(DEVICE, "Create simple device '%s'\n", name);
637         dev = calloc(1, sizeof(*dev));
638         if (!dev)
639                 return NULL;
640 
641         dev->external = external;
642         dev->set_state = simple_device_set_state;
643 
644         if (device_init(dev, &simple_device_type, name) < 0) {
645                 device_cleanup(dev);
646                 free(dev);
647                 return NULL;
648         }
649 
650         dev->default_config = true;
651         if (external)
652                 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
653 
654         device_check_state(dev);
655 
656         return dev;
657 }
658 
659 struct device *
660 device_find(const char *name)
661 {
662         struct device *dev;
663 
664         return avl_find_element(&devices, name, dev, avl);
665 }
666 
667 struct device *
668 __device_get(const char *name, int create, bool check_vlan)
669 {
670         struct device *dev;
671 
672         dev = avl_find_element(&devices, name, dev, avl);
673 
674         if (!dev && check_vlan && strchr(name, '.'))
675                 return get_vlan_device_chain(name, create);
676 
677         if (name[0] == '@')
678                 return device_alias_get(name + 1);
679 
680         if (dev) {
681                 if (create > 1 && !dev->external) {
682                         system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
683                         dev->external = true;
684                         device_set_present(dev, true);
685                 }
686                 return dev;
687         }
688 
689         if (!create)
690                 return NULL;
691 
692         return device_create_default(name, create > 1);
693 }
694 
695 static void
696 device_delete(struct device *dev)
697 {
698         if (!dev->avl.key)
699                 return;
700 
701         D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
702         avl_delete(&devices, &dev->avl);
703         dev->avl.key = NULL;
704 }
705 
706 static int device_cleanup_cb(void *ctx, struct safe_list *list)
707 {
708         struct device_user *dep = container_of(list, struct device_user, list);
709         if (dep->cb)
710                 dep->cb(dep, DEV_EVENT_REMOVE);
711 
712         device_release(dep);
713         return 0;
714 }
715 
716 void device_cleanup(struct device *dev)
717 {
718         D(DEVICE, "Clean up device '%s'\n", dev->ifname);
719         safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
720         safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
721         device_delete(dev);
722 }
723 
724 static void __device_set_present(struct device *dev, bool state)
725 {
726         if (dev->present == state)
727                 return;
728 
729         dev->present = state;
730         device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
731 }
732 
733 void
734 device_refresh_present(struct device *dev)
735 {
736         bool state = dev->sys_present;
737 
738         if (dev->disabled || dev->deferred)
739                 state = false;
740 
741         __device_set_present(dev, state);
742 }
743 
744 void
745 device_set_auth_status(struct device *dev, bool value)
746 {
747         if (dev->auth_status == value)
748                 return;
749 
750         dev->auth_status = value;
751         if (!dev->present)
752                 return;
753 
754         if (dev->auth_status) {
755                 device_broadcast_event(dev, DEV_EVENT_AUTH_UP);
756                 return;
757         }
758 
759         device_broadcast_event(dev, DEV_EVENT_LINK_DOWN);
760         if (!dev->link_active)
761                 return;
762 
763         device_broadcast_event(dev, DEV_EVENT_LINK_UP);
764 }
765 
766 void device_set_present(struct device *dev, bool state)
767 {
768         if (dev->sys_present == state)
769                 return;
770 
771         D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
772         dev->sys_present = state;
773         device_refresh_present(dev);
774 }
775 
776 void device_set_link(struct device *dev, bool state)
777 {
778         if (dev->link_active == state)
779                 return;
780 
781         netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
782 
783         dev->link_active = state;
784         if (!state)
785                 dev->auth_status = false;
786         device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
787 }
788 
789 void device_set_ifindex(struct device *dev, int ifindex)
790 {
791         if (dev->ifindex == ifindex)
792                 return;
793 
794         dev->ifindex = ifindex;
795         device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
796 }
797 
798 int device_set_ifname(struct device *dev, const char *name)
799 {
800         int ret = 0;
801 
802         if (!strcmp(dev->ifname, name))
803                 return 0;
804 
805         if (strlen(name) > sizeof(dev->ifname) - 1)
806                 return -1;
807 
808         if (dev->avl.key)
809                 avl_delete(&devices, &dev->avl);
810 
811         strcpy(dev->ifname, name);
812 
813         if (dev->avl.key)
814                 ret = avl_insert(&devices, &dev->avl);
815 
816         if (ret == 0)
817                 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
818 
819         return ret;
820 }
821 
822 static int device_refcount(struct device *dev)
823 {
824         struct list_head *list;
825         int count = 0;
826 
827         list_for_each(list, &dev->users.list)
828                 count++;
829 
830         list_for_each(list, &dev->aliases.list)
831                 count++;
832 
833         return count;
834 }
835 
836 static void
837 __device_add_user(struct device_user *dep, struct device *dev)
838 {
839         struct safe_list *head;
840 
841         dep->dev = dev;
842 
843         if (dep->alias)
844                 head = &dev->aliases;
845         else
846                 head = &dev->users;
847 
848         safe_list_add(&dep->list, head);
849         D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
850 
851         if (dep->cb && dev->present) {
852                 dep->cb(dep, DEV_EVENT_ADD);
853                 if (dev->active)
854                         dep->cb(dep, DEV_EVENT_UP);
855 
856                 if (dev->link_active)
857                         dep->cb(dep, DEV_EVENT_LINK_UP);
858         }
859 }
860 
861 void device_add_user(struct device_user *dep, struct device *dev)
862 {
863         if (dep->dev == dev)
864                 return;
865 
866         if (dep->dev)
867                 device_remove_user(dep);
868 
869         if (!dev)
870                 return;
871 
872         __device_add_user(dep, dev);
873 }
874 
875 static void
876 device_free(struct device *dev)
877 {
878         __devlock++;
879         free(dev->config);
880         device_cleanup(dev);
881         dev->type->free(dev);
882         __devlock--;
883 }
884 
885 static void
886 __device_free_unused(struct uloop_timeout *timeout)
887 {
888         struct device *dev, *tmp;
889 
890         avl_for_each_element_safe(&devices, dev, avl, tmp) {
891                 if (!safe_list_empty(&dev->users) ||
892                         !safe_list_empty(&dev->aliases) ||
893                         dev->current_config)
894                         continue;
895 
896                 device_free(dev);
897         }
898 }
899 
900 void device_free_unused(void)
901 {
902         static struct uloop_timeout free_timer = {
903                 .cb = __device_free_unused,
904         };
905 
906         uloop_timeout_set(&free_timer, 1);
907 }
908 
909 void device_remove_user(struct device_user *dep)
910 {
911         struct device *dev = dep->dev;
912 
913         if (!dep->dev)
914                 return;
915 
916         dep->hotplug = false;
917         if (dep->claimed)
918                 device_release(dep);
919 
920         safe_list_del(&dep->list);
921         dep->dev = NULL;
922         D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
923         device_free_unused();
924 }
925 
926 void
927 device_init_pending(void)
928 {
929         struct device *dev, *tmp;
930 
931         avl_for_each_element_safe(&devices, dev, avl, tmp) {
932                 if (!dev->config_pending)
933                         continue;
934 
935                 dev->type->config_init(dev);
936                 dev->config_pending = false;
937                 device_check_state(dev);
938         }
939 }
940 
941 bool
942 device_check_ip6segmentrouting(void)
943 {
944         struct device *dev;
945         bool ip6segmentrouting = false;
946 
947         avl_for_each_element(&devices, dev, avl)
948                 ip6segmentrouting |= dev->settings.ip6segmentrouting;
949 
950         return ip6segmentrouting;
951 }
952 
953 static enum dev_change_type
954 device_set_config(struct device *dev, struct device_type *type,
955                   struct blob_attr *attr)
956 {
957         struct blob_attr *tb[__DEV_ATTR_MAX];
958         const struct uci_blob_param_list *cfg = type->config_params;
959 
960         if (type != dev->type)
961                 return DEV_CONFIG_RECREATE;
962 
963         if (dev->type->reload)
964                 return dev->type->reload(dev, attr);
965 
966         if (uci_blob_check_equal(dev->config, attr, cfg))
967                 return DEV_CONFIG_NO_CHANGE;
968 
969         if (cfg == &device_attr_list) {
970                 memset(tb, 0, sizeof(tb));
971 
972                 if (attr)
973                         blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
974                                 blob_data(attr), blob_len(attr));
975 
976                 device_init_settings(dev, tb);
977                 return DEV_CONFIG_RESTART;
978         } else
979                 return DEV_CONFIG_RECREATE;
980 }
981 
982 enum dev_change_type
983 device_apply_config(struct device *dev, struct device_type *type,
984                     struct blob_attr *config)
985 {
986         enum dev_change_type change;
987 
988         change = device_set_config(dev, type, config);
989         if (dev->external) {
990                 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
991                 change = DEV_CONFIG_APPLIED;
992         }
993 
994         switch (change) {
995                 case DEV_CONFIG_RESTART:
996                 case DEV_CONFIG_APPLIED:
997                         D(DEVICE, "Device '%s': config applied\n", dev->ifname);
998                         config = blob_memdup(config);
999                         free(dev->config);
1000                         dev->config = config;
1001                         if (change == DEV_CONFIG_RESTART && dev->present) {
1002                                 int ret = 0;
1003 
1004                                 device_set_present(dev, false);
1005                                 if (dev->active && !dev->external) {
1006                                         ret = dev->set_state(dev, false);
1007                                         if (!ret)
1008                                                 ret = dev->set_state(dev, true);
1009                                 }
1010                                 if (!ret)
1011                                         device_set_present(dev, true);
1012                         }
1013                         break;
1014                 case DEV_CONFIG_NO_CHANGE:
1015                         D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
1016                         break;
1017                 case DEV_CONFIG_RECREATE:
1018                         break;
1019         }
1020 
1021         return change;
1022 }
1023 
1024 static void
1025 device_replace(struct device *dev, struct device *odev)
1026 {
1027         struct device_user *dep, *tmp;
1028 
1029         __devlock++;
1030         if (odev->present)
1031                 device_set_present(odev, false);
1032 
1033         list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
1034                 device_release(dep);
1035                 safe_list_del(&dep->list);
1036                 __device_add_user(dep, dev);
1037         }
1038         __devlock--;
1039 
1040         device_free(odev);
1041 }
1042 
1043 void
1044 device_reset_config(void)
1045 {
1046         struct device *dev;
1047 
1048         avl_for_each_element(&devices, dev, avl)
1049                 dev->current_config = false;
1050 }
1051 
1052 void
1053 device_reset_old(void)
1054 {
1055         struct device *dev, *tmp, *ndev;
1056 
1057         avl_for_each_element_safe(&devices, dev, avl, tmp) {
1058                 if (dev->current_config || dev->default_config)
1059                         continue;
1060 
1061                 if (dev->type != &simple_device_type)
1062                         continue;
1063 
1064                 ndev = device_create_default(dev->ifname, dev->external);
1065                 if (!ndev)
1066                         continue;
1067 
1068                 device_replace(ndev, dev);
1069         }
1070 }
1071 
1072 struct device *
1073 device_create(const char *name, struct device_type *type,
1074               struct blob_attr *config)
1075 {
1076         struct device *odev = NULL, *dev;
1077         enum dev_change_type change;
1078 
1079         odev = device_find(name);
1080         if (odev) {
1081                 odev->current_config = true;
1082                 change = device_apply_config(odev, type, config);
1083                 switch (change) {
1084                 case DEV_CONFIG_RECREATE:
1085                         D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
1086                         device_delete(odev);
1087                         break;
1088                 default:
1089                         return odev;
1090                 }
1091         } else
1092                 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
1093 
1094         config = blob_memdup(config);
1095         if (!config)
1096                 return NULL;
1097 
1098         dev = type->create(name, type, config);
1099         if (!dev)
1100                 return NULL;
1101 
1102         dev->current_config = true;
1103         dev->config = config;
1104         if (odev)
1105                 device_replace(dev, odev);
1106 
1107         if (!config_init && dev->config_pending) {
1108                 type->config_init(dev);
1109                 dev->config_pending = false;
1110         }
1111 
1112         device_check_state(dev);
1113 
1114         return dev;
1115 }
1116 
1117 void
1118 device_dump_status(struct blob_buf *b, struct device *dev)
1119 {
1120         struct device_settings st;
1121         void *c, *s;
1122 
1123         if (!dev) {
1124                 avl_for_each_element(&devices, dev, avl) {
1125                         if (!dev->present)
1126                                 continue;
1127                         c = blobmsg_open_table(b, dev->ifname);
1128                         device_dump_status(b, dev);
1129                         blobmsg_close_table(b, c);
1130                 }
1131 
1132                 return;
1133         }
1134 
1135         blobmsg_add_u8(b, "external", dev->external);
1136         blobmsg_add_u8(b, "present", dev->present);
1137         blobmsg_add_string(b, "type", dev->type->name);
1138 
1139         if (!dev->present)
1140                 return;
1141 
1142         blobmsg_add_u8(b, "up", !!dev->active);
1143         blobmsg_add_u8(b, "carrier", !!dev->link_active);
1144         blobmsg_add_u8(b, "auth_status", !!dev->auth_status);
1145 
1146         if (dev->type->dump_info)
1147                 dev->type->dump_info(dev, b);
1148         else
1149                 system_if_dump_info(dev, b);
1150 
1151         if (dev->active) {
1152                 device_merge_settings(dev, &st);
1153                 if (st.flags & DEV_OPT_MTU)
1154                         blobmsg_add_u32(b, "mtu", st.mtu);
1155                 if (st.flags & DEV_OPT_MTU6)
1156                         blobmsg_add_u32(b, "mtu6", st.mtu6);
1157                 if (st.flags & DEV_OPT_MACADDR)
1158                         blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
1159                 if (st.flags & DEV_OPT_TXQUEUELEN)
1160                         blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
1161                 if (st.flags & DEV_OPT_IPV6)
1162                         blobmsg_add_u8(b, "ipv6", st.ipv6);
1163                 if (st.flags & DEV_OPT_IP6SEGMENTROUTING)
1164                         blobmsg_add_u8(b, "ip6segmentrouting", st.ip6segmentrouting);
1165                 if (st.flags & DEV_OPT_PROMISC)
1166                         blobmsg_add_u8(b, "promisc", st.promisc);
1167                 if (st.flags & DEV_OPT_RPFILTER)
1168                         blobmsg_add_u32(b, "rpfilter", st.rpfilter);
1169                 if (st.flags & DEV_OPT_ACCEPTLOCAL)
1170                         blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
1171                 if (st.flags & DEV_OPT_IGMPVERSION)
1172                         blobmsg_add_u32(b, "igmpversion", st.igmpversion);
1173                 if (st.flags & DEV_OPT_MLDVERSION)
1174                         blobmsg_add_u32(b, "mldversion", st.mldversion);
1175                 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
1176                         blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
1177                         blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
1178                 }
1179                 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
1180                         blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
1181                         blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
1182                 }
1183                 if (st.flags & DEV_OPT_NEIGHLOCKTIME)
1184                         blobmsg_add_u32(b, "neigh4locktime", st.neigh4locktime);
1185                 if (st.flags & DEV_OPT_DADTRANSMITS)
1186                         blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
1187                 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
1188                         blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
1189                 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
1190                         blobmsg_add_u32(b, "multicast_router", st.multicast_router);
1191                 if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
1192                         blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
1193                 if (st.flags & DEV_OPT_MULTICAST)
1194                         blobmsg_add_u8(b, "multicast", st.multicast);
1195                 if (st.flags & DEV_OPT_LEARNING)
1196                         blobmsg_add_u8(b, "learning", st.learning);
1197                 if (st.flags & DEV_OPT_UNICAST_FLOOD)
1198                         blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
1199                 if (st.flags & DEV_OPT_SENDREDIRECTS)
1200                         blobmsg_add_u8(b, "sendredirects", st.sendredirects);
1201                 if (st.flags & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
1202                         blobmsg_add_u8(b, "drop_v4_unicast_in_l2_multicast", st.drop_v4_unicast_in_l2_multicast);
1203                 if (st.flags & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
1204                         blobmsg_add_u8(b, "drop_v6_unicast_in_l2_multicast", st.drop_v6_unicast_in_l2_multicast);
1205                 if (st.flags & DEV_OPT_DROP_GRATUITOUS_ARP)
1206                         blobmsg_add_u8(b, "drop_gratuitous_arp", st.drop_gratuitous_arp);
1207                 if (st.flags & DEV_OPT_DROP_UNSOLICITED_NA)
1208                         blobmsg_add_u8(b, "drop_unsolicited_na", st.drop_unsolicited_na);
1209                 if (st.flags & DEV_OPT_ARP_ACCEPT)
1210                         blobmsg_add_u8(b, "arp_accept", st.arp_accept);
1211                 if (st.flags & DEV_OPT_AUTH)
1212                         blobmsg_add_u8(b, "auth", st.auth);
1213         }
1214 
1215         s = blobmsg_open_table(b, "statistics");
1216         if (dev->type->dump_stats)
1217                 dev->type->dump_stats(dev, b);
1218         else
1219                 system_if_dump_stats(dev, b);
1220         blobmsg_close_table(b, s);
1221 }
1222 
1223 static void __init simple_device_type_init(void)
1224 {
1225         device_type_add(&simple_device_type);
1226 }
1227 
1228 void device_hotplug_event(const char *name, bool add)
1229 {
1230         struct device *dev;
1231 
1232         wireless_device_hotplug_event(name, add);
1233 
1234         dev = device_find(name);
1235         if (!dev || dev->type != &simple_device_type)
1236                 return;
1237 
1238         device_set_present(dev, add);
1239 }
1240 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt