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