Skip to contentSkip to conversation

the TSO packet mangling issue

PATCH Applied Herbert Xu 3 participants 13 messages Jan 27, 2005 – Feb 24, 2005 6 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Herbert Xu

author
#6 lore raw

Hi:

OK, here are the patches to COW TSO packets before the TCP/TCP header gets written over.

This one defines a new bit of dataref. It's set just before a packet joins the TCP write queue. It's cleared after the packet is taken off the queue and just before it's freed.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

===== include/linux/skbuff.h 1.59 vs edited =====

include/linux/skbuff.h +5 -1
@@ -134,6 +134,9 @@
 	__u16 size;
 };
 
+#define SKB_DATAREF_TCP 0x40000000
+#define SKB_DATAREF_MASK 0x3fffffff
+
 /* This data is invariant across clones and lives at
  * the end of the header data, ie. at skb->end.
  */
@@ -374,7 +377,8 @@
  */
 static inline int skb_cloned(const struct sk_buff *skb)
 {
-	return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
+	return skb->cloned &&
+	       (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
 }
 
 /**

===== include/net/tcp.h 1.103 vs edited =====

include/net/tcp.h +21 -0
@@ -1222,6 +1222,27 @@
 	return (tp->packets_out - tp->left_out + tp->retrans_out);
 }
 
+/* This marks the skb as owned by the TCP stack.  The TCP stack
+ * isn't fussy about others modifying the skb's content as long
+ * as they do so within the TCP/IP headers and leave the TCP
+ * payload alone.
+ */
+static inline void tcp_skb_hold(struct sk_buff *skb)
+{
+	atomic_add(SKB_DATAREF_TCP, &skb_shinfo(skb)->dataref);
+}
+
+static inline void tcp_skb_release(struct sk_buff *skb)
+{
+	atomic_sub(SKB_DATAREF_TCP, &skb_shinfo(skb)->dataref);
+}
+
+static inline void tcp_sk_stream_free_skb(struct sock *sk, struct sk_buff *skb)
+{
+	tcp_skb_release(skb);
+	sk_stream_free_skb(sk, skb);
+}
+
 /*
  * Which congestion algorithim is in use on the connection.
  */

===== net/ipv4/tcp.c 1.91 vs edited =====

net/ipv4/tcp.c +2 -1
@@ -598,6 +598,7 @@
 	TCP_SKB_CB(skb)->end_seq = tp->write_seq;
 	TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
 	TCP_SKB_CB(skb)->sacked = 0;
+	tcp_skb_hold(skb);
 	__skb_queue_tail(&sk->sk_write_queue, skb);
 	sk_charge_skb(sk, skb);
 	if (!sk->sk_send_head)
@@ -990,7 +991,7 @@
 		if (sk->sk_send_head == skb)
 			sk->sk_send_head = NULL;
 		__skb_unlink(skb, skb->list);
-		sk_stream_free_skb(sk, skb);
+		tcp_sk_stream_free_skb(sk, skb);
 	}
 
 do_error:

===== net/ipv4/tcp_input.c 1.89 vs edited =====

net/ipv4/tcp_input.c +1 -1
@@ -2470,7 +2470,7 @@
 		tcp_dec_pcount_approx(&tp->fackets_out, skb);
 		tcp_packets_out_dec(tp, skb);
 		__skb_unlink(skb, skb->list);
-		sk_stream_free_skb(sk, skb);
+		tcp_sk_stream_free_skb(sk, skb);
 	}
 
 	if (acked&FLAG_ACKED) {

===== net/ipv4/tcp_output.c 1.77 vs edited =====

net/ipv4/tcp_output.c +5 -2
@@ -400,6 +400,7 @@
 
 	/* Advance write_seq and place onto the write_queue. */
 	tp->write_seq = TCP_SKB_CB(skb)->end_seq;
+	tcp_skb_hold(skb);
 	__skb_queue_tail(&sk->sk_write_queue, skb);
 	sk_charge_skb(sk, skb);
 
@@ -960,7 +961,7 @@
 		 */
 		tcp_dec_pcount_approx(&tp->fackets_out, next_skb);
 		tcp_packets_out_dec(tp, next_skb);
-		sk_stream_free_skb(sk, next_skb);
+		tcp_sk_stream_free_skb(sk, next_skb);
 	}
 }
 
