1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/* darkstat 3
* copyright (c) 2001-2007 Emil Mikulic.
*
* decode.c: packet decoding.
*
* Given a captured packet, decode it and fill out a pktsummary struct which
* will be sent to the accounting code in acct.c
*
* You may use, modify and redistribute this file under the terms of the
* GNU General Public License version 2. (see COPYING.GPL)
*/
#include "darkstat.h"
#include "acct.h"
#include "cap.h"
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <assert.h>
#include "err.h"
#include <pcap.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h> /* inet_ntoa() */
/* need struct ether_header */
#if defined(__NetBSD__) || defined(__OpenBSD__)
# include <sys/queue.h>
# include <net/if.h>
# include <net/if_arp.h>
# include <netinet/if_ether.h>
#else
# ifdef __sun
# include <sys/ethernet.h>
# define ETHER_HDR_LEN 14
# else
# ifdef _AIX
# include <netinet/if_ether.h>
# define ETHER_HDR_LEN 14
# else
# include <net/ethernet.h>
# endif
# endif
#endif
#include <net/if.h> /* struct ifreq */
#include <netinet/in_systm.h> /* n_long */
#include <netinet/ip.h> /* struct ip */
#define __FAVOR_BSD
#include <netinet/tcp.h> /* struct tcphdr */
#include <netinet/udp.h> /* struct udphdr */
static void decode_ether(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void decode_loop(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void decode_ppp(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void decode_pppoe(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void decode_linux_sll(u_char *, const struct pcap_pkthdr *,
const u_char *);
static void decode_ip(const u_char *pdata, const uint32_t len,
pktsummary *sm);
/* Link-type header information */
static const linkhdr_t linkhdrs[] = {
/* linktype hdrlen handler */
{ DLT_EN10MB, ETHER_HDR_LEN, decode_ether },
{ DLT_LOOP, NULL_HDR_LEN, decode_loop },
{ DLT_NULL, NULL_HDR_LEN, decode_loop },
{ DLT_PPP, PPP_HDR_LEN, decode_ppp },
#if defined(__NetBSD__)
{ DLT_PPP_SERIAL, PPP_HDR_LEN, decode_ppp },
#endif
{ DLT_FDDI, FDDI_HDR_LEN, NULL },
{ DLT_PPP_ETHER, PPPOE_HDR_LEN, decode_pppoe },
#ifdef DLT_LINUX_SLL
{ DLT_LINUX_SLL, SLL_HDR_LEN, decode_linux_sll },
#endif
{ -1, -1, NULL }
};
/*
* Returns a pointer to the linkhdr_t record matching the given linktype, or
* NULL if no matching entry found.
*/
const linkhdr_t *
getlinkhdr(int linktype)
{
int i;
for (i=0; linkhdrs[i].linktype != -1; i++)
if (linkhdrs[i].linktype == linktype)
return (&(linkhdrs[i]));
return (NULL);
}
/*
* Returns the minimum caplen needed to decode everything up to the TCP/UDP
* packet headers. Argument lh is not allowed to be NULL.
*/
int
getcaplen(const linkhdr_t *lh)
{
assert(lh != NULL);
return (lh->hdrlen + IP_HDR_LEN + max(TCP_HDR_LEN, UDP_HDR_LEN));
}
/*
* Convert IP address to a numbers-and-dots notation in a static buffer
* provided by inet_ntoa().
*/
char *
ip_to_str(const in_addr_t ip)
{
struct in_addr in;
in.s_addr = ip;
return (inet_ntoa(in));
}
/* Decoding functions. */
static void
decode_ether(u_char *user _unused_,
const struct pcap_pkthdr *pheader,
const u_char *pdata)
{
u_short type;
const struct ether_header *hdr = (const struct ether_header *)pdata;
pktsummary sm;
memset(&sm, 0, sizeof(sm));
if (pheader->caplen < ETHER_HDR_LEN) {
verbosef("ether: packet too short (%u bytes)", pheader->caplen);
return;
}
#ifdef __sun
memcpy(sm.src_mac, hdr->ether_shost.ether_addr_octet, sizeof(sm.src_mac));
memcpy(sm.dst_mac, hdr->ether_dhost.ether_addr_octet, sizeof(sm.dst_mac));
#else
memcpy(sm.src_mac, hdr->ether_shost, sizeof(sm.src_mac));
memcpy(sm.dst_mac, hdr->ether_dhost, sizeof(sm.dst_mac));
#endif
type = ntohs( hdr->ether_type );
switch (type) {
case ETHERTYPE_IP:
decode_ip(pdata + ETHER_HDR_LEN, pheader->caplen - ETHER_HDR_LEN, &sm);
sm.time = pheader->ts.tv_sec;
acct_for(&sm);
break;
case ETHERTYPE_ARP:
/* known protocol, don't complain about it. */
break;
default:
verbosef("ether: unknown protocol (%04x)", type);
}
}
static void
decode_loop(u_char *user _unused_,
const struct pcap_pkthdr *pheader,
const u_char *pdata)
{
uint32_t family;
pktsummary sm;
memset(&sm, 0, sizeof(sm));
if (pheader->caplen < NULL_HDR_LEN) {
verbosef("loop: packet too short (%u bytes)", pheader->caplen);
return;
}
family = *(const uint32_t *)pdata;
#ifdef __OpenBSD__
family = ntohl(family);
#endif
if (family == AF_INET) {
/* OpenBSD tun or FreeBSD tun or FreeBSD lo */
decode_ip(pdata + NULL_HDR_LEN, pheader->caplen - NULL_HDR_LEN, &sm);
sm.time = pheader->ts.tv_sec;
acct_for(&sm);
}
else
verbosef("loop: unknown family (%x)", family);
}
static void
decode_ppp(u_char *user _unused_,
const struct pcap_pkthdr *pheader,
const u_char *pdata)
{
pktsummary sm;
memset(&sm, 0, sizeof(sm));
if (pdata[2] == 0x00 && pdata[3] == 0x21) {
decode_ip(pdata + PPP_HDR_LEN, pheader->caplen - PPP_HDR_LEN, &sm);
sm.time = pheader->ts.tv_sec;
acct_for(&sm);
} else
verbosef("non-IP PPP packet; ignoring.");
}
static void
decode_pppoe(u_char *user _unused_,
const struct pcap_pkthdr *pheader,
const u_char *pdata)
{
pktsummary sm;
memset(&sm, 0, sizeof(sm));
if (pheader->caplen < PPPOE_HDR_LEN) {
verbosef("loop: packet too short (%u bytes)", pheader->caplen);
return;
}
/*
* First, check if this is a session PPPoE packet
* by checking the CODE part of the header
* (2nd byte in header)
*/
if (pdata[1] == 0x00) {
/*
* Next, check if PPP packet type is 0x0021,
* which is PPP_IP
*/
if (pdata[6] == 0x00 && pdata[7] == 0x21) {
decode_ip(pdata + PPPOE_HDR_LEN, pheader->caplen - PPPOE_HDR_LEN, &sm);
sm.time = pheader->ts.tv_sec;
acct_for(&sm);
}
else
verbosef("non-IP PPPoE packet; ignoring.");
}
else
verbosef("non-session PPPoE packet; ignoring.");
}
/* very similar to decode_ether ... */
static void
decode_linux_sll(u_char *user _unused_,
const struct pcap_pkthdr *pheader,
const u_char *pdata)
{
const struct sll_header {
uint16_t packet_type;
uint16_t device_type;
uint16_t addr_length;
#define SLL_MAX_ADDRLEN 8
uint8_t addr[SLL_MAX_ADDRLEN];
uint16_t ether_type;
} *hdr = (const struct sll_header *)pdata;
u_short type;
pktsummary sm;
memset(&sm, 0, sizeof(sm));
if (pheader->caplen < SLL_HDR_LEN) {
verbosef("linux_sll: packet too short (%u bytes)", pheader->caplen);
return;
}
type = ntohs( hdr->ether_type );
switch (type) {
case ETHERTYPE_IP:
decode_ip(pdata + SLL_HDR_LEN, pheader->caplen - SLL_HDR_LEN, &sm);
sm.time = pheader->ts.tv_sec;
acct_for(&sm);
break;
case ETHERTYPE_ARP:
/* known protocol, don't complain about it. */
break;
default:
verbosef("linux_sll: unknown protocol (%04x)", type);
}
}
static void
decode_ip(const u_char *pdata, const uint32_t len, pktsummary *sm)
{
const struct ip *hdr = (const struct ip *)pdata;
if (len < IP_HDR_LEN) {
verbosef("ip: packet too short (%u bytes)", len);
return;
}
if (hdr->ip_v != 4) {
verbosef("ip: version %d (expecting 4)", hdr->ip_v);
return;
}
sm->len = ntohs(hdr->ip_len);
sm->proto = hdr->ip_p;
sm->src_ip = hdr->ip_src.s_addr;
sm->dest_ip = hdr->ip_dst.s_addr;
switch (sm->proto) {
case IPPROTO_TCP: {
const struct tcphdr *thdr =
(const struct tcphdr *)(pdata + IP_HDR_LEN);
if (len < IP_HDR_LEN + TCP_HDR_LEN) {
verbosef("tcp: packet too short (%u bytes)", len);
return;
}
sm->src_port = ntohs(thdr->th_sport);
sm->dest_port = ntohs(thdr->th_dport);
sm->tcp_flags = thdr->th_flags &
(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG);
break;
}
case IPPROTO_UDP: {
const struct udphdr *uhdr =
(const struct udphdr *)(pdata + IP_HDR_LEN);
if (len < IP_HDR_LEN + UDP_HDR_LEN) {
verbosef("udp: packet too short (%u bytes)", len);
return;
}
sm->src_port = ntohs(uhdr->uh_sport);
sm->dest_port = ntohs(uhdr->uh_dport);
break;
}
case IPPROTO_ICMP:
/* known protocol, don't complain about it */
break;
default:
verbosef("ip: unknown protocol %d", sm->proto);
}
}
/* vim:set ts=3 sw=3 tw=78 expandtab: */
|