move interface address handling to the device module, clean up arguments to system_...
[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 struct device_type {
13         const char *name;
14
15         void (*dump_status)(struct device *, struct blob_buf *buf);
16         int (*check_state)(struct device *);
17         void (*free)(struct device *);
18 };
19
20 enum {
21         DEV_OPT_MTU             = (1 << 0),
22         DEV_OPT_MACADDR         = (1 << 1),
23         DEV_OPT_TXQUEUELEN      = (1 << 2)
24 };
25
26 enum device_addr_flags {
27         /* address family for routes and addresses */
28         DEVADDR_INET4   = (0 << 0),
29         DEVADDR_INET6   = (1 << 0),
30         DEVADDR_FAMILY  = DEVADDR_INET4 | DEVADDR_INET6,
31
32         /* device route (no gateway) */
33         DEVADDR_DEVICE  = (1 << 1),
34 };
35
36 union if_addr {
37         struct in_addr in;
38         struct in6_addr in6;
39 };
40
41 struct device_addr {
42         struct list_head list;
43         void *ctx;
44
45         enum device_addr_flags flags;
46
47         unsigned int mask;
48         union if_addr addr;
49 };
50
51 struct device_route {
52         struct list_head list;
53         void *ctx;
54
55         enum device_addr_flags flags;
56
57         unsigned int mask;
58         union if_addr addr;
59         union if_addr nexthop;
60 };
61
62 /* 
63  * link layer device. typically represents a linux network device.
64  * can be used to support VLANs as well
65  */
66 struct device {
67         const struct device_type *type;
68
69         struct avl_node avl;
70         struct list_head users;
71
72         char ifname[IFNAMSIZ + 1];
73         int ifindex;
74
75         bool present;
76         int active;
77
78         /* set interface up or down */
79         device_state_cb set_state;
80
81         const struct device_hotplug_ops *hotplug_ops;
82
83         /* settings */
84         unsigned int flags;
85
86         unsigned int mtu;
87         unsigned int txqueuelen;
88         uint8_t macaddr[6];
89
90         uint32_t config_hash;
91 };
92
93 /* events broadcasted to all users of a device */
94 enum device_event {
95         DEV_EVENT_ADD,
96         DEV_EVENT_REMOVE,
97
98         DEV_EVENT_SETUP,
99         DEV_EVENT_TEARDOWN,
100         DEV_EVENT_UP,
101         DEV_EVENT_DOWN,
102
103         DEV_EVENT_LINK_UP,
104         DEV_EVENT_LINK_DOWN,
105 };
106
107 /*
108  * device dependency with callbacks
109  */
110 struct device_user {
111         struct list_head list;
112
113         struct device *dev;
114         void (*cb)(struct device_user *, enum device_event);
115 };
116
117 struct device_hotplug_ops {
118         int (*add)(struct device *main, struct device *member);
119         int (*del)(struct device *main, struct device *member);
120 };
121
122 static inline void
123 free_device(struct device *dev)
124 {
125         dev->type->free(dev);
126 }
127
128 void init_virtual_device(struct device *dev, const struct device_type *type, const char *name);
129 int init_device(struct device *iface, const struct device_type *type, const char *ifname);
130 void cleanup_device(struct device *iface);
131 struct device *get_device(const char *name, bool create);
132 void add_device_user(struct device_user *dep, struct device *iface);
133 void remove_device_user(struct device_user *dep);
134
135 void set_device_present(struct device *dev, bool state);
136 int claim_device(struct device *dev);
137 void release_device(struct device *dev);
138 int check_device_state(struct device *dev);
139
140 void cleanup_devices(void);
141
142 struct device *get_vlan_device_chain(const char *ifname, bool create);
143 struct device *bridge_create(const char *name, struct uci_section *s);
144
145 #endif