Blame view

net/ipv6/datagram.c 25 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
  
  #include <linux/errqueue.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
35
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36

a50feda54   Eric Dumazet   ipv6: bool/const ...
37
  static bool ipv6_mapped_addr_any(const struct in6_addr *a)
c15fea2d8   Max Matveev   ipv6: check for I...
38
  {
a50feda54   Eric Dumazet   ipv6: bool/const ...
39
  	return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
c15fea2d8   Max Matveev   ipv6: check for I...
40
  }
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  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;
e2d118a1c   Lorenzo Colitti   net: inet: Suppor...
55
  	fl6->flowi6_uid = sk->sk_uid;
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
56
57
58
59
60
61
62
63
64
  
  	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...
65
  int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
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
91
92
  {
  	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...
93
94
95
  	if (fix_sk_saddr) {
  		if (ipv6_addr_any(&np->saddr))
  			np->saddr = fl6.saddr;
7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
96

33c162a98   Martin KaFai Lau   ipv6: datagram: U...
97
98
99
100
101
102
  		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...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  	}
  
  	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...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  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...
137
138
  int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
  			   int addr_len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
  {
  	struct sockaddr_in6	*usin = (struct sockaddr_in6 *) uaddr;
67ba4152e   Ian Morris   ipv6: White-space...
141
142
  	struct inet_sock	*inet = inet_sk(sk);
  	struct ipv6_pinfo	*np = inet6_sk(sk);
a8f02befc   Paolo Abeni   net: ipv6: keep s...
143
144
145
  	struct in6_addr		*daddr, old_daddr;
  	__be32			fl6_flowlabel = 0;
  	__be32			old_fl6_flowlabel;
5defa8c92   Stefano Brivio   ipv6: old_dport s...
146
  	__be16			old_dport;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
150
151
152
  	int			addr_type;
  	int			err;
  
  	if (usin->sin6_family == AF_INET) {
  		if (__ipv6_only_sock(sk))
  			return -EAFNOSUPPORT;
03645a11a   Eric Dumazet   ipv6: lock socket...
153
  		err = __ip4_datagram_connect(sk, uaddr, addr_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
156
157
  		goto ipv4_connected;
  	}
  
  	if (addr_len < SIN6_LEN_RFC2133)
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
158
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159

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

7e2040db1   Martin KaFai Lau   ipv6: datagram: R...
163
  	if (np->sndflow)
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
164
  		fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165

052d2369d   Jonathan T. Leighton   ipv6: Handle IPv4...
166
  	if (ipv6_addr_any(&usin->sin6_addr)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
  		/*
  		 *	connect to self
  		 */
052d2369d   Jonathan T. Leighton   ipv6: Handle IPv4...
170
171
172
173
174
  		if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
  			ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
  					       &usin->sin6_addr);
  		else
  			usin->sin6_addr = in6addr_loopback;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
  	}
052d2369d   Jonathan T. Leighton   ipv6: Handle IPv4...
176
  	addr_type = ipv6_addr_type(&usin->sin6_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  	daddr = &usin->sin6_addr;
052d2369d   Jonathan T. Leighton   ipv6: Handle IPv4...
178
  	if (addr_type & IPV6_ADDR_MAPPED) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
182
183
184
185
186
187
  		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...
188
189
190
  		err = __ip4_datagram_connect(sk,
  					     (struct sockaddr *) &sin,
  					     sizeof(sin));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
  
  ipv4_connected:
  		if (err)
  			goto out;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
195

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
  		goto out;
  	}
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
212
  	if (__ipv6_addr_needs_scope_id(addr_type)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
216
217
218
219
220
  		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
221
  		}
1ac4f0088   Brian Haley   [IPV6]: IPV6_MULT...
222
223
  		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
224
225
226
227
228
229
  		/* Connect to link-local address requires an interface */
  		if (!sk->sk_bound_dev_if) {
  			err = -EINVAL;
  			goto out;
  		}
  	}
a8f02befc   Paolo Abeni   net: ipv6: keep s...
230
231
232
233
  	/* save the current peer information before updating it */
  	old_daddr = sk->sk_v6_daddr;
  	old_fl6_flowlabel = np->flow_label;
  	old_dport = inet->inet_dport;
efe4208f4   Eric Dumazet   ipv6: make lookup...
234
  	sk->sk_v6_daddr = *daddr;
80fbdb208   Martin KaFai Lau   ipv6: datagram: R...
235
  	np->flow_label = fl6_flowlabel;
c720c7e83   Eric Dumazet   inet: rename some...
236
  	inet->inet_dport = usin->sin6_port;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
241
  
  	/*
  	 *	Check for a route to destination an obtain the
  	 *	destination cache for it.
  	 */
33c162a98   Martin KaFai Lau   ipv6: datagram: U...
242
  	err = ip6_datagram_dst_update(sk, true);
85cb73ff9   Wei Wang   net: ipv6: reset ...
243
  	if (err) {
a8f02befc   Paolo Abeni   net: ipv6: keep s...
244
245
  		/* Restore the socket peer info, to keep it consistent with
  		 * the old socket state
85cb73ff9   Wei Wang   net: ipv6: reset ...
246
  		 */
a8f02befc   Paolo Abeni   net: ipv6: keep s...
247
248
249
  		sk->sk_v6_daddr = old_daddr;
  		np->flow_label = old_fl6_flowlabel;
  		inet->inet_dport = old_dport;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
  		goto out;
85cb73ff9   Wei Wang   net: ipv6: reset ...
251
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
  
  	sk->sk_state = TCP_ESTABLISHED;
877d1f629   Tom Herbert   net: Set sk_txhas...
254
  	sk_set_txhash(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
  	return err;
  }
0382a25af   Guillaume Nault   l2tp: lock socket...
258
  EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
03645a11a   Eric Dumazet   ipv6: lock socket...
259
260
261
262
263
264
265
266
267
268
  
  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...
269
  EXPORT_SYMBOL_GPL(ip6_datagram_connect);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270

82b276cd2   Hannes Frederic Sowa   ipv6: protect pro...
271
272
273
274
275
276
277
278
279
  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...
280
  void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
e69a4adc6   Al Viro   [IPV6]: Misc endi...
281
  		     __be16 port, u32 info, u8 *payload)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282
283
  {
  	struct ipv6_pinfo *np  = inet6_sk(sk);
cc70ab261   Arnaldo Carvalho de Melo   [ICMP6]: Introduc...
284
  	struct icmp6hdr *icmph = icmp6_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
286
287
288
289
290
291
292
  	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...
293
  	skb->protocol = htons(ETH_P_IPV6);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
296
  	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...
297
  	serr->ee.ee_type = icmph->icmp6_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
  	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...
302
303
  	serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
  				  skb_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
  	serr->port = port;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  	__skb_pull(skb, payload - skb->data);
bd82393ca   Arnaldo Carvalho de Melo   [SK_BUFF]: More s...
306
  	skb_reset_transport_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
  
  	if (sock_queue_err_skb(sk, skb))
  		kfree_skb(skb);
  }
4c9483b2f   David S. Miller   ipv6: Convert to ...
311
  void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  {
1c1e9d2b6   Eric Dumazet   ipv6: constify ip...
313
  	const struct ipv6_pinfo *np = inet6_sk(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
323
  	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...
324
  	skb->protocol = htons(ETH_P_IPV6);
1ced98e81   Arnaldo Carvalho de Melo   [SK_BUFF] ipv6: M...
325
326
  	skb_put(skb, sizeof(struct ipv6hdr));
  	skb_reset_network_header(skb);
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
327
  	iph = ipv6_hdr(skb);
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
328
  	iph->daddr = fl6->daddr;
c809028e7   Eric Dumazet   ipv6: fix kernel-...
329
  	ip6_flow_hdr(iph, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
  
  	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...
334
  	serr->ee.ee_type = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
  	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...
339
  	serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
1958b856c   David S. Miller   net: Put fl6_* ma...
340
  	serr->port = fl6->fl6_dport;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341

27a884dc3   Arnaldo Carvalho de Melo   [SK_BUFF]: Conver...
342
  	__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
bd82393ca   Arnaldo Carvalho de Melo   [SK_BUFF]: More s...
343
  	skb_reset_transport_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
346
347
  
  	if (sock_queue_err_skb(sk, skb))
  		kfree_skb(skb);
  }
4c9483b2f   David S. Miller   ipv6: Convert to ...
348
  void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
4b340ae20   Brian Haley   IPv6: Complete IP...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  {
  	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_...
365
  	iph->daddr = fl6->daddr;
4b340ae20   Brian Haley   IPv6: Complete IP...
366
367
  
  	mtu_info = IP6CBMTU(skb);
4b340ae20   Brian Haley   IPv6: Complete IP...
368
369
370
371
372
  
  	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 ...
373
  	mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
374
  	mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
4b340ae20   Brian Haley   IPv6: Complete IP...
375
376
377
378
379
380
381
  
  	__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...
382
383
384
385
386
387
388
389
390
  /* 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...
391
392
393
394
395
  /* 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.
c247f0534   Willem de Bruijn   ip: fix error que...
396
397
398
   */
  static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
  				      struct sock_exterr_skb *serr)
829ae9d61   Willem de Bruijn   net-timestamp: al...
399
  {
c247f0534   Willem de Bruijn   ip: fix error que...
400
401
402
403
404
405
  	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;
1862d6208   Willem de Bruijn   net-timestamp: av...
406
  	if (!IP6CB(skb)->iif)
c247f0534   Willem de Bruijn   ip: fix error que...
407
  		return false;
829ae9d61   Willem de Bruijn   net-timestamp: al...
408

c247f0534   Willem de Bruijn   ip: fix error que...
409
  	return true;
829ae9d61   Willem de Bruijn   net-timestamp: al...
410
  }
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
411
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
413
   *	Handle MSG_ERRQUEUE
   */
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
414
  int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct sock_exterr_skb *serr;
364a9e932   Willem de Bruijn   sock: deduplicate...
418
  	struct sk_buff *skb;
342dfc306   Steffen Hurrle   net: add build-ti...
419
  	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
424
425
426
427
  	struct {
  		struct sock_extended_err ee;
  		struct sockaddr_in6	 offender;
  	} errhdr;
  	int err;
  	int copied;
  
  	err = -EAGAIN;
364a9e932   Willem de Bruijn   sock: deduplicate...
428
  	skb = sock_dequeue_err_skb(sk);
63159f29b   Ian Morris   ipv6: coding styl...
429
  	if (!skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
431
432
433
434
435
436
  		goto out;
  
  	copied = skb->len;
  	if (copied > len) {
  		msg->msg_flags |= MSG_TRUNC;
  		copied = len;
  	}
51f3d02b9   David S. Miller   net: Add and use ...
437
  	err = skb_copy_datagram_msg(skb, 0, msg, copied);
960a26282   Eric Dumazet   net: better drop ...
438
439
440
441
  	if (unlikely(err)) {
  		kfree_skb(skb);
  		return err;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
444
  	sock_recv_timestamp(msg, sk, skb);
  
  	serr = SKB_EXT_ERR(skb);
34b99df4e   Julian Anastasov   ip: report the or...
445
  	if (sin && ipv6_datagram_support_addr(serr)) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
446
  		const unsigned char *nh = skb_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
  		sin->sin6_family = AF_INET6;
  		sin->sin6_flowinfo = 0;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
449
  		sin->sin6_port = serr->port;
d40a4de0b   Brian Haley   IPv6: fix IPV6_RE...
450
  		if (skb->protocol == htons(ETH_P_IPV6)) {
6c40d100c   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Use contain...
451
452
453
  			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
454
  			if (np->sndflow)
6502ca527   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Introduce i...
455
  				sin->sin6_flowinfo = ip6_flowinfo(ip6h);
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
456
457
458
  			sin->sin6_scope_id =
  				ipv6_iface_scope_id(&sin->sin6_addr,
  						    IP6CB(skb)->iif);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  		} else {
b301e82cf   Brian Haley   IPv6: use ipv6_ad...
460
461
  			ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  					       &sin->sin6_addr);
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
462
  			sin->sin6_scope_id = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  		}
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
464
  		*addr_len = sizeof(*sin);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
467
468
  	}
  
  	memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
  	sin = &errhdr.offender;
f812116b1   Willem de Bruijn   ip: zero sockaddr...
469
  	memset(sin, 0, sizeof(*sin));
c247f0534   Willem de Bruijn   ip: fix error que...
470
471
  
  	if (ip6_datagram_support_cmsg(skb, serr)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
  		sin->sin6_family = AF_INET6;
c247f0534   Willem de Bruijn   ip: fix error que...
473
  		if (np->rxopt.all)
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
474
  			ip6_datagram_recv_common_ctl(sk, msg, skb);
d40a4de0b   Brian Haley   IPv6: fix IPV6_RE...
475
  		if (skb->protocol == htons(ETH_P_IPV6)) {
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
476
  			sin->sin6_addr = ipv6_hdr(skb)->saddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
  			if (np->rxopt.all)
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
478
  				ip6_datagram_recv_specific_ctl(sk, msg, skb);
842df0739   Hannes Frederic Sowa   ipv6: use newly i...
479
480
481
  			sin->sin6_scope_id =
  				ipv6_iface_scope_id(&sin->sin6_addr,
  						    IP6CB(skb)->iif);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
  		} else {
b301e82cf   Brian Haley   IPv6: use ipv6_ad...
483
484
  			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  					       &sin->sin6_addr);
f812116b1   Willem de Bruijn   ip: zero sockaddr...
485
  			if (inet_sk(sk)->cmsg_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
488
489
490
491
492
493
494
495
  				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 ...
496
  	consume_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
  out:
  	return err;
  }
a495f8364   Chris Elston   ipv6: Export ipv6...
500
  EXPORT_SYMBOL_GPL(ipv6_recv_error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501

4b340ae20   Brian Haley   IPv6: Complete IP...
502
503
504
  /*
   *	Handle IPV6_RECVPATHMTU
   */
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
505
506
  int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
  		     int *addr_len)
4b340ae20   Brian Haley   IPv6: Complete IP...
507
508
509
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct sk_buff *skb;
4b340ae20   Brian Haley   IPv6: Complete IP...
510
  	struct ip6_mtuinfo mtu_info;
342dfc306   Steffen Hurrle   net: add build-ti...
511
  	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
4b340ae20   Brian Haley   IPv6: Complete IP...
512
513
514
515
516
  	int err;
  	int copied;
  
  	err = -EAGAIN;
  	skb = xchg(&np->rxpmtu, NULL);
63159f29b   Ian Morris   ipv6: coding styl...
517
  	if (!skb)
4b340ae20   Brian Haley   IPv6: Complete IP...
518
519
520
521
522
523
524
  		goto out;
  
  	copied = skb->len;
  	if (copied > len) {
  		msg->msg_flags |= MSG_TRUNC;
  		copied = len;
  	}
51f3d02b9   David S. Miller   net: Add and use ...
525
  	err = skb_copy_datagram_msg(skb, 0, msg, copied);
4b340ae20   Brian Haley   IPv6: Complete IP...
526
527
528
529
530
531
  	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...
532
533
534
535
536
  	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_...
537
  		sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
538
  		*addr_len = sizeof(*sin);
4b340ae20   Brian Haley   IPv6: Complete IP...
539
540
541
542
543
544
545
546
547
548
549
  	}
  
  	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
550

4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
551
552
  void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
  				 struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
554
  {
  	struct ipv6_pinfo *np = inet6_sk(sk);
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
555
  	bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
  
  	if (np->rxopt.bits.rxinfo) {
  		struct in6_pktinfo src_info;
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
559
560
561
562
563
564
565
566
567
  		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...
568
569
570
571
  
  		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
572
  	}
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
573
574
575
576
577
578
579
580
  }
  
  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
581
582
  
  	if (np->rxopt.bits.rxhlim) {
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
583
  		int hlim = ipv6_hdr(skb)->hop_limit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
585
  		put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  	}
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
586
  	if (np->rxopt.bits.rxtclass) {
e7219858a   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Use ipv6_ge...
587
  		int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
41a1f8ea4   YOSHIFUJI Hideaki   [IPV6]: Support I...
588
589
  		put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  	}
6502ca527   YOSHIFUJI Hideaki / 吉藤英明   ipv6: Introduce i...
590
591
592
593
  	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
594
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
595
596
  
  	/* HbH is allowed only once */
8b58a3984   Florian Westphal   ipv6: use flag in...
597
598
  	if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
  		u8 *ptr = nh + sizeof(struct ipv6hdr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
599
600
  		put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
601
602
603
604
605
606
607
608
  
  	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...
609
  		 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
610
611
612
613
  		 * (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...
614
  		u8 nexthdr = ipv6_hdr(skb)->nexthdr;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
615
616
  
  		while (off <= opt->lastopt) {
95c961747   Eric Dumazet   net: cleanup unsi...
617
  			unsigned int len;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
618
  			u8 *ptr = nh + off;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
619

b5a4257ce   Eldad Zack   net/ipv6/datagram...
620
  			switch (nexthdr) {
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  			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...
635
  				len = (ptr[1] + 2) << 2;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  				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_...
652
  		src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
653
654
655
  		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...
656
  		int hlim = ipv6_hdr(skb)->hop_limit;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
657
658
  		put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  	}
8b58a3984   Florian Westphal   ipv6: use flag in...
659
660
  	if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
  		u8 *ptr = nh + sizeof(struct ipv6hdr);
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
661
662
663
  		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...
664
  		u8 *ptr = nh + opt->dst0;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
665
  		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
667
  	if (np->rxopt.bits.osrcrt && opt->srcrt) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
668
  		struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
669
  		put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
  	}
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
671
  	if (np->rxopt.bits.odstopts && opt->dst1) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
672
  		u8 *ptr = nh + opt->dst1;
333fad536   YOSHIFUJI Hideaki   [IPV6]: Support s...
673
  		put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
  	}
6c4686228   Balazs Scheidler   tproxy: added tpr...
675
676
  	if (np->rxopt.bits.rxorigdstaddr) {
  		struct sockaddr_in6 sin6;
75664d803   Willem de Bruijn   ip: on queued skb...
677
  		__be16 _ports[2], *ports;
6c4686228   Balazs Scheidler   tproxy: added tpr...
678

75664d803   Willem de Bruijn   ip: on queued skb...
679
680
681
  		ports = skb_header_pointer(skb, skb_transport_offset(skb),
  					   sizeof(_ports), &_ports);
  		if (ports) {
6c4686228   Balazs Scheidler   tproxy: added tpr...
682
683
684
685
  			/* 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.
  			 */
6c4686228   Balazs Scheidler   tproxy: added tpr...
686
  			sin6.sin6_family = AF_INET6;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
687
  			sin6.sin6_addr = ipv6_hdr(skb)->daddr;
6c4686228   Balazs Scheidler   tproxy: added tpr...
688
689
  			sin6.sin6_port = ports[1];
  			sin6.sin6_flowinfo = 0;
3868b7aa7   Hannes Frederic Sowa   ipv6: report sin6...
690
691
692
  			sin6.sin6_scope_id =
  				ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
  						    opt->iif);
6c4686228   Balazs Scheidler   tproxy: added tpr...
693
694
695
696
  
  			put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
  		}
  	}
0cc0aa614   Willem de Bruijn   ipv6: add IPV6_RE...
697
698
699
700
701
  	if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
  		int val = opt->frag_max_size;
  
  		put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
  	}
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
702
703
704
705
706
707
708
  }
  
  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
709
  }
