dissector_80211: start of 80211 collection
[netsniff-ng.git] / src / proto_ipv4.h
blob19d04cca3da520a091407253074cf5874cab4206
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>.
4 * Copyright (C) 2009, 2010 Daniel Borkmann
5 * Copyright (C) 2012 Christoph Jaeger <christoph@netsniff-ng.org>
6 * Subject to the GPL, version 2.
7 */
9 #ifndef PROTO_IPV4_H
10 #define PROTO_IPV4_H
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <netinet/in.h> /* for ntohs() */
15 #include <arpa/inet.h> /* for inet_ntop() */
16 #include <asm/byteorder.h>
18 #include "csum.h"
19 #include "proto_struct.h"
20 #include "dissector_eth.h"
21 #include "pkt_buff.h"
22 #include "built_in.h"
24 struct ipv4hdr {
25 #if defined(__LITTLE_ENDIAN_BITFIELD)
26 __extension__ uint8_t h_ihl:4,
27 h_version:4;
28 #elif defined (__BIG_ENDIAN_BITFIELD)
29 __extension__ uint8_t h_version:4,
30 h_ihl:4;
31 #else
32 # error "Please fix <asm/byteorder.h>"
33 #endif
34 uint8_t h_tos;
35 uint16_t h_tot_len;
36 uint16_t h_id;
37 uint16_t h_frag_off;
38 uint8_t h_ttl;
39 uint8_t h_protocol;
40 uint16_t h_check;
41 uint32_t h_saddr;
42 uint32_t h_daddr;
43 } __packed;
45 #define FRAG_OFF_RESERVED_FLAG(x) ((x) & 0x8000)
46 #define FRAG_OFF_NO_FRAGMENT_FLAG(x) ((x) & 0x4000)
47 #define FRAG_OFF_MORE_FRAGMENT_FLAG(x) ((x) & 0x2000)
48 #define FRAG_OFF_FRAGMENT_OFFSET(x) ((x) & 0x1fff)
50 /* IP Option Numbers (http://www.iana.org/assignments/ip-parameters) */
51 #define IP_OPT_EOOL 0x00
52 #define IP_OPT_NOP 0x01
54 #define IP_OPT_COPIED_FLAG(x) ((x) & 0x80)
55 #define IP_OPT_CLASS(x) (((x) & 0x60) >> 5)
56 #define IP_OPT_NUMBER(x) ((x) & 0x1F)
58 static inline void ipv4(struct pkt_buff *pkt)
60 uint16_t csum, frag_off;
61 char src_ip[INET_ADDRSTRLEN];
62 char dst_ip[INET_ADDRSTRLEN];
63 struct ipv4hdr *ip = (struct ipv4hdr *) pkt_pull(pkt, sizeof(*ip));
64 uint8_t *opt;
65 ssize_t opts_len, opt_len;
67 if (!ip)
68 return;
70 frag_off = ntohs(ip->h_frag_off);
71 csum = calc_csum(ip, ip->h_ihl * 4, 0);
73 inet_ntop(AF_INET, &ip->h_saddr, src_ip, sizeof(src_ip));
74 inet_ntop(AF_INET, &ip->h_daddr, dst_ip, sizeof(dst_ip));
76 tprintf(" [ IPv4 ");
77 tprintf("Addr (%s => %s), ", src_ip, dst_ip);
78 tprintf("Proto (%u), ", ip->h_protocol);
79 tprintf("TTL (%u), ", ip->h_ttl);
80 tprintf("TOS (%u), ", ip->h_tos);
81 tprintf("Ver (%u), ", ip->h_version);
82 tprintf("IHL (%u), ", ip->h_ihl);
83 tprintf("Tlen (%u), ", ntohs(ip->h_tot_len));
84 tprintf("ID (%u), ", ntohs(ip->h_id));
85 tprintf("Res (%u), NoFrag (%u), MoreFrag (%u), FragOff (%u), ",
86 FRAG_OFF_RESERVED_FLAG(frag_off) ? 1 : 0,
87 FRAG_OFF_NO_FRAGMENT_FLAG(frag_off) ? 1 : 0,
88 FRAG_OFF_MORE_FRAGMENT_FLAG(frag_off) ? 1 : 0,
89 FRAG_OFF_FRAGMENT_OFFSET(frag_off));
90 tprintf("CSum (0x%.4x) is %s", ntohs(ip->h_check),
91 csum ? colorize_start_full(black, red) "bogus (!)"
92 colorize_end() : "ok");
93 if (csum)
94 tprintf("%s should be 0x%.4x%s", colorize_start_full(black, red),
95 csum_expected(ip->h_check, csum), colorize_end());
96 tprintf(" ]\n");
98 opts_len = max((uint8_t) ip->h_ihl, sizeof(*ip) / sizeof(uint32_t)) *
99 sizeof(uint32_t) - sizeof(*ip);
101 for (opt = pkt_pull(pkt, opts_len); opt && opts_len > 0; opt++) {
102 tprintf(" [ Option Copied (%u), Class (%u), Number (%u)",
103 IP_OPT_COPIED_FLAG(*opt) ? 1 : 0, IP_OPT_CLASS(*opt),
104 IP_OPT_NUMBER(*opt));
106 switch (*opt) {
107 case IP_OPT_EOOL:
108 case IP_OPT_NOP:
109 tprintf(" ]\n");
110 opts_len--;
111 break;
112 default:
114 * Assuming that EOOL and NOP are the only single-byte
115 * options, treat all other options as variable in
116 * length with a minimum of 2.
118 * TODO: option length might be incorrect in malformed packets,
119 * check and handle that
121 opt_len = *(++opt);
122 if (opt_len > opts_len) {
123 tprintf(", Len (%u, invalid) ]\n", opt_len);
124 goto out;
125 } else
126 tprintf(", Len (%u) ]\n", opt_len);
127 opts_len -= opt_len;
128 tprintf(" [ Data hex ");
129 for (opt_len -= 2; opt_len > 0; opt_len--)
130 tprintf(" %.2x", *(++opt));
131 tprintf(" ]\n");
132 break;
135 out:
136 /* cut off everything that is not part of IPv4 payload */
137 pkt_trim(pkt, pkt_len(pkt) - min(pkt_len(pkt), (ntohs(ip->h_tot_len) - ip->h_ihl * sizeof(uint32_t))));
138 pkt_set_proto(pkt, &eth_lay3, ip->h_protocol);
141 static inline void ipv4_less(struct pkt_buff *pkt)
143 char src_ip[INET_ADDRSTRLEN];
144 char dst_ip[INET_ADDRSTRLEN];
145 struct ipv4hdr *ip = (struct ipv4hdr *) pkt_pull(pkt, sizeof(*ip));
147 if (!ip)
148 return;
150 inet_ntop(AF_INET, &ip->h_saddr, src_ip, sizeof(src_ip));
151 inet_ntop(AF_INET, &ip->h_daddr, dst_ip, sizeof(dst_ip));
153 tprintf(" %s/%s Len %u", src_ip, dst_ip,
154 ntohs(ip->h_tot_len));
156 /* cut off IP options and everything that is not part of IPv4 payload */
157 pkt_pull(pkt, max((uint8_t) ip->h_ihl, sizeof(*ip) / sizeof(uint32_t))
158 * sizeof(uint32_t) - sizeof(*ip));
159 pkt_trim(pkt, pkt_len(pkt) - min(pkt_len(pkt), (ntohs(ip->h_tot_len) - ip->h_ihl * sizeof(uint32_t))));
160 pkt_set_proto(pkt, &eth_lay3, ip->h_protocol);
163 struct protocol ipv4_ops = {
164 .key = 0x0800,
165 .print_full = ipv4,
166 .print_less = ipv4_less,
169 #endif /* PROTO_IPV4_H */