/net/ipv4/xfrm4_tunnel.c

http://github.com/mirrors/linux · C · 117 lines · 94 code · 18 blank · 5 comment · 8 complexity · 3d3046b4a133aa8a04d6379a32dc1a08 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* xfrm4_tunnel.c: Generic IP tunnel transformer.
  3. *
  4. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  5. */
  6. #define pr_fmt(fmt) "IPsec: " fmt
  7. #include <linux/skbuff.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <net/xfrm.h>
  11. #include <net/ip.h>
  12. #include <net/protocol.h>
  13. static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
  14. {
  15. skb_push(skb, -skb_network_offset(skb));
  16. return 0;
  17. }
  18. static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
  19. {
  20. return ip_hdr(skb)->protocol;
  21. }
  22. static int ipip_init_state(struct xfrm_state *x)
  23. {
  24. if (x->props.mode != XFRM_MODE_TUNNEL)
  25. return -EINVAL;
  26. if (x->encap)
  27. return -EINVAL;
  28. x->props.header_len = sizeof(struct iphdr);
  29. return 0;
  30. }
  31. static void ipip_destroy(struct xfrm_state *x)
  32. {
  33. }
  34. static const struct xfrm_type ipip_type = {
  35. .description = "IPIP",
  36. .owner = THIS_MODULE,
  37. .proto = IPPROTO_IPIP,
  38. .init_state = ipip_init_state,
  39. .destructor = ipip_destroy,
  40. .input = ipip_xfrm_rcv,
  41. .output = ipip_output
  42. };
  43. static int xfrm_tunnel_rcv(struct sk_buff *skb)
  44. {
  45. return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
  46. }
  47. static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
  48. {
  49. return -ENOENT;
  50. }
  51. static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = {
  52. .handler = xfrm_tunnel_rcv,
  53. .err_handler = xfrm_tunnel_err,
  54. .priority = 3,
  55. };
  56. #if IS_ENABLED(CONFIG_IPV6)
  57. static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = {
  58. .handler = xfrm_tunnel_rcv,
  59. .err_handler = xfrm_tunnel_err,
  60. .priority = 2,
  61. };
  62. #endif
  63. static int __init ipip_init(void)
  64. {
  65. if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
  66. pr_info("%s: can't add xfrm type\n", __func__);
  67. return -EAGAIN;
  68. }
  69. if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
  70. pr_info("%s: can't add xfrm handler for AF_INET\n", __func__);
  71. xfrm_unregister_type(&ipip_type, AF_INET);
  72. return -EAGAIN;
  73. }
  74. #if IS_ENABLED(CONFIG_IPV6)
  75. if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
  76. pr_info("%s: can't add xfrm handler for AF_INET6\n", __func__);
  77. xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
  78. xfrm_unregister_type(&ipip_type, AF_INET);
  79. return -EAGAIN;
  80. }
  81. #endif
  82. return 0;
  83. }
  84. static void __exit ipip_fini(void)
  85. {
  86. #if IS_ENABLED(CONFIG_IPV6)
  87. if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
  88. pr_info("%s: can't remove xfrm handler for AF_INET6\n",
  89. __func__);
  90. #endif
  91. if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
  92. pr_info("%s: can't remove xfrm handler for AF_INET\n",
  93. __func__);
  94. xfrm_unregister_type(&ipip_type, AF_INET);
  95. }
  96. module_init(ipip_init);
  97. module_exit(ipip_fini);
  98. MODULE_LICENSE("GPL");
  99. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);