Blame view

net/ipv6/datagram.c 24.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /*
   *	common UDP/RAW code
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
3
   *	Linux INET6 implementation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
   *
   *	Authors:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
6
   *	Pedro Roque		<roque@di.fc.ul.pt>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
12
   *	This program is free software; you can redistribute it and/or
   *      modify it under the terms of the GNU General Public License
   *      as published by the Free Software Foundation; either version
   *      2 of the License, or (at your option) any later version.
   */
4fc268d24   Randy Dunlap   [PATCH] capable/c...
13
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
  #include <linux/errno.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
19
20
21
22
  #include <linux/interrupt.h>
  #include <linux/socket.h>
  #include <linux/sockios.h>
  #include <linux/in6.h>
  #include <linux/ipv6.h>
  #include <linux/route.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/slab.h>
a495f8364   Chris Elston   ipv6: Export ipv6...
24
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
28
29
30
  
  #include <net/ipv6.h>
  #include <net/ndisc.h>
  #include <net/addrconf.h>
  #include <net/transp_v6.h>
  #include <net/ip6_route.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
31
  #include <net/tcp_states.h>
e7219858a   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Use ipv6_ge...
32
  #include <net/dsfield.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
  
  #include <linux/errqueue.h>
  #include <asm/uaccess.h>
a50feda54   Eric Dumazet   ipv6: bool/const ...
36
  static bool ipv6_mapped_addr_any(const struct in6_addr *a)
c15fea2d8   Max Matveev   ipv6: check for I...
37
  {
a50feda54   Eric Dumazet   ipv6: bool/const ...
38
  	return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
c15fea2d8   Max Matveev   ipv6: check for I...
39
  }
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
  {
  	struct inet_sock *inet = inet_sk(sk);
  	struct ipv6_pinfo *np = inet6_sk(sk);
  
  	memset(fl6, 0, sizeof(*fl6));
  	fl6->flowi6_proto = sk->sk_protocol;
  	fl6->daddr = sk->sk_v6_daddr;
  	fl6->saddr = np->saddr;
  	fl6->flowi6_oif = sk->sk_bound_dev_if;
  	fl6->flowi6_mark = sk->sk_mark;
  	fl6->fl6_dport = inet->inet_dport;
  	fl6->fl6_sport = inet->inet_sport;
  	fl6->flowlabel = np->flow_label;
  
  	if (!fl6->flowi6_oif)
  		fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
  
  	if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
  		fl6->flowi6_oif = np->mcast_oif;
  
  	security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
  }
33c162a98   Martin KaFai Lau   ipv6: datagram: U...
63
  int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  {
  	struct ip6_flowlabel *flowlabel = NULL;
  	struct in6_addr *final_p, final;
  	struct ipv6_txoptions *opt;
  	struct dst_entry *dst;
  	struct inet_sock *inet = inet_sk(sk);
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct flowi6 fl6;
  	int err = 0;
  
  	if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
  		flowlabel = fl6_sock_lookup(sk, np->flow_label);
  		if (!flowlabel)
  			return -EINVAL;
  	}
  	ip6_datagram_flow_key_init(&fl6, sk);
  
  	rcu_read_lock();
  	opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
  	final_p = fl6_update_dst(&fl6, opt, &final);
  	rcu_read_unlock();
  
  	dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  	if (IS_ERR(dst)) {
  		err = PTR_ERR(dst);
  		goto out;
  	}
33c162a98   Martin KaFai Lau   ipv6: datagram: U...
91
92
93
  	if (fix_sk_saddr) {
  		if (ipv6_addr_any(&np->saddr))
  			np->saddr = fl6.saddr;
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
94

33c162a98   Martin KaFai Lau   ipv6: datagram: U...
95
96
97
98
99
100
  		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
  			sk->sk_v6_rcv_saddr = fl6.saddr;
  			inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  			if (sk->sk_prot->rehash)
  				sk->sk_prot->rehash(sk);
  		}
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	}
  
  	ip6_dst_store(sk, dst,
  		      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
  		      &sk->sk_v6_daddr : NULL,
  #ifdef CONFIG_IPV6_SUBTREES
  		      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
  		      &np->saddr :
  #endif
  		      NULL);
  
  out:
  	fl6_sock_release(flowlabel);
  	return err;
  }
e646b657f   Martin KaFai Lau   ipv6: udp: Do a r...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  void ip6_datagram_release_cb(struct sock *sk)
  {
  	struct dst_entry *dst;
  
  	if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  		return;
  
  	rcu_read_lock();
  	dst = __sk_dst_get(sk);
  	if (!dst || !dst->obsolete ||
  	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
  		rcu_read_unlock();
  		return;
  	}
  	rcu_read_unlock();
  
  	ip6_datagram_dst_update(sk, false);
  }
  EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
