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 17 void 18 printpacket(int dir, mb_t *m) 19 { 20 u_short len, off; 21 tcphdr_t *tcp; 22 uint16_t tcpflags; 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) && ((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