libnetfilter_queue  1.0.3
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/pktbuff.h>
24 
25 #include "internal.h"
26 
40 struct udphdr *nfq_udp_get_hdr(struct pkt_buff *pktb)
41 {
42  if (pktb->transport_header == NULL)
43  return NULL;
44 
45  /* No room for the UDP header. */
46  if (pktb->tail - pktb->transport_header < sizeof(struct udphdr))
47  return NULL;
48 
49  return (struct udphdr *)pktb->transport_header;
50 }
51 EXPORT_SYMBOL(nfq_udp_get_hdr);
52 
58 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
59 {
60  uint16_t len = ntohs(udph->len);
61 
62  /* the UDP packet is too short. */
63  if (len < sizeof(struct udphdr))
64  return NULL;
65 
66  /* malformed UDP packet. */
67  if (pktb->transport_header + len > pktb->tail)
68  return NULL;
69 
70  return pktb->transport_header + sizeof(struct udphdr);
71 }
72 EXPORT_SYMBOL(nfq_udp_get_payload);
73 
78 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
79 {
80  return pktb->tail - pktb->transport_header;
81 }
82 EXPORT_SYMBOL(nfq_udp_get_payload_len);
83 
94 void
95 nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
96 {
97  /* checksum field in header needs to be zero for calculation. */
98  udph->check = 0;
99  udph->check = nfq_checksum_tcpudp_ipv4(iph);
100 }
101 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv4);
102 
113 void
114 nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
115 {
116  /* checksum field in header needs to be zero for calculation. */
117  udph->check = 0;
118  udph->check = nfq_checksum_tcpudp_ipv6(ip6h, udph);
119 }
120 EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6);
121 
132 int
133 nfq_udp_mangle_ipv4(struct pkt_buff *pkt,
134  unsigned int match_offset, unsigned int match_len,
135  const char *rep_buffer, unsigned int rep_len)
136 {
137  struct iphdr *iph;
138  struct udphdr *udph;
139 
140  iph = (struct iphdr *)pkt->network_header;
141  udph = (struct udphdr *)(pkt->network_header + iph->ihl*4);
142 
143  if (!nfq_ip_mangle(pkt, iph->ihl*4 + sizeof(struct udphdr),
144  match_offset, match_len, rep_buffer, rep_len))
145  return 0;
146 
148 
149  return 1;
150 }
151 EXPORT_SYMBOL(nfq_udp_mangle_ipv4);
152 
161 int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
162 {
163  return snprintf(buf, size, "SPT=%u DPT=%u ",
164  htons(udph->source), htons(udph->dest));
165 }
166 EXPORT_SYMBOL(nfq_udp_snprintf);
167 
void nfq_udp_compute_checksum_ipv6(struct udphdr *udph, struct ip6_hdr *ip6h)
Definition: udp.c:114
unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
Definition: udp.c:78
int nfq_udp_snprintf(char *buf, size_t size, const struct udphdr *udph)
Definition: udp.c:161
void nfq_udp_compute_checksum_ipv4(struct udphdr *udph, struct iphdr *iph)
Definition: udp.c:95
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)
Definition: udp.c:133
void * nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
Definition: udp.c:58
int nfq_ip_mangle(struct pkt_buff *pkt, unsigned int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
Definition: ipv4.c:103
struct udphdr * nfq_udp_get_hdr(struct pkt_buff *pktb)
Definition: udp.c:40