Skip to content

Commit

Permalink
Bug 1709617 - Don't generate socket() in content process for getting …
Browse files Browse the repository at this point in the history
…MTU r=jesup

Cherry-picked patches from upstream using:
for commit in 7f0e11fa29ad83194b60180fdf64a0ea4908549a acfce46e428cc084b4bd0164e1b019261a8dbeda; do
  curl -vsSL https://github.com/sctplab/usrsctp/commit/${commit}.patch | patch -d netwerk/sctp/src/ -p 2;
done;

Differential Revision: https://phabricator.services.mozilla.com/D114509
  • Loading branch information
Alexandre Lissy committed May 11, 2021
1 parent 6c3cdc7 commit 27722db
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 40 deletions.
2 changes: 1 addition & 1 deletion netwerk/sctp/src/netinet/sctp_os_userspace.h
Expand Up @@ -882,7 +882,7 @@ int sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af);

#define SCTP_GATHER_MTU_FROM_ROUTE(sctp_ifa, sa, rt) ((rt != NULL) ? rt->rt_rmx.rmx_mtu : 0)

#define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) sctp_userspace_get_mtu_from_ifn(if_nametoindex(((struct ifaddrs *) (sctp_ifn))->ifa_name), AF_INET)
#define SCTP_GATHER_MTU_FROM_INTFC(sctp_ifn) (sctp_ifn->ifn_mtu)

#define SCTP_SET_MTU_OF_ROUTE(sa, rt, mtu) do { \
if (rt != NULL) \
Expand Down
116 changes: 77 additions & 39 deletions netwerk/sctp/src/netinet/sctp_userspace.c
Expand Up @@ -98,23 +98,42 @@ sctp_userspace_set_threadname(const char *name)
int
sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af)
{
#if defined(INET) || defined(INET6)
struct ifreq ifr;
int fd;
#endif
int mtu;

memset(&ifr, 0, sizeof(struct ifreq));
if (if_indextoname(if_index, ifr.ifr_name) != NULL) {
/* TODO can I use the raw socket here and not have to open a new one with each query? */
if ((fd = socket(af, SOCK_DGRAM, 0)) < 0)
return (0);
if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
switch (af) {
#if defined(INET)
case AF_INET:
#endif
#if defined(INET6)
case AF_INET6:
#endif
#if defined(INET) || defined(INET6)
memset(&ifr, 0, sizeof(struct ifreq));
mtu = 0;
if (if_indextoname(if_index, ifr.ifr_name) != NULL) {
/* TODO can I use the raw socket here and not have to open a new one with each query? */
if ((fd = socket(af, SOCK_DGRAM, 0)) < 0) {
break;
}
if (ioctl(fd, SIOCGIFMTU, &ifr) >= 0) {
mtu = ifr.ifr_mtu;
}
close(fd);
return (0);
}
close(fd);
return ifr.ifr_mtu;
} else {
return (0);
break;
#endif
case AF_CONN:
mtu = 1280;
break;
default:
mtu = 0;
break;
}
return (mtu);
}
#endif

Expand Down Expand Up @@ -143,41 +162,60 @@ timingsafe_bcmp(const void *b1, const void *b2, size_t n)
int
sctp_userspace_get_mtu_from_ifn(uint32_t if_index, int af)
{
#if defined(INET) || defined(INET6)
PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
DWORD AdapterAddrsSize, Err;
int ret;
#endif
int mtu;

ret = 0;
AdapterAddrsSize = 0;
pAdapterAddrs = NULL;
if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() sizing failed with error code %d, AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
ret = -1;
switch (af) {
#if defined(INET)
case AF_INET:
#endif
#if defined(INET6)
case AF_INET6:
#endif
#if defined(INET) || defined(INET6)
mtu = 0;
AdapterAddrsSize = 0;
pAdapterAddrs = NULL;
if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() sizing failed with error code %d, AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
mtu = -1;
goto cleanup;
}
}
if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n");
mtu = -1;
goto cleanup;
}
}
if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
SCTPDBG(SCTP_DEBUG_USR, "Memory allocation error!\n");
ret = -1;
goto cleanup;
}
if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() failed with error code %d\n", Err);
ret = -1;
goto cleanup;
}
for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
if (pAdapt->IfIndex == if_index) {
ret = pAdapt->Mtu;
break;
if ((Err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
SCTPDBG(SCTP_DEBUG_USR, "GetAdaptersAddresses() failed with error code %d\n", Err);
mtu = -1;
goto cleanup;
}
for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
if (pAdapt->IfIndex == if_index) {
mtu = pAdapt->Mtu;
break;
}
}
cleanup:
if (pAdapterAddrs != NULL) {
GlobalFree(pAdapterAddrs);
}
break;
#endif
case AF_CONN:
mtu = 1280;
break;
default:
mtu = 0;
break;
}
cleanup:
if (pAdapterAddrs != NULL) {
GlobalFree(pAdapterAddrs);
}
return (ret);
return (mtu);
}

void
Expand Down

0 comments on commit 27722db

Please sign in to comment.