netifd: Fix possible hotplug race conditions
[project/netifd.git] / 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 <netinet/in.h>
20
21 struct device;
22 struct device_user;
23 struct device_hotplug_ops;
24
25 typedef int (*device_state_cb)(struct device *, bool up);
26
27 enum {
28         DEV_ATTR_TYPE,
29         DEV_ATTR_IFNAME,
30         DEV_ATTR_MTU,
31         DEV_ATTR_MACADDR,
32         DEV_ATTR_TXQUEUELEN,
33         DEV_ATTR_ENABLED,
34         DEV_ATTR_IPV6,
35         DEV_ATTR_PROMISC,
36         DEV_ATTR_RPFILTER,
37         DEV_ATTR_ACCEPTLOCAL,
38         DEV_ATTR_IGMPVERSION,
39         DEV_ATTR_MLDVERSION,
40         DEV_ATTR_NEIGHREACHABLETIME,
41         __DEV_ATTR_MAX,
42 };
43
44 enum dev_change_type {
45         DEV_CONFIG_NO_CHANGE,
46         DEV_CONFIG_APPLIED,
47         DEV_CONFIG_RESTART,
48         DEV_CONFIG_RECREATE,
49 };
50
51 struct device_type {
52         struct list_head list;
53         const char *name;
54
55         bool keep_link_status;
56
57         const struct uci_blob_param_list *config_params;
58
59         struct device *(*create)(const char *name, struct blob_attr *attr);
60         void (*config_init)(struct device *);
61         enum dev_change_type (*reload)(struct device *, struct blob_attr *);
62         void (*dump_info)(struct device *, struct blob_buf *buf);
63         void (*dump_stats)(struct device *, struct blob_buf *buf);
64         int (*check_state)(struct device *);
65         void (*free)(struct device *);
66 };
67
68 enum {
69         DEV_OPT_MTU                     = (1 << 0),
70         DEV_OPT_MACADDR                 = (1 << 1),
71         DEV_OPT_TXQUEUELEN              = (1 << 2),
72         DEV_OPT_IPV6                    = (1 << 3),
73         DEV_OPT_PROMISC                 = (1 << 4),
74         DEV_OPT_RPFILTER                = (1 << 5),
75         DEV_OPT_ACCEPTLOCAL             = (1 << 6),
76         DEV_OPT_IGMPVERSION             = (1 << 7),
77         DEV_OPT_MLDVERSION              = (1 << 8),
78         DEV_OPT_NEIGHREACHABLETIME      = (1 << 9),
79 };
80
81 /* events broadcasted to all users of a device */
82 enum device_event {
83         DEV_EVENT_ADD,
84         DEV_EVENT_REMOVE,
85
86         DEV_EVENT_UPDATE_IFNAME,
87         DEV_EVENT_UPDATE_IFINDEX,
88
89         DEV_EVENT_SETUP,
90         DEV_EVENT_TEARDOWN,
91         DEV_EVENT_UP,
92         DEV_EVENT_DOWN,
93
94         DEV_EVENT_LINK_UP,
95         DEV_EVENT_LINK_DOWN,
96
97         /* Topology changed (i.e. bridge member added) */
98         DEV_EVENT_TOPO_CHANGE,
99
100         __DEV_EVENT_MAX
101 };
102
103 /*
104  * device dependency with callbacks
105  */
106 struct device_user {
107         struct safe_list list;
108
109         bool claimed;
110         bool hotplug;
111         bool alias;
112
113         uint8_t ev_idx[__DEV_EVENT_MAX];
114
115         struct device *dev;
116         void (*cb)(struct device_user *, enum device_event);
117 };
118
119 struct device_settings {
120         unsigned int flags;
121         unsigned int mtu;
122         unsigned int txqueuelen;
123         uint8_t macaddr[6];
124         bool ipv6;
125         bool promisc;
126         unsigned int rpfilter;
127         bool acceptlocal;
128         unsigned int igmpversion;
129         unsigned int mldversion;
130         unsigned int neigh4reachabletime;
131         unsigned int neigh6reachabletime;
132 };
133
134 /*
135  * link layer device. typically represents a linux network device.
136  * can be used to support VLANs as well
137  */
138 struct device {
139         const struct device_type *type;
140
141         struct avl_node avl;
142         struct safe_list users;
143         struct safe_list aliases;
144
145         char ifname[IFNAMSIZ + 1];
146         int ifindex;
147
148         struct blob_attr *config;
149         bool config_pending;
150         bool sys_present;
151         bool present;
152         int active;
153         bool link_active;
154
155         bool external;
156         bool disabled;
157         bool deferred;
158         bool hidden;
159
160         bool current_config;
161         bool default_config;
162         bool wireless;
163
164         /* set interface up or down */
165         device_state_cb set_state;
166
167         const struct device_hotplug_ops *hotplug_ops;
168
169         struct device_user parent;
170
171         struct device_settings orig_settings;
172         struct device_settings settings;
173 };
174
175 struct device_hotplug_ops {
176         int (*prepare)(struct device *dev);
177         int (*add)(struct device *main, struct device *member);
178         int (*del)(struct device *main, struct device *member);
179 };
180
181 extern const struct uci_blob_param_list device_attr_list;
182 extern const struct device_type simple_device_type;
183 extern const struct device_type bridge_device_type;
184 extern const struct device_type tunnel_device_type;
185 extern const struct device_type macvlan_device_type;
186 extern const struct device_type vlandev_device_type;
187
188 void device_lock(void);
189 void device_unlock(void);
190
191 struct device *device_create(const char *name, const struct device_type *type,
192                              struct blob_attr *config);
193 void device_init_settings(struct device *dev, struct blob_attr **tb);
194 void device_init_pending(void);
195
196 enum dev_change_type
197 device_set_config(struct device *dev, const struct device_type *type,
198                   struct blob_attr *attr);
199 enum dev_change_type
200 device_apply_config(struct device *dev, const struct device_type *type,
201                     struct blob_attr *config);
202
203 void device_reset_config(void);
204 void device_reset_old(void);
205
206 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);
207 int device_init(struct device *iface, const struct device_type *type, const char *ifname);
208 void device_cleanup(struct device *iface);
209 struct device *device_get(const char *name, int create);
210 void device_add_user(struct device_user *dep, struct device *iface);
211 void device_remove_user(struct device_user *dep);
212 void device_broadcast_event(struct device *dev, enum device_event ev);
213
214 void device_set_present(struct device *dev, bool state);
215 void device_set_link(struct device *dev, bool state);
216 void device_set_ifindex(struct device *dev, int ifindex);
217 void device_refresh_present(struct device *dev);
218 int device_claim(struct device_user *dep);
219 void device_release(struct device_user *dep);
220 int device_check_state(struct device *dev);
221 void device_dump_status(struct blob_buf *b, struct device *dev);
222
223 void device_free(struct device *dev);
224 void device_free_unused(struct device *dev);
225
226 struct device *get_vlan_device_chain(const char *ifname, bool create);
227 void alias_notify_device(const char *name, struct device *dev);
228 struct device *device_alias_get(const char *name);
229
230 static inline void
231 device_set_deferred(struct device *dev, bool value)
232 {
233         dev->deferred = value;
234         device_refresh_present(dev);
235 }
236
237 static inline void
238 device_set_disabled(struct device *dev, bool value)
239 {
240         dev->disabled = value;
241         device_refresh_present(dev);
242 }
243
244 #endif