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

Sources/netifd/device.h

  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 #ifndef __NETIFD_DEVICE_H
 15 #define __NETIFD_DEVICE_H
 16 
 17 #include <libubox/avl.h>
 18 #include <libubox/safe_list.h>
 19 #include <libubox/kvlist.h>
 20 #include <netinet/in.h>
 21 
 22 struct device;
 23 struct device_type;
 24 struct device_user;
 25 struct device_hotplug_ops;
 26 struct bridge_vlan;
 27 struct interface;
 28 
 29 typedef int (*device_state_cb)(struct device *, bool up);
 30 
 31 enum {
 32         /*
 33          * Live-applicable: pure per-netdev sysctls. A diff limited to these
 34          * can be pushed via system_if_apply_settings() without tearing the
 35          * device down. Keep this group first so DEV_OPT_LIVE_APPLY_MASK
 36          * stays a contiguous low-bit mask.
 37          */
 38         DEV_ATTR_IPV6,
 39         DEV_ATTR_IP6SEGMENTROUTING,
 40         DEV_ATTR_RPFILTER,
 41         DEV_ATTR_ACCEPTLOCAL,
 42         DEV_ATTR_IGMPVERSION,
 43         DEV_ATTR_MLDVERSION,
 44         DEV_ATTR_NEIGHREACHABLETIME,
 45         DEV_ATTR_NEIGHGCSTALETIME,
 46         DEV_ATTR_NEIGHLOCKTIME,
 47         DEV_ATTR_DADTRANSMITS,
 48         DEV_ATTR_SENDREDIRECTS,
 49         DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST,
 50         DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST,
 51         DEV_ATTR_DROP_GRATUITOUS_ARP,
 52         DEV_ATTR_DROP_UNSOLICITED_NA,
 53         DEV_ATTR_ARP_ACCEPT,
 54         DEV_ATTR_MTU6,
 55         DEV_ATTR_TXQUEUELEN,
 56         DEV_ATTR_PROMISC,
 57         DEV_ATTR_MULTICAST,
 58         __DEV_ATTR_LIVE_APPLY_MAX,
 59 
 60         /* Have a DEV_OPT_* flag but require teardown to re-apply. */
 61         DEV_ATTR_MTU = __DEV_ATTR_LIVE_APPLY_MAX,
 62         DEV_ATTR_MACADDR,
 63         DEV_ATTR_MULTICAST_TO_UNICAST,
 64         DEV_ATTR_MULTICAST_ROUTER,
 65         DEV_ATTR_MULTICAST_FAST_LEAVE,
 66         DEV_ATTR_LEARNING,
 67         DEV_ATTR_UNICAST_FLOOD,
 68         DEV_ATTR_BROADCAST_FLOOD,
 69         DEV_ATTR_ISOLATE,
 70         DEV_ATTR_AUTH,
 71         DEV_ATTR_SPEED,
 72         DEV_ATTR_DUPLEX,
 73         DEV_ATTR_PAUSE,
 74         DEV_ATTR_ASYM_PAUSE,
 75         DEV_ATTR_RXPAUSE,
 76         DEV_ATTR_TXPAUSE,
 77         DEV_ATTR_AUTONEG,
 78         DEV_ATTR_GRO,
 79         DEV_ATTR_MASTER,
 80         DEV_ATTR_EEE,
 81         DEV_ATTR_PSE,
 82         DEV_ATTR_PSE_PODL,
 83         DEV_ATTR_PSE_POWER_LIMIT,
 84         DEV_ATTR_PSE_PRIORITY,
 85 
 86         /* No DEV_OPT_* counterpart; carry data consumed outside flags. */
 87         DEV_ATTR_TYPE,
 88         DEV_ATTR_ENABLED,
 89         DEV_ATTR_VLAN,
 90         DEV_ATTR_AUTH_VLAN,
 91         DEV_ATTR_TAGS,
 92 
 93         __DEV_ATTR_MAX,
 94 };
 95 
 96 enum dev_change_type {
 97         DEV_CONFIG_NO_CHANGE,
 98         DEV_CONFIG_APPLIED,
 99         DEV_CONFIG_RESTART,
100         DEV_CONFIG_RECREATE,
101 };
102 
103 struct device_type {
104         struct list_head list;
105         const char *name;
106 
107         bool bridge_capability;
108         const char *name_prefix;
109 
110         const struct uci_blob_param_list *config_params;
111 
112         struct device *(*create)(const char *name, struct device_type *devtype,
113                 struct blob_attr *attr);
114         void (*config_init)(struct device *);
115         enum dev_change_type (*reload)(struct device *, struct blob_attr *,
116                                        struct blob_attr **tb_dev);
117         void (*vlan_update)(struct device *);
118         void (*dump_info)(struct device *, struct blob_buf *buf);
119         void (*dump_stats)(struct device *, struct blob_buf *buf);
120         int (*check_state)(struct device *);
121         void (*stp_init)(struct device *);
122         void (*free)(struct device *);
123 };
124 
125 /*
126  * DEV_OPT_X is the bit in device_settings.flags that marks attribute X
127  * as configured. Aligned with DEV_ATTR_X so the diff bitmap produced
128  * by uci_blob_diff(&device_attr_list, ...) is directly usable as a
129  * DEV_OPT_* mask. DEV_OPT_DEFAULT_MACADDR has no DEV_ATTR_* peer (it
130  * is synthesised by system_if_get_settings()) and is parked above the
131  * attr range.
132  */
133 enum {
134         DEV_OPT_IPV6                            = 1ULL << DEV_ATTR_IPV6,
135         DEV_OPT_IP6SEGMENTROUTING               = 1ULL << DEV_ATTR_IP6SEGMENTROUTING,
136         DEV_OPT_RPFILTER                        = 1ULL << DEV_ATTR_RPFILTER,
137         DEV_OPT_ACCEPTLOCAL                     = 1ULL << DEV_ATTR_ACCEPTLOCAL,
138         DEV_OPT_IGMPVERSION                     = 1ULL << DEV_ATTR_IGMPVERSION,
139         DEV_OPT_MLDVERSION                      = 1ULL << DEV_ATTR_MLDVERSION,
140         DEV_OPT_NEIGHREACHABLETIME              = 1ULL << DEV_ATTR_NEIGHREACHABLETIME,
141         DEV_OPT_NEIGHGCSTALETIME                = 1ULL << DEV_ATTR_NEIGHGCSTALETIME,
142         DEV_OPT_NEIGHLOCKTIME                   = 1ULL << DEV_ATTR_NEIGHLOCKTIME,
143         DEV_OPT_DADTRANSMITS                    = 1ULL << DEV_ATTR_DADTRANSMITS,
144         DEV_OPT_SENDREDIRECTS                   = 1ULL << DEV_ATTR_SENDREDIRECTS,
145         DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST = 1ULL << DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST,
146         DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST = 1ULL << DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST,
147         DEV_OPT_DROP_GRATUITOUS_ARP             = 1ULL << DEV_ATTR_DROP_GRATUITOUS_ARP,
148         DEV_OPT_DROP_UNSOLICITED_NA             = 1ULL << DEV_ATTR_DROP_UNSOLICITED_NA,
149         DEV_OPT_ARP_ACCEPT                      = 1ULL << DEV_ATTR_ARP_ACCEPT,
150         DEV_OPT_MTU6                            = 1ULL << DEV_ATTR_MTU6,
151         DEV_OPT_TXQUEUELEN                      = 1ULL << DEV_ATTR_TXQUEUELEN,
152         DEV_OPT_PROMISC                         = 1ULL << DEV_ATTR_PROMISC,
153         DEV_OPT_MULTICAST                       = 1ULL << DEV_ATTR_MULTICAST,
154 
155         DEV_OPT_MTU                             = 1ULL << DEV_ATTR_MTU,
156         DEV_OPT_MACADDR                         = 1ULL << DEV_ATTR_MACADDR,
157         DEV_OPT_MULTICAST_TO_UNICAST            = 1ULL << DEV_ATTR_MULTICAST_TO_UNICAST,
158         DEV_OPT_MULTICAST_ROUTER                = 1ULL << DEV_ATTR_MULTICAST_ROUTER,
159         DEV_OPT_MULTICAST_FAST_LEAVE            = 1ULL << DEV_ATTR_MULTICAST_FAST_LEAVE,
160         DEV_OPT_LEARNING                        = 1ULL << DEV_ATTR_LEARNING,
161         DEV_OPT_UNICAST_FLOOD                   = 1ULL << DEV_ATTR_UNICAST_FLOOD,
162         DEV_OPT_BROADCAST_FLOOD                 = 1ULL << DEV_ATTR_BROADCAST_FLOOD,
163         DEV_OPT_ISOLATE                         = 1ULL << DEV_ATTR_ISOLATE,
164         DEV_OPT_AUTH                            = 1ULL << DEV_ATTR_AUTH,
165         DEV_OPT_SPEED                           = 1ULL << DEV_ATTR_SPEED,
166         DEV_OPT_DUPLEX                          = 1ULL << DEV_ATTR_DUPLEX,
167         DEV_OPT_PAUSE                           = 1ULL << DEV_ATTR_PAUSE,
168         DEV_OPT_ASYM_PAUSE                      = 1ULL << DEV_ATTR_ASYM_PAUSE,
169         DEV_OPT_RXPAUSE                         = 1ULL << DEV_ATTR_RXPAUSE,
170         DEV_OPT_TXPAUSE                         = 1ULL << DEV_ATTR_TXPAUSE,
171         DEV_OPT_AUTONEG                         = 1ULL << DEV_ATTR_AUTONEG,
172         DEV_OPT_GRO                             = 1ULL << DEV_ATTR_GRO,
173         DEV_OPT_MASTER                          = 1ULL << DEV_ATTR_MASTER,
174         DEV_OPT_EEE                             = 1ULL << DEV_ATTR_EEE,
175         DEV_OPT_PSE                             = 1ULL << DEV_ATTR_PSE,
176         DEV_OPT_PSE_PODL                        = 1ULL << DEV_ATTR_PSE_PODL,
177         DEV_OPT_PSE_POWER_LIMIT                 = 1ULL << DEV_ATTR_PSE_POWER_LIMIT,
178         DEV_OPT_PSE_PRIORITY                    = 1ULL << DEV_ATTR_PSE_PRIORITY,
179 
180         DEV_OPT_DEFAULT_MACADDR                 = 1ULL << 63,
181 };
182 
183 #define DEV_OPT_LIVE_APPLY_MASK \
184         (((uint64_t)1 << __DEV_ATTR_LIVE_APPLY_MAX) - 1)
185 
186 /* events broadcasted to all users of a device */
187 enum device_event {
188         DEV_EVENT_ADD,
189         DEV_EVENT_REMOVE,
190 
191         DEV_EVENT_UPDATE_IFNAME,
192         DEV_EVENT_UPDATE_IFINDEX,
193 
194         DEV_EVENT_SETUP,
195         DEV_EVENT_TEARDOWN,
196         DEV_EVENT_UP,
197         DEV_EVENT_DOWN,
198 
199         DEV_EVENT_AUTH_UP,
200         DEV_EVENT_LINK_UP,
201         DEV_EVENT_LINK_DOWN,
202 
203         /* Topology changed (i.e. bridge member added) */
204         DEV_EVENT_TOPO_CHANGE,
205 
206         /* the vlan list of the device was updated */
207         DEV_EVENT_VLAN_UPDATE,
208 
209         __DEV_EVENT_MAX
210 };
211 
212 /*
213  * device dependency with callbacks
214  */
215 struct device_user {
216         struct safe_list list;
217 
218         bool claimed;
219         bool hotplug;
220         bool alias;
221 
222         uint8_t ev_idx[__DEV_EVENT_MAX];
223 
224         struct device *dev;
225         void (*cb)(struct device_user *, enum device_event);
226 };
227 
228 struct device_settings {
229         uint64_t flags;
230         uint64_t valid_flags;
231         unsigned int mtu;
232         unsigned int mtu6;
233         unsigned int txqueuelen;
234         uint8_t macaddr[6];
235         bool ipv6;
236         bool promisc;
237         unsigned int rpfilter;
238         bool acceptlocal;
239         unsigned int igmpversion;
240         unsigned int mldversion;
241         unsigned int neigh4reachabletime;
242         unsigned int neigh6reachabletime;
243         unsigned int neigh4gcstaletime;
244         unsigned int neigh6gcstaletime;
245         int neigh4locktime;
246         unsigned int dadtransmits;
247         bool multicast_to_unicast;
248         unsigned int multicast_router;
249         bool multicast_fast_leave;
250         bool multicast;
251         bool learning;
252         bool unicast_flood;
253         bool broadcast_flood;
254         bool sendredirects;
255         bool ip6segmentrouting;
256         bool isolate;
257         bool drop_v4_unicast_in_l2_multicast;
258         bool drop_v6_unicast_in_l2_multicast;
259         bool drop_gratuitous_arp;
260         bool drop_unsolicited_na;
261         bool arp_accept;
262         bool auth;
263         unsigned int speed;
264         bool duplex;
265         bool pause;
266         bool asym_pause;
267         bool rxpause;
268         bool txpause;
269         bool autoneg;
270         bool gro;
271         int master_ifindex;
272         bool eee;
273         bool pse;
274         bool pse_podl;
275         unsigned int pse_power_limit;
276         unsigned int pse_priority;
277 };
278 
279 struct device_vlan_range {
280         uint16_t start, end;
281 };
282 
283 /*
284  * link layer device. typically represents a linux network device.
285  * can be used to support VLANs as well
286  */
287 struct device {
288         struct device_type *type;
289 
290         struct avl_node avl;
291         struct safe_list users;
292         struct safe_list aliases;
293 
294         struct vlist_tree vlans;
295         struct kvlist vlan_aliases;
296         struct blob_attr *config_auth_vlans;
297         struct blob_attr *auth_vlans;
298         struct blob_attr *tags;
299 
300         char ifname[IFNAMSIZ];
301         int ifindex;
302 
303         struct blob_attr *config;
304         bool config_pending;
305         bool sys_present;
306         /* DEV_EVENT_ADD */
307         bool present;
308         /* DEV_EVENT_UP */
309         int active;
310         /* DEV_EVENT_LINK_UP */
311         bool link_active;
312         bool auth_status;
313 
314         bool external;
315         bool disabled;
316         bool deferred;
317         bool hidden;
318 
319         bool current_config;
320         bool iface_config;
321         bool default_config;
322         bool wireless;
323         bool wireless_ap;
324         bool wireless_proxyarp;
325         bool wireless_isolate;
326         bool bpdu_filter;
327 
328         struct interface *config_iface;
329         struct device_vlan_range *extra_vlan;
330         int n_extra_vlan;
331 
332         /* set interface up or down */
333         device_state_cb set_state;
334 
335         const struct device_hotplug_ops *hotplug_ops;
336         struct blob_attr *hotplug_link;
337 
338         struct device_user parent;
339 
340         struct device_settings orig_settings;
341         struct device_settings settings;
342 };
343 
344 struct device_hotplug_ops {
345         int (*prepare)(struct device *dev, struct device **bridge_dev);
346         int (*add)(struct device *main, struct device *member, struct blob_attr *vlan);
347         int (*del)(struct device *main, struct device *member, struct blob_attr *vlan);
348 };
349 
350 enum bridge_vlan_flags {
351         BRVLAN_F_SELF =         (1 << 0),
352         BRVLAN_F_PVID =         (1 << 1),
353         BRVLAN_F_UNTAGGED =     (1 << 2),
354 };
355 
356 struct bridge_vlan_port {
357         const char *ifname;
358         uint16_t flags;
359         int8_t check;
360 };
361 
362 struct bridge_vlan_hotplug_port {
363         struct list_head list;
364         struct bridge_vlan_port port;
365 };
366 
367 struct bridge_vlan {
368         struct vlist_node node;
369 
370         struct bridge_vlan_port *ports;
371         int n_ports;
372 
373         struct list_head hotplug_ports;
374 
375         uint16_t vid;
376         bool local;
377         bool pending;
378 };
379 
380 extern const struct uci_blob_param_list device_attr_list;
381 extern struct device_type simple_device_type;
382 extern struct device_type tunnel_device_type;
383 
384 void device_vlan_update(bool done);
385 void device_stp_init(void);
386 void device_type_for_each(const struct device_type *type,
387                           void (*cb)(struct device *dev));
388 
389 int device_type_add(struct device_type *devtype);
390 struct device_type *device_type_get(const char *tname);
391 struct device *device_create(const char *name, struct device_type *type,
392                              struct blob_attr *config);
393 void device_merge_settings(struct device *dev, struct device_settings *n);
394 void device_init_settings(struct device *dev, struct blob_attr **tb);
395 /*
396  * Parse device-attr blob into tb_dev, seed dev->settings, and invoke the
397  * type-specific reload hook for initial configuration during *_create().
398  */
399 void device_init_config(struct device *dev, struct blob_attr *attr);
400 void device_init_pending(void);
401 
402 /*
403  * Inspect a DEV_ATTR_* diff bitmap produced by uci_blob_diff against
404  * &device_attr_list. If every set bit falls into the live-applicable
405  * group, return true and set *apply_mask to the corresponding DEV_OPT_*
406  * bits. Return false with *apply_mask = 0 if any other bit is set.
407  */
408 bool device_diff_live_apply(unsigned long *diff, uint64_t *apply_mask);
409 
410 /*
411  * Push dev->settings values for the attributes covered by apply_mask to
412  * the kernel via system_if_apply_settings(), restoring originals for
413  * attributes that have been dropped from UCI. Caller must guarantee
414  * dev->active.
415  */
416 void device_apply_live_settings(struct device *dev, uint64_t apply_mask);
417 
418 enum dev_change_type
419 device_apply_config(struct device *dev, struct device_type *type,
420                     struct blob_attr *config);
421 
422 void device_reset_config(void);
423 void device_reset_old(void);
424 
425 int device_init_virtual(struct device *dev, struct device_type *type, const char *name);
426 int device_init(struct device *dev, struct device_type *type, const char *ifname);
427 void device_cleanup(struct device *dev);
428 struct device *device_find(const char *name);
429 
430 struct device *__device_get(const char *name, int create, bool check_vlan);
431 static inline struct device *device_get(const char *name, int create)
432 {
433         return __device_get(name, create, true);
434 }
435 
436 void device_add_user(struct device_user *dep, struct device *dev);
437 void device_remove_user(struct device_user *dep);
438 const char *device_event_name(enum device_event ev);
439 void __device_broadcast_event(struct device *dev, enum device_event ev);
440 #define device_broadcast_event(dev, ev) do {                                    \
441         struct device *__ev_dev = (dev);                                        \
442         D(DEVICE, "%s: event (%s)",                                             \
443           (__ev_dev && __ev_dev->ifname[0] ? __ev_dev->ifname : "(none)"),      \
444           device_event_name(ev));                                               \
445         __device_broadcast_event(__ev_dev, ev);                                 \
446 } while (0)
447 
448 void _device_set_present(struct device *dev, bool state);
449 #define device_set_present(dev, state) do {                                     \
450         struct device *__ev_dev = (dev);                                        \
451         bool __ev_state = state;                                                \
452         D(DEVICE, "%s: set present=%d",                                         \
453           (__ev_dev && __ev_dev->ifname[0] ? __ev_dev->ifname : "(none)"),      \
454           __ev_state);                                                          \
455         _device_set_present(__ev_dev, __ev_state);                              \
456 } while (0)
457 
458 void device_set_link(struct device *dev, bool state);
459 void device_set_ifindex(struct device *dev, int ifindex);
460 int device_set_ifname(struct device *dev, const char *name);
461 void device_refresh_present(struct device *dev);
462 int device_claim(struct device_user *dep);
463 void device_release(struct device_user *dep);
464 int device_check_state(struct device *dev);
465 void device_dump_status(struct blob_buf *b, struct device *dev);
466 
467 void device_free_unused(void);
468 
469 struct device *get_vlan_device_chain(const char *ifname, int create);
470 void alias_notify_device(const char *name, struct device *dev);
471 struct device *device_alias_get(const char *name);
472 
473 void device_set_auth_status(struct device *dev, bool value, struct blob_attr *vlans);
474 
475 static inline void
476 device_set_deferred(struct device *dev, bool value)
477 {
478         dev->deferred = value;
479         device_refresh_present(dev);
480 }
481 
482 static inline void
483 device_set_disabled(struct device *dev, bool value)
484 {
485         dev->disabled = value;
486         device_refresh_present(dev);
487 }
488 
489 static inline bool
490 device_link_active(struct device *dev)
491 {
492         if (dev->settings.auth && !dev->auth_status)
493                 return false;
494 
495         return dev->link_active;
496 }
497 
498 bool device_check_ip6segmentrouting(void);
499 void device_hotplug_event(const char *name, bool add);
500 
501 #endif
502 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt