nf_conntrack_irc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* IRC extension for IP connection tracking, Version 1.21
  2. * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
  3. * based on RR's ip_conntrack_ftp.c
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/in.h>
  14. #include <linux/tcp.h>
  15. #include <linux/netfilter.h>
  16. #include <net/netfilter/nf_conntrack.h>
  17. #include <net/netfilter/nf_conntrack_expect.h>
  18. #include <net/netfilter/nf_conntrack_helper.h>
  19. #include <linux/netfilter/nf_conntrack_irc.h>
  20. #define MAX_PORTS 8
  21. static unsigned short ports[MAX_PORTS];
  22. static int ports_c;
  23. static unsigned int max_dcc_channels = 8;
  24. static unsigned int dcc_timeout __read_mostly = 300;
  25. /* This is slow, but it's simple. --RR */
  26. static char *irc_buffer;
  27. static DEFINE_SPINLOCK(irc_buffer_lock);
  28. unsigned int (*nf_nat_irc_hook)(struct sk_buff **pskb,
  29. enum ip_conntrack_info ctinfo,
  30. unsigned int matchoff,
  31. unsigned int matchlen,
  32. struct nf_conntrack_expect *exp) __read_mostly;
  33. EXPORT_SYMBOL_GPL(nf_nat_irc_hook);
  34. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  35. MODULE_DESCRIPTION("IRC (DCC) connection tracking helper");
  36. MODULE_LICENSE("GPL");
  37. MODULE_ALIAS("ip_conntrack_irc");
  38. module_param_array(ports, ushort, &ports_c, 0400);
  39. MODULE_PARM_DESC(ports, "port numbers of IRC servers");
  40. module_param(max_dcc_channels, uint, 0400);
  41. MODULE_PARM_DESC(max_dcc_channels, "max number of expected DCC channels per "
  42. "IRC session");
  43. module_param(dcc_timeout, uint, 0400);
  44. MODULE_PARM_DESC(dcc_timeout, "timeout on for unestablished DCC channels");
  45. static const char *dccprotos[] = {
  46. "SEND ", "CHAT ", "MOVE ", "TSEND ", "SCHAT "
  47. };
  48. #define MINMATCHLEN 5
  49. #if 0
  50. #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s:" format, \
  51. __FILE__, __FUNCTION__ , ## args)
  52. #else
  53. #define DEBUGP(format, args...)
  54. #endif
  55. /* tries to get the ip_addr and port out of a dcc command
  56. * return value: -1 on failure, 0 on success
  57. * data pointer to first byte of DCC command data
  58. * data_end pointer to last byte of dcc command data
  59. * ip returns parsed ip of dcc command
  60. * port returns parsed port of dcc command
  61. * ad_beg_p returns pointer to first byte of addr data
  62. * ad_end_p returns pointer to last byte of addr data
  63. */
  64. static int parse_dcc(char *data, char *data_end, u_int32_t *ip,
  65. u_int16_t *port, char **ad_beg_p, char **ad_end_p)
  66. {
  67. /* at least 12: "AAAAAAAA P\1\n" */
  68. while (*data++ != ' ')
  69. if (data > data_end - 12)
  70. return -1;
  71. *ad_beg_p = data;
  72. *ip = simple_strtoul(data, &data, 10);
  73. /* skip blanks between ip and port */
  74. while (*data == ' ') {
  75. if (data >= data_end)
  76. return -1;
  77. data++;
  78. }
  79. *port = simple_strtoul(data, &data, 10);
  80. *ad_end_p = data;
  81. return 0;
  82. }
  83. static int help(struct sk_buff **pskb, unsigned int protoff,
  84. struct nf_conn *ct, enum ip_conntrack_info ctinfo)
  85. {
  86. unsigned int dataoff;
  87. struct tcphdr _tcph, *th;
  88. char *data, *data_limit, *ib_ptr;
  89. int dir = CTINFO2DIR(ctinfo);
  90. struct nf_conntrack_expect *exp;
  91. struct nf_conntrack_tuple *tuple;
  92. u_int32_t dcc_ip;
  93. u_int16_t dcc_port;
  94. __be16 port;
  95. int i, ret = NF_ACCEPT;
  96. char *addr_beg_p, *addr_end_p;
  97. typeof(nf_nat_irc_hook) nf_nat_irc;
  98. /* If packet is coming from IRC server */
  99. if (dir == IP_CT_DIR_REPLY)
  100. return NF_ACCEPT;
  101. /* Until there's been traffic both ways, don't look in packets. */
  102. if (ctinfo != IP_CT_ESTABLISHED &&
  103. ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
  104. return NF_ACCEPT;
  105. /* Not a full tcp header? */
  106. th = skb_header_pointer(*pskb, protoff, sizeof(_tcph), &_tcph);
  107. if (th == NULL)
  108. return NF_ACCEPT;
  109. /* No data? */
  110. dataoff = protoff + th->doff*4;
  111. if (dataoff >= (*pskb)->len)
  112. return NF_ACCEPT;
  113. spin_lock_bh(&irc_buffer_lock);
  114. ib_ptr = skb_header_pointer(*pskb, dataoff, (*pskb)->len - dataoff,
  115. irc_buffer);
  116. BUG_ON(ib_ptr == NULL);
  117. data = ib_ptr;
  118. data_limit = ib_ptr + (*pskb)->len - dataoff;
  119. /* strlen("\1DCC SENT t AAAAAAAA P\1\n")=24
  120. * 5+MINMATCHLEN+strlen("t AAAAAAAA P\1\n")=14 */
  121. while (data < data_limit - (19 + MINMATCHLEN)) {
  122. if (memcmp(data, "\1DCC ", 5)) {
  123. data++;
  124. continue;
  125. }
  126. data += 5;
  127. /* we have at least (19+MINMATCHLEN)-5 bytes valid data left */
  128. DEBUGP("DCC found in master %u.%u.%u.%u:%u %u.%u.%u.%u:%u...\n",
  129. NIPQUAD(iph->saddr), ntohs(th->source),
  130. NIPQUAD(iph->daddr), ntohs(th->dest));
  131. for (i = 0; i < ARRAY_SIZE(dccprotos); i++) {
  132. if (memcmp(data, dccprotos[i], strlen(dccprotos[i]))) {
  133. /* no match */
  134. continue;
  135. }
  136. data += strlen(dccprotos[i]);
  137. DEBUGP("DCC %s detected\n", dccprotos[i]);
  138. /* we have at least
  139. * (19+MINMATCHLEN)-5-dccprotos[i].matchlen bytes valid
  140. * data left (== 14/13 bytes) */
  141. if (parse_dcc((char *)data, data_limit, &dcc_ip,
  142. &dcc_port, &addr_beg_p, &addr_end_p)) {
  143. DEBUGP("unable to parse dcc command\n");
  144. continue;
  145. }
  146. DEBUGP("DCC bound ip/port: %u.%u.%u.%u:%u\n",
  147. HIPQUAD(dcc_ip), dcc_port);
  148. /* dcc_ip can be the internal OR external (NAT'ed) IP */
  149. tuple = &ct->tuplehash[dir].tuple;
  150. if (tuple->src.u3.ip != htonl(dcc_ip) &&
  151. tuple->dst.u3.ip != htonl(dcc_ip)) {
  152. if (net_ratelimit())
  153. printk(KERN_WARNING
  154. "Forged DCC command from "
  155. "%u.%u.%u.%u: %u.%u.%u.%u:%u\n",
  156. NIPQUAD(tuple->src.u3.ip),
  157. HIPQUAD(dcc_ip), dcc_port);
  158. continue;
  159. }
  160. exp = nf_conntrack_expect_alloc(ct);
  161. if (exp == NULL) {
  162. ret = NF_DROP;
  163. goto out;
  164. }
  165. tuple = &ct->tuplehash[!dir].tuple;
  166. port = htons(dcc_port);
  167. nf_conntrack_expect_init(exp, tuple->src.l3num,
  168. NULL, &tuple->dst.u3,
  169. IPPROTO_TCP, NULL, &port);
  170. nf_nat_irc = rcu_dereference(nf_nat_irc_hook);
  171. if (nf_nat_irc && ct->status & IPS_NAT_MASK)
  172. ret = nf_nat_irc(pskb, ctinfo,
  173. addr_beg_p - ib_ptr,
  174. addr_end_p - addr_beg_p,
  175. exp);
  176. else if (nf_conntrack_expect_related(exp) != 0)
  177. ret = NF_DROP;
  178. nf_conntrack_expect_put(exp);
  179. goto out;
  180. }
  181. }
  182. out:
  183. spin_unlock_bh(&irc_buffer_lock);
  184. return ret;
  185. }
  186. static struct nf_conntrack_helper irc[MAX_PORTS] __read_mostly;
  187. static char irc_names[MAX_PORTS][sizeof("irc-65535")] __read_mostly;
  188. static void nf_conntrack_irc_fini(void);
  189. static int __init nf_conntrack_irc_init(void)
  190. {
  191. int i, ret;
  192. char *tmpname;
  193. if (max_dcc_channels < 1) {
  194. printk("nf_ct_irc: max_dcc_channels must not be zero\n");
  195. return -EINVAL;
  196. }
  197. irc_buffer = kmalloc(65536, GFP_KERNEL);
  198. if (!irc_buffer)
  199. return -ENOMEM;
  200. /* If no port given, default to standard irc port */
  201. if (ports_c == 0)
  202. ports[ports_c++] = IRC_PORT;
  203. for (i = 0; i < ports_c; i++) {
  204. irc[i].tuple.src.l3num = AF_INET;
  205. irc[i].tuple.src.u.tcp.port = htons(ports[i]);
  206. irc[i].tuple.dst.protonum = IPPROTO_TCP;
  207. irc[i].mask.src.l3num = 0xFFFF;
  208. irc[i].mask.src.u.tcp.port = htons(0xFFFF);
  209. irc[i].mask.dst.protonum = 0xFF;
  210. irc[i].max_expected = max_dcc_channels;
  211. irc[i].timeout = dcc_timeout;
  212. irc[i].me = THIS_MODULE;
  213. irc[i].help = help;
  214. tmpname = &irc_names[i][0];
  215. if (ports[i] == IRC_PORT)
  216. sprintf(tmpname, "irc");
  217. else
  218. sprintf(tmpname, "irc-%u", i);
  219. irc[i].name = tmpname;
  220. ret = nf_conntrack_helper_register(&irc[i]);
  221. if (ret) {
  222. printk("nf_ct_irc: failed to register helper "
  223. "for pf: %u port: %u\n",
  224. irc[i].tuple.src.l3num, ports[i]);
  225. nf_conntrack_irc_fini();
  226. return ret;
  227. }
  228. }
  229. return 0;
  230. }
  231. /* This function is intentionally _NOT_ defined as __exit, because
  232. * it is needed by the init function */
  233. static void nf_conntrack_irc_fini(void)
  234. {
  235. int i;
  236. for (i = 0; i < ports_c; i++)
  237. nf_conntrack_helper_unregister(&irc[i]);
  238. kfree(irc_buffer);
  239. }
  240. module_init(nf_conntrack_irc_init);
  241. module_exit(nf_conntrack_irc_fini);