2 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
13 #include <string.h> /* for memcpy */
14 #include <arpa/inet.h>
15 #include <netinet/ip.h>
16 #include <netinet/ip6.h>
17 #include <netinet/tcp.h>
19 #include <libnetfilter_queue/libnetfilter_queue.h>
20 #include <libnetfilter_queue/libnetfilter_queue_tcp.h>
21 #include <libnetfilter_queue/pktbuff.h>
26 * \defgroup tcp TCP helper functions
31 * nfq_tcp_get - get the TCP header
32 * \param pktb: pointer to user-space network packet buffer
34 * This function returns NULL if an invalid TCP header is found. On success,
35 * it returns the TCP header.
37 * \note You have to call nfq_ip_set_transport_header or
38 * nfq_ip6_set_transport_header first to access the TCP header.
40 struct tcphdr *nfq_tcp_get_hdr(struct pkt_buff *pktb)
42 if (pktb->transport_header == NULL)
45 /* No room for the TCP header. */
46 if (pktb->tail - pktb->transport_header < sizeof(struct tcphdr))
49 return (struct tcphdr *)pktb->transport_header;
51 EXPORT_SYMBOL(nfq_tcp_get_hdr);
54 * nfq_tcp_get_payload - get the TCP packet payload
55 * \param tcph: pointer to the TCP header
56 * \param pktb: pointer to user-space network packet buffer
58 void *nfq_tcp_get_payload(struct tcphdr *tcph, struct pkt_buff *pktb)
60 unsigned int doff = tcph->doff * 4;
62 /* malformed TCP data offset. */
63 if (pktb->transport_header + doff >= pktb->tail)
66 return pktb->transport_header + doff;
68 EXPORT_SYMBOL(nfq_tcp_get_payload);
71 * nfq_tcp_get_payload_len - get the tcp packet payload
72 * \param tcph: pointer to the TCP header
73 * \param pktb: pointer to user-space network packet buffer
76 nfq_tcp_get_payload_len(struct tcphdr *tcph, struct pkt_buff *pktb)
78 return pktb->tail - pktb->transport_header;
80 EXPORT_SYMBOL(nfq_tcp_get_payload_len);
83 * nfq_tcp_set_checksum_ipv4 - computes IPv4/TCP packet checksum
84 * \param tcph: pointer to the TCP header
85 * \param iph: pointer to the IPv4 header
88 nfq_tcp_compute_checksum_ipv4(struct tcphdr *tcph, struct iphdr *iph)
90 /* checksum field in header needs to be zero for calculation. */
92 tcph->check = checksum_tcpudp_ipv4(iph);
94 EXPORT_SYMBOL(nfq_tcp_compute_checksum_ipv4);
97 * nfq_tcp_set_checksum_ipv6 - computes IPv6/TCP packet checksum
98 * \param tcph: pointer to the TCP header
99 * \param iph: pointer to the IPv6 header
102 nfq_tcp_compute_checksum_ipv6(struct tcphdr *tcph, struct ip6_hdr *ip6h)
104 /* checksum field in header needs to be zero for calculation. */
106 tcph->check = checksum_tcpudp_ipv6(ip6h, tcph);
108 EXPORT_SYMBOL(nfq_tcp_compute_checksum_ipv6);
111 * The union cast uses a gcc extension to avoid aliasing problems
112 * (union is compatible to any of its members)
113 * This means this part of the code is -fstrict-aliasing safe now.
120 #define tcp_flag_word(tp) ( ((union tcp_word_hdr *)(tp))->words[3])
123 * nfq_pkt_snprintf_tcp_hdr - print tcp header into one buffer in a humnan
125 * \param buf: pointer to buffer that is used to print the object
126 * \param size: size of the buffer (or remaining room in it).
127 * \param tcp: pointer to a valid tcp header.
130 int nfq_tcp_snprintf(char *buf, size_t size, const struct tcphdr *tcph)
134 #define TCP_RESERVED_BITS htonl(0x0F000000)
136 ret = snprintf(buf, size, "SPT=%u DPT=%u SEQ=%u ACK=%u "
137 "WINDOW=%u RES=%0x%02x ",
138 ntohs(tcph->source), ntohs(tcph->dest),
139 ntohl(tcph->seq), ntohl(tcph->ack_seq),
141 (uint8_t)(ntohl(tcp_flag_word(tcph) &
142 TCP_RESERVED_BITS) >> 22));
146 ret = snprintf(buf+len, size-len, "URG ");
150 ret = snprintf(buf+len, size-len, "ACK ");
154 ret = snprintf(buf+len, size-len, "PSH ");
158 ret = snprintf(buf+len, size-len, "RST ");
162 ret = snprintf(buf+len, size-len, "SYN ");
166 ret = snprintf(buf+len, size-len, "FIN ");
169 /* Not TCP options implemented yet, sorry. */
171 EXPORT_SYMBOL(nfq_tcp_snprintf);