0382a25af   Guillaume Nault   l2tp: lock socket...
135
136
  int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
  			   int addr_len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
  {
  	struct sockaddr_in6	*usin = (struct sockaddr_in6 *) uaddr;
67ba4152e   Ian Morris   ipv6: White-space...
139
140
  	struct inet_sock	*inet = inet_sk(sk);
  	struct ipv6_pinfo	*np = inet6_sk(sk);
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
141
  	struct in6_addr		*daddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
143
  	int			addr_type;
  	int			err;
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
144
  	__be32			fl6_flowlabel = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
147
148
  
  	if (usin->sin6_family == AF_INET) {
  		if (__ipv6_only_sock(sk))
  			return -EAFNOSUPPORT;
03645a11a   Eric Dumazet   ipv6: lock socket...
149
  		err = __ip4_datagram_connect(sk, uaddr, addr_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
  		goto ipv4_connected;
  	}
  
  	if (addr_len < SIN6_LEN_RFC2133)
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
154
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155

1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
156
157
  	if (usin->sin6_family != AF_INET6)
  		return -EAFNOSUPPORT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158

7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
159
  	if (np->sndflow)
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
160
  		fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  
  	addr_type = ipv6_addr_type(&usin->sin6_addr);
  
  	if (addr_type == IPV6_ADDR_ANY) {
  		/*
  		 *	connect to self
  		 */
  		usin->sin6_addr.s6_addr[15] = 0x01;
  	}
  
  	daddr = &usin->sin6_addr;
  
  	if (addr_type == IPV6_ADDR_MAPPED) {
  		struct sockaddr_in sin;
  
  		if (__ipv6_only_sock(sk)) {
  			err = -ENETUNREACH;
  			goto out;
  		}
  		sin.sin_family = AF_INET;
  		sin.sin_addr.s_addr = daddr->s6_addr32[3];
  		sin.sin_port = usin->sin6_port;
03645a11a   Eric Dumazet   ipv6: lock socket...
183
184
185
  		err = __ip4_datagram_connect(sk,
  					     (struct sockaddr *) &sin,
  					     sizeof(sin));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
187
188
189
  
  ipv4_connected:
  		if (err)
  			goto out;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
190

efe4208f4   Eric Dumazet   ipv6: make lookup...
191
  		ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192

c15fea2d8   Max Matveev   ipv6: check for I...
193
194
  		if (ipv6_addr_any(&np->saddr) ||
  		    ipv6_mapped_addr_any(&np->saddr))
c720c7e83   Eric Dumazet   inet: rename some...
195
  			ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
b301e82cf   Brian Haley   IPv6: use ipv6_ad...
196

efe4208f4   Eric Dumazet   ipv6: make lookup...
197
198
  		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
  		    ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
c720c7e83   Eric Dumazet   inet: rename some...
199
  			ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
efe4208f4   Eric Dumazet   ipv6: make lookup...
200
  					       &sk->sk_v6_rcv_saddr);
719f83585   Eric Dumazet   udp: add rehash o...
201
202
203
  			if (sk->sk_prot->rehash)
  				sk->sk_prot->rehash(sk);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
  		goto out;
  	}
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
207
  	if (__ipv6_addr_needs_scope_id(addr_type)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
  		if (addr_len >= sizeof(struct sockaddr_in6) &&
  		    usin->sin6_scope_id) {
  			if (sk->sk_bound_dev_if &&
  			    sk->sk_bound_dev_if != usin->sin6_scope_id) {
  				err = -EINVAL;
  				goto out;
  			}
  			sk->sk_bound_dev_if = usin->sin6_scope_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  		}
1ac4f0088   Brian Haley   [IPV6]: IPV6_MULT...
217
218
  		if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
  			sk->sk_bound_dev_if = np->mcast_oif;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219
220
221
222
223
224
  		/* Connect to link-local address requires an interface */
  		if (!sk->sk_bound_dev_if) {
  			err = -EINVAL;
  			goto out;
  		}
  	}
efe4208f4   Eric Dumazet   ipv6: make lookup...
225
  	sk->sk_v6_daddr = *daddr;
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
226
  	np->flow_label = fl6_flowlabel;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227

c720c7e83   Eric Dumazet   inet: rename some...
228
  	inet->inet_dport = usin->sin6_port;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
  
  	/*
  	 *	Check for a route to destination an obtain the
  	 *	destination cache for it.
  	 */
33c162a98   Martin KaFai Lau   ipv6: datagram: U...
234
  	err = ip6_datagram_dst_update(sk, true);
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
235
  	if (err)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
  
  	sk->sk_state = TCP_ESTABLISHED;
877d1f629   Tom Herbert   net: Set sk_txhas...
239
  	sk_set_txhash(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
  	return err;
  }
0382a25af   Guillaume Nault   l2tp: lock socket...
243
  EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
03645a11a   Eric Dumazet   ipv6: lock socket...
244
245
246
247
248
249
250
251
252
253
  
  int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  {
  	int res;
  
  	lock_sock(sk);
  	res = __ip6_datagram_connect(sk, uaddr, addr_len);
  	release_sock(sk);
  	return res;
  }
a495f8364   Chris Elston   ipv6: Export ipv6...
254
  EXPORT_SYMBOL_GPL(ip6_datagram_connect);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255

82b276cd2   Hannes Frederic Sowa   ipv6: protect pro...
256
257
258
259
260
261
262
263
264
  int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
  				 int addr_len)
  {
  	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
  	if (sin6->sin6_family != AF_INET6)
  		return -EAFNOSUPPORT;
  	return ip6_datagram_connect(sk, uaddr, addr_len);
  }
  EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
