summaryrefslogtreecommitdiffstats
path: root/src/extra/udp.c
blob: 8c44a66e8209927e95b00c203d30bbfabe060e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This code has been sponsored by Vyatta Inc. <http://www.vyatta.com>
 */

#include <stdio.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#define _GNU_SOURCE
#include <netinet/udp.h>

#include <libnetfilter_queue/libnetfilter_queue.h>
#include <libnetfilter_queue/libnetfilter_queue_udp.h>
#include <libnetfilter_queue/libnetfilter_queue_ipv4.h>
#include <libnetfilter_queue/pktbuff.h>

#include "internal.h"

/**
 * \defgroup udp UDP helper functions
 * @{
 */

/**
 * nfq_udp_get_hdr - get the UDP header.
 * \param head: pointer to the beginning of the packet
 * \param tail: pointer to the tail of the packet
 *
 * This function returns NULL if invalid UDP header is found. On success,
 * it returns the UDP header.
 */
struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)
{
	if (pktb->transport_header == NULL)
		return NULL;

	/* No room for the UDP header. */
	if (pktb->tail - pktb->transport_header < sizeof(struct udphdr))
		return NULL;

	return (struct udphdr *)pktb->transport_header;
}
EXPORT_SYMBOL(nfq_udp_get_hdr);

/**
 * nfq_udp_get_payload - get the UDP packet payload.
 * \param udph: the pointer to the UDP header.
 * \param tail: pointer to the tail of the packet
 */
void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
{
	uint16_t len = ntohs(udph->len);

	/* the UDP packet is too short. */
	if (len < sizeof(struct udphdr))
		return NULL;

	/* malformed UDP packet. */
	if (pktb->transport_header + len > pktb->tail)
		return NULL;

	return pktb->transport_header + sizeof(struct udphdr);
}
EXPORT_SYMBOL(nfq_udp_get_payload);

/**
 * nfq_udp_get_payload_len - get the udp packet payload.
 * \param udp: the pointer to the udp header.
 */
unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
{
	return pktb->tail - pktb->transport_header;
}
EXPORT_SYMBOL(nfq_udp_get_payload_len);

/**
 * nfq_udp_set_checksum_ipv4 - computes a IPv4/TCP packet's segment
 * \param iphdrp: pointer to the ip header
 * \param ippayload: payload of the ip packet
 *
 * \returns the checksum of the udp segment.
 *
 * \see nfq_pkt_compute_ip_checksum
 * \see nfq_pkt_compute_udp_checksum
 */
void
nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
{
	/* checksum field in header needs to be zero for calculation. */
	udph->check = 0;
	udph->check = nfq_checksum_tcpudp_ipv4(iph);
}
EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);

/**
 * nfq_udp_set_checksum_ipv6 - computes a IPv6/TCP packet's segment
 * \param iphdrp: pointer to the ip header
 * \param ippayload: payload of the ip packet
 *
 * \returns the checksum of the udp segment.
 *
 * \see nfq_pkt_compute_ip_checksum
 * \see nfq_pkt_compute_udp_checksum
 */
void
nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
{
	/* checksum field in header needs to be zero for calculation. */
	udph->check = 0;
	udph->check = nfq_checksum_tcpudp_ipv6(ip6h, udph);
}
EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);

/**
 * nfq_tcp_mangle_ipv4 - mangle TCP/IPv4 packet buffer
 * \param pktb: pointer to network packet buffer
 * \param match_offset: offset to content that you want to mangle
 * \param match_len: length of the existing content you want to mangle
 * \param rep_buffer: pointer to data you want to use to replace current content 
 * \param rep_len: length of data you want to use to replace current content
 *
 * \note This function recalculates the IPv4 and TCP checksums for you.
 */
int
nfq_udp_mangle_ipv4(struct pkt_buff *pkt,
		    unsigned int match_offset, unsigned int match_len,
		    const char *rep_buffer, unsigned int rep_len)
{
	struct iphdr *iph;
	struct udphdr *udph;

	iph = (struct iphdr *)pkt->network_header;
	udph = (struct udphdr *)(pkt->network_header + iph->ihl*4);

	if (!nfq_ip_mangle(pkt, iph->ihl*4 + sizeof(struct udphdr),
				match_offset, match_len, rep_buffer, rep_len))
		return 0;

	nfq_udp_compute_checksum_ipv4(udph, iph);

	return 1;
}
EXPORT_SYMBOL(nfq_udp_mangle_ipv4);

/**
 * nfq_pkt_snprintf_udp_hdr - print udp header into one buffer in a humnan
 * readable way
 * \param buf: pointer to buffer that is used to print the object
 * \param size: size of the buffer (or remaining room in it).
 * \param udp: pointer to a valid udp header.
 *
 */
int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
{
	return snprintf(buf, size, "SPT=%u DPT=%u ",
			htons(udph->source), htons(udph->dest));
}
EXPORT_SYMBOL(nfq_udp_snprintf);

/**
 * @}
 */