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