nf_defrag_ipv6_hooks.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/ipv6.h>
  10. #include <linux/in6.h>
  11. #include <linux/netfilter.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/icmp.h>
  15. #include <linux/sysctl.h>
  16. #include <net/ipv6.h>
  17. #include <net/inet_frag.h>
  18. #include <linux/netfilter_ipv6.h>
  19. #include <linux/netfilter_bridge.h>
  20. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  21. #include <net/netfilter/nf_conntrack.h>
  22. #include <net/netfilter/nf_conntrack_helper.h>
  23. #include <net/netfilter/nf_conntrack_l4proto.h>
  24. #include <net/netfilter/nf_conntrack_l3proto.h>
  25. #include <net/netfilter/nf_conntrack_core.h>
  26. #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
  27. #endif
  28. #include <net/netfilter/nf_conntrack_zones.h>
  29. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  30. static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
  31. struct sk_buff *skb)
  32. {
  33. u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
  34. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  35. if (skb->nfct) {
  36. enum ip_conntrack_info ctinfo;
  37. const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  38. zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
  39. }
  40. #endif
  41. if (nf_bridge_in_prerouting(skb))
  42. return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
  43. if (hooknum == NF_INET_PRE_ROUTING)
  44. return IP6_DEFRAG_CONNTRACK_IN + zone_id;
  45. else
  46. return IP6_DEFRAG_CONNTRACK_OUT + zone_id;
  47. }
  48. static unsigned int ipv6_defrag(void *priv,
  49. struct sk_buff *skb,
  50. const struct nf_hook_state *state)
  51. {
  52. struct sk_buff *reasm;
  53. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  54. /* Previously seen (loopback)? */
  55. if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
  56. return NF_ACCEPT;
  57. #endif
  58. reasm = nf_ct_frag6_gather(state->net, skb,
  59. nf_ct6_defrag_user(state->hook, skb));
  60. /* queued */
  61. if (reasm == NULL)
  62. return NF_STOLEN;
  63. /* error occurred or not fragmented */
  64. if (reasm == skb)
  65. return NF_ACCEPT;
  66. nf_ct_frag6_consume_orig(reasm);
  67. NF_HOOK_THRESH(NFPROTO_IPV6, state->hook, state->net, state->sk, reasm,
  68. state->in, state->out,
  69. state->okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
  70. return NF_STOLEN;
  71. }
  72. static struct nf_hook_ops ipv6_defrag_ops[] = {
  73. {
  74. .hook = ipv6_defrag,
  75. .pf = NFPROTO_IPV6,
  76. .hooknum = NF_INET_PRE_ROUTING,
  77. .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
  78. },
  79. {
  80. .hook = ipv6_defrag,
  81. .pf = NFPROTO_IPV6,
  82. .hooknum = NF_INET_LOCAL_OUT,
  83. .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
  84. },
  85. };
  86. static int __init nf_defrag_init(void)
  87. {
  88. int ret = 0;
  89. ret = nf_ct_frag6_init();
  90. if (ret < 0) {
  91. pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
  92. return ret;
  93. }
  94. ret = nf_register_hooks(ipv6_defrag_ops, ARRAY_SIZE(ipv6_defrag_ops));
  95. if (ret < 0) {
  96. pr_err("nf_defrag_ipv6: can't register hooks\n");
  97. goto cleanup_frag6;
  98. }
  99. return ret;
  100. cleanup_frag6:
  101. nf_ct_frag6_cleanup();
  102. return ret;
  103. }
  104. static void __exit nf_defrag_fini(void)
  105. {
  106. nf_unregister_hooks(ipv6_defrag_ops, ARRAY_SIZE(ipv6_defrag_ops));
  107. nf_ct_frag6_cleanup();
  108. }
  109. void nf_defrag_ipv6_enable(void)
  110. {
  111. }
  112. EXPORT_SYMBOL_GPL(nf_defrag_ipv6_enable);
  113. module_init(nf_defrag_init);
  114. module_exit(nf_defrag_fini);
  115. MODULE_LICENSE("GPL");