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 void
printpacket(int dir,mb_t * m)17 printpacket(int dir, mb_t *m)
18 {
19 u_short len, off;
20 tcphdr_t *tcp;
21 uint16_t tcpflags;
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) &&
86 ((tcpflags = __tcp_get_flags(tcp)) != 0)) {
87 putchar(' ');
88 if (tcpflags & TH_FIN)
89 putchar('F');
90 if (tcpflags & TH_SYN)
91 putchar('S');
92 if (tcpflags & TH_RST)
93 putchar('R');
94 if (tcpflags & TH_PUSH)
95 putchar('P');
96 if (tcpflags & TH_ACK)
97 putchar('A');
98 if (tcpflags & TH_URG)
99 putchar('U');
100 if (tcpflags & TH_ECN)
101 putchar('E');
102 if (tcpflags & TH_CWR)
103 putchar('W');
104 if (tcpflags & TH_AE)
105 putchar('e');
106 }
107 }
108
109 putchar('\n');
110 }
111