21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/slab.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>
35 #include <asm/uaccess.h>
36 #include <asm/ioctls.h>
53 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
56 #include <linux/mroute6.h>
64 #include <linux/export.h>
71 unsigned short num,
const struct in6_addr *loc_addr,
72 const struct in6_addr *rmt_addr,
int dif)
75 bool is_multicast = ipv6_addr_is_multicast(loc_addr);
78 if (inet_sk(sk)->inet_num == num) {
81 if (!net_eq(sock_net(sk), net))
84 if (!ipv6_addr_any(&np->
daddr) &&
85 !ipv6_addr_equal(&np->
daddr, rmt_addr))
88 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
92 if (ipv6_addr_equal(&np->
rcv_saddr, loc_addr))
110 static int icmpv6_filter(
const struct sock *sk,
const struct sk_buff *
skb)
115 hdr = skb_header_pointer(skb, skb_transport_offset(skb),
116 sizeof(_hdr), &_hdr);
118 const __u32 *
data = &raw6_sk(sk)->filter.data[0];
121 return (data[type >> 5] & (1
U << (type & 31))) != 0;
126 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
131 int rawv6_mh_filter_register(mh_filter_t
filter)
138 int rawv6_mh_filter_unregister(mh_filter_t
filter)
160 bool delivered =
false;
164 saddr = &ipv6_hdr(skb)->saddr;
170 sk = sk_head(&raw_v6_hashinfo.
ht[hash]);
175 net = dev_net(skb->
dev);
176 sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr,
IP6CB(skb)->iif);
184 filtered = icmpv6_filter(sk, skb);
187 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
199 filtered = filter ? (*filter)(
sk,
skb) : 0;
219 sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
232 if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
235 return raw_sk !=
NULL;
250 addr_type = ipv6_addr_type(&addr->
sin6_addr);
277 if (!sk->sk_bound_dev_if)
282 sk->sk_bound_dev_if);
300 inet->inet_rcv_saddr = inet->
inet_saddr = v4addr;
302 if (!(addr_type & IPV6_ADDR_MULTICAST))
312 static void rawv6_err(
struct sock *sk,
struct sk_buff *skb,
350 u8 type,
u8 code,
int inner_offset,
__be32 info)
360 sk = sk_head(&raw_v6_hashinfo.
ht[hash]);
364 saddr = &ip6h->
saddr;
365 daddr = &ip6h->
daddr;
366 net = dev_net(skb->
dev);
368 while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
370 rawv6_err(sk, skb,
NULL, type, code,
378 static inline int rawv6_rcv_skb(
struct sock *sk,
struct sk_buff *skb)
381 skb_checksum_complete(skb)) {
419 skb_postpull_rcsum(skb, skb_network_header(skb),
420 skb_network_header_len(skb));
422 &ipv6_hdr(skb)->daddr,
426 if (!skb_csum_unnecessary(skb))
428 &ipv6_hdr(skb)->daddr,
433 if (skb_checksum_complete(skb)) {
440 rawv6_rcv_skb(sk, skb);
450 static int rawv6_recvmsg(
struct kiocb *
iocb,
struct sock *sk,
452 int noblock,
int flags,
int *addr_len)
464 *addr_len=
sizeof(*sin6);
482 if (skb_csum_unnecessary(skb)) {
503 if (ipv6_addr_type(&sin6->
sin6_addr) & IPV6_ADDR_LINKLOCAL)
507 sock_recv_ts_and_drops(msg, sk, skb);
531 static int rawv6_push_pending_frames(
struct sock *sk,
struct flowi6 *fl6,
549 total_len = inet_sk(sk)->cork.base.length;
550 if (offset >= total_len - 1) {
561 tmp_csum = skb->
csum;
567 tmp_csum = csum_add(tmp_csum, skb->
csum);
572 len = skb->
len - skb_transport_offset(skb);
584 offset += skb_transport_offset(skb);
590 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
593 total_len, fl6->flowi6_proto, tmp_csum);
607 static int rawv6_send_hdrinc(
struct sock *sk,
void *
from,
int length,
617 int tlen = rt->
dst.dev->needed_tailroom;
619 if (length > rt->
dst.dev->mtu) {
627 length + hlen + tlen + 15,
631 skb_reserve(skb, hlen);
635 skb_dst_set(skb, &rt->
dst);
639 skb_reset_network_header(skb);
651 rt->
dst.dev, dst_output);
669 static int rawv6_probe_proto_opt(
struct flowi6 *fl6,
struct msghdr *msg)
686 switch (fl6->flowi6_proto) {
701 if (
get_user(fl6->fl6_icmp_type, type) ||
713 if (
get_user(fl6->fl6_mh_type, &p[2 - len]))
730 static int rawv6_sendmsg(
struct kiocb *iocb,
struct sock *sk,
731 struct msghdr *msg,
size_t len)
735 struct in6_addr *daddr, *final_p,
final;
763 memset(&fl6, 0,
sizeof(fl6));
790 if (flowlabel ==
NULL)
792 daddr = &flowlabel->
dst;
801 ipv6_addr_equal(daddr, &np->
daddr))
806 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
817 if (fl6.flowi6_oif == 0)
818 fl6.flowi6_oif = sk->sk_bound_dev_if;
826 &hlimit, &tclass, &dontfrag);
828 fl6_sock_release(flowlabel);
833 if (flowlabel ==
NULL)
845 fl6.flowi6_proto =
proto;
846 err = rawv6_probe_proto_opt(&fl6, msg);
850 if (!ipv6_addr_any(daddr))
853 fl6.
daddr.s6_addr[15] = 0x1;
854 if (ipv6_addr_any(&fl6.
saddr) && !ipv6_addr_any(&np->
saddr))
859 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.
daddr))
861 else if (!fl6.flowi6_oif)
863 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
871 if (ipv6_addr_is_multicast(&fl6.
daddr))
894 len, 0, hlimit, tclass, opt, &fl6, (
struct rt6_info*)dst,
900 err = rawv6_push_pending_frames(sk, &fl6, rp);
906 fl6_sock_release(flowlabel);
907 return err<0?err:len;
910 if (!(msg->
msg_flags & MSG_PROBE) || len)
911 goto back_from_confirm;
916 static int rawv6_seticmpfilter(
struct sock *sk,
int level,
int optname,
917 char __user *optval,
int optlen)
933 static int rawv6_geticmpfilter(
struct sock *sk,
int level,
int optname,
934 char __user *optval,
int __user *optlen)
959 static int do_rawv6_setsockopt(
struct sock *sk,
int level,
int optname,
960 char __user *optval,
unsigned int optlen)
965 if (
get_user(val, (
int __user *)optval))
985 if (val > 0 && (val&1))
1001 static int rawv6_setsockopt(
struct sock *sk,
int level,
int optname,
1002 char __user *optval,
unsigned int optlen)
1011 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1019 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1022 #ifdef CONFIG_COMPAT
1023 static int compat_rawv6_setsockopt(
struct sock *sk,
int level,
int optname,
1024 char __user *optval,
unsigned int optlen)
1032 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1040 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1044 static int do_rawv6_getsockopt(
struct sock *sk,
int level,
int optname,
1045 char __user *optval,
int __user *optlen)
1070 len =
min_t(
unsigned int,
sizeof(
int), len);
1079 static int rawv6_getsockopt(
struct sock *sk,
int level,
int optname,
1080 char __user *optval,
int __user *optlen)
1089 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
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)
1110 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1118 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1122 static int rawv6_ioctl(
struct sock *sk,
int cmd,
unsigned long arg)
1126 int amount = sk_wmem_alloc_get(sk);
1128 return put_user(amount, (
int __user *)arg);
1139 return put_user(amount, (
int __user *)arg);
1143 #ifdef CONFIG_IPV6_MROUTE
1151 #ifdef CONFIG_COMPAT
1152 static int compat_rawv6_ioctl(
struct sock *sk,
unsigned int cmd,
unsigned long arg)
1159 #ifdef CONFIG_IPV6_MROUTE
1160 return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1168 static void rawv6_close(
struct sock *sk,
long timeout)
1176 static void raw6_destroy(
struct sock *sk)
1185 static int rawv6_init_sk(
struct sock *sk)
1189 switch (inet_sk(sk)->inet_num) {
1207 .close = rawv6_close,
1208 .destroy = raw6_destroy,
1211 .ioctl = rawv6_ioctl,
1212 .init = rawv6_init_sk,
1213 .setsockopt = rawv6_setsockopt,
1214 .getsockopt = rawv6_getsockopt,
1215 .sendmsg = rawv6_sendmsg,
1216 .recvmsg = rawv6_recvmsg,
1218 .backlog_rcv = rawv6_rcv_skb,
1222 .h.raw_hash = &raw_v6_hashinfo,
1223 #ifdef CONFIG_COMPAT
1224 .compat_setsockopt = compat_rawv6_setsockopt,
1225 .compat_getsockopt = compat_rawv6_getsockopt,
1226 .compat_ioctl = compat_rawv6_ioctl,
1230 #ifdef CONFIG_PROC_FS
1231 static void raw6_sock_seq_show(
struct seq_file *seq,
struct sock *
sp,
int i)
1240 srcp = inet_sk(sp)->inet_num;
1242 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1243 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
1245 src->s6_addr32[0], src->s6_addr32[1],
1246 src->s6_addr32[2], src->s6_addr32[3], srcp,
1247 dest->s6_addr32[0], dest->s6_addr32[1],
1248 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1250 sk_wmem_alloc_get(sp),
1251 sk_rmem_alloc_get(sp),
1259 static int raw6_seq_show(
struct seq_file *seq,
void *
v)
1266 "st tx_queue rx_queue tr tm->when retrnsmt"
1267 " uid timeout inode ref pointer drops\n");
1269 raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
1274 .
start = raw_seq_start,
1275 .next = raw_seq_next,
1276 .stop = raw_seq_stop,
1277 .show = raw6_seq_show,
1282 return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
1287 .open = raw6_seq_open,
1293 static int __net_init raw6_init_net(
struct net *net)
1301 static void __net_exit raw6_exit_net(
struct net *net)
1307 .
init = raw6_init_net,
1308 .exit = raw6_exit_net,
1311 int __init raw6_proc_init(
void)
1316 void raw6_proc_exit(
void)
1323 static const struct proto_ops inet6_sockraw_ops = {
1342 #ifdef CONFIG_COMPAT
1352 .ops = &inet6_sockraw_ops,