Linux 4.19.133
[linux/fpc-iii.git] / net / ipv6 / raw.c
blob8d19729f851629f50f0387b9862540ee25bee980
1 /*
2 * RAW sockets for IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Adapted from linux/net/ipv4/raw.c
10 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
13 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/slab.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
27 #include <linux/in6.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/icmpv6.h>
31 #include <linux/netfilter.h>
32 #include <linux/netfilter_ipv6.h>
33 #include <linux/skbuff.h>
34 #include <linux/compat.h>
35 #include <linux/uaccess.h>
36 #include <asm/ioctls.h>
38 #include <net/net_namespace.h>
39 #include <net/ip.h>
40 #include <net/sock.h>
41 #include <net/snmp.h>
43 #include <net/ipv6.h>
44 #include <net/ndisc.h>
45 #include <net/protocol.h>
46 #include <net/ip6_route.h>
47 #include <net/ip6_checksum.h>
48 #include <net/addrconf.h>
49 #include <net/transp_v6.h>
50 #include <net/udp.h>
51 #include <net/inet_common.h>
52 #include <net/tcp_states.h>
53 #if IS_ENABLED(CONFIG_IPV6_MIP6)
54 #include <net/mip6.h>
55 #endif
56 #include <linux/mroute6.h>
58 #include <net/raw.h>
59 #include <net/rawv6.h>
60 #include <net/xfrm.h>
62 #include <linux/proc_fs.h>
63 #include <linux/seq_file.h>
64 #include <linux/export.h>
66 #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */
68 struct raw_hashinfo raw_v6_hashinfo = {
69 .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
71 EXPORT_SYMBOL_GPL(raw_v6_hashinfo);
73 struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
74 unsigned short num, const struct in6_addr *loc_addr,
75 const struct in6_addr *rmt_addr, int dif, int sdif)
77 bool is_multicast = ipv6_addr_is_multicast(loc_addr);
79 sk_for_each_from(sk)
80 if (inet_sk(sk)->inet_num == num) {
82 if (!net_eq(sock_net(sk), net))
83 continue;
85 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
86 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
87 continue;
89 if (sk->sk_bound_dev_if &&
90 sk->sk_bound_dev_if != dif &&
91 sk->sk_bound_dev_if != sdif)
92 continue;
94 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
95 if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
96 goto found;
97 if (is_multicast &&
98 inet6_mc_check(sk, loc_addr, rmt_addr))
99 goto found;
100 continue;
102 goto found;
104 sk = NULL;
105 found:
106 return sk;
108 EXPORT_SYMBOL_GPL(__raw_v6_lookup);
111 * 0 - deliver
112 * 1 - block
114 static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb)
116 struct icmp6hdr _hdr;
117 const struct icmp6hdr *hdr;
119 /* We require only the four bytes of the ICMPv6 header, not any
120 * additional bytes of message body in "struct icmp6hdr".
122 hdr = skb_header_pointer(skb, skb_transport_offset(skb),
123 ICMPV6_HDRLEN, &_hdr);
124 if (hdr) {
125 const __u32 *data = &raw6_sk(sk)->filter.data[0];
126 unsigned int type = hdr->icmp6_type;
128 return (data[type >> 5] & (1U << (type & 31))) != 0;
130 return 1;
133 #if IS_ENABLED(CONFIG_IPV6_MIP6)
134 typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb);
136 static mh_filter_t __rcu *mh_filter __read_mostly;
138 int rawv6_mh_filter_register(mh_filter_t filter)
140 rcu_assign_pointer(mh_filter, filter);
141 return 0;
143 EXPORT_SYMBOL(rawv6_mh_filter_register);
145 int rawv6_mh_filter_unregister(mh_filter_t filter)
147 RCU_INIT_POINTER(mh_filter, NULL);
148 synchronize_rcu();
149 return 0;
151 EXPORT_SYMBOL(rawv6_mh_filter_unregister);
153 #endif
156 * demultiplex raw sockets.
157 * (should consider queueing the skb in the sock receive_queue
158 * without calling rawv6.c)
160 * Caller owns SKB so we must make clones.
162 static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
164 const struct in6_addr *saddr;
165 const struct in6_addr *daddr;
166 struct sock *sk;
167 bool delivered = false;
168 __u8 hash;
169 struct net *net;
171 saddr = &ipv6_hdr(skb)->saddr;
172 daddr = saddr + 1;
174 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
176 read_lock(&raw_v6_hashinfo.lock);
177 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
179 if (!sk)
180 goto out;
182 net = dev_net(skb->dev);
183 sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr,
184 inet6_iif(skb), inet6_sdif(skb));
186 while (sk) {
187 int filtered;
189 delivered = true;
190 switch (nexthdr) {
191 case IPPROTO_ICMPV6:
192 filtered = icmpv6_filter(sk, skb);
193 break;
195 #if IS_ENABLED(CONFIG_IPV6_MIP6)
196 case IPPROTO_MH:
198 /* XXX: To validate MH only once for each packet,
199 * this is placed here. It should be after checking
200 * xfrm policy, however it doesn't. The checking xfrm
201 * policy is placed in rawv6_rcv() because it is
202 * required for each socket.
204 mh_filter_t *filter;
206 filter = rcu_dereference(mh_filter);
207 filtered = filter ? (*filter)(sk, skb) : 0;
208 break;
210 #endif
211 default:
212 filtered = 0;
213 break;
216 if (filtered < 0)
217 break;
218 if (filtered == 0) {
219 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
221 /* Not releasing hash table! */
222 if (clone) {
223 nf_reset(clone);
224 rawv6_rcv(sk, clone);
227 sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
228 inet6_iif(skb), inet6_sdif(skb));
230 out:
231 read_unlock(&raw_v6_hashinfo.lock);
232 return delivered;
235 bool raw6_local_deliver(struct sk_buff *skb, int nexthdr)
237 struct sock *raw_sk;
239 raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]);
240 if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
241 raw_sk = NULL;
243 return raw_sk != NULL;
246 /* This cleans up af_inet6 a bit. -DaveM */
247 static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
249 struct inet_sock *inet = inet_sk(sk);
250 struct ipv6_pinfo *np = inet6_sk(sk);
251 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
252 __be32 v4addr = 0;
253 int addr_type;
254 int err;
256 if (addr_len < SIN6_LEN_RFC2133)
257 return -EINVAL;
259 if (addr->sin6_family != AF_INET6)
260 return -EINVAL;
262 addr_type = ipv6_addr_type(&addr->sin6_addr);
264 /* Raw sockets are IPv6 only */
265 if (addr_type == IPV6_ADDR_MAPPED)
266 return -EADDRNOTAVAIL;
268 lock_sock(sk);
270 err = -EINVAL;
271 if (sk->sk_state != TCP_CLOSE)
272 goto out;
274 rcu_read_lock();
275 /* Check if the address belongs to the host. */
276 if (addr_type != IPV6_ADDR_ANY) {
277 struct net_device *dev = NULL;
279 if (__ipv6_addr_needs_scope_id(addr_type)) {
280 if (addr_len >= sizeof(struct sockaddr_in6) &&
281 addr->sin6_scope_id) {
282 /* Override any existing binding, if another
283 * one is supplied by user.
285 sk->sk_bound_dev_if = addr->sin6_scope_id;
288 /* Binding to link-local address requires an interface */
289 if (!sk->sk_bound_dev_if)
290 goto out_unlock;
293 if (sk->sk_bound_dev_if) {
294 err = -ENODEV;
295 dev = dev_get_by_index_rcu(sock_net(sk),
296 sk->sk_bound_dev_if);
297 if (!dev)
298 goto out_unlock;
301 /* ipv4 addr of the socket is invalid. Only the
302 * unspecified and mapped address have a v4 equivalent.
304 v4addr = LOOPBACK4_IPV6;
305 if (!(addr_type & IPV6_ADDR_MULTICAST) &&
306 !sock_net(sk)->ipv6.sysctl.ip_nonlocal_bind) {
307 err = -EADDRNOTAVAIL;
308 if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
309 dev, 0)) {
310 goto out_unlock;
315 inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
316 sk->sk_v6_rcv_saddr = addr->sin6_addr;
317 if (!(addr_type & IPV6_ADDR_MULTICAST))
318 np->saddr = addr->sin6_addr;
319 err = 0;
320 out_unlock:
321 rcu_read_unlock();
322 out:
323 release_sock(sk);
324 return err;
327 static void rawv6_err(struct sock *sk, struct sk_buff *skb,
328 struct inet6_skb_parm *opt,
329 u8 type, u8 code, int offset, __be32 info)
331 struct inet_sock *inet = inet_sk(sk);
332 struct ipv6_pinfo *np = inet6_sk(sk);
333 int err;
334 int harderr;
336 /* Report error on raw socket, if:
337 1. User requested recverr.
338 2. Socket is connected (otherwise the error indication
339 is useless without recverr and error is hard.
341 if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
342 return;
344 harderr = icmpv6_err_convert(type, code, &err);
345 if (type == ICMPV6_PKT_TOOBIG) {
346 ip6_sk_update_pmtu(skb, sk, info);
347 harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
349 if (type == NDISC_REDIRECT) {
350 ip6_sk_redirect(skb, sk);
351 return;
353 if (np->recverr) {
354 u8 *payload = skb->data;
355 if (!inet->hdrincl)
356 payload += offset;
357 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
360 if (np->recverr || harderr) {
361 sk->sk_err = err;
362 sk->sk_error_report(sk);
366 void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
367 u8 type, u8 code, int inner_offset, __be32 info)
369 struct sock *sk;
370 int hash;
371 const struct in6_addr *saddr, *daddr;
372 struct net *net;
374 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
376 read_lock(&raw_v6_hashinfo.lock);
377 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
378 if (sk) {
379 /* Note: ipv6_hdr(skb) != skb->data */
380 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
381 saddr = &ip6h->saddr;
382 daddr = &ip6h->daddr;
383 net = dev_net(skb->dev);
385 while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
386 inet6_iif(skb), inet6_iif(skb)))) {
387 rawv6_err(sk, skb, NULL, type, code,
388 inner_offset, info);
389 sk = sk_next(sk);
392 read_unlock(&raw_v6_hashinfo.lock);
395 static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
397 if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
398 skb_checksum_complete(skb)) {
399 atomic_inc(&sk->sk_drops);
400 kfree_skb(skb);
401 return NET_RX_DROP;
404 /* Charge it to the socket. */
405 skb_dst_drop(skb);
406 if (sock_queue_rcv_skb(sk, skb) < 0) {
407 kfree_skb(skb);
408 return NET_RX_DROP;
411 return 0;
415 * This is next to useless...
416 * if we demultiplex in network layer we don't need the extra call
417 * just to queue the skb...
418 * maybe we could have the network decide upon a hint if it
419 * should call raw_rcv for demultiplexing
421 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
423 struct inet_sock *inet = inet_sk(sk);
424 struct raw6_sock *rp = raw6_sk(sk);
426 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
427 atomic_inc(&sk->sk_drops);
428 kfree_skb(skb);
429 return NET_RX_DROP;
432 if (!rp->checksum)
433 skb->ip_summed = CHECKSUM_UNNECESSARY;
435 if (skb->ip_summed == CHECKSUM_COMPLETE) {
436 skb_postpull_rcsum(skb, skb_network_header(skb),
437 skb_network_header_len(skb));
438 if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
439 &ipv6_hdr(skb)->daddr,
440 skb->len, inet->inet_num, skb->csum))
441 skb->ip_summed = CHECKSUM_UNNECESSARY;
443 if (!skb_csum_unnecessary(skb))
444 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
445 &ipv6_hdr(skb)->daddr,
446 skb->len,
447 inet->inet_num, 0));
449 if (inet->hdrincl) {
450 if (skb_checksum_complete(skb)) {
451 atomic_inc(&sk->sk_drops);
452 kfree_skb(skb);
453 return NET_RX_DROP;
457 rawv6_rcv_skb(sk, skb);
458 return 0;
463 * This should be easy, if there is something there
464 * we return it, otherwise we block.
467 static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
468 int noblock, int flags, int *addr_len)
470 struct ipv6_pinfo *np = inet6_sk(sk);
471 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
472 struct sk_buff *skb;
473 size_t copied;
474 int err;
476 if (flags & MSG_OOB)
477 return -EOPNOTSUPP;
479 if (flags & MSG_ERRQUEUE)
480 return ipv6_recv_error(sk, msg, len, addr_len);
482 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
483 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
485 skb = skb_recv_datagram(sk, flags, noblock, &err);
486 if (!skb)
487 goto out;
489 copied = skb->len;
490 if (copied > len) {
491 copied = len;
492 msg->msg_flags |= MSG_TRUNC;
495 if (skb_csum_unnecessary(skb)) {
496 err = skb_copy_datagram_msg(skb, 0, msg, copied);
497 } else if (msg->msg_flags&MSG_TRUNC) {
498 if (__skb_checksum_complete(skb))
499 goto csum_copy_err;
500 err = skb_copy_datagram_msg(skb, 0, msg, copied);
501 } else {
502 err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
503 if (err == -EINVAL)
504 goto csum_copy_err;
506 if (err)
507 goto out_free;
509 /* Copy the address. */
510 if (sin6) {
511 sin6->sin6_family = AF_INET6;
512 sin6->sin6_port = 0;
513 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
514 sin6->sin6_flowinfo = 0;
515 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
516 inet6_iif(skb));
517 *addr_len = sizeof(*sin6);
520 sock_recv_ts_and_drops(msg, sk, skb);
522 if (np->rxopt.all)
523 ip6_datagram_recv_ctl(sk, msg, skb);
525 err = copied;
526 if (flags & MSG_TRUNC)
527 err = skb->len;
529 out_free:
530 skb_free_datagram(sk, skb);
531 out:
532 return err;
534 csum_copy_err:
535 skb_kill_datagram(sk, skb, flags);
537 /* Error for blocking case is chosen to masquerade
538 as some normal condition.
540 err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
541 goto out;
544 static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
545 struct raw6_sock *rp)
547 struct sk_buff *skb;
548 int err = 0;
549 int offset;
550 int len;
551 int total_len;
552 __wsum tmp_csum;
553 __sum16 csum;
555 if (!rp->checksum)
556 goto send;
558 skb = skb_peek(&sk->sk_write_queue);
559 if (!skb)
560 goto out;
562 offset = rp->offset;
563 total_len = inet_sk(sk)->cork.base.length;
564 if (offset >= total_len - 1) {
565 err = -EINVAL;
566 ip6_flush_pending_frames(sk);
567 goto out;
570 /* should be check HW csum miyazawa */
571 if (skb_queue_len(&sk->sk_write_queue) == 1) {
573 * Only one fragment on the socket.
575 tmp_csum = skb->csum;
576 } else {
577 struct sk_buff *csum_skb = NULL;
578 tmp_csum = 0;
580 skb_queue_walk(&sk->sk_write_queue, skb) {
581 tmp_csum = csum_add(tmp_csum, skb->csum);
583 if (csum_skb)
584 continue;
586 len = skb->len - skb_transport_offset(skb);
587 if (offset >= len) {
588 offset -= len;
589 continue;
592 csum_skb = skb;
595 skb = csum_skb;
598 offset += skb_transport_offset(skb);
599 err = skb_copy_bits(skb, offset, &csum, 2);
600 if (err < 0) {
601 ip6_flush_pending_frames(sk);
602 goto out;
605 /* in case cksum was not initialized */
606 if (unlikely(csum))
607 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
609 csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
610 total_len, fl6->flowi6_proto, tmp_csum);
612 if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
613 csum = CSUM_MANGLED_0;
615 BUG_ON(skb_store_bits(skb, offset, &csum, 2));
617 send:
618 err = ip6_push_pending_frames(sk);
619 out:
620 return err;
623 static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
624 struct flowi6 *fl6, struct dst_entry **dstp,
625 unsigned int flags, const struct sockcm_cookie *sockc)
627 struct ipv6_pinfo *np = inet6_sk(sk);
628 struct net *net = sock_net(sk);
629 struct ipv6hdr *iph;
630 struct sk_buff *skb;
631 int err;
632 struct rt6_info *rt = (struct rt6_info *)*dstp;
633 int hlen = LL_RESERVED_SPACE(rt->dst.dev);
634 int tlen = rt->dst.dev->needed_tailroom;
636 if (length > rt->dst.dev->mtu) {
637 ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
638 return -EMSGSIZE;
640 if (length < sizeof(struct ipv6hdr))
641 return -EINVAL;
642 if (flags&MSG_PROBE)
643 goto out;
645 skb = sock_alloc_send_skb(sk,
646 length + hlen + tlen + 15,
647 flags & MSG_DONTWAIT, &err);
648 if (!skb)
649 goto error;
650 skb_reserve(skb, hlen);
652 skb->protocol = htons(ETH_P_IPV6);
653 skb->priority = sk->sk_priority;
654 skb->mark = sk->sk_mark;
655 skb->tstamp = sockc->transmit_time;
657 skb_put(skb, length);
658 skb_reset_network_header(skb);
659 iph = ipv6_hdr(skb);
661 skb->ip_summed = CHECKSUM_NONE;
663 sock_tx_timestamp(sk, sockc->tsflags, &skb_shinfo(skb)->tx_flags);
665 if (flags & MSG_CONFIRM)
666 skb_set_dst_pending_confirm(skb, 1);
668 skb->transport_header = skb->network_header;
669 err = memcpy_from_msg(iph, msg, length);
670 if (err) {
671 err = -EFAULT;
672 kfree_skb(skb);
673 goto error;
676 skb_dst_set(skb, &rt->dst);
677 *dstp = NULL;
679 /* if egress device is enslaved to an L3 master device pass the
680 * skb to its handler for processing
682 skb = l3mdev_ip6_out(sk, skb);
683 if (unlikely(!skb))
684 return 0;
686 /* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev
687 * in the error path. Since skb has been freed, the dst could
688 * have been queued for deletion.
690 rcu_read_lock();
691 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
692 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
693 NULL, rt->dst.dev, dst_output);
694 if (err > 0)
695 err = net_xmit_errno(err);
696 if (err) {
697 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
698 rcu_read_unlock();
699 goto error_check;
701 rcu_read_unlock();
702 out:
703 return 0;
705 error:
706 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
707 error_check:
708 if (err == -ENOBUFS && !np->recverr)
709 err = 0;
710 return err;
713 struct raw6_frag_vec {
714 struct msghdr *msg;
715 int hlen;
716 char c[4];
719 static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
721 int err = 0;
722 switch (fl6->flowi6_proto) {
723 case IPPROTO_ICMPV6:
724 rfv->hlen = 2;
725 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
726 if (!err) {
727 fl6->fl6_icmp_type = rfv->c[0];
728 fl6->fl6_icmp_code = rfv->c[1];
730 break;
731 case IPPROTO_MH:
732 rfv->hlen = 4;
733 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
734 if (!err)
735 fl6->fl6_mh_type = rfv->c[2];
737 return err;
740 static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
741 struct sk_buff *skb)
743 struct raw6_frag_vec *rfv = from;
745 if (offset < rfv->hlen) {
746 int copy = min(rfv->hlen - offset, len);
748 if (skb->ip_summed == CHECKSUM_PARTIAL)
749 memcpy(to, rfv->c + offset, copy);
750 else
751 skb->csum = csum_block_add(
752 skb->csum,
753 csum_partial_copy_nocheck(rfv->c + offset,
754 to, copy, 0),
755 odd);
757 odd = 0;
758 offset += copy;
759 to += copy;
760 len -= copy;
762 if (!len)
763 return 0;
766 offset -= rfv->hlen;
768 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
771 static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
773 struct ipv6_txoptions *opt_to_free = NULL;
774 struct ipv6_txoptions opt_space;
775 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
776 struct in6_addr *daddr, *final_p, final;
777 struct inet_sock *inet = inet_sk(sk);
778 struct ipv6_pinfo *np = inet6_sk(sk);
779 struct raw6_sock *rp = raw6_sk(sk);
780 struct ipv6_txoptions *opt = NULL;
781 struct ip6_flowlabel *flowlabel = NULL;
782 struct dst_entry *dst = NULL;
783 struct raw6_frag_vec rfv;
784 struct flowi6 fl6;
785 struct ipcm6_cookie ipc6;
786 int addr_len = msg->msg_namelen;
787 int hdrincl;
788 u16 proto;
789 int err;
791 /* Rough check on arithmetic overflow,
792 better check is made in ip6_append_data().
794 if (len > INT_MAX)
795 return -EMSGSIZE;
797 /* Mirror BSD error message compatibility */
798 if (msg->msg_flags & MSG_OOB)
799 return -EOPNOTSUPP;
801 /* hdrincl should be READ_ONCE(inet->hdrincl)
802 * but READ_ONCE() doesn't work with bit fields.
803 * Doing this indirectly yields the same result.
805 hdrincl = inet->hdrincl;
806 hdrincl = READ_ONCE(hdrincl);
809 * Get and verify the address.
811 memset(&fl6, 0, sizeof(fl6));
813 fl6.flowi6_mark = sk->sk_mark;
814 fl6.flowi6_uid = sk->sk_uid;
816 ipcm6_init(&ipc6);
817 ipc6.sockc.tsflags = sk->sk_tsflags;
819 if (sin6) {
820 if (addr_len < SIN6_LEN_RFC2133)
821 return -EINVAL;
823 if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
824 return -EAFNOSUPPORT;
826 /* port is the proto value [0..255] carried in nexthdr */
827 proto = ntohs(sin6->sin6_port);
829 if (!proto)
830 proto = inet->inet_num;
831 else if (proto != inet->inet_num)
832 return -EINVAL;
834 if (proto > 255)
835 return -EINVAL;
837 daddr = &sin6->sin6_addr;
838 if (np->sndflow) {
839 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
840 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
841 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
842 if (!flowlabel)
843 return -EINVAL;
848 * Otherwise it will be difficult to maintain
849 * sk->sk_dst_cache.
851 if (sk->sk_state == TCP_ESTABLISHED &&
852 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
853 daddr = &sk->sk_v6_daddr;
855 if (addr_len >= sizeof(struct sockaddr_in6) &&
856 sin6->sin6_scope_id &&
857 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
858 fl6.flowi6_oif = sin6->sin6_scope_id;
859 } else {
860 if (sk->sk_state != TCP_ESTABLISHED)
861 return -EDESTADDRREQ;
863 proto = inet->inet_num;
864 daddr = &sk->sk_v6_daddr;
865 fl6.flowlabel = np->flow_label;
868 if (fl6.flowi6_oif == 0)
869 fl6.flowi6_oif = sk->sk_bound_dev_if;
871 if (msg->msg_controllen) {
872 opt = &opt_space;
873 memset(opt, 0, sizeof(struct ipv6_txoptions));
874 opt->tot_len = sizeof(struct ipv6_txoptions);
875 ipc6.opt = opt;
877 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
878 if (err < 0) {
879 fl6_sock_release(flowlabel);
880 return err;
882 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
883 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
884 if (!flowlabel)
885 return -EINVAL;
887 if (!(opt->opt_nflen|opt->opt_flen))
888 opt = NULL;
890 if (!opt) {
891 opt = txopt_get(np);
892 opt_to_free = opt;
894 if (flowlabel)
895 opt = fl6_merge_options(&opt_space, flowlabel, opt);
896 opt = ipv6_fixup_options(&opt_space, opt);
898 fl6.flowi6_proto = proto;
900 if (!hdrincl) {
901 rfv.msg = msg;
902 rfv.hlen = 0;
903 err = rawv6_probe_proto_opt(&rfv, &fl6);
904 if (err)
905 goto out;
908 if (!ipv6_addr_any(daddr))
909 fl6.daddr = *daddr;
910 else
911 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
912 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
913 fl6.saddr = np->saddr;
915 final_p = fl6_update_dst(&fl6, opt, &final);
917 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
918 fl6.flowi6_oif = np->mcast_oif;
919 else if (!fl6.flowi6_oif)
920 fl6.flowi6_oif = np->ucast_oif;
921 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
923 if (hdrincl)
924 fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
926 if (ipc6.tclass < 0)
927 ipc6.tclass = np->tclass;
929 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
931 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
932 if (IS_ERR(dst)) {
933 err = PTR_ERR(dst);
934 goto out;
936 if (ipc6.hlimit < 0)
937 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
939 if (ipc6.dontfrag < 0)
940 ipc6.dontfrag = np->dontfrag;
942 if (msg->msg_flags&MSG_CONFIRM)
943 goto do_confirm;
945 back_from_confirm:
946 if (hdrincl)
947 err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst,
948 msg->msg_flags, &ipc6.sockc);
949 else {
950 ipc6.opt = opt;
951 lock_sock(sk);
952 err = ip6_append_data(sk, raw6_getfrag, &rfv,
953 len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
954 msg->msg_flags);
956 if (err)
957 ip6_flush_pending_frames(sk);
958 else if (!(msg->msg_flags & MSG_MORE))
959 err = rawv6_push_pending_frames(sk, &fl6, rp);
960 release_sock(sk);
962 done:
963 dst_release(dst);
964 out:
965 fl6_sock_release(flowlabel);
966 txopt_put(opt_to_free);
967 return err < 0 ? err : len;
968 do_confirm:
969 if (msg->msg_flags & MSG_PROBE)
970 dst_confirm_neigh(dst, &fl6.daddr);
971 if (!(msg->msg_flags & MSG_PROBE) || len)
972 goto back_from_confirm;
973 err = 0;
974 goto done;
977 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
978 char __user *optval, int optlen)
980 switch (optname) {
981 case ICMPV6_FILTER:
982 if (optlen > sizeof(struct icmp6_filter))
983 optlen = sizeof(struct icmp6_filter);
984 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
985 return -EFAULT;
986 return 0;
987 default:
988 return -ENOPROTOOPT;
991 return 0;
994 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
995 char __user *optval, int __user *optlen)
997 int len;
999 switch (optname) {
1000 case ICMPV6_FILTER:
1001 if (get_user(len, optlen))
1002 return -EFAULT;
1003 if (len < 0)
1004 return -EINVAL;
1005 if (len > sizeof(struct icmp6_filter))
1006 len = sizeof(struct icmp6_filter);
1007 if (put_user(len, optlen))
1008 return -EFAULT;
1009 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
1010 return -EFAULT;
1011 return 0;
1012 default:
1013 return -ENOPROTOOPT;
1016 return 0;
1020 static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
1021 char __user *optval, unsigned int optlen)
1023 struct raw6_sock *rp = raw6_sk(sk);
1024 int val;
1026 if (get_user(val, (int __user *)optval))
1027 return -EFAULT;
1029 switch (optname) {
1030 case IPV6_HDRINCL:
1031 if (sk->sk_type != SOCK_RAW)
1032 return -EINVAL;
1033 inet_sk(sk)->hdrincl = !!val;
1034 return 0;
1035 case IPV6_CHECKSUM:
1036 if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
1037 level == IPPROTO_IPV6) {
1039 * RFC3542 tells that IPV6_CHECKSUM socket
1040 * option in the IPPROTO_IPV6 level is not
1041 * allowed on ICMPv6 sockets.
1042 * If you want to set it, use IPPROTO_RAW
1043 * level IPV6_CHECKSUM socket option
1044 * (Linux extension).
1046 return -EINVAL;
1049 /* You may get strange result with a positive odd offset;
1050 RFC2292bis agrees with me. */
1051 if (val > 0 && (val&1))
1052 return -EINVAL;
1053 if (val < 0) {
1054 rp->checksum = 0;
1055 } else {
1056 rp->checksum = 1;
1057 rp->offset = val;
1060 return 0;
1062 default:
1063 return -ENOPROTOOPT;
1067 static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1068 char __user *optval, unsigned int optlen)
1070 switch (level) {
1071 case SOL_RAW:
1072 break;
1074 case SOL_ICMPV6:
1075 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1076 return -EOPNOTSUPP;
1077 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1078 case SOL_IPV6:
1079 if (optname == IPV6_CHECKSUM ||
1080 optname == IPV6_HDRINCL)
1081 break;
1082 /* fall through */
1083 default:
1084 return ipv6_setsockopt(sk, level, optname, optval, optlen);
1087 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1090 #ifdef CONFIG_COMPAT
1091 static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
1092 char __user *optval, unsigned int optlen)
1094 switch (level) {
1095 case SOL_RAW:
1096 break;
1097 case SOL_ICMPV6:
1098 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1099 return -EOPNOTSUPP;
1100 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1101 case SOL_IPV6:
1102 if (optname == IPV6_CHECKSUM ||
1103 optname == IPV6_HDRINCL)
1104 break;
1105 /* fall through */
1106 default:
1107 return compat_ipv6_setsockopt(sk, level, optname,
1108 optval, optlen);
1110 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1112 #endif
1114 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
1115 char __user *optval, int __user *optlen)
1117 struct raw6_sock *rp = raw6_sk(sk);
1118 int val, len;
1120 if (get_user(len, optlen))
1121 return -EFAULT;
1123 switch (optname) {
1124 case IPV6_HDRINCL:
1125 val = inet_sk(sk)->hdrincl;
1126 break;
1127 case IPV6_CHECKSUM:
1129 * We allow getsockopt() for IPPROTO_IPV6-level
1130 * IPV6_CHECKSUM socket option on ICMPv6 sockets
1131 * since RFC3542 is silent about it.
1133 if (rp->checksum == 0)
1134 val = -1;
1135 else
1136 val = rp->offset;
1137 break;
1139 default:
1140 return -ENOPROTOOPT;
1143 len = min_t(unsigned int, sizeof(int), len);
1145 if (put_user(len, optlen))
1146 return -EFAULT;
1147 if (copy_to_user(optval, &val, len))
1148 return -EFAULT;
1149 return 0;
1152 static int rawv6_getsockopt(struct sock *sk, int level, int optname,
1153 char __user *optval, int __user *optlen)
1155 switch (level) {
1156 case SOL_RAW:
1157 break;
1159 case SOL_ICMPV6:
1160 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1161 return -EOPNOTSUPP;
1162 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1163 case SOL_IPV6:
1164 if (optname == IPV6_CHECKSUM ||
1165 optname == IPV6_HDRINCL)
1166 break;
1167 /* fall through */
1168 default:
1169 return ipv6_getsockopt(sk, level, optname, optval, optlen);
1172 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1175 #ifdef CONFIG_COMPAT
1176 static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
1177 char __user *optval, int __user *optlen)
1179 switch (level) {
1180 case SOL_RAW:
1181 break;
1182 case SOL_ICMPV6:
1183 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1184 return -EOPNOTSUPP;
1185 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1186 case SOL_IPV6:
1187 if (optname == IPV6_CHECKSUM ||
1188 optname == IPV6_HDRINCL)
1189 break;
1190 /* fall through */
1191 default:
1192 return compat_ipv6_getsockopt(sk, level, optname,
1193 optval, optlen);
1195 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1197 #endif
1199 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
1201 switch (cmd) {
1202 case SIOCOUTQ: {
1203 int amount = sk_wmem_alloc_get(sk);
1205 return put_user(amount, (int __user *)arg);
1207 case SIOCINQ: {
1208 struct sk_buff *skb;
1209 int amount = 0;
1211 spin_lock_bh(&sk->sk_receive_queue.lock);
1212 skb = skb_peek(&sk->sk_receive_queue);
1213 if (skb)
1214 amount = skb->len;
1215 spin_unlock_bh(&sk->sk_receive_queue.lock);
1216 return put_user(amount, (int __user *)arg);
1219 default:
1220 #ifdef CONFIG_IPV6_MROUTE
1221 return ip6mr_ioctl(sk, cmd, (void __user *)arg);
1222 #else
1223 return -ENOIOCTLCMD;
1224 #endif
1228 #ifdef CONFIG_COMPAT
1229 static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1231 switch (cmd) {
1232 case SIOCOUTQ:
1233 case SIOCINQ:
1234 return -ENOIOCTLCMD;
1235 default:
1236 #ifdef CONFIG_IPV6_MROUTE
1237 return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1238 #else
1239 return -ENOIOCTLCMD;
1240 #endif
1243 #endif
1245 static void rawv6_close(struct sock *sk, long timeout)
1247 if (inet_sk(sk)->inet_num == IPPROTO_RAW)
1248 ip6_ra_control(sk, -1);
1249 ip6mr_sk_done(sk);
1250 sk_common_release(sk);
1253 static void raw6_destroy(struct sock *sk)
1255 lock_sock(sk);
1256 ip6_flush_pending_frames(sk);
1257 release_sock(sk);
1259 inet6_destroy_sock(sk);
1262 static int rawv6_init_sk(struct sock *sk)
1264 struct raw6_sock *rp = raw6_sk(sk);
1266 switch (inet_sk(sk)->inet_num) {
1267 case IPPROTO_ICMPV6:
1268 rp->checksum = 1;
1269 rp->offset = 2;
1270 break;
1271 case IPPROTO_MH:
1272 rp->checksum = 1;
1273 rp->offset = 4;
1274 break;
1275 default:
1276 break;
1278 return 0;
1281 struct proto rawv6_prot = {
1282 .name = "RAWv6",
1283 .owner = THIS_MODULE,
1284 .close = rawv6_close,
1285 .destroy = raw6_destroy,
1286 .connect = ip6_datagram_connect_v6_only,
1287 .disconnect = __udp_disconnect,
1288 .ioctl = rawv6_ioctl,
1289 .init = rawv6_init_sk,
1290 .setsockopt = rawv6_setsockopt,
1291 .getsockopt = rawv6_getsockopt,
1292 .sendmsg = rawv6_sendmsg,
1293 .recvmsg = rawv6_recvmsg,
1294 .bind = rawv6_bind,
1295 .backlog_rcv = rawv6_rcv_skb,
1296 .hash = raw_hash_sk,
1297 .unhash = raw_unhash_sk,
1298 .obj_size = sizeof(struct raw6_sock),
1299 .useroffset = offsetof(struct raw6_sock, filter),
1300 .usersize = sizeof_field(struct raw6_sock, filter),
1301 .h.raw_hash = &raw_v6_hashinfo,
1302 #ifdef CONFIG_COMPAT
1303 .compat_setsockopt = compat_rawv6_setsockopt,
1304 .compat_getsockopt = compat_rawv6_getsockopt,
1305 .compat_ioctl = compat_rawv6_ioctl,
1306 #endif
1307 .diag_destroy = raw_abort,
1310 #ifdef CONFIG_PROC_FS
1311 static int raw6_seq_show(struct seq_file *seq, void *v)
1313 if (v == SEQ_START_TOKEN) {
1314 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1315 } else {
1316 struct sock *sp = v;
1317 __u16 srcp = inet_sk(sp)->inet_num;
1318 ip6_dgram_sock_seq_show(seq, v, srcp, 0,
1319 raw_seq_private(seq)->bucket);
1321 return 0;
1324 static const struct seq_operations raw6_seq_ops = {
1325 .start = raw_seq_start,
1326 .next = raw_seq_next,
1327 .stop = raw_seq_stop,
1328 .show = raw6_seq_show,
1331 static int __net_init raw6_init_net(struct net *net)
1333 if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops,
1334 sizeof(struct raw_iter_state), &raw_v6_hashinfo))
1335 return -ENOMEM;
1337 return 0;
1340 static void __net_exit raw6_exit_net(struct net *net)
1342 remove_proc_entry("raw6", net->proc_net);
1345 static struct pernet_operations raw6_net_ops = {
1346 .init = raw6_init_net,
1347 .exit = raw6_exit_net,
1350 int __init raw6_proc_init(void)
1352 return register_pernet_subsys(&raw6_net_ops);
1355 void raw6_proc_exit(void)
1357 unregister_pernet_subsys(&raw6_net_ops);
1359 #endif /* CONFIG_PROC_FS */
1361 /* Same as inet6_dgram_ops, sans udp_poll. */
1362 const struct proto_ops inet6_sockraw_ops = {
1363 .family = PF_INET6,
1364 .owner = THIS_MODULE,
1365 .release = inet6_release,
1366 .bind = inet6_bind,
1367 .connect = inet_dgram_connect, /* ok */
1368 .socketpair = sock_no_socketpair, /* a do nothing */
1369 .accept = sock_no_accept, /* a do nothing */
1370 .getname = inet6_getname,
1371 .poll = datagram_poll, /* ok */
1372 .ioctl = inet6_ioctl, /* must change */
1373 .listen = sock_no_listen, /* ok */
1374 .shutdown = inet_shutdown, /* ok */
1375 .setsockopt = sock_common_setsockopt, /* ok */
1376 .getsockopt = sock_common_getsockopt, /* ok */
1377 .sendmsg = inet_sendmsg, /* ok */
1378 .recvmsg = sock_common_recvmsg, /* ok */
1379 .mmap = sock_no_mmap,
1380 .sendpage = sock_no_sendpage,
1381 #ifdef CONFIG_COMPAT
1382 .compat_setsockopt = compat_sock_common_setsockopt,
1383 .compat_getsockopt = compat_sock_common_getsockopt,
1384 #endif
1387 static struct inet_protosw rawv6_protosw = {
1388 .type = SOCK_RAW,
1389 .protocol = IPPROTO_IP, /* wild card */
1390 .prot = &rawv6_prot,
1391 .ops = &inet6_sockraw_ops,
1392 .flags = INET_PROTOSW_REUSE,
1395 int __init rawv6_init(void)
1397 return inet6_register_protosw(&rawv6_protosw);
1400 void rawv6_exit(void)
1402 inet6_unregister_protosw(&rawv6_protosw);