]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/network.h
Merge pull request #3235 from xinhua9569/master
[mirror_lxc.git] / src / lxc / network.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_NETWORK_H
4 #define __LXC_NETWORK_H
5
6 #include <arpa/inet.h>
7 #include <linux/types.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12
13 #include "list.h"
14
15 struct lxc_conf;
16 struct lxc_handler;
17 struct lxc_netdev;
18
19 enum {
20 LXC_NET_EMPTY,
21 LXC_NET_VETH,
22 LXC_NET_MACVLAN,
23 LXC_NET_IPVLAN,
24 LXC_NET_PHYS,
25 LXC_NET_VLAN,
26 LXC_NET_NONE,
27 LXC_NET_MAXCONFTYPE,
28 };
29
30 /*
31 * Defines the structure to configure an ipv4 address
32 * @address : ipv4 address
33 * @broadcast : ipv4 broadcast address
34 * @mask : network mask
35 */
36 struct lxc_inetdev {
37 struct in_addr addr;
38 struct in_addr bcast;
39 unsigned int prefix;
40 };
41
42 struct lxc_route {
43 struct in_addr addr;
44 };
45
46 /*
47 * Defines the structure to configure an ipv6 address
48 * @flags : set the address up
49 * @address : ipv6 address
50 * @broadcast : ipv6 broadcast address
51 * @mask : network mask
52 */
53 struct lxc_inet6dev {
54 struct in6_addr addr;
55 struct in6_addr mcast;
56 struct in6_addr acast;
57 unsigned int prefix;
58 };
59
60 struct lxc_route6 {
61 struct in6_addr addr;
62 };
63
64 /* Contains information about the host side veth device.
65 * @pair : Name of the host side veth device.
66 * If the user requested that the host veth device be created with a
67 * specific names this field will be set. If this field is set @veth1
68 * is not set.
69 * @veth1 : Name of the host side veth device.
70 * If the user did not request that the host veth device be created
71 * with a specific name this field will be set. If this field is set
72 * @pair is not set.
73 * @ifindex : Ifindex of the network device.
74 */
75 struct ifla_veth {
76 char pair[IFNAMSIZ];
77 char veth1[IFNAMSIZ];
78 int ifindex;
79 struct lxc_list ipv4_routes;
80 struct lxc_list ipv6_routes;
81 int mode; /* bridge, router */
82 };
83
84 struct ifla_vlan {
85 unsigned int flags;
86 unsigned int fmask;
87 unsigned short vid;
88 unsigned short pad;
89 };
90
91 struct ifla_macvlan {
92 int mode; /* private, vepa, bridge, passthru */
93 };
94
95 struct ifla_ipvlan {
96 int mode; /* l3, l3s, l2 */
97 int isolation; /* bridge, private, vepa */
98 };
99
100 /* Contains information about the physical network device as seen from the host.
101 * @ifindex : The ifindex of the physical network device in the host's network
102 * namespace.
103 */
104 struct ifla_phys {
105 int ifindex;
106 int mtu;
107 };
108
109 union netdev_p {
110 struct ifla_macvlan macvlan_attr;
111 struct ifla_ipvlan ipvlan_attr;
112 struct ifla_phys phys_attr;
113 struct ifla_veth veth_attr;
114 struct ifla_vlan vlan_attr;
115 };
116
117 /*
118 * Defines a structure to configure a network device
119 * @idx : network counter
120 * @ifindex : ifindex of the network device
121 * Note that this is the ifindex of the network device in
122 * the container's network namespace. If the network device
123 * consists of a pair of network devices (e.g. veth pairs
124 * attached to a network bridge) then this index cannot be
125 * used to identify or modify the host veth device. See
126 * struct ifla_veth for the host side information.
127 * @type : network type (veth, macvlan, vlan, ...)
128 * @flags : flag of the network device (IFF_UP, ... )
129 * @link : lxc.net.[i].link, name of bridge or host iface to attach
130 * if any
131 * @name : lxc.net.[i].name, name of iface on the container side
132 * @created_name : the name with which this interface got created before
133 * being renamed to final_name.
134 * Currenly only used for veth devices.
135 * @hwaddr : mac address
136 * @mtu : maximum transmission unit
137 * @priv : information specific to the specificed network type
138 * Note that this is a union so whether accessing a struct
139 * is possible is dependent on the network type.
140 * @ipv4 : a list of ipv4 addresses to be set on the network device
141 * @ipv6 : a list of ipv6 addresses to be set on the network device
142 * @ipv4_gateway_auto : whether the ipv4 gateway is to be automatically gathered
143 * from the associated @link
144 * @ipv4_gateway_dev : whether the ipv4 gateway is to be set as a device route
145 * @ipv4_gateway : ipv4 gateway
146 * @ipv6_gateway_auto : whether the ipv6 gateway is to be automatically gathered
147 * from the associated @link
148 * @ipv6_gateway_dev : whether the ipv6 gateway is to be set as a device route
149 * @ipv6_gateway : ipv6 gateway
150 * @upscript : a script filename to be executed during interface
151 * configuration
152 * @downscript : a script filename to be executed during interface
153 * destruction
154 */
155 struct lxc_netdev {
156 ssize_t idx;
157 int ifindex;
158 int type;
159 int flags;
160 char link[IFNAMSIZ];
161 bool l2proxy;
162 char name[IFNAMSIZ];
163 char created_name[IFNAMSIZ];
164 char *hwaddr;
165 char *mtu;
166 union netdev_p priv;
167 struct lxc_list ipv4;
168 struct lxc_list ipv6;
169 bool ipv4_gateway_auto;
170 bool ipv4_gateway_dev;
171 struct in_addr *ipv4_gateway;
172 bool ipv6_gateway_auto;
173 bool ipv6_gateway_dev;
174 struct in6_addr *ipv6_gateway;
175 char *upscript;
176 char *downscript;
177 };
178
179 /* Convert a string mac address to a socket structure. */
180 extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr);
181
182 /* Move a device between namespaces. */
183 extern int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char *ifname);
184 extern int lxc_netdev_move_by_name(const char *ifname, pid_t pid,
185 const char *newname);
186
187 /* Delete a network device. */
188 extern int lxc_netdev_delete_by_name(const char *name);
189 extern int lxc_netdev_delete_by_index(int ifindex);
190
191 /* Change the device name. */
192 extern int lxc_netdev_rename_by_name(const char *oldname, const char *newname);
193 extern int lxc_netdev_rename_by_index(int ifindex, const char *newname);
194
195 extern int netdev_set_flag(const char *name, int flag);
196
197 /* Set the device network up or down. */
198 extern int lxc_netdev_isup(const char *name);
199 extern int lxc_netdev_up(const char *name);
200 extern int lxc_netdev_down(const char *name);
201
202 /* Change the mtu size for the specified device. */
203 extern int lxc_netdev_set_mtu(const char *name, int mtu);
204
205 /* Create a virtual network devices. */
206 extern int lxc_veth_create(const char *name1, const char *name2);
207 extern int lxc_macvlan_create(const char *master, const char *name, int mode);
208 extern int lxc_vlan_create(const char *master, const char *name,
209 unsigned short vid);
210
211 /* Set ip address. */
212 extern int lxc_ipv6_addr_add(int ifindex, struct in6_addr *addr,
213 struct in6_addr *mcast,
214 struct in6_addr *acast, int prefix);
215
216 extern int lxc_ipv4_addr_add(int ifindex, struct in_addr *addr,
217 struct in_addr *bcast, int prefix);
218
219 /* Get ip address. */
220 extern int lxc_ipv4_addr_get(int ifindex, struct in_addr **res);
221 extern int lxc_ipv6_addr_get(int ifindex, struct in6_addr **res);
222
223 /* Set default route. */
224 extern int lxc_ipv4_gateway_add(int ifindex, struct in_addr *gw);
225 extern int lxc_ipv6_gateway_add(int ifindex, struct in6_addr *gw);
226
227 /* Attach an interface to the bridge. */
228 extern int lxc_bridge_attach(const char *bridge, const char *ifname);
229 extern int lxc_ovs_delete_port(const char *bridge, const char *nic);
230
231 extern bool is_ovs_bridge(const char *bridge);
232
233 /* Create default gateway. */
234 extern int lxc_route_create_default(const char *addr, const char *ifname,
235 int gateway);
236
237 /* Delete default gateway. */
238 extern int lxc_route_delete_default(const char *addr, const char *ifname,
239 int gateway);
240
241 /* Activate neighbor proxying. */
242 extern int lxc_neigh_proxy_on(const char *name, int family);
243
244 /* Disable neighbor proxying. */
245 extern int lxc_neigh_proxy_off(const char *name, int family);
246
247 /* Activate IP forwarding. */
248 extern int lxc_ip_forwarding_on(const char *name, int family);
249
250 /* Disable IP forwarding. */
251 extern int lxc_ip_forwarding_off(const char *name, int family);
252
253 /* Generate a new unique network interface name.
254 * Allocated memory must be freed by caller.
255 */
256 extern char *lxc_mkifname(char *template);
257
258 extern const char *lxc_net_type_to_str(int type);
259 extern int setup_private_host_hw_addr(char *veth1);
260 extern int netdev_get_mtu(int ifindex);
261 extern int lxc_network_move_created_netdev_priv(struct lxc_handler *handler);
262 extern void lxc_delete_network(struct lxc_handler *handler);
263 extern int lxc_find_gateway_addresses(struct lxc_handler *handler);
264 extern int lxc_requests_empty_network(struct lxc_handler *handler);
265 extern int lxc_restore_phys_nics_to_netns(struct lxc_handler *handler);
266 extern int lxc_setup_network_in_child_namespaces(const struct lxc_conf *conf,
267 struct lxc_list *network);
268 extern int lxc_network_send_to_child(struct lxc_handler *handler);
269 extern int lxc_network_recv_from_parent(struct lxc_handler *handler);
270 extern int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler *handler);
271 extern int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler *handler);
272 extern int lxc_netns_set_nsid(int netns_fd);
273 extern int lxc_netns_get_nsid(__s32 fd);
274 extern int lxc_create_network(struct lxc_handler *handler);
275
276 extern char *is_wlan(const char *ifname);
277 extern int lxc_netdev_move_wlan(char *physname, const char *ifname, pid_t pid,
278 const char *newname);
279
280 #endif /* __LXC_NETWORK_H */