output_core.c 4.2 KB

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