network: use static memory for net device names

All network devices can only be of size < IFNAMSIZ. So let's spare the useless heap allocations and use static memory. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 9d967717
...@@ -764,11 +764,6 @@ static struct lxc_netdev *network_netdev(const char *key, const char *value, ...@@ -764,11 +764,6 @@ static struct lxc_netdev *network_netdev(const char *key, const char *value,
return netdev; return netdev;
} }
static int network_ifname(char **valuep, const char *value)
{
return set_config_string_item_max(valuep, value, IFNAMSIZ);
}
#ifndef MACVLAN_MODE_PRIVATE #ifndef MACVLAN_MODE_PRIVATE
#define MACVLAN_MODE_PRIVATE 1 #define MACVLAN_MODE_PRIVATE 1
#endif #endif
...@@ -870,7 +865,7 @@ static int set_config_network_link(const char *key, const char *value, ...@@ -870,7 +865,7 @@ static int set_config_network_link(const char *key, const char *value,
if (!netdev) if (!netdev)
return -1; return -1;
return network_ifname(&netdev->link, value); return network_ifname(netdev->link, value);
} }
static int set_config_network_name(const char *key, const char *value, static int set_config_network_name(const char *key, const char *value,
...@@ -882,7 +877,7 @@ static int set_config_network_name(const char *key, const char *value, ...@@ -882,7 +877,7 @@ static int set_config_network_name(const char *key, const char *value,
if (!netdev) if (!netdev)
return -1; return -1;
return network_ifname(&netdev->name, value); return network_ifname(netdev->name, value);
} }
static int set_config_network_veth_pair(const char *key, const char *value, static int set_config_network_veth_pair(const char *key, const char *value,
...@@ -899,7 +894,7 @@ static int set_config_network_veth_pair(const char *key, const char *value, ...@@ -899,7 +894,7 @@ static int set_config_network_veth_pair(const char *key, const char *value,
return -1; return -1;
} }
return network_ifname(&netdev->priv.veth_attr.pair, value); return network_ifname(netdev->priv.veth_attr.pair, value);
} }
static int set_config_network_macvlan_mode(const char *key, const char *value, static int set_config_network_macvlan_mode(const char *key, const char *value,
......
...@@ -305,3 +305,14 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf) ...@@ -305,3 +305,14 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
TRACE("downscript: %s", netdev->downscript); TRACE("downscript: %s", netdev->downscript);
} }
} }
int network_ifname(char *valuep, const char *value)
{
if (strlen(value) >= IFNAMSIZ) {
ERROR("Network devie name \"%s\" is too long (>= %zu)", value,
(size_t)IFNAMSIZ);
}
strcpy(valuep, value);
return 0;
}
...@@ -33,5 +33,6 @@ extern struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf, ...@@ -33,5 +33,6 @@ extern struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf,
extern struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf, extern struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx); unsigned int idx);
extern void lxc_log_configured_netdevs(const struct lxc_conf *conf); extern void lxc_log_configured_netdevs(const struct lxc_conf *conf);
extern int network_ifname(char *valuep, const char *value);
#endif /* __LXC_CONFILE_UTILS_H */ #endif /* __LXC_CONFILE_UTILS_H */
...@@ -523,7 +523,7 @@ static void exec_criu(struct criu_opts *opts) ...@@ -523,7 +523,7 @@ static void exec_criu(struct criu_opts *opts)
case LXC_NET_VETH: case LXC_NET_VETH:
veth = n->priv.veth_attr.pair; veth = n->priv.veth_attr.pair;
if (n->link) { if (n->link[0] != '\0') {
if (external_not_veth) if (external_not_veth)
fmt = "veth[%s]:%s@%s"; fmt = "veth[%s]:%s@%s";
else else
...@@ -542,7 +542,7 @@ static void exec_criu(struct criu_opts *opts) ...@@ -542,7 +542,7 @@ static void exec_criu(struct criu_opts *opts)
goto err; goto err;
break; break;
case LXC_NET_MACVLAN: case LXC_NET_MACVLAN:
if (!n->link) { if (n->link[0] == '\0') {
ERROR("no host interface for macvlan %s", n->name); ERROR("no host interface for macvlan %s", n->name);
goto err; goto err;
} }
...@@ -764,11 +764,17 @@ static bool restore_net_info(struct lxc_container *c) ...@@ -764,11 +764,17 @@ static bool restore_net_info(struct lxc_container *c)
snprintf(template, sizeof(template), "vethXXXXXX"); snprintf(template, sizeof(template), "vethXXXXXX");
if (!netdev->priv.veth_attr.pair) if (netdev->priv.veth_attr.pair[0] == '\0' &&
netdev->priv.veth_attr.pair = lxc_mkifname(template); netdev->priv.veth_attr.veth1[0] == '\0') {
char *tmp;
if (!netdev->priv.veth_attr.pair) tmp = lxc_mkifname(template);
if (!tmp)
goto out_unlock; goto out_unlock;
strcpy(netdev->priv.veth_attr.veth1, tmp);
free(tmp);
}
} }
has_error = false; has_error = false;
......
...@@ -101,7 +101,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -101,7 +101,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ]; char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ];
unsigned int mtu = 0; unsigned int mtu = 0;
if (netdev->priv.veth_attr.pair) { if (netdev->priv.veth_attr.pair[0] != '\0') {
veth1 = netdev->priv.veth_attr.pair; veth1 = netdev->priv.veth_attr.pair;
if (handler->conf->reboot) if (handler->conf->reboot)
lxc_netdev_delete_by_name(veth1); lxc_netdev_delete_by_name(veth1);
...@@ -163,7 +163,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -163,7 +163,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
WARN("Failed to parse mtu"); WARN("Failed to parse mtu");
else else
INFO("Retrieved mtu %d", mtu); INFO("Retrieved mtu %d", mtu);
} else if (netdev->link) { } else if (netdev->link[0] != '\0') {
bridge_index = if_nametoindex(netdev->link); bridge_index = if_nametoindex(netdev->link);
if (bridge_index) { if (bridge_index) {
mtu = netdev_get_mtu(bridge_index); mtu = netdev_get_mtu(bridge_index);
...@@ -186,7 +186,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -186,7 +186,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
} }
} }
if (netdev->link) { if (netdev->link[0] != '\0') {
err = lxc_bridge_attach(netdev->link, veth1); err = lxc_bridge_attach(netdev->link, veth1);
if (err) { if (err) {
ERROR("Failed to attach \"%s\" to bridge \"%s\": %s", ERROR("Failed to attach \"%s\" to bridge \"%s\": %s",
...@@ -217,7 +217,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -217,7 +217,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
out_delete: out_delete:
if (netdev->ifindex != 0) if (netdev->ifindex != 0)
lxc_netdev_delete_by_name(veth1); lxc_netdev_delete_by_name(veth1);
if (!netdev->priv.veth_attr.pair) if (netdev->priv.veth_attr.pair != veth1)
free(veth1); free(veth1);
free(veth2); free(veth2);
return -1; return -1;
...@@ -228,7 +228,7 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n ...@@ -228,7 +228,7 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
char peerbuf[IFNAMSIZ], *peer; char peerbuf[IFNAMSIZ], *peer;
int err; int err;
if (!netdev->link) { if (netdev->link[0] == '\0') {
ERROR("No link for macvlan network device specified"); ERROR("No link for macvlan network device specified");
return -1; return -1;
} }
...@@ -279,7 +279,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -279,7 +279,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
static uint16_t vlan_cntr = 0; static uint16_t vlan_cntr = 0;
unsigned int mtu = 0; unsigned int mtu = 0;
if (!netdev->link) { if (netdev->link[0] == '\0') {
ERROR("No link for vlan network device specified"); ERROR("No link for vlan network device specified");
return -1; return -1;
} }
...@@ -308,7 +308,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -308,7 +308,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
if (lxc_safe_uint(netdev->mtu, &mtu) < 0) { if (lxc_safe_uint(netdev->mtu, &mtu) < 0) {
ERROR("Failed to retrieve mtu from \"%d\"/\"%s\".", ERROR("Failed to retrieve mtu from \"%d\"/\"%s\".",
netdev->ifindex, netdev->ifindex,
netdev->name ? netdev->name : "(null)"); netdev->name[0] != '\0' ? netdev->name : "(null)");
return -1; return -1;
} }
err = lxc_netdev_set_mtu(peer, mtu); err = lxc_netdev_set_mtu(peer, mtu);
...@@ -325,7 +325,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -325,7 +325,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
static int instantiate_phys(struct lxc_handler *handler, struct lxc_netdev *netdev) 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"); ERROR("No link for physical interface specified");
return -1; return -1;
} }
...@@ -380,7 +380,7 @@ static int shutdown_veth(struct lxc_handler *handler, struct lxc_netdev *netdev) ...@@ -380,7 +380,7 @@ static int shutdown_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
char *veth1; char *veth1;
int err; int err;
if (netdev->priv.veth_attr.pair) if (netdev->priv.veth_attr.pair[0] != '\0')
veth1 = netdev->priv.veth_attr.pair; veth1 = netdev->priv.veth_attr.pair;
else else
veth1 = netdev->priv.veth_attr.veth1; veth1 = netdev->priv.veth_attr.veth1;
...@@ -1986,7 +1986,7 @@ int lxc_find_gateway_addresses(struct lxc_handler *handler) ...@@ -1986,7 +1986,7 @@ int lxc_find_gateway_addresses(struct lxc_handler *handler)
return -1; return -1;
} }
if (!netdev->link) { if (netdev->link[0] == '\0') {
ERROR("Automatic gateway detection needs a link interface"); ERROR("Automatic gateway detection needs a link interface");
return -1; return -1;
} }
...@@ -2060,7 +2060,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname, ...@@ -2060,7 +2060,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (netdev->link) if (netdev->link[0] != '\0')
strncpy(netdev_link, netdev->link, IFNAMSIZ); strncpy(netdev_link, netdev->link, IFNAMSIZ);
else else
strncpy(netdev_link, "none", IFNAMSIZ); strncpy(netdev_link, "none", IFNAMSIZ);
...@@ -2072,8 +2072,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname, ...@@ -2072,8 +2072,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath, INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath,
lxcname, pidstr, netdev_link, lxcname, pidstr, netdev_link,
netdev->name ? netdev->name : "(null)"); netdev->name[0] != '\0' ? netdev->name : "(null)");
if (netdev->name) if (netdev->name[0] != '\0')
execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create", execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create",
lxcpath, lxcname, pidstr, "veth", netdev_link, lxcpath, lxcname, pidstr, "veth", netdev_link,
netdev->name, (char *)NULL); netdev->name, (char *)NULL);
...@@ -2112,11 +2112,6 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname, ...@@ -2112,11 +2112,6 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
return -1; return -1;
} }
netdev->name = malloc(IFNAMSIZ + 1);
if (!netdev->name) {
SYSERROR("Failed to allocate memory");
return -1;
}
memset(netdev->name, 0, IFNAMSIZ + 1); memset(netdev->name, 0, IFNAMSIZ + 1);
strncpy(netdev->name, token, IFNAMSIZ); strncpy(netdev->name, token, IFNAMSIZ);
...@@ -2212,7 +2207,7 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, char *lxcname, ...@@ -2212,7 +2207,7 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, char *lxcname,
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!netdev->link) { if (netdev->link[0] == '\0') {
SYSERROR("Network link for network device \"%s\" is " SYSERROR("Network link for network device \"%s\" is "
"missing", netdev->priv.veth_attr.veth1); "missing", netdev->priv.veth_attr.veth1);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -2395,7 +2390,7 @@ int lxc_network_move_created_netdev_priv(const char *lxcpath, char *lxcname, ...@@ -2395,7 +2390,7 @@ int lxc_network_move_created_netdev_priv(const char *lxcpath, char *lxcname,
} }
DEBUG("Moved network device \"%s\"/\"%s\" to network namespace " 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); pid);
} }
...@@ -2482,18 +2477,18 @@ bool lxc_delete_network_priv(struct lxc_handler *handler) ...@@ -2482,18 +2477,18 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
INFO("Interface \"%s\" with index %d already " INFO("Interface \"%s\" with index %d already "
"deleted or existing in different network " "deleted or existing in different network "
"namespace", "namespace",
netdev->name ? netdev->name : "(null)", netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex); netdev->ifindex);
} else if (ret < 0) { } else if (ret < 0) {
deleted_all = false; deleted_all = false;
WARN("Failed to remove interface \"%s\" with " WARN("Failed to remove interface \"%s\" with "
"index %d: %s", "index %d: %s",
netdev->name ? netdev->name : "(null)", netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex, strerror(-ret)); netdev->ifindex, strerror(-ret));
continue; continue;
} }
INFO("Removed interface \"%s\" with index %d", INFO("Removed interface \"%s\" with index %d",
netdev->name ? netdev->name : "(null)", netdev->name[0] != '\0' ? netdev->name : "(null)",
netdev->ifindex); netdev->ifindex);
if (netdev->type != LXC_NET_VETH) if (netdev->type != LXC_NET_VETH)
...@@ -2502,11 +2497,11 @@ bool lxc_delete_network_priv(struct lxc_handler *handler) ...@@ -2502,11 +2497,11 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
/* Explicitly delete host veth device to prevent lingering /* Explicitly delete host veth device to prevent lingering
* devices. We had issues in LXD around this. * 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; hostveth = netdev->priv.veth_attr.pair;
else else
hostveth = netdev->priv.veth_attr.veth1; hostveth = netdev->priv.veth_attr.veth1;
if (*hostveth == '\0') if (hostveth[0] == '\0')
continue; continue;
ret = lxc_netdev_delete_by_name(hostveth); ret = lxc_netdev_delete_by_name(hostveth);
...@@ -2739,9 +2734,12 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev) ...@@ -2739,9 +2734,12 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev)
* When the IFLA_IFNAME attribute is passed something like "<prefix>%d" * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
* netlink will replace the format specifier with an appropriate index. * netlink will replace the format specifier with an appropriate index.
*/ */
if (!netdev->name) if (netdev->name[0] == '\0') {
netdev->name = netdev->type == LXC_NET_PHYS ? if (netdev->type == LXC_NET_PHYS)
netdev->link : "eth%d"; strcpy(netdev->name, netdev->link);
else
strcpy(netdev->name, "eth%d");
}
/* rename the interface name */ /* rename the interface name */
if (strcmp(ifname, netdev->name) != 0) { if (strcmp(ifname, netdev->name) != 0) {
......
...@@ -91,7 +91,7 @@ struct lxc_route6 { ...@@ -91,7 +91,7 @@ struct lxc_route6 {
* @ifindex : Ifindex of the network device. * @ifindex : Ifindex of the network device.
*/ */
struct ifla_veth { struct ifla_veth {
char *pair; char pair[IFNAMSIZ];
char veth1[IFNAMSIZ]; char veth1[IFNAMSIZ];
int ifindex; int ifindex;
}; };
...@@ -151,8 +151,8 @@ struct lxc_netdev { ...@@ -151,8 +151,8 @@ struct lxc_netdev {
int ifindex; int ifindex;
int type; int type;
int flags; int flags;
char *link; char link[IFNAMSIZ];
char *name; char name[IFNAMSIZ];
char *hwaddr; char *hwaddr;
char *mtu; char *mtu;
union netdev_p priv; union netdev_p priv;
......
...@@ -882,13 +882,6 @@ static int read_unpriv_netifindex(struct lxc_list *network) ...@@ -882,13 +882,6 @@ static int read_unpriv_netifindex(struct lxc_list *network)
if (netdev->type != LXC_NET_VETH) if (netdev->type != LXC_NET_VETH)
continue; continue;
netdev->name = malloc(IFNAMSIZ);
if (!netdev->name) {
ERROR("Out of memory.");
close(netpipe);
return -1;
}
if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) { if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
close(netpipe); close(netpipe);
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment