1
2/*
3 * Copyright (C) 2012 by Darren Reed.
4 *
5 * See the IPFILTER.LICENCE file for details on licencing.
6 *
7 * $Id$
8 */
9
10#include "ipf.h"
11
12#ifndef	IP_OFFMASK
13# define	IP_OFFMASK	0x3fff
14#endif
15
16
17void
18printpacket(int dir, mb_t *m)
19{
20	u_short len, off;
21	tcphdr_t *tcp;
22	ip_t *ip;
23
24	ip = MTOD(m, ip_t *);
25
26	if (IP_V(ip) == 6) {
27#ifdef USE_INET6
28		len = ntohs(((ip6_t *)ip)->ip6_plen);
29#else
30		len = ntohs(((u_short *)ip)[2]);
31#endif
32		len += 40;
33	} else {
34		len = ntohs(ip->ip_len);
35	}
36	ASSERT(len == msgdsize(m));
37
38	if ((opts & OPT_HEX) == OPT_HEX) {
39		u_char *s;
40		int i;
41
42		for (; m != NULL; m = m->mb_next) {
43			len = m->mb_len;
44			for (s = (u_char *)m->mb_data, i = 0; i < len; i++) {
45				PRINTF("%02x", *s++ & 0xff);
46				if (len - i > 1) {
47					i++;
48					PRINTF("%02x", *s++ & 0xff);
49				}
50				putchar(' ');
51			}
52		}
53		putchar('\n');
54		putchar('\n');
55		return;
56	}
57
58	if (IP_V(ip) == 6) {
59		printpacket6(dir, m);
60		return;
61	}
62
63	if (dir)
64		PRINTF("> ");
65	else
66		PRINTF("< ");
67
68	PRINTF("%s ", IFNAME(m->mb_ifp));
69
70	off = ntohs(ip->ip_off);
71	tcp = (struct tcphdr *)((char *)ip + (IP_HL(ip) << 2));
72	PRINTF("ip #%d %d(%d) %d", ntohs(ip->ip_id), ntohs(ip->ip_len),
73	       IP_HL(ip) << 2, ip->ip_p);
74	if (off & IP_OFFMASK)
75		PRINTF(" @%d", off << 3);
76	PRINTF(" %s", inet_ntoa(ip->ip_src));
77	if (!(off & IP_OFFMASK))
78		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
79			PRINTF(",%d", ntohs(tcp->th_sport));
80	PRINTF(" > ");
81	PRINTF("%s", inet_ntoa(ip->ip_dst));
82	if (!(off & IP_OFFMASK)) {
83		if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
84			PRINTF(",%d", ntohs(tcp->th_dport));
85		if ((ip->ip_p == IPPROTO_TCP) && (tcp->th_flags != 0)) {
86			putchar(' ');
87			if (tcp->th_flags & TH_FIN)
88				putchar('F');
89			if (tcp->th_flags & TH_SYN)
90				putchar('S');
91			if (tcp->th_flags & TH_RST)
92				putchar('R');
93			if (tcp->th_flags & TH_PUSH)
94				putchar('P');
95			if (tcp->th_flags & TH_ACK)
96				putchar('A');
97			if (tcp->th_flags & TH_URG)
98				putchar('U');
99			if (tcp->th_flags & TH_ECN)
100				putchar('E');
101			if (tcp->th_flags & TH_CWR)
102				putchar('C');
103		}
104	}
105
106	putchar('\n');
107}
108