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