PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Linux/net/ipv4/netfilter/nf_conntrack_proto_icmp.c

https://bitbucket.org/mateusz_krawczuk/rockchip2818-kernel
C | 324 lines | 251 code | 41 blank | 32 comment | 43 complexity | d72d69c1c52288780d4c110b2d1b365a MD5 | raw file
  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 <net/ip.h>
  15. #include <net/checksum.h>
  16. #include <linux/netfilter_ipv4.h>
  17. #include <net/netfilter/nf_conntrack_tuple.h>
  18. #include <net/netfilter/nf_conntrack_l4proto.h>
  19. #include <net/netfilter/nf_conntrack_core.h>
  20. #include <net/netfilter/nf_log.h>
  21. static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
  22. static int icmp_pkt_to_tuple(const struct sk_buff *skb,
  23. unsigned int dataoff,
  24. struct nf_conntrack_tuple *tuple)
  25. {
  26. const struct icmphdr *hp;
  27. struct icmphdr _hdr;
  28. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  29. if (hp == NULL)
  30. return 0;
  31. tuple->dst.u.icmp.type = hp->type;
  32. tuple->src.u.icmp.id = hp->un.echo.id;
  33. tuple->dst.u.icmp.code = hp->code;
  34. return 1;
  35. }
  36. /* Add 1; spaces filled with 0. */
  37. static const u_int8_t invmap[] = {
  38. [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
  39. [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
  40. [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
  41. [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
  42. [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
  43. [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
  44. [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
  45. [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
  46. };
  47. static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
  48. const struct nf_conntrack_tuple *orig)
  49. {
  50. if (orig->dst.u.icmp.type >= sizeof(invmap)
  51. || !invmap[orig->dst.u.icmp.type])
  52. return 0;
  53. tuple->src.u.icmp.id = orig->src.u.icmp.id;
  54. tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
  55. tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
  56. return 1;
  57. }
  58. /* Print out the per-protocol part of the tuple. */
  59. static int icmp_print_tuple(struct seq_file *s,
  60. const struct nf_conntrack_tuple *tuple)
  61. {
  62. return seq_printf(s, "type=%u code=%u id=%u ",
  63. tuple->dst.u.icmp.type,
  64. tuple->dst.u.icmp.code,
  65. ntohs(tuple->src.u.icmp.id));
  66. }
  67. /* Returns verdict for packet, or -1 for invalid. */
  68. static int icmp_packet(struct nf_conn *ct,
  69. const struct sk_buff *skb,
  70. unsigned int dataoff,
  71. enum ip_conntrack_info ctinfo,
  72. int pf,
  73. unsigned int hooknum)
  74. {
  75. /* Try to delete connection immediately after all replies:
  76. won't actually vanish as we still have skb, and del_timer
  77. means this will only run once even if count hits zero twice
  78. (theoretically possible with SMP) */
  79. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
  80. if (atomic_dec_and_test(&ct->proto.icmp.count)
  81. && del_timer(&ct->timeout))
  82. ct->timeout.function((unsigned long)ct);
  83. } else {
  84. atomic_inc(&ct->proto.icmp.count);
  85. nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
  86. nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
  87. }
  88. return NF_ACCEPT;
  89. }
  90. /* Called when a new connection for this protocol found. */
  91. static int icmp_new(struct nf_conn *ct,
  92. const struct sk_buff *skb, unsigned int dataoff)
  93. {
  94. static const u_int8_t valid_new[] = {
  95. [ICMP_ECHO] = 1,
  96. [ICMP_TIMESTAMP] = 1,
  97. [ICMP_INFO_REQUEST] = 1,
  98. [ICMP_ADDRESS] = 1
  99. };
  100. if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
  101. || !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
  102. /* Can't create a new ICMP `conn' with this. */
  103. pr_debug("icmp: can't create new conn with type %u\n",
  104. ct->tuplehash[0].tuple.dst.u.icmp.type);
  105. NF_CT_DUMP_TUPLE(&ct->tuplehash[0].tuple);
  106. return 0;
  107. }
  108. atomic_set(&ct->proto.icmp.count, 0);
  109. return 1;
  110. }
  111. /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
  112. static int
  113. icmp_error_message(struct sk_buff *skb,
  114. enum ip_conntrack_info *ctinfo,
  115. unsigned int hooknum)
  116. {
  117. struct nf_conntrack_tuple innertuple, origtuple;
  118. const struct nf_conntrack_l4proto *innerproto;
  119. const struct nf_conntrack_tuple_hash *h;
  120. NF_CT_ASSERT(skb->nfct == NULL);
  121. /* Are they talking about one of our connections? */
  122. if (!nf_ct_get_tuplepr(skb,
  123. skb_network_offset(skb) + ip_hdrlen(skb)
  124. + sizeof(struct icmphdr),
  125. PF_INET, &origtuple)) {
  126. pr_debug("icmp_error_message: failed to get tuple\n");
  127. return -NF_ACCEPT;
  128. }
  129. /* rcu_read_lock()ed by nf_hook_slow */
  130. innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
  131. /* Ordinarily, we'd expect the inverted tupleproto, but it's
  132. been preserved inside the ICMP. */
  133. if (!nf_ct_invert_tuple(&innertuple, &origtuple,
  134. &nf_conntrack_l3proto_ipv4, innerproto)) {
  135. pr_debug("icmp_error_message: no match\n");
  136. return -NF_ACCEPT;
  137. }
  138. *ctinfo = IP_CT_RELATED;
  139. h = nf_conntrack_find_get(&innertuple);
  140. if (!h) {
  141. pr_debug("icmp_error_message: no match\n");
  142. return -NF_ACCEPT;
  143. }
  144. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  145. *ctinfo += IP_CT_IS_REPLY;
  146. /* Update skb to refer to this connection */
  147. skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
  148. skb->nfctinfo = *ctinfo;
  149. return -NF_ACCEPT;
  150. }
  151. /* Small and modified version of icmp_rcv */
  152. static int
  153. icmp_error(struct sk_buff *skb, unsigned int dataoff,
  154. enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
  155. {
  156. const struct icmphdr *icmph;
  157. struct icmphdr _ih;
  158. /* Not enough header? */
  159. icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
  160. if (icmph == NULL) {
  161. if (LOG_INVALID(IPPROTO_ICMP))
  162. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  163. "nf_ct_icmp: short packet ");
  164. return -NF_ACCEPT;
  165. }
  166. /* See ip_conntrack_proto_tcp.c */
  167. if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
  168. nf_ip_checksum(skb, hooknum, dataoff, 0)) {
  169. if (LOG_INVALID(IPPROTO_ICMP))
  170. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  171. "nf_ct_icmp: bad HW ICMP checksum ");
  172. return -NF_ACCEPT;
  173. }
  174. /*
  175. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  176. *
  177. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  178. * discarded.
  179. */
  180. if (icmph->type > NR_ICMP_TYPES) {
  181. if (LOG_INVALID(IPPROTO_ICMP))
  182. nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
  183. "nf_ct_icmp: invalid ICMP type ");
  184. return -NF_ACCEPT;
  185. }
  186. /* Need to track icmp error message? */
  187. if (icmph->type != ICMP_DEST_UNREACH
  188. && icmph->type != ICMP_SOURCE_QUENCH
  189. && icmph->type != ICMP_TIME_EXCEEDED
  190. && icmph->type != ICMP_PARAMETERPROB
  191. && icmph->type != ICMP_REDIRECT)
  192. return NF_ACCEPT;
  193. return icmp_error_message(skb, ctinfo, hooknum);
  194. }
  195. #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
  196. #include <linux/netfilter/nfnetlink.h>
  197. #include <linux/netfilter/nfnetlink_conntrack.h>
  198. static int icmp_tuple_to_nlattr(struct sk_buff *skb,
  199. const struct nf_conntrack_tuple *t)
  200. {
  201. NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
  202. NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
  203. NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
  204. return 0;
  205. nla_put_failure:
  206. return -1;
  207. }
  208. static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
  209. [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
  210. [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
  211. [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
  212. };
  213. static int icmp_nlattr_to_tuple(struct nlattr *tb[],
  214. struct nf_conntrack_tuple *tuple)
  215. {
  216. if (!tb[CTA_PROTO_ICMP_TYPE]
  217. || !tb[CTA_PROTO_ICMP_CODE]
  218. || !tb[CTA_PROTO_ICMP_ID])
  219. return -EINVAL;
  220. tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
  221. tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
  222. tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
  223. if (tuple->dst.u.icmp.type >= sizeof(invmap)
  224. || !invmap[tuple->dst.u.icmp.type])
  225. return -EINVAL;
  226. return 0;
  227. }
  228. #endif
  229. #ifdef CONFIG_SYSCTL
  230. static struct ctl_table_header *icmp_sysctl_header;
  231. static struct ctl_table icmp_sysctl_table[] = {
  232. {
  233. .procname = "nf_conntrack_icmp_timeout",
  234. .data = &nf_ct_icmp_timeout,
  235. .maxlen = sizeof(unsigned int),
  236. .mode = 0644,
  237. .proc_handler = &proc_dointvec_jiffies,
  238. },
  239. {
  240. .ctl_name = 0
  241. }
  242. };
  243. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  244. static struct ctl_table icmp_compat_sysctl_table[] = {
  245. {
  246. .procname = "ip_conntrack_icmp_timeout",
  247. .data = &nf_ct_icmp_timeout,
  248. .maxlen = sizeof(unsigned int),
  249. .mode = 0644,
  250. .proc_handler = &proc_dointvec_jiffies,
  251. },
  252. {
  253. .ctl_name = 0
  254. }
  255. };
  256. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  257. #endif /* CONFIG_SYSCTL */
  258. struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
  259. {
  260. .l3proto = PF_INET,
  261. .l4proto = 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. .packet = icmp_packet,
  267. .new = icmp_new,
  268. .error = icmp_error,
  269. .destroy = NULL,
  270. .me = NULL,
  271. #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
  272. .tuple_to_nlattr = icmp_tuple_to_nlattr,
  273. .nlattr_to_tuple = icmp_nlattr_to_tuple,
  274. .nla_policy = icmp_nla_policy,
  275. #endif
  276. #ifdef CONFIG_SYSCTL
  277. .ctl_table_header = &icmp_sysctl_header,
  278. .ctl_table = icmp_sysctl_table,
  279. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  280. .ctl_compat_table = icmp_compat_sysctl_table,
  281. #endif
  282. #endif
  283. };