output_core.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IPv6 library code, needed by static components when full IPv6 support is
  4. * not configured or static. These functions are needed by GSO/GRO implementation.
  5. */
  6. #include <linux/export.h>
  7. #include <net/ip.h>
  8. #include <net/ipv6.h>
  9. #include <net/ip6_fib.h>
  10. #include <net/addrconf.h>
  11. #include <net/secure_seq.h>
  12. #include <linux/netfilter.h>
  13. static u32 __ipv6_select_ident(struct net *net,
  14. const struct in6_addr *dst,
  15. const struct in6_addr *src)
  16. {
  17. return get_random_u32_above(0);
  18. }
  19. /* This function exists only for tap drivers that must support broken
  20. * clients requesting UFO without specifying an IPv6 fragment ID.
  21. *
  22. * This is similar to ipv6_select_ident() but we use an independent hash
  23. * seed to limit information leakage.
  24. *
  25. * The network header must be set before calling this.
  26. */
  27. __be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb)
  28. {
  29. struct in6_addr buf[2];
  30. struct in6_addr *addrs;
  31. u32 id;
  32. addrs = skb_header_pointer(skb,
  33. skb_network_offset(skb) +
  34. offsetof(struct ipv6hdr, saddr),
  35. sizeof(buf), buf);
  36. if (!addrs)
  37. return 0;
  38. id = __ipv6_select_ident(net, &addrs[1], &addrs[0]);
  39. return htonl(id);
  40. }
  41. EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident);
  42. __be32 ipv6_select_ident(struct net *net,
  43. const struct in6_addr *daddr,
  44. const struct in6_addr *saddr)
  45. {
  46. u32 id;
  47. id = __ipv6_select_ident(net, daddr, saddr);
  48. return htonl(id);
  49. }
  50. EXPORT_SYMBOL(ipv6_select_ident);
  51. int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
  52. {
  53. unsigned int offset = sizeof(struct ipv6hdr);
  54. unsigned int packet_len = skb_tail_pointer(skb) -
  55. skb_network_header(skb);
  56. int found_rhdr = 0;
  57. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  58. while (offset <= packet_len) {
  59. struct ipv6_opt_hdr *exthdr;
  60. switch (**nexthdr) {
  61. case NEXTHDR_HOP:
  62. break;
  63. case NEXTHDR_ROUTING:
  64. found_rhdr = 1;
  65. break;
  66. case NEXTHDR_DEST:
  67. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  68. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  69. break;
  70. #endif
  71. if (found_rhdr)
  72. return offset;
  73. break;
  74. default:
  75. return offset;
  76. }
  77. if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
  78. return -EINVAL;
  79. exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
  80. offset);
  81. offset += ipv6_optlen(exthdr);
  82. if (offset > IPV6_MAXPLEN)
  83. return -EINVAL;
  84. *nexthdr = &exthdr->nexthdr;
  85. }
  86. return -EINVAL;
  87. }
  88. EXPORT_SYMBOL(ip6_find_1stfragopt);
  89. #if IS_ENABLED(CONFIG_IPV6)
  90. int ip6_dst_hoplimit(struct dst_entry *dst)
  91. {
  92. int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
  93. if (hoplimit == 0) {
  94. struct net_device *dev = dst->dev;
  95. struct inet6_dev *idev;
  96. rcu_read_lock();
  97. idev = __in6_dev_get(dev);
  98. if (idev)
  99. hoplimit = READ_ONCE(idev->cnf.hop_limit);
  100. else
  101. hoplimit = READ_ONCE(dev_net(dev)->ipv6.devconf_all->hop_limit);
  102. rcu_read_unlock();
  103. }
  104. return hoplimit;
  105. }
  106. EXPORT_SYMBOL(ip6_dst_hoplimit);
  107. #endif
  108. int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  109. {
  110. int len;
  111. len = skb->len - sizeof(struct ipv6hdr);
  112. if (len > IPV6_MAXPLEN)
  113. len = 0;
  114. ipv6_hdr(skb)->payload_len = htons(len);
  115. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  116. /* if egress device is enslaved to an L3 master device pass the
  117. * skb to its handler for processing
  118. */
  119. skb = l3mdev_ip6_out(sk, skb);
  120. if (unlikely(!skb))
  121. return 0;
  122. skb->protocol = htons(ETH_P_IPV6);
  123. return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
  124. net, sk, skb, NULL, skb_dst(skb)->dev,
  125. dst_output);
  126. }
  127. EXPORT_SYMBOL_GPL(__ip6_local_out);
  128. int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  129. {
  130. int err;
  131. err = __ip6_local_out(net, sk, skb);
  132. if (likely(err == 1))
  133. err = dst_output(net, sk, skb);
  134. return err;
  135. }
  136. EXPORT_SYMBOL_GPL(ip6_local_out);