output_core.c 3.7 KB

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