265
  void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
e69a4adc6   Al Viro   [IPV6]: Misc endi...
266
  		     __be16 port, u32 info, u8 *payload)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
  {
  	struct ipv6_pinfo *np  = inet6_sk(sk);
cc70ab261   Arnaldo Carvalho de Melo   [ICMP6]: Introduc...
269
  	struct icmp6hdr *icmph = icmp6_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
271
272
273
274
275
276
277
  	struct sock_exterr_skb *serr;
  
  	if (!np->recverr)
  		return;
  
  	skb = skb_clone(skb, GFP_ATOMIC);
  	if (!skb)
  		return;
d40a4de0b   Brian Haley   IPv6: fix IPV6_RE...
278
  	skb->protocol = htons(ETH_P_IPV6);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
  	serr = SKB_EXT_ERR(skb);
  	serr->ee.ee_errno = err;
  	serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
282
  	serr->ee.ee_type = icmph->icmp6_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
  	serr->ee.ee_code = icmph->icmp6_code;
  	serr->ee.ee_pad = 0;
  	serr->ee.ee_info = info;
  	serr->ee.ee_data = 0;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
287
288
  	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
  				  skb_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  	serr->port = port;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
  	__skb_pull(skb, payload - skb->data);
bd82393ca   Arnaldo Carvalho de Melo   [SK_BUFF]: More s...
291
  	skb_reset_transport_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
295
  
  	if (sock_queue_err_skb(sk, skb))
  		kfree_skb(skb);
  }
4c9483b2f   David S. Miller   ipv6: Convert to ...
296
  void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
  {
1c1e9d2b6   Eric Dumazet   ipv6: constify ip...
298
  	const struct ipv6_pinfo *np = inet6_sk(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
302
303
304
305
306
307
308
  	struct sock_exterr_skb *serr;
  	struct ipv6hdr *iph;
  	struct sk_buff *skb;
  
  	if (!np->recverr)
  		return;
  
  	skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  	if (!skb)
  		return;
d40a4de0b   Brian Haley   IPv6: fix IPV6_RE...
309
  	skb->protocol = htons(ETH_P_IPV6);
1ced98e81   Arnaldo Carvalho de Melo   [SK_BUFF] ipv6: M...
310
311
  	skb_put(skb, sizeof(struct ipv6hdr));
  	skb_reset_network_header(skb);
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
312
  	iph = ipv6_hdr(skb);
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
313
  	iph->daddr = fl6->daddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
  
  	serr = SKB_EXT_ERR(skb);
  	serr->ee.ee_errno = err;
  	serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
318
  	serr->ee.ee_type = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
  	serr->ee.ee_code = 0;
  	serr->ee.ee_pad = 0;
  	serr->ee.ee_info = info;
  	serr->ee.ee_data = 0;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
323
  	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
1958b856c   David S. Miller   net: Put fl6_* ma...
324
  	serr->port = fl6->fl6_dport;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325

27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
326
  	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
bd82393ca   Arnaldo Carvalho de Melo   [SK_BUFF]: More s...
327
  	skb_reset_transport_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
  
  	if (sock_queue_err_skb(sk, skb))
  		kfree_skb(skb);
  }
4c9483b2f   David S. Miller   ipv6: Convert to ...
332
  void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
4b340ae20   Brian Haley   IPv6: Complete IP...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct ipv6hdr *iph;
  	struct sk_buff *skb;
  	struct ip6_mtuinfo *mtu_info;
  
  	if (!np->rxopt.bits.rxpmtu)
  		return;
  
  	skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  	if (!skb)
  		return;
  
  	skb_put(skb, sizeof(struct ipv6hdr));
  	skb_reset_network_header(skb);
  	iph = ipv6_hdr(skb);
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
349
  	iph->daddr = fl6->daddr;
4b340ae20   Brian Haley   IPv6: Complete IP...
350
351
  
  	mtu_info = IP6CBMTU(skb);
4b340ae20   Brian Haley   IPv6: Complete IP...
352
353
354
355
356
  
  	mtu_info->ip6m_mtu = mtu;
  	mtu_info->ip6m_addr.sin6_family = AF_INET6;
  	mtu_info->ip6m_addr.sin6_port = 0;
  	mtu_info->ip6m_addr.sin6_flowinfo = 0;
4c9483b2f   David S. Miller   ipv6: Convert to ...
357
  	mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
358
  	mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
4b340ae20   Brian Haley   IPv6: Complete IP...
359
360
361
362
363
364
365
  
  	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  	skb_reset_transport_header(skb);
  
  	skb = xchg(&np->rxpmtu, skb);
  	kfree_skb(skb);
  }
34b99df4e   Julian Anastasov   ip: report the or...
366
367
368
369
370
371
372
373
374
  /* For some errors we have valid addr_offset even with zero payload and
   * zero port. Also, addr_offset should be supported if port is set.
   */
  static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
  {
  	return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
  	       serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  	       serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
  }
