blob: b5205311f372bdaaff140d651e4b42b27a3ed805 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Vlad Yasevich3c73a032012-11-15 08:49:20 +00002/*
3 * IPv6 library code, needed by static components when full IPv6 support is
4 * not configured or static. These functions are needed by GSO/GRO implementation.
5 */
6#include <linux/export.h>
Ben Hutchings5188cd42014-10-30 18:27:17 +00007#include <net/ip.h>
Vlad Yasevich3c73a032012-11-15 08:49:20 +00008#include <net/ipv6.h>
9#include <net/ip6_fib.h>
Cong Wang3ce9b352013-08-31 13:44:28 +080010#include <net/addrconf.h>
Hannes Frederic Sowa6dfac5c2014-03-30 18:28:03 +020011#include <net/secure_seq.h>
Pablo Neira Ayusoa2636532015-06-17 10:28:27 -050012#include <linux/netfilter.h>
Vlad Yasevich3c73a032012-11-15 08:49:20 +000013
Eric Dumazetdf453702019-03-27 12:40:33 -070014static u32 __ipv6_select_ident(struct net *net,
Martin KaFai Laufd0273d2015-05-22 20:55:57 -070015 const struct in6_addr *dst,
16 const struct in6_addr *src)
Vlad Yasevich0508c072015-02-03 16:36:15 -050017{
Jason A. Donenfeldd247aab2022-10-09 20:44:02 -060018 return get_random_u32_above(0);
Vlad Yasevich0508c072015-02-03 16:36:15 -050019}
20
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050021/* This function exists only for tap drivers that must support broken
22 * clients requesting UFO without specifying an IPv6 fragment ID.
23 *
24 * This is similar to ipv6_select_ident() but we use an independent hash
25 * seed to limit information leakage.
26 *
27 * The network header must be set before calling this.
28 */
29__be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb)
30{
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050031 struct in6_addr buf[2];
32 struct in6_addr *addrs;
33 u32 id;
34
35 addrs = skb_header_pointer(skb,
36 skb_network_offset(skb) +
37 offsetof(struct ipv6hdr, saddr),
38 sizeof(buf), buf);
39 if (!addrs)
40 return 0;
41
Eric Dumazetdf453702019-03-27 12:40:33 -070042 id = __ipv6_select_ident(net, &addrs[1], &addrs[0]);
Willem de Bruijn0c19f8462017-11-21 10:22:25 -050043 return htonl(id);
44}
45EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident);
46
Eric Dumazet7f159862015-05-25 16:02:21 -070047__be32 ipv6_select_ident(struct net *net,
48 const struct in6_addr *daddr,
49 const struct in6_addr *saddr)
Vlad Yasevich0508c072015-02-03 16:36:15 -050050{
Vlad Yasevich0508c072015-02-03 16:36:15 -050051 u32 id;
52
Eric Dumazetdf453702019-03-27 12:40:33 -070053 id = __ipv6_select_ident(net, daddr, saddr);
Martin KaFai Lau286c2342015-05-22 20:55:56 -070054 return htonl(id);
Vlad Yasevich0508c072015-02-03 16:36:15 -050055}
56EXPORT_SYMBOL(ipv6_select_ident);
57
Vlad Yasevich3c73a032012-11-15 08:49:20 +000058int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
59{
Sabrina Dubroca6399f1f2017-07-19 22:28:55 +020060 unsigned int offset = sizeof(struct ipv6hdr);
Simon Horman29a3cad2013-05-28 20:34:26 +000061 unsigned int packet_len = skb_tail_pointer(skb) -
62 skb_network_header(skb);
Vlad Yasevich3c73a032012-11-15 08:49:20 +000063 int found_rhdr = 0;
64 *nexthdr = &ipv6_hdr(skb)->nexthdr;
65
Craig Gallek24234962017-05-16 14:36:23 -040066 while (offset <= packet_len) {
67 struct ipv6_opt_hdr *exthdr;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000068
69 switch (**nexthdr) {
70
71 case NEXTHDR_HOP:
72 break;
73 case NEXTHDR_ROUTING:
74 found_rhdr = 1;
75 break;
76 case NEXTHDR_DEST:
77#if IS_ENABLED(CONFIG_IPV6_MIP6)
78 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
79 break;
80#endif
81 if (found_rhdr)
82 return offset;
83 break;
Ian Morris67ba4152014-08-24 21:53:10 +010084 default:
Vlad Yasevich3c73a032012-11-15 08:49:20 +000085 return offset;
86 }
87
Craig Gallek24234962017-05-16 14:36:23 -040088 if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
89 return -EINVAL;
90
Vlad Yasevich3c73a032012-11-15 08:49:20 +000091 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
92 offset);
Stefano Brivio3de33e12017-08-18 14:40:53 +020093 offset += ipv6_optlen(exthdr);
94 if (offset > IPV6_MAXPLEN)
Sabrina Dubroca6399f1f2017-07-19 22:28:55 +020095 return -EINVAL;
Craig Gallek24234962017-05-16 14:36:23 -040096 *nexthdr = &exthdr->nexthdr;
Vlad Yasevich3c73a032012-11-15 08:49:20 +000097 }
98
Craig Gallek24234962017-05-16 14:36:23 -040099 return -EINVAL;
Vlad Yasevich3c73a032012-11-15 08:49:20 +0000100}
101EXPORT_SYMBOL(ip6_find_1stfragopt);
Cong Wang3ce9b352013-08-31 13:44:28 +0800102
103#if IS_ENABLED(CONFIG_IPV6)
104int ip6_dst_hoplimit(struct dst_entry *dst)
105{
106 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
107 if (hoplimit == 0) {
108 struct net_device *dev = dst->dev;
109 struct inet6_dev *idev;
110
111 rcu_read_lock();
112 idev = __in6_dev_get(dev);
113 if (idev)
114 hoplimit = idev->cnf.hop_limit;
115 else
116 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
117 rcu_read_unlock();
118 }
119 return hoplimit;
120}
121EXPORT_SYMBOL(ip6_dst_hoplimit);
122#endif
Cong Wang788787b2013-08-31 13:44:29 +0800123
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500124int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Cong Wang788787b2013-08-31 13:44:29 +0800125{
126 int len;
127
128 len = skb->len - sizeof(struct ipv6hdr);
129 if (len > IPV6_MAXPLEN)
130 len = 0;
131 ipv6_hdr(skb)->payload_len = htons(len);
huizhangf6c20c52014-06-09 12:37:25 +0800132 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
Cong Wang788787b2013-08-31 13:44:29 +0800133
David Aherna8e3e1a2016-09-10 12:09:53 -0700134 /* if egress device is enslaved to an L3 master device pass the
135 * skb to its handler for processing
136 */
137 skb = l3mdev_ip6_out(sk, skb);
138 if (unlikely(!skb))
139 return 0;
140
Eli Cooperb4e479a92016-12-01 10:05:11 +0800141 skb->protocol = htons(ETH_P_IPV6);
142
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500143 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
144 net, sk, skb, NULL, skb_dst(skb)->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500145 dst_output);
Cong Wang788787b2013-08-31 13:44:29 +0800146}
147EXPORT_SYMBOL_GPL(__ip6_local_out);
148
Eric W. Biederman33224b12015-10-07 16:48:46 -0500149int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Cong Wang788787b2013-08-31 13:44:29 +0800150{
151 int err;
152
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500153 err = __ip6_local_out(net, sk, skb);
Cong Wang788787b2013-08-31 13:44:29 +0800154 if (likely(err == 1))
Eric W. Biederman13206b62015-10-07 16:48:35 -0500155 err = dst_output(net, sk, skb);
Cong Wang788787b2013-08-31 13:44:29 +0800156
157 return err;
158}
159EXPORT_SYMBOL_GPL(ip6_local_out);