/net/ipv6/netfilter/ip6table_mangle.c

https://github.com/srikard/linux · C · 146 lines · 112 code · 24 blank · 10 comment · 18 complexity · 9663876b0f154c7368ca3b83750f39b6 MD5 · raw file

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