diff options
author | Hans Dedecker <dedeckeh@gmail.com> | 2018-08-12 22:08:22 +0200 |
---|---|---|
committer | Hans Dedecker <dedeckeh@gmail.com> | 2018-08-14 17:06:38 +0200 |
commit | 522456b9f3ab07a78de17bf693abead4a296b028 (patch) | |
tree | 5e749e5e2aa3c4a3e77a0546d5148961398bccf6 /vlan.c | |
parent | 115a694ab9a44cd52a8ab18e07909c753de9fb94 (diff) |
device: gracefully handle device names exceeding IFNAMESIZ
Instead of truncating the device name when it exceeds IFNAMSIZ length;
let device_set_ifname return an error code and do not add the device
to the device list.
This avoids possible issues with device names becoming identical due the
truncation and as a result unexpected behavior.
Further let the different device types gracefully handle the error code
returned by device_init
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Diffstat (limited to 'vlan.c')
-rw-r--r-- | vlan.c | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -61,7 +61,7 @@ static int vlan_set_device_state(struct device *dev, bool up) return ret; } -static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) +static int vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) { char *name; @@ -69,7 +69,7 @@ static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) vldev->dev.hidden = dev->hidden; sprintf(name, "%s.%d", dev->ifname, vldev->id); - device_set_ifname(&vldev->dev, name); + return device_set_ifname(&vldev->dev, name); } static void vlan_dev_cb(struct device_user *dep, enum device_event ev) @@ -85,7 +85,8 @@ static void vlan_dev_cb(struct device_user *dep, enum device_event ev) device_set_present(&vldev->dev, false); break; case DEV_EVENT_UPDATE_IFNAME: - vlan_dev_set_name(vldev, dep->dev); + if (vlan_dev_set_name(vldev, dep->dev) < 0) + free_vlan_if(&vldev->dev); break; case DEV_EVENT_TOPO_CHANGE: /* Propagate topo changes */ @@ -106,9 +107,6 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) struct vlan_device *vldev; struct device_user *dep; - if (strlen(dev->ifname) > (IFNAMSIZ - 6)) - return NULL; - /* look for an existing interface before creating a new one */ list_for_each_entry(dep, &dev->users.list, list.list) { if (dep->cb != vlan_dev_cb) @@ -132,9 +130,12 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) vldev->id = id; - device_init(&vldev->dev, &vlan_type, NULL); + if (device_init(&vldev->dev, &vlan_type, NULL) < 0) + goto error; + + if (vlan_dev_set_name(vldev, dev) < 0) + goto error; - vlan_dev_set_name(vldev, dev); vldev->dev.default_config = true; vldev->set_state = vldev->dev.set_state; @@ -144,6 +145,11 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) device_add_user(&vldev->dep, dev); return &vldev->dev; + +error: + device_cleanup(&vldev->dev); + free(vldev); + return NULL; } static char *split_vlan(char *s) |