c247f0534   Willem de Bruijn   ip: fix error que...
375
376
377
378
379
380
381
382
383
384
385
  /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
   *
   * At one point, excluding local errors was a quick test to identify icmp/icmp6
   * errors. This is no longer true, but the test remained, so the v6 stack,
   * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
   *
   * Timestamp code paths do not initialize the fields expected by cmsg:
   * the PKTINFO fields in skb->cb[]. Fill those in here.
   */
  static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
  				      struct sock_exterr_skb *serr)
829ae9d61   Willem de Bruijn   net-timestamp: al...
386
  {
c247f0534   Willem de Bruijn   ip: fix error que...
387
388
389
390
391
392
393
394
395
  	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  	    serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
  		return true;
  
  	if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
  		return false;
  
  	if (!skb->dev)
  		return false;
829ae9d61   Willem de Bruijn   net-timestamp: al...
396
397
  
  	if (skb->protocol == htons(ETH_P_IPV6))
c247f0534   Willem de Bruijn   ip: fix error que...
398
  		IP6CB(skb)->iif = skb->dev->ifindex;
829ae9d61   Willem de Bruijn   net-timestamp: al...
399
  	else
c247f0534   Willem de Bruijn   ip: fix error que...
400
401
402
  		PKTINFO_SKB_CB(skb)->ipi_ifindex = skb->dev->ifindex;
  
  	return true;
829ae9d61   Willem de Bruijn   net-timestamp: al...
403
  }
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
404
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
406
   *	Handle MSG_ERRQUEUE
   */
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
407
  int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct sock_exterr_skb *serr;
364a9e932   Willem de Bruijn   sock: deduplicate...
411
  	struct sk_buff *skb;
342dfc306   Steffen Hurrle   net: add build-ti...
412
  	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
417
418
419
420
  	struct {
  		struct sock_extended_err ee;
  		struct sockaddr_in6	 offender;
  	} errhdr;
  	int err;
  	int copied;
  
  	err = -EAGAIN;
364a9e932   Willem de Bruijn   sock: deduplicate...
421
  	skb = sock_dequeue_err_skb(sk);
63159f29b   Ian Morris   ipv6: coding styl...
422
  	if (!skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
424
425
426
427
428
429
  		goto out;
  
  	copied = skb->len;
  	if (copied > len) {
  		msg->msg_flags |= MSG_TRUNC;
  		copied = len;
  	}
51f3d02b9   David S. Miller   net: Add and use ...
430
  	err = skb_copy_datagram_msg(skb, 0, msg, copied);
960a26282   Eric Dumazet   net: better drop ...
431
432
433
434
  	if (unlikely(err)) {
  		kfree_skb(skb);
  		return err;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
437
  	sock_recv_timestamp(msg, sk, skb);
  
  	serr = SKB_EXT_ERR(skb);
34b99df4e   Julian Anastasov   ip: report the or...
438
  	if (sin && ipv6_datagram_support_addr(serr)) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
439
  		const unsigned char *nh = skb_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
441
  		sin->sin6_family = AF_INET6;
  		sin->sin6_flowinfo = 0;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
442
  		sin->sin6_port = serr->port;
d40a4de0b   Brian Haley   IPv6: fix IPV6_RE...
443
  		if (skb->protocol == htons(ETH_P_IPV6)) {
6c40d100c   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Use contain...
444
445
446
  			const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
  								  struct ipv6hdr, daddr);
  			sin->sin6_addr = ip6h->daddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
  			if (np->sndflow)
6502ca527   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Introduce i...
448
  				sin->sin6_flowinfo = ip6_flowinfo(ip6h);
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
449
450
451
  			sin->sin6_scope_id =
  				ipv6_iface_scope_id(&sin->sin6_addr,
  						    IP6CB(skb)->iif);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
  		} else {
b301e82cf   Brian Haley   IPv6: use ipv6_ad...
453
454
  			ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  					       &sin->sin6_addr);
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
455
  			sin->sin6_scope_id = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
  		}
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
457
  		*addr_len = sizeof(*sin);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
461
  	}
  
  	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
  	sin = &errhdr.offender;
f812116b1   Willem de Bruijn   ip: zero sockaddr...
462
  	memset(sin, 0, sizeof(*sin));
c247f0534   Willem de Bruijn   ip: fix error que...
463
464
  
  	if (ip6_datagram_support_cmsg(skb, serr)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
  		sin->sin6_family = AF_INET6;
c247f0534   Willem de Bruijn   ip: fix error que...
466
  		if (np->rxopt.all)
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
467
  			ip6_datagram_recv_common_ctl(sk, msg, skb);
d40a4de0b   Brian Haley   IPv6: fix IPV6_RE...
468
  		if (skb->protocol == htons(ETH_P_IPV6)) {
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
469
  			sin->sin6_addr = ipv6_hdr(skb)->saddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
  			if (np->rxopt.all)
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
471
  				ip6_datagram_recv_specific_ctl(sk, msg, skb);
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
472
473
474
  			sin->sin6_scope_id =
  				ipv6_iface_scope_id(&sin->sin6_addr,
  						    IP6CB(skb)->iif);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
  		} else {
b301e82cf   Brian Haley   IPv6: use ipv6_ad...
476
477
  			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  					       &sin->sin6_addr);
f812116b1   Willem de Bruijn   ip: zero sockaddr...
478
  			if (inet_sk(sk)->cmsg_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
481
482
483
484
485
486
487
488
  				ip_cmsg_recv(msg, skb);
  		}
  	}
  
  	put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
  
  	/* Now we could try to dump offended packet options */
  
  	msg->msg_flags |= MSG_ERRQUEUE;
  	err = copied;
