pktgen: fix crash when generating IPv6 packets
Subsystems (2)
PATCH 1/5 pktgen: fix crash when generating IPv6 packets
For IPv6, sizeof(struct ipv6hdr) = 40, thus the following expression will result negative:
datalen = pkt_dev->cur_pkt_size - 14 -
sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
pkt_dev->pkt_overhead;And, the check "if (datalen < sizeof(struct pktgen_hdr))" will be passed as "datalen" is promoted to unsigned, therefore will cause a crash later.
This is a quick fix by checking if "datalen" is negative. The following patch will increase the default value of 'min_pkt_size' for IPv6.
This bug should exist for a long time, so Cc -stable too.
--- net/core/pktgen.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
net/core/pktgen.c +1 -1
@@ -2927,7 +2927,7 @@ static struct sk_buff *fill_packet_ipv6(struct net_device *odev,sizeof (struct ipv6hdr ) -sizeof (struct udphdr ) - pkt_dev->pkt_overhead; -if (datalen <sizeof (struct pktgen_hdr )) { +if (datalen <0 || datalen <sizeof (struct pktgen_hdr )) { datalen =sizeof (struct pktgen_hdr );net_info_ratelimited ("increased datalen to %d\n" , datalen); }
Signature · 1 line
1.7.7.6
PATCH 2/5 pktgen: set different default min_pkt_size for different protocols
ETH_ZLEN is too small for IPv6, so this default value is not suitable.
--- net/core/pktgen.c | 26 +++++++++++++++++++------- 1 files changed, 19 insertions(+), 7 deletions(-)
net/core/pktgen.c +19 -7
@@ -248,8 +248,8 @@ struct pktgen_dev {int removal_mark;/* non-zero => the device is marked for * removal by worker thread */ -int min_pkt_size;/* = ETH_ZLEN; */ -int max_pkt_size;/* = ETH_ZLEN; */ +int min_pkt_size; +int max_pkt_size;int pkt_overhead;/* overhead for MPLS, VLANs, IPSEC etc */ int nfrags;struct page *page;
@@ -2036,10 +2036,16 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)/* Set up Dest MAC */ memcpy (&(pkt_dev->hh[0 ]), pkt_dev->dst_mac,ETH_ALEN ); -/* Set up pkt size */ - pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; -if (pkt_dev->flags &F_IPV6 ) { +if (pkt_dev->min_pkt_size ==0 ) { + pkt_dev->min_pkt_size =14 +sizeof (struct ipv6hdr ) + +sizeof (struct udphdr ) + +sizeof (struct pktgen_hdr ) + + pkt_dev->pkt_overhead; + } + + pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; +/* * Skip this automatic address setting until locks or functions * gets exported
@@ -2086,6 +2092,14 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) }#endif }else { +if (pkt_dev->min_pkt_size ==0 ) { + pkt_dev->min_pkt_size =14 +sizeof (struct iphdr ) + +sizeof (struct udphdr ) + +sizeof (struct pktgen_hdr ) + + pkt_dev->pkt_overhead; + } + pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; + pkt_dev->saddr_min =0 ; pkt_dev->saddr_max =0 ;if (strlen (pkt_dev->src_min) ==0 ) {
@@ -3548,8 +3562,6 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) } pkt_dev->removal_mark =0 ; - pkt_dev->min_pkt_size =ETH_ZLEN ; - pkt_dev->max_pkt_size =ETH_ZLEN ; pkt_dev->nfrags =0 ; pkt_dev->delay = pg_delay_d; pkt_dev->count = pg_count_d;
Signature · 1 line
1.7.7.6
David Miller
maintainerRe: [PATCH 2/5] pktgen: set different default min_pkt_size for different protocols
From: Cong Wang <[email protected]>
Date: Tue, 9 Oct 2012 18:05:28 +0800
Quoted 5 lines
ETH_ZLENis too small for IPv6, so this default value is not suitable.Cc David S. Miller <[email protected]>Signed-off-by Cong Wang <[email protected]>
pkt_dev->max_pkt_size is no longer being initialized.
At least set it to whatever you compute in pkt_dev->min_pkt_size
Re: [PATCH 2/5] pktgen: set different default min_pkt_size for different protocols
Quoted 6 lines · David Miller
From: Cong Wang <[email protected]>
Date: Tue, 9 Oct 2012 18:05:28 +0800Quoted 3 lines
ETH_ZLENis too small for IPv6, so this default value is not suitable.
Quoted 6 lines
Quoted 2 lines
Cc David S. Miller <[email protected]>Signed-off-by Cong Wang <[email protected]>
pkt_dev->max_pkt_sizeis no longer being initialized.At least set it to whatever you compute in
pkt_dev->min_pkt_size
Oops, I forgot it. Will send an updated version.
Thanks!
PATCH 3/5 pktgen: display IPv4 address in human-readable format
In reply to Cong WangIt is weird to display IPv4 address in %x format, what's more, IPv6 address is disaplayed in human-readable format too. So, make it human-readable.
--- net/core/pktgen.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
net/core/pktgen.c +2 -2
@@ -702,8 +702,8 @@ static int pktgen_if_show(struct seq_file *seq, void *v) &pkt_dev->cur_in6_saddr, &pkt_dev->cur_in6_daddr); }else -seq_printf (seq," cur_saddr: 0x%x cur_daddr: 0x%x\n" , - pkt_dev->cur_saddr, pkt_dev->cur_daddr); +seq_printf (seq," cur_saddr: %pI4 cur_daddr: %pI4\n" , + &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);seq_printf (seq," cur_udp_dst: %d cur_udp_src: %d\n" , pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Signature · 1 line
1.7.7.6
PATCH 4/5 pktgen: enable automatic IPv6 address setting
In reply to Cong Wang--- net/core/pktgen.c | 18 +++++------------- 1 files changed, 5 insertions(+), 13 deletions(-)
net/core/pktgen.c +5 -13
@@ -2037,6 +2037,9 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)memcpy (&(pkt_dev->hh[0 ]), pkt_dev->dst_mac,ETH_ALEN );if (pkt_dev->flags &F_IPV6 ) { +int i, set =0 , err =1 ; +struct inet6_dev *idev; +if (pkt_dev->min_pkt_size ==0 ) { pkt_dev->min_pkt_size =14 +sizeof (struct ipv6hdr ) +sizeof (struct udphdr )
@@ -2046,15 +2049,6 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; -/* -* Skip this automatic address setting until locks or functions -* gets exported -*/ - -#ifdef NOTNOW -int i, set =0 , err =1 ; -struct inet6_dev *idev; -for (i =0 ; i <IN6_ADDR_HSIZE ; i++)if (pkt_dev->cur_in6_saddr.s6_addr[i]) { set =1 ;
@@ -2075,9 +2069,8 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)struct inet6_ifaddr *ifp;read_lock_bh (&idev->lock); -for (ifp = idev->addr_list; ifp; - ifp = ifp->if_next) { -if (ifp->scope ==IFA_LINK && +list_for_each_entry (ifp, &idev->addr_list, if_list) { +if ((ifp->scope &IFA_LINK ) && !(ifp->flags &IFA_F_TENTATIVE )) { pkt_dev->cur_in6_saddr = ifp->addr; err =0 ;
@@ -2090,7 +2083,6 @@ static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)if (err)pr_err ("ERROR: IPv6 link address not available\n" ); } -#endif }else {if (pkt_dev->min_pkt_size ==0 ) { pkt_dev->min_pkt_size =14 +sizeof (struct iphdr )
Signature · 1 line
1.7.7.6
PATCH 5/5 pktgen: replace scan_ip6() with in6_pton()
In reply to Cong Wang--- net/core/pktgen.c | 101 ++-------------------------------------------------- 1 files changed, 4 insertions(+), 97 deletions(-)
net/core/pktgen.c +4 -97
@@ -449,8 +449,6 @@ static void pktgen_stop_all_threads_ifs(void);static void pktgen_stop (struct pktgen_thread *t);static void pktgen_clear_counters (struct pktgen_dev *pkt_dev); -static unsigned int scan_ip6 (const char *s,char ip[16 ]); -/* Module parameters, defaults. */ static int pg_count_d__read_mostly =1000 ;static int pg_delay_d__read_mostly ;
@@ -1299,7 +1297,7 @@ static ssize_t pktgen_if_write(struct file *file,return -EFAULT ; buf[len] =0 ; -scan_ip6 (buf, pkt_dev->in6_daddr.s6_addr); +in6_pton (buf,-1 , pkt_dev->in6_daddr.s6_addr,-1 ,NULL );snprintf (buf,sizeof (buf),"%pI6c" , &pkt_dev->in6_daddr); pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
@@ -1322,7 +1320,7 @@ static ssize_t pktgen_if_write(struct file *file,return -EFAULT ; buf[len] =0 ; -scan_ip6 (buf, pkt_dev->min_in6_daddr.s6_addr); +in6_pton (buf,-1 , pkt_dev->min_in6_daddr.s6_addr,-1 ,NULL );snprintf (buf,sizeof (buf),"%pI6c" , &pkt_dev->min_in6_daddr); pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
@@ -1344,7 +1342,7 @@ static ssize_t pktgen_if_write(struct file *file,return -EFAULT ; buf[len] =0 ; -scan_ip6 (buf, pkt_dev->max_in6_daddr.s6_addr); +in6_pton (buf,-1 , pkt_dev->max_in6_daddr.s6_addr,-1 ,NULL );snprintf (buf,sizeof (buf),"%pI6c" , &pkt_dev->max_in6_daddr);if (debug)
@@ -1365,7 +1363,7 @@ static ssize_t pktgen_if_write(struct file *file,return -EFAULT ; buf[len] =0 ; -scan_ip6 (buf, pkt_dev->in6_saddr.s6_addr); +in6_pton (buf,-1 , pkt_dev->in6_saddr.s6_addr,-1 ,NULL );snprintf (buf,sizeof (buf),"%pI6c" , &pkt_dev->in6_saddr); pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
@@ -2764,97 +2762,6 @@ static struct sk_buff *fill_packet_ipv4(struct net_device *odev,return skb; } -/* -* scan_ip6, fmt_ip taken from dietlibc-0.21 -* Author Felix von Leitner <[email protected]> -* -* Slightly modified for kernel. -* Should be candidate for net/ipv4/utils.c -* --ro -*/ - -static unsigned int scan_ip6 (const char *s,char ip[16 ]) -{ -unsigned int i; -unsigned int len =0 ; -unsigned long u; -char suffix[16 ]; -unsigned int prefixlen =0 ; -unsigned int suffixlen =0 ; -__be32 tmp; -char *pos; - -for (i =0 ; i <16 ; i++) - ip[i] =0 ; - -for (;;) { -if (*s ==':' ) { - len++; -if (s[1 ] ==':' ) {/* Found "::", skip to part 2 */ - s +=2 ; - len++; -break ; - } - s++; - } - - u =simple_strtoul (s, &pos,16 ); - i = pos - s; -if (!i) -return 0 ; -if (prefixlen ==12 && s[i] =='.' ) { - -/* the last 4 bytes may be written as IPv4 address */ - - tmp =in_aton (s); -memcpy ((struct in_addr *)(ip +12 ), &tmp,sizeof (tmp)); -return i + len; - } - ip[prefixlen++] = (u >>8 ); - ip[prefixlen++] = (u &255 ); - s += i; - len += i; -if (prefixlen ==16 ) -return len; - } - -/* part 2, after "::" */ -for (;;) { -if (*s ==':' ) { -if (suffixlen ==0 ) -break ; - s++; - len++; - }else if (suffixlen !=0 ) -break ; - - u =simple_strtol (s, &pos,16 ); - i = pos - s; -if (!i) { -if (*s) - len--; -break ; - } -if (suffixlen + prefixlen <=12 && s[i] =='.' ) { - tmp =in_aton (s); -memcpy ((struct in_addr *)(suffix + suffixlen), &tmp, -sizeof (tmp)); - suffixlen +=4 ; - len +=strlen (s); -break ; - } - suffix[suffixlen++] = (u >>8 ); - suffix[suffixlen++] = (u &255 ); - s += i; - len += i; -if (prefixlen + suffixlen ==16 ) -break ; - } -for (i =0 ; i < suffixlen; i++) - ip[16 - suffixlen + i] = suffix[i]; -return len; -} -static struct sk_buff *fill_packet_ipv6 (struct net_device *odev,struct pktgen_dev *pkt_dev) {
Signature · 1 line
1.7.7.6