Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

l2tp: Support several sockets with same IP/port quadruple

Some l2tp providers will use 1701 as origin port and open several
tunnels for the same origin and target. On the Linux side, this
may mean opening several sockets, but then trafic will go to only
one of them, losing the trafic for the tunnel of the other socket
(or leaving it up to userland, consuming a lot of cpu%).

This can also happen when the l2tp provider uses a cluster, and
load-balancing happens to migrate from one origin IP to another one,
for which a socket was already established. Managing reassigning
tunnels from one socket to another would be very hairy for userland.

Lastly, as documented in l2tpconfig(1), as client it may be necessary
to use 1701 as origin port for odd firewalls reasons, which could
prevent from establishing several tunnels to a l2tp server, for the
same reason: trafic would get only on one of the two sockets.

With the V2 protocol it is however easy to route trafic to the proper
tunnel, by looking up the tunnel number in the network namespace. This
fixes the three cases altogether.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Link: https://lore.kernel.org/r/20240506215336.1470009-1-samuel.thibault@ens-lyon.org
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Samuel Thibault and committed by
Paolo Abeni
628bc3e5 83127eca

+21
+21
net/l2tp/l2tp_core.c
··· 794 794 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb) 795 795 { 796 796 struct l2tp_session *session = NULL; 797 + struct l2tp_tunnel *orig_tunnel = tunnel; 797 798 unsigned char *ptr, *optr; 798 799 u16 hdrflags; 799 800 u32 tunnel_id, session_id; ··· 846 845 /* Extract tunnel and session ID */ 847 846 tunnel_id = ntohs(*(__be16 *)ptr); 848 847 ptr += 2; 848 + 849 + if (tunnel_id != tunnel->tunnel_id) { 850 + /* We are receiving trafic for another tunnel, probably 851 + * because we have several tunnels between the same 852 + * IP/port quadruple, look it up. 853 + */ 854 + struct l2tp_tunnel *alt_tunnel; 855 + 856 + alt_tunnel = l2tp_tunnel_get(tunnel->l2tp_net, tunnel_id); 857 + if (!alt_tunnel || alt_tunnel->version != L2TP_HDR_VER_2) 858 + goto pass; 859 + tunnel = alt_tunnel; 860 + } 861 + 849 862 session_id = ntohs(*(__be16 *)ptr); 850 863 ptr += 2; 851 864 } else { ··· 890 875 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length); 891 876 l2tp_session_dec_refcount(session); 892 877 878 + if (tunnel != orig_tunnel) 879 + l2tp_tunnel_dec_refcount(tunnel); 880 + 893 881 return 0; 894 882 895 883 invalid: ··· 901 883 pass: 902 884 /* Put UDP header back */ 903 885 __skb_push(skb, sizeof(struct udphdr)); 886 + 887 + if (tunnel != orig_tunnel) 888 + l2tp_tunnel_dec_refcount(tunnel); 904 889 905 890 return 1; 906 891 }