fix a logic error leading to segfaults on updates
[project/netifd.git] / device.h
1 #ifndef __LL_H
2 #define __LL_H
3
4 #include <libubox/avl.h>
5 #include <netinet/in.h>
6
7 struct device;
8 struct device_hotplug_ops;
9
10 typedef int (*device_state_cb)(struct device *, bool up);
11
12 enum {
13         DEV_ATTR_TYPE,
14         DEV_ATTR_IFNAME,
15         DEV_ATTR_MTU,
16         DEV_ATTR_MACADDR,
17         DEV_ATTR_TXQUEUELEN,
18         __DEV_ATTR_MAX,
19 };
20
21 enum dev_change_type {
22         DEV_CONFIG_NO_CHANGE,
23         DEV_CONFIG_APPLIED,
24         DEV_CONFIG_RECREATE,
25 };
26
27 struct device_type {
28         struct list_head list;
29         const char *name;
30
31         const struct config_param_list *config_params;
32
33         struct device *(*create)(const char *name, struct blob_attr *attr);
34         void (*config_init)(struct device *);
35         enum dev_change_type (*reload)(struct device *, struct blob_attr *);
36         void (*dump_info)(struct device *, struct blob_buf *buf);
37         void (*dump_stats)(struct device *, struct blob_buf *buf);
38         int (*check_state)(struct device *);
39         void (*free)(struct device *);
40 };
41
42 enum {
43         DEV_OPT_MTU             = (1 << 0),
44         DEV_OPT_MACADDR         = (1 << 1),
45         DEV_OPT_TXQUEUELEN      = (1 << 2)
46 };
47
48 /* 
49  * link layer device. typically represents a linux network device.
50  * can be used to support VLANs as well
51  */
52 struct device {
53         const struct device_type *type;
54
55         struct avl_node avl;
56         struct list_head users;
57
58         char ifname[IFNAMSIZ + 1];
59         int ifindex;
60
61         struct blob_attr *config;
62         bool config_pending;
63         bool present;
64         int active;
65         bool external;
66
67         bool current_config;
68         bool default_config;
69
70         /* set interface up or down */
71         device_state_cb set_state;
72
73         const struct device_hotplug_ops *hotplug_ops;
74
75         /* settings */
76         unsigned int flags;
77
78         unsigned int mtu;
79         unsigned int txqueuelen;
80         uint8_t macaddr[6];
81 };
82
83 /* events broadcasted to all users of a device */
84 enum device_event {
85         DEV_EVENT_ADD,
86         DEV_EVENT_REMOVE,
87
88         DEV_EVENT_SETUP,
89         DEV_EVENT_TEARDOWN,
90         DEV_EVENT_UP,
91         DEV_EVENT_DOWN,
92
93         DEV_EVENT_LINK_UP,
94         DEV_EVENT_LINK_DOWN,
95 };
96
97 /*
98  * device dependency with callbacks
99  */
100 struct device_user {
101         struct list_head list;
102
103         bool claimed;
104         bool hotplug;
105
106         struct device *dev;
107         void (*cb)(struct device_user *, enum device_event);
108 };
109
110 struct device_hotplug_ops {
111         int (*add)(struct device *main, struct device *member);
112         int (*del)(struct device *main, struct device *member);
113 };
114
115 extern const struct config_param_list device_attr_list;
116 extern const struct device_type simple_device_type;
117 extern const struct device_type bridge_device_type;
118
119 void device_lock(void);
120 void device_unlock(void);
121
122 struct device *device_create(const char *name, const struct device_type *type,
123                              struct blob_attr *config);
124 void device_init_settings(struct device *dev, struct blob_attr **tb);
125 void device_init_pending(void);
126
127 enum dev_change_type
128 device_set_config(struct device *dev, const struct device_type *type,
129                   struct blob_attr *attr);
130
131 void device_reset_config(void);
132 void device_reset_old(void);
133
134 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
135 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
136 void device_cleanup(struct device *iface);
137 struct device *device_get(const char *name, int create);
138 void device_add_user(struct device_user *dep, struct device *iface);
139 void device_remove_user(struct device_user *dep);
140
141 void device_set_present(struct device *dev, bool state);
142 int device_claim(struct device_user *dep);
143 void device_release(struct device_user *dep);
144 int device_check_state(struct device *dev);
145 void device_dump_status(struct blob_buf *b, struct device *dev);
146
147 static inline void
148 device_free(struct device *dev)
149 {
150         dev->type->free(dev);
151 }
152
153 void device_free_unused(struct device *dev);
154
155 struct device *get_vlan_device_chain(const char *ifname, bool create);
156 void alias_notify_device(const char *name, struct device *dev);
157
158 #endif