Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
36be8e6c
Unverified
Commit
36be8e6c
authored
Sep 24, 2018
by
Stéphane Graber
Committed by
GitHub
Sep 24, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2640 from brauner/2018-09-23/netns_getifaddrs
network: add netns_getifaddrs() implementation
parents
7e270c97
61204b93
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
93 additions
and
85 deletions
+93
-85
netns_ifaddrs.c
src/include/netns_ifaddrs.c
+0
-0
netns_ifaddrs.h
src/include/netns_ifaddrs.h
+28
-9
Makefile.am
src/lxc/Makefile.am
+3
-8
confile.c
src/lxc/confile.c
+4
-9
lxccontainer.c
src/lxc/lxccontainer.c
+7
-12
macro.h
src/lxc/macro.h
+21
-1
network.c
src/lxc/network.c
+4
-38
nl.c
src/lxc/nl.c
+19
-0
nl.h
src/lxc/nl.h
+4
-0
lxc_device.c
src/lxc/tools/lxc_device.c
+3
-8
No files found.
src/include/ifaddrs.c
→
src/include/
netns_
ifaddrs.c
View file @
36be8e6c
This diff is collapsed.
Click to expand it.
src/include/ifaddrs.h
→
src/include/
netns_
ifaddrs.h
View file @
36be8e6c
#ifndef _IFADDRS_H
#define _IFADDRS_H
#ifndef _
LXC_NETNS_
IFADDRS_H
#define _
LXC_NETNS_
IFADDRS_H
#ifdef __cplusplus
extern
"C"
{
#endif
#include <features.h>
#include <linux/types.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <sys/socket.h>
struct
ifaddrs
{
struct
ifaddrs
*
ifa_next
;
struct
netns_ifaddrs
{
struct
netns_ifaddrs
*
ifa_next
;
/* Can - but shouldn't be - NULL. */
char
*
ifa_name
;
/* This field is not present struct ifaddrs. */
int
ifa_ifindex
;
unsigned
ifa_flags
;
/* This field is not present struct ifaddrs. */
int
ifa_mtu
;
/* This field is not present struct ifaddrs. */
int
ifa_prefixlen
;
struct
sockaddr
*
ifa_addr
;
struct
sockaddr
*
ifa_netmask
;
union
{
struct
sockaddr
*
ifu_broadaddr
;
struct
sockaddr
*
ifu_dstaddr
;
}
ifa_ifu
;
/* If you don't know what this is for don't touch it. */
void
*
ifa_data
;
};
#define ifa_broadaddr ifa_ifu.ifu_broadaddr
#define ifa_dstaddr ifa_ifu.ifu_dstaddr
void
freeifaddrs
(
struct
ifaddrs
*
);
int
getifaddrs
(
struct
ifaddrs
**
);
#define __ifa_broadaddr ifa_ifu.ifu_broadaddr
#define __ifa_dstaddr ifa_ifu.ifu_dstaddr
extern
void
netns_freeifaddrs
(
struct
netns_ifaddrs
*
);
extern
int
netns_getifaddrs
(
struct
netns_ifaddrs
**
ifap
,
__s32
netns_id
,
bool
*
netnsid_aware
);
#ifdef __cplusplus
}
#endif
#endif
#endif
/* _LXC_NETNS_IFADDRS_H */
src/lxc/Makefile.am
View file @
36be8e6c
...
...
@@ -14,6 +14,7 @@ noinst_HEADERS = api_extensions.h \
criu.h
\
error.h
\
file_utils.h
\
../include/netns_ifaddrs.h
\
initutils.h
\
list.h
\
log.h
\
...
...
@@ -41,10 +42,6 @@ noinst_HEADERS = api_extensions.h \
tools/arguments.h
\
utils.h
if
!HAVE_IFADDRS_H
noinst_HEADERS
+=
../include/ifaddrs.h
endif
if
IS_BIONIC
noinst_HEADERS
+=
../include/lxcmntent.h
\
../include/openpty.h
...
...
@@ -103,6 +100,7 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \
execute.c
\
freezer.c
\
file_utils.c file_utils.h
\
../include/netns_ifaddrs.c ../include/netns_ifaddrs.h
\
initutils.c initutils.h
\
list.h
\
log.c log.h
\
...
...
@@ -139,10 +137,6 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \
version.h
\
$(LSM_SOURCES)
if
!HAVE_IFADDRS_H
liblxc_la_SOURCES
+=
../include/ifaddrs.c ../include/ifaddrs.h
endif
if
IS_BIONIC
liblxc_la_SOURCES
+=
../include/lxcmntent.c ../include/lxcmntent.h
\
../include/openpty.c ../include/openpty.h
...
...
@@ -347,6 +341,7 @@ init_lxc_SOURCES = cmd/lxc_init.c \
string_utils.c string_utils.h
lxc_monitord_SOURCES
=
cmd/lxc_monitord.c
lxc_user_nic_SOURCES
=
cmd/lxc_user_nic.c
\
../include/netns_ifaddrs.c ../include/netns_ifaddrs.h
\
log.c log.h
\
namespace.c namespace.h
\
network.c network.h
\
...
...
src/lxc/confile.c
View file @
36be8e6c
...
...
@@ -48,6 +48,7 @@
#include "config.h"
#include "confile.h"
#include "confile_utils.h"
#include <../include/netns_ifaddrs.h>
#include "log.h"
#include "lxcseccomp.h"
#include "network.h"
...
...
@@ -55,12 +56,6 @@
#include "storage.h"
#include "utils.h"
#if HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include <../include/ifaddrs.h>
#endif
#if HAVE_SYS_PERSONALITY_H
#include <sys/personality.h>
#endif
...
...
@@ -324,14 +319,14 @@ static int set_config_net_flags(const char *key, const char *value,
static
int
create_matched_ifnames
(
const
char
*
value
,
struct
lxc_conf
*
lxc_conf
,
struct
lxc_netdev
*
netdev
)
{
struct
ifaddrs
*
ifaddr
,
*
ifa
;
struct
netns_
ifaddrs
*
ifaddr
,
*
ifa
;
int
n
;
int
ret
=
0
;
const
char
*
type_key
=
"lxc.net.type"
;
const
char
*
link_key
=
"lxc.net.link"
;
const
char
*
tmpvalue
=
"phys"
;
if
(
getifaddrs
(
&
ifaddr
)
==
-
1
)
{
if
(
netns_getifaddrs
(
&
ifaddr
,
-
1
,
&
(
bool
){
false
})
<
0
)
{
SYSERROR
(
"Get network interfaces failed"
);
return
-
1
;
}
...
...
@@ -359,7 +354,7 @@ static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
}
}
freeifaddrs
(
ifaddr
);
netns_
freeifaddrs
(
ifaddr
);
ifaddr
=
NULL
;
return
ret
;
...
...
src/lxc/lxccontainer.c
View file @
36be8e6c
...
...
@@ -53,6 +53,7 @@
#include "confile_utils.h"
#include "criu.h"
#include "error.h"
#include <../include/netns_ifaddrs.h>
#include "initutils.h"
#include "log.h"
#include "lxc.h"
...
...
@@ -78,12 +79,6 @@
#include <sys/mkdev.h>
#endif
#if HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include <../include/ifaddrs.h>
#endif
#if IS_BIONIC
#include <../include/lxcmntent.h>
#else
...
...
@@ -2327,7 +2322,7 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
if
(
pid
==
0
)
{
/* child */
int
ret
=
1
,
nbytes
;
struct
ifaddrs
*
interfaceArray
=
NULL
,
*
tempIfAddr
=
NULL
;
struct
netns_
ifaddrs
*
interfaceArray
=
NULL
,
*
tempIfAddr
=
NULL
;
/* close the read-end of the pipe */
close
(
pipefd
[
0
]);
...
...
@@ -2338,7 +2333,7 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
}
/* Grab the list of interfaces */
if
(
getifaddrs
(
&
interfaceArray
))
{
if
(
netns_getifaddrs
(
&
interfaceArray
,
-
1
,
&
(
bool
){
false
}
))
{
SYSERROR
(
"Failed to get interfaces list"
);
goto
out
;
}
...
...
@@ -2357,7 +2352,7 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
out
:
if
(
interfaceArray
)
freeifaddrs
(
interfaceArray
);
netns_
freeifaddrs
(
interfaceArray
);
/* close the write-end of the pipe, thus sending EOF to the reader */
close
(
pipefd
[
1
]);
...
...
@@ -2429,7 +2424,7 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
int
ret
=
1
;
char
*
address
=
NULL
;
void
*
tempAddrPtr
=
NULL
;
struct
ifaddrs
*
interfaceArray
=
NULL
,
*
tempIfAddr
=
NULL
;
struct
netns_
ifaddrs
*
interfaceArray
=
NULL
,
*
tempIfAddr
=
NULL
;
/* close the read-end of the pipe */
close
(
pipefd
[
0
]);
...
...
@@ -2440,7 +2435,7 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
}
/* Grab the list of interfaces */
if
(
getifaddrs
(
&
interfaceArray
))
{
if
(
netns_getifaddrs
(
&
interfaceArray
,
-
1
,
&
(
bool
){
false
}
))
{
SYSERROR
(
"Failed to get interfaces list"
);
goto
out
;
}
...
...
@@ -2496,7 +2491,7 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
out
:
if
(
interfaceArray
)
freeifaddrs
(
interfaceArray
);
netns_
freeifaddrs
(
interfaceArray
);
/* close the write-end of the pipe, thus sending EOF to the reader */
close
(
pipefd
[
1
]);
...
...
src/lxc/macro.h
View file @
36be8e6c
...
...
@@ -272,10 +272,20 @@ extern int __build_bug_on_failed;
#define IFLA_NEW_NETNSID 45
#endif
#ifndef IFLA_IF_NETNSID
#ifdef IFLA_IF_NETNSID
#ifndef IFLA_TARGET_NETNSID
#define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID
#endif
#else
#define IFLA_IF_NETNSID 46
#define IFLA_TARGET_NETNSID 46
#endif
#ifndef IFA_TARGET_NETNSID
#define IFA_TARGET_NETNSID 10
#endif
#ifndef RTM_NEWNSID
#define RTM_NEWNSID 88
#endif
...
...
@@ -304,6 +314,16 @@ extern int __build_bug_on_failed;
#define MACVLAN_MODE_PASSTHRU 8
#endif
/* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
enum
{
__LXC_NETNSA_NONE
,
#define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
__LXC_NETNSA_NSID
,
__LXC_NETNSA_PID
,
__LXC_NETNSA_FD
,
__LXC_NETNSA_MAX
,
};
/* Length of abstract unix domain socket socket address. */
#define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
...
...
src/lxc/network.c
View file @
36be8e6c
...
...
@@ -48,6 +48,7 @@
#include "af_unix.h"
#include "conf.h"
#include "config.h"
#include <../include/netns_ifaddrs.h>
#include "file_utils.h"
#include "log.h"
#include "macro.h"
...
...
@@ -55,12 +56,6 @@
#include "nl.h"
#include "utils.h"
#if HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include <../include/ifaddrs.h>
#endif
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
...
...
@@ -1950,7 +1945,7 @@ static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char
*
lxc_mkifname
(
char
*
template
)
{
int
ret
;
struct
ifaddrs
*
ifa
,
*
ifaddr
;
struct
netns_
ifaddrs
*
ifa
,
*
ifaddr
;
char
name
[
IFNAMSIZ
];
bool
exists
=
false
;
size_t
i
=
0
;
...
...
@@ -1967,7 +1962,7 @@ char *lxc_mkifname(char *template)
return
NULL
;
/* Get all the network interfaces. */
ret
=
getifaddrs
(
&
ifaddr
);
ret
=
netns_getifaddrs
(
&
ifaddr
,
-
1
,
&
(
bool
){
false
}
);
if
(
ret
<
0
)
{
SYSERROR
(
"Failed to get network interfaces"
);
return
NULL
;
...
...
@@ -2001,7 +1996,7 @@ char *lxc_mkifname(char *template)
break
;
}
freeifaddrs
(
ifaddr
);
netns_
freeifaddrs
(
ifaddr
);
(
void
)
strlcpy
(
template
,
name
,
strlen
(
template
)
+
1
);
return
template
;
...
...
@@ -3181,35 +3176,6 @@ void lxc_delete_network(struct lxc_handler *handler)
DEBUG
(
"Deleted network devices"
);
}
int
addattr
(
struct
nlmsghdr
*
n
,
size_t
maxlen
,
int
type
,
const
void
*
data
,
size_t
alen
)
{
int
len
=
RTA_LENGTH
(
alen
);
struct
rtattr
*
rta
;
errno
=
EMSGSIZE
;
if
(
NLMSG_ALIGN
(
n
->
nlmsg_len
)
+
RTA_ALIGN
(
len
)
>
maxlen
)
return
-
1
;
rta
=
NLMSG_TAIL
(
n
);
rta
->
rta_type
=
type
;
rta
->
rta_len
=
len
;
if
(
alen
)
memcpy
(
RTA_DATA
(
rta
),
data
,
alen
);
n
->
nlmsg_len
=
NLMSG_ALIGN
(
n
->
nlmsg_len
)
+
RTA_ALIGN
(
len
);
return
0
;
}
/* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
enum
{
__LXC_NETNSA_NONE
,
#define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
__LXC_NETNSA_NSID
,
__LXC_NETNSA_PID
,
__LXC_NETNSA_FD
,
__LXC_NETNSA_MAX
,
};
int
lxc_netns_set_nsid
(
int
fd
)
{
int
ret
;
...
...
src/lxc/nl.c
View file @
36be8e6c
...
...
@@ -345,3 +345,22 @@ extern int netlink_close(struct nl_handler *handler)
return
0
;
}
int
addattr
(
struct
nlmsghdr
*
n
,
size_t
maxlen
,
int
type
,
const
void
*
data
,
size_t
alen
)
{
int
len
=
RTA_LENGTH
(
alen
);
struct
rtattr
*
rta
;
errno
=
EMSGSIZE
;
if
(
NLMSG_ALIGN
(
n
->
nlmsg_len
)
+
RTA_ALIGN
(
len
)
>
maxlen
)
return
-
1
;
rta
=
NLMSG_TAIL
(
n
);
rta
->
rta_type
=
type
;
rta
->
rta_len
=
len
;
if
(
alen
)
memcpy
(
RTA_DATA
(
rta
),
data
,
alen
);
n
->
nlmsg_len
=
NLMSG_ALIGN
(
n
->
nlmsg_len
)
+
RTA_ALIGN
(
len
);
return
0
;
}
src/lxc/nl.h
View file @
36be8e6c
...
...
@@ -23,6 +23,8 @@
#ifndef __LXC_NL_H
#define __LXC_NL_H
#include <stdio.h>
/*
* Use this as a good size to allocate generic netlink messages
*/
...
...
@@ -259,5 +261,7 @@ void nlmsg_free(struct nlmsg *nlmsg);
*/
void
*
nlmsg_data
(
struct
nlmsg
*
nlmsg
);
extern
int
addattr
(
struct
nlmsghdr
*
n
,
size_t
maxlen
,
int
type
,
const
void
*
data
,
size_t
alen
);
#endif
src/lxc/tools/lxc_device.c
View file @
36be8e6c
...
...
@@ -30,15 +30,10 @@
#include <lxc/lxccontainer.h>
#include "arguments.h"
#include "../../include/netns_ifaddrs.h"
#include "log.h"
#include "utils.h"
#if HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include "../include/ifaddrs.h"
#endif
lxc_log_define
(
lxc_device
,
lxc
);
static
bool
is_interface
(
const
char
*
dev_name
,
pid_t
pid
);
...
...
@@ -73,7 +68,7 @@ static bool is_interface(const char *dev_name, pid_t pid)
}
if
(
p
==
0
)
{
struct
ifaddrs
*
interfaceArray
=
NULL
,
*
tempIfAddr
=
NULL
;
struct
netns_
ifaddrs
*
interfaceArray
=
NULL
,
*
tempIfAddr
=
NULL
;
if
(
!
switch_to_ns
(
pid
,
"net"
))
{
ERROR
(
"Failed to enter netns of container"
);
...
...
@@ -81,7 +76,7 @@ static bool is_interface(const char *dev_name, pid_t pid)
}
/* Grab the list of interfaces */
if
(
getifaddrs
(
&
interfaceArray
))
{
if
(
netns_getifaddrs
(
&
interfaceArray
,
-
1
,
&
(
bool
){
false
}
))
{
ERROR
(
"Failed to get interfaces list"
);
_exit
(
-
1
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment