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