960a26282   Eric Dumazet   net: better drop ...
489
  	consume_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
492
  out:
  	return err;
  }
a495f8364   Chris Elston   ipv6: Export ipv6...
493
  EXPORT_SYMBOL_GPL(ipv6_recv_error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494

4b340ae20   Brian Haley   IPv6: Complete IP...
495
496
497
  /*
   *	Handle IPV6_RECVPATHMTU
   */
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
498
499
  int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
  		     int *addr_len)
4b340ae20   Brian Haley   IPv6: Complete IP...
500
501
502
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct sk_buff *skb;
4b340ae20   Brian Haley   IPv6: Complete IP...
503
  	struct ip6_mtuinfo mtu_info;
342dfc306   Steffen Hurrle   net: add build-ti...
504
  	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
4b340ae20   Brian Haley   IPv6: Complete IP...
505
506
507
508
509
  	int err;
  	int copied;
  
  	err = -EAGAIN;
  	skb = xchg(&np->rxpmtu, NULL);
63159f29b   Ian Morris   ipv6: coding styl...
510
  	if (!skb)
4b340ae20   Brian Haley   IPv6: Complete IP...
511
512
513
514
515
516
517
  		goto out;
  
  	copied = skb->len;
  	if (copied > len) {
  		msg->msg_flags |= MSG_TRUNC;
  		copied = len;
  	}
51f3d02b9   David S. Miller   net: Add and use ...
518
  	err = skb_copy_datagram_msg(skb, 0, msg, copied);
4b340ae20   Brian Haley   IPv6: Complete IP...
519
520
521
522
523
524
  	if (err)
  		goto out_free_skb;
  
  	sock_recv_timestamp(msg, sk, skb);
  
  	memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
4b340ae20   Brian Haley   IPv6: Complete IP...
525
526
527
528
529
  	if (sin) {
  		sin->sin6_family = AF_INET6;
  		sin->sin6_flowinfo = 0;
  		sin->sin6_port = 0;
  		sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
530
  		sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
531
  		*addr_len = sizeof(*sin);
4b340ae20   Brian Haley   IPv6: Complete IP...
532
533
534
535
536
537
538
539
540
541
542
  	}
  
  	put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
  
  	err = copied;
  
  out_free_skb:
  	kfree_skb(skb);
  out:
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543

4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
544
545
  void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
  				 struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
547
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
548
  	bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
550
551
  
  	if (np->rxopt.bits.rxinfo) {
  		struct in6_pktinfo src_info;
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
552
553
554
555
556
557
558
559
560
  		if (is_ipv6) {
  			src_info.ipi6_ifindex = IP6CB(skb)->iif;
  			src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
  		} else {
  			src_info.ipi6_ifindex =
  				PKTINFO_SKB_CB(skb)->ipi_ifindex;
  			ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  					       &src_info.ipi6_addr);
  		}
829ae9d61   Willem de Bruijn   net-timestamp: al...
561
562
563
564
  
  		if (src_info.ipi6_ifindex >= 0)
  			put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
  				 sizeof(src_info), &src_info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
565
  	}
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
566
567
568
569
570
571
572
573
  }
  
  void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
  				    struct sk_buff *skb)
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct inet6_skb_parm *opt = IP6CB(skb);
  	unsigned char *nh = skb_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
  
  	if (np->rxopt.bits.rxhlim) {
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
576
  		int hlim = ipv6_hdr(skb)->hop_limit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
  		put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  	}
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
579
  	if (np->rxopt.bits.rxtclass) {
e7219858a   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Use ipv6_ge...
580
  		int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
581
582
  		put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  	}
