Skip to content

Commit ba6aac1

Browse files
q2vendavem330
authored andcommitted
udp: Access &udp_table via net.
We will soon introduce an optional per-netns hash table for UDP. This means we cannot use udp_table directly in most places. Instead, access it via net->ipv4.udp_table. The access will be valid only while initialising udp_table itself and creating/destroying each netns. Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 478aee5 commit ba6aac1

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

net/core/filter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6432,7 +6432,7 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
64326432
else
64336433
sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
64346434
dst4, tuple->ipv4.dport,
6435-
dif, sdif, &udp_table, NULL);
6435+
dif, sdif, net->ipv4.udp_table, NULL);
64366436
#if IS_ENABLED(CONFIG_IPV6)
64376437
} else {
64386438
struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
@@ -6448,7 +6448,7 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
64486448
src6, tuple->ipv6.sport,
64496449
dst6, tuple->ipv6.dport,
64506450
dif, sdif,
6451-
&udp_table, NULL);
6451+
net->ipv4.udp_table, NULL);
64526452
#endif
64536453
}
64546454

net/ipv4/udp.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static struct sock *udp4_lookup_run_bpf(struct net *net,
472472
struct sock *sk, *reuse_sk;
473473
bool no_reuseport;
474474

475-
if (udptable != &udp_table)
475+
if (udptable != net->ipv4.udp_table)
476476
return NULL; /* only UDP is supported */
477477

478478
no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP, saddr, sport,
@@ -553,10 +553,11 @@ struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb,
553553
__be16 sport, __be16 dport)
554554
{
555555
const struct iphdr *iph = ip_hdr(skb);
556+
struct net *net = dev_net(skb->dev);
556557

557-
return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
558+
return __udp4_lib_lookup(net, iph->saddr, sport,
558559
iph->daddr, dport, inet_iif(skb),
559-
inet_sdif(skb), &udp_table, NULL);
560+
inet_sdif(skb), net->ipv4.udp_table, NULL);
560561
}
561562

562563
/* Must be called under rcu_read_lock().
@@ -569,7 +570,7 @@ struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
569570
struct sock *sk;
570571

571572
sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport,
572-
dif, 0, &udp_table, NULL);
573+
dif, 0, net->ipv4.udp_table, NULL);
573574
if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
574575
sk = NULL;
575576
return sk;
@@ -807,7 +808,7 @@ int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
807808

808809
int udp_err(struct sk_buff *skb, u32 info)
809810
{
810-
return __udp4_lib_err(skb, info, &udp_table);
811+
return __udp4_lib_err(skb, info, dev_net(skb->dev)->ipv4.udp_table);
811812
}
812813

813814
/*
@@ -2524,13 +2525,14 @@ static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
25242525
__be16 rmt_port, __be32 rmt_addr,
25252526
int dif, int sdif)
25262527
{
2528+
struct udp_table *udptable = net->ipv4.udp_table;
25272529
unsigned short hnum = ntohs(loc_port);
25282530
struct sock *sk, *result;
25292531
struct udp_hslot *hslot;
25302532
unsigned int slot;
25312533

2532-
slot = udp_hashfn(net, hnum, udp_table.mask);
2533-
hslot = &udp_table.hash[slot];
2534+
slot = udp_hashfn(net, hnum, udptable->mask);
2535+
hslot = &udptable->hash[slot];
25342536

25352537
/* Do not bother scanning a too big list */
25362538
if (hslot->count > 10)
@@ -2558,6 +2560,7 @@ static struct sock *__udp4_lib_demux_lookup(struct net *net,
25582560
__be16 rmt_port, __be32 rmt_addr,
25592561
int dif, int sdif)
25602562
{
2563+
struct udp_table *udptable = net->ipv4.udp_table;
25612564
INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
25622565
unsigned short hnum = ntohs(loc_port);
25632566
unsigned int hash2, slot2;
@@ -2566,8 +2569,8 @@ static struct sock *__udp4_lib_demux_lookup(struct net *net,
25662569
struct sock *sk;
25672570

25682571
hash2 = ipv4_portaddr_hash(net, loc_addr, hnum);
2569-
slot2 = hash2 & udp_table.mask;
2570-
hslot2 = &udp_table.hash2[slot2];
2572+
slot2 = hash2 & udptable->mask;
2573+
hslot2 = &udptable->hash2[slot2];
25712574
ports = INET_COMBINED_PORTS(rmt_port, hnum);
25722575

25732576
udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
@@ -2649,7 +2652,7 @@ int udp_v4_early_demux(struct sk_buff *skb)
26492652

26502653
int udp_rcv(struct sk_buff *skb)
26512654
{
2652-
return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
2655+
return __udp4_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP);
26532656
}
26542657

26552658
void udp_destroy_sock(struct sock *sk)

net/ipv4/udp_diag.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ static void udp_dump(struct udp_table *table, struct sk_buff *skb,
147147
static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
148148
const struct inet_diag_req_v2 *r)
149149
{
150-
udp_dump(&udp_table, skb, cb, r);
150+
udp_dump(sock_net(cb->skb->sk)->ipv4.udp_table, skb, cb, r);
151151
}
152152

153153
static int udp_diag_dump_one(struct netlink_callback *cb,
154154
const struct inet_diag_req_v2 *req)
155155
{
156-
return udp_dump_one(&udp_table, cb, req);
156+
return udp_dump_one(sock_net(cb->skb->sk)->ipv4.udp_table, cb, req);
157157
}
158158

159159
static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
@@ -225,7 +225,7 @@ static int __udp_diag_destroy(struct sk_buff *in_skb,
225225
static int udp_diag_destroy(struct sk_buff *in_skb,
226226
const struct inet_diag_req_v2 *req)
227227
{
228-
return __udp_diag_destroy(in_skb, req, &udp_table);
228+
return __udp_diag_destroy(in_skb, req, sock_net(in_skb->sk)->ipv4.udp_table);
229229
}
230230

231231
static int udplite_diag_destroy(struct sk_buff *in_skb,

net/ipv4/udp_offload.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,11 @@ static struct sock *udp4_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
600600
__be16 dport)
601601
{
602602
const struct iphdr *iph = skb_gro_network_header(skb);
603+
struct net *net = dev_net(skb->dev);
603604

604-
return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
605+
return __udp4_lib_lookup(net, iph->saddr, sport,
605606
iph->daddr, dport, inet_iif(skb),
606-
inet_sdif(skb), &udp_table, NULL);
607+
inet_sdif(skb), net->ipv4.udp_table, NULL);
607608
}
608609

609610
INDIRECT_CALLABLE_SCOPE

net/ipv6/udp.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static inline struct sock *udp6_lookup_run_bpf(struct net *net,
217217
struct sock *sk, *reuse_sk;
218218
bool no_reuseport;
219219

220-
if (udptable != &udp_table)
220+
if (udptable != net->ipv4.udp_table)
221221
return NULL; /* only UDP is supported */
222222

223223
no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP, saddr, sport,
@@ -298,10 +298,11 @@ struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
298298
__be16 sport, __be16 dport)
299299
{
300300
const struct ipv6hdr *iph = ipv6_hdr(skb);
301+
struct net *net = dev_net(skb->dev);
301302

302-
return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
303+
return __udp6_lib_lookup(net, &iph->saddr, sport,
303304
&iph->daddr, dport, inet6_iif(skb),
304-
inet6_sdif(skb), &udp_table, NULL);
305+
inet6_sdif(skb), net->ipv4.udp_table, NULL);
305306
}
306307

