Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / ipv6 / raw.c
blob4c25339b19843501a3887fd6d6975953a57ec84d
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;
292 err = -ENODEV;
293 dev = dev_get_by_index_rcu(sock_net(sk),
294 sk->sk_bound_dev_if);
295 if (!dev)
296 goto out_unlock;
299 /* ipv4 addr of the socket is invalid. Only the
300 * unspecified and mapped address have a v4 equivalent.
302 v4addr = LOOPBACK4_IPV6;
303 if (!(addr_type & IPV6_ADDR_MULTICAST) &&
304 !sock_net(sk)->ipv6.sysctl.ip_nonlocal_bind) {
305 err = -EADDRNOTAVAIL;
306 if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
307 dev, 0)) {
308 goto out_unlock;
313 inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
314 sk->sk_v6_rcv_saddr = addr->sin6_addr;
315 if (!(addr_type & IPV6_ADDR_MULTICAST))
316 np->saddr = addr->sin6_addr;
317 err = 0;
318 out_unlock:
319 rcu_read_unlock();
320 out:
321 release_sock(sk);
322 return err;
325 static void rawv6_err(struct sock *sk, struct sk_buff *skb,
326 struct inet6_skb_parm *opt,
327 u8 type, u8 code, int offset, __be32 info)
329 struct inet_sock *inet = inet_sk(sk);
330 struct ipv6_pinfo *np = inet6_sk(sk);
331 int err;
332 int harderr;
334 /* Report error on raw socket, if:
335 1. User requested recverr.
336 2. Socket is connected (otherwise the error indication
337 is useless without recverr and error is hard.
339 if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
340 return;
342 harderr = icmpv6_err_convert(type, code, &err);
343 if (type == ICMPV6_PKT_TOOBIG) {
344 ip6_sk_update_pmtu(skb, sk, info);
345 harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
347 if (type == NDISC_REDIRECT) {
348 ip6_sk_redirect(skb, sk);
349 return;
351 if (np->recverr) {
352 u8 *payload = skb->data;
353 if (!inet->hdrincl)
354 payload += offset;
355 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
358 if (np->recverr || harderr) {
359 sk->sk_err = err;
360 sk->sk_error_report(sk);
364 void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
365 u8 type, u8 code, int inner_offset, __be32 info)
367 struct sock *sk;
368 int hash;
369 const struct in6_addr *saddr, *daddr;
370 struct net *net;
372 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
374 read_lock(&raw_v6_hashinfo.lock);
375 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
376 if (sk) {
377 /* Note: ipv6_hdr(skb) != skb->data */
378 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
379 saddr = &ip6h->saddr;
380 daddr = &ip6h->daddr;
381 net = dev_net(skb->dev);
383 while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
384 inet6_iif(skb), inet6_iif(skb)))) {
385 rawv6_err(sk, skb, NULL, type, code,
386 inner_offset, info);
387 sk = sk_next(sk);
390 read_unlock(&raw_v6_hashinfo.lock);
393 static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
395 if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
396 skb_checksum_complete(skb)) {
397 atomic_inc(&sk->sk_drops);
398 kfree_skb(skb);
399 return NET_RX_DROP;
402 /* Charge it to the socket. */
403 skb_dst_drop(skb);
404 if (sock_queue_rcv_skb(sk, skb) < 0) {
405 kfree_skb(skb);
406 return NET_RX_DROP;
409 return 0;
413 * This is next to useless...
414 * if we demultiplex in network layer we don't need the extra call
415 * just to queue the skb...
416 * maybe we could have the network decide upon a hint if it
417 * should call raw_rcv for demultiplexing
419 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
421 struct inet_sock *inet = inet_sk(sk);
422 struct raw6_sock *rp = raw6_sk(sk);
424 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
425 atomic_inc(&sk->sk_drops);
426 kfree_skb(skb);
427 return NET_RX_DROP;
430 if (!rp->checksum)
431 skb->ip_summed = CHECKSUM_UNNECESSARY;
433 if (skb->ip_summed == CHECKSUM_COMPLETE) {
434 skb_postpull_rcsum(skb, skb_network_header(skb),
435 skb_network_header_len(skb));
436 if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
437 &ipv6_hdr(skb)->daddr,
438 skb->len, inet->inet_num, skb->csum))
439 skb->ip_summed = CHECKSUM_UNNECESSARY;
441 if (!skb_csum_unnecessary(skb))
442 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
443 &ipv6_hdr(skb)->daddr,
444 skb->len,
445 inet->inet_num, 0));
447 if (inet->hdrincl) {
448 if (skb_checksum_complete(skb)) {
449 atomic_inc(&sk->sk_drops);
450 kfree_skb(skb);
451 return NET_RX_DROP;
455 rawv6_rcv_skb(sk, skb);
456 return 0;
461 * This should be easy, if there is something there
462 * we return it, otherwise we block.
465 static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
466 int noblock, int flags, int *addr_len)
468 struct ipv6_pinfo *np = inet6_sk(sk);
469 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
470 struct sk_buff *skb;
471 size_t copied;
472 int err;
474 if (flags & MSG_OOB)
475 return -EOPNOTSUPP;
477 if (flags & MSG_ERRQUEUE)
478 return ipv6_recv_error(sk, msg, len, addr_len);
480 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
481 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
483 skb = skb_recv_datagram(sk, flags, noblock, &err);
484 if (!skb)
485 goto out;
487 copied = skb->len;
488 if (copied > len) {
489 copied = len;
490 msg->msg_flags |= MSG_TRUNC;
493 if (skb_csum_unnecessary(skb)) {
494 err = skb_copy_datagram_msg(skb, 0, msg, copied);
495 } else if (msg->msg_flags&MSG_TRUNC) {
496 if (__skb_checksum_complete(skb))
497 goto csum_copy_err;
498 err = skb_copy_datagram_msg(skb, 0, msg, copied);
499 } else {
500 err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
501 if (err == -EINVAL)
502 goto csum_copy_err;
504 if (err)
505 goto out_free;
507 /* Copy the address. */
508 if (sin6) {
509 sin6->sin6_family = AF_INET6;
510 sin6->sin6_port = 0;
511 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
512 sin6->sin6_flowinfo = 0;
513 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
514 inet6_iif(skb));
515 *addr_len = sizeof(*sin6);
518 sock_recv_ts_and_drops(msg, sk, skb);
520 if (np->rxopt.all)
521 ip6_datagram_recv_ctl(sk, msg, skb);
523 err = copied;
524 if (flags & MSG_TRUNC)
525 err = skb->len;
527 out_free:
528 skb_free_datagram(sk, skb);
529 out:
530 return err;
532 csum_copy_err:
533 skb_kill_datagram(sk, skb, flags);
535 /* Error for blocking case is chosen to masquerade
536 as some normal condition.
538 err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
539 goto out;
542 static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
543 struct raw6_sock *rp)
545 struct sk_buff *skb;
546 int err = 0;
547 int offset;
548 int len;
549 int total_len;
550 __wsum tmp_csum;
551 __sum16 csum;
553 if (!rp->checksum)
554 goto send;
556 skb = skb_peek(&sk->sk_write_queue);
557 if (!skb)
558 goto out;
560 offset = rp->offset;
561 total_len = inet_sk(sk)->cork.base.length;
562 if (offset >= total_len - 1) {
563 err = -EINVAL;
564 ip6_flush_pending_frames(sk);
565 goto out;
568 /* should be check HW csum miyazawa */
569 if (skb_queue_len(&sk->sk_write_queue) == 1) {
571 * Only one fragment on the socket.
573 tmp_csum = skb->csum;
574 } else {
575 struct sk_buff *csum_skb = NULL;
576 tmp_csum = 0;
578 skb_queue_walk(&sk->sk_write_queue, skb) {
579 tmp_csum = csum_add(tmp_csum, skb->csum);
581 if (csum_skb)
582 continue;
584 len = skb->len - skb_transport_offset(skb);
585 if (offset >= len) {
586 offset -= len;
587 continue;
590 csum_skb = skb;
593 skb = csum_skb;
596 offset += skb_transport_offset(skb);
597 err = skb_copy_bits(skb, offset, &csum, 2);
598 if (err < 0) {
599 ip6_flush_pending_frames(sk);
600 goto out;
603 /* in case cksum was not initialized */
604 if (unlikely(csum))
605 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
607 csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
608 total_len, fl6->flowi6_proto, tmp_csum);
610 if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
611 csum = CSUM_MANGLED_0;
613 BUG_ON(skb_store_bits(skb, offset, &csum, 2));
615 send:
616 err = ip6_push_pending_frames(sk);
617 out:
618 return err;
621 static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
622 struct flowi6 *fl6, struct dst_entry **dstp,
623 unsigned int flags)
625 struct ipv6_pinfo *np = inet6_sk(sk);
626 struct net *net = sock_net(sk);
627 struct ipv6hdr *iph;
628 struct sk_buff *skb;
629 int err;
630 struct rt6_info *rt = (struct rt6_info *)*dstp;
631 int hlen = LL_RESERVED_SPACE(rt->dst.dev);
632 int tlen = rt->dst.dev->needed_tailroom;
634 if (length > rt->dst.dev->mtu) {
635 ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
636 return -EMSGSIZE;
638 if (length < sizeof(struct ipv6hdr))
639 return -EINVAL;
640 if (flags&MSG_PROBE)
641 goto out;
643 skb = sock_alloc_send_skb(sk,
644 length + hlen + tlen + 15,
645 flags & MSG_DONTWAIT, &err);
646 if (!skb)
647 goto error;
648 skb_reserve(skb, hlen);
650 skb->protocol = htons(ETH_P_IPV6);
651 skb->priority = sk->sk_priority;
652 skb->mark = sk->sk_mark;
653 skb_dst_set(skb, &rt->dst);
654 *dstp = NULL;
656 skb_put(skb, length);
657 skb_reset_network_header(skb);
658 iph = ipv6_hdr(skb);
660 skb->ip_summed = CHECKSUM_NONE;
662 if (flags & MSG_CONFIRM)
663 skb_set_dst_pending_confirm(skb, 1);
665 skb->transport_header = skb->network_header;
666 err = memcpy_from_msg(iph, msg, length);
667 if (err)
668 goto error_fault;
670 /* if egress device is enslaved to an L3 master device pass the
671 * skb to its handler for processing
673 skb = l3mdev_ip6_out(sk, skb);
674 if (unlikely(!skb))
675 return 0;
677 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
678 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
679 NULL, rt->dst.dev, dst_output);
680 if (err > 0)
681 err = net_xmit_errno(err);
682 if (err)
683 goto error;
684 out:
685 return 0;
687 error_fault:
688 err = -EFAULT;
689 kfree_skb(skb);
690 error:
691 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
692 if (err == -ENOBUFS && !np->recverr)
693 err = 0;
694 return err;
697 struct raw6_frag_vec {
698 struct msghdr *msg;
699 int hlen;
700 char c[4];
703 static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
705 int err = 0;
706 switch (fl6->flowi6_proto) {
707 case IPPROTO_ICMPV6:
708 rfv->hlen = 2;
709 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
710 if (!err) {
711 fl6->fl6_icmp_type = rfv->c[0];
712 fl6->fl6_icmp_code = rfv->c[1];
714 break;
715 case IPPROTO_MH:
716 rfv->hlen = 4;
717 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
718 if (!err)
719 fl6->fl6_mh_type = rfv->c[2];
721 return err;
724 static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
725 struct sk_buff *skb)
727 struct raw6_frag_vec *rfv = from;
729 if (offset < rfv->hlen) {
730 int copy = min(rfv->hlen - offset, len);
732 if (skb->ip_summed == CHECKSUM_PARTIAL)
733 memcpy(to, rfv->c + offset, copy);
734 else
735 skb->csum = csum_block_add(
736 skb->csum,
737 csum_partial_copy_nocheck(rfv->c + offset,
738 to, copy, 0),
739 odd);
741 odd = 0;
742 offset += copy;
743 to += copy;
744 len -= copy;
746 if (!len)
747 return 0;
750 offset -= rfv->hlen;
752 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
755 static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
757 struct ipv6_txoptions *opt_to_free = NULL;
758 struct ipv6_txoptions opt_space;
759 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
760 struct in6_addr *daddr, *final_p, final;
761 struct inet_sock *inet = inet_sk(sk);
762 struct ipv6_pinfo *np = inet6_sk(sk);
763 struct raw6_sock *rp = raw6_sk(sk);
764 struct ipv6_txoptions *opt = NULL;
765 struct ip6_flowlabel *flowlabel = NULL;
766 struct dst_entry *dst = NULL;
767 struct raw6_frag_vec rfv;
768 struct flowi6 fl6;
769 struct sockcm_cookie sockc;
770 struct ipcm6_cookie ipc6;
771 int addr_len = msg->msg_namelen;
772 u16 proto;
773 int err;
775 /* Rough check on arithmetic overflow,
776 better check is made in ip6_append_data().
778 if (len > INT_MAX)
779 return -EMSGSIZE;
781 /* Mirror BSD error message compatibility */
782 if (msg->msg_flags & MSG_OOB)
783 return -EOPNOTSUPP;
786 * Get and verify the address.
788 memset(&fl6, 0, sizeof(fl6));
790 fl6.flowi6_mark = sk->sk_mark;
791 fl6.flowi6_uid = sk->sk_uid;
793 ipc6.hlimit = -1;
794 ipc6.tclass = -1;
795 ipc6.dontfrag = -1;
796 ipc6.opt = NULL;
798 if (sin6) {
799 if (addr_len < SIN6_LEN_RFC2133)
800 return -EINVAL;
802 if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
803 return -EAFNOSUPPORT;
805 /* port is the proto value [0..255] carried in nexthdr */
806 proto = ntohs(sin6->sin6_port);
808 if (!proto)
809 proto = inet->inet_num;
810 else if (proto != inet->inet_num)
811 return -EINVAL;
813 if (proto > 255)
814 return -EINVAL;
816 daddr = &sin6->sin6_addr;
817 if (np->sndflow) {
818 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
819 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
820 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
821 if (!flowlabel)
822 return -EINVAL;
827 * Otherwise it will be difficult to maintain
828 * sk->sk_dst_cache.
830 if (sk->sk_state == TCP_ESTABLISHED &&
831 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
832 daddr = &sk->sk_v6_daddr;
834 if (addr_len >= sizeof(struct sockaddr_in6) &&
835 sin6->sin6_scope_id &&
836 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
837 fl6.flowi6_oif = sin6->sin6_scope_id;
838 } else {
839 if (sk->sk_state != TCP_ESTABLISHED)
840 return -EDESTADDRREQ;
842 proto = inet->inet_num;
843 daddr = &sk->sk_v6_daddr;
844 fl6.flowlabel = np->flow_label;
847 if (fl6.flowi6_oif == 0)
848 fl6.flowi6_oif = sk->sk_bound_dev_if;
850 sockc.tsflags = sk->sk_tsflags;
851 if (msg->msg_controllen) {
852 opt = &opt_space;
853 memset(opt, 0, sizeof(struct ipv6_txoptions));
854 opt->tot_len = sizeof(struct ipv6_txoptions);
855 ipc6.opt = opt;
857 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
858 if (err < 0) {
859 fl6_sock_release(flowlabel);
860 return err;
862 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
863 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
864 if (!flowlabel)
865 return -EINVAL;
867 if (!(opt->opt_nflen|opt->opt_flen))
868 opt = NULL;
870 if (!opt) {
871 opt = txopt_get(np);
872 opt_to_free = opt;
874 if (flowlabel)
875 opt = fl6_merge_options(&opt_space, flowlabel, opt);
876 opt = ipv6_fixup_options(&opt_space, opt);
878 fl6.flowi6_proto = proto;
879 rfv.msg = msg;
880 rfv.hlen = 0;
881 err = rawv6_probe_proto_opt(&rfv, &fl6);
882 if (err)
883 goto out;
885 if (!ipv6_addr_any(daddr))
886 fl6.daddr = *daddr;
887 else
888 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
889 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
890 fl6.saddr = np->saddr;
892 final_p = fl6_update_dst(&fl6, opt, &final);
894 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
895 fl6.flowi6_oif = np->mcast_oif;
896 else if (!fl6.flowi6_oif)
897 fl6.flowi6_oif = np->ucast_oif;
898 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
900 if (inet->hdrincl)
901 fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
903 if (ipc6.tclass < 0)
904 ipc6.tclass = np->tclass;
906 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
908 dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
909 if (IS_ERR(dst)) {
910 err = PTR_ERR(dst);
911 goto out;
913 if (ipc6.hlimit < 0)
914 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
916 if (ipc6.dontfrag < 0)
917 ipc6.dontfrag = np->dontfrag;
919 if (msg->msg_flags&MSG_CONFIRM)
920 goto do_confirm;
922 back_from_confirm:
923 if (inet->hdrincl)
924 err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, msg->msg_flags);
925 else {
926 ipc6.opt = opt;
927 lock_sock(sk);
928 err = ip6_append_data(sk, raw6_getfrag, &rfv,
929 len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
930 msg->msg_flags, &sockc);
932 if (err)
933 ip6_flush_pending_frames(sk);
934 else if (!(msg->msg_flags & MSG_MORE))
935 err = rawv6_push_pending_frames(sk, &fl6, rp);
936 release_sock(sk);
938 done:
939 dst_release(dst);
940 out:
941 fl6_sock_release(flowlabel);
942 txopt_put(opt_to_free);
943 return err < 0 ? err : len;
944 do_confirm:
945 if (msg->msg_flags & MSG_PROBE)
946 dst_confirm_neigh(dst, &fl6.daddr);
947 if (!(msg->msg_flags & MSG_PROBE) || len)
948 goto back_from_confirm;
949 err = 0;
950 goto done;
953 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
954 char __user *optval, int optlen)
956 switch (optname) {
957 case ICMPV6_FILTER:
958 if (optlen > sizeof(struct icmp6_filter))
959 optlen = sizeof(struct icmp6_filter);
960 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
961 return -EFAULT;
962 return 0;
963 default:
964 return -ENOPROTOOPT;
967 return 0;
970 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
971 char __user *optval, int __user *optlen)
973 int len;
975 switch (optname) {
976 case ICMPV6_FILTER:
977 if (get_user(len, optlen))
978 return -EFAULT;
979 if (len < 0)
980 return -EINVAL;
981 if (len > sizeof(struct icmp6_filter))
982 len = sizeof(struct icmp6_filter);
983 if (put_user(len, optlen))
984 return -EFAULT;
985 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
986 return -EFAULT;
987 return 0;
988 default:
989 return -ENOPROTOOPT;
992 return 0;
996 static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
997 char __user *optval, unsigned int optlen)
999 struct raw6_sock *rp = raw6_sk(sk);
1000 int val;
1002 if (get_user(val, (int __user *)optval))
1003 return -EFAULT;
1005 switch (optname) {
1006 case IPV6_HDRINCL:
1007 if (sk->sk_type != SOCK_RAW)
1008 return -EINVAL;
1009 inet_sk(sk)->hdrincl = !!val;
1010 return 0;
1011 case IPV6_CHECKSUM:
1012 if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
1013 level == IPPROTO_IPV6) {
1015 * RFC3542 tells that IPV6_CHECKSUM socket
1016 * option in the IPPROTO_IPV6 level is not
1017 * allowed on ICMPv6 sockets.
1018 * If you want to set it, use IPPROTO_RAW
1019 * level IPV6_CHECKSUM socket option
1020 * (Linux extension).
1022 return -EINVAL;
1025 /* You may get strange result with a positive odd offset;
1026 RFC2292bis agrees with me. */
1027 if (val > 0 && (val&1))
1028 return -EINVAL;
1029 if (val < 0) {
1030 rp->checksum = 0;
1031 } else {
1032 rp->checksum = 1;
1033 rp->offset = val;
1036 return 0;
1038 default:
1039 return -ENOPROTOOPT;
1043 static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1044 char __user *optval, unsigned int optlen)
1046 switch (level) {
1047 case SOL_RAW:
1048 break;
1050 case SOL_ICMPV6:
1051 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1052 return -EOPNOTSUPP;
1053 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1054 case SOL_IPV6:
1055 if (optname == IPV6_CHECKSUM ||
1056 optname == IPV6_HDRINCL)
1057 break;
1058 /* fall through */
1059 default:
1060 return ipv6_setsockopt(sk, level, optname, optval, optlen);
1063 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1066 #ifdef CONFIG_COMPAT
1067 static int compat_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;
1073 case SOL_ICMPV6:
1074 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1075 return -EOPNOTSUPP;
1076 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1077 case SOL_IPV6:
1078 if (optname == IPV6_CHECKSUM ||
1079 optname == IPV6_HDRINCL)
1080 break;
1081 /* fall through */
1082 default:
1083 return compat_ipv6_setsockopt(sk, level, optname,
1084 optval, optlen);
1086 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1088 #endif
1090 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
1091 char __user *optval, int __user *optlen)
1093 struct raw6_sock *rp = raw6_sk(sk);
1094 int val, len;
1096 if (get_user(len, optlen))
1097 return -EFAULT;
1099 switch (optname) {
1100 case IPV6_HDRINCL:
1101 val = inet_sk(sk)->hdrincl;
1102 break;
1103 case IPV6_CHECKSUM:
1105 * We allow getsockopt() for IPPROTO_IPV6-level
1106 * IPV6_CHECKSUM socket option on ICMPv6 sockets
1107 * since RFC3542 is silent about it.
1109 if (rp->checksum == 0)
1110 val = -1;
1111 else
1112 val = rp->offset;
1113 break;
1115 default:
1116 return -ENOPROTOOPT;
1119 len = min_t(unsigned int, sizeof(int), len);
1121 if (put_user(len, optlen))
1122 return -EFAULT;
1123 if (copy_to_user(optval, &val, len))
1124 return -EFAULT;
1125 return 0;
1128 static int rawv6_getsockopt(struct sock *sk, int level, int optname,
1129 char __user *optval, int __user *optlen)
1131 switch (level) {
1132 case SOL_RAW:
1133 break;
1135 case SOL_ICMPV6:
1136 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1137 return -EOPNOTSUPP;
1138 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1139 case SOL_IPV6:
1140 if (optname == IPV6_CHECKSUM ||
1141 optname == IPV6_HDRINCL)
1142 break;
1143 /* fall through */
1144 default:
1145 return ipv6_getsockopt(sk, level, optname, optval, optlen);
1148 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1151 #ifdef CONFIG_COMPAT
1152 static int compat_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;
1158 case SOL_ICMPV6:
1159 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1160 return -EOPNOTSUPP;
1161 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1162 case SOL_IPV6:
1163 if (optname == IPV6_CHECKSUM ||
1164 optname == IPV6_HDRINCL)
1165 break;
1166 /* fall through */
1167 default:
1168 return compat_ipv6_getsockopt(sk, level, optname,
1169 optval, optlen);
1171 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1173 #endif
1175 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
1177 switch (cmd) {
1178 case SIOCOUTQ: {
1179 int amount = sk_wmem_alloc_get(sk);
1181 return put_user(amount, (int __user *)arg);
1183 case SIOCINQ: {
1184 struct sk_buff *skb;
1185 int amount = 0;
1187 spin_lock_bh(&sk->sk_receive_queue.lock);
1188 skb = skb_peek(&sk->sk_receive_queue);
1189 if (skb)
1190 amount = skb->len;
1191 spin_unlock_bh(&sk->sk_receive_queue.lock);
1192 return put_user(amount, (int __user *)arg);
1195 default:
1196 #ifdef CONFIG_IPV6_MROUTE
1197 return ip6mr_ioctl(sk, cmd, (void __user *)arg);
1198 #else
1199 return -ENOIOCTLCMD;
1200 #endif
1204 #ifdef CONFIG_COMPAT
1205 static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1207 switch (cmd) {
1208 case SIOCOUTQ:
1209 case SIOCINQ:
1210 return -ENOIOCTLCMD;
1211 default:
1212 #ifdef CONFIG_IPV6_MROUTE
1213 return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1214 #else
1215 return -ENOIOCTLCMD;
1216 #endif
1219 #endif
1221 static void rawv6_close(struct sock *sk, long timeout)
1223 if (inet_sk(sk)->inet_num == IPPROTO_RAW)
1224 ip6_ra_control(sk, -1);
1225 ip6mr_sk_done(sk);
1226 sk_common_release(sk);
1229 static void raw6_destroy(struct sock *sk)
1231 lock_sock(sk);
1232 ip6_flush_pending_frames(sk);
1233 release_sock(sk);
1235 inet6_destroy_sock(sk);
1238 static int rawv6_init_sk(struct sock *sk)
1240 struct raw6_sock *rp = raw6_sk(sk);
1242 switch (inet_sk(sk)->inet_num) {
1243 case IPPROTO_ICMPV6:
1244 rp->checksum = 1;
1245 rp->offset = 2;
1246 break;
1247 case IPPROTO_MH:
1248 rp->checksum = 1;
1249 rp->offset = 4;
1250 break;
1251 default:
1252 break;
1254 return 0;
1257 struct proto rawv6_prot = {
1258 .name = "RAWv6",
1259 .owner = THIS_MODULE,
1260 .close = rawv6_close,
1261 .destroy = raw6_destroy,
1262 .connect = ip6_datagram_connect_v6_only,
1263 .disconnect = __udp_disconnect,
1264 .ioctl = rawv6_ioctl,
1265 .init = rawv6_init_sk,
1266 .setsockopt = rawv6_setsockopt,
1267 .getsockopt = rawv6_getsockopt,
1268 .sendmsg = rawv6_sendmsg,
1269 .recvmsg = rawv6_recvmsg,
1270 .bind = rawv6_bind,
1271 .backlog_rcv = rawv6_rcv_skb,
1272 .hash = raw_hash_sk,
1273 .unhash = raw_unhash_sk,
1274 .obj_size = sizeof(struct raw6_sock),
1275 .useroffset = offsetof(struct raw6_sock, filter),
1276 .usersize = sizeof_field(struct raw6_sock, filter),
1277 .h.raw_hash = &raw_v6_hashinfo,
1278 #ifdef CONFIG_COMPAT
1279 .compat_setsockopt = compat_rawv6_setsockopt,
1280 .compat_getsockopt = compat_rawv6_getsockopt,
1281 .compat_ioctl = compat_rawv6_ioctl,
1282 #endif
1283 .diag_destroy = raw_abort,
1286 #ifdef CONFIG_PROC_FS
1287 static int raw6_seq_show(struct seq_file *seq, void *v)
1289 if (v == SEQ_START_TOKEN) {
1290 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1291 } else {
1292 struct sock *sp = v;
1293 __u16 srcp = inet_sk(sp)->inet_num;
1294 ip6_dgram_sock_seq_show(seq, v, srcp, 0,
1295 raw_seq_private(seq)->bucket);
1297 return 0;
1300 static const struct seq_operations raw6_seq_ops = {
1301 .start = raw_seq_start,
1302 .next = raw_seq_next,
1303 .stop = raw_seq_stop,
1304 .show = raw6_seq_show,
1307 static int raw6_seq_open(struct inode *inode, struct file *file)
1309 return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
1312 static const struct file_operations raw6_seq_fops = {
1313 .open = raw6_seq_open,
1314 .read = seq_read,
1315 .llseek = seq_lseek,
1316 .release = seq_release_net,
1319 static int __net_init raw6_init_net(struct net *net)
1321 if (!proc_create("raw6", S_IRUGO, net->proc_net, &raw6_seq_fops))
1322 return -ENOMEM;
1324 return 0;
1327 static void __net_exit raw6_exit_net(struct net *net)
1329 remove_proc_entry("raw6", net->proc_net);
1332 static struct pernet_operations raw6_net_ops = {
1333 .init = raw6_init_net,
1334 .exit = raw6_exit_net,
1337 int __init raw6_proc_init(void)
1339 return register_pernet_subsys(&raw6_net_ops);
1342 void raw6_proc_exit(void)
1344 unregister_pernet_subsys(&raw6_net_ops);
1346 #endif /* CONFIG_PROC_FS */
1348 /* Same as inet6_dgram_ops, sans udp_poll. */
1349 const struct proto_ops inet6_sockraw_ops = {
1350 .family = PF_INET6,
1351 .owner = THIS_MODULE,
1352 .release = inet6_release,
1353 .bind = inet6_bind,
1354 .connect = inet_dgram_connect, /* ok */
1355 .socketpair = sock_no_socketpair, /* a do nothing */
1356 .accept = sock_no_accept, /* a do nothing */
1357 .getname = inet6_getname,
1358 .poll = datagram_poll, /* ok */
1359 .ioctl = inet6_ioctl, /* must change */
1360 .listen = sock_no_listen, /* ok */
1361 .shutdown = inet_shutdown, /* ok */
1362 .setsockopt = sock_common_setsockopt, /* ok */
1363 .getsockopt = sock_common_getsockopt, /* ok */
1364 .sendmsg = inet_sendmsg, /* ok */
1365 .recvmsg = sock_common_recvmsg, /* ok */
1366 .mmap = sock_no_mmap,
1367 .sendpage = sock_no_sendpage,
1368 #ifdef CONFIG_COMPAT
1369 .compat_setsockopt = compat_sock_common_setsockopt,
1370 .compat_getsockopt = compat_sock_common_getsockopt,
1371 #endif
1374 static struct inet_protosw rawv6_protosw = {
1375 .type = SOCK_RAW,
1376 .protocol = IPPROTO_IP, /* wild card */
1377 .prot = &rawv6_prot,
1378 .ops = &inet6_sockraw_ops,
1379 .flags = INET_PROTOSW_REUSE,
1382 int __init rawv6_init(void)
1384 return inet6_register_protosw(&rawv6_protosw);
1387 void rawv6_exit(void)
1389 inet6_unregister_protosw(&rawv6_protosw);