|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_internal.c,v 1.14 2004/03/16 18:40:59 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_internal.c - secret routines!
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
|
Packit |
03b34a |
* All rights reserved.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
03b34a |
* modification, are permitted provided that the following conditions
|
|
Packit |
03b34a |
* are met:
|
|
Packit |
03b34a |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
03b34a |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
03b34a |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
03b34a |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
03b34a |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
03b34a |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
Packit |
03b34a |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
03b34a |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
03b34a |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
03b34a |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
03b34a |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
03b34a |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
03b34a |
* SUCH DAMAGE.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if (HAVE_CONFIG_H)
|
|
Packit |
03b34a |
#include "../include/config.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#if (!(_WIN32) || (__CYGWIN__))
|
|
Packit |
03b34a |
#include "../include/libnet.h"
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include "../include/win32/libnet.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
void
|
|
Packit |
03b34a |
libnet_diag_dump_hex(const uint8_t *packet, uint32_t len, int swap, FILE *stream)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
int i, s_cnt;
|
|
Packit |
03b34a |
uint16_t *p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
p = (uint16_t *)packet;
|
|
Packit |
03b34a |
s_cnt = len / sizeof(uint16_t);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
fprintf(stream, "\t");
|
|
Packit |
03b34a |
for (i = 0; --s_cnt >= 0; i++)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if ((!(i % 8)))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stream, "\n%02x\t", (i * 2));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stream, "%04x ", swap ? ntohs(*(p++)) : *(p++));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Mop up an odd byte.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
if (len & 1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if ((!(i % 8)))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stream, "\n%02x\t", (i * 2));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stream, "%02x ", *(uint8_t *)p);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stream, "\n");
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
void
|
|
Packit |
03b34a |
libnet_diag_dump_context(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
fprintf(stderr, "fd:\t\t%d\n", l->fd);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
switch (l->injection_type)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
case LIBNET_LINK:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tLIBNET_LINK\n");
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
case LIBNET_RAW4:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tLIBNET_RAW4\n");
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
case LIBNET_RAW6:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tLIBNET_RAW6\n");
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
case LIBNET_LINK_ADV:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tLIBNET_LINK_ADV\n");
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
case LIBNET_RAW4_ADV:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tLIBNET_RAW4_ADV\n");
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
case LIBNET_RAW6_ADV:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tLIBNET_RAW6_ADV\n");
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
default:
|
|
Packit |
03b34a |
fprintf(stderr, "injection type:\tinvalid injection type %d\n",
|
|
Packit |
03b34a |
l->injection_type);
|
|
Packit |
03b34a |
break;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
fprintf(stderr, "pblock start:\t%p\n", l->protocol_blocks);
|
|
Packit |
03b34a |
fprintf(stderr, "pblock end:\t%p\n", l->pblock_end);
|
|
Packit |
03b34a |
fprintf(stderr, "link type:\t%d\n", l->link_type);
|
|
Packit |
03b34a |
fprintf(stderr, "link offset:\t%d\n", l->link_offset);
|
|
Packit |
03b34a |
fprintf(stderr, "aligner:\t%d\n", l->aligner);
|
|
Packit |
03b34a |
fprintf(stderr, "device:\t\t%s\n", l->device);
|
|
Packit |
03b34a |
fprintf(stderr, "packets sent:\t%lld\n", (long long int)l->stats.packets_sent);
|
|
Packit |
03b34a |
fprintf(stderr, "packet errors:\t%lld\n", (long long int)l->stats.packet_errors);
|
|
Packit |
03b34a |
fprintf(stderr, "bytes written:\t%lld\n", (long long int)l->stats.bytes_written);
|
|
Packit |
03b34a |
fprintf(stderr, "ptag state:\t%d\n", l->ptag_state);
|
|
Packit |
03b34a |
fprintf(stderr, "context label:\t%s\n", l->label);
|
|
Packit |
03b34a |
fprintf(stderr, "last errbuf:\t%s\n", l->err_buf);
|
|
Packit |
03b34a |
fprintf(stderr, "total size:\t%d\n", l->total_size);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
void
|
|
Packit |
03b34a |
libnet_diag_dump_pblock(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
for (p = l->protocol_blocks; p; p = p->next)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stderr, "pblock type:\t%s\n",
|
|
Packit |
03b34a |
libnet_diag_dump_pblock_type(p->type));
|
|
Packit |
03b34a |
fprintf(stderr, "ptag number:\t%d\n", p->ptag);
|
|
Packit |
03b34a |
fprintf(stderr, "pblock address:\t%p\n", p);
|
|
Packit |
03b34a |
fprintf(stderr, "next pblock\t%p ", p->next);
|
|
Packit |
03b34a |
if (p->next)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stderr, "(%s)",
|
|
Packit |
03b34a |
libnet_diag_dump_pblock_type(p->next->type));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stderr, "\n");
|
|
Packit |
03b34a |
fprintf(stderr, "prev pblock\t%p ", p->prev);
|
|
Packit |
03b34a |
if (p->prev)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stderr, "(%s)",
|
|
Packit |
03b34a |
libnet_diag_dump_pblock_type(p->prev->type));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stderr, "\n");
|
|
Packit |
03b34a |
fprintf(stderr, "buf:\t\t");
|
|
Packit |
03b34a |
for (n = 0; n < p->b_len; n++)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stderr, "%02x", p->buf[n]);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stderr, "\nbuffer length:\t%d\n", p->b_len);
|
|
Packit |
03b34a |
if ((p->flags) & LIBNET_PBLOCK_DO_CHECKSUM)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stderr, "checksum flag:\tYes\n");
|
|
Packit |
03b34a |
fprintf(stderr, "chksum length:\t%d\n", p->h_len);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
fprintf(stderr, "checksum flag:\tNo\n");
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
fprintf(stderr, "bytes copied:\t%d\n\n", p->copied);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
char *
|
|
Packit |
03b34a |
libnet_diag_dump_pblock_type(uint8_t type)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
switch (type)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/* below text can be regenerated using ./map-pblock-types */
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ARP_H:
|
|
Packit |
03b34a |
return ("arp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_DHCPV4_H:
|
|
Packit |
03b34a |
return ("dhcpv4");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_DNSV4_H:
|
|
Packit |
03b34a |
return ("dnsv4");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ETH_H:
|
|
Packit |
03b34a |
return ("eth");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_H:
|
|
Packit |
03b34a |
return ("icmpv4");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_ECHO_H:
|
|
Packit |
03b34a |
return ("icmpv4_echo");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_MASK_H:
|
|
Packit |
03b34a |
return ("icmpv4_mask");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_UNREACH_H:
|
|
Packit |
03b34a |
return ("icmpv4_unreach");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_TIMXCEED_H:
|
|
Packit |
03b34a |
return ("icmpv4_timxceed");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_REDIRECT_H:
|
|
Packit |
03b34a |
return ("icmpv4_redirect");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV4_TS_H:
|
|
Packit |
03b34a |
return ("icmpv4_ts");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IGMP_H:
|
|
Packit |
03b34a |
return ("igmp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPV4_H:
|
|
Packit |
03b34a |
return ("ipv4");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPO_H:
|
|
Packit |
03b34a |
return ("ipo");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPDATA:
|
|
Packit |
03b34a |
return ("ipdata");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_H:
|
|
Packit |
03b34a |
return ("ospf");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_HELLO_H:
|
|
Packit |
03b34a |
return ("ospf_hello");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_DBD_H:
|
|
Packit |
03b34a |
return ("ospf_dbd");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_LSR_H:
|
|
Packit |
03b34a |
return ("ospf_lsr");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_LSU_H:
|
|
Packit |
03b34a |
return ("ospf_lsu");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_LSA_H:
|
|
Packit |
03b34a |
return ("ospf_lsa");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_AUTH_H:
|
|
Packit |
03b34a |
return ("ospf_auth");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_OSPF_CKSUM:
|
|
Packit |
03b34a |
return ("ospf_cksum");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_LS_RTR_H:
|
|
Packit |
03b34a |
return ("ls_rtr");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_LS_NET_H:
|
|
Packit |
03b34a |
return ("ls_net");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_LS_SUM_H:
|
|
Packit |
03b34a |
return ("ls_sum");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_LS_AS_EXT_H:
|
|
Packit |
03b34a |
return ("ls_as_ext");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_NTP_H:
|
|
Packit |
03b34a |
return ("ntp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_RIP_H:
|
|
Packit |
03b34a |
return ("rip");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_TCP_H:
|
|
Packit |
03b34a |
return ("tcp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_TCPO_H:
|
|
Packit |
03b34a |
return ("tcpo");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_TCPDATA:
|
|
Packit |
03b34a |
return ("tcpdata");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_UDP_H:
|
|
Packit |
03b34a |
return ("udp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_VRRP_H:
|
|
Packit |
03b34a |
return ("vrrp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_DATA_H:
|
|
Packit |
03b34a |
return ("data");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_CDP_H:
|
|
Packit |
03b34a |
return ("cdp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPSEC_ESP_HDR_H:
|
|
Packit |
03b34a |
return ("ipsec_esp_hdr");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPSEC_ESP_FTR_H:
|
|
Packit |
03b34a |
return ("ipsec_esp_ftr");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPSEC_AH_H:
|
|
Packit |
03b34a |
return ("ipsec_ah");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_802_1Q_H:
|
|
Packit |
03b34a |
return ("802_1q");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_802_2_H:
|
|
Packit |
03b34a |
return ("802_2");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_802_2SNAP_H:
|
|
Packit |
03b34a |
return ("802_2snap");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_802_3_H:
|
|
Packit |
03b34a |
return ("802_3");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_STP_CONF_H:
|
|
Packit |
03b34a |
return ("stp_conf");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_STP_TCN_H:
|
|
Packit |
03b34a |
return ("stp_tcn");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ISL_H:
|
|
Packit |
03b34a |
return ("isl");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPV6_H:
|
|
Packit |
03b34a |
return ("ipv6");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_802_1X_H:
|
|
Packit |
03b34a |
return ("802_1x");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_RPC_CALL_H:
|
|
Packit |
03b34a |
return ("rpc_call");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_MPLS_H:
|
|
Packit |
03b34a |
return ("mpls");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_FDDI_H:
|
|
Packit |
03b34a |
return ("fddi");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_TOKEN_RING_H:
|
|
Packit |
03b34a |
return ("token_ring");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_BGP4_HEADER_H:
|
|
Packit |
03b34a |
return ("bgp4_header");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_BGP4_OPEN_H:
|
|
Packit |
03b34a |
return ("bgp4_open");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_BGP4_UPDATE_H:
|
|
Packit |
03b34a |
return ("bgp4_update");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_BGP4_NOTIFICATION_H:
|
|
Packit |
03b34a |
return ("bgp4_notification");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_GRE_H:
|
|
Packit |
03b34a |
return ("gre");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_GRE_SRE_H:
|
|
Packit |
03b34a |
return ("gre_sre");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPV6_FRAG_H:
|
|
Packit |
03b34a |
return ("ipv6_frag");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPV6_ROUTING_H:
|
|
Packit |
03b34a |
return ("ipv6_routing");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPV6_DESTOPTS_H:
|
|
Packit |
03b34a |
return ("ipv6_destopts");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_IPV6_HBHOPTS_H:
|
|
Packit |
03b34a |
return ("ipv6_hbhopts");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_SEBEK_H:
|
|
Packit |
03b34a |
return ("sebek");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_HSRP_H:
|
|
Packit |
03b34a |
return ("hsrp");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV6_H:
|
|
Packit |
03b34a |
return ("icmpv6");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV6_UNREACH_H:
|
|
Packit |
03b34a |
return ("icmpv6_unreach");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV6_NDP_NSOL_H:
|
|
Packit |
03b34a |
return ("icmpv6_ndp_nsol");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV6_NDP_NADV_H:
|
|
Packit |
03b34a |
return ("icmpv6_ndp_nadv");
|
|
Packit |
03b34a |
case LIBNET_PBLOCK_ICMPV6_NDP_OPT_H:
|
|
Packit |
03b34a |
return ("icmpv6_ndp_opt");
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return ("unrecognized pblock");
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|