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 if (sk
->sk_bound_dev_if
) {
295 dev
= dev_get_by_index_rcu(sock_net(sk
),
296 sk
->sk_bound_dev_if
);
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
,
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
;
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
);
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
)
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
);
354 u8
*payload
= skb
->data
;
357 ipv6_icmp_error(sk
, skb
, err
, 0, ntohl(info
), payload
);
360 if (np
->recverr
|| harderr
) {
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
)
371 const struct in6_addr
*saddr
, *daddr
;
374 hash
= nexthdr
& (RAW_HTABLE_SIZE
- 1);
376 read_lock(&raw_v6_hashinfo
.lock
);
377 sk
= sk_head(&raw_v6_hashinfo
.ht
[hash
]);
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
,
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
);
404 /* Charge it to the socket. */
406 if (sock_queue_rcv_skb(sk
, skb
) < 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
);
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
,
450 if (skb_checksum_complete(skb
)) {
451 atomic_inc(&sk
->sk_drops
);
457 rawv6_rcv_skb(sk
, skb
);
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
);
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
);
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
))
500 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
502 err
= skb_copy_and_csum_datagram_msg(skb
, 0, msg
);
509 /* Copy the address. */
511 sin6
->sin6_family
= AF_INET6
;
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
,
517 *addr_len
= sizeof(*sin6
);
520 sock_recv_ts_and_drops(msg
, sk
, skb
);
523 ip6_datagram_recv_ctl(sk
, msg
, skb
);
526 if (flags
& MSG_TRUNC
)
530 skb_free_datagram(sk
, skb
);
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
;
544 static int rawv6_push_pending_frames(struct sock
*sk
, struct flowi6
*fl6
,
545 struct raw6_sock
*rp
)
558 skb
= skb_peek(&sk
->sk_write_queue
);
563 total_len
= inet_sk(sk
)->cork
.base
.length
;
564 if (offset
>= total_len
- 1) {
566 ip6_flush_pending_frames(sk
);
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
;
577 struct sk_buff
*csum_skb
= NULL
;
580 skb_queue_walk(&sk
->sk_write_queue
, skb
) {
581 tmp_csum
= csum_add(tmp_csum
, skb
->csum
);
586 len
= skb
->len
- skb_transport_offset(skb
);
598 offset
+= skb_transport_offset(skb
);
599 err
= skb_copy_bits(skb
, offset
, &csum
, 2);
601 ip6_flush_pending_frames(sk
);
605 /* in case cksum was not initialized */
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));
618 err
= ip6_push_pending_frames(sk
);
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
);
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
);
640 if (length
< sizeof(struct ipv6hdr
))
645 skb
= sock_alloc_send_skb(sk
,
646 length
+ hlen
+ tlen
+ 15,
647 flags
& MSG_DONTWAIT
, &err
);
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
);
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
);
676 skb_dst_set(skb
, &rt
->dst
);
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
);
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.
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
);
695 err
= net_xmit_errno(err
);
697 IP6_INC_STATS(net
, rt
->rt6i_idev
, IPSTATS_MIB_OUTDISCARDS
);
706 IP6_INC_STATS(net
, rt
->rt6i_idev
, IPSTATS_MIB_OUTDISCARDS
);
708 if (err
== -ENOBUFS
&& !np
->recverr
)
713 struct raw6_frag_vec
{
719 static int rawv6_probe_proto_opt(struct raw6_frag_vec
*rfv
, struct flowi6
*fl6
)
722 switch (fl6
->flowi6_proto
) {
725 err
= memcpy_from_msg(rfv
->c
, rfv
->msg
, rfv
->hlen
);
727 fl6
->fl6_icmp_type
= rfv
->c
[0];
728 fl6
->fl6_icmp_code
= rfv
->c
[1];
733 err
= memcpy_from_msg(rfv
->c
, rfv
->msg
, rfv
->hlen
);
735 fl6
->fl6_mh_type
= rfv
->c
[2];
740 static int raw6_getfrag(void *from
, char *to
, int offset
, int len
, int odd
,
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
);
751 skb
->csum
= csum_block_add(
753 csum_partial_copy_nocheck(rfv
->c
+ offset
,
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
;
785 struct ipcm6_cookie ipc6
;
786 int addr_len
= msg
->msg_namelen
;
791 /* Rough check on arithmetic overflow,
792 better check is made in ip6_append_data().
797 /* Mirror BSD error message compatibility */
798 if (msg
->msg_flags
& MSG_OOB
)
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
;
817 ipc6
.sockc
.tsflags
= sk
->sk_tsflags
;
820 if (addr_len
< SIN6_LEN_RFC2133
)
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
);
830 proto
= inet
->inet_num
;
831 else if (proto
!= inet
->inet_num
)
837 daddr
= &sin6
->sin6_addr
;
839 fl6
.flowlabel
= sin6
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
840 if (fl6
.flowlabel
&IPV6_FLOWLABEL_MASK
) {
841 flowlabel
= fl6_sock_lookup(sk
, fl6
.flowlabel
);
848 * Otherwise it will be difficult to maintain
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
;
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
) {
873 memset(opt
, 0, sizeof(struct ipv6_txoptions
));
874 opt
->tot_len
= sizeof(struct ipv6_txoptions
);
877 err
= ip6_datagram_send_ctl(sock_net(sk
), sk
, msg
, &fl6
, &ipc6
);
879 fl6_sock_release(flowlabel
);
882 if ((fl6
.flowlabel
&IPV6_FLOWLABEL_MASK
) && !flowlabel
) {
883 flowlabel
= fl6_sock_lookup(sk
, fl6
.flowlabel
);
887 if (!(opt
->opt_nflen
|opt
->opt_flen
))
895 opt
= fl6_merge_options(&opt_space
, flowlabel
, opt
);
896 opt
= ipv6_fixup_options(&opt_space
, opt
);
898 fl6
.flowi6_proto
= proto
;
903 err
= rawv6_probe_proto_opt(&rfv
, &fl6
);
908 if (!ipv6_addr_any(daddr
))
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
));
924 fl6
.flowi6_flags
|= FLOWI_FLAG_KNOWN_NH
;
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
);
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
)
947 err
= rawv6_send_hdrinc(sk
, msg
, len
, &fl6
, &dst
,
948 msg
->msg_flags
, &ipc6
.sockc
);
952 err
= ip6_append_data(sk
, raw6_getfrag
, &rfv
,
953 len
, 0, &ipc6
, &fl6
, (struct rt6_info
*)dst
,
957 ip6_flush_pending_frames(sk
);
958 else if (!(msg
->msg_flags
& MSG_MORE
))
959 err
= rawv6_push_pending_frames(sk
, &fl6
, rp
);
965 fl6_sock_release(flowlabel
);
966 txopt_put(opt_to_free
);
967 return err
< 0 ? err
: len
;
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
;
977 static int rawv6_seticmpfilter(struct sock
*sk
, int level
, int optname
,
978 char __user
*optval
, int optlen
)
982 if (optlen
> sizeof(struct icmp6_filter
))
983 optlen
= sizeof(struct icmp6_filter
);
984 if (copy_from_user(&raw6_sk(sk
)->filter
, optval
, optlen
))
994 static int rawv6_geticmpfilter(struct sock
*sk
, int level
, int optname
,
995 char __user
*optval
, int __user
*optlen
)
1001 if (get_user(len
, optlen
))
1005 if (len
> sizeof(struct icmp6_filter
))
1006 len
= sizeof(struct icmp6_filter
);
1007 if (put_user(len
, optlen
))
1009 if (copy_to_user(optval
, &raw6_sk(sk
)->filter
, len
))
1013 return -ENOPROTOOPT
;
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
);
1026 if (get_user(val
, (int __user
*)optval
))
1031 if (sk
->sk_type
!= SOCK_RAW
)
1033 inet_sk(sk
)->hdrincl
= !!val
;
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).
1049 /* You may get strange result with a positive odd offset;
1050 RFC2292bis agrees with me. */
1051 if (val
> 0 && (val
&1))
1063 return -ENOPROTOOPT
;
1067 static int rawv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1068 char __user
*optval
, unsigned int optlen
)
1075 if (inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1077 return rawv6_seticmpfilter(sk
, level
, optname
, optval
, optlen
);
1079 if (optname
== IPV6_CHECKSUM
||
1080 optname
== IPV6_HDRINCL
)
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
)
1098 if (inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1100 return rawv6_seticmpfilter(sk
, level
, optname
, optval
, optlen
);
1102 if (optname
== IPV6_CHECKSUM
||
1103 optname
== IPV6_HDRINCL
)
1107 return compat_ipv6_setsockopt(sk
, level
, optname
,
1110 return do_rawv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
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
);
1120 if (get_user(len
, optlen
))
1125 val
= inet_sk(sk
)->hdrincl
;
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)
1140 return -ENOPROTOOPT
;
1143 len
= min_t(unsigned int, sizeof(int), len
);
1145 if (put_user(len
, optlen
))
1147 if (copy_to_user(optval
, &val
, len
))
1152 static int rawv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1153 char __user
*optval
, int __user
*optlen
)
1160 if (inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1162 return rawv6_geticmpfilter(sk
, level
, optname
, optval
, optlen
);
1164 if (optname
== IPV6_CHECKSUM
||
1165 optname
== IPV6_HDRINCL
)
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
)
1183 if (inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1185 return rawv6_geticmpfilter(sk
, level
, optname
, optval
, optlen
);
1187 if (optname
== IPV6_CHECKSUM
||
1188 optname
== IPV6_HDRINCL
)
1192 return compat_ipv6_getsockopt(sk
, level
, optname
,
1195 return do_rawv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1199 static int rawv6_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
1203 int amount
= sk_wmem_alloc_get(sk
);
1205 return put_user(amount
, (int __user
*)arg
);
1208 struct sk_buff
*skb
;
1211 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1212 skb
= skb_peek(&sk
->sk_receive_queue
);
1215 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1216 return put_user(amount
, (int __user
*)arg
);
1220 #ifdef CONFIG_IPV6_MROUTE
1221 return ip6mr_ioctl(sk
, cmd
, (void __user
*)arg
);
1223 return -ENOIOCTLCMD
;
1228 #ifdef CONFIG_COMPAT
1229 static int compat_rawv6_ioctl(struct sock
*sk
, unsigned int cmd
, unsigned long arg
)
1234 return -ENOIOCTLCMD
;
1236 #ifdef CONFIG_IPV6_MROUTE
1237 return ip6mr_compat_ioctl(sk
, cmd
, compat_ptr(arg
));
1239 return -ENOIOCTLCMD
;
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);
1250 sk_common_release(sk
);
1253 static void raw6_destroy(struct sock
*sk
)
1256 ip6_flush_pending_frames(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
:
1281 struct proto rawv6_prot
= {
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
,
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
,
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
);
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
);
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
))
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
= {
1364 .owner
= THIS_MODULE
,
1365 .release
= inet6_release
,
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
,
1387 static struct inet_protosw rawv6_protosw
= {
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
);