Skip to content

Commit 2ca7b0a

Browse files
herbertxDavid S. Miller
authored andcommitted
[NETFILTER]: Avoid skb_copy/pskb_copy/skb_realloc_headroom
This patch replaces unnecessary uses of skb_copy, pskb_copy and skb_realloc_headroom by functions such as skb_make_writable and pskb_expand_head. This allows us to remove the double pointers later. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent af1e1cf commit 2ca7b0a

10 files changed

Lines changed: 45 additions & 117 deletions

File tree

‎net/bridge/netfilter/ebt_dnat.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
*/
1010

11+
#include <linux/netfilter.h>
1112
#include <linux/netfilter_bridge/ebtables.h>
1213
#include <linux/netfilter_bridge/ebt_nat.h>
1314
#include <linux/module.h>
@@ -19,17 +20,9 @@ static int ebt_target_dnat(struct sk_buff **pskb, unsigned int hooknr,
1920
{
2021
struct ebt_nat_info *info = (struct ebt_nat_info *)data;
2122

22-
if (skb_shared(*pskb) || skb_cloned(*pskb)) {
23-
struct sk_buff *nskb;
23+
if (skb_make_writable(*pskb, 0))
24+
return NF_DROP;
2425

25-
nskb = skb_copy(*pskb, GFP_ATOMIC);
26-
if (!nskb)
27-
return NF_DROP;
28-
if ((*pskb)->sk)
29-
skb_set_owner_w(nskb, (*pskb)->sk);
30-
kfree_skb(*pskb);
31-
*pskb = nskb;
32-
}
3326
memcpy(eth_hdr(*pskb)->h_dest, info->mac, ETH_ALEN);
3427
return info->target;
3528
}

‎net/bridge/netfilter/ebt_redirect.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
*/
1010

11+
#include <linux/netfilter.h>
1112
#include <linux/netfilter_bridge/ebtables.h>
1213
#include <linux/netfilter_bridge/ebt_redirect.h>
1314
#include <linux/module.h>
@@ -20,17 +21,9 @@ static int ebt_target_redirect(struct sk_buff **pskb, unsigned int hooknr,
2021
{
2122
struct ebt_redirect_info *info = (struct ebt_redirect_info *)data;
2223

23-
if (skb_shared(*pskb) || skb_cloned(*pskb)) {
24-
struct sk_buff *nskb;
24+
if (skb_make_writable(*pskb, 0))
25+
return NF_DROP;
2526

26-
nskb = skb_copy(*pskb, GFP_ATOMIC);
27-
if (!nskb)
28-
return NF_DROP;
29-
if ((*pskb)->sk)
30-
skb_set_owner_w(nskb, (*pskb)->sk);
31-
kfree_skb(*pskb);
32-
*pskb = nskb;
33-
}
3427
if (hooknr != NF_BR_BROUTING)
3528
memcpy(eth_hdr(*pskb)->h_dest,
3629
in->br_port->br->dev->dev_addr, ETH_ALEN);

‎net/bridge/netfilter/ebt_snat.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
*/
1010

11+
#include <linux/netfilter.h>
1112
#include <linux/netfilter_bridge/ebtables.h>
1213
#include <linux/netfilter_bridge/ebt_nat.h>
1314
#include <linux/module.h>
@@ -21,17 +22,9 @@ static int ebt_target_snat(struct sk_buff **pskb, unsigned int hooknr,
2122
{
2223
struct ebt_nat_info *info = (struct ebt_nat_info *) data;
2324

24-
if (skb_shared(*pskb) || skb_cloned(*pskb)) {
25-
struct sk_buff *nskb;
25+
if (skb_make_writable(*pskb, 0))
26+
return NF_DROP;
2627

27-
nskb = skb_copy(*pskb, GFP_ATOMIC);
28-
if (!nskb)
29-
return NF_DROP;
30-
if ((*pskb)->sk)
31-
skb_set_owner_w(nskb, (*pskb)->sk);
32-
kfree_skb(*pskb);
33-
*pskb = nskb;
34-
}
3528
memcpy(eth_hdr(*pskb)->h_source, info->mac, ETH_ALEN);
3629
if (!(info->target & NAT_ARP_BIT) &&
3730
eth_hdr(*pskb)->h_proto == htons(ETH_P_ARP)) {

‎net/ipv4/netfilter.c‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/netfilter.h>
44
#include <linux/netfilter_ipv4.h>
55
#include <linux/ip.h>
6+
#include <linux/skbuff.h>
67
#include <net/route.h>
78
#include <net/xfrm.h>
89
#include <net/ip.h>
@@ -66,17 +67,10 @@ int ip_route_me_harder(struct sk_buff **pskb, unsigned addr_type)
6667

6768
/* Change in oif may mean change in hh_len. */
6869
hh_len = (*pskb)->dst->dev->hard_header_len;
69-
if (skb_headroom(*pskb) < hh_len) {
70-
struct sk_buff *nskb;
71-
72-
nskb = skb_realloc_headroom(*pskb, hh_len);
73-
if (!nskb)
74-
return -1;
75-
if ((*pskb)->sk)
76-
skb_set_owner_w(nskb, (*pskb)->sk);
77-
kfree_skb(*pskb);
78-
*pskb = nskb;
79-
}
70+
if (skb_headroom(*pskb) < hh_len &&
71+
pskb_expand_head(*pskb, hh_len - skb_headroom(*pskb), 0,
72+
GFP_ATOMIC))
73+
return -1;
8074

8175
return 0;
8276
}
@@ -107,17 +101,10 @@ int ip_xfrm_me_harder(struct sk_buff **pskb)
107101

108102
/* Change in oif may mean change in hh_len. */
109103
hh_len = (*pskb)->dst->dev->hard_header_len;
110-
if (skb_headroom(*pskb) < hh_len) {
111-
struct sk_buff *nskb;
112-
113-
nskb = skb_realloc_headroom(*pskb, hh_len);
114-
if (!nskb)
115-
return -1;
116-
if ((*pskb)->sk)
117-
skb_set_owner_w(nskb, (*pskb)->sk);
118-
kfree_skb(*pskb);
119-
*pskb = nskb;
120-
}
104+
if (skb_headroom(*pskb) < hh_len &&
105+
pskb_expand_head(*pskb, hh_len - skb_headroom(*pskb), 0,
106+
GFP_ATOMIC))
107+
return -1;
121108
return 0;
122109
}
123110
EXPORT_SYMBOL(ip_xfrm_me_harder);

‎net/ipv4/netfilter/arpt_mangle.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* module that allows mangling of the arp payload */
22
#include <linux/module.h>
3+
#include <linux/netfilter.h>
34
#include <linux/netfilter_arp/arpt_mangle.h>
45
#include <net/sock.h>
56

@@ -18,17 +19,8 @@ target(struct sk_buff **pskb,
1819
unsigned char *arpptr;
1920
int pln, hln;
2021

21-
if (skb_shared(*pskb) || skb_cloned(*pskb)) {
22-
struct sk_buff *nskb;
23-
24-
nskb = skb_copy(*pskb, GFP_ATOMIC);
25-
if (!nskb)
26-
return NF_DROP;
27-
if ((*pskb)->sk)
28-
skb_set_owner_w(nskb, (*pskb)->sk);
29-
kfree_skb(*pskb);
30-
*pskb = nskb;
31-
}
22+
if (skb_make_writable(*pskb, (*pskb)->len))
23+
return NF_DROP;
3224

3325
arp = arp_hdr(*pskb);
3426
arpptr = skb_network_header(*pskb) + sizeof(*arp);

‎net/ipv4/netfilter/ip_queue.c‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ static int
335335
ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
336336
{
337337
int diff;
338+
int err;
338339
struct iphdr *user_iph = (struct iphdr *)v->payload;
339340

340341
if (v->data_len < sizeof(*user_iph))
@@ -347,21 +348,14 @@ ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
347348
if (v->data_len > 0xFFFF)
348349
return -EINVAL;
349350
if (diff > skb_tailroom(e->skb)) {
350-
struct sk_buff *newskb;
351-
352-
newskb = skb_copy_expand(e->skb,
353-
skb_headroom(e->skb),
354-
diff,
355-
GFP_ATOMIC);
356-
if (newskb == NULL) {
357-
printk(KERN_WARNING "ip_queue: OOM "
358-
"in mangle, dropping packet\n");
359-
return -ENOMEM;
351+
err = pskb_expand_head(e->skb, 0,
352+
diff - skb_tailroom(e->skb),
353+
GFP_ATOMIC);
354+
if (err) {
355+
printk(KERN_WARNING "ip_queue: error "
356+
"in mangle, dropping packet: %d\n", -err);
357+
return err;
360358
}
361-
if (e->skb->sk)
362-
skb_set_owner_w(newskb, e->skb->sk);
363-
kfree_skb(e->skb);
364-
e->skb = newskb;
365359
}
366360
skb_put(e->skb, diff);
367361
}

‎net/ipv4/netfilter/nf_nat_helper.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,12 @@ static void mangle_contents(struct sk_buff *skb,
113113
/* Unusual, but possible case. */
114114
static int enlarge_skb(struct sk_buff **pskb, unsigned int extra)
115115
{
116-
struct sk_buff *nskb;
117-
118116
if ((*pskb)->len + extra > 65535)
119117
return 0;
120118

121-
nskb = skb_copy_expand(*pskb, skb_headroom(*pskb), extra, GFP_ATOMIC);
122-
if (!nskb)
119+
if (pskb_expand_head(*pskb, 0, extra - skb_tailroom(*pskb), GFP_ATOMIC))
123120
return 0;
124121

125-
/* Transfer socket to new skb. */
126-
if ((*pskb)->sk)
127-
skb_set_owner_w(nskb, (*pskb)->sk);
128-
kfree_skb(*pskb);
129-
*pskb = nskb;
130122
return 1;
131123
}
132124

‎net/ipv6/netfilter/ip6_queue.c‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ static int
332332
ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
333333
{
334334
int diff;
335+
int err;
335336
struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
336337

337338
if (v->data_len < sizeof(*user_iph))
@@ -344,21 +345,14 @@ ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
344345
if (v->data_len > 0xFFFF)
345346
return -EINVAL;
346347
if (diff > skb_tailroom(e->skb)) {
347-
struct sk_buff *newskb;
348-
349-
newskb = skb_copy_expand(e->skb,
350-
skb_headroom(e->skb),
351-
diff,
352-
GFP_ATOMIC);
353-
if (newskb == NULL) {
348+
err = pskb_expand_head(e->skb, 0,
349+
diff - skb_tailroom(e->skb),
350+
GFP_ATOMIC);
351+
if (err) {
354352
printk(KERN_WARNING "ip6_queue: OOM "
355353
"in mangle, dropping packet\n");
356-
return -ENOMEM;
354+
return err;
357355
}
358-
if (e->skb->sk)
359-
skb_set_owner_w(newskb, e->skb->sk);
360-
kfree_skb(e->skb);
361-
e->skb = newskb;
362356
}
363357
skb_put(e->skb, diff);
364358
}

‎net/netfilter/nfnetlink_queue.c‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ static int
617617
nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
618618
{
619619
int diff;
620+
int err;
620621

621622
diff = data_len - e->skb->len;
622623
if (diff < 0) {
@@ -626,21 +627,14 @@ nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
626627
if (data_len > 0xFFFF)
627628
return -EINVAL;
628629
if (diff > skb_tailroom(e->skb)) {
629-
struct sk_buff *newskb;
630-
631-
newskb = skb_copy_expand(e->skb,
632-
skb_headroom(e->skb),
633-
diff,
634-
GFP_ATOMIC);
635-
if (newskb == NULL) {
630+
err = pskb_expand_head(e->skb, 0,
631+
diff - skb_tailroom(e->skb),
632+
GFP_ATOMIC);
633+
if (err) {
636634
printk(KERN_WARNING "nf_queue: OOM "
637635
"in mangle, dropping packet\n");
638-
return -ENOMEM;
636+
return err;
639637
}
640-
if (e->skb->sk)
641-
skb_set_owner_w(newskb, e->skb->sk);
642-
kfree_skb(e->skb);
643-
e->skb = newskb;
644638
}
645639
skb_put(e->skb, diff);
646640
}

‎net/netfilter/xt_TCPMSS.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,10 @@ tcpmss_mangle_packet(struct sk_buff **pskb,
105105
* MSS Option not found ?! add it..
106106
*/
107107
if (skb_tailroom((*pskb)) < TCPOLEN_MSS) {
108-
struct sk_buff *newskb;
109-
110-
newskb = skb_copy_expand(*pskb, skb_headroom(*pskb),
111-
TCPOLEN_MSS, GFP_ATOMIC);
112-
if (!newskb)
108+
if (pskb_expand_head(*pskb, 0,
109+
TCPOLEN_MSS - skb_tailroom(*pskb),
110+
GFP_ATOMIC))
113111
return -1;
114-
kfree_skb(*pskb);
115-
*pskb = newskb;
116112
tcph = (struct tcphdr *)(skb_network_header(*pskb) + tcphoff);
117113
}
118114

0 commit comments

Comments
 (0)