sctp: support vrf processing
Subsystems (5)
Xin Long
maintainerv2 cover sctp: support vrf processing
This patchset adds the VRF processing in SCTP. Simliar to TCP/UDP, it includes socket bind and socket/association lookup changes.
For socket bind change, it allows sockets to bind to a VRF device and allows multiple sockets with the same IP and PORT to bind to different interfaces in patch 1-3.
For socket/association lookup change, it adds dif and sdif check in both asoc and ep lookup in patch 4 and 5, and when binding to nodev, users can decide if accept the packets received from one l3mdev by setup a sysctl option in patch 6.
Note with VRF support, in a netns, an association will be decided by src ip + src port + dst ip + dst port + bound_dev_if, and it's possible for ss to have:
# ss --sctp -n State Local Address:Port Peer Address:Port ESTAB 192.168.1.2%vrf-s1:1234 `- ESTAB 192.168.1.2%veth1:1234 192.168.1.1:1234 ESTAB 192.168.1.2%vrf-s2:1234 `- ESTAB 192.168.1.2%veth2:1234 192.168.1.1:1234
See the selftest in patch 7 for more usage.
Also, thanks Carlo for testing this patch series on their use.
v1->v2:
- In Patch 5, move sctp_sk_bound_dev_eq() definition to net/sctp/ input.c to avoid a build error when IP_SCTP is disabled, as Paolo suggested.
- In Patch 7, avoid one sleep by disabling the IPv6 dad, and remove another sleep by using ss to check if the server's ready, and also delete two unncessary sleeps in sctp_hello.c, as Paolo suggested.
Xin Long (7):
sctp: verify the bind address with the tb_id from l3mdev
sctp: check ipv6 addr with sk_bound_dev if set
sctp: check sk_bound_dev_if when matching ep in get_port
sctp: add skb_sdif in struct sctp_af
sctp: add dif and sdif check in asoc and ep lookup
sctp: add sysctl net.sctp.l3mdev_accept
selftests: add a selftest for sctp vrf
Documentation/networking/ip-sysctl.rst | 9 ++ include/net/netns/sctp.h | 4 + include/net/sctp/sctp.h | 6 +- include/net/sctp/structs.h | 9 +- net/sctp/diag.c | 3 +- net/sctp/endpointola.c | 13 +- net/sctp/input.c | 108 +++++++------- net/sctp/ipv6.c | 22 ++- net/sctp/protocol.c | 19 ++- net/sctp/socket.c | 9 +- net/sctp/sysctl.c | 11 ++ tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/sctp_hello.c | 137 +++++++++++++++++ tools/testing/selftests/net/sctp_vrf.sh | 178 +++++++++++++++++++++++ 14 files changed, 461 insertions(+), 69 deletions(-) create mode 100644 tools/testing/selftests/net/sctp_hello.c create mode 100755 tools/testing/selftests/net/sctp_vrf.sh
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 1/7 sctp: verify the bind address with the tb_id from l3mdev
After binding to a l3mdev, it should use the route table from the corresponding VRF to verify the addr when binding to an address.
Note ipv6 doesn't need it, as binding to ipv6 address does not verify the addr with route lookup.
--- net/sctp/protocol.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
net/sctp/protocol.c +6 -3
@@ -351,10 +351,13 @@ static int sctp_v4_addr_valid(union sctp_addr *addr,/* Should this be available for binding? */ static int sctp_v4_available (union sctp_addr *addr,struct sctp_sock *sp) { -struct net *net =sock_net (&sp->inet.sk); -int ret =inet_addr_type (net, addr->v4.sin_addr.s_addr); - +struct sock *sk = &sp->inet.sk; +struct net *net =sock_net (sk); +int tb_id =RT_TABLE_LOCAL ; +int ret; + tb_id =l3mdev_fib_table_by_index (net, sk->sk_bound_dev_if) ?: tb_id; + ret =inet_addr_type_table (net, addr->v4.sin_addr.s_addr, tb_id); if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) && ret != RTN_LOCAL && !sp->inet.freebind &&
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 3/7 sctp: check sk_bound_dev_if when matching ep in get_port
In reply to Xin LongIn sctp_get_port_local(), when binding to IP and PORT, it should also check sk_bound_dev_if to match listening sk if it's set by SO_BINDTOIFINDEX, so that multiple sockets with the same IP and PORT, but different sk_bound_dev_if can be listened at the same time.
--- net/sctp/socket.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/sctp/socket.c +4 -1
@@ -8398,6 +8398,7 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)* in an endpoint. */ sk_for_each_bound (sk2, &pp->owner) { +int bound_dev_if2 =READ_ONCE (sk2->sk_bound_dev_if);struct sctp_sock *sp2 =sctp_sk (sk2);struct sctp_endpoint *ep2 = sp2->ep;
@@ -8408,7 +8409,9 @@ static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr) uid_eq(uid, sock_i_uid(sk2))))continue ; -if (sctp_bind_addr_conflict (&ep2->base.bind_addr, +if ((!sk->sk_bound_dev_if || !bound_dev_if2 || + sk->sk_bound_dev_if == bound_dev_if2) && +sctp_bind_addr_conflict (&ep2->base.bind_addr, addr, sp2, sp)) { ret =1 ; goto fail_unlock;
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 5/7 sctp: add dif and sdif check in asoc and ep lookup
In reply to Xin LongThis patch at first adds a pernet global l3mdev_accept to decide if it accepts the packets from a l3mdev when a SCTP socket doesn't bind to any interface. It's set to 1 to avoid any possible incompatible issue, and in next patch, a sysctl will be introduced to allow to change it.
Then similar to inet/udp_sk_bound_dev_eq(), sctp_sk_bound_dev_eq() is added to check either dif or sdif is equal to sk_bound_dev_if, and to check sid is 0 or l3mdev_accept is 1 if sk_bound_dev_if is not set. This function is used to match a association or a endpoint, namely called by sctp_addrs_lookup_transport() and sctp_endpoint_is_match(). All functions that needs updating are:
sctp_rcv():
asoc:
__sctp_rcv_lookup()
__sctp_lookup_association() -> sctp_addrs_lookup_transport()
__sctp_rcv_lookup_harder()
__sctp_rcv_init_lookup()
__sctp_lookup_association() -> sctp_addrs_lookup_transport()
__sctp_rcv_walk_lookup()
__sctp_rcv_asconf_lookup()
__sctp_lookup_association() -> sctp_addrs_lookup_transport() ep:
__sctp_rcv_lookup_endpoint() -> sctp_endpoint_is_match()
sctp_connect():
sctp_endpoint_is_peeled_off()
__sctp_lookup_association()
sctp_has_association()
sctp_lookup_association()
__sctp_lookup_association() -> sctp_addrs_lookup_transport()sctp_diag_dump_one():
sctp_transport_lookup_process() -> sctp_addrs_lookup_transport()
--- include/net/netns/sctp.h | 4 ++ include/net/sctp/sctp.h | 6 ++- include/net/sctp/structs.h | 8 +-- net/sctp/diag.c | 3 +- net/sctp/endpointola.c | 13 +++-- net/sctp/input.c | 108 ++++++++++++++++++++----------------- net/sctp/protocol.c | 4 ++ net/sctp/socket.c | 4 +- 8 files changed, 89 insertions(+), 61 deletions(-)
include/net/netns/sctp.h +4 -0
@@ -175,6 +175,10 @@ struct netns_sctp {/* Threshold for autoclose timeout, in seconds. */ unsigned long max_autoclose; + +#ifdef CONFIG_NET_L3_MASTER_DEV +int l3mdev_accept; +#endif };#endif /* __NETNS_SCTP_H__ */
include/net/sctp/sctp.h +4 -2
@@ -114,7 +114,7 @@ struct sctp_transport *sctp_transport_get_idx(struct net *net, struct rhashtable_iter *iter, int pos);int sctp_transport_lookup_process (sctp_callback_t cb,struct net *net,const union sctp_addr *laddr, -const union sctp_addr *paddr,void *p); +const union sctp_addr *paddr,void *p,int dif);int sctp_transport_traverse_process (sctp_callback_t cb,sctp_callback_t cb_done,struct net *net,int *pos,void *p);int sctp_for_each_endpoint (int (*cb)(struct sctp_endpoint *,void *),void *p);
@@ -162,10 +162,12 @@ void sctp_unhash_transport(struct sctp_transport *t);struct sctp_transport *sctp_addrs_lookup_transport (struct net *net,const union sctp_addr *laddr, -const union sctp_addr *paddr); +const union sctp_addr *paddr, +int dif,int sdif);struct sctp_transport *sctp_epaddr_lookup_transport (const struct sctp_endpoint *ep,const union sctp_addr *paddr); +bool sctp_sk_bound_dev_eq (struct net *net,int bound_dev_if,int dif,int sdif);/* * sctp/proc.c
include/net/sctp/structs.h +5 -3
@@ -1379,10 +1379,12 @@ struct sctp_association *sctp_endpoint_lookup_assoc( struct sctp_transport **); bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,const union sctp_addr *paddr); -struct sctp_endpoint *sctp_endpoint_is_match (struct sctp_endpoint *, -struct net *,const union sctp_addr *); +struct sctp_endpoint *sctp_endpoint_is_match (struct sctp_endpoint *ep, +struct net *net, +const union sctp_addr *laddr, +int dif,int sdif);bool sctp_has_association (struct net *net,const union sctp_addr *laddr, -const union sctp_addr *paddr); +const union sctp_addr *paddr,int dif,int sdif); int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep, const struct sctp_association *asoc,
net/sctp/diag.c +2 -1
@@ -426,6 +426,7 @@ static int sctp_diag_dump_one(struct netlink_callback *cb,struct net *net =sock_net (skb->sk);const struct nlmsghdr *nlh = cb->nlh;union sctp_addr laddr, paddr; +int dif = req->id.idiag_if;struct sctp_comm_param commp = { .skb = skb, .r = req,
@@ -454,7 +455,7 @@ static int sctp_diag_dump_one(struct netlink_callback *cb, }return sctp_transport_lookup_process (sctp_sock_dump_one, - net, &laddr, &paddr, &commp); + net, &laddr, &paddr, &commp, dif); } static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
net/sctp/endpointola.c +9 -4
@@ -246,12 +246,15 @@ void sctp_endpoint_put(struct sctp_endpoint *ep)/* Is this the endpoint we are looking for? */ struct sctp_endpoint *sctp_endpoint_is_match (struct sctp_endpoint *ep,struct net *net, -const union sctp_addr *laddr) +const union sctp_addr *laddr, +int dif,int sdif) { +int bound_dev_if =READ_ONCE (ep->base.sk->sk_bound_dev_if);struct sctp_endpoint *retval =NULL ; -if ((htons (ep->base.bind_addr.port) == laddr->v4.sin_port) && -net_eq (ep->base.net, net)) { +if (net_eq (ep->base.net, net) && +sctp_sk_bound_dev_eq (net, bound_dev_if, dif, sdif) && + (htons (ep->base.bind_addr.port) == laddr->v4.sin_port)) {if (sctp_bind_addr_match (&ep->base.bind_addr, laddr,sctp_sk (ep->base.sk))) retval = ep;
@@ -298,6 +301,7 @@ struct sctp_association *sctp_endpoint_lookup_assoc(bool sctp_endpoint_is_peeled_off (struct sctp_endpoint *ep,const union sctp_addr *paddr) { +int bound_dev_if =READ_ONCE (ep->base.sk->sk_bound_dev_if);struct sctp_sockaddr_entry *addr;struct net *net = ep->base.net;struct sctp_bind_addr *bp;
@@ -307,7 +311,8 @@ bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,* so the address_list can not change. */ list_for_each_entry (addr, &bp->address_list, list) { -if (sctp_has_association (net, &addr->a, paddr)) +if (sctp_has_association (net, &addr->a, paddr, + bound_dev_if, bound_dev_if))return true; }
net/sctp/input.c +59 -49
286 lines not on this page · raw message
net/sctp/protocol.c +4 -0
@@ -1394,6 +1394,10 @@ static int __net_init sctp_defaults_init(struct net *net)/* Initialize maximum autoclose timeout. */ net->sctp.max_autoclose =INT_MAX /HZ ; +#ifdef CONFIG_NET_L3_MASTER_DEV + net->sctp.l3mdev_accept =1 ; +#endif + status =sctp_sysctl_net_register (net);if (status) goto err_sysctl_register;
net/sctp/socket.c +2 -2
@@ -5315,14 +5315,14 @@ EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);int sctp_transport_lookup_process (sctp_callback_t cb,struct net *net,const union sctp_addr *laddr, -const union sctp_addr *paddr,void *p) +const union sctp_addr *paddr,void *p,int dif) {struct sctp_transport *transport;struct sctp_endpoint *ep;int err = -ENOENT ;rcu_read_lock (); - transport =sctp_addrs_lookup_transport (net, laddr, paddr); + transport =sctp_addrs_lookup_transport (net, laddr, paddr, dif, dif);if (!transport) {rcu_read_unlock ();return err;
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 6/7 sctp: add sysctl net.sctp.l3mdev_accept
In reply to Xin LongThis patch is to add sysctl net.sctp.l3mdev_accept to allow users to change the pernet global l3mdev_accept.
--- Documentation/networking/ip-sysctl.rst | 9 +++++++++ net/sctp/sysctl.c | 11 +++++++++++ 2 files changed, 20 insertions(+)
Documentation/networking/ip-sysctl.rst +9 -0
@@ -3127,6 +3127,15 @@ ecn_enable - BOOLEAN Default: 1 +l3mdev_accept - BOOLEAN + Enabling this option allows a "global" bound socket to work + across L3 master domains (e.g., VRFs) with packets capable of + being received regardless of the L3 domain in which they + originated. Only valid when the kernel was compiled with + CONFIG_NET_L3_MASTER_DEV. + + Default: 1 (enabled) + ``/proc/sys/net/core/*`` ========================
net/sctp/sysctl.c +11 -0
@@ -347,6 +347,17 @@ static struct ctl_table sctp_net_table[] = { .extra1 = &max_autoclose_min, .extra2 = &max_autoclose_max, }, +#ifdef CONFIG_NET_L3_MASTER_DEV + { + .procname = "l3mdev_accept", + .data = &init_net.sctp.l3mdev_accept, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, +#endif { .procname = "pf_enable", .data = &init_net.sctp.pf_enable,
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 4/7 sctp: add skb_sdif in struct sctp_af
In reply to Xin LongAdd skb_sdif function in struct sctp_af to get the enslaved device for both ipv4 and ipv6 when adding SCTP VRF support in sctp_rcv in the next patch.
--- include/net/sctp/structs.h | 1 + net/sctp/ipv6.c | 8 +++++++- net/sctp/protocol.c | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-)
include/net/sctp/structs.h +1 -0
@@ -477,6 +477,7 @@ struct sctp_af {int (*available) (union sctp_addr *,struct sctp_sock *);int (*skb_iif) (const struct sk_buff *sk); +int (*skb_sdif)(const struct sk_buff *sk);int (*is_ce) (const struct sk_buff *sk);void (*seq_dump_addr)(struct seq_file *seq,union sctp_addr *addr);
net/sctp/ipv6.c +7 -1
@@ -842,7 +842,12 @@ static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)/* Where did this skb come from? */ static int sctp_v6_skb_iif (const struct sk_buff *skb) { -return IP6CB (skb)->iif; +return inet6_iif (skb); +} + +static int sctp_v6_skb_sdif (const struct sk_buff *skb) +{ +return inet6_sdif (skb); }/* Was this packet marked by Explicit Congestion Notification? */
@@ -1142,6 +1147,7 @@ static struct sctp_af sctp_af_inet6 = { .is_any = sctp_v6_is_any, .available = sctp_v6_available, .skb_iif = sctp_v6_skb_iif, + .skb_sdif = sctp_v6_skb_sdif, .is_ce = sctp_v6_is_ce, .seq_dump_addr = sctp_v6_seq_dump_addr, .ecn_capable = sctp_v6_ecn_capable,
net/sctp/protocol.c +6 -0
@@ -567,6 +567,11 @@ static int sctp_v4_skb_iif(const struct sk_buff *skb)return inet_iif (skb); } +static int sctp_v4_skb_sdif (const struct sk_buff *skb) +{ +return inet_sdif (skb); +} +/* Was this packet marked by Explicit Congestion Notification? */ static int sctp_v4_is_ce (const struct sk_buff *skb) {
@@ -1185,6 +1190,7 @@ static struct sctp_af sctp_af_inet = { .available = sctp_v4_available, .scope = sctp_v4_scope, .skb_iif = sctp_v4_skb_iif, + .skb_sdif = sctp_v4_skb_sdif, .is_ce = sctp_v4_is_ce, .seq_dump_addr = sctp_v4_seq_dump_addr, .ecn_capable = sctp_v4_ecn_capable,
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 2/7 sctp: check ipv6 addr with sk_bound_dev if set
In reply to Xin LongWhen binding to an ipv6 address, it calls ipv6_chk_addr() to check if this address is on any dev. If a socket binds to a l3mdev but no dev is passed to do this check, all l3mdev and slaves will be skipped and the check will fail.
This patch is to pass the bound_dev to make sure the devices under the same l3mdev can be returned in ipv6_chk_addr(). When the bound_dev is not a l3mdev or l3slave, l3mdev_master_dev_rcu() will return NULL in __ipv6_chk_addr_and_flags(), it will keep compitable with before when NULL dev was passed.
--- net/sctp/ipv6.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
net/sctp/ipv6.c +11 -3
@@ -680,9 +680,11 @@ static int sctp_v6_is_any(const union sctp_addr *addr)/* Should this be available for binding? */ static int sctp_v6_available (union sctp_addr *addr,struct sctp_sock *sp) { -int type; -struct net *net =sock_net (&sp->inet.sk);const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr; +struct sock *sk = &sp->inet.sk; +struct net *net =sock_net (sk); +struct net_device *dev =NULL ; +int type; type =ipv6_addr_type (in6);if (IPV6_ADDR_ANY == type)
@@ -696,8 +698,14 @@ static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)if (!(type &IPV6_ADDR_UNICAST ))return 0 ; +if (sk->sk_bound_dev_if) { + dev =dev_get_by_index_rcu (net, sk->sk_bound_dev_if); +if (!dev) +return 0 ; + } +return ipv6_can_nonlocal_bind (net, &sp->inet) || -ipv6_chk_addr (net, in6,NULL ,0 ); +ipv6_chk_addr (net, in6, dev,0 ); }/* This function checks if the address is a valid address to be used for
Signature · 1 line
2.31.1
Xin Long
maintainerPATCH v2 7/7 selftests: add a selftest for sctp vrf
In reply to Xin LongThis patch adds 12 small test cases: 01-04 test for the sysctl net.sctp.l3mdev_accept. 05-10 test for only binding to a right l3mdev device, the connection can be created. 11-12 test for two socks binding to different l3mdev devices at the same time, each of them can process the packets from the corresponding peer. The tests run for both IPv4 and IPv6 SCTP.
--- tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/sctp_hello.c | 137 +++++++++++++++++ tools/testing/selftests/net/sctp_vrf.sh | 178 +++++++++++++++++++++++ 3 files changed, 317 insertions(+) create mode 100644 tools/testing/selftests/net/sctp_hello.c create mode 100755 tools/testing/selftests/net/sctp_vrf.sh
tools/testing/selftests/net/Makefile +2 -0
@@ -72,6 +72,8 @@ TEST_GEN_PROGS += sk_bind_sendto_listen TEST_GEN_PROGS += sk_connect_zero_addr TEST_PROGS += test_ingress_egress_chaining.sh TEST_GEN_PROGS += so_incoming_cpu +TEST_PROGS += sctp_vrf.sh +TEST_GEN_FILES += sctp_hello TEST_FILES := settings
tools/testing/selftests/net/sctp_hello.c new file +137 -0
137 lines not on this page · raw message
tools/testing/selftests/net/sctp_vrf.sh new file +178 -0
178 lines not on this page · raw message
Signature · 1 line
2.31.1
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:
Quoted 8 lines · On Wed, 16 Nov 2022 15:01:15 -0500 you wrote:
This patchset adds the VRF processing in SCTP. Simliar to TCP/UDP, it includes socket bind and socket/association lookup changes.
For socket bind change, it allows sockets to bind to a VRF device and allows multiple sockets with the same IP and PORT to bind to different interfaces in patch 1-3.
[...]
Here is the summary with links:
- [PATCHv2,net-next,1/7] sctp: verify the bind address with the tb_id from l3mdev
https://git.kernel.org/netdev/net-next/c/26943aefa870 - [PATCHv2,net-next,2/7] sctp: check ipv6 addr with sk_bound_dev if set https://git.kernel.org/netdev/net-next/c/6fe1e52490a9 - [PATCHv2,net-next,3/7] sctp: check sk_bound_dev_if when matching ep in get_port https://git.kernel.org/netdev/net-next/c/f87b1ac06c88 - [PATCHv2,net-next,4/7] sctp: add skb_sdif in struct sctp_af https://git.kernel.org/netdev/net-next/c/33e93ed2209d - [PATCHv2,net-next,5/7] sctp: add dif and sdif check in asoc and ep lookup https://git.kernel.org/netdev/net-next/c/0af03170637f - [PATCHv2,net-next,6/7] sctp: add sysctl net.sctp.l3mdev_accept https://git.kernel.org/netdev/net-next/c/b712d0328c2c - [PATCHv2,net-next,7/7] selftests: add a selftest for sctp vrf https://git.kernel.org/netdev/net-next/c/a61bd7b9fef3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html