6502ca527   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Introduce i...
583
584
585
586
  	if (np->rxopt.bits.rxflow) {
  		__be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
  		if (flowinfo)
  			put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
588
589
  
  	/* HbH is allowed only once */
8b58a3984   Florian Westphal   ipv6: use flag in...
590
591
  	if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
  		u8 *ptr = nh + sizeof(struct ipv6hdr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
593
  		put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
594
595
596
597
598
599
600
601
  
  	if (opt->lastopt &&
  	    (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
  		/*
  		 * Silly enough, but we need to reparse in order to
  		 * report extension headers (except for HbH)
  		 * in order.
  		 *
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
602
  		 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
603
604
605
606
  		 * (and WILL NOT be) defined because
  		 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
  		 */
  		unsigned int off = sizeof(struct ipv6hdr);
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
607
  		u8 nexthdr = ipv6_hdr(skb)->nexthdr;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
608
609
  
  		while (off <= opt->lastopt) {
95c961747   Eric Dumazet   net: cleanup unsi...
610
  			unsigned int len;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
611
  			u8 *ptr = nh + off;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
612

b5a4257ce   Eldad Zack   net/ipv6/datagram...
613
  			switch (nexthdr) {
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
614
615
616
617
618
619
620
621
622
623
624
625
626
627
  			case IPPROTO_DSTOPTS:
  				nexthdr = ptr[0];
  				len = (ptr[1] + 1) << 3;
  				if (np->rxopt.bits.dstopts)
  					put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
  				break;
  			case IPPROTO_ROUTING:
  				nexthdr = ptr[0];
  				len = (ptr[1] + 1) << 3;
  				if (np->rxopt.bits.srcrt)
  					put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
  				break;
  			case IPPROTO_AH:
  				nexthdr = ptr[0];
a30598938   Ville Nuorvala   [IPV6]: Fix calcu...
628
  				len = (ptr[1] + 2) << 2;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
  				break;
  			default:
  				nexthdr = ptr[0];
  				len = (ptr[1] + 1) << 3;
  				break;
  			}
  
  			off += len;
  		}
  	}
  
  	/* socket options in old style */
  	if (np->rxopt.bits.rxoinfo) {
  		struct in6_pktinfo src_info;
  
  		src_info.ipi6_ifindex = opt->iif;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
645
  		src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
646
647
648
  		put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  	}
  	if (np->rxopt.bits.rxohlim) {
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
649
  		int hlim = ipv6_hdr(skb)->hop_limit;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
650
651
  		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  	}
8b58a3984   Florian Westphal   ipv6: use flag in...
652
653
  	if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
  		u8 *ptr = nh + sizeof(struct ipv6hdr);
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
654
655
656
  		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
  	}
  	if (np->rxopt.bits.odstopts && opt->dst0) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
657
  		u8 *ptr = nh + opt->dst0;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
658
  		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
660
  	if (np->rxopt.bits.osrcrt && opt->srcrt) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
661
  		struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
662
  		put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
664
  	if (np->rxopt.bits.odstopts && opt->dst1) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
665
  		u8 *ptr = nh + opt->dst1;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
666
  		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667
  	}
6c4686228   Balazs Scheidler   tproxy: added tpr...
668
669
  	if (np->rxopt.bits.rxorigdstaddr) {
  		struct sockaddr_in6 sin6;
747465ef7   Eric Dumazet   net: fix some spa...
670
  		__be16 *ports = (__be16 *) skb_transport_header(skb);
6c4686228   Balazs Scheidler   tproxy: added tpr...
671

d36a1cb1e   Willem de Bruijn   inet: fix IP(V6)_...
672
  		if (skb_transport_offset(skb) + 4 <= (int)skb->len) {
6c4686228   Balazs Scheidler   tproxy: added tpr...
673
674
675
676
677
678
  			/* All current transport protocols have the port numbers in the
  			 * first four bytes of the transport header and this function is
  			 * written with this assumption in mind.
  			 */
  
  			sin6.sin6_family = AF_INET6;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
679
  			sin6.sin6_addr = ipv6_hdr(skb)->daddr;
6c4686228   Balazs Scheidler   tproxy: added tpr...
680
681
  			sin6.sin6_port = ports[1];
  			sin6.sin6_flowinfo = 0;
3868b7aa7   Hannes Frederic Sowa   ipv6: report sin6...
682
683
684
  			sin6.sin6_scope_id =
  				ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
  						    opt->iif);
6c4686228   Balazs Scheidler   tproxy: added tpr...
685
686
687
688
  
  			put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
  		}
  	}
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
689
690
691
692
693
694
695
  }
  
  void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
  			  struct sk_buff *skb)
  {
  	ip6_datagram_recv_common_ctl(sk, msg, skb);
  	ip6_datagram_recv_specific_ctl(sk, msg, skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  }
8e72d37eb   Tom Parkin   ipv6: export ip6_...
697
  EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698

73df66f8b   Tom Parkin   ipv6: rename data...
699
700
  int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
  			  struct msghdr *msg, struct flowi6 *fl6,
26879da58   Wei Wang   ipv6: add new str...
701
  			  struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
703
704
705
706
  {
  	struct in6_pktinfo *src_info;
  	struct cmsghdr *cmsg;
  	struct ipv6_rt_hdr *rthdr;
  	struct ipv6_opt_hdr *hdr;
26879da58   Wei Wang   ipv6: add new str...
707
  	struct ipv6_txoptions *opt = ipc6->opt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
709
  	int len;
  	int err = 0;
f95b414ed   Gu Zheng   net: introduce he...
710
  	for_each_cmsghdr(cmsg, msg) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
  		int addr_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
713
714
715
716
  
  		if (!CMSG_OK(msg, cmsg)) {
  			err = -EINVAL;
  			goto exit_f;
  		}
ad1e46a83   Soheil Hassas Yeganeh   ipv6: process soc...
717
  		if (cmsg->cmsg_level == SOL_SOCKET) {
2632616bc   Eric Dumazet   sock: propagate _...
718
719
720
  			err = __sock_cmsg_send(sk, msg, cmsg, sockc);
  			if (err)
  				return err;
ad1e46a83   Soheil Hassas Yeganeh   ipv6: process soc...
721
722
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
724
725
726
  		if (cmsg->cmsg_level != SOL_IPV6)
  			continue;
  
  		switch (cmsg->cmsg_type) {
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
727
728
  		case IPV6_PKTINFO:
  		case IPV6_2292PKTINFO:
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
729
730
  		    {
  			struct net_device *dev = NULL;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
731
  			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
734
735
736
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
737

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
  			if (src_info->ipi6_ifindex) {
4c9483b2f   David S. Miller   ipv6: Convert to ...
739
740
  				if (fl6->flowi6_oif &&
  				    src_info->ipi6_ifindex != fl6->flowi6_oif)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
  					return -EINVAL;
4c9483b2f   David S. Miller   ipv6: Convert to ...
742
  				fl6->flowi6_oif = src_info->ipi6_ifindex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743
  			}
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
744
  			addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745

536b2e92f   Eric Dumazet   ipv6: no more dev...
746
  			rcu_read_lock();
4c9483b2f   David S. Miller   ipv6: Convert to ...
747
748
  			if (fl6->flowi6_oif) {
  				dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
536b2e92f   Eric Dumazet   ipv6: no more dev...
749
750
  				if (!dev) {
  					rcu_read_unlock();
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
751
  					return -ENODEV;
536b2e92f   Eric Dumazet   ipv6: no more dev...
752
753
754
  				}
  			} else if (addr_type & IPV6_ADDR_LINKLOCAL) {
  				rcu_read_unlock();
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
755
  				return -EINVAL;
536b2e92f   Eric Dumazet   ipv6: no more dev...
756
  			}
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
757

187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
758
759
  			if (addr_type != IPV6_ADDR_ANY) {
  				int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
2563fa595   Maciej Żenczykowski   net: make ipv6 PK...
760
  				if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
ec0506dbe   Maciej Żenczykowski   net: relax PKTINF...
761
  				    !ipv6_chk_addr(net, &src_info->ipi6_addr,
7c90cc2d4   FX Le Bail   ipv6: enable anyc...
762
763
764
  						   strict ? dev : NULL, 0) &&
  				    !ipv6_chk_acast_addr_src(net, dev,
  							     &src_info->ipi6_addr))
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
765
766
  					err = -EINVAL;
  				else
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
767
  					fl6->saddr = src_info->ipi6_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
  			}
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
769

536b2e92f   Eric Dumazet   ipv6: no more dev...
770
  			rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771

187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
772
773
  			if (err)
  				goto exit_f;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
  			break;
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
775
  		    }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776
777
  
  		case IPV6_FLOWINFO:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
778
  			if (cmsg->cmsg_len < CMSG_LEN(4)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
779
780
781
  				err = -EINVAL;
  				goto exit_f;
  			}
4c9483b2f   David S. Miller   ipv6: Convert to ...
782
783
  			if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  				if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
786
787
  					err = -EINVAL;
  					goto exit_f;
  				}
  			}
4c9483b2f   David S. Miller   ipv6: Convert to ...
788
  			fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
  			break;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
790
  		case IPV6_2292HOPOPTS:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
  		case IPV6_HOPOPTS:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
792
  			if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
795
796
797
798
799
800
801
802
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  			len = ((hdr->hdrlen + 1) << 3);
  			if (cmsg->cmsg_len < CMSG_LEN(len)) {
  				err = -EINVAL;
  				goto exit_f;
  			}
af31f412c   Eric W. Biederman   net: Allow userns...
803
  			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
805
806
807
808
809
  				err = -EPERM;
  				goto exit_f;
  			}
  			opt->opt_nflen += len;
  			opt->hopopt = hdr;
  			break;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
