Add routines to process IPv{4,6}-address-and-prefix-length pairs,

masking out the appropriate bits in the address.

Use them in the BGP and EIGRP dissectors.

svn path=/trunk/; revision=10327
This commit is contained in:
Guy Harris 2004-03-06 03:25:10 +00:00
parent 133a3b35cf
commit 27656155b7
6 changed files with 162 additions and 89 deletions

View File

@ -2,7 +2,7 @@
# Automake file for the EPAN library
# (Ethereal Protocol ANalyzer Library)
#
# $Id: Makefile.am,v 1.37 2004/03/04 08:26:20 jmayer Exp $
# $Id: Makefile.am,v 1.38 2004/03/06 03:25:10 guy Exp $
#
# Ethereal - Network traffic analyzer
# By Gerald Combs <gerald@ethereal.com>
@ -36,6 +36,8 @@ include Makefile.common
INCLUDES = -I$(srcdir)/..
libethereal_a_SOURCES = \
addr_and_mask.c \
addr_and_mask.h \
atalk-utils.c \
atalk-utils.h \
bitswap.c \

View File

@ -1,7 +1,7 @@
## Makefile for building ethereal.exe with Microsoft C and nmake
## Use: $(MAKE) /$(MAKEFLAGS) -f makefile.nmake
#
# $Id: Makefile.nmake,v 1.35 2004/03/02 18:55:30 ulfl Exp $
# $Id: Makefile.nmake,v 1.36 2004/03/06 03:25:10 guy Exp $
include ..\config.nmake
@ -35,7 +35,8 @@ DISSECTOR_SUPPORT_OBJECTS = $(DISSECTOR_SUPPORT_OBJECTS:../=)
$(CC) $(CVARSDLL) $(CFLAGS) -Fd.\ -c $(DISSECTOR_SUPPORT_SRC)
$(CC) $(CVARSDLL) $(CFLAGS) -Fd.\ -c $<
OBJECTS=atalk-utils.obj \
OBJECTS=addr_and_mask.obj \
atalk-utils.obj \
bitswap.obj \
circuit.obj \
column-utils.obj \

76
epan/addr_and_mask.c Normal file
View File

@ -0,0 +1,76 @@
/* addr_and_mask.c
* Routines to fetch IPv4 and IPv6 addresses from a tvbuff and then mask
* out bits other than those covered by a prefix length
*
* $Id: addr_and_mask.c,v 1.1 2004/03/06 03:25:10 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "tvbuff.h"
#include "ipv6-utils.h"
#include "addr_and_mask.h"
/*
* These routines return the length of the address in bytes on success
* and -1 if the prefix length is too long.
*/
int
ipv4_addr_and_mask(tvbuff_t *tvb, int offset, guint8 *addr, guint32 prefix_len)
{
guint32 addr_len;
if (prefix_len > 32)
return -1;
addr_len = (prefix_len + 7) / 8;
memset(addr, 0, 4);
tvb_memcpy(tvb, addr, offset, addr_len);
if (prefix_len % 8)
addr[addr_len - 1] &= ((0xff00 >> (prefix_len % 8)) & 0xff);
return addr_len;
}
int
ipv6_addr_and_mask(tvbuff_t *tvb, int offset, struct e_in6_addr *addr,
guint32 prefix_len)
{
guint32 addr_len;
if (prefix_len > 128)
return -1;
addr_len = (prefix_len + 7) / 8;
memset(addr->u6_addr.u6_addr8, 0, 16);
tvb_memcpy(tvb, addr->u6_addr.u6_addr8, offset, addr_len);
if (prefix_len % 8) {
addr->u6_addr.u6_addr8[addr_len - 1] &=
((0xff00 >> (prefix_len % 8)) & 0xff);
}
return addr_len;
}

45
epan/addr_and_mask.h Normal file
View File

@ -0,0 +1,45 @@
/* addr_and_mask.h
* Declarations of routines to fetch IPv4 and IPv6 addresses from a tvbuff
* and then mask out bits other than those covered by a prefix length
*
* $Id: addr_and_mask.h,v 1.1 2004/03/06 03:25:10 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __ADDR_AND_MASK_H__
#define __ADDR_AND_MASK_H__
/*
* These routines return PREFIX_LEN_OK on success, PREFIX_LEN_TOO_LONG if
* the prefix length is too long, and PREFIX_LEN_ZERO if the prefix length
* is 0.
*/
#define PREFIX_LEN_OK 0
#define PREFIX_LEN_TOO_LONG 1
#define PREFIX_LEN_ZERO 2
extern int ipv4_addr_and_mask(tvbuff_t *tvb, int offset, guint8 *addr,
guint32 prefix_len);
extern int ipv6_addr_and_mask(tvbuff_t *tvb, int offset,
struct e_in6_addr *addr, guint32 prefix_len);
#endif /* __ADDR_AND_MASK_H__ */

