* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#define _GNU_SOURCE
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
+#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/inotify.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+#include "../include/netns_ifaddrs.h"
#include "af_unix.h"
#include "conf.h"
#include "config.h"
#include "file_utils.h"
#include "log.h"
#include "macro.h"
+#include "memory_utils.h"
#include "network.h"
#include "nl.h"
+#include "raw_syscalls.h"
+#include "syscall_wrappers.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
#define PHYSNAME "/sys/class/net/%s/phy80211/name"
static char *is_wlan(const char *ifname)
{
+ __do_free char *path;
int i, ret;
long physlen;
size_t len;
- char *path;
FILE *f;
char *physname = NULL;
len = strlen(ifname) + strlen(PHYSNAME) - 1;
- path = alloca(len + 1);
+ path = must_realloc(NULL, len + 1);
ret = snprintf(path, len, PHYSNAME, ifname);
if (ret < 0 || (size_t)ret >= len)
goto bad;
if (err < 0)
goto out;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
+
do {
/* Restore the answer buffer length, it might have been
* overwritten by a previous receive.
}
} while (readmore);
+#pragma GCC diagnostic pop
+
/* If we end up here, we didn't find any result, so signal an error. */
err = -1;
static int neigh_proxy_set(const char *ifname, int family, int flag)
{
int ret;
- char path[MAXPATHLEN];
+ char path[PATH_MAX];
if (family != AF_INET && family != AF_INET6)
return -EINVAL;
- ret = snprintf(path, MAXPATHLEN, "/proc/sys/net/%s/conf/%s/%s",
+ ret = snprintf(path, PATH_MAX, "/proc/sys/net/%s/conf/%s/%s",
family == AF_INET ? "ipv4" : "ipv6", ifname,
family == AF_INET ? "proxy_arp" : "proxy_ndp");
- if (ret < 0 || (size_t)ret >= MAXPATHLEN)
+ if (ret < 0 || (size_t)ret >= PATH_MAX)
return -E2BIG;
return proc_sys_net_write(path, flag ? "1" : "0");
* the given RTM_NEWADDR message. Allocates memory for the address and stores
* that pointer in *res (so res should be an in_addr** or in6_addr**).
*/
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
+
static int ifa_get_local_ip(int family, struct nlmsghdr *msg, void **res)
{
int addrlen;
return 0;
}
+#pragma GCC diagnostic pop
+
static int ip_addr_get(int family, int ifindex, void **res)
{
int answer_len, err;
if (err < 0)
goto out;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
+
do {
/* Restore the answer buffer length, it might have been
* overwritten by a previous receive.
}
} while (readmore);
+#pragma GCC diagnostic pop
+
/* If we end up here, we didn't find any result, so signal an
* error.
*/
int lxc_ovs_delete_port(const char *bridge, const char *nic)
{
int ret;
- char cmd_output[MAXPATHLEN];
+ char cmd_output[PATH_MAX];
struct ovs_veth_args args;
args.bridge = bridge;
static int lxc_ovs_attach_bridge(const char *bridge, const char *nic)
{
int ret;
- char cmd_output[MAXPATHLEN];
+ char cmd_output[PATH_MAX];
struct ovs_veth_args args;
args.bridge = bridge;
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;
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;
}
/* Generate random names until we find one that doesn't exist. */
- while (true) {
+ for (;;) {
name[0] = '\0';
(void)strlcpy(name, template, IFNAMSIZ);
for (i = 0; i < strlen(name); i++) {
if (name[i] == 'X') {
#ifdef HAVE_RAND_R
- name[i] = padchar[rand_r(&seed) % (strlen(padchar) - 1)];
+ name[i] = padchar[rand_r(&seed) % strlen(padchar)];
#else
- name[i] = padchar[rand() % (strlen(padchar) - 1)];
+ name[i] = padchar[rand() % strlen(padchar)];
#endif
}
}
break;
}
- freeifaddrs(ifaddr);
+ netns_freeifaddrs(ifaddr);
(void)strlcpy(template, name, strlen(template) + 1);
return template;
int bytes, pipefd[2];
char *token, *saveptr = NULL;
char netdev_link[IFNAMSIZ];
- char buffer[MAXPATHLEN] = {0};
+ char buffer[PATH_MAX] = {0};
size_t retlen;
if (netdev->type != LXC_NET_VETH) {
}
if (child == 0) {
- int ret;
- size_t retlen;
char pidstr[INTTYPE_TO_STRLEN(pid_t)];
close(pipefd[0]);
/* close the write-end of the pipe */
close(pipefd[1]);
- bytes = lxc_read_nointr(pipefd[0], &buffer, MAXPATHLEN);
+ bytes = lxc_read_nointr(pipefd[0], &buffer, PATH_MAX);
if (bytes < 0) {
SYSERROR("Failed to read from pipe file descriptor");
close(pipefd[0]);
int bytes, ret;
pid_t child;
int pipefd[2];
- char buffer[MAXPATHLEN] = {0};
+ char buffer[PATH_MAX] = {0};
if (netdev->type != LXC_NET_VETH) {
ERROR("Network type %d not support for unprivileged use", netdev->type);
if (child == 0) {
char *hostveth;
- int ret;
close(pipefd[0]);
close(pipefd[1]);
- bytes = lxc_read_nointr(pipefd[0], &buffer, MAXPATHLEN);
+ bytes = lxc_read_nointr(pipefd[0], &buffer, PATH_MAX);
if (bytes < 0) {
SYSERROR("Failed to read from pipe file descriptor.");
close(pipefd[0]);
netdev->link);
clear_ifindices:
- /* We need to clear any ifindeces we recorded so liblxc won't
+ /* We need to clear any ifindices we recorded so liblxc won't
* have cached stale data which would cause it to fail on reboot
* we're we don't re-read the on-disk config file.
*/
netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex);
} else if (ret < 0) {
- errno = -ret;
- SYSWARN("Failed to remove interface \"%s\" with index %d",
- netdev->name[0] != '\0' ? netdev->name : "(null)",
- netdev->ifindex);
+ WARN("Failed to remove interface \"%s\" with index %d",
+ netdev->name[0] != '\0' ? netdev->name : "(null)",
+ netdev->ifindex);
goto clear_ifindices;
}
INFO("Removed interface \"%s\" with index %d",
ret = lxc_netdev_delete_by_name(hostveth);
if (ret < 0) {
- errno = -ret;
- SYSWARN("Failed to remove interface \"%s\" from \"%s\"",
- hostveth, netdev->link);
+ WARN("Failed to remove interface \"%s\" from \"%s\"",
+ hostveth, netdev->link);
goto clear_ifindices;
}
INFO("Removed interface \"%s\" from \"%s\"", hostveth, netdev->link);
hostveth, netdev->link);
clear_ifindices:
- /* We need to clear any ifindeces we recorded so liblxc won't
+ /* We need to clear any ifindices we recorded so liblxc won't
* have cached stale data which would cause it to fail on reboot
* we're we don't re-read the on-disk config file.
*/
/* set the network device up */
if (netdev->flags & IFF_UP) {
- int err;
-
err = lxc_netdev_up(current_ifname);
if (err) {
errno = -err;
return -1;
}
- TRACE("Sent network device names and ifindeces to parent");
+ TRACE("Sent network device names and ifindices to parent");
return 0;
}
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;
return -1;
memset(buf, 0, sizeof(buf));
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
hdr = (struct nlmsghdr *)buf;
msg = (struct rtgenmsg *)NLMSG_DATA(hdr);
+#pragma GCC diagnostic pop
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*msg));
hdr->nlmsg_type = RTM_NEWNSID;
if ((type <= max) && (!tb[type]))
tb[type] = rta;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
rta = RTA_NEXT(rta, len);
+#pragma GCC diagnostic pop
}
return 0;
return -1;
memset(buf, 0, sizeof(buf));
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
hdr = (struct nlmsghdr *)buf;
msg = (struct rtgenmsg *)NLMSG_DATA(hdr);
+#pragma GCC diagnostic pop
hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*msg));
hdr->nlmsg_type = RTM_GETNSID;
if (len < 0)
return -1;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-align"
parse_rtattr(tb, __LXC_NETNSA_MAX, NETNS_RTA(msg), len);
if (tb[__LXC_NETNSA_NSID])
return rta_getattr_s32(tb[__LXC_NETNSA_NSID]);
+#pragma GCC diagnostic pop
return -1;
}