@@ -1327,8 +1328,9 @@
 			if (nskb == NULL)
 				return -ENOMEM;
 			__skb_unlink(skb, &sk->sk_write_queue);
+			tcp_skb_hold(nskb);
 			__skb_queue_head(&sk->sk_write_queue, nskb);
-			sk_stream_free_skb(sk, skb);
+			tcp_sk_stream_free_skb(sk, skb);
 			sk_charge_skb(sk, nskb);
 			skb = nskb;
 		}
@@ -1493,6 +1495,7 @@
 	/* Send it off. */
 	TCP_SKB_CB(buff)->when = tcp_time_stamp;
 	tp->retrans_stamp = TCP_SKB_CB(buff)->when;
+	tcp_skb_hold(buff);
 	__skb_queue_tail(&sk->sk_write_queue, buff);
 	sk_charge_skb(sk, buff);
 	tp->packets_out += tcp_skb_pcount(buff);
author
#7 lore raw

This patch adds tcp_skb_cow() and uses it in e1000/tg3.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

===== drivers/net/tg3.c 1.231 vs edited =====

drivers/net/tg3.c +4 -0
@@ -32,6 +32,7 @@
 #include <linux/workqueue.h>
 
 #include <net/checksum.h>
+#include <net/tcp.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
@@ -3102,6 +3103,9 @@
 	if (skb->len > (tp->dev->mtu + ETH_HLEN) &&
 	    (mss = skb_shinfo(skb)->tso_size) != 0) {
 		int tcp_opt_len, ip_tcp_len;
+
+		if (tcp_skb_cow(skb))
+			goto out_unlock;
 
 		tcp_opt_len = ((skb->h.th->doff - 5) * 4);
 		ip_tcp_len = (skb->nh.iph->ihl * 4) + sizeof(struct tcphdr);

===== drivers/net/e1000/e1000.h 1.60 vs edited =====

drivers/net/e1000/e1000.h +1 -0
@@ -67,6 +67,7 @@
 #include <linux/reboot.h>
 #ifdef NETIF_F_TSO
 #include <net/checksum.h>
+#include <net/tcp.h>
 #endif
 #include <linux/workqueue.h>
 #include <linux/mii.h>

===== drivers/net/e1000/e1000_main.c 1.147 vs edited =====

drivers/net/e1000/e1000_main.c +16 -4
@@ -1522,7 +1522,7 @@
 #define E1000_TX_FLAGS_VLAN_MASK	0xffff0000
 #define E1000_TX_FLAGS_VLAN_SHIFT	16
 
-static inline boolean_t
+static inline int
 e1000_tso(struct e1000_adapter *adapter, struct sk_buff *skb)
 {
 #ifdef NETIF_F_TSO
@@ -1531,8 +1531,13 @@
 	uint32_t cmd_length = 0;
 	uint16_t ipcse, tucse, mss;
 	uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
+	int err;
 
 	if(skb_shinfo(skb)->tso_size) {
+		err = tcp_skb_cow(skb);
+		if (err)
+			return err;
+
 		hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
 		mss = skb_shinfo(skb)->tso_size;
 		skb->nh.iph->tot_len = 0;
@@ -1569,11 +1574,11 @@
 		if(++i == adapter->tx_ring.count) i = 0;
 		adapter->tx_ring.next_to_use = i;
 
-		return TRUE;
+		return 1;
 	}
 #endif
 
-	return FALSE;
+	return 0;
 }
 
 static inline boolean_t
@@ -1798,6 +1803,7 @@
 	unsigned int nr_frags = 0;
 	unsigned int mss = 0;
 	int count = 0;
+	int tso;
 	unsigned int f;
 	len -= skb->data_len;
 
@@ -1869,7 +1875,13 @@
 
 	first = adapter->tx_ring.next_to_use;
 	
-	if(likely(e1000_tso(adapter, skb)))
+	tso = e1000_tso(adapter, skb);
+	if (tso < 0) {
+		dev_kfree_skb_any(skb);
+		return NETDEV_TX_OK;
+	}
+
+	if (likely(tso))
 		tx_flags |= E1000_TX_FLAGS_TSO;
 	else if(likely(e1000_tx_csum(adapter, skb)))
 		tx_flags |= E1000_TX_FLAGS_CSUM;

===== include/net/tcp.h 1.104 vs edited =====

include/net/tcp.h +19 -0
@@ -1243,6 +1243,25 @@
 	sk_stream_free_skb(sk, skb);
 }
 
