]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/network.h
network: Adds support host side veth device static routes
[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_PHYS,
44 LXC_NET_VLAN,
45 LXC_NET_NONE,
46 LXC_NET_MAXCONFTYPE,
47 };
48
49 /*
50 * Defines the structure to configure an ipv4 address
51 * @address : ipv4 address
52 * @broadcast : ipv4 broadcast address
53 * @mask : network mask
54 */
55 struct lxc_inetdev {
56 struct in_addr addr;
57 struct in_addr bcast;
58 unsigned int prefix;
59 };
60
61 struct lxc_route {
62 struct in_addr addr;
63 };
64
65 /*
66 * Defines the structure to configure an ipv6 address
67 * @flags : set the address up
68 * @address : ipv6 address
69 * @broadcast : ipv6 broadcast address
70 * @mask : network mask
71 */
72 struct lxc_inet6dev {
73 struct in6_addr addr;
74 struct in6_addr mcast;
75 struct in6_addr acast;
76 unsigned int prefix;
77 };
78
79 struct lxc_route6 {
80 struct in6_addr addr;
81 };
82
83 /* Contains information about the host side veth device.
84 * @pair : Name of the host side veth device.
85 * If the user requested that the host veth device be created with a
86 * specific names this field will be set. If this field is set @veth1
87 * is not set.
88 * @veth1 : Name of the host side veth device.
89 * If the user did not request that the host veth device be created
90 * with a specific name this field will be set. If this field is set
91 * @pair is not set.
92 * @ifindex : Ifindex of the network device.
93 */
94 struct ifla_veth {
95 char pair[IFNAMSIZ];
96 char veth1[IFNAMSIZ];
97 int ifindex;
98 struct lxc_list ipv4_routes;
99 struct lxc_list ipv6_routes;
100 };
101
102 struct ifla_vlan {
103 unsigned int flags;
104 unsigned int fmask;
105 unsigned short vid;
106 unsigned short pad;
107 };
108
109 struct ifla_macvlan {
110 int mode; /* private, vepa, bridge, passthru */
111 };
112
113 /* Contains information about the physical network device as seen from the host.
114 * @ifindex : The ifindex of the physical network device in the host's network
115 * namespace.
116 */
117 struct ifla_phys {
118 int ifindex;
119 };
120
121 union netdev_p {
122 struct ifla_macvlan macvlan_attr;
123 struct ifla_phys phys_attr;
124 struct ifla_veth veth_attr;
125 struct ifla_vlan vlan_attr;
126 };
127
128 /*
129 * Defines a structure to configure a network device
130 * @idx : network counter
131 * @ifindex : ifindex of the network device
132 * Note that this is the ifindex of the network device in
133 * the container's network namespace. If the network device
134 * consists of a pair of network devices (e.g. veth pairs
135 * attached to a network bridge) then this index cannot be
136 * used to identify or modify the host veth device. See
137 * struct ifla_veth for the host side information.
138 * @type : network type (veth, macvlan, vlan, ...)
139 * @flags : flag of the network device (IFF_UP, ... )
140 * @link : lxc.net.[i].link, name of bridge or host iface to attach
141 * if any
142 * @name : lxc.net.[i].name, name of iface on the container side
143 * @hwaddr : mac address
144 * @mtu : maximum transmission unit
145 * @priv : information specific to the specificed network type
146 * Note that this is a union so whether accessing a struct
147 * is possible is dependent on the network type.
148 * @ipv4 : a list of ipv4 addresses to be set on the network device
149 * @ipv6 : a list of ipv6 addresses to be set on the network device
150 * @ipv4_gateway_auto : whether the ipv4 gateway is to be automatically gathered
151 * from the associated @link
152 * @ipv4_gateway : ipv4 gateway
153 * @ipv6_gateway_auto : whether the ipv6 gateway is to be automatically gathered
154 * from the associated @link
155 * @ipv6_gateway : ipv6 gateway
156 * @upscript : a script filename to be executed during interface
157 * configuration
158 * @downscript : a script filename to be executed during interface
159 * destruction
160 */
161 struct lxc_netdev {
162 ssize_t idx;
163 int ifindex;
164 int type;
165 int flags;
166 char link[IFNAMSIZ];
167 char name[IFNAMSIZ];
168 char *hwaddr;
169 char *mtu;
170 union netdev_p priv;
171 struct lxc_list ipv4;
172 struct lxc_list ipv6;
173 bool ipv4_gateway_auto;
174 struct in_addr *ipv4_gateway;
175 bool ipv6_gateway_auto;
176 struct in6_addr *ipv6_gateway;
177 char *upscript;
178 char *downscript;
179 };
180
181 /* Convert a string mac address to a socket structure. */
182 extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr);
183
184 /* Move a device between namespaces. */
185 extern int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char *ifname);
186 extern int lxc_netdev_move_by_name(const char *ifname, pid_t pid,
187 const char *newname);
188
189 /* Delete a network device. */
190 extern int lxc_netdev_delete_by_name(const char *name);
191 extern int lxc_netdev_delete_by_index(int ifindex);
192
193 /* Change the device name. */
194 extern int lxc_netdev_rename_by_name(const char *oldname, const char *newname);
195 extern int lxc_netdev_rename_by_index(int ifindex, const char *newname);
196
197 extern int netdev_set_flag(const char *name, int flag);
198
199 /* Set the device network up or down. */
200 extern int lxc_netdev_isup(const char *name);
201 extern int lxc_netdev_up(const char *name);
202 extern int lxc_netdev_down(const char *name);
203
204 /* Change the mtu size for the specified device. */
205 extern int lxc_netdev_set_mtu(const char *name, int mtu);
206
207 /* Create a virtual network devices. */
208 extern int lxc_veth_create(const char *name1, const char *name2);
209 extern int lxc_macvlan_create(const char *master, const char *name, int mode);
210 extern int lxc_vlan_create(const char *master, const char *name,
211 unsigned short vid);
212
213 /* Set ip address. */
214 extern int lxc_ipv6_addr_add(int ifindex, struct in6_addr *addr,
215 struct in6_addr *mcast,
216 struct in6_addr *acast, int prefix);
217
218 extern int lxc_ipv4_addr_add(int ifindex, struct in_addr *addr,
219 struct in_addr *bcast, int prefix);
220
221 /* Get ip address. */
222 extern int lxc_ipv4_addr_get(int ifindex, struct in_addr **res);
223 extern int lxc_ipv6_addr_get(int ifindex, struct in6_addr **res);
224
225 /* Set a destination route to an interface. */
226 extern int lxc_ipv4_dest_add(int ifindex, struct in_addr *dest, unsigned int netmask);
227 extern int lxc_ipv6_dest_add(int ifindex, struct in6_addr *dest, unsigned int netmask);
228
229 /* Set default route. */
230 extern int lxc_ipv4_gateway_add(int ifindex, struct in_addr *gw);
231 extern int lxc_ipv6_gateway_add(int ifindex, struct in6_addr *gw);
232
233 /* Attach an interface to the bridge. */
234 extern int lxc_bridge_attach(const char *bridge, const char *ifname);
235 extern int lxc_ovs_delete_port(const char *bridge, const char *nic);
236
237 extern bool is_ovs_bridge(const char *bridge);
238
239 /* Create default gateway. */
240 extern int lxc_route_create_default(const char *addr, const char *ifname,
241 int gateway);
242
243 /* Delete default gateway. */
244 extern int lxc_route_delete_default(const char *addr, const char *ifname,
245 int gateway);
246
247 /* Activate neighbor proxying. */
248 extern int lxc_neigh_proxy_on(const char *name, int family);
249
250 /* Disable neighbor proxying. */
251 extern int lxc_neigh_proxy_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_create_network_priv(struct lxc_handler *handler);
262 extern int lxc_network_move_created_netdev_priv(const char *lxcpath,
263 const char *lxcname,
264 struct lxc_list *network,
265 pid_t pid);
266 extern void lxc_delete_network(struct lxc_handler *handler);
267 extern int lxc_find_gateway_addresses(struct lxc_handler *handler);
268 extern int lxc_create_network_unpriv(const char *lxcpath, const char *lxcname,
269 struct lxc_list *network, pid_t pid, unsigned int hook_version);
270 extern int lxc_requests_empty_network(struct lxc_handler *handler);
271 extern int lxc_restore_phys_nics_to_netns(struct lxc_handler *handler);
272 extern int lxc_setup_network_in_child_namespaces(const struct lxc_conf *conf,
273 struct lxc_list *network);
274 extern int lxc_network_send_veth_names_to_child(struct lxc_handler *handler);
275 extern int lxc_network_recv_veth_names_from_parent(struct lxc_handler *handler);
276 extern int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler *handler);
277 extern int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler *handler);
278 extern int lxc_netns_set_nsid(int netns_fd);
279 extern int lxc_netns_get_nsid(__s32 fd);
280
281 #endif /* __LXC_NETWORK_H */