]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/network.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <arpa/inet.h>
40 #include <net/if_arp.h>
41 #include <net/ethernet.h>
42 #include <netinet/in.h>
43 #include <linux/netlink.h>
44 #include <linux/rtnetlink.h>
45 #include <linux/sockios.h>
46 #include <linux/if_bridge.h>
51 # define IFLA_LINKMODE 17
55 # define IFLA_LINKINFO 18
58 #ifndef IFLA_NET_NS_PID
59 # define IFLA_NET_NS_PID 19
62 #ifndef IFLA_INFO_KIND
63 # define IFLA_INFO_KIND 1
67 # define IFLA_VLAN_ID 1
70 #ifndef IFLA_INFO_DATA
71 # define IFLA_INFO_DATA 2
74 #ifndef VETH_INFO_PEER
75 # define VETH_INFO_PEER 1
80 struct ifinfomsg ifinfomsg
;
88 int lxc_device_move(int ifindex
, pid_t pid
)
90 struct nl_handler nlh
;
91 struct nlmsg
*nlmsg
= NULL
;
92 struct link_req
*link_req
;
95 if (netlink_open(&nlh
, NETLINK_ROUTE
))
98 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
102 link_req
= (struct link_req
*)nlmsg
;
103 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
104 link_req
->ifinfomsg
.ifi_index
= ifindex
;
105 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
106 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
107 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
109 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
112 if (netlink_transaction(&nlh
, nlmsg
, nlmsg
))
122 extern int lxc_device_delete(const char *name
)
124 struct nl_handler nlh
;
125 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
126 struct link_req
*link_req
;
127 int index
, len
, err
= -1;
129 if (netlink_open(&nlh
, NETLINK_ROUTE
))
133 if (len
== 1 || len
> IFNAMSIZ
)
136 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
140 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
144 index
= if_nametoindex(name
);
148 link_req
= (struct link_req
*)nlmsg
;
149 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
150 link_req
->ifinfomsg
.ifi_index
= index
;
151 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
152 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_ACK
|NLM_F_REQUEST
;
153 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_DELLINK
;
155 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
158 if (netlink_transaction(&nlh
, nlmsg
, answer
))
169 static int device_set_flag(const char *name
, int flag
)
171 struct nl_handler nlh
;
172 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
173 struct link_req
*link_req
;
174 int index
, len
, err
= -1;
176 if (netlink_open(&nlh
, NETLINK_ROUTE
))
180 if (len
== 1 || len
> IFNAMSIZ
)
183 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
187 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
191 index
= if_nametoindex(name
);
195 link_req
= (struct link_req
*)nlmsg
;
196 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
197 link_req
->ifinfomsg
.ifi_index
= index
;
198 link_req
->ifinfomsg
.ifi_change
|= IFF_UP
;
199 link_req
->ifinfomsg
.ifi_flags
|= flag
;
200 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
201 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
202 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
204 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
216 extern int lxc_device_set_mtu(const char *name
, int mtu
)
218 struct nl_handler nlh
;
219 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
220 struct link_req
*link_req
;
221 int index
, len
, err
= -1;
223 if (netlink_open(&nlh
, NETLINK_ROUTE
))
227 if (len
== 1 || len
> IFNAMSIZ
)
230 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
234 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
238 index
= if_nametoindex(name
);
242 link_req
= (struct link_req
*)nlmsg
;
243 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
244 link_req
->ifinfomsg
.ifi_index
= index
;
245 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
246 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
247 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
249 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
252 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
264 int lxc_device_up(const char *name
)
266 return device_set_flag(name
, IFF_UP
);
269 int lxc_device_down(const char *name
)
271 return device_set_flag(name
, 0);
274 int lxc_device_rename(const char *oldname
, const char *newname
)
276 struct nl_handler nlh
;
277 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
278 struct link_req
*link_req
;
279 int index
, len
, err
= -1;
281 if (netlink_open(&nlh
, NETLINK_ROUTE
))
284 len
= strlen(oldname
);
285 if (len
== 1 || len
> IFNAMSIZ
)
288 len
= strlen(newname
);
289 if (len
== 1 || len
> IFNAMSIZ
)
292 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
296 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
300 index
= if_nametoindex(oldname
);
304 link_req
= (struct link_req
*)nlmsg
;
305 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
306 link_req
->ifinfomsg
.ifi_index
= index
;
307 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
308 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_ACK
|NLM_F_REQUEST
;
309 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
311 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
314 if (netlink_transaction(&nlh
, nlmsg
, answer
))
325 int lxc_veth_create(const char *name1
, const char *name2
)
327 struct nl_handler nlh
;
328 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
329 struct link_req
*link_req
;
330 struct rtattr
*nest1
, *nest2
, *nest3
;
333 if (netlink_open(&nlh
, NETLINK_ROUTE
))
337 if (len
== 1 || len
> IFNAMSIZ
)
341 if (len
== 1 || len
> IFNAMSIZ
)
344 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
348 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
352 link_req
= (struct link_req
*)nlmsg
;
353 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
354 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
355 nlmsg
->nlmsghdr
.nlmsg_flags
=
356 NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
|NLM_F_ACK
;
357 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
359 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
363 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
366 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
370 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
374 nlmsg
->nlmsghdr
.nlmsg_len
+= sizeof(struct ifinfomsg
);
376 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
379 nla_end_nested(nlmsg
, nest3
);
381 nla_end_nested(nlmsg
, nest2
);
383 nla_end_nested(nlmsg
, nest1
);
385 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
388 if (netlink_transaction(&nlh
, nlmsg
, answer
))
399 /* XXX: merge with lxc_macvlan_create */
400 int lxc_vlan_create(const char *master
, const char *name
, ushort vlanid
)
402 struct nl_handler nlh
;
403 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
404 struct link_req
*link_req
;
405 struct rtattr
*nest
, *nest2
;
406 int lindex
, len
, err
= -1;
408 if (netlink_open(&nlh
, NETLINK_ROUTE
))
411 len
= strlen(master
);
412 if (len
== 1 || len
> IFNAMSIZ
)
416 if (len
== 1 || len
> IFNAMSIZ
)
419 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
423 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
427 lindex
= if_nametoindex(master
);
431 link_req
= (struct link_req
*)nlmsg
;
432 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
433 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
434 nlmsg
->nlmsghdr
.nlmsg_flags
=
435 NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
|NLM_F_ACK
;
436 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
438 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
442 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
445 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
448 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
450 nla_end_nested(nlmsg
, nest2
);
452 nla_end_nested(nlmsg
, nest
);
454 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
457 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
460 if (netlink_transaction(&nlh
, nlmsg
, answer
))
473 int lxc_macvlan_create(const char *master
, const char *name
)
475 struct nl_handler nlh
;
476 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
477 struct link_req
*link_req
;
479 int index
, len
, err
= -1;
481 if (netlink_open(&nlh
, NETLINK_ROUTE
))
484 len
= strlen(master
);
485 if (len
== 1 || len
> IFNAMSIZ
)
489 if (len
== 1 || len
> IFNAMSIZ
)
492 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
496 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
500 index
= if_nametoindex(master
);
504 link_req
= (struct link_req
*)nlmsg
;
505 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
506 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
507 nlmsg
->nlmsghdr
.nlmsg_flags
=
508 NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
|NLM_F_ACK
;
509 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
511 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
515 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
518 nla_end_nested(nlmsg
, nest
);
520 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
523 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
526 if (netlink_transaction(&nlh
, nlmsg
, answer
))
537 static int proc_sys_net_write(const char *path
, const char *value
)
541 fd
= open(path
, O_WRONLY
);
545 if (write(fd
, value
, strlen(value
)) < 0)
552 static int ip_forward_set(const char *ifname
, int family
, int flag
)
554 char path
[MAXPATHLEN
];
556 if (family
!= AF_INET
&& family
!= AF_INET6
)
559 snprintf(path
, MAXPATHLEN
, "/proc/sys/net/%s/conf/%s/forwarding",
560 family
== AF_INET
?"ipv4":"ipv6" , ifname
);
562 return proc_sys_net_write(path
, flag
?"1":"0");
565 int lxc_ip_forward_on(const char *ifname
, int family
)
567 return ip_forward_set(ifname
, family
, 1);
570 int lxc_ip_forward_off(const char *ifname
, int family
)
572 return ip_forward_set(ifname
, family
, 0);
575 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
577 char path
[MAXPATHLEN
];
579 if (family
!= AF_INET
&& family
!= AF_INET6
)
582 sprintf(path
, "/proc/sys/net/%s/conf/%s/%s",
583 family
== AF_INET
?"ipv4":"ipv6" , ifname
,
584 family
== AF_INET
?"proxy_arp":"proxy_ndp");
586 return proc_sys_net_write(path
, flag
?"1":"0");
589 int lxc_neigh_proxy_on(const char *name
, int family
)
591 return neigh_proxy_set(name
, family
, 1);
594 int lxc_neigh_proxy_off(const char *name
, int family
)
596 return neigh_proxy_set(name
, family
, 0);
599 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
606 sockaddr
->sa_family
= ARPHRD_ETHER
;
607 data
= (unsigned char *)sockaddr
->sa_data
;
609 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
614 else if (c
>= 'a' && c
<= 'f')
616 else if (c
>= 'A' && c
<= 'F')
625 else if (c
>= 'a' && c
<= 'f')
627 else if (c
>= 'A' && c
<= 'F')
629 else if (c
== ':' || c
== 0)
636 *data
++ = (unsigned char) (val
& 0377);
646 int lxc_ip_addr_add(int family
, int ifindex
, void *addr
, int prefix
)
648 struct nl_handler nlh
;
649 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
650 struct ip_req
*ip_req
;
654 addrlen
= family
== AF_INET
? sizeof(struct in_addr
) :
655 sizeof(struct in6_addr
);
657 if (netlink_open(&nlh
, NETLINK_ROUTE
))
660 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
664 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
668 ip_req
= (struct ip_req
*)nlmsg
;
669 ip_req
->nlmsg
.nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
670 ip_req
->nlmsg
.nlmsghdr
.nlmsg_flags
=
671 NLM_F_ACK
|NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
;
672 ip_req
->nlmsg
.nlmsghdr
.nlmsg_type
= RTM_NEWADDR
;
673 ip_req
->ifa
.ifa_prefixlen
= prefix
;
674 ip_req
->ifa
.ifa_index
= ifindex
;
675 ip_req
->ifa
.ifa_family
= family
;
676 ip_req
->ifa
.ifa_scope
= 0;
678 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
681 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
684 /* if (in_bcast.s_addr != INADDR_ANY) */
685 /* if (nla_put_buffer(nlmsg, IFA_BROADCAST, &in_bcast, */
686 /* sizeof(in_bcast))) */
689 if (netlink_transaction(&nlh
, nlmsg
, answer
))
700 static int bridge_add_del_interface(const char *bridge
,
701 const char *ifname
, int detach
)
706 if (strlen(ifname
) > IFNAMSIZ
)
709 index
= if_nametoindex(ifname
);
713 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
717 strncpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
718 ifr
.ifr_ifindex
= index
;
719 err
= ioctl(fd
, detach
?SIOCBRDELIF
:SIOCBRADDIF
, &ifr
);
725 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
727 return bridge_add_del_interface(bridge
, ifname
, 0);
730 int lxc_bridge_detach(const char *bridge
, const char *ifname
)
732 return bridge_add_del_interface(bridge
, ifname
, 1);