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.
14 #include <netinet/in.h> /* for ntohs() */
15 #include <arpa/inet.h> /* for inet_ntop() */
16 #include <asm/byteorder.h>
19 #include "proto_struct.h"
20 #include "dissector_eth.h"
25 #if defined(__LITTLE_ENDIAN_BITFIELD)
26 __extension__
uint8_t h_ihl
:4,
28 #elif defined (__BIG_ENDIAN_BITFIELD)
29 __extension__
uint8_t h_version
:4,
32 # error "Please fix <asm/byteorder.h>"
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
));
65 ssize_t opts_len
, opt_len
;
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
));
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");
94 tprintf("%s should be 0x%.4x%s", colorize_start_full(black
, red
),
95 csum_expected(ip
->h_check
, csum
), colorize_end());
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
));
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
122 if (opt_len
> opts_len
) {
123 tprintf(", Len (%u, invalid) ]\n", opt_len
);
126 tprintf(", Len (%u) ]\n", opt_len
);
128 tprintf(" [ Data hex ");
129 for (opt_len
-= 2; opt_len
> 0; opt_len
--)
130 tprintf(" %.2x", *(++opt
));
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
, ð_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
));
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
, ð_lay3
, ip
->h_protocol
);
163 struct protocol ipv4_ops
= {
166 .print_less
= ipv4_less
,
169 #endif /* PROTO_IPV4_H */