Skip to content

Commit

Permalink
[IP]: Introduce ip_hdrlen()
Browse files Browse the repository at this point in the history
For the common sequence "skb->nh.iph->ihl * 4", removing a good number of open
coded skb->nh.iph uses, now to go after the rest...

Just out of curiosity, here are the idioms found to get the same result:

skb->nh.iph->ihl << 2
skb->nh.iph->ihl<<2
skb->nh.iph->ihl * 4
skb->nh.iph->ihl*4
(skb->nh.iph)->ihl * sizeof(u32)

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Arnaldo Carvalho de Melo authored and David S. Miller committed Apr 26, 2007
1 parent 0272ffc commit c9bdd4b
Show file tree
Hide file tree
Showing 47 changed files with 145 additions and 141 deletions.
2 changes: 1 addition & 1 deletion drivers/net/bnx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -4527,7 +4527,7 @@ bnx2_start_xmit(struct sk_buff *skb, struct net_device *dev)
if (skb->h.th->doff > 5) {
tcp_opt_len = (skb->h.th->doff - 5) << 2;
}
ip_tcp_len = (skb->nh.iph->ihl << 2) + sizeof(struct tcphdr);
ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);

skb->nh.iph->check = 0;
skb->nh.iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/ehea/ehea_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ static inline void write_ip_start_end(struct ehea_swqe *swqe,
const struct sk_buff *skb)
{
swqe->ip_start = (u8)(((u64)skb->nh.iph) - ((u64)skb->data));
swqe->ip_end = (u8)(swqe->ip_start + skb->nh.iph->ihl * 4 - 1);
swqe->ip_end = (u8)(swqe->ip_start + ip_hdrlen(skb) - 1);
}

