From f40eabb01163f383e2471942da45f32361031e39 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 14 May 2012 13:14:14 +0200 Subject: add pkt_buff and protocol helper functions Signed-off-by: Pablo Neira Ayuso --- src/extra/udp.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/extra/udp.c (limited to 'src/extra/udp.c') diff --git a/src/extra/udp.c b/src/extra/udp.c new file mode 100644 index 0000000..c0b1b7d --- /dev/null +++ b/src/extra/udp.c @@ -0,0 +1,132 @@ +/* + * (C) 2012 by Pablo Neira Ayuso + * + * 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. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#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) +{ + unsigned int doff = udph->len; + + /* malformed UDP data offset. */ + if (pktb->transport_header + doff > pktb->tail) + return NULL; + + return pktb->transport_header + doff; +} +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 = 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 = checksum_tcpudp_ipv6(ip6h, udph); +} +EXPORT_SYMBOL(nfq_udp_compute_checksum_ipv6); + +/** + * 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); + +/** + * @} + */ -- cgit v1.2.3