3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Adapted from linux/net/ipv4/raw.c
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>
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>
51 #include <net/inet_common.h>
52 #include <net/tcp_states.h>
53 #if IS_ENABLED(CONFIG_IPV6_MIP6)
56 #include <linux/mroute6.h>
59 #include <net/rawv6.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);
80 if (inet_sk(sk)->inet_num == num) {
82 if (!net_eq(sock_net(sk), net))
85 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
86 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
89 if (sk->sk_bound_dev_if &&
90 sk->sk_bound_dev_if != dif &&
91 sk->sk_bound_dev_if != sdif)
94 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
95 if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
98 inet6_mc_check(sk, loc_addr, rmt_addr))
108 EXPORT_SYMBOL_GPL(__raw_v6_lookup);
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);
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;
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);
143 EXPORT_SYMBOL(rawv6_mh_filter_register);
145 int rawv6_mh_filter_unregister(mh_filter_t filter)
147 RCU_INIT_POINTER(mh_filter, NULL);
151 EXPORT_SYMBOL(rawv6_mh_filter_unregister);
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;
167 bool delivered = false;
171 saddr = &ipv6_hdr(skb)->saddr;
174 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
176 read_lock(&raw_v6_hashinfo.lock);
177 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
182 net = dev_net(skb->dev);
183 sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr,
184 inet6_iif(skb), inet6_sdif(skb));
192 filtered = icmpv6_filter(sk, skb);
195 #if IS_ENABLED(CONFIG_IPV6_MIP6)
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.
206 filter = rcu_dereference(mh_filter);
207 filtered = filter ? (*filter)(sk, skb) : 0;
219 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
221 /* Not releasing hash table! */
224 rawv6_rcv(sk, clone);
227 sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
228 inet6_iif(skb), inet6_sdif(skb));
231 read_unlock(&raw_v6_hashinfo.lock);
235 bool raw6_local_deliver(struct sk_buff *skb, int nexthdr)
239 raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]);
240 if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
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;
256 if (addr_len < SIN6_LEN_RFC2133)
259 if (addr->sin6_family != AF_INET6)
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;
271 if (sk->sk_state != TCP_CLOSE)
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)
293 dev = dev_get_by_index_rcu(sock_net(sk),
294 sk->sk_bound_dev_if);
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,
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;
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);
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)
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);
352 u8 *payload = skb->data;
355 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
358 if (np->recverr || harderr) {
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)
369 const struct in6_addr *saddr, *daddr;
372 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
374 read_lock(&raw_v6_hashinfo.lock);
375 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
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,
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);
402 /* Charge it to the socket. */
404 if (sock_queue_rcv_skb(sk, skb) < 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);
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,
448 if (skb_checksum_complete(skb)) {
449 atomic_inc(&sk->sk_drops);
455 rawv6_rcv_skb(sk, skb);
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);
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);
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))
498 err = skb_copy_datagram_msg(skb, 0, msg, copied);
500 err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
507 /* Copy the address. */
509 sin6->sin6_family = AF_INET6;
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,
515 *addr_len = sizeof(*sin6);
518 sock_recv_ts_and_drops(msg, sk, skb);
521 ip6_datagram_recv_ctl(sk, msg, skb);
524 if (flags & MSG_TRUNC)
528 skb_free_datagram(sk, skb);
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;
542 static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
543 struct raw6_sock *rp)
556 skb = skb_peek(&sk->sk_write_queue);
561 total_len = inet_sk(sk)->cork.base.length;
562 if (offset >= total_len - 1) {
564 ip6_flush_pending_frames(sk);
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;
575 struct sk_buff *csum_skb = NULL;
578 skb_queue_walk(&sk->sk_write_queue, skb) {
579 tmp_csum = csum_add(tmp_csum, skb->csum);
584 len = skb->len - skb_transport_offset(skb);
596 offset += skb_transport_offset(skb);
597 err = skb_copy_bits(skb, offset, &csum, 2);
599 ip6_flush_pending_frames(sk);
603 /* in case cksum was not initialized */
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));
616 err = ip6_push_pending_frames(sk);
621 static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
622 struct flowi6 *fl6, struct dst_entry **dstp,
625 struct ipv6_pinfo *np = inet6_sk(sk);
626 struct net *net = sock_net(sk);
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);
638 if (length < sizeof(struct ipv6hdr))
643 skb = sock_alloc_send_skb(sk,
644 length + hlen + tlen + 15,
645 flags & MSG_DONTWAIT, &err);
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);
656 skb_put(skb, length);
657 skb_reset_network_header(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);
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);
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);
681 err = net_xmit_errno(err);
691 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
692 if (err == -ENOBUFS && !np->recverr)
697 struct raw6_frag_vec {
703 static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
706 switch (fl6->flowi6_proto) {
709 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
711 fl6->fl6_icmp_type = rfv->c[0];
712 fl6->fl6_icmp_code = rfv->c[1];
717 err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
719 fl6->fl6_mh_type = rfv->c[2];
724 static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
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);
735 skb->csum = csum_block_add(
737 csum_partial_copy_nocheck(rfv->c + offset,
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;
769 struct sockcm_cookie sockc;
770 struct ipcm6_cookie ipc6;
771 int addr_len = msg->msg_namelen;
775 /* Rough check on arithmetic overflow,
776 better check is made in ip6_append_data().
781 /* Mirror BSD error message compatibility */
782 if (msg->msg_flags & MSG_OOB)
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;
799 if (addr_len < SIN6_LEN_RFC2133)
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);
809 proto = inet->inet_num;
810 else if (proto != inet->inet_num)
816 daddr = &sin6->sin6_addr;
818 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
819 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
820 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
827 * Otherwise it will be difficult to maintain
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;
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) {
853 memset(opt, 0, sizeof(struct ipv6_txoptions));
854 opt->tot_len = sizeof(struct ipv6_txoptions);
857 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
859 fl6_sock_release(flowlabel);
862 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
863 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
867 if (!(opt->opt_nflen|opt->opt_flen))
875 opt = fl6_merge_options(&opt_space, flowlabel, opt);
876 opt = ipv6_fixup_options(&opt_space, opt);
878 fl6.flowi6_proto = proto;
881 err = rawv6_probe_proto_opt(&rfv, &fl6);
885 if (!ipv6_addr_any(daddr))
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));
901 fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
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);
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)
924 err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, msg->msg_flags);
928 err = ip6_append_data(sk, raw6_getfrag, &rfv,
929 len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
930 msg->msg_flags, &sockc);
933 ip6_flush_pending_frames(sk);
934 else if (!(msg->msg_flags & MSG_MORE))
935 err = rawv6_push_pending_frames(sk, &fl6, rp);
941 fl6_sock_release(flowlabel);
942 txopt_put(opt_to_free);
943 return err < 0 ? err : len;
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;
953 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
954 char __user *optval, int optlen)
958 if (optlen > sizeof(struct icmp6_filter))
959 optlen = sizeof(struct icmp6_filter);
960 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
970 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
971 char __user *optval, int __user *optlen)
977 if (get_user(len, optlen))
981 if (len > sizeof(struct icmp6_filter))
982 len = sizeof(struct icmp6_filter);
983 if (put_user(len, optlen))
985 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
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);
1002 if (get_user(val, (int __user *)optval))
1007 if (sk->sk_type != SOCK_RAW)
1009 inet_sk(sk)->hdrincl = !!val;
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).
1025 /* You may get strange result with a positive odd offset;
1026 RFC2292bis agrees with me. */
1027 if (val > 0 && (val&1))
1039 return -ENOPROTOOPT;
1043 static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1044 char __user *optval, unsigned int optlen)
1051 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1053 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1055 if (optname == IPV6_CHECKSUM ||
1056 optname == IPV6_HDRINCL)
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)
1074 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1076 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1078 if (optname == IPV6_CHECKSUM ||
1079 optname == IPV6_HDRINCL)
1083 return compat_ipv6_setsockopt(sk, level, optname,
1086 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
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);
1096 if (get_user(len, optlen))
1101 val = inet_sk(sk)->hdrincl;
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)
1116 return -ENOPROTOOPT;
1119 len = min_t(unsigned int, sizeof(int), len);
1121 if (put_user(len, optlen))
1123 if (copy_to_user(optval, &val, len))
1128 static int rawv6_getsockopt(struct sock *sk, int level, int optname,
1129 char __user *optval, int __user *optlen)
1136 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1138 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1140 if (optname == IPV6_CHECKSUM ||
1141 optname == IPV6_HDRINCL)
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)
1159 if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1161 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1163 if (optname == IPV6_CHECKSUM ||
1164 optname == IPV6_HDRINCL)
1168 return compat_ipv6_getsockopt(sk, level, optname,
1171 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1175 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
1179 int amount = sk_wmem_alloc_get(sk);
1181 return put_user(amount, (int __user *)arg);
1184 struct sk_buff *skb;
1187 spin_lock_bh(&sk->sk_receive_queue.lock);
1188 skb = skb_peek(&sk->sk_receive_queue);
1191 spin_unlock_bh(&sk->sk_receive_queue.lock);
1192 return put_user(amount, (int __user *)arg);
1196 #ifdef CONFIG_IPV6_MROUTE
1197 return ip6mr_ioctl(sk, cmd, (void __user *)arg);
1199 return -ENOIOCTLCMD;
1204 #ifdef CONFIG_COMPAT
1205 static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1210 return -ENOIOCTLCMD;
1212 #ifdef CONFIG_IPV6_MROUTE
1213 return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1215 return -ENOIOCTLCMD;
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);
1226 sk_common_release(sk);
1229 static void raw6_destroy(struct sock *sk)
1232 ip6_flush_pending_frames(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:
1257 struct proto rawv6_prot = {
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,
1271 .backlog_rcv = rawv6_rcv_skb,
1272 .hash = raw_hash_sk,
1273 .unhash = raw_unhash_sk,
1274 .obj_size = sizeof(struct raw6_sock),
1275 .h.raw_hash = &raw_v6_hashinfo,
1276 #ifdef CONFIG_COMPAT
1277 .compat_setsockopt = compat_rawv6_setsockopt,
1278 .compat_getsockopt = compat_rawv6_getsockopt,
1279 .compat_ioctl = compat_rawv6_ioctl,
1281 .diag_destroy = raw_abort,
1284 #ifdef CONFIG_PROC_FS
1285 static int raw6_seq_show(struct seq_file *seq, void *v)
1287 if (v == SEQ_START_TOKEN) {
1288 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1290 struct sock *sp = v;
1291 __u16 srcp = inet_sk(sp)->inet_num;
1292 ip6_dgram_sock_seq_show(seq, v, srcp, 0,
1293 raw_seq_private(seq)->bucket);
1298 static const struct seq_operations raw6_seq_ops = {
1299 .start = raw_seq_start,
1300 .next = raw_seq_next,
1301 .stop = raw_seq_stop,
1302 .show = raw6_seq_show,
1305 static int raw6_seq_open(struct inode *inode, struct file *file)
1307 return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
1310 static const struct file_operations raw6_seq_fops = {
1311 .owner = THIS_MODULE,
1312 .open = raw6_seq_open,
1314 .llseek = seq_lseek,
1315 .release = seq_release_net,
1318 static int __net_init raw6_init_net(struct net *net)
1320 if (!proc_create("raw6", S_IRUGO, net->proc_net, &raw6_seq_fops))
1326 static void __net_exit raw6_exit_net(struct net *net)
1328 remove_proc_entry("raw6", net->proc_net);
1331 static struct pernet_operations raw6_net_ops = {
1332 .init = raw6_init_net,
1333 .exit = raw6_exit_net,
1336 int __init raw6_proc_init(void)
1338 return register_pernet_subsys(&raw6_net_ops);
1341 void raw6_proc_exit(void)
1343 unregister_pernet_subsys(&raw6_net_ops);
1345 #endif /* CONFIG_PROC_FS */
1347 /* Same as inet6_dgram_ops, sans udp_poll. */
1348 const struct proto_ops inet6_sockraw_ops = {
1350 .owner = THIS_MODULE,
1351 .release = inet6_release,
1353 .connect = inet_dgram_connect, /* ok */
1354 .socketpair = sock_no_socketpair, /* a do nothing */
1355 .accept = sock_no_accept, /* a do nothing */
1356 .getname = inet6_getname,
1357 .poll = datagram_poll, /* ok */
1358 .ioctl = inet6_ioctl, /* must change */
1359 .listen = sock_no_listen, /* ok */
1360 .shutdown = inet_shutdown, /* ok */
1361 .setsockopt = sock_common_setsockopt, /* ok */
1362 .getsockopt = sock_common_getsockopt, /* ok */
1363 .sendmsg = inet_sendmsg, /* ok */
1364 .recvmsg = sock_common_recvmsg, /* ok */
1365 .mmap = sock_no_mmap,
1366 .sendpage = sock_no_sendpage,
1367 #ifdef CONFIG_COMPAT
1368 .compat_setsockopt = compat_sock_common_setsockopt,
1369 .compat_getsockopt = compat_sock_common_getsockopt,
1373 static struct inet_protosw rawv6_protosw = {
1375 .protocol = IPPROTO_IP, /* wild card */
1376 .prot = &rawv6_prot,
1377 .ops = &inet6_sockraw_ops,
1378 .flags = INET_PROTOSW_REUSE,
1381 int __init rawv6_init(void)
1383 return inet6_register_protosw(&rawv6_protosw);
1386 void rawv6_exit(void)
1388 inet6_unregister_protosw(&rawv6_protosw);