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