xfrm6_output.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
  4. * Copyright (C) 2002 USAGI/WIDE Project
  5. * Copyright (c) 2004 Herbert Xu <[email protected]>
  6. */
  7. #include <linux/if_ether.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/icmpv6.h>
  12. #include <linux/netfilter_ipv6.h>
  13. #include <net/dst.h>
  14. #include <net/ipv6.h>
  15. #include <net/ip6_route.h>
  16. #include <net/xfrm.h>
  17. void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
  18. {
  19. struct flowi6 fl6;
  20. struct sock *sk = skb->sk;
  21. fl6.flowi6_oif = sk->sk_bound_dev_if;
  22. fl6.daddr = ipv6_hdr(skb)->daddr;
  23. ipv6_local_rxpmtu(sk, &fl6, mtu);
  24. }
  25. void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
  26. {
  27. struct flowi6 fl6;
  28. const struct ipv6hdr *hdr;
  29. struct sock *sk = skb->sk;
  30. hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  31. fl6.fl6_dport = inet_sk(sk)->inet_dport;
  32. fl6.daddr = hdr->daddr;
  33. ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
  34. }
  35. static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  36. {
  37. return xfrm_output(sk, skb);
  38. }
  39. static int xfrm6_noneed_fragment(struct sk_buff *skb)
  40. {
  41. struct frag_hdr *fh;
  42. u8 prevhdr = ipv6_hdr(skb)->nexthdr;
  43. if (prevhdr != NEXTHDR_FRAGMENT)
  44. return 0;
  45. fh = (struct frag_hdr *)(skb->data + sizeof(struct ipv6hdr));
  46. if (fh->nexthdr == NEXTHDR_ESP || fh->nexthdr == NEXTHDR_AUTH)
  47. return 1;
  48. return 0;
  49. }
  50. static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  51. {
  52. struct dst_entry *dst = skb_dst(skb);
  53. struct xfrm_state *x = dst->xfrm;
  54. unsigned int mtu;
  55. bool toobig;
  56. #ifdef CONFIG_NETFILTER
  57. if (!x) {
  58. IP6CB(skb)->flags |= IP6SKB_REROUTED;
  59. return dst_output(net, sk, skb);
  60. }
  61. #endif
  62. if (x->props.mode != XFRM_MODE_TUNNEL)
  63. goto skip_frag;
  64. if (skb->protocol == htons(ETH_P_IPV6))
  65. mtu = ip6_skb_dst_mtu(skb);
  66. else
  67. mtu = dst_mtu(skb_dst(skb));
  68. toobig = skb->len > mtu && !skb_is_gso(skb);
  69. if (toobig && xfrm6_local_dontfrag(skb->sk)) {
  70. xfrm6_local_rxpmtu(skb, mtu);
  71. kfree_skb(skb);
  72. return -EMSGSIZE;
  73. } else if (toobig && xfrm6_noneed_fragment(skb)) {
  74. skb->ignore_df = 1;
  75. goto skip_frag;
  76. } else if (!skb->ignore_df && toobig && skb->sk) {
  77. xfrm_local_error(skb, mtu);
  78. kfree_skb(skb);
  79. return -EMSGSIZE;
  80. }
  81. if (toobig || dst_allfrag(skb_dst(skb)))
  82. return ip6_fragment(net, sk, skb,
  83. __xfrm6_output_finish);
  84. skip_frag:
  85. return xfrm_output(sk, skb);
  86. }
  87. int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  88. {
  89. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  90. net, sk, skb, skb->dev, skb_dst(skb)->dev,
  91. __xfrm6_output,
  92. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  93. }