]>
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 <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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>
56 #include <../include/ifaddrs.h>
60 # define IFLA_LINKMODE 17
64 # define IFLA_LINKINFO 18
67 #ifndef IFLA_NET_NS_PID
68 # define IFLA_NET_NS_PID 19
71 #ifndef IFLA_INFO_KIND
72 # define IFLA_INFO_KIND 1
76 # define IFLA_VLAN_ID 1
79 #ifndef IFLA_INFO_DATA
80 # define IFLA_INFO_DATA 2
83 #ifndef VETH_INFO_PEER
84 # define VETH_INFO_PEER 1
87 #ifndef IFLA_MACVLAN_MODE
88 # define IFLA_MACVLAN_MODE 1
93 struct ifinfomsg ifinfomsg
;
106 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
)
108 struct nl_handler nlh
;
109 struct nlmsg
*nlmsg
= NULL
;
110 struct link_req
*link_req
;
113 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
118 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
122 link_req
= (struct link_req
*)nlmsg
;
123 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
124 link_req
->ifinfomsg
.ifi_index
= ifindex
;
125 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
126 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
127 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
129 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
132 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
139 int lxc_netdev_move_by_name(char *ifname
, pid_t pid
)
143 index
= if_nametoindex(ifname
);
147 return lxc_netdev_move_by_index(index
, pid
);
150 int lxc_netdev_delete_by_index(int ifindex
)
152 struct nl_handler nlh
;
153 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
154 struct link_req
*link_req
;
157 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
162 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
166 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
170 link_req
= (struct link_req
*)nlmsg
;
171 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
172 link_req
->ifinfomsg
.ifi_index
= ifindex
;
173 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
174 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_ACK
|NLM_F_REQUEST
;
175 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_DELLINK
;
177 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
185 int lxc_netdev_delete_by_name(const char *name
)
189 index
= if_nametoindex(name
);
193 return lxc_netdev_delete_by_index(index
);
196 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
198 struct nl_handler nlh
;
199 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
200 struct link_req
*link_req
;
203 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
207 len
= strlen(newname
);
208 if (len
== 1 || len
>= IFNAMSIZ
)
212 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
216 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
220 link_req
= (struct link_req
*)nlmsg
;
221 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
222 link_req
->ifinfomsg
.ifi_index
= ifindex
;
223 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
224 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_ACK
|NLM_F_REQUEST
;
225 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
227 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
230 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
238 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
242 len
= strlen(oldname
);
243 if (len
== 1 || len
>= IFNAMSIZ
)
246 index
= if_nametoindex(oldname
);
250 return lxc_netdev_rename_by_index(index
, newname
);
253 int netdev_set_flag(const char *name
, int flag
)
255 struct nl_handler nlh
;
256 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
257 struct link_req
*link_req
;
260 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
266 if (len
== 1 || len
>= IFNAMSIZ
)
270 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
274 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
279 index
= if_nametoindex(name
);
283 link_req
= (struct link_req
*)nlmsg
;
284 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
285 link_req
->ifinfomsg
.ifi_index
= index
;
286 link_req
->ifinfomsg
.ifi_change
|= IFF_UP
;
287 link_req
->ifinfomsg
.ifi_flags
|= flag
;
288 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
289 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
290 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
292 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
300 int lxc_netdev_set_mtu(const char *name
, int mtu
)
302 struct nl_handler nlh
;
303 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
304 struct link_req
*link_req
;
307 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
313 if (len
== 1 || len
>= IFNAMSIZ
)
317 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
321 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
326 index
= if_nametoindex(name
);
330 link_req
= (struct link_req
*)nlmsg
;
331 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
332 link_req
->ifinfomsg
.ifi_index
= index
;
333 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
334 nlmsg
->nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
335 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
337 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
340 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
348 int lxc_netdev_up(const char *name
)
350 return netdev_set_flag(name
, IFF_UP
);
353 int lxc_netdev_down(const char *name
)
355 return netdev_set_flag(name
, 0);
358 int lxc_veth_create(const char *name1
, const char *name2
)
360 struct nl_handler nlh
;
361 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
362 struct link_req
*link_req
;
363 struct rtattr
*nest1
, *nest2
, *nest3
;
366 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
372 if (len
== 1 || len
>= IFNAMSIZ
)
376 if (len
== 1 || len
>= IFNAMSIZ
)
380 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
384 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
388 link_req
= (struct link_req
*)nlmsg
;
389 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
390 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
391 nlmsg
->nlmsghdr
.nlmsg_flags
=
392 NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
|NLM_F_ACK
;
393 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
396 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
400 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
403 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
407 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
411 nlmsg
->nlmsghdr
.nlmsg_len
+= sizeof(struct ifinfomsg
);
413 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
416 nla_end_nested(nlmsg
, nest3
);
418 nla_end_nested(nlmsg
, nest2
);
420 nla_end_nested(nlmsg
, nest1
);
422 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
425 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
433 /* XXX: merge with lxc_macvlan_create */
434 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
436 struct nl_handler nlh
;
437 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
438 struct link_req
*link_req
;
439 struct rtattr
*nest
, *nest2
;
440 int lindex
, len
, err
;
442 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
447 len
= strlen(master
);
448 if (len
== 1 || len
>= IFNAMSIZ
)
452 if (len
== 1 || len
>= IFNAMSIZ
)
456 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
460 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
465 lindex
= if_nametoindex(master
);
469 link_req
= (struct link_req
*)nlmsg
;
470 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
471 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
472 nlmsg
->nlmsghdr
.nlmsg_flags
=
473 NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
|NLM_F_ACK
;
474 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
476 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
480 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
483 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
487 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
490 nla_end_nested(nlmsg
, nest2
);
492 nla_end_nested(nlmsg
, nest
);
494 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
497 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
500 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
510 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
512 struct nl_handler nlh
;
513 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
514 struct link_req
*link_req
;
515 struct rtattr
*nest
, *nest2
;
518 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
523 len
= strlen(master
);
524 if (len
== 1 || len
>= IFNAMSIZ
)
528 if (len
== 1 || len
>= IFNAMSIZ
)
532 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
536 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
541 index
= if_nametoindex(master
);
545 link_req
= (struct link_req
*)nlmsg
;
546 link_req
->ifinfomsg
.ifi_family
= AF_UNSPEC
;
547 nlmsg
->nlmsghdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
548 nlmsg
->nlmsghdr
.nlmsg_flags
=
549 NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
|NLM_F_ACK
;
550 nlmsg
->nlmsghdr
.nlmsg_type
= RTM_NEWLINK
;
552 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
556 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
560 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
564 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
567 nla_end_nested(nlmsg
, nest2
);
570 nla_end_nested(nlmsg
, nest
);
572 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
575 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
578 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
586 static int proc_sys_net_write(const char *path
, const char *value
)
591 fd
= open(path
, O_WRONLY
);
596 if (write(fd
, value
, strlen(value
)) < 0)
605 static int ip_forward_set(const char *ifname
, int family
, int flag
)
607 char path
[MAXPATHLEN
];
610 if (family
!= AF_INET
&& family
!= AF_INET6
)
613 rc
= snprintf(path
, MAXPATHLEN
, "/proc/sys/net/%s/conf/%s/forwarding",
614 family
== AF_INET
?"ipv4":"ipv6" , ifname
);
615 if (rc
>= MAXPATHLEN
)
618 return proc_sys_net_write(path
, flag
?"1":"0");
621 int lxc_ip_forward_on(const char *ifname
, int family
)
623 return ip_forward_set(ifname
, family
, 1);
626 int lxc_ip_forward_off(const char *ifname
, int family
)
628 return ip_forward_set(ifname
, family
, 0);
631 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
633 char path
[MAXPATHLEN
];
636 if (family
!= AF_INET
&& family
!= AF_INET6
)
639 ret
= snprintf(path
, MAXPATHLEN
, "/proc/sys/net/%s/conf/%s/%s",
640 family
== AF_INET
?"ipv4":"ipv6" , ifname
,
641 family
== AF_INET
?"proxy_arp":"proxy_ndp");
642 if (ret
< 0 || ret
>= MAXPATHLEN
)
645 return proc_sys_net_write(path
, flag
?"1":"0");
648 int lxc_neigh_proxy_on(const char *name
, int family
)
650 return neigh_proxy_set(name
, family
, 1);
653 int lxc_neigh_proxy_off(const char *name
, int family
)
655 return neigh_proxy_set(name
, family
, 0);
658 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
665 sockaddr
->sa_family
= ARPHRD_ETHER
;
666 data
= (unsigned char *)sockaddr
->sa_data
;
668 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
673 else if (c
>= 'a' && c
<= 'f')
675 else if (c
>= 'A' && c
<= 'F')
684 else if (c
>= 'a' && c
<= 'f')
686 else if (c
>= 'A' && c
<= 'F')
688 else if (c
== ':' || c
== 0)
695 *data
++ = (unsigned char) (val
& 0377);
705 static int ip_addr_add(int family
, int ifindex
,
706 void *addr
, void *bcast
, void *acast
, int prefix
)
708 struct nl_handler nlh
;
709 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
710 struct ip_req
*ip_req
;
714 addrlen
= family
== AF_INET
? sizeof(struct in_addr
) :
715 sizeof(struct in6_addr
);
717 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
722 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
726 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
730 ip_req
= (struct ip_req
*)nlmsg
;
731 ip_req
->nlmsg
.nlmsghdr
.nlmsg_len
=
732 NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
733 ip_req
->nlmsg
.nlmsghdr
.nlmsg_flags
=
734 NLM_F_ACK
|NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
;
735 ip_req
->nlmsg
.nlmsghdr
.nlmsg_type
= RTM_NEWADDR
;
736 ip_req
->ifa
.ifa_prefixlen
= prefix
;
737 ip_req
->ifa
.ifa_index
= ifindex
;
738 ip_req
->ifa
.ifa_family
= family
;
739 ip_req
->ifa
.ifa_scope
= 0;
742 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
745 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
748 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
751 /* TODO : multicast, anycast with ipv6 */
752 err
= -EPROTONOSUPPORT
;
753 if (family
== AF_INET6
&&
754 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
755 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
758 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
766 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
767 struct in6_addr
*mcast
,
768 struct in6_addr
*acast
, int prefix
)
770 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
773 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
,
774 struct in_addr
*bcast
, int prefix
)
776 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
779 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present)
780 * address from the given RTM_NEWADDR message. Allocates memory for the
781 * address and stores that pointer in *res (so res should be an
782 * in_addr** or in6_addr**).
784 static int ifa_get_local_ip(int family
, struct ip_req
*ip_info
, void** res
) {
785 struct rtattr
*rta
= IFA_RTA(&ip_info
->ifa
);
786 int attr_len
= IFA_PAYLOAD(&ip_info
->nlmsg
.nlmsghdr
);
789 if (ip_info
->ifa
.ifa_family
!= family
)
792 addrlen
= family
== AF_INET
? sizeof(struct in_addr
) :
793 sizeof(struct in6_addr
);
795 /* Loop over the rtattr's in this message */
796 while(RTA_OK(rta
, attr_len
)) {
797 /* Found a local address for the requested interface,
799 if (rta
->rta_type
== IFA_LOCAL
|| rta
->rta_type
== IFA_ADDRESS
) {
800 /* Sanity check. The family check above should
801 * make sure the address length is correct, but
802 * check here just in case */
803 if (RTA_PAYLOAD(rta
) != addrlen
)
806 /* We might have found an IFA_ADDRESS before,
807 * which we now overwrite with an IFA_LOCAL. */
809 *res
= malloc(addrlen
);
814 memcpy(*res
, RTA_DATA(rta
), addrlen
);
816 if (rta
->rta_type
== IFA_LOCAL
)
819 rta
= RTA_NEXT(rta
, attr_len
);
824 static int ip_addr_get(int family
, int ifindex
, void **res
)
826 struct nl_handler nlh
;
827 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
828 struct ip_req
*ip_req
, *ip_info
;
829 struct nlmsghdr
*msg
;
831 int recv_len
= 0, answer_len
;
834 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
839 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
843 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
847 /* Save the answer buffer length, since it will be overwritten
848 * on the first receive (and we might need to receive more than
850 answer_len
= answer
->nlmsghdr
.nlmsg_len
;
852 ip_req
= (struct ip_req
*)nlmsg
;
853 ip_req
->nlmsg
.nlmsghdr
.nlmsg_len
=
854 NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
855 ip_req
->nlmsg
.nlmsghdr
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ROOT
;
856 ip_req
->nlmsg
.nlmsghdr
.nlmsg_type
= RTM_GETADDR
;
857 ip_req
->ifa
.ifa_family
= family
;
859 /* Send the request for addresses, which returns all addresses
860 * on all interfaces. */
861 err
= netlink_send(&nlh
, nlmsg
);
866 /* Restore the answer buffer length, it might have been
867 * overwritten by a previous receive. */
868 answer
->nlmsghdr
.nlmsg_len
= answer_len
;
870 /* Get the (next) batch of reply messages */
871 err
= netlink_rcv(&nlh
, answer
);
878 /* Satisfy the typing for the netlink macros */
879 msg
= &answer
->nlmsghdr
;
881 while (NLMSG_OK(msg
, recv_len
)) {
882 /* Stop reading if we see an error message */
883 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
884 struct nlmsgerr
*errmsg
= (struct nlmsgerr
*)NLMSG_DATA(msg
);
889 /* Stop reading if we see a NLMSG_DONE message */
890 if (msg
->nlmsg_type
== NLMSG_DONE
) {
895 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
900 ip_info
= (struct ip_req
*)msg
;
901 if (ip_info
->ifa
.ifa_index
== ifindex
) {
902 if (ifa_get_local_ip(family
, ip_info
, res
) < 0) {
907 /* Found a result, stop searching */
912 /* Keep reading more data from the socket if the
913 * last message had the NLF_F_MULTI flag set */
914 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
916 /* Look at the next message received in this buffer */
917 msg
= NLMSG_NEXT(msg
, recv_len
);
921 /* If we end up here, we didn't find any result, so signal an
932 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
934 return ip_addr_get(AF_INET6
, ifindex
, (void**)res
);
937 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
** res
)
939 return ip_addr_get(AF_INET
, ifindex
, (void**)res
);
942 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
944 struct nl_handler nlh
;
945 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
946 struct rt_req
*rt_req
;
950 addrlen
= family
== AF_INET
? sizeof(struct in_addr
) :
951 sizeof(struct in6_addr
);
953 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
958 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
962 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
966 rt_req
= (struct rt_req
*)nlmsg
;
967 rt_req
->nlmsg
.nlmsghdr
.nlmsg_len
=
968 NLMSG_LENGTH(sizeof(struct rtmsg
));
969 rt_req
->nlmsg
.nlmsghdr
.nlmsg_flags
=
970 NLM_F_ACK
|NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
;
971 rt_req
->nlmsg
.nlmsghdr
.nlmsg_type
= RTM_NEWROUTE
;
972 rt_req
->rt
.rtm_family
= family
;
973 rt_req
->rt
.rtm_table
= RT_TABLE_MAIN
;
974 rt_req
->rt
.rtm_scope
= RT_SCOPE_UNIVERSE
;
975 rt_req
->rt
.rtm_protocol
= RTPROT_BOOT
;
976 rt_req
->rt
.rtm_type
= RTN_UNICAST
;
977 /* "default" destination */
978 rt_req
->rt
.rtm_dst_len
= 0;
981 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
984 /* Adding the interface index enables the use of link-local
985 * addresses for the gateway */
986 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
989 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
997 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
999 return ip_gateway_add(AF_INET
, ifindex
, gw
);
1002 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
1004 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
1007 static int ip_route_dest_add(int family
, int ifindex
, void *dest
)
1009 struct nl_handler nlh
;
1010 struct nlmsg
*nlmsg
= NULL
, *answer
= NULL
;
1011 struct rt_req
*rt_req
;
1015 addrlen
= family
== AF_INET
? sizeof(struct in_addr
) :
1016 sizeof(struct in6_addr
);
1018 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1023 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1027 answer
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1031 rt_req
= (struct rt_req
*)nlmsg
;
1032 rt_req
->nlmsg
.nlmsghdr
.nlmsg_len
=
1033 NLMSG_LENGTH(sizeof(struct rtmsg
));
1034 rt_req
->nlmsg
.nlmsghdr
.nlmsg_flags
=
1035 NLM_F_ACK
|NLM_F_REQUEST
|NLM_F_CREATE
|NLM_F_EXCL
;
1036 rt_req
->nlmsg
.nlmsghdr
.nlmsg_type
= RTM_NEWROUTE
;
1037 rt_req
->rt
.rtm_family
= family
;
1038 rt_req
->rt
.rtm_table
= RT_TABLE_MAIN
;
1039 rt_req
->rt
.rtm_scope
= RT_SCOPE_LINK
;
1040 rt_req
->rt
.rtm_protocol
= RTPROT_BOOT
;
1041 rt_req
->rt
.rtm_type
= RTN_UNICAST
;
1042 rt_req
->rt
.rtm_dst_len
= addrlen
*8;
1045 if (nla_put_buffer(nlmsg
, RTA_DST
, dest
, addrlen
))
1047 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
1049 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1051 netlink_close(&nlh
);
1057 int lxc_ipv4_dest_add(int ifindex
, struct in_addr
*dest
)
1059 return ip_route_dest_add(AF_INET
, ifindex
, dest
);
1062 int lxc_ipv6_dest_add(int ifindex
, struct in6_addr
*dest
)
1064 return ip_route_dest_add(AF_INET6
, ifindex
, dest
);
1068 * There is a lxc_bridge_attach, but no need of a bridge detach
1069 * as automatically done by kernel when a netdev is deleted.
1071 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
1076 if (strlen(ifname
) >= IFNAMSIZ
)
1079 index
= if_nametoindex(ifname
);
1084 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1089 strncpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
-1);
1090 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1091 ifr
.ifr_ifindex
= index
;
1092 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
1102 static char* lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
1103 [LXC_NET_VETH
] = "veth",
1104 [LXC_NET_MACVLAN
] = "macvlan",
1105 [LXC_NET_VLAN
] = "vlan",
1106 [LXC_NET_PHYS
] = "phys",
1107 [LXC_NET_EMPTY
] = "empty",
1110 const char *lxc_net_type_to_str(int type
)
1112 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
1114 return lxc_network_types
[type
];
1117 static char padchar
[] =
1118 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1120 char *lxc_mkifname(char *template)
1126 struct ifaddrs
*ifaddr
, *ifa
;
1129 /* Get all the network interfaces */
1130 getifaddrs(&ifaddr
);
1132 /* Initialize the random number generator */
1134 urandom
= fopen ("/dev/urandom", "r");
1136 if (urandom
!= NULL
) {
1137 if (fread (&seed
, sizeof(seed
), 1, urandom
) <= 0)
1150 /* Generate random names until we find one that doesn't exist */
1153 name
= strdup(template);
1158 for (i
= 0; i
< strlen(name
); i
++) {
1159 if (name
[i
] == 'X') {
1161 name
[i
] = padchar
[rand_r(&seed
) % (strlen(padchar
) - 1)];
1163 name
[i
] = padchar
[rand() % (strlen(padchar
) - 1)];
1168 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
1169 if (strcmp(ifa
->ifa_name
, name
) == 0) {
1181 freeifaddrs(ifaddr
);
1185 int setup_private_host_hw_addr(char *veth1
)
1192 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1197 snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
1198 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
1206 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
1207 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);