11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * RAW - implementation of IP "raw" sockets. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Version: $Id: raw.c,v 1.64 2002/02/01 22:01:04 davem Exp $ 91da177e4SLinus Torvalds * 1002c30a84SJesper Juhl * Authors: Ross Biro 111da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Fixes: 141da177e4SLinus Torvalds * Alan Cox : verify_area() fixed up 151da177e4SLinus Torvalds * Alan Cox : ICMP error handling 161da177e4SLinus Torvalds * Alan Cox : EMSGSIZE if you send too big a packet 171da177e4SLinus Torvalds * Alan Cox : Now uses generic datagrams and shared 181da177e4SLinus Torvalds * skbuff library. No more peek crashes, 191da177e4SLinus Torvalds * no more backlogs 201da177e4SLinus Torvalds * Alan Cox : Checks sk->broadcast. 211da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram/skb_copy_datagram 221da177e4SLinus Torvalds * Alan Cox : Raw passes ip options too 231da177e4SLinus Torvalds * Alan Cox : Setsocketopt added 241da177e4SLinus Torvalds * Alan Cox : Fixed error return for broadcasts 251da177e4SLinus Torvalds * Alan Cox : Removed wake_up calls 261da177e4SLinus Torvalds * Alan Cox : Use ttl/tos 271da177e4SLinus Torvalds * Alan Cox : Cleaned up old debugging 281da177e4SLinus Torvalds * Alan Cox : Use new kernel side addresses 291da177e4SLinus Torvalds * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets. 301da177e4SLinus Torvalds * Alan Cox : BSD style RAW socket demultiplexing. 311da177e4SLinus Torvalds * Alan Cox : Beginnings of mrouted support. 321da177e4SLinus Torvalds * Alan Cox : Added IP_HDRINCL option. 331da177e4SLinus Torvalds * Alan Cox : Skip broadcast check if BSDism set. 341da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 371da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 381da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 391da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 42715b49efSAlan Cox #include <linux/types.h> 431da177e4SLinus Torvalds #include <asm/atomic.h> 441da177e4SLinus Torvalds #include <asm/byteorder.h> 451da177e4SLinus Torvalds #include <asm/current.h> 461da177e4SLinus Torvalds #include <asm/uaccess.h> 471da177e4SLinus Torvalds #include <asm/ioctls.h> 481da177e4SLinus Torvalds #include <linux/stddef.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/errno.h> 511da177e4SLinus Torvalds #include <linux/aio.h> 521da177e4SLinus Torvalds #include <linux/kernel.h> 531da177e4SLinus Torvalds #include <linux/spinlock.h> 541da177e4SLinus Torvalds #include <linux/sockios.h> 551da177e4SLinus Torvalds #include <linux/socket.h> 561da177e4SLinus Torvalds #include <linux/in.h> 571da177e4SLinus Torvalds #include <linux/mroute.h> 581da177e4SLinus Torvalds #include <linux/netdevice.h> 591da177e4SLinus Torvalds #include <linux/in_route.h> 601da177e4SLinus Torvalds #include <linux/route.h> 611da177e4SLinus Torvalds #include <linux/skbuff.h> 621da177e4SLinus Torvalds #include <net/dst.h> 631da177e4SLinus Torvalds #include <net/sock.h> 641da177e4SLinus Torvalds #include <linux/gfp.h> 651da177e4SLinus Torvalds #include <linux/ip.h> 661da177e4SLinus Torvalds #include <linux/net.h> 671da177e4SLinus Torvalds #include <net/ip.h> 681da177e4SLinus Torvalds #include <net/icmp.h> 691da177e4SLinus Torvalds #include <net/udp.h> 701da177e4SLinus Torvalds #include <net/raw.h> 711da177e4SLinus Torvalds #include <net/snmp.h> 72c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 731da177e4SLinus Torvalds #include <net/inet_common.h> 741da177e4SLinus Torvalds #include <net/checksum.h> 751da177e4SLinus Torvalds #include <net/xfrm.h> 761da177e4SLinus Torvalds #include <linux/rtnetlink.h> 771da177e4SLinus Torvalds #include <linux/proc_fs.h> 781da177e4SLinus Torvalds #include <linux/seq_file.h> 791da177e4SLinus Torvalds #include <linux/netfilter.h> 801da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds struct hlist_head raw_v4_htable[RAWV4_HTABLE_SIZE]; 831da177e4SLinus Torvalds DEFINE_RWLOCK(raw_v4_lock); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static void raw_v4_hash(struct sock *sk) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds struct hlist_head *head = &raw_v4_htable[inet_sk(sk)->num & 881da177e4SLinus Torvalds (RAWV4_HTABLE_SIZE - 1)]; 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds write_lock_bh(&raw_v4_lock); 911da177e4SLinus Torvalds sk_add_node(sk, head); 921da177e4SLinus Torvalds sock_prot_inc_use(sk->sk_prot); 931da177e4SLinus Torvalds write_unlock_bh(&raw_v4_lock); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static void raw_v4_unhash(struct sock *sk) 971da177e4SLinus Torvalds { 981da177e4SLinus Torvalds write_lock_bh(&raw_v4_lock); 991da177e4SLinus Torvalds if (sk_del_node_init(sk)) 1001da177e4SLinus Torvalds sock_prot_dec_use(sk->sk_prot); 1011da177e4SLinus Torvalds write_unlock_bh(&raw_v4_lock); 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num, 1056d741653SAlexey Dobriyan __be32 raddr, __be32 laddr, 1061da177e4SLinus Torvalds int dif) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds struct hlist_node *node; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds sk_for_each_from(sk, node) { 1111da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds if (inet->num == num && 1141da177e4SLinus Torvalds !(inet->daddr && inet->daddr != raddr) && 1151da177e4SLinus Torvalds !(inet->rcv_saddr && inet->rcv_saddr != laddr) && 1161da177e4SLinus Torvalds !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)) 1171da177e4SLinus Torvalds goto found; /* gotcha */ 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds sk = NULL; 1201da177e4SLinus Torvalds found: 1211da177e4SLinus Torvalds return sk; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* 1251da177e4SLinus Torvalds * 0 - deliver 1261da177e4SLinus Torvalds * 1 - block 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds int type; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct icmphdr))) 1331da177e4SLinus Torvalds return 1; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds type = skb->h.icmph->type; 1361da177e4SLinus Torvalds if (type < 32) { 1371da177e4SLinus Torvalds __u32 data = raw_sk(sk)->filter.data; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds return ((1 << type) & data) != 0; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Do not block unknown ICMP types */ 1431da177e4SLinus Torvalds return 0; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* IP input processing comes here for RAW socket delivery. 1471da177e4SLinus Torvalds * Caller owns SKB, so we must make clones. 1481da177e4SLinus Torvalds * 1491da177e4SLinus Torvalds * RFC 1122: SHOULD pass TOS value up to the transport layer. 1501da177e4SLinus Torvalds * -> It does. And not only TOS, but all IP header. 1511da177e4SLinus Torvalds */ 152d13964f4SPatrick McHardy int raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct sock *sk; 1551da177e4SLinus Torvalds struct hlist_head *head; 156d13964f4SPatrick McHardy int delivered = 0; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds read_lock(&raw_v4_lock); 1591da177e4SLinus Torvalds head = &raw_v4_htable[hash]; 1601da177e4SLinus Torvalds if (hlist_empty(head)) 1611da177e4SLinus Torvalds goto out; 1621da177e4SLinus Torvalds sk = __raw_v4_lookup(__sk_head(head), iph->protocol, 1631da177e4SLinus Torvalds iph->saddr, iph->daddr, 1641da177e4SLinus Torvalds skb->dev->ifindex); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds while (sk) { 167d13964f4SPatrick McHardy delivered = 1; 1681da177e4SLinus Torvalds if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) { 1691da177e4SLinus Torvalds struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds /* Not releasing hash table! */ 1721da177e4SLinus Torvalds if (clone) 1731da177e4SLinus Torvalds raw_rcv(sk, clone); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds sk = __raw_v4_lookup(sk_next(sk), iph->protocol, 1761da177e4SLinus Torvalds iph->saddr, iph->daddr, 1771da177e4SLinus Torvalds skb->dev->ifindex); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds out: 1801da177e4SLinus Torvalds read_unlock(&raw_v4_lock); 181d13964f4SPatrick McHardy return delivered; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds void raw_err (struct sock *sk, struct sk_buff *skb, u32 info) 1851da177e4SLinus Torvalds { 1861da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1871da177e4SLinus Torvalds int type = skb->h.icmph->type; 1881da177e4SLinus Torvalds int code = skb->h.icmph->code; 1891da177e4SLinus Torvalds int err = 0; 1901da177e4SLinus Torvalds int harderr = 0; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* Report error on raw socket, if: 1931da177e4SLinus Torvalds 1. User requested ip_recverr. 1941da177e4SLinus Torvalds 2. Socket is connected (otherwise the error indication 1951da177e4SLinus Torvalds is useless without ip_recverr and error is hard. 1961da177e4SLinus Torvalds */ 1971da177e4SLinus Torvalds if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED) 1981da177e4SLinus Torvalds return; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds switch (type) { 2011da177e4SLinus Torvalds default: 2021da177e4SLinus Torvalds case ICMP_TIME_EXCEEDED: 2031da177e4SLinus Torvalds err = EHOSTUNREACH; 2041da177e4SLinus Torvalds break; 2051da177e4SLinus Torvalds case ICMP_SOURCE_QUENCH: 2061da177e4SLinus Torvalds return; 2071da177e4SLinus Torvalds case ICMP_PARAMETERPROB: 2081da177e4SLinus Torvalds err = EPROTO; 2091da177e4SLinus Torvalds harderr = 1; 2101da177e4SLinus Torvalds break; 2111da177e4SLinus Torvalds case ICMP_DEST_UNREACH: 2121da177e4SLinus Torvalds err = EHOSTUNREACH; 2131da177e4SLinus Torvalds if (code > NR_ICMP_UNREACH) 2141da177e4SLinus Torvalds break; 2151da177e4SLinus Torvalds err = icmp_err_convert[code].errno; 2161da177e4SLinus Torvalds harderr = icmp_err_convert[code].fatal; 2171da177e4SLinus Torvalds if (code == ICMP_FRAG_NEEDED) { 2181da177e4SLinus Torvalds harderr = inet->pmtudisc != IP_PMTUDISC_DONT; 2191da177e4SLinus Torvalds err = EMSGSIZE; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (inet->recverr) { 2241da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr*)skb->data; 2251da177e4SLinus Torvalds u8 *payload = skb->data + (iph->ihl << 2); 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds if (inet->hdrincl) 2281da177e4SLinus Torvalds payload = skb->data; 2291da177e4SLinus Torvalds ip_icmp_error(sk, skb, err, 0, info, payload); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (inet->recverr || harderr) { 2331da177e4SLinus Torvalds sk->sk_err = err; 2341da177e4SLinus Torvalds sk->sk_error_report(sk); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds /* Charge it to the socket. */ 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds if (sock_queue_rcv_skb(sk, skb) < 0) { 2431da177e4SLinus Torvalds /* FIXME: increment a raw drops counter here */ 2441da177e4SLinus Torvalds kfree_skb(skb); 2451da177e4SLinus Torvalds return NET_RX_DROP; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds return NET_RX_SUCCESS; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds int raw_rcv(struct sock *sk, struct sk_buff *skb) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 2541da177e4SLinus Torvalds kfree_skb(skb); 2551da177e4SLinus Torvalds return NET_RX_DROP; 2561da177e4SLinus Torvalds } 257b59c2701SPatrick McHardy nf_reset(skb); 2581da177e4SLinus Torvalds 259*d56f90a7SArnaldo Carvalho de Melo skb_push(skb, skb->data - skb_network_header(skb)); 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds raw_rcv_skb(sk, skb); 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 265f7d7fc03SJesper Juhl static int raw_send_hdrinc(struct sock *sk, void *from, size_t length, 2661da177e4SLinus Torvalds struct rtable *rt, 2671da177e4SLinus Torvalds unsigned int flags) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2701da177e4SLinus Torvalds int hh_len; 2711da177e4SLinus Torvalds struct iphdr *iph; 2721da177e4SLinus Torvalds struct sk_buff *skb; 2731da177e4SLinus Torvalds int err; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (length > rt->u.dst.dev->mtu) { 2761da177e4SLinus Torvalds ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport, 2771da177e4SLinus Torvalds rt->u.dst.dev->mtu); 2781da177e4SLinus Torvalds return -EMSGSIZE; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds if (flags&MSG_PROBE) 2811da177e4SLinus Torvalds goto out; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds hh_len = LL_RESERVED_SPACE(rt->u.dst.dev); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, length+hh_len+15, 2861da177e4SLinus Torvalds flags&MSG_DONTWAIT, &err); 2871da177e4SLinus Torvalds if (skb == NULL) 2881da177e4SLinus Torvalds goto error; 2891da177e4SLinus Torvalds skb_reserve(skb, hh_len); 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds skb->priority = sk->sk_priority; 2921da177e4SLinus Torvalds skb->dst = dst_clone(&rt->u.dst); 2931da177e4SLinus Torvalds 2947e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2957e28ecc2SArnaldo Carvalho de Melo iph = skb->nh.iph; 2967e28ecc2SArnaldo Carvalho de Melo skb_put(skb, length); 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds skb->h.raw = skb->nh.raw; 3011da177e4SLinus Torvalds err = memcpy_fromiovecend((void *)iph, from, 0, length); 3021da177e4SLinus Torvalds if (err) 3031da177e4SLinus Torvalds goto error_fault; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* We don't modify invalid header */ 306f7d7fc03SJesper Juhl if (length >= sizeof(*iph) && iph->ihl * 4U <= length) { 3071da177e4SLinus Torvalds if (!iph->saddr) 3081da177e4SLinus Torvalds iph->saddr = rt->rt_src; 3091da177e4SLinus Torvalds iph->check = 0; 3101da177e4SLinus Torvalds iph->tot_len = htons(length); 3111da177e4SLinus Torvalds if (!iph->id) 3121da177e4SLinus Torvalds ip_select_ident(iph, &rt->u.dst, NULL); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev, 3181da177e4SLinus Torvalds dst_output); 3191da177e4SLinus Torvalds if (err > 0) 3201da177e4SLinus Torvalds err = inet->recverr ? net_xmit_errno(err) : 0; 3211da177e4SLinus Torvalds if (err) 3221da177e4SLinus Torvalds goto error; 3231da177e4SLinus Torvalds out: 3241da177e4SLinus Torvalds return 0; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds error_fault: 3271da177e4SLinus Torvalds err = -EFAULT; 3281da177e4SLinus Torvalds kfree_skb(skb); 3291da177e4SLinus Torvalds error: 3301da177e4SLinus Torvalds IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS); 3311da177e4SLinus Torvalds return err; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 334a27b58feSHeiko Carstens static int raw_probe_proto_opt(struct flowi *fl, struct msghdr *msg) 3351da177e4SLinus Torvalds { 3361da177e4SLinus Torvalds struct iovec *iov; 3371da177e4SLinus Torvalds u8 __user *type = NULL; 3381da177e4SLinus Torvalds u8 __user *code = NULL; 3391da177e4SLinus Torvalds int probed = 0; 34093765d8aSJesper Juhl unsigned int i; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (!msg->msg_iov) 343a27b58feSHeiko Carstens return 0; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds for (i = 0; i < msg->msg_iovlen; i++) { 3461da177e4SLinus Torvalds iov = &msg->msg_iov[i]; 3471da177e4SLinus Torvalds if (!iov) 3481da177e4SLinus Torvalds continue; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds switch (fl->proto) { 3511da177e4SLinus Torvalds case IPPROTO_ICMP: 3521da177e4SLinus Torvalds /* check if one-byte field is readable or not. */ 3531da177e4SLinus Torvalds if (iov->iov_base && iov->iov_len < 1) 3541da177e4SLinus Torvalds break; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds if (!type) { 3571da177e4SLinus Torvalds type = iov->iov_base; 3581da177e4SLinus Torvalds /* check if code field is readable or not. */ 3591da177e4SLinus Torvalds if (iov->iov_len > 1) 3601da177e4SLinus Torvalds code = type + 1; 3611da177e4SLinus Torvalds } else if (!code) 3621da177e4SLinus Torvalds code = iov->iov_base; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds if (type && code) { 365a27b58feSHeiko Carstens if (get_user(fl->fl_icmp_type, type) || 366a27b58feSHeiko Carstens get_user(fl->fl_icmp_code, code)) 367a27b58feSHeiko Carstens return -EFAULT; 3681da177e4SLinus Torvalds probed = 1; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds break; 3711da177e4SLinus Torvalds default: 3721da177e4SLinus Torvalds probed = 1; 3731da177e4SLinus Torvalds break; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds if (probed) 3761da177e4SLinus Torvalds break; 3771da177e4SLinus Torvalds } 378a27b58feSHeiko Carstens return 0; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 3821da177e4SLinus Torvalds size_t len) 3831da177e4SLinus Torvalds { 3841da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3851da177e4SLinus Torvalds struct ipcm_cookie ipc; 3861da177e4SLinus Torvalds struct rtable *rt = NULL; 3871da177e4SLinus Torvalds int free = 0; 3883ca3c68eSAl Viro __be32 daddr; 389c1d18f9fSAl Viro __be32 saddr; 3901da177e4SLinus Torvalds u8 tos; 3911da177e4SLinus Torvalds int err; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds err = -EMSGSIZE; 394926d4b81SJesper Juhl if (len > 0xFFFF) 3951da177e4SLinus Torvalds goto out; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* 3981da177e4SLinus Torvalds * Check the flags. 3991da177e4SLinus Torvalds */ 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds err = -EOPNOTSUPP; 4021da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */ 4031da177e4SLinus Torvalds goto out; /* compatibility */ 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * Get and verify the address. 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (msg->msg_namelen) { 4101da177e4SLinus Torvalds struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name; 4111da177e4SLinus Torvalds err = -EINVAL; 4121da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(*usin)) 4131da177e4SLinus Torvalds goto out; 4141da177e4SLinus Torvalds if (usin->sin_family != AF_INET) { 4151da177e4SLinus Torvalds static int complained; 4161da177e4SLinus Torvalds if (!complained++) 4171da177e4SLinus Torvalds printk(KERN_INFO "%s forgot to set AF_INET in " 4181da177e4SLinus Torvalds "raw sendmsg. Fix it!\n", 4191da177e4SLinus Torvalds current->comm); 4201da177e4SLinus Torvalds err = -EAFNOSUPPORT; 4211da177e4SLinus Torvalds if (usin->sin_family) 4221da177e4SLinus Torvalds goto out; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds daddr = usin->sin_addr.s_addr; 4251da177e4SLinus Torvalds /* ANK: I did not forget to get protocol from port field. 4261da177e4SLinus Torvalds * I just do not know, who uses this weirdness. 4271da177e4SLinus Torvalds * IP_HDRINCL is much more convenient. 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds } else { 4301da177e4SLinus Torvalds err = -EDESTADDRREQ; 4311da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 4321da177e4SLinus Torvalds goto out; 4331da177e4SLinus Torvalds daddr = inet->daddr; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds ipc.addr = inet->saddr; 4371da177e4SLinus Torvalds ipc.opt = NULL; 4381da177e4SLinus Torvalds ipc.oif = sk->sk_bound_dev_if; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds if (msg->msg_controllen) { 4411da177e4SLinus Torvalds err = ip_cmsg_send(msg, &ipc); 4421da177e4SLinus Torvalds if (err) 4431da177e4SLinus Torvalds goto out; 4441da177e4SLinus Torvalds if (ipc.opt) 4451da177e4SLinus Torvalds free = 1; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds saddr = ipc.addr; 4491da177e4SLinus Torvalds ipc.addr = daddr; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds if (!ipc.opt) 4521da177e4SLinus Torvalds ipc.opt = inet->opt; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds if (ipc.opt) { 4551da177e4SLinus Torvalds err = -EINVAL; 4561da177e4SLinus Torvalds /* Linux does not mangle headers on raw sockets, 4571da177e4SLinus Torvalds * so that IP options + IP_HDRINCL is non-sense. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds if (inet->hdrincl) 4601da177e4SLinus Torvalds goto done; 4611da177e4SLinus Torvalds if (ipc.opt->srr) { 4621da177e4SLinus Torvalds if (!daddr) 4631da177e4SLinus Torvalds goto done; 4641da177e4SLinus Torvalds daddr = ipc.opt->faddr; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds tos = RT_CONN_FLAGS(sk); 4681da177e4SLinus Torvalds if (msg->msg_flags & MSG_DONTROUTE) 4691da177e4SLinus Torvalds tos |= RTO_ONLINK; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if (MULTICAST(daddr)) { 4721da177e4SLinus Torvalds if (!ipc.oif) 4731da177e4SLinus Torvalds ipc.oif = inet->mc_index; 4741da177e4SLinus Torvalds if (!saddr) 4751da177e4SLinus Torvalds saddr = inet->mc_addr; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds struct flowi fl = { .oif = ipc.oif, 4801da177e4SLinus Torvalds .nl_u = { .ip4_u = 4811da177e4SLinus Torvalds { .daddr = daddr, 4821da177e4SLinus Torvalds .saddr = saddr, 4831da177e4SLinus Torvalds .tos = tos } }, 4841da177e4SLinus Torvalds .proto = inet->hdrincl ? IPPROTO_RAW : 4851da177e4SLinus Torvalds sk->sk_protocol, 4861da177e4SLinus Torvalds }; 487a27b58feSHeiko Carstens if (!inet->hdrincl) { 488a27b58feSHeiko Carstens err = raw_probe_proto_opt(&fl, msg); 489a27b58feSHeiko Carstens if (err) 490a27b58feSHeiko Carstens goto done; 491a27b58feSHeiko Carstens } 4921da177e4SLinus Torvalds 493beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 4948eb9086fSDavid S. Miller err = ip_route_output_flow(&rt, &fl, sk, 1); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds if (err) 4971da177e4SLinus Torvalds goto done; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds err = -EACCES; 5001da177e4SLinus Torvalds if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST)) 5011da177e4SLinus Torvalds goto done; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (msg->msg_flags & MSG_CONFIRM) 5041da177e4SLinus Torvalds goto do_confirm; 5051da177e4SLinus Torvalds back_from_confirm: 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (inet->hdrincl) 5081da177e4SLinus Torvalds err = raw_send_hdrinc(sk, msg->msg_iov, len, 5091da177e4SLinus Torvalds rt, msg->msg_flags); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds else { 5121da177e4SLinus Torvalds if (!ipc.addr) 5131da177e4SLinus Torvalds ipc.addr = rt->rt_dst; 5141da177e4SLinus Torvalds lock_sock(sk); 5151da177e4SLinus Torvalds err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0, 5161da177e4SLinus Torvalds &ipc, rt, msg->msg_flags); 5171da177e4SLinus Torvalds if (err) 5181da177e4SLinus Torvalds ip_flush_pending_frames(sk); 5191da177e4SLinus Torvalds else if (!(msg->msg_flags & MSG_MORE)) 5201da177e4SLinus Torvalds err = ip_push_pending_frames(sk); 5211da177e4SLinus Torvalds release_sock(sk); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds done: 5241da177e4SLinus Torvalds if (free) 5251da177e4SLinus Torvalds kfree(ipc.opt); 5261da177e4SLinus Torvalds ip_rt_put(rt); 5271da177e4SLinus Torvalds 5285418c692SJesper Juhl out: 5295418c692SJesper Juhl if (err < 0) 5305418c692SJesper Juhl return err; 5315418c692SJesper Juhl return len; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds do_confirm: 5341da177e4SLinus Torvalds dst_confirm(&rt->u.dst); 5351da177e4SLinus Torvalds if (!(msg->msg_flags & MSG_PROBE) || len) 5361da177e4SLinus Torvalds goto back_from_confirm; 5371da177e4SLinus Torvalds err = 0; 5381da177e4SLinus Torvalds goto done; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds static void raw_close(struct sock *sk, long timeout) 5421da177e4SLinus Torvalds { 5431da177e4SLinus Torvalds /* 5441da177e4SLinus Torvalds * Raw sockets may have direct kernel refereneces. Kill them. 5451da177e4SLinus Torvalds */ 5461da177e4SLinus Torvalds ip_ra_control(sk, 0, NULL); 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds sk_common_release(sk); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* This gets rid of all the nasties in af_inet. -DaveM */ 5521da177e4SLinus Torvalds static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5551da177e4SLinus Torvalds struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; 5561da177e4SLinus Torvalds int ret = -EINVAL; 5571da177e4SLinus Torvalds int chk_addr_ret; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in)) 5601da177e4SLinus Torvalds goto out; 5611da177e4SLinus Torvalds chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr); 5621da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 5631da177e4SLinus Torvalds if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL && 5641da177e4SLinus Torvalds chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) 5651da177e4SLinus Torvalds goto out; 5661da177e4SLinus Torvalds inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr; 5671da177e4SLinus Torvalds if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 5681da177e4SLinus Torvalds inet->saddr = 0; /* Use device */ 5691da177e4SLinus Torvalds sk_dst_reset(sk); 5701da177e4SLinus Torvalds ret = 0; 5711da177e4SLinus Torvalds out: return ret; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds /* 5751da177e4SLinus Torvalds * This should be easy, if there is something there 5761da177e4SLinus Torvalds * we return it, otherwise we block. 5771da177e4SLinus Torvalds */ 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 5801da177e4SLinus Torvalds size_t len, int noblock, int flags, int *addr_len) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5831da177e4SLinus Torvalds size_t copied = 0; 5841da177e4SLinus Torvalds int err = -EOPNOTSUPP; 5851da177e4SLinus Torvalds struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 5861da177e4SLinus Torvalds struct sk_buff *skb; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds if (flags & MSG_OOB) 5891da177e4SLinus Torvalds goto out; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds if (addr_len) 5921da177e4SLinus Torvalds *addr_len = sizeof(*sin); 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) { 5951da177e4SLinus Torvalds err = ip_recv_error(sk, msg, len); 5961da177e4SLinus Torvalds goto out; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags, noblock, &err); 6001da177e4SLinus Torvalds if (!skb) 6011da177e4SLinus Torvalds goto out; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds copied = skb->len; 6041da177e4SLinus Torvalds if (len < copied) { 6051da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 6061da177e4SLinus Torvalds copied = len; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 6101da177e4SLinus Torvalds if (err) 6111da177e4SLinus Torvalds goto done; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds sock_recv_timestamp(msg, sk, skb); 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds /* Copy the address. */ 6161da177e4SLinus Torvalds if (sin) { 6171da177e4SLinus Torvalds sin->sin_family = AF_INET; 6181da177e4SLinus Torvalds sin->sin_addr.s_addr = skb->nh.iph->saddr; 619f59fc7f3STetsuo Handa sin->sin_port = 0; 6201da177e4SLinus Torvalds memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds if (inet->cmsg_flags) 6231da177e4SLinus Torvalds ip_cmsg_recv(msg, skb); 6241da177e4SLinus Torvalds if (flags & MSG_TRUNC) 6251da177e4SLinus Torvalds copied = skb->len; 6261da177e4SLinus Torvalds done: 6271da177e4SLinus Torvalds skb_free_datagram(sk, skb); 6285418c692SJesper Juhl out: 6295418c692SJesper Juhl if (err) 6305418c692SJesper Juhl return err; 6315418c692SJesper Juhl return copied; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds static int raw_init(struct sock *sk) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds struct raw_sock *rp = raw_sk(sk); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (inet_sk(sk)->num == IPPROTO_ICMP) 6391da177e4SLinus Torvalds memset(&rp->filter, 0, sizeof(rp->filter)); 6401da177e4SLinus Torvalds return 0; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen) 6441da177e4SLinus Torvalds { 6451da177e4SLinus Torvalds if (optlen > sizeof(struct icmp_filter)) 6461da177e4SLinus Torvalds optlen = sizeof(struct icmp_filter); 6471da177e4SLinus Torvalds if (copy_from_user(&raw_sk(sk)->filter, optval, optlen)) 6481da177e4SLinus Torvalds return -EFAULT; 6491da177e4SLinus Torvalds return 0; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen) 6531da177e4SLinus Torvalds { 6541da177e4SLinus Torvalds int len, ret = -EFAULT; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (get_user(len, optlen)) 6571da177e4SLinus Torvalds goto out; 6581da177e4SLinus Torvalds ret = -EINVAL; 6591da177e4SLinus Torvalds if (len < 0) 6601da177e4SLinus Torvalds goto out; 6611da177e4SLinus Torvalds if (len > sizeof(struct icmp_filter)) 6621da177e4SLinus Torvalds len = sizeof(struct icmp_filter); 6631da177e4SLinus Torvalds ret = -EFAULT; 6641da177e4SLinus Torvalds if (put_user(len, optlen) || 6651da177e4SLinus Torvalds copy_to_user(optval, &raw_sk(sk)->filter, len)) 6661da177e4SLinus Torvalds goto out; 6671da177e4SLinus Torvalds ret = 0; 6681da177e4SLinus Torvalds out: return ret; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6713fdadf7dSDmitry Mishin static int do_raw_setsockopt(struct sock *sk, int level, int optname, 6721da177e4SLinus Torvalds char __user *optval, int optlen) 6731da177e4SLinus Torvalds { 6741da177e4SLinus Torvalds if (optname == ICMP_FILTER) { 6751da177e4SLinus Torvalds if (inet_sk(sk)->num != IPPROTO_ICMP) 6761da177e4SLinus Torvalds return -EOPNOTSUPP; 6771da177e4SLinus Torvalds else 6781da177e4SLinus Torvalds return raw_seticmpfilter(sk, optval, optlen); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds return -ENOPROTOOPT; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6833fdadf7dSDmitry Mishin static int raw_setsockopt(struct sock *sk, int level, int optname, 6843fdadf7dSDmitry Mishin char __user *optval, int optlen) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds if (level != SOL_RAW) 6873fdadf7dSDmitry Mishin return ip_setsockopt(sk, level, optname, optval, optlen); 6883fdadf7dSDmitry Mishin return do_raw_setsockopt(sk, level, optname, optval, optlen); 6893fdadf7dSDmitry Mishin } 6901da177e4SLinus Torvalds 6913fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 6923fdadf7dSDmitry Mishin static int compat_raw_setsockopt(struct sock *sk, int level, int optname, 6933fdadf7dSDmitry Mishin char __user *optval, int optlen) 6943fdadf7dSDmitry Mishin { 6953fdadf7dSDmitry Mishin if (level != SOL_RAW) 696543d9cfeSArnaldo Carvalho de Melo return compat_ip_setsockopt(sk, level, optname, optval, optlen); 6973fdadf7dSDmitry Mishin return do_raw_setsockopt(sk, level, optname, optval, optlen); 6983fdadf7dSDmitry Mishin } 6993fdadf7dSDmitry Mishin #endif 7003fdadf7dSDmitry Mishin 7013fdadf7dSDmitry Mishin static int do_raw_getsockopt(struct sock *sk, int level, int optname, 7023fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7033fdadf7dSDmitry Mishin { 7041da177e4SLinus Torvalds if (optname == ICMP_FILTER) { 7051da177e4SLinus Torvalds if (inet_sk(sk)->num != IPPROTO_ICMP) 7061da177e4SLinus Torvalds return -EOPNOTSUPP; 7071da177e4SLinus Torvalds else 7081da177e4SLinus Torvalds return raw_geticmpfilter(sk, optval, optlen); 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds return -ENOPROTOOPT; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7133fdadf7dSDmitry Mishin static int raw_getsockopt(struct sock *sk, int level, int optname, 7143fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7153fdadf7dSDmitry Mishin { 7163fdadf7dSDmitry Mishin if (level != SOL_RAW) 7173fdadf7dSDmitry Mishin return ip_getsockopt(sk, level, optname, optval, optlen); 7183fdadf7dSDmitry Mishin return do_raw_getsockopt(sk, level, optname, optval, optlen); 7193fdadf7dSDmitry Mishin } 7203fdadf7dSDmitry Mishin 7213fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7223fdadf7dSDmitry Mishin static int compat_raw_getsockopt(struct sock *sk, int level, int optname, 7233fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7243fdadf7dSDmitry Mishin { 7253fdadf7dSDmitry Mishin if (level != SOL_RAW) 726543d9cfeSArnaldo Carvalho de Melo return compat_ip_getsockopt(sk, level, optname, optval, optlen); 7273fdadf7dSDmitry Mishin return do_raw_getsockopt(sk, level, optname, optval, optlen); 7283fdadf7dSDmitry Mishin } 7293fdadf7dSDmitry Mishin #endif 7303fdadf7dSDmitry Mishin 7311da177e4SLinus Torvalds static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds switch (cmd) { 7341da177e4SLinus Torvalds case SIOCOUTQ: { 7351da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 7361da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds case SIOCINQ: { 7391da177e4SLinus Torvalds struct sk_buff *skb; 7401da177e4SLinus Torvalds int amount = 0; 7411da177e4SLinus Torvalds 742e0f9f858SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 7431da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 7441da177e4SLinus Torvalds if (skb != NULL) 7451da177e4SLinus Torvalds amount = skb->len; 746e0f9f858SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 7471da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds default: 7511da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 7521da177e4SLinus Torvalds return ipmr_ioctl(sk, cmd, (void __user *)arg); 7531da177e4SLinus Torvalds #else 7541da177e4SLinus Torvalds return -ENOIOCTLCMD; 7551da177e4SLinus Torvalds #endif 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds struct proto raw_prot = { 7601da177e4SLinus Torvalds .name = "RAW", 7611da177e4SLinus Torvalds .owner = THIS_MODULE, 7621da177e4SLinus Torvalds .close = raw_close, 7631da177e4SLinus Torvalds .connect = ip4_datagram_connect, 7641da177e4SLinus Torvalds .disconnect = udp_disconnect, 7651da177e4SLinus Torvalds .ioctl = raw_ioctl, 7661da177e4SLinus Torvalds .init = raw_init, 7671da177e4SLinus Torvalds .setsockopt = raw_setsockopt, 7681da177e4SLinus Torvalds .getsockopt = raw_getsockopt, 7691da177e4SLinus Torvalds .sendmsg = raw_sendmsg, 7701da177e4SLinus Torvalds .recvmsg = raw_recvmsg, 7711da177e4SLinus Torvalds .bind = raw_bind, 7721da177e4SLinus Torvalds .backlog_rcv = raw_rcv_skb, 7731da177e4SLinus Torvalds .hash = raw_v4_hash, 7741da177e4SLinus Torvalds .unhash = raw_v4_unhash, 7751da177e4SLinus Torvalds .obj_size = sizeof(struct raw_sock), 776543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 777543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_raw_setsockopt, 778543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_raw_getsockopt, 779543d9cfeSArnaldo Carvalho de Melo #endif 7801da177e4SLinus Torvalds }; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7831da177e4SLinus Torvalds struct raw_iter_state { 7841da177e4SLinus Torvalds int bucket; 7851da177e4SLinus Torvalds }; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds #define raw_seq_private(seq) ((struct raw_iter_state *)(seq)->private) 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds static struct sock *raw_get_first(struct seq_file *seq) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct sock *sk; 7921da177e4SLinus Torvalds struct raw_iter_state* state = raw_seq_private(seq); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < RAWV4_HTABLE_SIZE; ++state->bucket) { 7951da177e4SLinus Torvalds struct hlist_node *node; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds sk_for_each(sk, node, &raw_v4_htable[state->bucket]) 7981da177e4SLinus Torvalds if (sk->sk_family == PF_INET) 7991da177e4SLinus Torvalds goto found; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds sk = NULL; 8021da177e4SLinus Torvalds found: 8031da177e4SLinus Torvalds return sk; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds struct raw_iter_state* state = raw_seq_private(seq); 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds do { 8111da177e4SLinus Torvalds sk = sk_next(sk); 8121da177e4SLinus Torvalds try_again: 8131da177e4SLinus Torvalds ; 8141da177e4SLinus Torvalds } while (sk && sk->sk_family != PF_INET); 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds if (!sk && ++state->bucket < RAWV4_HTABLE_SIZE) { 8171da177e4SLinus Torvalds sk = sk_head(&raw_v4_htable[state->bucket]); 8181da177e4SLinus Torvalds goto try_again; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds return sk; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos) 8241da177e4SLinus Torvalds { 8251da177e4SLinus Torvalds struct sock *sk = raw_get_first(seq); 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds if (sk) 8281da177e4SLinus Torvalds while (pos && (sk = raw_get_next(seq, sk)) != NULL) 8291da177e4SLinus Torvalds --pos; 8301da177e4SLinus Torvalds return pos ? NULL : sk; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds static void *raw_seq_start(struct seq_file *seq, loff_t *pos) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds read_lock(&raw_v4_lock); 8361da177e4SLinus Torvalds return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds static void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds struct sock *sk; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 8441da177e4SLinus Torvalds sk = raw_get_first(seq); 8451da177e4SLinus Torvalds else 8461da177e4SLinus Torvalds sk = raw_get_next(seq, v); 8471da177e4SLinus Torvalds ++*pos; 8481da177e4SLinus Torvalds return sk; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds static void raw_seq_stop(struct seq_file *seq, void *v) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds read_unlock(&raw_v4_lock); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds static __inline__ char *get_raw_sock(struct sock *sp, char *tmpbuf, int i) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sp); 859714e85beSAl Viro __be32 dest = inet->daddr, 8601da177e4SLinus Torvalds src = inet->rcv_saddr; 8611da177e4SLinus Torvalds __u16 destp = 0, 8621da177e4SLinus Torvalds srcp = inet->num; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X" 8651da177e4SLinus Torvalds " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p", 8661da177e4SLinus Torvalds i, src, srcp, dest, destp, sp->sk_state, 8671da177e4SLinus Torvalds atomic_read(&sp->sk_wmem_alloc), 8681da177e4SLinus Torvalds atomic_read(&sp->sk_rmem_alloc), 8691da177e4SLinus Torvalds 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 8701da177e4SLinus Torvalds atomic_read(&sp->sk_refcnt), sp); 8711da177e4SLinus Torvalds return tmpbuf; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds static int raw_seq_show(struct seq_file *seq, void *v) 8751da177e4SLinus Torvalds { 8761da177e4SLinus Torvalds char tmpbuf[129]; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 8791da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 8801da177e4SLinus Torvalds " sl local_address rem_address st tx_queue " 8811da177e4SLinus Torvalds "rx_queue tr tm->when retrnsmt uid timeout " 8821da177e4SLinus Torvalds "inode"); 8831da177e4SLinus Torvalds else { 8841da177e4SLinus Torvalds struct raw_iter_state *state = raw_seq_private(seq); 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds seq_printf(seq, "%-127s\n", 8871da177e4SLinus Torvalds get_raw_sock(v, tmpbuf, state->bucket)); 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds return 0; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds static struct seq_operations raw_seq_ops = { 8931da177e4SLinus Torvalds .start = raw_seq_start, 8941da177e4SLinus Torvalds .next = raw_seq_next, 8951da177e4SLinus Torvalds .stop = raw_seq_stop, 8961da177e4SLinus Torvalds .show = raw_seq_show, 8971da177e4SLinus Torvalds }; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds static int raw_seq_open(struct inode *inode, struct file *file) 9001da177e4SLinus Torvalds { 9011da177e4SLinus Torvalds struct seq_file *seq; 9021da177e4SLinus Torvalds int rc = -ENOMEM; 9031da177e4SLinus Torvalds struct raw_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (!s) 9061da177e4SLinus Torvalds goto out; 9071da177e4SLinus Torvalds rc = seq_open(file, &raw_seq_ops); 9081da177e4SLinus Torvalds if (rc) 9091da177e4SLinus Torvalds goto out_kfree; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds seq = file->private_data; 9121da177e4SLinus Torvalds seq->private = s; 9131da177e4SLinus Torvalds memset(s, 0, sizeof(*s)); 9141da177e4SLinus Torvalds out: 9151da177e4SLinus Torvalds return rc; 9161da177e4SLinus Torvalds out_kfree: 9171da177e4SLinus Torvalds kfree(s); 9181da177e4SLinus Torvalds goto out; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9219a32144eSArjan van de Ven static const struct file_operations raw_seq_fops = { 9221da177e4SLinus Torvalds .owner = THIS_MODULE, 9231da177e4SLinus Torvalds .open = raw_seq_open, 9241da177e4SLinus Torvalds .read = seq_read, 9251da177e4SLinus Torvalds .llseek = seq_lseek, 9261da177e4SLinus Torvalds .release = seq_release_private, 9271da177e4SLinus Torvalds }; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds int __init raw_proc_init(void) 9301da177e4SLinus Torvalds { 9311da177e4SLinus Torvalds if (!proc_net_fops_create("raw", S_IRUGO, &raw_seq_fops)) 9321da177e4SLinus Torvalds return -ENOMEM; 9331da177e4SLinus Torvalds return 0; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds void __init raw_proc_exit(void) 9371da177e4SLinus Torvalds { 9381da177e4SLinus Torvalds proc_net_remove("raw"); 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 941