Branch data Line data Source code
1 : : /*
2 : : * common UDP/RAW code
3 : : * Linux INET6 implementation
4 : : *
5 : : * Authors:
6 : : * Pedro Roque <roque@di.fc.ul.pt>
7 : : *
8 : : * This program is free software; you can redistribute it and/or
9 : : * modify it under the terms of the GNU General Public License
10 : : * as published by the Free Software Foundation; either version
11 : : * 2 of the License, or (at your option) any later version.
12 : : */
13 : :
14 : : #include <linux/capability.h>
15 : : #include <linux/errno.h>
16 : : #include <linux/types.h>
17 : : #include <linux/kernel.h>
18 : : #include <linux/interrupt.h>
19 : : #include <linux/socket.h>
20 : : #include <linux/sockios.h>
21 : : #include <linux/in6.h>
22 : : #include <linux/ipv6.h>
23 : : #include <linux/route.h>
24 : : #include <linux/slab.h>
25 : : #include <linux/export.h>
26 : :
27 : : #include <net/ipv6.h>
28 : : #include <net/ndisc.h>
29 : : #include <net/addrconf.h>
30 : : #include <net/transp_v6.h>
31 : : #include <net/ip6_route.h>
32 : : #include <net/tcp_states.h>
33 : : #include <net/dsfield.h>
34 : :
35 : : #include <linux/errqueue.h>
36 : : #include <asm/uaccess.h>
37 : :
38 : : static bool ipv6_mapped_addr_any(const struct in6_addr *a)
39 : : {
40 [ # # ][ # # ]: 0 : return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
[ # # ][ # # ]
41 : : }
42 : :
43 : 0 : int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
44 : : {
45 : : struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
46 : : struct inet_sock *inet = inet_sk(sk);
47 : : struct ipv6_pinfo *np = inet6_sk(sk);
48 : : struct in6_addr *daddr, *final_p, final;
49 : : struct dst_entry *dst;
50 : : struct flowi6 fl6;
51 : : struct ip6_flowlabel *flowlabel = NULL;
52 : : struct ipv6_txoptions *opt;
53 : : int addr_type;
54 : : int err;
55 : :
56 [ + + ]: 8 : if (usin->sin6_family == AF_INET) {
57 [ + - ]: 4 : if (__ipv6_only_sock(sk))
58 : : return -EAFNOSUPPORT;
59 : 4 : err = ip4_datagram_connect(sk, uaddr, addr_len);
60 : 4 : goto ipv4_connected;
61 : : }
62 : :
63 [ + - ]: 4 : if (addr_len < SIN6_LEN_RFC2133)
64 : : return -EINVAL;
65 : :
66 [ + - ]: 4 : if (usin->sin6_family != AF_INET6)
67 : : return -EAFNOSUPPORT;
68 : :
69 : 4 : memset(&fl6, 0, sizeof(fl6));
70 [ - + ]: 4 : if (np->sndflow) {
71 : 0 : fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
72 [ # # ]: 0 : if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
73 : 0 : flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
74 [ # # ]: 0 : if (flowlabel == NULL)
75 : : return -EINVAL;
76 : : }
77 : : }
78 : :
79 : 4 : addr_type = ipv6_addr_type(&usin->sin6_addr);
80 : :
81 [ + + ]: 4 : if (addr_type == IPV6_ADDR_ANY) {
82 : : /*
83 : : * connect to self
84 : : */
85 : 3 : usin->sin6_addr.s6_addr[15] = 0x01;
86 : : }
87 : :
88 : : daddr = &usin->sin6_addr;
89 : :
90 [ - + ]: 4 : if (addr_type == IPV6_ADDR_MAPPED) {
91 : : struct sockaddr_in sin;
92 : :
93 [ # # ]: 0 : if (__ipv6_only_sock(sk)) {
94 : : err = -ENETUNREACH;
95 : : goto out;
96 : : }
97 : 0 : sin.sin_family = AF_INET;
98 : 0 : sin.sin_addr.s_addr = daddr->s6_addr32[3];
99 : 0 : sin.sin_port = usin->sin6_port;
100 : :
101 : 0 : err = ip4_datagram_connect(sk,
102 : : (struct sockaddr *) &sin,
103 : : sizeof(sin));
104 : :
105 : : ipv4_connected:
106 [ + - ]: 4 : if (err)
107 : : goto out;
108 : :
109 : 4 : ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
110 : :
111 [ - + ][ # # ]: 4 : if (ipv6_addr_any(&np->saddr) ||
112 : : ipv6_mapped_addr_any(&np->saddr))
113 : 4 : ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
114 : :
115 [ - + ][ # # ]: 4 : if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
116 : : ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
117 : 4 : ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
118 : : &sk->sk_v6_rcv_saddr);
119 [ + - ]: 4 : if (sk->sk_prot->rehash)
120 : 4 : sk->sk_prot->rehash(sk);
121 : : }
122 : :
123 : : goto out;
124 : : }
125 : :
126 [ - + ]: 4 : if (__ipv6_addr_needs_scope_id(addr_type)) {
127 [ # # ][ # # ]: 0 : if (addr_len >= sizeof(struct sockaddr_in6) &&
128 : 0 : usin->sin6_scope_id) {
129 [ # # ][ # # ]: 0 : if (sk->sk_bound_dev_if &&
130 : 0 : sk->sk_bound_dev_if != usin->sin6_scope_id) {
131 : : err = -EINVAL;
132 : : goto out;
133 : : }
134 : 0 : sk->sk_bound_dev_if = usin->sin6_scope_id;
135 : : }
136 : :
137 [ # # ][ # # ]: 0 : if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
138 : 0 : sk->sk_bound_dev_if = np->mcast_oif;
139 : :
140 : : /* Connect to link-local address requires an interface */
141 [ # # ]: 0 : if (!sk->sk_bound_dev_if) {
142 : : err = -EINVAL;
143 : : goto out;
144 : : }
145 : : }
146 : :
147 : 4 : sk->sk_v6_daddr = *daddr;
148 : 4 : np->flow_label = fl6.flowlabel;
149 : :
150 : 4 : inet->inet_dport = usin->sin6_port;
151 : :
152 : : /*
153 : : * Check for a route to destination an obtain the
154 : : * destination cache for it.
155 : : */
156 : :
157 : 4 : fl6.flowi6_proto = sk->sk_protocol;
158 : 4 : fl6.daddr = sk->sk_v6_daddr;
159 : 4 : fl6.saddr = np->saddr;
160 : 4 : fl6.flowi6_oif = sk->sk_bound_dev_if;
161 : 4 : fl6.flowi6_mark = sk->sk_mark;
162 : 4 : fl6.fl6_dport = inet->inet_dport;
163 : 4 : fl6.fl6_sport = inet->inet_sport;
164 : :
165 [ + - ][ - + ]: 4 : if (!fl6.flowi6_oif && (addr_type&IPV6_ADDR_MULTICAST))
166 : 0 : fl6.flowi6_oif = np->mcast_oif;
167 : :
168 : 4 : security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
169 : :
170 [ - + ]: 4 : opt = flowlabel ? flowlabel->opt : np->opt;
171 : 4 : final_p = fl6_update_dst(&fl6, opt, &final);
172 : :
173 : 4 : dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
174 : : err = 0;
175 [ + + ]: 4 : if (IS_ERR(dst)) {
176 : : err = PTR_ERR(dst);
177 : 1 : goto out;
178 : : }
179 : :
180 : : /* source address lookup done in ip6_dst_lookup */
181 : :
182 [ + - ]: 3 : if (ipv6_addr_any(&np->saddr))
183 : 3 : np->saddr = fl6.saddr;
184 : :
185 [ + - ]: 3 : if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
186 : 3 : sk->sk_v6_rcv_saddr = fl6.saddr;
187 : 3 : inet->inet_rcv_saddr = LOOPBACK4_IPV6;
188 [ + - ]: 3 : if (sk->sk_prot->rehash)
189 : 3 : sk->sk_prot->rehash(sk);
190 : : }
191 : :
192 [ + - ]: 3 : ip6_dst_store(sk, dst,
193 : : ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
194 : : &sk->sk_v6_daddr : NULL,
195 : : #ifdef CONFIG_IPV6_SUBTREES
196 : : ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
197 : : &np->saddr :
198 : : #endif
199 : : NULL);
200 : :
201 : 3 : sk->sk_state = TCP_ESTABLISHED;
202 : : out:
203 : : fl6_sock_release(flowlabel);
204 : 8 : return err;
205 : : }
206 : : EXPORT_SYMBOL_GPL(ip6_datagram_connect);
207 : :
208 : 0 : void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
209 : : __be16 port, u32 info, u8 *payload)
210 : : {
211 : : struct ipv6_pinfo *np = inet6_sk(sk);
212 : : struct icmp6hdr *icmph = icmp6_hdr(skb);
213 : : struct sock_exterr_skb *serr;
214 : :
215 [ # # ]: 0 : if (!np->recverr)
216 : : return;
217 : :
218 : 0 : skb = skb_clone(skb, GFP_ATOMIC);
219 [ # # ]: 0 : if (!skb)
220 : : return;
221 : :
222 : 0 : skb->protocol = htons(ETH_P_IPV6);
223 : :
224 : : serr = SKB_EXT_ERR(skb);
225 : 0 : serr->ee.ee_errno = err;
226 : 0 : serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
227 : 0 : serr->ee.ee_type = icmph->icmp6_type;
228 : 0 : serr->ee.ee_code = icmph->icmp6_code;
229 : 0 : serr->ee.ee_pad = 0;
230 : 0 : serr->ee.ee_info = info;
231 : 0 : serr->ee.ee_data = 0;
232 : 0 : serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
233 : : skb_network_header(skb);
234 : 0 : serr->port = port;
235 : :
236 : 0 : __skb_pull(skb, payload - skb->data);
237 : : skb_reset_transport_header(skb);
238 : :
239 [ # # ]: 0 : if (sock_queue_err_skb(sk, skb))
240 : 0 : kfree_skb(skb);
241 : : }
242 : :
243 : 0 : void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
244 : : {
245 : : struct ipv6_pinfo *np = inet6_sk(sk);
246 : : struct sock_exterr_skb *serr;
247 : : struct ipv6hdr *iph;
248 : 0 : struct sk_buff *skb;
249 : :
250 [ # # ]: 0 : if (!np->recverr)
251 : : return;
252 : :
253 : : skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
254 [ # # ]: 0 : if (!skb)
255 : : return;
256 : :
257 : 0 : skb->protocol = htons(ETH_P_IPV6);
258 : :
259 : 0 : skb_put(skb, sizeof(struct ipv6hdr));
260 : : skb_reset_network_header(skb);
261 : : iph = ipv6_hdr(skb);
262 : 0 : iph->daddr = fl6->daddr;
263 : :
264 : : serr = SKB_EXT_ERR(skb);
265 : 0 : serr->ee.ee_errno = err;
266 : 0 : serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
267 : 0 : serr->ee.ee_type = 0;
268 : 0 : serr->ee.ee_code = 0;
269 : 0 : serr->ee.ee_pad = 0;
270 : 0 : serr->ee.ee_info = info;
271 : 0 : serr->ee.ee_data = 0;
272 : 0 : serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
273 : 0 : serr->port = fl6->fl6_dport;
274 : :
275 : 0 : __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
276 : : skb_reset_transport_header(skb);
277 : :
278 [ # # ]: 0 : if (sock_queue_err_skb(sk, skb))
279 : 0 : kfree_skb(skb);
280 : : }
281 : :
282 : 0 : void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
283 : : {
284 : : struct ipv6_pinfo *np = inet6_sk(sk);
285 : : struct ipv6hdr *iph;
286 : 0 : struct sk_buff *skb;
287 : : struct ip6_mtuinfo *mtu_info;
288 : :
289 [ # # ]: 0 : if (!np->rxopt.bits.rxpmtu)
290 : : return;
291 : :
292 : : skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
293 [ # # ]: 0 : if (!skb)
294 : : return;
295 : :
296 : 0 : skb_put(skb, sizeof(struct ipv6hdr));
297 : : skb_reset_network_header(skb);
298 : : iph = ipv6_hdr(skb);
299 : 0 : iph->daddr = fl6->daddr;
300 : :
301 : : mtu_info = IP6CBMTU(skb);
302 : :
303 : 0 : mtu_info->ip6m_mtu = mtu;
304 : 0 : mtu_info->ip6m_addr.sin6_family = AF_INET6;
305 : 0 : mtu_info->ip6m_addr.sin6_port = 0;
306 : 0 : mtu_info->ip6m_addr.sin6_flowinfo = 0;
307 : 0 : mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
308 : 0 : mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
309 : :
310 : 0 : __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
311 : : skb_reset_transport_header(skb);
312 : :
313 : 0 : skb = xchg(&np->rxpmtu, skb);
314 : 0 : kfree_skb(skb);
315 : : }
316 : :
317 : : /*
318 : : * Handle MSG_ERRQUEUE
319 : : */
320 : 0 : int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
321 : : {
322 : : struct ipv6_pinfo *np = inet6_sk(sk);
323 : : struct sock_exterr_skb *serr;
324 : 0 : struct sk_buff *skb, *skb2;
325 : : struct sockaddr_in6 *sin;
326 : : struct {
327 : : struct sock_extended_err ee;
328 : : struct sockaddr_in6 offender;
329 : : } errhdr;
330 : : int err;
331 : : int copied;
332 : :
333 : : err = -EAGAIN;
334 : 0 : skb = skb_dequeue(&sk->sk_error_queue);
335 [ # # ]: 0 : if (skb == NULL)
336 : : goto out;
337 : :
338 : 0 : copied = skb->len;
339 [ # # ]: 0 : if (copied > len) {
340 : 0 : msg->msg_flags |= MSG_TRUNC;
341 : : copied = len;
342 : : }
343 : 0 : err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
344 [ # # ]: 0 : if (err)
345 : : goto out_free_skb;
346 : :
347 : : sock_recv_timestamp(msg, sk, skb);
348 : :
349 : : serr = SKB_EXT_ERR(skb);
350 : :
351 : 0 : sin = (struct sockaddr_in6 *)msg->msg_name;
352 [ # # ]: 0 : if (sin) {
353 : : const unsigned char *nh = skb_network_header(skb);
354 : 0 : sin->sin6_family = AF_INET6;
355 : 0 : sin->sin6_flowinfo = 0;
356 : 0 : sin->sin6_port = serr->port;
357 [ # # ]: 0 : if (skb->protocol == htons(ETH_P_IPV6)) {
358 : 0 : const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
359 : : struct ipv6hdr, daddr);
360 : 0 : sin->sin6_addr = ip6h->daddr;
361 [ # # ]: 0 : if (np->sndflow)
362 : 0 : sin->sin6_flowinfo = ip6_flowinfo(ip6h);
363 : 0 : sin->sin6_scope_id =
364 : 0 : ipv6_iface_scope_id(&sin->sin6_addr,
365 : : IP6CB(skb)->iif);
366 : : } else {
367 : 0 : ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
368 : : &sin->sin6_addr);
369 : 0 : sin->sin6_scope_id = 0;
370 : : }
371 : 0 : *addr_len = sizeof(*sin);
372 : : }
373 : :
374 : 0 : memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
375 : : sin = &errhdr.offender;
376 : 0 : sin->sin6_family = AF_UNSPEC;
377 [ # # ]: 0 : if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
378 : 0 : sin->sin6_family = AF_INET6;
379 : 0 : sin->sin6_flowinfo = 0;
380 : 0 : sin->sin6_port = 0;
381 [ # # ]: 0 : if (skb->protocol == htons(ETH_P_IPV6)) {
382 : 0 : sin->sin6_addr = ipv6_hdr(skb)->saddr;
383 [ # # ]: 0 : if (np->rxopt.all)
384 : 0 : ip6_datagram_recv_ctl(sk, msg, skb);
385 : 0 : sin->sin6_scope_id =
386 : 0 : ipv6_iface_scope_id(&sin->sin6_addr,
387 : : IP6CB(skb)->iif);
388 : : } else {
389 : : struct inet_sock *inet = inet_sk(sk);
390 : :
391 : 0 : ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
392 : : &sin->sin6_addr);
393 : 0 : sin->sin6_scope_id = 0;
394 [ # # ]: 0 : if (inet->cmsg_flags)
395 : 0 : ip_cmsg_recv(msg, skb);
396 : : }
397 : : }
398 : :
399 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
400 : :
401 : : /* Now we could try to dump offended packet options */
402 : :
403 : 0 : msg->msg_flags |= MSG_ERRQUEUE;
404 : : err = copied;
405 : :
406 : : /* Reset and regenerate socket error */
407 : : spin_lock_bh(&sk->sk_error_queue.lock);
408 : 0 : sk->sk_err = 0;
409 [ # # ]: 0 : if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
410 : 0 : sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
411 : : spin_unlock_bh(&sk->sk_error_queue.lock);
412 : 0 : sk->sk_error_report(sk);
413 : : } else {
414 : : spin_unlock_bh(&sk->sk_error_queue.lock);
415 : : }
416 : :
417 : : out_free_skb:
418 : 0 : kfree_skb(skb);
419 : : out:
420 : 0 : return err;
421 : : }
422 : : EXPORT_SYMBOL_GPL(ipv6_recv_error);
423 : :
424 : : /*
425 : : * Handle IPV6_RECVPATHMTU
426 : : */
427 : 0 : int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
428 : : int *addr_len)
429 : : {
430 : : struct ipv6_pinfo *np = inet6_sk(sk);
431 : : struct sk_buff *skb;
432 : : struct sockaddr_in6 *sin;
433 : : struct ip6_mtuinfo mtu_info;
434 : : int err;
435 : : int copied;
436 : :
437 : : err = -EAGAIN;
438 : 0 : skb = xchg(&np->rxpmtu, NULL);
439 [ # # ]: 0 : if (skb == NULL)
440 : : goto out;
441 : :
442 : 0 : copied = skb->len;
443 [ # # ]: 0 : if (copied > len) {
444 : 0 : msg->msg_flags |= MSG_TRUNC;
445 : : copied = len;
446 : : }
447 : 0 : err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
448 [ # # ]: 0 : if (err)
449 : : goto out_free_skb;
450 : :
451 : : sock_recv_timestamp(msg, sk, skb);
452 : :
453 : 0 : memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
454 : :
455 : 0 : sin = (struct sockaddr_in6 *)msg->msg_name;
456 [ # # ]: 0 : if (sin) {
457 : 0 : sin->sin6_family = AF_INET6;
458 : 0 : sin->sin6_flowinfo = 0;
459 : 0 : sin->sin6_port = 0;
460 : 0 : sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
461 : 0 : sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
462 : 0 : *addr_len = sizeof(*sin);
463 : : }
464 : :
465 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
466 : :
467 : : err = copied;
468 : :
469 : : out_free_skb:
470 : 0 : kfree_skb(skb);
471 : : out:
472 : 0 : return err;
473 : : }
474 : :
475 : :
476 : 0 : int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
477 : 0 : struct sk_buff *skb)
478 : : {
479 : : struct ipv6_pinfo *np = inet6_sk(sk);
480 : : struct inet6_skb_parm *opt = IP6CB(skb);
481 : : unsigned char *nh = skb_network_header(skb);
482 : :
483 [ # # ]: 0 : if (np->rxopt.bits.rxinfo) {
484 : : struct in6_pktinfo src_info;
485 : :
486 : 0 : src_info.ipi6_ifindex = opt->iif;
487 : 0 : src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
488 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
489 : : }
490 : :
491 [ # # ]: 0 : if (np->rxopt.bits.rxhlim) {
492 : 0 : int hlim = ipv6_hdr(skb)->hop_limit;
493 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
494 : : }
495 : :
496 [ # # ]: 0 : if (np->rxopt.bits.rxtclass) {
497 : 0 : int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
498 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
499 : : }
500 : :
501 [ # # ]: 0 : if (np->rxopt.bits.rxflow) {
502 : 0 : __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
503 [ # # ]: 0 : if (flowinfo)
504 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
505 : : }
506 : :
507 : : /* HbH is allowed only once */
508 [ # # ][ # # ]: 0 : if (np->rxopt.bits.hopopts && opt->hop) {
509 : 0 : u8 *ptr = nh + opt->hop;
510 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
511 : : }
512 : :
513 [ # # ][ # # ]: 0 : if (opt->lastopt &&
514 : 0 : (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
515 : : /*
516 : : * Silly enough, but we need to reparse in order to
517 : : * report extension headers (except for HbH)
518 : : * in order.
519 : : *
520 : : * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
521 : : * (and WILL NOT be) defined because
522 : : * IPV6_RECVDSTOPTS is more generic. --yoshfuji
523 : : */
524 : : unsigned int off = sizeof(struct ipv6hdr);
525 : 0 : u8 nexthdr = ipv6_hdr(skb)->nexthdr;
526 : :
527 [ # # ]: 0 : while (off <= opt->lastopt) {
528 : : unsigned int len;
529 : 0 : u8 *ptr = nh + off;
530 : :
531 [ # # # # ]: 0 : switch (nexthdr) {
532 : : case IPPROTO_DSTOPTS:
533 : 0 : nexthdr = ptr[0];
534 : 0 : len = (ptr[1] + 1) << 3;
535 [ # # ]: 0 : if (np->rxopt.bits.dstopts)
536 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
537 : : break;
538 : : case IPPROTO_ROUTING:
539 : 0 : nexthdr = ptr[0];
540 : 0 : len = (ptr[1] + 1) << 3;
541 [ # # ]: 0 : if (np->rxopt.bits.srcrt)
542 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
543 : : break;
544 : : case IPPROTO_AH:
545 : 0 : nexthdr = ptr[0];
546 : 0 : len = (ptr[1] + 2) << 2;
547 : 0 : break;
548 : : default:
549 : 0 : nexthdr = ptr[0];
550 : 0 : len = (ptr[1] + 1) << 3;
551 : 0 : break;
552 : : }
553 : :
554 : 0 : off += len;
555 : : }
556 : : }
557 : :
558 : : /* socket options in old style */
559 [ # # ]: 0 : if (np->rxopt.bits.rxoinfo) {
560 : : struct in6_pktinfo src_info;
561 : :
562 : 0 : src_info.ipi6_ifindex = opt->iif;
563 : 0 : src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
564 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
565 : : }
566 [ # # ]: 0 : if (np->rxopt.bits.rxohlim) {
567 : 0 : int hlim = ipv6_hdr(skb)->hop_limit;
568 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
569 : : }
570 [ # # ][ # # ]: 0 : if (np->rxopt.bits.ohopopts && opt->hop) {
571 : 0 : u8 *ptr = nh + opt->hop;
572 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
573 : : }
574 [ # # ][ # # ]: 0 : if (np->rxopt.bits.odstopts && opt->dst0) {
575 : 0 : u8 *ptr = nh + opt->dst0;
576 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
577 : : }
578 [ # # ][ # # ]: 0 : if (np->rxopt.bits.osrcrt && opt->srcrt) {
579 : 0 : struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
580 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
581 : : }
582 [ # # ][ # # ]: 0 : if (np->rxopt.bits.odstopts && opt->dst1) {
583 : 0 : u8 *ptr = nh + opt->dst1;
584 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
585 : : }
586 [ # # ]: 0 : if (np->rxopt.bits.rxorigdstaddr) {
587 : : struct sockaddr_in6 sin6;
588 : : __be16 *ports = (__be16 *) skb_transport_header(skb);
589 : :
590 [ # # ]: 0 : if (skb_transport_offset(skb) + 4 <= skb->len) {
591 : : /* All current transport protocols have the port numbers in the
592 : : * first four bytes of the transport header and this function is
593 : : * written with this assumption in mind.
594 : : */
595 : :
596 : 0 : sin6.sin6_family = AF_INET6;
597 : 0 : sin6.sin6_addr = ipv6_hdr(skb)->daddr;
598 : 0 : sin6.sin6_port = ports[1];
599 : 0 : sin6.sin6_flowinfo = 0;
600 : 0 : sin6.sin6_scope_id =
601 : 0 : ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
602 : : opt->iif);
603 : :
604 : 0 : put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
605 : : }
606 : : }
607 : 0 : return 0;
608 : : }
609 : : EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
610 : :
611 : 0 : int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
612 : 0 : struct msghdr *msg, struct flowi6 *fl6,
613 : : struct ipv6_txoptions *opt,
614 : : int *hlimit, int *tclass, int *dontfrag)
615 : : {
616 : : struct in6_pktinfo *src_info;
617 : : struct cmsghdr *cmsg;
618 : : struct ipv6_rt_hdr *rthdr;
619 : : struct ipv6_opt_hdr *hdr;
620 : : int len;
621 : : int err = 0;
622 : :
623 [ # # ][ # # ]: 0 : for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
624 : : int addr_type;
625 : :
626 [ # # ][ # # ]: 0 : if (!CMSG_OK(msg, cmsg)) {
627 : : err = -EINVAL;
628 : : goto exit_f;
629 : : }
630 : :
631 [ # # ]: 0 : if (cmsg->cmsg_level != SOL_IPV6)
632 : 0 : continue;
633 : :
634 [ # # # # : 0 : switch (cmsg->cmsg_type) {
# # # # #
# ]
635 : : case IPV6_PKTINFO:
636 : : case IPV6_2292PKTINFO:
637 : : {
638 : : struct net_device *dev = NULL;
639 : :
640 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
641 : : err = -EINVAL;
642 : : goto exit_f;
643 : : }
644 : :
645 : : src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
646 : :
647 [ # # ]: 0 : if (src_info->ipi6_ifindex) {
648 [ # # ][ # # ]: 0 : if (fl6->flowi6_oif &&
649 : : src_info->ipi6_ifindex != fl6->flowi6_oif)
650 : : return -EINVAL;
651 : 0 : fl6->flowi6_oif = src_info->ipi6_ifindex;
652 : : }
653 : :
654 : 0 : addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
655 : :
656 : : rcu_read_lock();
657 [ # # ]: 0 : if (fl6->flowi6_oif) {
658 : 0 : dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
659 [ # # ]: 0 : if (!dev) {
660 : : rcu_read_unlock();
661 : 0 : return -ENODEV;
662 : : }
663 [ # # ]: 0 : } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
664 : : rcu_read_unlock();
665 : 0 : return -EINVAL;
666 : : }
667 : :
668 [ # # ]: 0 : if (addr_type != IPV6_ADDR_ANY) {
669 : : int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
670 [ # # ]: 0 : if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
[ # # # # ]
671 [ # # ]: 0 : !ipv6_chk_addr(net, &src_info->ipi6_addr,
672 : : strict ? dev : NULL, 0))
673 : : err = -EINVAL;
674 : : else
675 : 0 : fl6->saddr = src_info->ipi6_addr;
676 : : }
677 : :
678 : : rcu_read_unlock();
679 : :
680 [ # # ]: 0 : if (err)
681 : : goto exit_f;
682 : :
683 : : break;
684 : : }
685 : :
686 : : case IPV6_FLOWINFO:
687 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(4)) {
688 : : err = -EINVAL;
689 : : goto exit_f;
690 : : }
691 : :
692 [ # # ]: 0 : if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
693 [ # # ]: 0 : if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
694 : : err = -EINVAL;
695 : : goto exit_f;
696 : : }
697 : : }
698 : 0 : fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
699 : 0 : break;
700 : :
701 : : case IPV6_2292HOPOPTS:
702 : : case IPV6_HOPOPTS:
703 [ # # ][ # # ]: 0 : if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
704 : : err = -EINVAL;
705 : : goto exit_f;
706 : : }
707 : :
708 : 0 : hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
709 : 0 : len = ((hdr->hdrlen + 1) << 3);
710 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(len)) {
711 : : err = -EINVAL;
712 : : goto exit_f;
713 : : }
714 [ # # ]: 0 : if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
715 : : err = -EPERM;
716 : : goto exit_f;
717 : : }
718 : 0 : opt->opt_nflen += len;
719 : 0 : opt->hopopt = hdr;
720 : 0 : break;
721 : :
722 : : case IPV6_2292DSTOPTS:
723 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
724 : : err = -EINVAL;
725 : : goto exit_f;
726 : : }
727 : :
728 : 0 : hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
729 : 0 : len = ((hdr->hdrlen + 1) << 3);
730 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(len)) {
731 : : err = -EINVAL;
732 : : goto exit_f;
733 : : }
734 [ # # ]: 0 : if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
735 : : err = -EPERM;
736 : : goto exit_f;
737 : : }
738 [ # # ]: 0 : if (opt->dst1opt) {
739 : : err = -EINVAL;
740 : : goto exit_f;
741 : : }
742 : 0 : opt->opt_flen += len;
743 : 0 : opt->dst1opt = hdr;
744 : 0 : break;
745 : :
746 : : case IPV6_DSTOPTS:
747 : : case IPV6_RTHDRDSTOPTS:
748 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
749 : : err = -EINVAL;
750 : : goto exit_f;
751 : : }
752 : :
753 : 0 : hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
754 : 0 : len = ((hdr->hdrlen + 1) << 3);
755 [ # # ]: 0 : if (cmsg->cmsg_len < CMSG_LEN(len)) {
756 : : err = -EINVAL;
757 : : goto exit_f;
758 : : }
759 [ # # ]: 0 : if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
760 : : err = -EPERM;
761 : : goto exit_f;
762 : : }
763 [ # # ]: 0 : if (cmsg->cmsg_type == IPV6_DSTOPTS) {
764 : 0 : opt->opt_flen += len;
765 : 0 : opt->dst1opt = hdr;
766 : : } else {
767 : 0 : opt->opt_nflen += len;
768 : 0 : opt->dst0opt = hdr;
769 : : }
770 : : break;
771 : :
772 : : case IPV6_2292RTHDR:
773 : : case IPV6_RTHDR:
774 : : if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
775 : : err = -EINVAL;
776 : : goto exit_f;
777 : : }
778 : :
779 : : rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
780 : :
781 : : switch (rthdr->type) {
782 : : #if IS_ENABLED(CONFIG_IPV6_MIP6)
783 : : case IPV6_SRCRT_TYPE_2:
784 : : if (rthdr->hdrlen != 2 ||
785 : : rthdr->segments_left != 1) {
786 : : err = -EINVAL;
787 : : goto exit_f;
788 : : }
789 : : break;
790 : : #endif
791 : : default:
792 : : err = -EINVAL;
793 : : goto exit_f;
794 : : }
795 : :
796 : : len = ((rthdr->hdrlen + 1) << 3);
797 : :
798 : : if (cmsg->cmsg_len < CMSG_LEN(len)) {
799 : : err = -EINVAL;
800 : : goto exit_f;
801 : : }
802 : :
803 : : /* segments left must also match */
804 : : if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
805 : : err = -EINVAL;
806 : : goto exit_f;
807 : : }
808 : :
809 : : opt->opt_nflen += len;
810 : : opt->srcrt = rthdr;
811 : :
812 : : if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
813 : : int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
814 : :
815 : : opt->opt_nflen += dsthdrlen;
816 : : opt->dst0opt = opt->dst1opt;
817 : : opt->dst1opt = NULL;
818 : : opt->opt_flen -= dsthdrlen;
819 : : }
820 : :
821 : : break;
822 : :
823 : : case IPV6_2292HOPLIMIT:
824 : : case IPV6_HOPLIMIT:
825 [ # # ]: 0 : if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
826 : : err = -EINVAL;
827 : : goto exit_f;
828 : : }
829 : :
830 : 0 : *hlimit = *(int *)CMSG_DATA(cmsg);
831 [ # # ]: 0 : if (*hlimit < -1 || *hlimit > 0xff) {
832 : : err = -EINVAL;
833 : : goto exit_f;
834 : : }
835 : :
836 : : break;
837 : :
838 : : case IPV6_TCLASS:
839 : : {
840 : : int tc;
841 : :
842 : : err = -EINVAL;
843 [ # # ]: 0 : if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
844 : : goto exit_f;
845 : :
846 : 0 : tc = *(int *)CMSG_DATA(cmsg);
847 [ # # ]: 0 : if (tc < -1 || tc > 0xff)
848 : : goto exit_f;
849 : :
850 : : err = 0;
851 : 0 : *tclass = tc;
852 : :
853 : 0 : break;
854 : : }
855 : :
856 : : case IPV6_DONTFRAG:
857 : : {
858 : : int df;
859 : :
860 : : err = -EINVAL;
861 [ # # ]: 0 : if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
862 : : goto exit_f;
863 : :
864 : 0 : df = *(int *)CMSG_DATA(cmsg);
865 [ # # ]: 0 : if (df < 0 || df > 1)
866 : : goto exit_f;
867 : :
868 : : err = 0;
869 : 0 : *dontfrag = df;
870 : :
871 : 0 : break;
872 : : }
873 : : default:
874 [ # # ][ # # ]: 0 : LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
875 : : cmsg->cmsg_type);
876 : : err = -EINVAL;
877 : : goto exit_f;
878 : : }
879 : : }
880 : :
881 : : exit_f:
882 : 0 : return err;
883 : : }
884 : : EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
885 : :
886 : 0 : void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
887 : : __u16 srcp, __u16 destp, int bucket)
888 : : {
889 : : const struct in6_addr *dest, *src;
890 : :
891 : : dest = &sp->sk_v6_daddr;
892 : : src = &sp->sk_v6_rcv_saddr;
893 : 2 : seq_printf(seq,
894 : : "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
895 : : "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
896 : : bucket,
897 : : src->s6_addr32[0], src->s6_addr32[1],
898 : : src->s6_addr32[2], src->s6_addr32[3], srcp,
899 : : dest->s6_addr32[0], dest->s6_addr32[1],
900 : : dest->s6_addr32[2], dest->s6_addr32[3], destp,
901 : 1 : sp->sk_state,
902 : : sk_wmem_alloc_get(sp),
903 : : sk_rmem_alloc_get(sp),
904 : : 0, 0L, 0,
905 : : from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
906 : : 0,
907 : : sock_i_ino(sp),
908 : : atomic_read(&sp->sk_refcnt), sp,
909 : : atomic_read(&sp->sk_drops));
910 : 1 : }
|