8e72d37eb   Tom Parkin   ipv6: export ip6_...
710
  EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711

73df66f8b   Tom Parkin   ipv6: rename data...
712
713
  int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
  			  struct msghdr *msg, struct flowi6 *fl6,
26879da58   Wei Wang   ipv6: add new str...
714
  			  struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
716
717
718
719
  {
  	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...
720
  	struct ipv6_txoptions *opt = ipc6->opt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
722
  	int len;
  	int err = 0;
f95b414ed   Gu Zheng   net: introduce he...
723
  	for_each_cmsghdr(cmsg, msg) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
  		int addr_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
727
728
729
  
  		if (!CMSG_OK(msg, cmsg)) {
  			err = -EINVAL;
  			goto exit_f;
  		}
ad1e46a83   Soheil Hassas Yeganeh   ipv6: process soc...
730
  		if (cmsg->cmsg_level == SOL_SOCKET) {
2632616bc   Eric Dumazet   sock: propagate _...
731
732
733
  			err = __sock_cmsg_send(sk, msg, cmsg, sockc);
  			if (err)
  				return err;
ad1e46a83   Soheil Hassas Yeganeh   ipv6: process soc...
734
735
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
  		if (cmsg->cmsg_level != SOL_IPV6)
  			continue;
  
  		switch (cmsg->cmsg_type) {
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
740
741
  		case IPV6_PKTINFO:
  		case IPV6_2292PKTINFO:
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
742
743
  		    {
  			struct net_device *dev = NULL;
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
744
  			if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
747
748
749
  				err = -EINVAL;
  				goto exit_f;
  			}
  
  			src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
750

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
  			if (src_info->ipi6_ifindex) {
4c9483b2f   David S. Miller   ipv6: Convert to ...
752
753
  				if (fl6->flowi6_oif &&
  				    src_info->ipi6_ifindex != fl6->flowi6_oif)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
  					return -EINVAL;
4c9483b2f   David S. Miller   ipv6: Convert to ...
755
  				fl6->flowi6_oif = src_info->ipi6_ifindex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756
  			}
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
757
  			addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
758

536b2e92f   Eric Dumazet   ipv6: no more dev...
759
  			rcu_read_lock();
4c9483b2f   David S. Miller   ipv6: Convert to ...
760
761
  			if (fl6->flowi6_oif) {
  				dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
536b2e92f   Eric Dumazet   ipv6: no more dev...
762
763
  				if (!dev) {
  					rcu_read_unlock();
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
764
  					return -ENODEV;
536b2e92f   Eric Dumazet   ipv6: no more dev...
765
766
767
  				}
  			} else if (addr_type & IPV6_ADDR_LINKLOCAL) {
  				rcu_read_unlock();
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
768
  				return -EINVAL;
536b2e92f   Eric Dumazet   ipv6: no more dev...
769
  			}
1ab1457c4   YOSHIFUJI Hideaki   [NET] IPV6: Fix w...
770

187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
771
772
  			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...
773
  				if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
ec0506dbe   Maciej Żenczykowski   net: relax PKTINF...
774
  				    !ipv6_chk_addr(net, &src_info->ipi6_addr,
7c90cc2d4   FX Le Bail   ipv6: enable anyc...
775
776
777
  						   strict ? dev : NULL, 0) &&
  				    !ipv6_chk_acast_addr_src(net, dev,
  							     &src_info->ipi6_addr))
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
778
779
  					err = -EINVAL;
  				else
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
780
  					fl6->saddr = src_info->ipi6_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
  			}
187e38384   YOSHIFUJI Hideaki   [IPV6]: Check out...
782

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

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

2e5d31688   Paolo Abeni   udp: fix rx queue...
980
981
  void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
  			       __u16 srcp, __u16 destp, int rqueue, int bucket)
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
982
  {
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
983
  	const struct in6_addr *dest, *src;
efe4208f4   Eric Dumazet   ipv6: make lookup...
984
985
  	dest  = &sp->sk_v6_daddr;
  	src   = &sp->sk_v6_rcv_saddr;
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
986
987
  	seq_printf(seq,
  		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
d14c5ab6b   Francesco Fusco   net: proc_fs: tri...
988
989
  		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d
  ",
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
990
991
992
993
994
995
996
  		   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),
2e5d31688   Paolo Abeni   udp: fix rx queue...
997
  		   rqueue,
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
998
999
1000
1001
  		   0, 0L, 0,
  		   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  		   0,
  		   sock_i_ino(sp),
41c6d650f   Reshetova, Elena   net: convert sock...
1002
  		   refcount_read(&sp->sk_refcnt), sp,
17ef66afc   Lorenzo Colitti   net: ipv6: Unify ...
1003
1004
  		   atomic_read(&sp->sk_drops));
  }