147232436a8ead3aeecbda3ab87b1fb20bc42d4a
[project/netifd.git] / interface.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_INTERFACE_H
15 #define __NETIFD_INTERFACE_H
16
17 #include "device.h"
18 #include "config.h"
19
20 struct interface;
21 struct interface_proto_state;
22
23 enum interface_event {
24         IFEV_DOWN,
25         IFEV_UP,
26         IFEV_UPDATE,
27         IFEV_FREE,
28         IFEV_RELOAD,
29         IFEV_LINK_UP,
30 };
31
32 enum interface_state {
33         IFS_SETUP,
34         IFS_UP,
35         IFS_TEARDOWN,
36         IFS_DOWN,
37 };
38
39 enum interface_config_state {
40         IFC_NORMAL,
41         IFC_RELOAD,
42         IFC_REMOVE
43 };
44
45 enum interface_id_selection_type {
46         IFID_FIXED,
47         IFID_RANDOM,
48         IFID_EUI64
49 };
50
51 enum interface_update_flags {
52         IUF_ADDRESS     = (1 << 0),
53         IUF_ROUTE       = (1 << 1),
54         IUF_PREFIX      = (1 << 2),
55         IUF_DATA        = (1 << 3),
56 };
57
58 struct interface_error {
59         struct list_head list;
60
61         const char *subsystem;
62         const char *code;
63         const char *data[];
64 };
65
66 struct interface_user {
67         struct list_head list;
68         struct interface *iface;
69         void (*cb)(struct interface_user *dep, struct interface *iface, enum interface_event ev);
70 };
71
72 struct interface_ip_settings {
73         struct interface *iface;
74         bool enabled;
75         bool no_defaultroute;
76         bool no_dns;
77         bool no_delegation;
78
79         struct vlist_tree addr;
80         struct vlist_tree route;
81         struct vlist_tree prefix;
82
83         struct vlist_simple_tree dns_servers;
84         struct vlist_simple_tree dns_search;
85 };
86
87 struct interface_data {
88         struct avl_node node;
89         struct blob_attr data[];
90 };
91
92 struct interface_assignment_class {
93         struct list_head head;
94         char name[];
95 };
96
97 /*
98  * interface configuration
99  */
100 struct interface {
101         struct vlist_node node;
102         struct list_head hotplug_list;
103         enum interface_event hotplug_ev;
104
105         const char *name;
106         const char *ifname;
107
108         bool available;
109         bool autostart;
110         bool config_autostart;
111         bool device_config;
112         bool enabled;
113         bool link_state;
114         bool force_link;
115         bool dynamic;
116         bool policy_rules_set;
117         bool link_up_event;
118
119         time_t start_time;
120         enum interface_state state;
121         enum interface_config_state config_state;
122         enum interface_update_flags updated;
123
124         struct list_head users;
125
126         /* for alias interface */
127         const char *parent_ifname;
128         struct interface_user parent_iface;
129
130         /* main interface that the interface is bound to */
131         struct device_user main_dev;
132         struct device_user ext_dev;
133
134         /* interface that layer 3 communication will go through */
135         struct device_user l3_dev;
136
137         struct blob_attr *config;
138
139         /* primary protocol state */
140         const struct proto_handler *proto_handler;
141         struct interface_proto_state *proto;
142
143         struct interface_ip_settings proto_ip;
144         struct interface_ip_settings config_ip;
145         struct vlist_tree host_routes;
146
147         int metric;
148         int dns_metric;
149         unsigned int ip4table;
150         unsigned int ip6table;
151
152         /* IPv6 assignment parameters */
153         enum interface_id_selection_type assignment_iface_id_selection;
154         struct in6_addr assignment_fixed_iface_id;
155         uint8_t assignment_length;
156         int32_t assignment_hint;
157         struct list_head assignment_classes;
158         int assignment_weight;
159
160         /* errors/warnings while trying to bring up the interface */
161         struct list_head errors;
162
163         /* extra data provided by protocol handlers or modules */
164         struct avl_tree data;
165
166         struct uloop_timeout remove_timer;
167         struct ubus_object ubus;
168 };
169
170
171 extern struct vlist_tree interfaces;
172 extern const struct uci_blob_param_list interface_attr_list;
173
174 struct interface *interface_alloc(const char *name, struct blob_attr *config);
175
176 void interface_set_dynamic(struct interface *iface);
177
178 void interface_add(struct interface *iface, struct blob_attr *config);
179 bool interface_add_alias(struct interface *iface, struct blob_attr *config);
180
181 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state);
182
183 void interface_set_available(struct interface *iface, bool new_state);
184 int interface_set_up(struct interface *iface);
185 int interface_set_down(struct interface *iface);
186 void __interface_set_down(struct interface *iface, bool force);
187
188 void interface_set_main_dev(struct interface *iface, struct device *dev);
189 void interface_set_l3_dev(struct interface *iface, struct device *dev);
190
191 void interface_add_user(struct interface_user *dep, struct interface *iface);
192 void interface_remove_user(struct interface_user *dep);
193
194 int interface_remove_link(struct interface *iface, struct device *dev);
195 int interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext);
196
197 void interface_add_error(struct interface *iface, const char *subsystem,
198                          const char *code, const char **data, int n_data);
199
200 int interface_add_data(struct interface *iface, const struct blob_attr *data);
201 int interface_parse_data(struct interface *iface, const struct blob_attr *attr);
202
203 void interface_update_start(struct interface *iface, const bool keep_old);
204 void interface_update_complete(struct interface *iface);
205
206 void interface_start_pending(void);
207
208 #endif