2 * Netgrind -- IP Layer Analyser
4 * (c) 2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
11 #include "netgrind/pkt.h"
12 #include "netgrind/netgrind.h"
15 #include <netinet/in.h>
16 #include <netinet/ip.h>
18 uns tcpip_calc_checksum(void *data, uns len, uns csum)
24 csum += (x[0] << 8) | x[1];
25 if (csum & 0xffff0000)
36 if (csum & 0xffff0000)
45 uns tcpip_verify_checksum(uns csum)
48 /* FIXME: Fix checksum calculation! */
49 return (csum == 0xffff);
52 struct pkt_stats stat_ip_in, stat_ip_invalid, stat_ip_uninteresting, stat_ip_fragmented, stat_ip_badsum;
54 void ip_got_packet(struct pkt *p)
58 pkt_account(&stat_ip_in, p);
59 if (!(iph = pkt_peek(p, sizeof(*iph))))
63 if (iph->version != 4)
65 uns hdrlen = 4*iph->ihl;
66 if (pkt_len(p) < hdrlen)
68 if (!tcpip_verify_checksum(tcpip_calc_checksum(p->data, hdrlen, 0)))
70 pkt_account(&stat_ip_badsum, p);
73 uns len = ntohs(iph->tot_len);
74 if (len < hdrlen || len > pkt_len(p))
76 pkt_unappend(p, pkt_len(p) - len);
79 if (iph->protocol != IPPROTO_TCP)
81 pkt_account(&stat_ip_uninteresting, p);
84 /* XXX: Fragmentation not supported yet, but well-behaved TCP stacks don't use it anyway */
85 if (ntohs(iph->frag_off) & 0x3fff)
87 pkt_account(&stat_ip_fragmented, p);
90 tcp_got_packet(iph, p);
94 pkt_account(&stat_ip_invalid, p);