+/* Return an skb which is writeable up to the point where the
+ * TCP payload starts.
+ */
+static inline int tcp_skb_cow(struct sk_buff *skb)
+{
+	int dataref;
+
+	if (!skb->cloned)
+		return 0;
+
+	dataref = atomic_read(&skb_shinfo(skb)->dataref);
+	dataref -= !!(dataref & SKB_DATAREF_TCP);
+	dataref &= SKB_DATAREF_MASK;
+	if (dataref == 1)
+		return 0;
+
+	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
+}
+
 /*
  * Which congestion algorithim is in use on the connection.
  */
#8 lore raw

Hello!

Quoted 1 line

This patch adds tcp_skb_cow() and uses it in e1000/tg3.

Looks almost nice to me.

Actually, I would prefer to unbind this from tcp, it can be used for something else, sctp or anything else who sends clones of skbs and wants to protect only data part.

Also, tcp_skb_cow() is not good. F.e. the first thing, which I thought of, was WLAN drivers which rewrite ethernet header and either screw up tcpdumps (and break net/bridge) or have to copy each tcp skb. I used to patch it with:

driver/if_ath.c 2004-06-04 11:06:43.000000000 +0400 renamed from madwifi-cvs.current/madwifi/driver/if_ath.c 2004-06-08 17:38:20.587760856 +0400 +3 -1
diff -ru ../madwifi-cvs.current/madwifi/driver/if_ath.c madwifi/driver/if_ath.c
@@ -759,7 +759,9 @@
 
        if (ic->ic_flags & IEEE80211_F_WEPON)
                len += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
