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 unsigned int doff = udph->len;
61 /* malformed UDP data offset. */
62 if (pktb->transport_header + doff > pktb->tail)
65 return pktb->transport_header + doff;
67 EXPORT_SYMBOL(nfq_udp_get_payload);
70 * nfq_udp_get_payload_len - get the udp packet payload.
71 * \param udp: the pointer to the udp header.
73 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
75 return pktb->tail - pktb->transport_header;
77 EXPORT_SYMBOL(nfq_udp_get_payload_len);
80 * nfq_udp_set_checksum_ipv4 - computes a IPv4/TCP packet's segment
81 * \param iphdrp: pointer to the ip header
82 * \param ippayload: payload of the ip packet
84 * \returns the checksum of the udp segment.
86 * \see nfq_pkt_compute_ip_checksum
87 * \see nfq_pkt_compute_udp_checksum
90 nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
92 /* checksum field in header needs to be zero for calculation. */
94 udph->check = checksum_tcpudp_ipv4(iph);
96 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);
99 * nfq_udp_set_checksum_ipv6 - computes a IPv6/TCP packet's segment
100 * \param iphdrp: pointer to the ip header
101 * \param ippayload: payload of the ip packet
103 * \returns the checksum of the udp segment.
105 * \see nfq_pkt_compute_ip_checksum
106 * \see nfq_pkt_compute_udp_checksum
109 nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
111 /* checksum field in header needs to be zero for calculation. */
113 udph->check = checksum_tcpudp_ipv6(ip6h, udph);
115 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);
118 nfq_udp_mangle_ipv4(struct pkt_buff *pkt,
119 unsigned int match_offset, unsigned int match_len,
120 const char *rep_buffer, unsigned int rep_len)
125 iph = (struct iphdr *)pkt->network_header;
126 udph = (struct udphdr *)(pkt->network_header + iph->ihl*4);
128 if (!nfq_ip_mangle(pkt, iph->ihl*4 + sizeof(struct udphdr),
129 match_offset, match_len, rep_buffer, rep_len))
132 nfq_udp_compute_checksum_ipv4(udph, iph);
136 EXPORT_SYMBOL(nfq_udp_mangle_ipv4);
139 * nfq_pkt_snprintf_udp_hdr - print udp header into one buffer in a humnan
141 * \param buf: pointer to buffer that is used to print the object
142 * \param size: size of the buffer (or remaining room in it).
143 * \param udp: pointer to a valid udp header.
146 int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
148 return snprintf(buf, size, "SPT=%u DPT=%u ",
149 htons(udph->source), htons(udph->dest));
151 EXPORT_SYMBOL(nfq_udp_snprintf);