📄 ip_nat_helper.c
字号:
/* ip_nat_mangle.c - generic support functions for NAT helpers * * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org> * * distributed under the terms of GNU GPL * * 14 Jan 2002 Harald Welte <laforge@gnumonks.org>: * - add support for SACK adjustment * 14 Mar 2002 Harald Welte <laforge@gnumonks.org>: * - merge SACK support into newnat API * 16 Aug 2002 Brian J. Murrell <netfilter@interlinx.bc.ca>: * - make ip_nat_resize_packet more generic (TCP and UDP) * - add ip_nat_mangle_udp_packet */#include <linux/version.h>#include <linux/config.h>#include <linux/module.h>#include <linux/kmod.h>#include <linux/types.h>#include <linux/timer.h>#include <linux/skbuff.h>#include <linux/netfilter_ipv4.h>#include <linux/brlock.h>#include <net/checksum.h>#include <net/icmp.h>#include <net/ip.h>#include <net/tcp.h>#include <net/udp.h>#define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)#define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)#include <linux/netfilter_ipv4/ip_conntrack.h>#include <linux/netfilter_ipv4/ip_conntrack_helper.h>#include <linux/netfilter_ipv4/ip_nat.h>#include <linux/netfilter_ipv4/ip_nat_protocol.h>#include <linux/netfilter_ipv4/ip_nat_core.h>#include <linux/netfilter_ipv4/ip_nat_helper.h>#include <linux/netfilter_ipv4/listhelp.h>#if 0#define DEBUGP printk#define DUMP_OFFSET(x) printk("offset_before=%d, offset_after=%d, correction_pos=%u\n", x->offset_before, x->offset_after, x->correction_pos);#else#define DEBUGP(format, args...)#define DUMP_OFFSET(x)#endifDECLARE_LOCK(ip_nat_seqofs_lock); static inline int ip_nat_resize_packet(struct sk_buff **skb, struct ip_conntrack *ct, enum ip_conntrack_info ctinfo, int new_size){ struct iphdr *iph; int dir; struct ip_nat_seq *this_way, *other_way; DEBUGP("ip_nat_resize_packet: old_size = %u, new_size = %u\n", (*skb)->len, new_size); dir = CTINFO2DIR(ctinfo); this_way = &ct->nat.info.seq[dir]; other_way = &ct->nat.info.seq[!dir]; if (new_size > (*skb)->len + skb_tailroom(*skb)) { struct sk_buff *newskb; newskb = skb_copy_expand(*skb, skb_headroom(*skb), new_size - (*skb)->len, GFP_ATOMIC); if (!newskb) { printk("ip_nat_resize_packet: oom\n"); return 0; } else { kfree_skb(*skb); *skb = newskb; } } iph = (*skb)->nh.iph; if (iph->protocol == IPPROTO_TCP) { struct tcphdr *tcph = (void *)iph + iph->ihl*4; void *data = (void *)tcph + tcph->doff*4; DEBUGP("ip_nat_resize_packet: Seq_offset before: "); DUMP_OFFSET(this_way); LOCK_BH(&ip_nat_seqofs_lock); /* SYN adjust. If it's uninitialized, of this is after last * correction, record it: we don't handle more than one * adjustment in the window, but do deal with common case of a * retransmit */ if (this_way->offset_before == this_way->offset_after || before(this_way->correction_pos, ntohl(tcph->seq))) { this_way->correction_pos = ntohl(tcph->seq); this_way->offset_before = this_way->offset_after; this_way->offset_after = (int32_t) this_way->offset_before + new_size - (*skb)->len; } UNLOCK_BH(&ip_nat_seqofs_lock); DEBUGP("ip_nat_resize_packet: Seq_offset after: "); DUMP_OFFSET(this_way); } return 1;}/* Generic function for mangling variable-length address changes inside * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX * command in FTP). * * Takes care about all the nasty sequence number changes, checksumming, * skb enlargement, ... * * */int ip_nat_mangle_tcp_packet(struct sk_buff **skb, struct ip_conntrack *ct, enum ip_conntrack_info ctinfo, unsigned int match_offset, unsigned int match_len, char *rep_buffer, unsigned int rep_len){ struct iphdr *iph = (*skb)->nh.iph; struct tcphdr *tcph; unsigned char *data; u_int32_t tcplen, newlen, newtcplen; tcplen = (*skb)->len - iph->ihl*4; newtcplen = tcplen - match_len + rep_len; newlen = iph->ihl*4 + newtcplen; if (newlen > 65535) { if (net_ratelimit()) printk("ip_nat_mangle_tcp_packet: nat'ed packet " "exceeds maximum packet size\n"); return 0; } if ((*skb)->len != newlen) { if (!ip_nat_resize_packet(skb, ct, ctinfo, newlen)) { printk("resize_packet failed!!\n"); return 0; } } /* Alexey says: if a hook changes _data_ ... it can break original packet sitting in tcp queue and this is fatal */ if (skb_cloned(*skb)) { struct sk_buff *nskb = skb_copy(*skb, GFP_ATOMIC); if (!nskb) { if (net_ratelimit()) printk("Out of memory cloning TCP packet\n"); return 0; } /* Rest of kernel will get very unhappy if we pass it a suddenly-orphaned skbuff */ if ((*skb)->sk) skb_set_owner_w(nskb, (*skb)->sk); kfree_skb(*skb); *skb = nskb; } /* skb may be copied !! */ iph = (*skb)->nh.iph; tcph = (void *)iph + iph->ihl*4; data = (void *)tcph + tcph->doff*4; if (rep_len != match_len) /* move post-replacement */ memmove(data + match_offset + rep_len, data + match_offset + match_len, (*skb)->tail - (data + match_offset + match_len)); /* insert data from buffer */ memcpy(data + match_offset, rep_buffer, rep_len); /* update skb info */ if (newlen > (*skb)->len) { DEBUGP("ip_nat_mangle_tcp_packet: Extending packet by " "%u to %u bytes\n", newlen - (*skb)->len, newlen); skb_put(*skb, newlen - (*skb)->len); } else { DEBUGP("ip_nat_mangle_tcp_packet: Shrinking packet from " "%u to %u bytes\n", (*skb)->len, newlen); skb_trim(*skb, newlen); } /* fix checksum information */ iph->tot_len = htons(newlen); (*skb)->csum = csum_partial((char *)tcph + tcph->doff*4, newtcplen - tcph->doff*4, 0); tcph->check = 0; tcph->check = tcp_v4_check(tcph, newtcplen, iph->saddr, iph->daddr, csum_partial((char *)tcph, tcph->doff*4, (*skb)->csum)); ip_send_check(iph); return 1;} /* Generic function for mangling variable-length address changes inside * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX * command in the Amanda protocol) * * Takes care about all the nasty sequence number changes, checksumming, * skb enlargement, ... * * XXX - This function could be merged with ip_nat_mangle_tcp_packet which * should be fairly easy to do. */int ip_nat_mangle_udp_packet(struct sk_buff **skb, struct ip_conntrack *ct, enum ip_conntrack_info ctinfo, unsigned int match_offset, unsigned int match_len, char *rep_buffer, unsigned int rep_len){ struct iphdr *iph = (*skb)->nh.iph; struct udphdr *udph = (void *)iph + iph->ihl * 4; unsigned char *data; u_int32_t udplen, newlen, newudplen; udplen = (*skb)->len - iph->ihl*4; newudplen = udplen - match_len + rep_len; newlen = iph->ihl*4 + newudplen; if (newlen > 65535) { if (net_ratelimit()) printk("ip_nat_mangle_udp_packet: nat'ed packet " "exceeds maximum packet size\n"); return 0; } if ((*skb)->len != newlen) { if (!ip_nat_resize_packet(skb, ct, ctinfo, newlen)) { printk("resize_packet failed!!\n"); return 0; } } /* Alexey says: if a hook changes _data_ ... it can break original packet sitting in tcp queue and this is fatal */ if (skb_cloned(*skb)) { struct sk_buff *nskb = skb_copy(*skb, GFP_ATOMIC); if (!nskb) { if (net_ratelimit()) printk("Out of memory cloning TCP packet\n"); return 0; } /* Rest of kernel will get very unhappy if we pass it a suddenly-orphaned skbuff */ if ((*skb)->sk) skb_set_owner_w(nskb, (*skb)->sk); kfree_skb(*skb); *skb = nskb; } /* skb may be copied !! */ iph = (*skb)->nh.iph; udph = (void *)iph + iph->ihl*4; data = (void *)udph + sizeof(struct udphdr); if (rep_len != match_len) /* move post-replacement */ memmove(data + match_offset + rep_len, data + match_offset + match_len, (*skb)->tail - (data + match_offset + match_len)); /* insert data from buffer */ memcpy(data + match_offset, rep_buffer, rep_len); /* update skb info */ if (newlen > (*skb)->len) { DEBUGP("ip_nat_mangle_udp_packet: Extending packet by " "%u to %u bytes\n", newlen - (*skb)->len, newlen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -