blob: 4a7e5ffa51083112fa3927cfe0c5f7d36cd60235 [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>
Pablo Neira Ayusoa2636532015-06-17 10:28:27 -050011#include <linux/netfilter.h>
Vlad Yasevich3c73a032012-11-15 08:49:20 +000012
Hannes Frederic Sowa5a352dd2015-03-25 17:07:45 +010013static u32 __ipv6_select_ident(struct net *net, u32 hashrnd,
Martin KaFai Laufd0273d2015-05-22 20:55:57 -070014 const struct in6_addr *dst,
15 const struct in6_addr *src)
Vlad Yasevich0508c072015-02-03 16:36:15 -050016{
17 u32 hash, id;
18
19 hash = __ipv6_addr_jhash(dst, hashrnd);
20 hash = __ipv6_addr_jhash(src, hash);
Hannes Frederic Sowa5a352dd2015-03-25 17:07:45 +010021 hash ^= net_hash_mix(net);
Vlad Yasevich0508c072015-02-03 16:36:15 -050022
23 /* Treat id of 0 as unset and if we get 0 back from ip_idents_reserve,
24 * set the hight order instead thus minimizing possible future
25 * collisions.
26 */
27 id = ip_idents_reserve(hash, 1);
28 if (unlikely(!id))
29 id = 1 << 31;
30
31 return id;
32}
33
Eric Dumazet7f159862015-05-25 16:02:21 -070034__be32 ipv6_select_ident(struct net *net,
35 const struct in6_addr *daddr,
36 const struct in6_addr *saddr)
Vlad Yasevich0508c072015-02-03 16:36:15 -050037{
38 static u32 ip6_idents_hashrnd __read_mostly;
39 u32 id;
40
41 net_get_random_once(&ip6_idents_hashrnd, sizeof(ip6_idents_hashrnd));
42
Martin KaFai Laufd0273d2015-05-22 20:55:57 -070043 id = __ipv6_select_ident(net, ip6_idents_hashrnd, daddr, saddr);
Martin KaFai Lau286c2342015-05-22 20:55:56 -070044 return htonl(id);
Vlad Yasevich0508c072015-02-03 16:36:15 -050045}
46EXPORT_SYMBOL(ipv6_select_ident);
47
Vlad Yasevich3c73a032012-11-15 08:49:20 +000048int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
49{
Sabrina Dubroca6399f1f2017-07-19 22:28:55 +020050 unsigned int offset = sizeof(struct ipv6hdr);
Simon Horman29a3cad2013-05-28 20:34:26 +000051 unsigned int packet_len = skb_tail_pointer(skb) -
52 skb_network_header(skb);
Vlad Yasevich3c73a032012-11-15 08:49:20 +000053 int found_rhdr = 0;
54 *nexthdr = &ipv6_hdr(skb)->nexthdr;
55
Craig Gallek24234962017-05-16 14:36:23 -040056 while (offset <= packet_len) {
57 struct ipv6_opt_hdr *exthdr;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000058
59 switch (**nexthdr) {
60
61 case NEXTHDR_HOP:
62 break;
63 case NEXTHDR_ROUTING:
64 found_rhdr = 1;
65 break;
66 case NEXTHDR_DEST:
67#if IS_ENABLED(CONFIG_IPV6_MIP6)
68 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
69 break;
70#endif
71 if (found_rhdr)
72 return offset;
73 break;
Ian Morris67ba4152014-08-24 21:53:10 +010074 default:
Vlad Yasevich3c73a032012-11-15 08:49:20 +000075 return offset;
76 }
77
Craig Gallek24234962017-05-16 14:36:23 -040078 if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
79 return -EINVAL;
80
Vlad Yasevich3c73a032012-11-15 08:49:20 +000081 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
82 offset);
Stefano Brivio3de33e12017-08-18 14:40:53 +020083 offset += ipv6_optlen(exthdr);
84 if (offset > IPV6_MAXPLEN)
Sabrina Dubroca6399f1f2017-07-19 22:28:55 +020085 return -EINVAL;
Craig Gallek24234962017-05-16 14:36:23 -040086 *nexthdr = &exthdr->nexthdr;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000087 }
88
Craig Gallek24234962017-05-16 14:36:23 -040089 return -EINVAL;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000090}
91EXPORT_SYMBOL(ip6_find_1stfragopt);
Cong Wang3ce9b352013-08-31 13:44:28 +080092
93#if IS_ENABLED(CONFIG_IPV6)
94int 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
101 rcu_read_lock();
102 idev = __in6_dev_get(dev);
103 if (idev)
104 hoplimit = idev->cnf.hop_limit;
105 else
106 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
107 rcu_read_unlock();
108 }
109 return hoplimit;
110}
111EXPORT_SYMBOL(ip6_dst_hoplimit);
112#endif
Cong Wang788787b2013-08-31 13:44:29 +0800113
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500114int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Cong Wang788787b2013-08-31 13:44:29 +0800115{
116 int len;
117
118 len = skb->len - sizeof(struct ipv6hdr);
119 if (len > IPV6_MAXPLEN)
120 len = 0;
121 ipv6_hdr(skb)->payload_len = htons(len);
huizhangf6c20c52014-06-09 12:37:25 +0800122 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
Cong Wang788787b2013-08-31 13:44:29 +0800123
David Aherna8e3e1a2016-09-10 12:09:53 -0700124 /* if egress device is enslaved to an L3 master device pass the
125 * skb to its handler for processing
126 */
127 skb = l3mdev_ip6_out(sk, skb);
128 if (unlikely(!skb))
129 return 0;
130
Eli Cooperb4e479a92016-12-01 10:05:11 +0800131 skb->protocol = htons(ETH_P_IPV6);
132
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500133 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
134 net, sk, skb, NULL, skb_dst(skb)->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500135 dst_output);
Cong Wang788787b2013-08-31 13:44:29 +0800136}
137EXPORT_SYMBOL_GPL(__ip6_local_out);
138
Eric W. Biederman33224b12015-10-07 16:48:46 -0500139int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Cong Wang788787b2013-08-31 13:44:29 +0800140{
141 int err;
142
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500143 err = __ip6_local_out(net, sk, skb);
Cong Wang788787b2013-08-31 13:44:29 +0800144 if (likely(err == 1))
Eric W. Biederman13206b62015-10-07 16:48:35 -0500145 err = dst_output(net, sk, skb);
Cong Wang788787b2013-08-31 13:44:29 +0800146
147 return err;
148}
149EXPORT_SYMBOL_GPL(ip6_local_out);