nf_conntrack_proto_icmp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3. * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/timer.h>
  11. #include <linux/netfilter.h>
  12. #include <linux/in.h>
  13. #include <linux/icmp.h>
  14. #include <linux/seq_file.h>
  15. #include <net/ip.h>
  16. #include <net/checksum.h>
  17. #include <linux/netfilter_ipv4.h>
  18. #include <net/netfilter/nf_conntrack_tuple.h>
  19. #include <net/netfilter/nf_conntrack_l4proto.h>
  20. #include <net/netfilter/nf_conntrack_core.h>
  21. #include <net/netfilter/nf_conntrack_zones.h>
  22. #include <net/netfilter/nf_log.h>
  23. static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
  24. static inline struct nf_icmp_net *icmp_pernet(struct net *net)
  25. {
  26. return &net->ct.nf_ct_proto.icmp;
  27. }
  28. static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
  29. struct net *net, struct nf_conntrack_tuple *tuple)
  30. {
  31. const struct icmphdr *hp;
  32. struct icmphdr _hdr;
  33. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  34. if (hp == NULL)
  35. return false;
  36. tuple->dst.u.icmp.type = hp->type;
  37. tuple->src.u.icmp.id = hp->un.echo.id;
  38. tuple->dst.u.icmp.code = hp->code;
  39. return true;
  40. }
  41. /* Add 1; spaces filled with 0. */
  42. static const u_int8_t invmap[] = {
  43. [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
  44. [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
  45. [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
  46. [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
  47. [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
  48. [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
  49. [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
  50. [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
  51. };
  52. static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
  53. const struct nf_conntrack_tuple *orig)
  54. {
  55. if (orig->dst.u.icmp.type >= sizeof(invmap) ||
  56. !invmap[orig->dst.u.icmp.type])
  57. return false;
  58. tuple->src.u.icmp.id = orig->src.u.icmp.id;
  59. tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
  60. tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
  61. return true;
  62. }
  63. /* Print out the per-protocol part of the tuple. */
  64. static void icmp_print_tuple(struct seq_file *s,
  65. const struct nf_conntrack_tuple *tuple)
  66. {
  67. seq_printf(s, "type=%u code=%u id=%u ",
  68. tuple->dst.u.icmp.type,
  69. tuple->dst.u.icmp.code,
  70. ntohs(tuple->src.u.icmp.id));
  71. }
  72. static unsigned int *icmp_get_timeouts(struct net *net)
  73. {
  74. return &icmp_pernet(net)->timeout;
  75. }
  76. /* Returns verdict for packet, or -1 for invalid. */
  77. static int icmp_packet(struct nf_conn *ct,
  78. const struct sk_buff *skb,
  79. unsigned int dataoff,
  80. enum ip_conntrack_info ctinfo,
  81. u_int8_t pf,
  82. unsigned int hooknum,
  83. unsigned int *timeout)
  84. {
  85. /* Do not immediately delete the connection after the first
  86. successful reply to avoid excessive conntrackd traffic
  87. and also to handle correctly ICMP echo reply duplicates. */
  88. nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
  89. return NF_ACCEPT;
  90. }
  91. /* Called when a new connection for this protocol found. */
  92. static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
  93. unsigned int dataoff, unsigned int *timeouts)
  94. {
  95. static const u_int8_t valid_new[] = {
  96. [ICMP_ECHO] = 1,
  97. [ICMP_TIMESTAMP] = 1,
  98. [ICMP_INFO_REQUEST] = 1,
  99. [ICMP_ADDRESS] = 1
  100. };
  101. if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
  102. !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
  103. /* Can't create a new ICMP `conn' with this. */
  104. pr_debug("icmp: can't create new conn with type %u\n",
  105. ct->tuplehash[0].tuple.dst.u.icmp.type);
  106. nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
  107. return false;
  108. }
  109. return true;
  110. }
  111. /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
  112. static int
  113. icmp_error_message(struct net *net, struct nf_conn *tmpl, 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. const struct nf_conntrack_zone *zone;
  121. struct nf_conntrack_zone tmp;
  122. NF_CT_ASSERT(skb->nfct == NULL);
  123. zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
  124. /* Are they talking about one of our connections? */
  125. if (!nf_ct_get_tuplepr(skb,
  126. skb_network_offset(skb) + ip_hdrlen(skb)
  127. + sizeof(struct icmphdr),
  128. PF_INET, net, &origtuple)) {
  129. pr_debug("icmp_error_message: failed to get tuple\n");
  130. return -NF_ACCEPT;
  131. }
  132. /* rcu_read_lock()ed by nf_hook_slow */
  133. innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
  134. /* Ordinarily, we'd expect the inverted tupleproto, but it's
  135. been preserved inside the ICMP. */
  136. if (!nf_ct_invert_tuple(&innertuple, &origtuple,
  137. &nf_conntrack_l3proto_ipv4, innerproto)) {
  138. pr_debug("icmp_error_message: no match\n");
  139. return -NF_ACCEPT;
  140. }
  141. *ctinfo = IP_CT_RELATED;
  142. h = nf_conntrack_find_get(net, zone, &innertuple);
  143. if (!h) {
  144. pr_debug("icmp_error_message: no match\n");
  145. return -NF_ACCEPT;
  146. }
  147. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  148. *ctinfo += IP_CT_IS_REPLY;
  149. /* Update skb to refer to this connection */
  150. skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
  151. skb->nfctinfo = *ctinfo;
  152. return NF_ACCEPT;
  153. }
  154. /* Small and modified version of icmp_rcv */
  155. static int
  156. icmp_error(struct net *net, struct nf_conn *tmpl,
  157. struct sk_buff *skb, unsigned int dataoff,
  158. enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
  159. {
  160. const struct icmphdr *icmph;
  161. struct icmphdr _ih;
  162. /* Not enough header? */
  163. icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
  164. if (icmph == NULL) {
  165. if (LOG_INVALID(net, IPPROTO_ICMP))
  166. nf_log_packet(net, PF_INET, 0, skb, NULL, NULL,
  167. NULL, "nf_ct_icmp: short packet ");
  168. return -NF_ACCEPT;
  169. }
  170. /* See ip_conntrack_proto_tcp.c */
  171. if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
  172. nf_ip_checksum(skb, hooknum, dataoff, 0)) {
  173. if (LOG_INVALID(net, IPPROTO_ICMP))
  174. nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
  175. "nf_ct_icmp: bad HW ICMP checksum ");
  176. return -NF_ACCEPT;
  177. }
  178. /*
  179. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  180. *
  181. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  182. * discarded.
  183. */
  184. if (icmph->type > NR_ICMP_TYPES) {
  185. if (LOG_INVALID(net, IPPROTO_ICMP))
  186. nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
  187. "nf_ct_icmp: invalid ICMP type ");
  188. return -NF_ACCEPT;
  189. }
  190. /* Need to track icmp error message? */
  191. if (icmph->type != ICMP_DEST_UNREACH &&
  192. icmph->type != ICMP_SOURCE_QUENCH &&
  193. icmph->type != ICMP_TIME_EXCEEDED &&
  194. icmph->type != ICMP_PARAMETERPROB &&
  195. icmph->type != ICMP_REDIRECT)
  196. return NF_ACCEPT;
  197. return icmp_error_message(net, tmpl, skb, ctinfo, hooknum);
  198. }
  199. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  200. #include <linux/netfilter/nfnetlink.h>
  201. #include <linux/netfilter/nfnetlink_conntrack.h>
  202. static int icmp_tuple_to_nlattr(struct sk_buff *skb,
  203. const struct nf_conntrack_tuple *t)
  204. {
  205. if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
  206. nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
  207. nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
  208. goto nla_put_failure;
  209. return 0;
  210. nla_put_failure:
  211. return -1;
  212. }
  213. static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
  214. [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
  215. [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
  216. [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
  217. };
  218. static int icmp_nlattr_to_tuple(struct nlattr *tb[],
  219. struct nf_conntrack_tuple *tuple)
  220. {
  221. if (!tb[CTA_PROTO_ICMP_TYPE] ||
  222. !tb[CTA_PROTO_ICMP_CODE] ||
  223. !tb[CTA_PROTO_ICMP_ID])
  224. return -EINVAL;
  225. tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
  226. tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
  227. tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
  228. if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
  229. !invmap[tuple->dst.u.icmp.type])
  230. return -EINVAL;
  231. return 0;
  232. }
  233. static int icmp_nlattr_tuple_size(void)
  234. {
  235. return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
  236. }
  237. #endif
  238. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  239. #include <linux/netfilter/nfnetlink.h>
  240. #include <linux/netfilter/nfnetlink_cttimeout.h>
  241. static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
  242. struct net *net, void *data)
  243. {
  244. unsigned int *timeout = data;
  245. struct nf_icmp_net *in = icmp_pernet(net);
  246. if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
  247. *timeout =
  248. ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
  249. } else {
  250. /* Set default ICMP timeout. */
  251. *timeout = in->timeout;
  252. }
  253. return 0;
  254. }
  255. static int
  256. icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  257. {
  258. const unsigned int *timeout = data;
  259. if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
  260. goto nla_put_failure;
  261. return 0;
  262. nla_put_failure:
  263. return -ENOSPC;
  264. }
  265. static const struct nla_policy
  266. icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
  267. [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
  268. };
  269. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  270. #ifdef CONFIG_SYSCTL
  271. static struct ctl_table icmp_sysctl_table[] = {
  272. {
  273. .procname = "nf_conntrack_icmp_timeout",
  274. .maxlen = sizeof(unsigned int),
  275. .mode = 0644,
  276. .proc_handler = proc_dointvec_jiffies,
  277. },
  278. { }
  279. };
  280. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  281. static struct ctl_table icmp_compat_sysctl_table[] = {
  282. {
  283. .procname = "ip_conntrack_icmp_timeout",
  284. .maxlen = sizeof(unsigned int),
  285. .mode = 0644,
  286. .proc_handler = proc_dointvec_jiffies,
  287. },
  288. { }
  289. };
  290. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  291. #endif /* CONFIG_SYSCTL */
  292. static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
  293. struct nf_icmp_net *in)
  294. {
  295. #ifdef CONFIG_SYSCTL
  296. pn->ctl_table = kmemdup(icmp_sysctl_table,
  297. sizeof(icmp_sysctl_table),
  298. GFP_KERNEL);
  299. if (!pn->ctl_table)
  300. return -ENOMEM;
  301. pn->ctl_table[0].data = &in->timeout;
  302. #endif
  303. return 0;
  304. }
  305. static int icmp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
  306. struct nf_icmp_net *in)
  307. {
  308. #ifdef CONFIG_SYSCTL
  309. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  310. pn->ctl_compat_table = kmemdup(icmp_compat_sysctl_table,
  311. sizeof(icmp_compat_sysctl_table),
  312. GFP_KERNEL);
  313. if (!pn->ctl_compat_table)
  314. return -ENOMEM;
  315. pn->ctl_compat_table[0].data = &in->timeout;
  316. #endif
  317. #endif
  318. return 0;
  319. }
  320. static int icmp_init_net(struct net *net, u_int16_t proto)
  321. {
  322. int ret;
  323. struct nf_icmp_net *in = icmp_pernet(net);
  324. struct nf_proto_net *pn = &in->pn;
  325. in->timeout = nf_ct_icmp_timeout;
  326. ret = icmp_kmemdup_compat_sysctl_table(pn, in);
  327. if (ret < 0)
  328. return ret;
  329. ret = icmp_kmemdup_sysctl_table(pn, in);
  330. if (ret < 0)
  331. nf_ct_kfree_compat_sysctl_table(pn);
  332. return ret;
  333. }
  334. static struct nf_proto_net *icmp_get_net_proto(struct net *net)
  335. {
  336. return &net->ct.nf_ct_proto.icmp.pn;
  337. }
  338. struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
  339. {
  340. .l3proto = PF_INET,
  341. .l4proto = IPPROTO_ICMP,
  342. .name = "icmp",
  343. .pkt_to_tuple = icmp_pkt_to_tuple,
  344. .invert_tuple = icmp_invert_tuple,
  345. .print_tuple = icmp_print_tuple,
  346. .packet = icmp_packet,
  347. .get_timeouts = icmp_get_timeouts,
  348. .new = icmp_new,
  349. .error = icmp_error,
  350. .destroy = NULL,
  351. .me = NULL,
  352. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  353. .tuple_to_nlattr = icmp_tuple_to_nlattr,
  354. .nlattr_tuple_size = icmp_nlattr_tuple_size,
  355. .nlattr_to_tuple = icmp_nlattr_to_tuple,
  356. .nla_policy = icmp_nla_policy,
  357. #endif
  358. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  359. .ctnl_timeout = {
  360. .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
  361. .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
  362. .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
  363. .obj_size = sizeof(unsigned int),
  364. .nla_policy = icmp_timeout_nla_policy,
  365. },
  366. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  367. .init_net = icmp_init_net,
  368. .get_net_proto = icmp_get_net_proto,
  369. };