810
  		case IPV6_2292DSTOPTS:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
811
  			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
814
815
816
817
818
819
820
821
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  			len = ((hdr->hdrlen + 1) << 3);
  			if (cmsg->cmsg_len < CMSG_LEN(len)) {
  				err = -EINVAL;
  				goto exit_f;
  			}
af31f412c   Eric W. Biederman   net: Allow userns...
822
  			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
825
826
827
828
829
830
831
832
  				err = -EPERM;
  				goto exit_f;
  			}
  			if (opt->dst1opt) {
  				err = -EINVAL;
  				goto exit_f;
  			}
  			opt->opt_flen += len;
  			opt->dst1opt = hdr;
  			break;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
833
834
835
836
837
838
839
840
841
842
843
844
845
  		case IPV6_DSTOPTS:
  		case IPV6_RTHDRDSTOPTS:
  			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  			len = ((hdr->hdrlen + 1) << 3);
  			if (cmsg->cmsg_len < CMSG_LEN(len)) {
  				err = -EINVAL;
  				goto exit_f;
  			}
af31f412c   Eric W. Biederman   net: Allow userns...
846
  			if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
847
848
849
850
851
852
853
854
855
856
857
858
859
  				err = -EPERM;
  				goto exit_f;
  			}
  			if (cmsg->cmsg_type == IPV6_DSTOPTS) {
  				opt->opt_flen += len;
  				opt->dst1opt = hdr;
  			} else {
  				opt->opt_nflen += len;
  				opt->dst0opt = hdr;
  			}
  			break;
  
  		case IPV6_2292RTHDR:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
860
  		case IPV6_RTHDR:
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
861
  			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
