ipv4.c
/*
* (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 <libnetfilter_queue/libnetfilter_queue.h>
#include <libnetfilter_queue/libnetfilter_queue_ipv4.h>
#include <libnetfilter_queue/pktbuff.h>
#include "internal.h"
EXPORT_SYMBOL
struct iphdr *nfq_ip_get_hdr(struct pkt_buff *pktb)
{
struct iphdr *iph;
unsigned int pktlen = pktb_tail(pktb) - pktb->network_header;
/* Not enough room for IPv4 header. */
if (pktlen < sizeof(struct iphdr))
return NULL;
iph = (struct iphdr *)pktb->network_header;
/* Not IPv4 packet. */
if (iph->version != 4)
return NULL;
/* Malformed IPv4 total length field. */
if (ntohs(iph->tot_len) > pktlen)
return NULL;
return iph;
}
EXPORT_SYMBOL
int nfq_ip_set_transport_header(struct pkt_buff *pktb, struct iphdr *iph)
{
int doff = iph->ihl * 4;
/* Wrong offset to IPv4 payload. */
if ((int)pktb->len - doff <= 0)
return -1;
pktb->transport_header = pktb->network_header + doff;
return 0;
}
EXPORT_SYMBOL
void nfq_ip_set_checksum(struct iphdr *iph)
{
uint32_t iph_len = iph->ihl * 4;
iph->check = 0;
iph->check = nfq_checksum(0, (uint16_t *)iph, iph_len);
}
EXPORT_SYMBOL
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)
{
struct iphdr *iph = (struct iphdr *) pktb->network_header;
if (!pktb_mangle(pktb, dataoff, match_offset, match_len, rep_buffer,
rep_len))
return 0;
/* fix IP hdr checksum information */
iph->tot_len = htons(pktb_tail(pktb) - pktb->network_header);
nfq_ip_set_checksum(iph);
return 1;
}
EXPORT_SYMBOL
int nfq_ip_snprintf(char *buf, size_t size, const struct iphdr *iph)
{
int ret;
struct in_addr src = { iph->saddr };
struct in_addr dst = { iph->daddr };
char src_str[INET_ADDRSTRLEN];
char dst_str[INET_ADDRSTRLEN];
ret = snprintf(buf, size, "SRC=%s DST=%s LEN=%u TOS=0x%X "
"PREC=0x%X TTL=%u ID=%u PROTO=%u ",
inet_ntop(AF_INET, &src, src_str, INET_ADDRSTRLEN),
inet_ntop(AF_INET, &dst, dst_str, INET_ADDRSTRLEN),
ntohs(iph->tot_len), IPTOS_TOS(iph->tos),
IPTOS_PREC(iph->tos), iph->ttl, ntohs(iph->id),
iph->protocol);
return ret;
}
nfq_ip_set_checksum
void nfq_ip_set_checksum(struct iphdr *iph)
Definition ipv4.c:102
nfq_ip_get_hdr
struct iphdr * nfq_ip_get_hdr(struct pkt_buff *pktb)
Definition ipv4.c:40
nfq_ip_mangle
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
nfq_ip_snprintf
int nfq_ip_snprintf(char *buf, size_t size, const struct iphdr *iph)
Definition ipv4.c:153
nfq_ip_set_transport_header
int nfq_ip_set_transport_header(struct pkt_buff *pktb, struct iphdr *iph)
Definition ipv4.c:73
pktb_mangle
int pktb_mangle(struct pkt_buff *pktb, int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
Definition pktbuff.c:314