View File

@ -2,7 +2,7 @@
* Routines for BGP packet dissection.
* Copyright 1999, Jun-ichiro itojun Hagino <itojun@itojun.org>
*
* $Id: packet-bgp.c,v 1.86 2004/03/06 02:26:31 guy Exp $
* $Id: packet-bgp.c,v 1.87 2004/03/06 03:25:09 guy Exp $
*
* Supports:
* RFC1771 A Border Gateway Protocol 4 (BGP-4)
@ -56,6 +56,7 @@
#endif
#include <epan/packet.h>
#include <epan/addr_and_mask.h>
#include "packet-bgp.h"
#include "packet-ipv6.h"
#include "packet-frame.h"
@ -368,20 +369,14 @@ decode_prefix4(proto_tree *tree, int hf_addr, tvbuff_t *tvb, gint offset,
guint8 plen; /* prefix length */
int length; /* number of octets needed for prefix */
/* snarf length */
/* snarf length and prefix */
plen = tvb_get_guint8(tvb, offset);
if (plen > 32 || plen == 0) {
proto_tree_add_text(tree, tvb, offset, 1, "%s length %u invalid",
length = ipv4_addr_and_mask(tvb, offset + 1, ip_addr.addr_bytes, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, offset, 1, "%s length %u invalid (> 32)",
tag, plen);
return -1;
}
length = (plen + 7) / 8;
/* snarf prefix */
memset((void *)&ip_addr, 0, sizeof(ip_addr));
tvb_memcpy(tvb, ip_addr.addr_bytes, offset + 1, length);
if (plen % 8)
ip_addr.addr_bytes[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
/* put prefix into protocol tree */
ti = proto_tree_add_text(tree, tvb, offset,
@ -413,20 +408,14 @@ decode_prefix6(proto_tree *tree, int hf_addr, tvbuff_t *tvb, gint offset,
int plen; /* prefix length */
int length; /* number of octets needed for prefix */
/* snarf length */
/* snarf length and prefix */
plen = tvb_get_guint8(tvb, offset);
if (plen > 128 || plen == 0) {
length = ipv6_addr_and_mask(tvb, offset + 1, &addr, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, offset, 1, "%s length %u invalid",
tag, plen);
return -1;
}
length = (plen + 7) / 8;
/* snarf prefix */
memset(&addr, 0, sizeof(addr));
tvb_memcpy(tvb, (guint8 *)&addr, offset + 1, length);
if (plen % 8)
addr.s6_addr[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
/* put prefix into protocol tree */
ti = proto_tree_add_text(tree, tvb, offset,
@ -663,19 +652,14 @@ decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
return -1;
}
plen -= (labnum * 3*8);
if (plen > 32 || plen == 0) {
length = ipv4_addr_and_mask(tvb, offset, ip4addr.addr_bytes, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, start_offset, 1,
"%s IPv4 prefix length %u invalid",
tag, plen + (labnum * 3*8));
return -1;
}
length = (plen + 7) / 8;
memset((void *)&ip4addr, 0, sizeof(ip4addr));
tvb_memcpy(tvb, (void *)ip4addr.addr_bytes, offset, length);
if (plen % 8)
ip4addr.addr_bytes[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
ti = proto_tree_add_text(tree, tvb, start_offset,
(offset + 1 + length) - start_offset,
"Label Stack=%s IP=%s/%u",
@ -718,19 +702,14 @@ decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
switch (rd_type) {
case FORMAT_AS2_LOC: /* Code borrowed from the decode_prefix4 function */
if (plen > 32 || plen == 0) {
length = ipv4_addr_and_mask(tvb, offset + 8, ip4addr.addr_bytes, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, start_offset, 1,
"%s IPv4 prefix length %u invalid",
tag, plen + (labnum * 3*8) + 8*8);
return -1;
}
length = (plen + 7) / 8;
memset(ip4addr.addr_bytes, 0, 4);
tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 8, length);
if (plen % 8)
ip4addr.addr_bytes[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
ti = proto_tree_add_text(tree, tvb, start_offset,
(offset + 8 + length) - start_offset,
"Label Stack=%s RD=%u:%u, IP=%s/%u",
@ -753,19 +732,14 @@ decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
case FORMAT_IP_LOC: /* Code borrowed from the decode_prefix4 function */
tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 2, 4);
if (plen > 32 || plen == 0) {
length = ipv4_addr_and_mask(tvb, offset + 8, ip4addr2.addr_bytes, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, start_offset, 1,
"%s IPv4 prefix length %u invalid",
tag, plen + (labnum * 3*8) + 8*8);
return -1;
}
length = (plen + 7) / 8;
memset((void *)&ip4addr2, 0, sizeof(ip4addr2));
tvb_memcpy(tvb, ip4addr2.addr_bytes, offset + 8, length);
if (plen % 8)
ip4addr2.addr_bytes[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
ti = proto_tree_add_text(tree, tvb, start_offset,
(offset + 8 + length) - start_offset,
"Label Stack=%s RD=%s:%u, IP=%s/%u",
@ -815,18 +789,13 @@ decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
}
plen -= (labnum * 3*8);
if (plen > 128 || plen == 0) {
length = ipv6_addr_and_mask(tvb, offset, &ip6addr, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, start_offset, 1,
"%s IPv6 prefix length %u invalid", tag, plen);
return -1;
}
length = (plen + 7) / 8;
memset(ip6addr.u6_addr.u6_addr8, 0, 16);
tvb_memcpy(tvb, ip6addr.u6_addr.u6_addr8, offset, length);
if (plen % 8)
ip6addr.u6_addr.u6_addr8[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
ti = proto_tree_add_text(tree, tvb, start_offset,
(offset + length) - start_offset,
"Label Stack=%s, IP=%s/%u",
@ -861,19 +830,14 @@ decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
switch (rd_type) {
case FORMAT_AS2_LOC:
if (plen > 128 || plen == 0) {
length = ipv6_addr_and_mask(tvb, offset + 8, &ip6addr, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, start_offset, 1,
"%s IPv6 prefix length %u invalid",
tag, plen + (labnum * 3*8) + 8*8);
return -1;
}
length = (plen + 7) / 8;
memset(ip6addr.u6_addr.u6_addr8, 0, 16);
tvb_memcpy(tvb, ip6addr.u6_addr.u6_addr8, offset + 8, length);
if (plen % 8)
ip6addr.u6_addr.u6_addr8[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
ti = proto_tree_add_text(tree, tvb, start_offset,
(offset + 8 + length) - start_offset,
"Label Stack=%s RD=%u:%u, IP=%s/%u",
@ -887,17 +851,13 @@ decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
case FORMAT_IP_LOC:
tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 2, 4);
if (plen > 128 || plen == 0) {
length = ipv6_addr_and_mask(tvb, offset + 8, &ip6addr, plen);
if (length < 0) {
proto_tree_add_text(tree, tvb, start_offset, 1,
"%s IPv6 prefix length %u invalid",
tag, plen + (labnum * 3*8) + 8*8);
return -1;
}
length = (plen + 7) / 8;
memset(ip6addr.u6_addr.u6_addr8, 0, 16);
tvb_memcpy(tvb, ip6addr.u6_addr.u6_addr8, offset + 8, length);
if (plen % 8)
ip6addr.u6_addr.u6_addr8[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
ti = proto_tree_add_text(tree, tvb, start_offset,
(offset + 8 + length) - start_offset,

View File

@ -2,7 +2,7 @@
* Routines for EIGRP dissection
* Copyright 2000, Paul Ionescu <paul@acorp.ro>
*
* $Id: packet-eigrp.c,v 1.26 2004/03/06 02:20:55 guy Exp $
* $Id: packet-eigrp.c,v 1.27 2004/03/06 03:25:09 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@ -32,6 +32,7 @@
#include <epan/resolv.h>
#include <epan/atalk-utils.h>
#include <epan/addr_and_mask.h>
#include "ipproto.h"
#include "packet-ipx.h"
@ -307,7 +308,8 @@ static void dissect_eigrp_nms (tvbuff_t *tvb, proto_tree *tree, proto_item *ti)
static void dissect_eigrp_ip_int (tvbuff_t *tvb, proto_tree *tree, proto_item *ti)
{
guint8 ip_addr[4],length,addr_len;
guint8 ip_addr[4],length;
int addr_len;
tvb_memcpy(tvb,ip_addr,0,4);
proto_tree_add_text (tree,tvb,0,4, "Next Hop = %s",ip_to_str(ip_addr));
@ -319,21 +321,14 @@ static void dissect_eigrp_ip_int (tvbuff_t *tvb, proto_tree *tree, proto_item *t
proto_tree_add_text (tree,tvb,17,1,"Load = %u",tvb_get_guint8(tvb,17));
proto_tree_add_text (tree,tvb,18,2,"Reserved ");
length=tvb_get_guint8(tvb,20);
if (length > 32) {
/* XXX - the EIGRP page whose URL appears at the top says this
field is 24 bits; what if the prefix length is > 24? */
addr_len=ipv4_addr_and_mask (tvb,21,ip_addr,length);
if (addr_len < 0) {
proto_tree_add_text (tree,tvb,20,1,"Prefix length = %u (invalid, must be <= 32)",length);
proto_item_append_text (ti," [Invalid prefix length %u > 32]",length);
} else if (length == 0) {
proto_tree_add_text (tree,tvb,20,1,"Prefix length = %u (invalid, must be > 0)",length);
proto_item_append_text (ti," [Invalid prefix length %u]",length);
} else {
proto_tree_add_text (tree,tvb,20,1,"Prefix Length = %u",length);
addr_len=(length+7)/8;
ip_addr[0]=ip_addr[1]=ip_addr[2]=ip_addr[3]=0;
/* XXX - the EIGRP page whose URL appears at the top says this
field is 24 bits; what if the prefix length is > 24? */
tvb_memcpy(tvb,ip_addr,21,addr_len);
if (length%8)
ip_addr[addr_len - 1] &= ((0xff00 >> (length % 8)) & 0xff);
proto_tree_add_text (tree,tvb,21,addr_len,"Destination = %s",ip_to_str(ip_addr));
proto_item_append_text (ti," = %s/%u%s",ip_to_str(ip_addr),length,((tvb_get_ntohl(tvb,4)==0xffffffff)?" - Destination unreachable":""));
}
@ -341,7 +336,8 @@ static void dissect_eigrp_ip_int (tvbuff_t *tvb, proto_tree *tree, proto_item *t
static void dissect_eigrp_ip_ext (tvbuff_t *tvb, proto_tree *tree, proto_item *ti)
{
guint8 ip_addr[4],length,addr_len;
guint8 ip_addr[4],length;
int addr_len;
tvb_memcpy(tvb,ip_addr,0,4);
proto_tree_add_text (tree,tvb,0,4,"Next Hop = %s",ip_to_str(ip_addr));
@ -362,21 +358,14 @@ static void dissect_eigrp_ip_ext (tvbuff_t *tvb, proto_tree *tree, proto_item *t
proto_tree_add_text (tree,tvb,37,1,"Load = %u",tvb_get_guint8(tvb,37));
proto_tree_add_text (tree,tvb,38,2,"Reserved ");
length=tvb_get_guint8(tvb,40);
if (length > 32) {
/* XXX - the EIGRP page whose URL appears at the top says this
field is 24 bits; what if the prefix length is > 24? */
addr_len=ipv4_addr_and_mask (tvb,41,ip_addr,length);
if (addr_len < 0) {
proto_tree_add_text (tree,tvb,40,1,"Prefix length = %u (invalid, must be <= 32)",length);
proto_item_append_text (ti," [Invalid prefix length %u > 32]",length);
} else if (length == 0) {
proto_tree_add_text (tree,tvb,40,1,"Prefix length = %u (invalid, must be > 0)",length);
proto_item_append_text (ti," [Invalid prefix length %u]",length);
} else {
proto_tree_add_text (tree,tvb,40,1,"Prefix Length = %u",length);
addr_len=(length+7)/8;
ip_addr[0]=ip_addr[1]=ip_addr[2]=ip_addr[3]=0;
/* XXX - the EIGRP page whose URL appears at the top says this
field is 24 bits; what if the prefix length is > 24? */
tvb_memcpy(tvb,ip_addr,41,addr_len);
if (length%8)
ip_addr[addr_len - 1] &= ((0xff00 >> (length % 8)) & 0xff);
proto_tree_add_text (tree,tvb,41,addr_len,"Destination = %s",ip_to_str(ip_addr));
proto_item_append_text (ti," = %s/%u%s",ip_to_str(ip_addr),length,((tvb_get_ntohl(tvb,4)==0xffffffff)?" - Destination unreachable":""));
}