Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* IRC extension for TCP NAT alteration. |
| 2 | * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2004 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation |
| 4 | * based on a copy of RR's ip_nat_ftp.c |
| 5 | * |
| 6 | * ip_nat_irc.c,v 1.16 2001/12/06 07:42:10 laforge Exp |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/netfilter_ipv4.h> |
| 16 | #include <linux/ip.h> |
| 17 | #include <linux/tcp.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <net/tcp.h> |
| 20 | #include <linux/netfilter_ipv4/ip_nat.h> |
| 21 | #include <linux/netfilter_ipv4/ip_nat_helper.h> |
| 22 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| 23 | #include <linux/netfilter_ipv4/ip_conntrack_irc.h> |
| 24 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | |
| 27 | #if 0 |
| 28 | #define DEBUGP printk |
| 29 | #else |
| 30 | #define DEBUGP(format, args...) |
| 31 | #endif |
| 32 | |
| 33 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); |
| 34 | MODULE_DESCRIPTION("IRC (DCC) NAT helper"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
| 37 | static unsigned int help(struct sk_buff **pskb, |
| 38 | enum ip_conntrack_info ctinfo, |
| 39 | unsigned int matchoff, |
| 40 | unsigned int matchlen, |
| 41 | struct ip_conntrack_expect *exp) |
| 42 | { |
| 43 | u_int16_t port; |
| 44 | unsigned int ret; |
| 45 | |
| 46 | /* "4294967296 65635 " */ |
| 47 | char buffer[18]; |
| 48 | |
| 49 | DEBUGP("IRC_NAT: info (seq %u + %u) in %u\n", |
| 50 | expect->seq, exp_irc_info->len, |
| 51 | ntohl(tcph->seq)); |
| 52 | |
| 53 | /* Reply comes from server. */ |
| 54 | exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port; |
| 55 | exp->dir = IP_CT_DIR_REPLY; |
| 56 | |
| 57 | /* When you see the packet, we need to NAT it the same as the |
| 58 | * this one. */ |
| 59 | exp->expectfn = ip_nat_follow_master; |
| 60 | |
| 61 | /* Try to get same port: if not, try to change it. */ |
| 62 | for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) { |
| 63 | exp->tuple.dst.u.tcp.port = htons(port); |
| 64 | if (ip_conntrack_expect_related(exp) == 0) |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | if (port == 0) { |
| 69 | ip_conntrack_expect_free(exp); |
| 70 | return NF_DROP; |
| 71 | } |
| 72 | |
| 73 | /* strlen("\1DCC CHAT chat AAAAAAAA P\1\n")=27 |
| 74 | * strlen("\1DCC SCHAT chat AAAAAAAA P\1\n")=28 |
| 75 | * strlen("\1DCC SEND F AAAAAAAA P S\1\n")=26 |
| 76 | * strlen("\1DCC MOVE F AAAAAAAA P S\1\n")=26 |
| 77 | * strlen("\1DCC TSEND F AAAAAAAA P S\1\n")=27 |
| 78 | * AAAAAAAAA: bound addr (1.0.0.0==16777216, min 8 digits, |
| 79 | * 255.255.255.255==4294967296, 10 digits) |
| 80 | * P: bound port (min 1 d, max 5d (65635)) |
| 81 | * F: filename (min 1 d ) |
| 82 | * S: size (min 1 d ) |
| 83 | * 0x01, \n: terminators |
| 84 | */ |
| 85 | |
| 86 | /* AAA = "us", ie. where server normally talks to. */ |
| 87 | sprintf(buffer, "%u %u", |
| 88 | ntohl(exp->master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip), |
| 89 | port); |
| 90 | DEBUGP("ip_nat_irc: Inserting '%s' == %u.%u.%u.%u, port %u\n", |
| 91 | buffer, NIPQUAD(exp->tuple.src.ip), port); |
| 92 | |
| 93 | ret = ip_nat_mangle_tcp_packet(pskb, exp->master, ctinfo, |
| 94 | matchoff, matchlen, buffer, |
| 95 | strlen(buffer)); |
| 96 | if (ret != NF_ACCEPT) |
| 97 | ip_conntrack_unexpect_related(exp); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static void __exit fini(void) |
| 102 | { |
| 103 | ip_nat_irc_hook = NULL; |
| 104 | /* Make sure noone calls it, meanwhile. */ |
| 105 | synchronize_net(); |
| 106 | } |
| 107 | |
| 108 | static int __init init(void) |
| 109 | { |
| 110 | BUG_ON(ip_nat_irc_hook); |
| 111 | ip_nat_irc_hook = help; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */ |
| 116 | static int warn_set(const char *val, struct kernel_param *kp) |
| 117 | { |
| 118 | printk(KERN_INFO __stringify(KBUILD_MODNAME) |
| 119 | ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n"); |
| 120 | return 0; |
| 121 | } |
| 122 | module_param_call(ports, warn_set, NULL, NULL, 0); |
| 123 | |
| 124 | module_init(init); |
| 125 | module_exit(fini); |