fou6.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/errno.h>
  4. #include <linux/socket.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/ip.h>
  7. #include <linux/udp.h>
  8. #include <linux/icmpv6.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <net/fou.h>
  12. #include <net/ip.h>
  13. #include <net/ip6_tunnel.h>
  14. #include <net/ip6_checksum.h>
  15. #include <net/protocol.h>
  16. #include <net/udp.h>
  17. #include <net/udp_tunnel.h>
  18. #if IS_ENABLED(CONFIG_IPV6_FOU_TUNNEL)
  19. static void fou6_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
  20. struct flowi6 *fl6, u8 *protocol, __be16 sport)
  21. {
  22. struct udphdr *uh;
  23. skb_push(skb, sizeof(struct udphdr));
  24. skb_reset_transport_header(skb);
  25. uh = udp_hdr(skb);
  26. uh->dest = e->dport;
  27. uh->source = sport;
  28. uh->len = htons(skb->len);
  29. udp6_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM6), skb,
  30. &fl6->saddr, &fl6->daddr, skb->len);
  31. *protocol = IPPROTO_UDP;
  32. }
  33. static int fou6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  34. u8 *protocol, struct flowi6 *fl6)
  35. {
  36. __be16 sport;
  37. int err;
  38. int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
  39. SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  40. err = __fou_build_header(skb, e, protocol, &sport, type);
  41. if (err)
  42. return err;
  43. fou6_build_udp(skb, e, fl6, protocol, sport);
  44. return 0;
  45. }
  46. static int gue6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  47. u8 *protocol, struct flowi6 *fl6)
  48. {
  49. __be16 sport;
  50. int err;
  51. int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
  52. SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  53. err = __gue_build_header(skb, e, protocol, &sport, type);
  54. if (err)
  55. return err;
  56. fou6_build_udp(skb, e, fl6, protocol, sport);
  57. return 0;
  58. }
  59. static int gue6_err_proto_handler(int proto, struct sk_buff *skb,
  60. struct inet6_skb_parm *opt,
  61. u8 type, u8 code, int offset, __be32 info)
  62. {
  63. const struct inet6_protocol *ipprot;
  64. ipprot = rcu_dereference(inet6_protos[proto]);
  65. if (ipprot && ipprot->err_handler) {
  66. if (!ipprot->err_handler(skb, opt, type, code, offset, info))
  67. return 0;
  68. }
  69. return -ENOENT;
  70. }
  71. static int gue6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  72. u8 type, u8 code, int offset, __be32 info)
  73. {
  74. int transport_offset = skb_transport_offset(skb);
  75. struct guehdr *guehdr;
  76. size_t len, optlen;
  77. int ret;
  78. len = sizeof(struct udphdr) + sizeof(struct guehdr);
  79. if (!pskb_may_pull(skb, transport_offset + len))
  80. return -EINVAL;
  81. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  82. switch (guehdr->version) {
  83. case 0: /* Full GUE header present */
  84. break;
  85. case 1: {
  86. /* Direct encasulation of IPv4 or IPv6 */
  87. skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
  88. switch (((struct iphdr *)guehdr)->version) {
  89. case 4:
  90. ret = gue6_err_proto_handler(IPPROTO_IPIP, skb, opt,
  91. type, code, offset, info);
  92. goto out;
  93. case 6:
  94. ret = gue6_err_proto_handler(IPPROTO_IPV6, skb, opt,
  95. type, code, offset, info);
  96. goto out;
  97. default:
  98. ret = -EOPNOTSUPP;
  99. goto out;
  100. }
  101. }
  102. default: /* Undefined version */
  103. return -EOPNOTSUPP;
  104. }
  105. if (guehdr->control)
  106. return -ENOENT;
  107. optlen = guehdr->hlen << 2;
  108. if (!pskb_may_pull(skb, transport_offset + len + optlen))
  109. return -EINVAL;
  110. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  111. if (validate_gue_flags(guehdr, optlen))
  112. return -EINVAL;
  113. /* Handling exceptions for direct UDP encapsulation in GUE would lead to
  114. * recursion. Besides, this kind of encapsulation can't even be
  115. * configured currently. Discard this.
  116. */
  117. if (guehdr->proto_ctype == IPPROTO_UDP ||
  118. guehdr->proto_ctype == IPPROTO_UDPLITE)
  119. return -EOPNOTSUPP;
  120. skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
  121. ret = gue6_err_proto_handler(guehdr->proto_ctype, skb,
  122. opt, type, code, offset, info);
  123. out:
  124. skb_set_transport_header(skb, transport_offset);
  125. return ret;
  126. }
  127. static const struct ip6_tnl_encap_ops fou_ip6tun_ops = {
  128. .encap_hlen = fou_encap_hlen,
  129. .build_header = fou6_build_header,
  130. .err_handler = gue6_err,
  131. };
  132. static const struct ip6_tnl_encap_ops gue_ip6tun_ops = {
  133. .encap_hlen = gue_encap_hlen,
  134. .build_header = gue6_build_header,
  135. .err_handler = gue6_err,
  136. };
  137. static int ip6_tnl_encap_add_fou_ops(void)
  138. {
  139. int ret;
  140. ret = ip6_tnl_encap_add_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
  141. if (ret < 0) {
  142. pr_err("can't add fou6 ops\n");
  143. return ret;
  144. }
  145. ret = ip6_tnl_encap_add_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
  146. if (ret < 0) {
  147. pr_err("can't add gue6 ops\n");
  148. ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static void ip6_tnl_encap_del_fou_ops(void)
  154. {
  155. ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
  156. ip6_tnl_encap_del_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
  157. }
  158. #else
  159. static int ip6_tnl_encap_add_fou_ops(void)
  160. {
  161. return 0;
  162. }
  163. static void ip6_tnl_encap_del_fou_ops(void)
  164. {
  165. }
  166. #endif
  167. static int __init fou6_init(void)
  168. {
  169. int ret;
  170. ret = ip6_tnl_encap_add_fou_ops();
  171. return ret;
  172. }
  173. static void __exit fou6_fini(void)
  174. {
  175. ip6_tnl_encap_del_fou_ops();
  176. }
  177. module_init(fou6_init);
  178. module_exit(fou6_fini);
  179. MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
  180. MODULE_LICENSE("GPL");
  181. MODULE_DESCRIPTION("Foo over UDP (IPv6)");