SO_BINDTOPREFIX
Subsystems (6)
cover SO_BINDTOPREFIX
Since the net-next window just opened, I'm resubmitting my RFC for the SO_BINDTOSUBNET patch, following Mark Smith's suggestion to rename the whole thing to a more clear SO_BINDTOPREFIX.
Some arguments for and against it since the first submission:
* SO_BINDTOPREFIX is an arbitrary option and can be seens as nother use
* case of the SO_REUSEPORT BPF patch
* but at the same time using BPF requires more work/code on the server and since the bind to prefix use case could potentially become a common one maybe there is some value in having it as an option instead of having to code (either manually or with clang) an eBPF program that would do the same
* it may probably possible to archive the same results using VRF. This would require to create a VRF device, configure the device routing table and make each bind each process to a different VRF device (but I'm not sure how this would work/interfere with an existing iptables setup for example)
-----------------------------------------------------------------------------
This series introduces support for the SO_BINDTOPREFIX socket option, which allows a listener socket to bind to a prefix instead of * or a single address.
Motivation:
consider a set of servers, each one with thousands and thousands of IP addresses. Since assigning /32 or /128 IP individual addresses would be inefficient, one solution can be assigning prefixes using local routes (with 'ip route add local').
This allows a listener to listen and terminate connections going to any of the IP addresses of these prefixes without explicitly configuring all the IP addresses of the prefix range.
This is very efficient.
Unfortunately there may be the need to use different prefixes for different purposes.
One can imagine port 80 being served by one HTTP server for some IP prefix, while another server used for another prefix. Right now Linux does not allow this.
It is either possible to bind to *, indicating ALL traffic going to given port, or to individual IP addresses.
The first only allows to accept connections from all the prefixes. The latter does not scale well with lots of IP addresses.
Using bindtoprefix would solve this problem: just by adding a local route rule and setting the SO_BINDTOPREFIX option for a socket it would be possible to easily partition traffic by prefixes.
API:
the prefix is specified (as argument of the setsockopt syscall) by the address of the network, and the prefix length of the netmask.
IPv4:
struct ipv4_prefix {
__be32 net;
u_char plen;
};and IPv6:
struct ipv6_prefix {
struct in6_addr net;
u_char plen;
};Bind conflicts:
two sockets with the bindtoprefix option enabled generate a bind conflict if their network addresses masked with the shortest of their prefix are equal.
The bindtoprefix option can be combined with soreuseport so that two listener can bind on the same prefix.
Any questions/feedback appreciated.
Thanks,
Gilberto
Gilberto Bertin (4):
bindtoprefix: infrastructure
bindtoprefix: TCP/IPv4 implementation
bindtoprefix: TCP/IPv6 implementation
bindtoprefix: UPD implementation
include/net/sock.h | 20 +++++++ include/uapi/asm-generic/socket.h | 1 + net/core/sock.c | 111 ++++++++++++++++++++++++++++++++++++++ net/ipv4/inet_connection_sock.c | 20 ++++++- net/ipv4/inet_hashtables.c | 9 ++++ net/ipv4/udp.c | 36 +++++++++++++ net/ipv6/inet6_connection_sock.c | 17 +++++- net/ipv6/inet6_hashtables.c | 6 +++ 8 files changed, 218 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.3
RFC 1/4 bindtoprefix: infrastructure
--- include/net/sock.h | 20 +++++++ include/uapi/asm-generic/socket.h | 1 + net/core/sock.c | 111 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+)
include/net/sock.h +20 -0
@@ -109,6 +109,16 @@ typedef struct {#endif } socket_lock_t; +struct ipv4_prefix { +__be32 net; +u_char plen; +}; + +struct ipv6_prefix { +struct in6_addr net; +u_char plen; +}; +struct sock ;struct proto ;struct net ;
@@ -176,6 +186,13 @@ struct sock_common { unsigned char skc_ipv6only:1; unsigned char skc_net_refcnt:1;int skc_bound_dev_if; + +unsigned char skc_bind_to_prefix; +union { +struct ipv4_prefix skc_bind_prefix4; +struct ipv6_prefix skc_bind_prefix6; + }; +union {struct hlist_node skc_bind_node;struct hlist_nulls_node skc_portaddr_node;
@@ -327,6 +344,9 @@ struct sock {#define sk_state __sk_common.skc_state#define sk_reuse __sk_common.skc_reuse#define sk_reuseport __sk_common.skc_reuseport +#define sk_bind_to_prefix __sk_common.skc_bind_to_prefix +#define sk_bind_prefix4 __sk_common.skc_bind_prefix4 +#define sk_bind_prefix6 __sk_common.skc_bind_prefix6#define sk_ipv6only __sk_common.skc_ipv6only#define sk_net_refcnt __sk_common.skc_net_refcnt#define sk_bound_dev_if __sk_common.skc_bound_dev_if
include/uapi/asm-generic/socket.h +1 -0
@@ -30,6 +30,7 @@#define SO_SNDLOWAT 19#define SO_RCVTIMEO 20#define SO_SNDTIMEO 21 +#define SO_BINDTOPREFIX 22#endif /* Security levels - as per NRL IPv6 - don't actually do anything */
net/core/sock.c +111 -0
135 lines not on this page · raw message
Signature · 1 line
2.7.3
RFC 2/4 bindtoprefix: TCP/IPv4 implementation
In reply to Gilberto Bertin--- net/ipv4/inet_connection_sock.c | 20 +++++++++++++++++++- net/ipv4/inet_hashtables.c | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-)
net/ipv4/inet_connection_sock.c +19 -1
@@ -15,6 +15,7 @@#include <linux/module.h> #include <linux/jhash.h> +#include <linux/inetdevice.h> #include <net/inet_connection_sock.h> #include <net/inet_hashtables.h>
@@ -43,6 +44,22 @@ void inet_get_local_port_range(struct net *net, int *low, int *high) }EXPORT_SYMBOL (inet_get_local_port_range); +static inline int inet_csk_bind_prefix_conflict (const struct sock *sk, +const struct sock *sk2) +{ +__be32 mask; + +if (sk->sk_bind_to_prefix && sk2->sk_bind_to_prefix) { + mask =inet_make_mask (min (sk->sk_bind_prefix4.plen, + sk2->sk_bind_prefix4.plen)); + +return (sk->sk_bind_prefix4.net & mask) == + (sk2->sk_bind_prefix4.net & mask); + } + +return 0 ; +} +int inet_csk_bind_conflict (const struct sock *sk,const struct inet_bind_bucket *tb,bool relax) {
@@ -63,7 +80,8 @@ int inet_csk_bind_conflict(const struct sock *sk, !inet_v6_ipv6only(sk2) && (!sk->sk_bound_dev_if || !sk2->sk_bound_dev_if || - sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) { + sk->sk_bound_dev_if == sk2->sk_bound_dev_if) && + inet_csk_bind_prefix_conflict(sk, sk2)) { if ((!reuse || !sk2->sk_reuse || sk2->sk_state == TCP_LISTEN) && (!reuseport || !sk2->sk_reuseport ||
net/ipv4/inet_hashtables.c +9 -0
@@ -13,6 +13,7 @@* 2 of the License, or (at your option) any later version. */ +#include <linux/inetdevice.h> #include <linux/module.h> #include <linux/random.h> #include <linux/sched.h>
@@ -189,6 +190,14 @@ static inline int compute_score(struct sock *sk, struct net *net,return -1 ; score +=4 ; } +if (sk->sk_bind_to_prefix) { +__be32 mask =inet_make_mask (sk->sk_bind_prefix4.plen); + +if ((sk->sk_bind_prefix4.net & mask) != (daddr & mask)) +return -1 ; + score +=4 ; + } +if (sk->sk_incoming_cpu ==raw_smp_processor_id ()) score++; }
Signature · 1 line
2.7.3
RFC 3/4 bindtoprefix: TCP/IPv6 implementation
In reply to Gilberto Bertin--- net/ipv6/inet6_connection_sock.c | 17 ++++++++++++++++- net/ipv6/inet6_hashtables.c | 6 ++++++ 2 files changed, 22 insertions(+), 1 deletion(-)
net/ipv6/inet6_connection_sock.c +16 -1
@@ -27,6 +27,20 @@#include <net/sock.h> #include <net/inet6_connection_sock.h> +int inet6_csk_bind_prefix_conflict (const struct sock *sk, +const struct sock *sk2) +{ +u_char plen; + + plen =min (sk->sk_bind_prefix6.plen, sk2->sk_bind_prefix6.plen); + +if (sk->sk_bind_to_prefix && sk2->sk_bind_to_prefix) +return ipv6_prefix_equal (&sk->sk_bind_prefix6.net, + &sk2->sk_bind_prefix6.net, plen); + +return 0 ; +} +int inet6_csk_bind_conflict (const struct sock *sk,const struct inet_bind_bucket *tb,bool relax) {
@@ -44,7 +58,8 @@ int inet6_csk_bind_conflict(const struct sock *sk,if (sk != sk2 && (!sk->sk_bound_dev_if || !sk2->sk_bound_dev_if || - sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) { + sk->sk_bound_dev_if == sk2->sk_bound_dev_if) && +inet6_csk_bind_prefix_conflict (sk, sk2)) { if ((!reuse || !sk2->sk_reuse || sk2->sk_state == TCP_LISTEN) && (!reuseport || !sk2->sk_reuseport ||
net/ipv6/inet6_hashtables.c +6 -0
@@ -114,6 +114,12 @@ static inline int compute_score(struct sock *sk, struct net *net,return -1 ; score++; } +if (sk->sk_bind_to_prefix) { +if (!ipv6_prefix_equal (&sk->sk_bind_prefix6.net, daddr, + sk->sk_bind_prefix6.plen)) +return -1 ; + score++; + }if (sk->sk_incoming_cpu ==raw_smp_processor_id ()) score++; }
Signature · 1 line
2.7.3
RFC 4/4 bindtoprefix: UPD implementation
In reply to Gilberto Bertin--- net/ipv4/udp.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
net/ipv4/udp.c +36 -0
@@ -133,6 +133,23 @@ EXPORT_SYMBOL(udp_memory_allocated);#define MAX_UDP_PORTS 65536#define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) +static inline int udp_csk_bind_prefix_conflict (const struct sock *sk, +const struct sock *sk2) +{ +__be32 mask; + +if (sk->sk_bind_to_prefix && sk2->sk_bind_to_prefix) { + mask =inet_make_mask (min (sk->sk_bind_prefix4.plen, + sk2->sk_bind_prefix4.plen)); + +return (sk->sk_bind_prefix4.net & mask) == + (sk2->sk_bind_prefix4.net & mask); + } + +return 0 ; +} + + static int udp_lib_lport_inuse(struct net *net, __u16 num, const struct udp_hslot *hslot, unsigned long *bitmap,
@@ -153,6 +170,7 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num, (!sk2->sk_reuse || !sk->sk_reuse) && (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && +udp_csk_bind_prefix_conflict (sk, sk2) && (!sk2->sk_reuseport || !sk->sk_reuseport ||rcu_access_pointer (sk->sk_reuseport_cb) || !uid_eq(uid, sock_i_uid(sk2))) &&
@@ -189,6 +207,7 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num, (!sk2->sk_reuse || !sk->sk_reuse) && (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && +udp_csk_bind_prefix_conflict (sk, sk2) && (!sk2->sk_reuseport || !sk->sk_reuseport ||rcu_access_pointer (sk->sk_reuseport_cb) || !uid_eq(uid, sock_i_uid(sk2))) &&
@@ -426,6 +445,15 @@ static inline int compute_score(struct sock *sk, struct net *net,return -1 ; score +=4 ; } + +if (sk->sk_bind_to_prefix) { +__be32 mask =inet_make_mask (sk->sk_bind_prefix4.plen); + +if ((sk->sk_bind_prefix4.net & mask) != (daddr & mask)) +return -1 ; + score +=4 ; + } +if (sk->sk_incoming_cpu ==raw_smp_processor_id ()) score++;return score;
@@ -471,6 +499,14 @@ static inline int compute_score2(struct sock *sk, struct net *net, score +=4 ; } +if (sk->sk_bind_to_prefix) { +__be32 mask =inet_make_mask (sk->sk_bind_prefix4.plen); + +if ((sk->sk_bind_prefix4.net & mask) != (daddr & mask)) +return -1 ; + score +=4 ; + } +if (sk->sk_incoming_cpu ==raw_smp_processor_id ()) score++;
Signature · 1 line
2.7.3
Quoted 3 lines · Gilberto Bertin
Since the net-next window just opened, I'm resubmitting my RFC for the
SO_BINDTOSUBNETpatch, following Mark Smith's suggestion to rename the whole thing to a more clearSO_BINDTOPREFIX.
Please do not add such monolithic option.
BPF is absolutely the way to go here, as it allows for whatever user specified tweaks, like a list of destination subnetwork, or/and a list of source network, or the date/time of the day, or port knocking without netfilter, or ... you name it.
Simply add an option to load a BPF filter on a socket, used to vary the various compute_score() functions.
No hard coded knowledge in the kernel, but a generic interface.