Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 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> |
Ben Hutchings | 5188cd4 | 2014-10-30 18:27:17 +0000 | [diff] [blame] | 6 | #include <net/ip.h> |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 7 | #include <net/ipv6.h> |
| 8 | #include <net/ip6_fib.h> |
Cong Wang | 3ce9b35 | 2013-08-31 13:44:28 +0800 | [diff] [blame] | 9 | #include <net/addrconf.h> |
Hannes Frederic Sowa | 6dfac5c | 2014-03-30 18:28:03 +0200 | [diff] [blame] | 10 | #include <net/secure_seq.h> |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 11 | |
Ben Hutchings | 5188cd4 | 2014-10-30 18:27:17 +0000 | [diff] [blame] | 12 | /* This function exists only for tap drivers that must support broken |
| 13 | * clients requesting UFO without specifying an IPv6 fragment ID. |
| 14 | * |
| 15 | * This is similar to ipv6_select_ident() but we use an independent hash |
| 16 | * seed to limit information leakage. |
| 17 | * |
| 18 | * The network header must be set before calling this. |
| 19 | */ |
| 20 | void ipv6_proxy_select_ident(struct sk_buff *skb) |
| 21 | { |
| 22 | static u32 ip6_proxy_idents_hashrnd __read_mostly; |
| 23 | struct in6_addr buf[2]; |
| 24 | struct in6_addr *addrs; |
| 25 | u32 hash, id; |
| 26 | |
| 27 | addrs = skb_header_pointer(skb, |
| 28 | skb_network_offset(skb) + |
| 29 | offsetof(struct ipv6hdr, saddr), |
| 30 | sizeof(buf), buf); |
| 31 | if (!addrs) |
| 32 | return; |
| 33 | |
| 34 | net_get_random_once(&ip6_proxy_idents_hashrnd, |
| 35 | sizeof(ip6_proxy_idents_hashrnd)); |
| 36 | |
| 37 | hash = __ipv6_addr_jhash(&addrs[1], ip6_proxy_idents_hashrnd); |
| 38 | hash = __ipv6_addr_jhash(&addrs[0], hash); |
| 39 | |
| 40 | id = ip_idents_reserve(hash, 1); |
| 41 | skb_shinfo(skb)->ip6_frag_id = htonl(id); |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident); |
| 44 | |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 45 | int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr) |
| 46 | { |
| 47 | u16 offset = sizeof(struct ipv6hdr); |
| 48 | struct ipv6_opt_hdr *exthdr = |
| 49 | (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1); |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 50 | unsigned int packet_len = skb_tail_pointer(skb) - |
| 51 | skb_network_header(skb); |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 52 | int found_rhdr = 0; |
| 53 | *nexthdr = &ipv6_hdr(skb)->nexthdr; |
| 54 | |
| 55 | while (offset + 1 <= packet_len) { |
| 56 | |
| 57 | switch (**nexthdr) { |
| 58 | |
| 59 | case NEXTHDR_HOP: |
| 60 | break; |
| 61 | case NEXTHDR_ROUTING: |
| 62 | found_rhdr = 1; |
| 63 | break; |
| 64 | case NEXTHDR_DEST: |
| 65 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 66 | if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) |
| 67 | break; |
| 68 | #endif |
| 69 | if (found_rhdr) |
| 70 | return offset; |
| 71 | break; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 72 | default: |
Vlad Yasevich | 3c73a03 | 2012-11-15 08:49:20 +0000 | [diff] [blame] | 73 | return offset; |
| 74 | } |
| 75 | |
| 76 | offset += ipv6_optlen(exthdr); |
| 77 | *nexthdr = &exthdr->nexthdr; |
| 78 | exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) + |
| 79 | offset); |
| 80 | } |
| 81 | |
| 82 | return offset; |
| 83 | } |
| 84 | EXPORT_SYMBOL(ip6_find_1stfragopt); |
Cong Wang | 3ce9b35 | 2013-08-31 13:44:28 +0800 | [diff] [blame] | 85 | |
| 86 | #if IS_ENABLED(CONFIG_IPV6) |
| 87 | int ip6_dst_hoplimit(struct dst_entry *dst) |
| 88 | { |
| 89 | int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); |
| 90 | if (hoplimit == 0) { |
| 91 | struct net_device *dev = dst->dev; |
| 92 | struct inet6_dev *idev; |
| 93 | |
| 94 | rcu_read_lock(); |
| 95 | idev = __in6_dev_get(dev); |
| 96 | if (idev) |
| 97 | hoplimit = idev->cnf.hop_limit; |
| 98 | else |
| 99 | hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; |
| 100 | rcu_read_unlock(); |
| 101 | } |
| 102 | return hoplimit; |
| 103 | } |
| 104 | EXPORT_SYMBOL(ip6_dst_hoplimit); |
| 105 | #endif |
Cong Wang | 788787b | 2013-08-31 13:44:29 +0800 | [diff] [blame] | 106 | |
| 107 | int __ip6_local_out(struct sk_buff *skb) |
| 108 | { |
| 109 | int len; |
| 110 | |
| 111 | len = skb->len - sizeof(struct ipv6hdr); |
| 112 | if (len > IPV6_MAXPLEN) |
| 113 | len = 0; |
| 114 | ipv6_hdr(skb)->payload_len = htons(len); |
huizhang | f6c20c5 | 2014-06-09 12:37:25 +0800 | [diff] [blame] | 115 | IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); |
Cong Wang | 788787b | 2013-08-31 13:44:29 +0800 | [diff] [blame] | 116 | |
| 117 | return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, |
| 118 | skb_dst(skb)->dev, dst_output); |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(__ip6_local_out); |
| 121 | |
| 122 | int ip6_local_out(struct sk_buff *skb) |
| 123 | { |
| 124 | int err; |
| 125 | |
| 126 | err = __ip6_local_out(skb); |
| 127 | if (likely(err == 1)) |
| 128 | err = dst_output(skb); |
| 129 | |
| 130 | return err; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(ip6_local_out); |