add mangle functions for IPv4/TCP and IPv4/UDP
[platform/upstream/libnetfilter_queue.git] / src / extra / udp.c
1 /*
2  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
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.
8  *
9  * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
10  */
11
12 #include <stdio.h>
13 #include <stdbool.h>
14 #include <arpa/inet.h>
15 #include <netinet/ip.h>
16 #include <netinet/ip6.h>
17 #include <netinet/udp.h>
18
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>
23
24 #include "internal.h"
25
26 /**
27  * \defgroup udp UDP helper functions
28  * @{
29  */
30
31 /**
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
35  *
36  * This function returns NULL if invalid UDP header is found. On success,
37  * it returns the UDP header.
38  */
39 struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)
40 {
41         if (pktb->transport_header == NULL)
42                 return NULL;
43
44         /* No room for the UDP header. */
45         if (pktb->tail - pktb->transport_header < sizeof(struct udphdr))
46                 return NULL;
47
48         return (struct udphdr *)pktb->transport_header;
49 }
50 EXPORT_SYMBOL(nfq_udp_get_hdr);
51
52 /**
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
56  */
57 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
58 {
59         unsigned int doff = udph->len;
60
61         /* malformed UDP data offset. */
62         if (pktb->transport_header + doff > pktb->tail)
63                 return NULL;
64
65         return pktb->transport_header + doff;
66 }
67 EXPORT_SYMBOL(nfq_udp_get_payload);
68
69 /**
70  * nfq_udp_get_payload_len - get the udp packet payload.
71  * \param udp: the pointer to the udp header.
72  */
73 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
74 {
75         return pktb->tail - pktb->transport_header;
76 }
77 EXPORT_SYMBOL(nfq_udp_get_payload_len);
78
79 /**
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
83  *
84  * \returns the checksum of the udp segment.
85  *
86  * \see nfq_pkt_compute_ip_checksum
87  * \see nfq_pkt_compute_udp_checksum
88  */
89 void
90 nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
91 {
92         /* checksum field in header needs to be zero for calculation. */
93         udph->check = 0;
94         udph->check = checksum_tcpudp_ipv4(iph);
95 }
96 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);
97
98 /**
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
102  *
103  * \returns the checksum of the udp segment.
104  *
105  * \see nfq_pkt_compute_ip_checksum
106  * \see nfq_pkt_compute_udp_checksum
107  */
108 void
109 nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
110 {
111         /* checksum field in header needs to be zero for calculation. */
112         udph->check = 0;
113         udph->check = checksum_tcpudp_ipv6(ip6h, udph);
114 }
115 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);
116
117 int
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)
121 {
122         struct iphdr *iph;
123         struct udphdr *udph;
124
125         iph = (struct iphdr *)pkt->network_header;
126         udph = (struct udphdr *)(pkt->network_header + iph->ihl*4);
127
128         if (!nfq_ip_mangle(pkt, iph->ihl*4 + sizeof(struct udphdr),
129                                 match_offset, match_len, rep_buffer, rep_len))
130                 return 0;
131
132         nfq_udp_compute_checksum_ipv4(udph, iph);
133
134         return 1;
135 }
136 EXPORT_SYMBOL(nfq_udp_mangle_ipv4);
137
138 /**
139  * nfq_pkt_snprintf_udp_hdr - print udp header into one buffer in a humnan
140  * readable way
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.
144  *
145  */
146 int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
147 {
148         return snprintf(buf, size, "SPT=%u DPT=%u ",
149                         htons(udph->source), htons(udph->dest));
150 }
151 EXPORT_SYMBOL(nfq_udp_snprintf);
152
153 /**
154  * @}
155  */