Skip to content

Commit 6462de8

Browse files
committed
Merge branch 'l3mdev-accept'
David Ahern says: ==================== net: Allow accepted sockets to be bound to l3mdev domain Allow accepted sockets to derive their sk_bound_dev_if setting from the l3mdev domain in which the packets originated. This version adds a sysctl to control whether the setting is inherited, making the functionality similar to sk_mark and its sysctl_tcp_fwmark_accept setting. This effectively allow a process to have a "VRF-global" listen socket, with child sockets bound to the VRF device in which the packet originated. A similar behavior can be achieved using sk_mark, but a solution using marks is incomplete as it does not handle duplicate addresses in different L3 domains/VRFs. Allowing sockets to inherit the sk_bound_dev_if from l3mdev domain provides a complete solution. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents cc9da6c + 6dd9a14 commit 6462de8

9 files changed

Lines changed: 65 additions & 5 deletions

File tree

‎Documentation/networking/ip-sysctl.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ tcp_keepalive_intvl - INTEGER
335335
after probes started. Default value: 75sec i.e. connection
336336
will be aborted after ~11 minutes of retries.
337337

338+
tcp_l3mdev_accept - BOOLEAN
339+
Enables child sockets to inherit the L3 master device index.
340+
Enabling this option allows a "global" listen socket to work
341+
across L3 master domains (e.g., VRFs) with connected sockets
342+
derived from the listen socket to be bound to the L3 domain in
343+
which the packets originated. Only valid when the kernel was
344+
compiled with CONFIG_NET_L3_MASTER_DEV.
345+
338346
tcp_low_latency - BOOLEAN
339347
If set, the TCP stack makes decisions that prefer lower
340348
latency as opposed to higher throughput. By default, this

‎include/net/inet_sock.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <net/request_sock.h>
2929
#include <net/netns/hash.h>
3030
#include <net/tcp_states.h>
31+
#include <net/l3mdev.h>
3132

3233
/** struct ip_options - IP Options
3334
*
@@ -113,6 +114,19 @@ static inline u32 inet_request_mark(const struct sock *sk, struct sk_buff *skb)
113114
return sk->sk_mark;
114115
}
115116

117+
static inline int inet_request_bound_dev_if(const struct sock *sk,
118+
struct sk_buff *skb)
119+
{
120+
#ifdef CONFIG_NET_L3_MASTER_DEV
121+
struct net *net = sock_net(sk);
122+
123+
if (!sk->sk_bound_dev_if && net->ipv4.sysctl_tcp_l3mdev_accept)
124+
return l3mdev_master_ifindex_by_index(net, skb->skb_iif);
125+
#endif
126+
127+
return sk->sk_bound_dev_if;
128+
}
129+
116130
struct inet_cork {
117131
unsigned int flags;
118132
__be32 addr;

‎include/net/l3mdev.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ static inline int l3mdev_master_ifindex(struct net_device *dev)
5151
return ifindex;
5252
}
5353

54+
static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
55+
{
56+
struct net_device *dev;
57+
int rc = 0;
58+
59+
if (likely(ifindex)) {
60+
rcu_read_lock();
61+
62+
dev = dev_get_by_index_rcu(net, ifindex);
63+
if (dev)
64+
rc = l3mdev_master_ifindex_rcu(dev);
65+
66+
rcu_read_unlock();
67+
}
68+
69+
return rc;
70+
}
71+
5472
/* get index of an interface to use for FIB lookups. For devices
5573
* enslaved to an L3 master device FIB lookups are based on the
5674
* master index
@@ -167,6 +185,11 @@ static inline int l3mdev_master_ifindex(struct net_device *dev)
167185
return 0;
168186
}
169187

188+
static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
189+
{
190+
return 0;
191+
}
192+
170193
static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
171194
{
172195
return dev ? dev->ifindex : 0;

‎include/net/netns/ipv4.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ struct netns_ipv4 {
8686

8787
int sysctl_fwmark_reflect;
8888
int sysctl_tcp_fwmark_accept;
89+
#ifdef CONFIG_NET_L3_MASTER_DEV
90+
int sysctl_tcp_l3mdev_accept;
91+
#endif
8992
int sysctl_tcp_mtu_probing;
9093
int sysctl_tcp_base_mss;
9194
int sysctl_tcp_probe_threshold;

‎net/ipv4/syncookies.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
351351
treq->snt_synack.v64 = 0;
352352
treq->tfo_listener = false;
353353

354-
ireq->ir_iif = sk->sk_bound_dev_if;
354+
ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
355355

356356
/* We throwed the options of the initial SYN away, so we hope
357357
* the ACK carries the same options again (see RFC1122 4.2.3.8)
@@ -371,7 +371,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
371371
* hasn't changed since we received the original syn, but I see
372372
* no easy way to do this.
373373
*/
374-
flowi4_init_output(&fl4, sk->sk_bound_dev_if, ireq->ir_mark,
374+
flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark,
375375
RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
376376
inet_sk_flowi_flags(sk),
377377
opt->srr ? opt->faddr : ireq->ir_rmt_addr,

‎net/ipv4/sysctl_net_ipv4.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,17 @@ static struct ctl_table ipv4_net_table[] = {
915915
.mode = 0644,
916916
.proc_handler = proc_dointvec,
917917
},
918+
#ifdef CONFIG_NET_L3_MASTER_DEV
919+
{
920+
.procname = "tcp_l3mdev_accept",
921+
.data = &init_net.ipv4.sysctl_tcp_l3mdev_accept,
922+
.maxlen = sizeof(int),
923+
.mode = 0644,
924+
.proc_handler = proc_dointvec_minmax,
925+
.extra1 = &zero,
926+
.extra2 = &one,
927+
},
928+
#endif
918929
{
919930
.procname = "tcp_mtu_probing",
920931
.data = &init_net.ipv4.sysctl_tcp_mtu_probing,

‎net/ipv4/tcp_input.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6204,7 +6204,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
62046204
tcp_openreq_init(req, &tmp_opt, skb, sk);
62056205

62066206
/* Note: tcp_v6_init_req() might override ir_iif for link locals */
6207-
inet_rsk(req)->ir_iif = sk->sk_bound_dev_if;
6207+
inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
62086208

62096209
af_ops->init_req(req, sk, skb);
62106210

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
12761276
ireq = inet_rsk(req);
12771277
sk_daddr_set(newsk, ireq->ir_rmt_addr);
12781278
sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
1279+
newsk->sk_bound_dev_if = ireq->ir_iif;
12791280
newinet->inet_saddr = ireq->ir_loc_addr;
12801281
inet_opt = ireq->opt;
12811282
rcu_assign_pointer(newinet->inet_opt, inet_opt);

‎net/ipv6/syncookies.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
193193
ireq->pktopts = skb;
194194
}
195195

196-
ireq->ir_iif = sk->sk_bound_dev_if;
196+
ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
197197
/* So that link locals have meaning */
198198
if (!sk->sk_bound_dev_if &&
199199
ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
@@ -224,7 +224,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
224224
fl6.daddr = ireq->ir_v6_rmt_addr;
225225
final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
226226
fl6.saddr = ireq->ir_v6_loc_addr;
227-
fl6.flowi6_oif = sk->sk_bound_dev_if;
227+
fl6.flowi6_oif = ireq->ir_iif;
228228
fl6.flowi6_mark = ireq->ir_mark;
229229
fl6.fl6_dport = ireq->ir_rmt_port;
230230
fl6.fl6_sport = inet_sk(sk)->inet_sport;

0 commit comments

Comments
 (0)