xref: /linux/net/ipv6/output_core.c (revision 04303f8ec14269b0ea2553863553bc7eaadca1f8)
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 
12 u32 __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 
30 /* 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  */
38 void 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;
43 	u32 id;
44 
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 
55 	id = __ipv6_select_ident(ip6_proxy_idents_hashrnd,
56 				 &addrs[1], &addrs[0]);
57 	skb_shinfo(skb)->ip6_frag_id = id;
58 }
59 EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident);
60 
61 void 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 }
72 EXPORT_SYMBOL(ipv6_select_ident);
73 
74 int 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);
79 	unsigned int packet_len = skb_tail_pointer(skb) -
80 		skb_network_header(skb);
81 	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;
101 		default:
102 			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 }
113 EXPORT_SYMBOL(ip6_find_1stfragopt);
114 
115 #if IS_ENABLED(CONFIG_IPV6)
116 int 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 }
133 EXPORT_SYMBOL(ip6_dst_hoplimit);
134 #endif
135 
136 int __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);
144 	IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
145 
146 	return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
147 		       skb_dst(skb)->dev, dst_output);
148 }
149 EXPORT_SYMBOL_GPL(__ip6_local_out);
150 
151 int 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 }
161 EXPORT_SYMBOL_GPL(ip6_local_out);
162