net/if: Drop ifmediareq and just use the regular ifreq for SIOCGIFMEDIA.

This was introduced into the main API in 2010 (d72ede75fb),
but was actually only fully used for the past month (c2a9a890f3)
when SIOCGIFMEDIA was supported for all *BSD drivers and not just WiFi.
Most userland consumers of this structure did not use it correctly,
as was the case in #17770, and only worked because in the fallback case
the network stack just treated it as if it were an ifreq.

Nothing actually used the ifm_count/ifm_ulist (though tentative APIs
were exposed for it) as noted by previous commits; and the fact that
Haiku's IFM_* declarations are so spartan makes most of the returned
values unintelligible to userland without using FreeBSD compat headers.

If, in the future, we decide to implement ifmedia listing and selection
properly, that should likely be done with separate ioctls instead of
having multi-function ones like this.

This is technically an ABI break, but in practice it should not matter:
ifmediareq::ifm_current aligns with ifreq::ifr_media, so the things
that used this structure like our in-tree code did will continue to work.
Until this past May, the only other field that was usually set was
ifm_active, but in the absence of setting ifm_status all non-Haiku
consumers should ignore it completely.

The only consumer of this ioctl that I know of out of the tree,
wpa_supplicant, still works after these changes.
This commit is contained in:
Augustin Cavalier
2022-06-13 22:31:33 -04:00
parent ecf18ba4c8
commit 44fa45df3a
8 changed files with 26 additions and 70 deletions
-3
View File
@@ -99,9 +99,6 @@ public:
uint32 Flags() const; uint32 Flags() const;
bool HasLink() const; bool HasLink() const;
int32 CountMedia() const;
int32 GetMediaAt(int32 index) const;
int32 Media() const; int32 Media() const;
status_t SetMedia(int32 media); status_t SetMedia(int32 media);
-11
View File
@@ -64,17 +64,6 @@ struct ifaliasreq {
uint32_t ifra_flags; uint32_t ifra_flags;
}; };
/* used with SIOCGIFMEDIA */
struct ifmediareq {
char ifm_name[IF_NAMESIZE];
int ifm_current;
int ifm_mask;
int ifm_status;
int ifm_active;
int ifm_count;
int* ifm_ulist;
};
/* interface flags */ /* interface flags */
#define IFF_UP 0x0001 #define IFF_UP 0x0001
@@ -918,19 +918,16 @@ interface_protocol_control(net_datalink_protocol* _protocol, int32 option,
case SIOCGIFMEDIA: case SIOCGIFMEDIA:
{ {
// get media // get media
if (length > 0 && length < sizeof(ifmediareq)) const size_t copylen = offsetof(ifreq, ifr_media) + sizeof(ifreq::ifr_media);
if (length > 0 && length < copylen)
return B_BAD_VALUE; return B_BAD_VALUE;
struct ifmediareq request; struct ifreq request;
if (user_memcpy(&request, argument, sizeof(request)) != B_OK) if (user_memcpy(&request, argument, copylen) != B_OK)
return B_BAD_ADDRESS; return B_BAD_ADDRESS;
// TODO: Support retrieving the media list? request.ifr_media = interface->device->media;
memset(&request, 0, sizeof(struct ifmediareq)); return user_memcpy(argument, &request, copylen);
request.ifm_active = request.ifm_current
= interface->device->media;
return user_memcpy(argument, &request, sizeof(request));
} }
case SIOCGIFMETRIC: case SIOCGIFMETRIC:
+6 -14
View File
@@ -455,30 +455,22 @@ link_control(net_protocol* _protocol, int level, int option, void* value,
case SIOCGIFMEDIA: case SIOCGIFMEDIA:
{ {
// get media // get media
if (*_length > 0 && *_length < sizeof(ifmediareq)) const size_t copylen = offsetof(ifreq, ifr_media) + sizeof(ifreq::ifr_media);
if (*_length > 0 && *_length < copylen)
return B_BAD_VALUE; return B_BAD_VALUE;
net_device_interface* interface; net_device_interface* interface;
struct ifmediareq request; struct ifreq request;
if (!user_request_get_device_interface(value, (ifreq&)request, if (!user_request_get_device_interface(value, request, interface))
interface))
return B_BAD_ADDRESS; return B_BAD_ADDRESS;
if (interface == NULL) if (interface == NULL)
return B_DEVICE_NOT_FOUND; return B_DEVICE_NOT_FOUND;
if (user_memcpy(&request, value, sizeof(ifmediareq)) != B_OK) { request.ifr_media = interface->device->media;
put_device_interface(interface);
return B_BAD_ADDRESS;
}
// We do not support SIOCSIFMEDIA here, so ignore the media list.
memset(&request, 0, sizeof(struct ifmediareq));
request.ifm_active = request.ifm_current = interface->device->media;
put_device_interface(interface); put_device_interface(interface);
return user_memcpy(value, &request, sizeof(struct ifmediareq)); return user_memcpy(value, &request, copylen);
} }
case SIOCSPACKETCAP: case SIOCSPACKETCAP:
+2 -27
View File
@@ -617,39 +617,14 @@ BNetworkDevice::HasLink() const
} }
int32
BNetworkDevice::CountMedia() const
{
ifmediareq request;
request.ifm_count = 0;
request.ifm_ulist = NULL;
if (do_request(request, Name(), SIOCGIFMEDIA) != B_OK)
return -1;
return request.ifm_count;
}
int32 int32
BNetworkDevice::Media() const BNetworkDevice::Media() const
{ {
ifmediareq request; ifreq request;
request.ifm_count = 0;
request.ifm_ulist = NULL;
if (do_request(request, Name(), SIOCGIFMEDIA) != B_OK) if (do_request(request, Name(), SIOCGIFMEDIA) != B_OK)
return -1; return -1;
return request.ifm_current; return request.ifr_media;
}
int32
BNetworkDevice::GetMediaAt(int32 index) const
{
// TODO: this could do some caching
return 0;
} }
@@ -257,14 +257,11 @@ BNetworkInterface::MTU() const
int32 int32
BNetworkInterface::Media() const BNetworkInterface::Media() const
{ {
ifmediareq request; ifreq request;
request.ifm_count = 0;
request.ifm_ulist = NULL;
if (do_request(AF_INET, request, Name(), SIOCGIFMEDIA) != B_OK) if (do_request(AF_INET, request, Name(), SIOCGIFMEDIA) != B_OK)
return -1; return -1;
return request.ifm_current; return request.ifr_media;
} }
@@ -117,6 +117,16 @@ struct if_data {
struct timeval ifi_lastchange; /* time of last administrative change */ struct timeval ifi_lastchange; /* time of last administrative change */
}; };
struct ifmediareq {
char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */
int ifm_current; /* current media options */
int ifm_mask; /* don't care mask */
int ifm_status; /* media status */
int ifm_active; /* active options */
int ifm_count; /* # entries in ifm_ulist array */
int *ifm_ulist; /* media words */
};
struct ifdrv { struct ifdrv {
char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */ char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */
unsigned long ifd_cmd; unsigned long ifd_cmd;
@@ -328,7 +328,6 @@ compat_control(void *cookie, uint32 op, void *arg, size_t length)
case SIOCSIFFLAGS: case SIOCSIFFLAGS:
case SIOCSIFMEDIA: case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
case SIOCSIFMTU: case SIOCSIFMTU:
{ {
IFF_LOCKGIANT(ifp); IFF_LOCKGIANT(ifp);