307308
/* Must be called under rcu_read_lock().
@@ -314,7 +315,7 @@ struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be
314315
struct sock *sk;
315316

316317
sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
317-
dif, 0, &udp_table, NULL);
318+
dif, 0, net->ipv4.udp_table, NULL);
318319
if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
319320
sk = NULL;
320321
return sk;
@@ -689,7 +690,8 @@ static __inline__ int udpv6_err(struct sk_buff *skb,
689690
struct inet6_skb_parm *opt, u8 type,
690691
u8 code, int offset, __be32 info)
691692
{
692-
return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
693+
return __udp6_lib_err(skb, opt, type, code, offset, info,
694+
dev_net(skb->dev)->ipv4.udp_table);
693695
}
694696

695697
static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
@@ -1063,15 +1065,16 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net,
10631065
__be16 rmt_port, const struct in6_addr *rmt_addr,
10641066
int dif, int sdif)
10651067
{
1068+
struct udp_table *udptable = net->ipv4.udp_table;
10661069
unsigned short hnum = ntohs(loc_port);
10671070
unsigned int hash2, slot2;
10681071
struct udp_hslot *hslot2;
10691072
__portpair ports;
10701073
struct sock *sk;
10711074

10721075
hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
1073-
slot2 = hash2 & udp_table.mask;
1074-
hslot2 = &udp_table.hash2[slot2];
1076+
slot2 = hash2 & udptable->mask;
1077+
hslot2 = &udptable->hash2[slot2];
10751078
ports = INET_COMBINED_PORTS(rmt_port, hnum);
10761079

10771080
udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
@@ -1127,7 +1130,7 @@ void udp_v6_early_demux(struct sk_buff *skb)
11271130

11281131
INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
11291132
{
1130-
return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1133+
return __udp6_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP);
11311134
}
11321135

11331136
/*

net/ipv6/udp_offload.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ static struct sock *udp6_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
116116
__be16 dport)
117117
{
118118
const struct ipv6hdr *iph = skb_gro_network_header(skb);
119+
struct net *net = dev_net(skb->dev);
119120

120-
return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
121+
return __udp6_lib_lookup(net, &iph->saddr, sport,
121122
&iph->daddr, dport, inet6_iif(skb),
122-
inet6_sdif(skb), &udp_table, NULL);
123+
inet6_sdif(skb), net->ipv4.udp_table, NULL);
123124
}
124125

125126
INDIRECT_CALLABLE_SCOPE

0 commit comments

Comments
 (0)