6e6baed250d5cc34bedd09efa3eb79470f05ff13
[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         uint16_t len = ntohs(udph->len);
60
61         /* the UDP packet is too short. */
62         if (len < sizeof(struct udphdr))
63                 return NULL;
64
65         /* malformed UDP packet. */
66         if (pktb->transport_header + len > pktb->tail)
67                 return NULL;
68
69         return pktb->transport_header + sizeof(struct udphdr);
70 }
71 EXPORT_SYMBOL(nfq_udp_get_payload);
72
73 /**
74  * nfq_udp_get_payload_len - get the udp packet payload.
75  * \param udp: the pointer to the udp header.
76  */
77 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
78 {
79         return pktb->tail - pktb->transport_header;
80 }
81 EXPORT_SYMBOL(nfq_udp_get_payload_len);
82
83 /**
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
87  *
88  * \returns the checksum of the udp segment.
89  *
90  * \see nfq_pkt_compute_ip_checksum
91  * \see nfq_pkt_compute_udp_checksum
92  */
93 void
94 nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
95 {
96         /* checksum field in header needs to be zero for calculation. */
97         udph->check = 0;
98         udph->check = nfq_checksum_tcpudp_ipv4(iph);
99 }
100 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);
101
102 /**
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
106  *
107  * \returns the checksum of the udp segment.
108  *
109  * \see nfq_pkt_compute_ip_checksum
110  * \see nfq_pkt_compute_udp_checksum
111  */
112 void
113 nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
114 {
115         /* checksum field in header needs to be zero for calculation. */
116         udph->check = 0;
117         udph->check = nfq_checksum_tcpudp_ipv6(ip6h, udph);
118 }
119 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);
120
121 /**
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
128  *
129  * \note This function recalculates the IPv4 and TCP checksums for you.
130  */
131 int
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)
135 {
136         struct iphdr *iph;
137         struct udphdr *udph;
138
139         iph = (struct iphdr *)pkt->network_header;
140         udph = (struct udphdr *)(pkt->network_header + iph->ihl*4);
141
142         if (!nfq_ip_mangle(pkt, iph->ihl*4 + sizeof(struct udphdr),
143                                 match_offset, match_len, rep_buffer, rep_len))
144                 return 0;
145
146         nfq_udp_compute_checksum_ipv4(udph, iph);
147
148         return 1;
149 }
150 EXPORT_SYMBOL(nfq_udp_mangle_ipv4);
151
152 /**
153  * nfq_pkt_snprintf_udp_hdr - print udp header into one buffer in a humnan
154  * readable way
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.
158  *
159  */
160 int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
161 {
162         return snprintf(buf, size, "SPT=%u DPT=%u ",
163                         htons(udph->source), htons(udph->dest));
164 }
165 EXPORT_SYMBOL(nfq_udp_snprintf);
166
167 /**
168  * @}
169  */