blob: 54520a0bd5e3b5feac3d4bc4221fac77ab990031 [file] [log] [blame]
Vlad Yasevich3c73a032012-11-15 08:49:20 +00001/*
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 Hutchings5188cd42014-10-30 18:27:17 +00006#include <net/ip.h>
Vlad Yasevich3c73a032012-11-15 08:49:20 +00007#include <net/ipv6.h>
8#include <net/ip6_fib.h>
Cong Wang3ce9b352013-08-31 13:44:28 +08009#include <net/addrconf.h>
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +020010#include <net/secure_seq.h>
Vlad Yasevich3c73a032012-11-15 08:49:20 +000011
Vlad Yasevich0508c072015-02-03 16:36:15 -050012u32 __ipv6_select_ident(u32 hashrnd, struct in6_addr *dst, struct in6_addr *src)
13{
14 u32 hash, id;
15
16 hash = __ipv6_addr_jhash(dst, hashrnd);
17 hash = __ipv6_addr_jhash(src, hash);
18
19 /* Treat id of 0 as unset and if we get 0 back from ip_idents_reserve,
20 * set the hight order instead thus minimizing possible future
21 * collisions.
22 */
23 id = ip_idents_reserve(hash, 1);
24 if (unlikely(!id))
25 id = 1 << 31;
26
27 return id;
28}
29
Ben Hutchings5188cd42014-10-30 18:27:17 +000030/* This function exists only for tap drivers that must support broken
31 * clients requesting UFO without specifying an IPv6 fragment ID.
32 *
33 * This is similar to ipv6_select_ident() but we use an independent hash
34 * seed to limit information leakage.
35 *
36 * The network header must be set before calling this.
37 */
38void ipv6_proxy_select_ident(struct sk_buff *skb)
39{
40 static u32 ip6_proxy_idents_hashrnd __read_mostly;
41 struct in6_addr buf[2];
42 struct in6_addr *addrs;
Vlad Yasevich0508c072015-02-03 16:36:15 -050043 u32 id;
Ben Hutchings5188cd42014-10-30 18:27:17 +000044
45 addrs = skb_header_pointer(skb,
46 skb_network_offset(skb) +
47 offsetof(struct ipv6hdr, saddr),
48 sizeof(buf), buf);
49 if (!addrs)
50 return;
51
52 net_get_random_once(&ip6_proxy_idents_hashrnd,
53 sizeof(ip6_proxy_idents_hashrnd));
54
Vlad Yasevich0508c072015-02-03 16:36:15 -050055 id = __ipv6_select_ident(ip6_proxy_idents_hashrnd,
56 &addrs[1], &addrs[0]);
57 skb_shinfo(skb)->ip6_frag_id = id;
Ben Hutchings5188cd42014-10-30 18:27:17 +000058}
59EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident);
60
Vlad Yasevich0508c072015-02-03 16:36:15 -050061void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
62{
63 static u32 ip6_idents_hashrnd __read_mostly;
64 u32 id;
65
66 net_get_random_once(&ip6_idents_hashrnd, sizeof(ip6_idents_hashrnd));
67
68 id = __ipv6_select_ident(ip6_idents_hashrnd, &rt->rt6i_dst.addr,
69 &rt->rt6i_src.addr);
70 fhdr->identification = htonl(id);
71}
72EXPORT_SYMBOL(ipv6_select_ident);
73
Vlad Yasevich3c73a032012-11-15 08:49:20 +000074int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
75{
76 u16 offset = sizeof(struct ipv6hdr);
77 struct ipv6_opt_hdr *exthdr =
78 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
Simon Horman29a3cad2013-05-28 20:34:26 +000079 unsigned int packet_len = skb_tail_pointer(skb) -
80 skb_network_header(skb);
Vlad Yasevich3c73a032012-11-15 08:49:20 +000081 int found_rhdr = 0;
82 *nexthdr = &ipv6_hdr(skb)->nexthdr;
83
84 while (offset + 1 <= packet_len) {
85
86 switch (**nexthdr) {
87
88 case NEXTHDR_HOP:
89 break;
90 case NEXTHDR_ROUTING:
91 found_rhdr = 1;
92 break;
93 case NEXTHDR_DEST:
94#if IS_ENABLED(CONFIG_IPV6_MIP6)
95 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
96 break;
97#endif
98 if (found_rhdr)
99 return offset;
100 break;
Ian Morris67ba4152014-08-24 21:53:10 +0100101 default:
Vlad Yasevich3c73a032012-11-15 08:49:20 +0000102 return offset;
103 }
104
105 offset += ipv6_optlen(exthdr);
106 *nexthdr = &exthdr->nexthdr;
107 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
108 offset);
109 }
110
111 return offset;
112}
113EXPORT_SYMBOL(ip6_find_1stfragopt);
Cong Wang3ce9b352013-08-31 13:44:28 +0800114
115#if IS_ENABLED(CONFIG_IPV6)
116int ip6_dst_hoplimit(struct dst_entry *dst)
117{
118 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
119 if (hoplimit == 0) {
120 struct net_device *dev = dst->dev;
121 struct inet6_dev *idev;
122
123 rcu_read_lock();
124 idev = __in6_dev_get(dev);
125 if (idev)
126 hoplimit = idev->cnf.hop_limit;
127 else
128 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
129 rcu_read_unlock();
130 }
131 return hoplimit;
132}
133EXPORT_SYMBOL(ip6_dst_hoplimit);
134#endif
Cong Wang788787b2013-08-31 13:44:29 +0800135
136int __ip6_local_out(struct sk_buff *skb)
137{
138 int len;
139
140 len = skb->len - sizeof(struct ipv6hdr);
141 if (len > IPV6_MAXPLEN)
142 len = 0;
143 ipv6_hdr(skb)->payload_len = htons(len);
huizhangf6c20c52014-06-09 12:37:25 +0800144 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
Cong Wang788787b2013-08-31 13:44:29 +0800145
146 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
147 skb_dst(skb)->dev, dst_output);
148}
149EXPORT_SYMBOL_GPL(__ip6_local_out);
150
151int ip6_local_out(struct sk_buff *skb)
152{
153 int err;
154
155 err = __ip6_local_out(skb);
156 if (likely(err == 1))
157 err = dst_output(skb);
158
159 return err;
160}
161EXPORT_SYMBOL_GPL(ip6_local_out);