xfrm4_tunnel.c 2.8 KB

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