system-linux: improve handling of device rename
[oweals/netifd.git] / interface-ip.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 __INTERFACE_IP_H
15 #define __INTERFACE_IP_H
16
17 #include "interface.h"
18
19 enum device_addr_flags {
20         /* address family for routes and addresses */
21         DEVADDR_INET4           = (0 << 0),
22         DEVADDR_INET6           = (1 << 0),
23         DEVADDR_FAMILY          = DEVADDR_INET4 | DEVADDR_INET6,
24
25         /* externally added address */
26         DEVADDR_EXTERNAL        = (1 << 2),
27
28         /* route overrides the default interface metric */
29         DEVROUTE_METRIC         = (1 << 3),
30
31         /* route overrides the default interface mtu */
32         DEVROUTE_MTU            = (1 << 4),
33
34         /* route overrides the default proto type */
35         DEVROUTE_PROTO          = (1 << 5),
36
37         /* address is off-link (no subnet-route) */
38         DEVADDR_OFFLINK         = (1 << 6),
39
40         /* route resides in different table */
41         DEVROUTE_TABLE          = (1 << 7),
42
43         /* route resides in default source-route table */
44         DEVROUTE_SRCTABLE       = (1 << 8),
45
46         /* route is on-link */
47         DEVROUTE_ONLINK         = (1 << 9),
48
49         /* route overrides the default route type */
50         DEVROUTE_TYPE           = (1 << 10),
51
52         /* neighbor mac address */
53         DEVNEIGH_MAC            = (1 << 11),
54 };
55
56 union if_addr {
57         struct in_addr in;
58         struct in6_addr in6;
59 };
60
61 struct device_prefix_assignment {
62         struct list_head head;
63         int32_t assigned;
64         uint8_t length;
65         int weight;
66         struct in6_addr addr;
67         bool enabled;
68         char name[];
69 };
70
71 struct device_prefix {
72         struct vlist_node node;
73         struct list_head head;
74         struct list_head assignments;
75         struct interface *iface;
76         time_t valid_until;
77         time_t preferred_until;
78
79         struct in6_addr excl_addr;
80         uint8_t excl_length;
81
82         struct in6_addr addr;
83         uint8_t length;
84
85         char pclass[];
86 };
87
88 struct device_route {
89         struct vlist_node node;
90         struct interface *iface;
91
92         bool enabled;
93         bool keep;
94         bool failed;
95
96         union if_addr nexthop;
97         int mtu;
98         unsigned int type;
99         unsigned int proto;
100         time_t valid_until;
101
102         /* must be last */
103         enum device_addr_flags flags;
104         int metric; /* there can be multiple routes to the same target */
105         unsigned int table;
106         unsigned int mask;
107         unsigned int sourcemask;
108         union if_addr addr;
109         union if_addr source;
110 };
111
112 struct device_neighbor {
113         struct vlist_node node;
114
115         bool failed;
116         bool proxy;
117         bool keep;
118         bool enabled;
119         bool router;
120
121         uint8_t macaddr[6];
122         enum device_addr_flags flags;
123         union if_addr addr;
124 };
125
126 struct device_addr {
127         struct vlist_node node;
128         bool enabled;
129         bool failed;
130         unsigned int policy_table;
131
132         struct device_route subnet;
133
134         /* ipv4 only */
135         uint32_t broadcast;
136         uint32_t point_to_point;
137
138         /* ipv6 only */
139         time_t valid_until;
140         time_t preferred_until;
141         char *pclass;
142
143         /* must be last */
144         enum device_addr_flags flags;
145         unsigned int mask;
146         union if_addr addr;
147 };
148
149 struct device_source_table {
150         struct list_head head;
151         uint32_t table;
152         uint16_t refcount;
153         uint8_t v6;
154         uint8_t mask;
155         union if_addr addr;
156 };
157
158 struct dns_server {
159         struct vlist_simple_node node;
160         int af;
161         union if_addr addr;
162 };
163
164 struct dns_search_domain {
165         struct vlist_simple_node node;
166         char name[];
167 };
168
169 extern const struct uci_blob_param_list route_attr_list;
170 extern const struct uci_blob_param_list neighbor_attr_list;
171 extern struct list_head prefixes;
172
173 void interface_ip_init(struct interface *iface);
174 void interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list);
175 void interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list);
176 void interface_write_resolv_conf(const char *jail);
177
178 void interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6);
179 void interface_ip_add_neighbor(struct interface *iface, struct blob_attr *attr, bool v6);
180 void interface_ip_update_start(struct interface_ip_settings *ip);
181 void interface_ip_update_complete(struct interface_ip_settings *ip);
182 void interface_ip_flush(struct interface_ip_settings *ip);
183 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled);
184 void interface_ip_update_metric(struct interface_ip_settings *ip, int metric);
185
186 struct interface *interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface);
187
188 struct device_prefix* interface_ip_add_device_prefix(struct interface *iface,
189                 struct in6_addr *addr, uint8_t length, time_t valid_until, time_t preferred_until,
190                 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass);
191 void interface_ip_set_ula_prefix(const char *prefix);
192 void interface_refresh_assignments(bool hint);
193 void interface_update_prefix_delegation(struct interface_ip_settings *ip);
194
195 #endif