if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
ret = create_matched_ifnames(value, lxc_conf, netdev);
else
- ret = network_ifname(&netdev->link, value);
+ ret = network_ifname(netdev->link, value);
return ret;
}
if (!netdev)
return -1;
- return network_ifname(&netdev->name, value);
+ return network_ifname(netdev->name, value);
}
static int set_config_net_veth_pair(const char *key, const char *value,
if (!netdev)
return -1;
- return network_ifname(&netdev->priv.veth_attr.pair, value);
+ return network_ifname(netdev->priv.veth_attr.pair, value);
}
static int set_config_net_macvlan_mode(const char *key, const char *value,
if (!netdev)
return -1;
- free(netdev->name);
- netdev->name = NULL;
+ netdev->name[0] = '\0';
return 0;
}
if (!netdev)
return -1;
- free(netdev->link);
- netdev->link = NULL;
+ netdev->link[0] = '\0';
return 0;
}
if (!netdev)
return -1;
- free(netdev->priv.veth_attr.pair);
- netdev->priv.veth_attr.pair = NULL;
+ netdev->priv.veth_attr.pair[0] = '\0';
return 0;
}
if (!netdev)
return -1;
- if (netdev->link)
+ if (netdev->link[0] != '\0')
strprint(retv, inlen, "%s", netdev->link);
return fulllen;
if (!netdev)
return -1;
- if (netdev->name)
+ if (netdev->name[0] != '\0')
strprint(retv, inlen, "%s", netdev->name);
return fulllen;
return 0;
strprint(retv, inlen, "%s",
- netdev->priv.veth_attr.pair ? netdev->priv.veth_attr.pair
- : netdev->priv.veth_attr.veth1);
+ netdev->priv.veth_attr.pair[0] != '\0'
+ ? netdev->priv.veth_attr.pair
+ : netdev->priv.veth_attr.veth1);
return fulllen;
}
lxc_list_del(it);
- free(netdev->link);
- free(netdev->name);
- if (netdev->type == LXC_NET_VETH)
- free(netdev->priv.veth_attr.pair);
free(netdev->upscript);
free(netdev->downscript);
free(netdev->hwaddr);
lxc_list_init(&netdev->ipv4);
lxc_list_init(&netdev->ipv6);
+ netdev->name[0] = '\0';
+ netdev->link[0] = '\0';
+ memset(&netdev->priv, 0, sizeof(netdev->priv));
+ /* I'm not completely sure if the memset takes care to zero the arrays
+ * in the union as well. So let's make extra sure and set the first byte
+ * to zero so that we don't have any surprises.
+ */
+ netdev->priv.veth_attr.pair[0] = '\0';
+ netdev->priv.veth_attr.veth1[0] = '\0';
+
list = malloc(sizeof(*list));
if (!list) {
SYSERROR("failed to allocate memory");
free(it);
ret = create_matched_ifnames(value, lxc_conf, NULL);
} else {
- ret = network_ifname(&netdev->link, value);
+ ret = network_ifname(netdev->link, value);
}
return ret;
if (!netdev)
return -1;
- return network_ifname(&netdev->name, value);
+ return network_ifname(netdev->name, value);
}
int set_config_network_legacy_veth_pair(const char *key, const char *value,
return -1;
}
- return network_ifname(&netdev->priv.veth_attr.pair, value);
+ return network_ifname(netdev->priv.veth_attr.pair, value);
}
int set_config_network_legacy_macvlan_mode(const char *key, const char *value,
if (!netdev)
return -1;
if (strcmp(p1, "name") == 0) {
- if (netdev->name)
+ if (netdev->name[0] != '\0')
strprint(retv, inlen, "%s", netdev->name);
} else if (strcmp(p1, "type") == 0) {
strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
} else if (strcmp(p1, "link") == 0) {
- if (netdev->link)
+ if (netdev->link[0] != '\0')
strprint(retv, inlen, "%s", netdev->link);
} else if (strcmp(p1, "flags") == 0) {
if (netdev->flags & IFF_UP)
} else if (strcmp(p1, "veth.pair") == 0) {
if (netdev->type == LXC_NET_VETH) {
strprint(retv, inlen, "%s",
- netdev->priv.veth_attr.pair
+ netdev->priv.veth_attr.pair[0] != '\0'
? netdev->priv.veth_attr.pair
: netdev->priv.veth_attr.veth1);
}
memset(netdev, 0, sizeof(*netdev));
lxc_list_init(&netdev->ipv4);
lxc_list_init(&netdev->ipv6);
+ netdev->name[0] = '\0';
+ netdev->link[0] = '\0';
+ memset(&netdev->priv, 0, sizeof(netdev->priv));
+ /* I'm not completely sure if the memset takes care to zero the arrays
+ * in the union as well. So let's make extra sure and set the first byte
+ * to zero so that we don't have any surprises.
+ */
+ netdev->priv.veth_attr.pair[0] = '\0';
+ netdev->priv.veth_attr.veth1[0] = '\0';
/* give network a unique index */
netdev->idx = idx;
switch (netdev->type) {
case LXC_NET_VETH:
TRACE("type: veth");
- if (netdev->priv.veth_attr.pair)
+ if (netdev->priv.veth_attr.pair[0] != '\0')
TRACE("veth pair: %s",
netdev->priv.veth_attr.pair);
if (netdev->priv.veth_attr.veth1[0] != '\0')
if (netdev->type != LXC_NET_EMPTY) {
TRACE("flags: %s",
netdev->flags == IFF_UP ? "up" : "none");
- if (netdev->link)
+ if (netdev->link[0] != '\0')
TRACE("link: %s", netdev->link);
- if (netdev->name)
+ if (netdev->name[0] != '\0')
TRACE("name: %s", netdev->name);
if (netdev->hwaddr)
TRACE("hwaddr: %s", netdev->hwaddr);
{
struct lxc_list *cur, *next;
- free(netdev->link);
- free(netdev->name);
- if (netdev->type == LXC_NET_VETH)
- free(netdev->priv.veth_attr.pair);
free(netdev->upscript);
free(netdev->downscript);
free(netdev->hwaddr);
return 0;
}
-int network_ifname(char **valuep, const char *value)
+int network_ifname(char *valuep, const char *value)
{
- return set_config_string_item_max(valuep, value, IFNAMSIZ);
+ if (strlen(value) >= IFNAMSIZ) {
+ ERROR("Network devie name \"%s\" is too long (>= %zu)", value,
+ (size_t)IFNAMSIZ);
+ }
+
+ strcpy(valuep, value);
+ return 0;
}
int rand_complete_hwaddr(char *hwaddr)
size_t max);
extern int set_config_path_item(char **conf_item, const char *value);
extern int config_ip_prefix(struct in_addr *addr);
-extern int network_ifname(char **valuep, const char *value);
+extern int network_ifname(char *valuep, const char *value);
extern int rand_complete_hwaddr(char *hwaddr);
extern bool lxc_config_net_hwaddr(const char *line);
extern void update_hwaddr(const char *line);
case LXC_NET_VETH:
veth = n->priv.veth_attr.pair;
- if (n->link) {
+ if (n->link[0] != '\0') {
if (external_not_veth)
fmt = "veth[%s]:%s@%s";
else
goto err;
break;
case LXC_NET_MACVLAN:
- if (!n->link) {
+ if (n->link[0] == '\0') {
ERROR("no host interface for macvlan %s", n->name);
goto err;
}
snprintf(template, sizeof(template), "vethXXXXXX");
- if (!netdev->priv.veth_attr.pair)
- netdev->priv.veth_attr.pair = lxc_mkifname(template);
+ if (netdev->priv.veth_attr.pair[0] == '\0' &&
+ netdev->priv.veth_attr.veth1[0] == '\0') {
+ char *tmp;
- if (!netdev->priv.veth_attr.pair)
- goto out_unlock;
+ tmp = lxc_mkifname(template);
+ if (!tmp)
+ goto out_unlock;
+
+ strcpy(netdev->priv.veth_attr.veth1, tmp);
+ free(tmp);
+ }
}
has_error = false;
char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ];
unsigned int mtu = 0;
- if (netdev->priv.veth_attr.pair) {
+ if (netdev->priv.veth_attr.pair[0] != '\0') {
veth1 = netdev->priv.veth_attr.pair;
if (handler->conf->reboot)
lxc_netdev_delete_by_name(veth1);
WARN("Failed to parse mtu");
else
INFO("Retrieved mtu %d", mtu);
- } else if (netdev->link) {
+ } else if (netdev->link[0] != '\0') {
bridge_index = if_nametoindex(netdev->link);
if (bridge_index) {
mtu = netdev_get_mtu(bridge_index);
}
}
- if (netdev->link) {
+ if (netdev->link[0] != '\0') {
err = lxc_bridge_attach(netdev->link, veth1);
if (err) {
ERROR("Failed to attach \"%s\" to bridge \"%s\": %s",
out_delete:
if (netdev->ifindex != 0)
lxc_netdev_delete_by_name(veth1);
- if (!netdev->priv.veth_attr.pair)
+ if (netdev->priv.veth_attr.pair != veth1)
free(veth1);
free(veth2);
return -1;
char peerbuf[IFNAMSIZ], *peer;
int err;
- if (!netdev->link) {
+ if (netdev->link[0] == '\0') {
ERROR("No link for macvlan network device specified");
return -1;
}
static uint16_t vlan_cntr = 0;
unsigned int mtu = 0;
- if (!netdev->link) {
+ if (netdev->link[0] == '\0') {
ERROR("No link for vlan network device specified");
return -1;
}
if (lxc_safe_uint(netdev->mtu, &mtu) < 0) {
ERROR("Failed to retrieve mtu from \"%d\"/\"%s\".",
netdev->ifindex,
- netdev->name ? netdev->name : "(null)");
+ netdev->name[0] != '\0' ? netdev->name : "(null)");
return -1;
}
err = lxc_netdev_set_mtu(peer, mtu);
static int instantiate_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
{
- if (!netdev->link) {
+ if (netdev->link[0] == '\0') {
ERROR("No link for physical interface specified");
return -1;
}
char *veth1;
int err;
- if (netdev->priv.veth_attr.pair)
+ if (netdev->priv.veth_attr.pair[0] != '\0')
veth1 = netdev->priv.veth_attr.pair;
else
veth1 = netdev->priv.veth_attr.veth1;
return -1;
}
- if (!netdev->link) {
+ if (netdev->link[0] == '\0') {
ERROR("Automatic gateway detection needs a link interface");
return -1;
}
exit(EXIT_FAILURE);
}
- if (netdev->link)
+ if (netdev->link[0] != '\0')
strncpy(netdev_link, netdev->link, IFNAMSIZ);
else
strncpy(netdev_link, "none", IFNAMSIZ);
INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath,
lxcname, pidstr, netdev_link,
- netdev->name ? netdev->name : "(null)");
- if (netdev->name)
+ netdev->name[0] != '\0' ? netdev->name : "(null)");
+ if (netdev->name[0] != '\0')
execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create",
lxcpath, lxcname, pidstr, "veth", netdev_link,
netdev->name, (char *)NULL);
return -1;
}
- netdev->name = malloc(IFNAMSIZ + 1);
- if (!netdev->name) {
- SYSERROR("Failed to allocate memory");
- return -1;
- }
memset(netdev->name, 0, IFNAMSIZ + 1);
strncpy(netdev->name, token, IFNAMSIZ);
exit(EXIT_FAILURE);
}
- if (!netdev->link) {
+ if (netdev->link[0] == '\0') {
SYSERROR("Network link for network device \"%s\" is "
"missing", netdev->priv.veth_attr.veth1);
exit(EXIT_FAILURE);
}
DEBUG("Moved network device \"%s\"/\"%s\" to network namespace "
- "of %d:", ifname, netdev->name ? netdev->name : "(null)",
+ "of %d:", ifname, netdev->name[0] != '\0' ? netdev->name : "(null)",
pid);
}
INFO("Interface \"%s\" with index %d already "
"deleted or existing in different network "
"namespace",
- netdev->name ? netdev->name : "(null)",
+ netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex);
} else if (ret < 0) {
deleted_all = false;
WARN("Failed to remove interface \"%s\" with "
"index %d: %s",
- netdev->name ? netdev->name : "(null)",
+ netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex, strerror(-ret));
continue;
}
INFO("Removed interface \"%s\" with index %d",
- netdev->name ? netdev->name : "(null)",
+ netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex);
if (netdev->type != LXC_NET_VETH)
/* Explicitly delete host veth device to prevent lingering
* devices. We had issues in LXD around this.
*/
- if (netdev->priv.veth_attr.pair)
+ if (netdev->priv.veth_attr.pair[0] != '\0')
hostveth = netdev->priv.veth_attr.pair;
else
hostveth = netdev->priv.veth_attr.veth1;
- if (*hostveth == '\0')
+ if (hostveth[0] == '\0')
continue;
ret = lxc_netdev_delete_by_name(hostveth);
* When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
* netlink will replace the format specifier with an appropriate index.
*/
- if (!netdev->name)
- netdev->name = netdev->type == LXC_NET_PHYS ?
- netdev->link : "eth%d";
+ if (netdev->name[0] == '\0') {
+ if (netdev->type == LXC_NET_PHYS)
+ strcpy(netdev->name, netdev->link);
+ else
+ strcpy(netdev->name, "eth%d");
+ }
/* rename the interface name */
if (strcmp(ifname, netdev->name) != 0) {
* @ifindex : Ifindex of the network device.
*/
struct ifla_veth {
- char *pair;
+ char pair[IFNAMSIZ];
char veth1[IFNAMSIZ];
int ifindex;
};
int ifindex;
int type;
int flags;
- char *link;
- char *name;
+ char link[IFNAMSIZ];
+ char name[IFNAMSIZ];
char *hwaddr;
char *mtu;
union netdev_p priv;
if (netdev->type != LXC_NET_VETH)
continue;
- netdev->name = malloc(IFNAMSIZ);
- if (!netdev->name) {
- ERROR("Out of memory.");
- close(netpipe);
- return -1;
- }
-
if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
close(netpipe);
return -1;