From 9c89560e6cdf44a21b2eff8765973ed8665fa07f Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Sun, 22 Feb 2015 20:14:14 +0100 Subject: [PATCH] Use IP_PORTRANGE_HIGH for BFD where available --- lib/socket.h | 1 + proto/bfd/packets.c | 2 +- sysdep/unix/io.c | 16 +++++++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/socket.h b/lib/socket.h index a5b85aa2..683cdde3 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -103,6 +103,7 @@ extern int sk_priority_control; /* Suggested priority for control traffic, shou #define SKF_LADDR_RX 0x04 /* Report local address for RX packets */ #define SKF_TTL_RX 0x08 /* Report TTL / Hop Limit for RX packets */ #define SKF_BIND 0x10 /* Bind datagram socket to given source address */ +#define SKF_HIGH_PORT 0x20 /* Choose port from high range if possible */ #define SKF_THREAD 0x100 /* Socked used in thread, Do not add to main loop */ #define SKF_TRUNCATED 0x200 /* Received packet was truncated, set by IO layer */ diff --git a/proto/bfd/packets.c b/proto/bfd/packets.c index 8818453b..b5fd6782 100644 --- a/proto/bfd/packets.c +++ b/proto/bfd/packets.c @@ -231,7 +231,7 @@ bfd_open_tx_sk(struct bfd_proto *p, ip_addr local, struct iface *ifa) sk->tos = IP_PREC_INTERNET_CONTROL; sk->priority = sk_priority_control; sk->ttl = ifa ? 255 : -1; - sk->flags = SKF_THREAD | SKF_BIND; + sk->flags = SKF_THREAD | SKF_BIND | SKF_HIGH_PORT; #ifdef IPV6 sk->flags |= SKF_V6ONLY; diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index 04f0fe50..daf9d054 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -1305,7 +1305,7 @@ sk_passive_connected(sock *s, int type) log(L_ERR "SOCK: Incoming connection: %s%#m", t->err); /* FIXME: handle it better in rfree() */ - close(t->fd); + close(t->fd); t->fd = -1; rfree(t); return 1; @@ -1348,7 +1348,7 @@ sk_open(sock *s) bind_addr = s->saddr; do_bind = bind_port || ipa_nonzero(bind_addr); break; - + case SK_UDP: fd = socket(af, SOCK_DGRAM, IPPROTO_UDP); bind_port = s->sport; @@ -1399,6 +1399,14 @@ sk_open(sock *s) } #endif } +#ifdef IP_PORTRANGE + else if (s->flags & SKF_HIGH_PORT) + { + int range = IP_PORTRANGE_HIGH; + if (setsockopt(fd, IPPROTO_IP, IP_PORTRANGE, &range, sizeof(range)) < 0) + log(L_WARN "Socket error: %s%#m", "IP_PORTRANGE"); + } +#endif sockaddr_fill(&sa, af, bind_addr, s->iface, bind_port); if (bind(fd, &sa.sa, SA_LEN(sa)) < 0) @@ -1661,7 +1669,7 @@ sk_rx_ready(sock *s) redo: rv = select(s->fd+1, &rd, &wr, NULL, &timo); - + if ((rv < 0) && (errno == EINTR || errno == EAGAIN)) goto redo; @@ -2026,5 +2034,3 @@ test_old_bird(char *path) die("I found another BIRD running."); close(fd); } - -