]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/network.c
network: do not log false friends
[mirror_lxc.git] / src / lxc / network.c
index 72a3d6b7ab544f4bcc53c9a3ba6ab1a9a66710d1..fd7d867fc58a74edafac5bc5026432c519244a56 100644 (file)
  * 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
@@ -550,15 +550,15 @@ out:
 #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;
@@ -962,6 +962,9 @@ int netdev_get_mtu(int ifindex)
        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.
@@ -1024,6 +1027,8 @@ int netdev_get_mtu(int ifindex)
                }
        } while (readmore);
 
+#pragma GCC diagnostic pop
+
        /* If we end up here, we didn't find any result, so signal an error. */
        err = -1;
 
@@ -1354,15 +1359,15 @@ static int proc_sys_net_write(const char *path, const char *value)
 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");
@@ -1501,6 +1506,9 @@ int lxc_ipv4_addr_add(int ifindex, struct in_addr *addr, struct in_addr *bcast,
  * 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;
@@ -1546,6 +1554,8 @@ static int ifa_get_local_ip(int family, struct nlmsghdr *msg, void **res)
        return 0;
 }
 
+#pragma GCC diagnostic pop
+
 static int ip_addr_get(int family, int ifindex, void **res)
 {
        int answer_len, err;
@@ -1588,6 +1598,9 @@ static int ip_addr_get(int family, int ifindex, void **res)
        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.
@@ -1647,6 +1660,8 @@ static int ip_addr_get(int family, int ifindex, void **res)
                }
        } while (readmore);
 
+#pragma GCC diagnostic pop
+
        /* If we end up here, we didn't find any result, so signal an
         * error.
         */
@@ -1833,7 +1848,7 @@ static int lxc_ovs_delete_port_exec(void *data)
 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;
@@ -1861,7 +1876,7 @@ static int lxc_ovs_attach_bridge_exec(void *data)
 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;
@@ -1935,7 +1950,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;
@@ -1952,14 +1967,14 @@ 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;
        }
 
        /* Generate random names until we find one that doesn't exist. */
-       while (true) {
+       for (;;) {
                name[0] = '\0';
                (void)strlcpy(name, template, IFNAMSIZ);
 
@@ -1968,9 +1983,9 @@ char *lxc_mkifname(char *template)
                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
                        }
                }
@@ -1986,7 +2001,7 @@ char *lxc_mkifname(char *template)
                        break;
        }
 
-       freeifaddrs(ifaddr);
+       netns_freeifaddrs(ifaddr);
        (void)strlcpy(template, name, strlen(template) + 1);
 
        return template;
@@ -2079,7 +2094,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
        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) {
@@ -2102,8 +2117,6 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
        }
 
        if (child == 0) {
-               int ret;
-               size_t retlen;
                char pidstr[INTTYPE_TO_STRLEN(pid_t)];
 
                close(pipefd[0]);
@@ -2149,7 +2162,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcna
        /* 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]);
@@ -2243,7 +2256,7 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, const char *lxcna
        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);
@@ -2266,7 +2279,6 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, const char *lxcna
 
        if (child == 0) {
                char *hostveth;
-               int ret;
 
                close(pipefd[0]);
 
@@ -2305,7 +2317,7 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, const char *lxcna
 
        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]);
@@ -2407,7 +2419,7 @@ bool lxc_delete_network_unpriv(struct lxc_handler *handler)
                     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.
                 */
@@ -2569,10 +2581,9 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
                             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",
@@ -2594,9 +2605,8 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
 
                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);
@@ -2618,7 +2628,7 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
                             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.
                 */
@@ -2911,8 +2921,6 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev)
 
        /* set the network device up */
        if (netdev->flags & IFF_UP) {
-               int err;
-
                err = lxc_netdev_up(current_ifname);
                if (err) {
                        errno = -err;
@@ -3117,7 +3125,7 @@ int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler *handler)
                        return -1;
        }
 
-       TRACE("Sent network device names and ifindeces to parent");
+       TRACE("Sent network device names and ifindices to parent");
        return 0;
 }
 
@@ -3166,35 +3174,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;
@@ -3213,8 +3192,12 @@ int lxc_netns_set_nsid(int fd)
                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;
@@ -3252,7 +3235,10 @@ static int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int le
                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;
@@ -3287,8 +3273,12 @@ int lxc_netns_get_nsid(int fd)
                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;
@@ -3313,9 +3303,12 @@ int lxc_netns_get_nsid(int fd)
        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;
 }