static inline void write_tcp_offset_end(struct ehea_swqe *swqe,
Expand Down Expand Up @@ -1300,7 +1300,7 @@ static void write_swqe2_TSO(struct sk_buff *skb,
/* copy only eth/ip/tcp headers to immediate data and
* the rest of skb->data to sg1entry
*/
headersize = ETH_HLEN + (skb->nh.iph->ihl * 4) + (skb->h.th->doff * 4);
headersize = ETH_HLEN + ip_hdrlen(skb) + (skb->h.th->doff * 4);

skb_data_size = skb->len - skb->data_len;

Expand Down
8 changes: 5 additions & 3 deletions drivers/net/netxen/netxen_nic_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "netxen_nic_hw.h"
#include "netxen_nic_phan_reg.h"

#include <net/ip.h>

/* PCI Windowing for DDR regions. */

#define ADDR_IN_RANGE(addr, low, high) \
Expand Down Expand Up @@ -371,9 +373,9 @@ void netxen_tso_check(struct netxen_adapter *adapter,
struct cmd_desc_type0 *desc, struct sk_buff *skb)
{
if (desc->mss) {
desc->total_hdr_length = sizeof(struct ethhdr) +
((skb->nh.iph)->ihl * sizeof(u32)) +
((skb->h.th)->doff * sizeof(u32));
desc->total_hdr_length = (sizeof(struct ethhdr) +
ip_hdrlen(skb) +
skb->h.th->doff * 4);
netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO);
} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
if (skb->nh.iph->protocol == IPPROTO_TCP) {
Expand Down
6 changes: 3 additions & 3 deletions drivers/net/netxen/netxen_nic_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include <linux/dma-mapping.h>
#include <linux/vmalloc.h>
#include <net/ip.h>

MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
MODULE_LICENSE("GPL");
Expand Down Expand Up @@ -778,9 +779,8 @@ static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
if (skb_shinfo(skb)->gso_size > 0) {

no_of_desc++;
if (((skb->nh.iph)->ihl * sizeof(u32)) +
((skb->h.th)->doff * sizeof(u32)) +
sizeof(struct ethhdr) >
if ((ip_hdrlen(skb) + skb->h.th->doff * 4 +
sizeof(struct ethhdr)) >
(sizeof(struct cmd_desc_type0) - 2)) {
no_of_desc++;
}
Expand Down
3 changes: 2 additions & 1 deletion drivers/net/sky2.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <linux/ethtool.h>
#include <linux/pci.h>
#include <linux/ip.h>
#include <net/ip.h>
#include <linux/tcp.h>
#include <linux/in.h>
#include <linux/delay.h>
Expand Down Expand Up @@ -1392,7 +1393,7 @@ static int sky2_xmit_frame(struct sk_buff *skb, struct net_device *dev)
mss = skb_shinfo(skb)->gso_size;
if (mss != 0) {
mss += ((skb->h.th->doff - 5) * 4); /* TCP options */
mss += (skb->nh.iph->ihl * 4) + sizeof(struct tcphdr);
mss += ip_hdrlen(skb) + sizeof(struct tcphdr);
mss += ETH_HLEN;

if (mss != sky2->tx_last_mss) {
Expand Down
6 changes: 3 additions & 3 deletions drivers/net/tg3.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <linux/dma-mapping.h>

#include <net/checksum.h>
#include <net/ip.h>

#include <asm/system.h>
#include <asm/io.h>
Expand Down Expand Up @@ -3909,8 +3910,7 @@ static int tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
mss |= (skb_headlen(skb) - ETH_HLEN) << 9;
else {
tcp_opt_len = ((skb->h.th->doff - 5) * 4);
ip_tcp_len = (skb->nh.iph->ihl * 4) +
sizeof(struct tcphdr);
ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);

skb->nh.iph->check = 0;
skb->nh.iph->tot_len = htons(mss + ip_tcp_len +
Expand Down Expand Up @@ -4064,7 +4064,7 @@ static int tg3_start_xmit_dma_bug(struct sk_buff *skb, struct net_device *dev)
}

tcp_opt_len = ((skb->h.th->doff - 5) * 4);
ip_tcp_len = (skb->nh.iph->ihl * 4) + sizeof(struct tcphdr);
ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);

hdr_len = ip_tcp_len + tcp_opt_len;
if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Expand Down
13 changes: 8 additions & 5 deletions drivers/s390/net/qeth_eddp.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,11 @@ qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
QETH_DBF_TEXT(trace, 5, "eddpficx");
/* create our segmentation headers and copy original headers */
if (skb->protocol == htons(ETH_P_IP))
eddp = qeth_eddp_create_eddp_data(qhdr, (u8 *)skb->nh.iph,
skb->nh.iph->ihl*4,
(u8 *)skb->h.th, skb->h.th->doff*4);
eddp = qeth_eddp_create_eddp_data(qhdr,
skb_network_header(skb),
ip_hdrlen(skb),
skb->h.raw,
skb->h.th->doff * 4);
else
eddp = qeth_eddp_create_eddp_data(qhdr, (u8 *)skb->nh.ipv6h,
sizeof(struct ipv6hdr),
Expand Down Expand Up @@ -590,8 +592,9 @@ qeth_eddp_create_context_tcp(struct qeth_card *card, struct sk_buff *skb,
QETH_DBF_TEXT(trace, 5, "creddpct");
if (skb->protocol == htons(ETH_P_IP))
ctx = qeth_eddp_create_context_generic(card, skb,
sizeof(struct qeth_hdr) + skb->nh.iph->ihl*4 +
skb->h.th->doff*4);
(sizeof(struct qeth_hdr) +
ip_hdrlen(skb) +
skb->h.th->doff * 4));
else if (skb->protocol == htons(ETH_P_IPV6))
ctx = qeth_eddp_create_context_generic(card, skb,
sizeof(struct qeth_hdr) + sizeof(struct ipv6hdr) +
Expand Down
7 changes: 6 additions & 1 deletion include/net/ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <linux/types.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/skbuff.h>

#include <net/inet_sock.h>
#include <net/snmp.h>
Expand All @@ -43,6 +44,11 @@ struct inet_skb_parm
#define IPSKB_REROUTED 16
};

static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
{
return skb->nh.iph->ihl * 4;
}

struct ipcm_cookie
{
__be32 addr;
Expand Down Expand Up @@ -74,7 +80,6 @@ struct msghdr;
struct net_device;
struct packet_type;
struct rtable;
struct sk_buff;
struct sockaddr;

extern void ip_mc_dropsocket(struct sock *);
Expand Down
4 changes: 2 additions & 2 deletions net/ipv4/ip_fragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
flags = offset & ~IP_OFFSET;
offset &= IP_OFFSET;
offset <<= 3; /* offset is in 8-byte chunks */
ihl = skb->nh.iph->ihl * 4;
ihl = ip_hdrlen(skb);

/* Determine the position of this fragment. */
end = offset + skb->len - ihl;
Expand Down Expand Up @@ -624,7 +624,7 @@ static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
BUG_TRAP(FRAG_CB(head)->offset == 0);

/* Allocate a new buffer for the datagram. */
ihlen = head->nh.iph->ihl*4;
ihlen = ip_hdrlen(head);
len = ihlen + qp->len;

if (len > 65535)
Expand Down
4 changes: 1 addition & 3 deletions net/ipv4/ip_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ int ip_call_ra_chain(struct sk_buff *skb)

static inline int ip_local_deliver_finish(struct sk_buff *skb)
{
int ihl = skb->nh.iph->ihl*4;

__skb_pull(skb, ihl);
__skb_pull(skb, ip_hdrlen(skb));

/* Point into the IP datagram, just past the header. */
skb->h.raw = skb->data;
Expand Down
2 changes: 1 addition & 1 deletion net/ipv4/ipmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c)
static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert)
{
struct sk_buff *skb;
int ihl = pkt->nh.iph->ihl<<2;
const int ihl = ip_hdrlen(pkt);
struct igmphdr *igmp;
struct igmpmsg *msg;
int ret;
Expand Down
4 changes: 2 additions & 2 deletions net/ipv4/ipvs/ip_vs_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ static inline int app_tcp_pkt_out(struct ip_vs_conn *cp, struct sk_buff **pskb,
struct ip_vs_app *app)
{
int diff;
unsigned int tcp_offset = (*pskb)->nh.iph->ihl*4;
const unsigned int tcp_offset = ip_hdrlen(*pskb);
struct tcphdr *th;
__u32 seq;

Expand Down Expand Up @@ -406,7 +406,7 @@ static inline int app_tcp_pkt_in(struct ip_vs_conn *cp, struct sk_buff **pskb,
struct ip_vs_app *app)
{
int diff;
unsigned int tcp_offset = (*pskb)->nh.iph->ihl*4;
const unsigned int tcp_offset = ip_hdrlen(*pskb);
struct tcphdr *th;
__u32 seq;

Expand Down
3 changes: 1 addition & 2 deletions net/ipv4/ipvs/ip_vs_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,7 @@ static inline int is_tcp_reset(const struct sk_buff *skb)
{
struct tcphdr _tcph, *th;

th = skb_header_pointer(skb, skb->nh.iph->ihl * 4,
sizeof(_tcph), &_tcph);
th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
if (th == NULL)
return 0;
return th->rst;
Expand Down
12 changes: 5 additions & 7 deletions net/ipv4/ipvs/ip_vs_proto_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ tcp_conn_schedule(struct sk_buff *skb,
struct ip_vs_service *svc;
struct tcphdr _tcph, *th;

th = skb_header_pointer(skb, skb->nh.iph->ihl*4,
sizeof(_tcph), &_tcph);
th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
if (th == NULL) {
*verdict = NF_DROP;
return 0;
Expand Down Expand Up @@ -127,7 +126,7 @@ tcp_snat_handler(struct sk_buff **pskb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
{
struct tcphdr *tcph;
unsigned int tcphoff = (*pskb)->nh.iph->ihl * 4;
const unsigned int tcphoff = ip_hdrlen(*pskb);

/* csum_check requires unshared skb */
if (!ip_vs_make_skb_writable(pskb, tcphoff+sizeof(*tcph)))
Expand Down Expand Up @@ -175,7 +174,7 @@ tcp_dnat_handler(struct sk_buff **pskb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
{
struct tcphdr *tcph;
unsigned int tcphoff = (*pskb)->nh.iph->ihl * 4;
const unsigned int tcphoff = ip_hdrlen(*pskb);

/* csum_check requires unshared skb */
if (!ip_vs_make_skb_writable(pskb, tcphoff+sizeof(*tcph)))
Expand Down Expand Up @@ -224,7 +223,7 @@ tcp_dnat_handler(struct sk_buff **pskb,
static int
tcp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
{
unsigned int tcphoff = skb->nh.iph->ihl*4;
const unsigned int tcphoff = ip_hdrlen(skb);

switch (skb->ip_summed) {
case CHECKSUM_NONE:
Expand Down Expand Up @@ -467,8 +466,7 @@ tcp_state_transition(struct ip_vs_conn *cp, int direction,
{
struct tcphdr _tcph, *th;

th = skb_header_pointer(skb, skb->nh.iph->ihl*4,
sizeof(_tcph), &_tcph);
th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
if (th == NULL)
return 0;

Expand Down
12 changes: 6 additions & 6 deletions net/ipv4/ipvs/ip_vs_proto_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <linux/udp.h>

#include <net/ip_vs.h>

#include <net/ip.h>

static struct ip_vs_conn *
udp_conn_in_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
Expand Down Expand Up @@ -56,7 +56,7 @@ udp_conn_out_get(const struct sk_buff *skb, struct ip_vs_protocol *pp,
struct ip_vs_conn *cp;
__be16 _ports[2], *pptr;

pptr = skb_header_pointer(skb, skb->nh.iph->ihl*4,
pptr = skb_header_pointer(skb, ip_hdrlen(skb),
sizeof(_ports), _ports);
if (pptr == NULL)
return NULL;
Expand All @@ -82,7 +82,7 @@ udp_conn_schedule(struct sk_buff *skb, struct ip_vs_protocol *pp,
struct ip_vs_service *svc;
struct udphdr _udph, *uh;

uh = skb_header_pointer(skb, skb->nh.iph->ihl*4,
uh = skb_header_pointer(skb, ip_hdrlen(skb),
sizeof(_udph), &_udph);
if (uh == NULL) {
*verdict = NF_DROP;
Expand Down Expand Up @@ -133,7 +133,7 @@ udp_snat_handler(struct sk_buff **pskb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
{
struct udphdr *udph;
unsigned int udphoff = (*pskb)->nh.iph->ihl * 4;
const unsigned int udphoff = ip_hdrlen(*pskb);

/* csum_check requires unshared skb */
if (!ip_vs_make_skb_writable(pskb, udphoff+sizeof(*udph)))
Expand Down Expand Up @@ -187,7 +187,7 @@ udp_dnat_handler(struct sk_buff **pskb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp)
{
struct udphdr *udph;
unsigned int udphoff = (*pskb)->nh.iph->ihl * 4;
unsigned int udphoff = ip_hdrlen(*pskb);

/* csum_check requires unshared skb */
if (!ip_vs_make_skb_writable(pskb, udphoff+sizeof(*udph)))
Expand Down Expand Up @@ -239,7 +239,7 @@ static int
udp_csum_check(struct sk_buff *skb, struct ip_vs_protocol *pp)
{
struct udphdr _udph, *uh;
unsigned int udphoff = skb->nh.iph->ihl*4;
const unsigned int udphoff = ip_hdrlen(skb);

uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph);
if (uh == NULL)
Expand Down
2 changes: 1 addition & 1 deletion net/ipv4/netfilter/ip_conntrack_amanda.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static int help(struct sk_buff **pskb,
ip_ct_refresh(ct, *pskb, master_timeout * HZ);

/* No data? */
dataoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct udphdr);
dataoff = ip_hdrlen(*pskb) + sizeof(struct udphdr);
if (dataoff >= (*pskb)->len) {
if (net_ratelimit())
printk("amanda_help: skblen = %u\n", (*pskb)->len);
Expand Down
3 changes: 1 addition & 2 deletions net/ipv4/netfilter/ip_conntrack_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,7 @@ resolve_normal_ct(struct sk_buff *skb,

IP_NF_ASSERT((skb->nh.iph->frag_off & htons(IP_OFFSET)) == 0);

if (!ip_ct_get_tuple(skb->nh.iph, skb, skb->nh.iph->ihl*4,
&tuple,proto))
if (!ip_ct_get_tuple(skb->nh.iph, skb, ip_hdrlen(skb), &tuple,proto))
return NULL;

/* look for tuple match */
Expand Down
4 changes: 2 additions & 2 deletions net/ipv4/netfilter/ip_conntrack_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ static int help(struct sk_buff **pskb,
return NF_ACCEPT;
}

th = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl*4,
th = skb_header_pointer(*pskb, ip_hdrlen(*pskb),
sizeof(_tcph), &_tcph);
if (th == NULL)
return NF_ACCEPT;

dataoff = (*pskb)->nh.iph->ihl*4 + th->doff*4;
dataoff = ip_hdrlen(*pskb) + th->doff * 4;
/* No data? */
if (dataoff >= (*pskb)->len) {
DEBUGP("ftp: pskblen = %u\n", (*pskb)->len);
Expand Down
9 changes: 4 additions & 5 deletions net/ipv4/netfilter/ip_conntrack_helper_h323.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ static int get_tpkt_data(struct sk_buff **pskb, struct ip_conntrack *ct,
int tpktoff;

/* Get TCP header */
th = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl * 4,
th = skb_header_pointer(*pskb, ip_hdrlen(*pskb),
sizeof(_tcph), &_tcph);
if (th == NULL)
return 0;

/* Get TCP data offset */
tcpdataoff = (*pskb)->nh.iph->ihl * 4 + th->doff * 4;
tcpdataoff = ip_hdrlen(*pskb) + th->doff * 4;

/* Get TCP data length */
tcpdatalen = (*pskb)->len - tcpdataoff;
Expand Down Expand Up @@ -1185,11 +1185,10 @@ static unsigned char *get_udp_data(struct sk_buff **pskb, int *datalen)
struct udphdr _uh, *uh;
int dataoff;

uh = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl * 4, sizeof(_uh),
&_uh);
uh = skb_header_pointer(*pskb, ip_hdrlen(*pskb), sizeof(_uh), &_uh);
if (uh == NULL)
return NULL;
dataoff = (*pskb)->nh.iph->ihl * 4 + sizeof(_uh);
dataoff = ip_hdrlen(*pskb) + sizeof(_uh);
if (dataoff >= (*pskb)->len)
return NULL;
*datalen = (*pskb)->len - dataoff;
Expand Down
Loading

0 comments on commit c9bdd4b

Please sign in to comment.