1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 22:43:50 +01:00

Merge pull request #13253 from nmeum/pr/random_ephemeral_port

gnrc_sock_udp: choose random ephemeral port
This commit is contained in:
Martine Lenders 2020-03-26 15:00:14 +01:00 committed by GitHub
commit f39cfc7556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 15 deletions

View File

@ -55,14 +55,6 @@ extern "C" {
*/
#define GNRC_SOCK_DYN_PORTRANGE_ERR (0)
/**
* @brief Offset for next dynamic port
*
* Currently set to a static (prime) offset, but could be random, too
* see https://tools.ietf.org/html/rfc6056#section-3.3.3
*/
#define GNRC_SOCK_DYN_PORTRANGE_OFF (17U)
/**
* @brief Internal helper functions for GNRC
* @internal

View File

@ -25,6 +25,7 @@
#include "net/gnrc/udp.h"
#include "net/sock/udp.h"
#include "net/udp.h"
#include "random.h"
#include "gnrc_sock_internal.h"
@ -32,8 +33,6 @@
static sock_udp_t *_udp_socks = NULL;
#endif
static uint16_t _dyn_port_next = 0;
/**
* @brief Checks if a given UDP port is already used by another sock
*/
@ -66,17 +65,16 @@ static bool _dyn_port_used(uint16_t port)
/**
* @brief returns a UDP port, and checks for reuse if required
*
* complies to RFC 6056, see https://tools.ietf.org/html/rfc6056#section-3.3.3
* implements "Another Simple Port Randomization Algorithm" as specified in
* RFC 6056, see https://tools.ietf.org/html/rfc6056#section-3.3.2
*/
static uint16_t _get_dyn_port(sock_udp_t *sock)
{
unsigned count = GNRC_SOCK_DYN_PORTRANGE_NUM;
do {
uint16_t port = GNRC_SOCK_DYN_PORTRANGE_MIN +
(_dyn_port_next * GNRC_SOCK_DYN_PORTRANGE_OFF) % GNRC_SOCK_DYN_PORTRANGE_NUM;
_dyn_port_next++;
if ((sock == NULL) || (sock->flags & SOCK_FLAGS_REUSE_EP) ||
!_dyn_port_used(port)) {
(random_uint32() % GNRC_SOCK_DYN_PORTRANGE_NUM);
if ((sock && (sock->flags & SOCK_FLAGS_REUSE_EP)) || !_dyn_port_used(port)) {
return port;
}
--count;