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