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>
14 #include <arpa/inet.h>
15 #include <netinet/ip.h>
16 #include <netinet/ip6.h>
17 #include <netinet/udp.h>
19 #include <libnetfilter_queue/libnetfilter_queue.h>
20 #include <libnetfilter_queue/libnetfilter_queue_udp.h>
21 #include <libnetfilter_queue/libnetfilter_queue_ipv4.h>
22 #include <libnetfilter_queue/pktbuff.h>
27 * \defgroup udp UDP helper functions
32 * nfq_udp_get_hdr - get the UDP header.
33 * \param head: pointer to the beginning of the packet
34 * \param tail: pointer to the tail of the packet
36 * This function returns NULL if invalid UDP header is found. On success,
37 * it returns the UDP header.
39 struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)
41 if (pktb->transport_header == NULL)
44 /* No room for the UDP header. */
45 if (pktb->tail - pktb->transport_header < sizeof(struct udphdr))
48 return (struct udphdr *)pktb->transport_header;
50 EXPORT_SYMBOL(nfq_udp_get_hdr);
53 * nfq_udp_get_payload - get the UDP packet payload.
54 * \param udph: the pointer to the UDP header.
55 * \param tail: pointer to the tail of the packet
57 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
59 uint16_t len = ntohs(udph->len);
61 /* the UDP packet is too short. */
62 if (len < sizeof(struct udphdr))
65 /* malformed UDP packet. */
66 if (pktb->transport_header + len > pktb->tail)
69 return pktb->transport_header + sizeof(struct udphdr);
71 EXPORT_SYMBOL(nfq_udp_get_payload);
74 * nfq_udp_get_payload_len - get the udp packet payload.
75 * \param udp: the pointer to the udp header.
77 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
79 return pktb->tail - pktb->transport_header;
81 EXPORT_SYMBOL(nfq_udp_get_payload_len);
84 * nfq_udp_set_checksum_ipv4 - computes a IPv4/TCP packet's segment
85 * \param iphdrp: pointer to the ip header
86 * \param ippayload: payload of the ip packet
88 * \returns the checksum of the udp segment.
90 * \see nfq_pkt_compute_ip_checksum
91 * \see nfq_pkt_compute_udp_checksum
94 nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
96 /* checksum field in header needs to be zero for calculation. */
98 udph->check = nfq_checksum_tcpudp_ipv4(iph);
100 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);
103 * nfq_udp_set_checksum_ipv6 - computes a IPv6/TCP packet's segment
104 * \param iphdrp: pointer to the ip header
105 * \param ippayload: payload of the ip packet
107 * \returns the checksum of the udp segment.
109 * \see nfq_pkt_compute_ip_checksum
110 * \see nfq_pkt_compute_udp_checksum
113 nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
115 /* checksum field in header needs to be zero for calculation. */
117 udph->check = nfq_checksum_tcpudp_ipv6(ip6h, udph);
119 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);
122 * nfq_tcp_mangle_ipv4 - mangle TCP/IPv4 packet buffer
123 * \param pktb: pointer to network packet buffer
124 * \param match_offset: offset to content that you want to mangle
125 * \param match_len: length of the existing content you want to mangle
126 * \param rep_buffer: pointer to data you want to use to replace current content
127 * \param rep_len: length of data you want to use to replace current content
129 * \note This function recalculates the IPv4 and TCP checksums for you.
132 nfq_udp_mangle_ipv4(struct pkt_buff *pkt,
133 unsigned int match_offset, unsigned int match_len,
134 const char *rep_buffer, unsigned int rep_len)
139 iph = (struct iphdr *)pkt->network_header;
140 udph = (struct udphdr *)(pkt->network_header + iph->ihl*4);
142 if (!nfq_ip_mangle(pkt, iph->ihl*4 + sizeof(struct udphdr),
143 match_offset, match_len, rep_buffer, rep_len))
146 nfq_udp_compute_checksum_ipv4(udph, iph);
150 EXPORT_SYMBOL(nfq_udp_mangle_ipv4);
153 * nfq_pkt_snprintf_udp_hdr - print udp header into one buffer in a humnan
155 * \param buf: pointer to buffer that is used to print the object
156 * \param size: size of the buffer (or remaining room in it).
157 * \param udp: pointer to a valid udp header.
160 int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
162 return snprintf(buf, size, "SPT=%u DPT=%u ",
163 htons(udph->source), htons(udph->dest));
165 EXPORT_SYMBOL(nfq_udp_snprintf);