nf_conntrack_proto_icmpv6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C)2003,2004 USAGI/WIDE Project
  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. * Author:
  9. * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
  10. */
  11. #include <linux/types.h>
  12. #include <linux/timer.h>
  13. #include <linux/module.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/in6.h>
  16. #include <linux/icmpv6.h>
  17. #include <linux/ipv6.h>
  18. #include <net/ipv6.h>
  19. #include <net/ip6_checksum.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/netfilter_ipv6.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. #include <net/netfilter/nf_conntrack_zones.h>
  26. #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
  27. #include <net/netfilter/nf_log.h>
  28. static unsigned int nf_ct_icmpv6_timeout __read_mostly = 30*HZ;
  29. static inline struct nf_icmp_net *icmpv6_pernet(struct net *net)
  30. {
  31. return &net->ct.nf_ct_proto.icmpv6;
  32. }
  33. static bool icmpv6_pkt_to_tuple(const struct sk_buff *skb,
  34. unsigned int dataoff,
  35. struct net *net,
  36. struct nf_conntrack_tuple *tuple)
  37. {
  38. const struct icmp6hdr *hp;
  39. struct icmp6hdr _hdr;
  40. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  41. if (hp == NULL)
  42. return false;
  43. tuple->dst.u.icmp.type = hp->icmp6_type;
  44. tuple->src.u.icmp.id = hp->icmp6_identifier;
  45. tuple->dst.u.icmp.code = hp->icmp6_code;
  46. return true;
  47. }
  48. /* Add 1; spaces filled with 0. */
  49. static const u_int8_t invmap[] = {
  50. [ICMPV6_ECHO_REQUEST - 128] = ICMPV6_ECHO_REPLY + 1,
  51. [ICMPV6_ECHO_REPLY - 128] = ICMPV6_ECHO_REQUEST + 1,
  52. [ICMPV6_NI_QUERY - 128] = ICMPV6_NI_REPLY + 1,
  53. [ICMPV6_NI_REPLY - 128] = ICMPV6_NI_QUERY + 1
  54. };
  55. static const u_int8_t noct_valid_new[] = {
  56. [ICMPV6_MGM_QUERY - 130] = 1,
  57. [ICMPV6_MGM_REPORT - 130] = 1,
  58. [ICMPV6_MGM_REDUCTION - 130] = 1,
  59. [NDISC_ROUTER_SOLICITATION - 130] = 1,
  60. [NDISC_ROUTER_ADVERTISEMENT - 130] = 1,
  61. [NDISC_NEIGHBOUR_SOLICITATION - 130] = 1,
  62. [NDISC_NEIGHBOUR_ADVERTISEMENT - 130] = 1,
  63. [ICMPV6_MLD2_REPORT - 130] = 1
  64. };
  65. static bool icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple,
  66. const struct nf_conntrack_tuple *orig)
  67. {
  68. int type = orig->dst.u.icmp.type - 128;
  69. if (type < 0 || type >= sizeof(invmap) || !invmap[type])
  70. return false;
  71. tuple->src.u.icmp.id = orig->src.u.icmp.id;
  72. tuple->dst.u.icmp.type = invmap[type] - 1;
  73. tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
  74. return true;
  75. }
  76. /* Print out the per-protocol part of the tuple. */
  77. static void icmpv6_print_tuple(struct seq_file *s,
  78. const struct nf_conntrack_tuple *tuple)
  79. {
  80. seq_printf(s, "type=%u code=%u id=%u ",
  81. tuple->dst.u.icmp.type,
  82. tuple->dst.u.icmp.code,
  83. ntohs(tuple->src.u.icmp.id));
  84. }
  85. static unsigned int *icmpv6_get_timeouts(struct net *net)
  86. {
  87. return &icmpv6_pernet(net)->timeout;
  88. }
  89. /* Returns verdict for packet, or -1 for invalid. */
  90. static int icmpv6_packet(struct nf_conn *ct,
  91. const struct sk_buff *skb,
  92. unsigned int dataoff,
  93. enum ip_conntrack_info ctinfo,
  94. u_int8_t pf,
  95. unsigned int hooknum,
  96. unsigned int *timeout)
  97. {
  98. /* Do not immediately delete the connection after the first
  99. successful reply to avoid excessive conntrackd traffic
  100. and also to handle correctly ICMP echo reply duplicates. */
  101. nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
  102. return NF_ACCEPT;
  103. }
  104. /* Called when a new connection for this protocol found. */
  105. static bool icmpv6_new(struct nf_conn *ct, const struct sk_buff *skb,
  106. unsigned int dataoff, unsigned int *timeouts)
  107. {
  108. static const u_int8_t valid_new[] = {
  109. [ICMPV6_ECHO_REQUEST - 128] = 1,
  110. [ICMPV6_NI_QUERY - 128] = 1
  111. };
  112. int type = ct->tuplehash[0].tuple.dst.u.icmp.type - 128;
  113. if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) {
  114. /* Can't create a new ICMPv6 `conn' with this. */
  115. pr_debug("icmpv6: can't create new conn with type %u\n",
  116. type + 128);
  117. nf_ct_dump_tuple_ipv6(&ct->tuplehash[0].tuple);
  118. if (LOG_INVALID(nf_ct_net(ct), IPPROTO_ICMPV6))
  119. nf_log_packet(nf_ct_net(ct), PF_INET6, 0, skb, NULL,
  120. NULL, NULL,
  121. "nf_ct_icmpv6: invalid new with type %d ",
  122. type + 128);
  123. return false;
  124. }
  125. return true;
  126. }
  127. static int
  128. icmpv6_error_message(struct net *net, struct nf_conn *tmpl,
  129. struct sk_buff *skb,
  130. unsigned int icmp6off,
  131. enum ip_conntrack_info *ctinfo,
  132. unsigned int hooknum)
  133. {
  134. struct nf_conntrack_tuple intuple, origtuple;
  135. const struct nf_conntrack_tuple_hash *h;
  136. const struct nf_conntrack_l4proto *inproto;
  137. struct nf_conntrack_zone tmp;
  138. NF_CT_ASSERT(skb->nfct == NULL);
  139. /* Are they talking about one of our connections? */
  140. if (!nf_ct_get_tuplepr(skb,
  141. skb_network_offset(skb)
  142. + sizeof(struct ipv6hdr)
  143. + sizeof(struct icmp6hdr),
  144. PF_INET6, net, &origtuple)) {
  145. pr_debug("icmpv6_error: Can't get tuple\n");
  146. return -NF_ACCEPT;
  147. }
  148. /* rcu_read_lock()ed by nf_hook_slow */
  149. inproto = __nf_ct_l4proto_find(PF_INET6, origtuple.dst.protonum);
  150. /* Ordinarily, we'd expect the inverted tupleproto, but it's
  151. been preserved inside the ICMP. */
  152. if (!nf_ct_invert_tuple(&intuple, &origtuple,
  153. &nf_conntrack_l3proto_ipv6, inproto)) {
  154. pr_debug("icmpv6_error: Can't invert tuple\n");
  155. return -NF_ACCEPT;
  156. }
  157. *ctinfo = IP_CT_RELATED;
  158. h = nf_conntrack_find_get(net, nf_ct_zone_tmpl(tmpl, skb, &tmp),
  159. &intuple);
  160. if (!h) {
  161. pr_debug("icmpv6_error: no match\n");
  162. return -NF_ACCEPT;
  163. } else {
  164. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  165. *ctinfo += IP_CT_IS_REPLY;
  166. }
  167. /* Update skb to refer to this connection */
  168. skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
  169. skb->nfctinfo = *ctinfo;
  170. return NF_ACCEPT;
  171. }
  172. static int
  173. icmpv6_error(struct net *net, struct nf_conn *tmpl,
  174. struct sk_buff *skb, unsigned int dataoff,
  175. enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
  176. {
  177. const struct icmp6hdr *icmp6h;
  178. struct icmp6hdr _ih;
  179. int type;
  180. icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
  181. if (icmp6h == NULL) {
  182. if (LOG_INVALID(net, IPPROTO_ICMPV6))
  183. nf_log_packet(net, PF_INET6, 0, skb, NULL, NULL, NULL,
  184. "nf_ct_icmpv6: short packet ");
  185. return -NF_ACCEPT;
  186. }
  187. if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
  188. nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) {
  189. if (LOG_INVALID(net, IPPROTO_ICMPV6))
  190. nf_log_packet(net, PF_INET6, 0, skb, NULL, NULL, NULL,
  191. "nf_ct_icmpv6: ICMPv6 checksum failed ");
  192. return -NF_ACCEPT;
  193. }
  194. type = icmp6h->icmp6_type - 130;
  195. if (type >= 0 && type < sizeof(noct_valid_new) &&
  196. noct_valid_new[type]) {
  197. skb->nfct = &nf_ct_untracked_get()->ct_general;
  198. skb->nfctinfo = IP_CT_NEW;
  199. nf_conntrack_get(skb->nfct);
  200. return NF_ACCEPT;
  201. }
  202. /* is not error message ? */
  203. if (icmp6h->icmp6_type >= 128)
  204. return NF_ACCEPT;
  205. return icmpv6_error_message(net, tmpl, skb, dataoff, ctinfo, hooknum);
  206. }
  207. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  208. #include <linux/netfilter/nfnetlink.h>
  209. #include <linux/netfilter/nfnetlink_conntrack.h>
  210. static int icmpv6_tuple_to_nlattr(struct sk_buff *skb,
  211. const struct nf_conntrack_tuple *t)
  212. {
  213. if (nla_put_be16(skb, CTA_PROTO_ICMPV6_ID, t->src.u.icmp.id) ||
  214. nla_put_u8(skb, CTA_PROTO_ICMPV6_TYPE, t->dst.u.icmp.type) ||
  215. nla_put_u8(skb, CTA_PROTO_ICMPV6_CODE, t->dst.u.icmp.code))
  216. goto nla_put_failure;
  217. return 0;
  218. nla_put_failure:
  219. return -1;
  220. }
  221. static const struct nla_policy icmpv6_nla_policy[CTA_PROTO_MAX+1] = {
  222. [CTA_PROTO_ICMPV6_TYPE] = { .type = NLA_U8 },
  223. [CTA_PROTO_ICMPV6_CODE] = { .type = NLA_U8 },
  224. [CTA_PROTO_ICMPV6_ID] = { .type = NLA_U16 },
  225. };
  226. static int icmpv6_nlattr_to_tuple(struct nlattr *tb[],
  227. struct nf_conntrack_tuple *tuple)
  228. {
  229. if (!tb[CTA_PROTO_ICMPV6_TYPE] ||
  230. !tb[CTA_PROTO_ICMPV6_CODE] ||
  231. !tb[CTA_PROTO_ICMPV6_ID])
  232. return -EINVAL;
  233. tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]);
  234. tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]);
  235. tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMPV6_ID]);
  236. if (tuple->dst.u.icmp.type < 128 ||
  237. tuple->dst.u.icmp.type - 128 >= sizeof(invmap) ||
  238. !invmap[tuple->dst.u.icmp.type - 128])
  239. return -EINVAL;
  240. return 0;
  241. }
  242. static int icmpv6_nlattr_tuple_size(void)
  243. {
  244. return nla_policy_len(icmpv6_nla_policy, CTA_PROTO_MAX + 1);
  245. }
  246. #endif
  247. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  248. #include <linux/netfilter/nfnetlink.h>
  249. #include <linux/netfilter/nfnetlink_cttimeout.h>
  250. static int icmpv6_timeout_nlattr_to_obj(struct nlattr *tb[],
  251. struct net *net, void *data)
  252. {
  253. unsigned int *timeout = data;
  254. struct nf_icmp_net *in = icmpv6_pernet(net);
  255. if (tb[CTA_TIMEOUT_ICMPV6_TIMEOUT]) {
  256. *timeout =
  257. ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMPV6_TIMEOUT])) * HZ;
  258. } else {
  259. /* Set default ICMPv6 timeout. */
  260. *timeout = in->timeout;
  261. }
  262. return 0;
  263. }
  264. static int
  265. icmpv6_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  266. {
  267. const unsigned int *timeout = data;
  268. if (nla_put_be32(skb, CTA_TIMEOUT_ICMPV6_TIMEOUT, htonl(*timeout / HZ)))
  269. goto nla_put_failure;
  270. return 0;
  271. nla_put_failure:
  272. return -ENOSPC;
  273. }
  274. static const struct nla_policy
  275. icmpv6_timeout_nla_policy[CTA_TIMEOUT_ICMPV6_MAX+1] = {
  276. [CTA_TIMEOUT_ICMPV6_TIMEOUT] = { .type = NLA_U32 },
  277. };
  278. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  279. #ifdef CONFIG_SYSCTL
  280. static struct ctl_table icmpv6_sysctl_table[] = {
  281. {
  282. .procname = "nf_conntrack_icmpv6_timeout",
  283. .maxlen = sizeof(unsigned int),
  284. .mode = 0644,
  285. .proc_handler = proc_dointvec_jiffies,
  286. },
  287. { }
  288. };
  289. #endif /* CONFIG_SYSCTL */
  290. static int icmpv6_kmemdup_sysctl_table(struct nf_proto_net *pn,
  291. struct nf_icmp_net *in)
  292. {
  293. #ifdef CONFIG_SYSCTL
  294. pn->ctl_table = kmemdup(icmpv6_sysctl_table,
  295. sizeof(icmpv6_sysctl_table),
  296. GFP_KERNEL);
  297. if (!pn->ctl_table)
  298. return -ENOMEM;
  299. pn->ctl_table[0].data = &in->timeout;
  300. #endif
  301. return 0;
  302. }
  303. static int icmpv6_init_net(struct net *net, u_int16_t proto)
  304. {
  305. struct nf_icmp_net *in = icmpv6_pernet(net);
  306. struct nf_proto_net *pn = &in->pn;
  307. in->timeout = nf_ct_icmpv6_timeout;
  308. return icmpv6_kmemdup_sysctl_table(pn, in);
  309. }
  310. static struct nf_proto_net *icmpv6_get_net_proto(struct net *net)
  311. {
  312. return &net->ct.nf_ct_proto.icmpv6.pn;
  313. }
  314. struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 __read_mostly =
  315. {
  316. .l3proto = PF_INET6,
  317. .l4proto = IPPROTO_ICMPV6,
  318. .name = "icmpv6",
  319. .pkt_to_tuple = icmpv6_pkt_to_tuple,
  320. .invert_tuple = icmpv6_invert_tuple,
  321. .print_tuple = icmpv6_print_tuple,
  322. .packet = icmpv6_packet,
  323. .get_timeouts = icmpv6_get_timeouts,
  324. .new = icmpv6_new,
  325. .error = icmpv6_error,
  326. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  327. .tuple_to_nlattr = icmpv6_tuple_to_nlattr,
  328. .nlattr_tuple_size = icmpv6_nlattr_tuple_size,
  329. .nlattr_to_tuple = icmpv6_nlattr_to_tuple,
  330. .nla_policy = icmpv6_nla_policy,
  331. #endif
  332. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  333. .ctnl_timeout = {
  334. .nlattr_to_obj = icmpv6_timeout_nlattr_to_obj,
  335. .obj_to_nlattr = icmpv6_timeout_obj_to_nlattr,
  336. .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
  337. .obj_size = sizeof(unsigned int),
  338. .nla_policy = icmpv6_timeout_nla_policy,
  339. },
  340. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  341. .init_net = icmpv6_init_net,
  342. .get_net_proto = icmpv6_get_net_proto,
  343. };