Skip to content

Commit

Permalink
Add bind-address-ipv4 to upnp (#845)
Browse files Browse the repository at this point in the history
Pass the bind-address-ipv4 to upnpDiscover to allow for upnp to only
use the interface specified. Prevents upnp packet leaks.

Authored-by: LaserEyess <lasereyess@lasereyess.net>
  • Loading branch information
LaserEyess committed Jan 21, 2022
1 parent f94f792 commit 9e5e27a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
7 changes: 6 additions & 1 deletion libtransmission/port-forwarding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ static void natPulse(tr_shared* s, bool do_check)
s->session->public_peer_port = public_peer_port;
}

s->upnpStatus = tr_upnpPulse(s->upnp, private_peer_port, is_enabled, do_check);
s->upnpStatus = tr_upnpPulse(
s->upnp,
private_peer_port,
is_enabled,
do_check,
tr_address_to_string(&s->session->bind_ipv4->addr));

auto const new_status = tr_sharedTraversalStatus(s);

Expand Down
11 changes: 2 additions & 9 deletions libtransmission/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,6 @@ void tr_sessionSetEncryption(tr_session* session, tr_encryption_mode mode)
****
***/

struct tr_bindinfo
{
tr_socket_t socket;
tr_address addr;
struct event* ev;
};

static void close_bindinfo(struct tr_bindinfo* b)
{
if (b != nullptr && b->socket != TR_BAD_SOCKET)
Expand Down Expand Up @@ -1817,8 +1810,6 @@ static void sessionCloseImplStart(tr_session* session)
{
session->is_closing_ = true;

free_incoming_peer_port(session);

if (session->isLPDEnabled)
{
tr_lpdUninit(session);
Expand All @@ -1835,6 +1826,8 @@ static void sessionCloseImplStart(tr_session* session)

tr_verifyClose(session);
tr_sharedClose(session);

free_incoming_peer_port(session);
session->rpc_server_.reset();

/* Close the torrents. Get the most active ones first so that
Expand Down
7 changes: 7 additions & 0 deletions libtransmission/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ struct tr_blocklistFile;
struct tr_cache;
struct tr_fdInfo;

struct tr_bindinfo
{
int socket;
tr_address addr;
struct event* ev;
};

struct tr_turtle_info
{
/* TR_UP and TR_DOWN speed limits */
Expand Down
13 changes: 6 additions & 7 deletions libtransmission/upnp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void tr_upnpClose(tr_upnp* handle)
*** Wrappers for miniupnpc functions
**/

static struct UPNPDev* tr_upnpDiscover(int msec)
static struct UPNPDev* tr_upnpDiscover(int msec, char const* bindaddr)
{
UPNPDev* ret = nullptr;
auto have_err = bool{};
Expand All @@ -85,14 +85,14 @@ static struct UPNPDev* tr_upnpDiscover(int msec)
int err = UPNPDISCOVER_SUCCESS;

#if (MINIUPNPC_API_VERSION >= 14) /* adds ttl */
ret = upnpDiscover(msec, nullptr, nullptr, 0, 0, 2, &err);
ret = upnpDiscover(msec, bindaddr, nullptr, 0, 0, 2, &err);
#else
ret = upnpDiscover(msec, nullptr, nullptr, 0, 0, &err);
ret = upnpDiscover(msec, bindaddr, nullptr, 0, 0, &err);
#endif

have_err = err != UPNPDISCOVER_SUCCESS;
#else
ret = upnpDiscover(msec, nullptr, nullptr, 0);
ret = upnpDiscover(msec, bindaddr, nullptr, 0);
have_err = ret == nullptr;
#endif

Expand Down Expand Up @@ -218,12 +218,11 @@ enum
UPNP_IGD_INVALID = 3
};

tr_port_forwarding tr_upnpPulse(tr_upnp* handle, tr_port port, bool isEnabled, bool doPortCheck)
tr_port_forwarding tr_upnpPulse(tr_upnp* handle, tr_port port, bool isEnabled, bool doPortCheck, char const* bindaddr)
{
if (isEnabled && handle->state == TR_UPNP_DISCOVER)
{
auto* const devlist = tr_upnpDiscover(2000);

auto* const devlist = tr_upnpDiscover(2000, bindaddr);
errno = 0;

if (UPNP_GetValidIGD(devlist, &handle->urls, &handle->data, handle->lanaddr, sizeof(handle->lanaddr)) ==
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/upnp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ tr_upnp* tr_upnpInit(void);

void tr_upnpClose(tr_upnp*);

tr_port_forwarding tr_upnpPulse(tr_upnp*, tr_port port, bool isEnabled, bool doPortCheck);
tr_port_forwarding tr_upnpPulse(tr_upnp*, tr_port port, bool isEnabled, bool doPortCheck, char const*);

/* @} */

0 comments on commit 9e5e27a

Please sign in to comment.