def8c29ccc1cbf9d621fe0ea4e64aa2b0d587eb7
2 * copyright (c) 2001-2011 Emil Mikulic.
4 * decode.c: packet decoding.
6 * Given a captured packet, decode it and fill out a pktsummary struct which
7 * will be sent to the accounting code in acct.c
9 * You may use, modify and redistribute this file under the terms of the
10 * GNU General Public License version 2. (see COPYING.GPL)
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
27 #include <arpa/inet.h> /* inet_ntoa() */
29 /* need struct ether_header */
30 #if defined(__NetBSD__) || defined(__OpenBSD__)
31 # include <sys/queue.h>
33 # include <net/if_arp.h>
34 # include <netinet/if_ether.h>
37 # include <sys/ethernet.h>
38 # define ETHER_HDR_LEN 14
41 # include <netinet/if_ether.h>
42 # define ETHER_HDR_LEN 14
44 # include <net/ethernet.h>
48 #ifndef ETHERTYPE_PPPOE
49 #define ETHERTYPE_PPPOE 0x8864
52 #ifndef ETHERTYPE_IPV6
53 # ifdef HAVE_NET_IF_ETHER_H
54 # include <net/if_ether.h> /* ETH_P_IPV6 for GNU/kfreebsd */
57 # define ETHERTYPE_IPV6 ETH_P_IPV6
61 #include <net/if.h> /* struct ifreq */
62 #include <netinet/in_systm.h> /* n_long */
63 #include <netinet/ip.h> /* struct ip */
64 #include <netinet/ip6.h> /* struct ip6_hdr */
66 #include <netinet/tcp.h> /* struct tcphdr */
67 #include <netinet/udp.h> /* struct udphdr */
70 #define IPV6_VERSION 0x60
73 #ifndef IPV6_VERSION_MASK
74 #define IPV6_VERSION_MASK 0xf0
77 static void decode_ether(u_char
*, const struct pcap_pkthdr
*,
79 static void decode_loop(u_char
*, const struct pcap_pkthdr
*,
81 static void decode_null(u_char
*, const struct pcap_pkthdr
*,
83 static void decode_ppp(u_char
*, const struct pcap_pkthdr
*,
85 static void decode_pppoe(u_char
*, const struct pcap_pkthdr
*,
87 static void decode_pppoe_real(const u_char
*pdata
, const uint32_t len
,
88 struct pktsummary
*sm
);
89 static void decode_linux_sll(u_char
*, const struct pcap_pkthdr
*,
91 static void decode_raw(u_char
*, const struct pcap_pkthdr
*,
93 static void decode_ip(const u_char
*pdata
, const uint32_t len
,
94 struct pktsummary
*sm
);
95 static void decode_ipv6(const u_char
*pdata
, const uint32_t len
,
96 struct pktsummary
*sm
);
98 /* Link-type header information */
99 static const struct linkhdr linkhdrs
[] = {
100 /* linktype hdrlen handler */
101 { DLT_EN10MB
, ETHER_HDR_LEN
, decode_ether
},
102 { DLT_LOOP
, NULL_HDR_LEN
, decode_loop
},
103 { DLT_NULL
, NULL_HDR_LEN
, decode_null
},
104 { DLT_PPP
, PPP_HDR_LEN
, decode_ppp
},
105 #if defined(__NetBSD__)
106 { DLT_PPP_SERIAL
, PPP_HDR_LEN
, decode_ppp
},
108 { DLT_FDDI
, FDDI_HDR_LEN
, NULL
},
109 { DLT_PPP_ETHER
, PPPOE_HDR_LEN
, decode_pppoe
},
111 { DLT_LINUX_SLL
, SLL_HDR_LEN
, decode_linux_sll
},
113 { DLT_RAW
, RAW_HDR_LEN
, decode_raw
},
118 * Returns a pointer to the linkhdr record matching the given linktype, or
119 * NULL if no matching entry found.
121 const struct linkhdr
*
122 getlinkhdr(const int linktype
)
126 for (i
=0; linkhdrs
[i
].linktype
!= -1; i
++)
127 if (linkhdrs
[i
].linktype
== linktype
)
128 return (&(linkhdrs
[i
]));
133 * Returns the minimum snaplen needed to decode everything up to the TCP/UDP
134 * packet headers. The IPv6 header is normative. The argument lh is not
135 * allowed to be NULL.
138 getsnaplen(const struct linkhdr
*lh
)
140 return (int)(lh
->hdrlen
+ IPV6_HDR_LEN
+ MAX(TCP_HDR_LEN
, UDP_HDR_LEN
));
143 /* Decoding functions. */
145 decode_ether(u_char
*user _unused_
,
146 const struct pcap_pkthdr
*pheader
,
150 const struct ether_header
*hdr
= (const struct ether_header
*)pdata
;
151 struct pktsummary sm
;
152 memset(&sm
, 0, sizeof(sm
));
153 sm
.time
= pheader
->ts
.tv_sec
;
155 if (pheader
->caplen
< ETHER_HDR_LEN
) {
156 verbosef("ether: packet too short (%u bytes)", pheader
->caplen
);
161 memcpy(sm
.src_mac
, hdr
->ether_shost
.ether_addr_octet
, sizeof(sm
.src_mac
));
162 memcpy(sm
.dst_mac
, hdr
->ether_dhost
.ether_addr_octet
, sizeof(sm
.dst_mac
));
164 memcpy(sm
.src_mac
, hdr
->ether_shost
, sizeof(sm
.src_mac
));
165 memcpy(sm
.dst_mac
, hdr
->ether_dhost
, sizeof(sm
.dst_mac
));
168 type
= ntohs( hdr
->ether_type
);
172 if (!opt_want_pppoe
) {
173 decode_ip(pdata
+ ETHER_HDR_LEN
,
174 pheader
->caplen
- ETHER_HDR_LEN
, &sm
);
177 verbosef("ether: discarded IP packet, expecting PPPoE instead");
180 /* known protocol, don't complain about it. */
182 case ETHERTYPE_PPPOE
:
184 decode_pppoe_real(pdata
+ ETHER_HDR_LEN
,
185 pheader
->caplen
- ETHER_HDR_LEN
, &sm
);
187 verbosef("ether: got PPPoE frame: maybe you want --pppoe");
190 verbosef("ether: unknown protocol (0x%04x)", type
);
194 /* Very similar to decode_null, except on OpenBSD we need to think
195 * about family endianness.
198 decode_loop(u_char
*user _unused_
,
199 const struct pcap_pkthdr
*pheader
,
203 struct pktsummary sm
;
204 memset(&sm
, 0, sizeof(sm
));
206 if (pheader
->caplen
< NULL_HDR_LEN
) {
207 verbosef("loop: packet too short (%u bytes)", pheader
->caplen
);
210 family
= *(const uint32_t *)pdata
;
212 family
= ntohl(family
);
214 if (family
== AF_INET
) {
215 decode_ip(pdata
+ NULL_HDR_LEN
, pheader
->caplen
- NULL_HDR_LEN
, &sm
);
216 sm
.time
= pheader
->ts
.tv_sec
;
219 else if (family
== AF_INET6
) {
220 decode_ipv6(pdata
+ NULL_HDR_LEN
, pheader
->caplen
- NULL_HDR_LEN
, &sm
);
221 sm
.time
= pheader
->ts
.tv_sec
;
225 verbosef("loop: unknown family (%x)", family
);
229 decode_null(u_char
*user _unused_
,
230 const struct pcap_pkthdr
*pheader
,
234 struct pktsummary sm
;
235 memset(&sm
, 0, sizeof(sm
));
237 if (pheader
->caplen
< NULL_HDR_LEN
) {
238 verbosef("null: packet too short (%u bytes)", pheader
->caplen
);
241 family
= *(const uint32_t *)pdata
;
242 if (family
== AF_INET
) {
243 decode_ip(pdata
+ NULL_HDR_LEN
, pheader
->caplen
- NULL_HDR_LEN
, &sm
);
244 sm
.time
= pheader
->ts
.tv_sec
;
247 else if (family
== AF_INET6
) {
248 decode_ipv6(pdata
+ NULL_HDR_LEN
, pheader
->caplen
- NULL_HDR_LEN
, &sm
);
249 sm
.time
= pheader
->ts
.tv_sec
;
253 verbosef("null: unknown family (%x)", family
);
257 decode_ppp(u_char
*user _unused_
,
258 const struct pcap_pkthdr
*pheader
,
261 struct pktsummary sm
;
262 memset(&sm
, 0, sizeof(sm
));
264 if (pheader
->caplen
< PPPOE_HDR_LEN
) {
265 verbosef("ppp: packet too short (%u bytes)", pheader
->caplen
);
269 if (pdata
[2] == 0x00 && pdata
[3] == 0x21) {
270 decode_ip(pdata
+ PPP_HDR_LEN
, pheader
->caplen
- PPP_HDR_LEN
, &sm
);
271 sm
.time
= pheader
->ts
.tv_sec
;
274 verbosef("non-IP PPP packet; ignoring.");
278 decode_pppoe(u_char
*user _unused_
,
279 const struct pcap_pkthdr
*pheader
,
282 struct pktsummary sm
;
283 memset(&sm
, 0, sizeof(sm
));
284 sm
.time
= pheader
->ts
.tv_sec
;
285 decode_pppoe_real(pdata
, pheader
->caplen
, &sm
);
289 decode_pppoe_real(const u_char
*pdata
, const uint32_t len
,
290 struct pktsummary
*sm
)
292 if (len
< PPPOE_HDR_LEN
) {
293 verbosef("pppoe: packet too short (%u bytes)", len
);
297 if (pdata
[1] != 0x00) {
298 verbosef("pppoe: code = 0x%02x, expecting 0; ignoring.", pdata
[1]);
302 if ((pdata
[6] == 0xc0) && (pdata
[7] == 0x21)) return; /* LCP */
303 if ((pdata
[6] == 0xc0) && (pdata
[7] == 0x25)) return; /* LQR */
305 if ((pdata
[6] == 0x00) && (pdata
[7] == 0x21)) {
306 decode_ip(pdata
+ PPPOE_HDR_LEN
, len
- PPPOE_HDR_LEN
, sm
);
309 verbosef("pppoe: non-IP PPPoE packet (0x%02x%02x); ignoring.",
313 /* very similar to decode_ether ... */
315 decode_linux_sll(u_char
*user _unused_
,
316 const struct pcap_pkthdr
*pheader
,
319 const struct sll_header
{
320 uint16_t packet_type
;
321 uint16_t device_type
;
322 uint16_t addr_length
;
323 #define SLL_MAX_ADDRLEN 8
324 uint8_t addr
[SLL_MAX_ADDRLEN
];
326 } *hdr
= (const struct sll_header
*)pdata
;
328 struct pktsummary sm
;
329 memset(&sm
, 0, sizeof(sm
));
331 if (pheader
->caplen
< SLL_HDR_LEN
) {
332 verbosef("linux_sll: packet too short (%u bytes)", pheader
->caplen
);
336 type
= ntohs( hdr
->ether_type
);
340 decode_ip(pdata
+ SLL_HDR_LEN
, pheader
->caplen
- SLL_HDR_LEN
, &sm
);
341 sm
.time
= pheader
->ts
.tv_sec
;
345 /* known protocol, don't complain about it. */
348 verbosef("linux_sll: unknown protocol (%04x)", type
);
353 decode_raw(u_char
*user _unused_
,
354 const struct pcap_pkthdr
*pheader
,
357 struct pktsummary sm
;
358 memset(&sm
, 0, sizeof(sm
));
360 decode_ip(pdata
, pheader
->caplen
, &sm
);
361 sm
.time
= pheader
->ts
.tv_sec
;
365 static void decode_ip_payload(const u_char
*pdata
, const uint32_t len
,
366 struct pktsummary
*sm
);
369 decode_ip(const u_char
*pdata
, const uint32_t len
, struct pktsummary
*sm
)
371 const struct ip
*hdr
= (const struct ip
*)pdata
;
373 if (hdr
->ip_v
== 6) {
374 /* Redirect parsing of IPv6 packets. */
375 decode_ipv6(pdata
, len
, sm
);
378 if (len
< IP_HDR_LEN
) {
379 verbosef("ip: packet too short (%u bytes)", len
);
382 if (hdr
->ip_v
!= 4) {
383 verbosef("ip: version %d (expecting 4 or 6)", hdr
->ip_v
);
387 sm
->len
= ntohs(hdr
->ip_len
);
388 sm
->proto
= hdr
->ip_p
;
390 sm
->src
.family
= IPv4
;
391 sm
->src
.ip
.v4
= hdr
->ip_src
.s_addr
;
393 sm
->dst
.family
= IPv4
;
394 sm
->dst
.ip
.v4
= hdr
->ip_dst
.s_addr
;
396 decode_ip_payload(pdata
+ IP_HDR_LEN
, len
- IP_HDR_LEN
, sm
);
400 decode_ipv6(const u_char
*pdata
, const uint32_t len
, struct pktsummary
*sm
)
402 const struct ip6_hdr
*hdr
= (const struct ip6_hdr
*)pdata
;
404 if (len
< IPV6_HDR_LEN
) {
405 verbosef("ipv6: packet too short (%u bytes)", len
);
409 if ((hdr
->ip6_vfc
& IPV6_VERSION_MASK
) != IPV6_VERSION
) {
410 verbosef("ipv6: bad version (%02x, expecting %02x)",
411 hdr
->ip6_vfc
& IPV6_VERSION_MASK
,
416 sm
->len
= ntohs(hdr
->ip6_plen
) + IPV6_HDR_LEN
;
417 sm
->proto
= hdr
->ip6_nxt
;
419 sm
->src
.family
= IPv6
;
420 memcpy(&sm
->src
.ip
.v6
, &hdr
->ip6_src
, sizeof(sm
->src
.ip
.v6
));
422 sm
->dst
.family
= IPv6
;
423 memcpy(&sm
->dst
.ip
.v6
, &hdr
->ip6_dst
, sizeof(sm
->dst
.ip
.v6
));
425 decode_ip_payload(pdata
+ IPV6_HDR_LEN
, len
- IPV6_HDR_LEN
, sm
);
429 decode_ip_payload(const u_char
*pdata
, const uint32_t len
,
430 struct pktsummary
*sm
)
434 const struct tcphdr
*thdr
= (const struct tcphdr
*)pdata
;
435 if (len
< TCP_HDR_LEN
) {
436 verbosef("tcp: packet too short (%u bytes)", len
);
437 sm
->proto
= IPPROTO_INVALID
; /* don't do accounting! */
440 sm
->src_port
= ntohs(thdr
->th_sport
);
441 sm
->dst_port
= ntohs(thdr
->th_dport
);
442 sm
->tcp_flags
= thdr
->th_flags
&
443 (TH_FIN
|TH_SYN
|TH_RST
|TH_PUSH
|TH_ACK
|TH_URG
);
448 const struct udphdr
*uhdr
= (const struct udphdr
*)pdata
;
449 if (len
< UDP_HDR_LEN
) {
450 verbosef("udp: packet too short (%u bytes)", len
);
451 sm
->proto
= IPPROTO_INVALID
; /* don't do accounting! */
454 sm
->src_port
= ntohs(uhdr
->uh_sport
);
455 sm
->dst_port
= ntohs(uhdr
->uh_dport
);
464 /* known protocol, don't complain about it */
468 verbosef("ip: unknown protocol %d", sm
->proto
);
472 /* vim:set ts=3 sw=3 tw=78 expandtab: */