libnetfilter_queue  1.0.5
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 #define _GNU_SOURCE
18 #include <netinet/udp.h>
19 
20 #include <libnetfilter_queue/libnetfilter_queue.h>
21 #include <libnetfilter_queue/libnetfilter_queue_udp.h>
22 #include <libnetfilter_queue/libnetfilter_queue_ipv4.h>
23 #include <libnetfilter_queue/libnetfilter_queue_ipv6.h>
24 #include <libnetfilter_queue/pktbuff.h>
25 
26 #include "internal.h"
27 
42 EXPORT_SYMBOL
43 struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)
44 {
45  if (pktb->transport_header == NULL)
46  return NULL;
47 
48  /* No room for the UDP header. */
49  if (pktb_tail(pktb) - pktb->transport_header < sizeof(struct udphdr))
50  return NULL;
51 
52  return (struct udphdr *)pktb->transport_header;
53 }
54 
61 EXPORT_SYMBOL
62 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
63 {
64  uint16_t len = ntohs(udph->len);
65 
66  /* the UDP packet is too short. */
67  if (len < sizeof(struct udphdr))
68  return NULL;
69 
70  /* malformed UDP packet. */
71  if (pktb->transport_header + len > pktb_tail(pktb))
72  return NULL;
73 
74  return pktb->transport_header + sizeof(struct udphdr);
75 }
76 
83 EXPORT_SYMBOL
84 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
85 {
86  return pktb_tail(pktb) - pktb->transport_header - sizeof(struct udphdr);
87 }
88 
107 EXPORT_SYMBOL
108 void nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
109 {
110  /* checksum field in header needs to be zero for calculation. */
111  udph->check = 0;
112  udph->check = nfq_checksum_tcpudp_ipv4(iph, IPPROTO_UDP);
113 }
114 
125 EXPORT_SYMBOL
126 void nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
127 {
128  /* checksum field in header needs to be zero for calculation. */
129  udph->check = 0;
130  udph->check = nfq_checksum_tcpudp_ipv6(ip6h, udph, IPPROTO_UDP);
131 }
132 
149 EXPORT_SYMBOL
150 int nfq_udp_mangle_ipv4(struct pkt_buff *pktb,
151  unsigned int match_offset, unsigned int match_len,
152  const char *rep_buffer, unsigned int rep_len)
153 {
154  struct iphdr *iph;
155  struct udphdr *udph;
156 
157  iph = (struct iphdr *)pktb->network_header;
158  udph = (struct udphdr *)(pktb->network_header + iph->ihl*4);
159 
160  udph->len = htons(ntohs(udph->len) + rep_len - match_len);
161 
162  if (!nfq_ip_mangle(pktb, iph->ihl*4 + sizeof(struct udphdr),
163  match_offset, match_len, rep_buffer, rep_len))
164  return 0;
165 
167 
168  return 1;
169 }
170 
183 EXPORT_SYMBOL
184 int nfq_udp_mangle_ipv6(struct pkt_buff *pktb,
185  unsigned int match_offset, unsigned int match_len,
186  const char *rep_buffer, unsigned int rep_len)
187 {
188  struct ip6_hdr *ip6h;
189  struct udphdr *udph;
190 
191  ip6h = (struct ip6_hdr *)pktb->network_header;
192  udph = (struct udphdr *)(pktb->transport_header);
193  if (!udph)
194  return 0;
195 
196  udph->len = htons(ntohs(udph->len) + rep_len - match_len);
197 
198  if (!nfq_ip6_mangle(pktb,
199  pktb->transport_header - pktb->network_header +
200  sizeof(struct udphdr),
201  match_offset, match_len, rep_buffer, rep_len))
202  return 0;
203 
204  nfq_udp_compute_checksum_ipv6(udph, ip6h);
205 
206  return 1;
207 }
208 
219 EXPORT_SYMBOL
220 int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
221 {
222  return snprintf(buf, size, "SPT=%u DPT=%u ",
223  htons(udph->source), htons(udph->dest));
224 }
225 
int nfq_ip6_mangle(struct pkt_buff *pktb, unsigned int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
Definition: ipv6.c:131
void nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
Definition: udp.c:108
unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
Definition: udp.c:84
int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
Definition: udp.c:220
void nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
Definition: udp.c:126
int nfq_udp_mangle_ipv6(struct pkt_buff *pktb, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
Definition: udp.c:184
int nfq_udp_mangle_ipv4(struct pkt_buff *pktb, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
Definition: udp.c:150
void * nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
Definition: udp.c:62
struct udphdr * nfq_udp_get_hdr(struct pkt_buff *pktb)
Definition: udp.c:43
int nfq_ip_mangle(struct pkt_buff *pktb, unsigned int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
Definition: ipv4.c:127