0f08c2666298393363cab34a3474ddb399ef5b9c
[project/netifd.git] / system.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_SYSTEM_H
15 #define __NETIFD_SYSTEM_H
16
17 #include <net/if.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <arpa/inet.h>
21 #include "device.h"
22 #include "interface-ip.h"
23 #include "iprule.h"
24 #include "utils.h"
25
26 struct netdev_type {
27 unsigned short id;
28 const char *name;
29 };
30
31 static const struct netdev_type netdev_types[] = {
32 {ARPHRD_NETROM, "netrom"},
33 {ARPHRD_ETHER, "ethernet"},
34 {ARPHRD_EETHER, "eethernet"},
35 {ARPHRD_AX25, "ax25"},
36 {ARPHRD_PRONET, "pronet"},
37 {ARPHRD_CHAOS, "chaos"},
38 {ARPHRD_IEEE802, "ieee802"},
39 {ARPHRD_ARCNET, "arcnet"},
40 {ARPHRD_APPLETLK, "appletlk"},
41 {ARPHRD_DLCI, "dlci"},
42 {ARPHRD_ATM, "atm"},
43 {ARPHRD_METRICOM, "metricom"},
44 {ARPHRD_IEEE1394, "ieee1394"},
45 {ARPHRD_EUI64, "eui64"},
46 {ARPHRD_INFINIBAND, "infiniband"},
47 {ARPHRD_SLIP, "slip"},
48 {ARPHRD_CSLIP, "cslip"},
49 {ARPHRD_SLIP6, "slip6"},
50 {ARPHRD_CSLIP6, "cslip6"},
51 {ARPHRD_RSRVD, "rsrvd"},
52 {ARPHRD_ADAPT, "adapt"},
53 {ARPHRD_ROSE, "rose"},
54 {ARPHRD_X25, "x25"},
55 {ARPHRD_HWX25, "hwx25"},
56 {ARPHRD_PPP, "ppp"},
57 {ARPHRD_CISCO, "cisco"},
58 {ARPHRD_LAPB, "lapb"},
59 {ARPHRD_DDCMP, "ddcmp"},
60 {ARPHRD_RAWHDLC, "rawhdlc"},
61 {ARPHRD_TUNNEL, "tunnel"},
62 {ARPHRD_TUNNEL6, "tunnel6"},
63 {ARPHRD_FRAD, "frad"},
64 {ARPHRD_SKIP, "skip"},
65 {ARPHRD_LOOPBACK, "loopback"},
66 {ARPHRD_LOCALTLK, "localtlk"},
67 {ARPHRD_FDDI, "fddi"},
68 {ARPHRD_BIF, "bif"},
69 {ARPHRD_SIT, "sit"},
70 {ARPHRD_IPDDP, "ipddp"},
71 {ARPHRD_IPGRE, "ipgre"},
72 {ARPHRD_PIMREG,"pimreg"},
73 {ARPHRD_HIPPI, "hippi"},
74 {ARPHRD_ASH, "ash"},
75 {ARPHRD_ECONET, "econet"},
76 {ARPHRD_IRDA, "irda"},
77 {ARPHRD_FCPP, "fcpp"},
78 {ARPHRD_FCAL, "fcal"},
79 {ARPHRD_FCPL, "fcpl"},
80 {ARPHRD_FCFABRIC, "fcfabric"},
81 {ARPHRD_IEEE80211, "ieee80211"},
82 {ARPHRD_IEEE80211_PRISM, "ie80211-prism"},
83 {ARPHRD_IEEE80211_RADIOTAP, "ieee80211-radiotap"},
84 #ifdef ARPHRD_PHONET
85 {ARPHRD_PHONET, "phonet"},
86 #endif
87 #ifdef ARPHRD_PHONET_PIPE
88 {ARPHRD_PHONET_PIPE, "phonet-pipe"},
89 #endif
90 {ARPHRD_IEEE802154, "ieee802154"},
91 {ARPHRD_VOID, "void"},
92 {ARPHRD_NONE, "none"}
93 };
94
95 enum tunnel_param {
96 TUNNEL_ATTR_TYPE,
97 TUNNEL_ATTR_REMOTE,
98 TUNNEL_ATTR_LOCAL,
99 TUNNEL_ATTR_MTU,
100 TUNNEL_ATTR_DF,
101 TUNNEL_ATTR_TTL,
102 TUNNEL_ATTR_TOS,
103 TUNNEL_ATTR_LINK,
104 TUNNEL_ATTR_DATA,
105 __TUNNEL_ATTR_MAX
106 };
107
108 extern const struct uci_blob_param_list tunnel_attr_list;
109
110 enum vxlan_data {
111 VXLAN_DATA_ATTR_ID,
112 VXLAN_DATA_ATTR_PORT,
113 VXLAN_DATA_ATTR_MACADDR,
114 VXLAN_DATA_ATTR_RXCSUM,
115 VXLAN_DATA_ATTR_TXCSUM,
116 VXLAN_DATA_ATTR_SRCPORTMIN,
117 VXLAN_DATA_ATTR_SRCPORTMAX,
118 VXLAN_DATA_ATTR_LEARNING,
119 VXLAN_DATA_ATTR_RSC,
120 VXLAN_DATA_ATTR_PROXY,
121 VXLAN_DATA_ATTR_L2MISS,
122 VXLAN_DATA_ATTR_L3MISS,
123 VXLAN_DATA_ATTR_GBP,
124 VXLAN_DATA_ATTR_AGEING,
125 VXLAN_DATA_ATTR_LIMIT,
126 __VXLAN_DATA_ATTR_MAX
127 };
128
129 enum gre_data {
130 GRE_DATA_IKEY,
131 GRE_DATA_OKEY,
132 GRE_DATA_ICSUM,
133 GRE_DATA_OCSUM,
134 GRE_DATA_ISEQNO,
135 GRE_DATA_OSEQNO,
136 GRE_DATA_ENCAPLIMIT,
137 __GRE_DATA_ATTR_MAX
138 };
139
140 enum vti_data {
141 VTI_DATA_IKEY,
142 VTI_DATA_OKEY,
143 __VTI_DATA_ATTR_MAX
144 };
145
146 enum xfrm_data {
147 XFRM_DATA_IF_ID,
148 __XFRM_DATA_ATTR_MAX
149 };
150
151 enum sixrd_data {
152 SIXRD_DATA_PREFIX,
153 SIXRD_DATA_RELAY_PREFIX,
154 __SIXRD_DATA_ATTR_MAX
155 };
156
157 enum ipip6_data {
158 IPIP6_DATA_ENCAPLIMIT,
159 IPIP6_DATA_FMRS,
160 __IPIP6_DATA_ATTR_MAX
161 };
162
163 enum fmr_data {
164 FMR_DATA_PREFIX6,
165 FMR_DATA_PREFIX4,
166 FMR_DATA_EALEN,
167 FMR_DATA_OFFSET,
168 __FMR_DATA_ATTR_MAX
169 };
170
171 extern const struct uci_blob_param_list vxlan_data_attr_list;
172 extern const struct uci_blob_param_list gre_data_attr_list;
173 extern const struct uci_blob_param_list vti_data_attr_list;
174 extern const struct uci_blob_param_list xfrm_data_attr_list;
175 extern const struct uci_blob_param_list sixrd_data_attr_list;
176 extern const struct uci_blob_param_list ipip6_data_attr_list;
177 extern const struct uci_blob_param_list fmr_data_attr_list;
178
179 enum bridge_opt {
180 /* stp, forward delay, max age and hello time are always set */
181 BRIDGE_OPT_AGEING_TIME = (1 << 0),
182 BRIDGE_OPT_ROBUSTNESS = (1 << 1),
183 BRIDGE_OPT_QUERY_INTERVAL = (1 << 2),
184 BRIDGE_OPT_QUERY_RESPONSE_INTERVAL = (1 << 3),
185 BRIDGE_OPT_LAST_MEMBER_INTERVAL = (1 << 4),
186 };
187
188 struct bridge_config {
189 enum bridge_opt flags;
190 bool stp;
191 bool stp_kernel;
192 const char *stp_proto;
193
194 bool igmp_snoop;
195 bool multicast_querier;
196 int robustness;
197 int query_interval;
198 int query_response_interval;
199 int last_member_interval;
200
201 unsigned short priority;
202 int forward_delay;
203 bool bridge_empty;
204
205 int ageing_time;
206 int hello_time;
207 int max_age;
208 int hash_max;
209
210 bool vlan_filtering;
211 };
212
213 enum macvlan_opt {
214 MACVLAN_OPT_MACADDR = (1 << 0),
215 };
216
217 struct macvlan_config {
218 const char *mode;
219
220 enum macvlan_opt flags;
221 unsigned char macaddr[6];
222 };
223
224 enum veth_opt {
225 VETH_OPT_MACADDR = (1 << 0),
226 VETH_OPT_PEER_NAME = (1 << 1),
227 VETH_OPT_PEER_MACADDR = (1 << 2),
228 };
229
230 struct veth_config {
231 enum veth_opt flags;
232
233 unsigned char macaddr[6];
234 char peer_name[IFNAMSIZ];
235 unsigned char peer_macaddr[6];
236 };
237
238 enum vlan_proto {
239 VLAN_PROTO_8021Q = 0x8100,
240 VLAN_PROTO_8021AD = 0x88A8
241 };
242
243 struct vlan_qos_mapping {
244 struct vlist_simple_node node; /* entry in vlandev_config->{e,in}gress_qos_mapping_list */
245 uint32_t from;
246 uint32_t to;
247 };
248
249 struct vlandev_config {
250 enum vlan_proto proto;
251 uint16_t vid;
252 struct vlist_simple_tree ingress_qos_mapping_list; /* list of struct vlan_qos_mapping */
253 struct vlist_simple_tree egress_qos_mapping_list; /* list of struct vlan_qos_mapping */
254 };
255
256 enum bonding_mode {
257 BONDING_MODE_BALANCE_RR,
258 BONDING_MODE_ACTIVE_BACKUP,
259 BONDING_MODE_BALANCE_XOR,
260 BONDING_MODE_BROADCAST,
261 BONDING_MODE_8023AD,
262 BONDING_MODE_BALANCE_TLB,
263 BONDING_MODE_BALANCE_ALB,
264 __BONDING_MODE_MAX,
265 };
266
267 struct bonding_config {
268 enum bonding_mode policy;
269 const char *xmit_hash_policy;
270 bool all_ports_active;
271 int min_links;
272 const char *ad_actor_system;
273 int ad_actor_sys_prio;
274 const char *ad_select;
275 const char *lacp_rate;
276 int packets_per_port;
277 int lp_interval;
278 bool dynamic_lb;
279 int resend_igmp;
280 int num_peer_notif;
281 const char *primary;
282 const char *primary_reselect;
283 const char *failover_mac;
284 bool monitor_arp;
285 int monitor_interval;
286 struct blob_attr *arp_target;
287 bool arp_all_targets;
288 const char *arp_validate;
289 bool use_carrier;
290 int updelay;
291 int downdelay;
292 };
293
294 static inline int system_get_addr_family(unsigned int flags)
295 {
296 if ((flags & DEVADDR_FAMILY) == DEVADDR_INET6)
297 return AF_INET6;
298 else
299 return AF_INET;
300 }
301
302 static inline int system_get_addr_len(unsigned int flags)
303 {
304 if ((flags & DEVADDR_FAMILY) != DEVADDR_INET6)
305 return sizeof(struct in_addr);
306 else
307 return sizeof(struct in6_addr);
308 }
309
310 extern const char * const bonding_policy_str[__BONDING_MODE_MAX];
311
312 int system_init(void);
313
314 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg);
315 int system_bridge_delbr(struct device *bridge);
316 int system_bridge_addif(struct device *bridge, struct device *dev);
317 int system_bridge_delif(struct device *bridge, struct device *dev);
318 int system_bridge_vlan(const char *iface, uint16_t vid, bool add, unsigned int vflags);
319 int system_bridge_vlan_check(struct device *dev, char *ifname);
320 void system_bridge_set_stp_state(struct device *dev, bool val);
321
322 int system_bonding_set_device(struct device *dev, struct bonding_config *cfg);
323 int system_bonding_set_port(struct device *dev, struct device *port, bool add, bool primary);
324
325 int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvlan_config *cfg);
326 int system_macvlan_del(struct device *macvlan);
327
328 int system_veth_add(struct device *veth, struct veth_config *cfg);
329 int system_veth_del(struct device *veth);
330
331 int system_vlan_add(struct device *dev, int id);
332 int system_vlan_del(struct device *dev);
333
334 int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg);
335 int system_vlandev_del(struct device *vlandev);
336
337 void system_if_get_settings(struct device *dev, struct device_settings *s);
338 void system_if_clear_state(struct device *dev);
339 int system_if_up(struct device *dev);
340 int system_if_down(struct device *dev);
341 int system_if_check(struct device *dev);
342 int system_if_resolve(struct device *dev);
343
344 int system_if_dump_info(struct device *dev, struct blob_buf *b);
345 int system_if_dump_stats(struct device *dev, struct blob_buf *b);
346 struct device *system_if_get_parent(struct device *dev);
347 bool system_if_force_external(const char *ifname);
348 void system_if_apply_settings(struct device *dev, struct device_settings *s,
349 uint64_t apply_mask);
350
351 int system_add_address(struct device *dev, struct device_addr *addr);
352 int system_del_address(struct device *dev, struct device_addr *addr);
353
354 int system_add_route(struct device *dev, struct device_route *route);
355 int system_del_route(struct device *dev, struct device_route *route);
356 int system_flush_routes(void);
357
358 int system_add_neighbor(struct device *dev, struct device_neighbor * neighbor);
359 int system_del_neighbor(struct device *dev, struct device_neighbor * neighbor);
360
361 bool system_resolve_rt_type(const char *type, unsigned int *id);
362 bool system_resolve_rt_proto(const char *type, unsigned int *id);
363 bool system_resolve_rt_table(const char *name, unsigned int *id);
364 bool system_is_default_rt_table(unsigned int id);
365 bool system_resolve_rpfilter(const char *filter, unsigned int *id);
366
367 int system_del_ip_tunnel(const struct device *dev);
368 int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr);
369
370 int system_add_iprule(struct iprule *rule);
371 int system_del_iprule(struct iprule *rule);
372 int system_flush_iprules(void);
373
374 bool system_resolve_iprule_action(const char *action, unsigned int *id);
375
376 time_t system_get_rtime(void);
377
378 void system_fd_set_cloexec(int fd);
379
380 int system_update_ipv6_mtu(struct device *dev, int mtu);
381
382 int system_link_netns_move(struct device *dev, const pid_t target_ns, const char *target_ifname);
383 int system_netns_open(const pid_t target_ns);
384 int system_netns_set(int netns_fd);
385
386 #endif