Skip to content

Commit

Permalink
inet: Replace ioctl with rtnl use in connman_inet_add_to_bridge()
Browse files Browse the repository at this point in the history
[inet] Replace ioctl with rtnl use in connman_inet_add_to_bridge(). JB#62287

Use the internal rtnl helpers to construct the rtnl RTM_NEWLINK message
to set the bridge. This replaces ioctl() use to allow setting
IFLA_MASTER index.
  • Loading branch information
LaakkonenJussi committed Sep 20, 2024
1 parent 00a0fb1 commit f4be737
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions connman/src/inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,8 @@ int connman_inet_remove_from_bridge(int index, const char *bridge)
if (!bridge)
return -EINVAL;

DBG("index %d name %s", index, bridge);

sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sk < 0) {
err = -errno;
Expand All @@ -1411,32 +1413,40 @@ int connman_inet_remove_from_bridge(int index, const char *bridge)

int connman_inet_add_to_bridge(int index, const char *bridge)
{
struct ifreq ifr;
int sk, err = 0;
struct __connman_inet_rtnl_handle rth;
int bridge_index;
int err;

if (!bridge)
bridge_index = connman_inet_ifindex(bridge);
if (bridge_index < 0)
return -EINVAL;

sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sk < 0) {
err = -errno;
goto out;
}
DBG("bridge index %d", bridge_index);

memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name) - 1);
ifr.ifr_ifindex = index;
rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
rth.req.n.nlmsg_type = RTM_NEWLINK;

if (ioctl(sk, SIOCBRADDIF, &ifr) < 0)
err = -errno;
rth.req.u.f.ifi.ifi_family = AF_UNSPEC;
rth.req.u.f.ifi.ifi_index = index;

close(sk);
__connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req),
IFLA_MASTER, &bridge_index.
sizeof(int));

out:
if (err < 0)
err = __connman_inet_rtnl_open(&rth);
if (err)
goto done;

err = __connman_inet_rtnl_send(&rth, &rth.req.n);

done:
if (err)
connman_error("Add interface to bridge error %s",
strerror(-err));

__connman_inet_rtnl_close(&rth);

return err;
}

Expand Down

0 comments on commit f4be737

Please sign in to comment.