Re: [PATCH] tcp: harmonize tcp_vx_rcv header length assumptions

From: William Allen Simpson
Date: Tue Jan 12 2010 - 12:14:27 EST

Eric Dumazet wrote:
2) This part :

- th = tcp_hdr(skb);
- if (th->doff < sizeof(struct tcphdr)/4)
+ /* Check bad doff, compare doff directly to constant value */
+ tcp_header_len = tcp_hdr(skb)->doff;
+ if (tcp_header_len < (sizeof(struct tcphdr) / 4))
goto bad_packet;
- if (!pskb_may_pull(skb, th->doff*4))
+ /* Check too short header and options */
+ tcp_header_len *= 4;
+ if (!pskb_may_pull(skb, tcp_header_len))
goto discard_it;

could be : (no need for 4 multiplies/divides)

tcp_header_len = tcp_header_len_th(tcp_hdr(skb));
if (tcp_header_len < sizeof(struct tcphdr))
goto bad_packet;
/* Check too short header and options */
if (!pskb_may_pull(skb, tcp_header_len))
goto discard_it;

Actually, tcp_header_len_th() has a multiply by 4 in it, too.

My code exactly parallels the existing code. That is slightly faster
for bad packets, as it does the raw comparison first, saving the
multiply by 4 until it has passed that test.

My change just saves a store (and maybe a register load) over the
existing code. This is supposed to be "fast path" after all....

Also adds comments that explain what we're doing. As I mentioned
earlier in the thread:

... back on Nov 10th, Ilpo brought to my attention --
*hidden* inside the pskb_may_pull() -- the tcp header length is range
checked for being too short (skb->len < th->doff * 4).

Anytime I find the code isn't obvious to me, I figure the next person
will benefit from some more comments....

(Of course, this patch also fixes existing comments that are not true!)
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at