nf_conntrack_proto_icmp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
  9. * - enable working with Layer 3 protocol independent connection tracking.
  10. *
  11. * Derived from net/ipv4/netfilter/ip_conntrack_proto_icmp.c
  12. */
  13. #include <linux/types.h>
  14. #include <linux/timer.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/in.h>
  17. #include <linux/icmp.h>
  18. #include <linux/seq_file.h>
  19. #include <net/ip.h>
  20. #include <net/checksum.h>
  21. #include <linux/netfilter_ipv4.h>
  22. #include <net/netfilter/nf_conntrack_tuple.h>
  23. #include <net/netfilter/nf_conntrack_l4proto.h>
  24. #include <net/netfilter/nf_conntrack_core.h>
  25. static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
  26. #if 0
  27. #define DEBUGP printk
  28. #else
  29. #define DEBUGP(format, args...)
  30. #endif
  31. static int icmp_pkt_to_tuple(const struct sk_buff *skb,
  32. unsigned int dataoff,
  33. struct nf_conntrack_tuple *tuple)
  34. {
  35. struct icmphdr _hdr, *hp;
  36. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  37. if (hp == NULL)
  38. return 0;
  39. tuple->dst.u.icmp.type = hp->type;
  40. tuple->src.u.icmp.id = hp->un.echo.id;
  41. tuple->dst.u.icmp.code = hp->code;
  42. return 1;
  43. }
  44. /* Add 1; spaces filled with 0. */
  45. static const u_int8_t invmap[] = {
  46. [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
  47. [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
  48. [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
  49. [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
  50. [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
  51. [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
  52. [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
  53. [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
  54. };
  55. static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
  56. const struct nf_conntrack_tuple *orig)
  57. {
  58. if (orig->dst.u.icmp.type >= sizeof(invmap)
  59. || !invmap[orig->dst.u.icmp.type])
  60. return 0;
  61. tuple->src.u.icmp.id = orig->src.u.icmp.id;
  62. tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
  63. tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
  64. return 1;
  65. }
  66. /* Print out the per-protocol part of the tuple. */
  67. static int icmp_print_tuple(struct seq_file *s,
  68. const struct nf_conntrack_tuple *tuple)
  69. {
  70. return seq_printf(s, "type=%u code=%u id=%u ",
  71. tuple->dst.u.icmp.type,
  72. tuple->dst.u.icmp.code,
  73. ntohs(tuple->src.u.icmp.id));
  74. }
  75. /* Print out the private part of the conntrack. */
  76. static int icmp_print_conntrack(struct seq_file *s,
  77. const struct nf_conn *conntrack)
  78. {
  79. return 0;
  80. }
  81. /* Returns verdict for packet, or -1 for invalid. */
  82. static int icmp_packet(struct nf_conn *ct,
  83. const struct sk_buff *skb,
  84. unsigned int dataoff,
  85. enum ip_conntrack_info ctinfo,
  86. int pf,
  87. unsigned int hooknum)
  88. {
  89. /* Try to delete connection immediately after all replies:
  90. won't actually vanish as we still have skb, and del_timer
  91. means this will only run once even if count hits zero twice
  92. (theoretically possible with SMP) */
  93. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
  94. if (atomic_dec_and_test(&ct->proto.icmp.count)
  95. && del_timer(&ct->timeout))
  96. ct->timeout.function((unsigned long)ct);
  97. } else {
  98. atomic_inc(&ct->proto.icmp.count);
  99. nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
  100. nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
  101. }
  102. return NF_ACCEPT;
  103. }
  104. /* Called when a new connection for this protocol found. */
  105. static int icmp_new(struct nf_conn *conntrack,
  106. const struct sk_buff *skb, unsigned int dataoff)
  107. {
  108. static const u_int8_t valid_new[] = {
  109. [ICMP_ECHO] = 1,
  110. [ICMP_TIMESTAMP] = 1,
  111. [ICMP_INFO_REQUEST] = 1,
  112. [ICMP_ADDRESS] = 1
  113. };
  114. if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
  115. || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
  116. /* Can't create a new ICMP `conn' with this. */
  117. DEBUGP("icmp: can't create new conn with type %u\n",
  118. conntrack->tuplehash[0].tuple.dst.u.icmp.type);
  119. NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
  120. return 0;
  121. }
  122. atomic_set(&conntrack->proto.icmp.count, 0);
  123. return 1;
  124. }
  125. extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;
  126. /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
  127. static int
  128. icmp_error_message(struct sk_buff *skb,
  129. enum ip_conntrack_info *ctinfo,
  130. unsigned int hooknum)
  131. {
  132. struct nf_conntrack_tuple innertuple, origtuple;
  133. struct {
  134. struct icmphdr icmp;
  135. struct iphdr ip;
  136. } _in, *inside;
  137. struct nf_conntrack_l4proto *innerproto;
  138. struct nf_conntrack_tuple_hash *h;
  139. int dataoff;
  140. NF_CT_ASSERT(skb->nfct == NULL);
  141. /* Not enough header? */
  142. inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
  143. if (inside == NULL)
  144. return -NF_ACCEPT;
  145. /* Ignore ICMP's containing fragments (shouldn't happen) */
  146. if (inside->ip.frag_off & htons(IP_OFFSET)) {
  147. DEBUGP("icmp_error_message: fragment of proto %u\n",
  148. inside->ip.protocol);
  149. return -NF_ACCEPT;
  150. }
  151. /* rcu_read_lock()ed by nf_hook_slow */
  152. innerproto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
  153. dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp);
  154. /* Are they talking about one of our connections? */
  155. if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET,
  156. inside->ip.protocol, &origtuple,
  157. &nf_conntrack_l3proto_ipv4, innerproto)) {
  158. DEBUGP("icmp_error_message: ! get_tuple p=%u",
  159. inside->ip.protocol);
  160. return -NF_ACCEPT;
  161. }
  162. /* Ordinarily, we'd expect the inverted tupleproto, but it's
  163. been preserved inside the ICMP. */
  164. if (!nf_ct_invert_tuple(&innertuple, &origtuple,
  165. &nf_conntrack_l3proto_ipv4, innerproto)) {
  166. DEBUGP("icmp_error_message: no match\n");
  167. return -NF_ACCEPT;
  168. }
  169. *ctinfo = IP_CT_RELATED;
  170. h = nf_conntrack_find_get(&innertuple, NULL);
  171. if (!h) {
  172. /* Locally generated ICMPs will match inverted if they
  173. haven't been SNAT'ed yet */
  174. /* FIXME: NAT code has to handle half-done double NAT --RR */
  175. if (hooknum == NF_IP_LOCAL_OUT)
  176. h = nf_conntrack_find_get(&origtuple, NULL);
  177. if (!h) {
  178. DEBUGP("icmp_error_message: no match\n");
  179. return -NF_ACCEPT;
  180. }
  181. /* Reverse direction from that found */
  182. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  183. *ctinfo += IP_CT_IS_REPLY;
  184. } else {
  185. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  186. *ctinfo += IP_CT_IS_REPLY;
  187. }
  188. /* Update skb to refer to this connection */
  189. skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
  190. skb->nfctinfo = *ctinfo;
  191. return -NF_ACCEPT;
  192. }
  193. /* Small and modified version of icmp_rcv */
  194. static int
  195. icmp_error(struct sk_buff *skb, unsigned int dataoff,
  196. enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
  197. {
  198. struct icmphdr _ih, *icmph;
  199. /* Not enough header? */
  200. icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
  201. if (icmph == NULL) {
  202. if (LOG_INVALID(IPPROTO_ICMP))
  203. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  204. "nf_ct_icmp: short packet ");
  205. return -NF_ACCEPT;
  206. }
  207. /* See ip_conntrack_proto_tcp.c */
  208. if (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
  209. nf_ip_checksum(skb, hooknum, dataoff, 0)) {
  210. if (LOG_INVALID(IPPROTO_ICMP))
  211. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  212. "nf_ct_icmp: bad HW ICMP checksum ");
  213. return -NF_ACCEPT;
  214. }
  215. /*
  216. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  217. *
  218. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  219. * discarded.
  220. */
  221. if (icmph->type > NR_ICMP_TYPES) {
  222. if (LOG_INVALID(IPPROTO_ICMP))
  223. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  224. "nf_ct_icmp: invalid ICMP type ");
  225. return -NF_ACCEPT;
  226. }
  227. /* Need to track icmp error message? */
  228. if (icmph->type != ICMP_DEST_UNREACH
  229. && icmph->type != ICMP_SOURCE_QUENCH
  230. && icmph->type != ICMP_TIME_EXCEEDED
  231. && icmph->type != ICMP_PARAMETERPROB
  232. && icmph->type != ICMP_REDIRECT)
  233. return NF_ACCEPT;
  234. return icmp_error_message(skb, ctinfo, hooknum);
  235. }
  236. #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
  237. #include <linux/netfilter/nfnetlink.h>
  238. #include <linux/netfilter/nfnetlink_conntrack.h>
  239. static int icmp_tuple_to_nfattr(struct sk_buff *skb,
  240. const struct nf_conntrack_tuple *t)
  241. {
  242. NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
  243. &t->src.u.icmp.id);
  244. NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
  245. &t->dst.u.icmp.type);
  246. NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
  247. &t->dst.u.icmp.code);
  248. return 0;
  249. nfattr_failure:
  250. return -1;
  251. }
  252. static const size_t cta_min_proto[CTA_PROTO_MAX] = {
  253. [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
  254. [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
  255. [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
  256. };
  257. static int icmp_nfattr_to_tuple(struct nfattr *tb[],
  258. struct nf_conntrack_tuple *tuple)
  259. {
  260. if (!tb[CTA_PROTO_ICMP_TYPE-1]
  261. || !tb[CTA_PROTO_ICMP_CODE-1]
  262. || !tb[CTA_PROTO_ICMP_ID-1])
  263. return -EINVAL;
  264. if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
  265. return -EINVAL;
  266. tuple->dst.u.icmp.type =
  267. *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
  268. tuple->dst.u.icmp.code =
  269. *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
  270. tuple->src.u.icmp.id =
  271. *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
  272. if (tuple->dst.u.icmp.type >= sizeof(invmap)
  273. || !invmap[tuple->dst.u.icmp.type])
  274. return -EINVAL;
  275. return 0;
  276. }
  277. #endif
  278. #ifdef CONFIG_SYSCTL
  279. static struct ctl_table_header *icmp_sysctl_header;
  280. static struct ctl_table icmp_sysctl_table[] = {
  281. {
  282. .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
  283. .procname = "nf_conntrack_icmp_timeout",
  284. .data = &nf_ct_icmp_timeout,
  285. .maxlen = sizeof(unsigned int),
  286. .mode = 0644,
  287. .proc_handler = &proc_dointvec_jiffies,
  288. },
  289. {
  290. .ctl_name = 0
  291. }
  292. };
  293. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  294. static struct ctl_table icmp_compat_sysctl_table[] = {
  295. {
  296. .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
  297. .procname = "ip_conntrack_icmp_timeout",
  298. .data = &nf_ct_icmp_timeout,
  299. .maxlen = sizeof(unsigned int),
  300. .mode = 0644,
  301. .proc_handler = &proc_dointvec_jiffies,
  302. },
  303. {
  304. .ctl_name = 0
  305. }
  306. };
  307. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  308. #endif /* CONFIG_SYSCTL */
  309. struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
  310. {
  311. .l3proto = PF_INET,
  312. .l4proto = IPPROTO_ICMP,
  313. .name = "icmp",
  314. .pkt_to_tuple = icmp_pkt_to_tuple,
  315. .invert_tuple = icmp_invert_tuple,
  316. .print_tuple = icmp_print_tuple,
  317. .print_conntrack = icmp_print_conntrack,
  318. .packet = icmp_packet,
  319. .new = icmp_new,
  320. .error = icmp_error,
  321. .destroy = NULL,
  322. .me = NULL,
  323. #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
  324. .tuple_to_nfattr = icmp_tuple_to_nfattr,
  325. .nfattr_to_tuple = icmp_nfattr_to_tuple,
  326. #endif
  327. #ifdef CONFIG_SYSCTL
  328. .ctl_table_header = &icmp_sysctl_header,
  329. .ctl_table = icmp_sysctl_table,
  330. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  331. .ctl_compat_table = icmp_compat_sysctl_table,
  332. #endif
  333. #endif
  334. };
  335. EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);