output_core.c 4.2 KB

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