scripts/netifd-wireless.sh: add support for specifying the operating band
[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 <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 DEV_ATTR_TYPE,
33 DEV_ATTR_MTU,
34 DEV_ATTR_MTU6,
35 DEV_ATTR_MACADDR,
36 DEV_ATTR_TXQUEUELEN,
37 DEV_ATTR_ENABLED,
38 DEV_ATTR_IPV6,
39 DEV_ATTR_PROMISC,
40 DEV_ATTR_RPFILTER,
41 DEV_ATTR_ACCEPTLOCAL,
42 DEV_ATTR_IGMPVERSION,
43 DEV_ATTR_MLDVERSION,
44 DEV_ATTR_NEIGHREACHABLETIME,
45 DEV_ATTR_DADTRANSMITS,
46 DEV_ATTR_MULTICAST_TO_UNICAST,
47 DEV_ATTR_MULTICAST_ROUTER,
48 DEV_ATTR_MULTICAST_FAST_LEAVE,
49 DEV_ATTR_MULTICAST,
50 DEV_ATTR_LEARNING,
51 DEV_ATTR_UNICAST_FLOOD,
52 DEV_ATTR_NEIGHGCSTALETIME,
53 DEV_ATTR_SENDREDIRECTS,
54 DEV_ATTR_NEIGHLOCKTIME,
55 DEV_ATTR_ISOLATE,
56 DEV_ATTR_IP6SEGMENTROUTING,
57 DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST,
58 DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST,
59 DEV_ATTR_DROP_GRATUITOUS_ARP,
60 DEV_ATTR_DROP_UNSOLICITED_NA,
61 DEV_ATTR_ARP_ACCEPT,
62 DEV_ATTR_AUTH,
63 __DEV_ATTR_MAX,
64 };
65
66 enum dev_change_type {
67 DEV_CONFIG_NO_CHANGE,
68 DEV_CONFIG_APPLIED,
69 DEV_CONFIG_RESTART,
70 DEV_CONFIG_RECREATE,
71 };
72
73 struct device_type {
74 struct list_head list;
75 const char *name;
76
77 bool bridge_capability;
78 const char *name_prefix;
79
80 const struct uci_blob_param_list *config_params;
81
82 struct device *(*create)(const char *name, struct device_type *devtype,
83 struct blob_attr *attr);
84 void (*config_init)(struct device *);
85 enum dev_change_type (*reload)(struct device *, struct blob_attr *);
86 void (*dump_info)(struct device *, struct blob_buf *buf);
87 void (*dump_stats)(struct device *, struct blob_buf *buf);
88 int (*check_state)(struct device *);
89 void (*free)(struct device *);
90 };
91
92 enum {
93 DEV_OPT_MTU = (1 << 0),
94 DEV_OPT_MACADDR = (1 << 1),
95 DEV_OPT_TXQUEUELEN = (1 << 2),
96 DEV_OPT_IPV6 = (1 << 3),
97 DEV_OPT_PROMISC = (1 << 4),
98 DEV_OPT_RPFILTER = (1 << 5),
99 DEV_OPT_ACCEPTLOCAL = (1 << 6),
100 DEV_OPT_IGMPVERSION = (1 << 7),
101 DEV_OPT_MLDVERSION = (1 << 8),
102 DEV_OPT_NEIGHREACHABLETIME = (1 << 9),
103 DEV_OPT_DEFAULT_MACADDR = (1 << 10),
104 DEV_OPT_AUTH = (1 << 11),
105 DEV_OPT_MTU6 = (1 << 12),
106 DEV_OPT_DADTRANSMITS = (1 << 13),
107 DEV_OPT_MULTICAST_TO_UNICAST = (1 << 14),
108 DEV_OPT_MULTICAST_ROUTER = (1 << 15),
109 DEV_OPT_MULTICAST = (1 << 16),
110 DEV_OPT_LEARNING = (1 << 17),
111 DEV_OPT_UNICAST_FLOOD = (1 << 18),
112 DEV_OPT_NEIGHGCSTALETIME = (1 << 19),
113 DEV_OPT_MULTICAST_FAST_LEAVE = (1 << 20),
114 DEV_OPT_SENDREDIRECTS = (1 << 21),
115 DEV_OPT_NEIGHLOCKTIME = (1 << 22),
116 DEV_OPT_ISOLATE = (1 << 23),
117 DEV_OPT_IP6SEGMENTROUTING = (1 << 24),
118 DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST = (1 << 25),
119 DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST = (1 << 26),
120 DEV_OPT_DROP_GRATUITOUS_ARP = (1 << 27),
121 DEV_OPT_DROP_UNSOLICITED_NA = (1 << 28),
122 DEV_OPT_ARP_ACCEPT = (1 << 29),
123 };
124
125 /* events broadcasted to all users of a device */
126 enum device_event {
127 DEV_EVENT_ADD,
128 DEV_EVENT_REMOVE,
129
130 DEV_EVENT_UPDATE_IFNAME,
131 DEV_EVENT_UPDATE_IFINDEX,
132
133 DEV_EVENT_SETUP,
134 DEV_EVENT_TEARDOWN,
135 DEV_EVENT_UP,
136 DEV_EVENT_DOWN,
137
138 DEV_EVENT_AUTH_UP,
139 DEV_EVENT_LINK_UP,
140 DEV_EVENT_LINK_DOWN,
141
142 /* Topology changed (i.e. bridge member added) */
143 DEV_EVENT_TOPO_CHANGE,
144
145 __DEV_EVENT_MAX
146 };
147
148 /*
149 * device dependency with callbacks
150 */
151 struct device_user {
152 struct safe_list list;
153
154 bool claimed;
155 bool hotplug;
156 bool alias;
157
158 uint8_t ev_idx[__DEV_EVENT_MAX];
159
160 struct device *dev;
161 void (*cb)(struct device_user *, enum device_event);
162 };
163
164 struct device_settings {
165 unsigned int flags;
166 unsigned int valid_flags;
167 unsigned int mtu;
168 unsigned int mtu6;
169 unsigned int txqueuelen;
170 uint8_t macaddr[6];
171 bool ipv6;
172 bool promisc;
173 unsigned int rpfilter;
174 bool acceptlocal;
175 unsigned int igmpversion;
176 unsigned int mldversion;
177 unsigned int neigh4reachabletime;
178 unsigned int neigh6reachabletime;
179 unsigned int neigh4gcstaletime;
180 unsigned int neigh6gcstaletime;
181 int neigh4locktime;
182 unsigned int dadtransmits;
183 bool multicast_to_unicast;
184 unsigned int multicast_router;
185 bool multicast_fast_leave;
186 bool multicast;
187 bool learning;
188 bool unicast_flood;
189 bool sendredirects;
190 bool ip6segmentrouting;
191 bool isolate;
192 bool drop_v4_unicast_in_l2_multicast;
193 bool drop_v6_unicast_in_l2_multicast;
194 bool drop_gratuitous_arp;
195 bool drop_unsolicited_na;
196 bool arp_accept;
197 bool auth;
198 };
199
200 /*
201 * link layer device. typically represents a linux network device.
202 * can be used to support VLANs as well
203 */
204 struct device {
205 struct device_type *type;
206
207 struct avl_node avl;
208 struct safe_list users;
209 struct safe_list aliases;
210
211 struct vlist_tree vlans;
212 struct kvlist vlan_aliases;
213
214 char ifname[IFNAMSIZ + 1];
215 int ifindex;
216
217 struct blob_attr *config;
218 bool config_pending;
219 bool sys_present;
220 /* DEV_EVENT_ADD */
221 bool present;
222 /* DEV_EVENT_UP */
223 int active;
224 /* DEV_EVENT_LINK_UP */
225 bool link_active;
226 bool auth_status;
227
228 bool external;
229 bool disabled;
230 bool deferred;
231 bool hidden;
232
233 bool current_config;
234 bool iface_config;
235 bool default_config;
236 bool wireless;
237 bool wireless_ap;
238 bool wireless_isolate;
239
240 struct interface *config_iface;
241
242 /* set interface up or down */
243 device_state_cb set_state;
244
245 const struct device_hotplug_ops *hotplug_ops;
246
247 struct device_user parent;
248
249 struct device_settings orig_settings;
250 struct device_settings settings;
251 };
252
253 struct device_hotplug_ops {
254 int (*prepare)(struct device *dev, struct device **bridge_dev);
255 int (*add)(struct device *main, struct device *member, struct blob_attr *vlan);
256 int (*del)(struct device *main, struct device *member);
257 };
258
259 enum bridge_vlan_flags {
260 BRVLAN_F_SELF = (1 << 0),
261 BRVLAN_F_PVID = (1 << 1),
262 BRVLAN_F_UNTAGGED = (1 << 2),
263 };
264
265 struct bridge_vlan_port {
266 const char *ifname;
267 uint16_t flags;
268 };
269
270 struct bridge_vlan {
271 struct vlist_node node;
272
273 struct bridge_vlan_port *ports;
274 int n_ports;
275
276 struct list_head hotplug_ports;
277
278 uint16_t vid;
279 bool local;
280 };
281
282 extern const struct uci_blob_param_list device_attr_list;
283 extern struct device_type simple_device_type;
284 extern struct device_type tunnel_device_type;
285
286 void device_lock(void);
287 void device_unlock(void);
288
289 void device_vlan_update(bool done);
290
291 int device_type_add(struct device_type *devtype);
292 struct device_type *device_type_get(const char *tname);
293 struct device *device_create(const char *name, struct device_type *type,
294 struct blob_attr *config);
295 void device_merge_settings(struct device *dev, struct device_settings *n);
296 void device_init_settings(struct device *dev, struct blob_attr **tb);
297 void device_init_pending(void);
298
299 enum dev_change_type
300 device_apply_config(struct device *dev, struct device_type *type,
301 struct blob_attr *config);
302
303 void device_reset_config(void);
304 void device_reset_old(void);
305
306 int device_init_virtual(struct device *dev, struct device_type *type, const char *name);
307 int device_init(struct device *dev, struct device_type *type, const char *ifname);
308 void device_cleanup(struct device *dev);
309 struct device *device_find(const char *name);
310 struct device *device_get(const char *name, int create);
311 void device_add_user(struct device_user *dep, struct device *dev);
312 void device_remove_user(struct device_user *dep);
313 void device_broadcast_event(struct device *dev, enum device_event ev);
314
315 void device_set_present(struct device *dev, bool state);
316 void device_set_link(struct device *dev, bool state);
317 void device_set_ifindex(struct device *dev, int ifindex);
318 int device_set_ifname(struct device *dev, const char *name);
319 void device_refresh_present(struct device *dev);
320 int device_claim(struct device_user *dep);
321 void device_release(struct device_user *dep);
322 int device_check_state(struct device *dev);
323 void device_dump_status(struct blob_buf *b, struct device *dev);
324
325 void device_free_unused(struct device *dev);
326
327 struct device *get_vlan_device_chain(const char *ifname, bool create);
328 void alias_notify_device(const char *name, struct device *dev);
329 struct device *device_alias_get(const char *name);
330
331 void device_set_auth_status(struct device *dev, bool value);
332
333 static inline void
334 device_set_deferred(struct device *dev, bool value)
335 {
336 dev->deferred = value;
337 device_refresh_present(dev);
338 }
339
340 static inline void
341 device_set_disabled(struct device *dev, bool value)
342 {
343 dev->disabled = value;
344 device_refresh_present(dev);
345 }
346
347 static inline bool
348 device_link_active(struct device *dev)
349 {
350 if (dev->settings.auth && !dev->auth_status)
351 return false;
352
353 return dev->link_active;
354 }
355
356 bool device_check_ip6segmentrouting(void);
357
358 #endif