862
863
864
865
866
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
280a9d340   Masahide NAKAMURA   [IPV6] MIP6: Add ...
867
  			switch (rthdr->type) {
07a936260   Amerigo Wang   ipv6: use IS_ENAB...
868
  #if IS_ENABLED(CONFIG_IPV6_MIP6)
280a9d340   Masahide NAKAMURA   [IPV6] MIP6: Add ...
869
  			case IPV6_SRCRT_TYPE_2:
6e093d9df   Brian Haley   ipv6: routing hea...
870
871
872
873
874
  				if (rthdr->hdrlen != 2 ||
  				    rthdr->segments_left != 1) {
  					err = -EINVAL;
  					goto exit_f;
  				}
280a9d340   Masahide NAKAMURA   [IPV6] MIP6: Add ...
875
  				break;
bb4dbf9e6   YOSHIFUJI Hideaki   [IPV6]: Do not se...
876
  #endif
280a9d340   Masahide NAKAMURA   [IPV6] MIP6: Add ...
877
  			default:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
879
880
881
882
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			len = ((rthdr->hdrlen + 1) << 3);
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
883
  			if (cmsg->cmsg_len < CMSG_LEN(len)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
885
886
887
888
889
890
891
892
893
894
895
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			/* segments left must also match */
  			if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			opt->opt_nflen += len;
  			opt->srcrt = rthdr;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
896
  			if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
897
898
899
900
901
902
903
904
905
  				int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
  
  				opt->opt_nflen += dsthdrlen;
  				opt->dst0opt = opt->dst1opt;
  				opt->dst1opt = NULL;
  				opt->opt_flen -= dsthdrlen;
  			}
  
  			break;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
906
  		case IPV6_2292HOPLIMIT:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
909
910
911
  		case IPV6_HOPLIMIT:
  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  				err = -EINVAL;
  				goto exit_f;
  			}
26879da58   Wei Wang   ipv6: add new str...
912
913
  			ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
  			if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
e8766fc86   Shan Wei   ipv6: Check the h...
914
915
916
  				err = -EINVAL;
  				goto exit_f;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
  			break;
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
918
919
920
921
922
  		case IPV6_TCLASS:
  		    {
  			int tc;
  
  			err = -EINVAL;
b5a4257ce   Eldad Zack   net/ipv6/datagram...
923
  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
924
  				goto exit_f;
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
925
926
  
  			tc = *(int *)CMSG_DATA(cmsg);
d0ee011f7   Rémi Denis-Courmont   [IPV6]: Accept -1...
927
  			if (tc < -1 || tc > 0xff)
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
928
929
930
  				goto exit_f;
  
  			err = 0;
26879da58   Wei Wang   ipv6: add new str...
931
  			ipc6->tclass = tc;
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
932
933
934
  
  			break;
  		    }
13b52cd44   Brian Haley   IPv6: Add dontfra...
935
936
937
938
939
940
  
  		case IPV6_DONTFRAG:
  		    {
  			int df;
  
  			err = -EINVAL;
b5a4257ce   Eldad Zack   net/ipv6/datagram...
941
  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
13b52cd44   Brian Haley   IPv6: Add dontfra...
942
  				goto exit_f;
13b52cd44   Brian Haley   IPv6: Add dontfra...
943
944
945
946
947
948
  
  			df = *(int *)CMSG_DATA(cmsg);
  			if (df < 0 || df > 1)
  				goto exit_f;
  
  			err = 0;
26879da58   Wei Wang   ipv6: add new str...
949
  			ipc6->dontfrag = df;
13b52cd44   Brian Haley   IPv6: Add dontfra...
950
951
952
  
  			break;
  		    }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
953
  		default:
ba7a46f16   Joe Perches   net: Convert LIMI...
954
955
956
  			net_dbg_ratelimited("invalid cmsg type: %d
  ",
  					    cmsg->cmsg_type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
957
  			err = -EINVAL;
4a36702e0   Miao Xie   IPv6: datagram_se...
958
  			goto exit_f;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
959
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
960
961
962
963
964
  	}
  
  exit_f:
  	return err;
  }
73df66f8b   Tom Parkin   ipv6: rename data...
965
  EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
966
967
968
969
  
  void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
  			     __u16 srcp, __u16 destp, int bucket)
  {
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
970
  	const struct in6_addr *dest, *src;
efe4208f4   Eric Dumazet   ipv6: make lookup...
971
972
  	dest  = &sp->sk_v6_daddr;
  	src   = &sp->sk_v6_rcv_saddr;
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
973
974
  	seq_printf(seq,
  		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
d14c5ab6b   Francesco Fusco   net: proc_fs: tri...
975
976
  		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d
  ",
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
  		   bucket,
  		   src->s6_addr32[0], src->s6_addr32[1],
  		   src->s6_addr32[2], src->s6_addr32[3], srcp,
  		   dest->s6_addr32[0], dest->s6_addr32[1],
  		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
  		   sp->sk_state,
  		   sk_wmem_alloc_get(sp),
  		   sk_rmem_alloc_get(sp),
  		   0, 0L, 0,
  		   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  		   0,
  		   sock_i_ino(sp),
  		   atomic_read(&sp->sk_refcnt), sp,
  		   atomic_read(&sp->sk_drops));
  }