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> 62457c4cbcSEric W. Biederman #include <net/net_namespace.h> 631da177e4SLinus Torvalds #include <net/dst.h> 641da177e4SLinus Torvalds #include <net/sock.h> 651da177e4SLinus Torvalds #include <linux/gfp.h> 661da177e4SLinus Torvalds #include <linux/ip.h> 671da177e4SLinus Torvalds #include <linux/net.h> 681da177e4SLinus Torvalds #include <net/ip.h> 691da177e4SLinus Torvalds #include <net/icmp.h> 701da177e4SLinus Torvalds #include <net/udp.h> 711da177e4SLinus Torvalds #include <net/raw.h> 721da177e4SLinus Torvalds #include <net/snmp.h> 73c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 741da177e4SLinus Torvalds #include <net/inet_common.h> 751da177e4SLinus Torvalds #include <net/checksum.h> 761da177e4SLinus Torvalds #include <net/xfrm.h> 771da177e4SLinus Torvalds #include <linux/rtnetlink.h> 781da177e4SLinus Torvalds #include <linux/proc_fs.h> 791da177e4SLinus Torvalds #include <linux/seq_file.h> 801da177e4SLinus Torvalds #include <linux/netfilter.h> 811da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 821da177e4SLinus Torvalds 83b673e4dfSPavel Emelyanov static struct raw_hashinfo raw_v4_hashinfo = { 84938b93adSRobert P. J. Day .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock), 85b673e4dfSPavel Emelyanov }; 861da177e4SLinus Torvalds 87fc8717baSPavel Emelyanov void raw_hash_sk(struct sock *sk) 881da177e4SLinus Torvalds { 89fc8717baSPavel Emelyanov struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; 9065b4c50bSPavel Emelyanov struct hlist_head *head; 911da177e4SLinus Torvalds 9265b4c50bSPavel Emelyanov head = &h->ht[inet_sk(sk)->num & (RAW_HTABLE_SIZE - 1)]; 9365b4c50bSPavel Emelyanov 9465b4c50bSPavel Emelyanov write_lock_bh(&h->lock); 951da177e4SLinus Torvalds sk_add_node(sk, head); 9665f76517SEric Dumazet sock_prot_inuse_add(sk->sk_prot, 1); 9765b4c50bSPavel Emelyanov write_unlock_bh(&h->lock); 9865b4c50bSPavel Emelyanov } 9965b4c50bSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_hash_sk); 10065b4c50bSPavel Emelyanov 101fc8717baSPavel Emelyanov void raw_unhash_sk(struct sock *sk) 102ab70768eSPavel Emelyanov { 103fc8717baSPavel Emelyanov struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; 104fc8717baSPavel Emelyanov 105ab70768eSPavel Emelyanov write_lock_bh(&h->lock); 106ab70768eSPavel Emelyanov if (sk_del_node_init(sk)) 10765f76517SEric Dumazet sock_prot_inuse_add(sk->sk_prot, -1); 108ab70768eSPavel Emelyanov write_unlock_bh(&h->lock); 109ab70768eSPavel Emelyanov } 110ab70768eSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_unhash_sk); 111ab70768eSPavel Emelyanov 112be185884SPavel Emelyanov static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk, 113be185884SPavel Emelyanov unsigned short num, __be32 raddr, __be32 laddr, int dif) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds struct hlist_node *node; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds sk_for_each_from(sk, node) { 1181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1191da177e4SLinus Torvalds 120be185884SPavel Emelyanov if (sk->sk_net == net && inet->num == num && 1211da177e4SLinus Torvalds !(inet->daddr && inet->daddr != raddr) && 1221da177e4SLinus Torvalds !(inet->rcv_saddr && inet->rcv_saddr != laddr) && 1231da177e4SLinus Torvalds !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)) 1241da177e4SLinus Torvalds goto found; /* gotcha */ 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds sk = NULL; 1271da177e4SLinus Torvalds found: 1281da177e4SLinus Torvalds return sk; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * 0 - deliver 1331da177e4SLinus Torvalds * 1 - block 1341da177e4SLinus Torvalds */ 1351da177e4SLinus Torvalds static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds int type; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds if (!pskb_may_pull(skb, sizeof(struct icmphdr))) 1401da177e4SLinus Torvalds return 1; 1411da177e4SLinus Torvalds 14288c7664fSArnaldo Carvalho de Melo type = icmp_hdr(skb)->type; 1431da177e4SLinus Torvalds if (type < 32) { 1441da177e4SLinus Torvalds __u32 data = raw_sk(sk)->filter.data; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds return ((1 << type) & data) != 0; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* Do not block unknown ICMP types */ 1501da177e4SLinus Torvalds return 0; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* IP input processing comes here for RAW socket delivery. 1541da177e4SLinus Torvalds * Caller owns SKB, so we must make clones. 1551da177e4SLinus Torvalds * 1561da177e4SLinus Torvalds * RFC 1122: SHOULD pass TOS value up to the transport layer. 1571da177e4SLinus Torvalds * -> It does. And not only TOS, but all IP header. 1581da177e4SLinus Torvalds */ 1597bc54c90SPavel Emelyanov static int raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds struct sock *sk; 1621da177e4SLinus Torvalds struct hlist_head *head; 163d13964f4SPatrick McHardy int delivered = 0; 164be185884SPavel Emelyanov struct net *net; 1651da177e4SLinus Torvalds 166b673e4dfSPavel Emelyanov read_lock(&raw_v4_hashinfo.lock); 167b673e4dfSPavel Emelyanov head = &raw_v4_hashinfo.ht[hash]; 1681da177e4SLinus Torvalds if (hlist_empty(head)) 1691da177e4SLinus Torvalds goto out; 170be185884SPavel Emelyanov 171*c346dca1SYOSHIFUJI Hideaki net = dev_net(skb->dev); 172be185884SPavel Emelyanov sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol, 1731da177e4SLinus Torvalds iph->saddr, iph->daddr, 1741da177e4SLinus Torvalds skb->dev->ifindex); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds while (sk) { 177d13964f4SPatrick McHardy delivered = 1; 1781da177e4SLinus Torvalds if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) { 1791da177e4SLinus Torvalds struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* Not releasing hash table! */ 1821da177e4SLinus Torvalds if (clone) 1831da177e4SLinus Torvalds raw_rcv(sk, clone); 1841da177e4SLinus Torvalds } 185be185884SPavel Emelyanov sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol, 1861da177e4SLinus Torvalds iph->saddr, iph->daddr, 1871da177e4SLinus Torvalds skb->dev->ifindex); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds out: 190b673e4dfSPavel Emelyanov read_unlock(&raw_v4_hashinfo.lock); 191d13964f4SPatrick McHardy return delivered; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1947bc54c90SPavel Emelyanov int raw_local_deliver(struct sk_buff *skb, int protocol) 1957bc54c90SPavel Emelyanov { 1967bc54c90SPavel Emelyanov int hash; 1977bc54c90SPavel Emelyanov struct sock *raw_sk; 1987bc54c90SPavel Emelyanov 199b673e4dfSPavel Emelyanov hash = protocol & (RAW_HTABLE_SIZE - 1); 200b673e4dfSPavel Emelyanov raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]); 2017bc54c90SPavel Emelyanov 2027bc54c90SPavel Emelyanov /* If there maybe a raw socket we must check - if not we 2037bc54c90SPavel Emelyanov * don't care less 2047bc54c90SPavel Emelyanov */ 2057bc54c90SPavel Emelyanov if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash)) 2067bc54c90SPavel Emelyanov raw_sk = NULL; 2077bc54c90SPavel Emelyanov 2087bc54c90SPavel Emelyanov return raw_sk != NULL; 2097bc54c90SPavel Emelyanov 2107bc54c90SPavel Emelyanov } 2117bc54c90SPavel Emelyanov 2127bc54c90SPavel Emelyanov static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21588c7664fSArnaldo Carvalho de Melo const int type = icmp_hdr(skb)->type; 21688c7664fSArnaldo Carvalho de Melo const int code = icmp_hdr(skb)->code; 2171da177e4SLinus Torvalds int err = 0; 2181da177e4SLinus Torvalds int harderr = 0; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* Report error on raw socket, if: 2211da177e4SLinus Torvalds 1. User requested ip_recverr. 2221da177e4SLinus Torvalds 2. Socket is connected (otherwise the error indication 2231da177e4SLinus Torvalds is useless without ip_recverr and error is hard. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED) 2261da177e4SLinus Torvalds return; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds switch (type) { 2291da177e4SLinus Torvalds default: 2301da177e4SLinus Torvalds case ICMP_TIME_EXCEEDED: 2311da177e4SLinus Torvalds err = EHOSTUNREACH; 2321da177e4SLinus Torvalds break; 2331da177e4SLinus Torvalds case ICMP_SOURCE_QUENCH: 2341da177e4SLinus Torvalds return; 2351da177e4SLinus Torvalds case ICMP_PARAMETERPROB: 2361da177e4SLinus Torvalds err = EPROTO; 2371da177e4SLinus Torvalds harderr = 1; 2381da177e4SLinus Torvalds break; 2391da177e4SLinus Torvalds case ICMP_DEST_UNREACH: 2401da177e4SLinus Torvalds err = EHOSTUNREACH; 2411da177e4SLinus Torvalds if (code > NR_ICMP_UNREACH) 2421da177e4SLinus Torvalds break; 2431da177e4SLinus Torvalds err = icmp_err_convert[code].errno; 2441da177e4SLinus Torvalds harderr = icmp_err_convert[code].fatal; 2451da177e4SLinus Torvalds if (code == ICMP_FRAG_NEEDED) { 2461da177e4SLinus Torvalds harderr = inet->pmtudisc != IP_PMTUDISC_DONT; 2471da177e4SLinus Torvalds err = EMSGSIZE; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds if (inet->recverr) { 2521da177e4SLinus Torvalds struct iphdr *iph = (struct iphdr*)skb->data; 2531da177e4SLinus Torvalds u8 *payload = skb->data + (iph->ihl << 2); 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds if (inet->hdrincl) 2561da177e4SLinus Torvalds payload = skb->data; 2571da177e4SLinus Torvalds ip_icmp_error(sk, skb, err, 0, info, payload); 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds if (inet->recverr || harderr) { 2611da177e4SLinus Torvalds sk->sk_err = err; 2621da177e4SLinus Torvalds sk->sk_error_report(sk); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2667bc54c90SPavel Emelyanov void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info) 2677bc54c90SPavel Emelyanov { 2687bc54c90SPavel Emelyanov int hash; 2697bc54c90SPavel Emelyanov struct sock *raw_sk; 2707bc54c90SPavel Emelyanov struct iphdr *iph; 271be185884SPavel Emelyanov struct net *net; 2727bc54c90SPavel Emelyanov 273b673e4dfSPavel Emelyanov hash = protocol & (RAW_HTABLE_SIZE - 1); 2747bc54c90SPavel Emelyanov 275b673e4dfSPavel Emelyanov read_lock(&raw_v4_hashinfo.lock); 276b673e4dfSPavel Emelyanov raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]); 2777bc54c90SPavel Emelyanov if (raw_sk != NULL) { 2787bc54c90SPavel Emelyanov iph = (struct iphdr *)skb->data; 279*c346dca1SYOSHIFUJI Hideaki net = dev_net(skb->dev); 280be185884SPavel Emelyanov 281be185884SPavel Emelyanov while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol, 282be185884SPavel Emelyanov iph->daddr, iph->saddr, 2837bc54c90SPavel Emelyanov skb->dev->ifindex)) != NULL) { 2847bc54c90SPavel Emelyanov raw_err(raw_sk, skb, info); 2857bc54c90SPavel Emelyanov raw_sk = sk_next(raw_sk); 2867bc54c90SPavel Emelyanov iph = (struct iphdr *)skb->data; 2877bc54c90SPavel Emelyanov } 2887bc54c90SPavel Emelyanov } 289b673e4dfSPavel Emelyanov read_unlock(&raw_v4_hashinfo.lock); 2907bc54c90SPavel Emelyanov } 2917bc54c90SPavel Emelyanov 2921da177e4SLinus Torvalds static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds /* Charge it to the socket. */ 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds if (sock_queue_rcv_skb(sk, skb) < 0) { 29733c732c3SWang Chen atomic_inc(&sk->sk_drops); 2981da177e4SLinus Torvalds kfree_skb(skb); 2991da177e4SLinus Torvalds return NET_RX_DROP; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds return NET_RX_SUCCESS; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds int raw_rcv(struct sock *sk, struct sk_buff *skb) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 30833c732c3SWang Chen atomic_inc(&sk->sk_drops); 3091da177e4SLinus Torvalds kfree_skb(skb); 3101da177e4SLinus Torvalds return NET_RX_DROP; 3111da177e4SLinus Torvalds } 312b59c2701SPatrick McHardy nf_reset(skb); 3131da177e4SLinus Torvalds 314d56f90a7SArnaldo Carvalho de Melo skb_push(skb, skb->data - skb_network_header(skb)); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds raw_rcv_skb(sk, skb); 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 320f7d7fc03SJesper Juhl static int raw_send_hdrinc(struct sock *sk, void *from, size_t length, 3211da177e4SLinus Torvalds struct rtable *rt, 3221da177e4SLinus Torvalds unsigned int flags) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3251da177e4SLinus Torvalds int hh_len; 3261da177e4SLinus Torvalds struct iphdr *iph; 3271da177e4SLinus Torvalds struct sk_buff *skb; 328f844c74fSHerbert Xu unsigned int iphlen; 3291da177e4SLinus Torvalds int err; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds if (length > rt->u.dst.dev->mtu) { 3321da177e4SLinus Torvalds ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport, 3331da177e4SLinus Torvalds rt->u.dst.dev->mtu); 3341da177e4SLinus Torvalds return -EMSGSIZE; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds if (flags&MSG_PROBE) 3371da177e4SLinus Torvalds goto out; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds hh_len = LL_RESERVED_SPACE(rt->u.dst.dev); 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, length+hh_len+15, 3421da177e4SLinus Torvalds flags&MSG_DONTWAIT, &err); 3431da177e4SLinus Torvalds if (skb == NULL) 3441da177e4SLinus Torvalds goto error; 3451da177e4SLinus Torvalds skb_reserve(skb, hh_len); 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds skb->priority = sk->sk_priority; 3484a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 3491da177e4SLinus Torvalds skb->dst = dst_clone(&rt->u.dst); 3501da177e4SLinus Torvalds 3517e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 352eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 3537e28ecc2SArnaldo Carvalho de Melo skb_put(skb, length); 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3561da177e4SLinus Torvalds 357b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 3581da177e4SLinus Torvalds err = memcpy_fromiovecend((void *)iph, from, 0, length); 3591da177e4SLinus Torvalds if (err) 3601da177e4SLinus Torvalds goto error_fault; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds /* We don't modify invalid header */ 363f844c74fSHerbert Xu iphlen = iph->ihl * 4; 364f844c74fSHerbert Xu if (iphlen >= sizeof(*iph) && iphlen <= length) { 3651da177e4SLinus Torvalds if (!iph->saddr) 3661da177e4SLinus Torvalds iph->saddr = rt->rt_src; 3671da177e4SLinus Torvalds iph->check = 0; 3681da177e4SLinus Torvalds iph->tot_len = htons(length); 3691da177e4SLinus Torvalds if (!iph->id) 3701da177e4SLinus Torvalds ip_select_ident(iph, &rt->u.dst, NULL); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 3731da177e4SLinus Torvalds } 37496793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 37596793b48SDavid L Stevens icmp_out_count(((struct icmphdr *) 37696793b48SDavid L Stevens skb_transport_header(skb))->type); 3771da177e4SLinus Torvalds 3786e23ae2aSPatrick McHardy err = NF_HOOK(PF_INET, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev, 3791da177e4SLinus Torvalds dst_output); 3801da177e4SLinus Torvalds if (err > 0) 3811da177e4SLinus Torvalds err = inet->recverr ? net_xmit_errno(err) : 0; 3821da177e4SLinus Torvalds if (err) 3831da177e4SLinus Torvalds goto error; 3841da177e4SLinus Torvalds out: 3851da177e4SLinus Torvalds return 0; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds error_fault: 3881da177e4SLinus Torvalds err = -EFAULT; 3891da177e4SLinus Torvalds kfree_skb(skb); 3901da177e4SLinus Torvalds error: 3911da177e4SLinus Torvalds IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS); 3921da177e4SLinus Torvalds return err; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 395a27b58feSHeiko Carstens static int raw_probe_proto_opt(struct flowi *fl, struct msghdr *msg) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds struct iovec *iov; 3981da177e4SLinus Torvalds u8 __user *type = NULL; 3991da177e4SLinus Torvalds u8 __user *code = NULL; 4001da177e4SLinus Torvalds int probed = 0; 40193765d8aSJesper Juhl unsigned int i; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds if (!msg->msg_iov) 404a27b58feSHeiko Carstens return 0; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds for (i = 0; i < msg->msg_iovlen; i++) { 4071da177e4SLinus Torvalds iov = &msg->msg_iov[i]; 4081da177e4SLinus Torvalds if (!iov) 4091da177e4SLinus Torvalds continue; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds switch (fl->proto) { 4121da177e4SLinus Torvalds case IPPROTO_ICMP: 4131da177e4SLinus Torvalds /* check if one-byte field is readable or not. */ 4141da177e4SLinus Torvalds if (iov->iov_base && iov->iov_len < 1) 4151da177e4SLinus Torvalds break; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds if (!type) { 4181da177e4SLinus Torvalds type = iov->iov_base; 4191da177e4SLinus Torvalds /* check if code field is readable or not. */ 4201da177e4SLinus Torvalds if (iov->iov_len > 1) 4211da177e4SLinus Torvalds code = type + 1; 4221da177e4SLinus Torvalds } else if (!code) 4231da177e4SLinus Torvalds code = iov->iov_base; 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds if (type && code) { 426a27b58feSHeiko Carstens if (get_user(fl->fl_icmp_type, type) || 427a27b58feSHeiko Carstens get_user(fl->fl_icmp_code, code)) 428a27b58feSHeiko Carstens return -EFAULT; 4291da177e4SLinus Torvalds probed = 1; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds break; 4321da177e4SLinus Torvalds default: 4331da177e4SLinus Torvalds probed = 1; 4341da177e4SLinus Torvalds break; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds if (probed) 4371da177e4SLinus Torvalds break; 4381da177e4SLinus Torvalds } 439a27b58feSHeiko Carstens return 0; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 4431da177e4SLinus Torvalds size_t len) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4461da177e4SLinus Torvalds struct ipcm_cookie ipc; 4471da177e4SLinus Torvalds struct rtable *rt = NULL; 4481da177e4SLinus Torvalds int free = 0; 4493ca3c68eSAl Viro __be32 daddr; 450c1d18f9fSAl Viro __be32 saddr; 4511da177e4SLinus Torvalds u8 tos; 4521da177e4SLinus Torvalds int err; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds err = -EMSGSIZE; 455926d4b81SJesper Juhl if (len > 0xFFFF) 4561da177e4SLinus Torvalds goto out; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds /* 4591da177e4SLinus Torvalds * Check the flags. 4601da177e4SLinus Torvalds */ 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds err = -EOPNOTSUPP; 4631da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */ 4641da177e4SLinus Torvalds goto out; /* compatibility */ 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /* 4671da177e4SLinus Torvalds * Get and verify the address. 4681da177e4SLinus Torvalds */ 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (msg->msg_namelen) { 4711da177e4SLinus Torvalds struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name; 4721da177e4SLinus Torvalds err = -EINVAL; 4731da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(*usin)) 4741da177e4SLinus Torvalds goto out; 4751da177e4SLinus Torvalds if (usin->sin_family != AF_INET) { 4761da177e4SLinus Torvalds static int complained; 4771da177e4SLinus Torvalds if (!complained++) 4781da177e4SLinus Torvalds printk(KERN_INFO "%s forgot to set AF_INET in " 4791da177e4SLinus Torvalds "raw sendmsg. Fix it!\n", 4801da177e4SLinus Torvalds current->comm); 4811da177e4SLinus Torvalds err = -EAFNOSUPPORT; 4821da177e4SLinus Torvalds if (usin->sin_family) 4831da177e4SLinus Torvalds goto out; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds daddr = usin->sin_addr.s_addr; 4861da177e4SLinus Torvalds /* ANK: I did not forget to get protocol from port field. 4871da177e4SLinus Torvalds * I just do not know, who uses this weirdness. 4881da177e4SLinus Torvalds * IP_HDRINCL is much more convenient. 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds } else { 4911da177e4SLinus Torvalds err = -EDESTADDRREQ; 4921da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 4931da177e4SLinus Torvalds goto out; 4941da177e4SLinus Torvalds daddr = inet->daddr; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds ipc.addr = inet->saddr; 4981da177e4SLinus Torvalds ipc.opt = NULL; 4991da177e4SLinus Torvalds ipc.oif = sk->sk_bound_dev_if; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (msg->msg_controllen) { 50205cf89d4SDenis V. Lunev err = ip_cmsg_send(sk->sk_net, msg, &ipc); 5031da177e4SLinus Torvalds if (err) 5041da177e4SLinus Torvalds goto out; 5051da177e4SLinus Torvalds if (ipc.opt) 5061da177e4SLinus Torvalds free = 1; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds saddr = ipc.addr; 5101da177e4SLinus Torvalds ipc.addr = daddr; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds if (!ipc.opt) 5131da177e4SLinus Torvalds ipc.opt = inet->opt; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds if (ipc.opt) { 5161da177e4SLinus Torvalds err = -EINVAL; 5171da177e4SLinus Torvalds /* Linux does not mangle headers on raw sockets, 5181da177e4SLinus Torvalds * so that IP options + IP_HDRINCL is non-sense. 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds if (inet->hdrincl) 5211da177e4SLinus Torvalds goto done; 5221da177e4SLinus Torvalds if (ipc.opt->srr) { 5231da177e4SLinus Torvalds if (!daddr) 5241da177e4SLinus Torvalds goto done; 5251da177e4SLinus Torvalds daddr = ipc.opt->faddr; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds tos = RT_CONN_FLAGS(sk); 5291da177e4SLinus Torvalds if (msg->msg_flags & MSG_DONTROUTE) 5301da177e4SLinus Torvalds tos |= RTO_ONLINK; 5311da177e4SLinus Torvalds 532f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 5331da177e4SLinus Torvalds if (!ipc.oif) 5341da177e4SLinus Torvalds ipc.oif = inet->mc_index; 5351da177e4SLinus Torvalds if (!saddr) 5361da177e4SLinus Torvalds saddr = inet->mc_addr; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct flowi fl = { .oif = ipc.oif, 5414a19ec58SLaszlo Attila Toth .mark = sk->sk_mark, 5421da177e4SLinus Torvalds .nl_u = { .ip4_u = 5431da177e4SLinus Torvalds { .daddr = daddr, 5441da177e4SLinus Torvalds .saddr = saddr, 5451da177e4SLinus Torvalds .tos = tos } }, 5461da177e4SLinus Torvalds .proto = inet->hdrincl ? IPPROTO_RAW : 5471da177e4SLinus Torvalds sk->sk_protocol, 5481da177e4SLinus Torvalds }; 549a27b58feSHeiko Carstens if (!inet->hdrincl) { 550a27b58feSHeiko Carstens err = raw_probe_proto_opt(&fl, msg); 551a27b58feSHeiko Carstens if (err) 552a27b58feSHeiko Carstens goto done; 553a27b58feSHeiko Carstens } 5541da177e4SLinus Torvalds 555beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 55605cf89d4SDenis V. Lunev err = ip_route_output_flow(sk->sk_net, &rt, &fl, sk, 1); 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds if (err) 5591da177e4SLinus Torvalds goto done; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds err = -EACCES; 5621da177e4SLinus Torvalds if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST)) 5631da177e4SLinus Torvalds goto done; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds if (msg->msg_flags & MSG_CONFIRM) 5661da177e4SLinus Torvalds goto do_confirm; 5671da177e4SLinus Torvalds back_from_confirm: 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (inet->hdrincl) 5701da177e4SLinus Torvalds err = raw_send_hdrinc(sk, msg->msg_iov, len, 5711da177e4SLinus Torvalds rt, msg->msg_flags); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds else { 5741da177e4SLinus Torvalds if (!ipc.addr) 5751da177e4SLinus Torvalds ipc.addr = rt->rt_dst; 5761da177e4SLinus Torvalds lock_sock(sk); 5771da177e4SLinus Torvalds err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0, 5781da177e4SLinus Torvalds &ipc, rt, msg->msg_flags); 5791da177e4SLinus Torvalds if (err) 5801da177e4SLinus Torvalds ip_flush_pending_frames(sk); 5811da177e4SLinus Torvalds else if (!(msg->msg_flags & MSG_MORE)) 5821da177e4SLinus Torvalds err = ip_push_pending_frames(sk); 5831da177e4SLinus Torvalds release_sock(sk); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds done: 5861da177e4SLinus Torvalds if (free) 5871da177e4SLinus Torvalds kfree(ipc.opt); 5881da177e4SLinus Torvalds ip_rt_put(rt); 5891da177e4SLinus Torvalds 5905418c692SJesper Juhl out: 5915418c692SJesper Juhl if (err < 0) 5925418c692SJesper Juhl return err; 5935418c692SJesper Juhl return len; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds do_confirm: 5961da177e4SLinus Torvalds dst_confirm(&rt->u.dst); 5971da177e4SLinus Torvalds if (!(msg->msg_flags & MSG_PROBE) || len) 5981da177e4SLinus Torvalds goto back_from_confirm; 5991da177e4SLinus Torvalds err = 0; 6001da177e4SLinus Torvalds goto done; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds static void raw_close(struct sock *sk, long timeout) 6041da177e4SLinus Torvalds { 6051da177e4SLinus Torvalds /* 6061da177e4SLinus Torvalds * Raw sockets may have direct kernel refereneces. Kill them. 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds ip_ra_control(sk, 0, NULL); 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds sk_common_release(sk); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* This gets rid of all the nasties in af_inet. -DaveM */ 6141da177e4SLinus Torvalds static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 6171da177e4SLinus Torvalds struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; 6181da177e4SLinus Torvalds int ret = -EINVAL; 6191da177e4SLinus Torvalds int chk_addr_ret; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in)) 6221da177e4SLinus Torvalds goto out; 623e5ba31f1SPavel Emelyanov chk_addr_ret = inet_addr_type(sk->sk_net, addr->sin_addr.s_addr); 6241da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 6251da177e4SLinus Torvalds if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL && 6261da177e4SLinus Torvalds chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) 6271da177e4SLinus Torvalds goto out; 6281da177e4SLinus Torvalds inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr; 6291da177e4SLinus Torvalds if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 6301da177e4SLinus Torvalds inet->saddr = 0; /* Use device */ 6311da177e4SLinus Torvalds sk_dst_reset(sk); 6321da177e4SLinus Torvalds ret = 0; 6331da177e4SLinus Torvalds out: return ret; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds /* 6371da177e4SLinus Torvalds * This should be easy, if there is something there 6381da177e4SLinus Torvalds * we return it, otherwise we block. 6391da177e4SLinus Torvalds */ 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 6421da177e4SLinus Torvalds size_t len, int noblock, int flags, int *addr_len) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 6451da177e4SLinus Torvalds size_t copied = 0; 6461da177e4SLinus Torvalds int err = -EOPNOTSUPP; 6471da177e4SLinus Torvalds struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 6481da177e4SLinus Torvalds struct sk_buff *skb; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds if (flags & MSG_OOB) 6511da177e4SLinus Torvalds goto out; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds if (addr_len) 6541da177e4SLinus Torvalds *addr_len = sizeof(*sin); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) { 6571da177e4SLinus Torvalds err = ip_recv_error(sk, msg, len); 6581da177e4SLinus Torvalds goto out; 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags, noblock, &err); 6621da177e4SLinus Torvalds if (!skb) 6631da177e4SLinus Torvalds goto out; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds copied = skb->len; 6661da177e4SLinus Torvalds if (len < copied) { 6671da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 6681da177e4SLinus Torvalds copied = len; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 6721da177e4SLinus Torvalds if (err) 6731da177e4SLinus Torvalds goto done; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds sock_recv_timestamp(msg, sk, skb); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /* Copy the address. */ 6781da177e4SLinus Torvalds if (sin) { 6791da177e4SLinus Torvalds sin->sin_family = AF_INET; 680eddc9ec5SArnaldo Carvalho de Melo sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 681f59fc7f3STetsuo Handa sin->sin_port = 0; 6821da177e4SLinus Torvalds memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds if (inet->cmsg_flags) 6851da177e4SLinus Torvalds ip_cmsg_recv(msg, skb); 6861da177e4SLinus Torvalds if (flags & MSG_TRUNC) 6871da177e4SLinus Torvalds copied = skb->len; 6881da177e4SLinus Torvalds done: 6891da177e4SLinus Torvalds skb_free_datagram(sk, skb); 6905418c692SJesper Juhl out: 6915418c692SJesper Juhl if (err) 6925418c692SJesper Juhl return err; 6935418c692SJesper Juhl return copied; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds static int raw_init(struct sock *sk) 6971da177e4SLinus Torvalds { 6981da177e4SLinus Torvalds struct raw_sock *rp = raw_sk(sk); 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds if (inet_sk(sk)->num == IPPROTO_ICMP) 7011da177e4SLinus Torvalds memset(&rp->filter, 0, sizeof(rp->filter)); 7021da177e4SLinus Torvalds return 0; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen) 7061da177e4SLinus Torvalds { 7071da177e4SLinus Torvalds if (optlen > sizeof(struct icmp_filter)) 7081da177e4SLinus Torvalds optlen = sizeof(struct icmp_filter); 7091da177e4SLinus Torvalds if (copy_from_user(&raw_sk(sk)->filter, optval, optlen)) 7101da177e4SLinus Torvalds return -EFAULT; 7111da177e4SLinus Torvalds return 0; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds int len, ret = -EFAULT; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds if (get_user(len, optlen)) 7191da177e4SLinus Torvalds goto out; 7201da177e4SLinus Torvalds ret = -EINVAL; 7211da177e4SLinus Torvalds if (len < 0) 7221da177e4SLinus Torvalds goto out; 7231da177e4SLinus Torvalds if (len > sizeof(struct icmp_filter)) 7241da177e4SLinus Torvalds len = sizeof(struct icmp_filter); 7251da177e4SLinus Torvalds ret = -EFAULT; 7261da177e4SLinus Torvalds if (put_user(len, optlen) || 7271da177e4SLinus Torvalds copy_to_user(optval, &raw_sk(sk)->filter, len)) 7281da177e4SLinus Torvalds goto out; 7291da177e4SLinus Torvalds ret = 0; 7301da177e4SLinus Torvalds out: return ret; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7333fdadf7dSDmitry Mishin static int do_raw_setsockopt(struct sock *sk, int level, int optname, 7341da177e4SLinus Torvalds char __user *optval, int optlen) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds if (optname == ICMP_FILTER) { 7371da177e4SLinus Torvalds if (inet_sk(sk)->num != IPPROTO_ICMP) 7381da177e4SLinus Torvalds return -EOPNOTSUPP; 7391da177e4SLinus Torvalds else 7401da177e4SLinus Torvalds return raw_seticmpfilter(sk, optval, optlen); 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds return -ENOPROTOOPT; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7453fdadf7dSDmitry Mishin static int raw_setsockopt(struct sock *sk, int level, int optname, 7463fdadf7dSDmitry Mishin char __user *optval, int optlen) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds if (level != SOL_RAW) 7493fdadf7dSDmitry Mishin return ip_setsockopt(sk, level, optname, optval, optlen); 7503fdadf7dSDmitry Mishin return do_raw_setsockopt(sk, level, optname, optval, optlen); 7513fdadf7dSDmitry Mishin } 7521da177e4SLinus Torvalds 7533fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7543fdadf7dSDmitry Mishin static int compat_raw_setsockopt(struct sock *sk, int level, int optname, 7553fdadf7dSDmitry Mishin char __user *optval, int optlen) 7563fdadf7dSDmitry Mishin { 7573fdadf7dSDmitry Mishin if (level != SOL_RAW) 758543d9cfeSArnaldo Carvalho de Melo return compat_ip_setsockopt(sk, level, optname, optval, optlen); 7593fdadf7dSDmitry Mishin return do_raw_setsockopt(sk, level, optname, optval, optlen); 7603fdadf7dSDmitry Mishin } 7613fdadf7dSDmitry Mishin #endif 7623fdadf7dSDmitry Mishin 7633fdadf7dSDmitry Mishin static int do_raw_getsockopt(struct sock *sk, int level, int optname, 7643fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7653fdadf7dSDmitry Mishin { 7661da177e4SLinus Torvalds if (optname == ICMP_FILTER) { 7671da177e4SLinus Torvalds if (inet_sk(sk)->num != IPPROTO_ICMP) 7681da177e4SLinus Torvalds return -EOPNOTSUPP; 7691da177e4SLinus Torvalds else 7701da177e4SLinus Torvalds return raw_geticmpfilter(sk, optval, optlen); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds return -ENOPROTOOPT; 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7753fdadf7dSDmitry Mishin static int raw_getsockopt(struct sock *sk, int level, int optname, 7763fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7773fdadf7dSDmitry Mishin { 7783fdadf7dSDmitry Mishin if (level != SOL_RAW) 7793fdadf7dSDmitry Mishin return ip_getsockopt(sk, level, optname, optval, optlen); 7803fdadf7dSDmitry Mishin return do_raw_getsockopt(sk, level, optname, optval, optlen); 7813fdadf7dSDmitry Mishin } 7823fdadf7dSDmitry Mishin 7833fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7843fdadf7dSDmitry Mishin static int compat_raw_getsockopt(struct sock *sk, int level, int optname, 7853fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 7863fdadf7dSDmitry Mishin { 7873fdadf7dSDmitry Mishin if (level != SOL_RAW) 788543d9cfeSArnaldo Carvalho de Melo return compat_ip_getsockopt(sk, level, optname, optval, optlen); 7893fdadf7dSDmitry Mishin return do_raw_getsockopt(sk, level, optname, optval, optlen); 7903fdadf7dSDmitry Mishin } 7913fdadf7dSDmitry Mishin #endif 7923fdadf7dSDmitry Mishin 7931da177e4SLinus Torvalds static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg) 7941da177e4SLinus Torvalds { 7951da177e4SLinus Torvalds switch (cmd) { 7961da177e4SLinus Torvalds case SIOCOUTQ: { 7971da177e4SLinus Torvalds int amount = atomic_read(&sk->sk_wmem_alloc); 7981da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds case SIOCINQ: { 8011da177e4SLinus Torvalds struct sk_buff *skb; 8021da177e4SLinus Torvalds int amount = 0; 8031da177e4SLinus Torvalds 804e0f9f858SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8051da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 8061da177e4SLinus Torvalds if (skb != NULL) 8071da177e4SLinus Torvalds amount = skb->len; 808e0f9f858SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8091da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds default: 8131da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 8141da177e4SLinus Torvalds return ipmr_ioctl(sk, cmd, (void __user *)arg); 8151da177e4SLinus Torvalds #else 8161da177e4SLinus Torvalds return -ENOIOCTLCMD; 8171da177e4SLinus Torvalds #endif 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 82147a31a6fSEric Dumazet DEFINE_PROTO_INUSE(raw) 82247a31a6fSEric Dumazet 8231da177e4SLinus Torvalds struct proto raw_prot = { 8241da177e4SLinus Torvalds .name = "RAW", 8251da177e4SLinus Torvalds .owner = THIS_MODULE, 8261da177e4SLinus Torvalds .close = raw_close, 8271da177e4SLinus Torvalds .connect = ip4_datagram_connect, 8281da177e4SLinus Torvalds .disconnect = udp_disconnect, 8291da177e4SLinus Torvalds .ioctl = raw_ioctl, 8301da177e4SLinus Torvalds .init = raw_init, 8311da177e4SLinus Torvalds .setsockopt = raw_setsockopt, 8321da177e4SLinus Torvalds .getsockopt = raw_getsockopt, 8331da177e4SLinus Torvalds .sendmsg = raw_sendmsg, 8341da177e4SLinus Torvalds .recvmsg = raw_recvmsg, 8351da177e4SLinus Torvalds .bind = raw_bind, 8361da177e4SLinus Torvalds .backlog_rcv = raw_rcv_skb, 837fc8717baSPavel Emelyanov .hash = raw_hash_sk, 838fc8717baSPavel Emelyanov .unhash = raw_unhash_sk, 8391da177e4SLinus Torvalds .obj_size = sizeof(struct raw_sock), 840fc8717baSPavel Emelyanov .h.raw_hash = &raw_v4_hashinfo, 841543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 842543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_raw_setsockopt, 843543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_raw_getsockopt, 844543d9cfeSArnaldo Carvalho de Melo #endif 84547a31a6fSEric Dumazet REF_PROTO_INUSE(raw) 8461da177e4SLinus Torvalds }; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 8491da177e4SLinus Torvalds static struct sock *raw_get_first(struct seq_file *seq) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds struct sock *sk; 8521da177e4SLinus Torvalds struct raw_iter_state* state = raw_seq_private(seq); 8531da177e4SLinus Torvalds 854b673e4dfSPavel Emelyanov for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE; 855b673e4dfSPavel Emelyanov ++state->bucket) { 8561da177e4SLinus Torvalds struct hlist_node *node; 8571da177e4SLinus Torvalds 85842a73808SPavel Emelyanov sk_for_each(sk, node, &state->h->ht[state->bucket]) 859377cf82dSDenis V. Lunev if (sk->sk_net == state->p.net) 8601da177e4SLinus Torvalds goto found; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds sk = NULL; 8631da177e4SLinus Torvalds found: 8641da177e4SLinus Torvalds return sk; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk) 8681da177e4SLinus Torvalds { 8691da177e4SLinus Torvalds struct raw_iter_state* state = raw_seq_private(seq); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds do { 8721da177e4SLinus Torvalds sk = sk_next(sk); 8731da177e4SLinus Torvalds try_again: 8741da177e4SLinus Torvalds ; 875377cf82dSDenis V. Lunev } while (sk && sk->sk_net != state->p.net); 8761da177e4SLinus Torvalds 877b673e4dfSPavel Emelyanov if (!sk && ++state->bucket < RAW_HTABLE_SIZE) { 87842a73808SPavel Emelyanov sk = sk_head(&state->h->ht[state->bucket]); 8791da177e4SLinus Torvalds goto try_again; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds return sk; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos) 8851da177e4SLinus Torvalds { 8861da177e4SLinus Torvalds struct sock *sk = raw_get_first(seq); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds if (sk) 8891da177e4SLinus Torvalds while (pos && (sk = raw_get_next(seq, sk)) != NULL) 8901da177e4SLinus Torvalds --pos; 8911da177e4SLinus Torvalds return pos ? NULL : sk; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 89442a73808SPavel Emelyanov void *raw_seq_start(struct seq_file *seq, loff_t *pos) 8951da177e4SLinus Torvalds { 89642a73808SPavel Emelyanov struct raw_iter_state *state = raw_seq_private(seq); 89742a73808SPavel Emelyanov 89842a73808SPavel Emelyanov read_lock(&state->h->lock); 8991da177e4SLinus Torvalds return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 9001da177e4SLinus Torvalds } 90142a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_start); 9021da177e4SLinus Torvalds 90342a73808SPavel Emelyanov void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos) 9041da177e4SLinus Torvalds { 9051da177e4SLinus Torvalds struct sock *sk; 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 9081da177e4SLinus Torvalds sk = raw_get_first(seq); 9091da177e4SLinus Torvalds else 9101da177e4SLinus Torvalds sk = raw_get_next(seq, v); 9111da177e4SLinus Torvalds ++*pos; 9121da177e4SLinus Torvalds return sk; 9131da177e4SLinus Torvalds } 91442a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_next); 9151da177e4SLinus Torvalds 91642a73808SPavel Emelyanov void raw_seq_stop(struct seq_file *seq, void *v) 9171da177e4SLinus Torvalds { 91842a73808SPavel Emelyanov struct raw_iter_state *state = raw_seq_private(seq); 91942a73808SPavel Emelyanov 92042a73808SPavel Emelyanov read_unlock(&state->h->lock); 9211da177e4SLinus Torvalds } 92242a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_stop); 9231da177e4SLinus Torvalds 9248cd850efSDenis V. Lunev static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i) 9251da177e4SLinus Torvalds { 9261da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sp); 927714e85beSAl Viro __be32 dest = inet->daddr, 9281da177e4SLinus Torvalds src = inet->rcv_saddr; 9291da177e4SLinus Torvalds __u16 destp = 0, 9301da177e4SLinus Torvalds srcp = inet->num; 9311da177e4SLinus Torvalds 9328cd850efSDenis V. Lunev seq_printf(seq, "%4d: %08X:%04X %08X:%04X" 93333c732c3SWang Chen " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d", 9341da177e4SLinus Torvalds i, src, srcp, dest, destp, sp->sk_state, 9351da177e4SLinus Torvalds atomic_read(&sp->sk_wmem_alloc), 9361da177e4SLinus Torvalds atomic_read(&sp->sk_rmem_alloc), 9371da177e4SLinus Torvalds 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 93833c732c3SWang Chen atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops)); 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds static int raw_seq_show(struct seq_file *seq, void *v) 9421da177e4SLinus Torvalds { 9431da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 9448cd850efSDenis V. Lunev seq_printf(seq, " sl local_address rem_address st tx_queue " 9451da177e4SLinus Torvalds "rx_queue tr tm->when retrnsmt uid timeout " 9468cd850efSDenis V. Lunev "inode drops\n"); 9478cd850efSDenis V. Lunev else 9488cd850efSDenis V. Lunev raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket); 9491da177e4SLinus Torvalds return 0; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 952f690808eSStephen Hemminger static const struct seq_operations raw_seq_ops = { 9531da177e4SLinus Torvalds .start = raw_seq_start, 9541da177e4SLinus Torvalds .next = raw_seq_next, 9551da177e4SLinus Torvalds .stop = raw_seq_stop, 9561da177e4SLinus Torvalds .show = raw_seq_show, 9571da177e4SLinus Torvalds }; 9581da177e4SLinus Torvalds 9593046d767SDenis V. Lunev int raw_seq_open(struct inode *ino, struct file *file, 9603046d767SDenis V. Lunev struct raw_hashinfo *h, const struct seq_operations *ops) 9611da177e4SLinus Torvalds { 962f51d599fSPavel Emelyanov int err; 96342a73808SPavel Emelyanov struct raw_iter_state *i; 96442a73808SPavel Emelyanov 9653046d767SDenis V. Lunev err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state)); 966f51d599fSPavel Emelyanov if (err < 0) 967f51d599fSPavel Emelyanov return err; 96842a73808SPavel Emelyanov 969f51d599fSPavel Emelyanov i = raw_seq_private((struct seq_file *)file->private_data); 97042a73808SPavel Emelyanov i->h = h; 97142a73808SPavel Emelyanov return 0; 97242a73808SPavel Emelyanov } 97342a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_open); 97442a73808SPavel Emelyanov 97542a73808SPavel Emelyanov static int raw_v4_seq_open(struct inode *inode, struct file *file) 97642a73808SPavel Emelyanov { 9773046d767SDenis V. Lunev return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9809a32144eSArjan van de Ven static const struct file_operations raw_seq_fops = { 9811da177e4SLinus Torvalds .owner = THIS_MODULE, 98242a73808SPavel Emelyanov .open = raw_v4_seq_open, 9831da177e4SLinus Torvalds .read = seq_read, 9841da177e4SLinus Torvalds .llseek = seq_lseek, 985f51d599fSPavel Emelyanov .release = seq_release_net, 9861da177e4SLinus Torvalds }; 9871da177e4SLinus Torvalds 988a308da16SPavel Emelyanov static __net_init int raw_init_net(struct net *net) 989a308da16SPavel Emelyanov { 990a308da16SPavel Emelyanov if (!proc_net_fops_create(net, "raw", S_IRUGO, &raw_seq_fops)) 991a308da16SPavel Emelyanov return -ENOMEM; 992a308da16SPavel Emelyanov 993a308da16SPavel Emelyanov return 0; 994a308da16SPavel Emelyanov } 995a308da16SPavel Emelyanov 996a308da16SPavel Emelyanov static __net_exit void raw_exit_net(struct net *net) 997a308da16SPavel Emelyanov { 998a308da16SPavel Emelyanov proc_net_remove(net, "raw"); 999a308da16SPavel Emelyanov } 1000a308da16SPavel Emelyanov 1001a308da16SPavel Emelyanov static __net_initdata struct pernet_operations raw_net_ops = { 1002a308da16SPavel Emelyanov .init = raw_init_net, 1003a308da16SPavel Emelyanov .exit = raw_exit_net, 1004a308da16SPavel Emelyanov }; 1005a308da16SPavel Emelyanov 10061da177e4SLinus Torvalds int __init raw_proc_init(void) 10071da177e4SLinus Torvalds { 1008a308da16SPavel Emelyanov return register_pernet_subsys(&raw_net_ops); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds void __init raw_proc_exit(void) 10121da177e4SLinus Torvalds { 1013a308da16SPavel Emelyanov unregister_pernet_subsys(&raw_net_ops); 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 1016