ip6table_mangle.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
  3. *
  4. * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
  5. * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/netfilter_ipv6/ip6_tables.h>
  13. #include <linux/slab.h>
  14. #include <net/ipv6.h>
  15. MODULE_LICENSE("GPL");
  16. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  17. MODULE_DESCRIPTION("ip6tables mangle table");
  18. #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
  19. (1 << NF_INET_LOCAL_IN) | \
  20. (1 << NF_INET_FORWARD) | \
  21. (1 << NF_INET_LOCAL_OUT) | \
  22. (1 << NF_INET_POST_ROUTING))
  23. static const struct xt_table packet_mangler = {
  24. .name = "mangle",
  25. .valid_hooks = MANGLE_VALID_HOOKS,
  26. .me = THIS_MODULE,
  27. .af = NFPROTO_IPV6,
  28. .priority = NF_IP6_PRI_MANGLE,
  29. };
  30. static unsigned int
  31. ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
  32. {
  33. unsigned int ret;
  34. struct in6_addr saddr, daddr;
  35. u_int8_t hop_limit;
  36. u_int32_t flowlabel, mark;
  37. int err;
  38. #if 0
  39. /* root is playing with raw sockets. */
  40. if (skb->len < sizeof(struct iphdr) ||
  41. ip_hdrlen(skb) < sizeof(struct iphdr)) {
  42. net_warn_ratelimited("ip6t_hook: happy cracking\n");
  43. return NF_ACCEPT;
  44. }
  45. #endif
  46. /* save source/dest address, mark, hoplimit, flowlabel, priority, */
  47. memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
  48. memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
  49. mark = skb->mark;
  50. hop_limit = ipv6_hdr(skb)->hop_limit;
  51. /* flowlabel and prio (includes version, which shouldn't change either */
  52. flowlabel = *((u_int32_t *)ipv6_hdr(skb));
  53. ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
  54. if (ret != NF_DROP && ret != NF_STOLEN &&
  55. (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
  56. !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
  57. skb->mark != mark ||
  58. ipv6_hdr(skb)->hop_limit != hop_limit ||
  59. flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
  60. err = ip6_route_me_harder(state->net, skb);
  61. if (err < 0)
  62. ret = NF_DROP_ERR(err);
  63. }
  64. return ret;
  65. }
  66. /* The work comes in here from netfilter.c. */
  67. static unsigned int
  68. ip6table_mangle_hook(void *priv, struct sk_buff *skb,
  69. const struct nf_hook_state *state)
  70. {
  71. if (state->hook == NF_INET_LOCAL_OUT)
  72. return ip6t_mangle_out(skb, state);
  73. if (state->hook == NF_INET_POST_ROUTING)
  74. return ip6t_do_table(skb, state,
  75. state->net->ipv6.ip6table_mangle);
  76. /* INPUT/FORWARD */
  77. return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
  78. }
  79. static struct nf_hook_ops *mangle_ops __read_mostly;
  80. static int __net_init ip6table_mangle_net_init(struct net *net)
  81. {
  82. struct ip6t_replace *repl;
  83. repl = ip6t_alloc_initial_table(&packet_mangler);
  84. if (repl == NULL)
  85. return -ENOMEM;
  86. net->ipv6.ip6table_mangle =
  87. ip6t_register_table(net, &packet_mangler, repl);
  88. kfree(repl);
  89. return PTR_ERR_OR_ZERO(net->ipv6.ip6table_mangle);
  90. }
  91. static void __net_exit ip6table_mangle_net_exit(struct net *net)
  92. {
  93. ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
  94. }
  95. static struct pernet_operations ip6table_mangle_net_ops = {
  96. .init = ip6table_mangle_net_init,
  97. .exit = ip6table_mangle_net_exit,
  98. };
  99. static int __init ip6table_mangle_init(void)
  100. {
  101. int ret;
  102. ret = register_pernet_subsys(&ip6table_mangle_net_ops);
  103. if (ret < 0)
  104. return ret;
  105. /* Register hooks */
  106. mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
  107. if (IS_ERR(mangle_ops)) {
  108. ret = PTR_ERR(mangle_ops);
  109. goto cleanup_table;
  110. }
  111. return ret;
  112. cleanup_table:
  113. unregister_pernet_subsys(&ip6table_mangle_net_ops);
  114. return ret;
  115. }
  116. static void __exit ip6table_mangle_fini(void)
  117. {
  118. xt_hook_unlink(&packet_mangler, mangle_ops);
  119. unregister_pernet_subsys(&ip6table_mangle_net_ops);
  120. }
  121. module_init(ip6table_mangle_init);
  122. module_exit(ip6table_mangle_fini);