-       if ((skb_headroom(skb) < len) &&
+
+       if (len < skb_headroom(skb)) len=skb_headroom(skb);
+       if ((skb_cloned(skb) || (skb_headroom(skb) < len)) &&
            pskb_expand_head(skb, len - skb_headroom(skb), 0, GFP_ATOMIC)) {
                dev_kfree_skb(skb);
                return -ENOMEM;

I think it would be good to make a function sort of:

static inline int skb_header_cloned(struct sk_buff *skb)

{
	int dataref;

	if (!skb->cloned)
		return 0;

	dataref = atomic_read(&skb_shinfo(skb)->dataref);
	dataref -= !!(dataref & SKB_DATAREF_TCP);
	dataref &= SKB_DATAREF_MASK;
	return (dataref != 1);
}

In this case many of skb_cloned() could be just replaced with skb_header_cloned().

Alexey

author
#9 lore raw

PATCH 1/2 Add skb_header_release and use it in net/ipv4/tcp

Quoted 4 lines · Alexey Kuznetsov

Actually, I would prefer to unbind this from tcp, it can be used for something else, sctp or anything else who sends clones of skbs and wants to protect only data part.

Good point. Let's generalise it into a counter as well.

This patch adds skb_header_release which can be called when the owner of an skb no longer needs to access the header at all. What constitutes the header is left up to the users of the skb to define.

For instance, for outbound TCP packets we define the header to be anything in front of the TCP payload. Therefore we add skb_header_release calls to all the paths where outound TCP packets are produced.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

===== include/linux/skbuff.h 1.59 vs edited =====

include/linux/skbuff.h +34 -2
@@ -146,6 +146,20 @@
 	skb_frag_t	frags[MAX_SKB_FRAGS];
 };
 
+/* We divide dataref into two halves.  The higher 16 bits hold references
+ * to the payload part of skb->data.  The lower 16 bits hold references to
+ * the entire skb->data.  It is up to the users of the skb to agree on
+ * where the payload starts.
+ *
+ * All users must obey the rule that the skb->data reference count must be
+ * greater than or equal to the payload reference count.
+ *
+ * Holding a reference to the payload part means that the user does not
+ * care about modifications to the header part of skb->data.
+ */
+#define SKB_DATAREF_SHIFT 16
+#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
+
 /** 
  *	struct sk_buff - socket buffer
  *	@next: Next buffer in list
@@ -167,6 +181,7 @@
  *	@csum: Checksum
  *	@__unused: Dead field, may be reused
  *	@cloned: Head may be cloned (check refcnt to be sure)
+ *	@nohdr: Payload reference only, must not modify header
  *	@pkt_type: Packet class
  *	@ip_summed: Driver fed us an IP checksum
  *	@priority: Packet queueing priority
@@ -238,7 +253,8 @@
 				mac_len,
 				csum;
 	unsigned char		local_df,
-				cloned,
+				cloned:1,
+				nohdr:1,
 				pkt_type,
 				ip_summed;
 	__u32			priority;
@@ -374,7 +390,23 @@
  */
 static inline int skb_cloned(const struct sk_buff *skb)
 {
-	return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
+	return skb->cloned &&
+	       (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
+}
+
+/**
+ *	skb_header_release - release reference to header
+ *	@skb: buffer to operate on
+ *
+ *	Drop a reference to the header part of the buffer.  This is done
+ *	by acquiring a payload reference.  You must not read from the header
+ *	part of skb->data after this.
+ */
+static inline void skb_header_release(struct sk_buff *skb)
+{
+	BUG_ON(skb->nohdr);
+	skb->nohdr = 1;
+	atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
 }
 
 /**

===== net/core/skbuff.c 1.41 vs edited =====

net/core/skbuff.c +4 -1
@@ -241,7 +241,8 @@
 void skb_release_data(struct sk_buff *skb)
 {
 	if (!skb->cloned ||
-	    atomic_dec_and_test(&(skb_shinfo(skb)->dataref))) {
+	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
+			       &skb_shinfo(skb)->dataref)) {
 		if (skb_shinfo(skb)->nr_frags) {
 			int i;
 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
@@ -353,6 +354,7 @@
 	C(csum);
 	C(local_df);
 	n->cloned = 1;
+	n->nohdr = 0;
 	C(pkt_type);
 	C(ip_summed);
 	C(priority);
@@ -604,6 +606,7 @@
 	skb->h.raw   += off;
 	skb->nh.raw  += off;
 	skb->cloned   = 0;
+	skb->nohdr    = 0;
 	atomic_set(&skb_shinfo(skb)->dataref, 1);
 	return 0;
 

===== net/ipv4/tcp.c 1.91 vs edited =====

net/ipv4/tcp.c +1 -0
@@ -598,6 +598,7 @@
 	TCP_SKB_CB(skb)->end_seq = tp->write_seq;
 	TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
 	TCP_SKB_CB(skb)->sacked = 0;
+	skb_header_release(skb);
 	__skb_queue_tail(&sk->sk_write_queue, skb);
 	sk_charge_skb(sk, skb);
 	if (!sk->sk_send_head)

===== net/ipv4/tcp_output.c 1.77 vs edited =====

net/ipv4/tcp_output.c +3 -0
@@ -400,6 +400,7 @@
 
 	/* Advance write_seq and place onto the write_queue. */
 	tp->write_seq = TCP_SKB_CB(skb)->end_seq;
+	skb_header_release(skb);
 	__skb_queue_tail(&sk->sk_write_queue, skb);
 	sk_charge_skb(sk, skb);
 
@@ -1327,6 +1328,7 @@
 			if (nskb == NULL)
 				return -ENOMEM;
 			__skb_unlink(skb, &sk->sk_write_queue);
+			skb_header_release(nskb);
 			__skb_queue_head(&sk->sk_write_queue, nskb);
 			sk_stream_free_skb(sk, skb);
 			sk_charge_skb(sk, nskb);
@@ -1493,6 +1495,7 @@
 	/* Send it off. */
 	TCP_SKB_CB(buff)->when = tcp_time_stamp;
 	tp->retrans_stamp = TCP_SKB_CB(buff)->when;
+	skb_header_release(buff);
 	__skb_queue_tail(&sk->sk_write_queue, buff);
 	sk_charge_skb(sk, buff);
 	tp->packets_out += tcp_skb_pcount(buff);
author
#10 lore raw

PATCH 2/2 Add skb_header_cloned and use it in e1000/tg3

Hi:

This patch adds skb_header_cloned which tells us whether we need to copy the data before we can modify the header part of the skb. Again, what constitutes the header is left up to the users of the skb to define.

This patch then uses this function in e1000/tg3 to copy the data before the TCP/IP header is modified.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

===== drivers/net/tg3.c 1.231 vs edited =====

drivers/net/tg3.c +4 -0
@@ -3103,6 +3103,10 @@
 	    (mss = skb_shinfo(skb)->tso_size) != 0) {
 		int tcp_opt_len, ip_tcp_len;
 
+		if (skb_header_cloned(skb) &&
+		    pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
+			goto out_unlock;
+
 		tcp_opt_len = ((skb->h.th->doff - 5) * 4);
 		ip_tcp_len = (skb->nh.iph->ihl * 4) + sizeof(struct tcphdr);
 

===== drivers/net/e1000/e1000_main.c 1.147 vs edited =====

drivers/net/e1000/e1000_main.c +18 -4
@@ -1522,7 +1522,7 @@
 #define E1000_TX_FLAGS_VLAN_MASK	0xffff0000
 #define E1000_TX_FLAGS_VLAN_SHIFT	16
 
-static inline boolean_t
+static inline int
 e1000_tso(struct e1000_adapter *adapter, struct sk_buff *skb)
 {
 #ifdef NETIF_F_TSO
@@ -1531,8 +1531,15 @@
 	uint32_t cmd_length = 0;
 	uint16_t ipcse, tucse, mss;
 	uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
+	int err;
 
 	if(skb_shinfo(skb)->tso_size) {
+		if (skb_header_cloned(skb)) {
+			err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
+			if (err)
+				return err;
+		}
+
 		hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
 		mss = skb_shinfo(skb)->tso_size;
 		skb->nh.iph->tot_len = 0;
@@ -1569,11 +1576,11 @@
 		if(++i == adapter->tx_ring.count) i = 0;
 		adapter->tx_ring.next_to_use = i;
 
-		return TRUE;
+		return 1;
 	}
 #endif
 
-	return FALSE;
+	return 0;
 }
 
 static inline boolean_t
@@ -1798,6 +1805,7 @@
 	unsigned int nr_frags = 0;
 	unsigned int mss = 0;
 	int count = 0;
+	int tso;
 	unsigned int f;
 	len -= skb->data_len;
 
@@ -1869,7 +1877,13 @@
 
 	first = adapter->tx_ring.next_to_use;
 	
-	if(likely(e1000_tso(adapter, skb)))
+	tso = e1000_tso(adapter, skb);
+	if (tso < 0) {
+		dev_kfree_skb_any(skb);
+		return NETDEV_TX_OK;
+	}
+
+	if (likely(tso))
 		tx_flags |= E1000_TX_FLAGS_TSO;
 	else if(likely(e1000_tx_csum(adapter, skb)))
 		tx_flags |= E1000_TX_FLAGS_CSUM;

===== include/linux/skbuff.h 1.60 vs edited =====

include/linux/skbuff.h +19 -0
@@ -395,6 +395,25 @@
 }
 
 /**
+ *	skb_header_cloned - is the header a clone
+ *	@skb: buffer to check
+ *
+ *	Returns true if modifying the header part of the buffer requires
+ *	the data to be copied.
+ */
+static inline int skb_header_cloned(const struct sk_buff *skb)
+{
+	int dataref;
+
+	if (!skb->cloned)
+		return 0;
+
+	dataref = atomic_read(&skb_shinfo(skb)->dataref);
+	dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
+	return dataref != 1;
+}
+
+/**
  *	skb_header_release - release reference to header
  *	@skb: buffer to operate on
  *
maintainer
#11 lore raw

Re: [2/2] [NET] Add skb_header_cloned and use it in e1000/tg3

Quoted 8 lines · On Sat, 29 Jan 2005 14:22:10 +1100 Herbert Xu <[email protected]>…

This patch adds skb_header_cloned which tells us whether we need to copy the data before we can modify the header part of the skb. Again, what constitutes the header is left up to the users of the skb to define.

This patch then uses this function in e1000/tg3 to copy the data before the TCP/IP header is modified.

These two patches look great. I want to review them some more but I do intend to try and get them into 2.6.11-final.

author
#12 lore raw

Re: [2/2] [NET] Add skb_header_cloned and use it in e1000/tg3

Quoted 3 lines · David S. Miller

These two patches look great. I want to review them some more but I do intend to try and get them into 2.6.11-final.

Actually I would've thought that this is the sort of patch that should wait until 2.6.12-pre is open. The bug that it fixes isn't that serious. All I've heard so far are tcpdump showing bogus results.

The potential for breakage on the other side is much more significant.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#13 lore raw

Re: [2/2] [NET] Add skb_header_cloned and use it in e1000/tg3

In reply to Herbert Xu
Quoted 2 lines · On Sat, 29 Jan 2005 14:22:10 +1100 Herbert Xu <[email protected]>…

This patch then uses this function in e1000/tg3 to copy the data before the TCP/IP header is modified.

The tg3 version creates an SKB leak. If pskb_expand_head() fails, we just unlock and return NETDEV_TX_OK. The upper layer assumes the driver took ownership of the SKB in such a case. I would recommend just kfree_skb()'ing the thing when this happens.

author
#14 lore raw

Re: [2/2] [NET] Add skb_header_cloned and use it in e1000/tg3

Quoted 5 lines · David S. Miller

The tg3 version creates an SKB leak. If pskb_expand_head() fails, we just unlock and return NETDEV_TX_OK. The upper layer assumes the driver took ownership of the SKB in such a case. I would recommend just kfree_skb()'ing the thing when this happens.

Indeed. Here is the corrected version.

This patch adds skb_header_cloned which tells us whether we need to copy the data before we can modify the header part of the skb. Again, what constitutes the header is left up to the users of the skb to define.

This patch then uses this function in e1000/tg3 to copy the data before the TCP/IP header is modified.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

===== drivers/net/tg3.c 1.241 vs edited =====

drivers/net/tg3.c +6 -0
@@ -3096,6 +3096,12 @@
 	    (mss = skb_shinfo(skb)->tso_size) != 0) {
 		int tcp_opt_len, ip_tcp_len;
 
+		if (skb_header_cloned(skb) &&
+		    pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
+			dev_kfree_skb(skb);
+			goto out_unlock;
+		}
+
 		tcp_opt_len = ((skb->h.th->doff - 5) * 4);
 		ip_tcp_len = (skb->nh.iph->ihl * 4) + sizeof(struct tcphdr);
 

===== drivers/net/e1000/e1000_main.c 1.147 vs edited =====

drivers/net/e1000/e1000_main.c +18 -4
@@ -1522,7 +1522,7 @@
 #define E1000_TX_FLAGS_VLAN_MASK	0xffff0000
 #define E1000_TX_FLAGS_VLAN_SHIFT	16
 
-static inline boolean_t
+static inline int
 e1000_tso(struct e1000_adapter *adapter, struct sk_buff *skb)
 {
 #ifdef NETIF_F_TSO
@@ -1531,8 +1531,15 @@
 	uint32_t cmd_length = 0;
 	uint16_t ipcse, tucse, mss;
 	uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
+	int err;
 
 	if(skb_shinfo(skb)->tso_size) {
+		if (skb_header_cloned(skb)) {
+			err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
+			if (err)
+				return err;
+		}
+
 		hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
 		mss = skb_shinfo(skb)->tso_size;
 		skb->nh.iph->tot_len = 0;
@@ -1569,11 +1576,11 @@
 		if(++i == adapter->tx_ring.count) i = 0;
 		adapter->tx_ring.next_to_use = i;
 
-		return TRUE;
+		return 1;
 	}
 #endif
 
-	return FALSE;
+	return 0;
 }
 
 static inline boolean_t
@@ -1798,6 +1805,7 @@
 	unsigned int nr_frags = 0;
 	unsigned int mss = 0;
 	int count = 0;
+	int tso;
 	unsigned int f;
 	len -= skb->data_len;
 
@@ -1869,7 +1877,13 @@
 
 	first = adapter->tx_ring.next_to_use;
 	
-	if(likely(e1000_tso(adapter, skb)))
+	tso = e1000_tso(adapter, skb);
+	if (tso < 0) {
+		dev_kfree_skb_any(skb);
+		return NETDEV_TX_OK;
+	}
+
+	if (likely(tso))
 		tx_flags |= E1000_TX_FLAGS_TSO;
 	else if(likely(e1000_tx_csum(adapter, skb)))
 		tx_flags |= E1000_TX_FLAGS_CSUM;

===== include/linux/skbuff.h 1.60 vs edited =====

include/linux/skbuff.h +19 -0
@@ -395,6 +395,25 @@
 }
 
 /**
+ *	skb_header_cloned - is the header a clone
+ *	@skb: buffer to check
+ *
+ *	Returns true if modifying the header part of the buffer requires
+ *	the data to be copied.
+ */
+static inline int skb_header_cloned(const struct sk_buff *skb)
+{
+	int dataref;
+
+	if (!skb->cloned)
+		return 0;
+
+	dataref = atomic_read(&skb_shinfo(skb)->dataref);
+	dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
+	return dataref != 1;
+}
+
+/**
  *	skb_header_release - release reference to header
  *	@skb: buffer to operate on
  *
maintainer
#15 lore raw

Re: [2/2] [NET] Add skb_header_cloned and use it in e1000/tg3

Quoted 9 lines · On Wed, 16 Feb 2005 07:24:53 +1100 Herbert Xu <[email protected]>…
Quoted 5 lines · David S. Miller

The tg3 version creates an SKB leak. If pskb_expand_head() fails, we just unlock and return NETDEV_TX_OK. The upper layer assumes the driver took ownership of the SKB in such a case. I would recommend just kfree_skb()'ing the thing when this happens.

Indeed. Here is the corrected version.

Quoted 8 lines

This patch adds skb_header_cloned which tells us whether we need to copy the data before we can modify the header part of the skb. Again, what constitutes the header is left up to the users of the skb to define.

This patch then uses this function in e1000/tg3 to copy the data before the TCP/IP header is modified.

Also queued to 2.6.12, thanks. Someone should cook up the versions for other drivers supporting TSO.

author
#16 lore raw

[NET] Check skb_header_cloned for TSO packets in ixgb

Quoted 3 lines · David S. Miller

Also queued to 2.6.12, thanks. Someone should cook up the versions for other drivers supporting TSO.

I only found one other TSO driver that touches the headers, ixgb. Here is the patch which adds an skb_header_cloned check to it.

Do you know of any others?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

===== drivers/net/ixgb/ixgb_main.c 1.33 vs edited =====

drivers/net/ixgb/ixgb_main.c +18 -4
@@ -1155,7 +1155,7 @@
 #define IXGB_TX_FLAGS_VLAN		0x00000002
 #define IXGB_TX_FLAGS_TSO		0x00000004
 
-static inline boolean_t
+static inline int
 ixgb_tso(struct ixgb_adapter *adapter, struct sk_buff *skb)
 {
 #ifdef NETIF_F_TSO
@@ -1163,8 +1163,15 @@
 	unsigned int i;
 	uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
 	uint16_t ipcse, tucse, mss;
+	int err;
 
 	if(likely(skb_shinfo(skb)->tso_size)) {
+		if (skb_header_cloned(skb)) {
+			err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
+			if (err)
+				return err;
+		}
+
 		hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
 		mss = skb_shinfo(skb)->tso_size;
 		skb->nh.iph->tot_len = 0;
@@ -1203,11 +1210,11 @@
 		if(++i == adapter->tx_ring.count) i = 0;
 		adapter->tx_ring.next_to_use = i;
 
-		return TRUE;
+		return 1;
 	}
 #endif
 
-	return FALSE;
+	return 0;
 }
 
 static inline boolean_t
@@ -1378,6 +1385,7 @@
 	unsigned int tx_flags = 0;
 	unsigned long flags;
 	int vlan_id = 0;
+	int tso;
 
 	if(skb->len <= 0) {
 		dev_kfree_skb_any(skb);
@@ -1399,7 +1407,13 @@
 
 	first = adapter->tx_ring.next_to_use;
 	
-	if(ixgb_tso(adapter, skb))
+	tso = ixgb_tso(adapter, skb);
+	if (tso < 0) {
+		dev_kfree_skb_any(skb);
+		return NETDEV_TX_OK;
+	}
+
+	if (tso)
 		tx_flags |= IXGB_TX_FLAGS_TSO;
 	else if(ixgb_tx_csum(adapter, skb))
 		tx_flags |= IXGB_TX_FLAGS_CSUM;
maintainer Applied
#17 lore raw

Re: [NET] Check skb_header_cloned for TSO packets in ixgb

Quoted 9 lines · On Thu, 24 Feb 2005 21:29:51 +1100 Herbert Xu <[email protected]>…
Quoted 3 lines · David S. Miller

Also queued to 2.6.12, thanks. Someone should cook up the versions for other drivers supporting TSO.

I only found one other TSO driver that touches the headers, ixgb. Here is the patch which adds an skb_header_cloned check to it.

Applied, thanks.

Quoted 1 line

Do you know of any others?

I thought there was another that messed with the headers, but I can't spot it anymore :-)

maintainer Applied
#18 lore raw

Re: [1/2] [NET] Add skb_header_release and use it in net/ipv4/tcp

In reply to Herbert Xu
Quoted 9 lines · On Sat, 29 Jan 2005 14:17:40 +1100 Herbert Xu <[email protected]>…

This patch adds skb_header_release which can be called when the owner of an skb no longer needs to access the header at all. What constitutes the header is left up to the users of the skb to define.

For instance, for outbound TCP packets we define the header to be anything in front of the TCP payload. Therefore we add skb_header_release calls to all the paths where outound TCP packets are produced.

Queued to 2.6.12-pending