ip_conntrack_proto_icmp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #include <linux/types.h>
  9. #include <linux/timer.h>
  10. #include <linux/netfilter.h>
  11. #include <linux/in.h>
  12. #include <linux/icmp.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/skbuff.h>
  15. #include <net/ip.h>
  16. #include <net/checksum.h>
  17. #include <linux/netfilter_ipv4.h>
  18. #include <linux/netfilter_ipv4/ip_conntrack.h>
  19. #include <linux/netfilter_ipv4/ip_conntrack_core.h>
  20. #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
  21. unsigned int ip_ct_icmp_timeout __read_mostly = 30*HZ;
  22. #if 0
  23. #define DEBUGP printk
  24. #else
  25. #define DEBUGP(format, args...)
  26. #endif
  27. static int icmp_pkt_to_tuple(const struct sk_buff *skb,
  28. unsigned int dataoff,
  29. struct ip_conntrack_tuple *tuple)
  30. {
  31. struct icmphdr _hdr, *hp;
  32. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  33. if (hp == NULL)
  34. return 0;
  35. tuple->dst.u.icmp.type = hp->type;
  36. tuple->src.u.icmp.id = hp->un.echo.id;
  37. tuple->dst.u.icmp.code = hp->code;
  38. return 1;
  39. }
  40. /* Add 1; spaces filled with 0. */
  41. static const u_int8_t invmap[] = {
  42. [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
  43. [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
  44. [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
  45. [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
  46. [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
  47. [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
  48. [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
  49. [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
  50. };
  51. static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
  52. const struct ip_conntrack_tuple *orig)
  53. {
  54. if (orig->dst.u.icmp.type >= sizeof(invmap)
  55. || !invmap[orig->dst.u.icmp.type])
  56. return 0;
  57. tuple->src.u.icmp.id = orig->src.u.icmp.id;
  58. tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
  59. tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
  60. return 1;
  61. }
  62. /* Print out the per-protocol part of the tuple. */
  63. static int icmp_print_tuple(struct seq_file *s,
  64. const struct ip_conntrack_tuple *tuple)
  65. {
  66. return seq_printf(s, "type=%u code=%u id=%u ",
  67. tuple->dst.u.icmp.type,
  68. tuple->dst.u.icmp.code,
  69. ntohs(tuple->src.u.icmp.id));
  70. }
  71. /* Print out the private part of the conntrack. */
  72. static int icmp_print_conntrack(struct seq_file *s,
  73. const struct ip_conntrack *conntrack)
  74. {
  75. return 0;
  76. }
  77. /* Returns verdict for packet, or -1 for invalid. */
  78. static int icmp_packet(struct ip_conntrack *ct,
  79. const struct sk_buff *skb,
  80. enum ip_conntrack_info ctinfo)
  81. {
  82. /* Try to delete connection immediately after all replies:
  83. won't actually vanish as we still have skb, and del_timer
  84. means this will only run once even if count hits zero twice
  85. (theoretically possible with SMP) */
  86. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
  87. if (atomic_dec_and_test(&ct->proto.icmp.count)
  88. && del_timer(&ct->timeout))
  89. ct->timeout.function((unsigned long)ct);
  90. } else {
  91. atomic_inc(&ct->proto.icmp.count);
  92. ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
  93. ip_ct_refresh_acct(ct, ctinfo, skb, ip_ct_icmp_timeout);
  94. }
  95. return NF_ACCEPT;
  96. }
  97. /* Called when a new connection for this protocol found. */
  98. static int icmp_new(struct ip_conntrack *conntrack,
  99. const struct sk_buff *skb)
  100. {
  101. static const u_int8_t valid_new[] = {
  102. [ICMP_ECHO] = 1,
  103. [ICMP_TIMESTAMP] = 1,
  104. [ICMP_INFO_REQUEST] = 1,
  105. [ICMP_ADDRESS] = 1
  106. };
  107. if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
  108. || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
  109. /* Can't create a new ICMP `conn' with this. */
  110. DEBUGP("icmp: can't create new conn with type %u\n",
  111. conntrack->tuplehash[0].tuple.dst.u.icmp.type);
  112. DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
  113. return 0;
  114. }
  115. atomic_set(&conntrack->proto.icmp.count, 0);
  116. return 1;
  117. }
  118. static int
  119. icmp_error_message(struct sk_buff *skb,
  120. enum ip_conntrack_info *ctinfo,
  121. unsigned int hooknum)
  122. {
  123. struct ip_conntrack_tuple innertuple, origtuple;
  124. struct {
  125. struct icmphdr icmp;
  126. struct iphdr ip;
  127. } _in, *inside;
  128. struct ip_conntrack_protocol *innerproto;
  129. struct ip_conntrack_tuple_hash *h;
  130. int dataoff;
  131. IP_NF_ASSERT(skb->nfct == NULL);
  132. /* Not enough header? */
  133. inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
  134. if (inside == NULL)
  135. return -NF_ACCEPT;
  136. /* Ignore ICMP's containing fragments (shouldn't happen) */
  137. if (inside->ip.frag_off & htons(IP_OFFSET)) {
  138. DEBUGP("icmp_error_track: fragment of proto %u\n",
  139. inside->ip.protocol);
  140. return -NF_ACCEPT;
  141. }
  142. innerproto = ip_conntrack_proto_find_get(inside->ip.protocol);
  143. dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp) + inside->ip.ihl*4;
  144. /* Are they talking about one of our connections? */
  145. if (!ip_ct_get_tuple(&inside->ip, skb, dataoff, &origtuple, innerproto)) {
  146. DEBUGP("icmp_error: ! get_tuple p=%u", inside->ip.protocol);
  147. ip_conntrack_proto_put(innerproto);
  148. return -NF_ACCEPT;
  149. }
  150. /* Ordinarily, we'd expect the inverted tupleproto, but it's
  151. been preserved inside the ICMP. */
  152. if (!ip_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
  153. DEBUGP("icmp_error_track: Can't invert tuple\n");
  154. ip_conntrack_proto_put(innerproto);
  155. return -NF_ACCEPT;
  156. }
  157. ip_conntrack_proto_put(innerproto);
  158. *ctinfo = IP_CT_RELATED;
  159. h = ip_conntrack_find_get(&innertuple, NULL);
  160. if (!h) {
  161. /* Locally generated ICMPs will match inverted if they
  162. haven't been SNAT'ed yet */
  163. /* FIXME: NAT code has to handle half-done double NAT --RR */
  164. if (hooknum == NF_IP_LOCAL_OUT)
  165. h = ip_conntrack_find_get(&origtuple, NULL);
  166. if (!h) {
  167. DEBUGP("icmp_error_track: no match\n");
  168. return -NF_ACCEPT;
  169. }
  170. /* Reverse direction from that found */
  171. if (DIRECTION(h) != IP_CT_DIR_REPLY)
  172. *ctinfo += IP_CT_IS_REPLY;
  173. } else {
  174. if (DIRECTION(h) == IP_CT_DIR_REPLY)
  175. *ctinfo += IP_CT_IS_REPLY;
  176. }
  177. /* Update skb to refer to this connection */
  178. skb->nfct = &tuplehash_to_ctrack(h)->ct_general;
  179. skb->nfctinfo = *ctinfo;
  180. return -NF_ACCEPT;
  181. }
  182. /* Small and modified version of icmp_rcv */
  183. static int
  184. icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
  185. unsigned int hooknum)
  186. {
  187. struct icmphdr _ih, *icmph;
  188. /* Not enough header? */
  189. icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
  190. if (icmph == NULL) {
  191. if (LOG_INVALID(IPPROTO_ICMP))
  192. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  193. "ip_ct_icmp: short packet ");
  194. return -NF_ACCEPT;
  195. }
  196. /* See ip_conntrack_proto_tcp.c */
  197. if (ip_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
  198. nf_ip_checksum(skb, hooknum, skb->nh.iph->ihl * 4, 0)) {
  199. if (LOG_INVALID(IPPROTO_ICMP))
  200. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  201. "ip_ct_icmp: bad ICMP checksum ");
  202. return -NF_ACCEPT;
  203. }
  204. /*
  205. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  206. *
  207. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  208. * discarded.
  209. */
  210. if (icmph->type > NR_ICMP_TYPES) {
  211. if (LOG_INVALID(IPPROTO_ICMP))
  212. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  213. "ip_ct_icmp: invalid ICMP type ");
  214. return -NF_ACCEPT;
  215. }
  216. /* Need to track icmp error message? */
  217. if (icmph->type != ICMP_DEST_UNREACH
  218. && icmph->type != ICMP_SOURCE_QUENCH
  219. && icmph->type != ICMP_TIME_EXCEEDED
  220. && icmph->type != ICMP_PARAMETERPROB
  221. && icmph->type != ICMP_REDIRECT)
  222. return NF_ACCEPT;
  223. return icmp_error_message(skb, ctinfo, hooknum);
  224. }
  225. #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
  226. defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
  227. static int icmp_tuple_to_nfattr(struct sk_buff *skb,
  228. const struct ip_conntrack_tuple *t)
  229. {
  230. NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(__be16),
  231. &t->src.u.icmp.id);
  232. NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
  233. &t->dst.u.icmp.type);
  234. NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
  235. &t->dst.u.icmp.code);
  236. return 0;
  237. nfattr_failure:
  238. return -1;
  239. }
  240. static int icmp_nfattr_to_tuple(struct nfattr *tb[],
  241. struct ip_conntrack_tuple *tuple)
  242. {
  243. if (!tb[CTA_PROTO_ICMP_TYPE-1]
  244. || !tb[CTA_PROTO_ICMP_CODE-1]
  245. || !tb[CTA_PROTO_ICMP_ID-1])
  246. return -EINVAL;
  247. tuple->dst.u.icmp.type =
  248. *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
  249. tuple->dst.u.icmp.code =
  250. *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
  251. tuple->src.u.icmp.id =
  252. *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
  253. if (tuple->dst.u.icmp.type >= sizeof(invmap)
  254. || !invmap[tuple->dst.u.icmp.type])
  255. return -EINVAL;
  256. return 0;
  257. }
  258. #endif
  259. struct ip_conntrack_protocol ip_conntrack_protocol_icmp =
  260. {
  261. .proto = IPPROTO_ICMP,
  262. .name = "icmp",
  263. .pkt_to_tuple = icmp_pkt_to_tuple,
  264. .invert_tuple = icmp_invert_tuple,
  265. .print_tuple = icmp_print_tuple,
  266. .print_conntrack = icmp_print_conntrack,
  267. .packet = icmp_packet,
  268. .new = icmp_new,
  269. .error = icmp_error,
  270. #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
  271. defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
  272. .tuple_to_nfattr = icmp_tuple_to_nfattr,
  273. .nfattr_to_tuple = icmp_nfattr_to_tuple,
  274. #endif
  275. };