3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Adapted from linux/net/ipv4/raw.c
10 * $Id: raw.c,v 1.51 2002/02/01 22:01:04 davem Exp $
13 * Hideaki YOSHIFUJI : sin6_scope_id support
14 * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/netfilter.h>
33 #include <linux/netfilter_ipv6.h>
34 #include <linux/skbuff.h>
35 #include <asm/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 defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
58 #include <net/rawv6.h>
61 #include <linux/proc_fs.h>
62 #include <linux/seq_file.h>
64 static struct raw_hashinfo raw_v6_hashinfo
= {
65 .lock
= __RW_LOCK_UNLOCKED(),
68 static void raw_v6_hash(struct sock
*sk
)
70 raw_hash_sk(sk
, &raw_v6_hashinfo
);
73 static void raw_v6_unhash(struct sock
*sk
)
75 raw_unhash_sk(sk
, &raw_v6_hashinfo
);
79 static struct sock
*__raw_v6_lookup(struct sock
*sk
, unsigned short num
,
80 struct in6_addr
*loc_addr
, struct in6_addr
*rmt_addr
, int dif
)
82 struct hlist_node
*node
;
83 int is_multicast
= ipv6_addr_is_multicast(loc_addr
);
85 sk_for_each_from(sk
, node
)
86 if (inet_sk(sk
)->num
== num
) {
87 struct ipv6_pinfo
*np
= inet6_sk(sk
);
89 if (!ipv6_addr_any(&np
->daddr
) &&
90 !ipv6_addr_equal(&np
->daddr
, rmt_addr
))
93 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= dif
)
96 if (!ipv6_addr_any(&np
->rcv_saddr
)) {
97 if (ipv6_addr_equal(&np
->rcv_saddr
, loc_addr
))
100 inet6_mc_check(sk
, loc_addr
, rmt_addr
))
115 static __inline__
int icmpv6_filter(struct sock
*sk
, struct sk_buff
*skb
)
117 struct icmp6hdr
*icmph
;
118 struct raw6_sock
*rp
= raw6_sk(sk
);
120 if (pskb_may_pull(skb
, sizeof(struct icmp6hdr
))) {
121 __u32
*data
= &rp
->filter
.data
[0];
124 icmph
= (struct icmp6hdr
*) skb
->data
;
125 bit_nr
= icmph
->icmp6_type
;
127 return (data
[bit_nr
>> 5] & (1 << (bit_nr
& 31))) != 0;
132 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
133 static int (*mh_filter
)(struct sock
*sock
, struct sk_buff
*skb
);
135 int rawv6_mh_filter_register(int (*filter
)(struct sock
*sock
,
136 struct sk_buff
*skb
))
138 rcu_assign_pointer(mh_filter
, filter
);
141 EXPORT_SYMBOL(rawv6_mh_filter_register
);
143 int rawv6_mh_filter_unregister(int (*filter
)(struct sock
*sock
,
144 struct sk_buff
*skb
))
146 rcu_assign_pointer(mh_filter
, NULL
);
150 EXPORT_SYMBOL(rawv6_mh_filter_unregister
);
155 * demultiplex raw sockets.
156 * (should consider queueing the skb in the sock receive_queue
157 * without calling rawv6.c)
159 * Caller owns SKB so we must make clones.
161 static int ipv6_raw_deliver(struct sk_buff
*skb
, int nexthdr
)
163 struct in6_addr
*saddr
;
164 struct in6_addr
*daddr
;
169 saddr
= &ipv6_hdr(skb
)->saddr
;
172 hash
= nexthdr
& (MAX_INET_PROTOS
- 1);
174 read_lock(&raw_v6_hashinfo
.lock
);
175 sk
= sk_head(&raw_v6_hashinfo
.ht
[hash
]);
178 * The first socket found will be delivered after
179 * delivery to transport protocols.
185 sk
= __raw_v6_lookup(sk
, nexthdr
, daddr
, saddr
, IP6CB(skb
)->iif
);
193 filtered
= icmpv6_filter(sk
, skb
);
196 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
199 /* XXX: To validate MH only once for each packet,
200 * this is placed here. It should be after checking
201 * xfrm policy, however it doesn't. The checking xfrm
202 * policy is placed in rawv6_rcv() because it is
203 * required for each socket.
205 int (*filter
)(struct sock
*sock
, struct sk_buff
*skb
);
207 filter
= rcu_dereference(mh_filter
);
208 filtered
= filter
? filter(sk
, skb
) : 0;
220 struct sk_buff
*clone
= skb_clone(skb
, GFP_ATOMIC
);
222 /* Not releasing hash table! */
225 rawv6_rcv(sk
, clone
);
228 sk
= __raw_v6_lookup(sk_next(sk
), nexthdr
, daddr
, saddr
,
232 read_unlock(&raw_v6_hashinfo
.lock
);
236 int raw6_local_deliver(struct sk_buff
*skb
, int nexthdr
)
240 raw_sk
= sk_head(&raw_v6_hashinfo
.ht
[nexthdr
& (MAX_INET_PROTOS
- 1)]);
241 if (raw_sk
&& !ipv6_raw_deliver(skb
, nexthdr
))
244 return raw_sk
!= NULL
;
247 /* This cleans up af_inet6 a bit. -DaveM */
248 static int rawv6_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int addr_len
)
250 struct inet_sock
*inet
= inet_sk(sk
);
251 struct ipv6_pinfo
*np
= inet6_sk(sk
);
252 struct sockaddr_in6
*addr
= (struct sockaddr_in6
*) uaddr
;
257 if (addr_len
< SIN6_LEN_RFC2133
)
259 addr_type
= ipv6_addr_type(&addr
->sin6_addr
);
261 /* Raw sockets are IPv6 only */
262 if (addr_type
== IPV6_ADDR_MAPPED
)
263 return(-EADDRNOTAVAIL
);
268 if (sk
->sk_state
!= TCP_CLOSE
)
271 /* Check if the address belongs to the host. */
272 if (addr_type
!= IPV6_ADDR_ANY
) {
273 struct net_device
*dev
= NULL
;
275 if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
276 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
277 addr
->sin6_scope_id
) {
278 /* Override any existing binding, if another
279 * one is supplied by user.
281 sk
->sk_bound_dev_if
= addr
->sin6_scope_id
;
284 /* Binding to link-local address requires an interface */
285 if (!sk
->sk_bound_dev_if
)
288 dev
= dev_get_by_index(&init_net
, sk
->sk_bound_dev_if
);
295 /* ipv4 addr of the socket is invalid. Only the
296 * unspecified and mapped address have a v4 equivalent.
298 v4addr
= LOOPBACK4_IPV6
;
299 if (!(addr_type
& IPV6_ADDR_MULTICAST
)) {
300 err
= -EADDRNOTAVAIL
;
301 if (!ipv6_chk_addr(&addr
->sin6_addr
, dev
, 0)) {
311 inet
->rcv_saddr
= inet
->saddr
= v4addr
;
312 ipv6_addr_copy(&np
->rcv_saddr
, &addr
->sin6_addr
);
313 if (!(addr_type
& IPV6_ADDR_MULTICAST
))
314 ipv6_addr_copy(&np
->saddr
, &addr
->sin6_addr
);
321 static void rawv6_err(struct sock
*sk
, struct sk_buff
*skb
,
322 struct inet6_skb_parm
*opt
,
323 int type
, int code
, int offset
, __be32 info
)
325 struct inet_sock
*inet
= inet_sk(sk
);
326 struct ipv6_pinfo
*np
= inet6_sk(sk
);
330 /* Report error on raw socket, if:
331 1. User requested recverr.
332 2. Socket is connected (otherwise the error indication
333 is useless without recverr and error is hard.
335 if (!np
->recverr
&& sk
->sk_state
!= TCP_ESTABLISHED
)
338 harderr
= icmpv6_err_convert(type
, code
, &err
);
339 if (type
== ICMPV6_PKT_TOOBIG
)
340 harderr
= (np
->pmtudisc
== IPV6_PMTUDISC_DO
);
343 u8
*payload
= skb
->data
;
346 ipv6_icmp_error(sk
, skb
, err
, 0, ntohl(info
), payload
);
349 if (np
->recverr
|| harderr
) {
351 sk
->sk_error_report(sk
);
355 void raw6_icmp_error(struct sk_buff
*skb
, int nexthdr
,
356 int type
, int code
, int inner_offset
, __be32 info
)
360 struct in6_addr
*saddr
, *daddr
;
362 hash
= nexthdr
& (RAW_HTABLE_SIZE
- 1);
364 read_lock(&raw_v6_hashinfo
.lock
);
365 sk
= sk_head(&raw_v6_hashinfo
.ht
[hash
]);
367 saddr
= &ipv6_hdr(skb
)->saddr
;
368 daddr
= &ipv6_hdr(skb
)->daddr
;
370 while ((sk
= __raw_v6_lookup(sk
, nexthdr
, saddr
, daddr
,
372 rawv6_err(sk
, skb
, NULL
, type
, code
,
377 read_unlock(&raw_v6_hashinfo
.lock
);
380 static inline int rawv6_rcv_skb(struct sock
* sk
, struct sk_buff
* skb
)
382 if ((raw6_sk(sk
)->checksum
|| sk
->sk_filter
) &&
383 skb_checksum_complete(skb
)) {
384 atomic_inc(&sk
->sk_drops
);
389 /* Charge it to the socket. */
390 if (sock_queue_rcv_skb(sk
,skb
)<0) {
391 atomic_inc(&sk
->sk_drops
);
400 * This is next to useless...
401 * if we demultiplex in network layer we don't need the extra call
402 * just to queue the skb...
403 * maybe we could have the network decide upon a hint if it
404 * should call raw_rcv for demultiplexing
406 int rawv6_rcv(struct sock
*sk
, struct sk_buff
*skb
)
408 struct inet_sock
*inet
= inet_sk(sk
);
409 struct raw6_sock
*rp
= raw6_sk(sk
);
411 if (!xfrm6_policy_check(sk
, XFRM_POLICY_IN
, skb
)) {
412 atomic_inc(&sk
->sk_drops
);
418 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
420 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
421 skb_postpull_rcsum(skb
, skb_network_header(skb
),
422 skb_network_header_len(skb
));
423 if (!csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
424 &ipv6_hdr(skb
)->daddr
,
425 skb
->len
, inet
->num
, skb
->csum
))
426 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
428 if (!skb_csum_unnecessary(skb
))
429 skb
->csum
= ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
430 &ipv6_hdr(skb
)->daddr
,
435 if (skb_checksum_complete(skb
)) {
436 atomic_inc(&sk
->sk_drops
);
442 rawv6_rcv_skb(sk
, skb
);
448 * This should be easy, if there is something there
449 * we return it, otherwise we block.
452 static int rawv6_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
453 struct msghdr
*msg
, size_t len
,
454 int noblock
, int flags
, int *addr_len
)
456 struct ipv6_pinfo
*np
= inet6_sk(sk
);
457 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)msg
->msg_name
;
466 *addr_len
=sizeof(*sin6
);
468 if (flags
& MSG_ERRQUEUE
)
469 return ipv6_recv_error(sk
, msg
, len
);
471 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
478 msg
->msg_flags
|= MSG_TRUNC
;
481 if (skb_csum_unnecessary(skb
)) {
482 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
483 } else if (msg
->msg_flags
&MSG_TRUNC
) {
484 if (__skb_checksum_complete(skb
))
486 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
488 err
= skb_copy_and_csum_datagram_iovec(skb
, 0, msg
->msg_iov
);
495 /* Copy the address. */
497 sin6
->sin6_family
= AF_INET6
;
499 ipv6_addr_copy(&sin6
->sin6_addr
, &ipv6_hdr(skb
)->saddr
);
500 sin6
->sin6_flowinfo
= 0;
501 sin6
->sin6_scope_id
= 0;
502 if (ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
503 sin6
->sin6_scope_id
= IP6CB(skb
)->iif
;
506 sock_recv_timestamp(msg
, sk
, skb
);
509 datagram_recv_ctl(sk
, msg
, skb
);
512 if (flags
& MSG_TRUNC
)
516 skb_free_datagram(sk
, skb
);
521 skb_kill_datagram(sk
, skb
, flags
);
523 /* Error for blocking case is chosen to masquerade
524 as some normal condition.
526 err
= (flags
&MSG_DONTWAIT
) ? -EAGAIN
: -EHOSTUNREACH
;
527 atomic_inc(&sk
->sk_drops
);
531 static int rawv6_push_pending_frames(struct sock
*sk
, struct flowi
*fl
,
532 struct raw6_sock
*rp
)
545 if ((skb
= skb_peek(&sk
->sk_write_queue
)) == NULL
)
549 total_len
= inet_sk(sk
)->cork
.length
- (skb_network_header(skb
) -
551 if (offset
>= total_len
- 1) {
553 ip6_flush_pending_frames(sk
);
557 /* should be check HW csum miyazawa */
558 if (skb_queue_len(&sk
->sk_write_queue
) == 1) {
560 * Only one fragment on the socket.
562 tmp_csum
= skb
->csum
;
564 struct sk_buff
*csum_skb
= NULL
;
567 skb_queue_walk(&sk
->sk_write_queue
, skb
) {
568 tmp_csum
= csum_add(tmp_csum
, skb
->csum
);
573 len
= skb
->len
- skb_transport_offset(skb
);
585 offset
+= skb_transport_offset(skb
);
586 if (skb_copy_bits(skb
, offset
, &csum
, 2))
589 /* in case cksum was not initialized */
591 tmp_csum
= csum_sub(tmp_csum
, csum_unfold(csum
));
593 csum
= csum_ipv6_magic(&fl
->fl6_src
,
595 total_len
, fl
->proto
, tmp_csum
);
597 if (csum
== 0 && fl
->proto
== IPPROTO_UDP
)
598 csum
= CSUM_MANGLED_0
;
600 if (skb_store_bits(skb
, offset
, &csum
, 2))
604 err
= ip6_push_pending_frames(sk
);
609 static int rawv6_send_hdrinc(struct sock
*sk
, void *from
, int length
,
610 struct flowi
*fl
, struct rt6_info
*rt
,
613 struct ipv6_pinfo
*np
= inet6_sk(sk
);
619 if (length
> rt
->u
.dst
.dev
->mtu
) {
620 ipv6_local_error(sk
, EMSGSIZE
, fl
, rt
->u
.dst
.dev
->mtu
);
626 hh_len
= LL_RESERVED_SPACE(rt
->u
.dst
.dev
);
628 skb
= sock_alloc_send_skb(sk
, length
+hh_len
+15,
629 flags
&MSG_DONTWAIT
, &err
);
632 skb_reserve(skb
, hh_len
);
634 skb
->priority
= sk
->sk_priority
;
635 skb
->dst
= dst_clone(&rt
->u
.dst
);
637 skb_put(skb
, length
);
638 skb_reset_network_header(skb
);
641 skb
->ip_summed
= CHECKSUM_NONE
;
643 skb
->transport_header
= skb
->network_header
;
644 err
= memcpy_fromiovecend((void *)iph
, from
, 0, length
);
648 IP6_INC_STATS(rt
->rt6i_idev
, IPSTATS_MIB_OUTREQUESTS
);
649 err
= NF_HOOK(PF_INET6
, NF_INET_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
,
652 err
= np
->recverr
? net_xmit_errno(err
) : 0;
662 IP6_INC_STATS(rt
->rt6i_idev
, IPSTATS_MIB_OUTDISCARDS
);
666 static int rawv6_probe_proto_opt(struct flowi
*fl
, struct msghdr
*msg
)
669 u8 __user
*type
= NULL
;
670 u8 __user
*code
= NULL
;
678 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
679 iov
= &msg
->msg_iov
[i
];
685 /* check if one-byte field is readable or not. */
686 if (iov
->iov_base
&& iov
->iov_len
< 1)
690 type
= iov
->iov_base
;
691 /* check if code field is readable or not. */
692 if (iov
->iov_len
> 1)
695 code
= iov
->iov_base
;
698 if (get_user(fl
->fl_icmp_type
, type
) ||
699 get_user(fl
->fl_icmp_code
, code
))
705 if (iov
->iov_base
&& iov
->iov_len
< 1)
707 /* check if type field is readable or not. */
708 if (iov
->iov_len
> 2 - len
) {
709 u8 __user
*p
= iov
->iov_base
;
710 if (get_user(fl
->fl_mh_type
, &p
[2 - len
]))
727 static int rawv6_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
728 struct msghdr
*msg
, size_t len
)
730 struct ipv6_txoptions opt_space
;
731 struct sockaddr_in6
* sin6
= (struct sockaddr_in6
*) msg
->msg_name
;
732 struct in6_addr
*daddr
, *final_p
= NULL
, final
;
733 struct inet_sock
*inet
= inet_sk(sk
);
734 struct ipv6_pinfo
*np
= inet6_sk(sk
);
735 struct raw6_sock
*rp
= raw6_sk(sk
);
736 struct ipv6_txoptions
*opt
= NULL
;
737 struct ip6_flowlabel
*flowlabel
= NULL
;
738 struct dst_entry
*dst
= NULL
;
740 int addr_len
= msg
->msg_namelen
;
746 /* Rough check on arithmetic overflow,
747 better check is made in ip6_append_data().
752 /* Mirror BSD error message compatibility */
753 if (msg
->msg_flags
& MSG_OOB
)
757 * Get and verify the address.
759 memset(&fl
, 0, sizeof(fl
));
762 if (addr_len
< SIN6_LEN_RFC2133
)
765 if (sin6
->sin6_family
&& sin6
->sin6_family
!= AF_INET6
)
766 return(-EAFNOSUPPORT
);
768 /* port is the proto value [0..255] carried in nexthdr */
769 proto
= ntohs(sin6
->sin6_port
);
773 else if (proto
!= inet
->num
)
779 daddr
= &sin6
->sin6_addr
;
781 fl
.fl6_flowlabel
= sin6
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
782 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
783 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
784 if (flowlabel
== NULL
)
786 daddr
= &flowlabel
->dst
;
791 * Otherwise it will be difficult to maintain
794 if (sk
->sk_state
== TCP_ESTABLISHED
&&
795 ipv6_addr_equal(daddr
, &np
->daddr
))
798 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
799 sin6
->sin6_scope_id
&&
800 ipv6_addr_type(daddr
)&IPV6_ADDR_LINKLOCAL
)
801 fl
.oif
= sin6
->sin6_scope_id
;
803 if (sk
->sk_state
!= TCP_ESTABLISHED
)
804 return -EDESTADDRREQ
;
808 fl
.fl6_flowlabel
= np
->flow_label
;
811 if (ipv6_addr_any(daddr
)) {
813 * unspecified destination address
814 * treated as error... is this correct ?
816 fl6_sock_release(flowlabel
);
821 fl
.oif
= sk
->sk_bound_dev_if
;
823 if (msg
->msg_controllen
) {
825 memset(opt
, 0, sizeof(struct ipv6_txoptions
));
826 opt
->tot_len
= sizeof(struct ipv6_txoptions
);
828 err
= datagram_send_ctl(msg
, &fl
, opt
, &hlimit
, &tclass
);
830 fl6_sock_release(flowlabel
);
833 if ((fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) && !flowlabel
) {
834 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
835 if (flowlabel
== NULL
)
838 if (!(opt
->opt_nflen
|opt
->opt_flen
))
844 opt
= fl6_merge_options(&opt_space
, flowlabel
, opt
);
845 opt
= ipv6_fixup_options(&opt_space
, opt
);
848 err
= rawv6_probe_proto_opt(&fl
, msg
);
852 ipv6_addr_copy(&fl
.fl6_dst
, daddr
);
853 if (ipv6_addr_any(&fl
.fl6_src
) && !ipv6_addr_any(&np
->saddr
))
854 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
856 /* merge ip6_build_xmit from ip6_output */
857 if (opt
&& opt
->srcrt
) {
858 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) opt
->srcrt
;
859 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
860 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
864 if (!fl
.oif
&& ipv6_addr_is_multicast(&fl
.fl6_dst
))
865 fl
.oif
= np
->mcast_oif
;
866 security_sk_classify_flow(sk
, &fl
);
868 err
= ip6_dst_lookup(sk
, &dst
, &fl
);
872 ipv6_addr_copy(&fl
.fl6_dst
, final_p
);
874 if ((err
= __xfrm_lookup(&dst
, &fl
, sk
, XFRM_LOOKUP_WAIT
)) < 0) {
876 err
= ip6_dst_blackhole(sk
, &dst
, &fl
);
882 if (ipv6_addr_is_multicast(&fl
.fl6_dst
))
883 hlimit
= np
->mcast_hops
;
885 hlimit
= np
->hop_limit
;
887 hlimit
= dst_metric(dst
, RTAX_HOPLIMIT
);
889 hlimit
= ipv6_get_hoplimit(dst
->dev
);
898 if (msg
->msg_flags
&MSG_CONFIRM
)
903 err
= rawv6_send_hdrinc(sk
, msg
->msg_iov
, len
, &fl
, (struct rt6_info
*)dst
, msg
->msg_flags
);
906 err
= ip6_append_data(sk
, ip_generic_getfrag
, msg
->msg_iov
,
907 len
, 0, hlimit
, tclass
, opt
, &fl
, (struct rt6_info
*)dst
,
911 ip6_flush_pending_frames(sk
);
912 else if (!(msg
->msg_flags
& MSG_MORE
))
913 err
= rawv6_push_pending_frames(sk
, &fl
, rp
);
919 fl6_sock_release(flowlabel
);
920 return err
<0?err
:len
;
923 if (!(msg
->msg_flags
& MSG_PROBE
) || len
)
924 goto back_from_confirm
;
929 static int rawv6_seticmpfilter(struct sock
*sk
, int level
, int optname
,
930 char __user
*optval
, int optlen
)
934 if (optlen
> sizeof(struct icmp6_filter
))
935 optlen
= sizeof(struct icmp6_filter
);
936 if (copy_from_user(&raw6_sk(sk
)->filter
, optval
, optlen
))
946 static int rawv6_geticmpfilter(struct sock
*sk
, int level
, int optname
,
947 char __user
*optval
, int __user
*optlen
)
953 if (get_user(len
, optlen
))
957 if (len
> sizeof(struct icmp6_filter
))
958 len
= sizeof(struct icmp6_filter
);
959 if (put_user(len
, optlen
))
961 if (copy_to_user(optval
, &raw6_sk(sk
)->filter
, len
))
972 static int do_rawv6_setsockopt(struct sock
*sk
, int level
, int optname
,
973 char __user
*optval
, int optlen
)
975 struct raw6_sock
*rp
= raw6_sk(sk
);
978 if (get_user(val
, (int __user
*)optval
))
983 /* You may get strange result with a positive odd offset;
984 RFC2292bis agrees with me. */
985 if (val
> 0 && (val
&1))
998 return(-ENOPROTOOPT
);
1002 static int rawv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1003 char __user
*optval
, int optlen
)
1010 if (inet_sk(sk
)->num
!= IPPROTO_ICMPV6
)
1012 return rawv6_seticmpfilter(sk
, level
, optname
, optval
,
1015 if (optname
== IPV6_CHECKSUM
)
1018 return ipv6_setsockopt(sk
, level
, optname
, optval
,
1022 return do_rawv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1025 #ifdef CONFIG_COMPAT
1026 static int compat_rawv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1027 char __user
*optval
, int optlen
)
1033 if (inet_sk(sk
)->num
!= IPPROTO_ICMPV6
)
1035 return rawv6_seticmpfilter(sk
, level
, optname
, optval
, optlen
);
1037 if (optname
== IPV6_CHECKSUM
)
1040 return compat_ipv6_setsockopt(sk
, level
, optname
,
1043 return do_rawv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1047 static int do_rawv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1048 char __user
*optval
, int __user
*optlen
)
1050 struct raw6_sock
*rp
= raw6_sk(sk
);
1053 if (get_user(len
,optlen
))
1058 if (rp
->checksum
== 0)
1065 return -ENOPROTOOPT
;
1068 len
= min_t(unsigned int, sizeof(int), len
);
1070 if (put_user(len
, optlen
))
1072 if (copy_to_user(optval
,&val
,len
))
1077 static int rawv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1078 char __user
*optval
, int __user
*optlen
)
1085 if (inet_sk(sk
)->num
!= IPPROTO_ICMPV6
)
1087 return rawv6_geticmpfilter(sk
, level
, optname
, optval
,
1090 if (optname
== IPV6_CHECKSUM
)
1093 return ipv6_getsockopt(sk
, level
, optname
, optval
,
1097 return do_rawv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1100 #ifdef CONFIG_COMPAT
1101 static int compat_rawv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1102 char __user
*optval
, int __user
*optlen
)
1108 if (inet_sk(sk
)->num
!= IPPROTO_ICMPV6
)
1110 return rawv6_geticmpfilter(sk
, level
, optname
, optval
, optlen
);
1112 if (optname
== IPV6_CHECKSUM
)
1115 return compat_ipv6_getsockopt(sk
, level
, optname
,
1118 return do_rawv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1122 static int rawv6_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
1127 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
1128 return put_user(amount
, (int __user
*)arg
);
1132 struct sk_buff
*skb
;
1135 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1136 skb
= skb_peek(&sk
->sk_receive_queue
);
1138 amount
= skb
->tail
- skb
->transport_header
;
1139 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1140 return put_user(amount
, (int __user
*)arg
);
1144 return -ENOIOCTLCMD
;
1148 static void rawv6_close(struct sock
*sk
, long timeout
)
1150 if (inet_sk(sk
)->num
== IPPROTO_RAW
)
1151 ip6_ra_control(sk
, -1, NULL
);
1153 sk_common_release(sk
);
1156 static int rawv6_init_sk(struct sock
*sk
)
1158 struct raw6_sock
*rp
= raw6_sk(sk
);
1160 switch (inet_sk(sk
)->num
) {
1161 case IPPROTO_ICMPV6
:
1175 DEFINE_PROTO_INUSE(rawv6
)
1177 struct proto rawv6_prot
= {
1179 .owner
= THIS_MODULE
,
1180 .close
= rawv6_close
,
1181 .connect
= ip6_datagram_connect
,
1182 .disconnect
= udp_disconnect
,
1183 .ioctl
= rawv6_ioctl
,
1184 .init
= rawv6_init_sk
,
1185 .destroy
= inet6_destroy_sock
,
1186 .setsockopt
= rawv6_setsockopt
,
1187 .getsockopt
= rawv6_getsockopt
,
1188 .sendmsg
= rawv6_sendmsg
,
1189 .recvmsg
= rawv6_recvmsg
,
1191 .backlog_rcv
= rawv6_rcv_skb
,
1192 .hash
= raw_v6_hash
,
1193 .unhash
= raw_v6_unhash
,
1194 .obj_size
= sizeof(struct raw6_sock
),
1195 #ifdef CONFIG_COMPAT
1196 .compat_setsockopt
= compat_rawv6_setsockopt
,
1197 .compat_getsockopt
= compat_rawv6_getsockopt
,
1199 REF_PROTO_INUSE(rawv6
)
1202 #ifdef CONFIG_PROC_FS
1203 static void raw6_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
, int i
)
1205 struct ipv6_pinfo
*np
= inet6_sk(sp
);
1206 struct in6_addr
*dest
, *src
;
1210 src
= &np
->rcv_saddr
;
1212 srcp
= inet_sk(sp
)->num
;
1214 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1215 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1217 src
->s6_addr32
[0], src
->s6_addr32
[1],
1218 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1219 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1220 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1222 atomic_read(&sp
->sk_wmem_alloc
),
1223 atomic_read(&sp
->sk_rmem_alloc
),
1227 atomic_read(&sp
->sk_refcnt
), sp
, atomic_read(&sp
->sk_drops
));
1230 static int raw6_seq_show(struct seq_file
*seq
, void *v
)
1232 if (v
== SEQ_START_TOKEN
)
1237 "st tx_queue rx_queue tr tm->when retrnsmt"
1238 " uid timeout inode drops\n");
1240 raw6_sock_seq_show(seq
, v
, raw_seq_private(seq
)->bucket
);
1244 static const struct seq_operations raw6_seq_ops
= {
1245 .start
= raw_seq_start
,
1246 .next
= raw_seq_next
,
1247 .stop
= raw_seq_stop
,
1248 .show
= raw6_seq_show
,
1251 static int raw6_seq_open(struct inode
*inode
, struct file
*file
)
1253 return raw_seq_open(file
, &raw_v6_hashinfo
, PF_INET6
);
1256 static const struct file_operations raw6_seq_fops
= {
1257 .owner
= THIS_MODULE
,
1258 .open
= raw6_seq_open
,
1260 .llseek
= seq_lseek
,
1261 .release
= seq_release_private
,
1264 int __init
raw6_proc_init(void)
1266 if (!proc_net_fops_create(&init_net
, "raw6", S_IRUGO
, &raw6_seq_fops
))
1271 void raw6_proc_exit(void)
1273 proc_net_remove(&init_net
, "raw6");
1275 #endif /* CONFIG_PROC_FS */
1277 /* Same as inet6_dgram_ops, sans udp_poll. */
1278 static const struct proto_ops inet6_sockraw_ops
= {
1280 .owner
= THIS_MODULE
,
1281 .release
= inet6_release
,
1283 .connect
= inet_dgram_connect
, /* ok */
1284 .socketpair
= sock_no_socketpair
, /* a do nothing */
1285 .accept
= sock_no_accept
, /* a do nothing */
1286 .getname
= inet6_getname
,
1287 .poll
= datagram_poll
, /* ok */
1288 .ioctl
= inet6_ioctl
, /* must change */
1289 .listen
= sock_no_listen
, /* ok */
1290 .shutdown
= inet_shutdown
, /* ok */
1291 .setsockopt
= sock_common_setsockopt
, /* ok */
1292 .getsockopt
= sock_common_getsockopt
, /* ok */
1293 .sendmsg
= inet_sendmsg
, /* ok */
1294 .recvmsg
= sock_common_recvmsg
, /* ok */
1295 .mmap
= sock_no_mmap
,
1296 .sendpage
= sock_no_sendpage
,
1297 #ifdef CONFIG_COMPAT
1298 .compat_setsockopt
= compat_sock_common_setsockopt
,
1299 .compat_getsockopt
= compat_sock_common_getsockopt
,
1303 static struct inet_protosw rawv6_protosw
= {
1305 .protocol
= IPPROTO_IP
, /* wild card */
1306 .prot
= &rawv6_prot
,
1307 .ops
= &inet6_sockraw_ops
,
1308 .capability
= CAP_NET_RAW
,
1309 .no_check
= UDP_CSUM_DEFAULT
,
1310 .flags
= INET_PROTOSW_REUSE
,
1313 int __init
rawv6_init(void)
1317 ret
= inet6_register_protosw(&rawv6_protosw
);
1324 void rawv6_exit(void)
1326 inet6_unregister_protosw(&rawv6_protosw
);