Don't always assume routes & addresses are applied
[project/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 automatically added by kernel */
35 DEVADDR_KERNEL = (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
47 union if_addr {
48 struct in_addr in;
49 struct in6_addr in6;
50 };
51
52 struct device_prefix_assignment {
53 struct list_head head;
54 int32_t assigned;
55 uint8_t length;
56 bool enabled;
57 char name[];
58 };
59
60 struct device_prefix {
61 struct vlist_node node;
62 struct list_head head;
63 struct list_head assignments;
64 struct interface *iface;
65 time_t valid_until;
66 time_t preferred_until;
67
68 struct in6_addr excl_addr;
69 uint8_t excl_length;
70
71 struct in6_addr addr;
72 uint8_t length;
73
74 char pclass[];
75 };
76
77 struct device_addr {
78 struct vlist_node node;
79 bool enabled;
80 bool failed;
81
82 /* ipv4 only */
83 uint32_t broadcast;
84 uint32_t point_to_point;
85
86 /* ipv6 only */
87 time_t valid_until;
88 time_t preferred_until;
89 char *pclass;
90
91 /* must be last */
92 enum device_addr_flags flags;
93 unsigned int mask;
94 union if_addr addr;
95 };
96
97 struct device_route {
98 struct vlist_node node;
99 struct interface *iface;
100
101 bool enabled;
102 bool keep;
103 bool failed;
104
105 union if_addr nexthop;
106 int mtu;
107 time_t valid_until;
108
109 /* must be last */
110 enum device_addr_flags flags;
111 int metric; // there can be multiple routes to the same target
112 unsigned int table;
113 unsigned int mask;
114 unsigned int sourcemask;
115 union if_addr addr;
116 union if_addr source;
117 };
118
119 struct device_source_table {
120 struct list_head head;
121 uint32_t table;
122 uint16_t refcount;
123 uint8_t v6;
124 uint8_t mask;
125 union if_addr addr;
126 };
127
128 struct dns_server {
129 struct vlist_simple_node node;
130 int af;
131 union if_addr addr;
132 };
133
134 struct dns_search_domain {
135 struct vlist_simple_node node;
136 char name[];
137 };
138
139 extern const struct uci_blob_param_list route_attr_list;
140 extern struct list_head prefixes;
141
142 void interface_ip_init(struct interface *iface);
143 void interface_add_dns_server(struct interface_ip_settings *ip, const char *str);
144 void interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list);
145 void interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list);
146 void interface_write_resolv_conf(void);
147
148 void interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6);
149
150 void interface_ip_update_start(struct interface_ip_settings *ip);
151 void interface_ip_update_complete(struct interface_ip_settings *ip);
152 void interface_ip_flush(struct interface_ip_settings *ip);
153 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled);
154 void interface_ip_update_metric(struct interface_ip_settings *ip, int metric);
155
156 struct interface *interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface);
157
158 struct device_prefix* interface_ip_add_device_prefix(struct interface *iface,
159 struct in6_addr *addr, uint8_t length, time_t valid_until, time_t preferred_until,
160 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass);
161 void interface_ip_set_ula_prefix(const char *prefix);
162 void interface_refresh_assignments(bool hint);
163
164 #endif