12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * RAW - implementation of IP "raw" sockets. 81da177e4SLinus Torvalds * 902c30a84SJesper Juhl * Authors: Ross Biro 101da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Fixes: 131da177e4SLinus Torvalds * Alan Cox : verify_area() fixed up 141da177e4SLinus Torvalds * Alan Cox : ICMP error handling 151da177e4SLinus Torvalds * Alan Cox : EMSGSIZE if you send too big a packet 161da177e4SLinus Torvalds * Alan Cox : Now uses generic datagrams and shared 171da177e4SLinus Torvalds * skbuff library. No more peek crashes, 181da177e4SLinus Torvalds * no more backlogs 191da177e4SLinus Torvalds * Alan Cox : Checks sk->broadcast. 201da177e4SLinus Torvalds * Alan Cox : Uses skb_free_datagram/skb_copy_datagram 211da177e4SLinus Torvalds * Alan Cox : Raw passes ip options too 221da177e4SLinus Torvalds * Alan Cox : Setsocketopt added 231da177e4SLinus Torvalds * Alan Cox : Fixed error return for broadcasts 241da177e4SLinus Torvalds * Alan Cox : Removed wake_up calls 251da177e4SLinus Torvalds * Alan Cox : Use ttl/tos 261da177e4SLinus Torvalds * Alan Cox : Cleaned up old debugging 271da177e4SLinus Torvalds * Alan Cox : Use new kernel side addresses 281da177e4SLinus Torvalds * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets. 291da177e4SLinus Torvalds * Alan Cox : BSD style RAW socket demultiplexing. 301da177e4SLinus Torvalds * Alan Cox : Beginnings of mrouted support. 311da177e4SLinus Torvalds * Alan Cox : Added IP_HDRINCL option. 321da177e4SLinus Torvalds * Alan Cox : Skip broadcast check if BSDism set. 331da177e4SLinus Torvalds * David S. Miller : New socket lookup architecture. 341da177e4SLinus Torvalds */ 351da177e4SLinus Torvalds 36715b49efSAlan Cox #include <linux/types.h> 3760063497SArun Sharma #include <linux/atomic.h> 381da177e4SLinus Torvalds #include <asm/byteorder.h> 391da177e4SLinus Torvalds #include <asm/current.h> 407c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 411da177e4SLinus Torvalds #include <asm/ioctls.h> 421da177e4SLinus Torvalds #include <linux/stddef.h> 431da177e4SLinus Torvalds #include <linux/slab.h> 441da177e4SLinus Torvalds #include <linux/errno.h> 451da177e4SLinus Torvalds #include <linux/kernel.h> 46bc3b2d7fSPaul Gortmaker #include <linux/export.h> 471da177e4SLinus Torvalds #include <linux/spinlock.h> 481da177e4SLinus Torvalds #include <linux/sockios.h> 491da177e4SLinus Torvalds #include <linux/socket.h> 501da177e4SLinus Torvalds #include <linux/in.h> 511da177e4SLinus Torvalds #include <linux/mroute.h> 521da177e4SLinus Torvalds #include <linux/netdevice.h> 531da177e4SLinus Torvalds #include <linux/in_route.h> 541da177e4SLinus Torvalds #include <linux/route.h> 551da177e4SLinus Torvalds #include <linux/skbuff.h> 56f5220d63SQuentin Armitage #include <linux/igmp.h> 57457c4cbcSEric W. Biederman #include <net/net_namespace.h> 581da177e4SLinus Torvalds #include <net/dst.h> 591da177e4SLinus Torvalds #include <net/sock.h> 601da177e4SLinus Torvalds #include <linux/ip.h> 611da177e4SLinus Torvalds #include <linux/net.h> 621da177e4SLinus Torvalds #include <net/ip.h> 631da177e4SLinus Torvalds #include <net/icmp.h> 641da177e4SLinus Torvalds #include <net/udp.h> 651da177e4SLinus Torvalds #include <net/raw.h> 661da177e4SLinus Torvalds #include <net/snmp.h> 67c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 681da177e4SLinus Torvalds #include <net/inet_common.h> 691da177e4SLinus Torvalds #include <net/checksum.h> 701da177e4SLinus Torvalds #include <net/xfrm.h> 711da177e4SLinus Torvalds #include <linux/rtnetlink.h> 721da177e4SLinus Torvalds #include <linux/proc_fs.h> 731da177e4SLinus Torvalds #include <linux/seq_file.h> 741da177e4SLinus Torvalds #include <linux/netfilter.h> 751da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 76709b46e8SEric W. Biederman #include <linux/compat.h> 77c008ba5bSHerbert Xu #include <linux/uio.h> 78c008ba5bSHerbert Xu 79c008ba5bSHerbert Xu struct raw_frag_vec { 80b61e9dccSAl Viro struct msghdr *msg; 81c008ba5bSHerbert Xu union { 82c008ba5bSHerbert Xu struct icmphdr icmph; 83c008ba5bSHerbert Xu char c[1]; 84c008ba5bSHerbert Xu } hdr; 85c008ba5bSHerbert Xu int hlen; 86c008ba5bSHerbert Xu }; 871da177e4SLinus Torvalds 880daf07e5SEric Dumazet struct raw_hashinfo raw_v4_hashinfo; 89432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(raw_v4_hashinfo); 901da177e4SLinus Torvalds 91086c653fSCraig Gallek int raw_hash_sk(struct sock *sk) 921da177e4SLinus Torvalds { 93fc8717baSPavel Emelyanov struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; 940daf07e5SEric Dumazet struct hlist_nulls_head *hlist; 951da177e4SLinus Torvalds 960daf07e5SEric Dumazet hlist = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)]; 9765b4c50bSPavel Emelyanov 9865b4c50bSPavel Emelyanov write_lock_bh(&h->lock); 990daf07e5SEric Dumazet hlist_nulls_add_head_rcu(&sk->sk_nulls_node, hlist); 1000daf07e5SEric Dumazet sock_set_flag(sk, SOCK_RCU_FREE); 10165b4c50bSPavel Emelyanov write_unlock_bh(&h->lock); 102b3cb764aSEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 103086c653fSCraig Gallek 104086c653fSCraig Gallek return 0; 10565b4c50bSPavel Emelyanov } 10665b4c50bSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_hash_sk); 10765b4c50bSPavel Emelyanov 108fc8717baSPavel Emelyanov void raw_unhash_sk(struct sock *sk) 109ab70768eSPavel Emelyanov { 110fc8717baSPavel Emelyanov struct raw_hashinfo *h = sk->sk_prot->h.raw_hash; 111fc8717baSPavel Emelyanov 112ab70768eSPavel Emelyanov write_lock_bh(&h->lock); 1130daf07e5SEric Dumazet if (__sk_nulls_del_node_init_rcu(sk)) 114c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 115ab70768eSPavel Emelyanov write_unlock_bh(&h->lock); 116ab70768eSPavel Emelyanov } 117ab70768eSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_unhash_sk); 118ab70768eSPavel Emelyanov 119ba44f818SEric Dumazet bool raw_v4_match(struct net *net, struct sock *sk, unsigned short num, 120ba44f818SEric Dumazet __be32 raddr, __be32 laddr, int dif, int sdif) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 1231da177e4SLinus Torvalds 124c720c7e8SEric Dumazet if (net_eq(sock_net(sk), net) && inet->inet_num == num && 125c720c7e8SEric Dumazet !(inet->inet_daddr && inet->inet_daddr != raddr) && 126c720c7e8SEric Dumazet !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) && 1277055420fSDuncan Eastoe raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 128ba44f818SEric Dumazet return true; 129ba44f818SEric Dumazet return false; 1301da177e4SLinus Torvalds } 131ba44f818SEric Dumazet EXPORT_SYMBOL_GPL(raw_v4_match); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* 1341da177e4SLinus Torvalds * 0 - deliver 1351da177e4SLinus Torvalds * 1 - block 1361da177e4SLinus Torvalds */ 137ab43ed8bSEric Dumazet static int icmp_filter(const struct sock *sk, const struct sk_buff *skb) 1381da177e4SLinus Torvalds { 139ab43ed8bSEric Dumazet struct icmphdr _hdr; 140ab43ed8bSEric Dumazet const struct icmphdr *hdr; 1411da177e4SLinus Torvalds 142ab43ed8bSEric Dumazet hdr = skb_header_pointer(skb, skb_transport_offset(skb), 143ab43ed8bSEric Dumazet sizeof(_hdr), &_hdr); 144ab43ed8bSEric Dumazet if (!hdr) 1451da177e4SLinus Torvalds return 1; 1461da177e4SLinus Torvalds 147ab43ed8bSEric Dumazet if (hdr->type < 32) { 1481da177e4SLinus Torvalds __u32 data = raw_sk(sk)->filter.data; 1491da177e4SLinus Torvalds 150ab43ed8bSEric Dumazet return ((1U << hdr->type) & data) != 0; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* Do not block unknown ICMP types */ 1541da177e4SLinus Torvalds return 0; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* IP input processing comes here for RAW socket delivery. 1581da177e4SLinus Torvalds * Caller owns SKB, so we must make clones. 1591da177e4SLinus Torvalds * 1601da177e4SLinus Torvalds * RFC 1122: SHOULD pass TOS value up to the transport layer. 1611da177e4SLinus Torvalds * -> It does. And not only TOS, but all IP header. 1621da177e4SLinus Torvalds */ 163b71d1d42SEric Dumazet static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash) 1641da177e4SLinus Torvalds { 165ba44f818SEric Dumazet struct net *net = dev_net(skb->dev); 1660daf07e5SEric Dumazet struct hlist_nulls_head *hlist; 1670daf07e5SEric Dumazet struct hlist_nulls_node *hnode; 16867359930SDavid Ahern int sdif = inet_sdif(skb); 16919e4e768SDavid Ahern int dif = inet_iif(skb); 170d13964f4SPatrick McHardy int delivered = 0; 171ba44f818SEric Dumazet struct sock *sk; 1721da177e4SLinus Torvalds 1730daf07e5SEric Dumazet hlist = &raw_v4_hashinfo.ht[hash]; 1740daf07e5SEric Dumazet rcu_read_lock(); 1750daf07e5SEric Dumazet hlist_nulls_for_each_entry(sk, hnode, hlist, sk_nulls_node) { 176ba44f818SEric Dumazet if (!raw_v4_match(net, sk, iph->protocol, 177ba44f818SEric Dumazet iph->saddr, iph->daddr, dif, sdif)) 178ba44f818SEric Dumazet continue; 179d13964f4SPatrick McHardy delivered = 1; 180f5220d63SQuentin Armitage if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) && 181f5220d63SQuentin Armitage ip_mc_sf_allow(sk, iph->daddr, iph->saddr, 18260d9b031SDavid Ahern skb->dev->ifindex, sdif)) { 1831da177e4SLinus Torvalds struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC); 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* Not releasing hash table! */ 1861da177e4SLinus Torvalds if (clone) 1871da177e4SLinus Torvalds raw_rcv(sk, clone); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds } 1900daf07e5SEric Dumazet rcu_read_unlock(); 191d13964f4SPatrick McHardy return delivered; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1947bc54c90SPavel Emelyanov int raw_local_deliver(struct sk_buff *skb, int protocol) 1957bc54c90SPavel Emelyanov { 196ba44f818SEric Dumazet int hash = protocol & (RAW_HTABLE_SIZE - 1); 1977bc54c90SPavel Emelyanov 198ba44f818SEric Dumazet return raw_v4_input(skb, ip_hdr(skb), hash); 1997bc54c90SPavel Emelyanov } 2007bc54c90SPavel Emelyanov 2017bc54c90SPavel Emelyanov static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 20488c7664fSArnaldo Carvalho de Melo const int type = icmp_hdr(skb)->type; 20588c7664fSArnaldo Carvalho de Melo const int code = icmp_hdr(skb)->code; 2061da177e4SLinus Torvalds int err = 0; 2071da177e4SLinus Torvalds int harderr = 0; 2081da177e4SLinus Torvalds 20936393395SDavid S. Miller if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) 21036393395SDavid S. Miller ipv4_sk_update_pmtu(skb, sk, info); 2118d65b119SDuan Jiong else if (type == ICMP_REDIRECT) { 21255be7a9cSDavid S. Miller ipv4_sk_redirect(skb, sk); 2138d65b119SDuan Jiong return; 2148d65b119SDuan Jiong } 21536393395SDavid S. Miller 2161da177e4SLinus Torvalds /* Report error on raw socket, if: 2171da177e4SLinus Torvalds 1. User requested ip_recverr. 2181da177e4SLinus Torvalds 2. Socket is connected (otherwise the error indication 2191da177e4SLinus Torvalds is useless without ip_recverr and error is hard. 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED) 2221da177e4SLinus Torvalds return; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds switch (type) { 2251da177e4SLinus Torvalds default: 2261da177e4SLinus Torvalds case ICMP_TIME_EXCEEDED: 2271da177e4SLinus Torvalds err = EHOSTUNREACH; 2281da177e4SLinus Torvalds break; 2291da177e4SLinus Torvalds case ICMP_SOURCE_QUENCH: 2301da177e4SLinus Torvalds return; 2311da177e4SLinus Torvalds case ICMP_PARAMETERPROB: 2321da177e4SLinus Torvalds err = EPROTO; 2331da177e4SLinus Torvalds harderr = 1; 2341da177e4SLinus Torvalds break; 2351da177e4SLinus Torvalds case ICMP_DEST_UNREACH: 2361da177e4SLinus Torvalds err = EHOSTUNREACH; 2371da177e4SLinus Torvalds if (code > NR_ICMP_UNREACH) 2381da177e4SLinus Torvalds break; 2391da177e4SLinus Torvalds if (code == ICMP_FRAG_NEEDED) { 2401da177e4SLinus Torvalds harderr = inet->pmtudisc != IP_PMTUDISC_DONT; 2411da177e4SLinus Torvalds err = EMSGSIZE; 24275511449SMiaohe Lin } else { 24375511449SMiaohe Lin err = icmp_err_convert[code].errno; 24475511449SMiaohe Lin harderr = icmp_err_convert[code].fatal; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds if (inet->recverr) { 249b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data; 2501da177e4SLinus Torvalds u8 *payload = skb->data + (iph->ihl << 2); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds if (inet->hdrincl) 2531da177e4SLinus Torvalds payload = skb->data; 2541da177e4SLinus Torvalds ip_icmp_error(sk, skb, err, 0, info, payload); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (inet->recverr || harderr) { 2581da177e4SLinus Torvalds sk->sk_err = err; 259e3ae2365SAlexander Aring sk_error_report(sk); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2637bc54c90SPavel Emelyanov void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info) 2647bc54c90SPavel Emelyanov { 265*5da39e31SKuniyuki Iwashima struct net *net = dev_net(skb->dev); 2660daf07e5SEric Dumazet struct hlist_nulls_head *hlist; 2670daf07e5SEric Dumazet struct hlist_nulls_node *hnode; 26867359930SDavid Ahern int dif = skb->dev->ifindex; 26967359930SDavid Ahern int sdif = inet_sdif(skb); 270ba44f818SEric Dumazet const struct iphdr *iph; 271ba44f818SEric Dumazet struct sock *sk; 272ba44f818SEric Dumazet int hash; 27367359930SDavid Ahern 274ba44f818SEric Dumazet hash = protocol & (RAW_HTABLE_SIZE - 1); 2750daf07e5SEric Dumazet hlist = &raw_v4_hashinfo.ht[hash]; 276be185884SPavel Emelyanov 2770daf07e5SEric Dumazet rcu_read_lock(); 2780daf07e5SEric Dumazet hlist_nulls_for_each_entry(sk, hnode, hlist, sk_nulls_node) { 279b71d1d42SEric Dumazet iph = (const struct iphdr *)skb->data; 280ba44f818SEric Dumazet if (!raw_v4_match(net, sk, iph->protocol, 281ba44f818SEric Dumazet iph->saddr, iph->daddr, dif, sdif)) 282ba44f818SEric Dumazet continue; 283ba44f818SEric Dumazet raw_err(sk, skb, info); 2847bc54c90SPavel Emelyanov } 2850daf07e5SEric Dumazet rcu_read_unlock(); 2867bc54c90SPavel Emelyanov } 2877bc54c90SPavel Emelyanov 2881da177e4SLinus Torvalds static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds /* Charge it to the socket. */ 2911da177e4SLinus Torvalds 292fbf8866dSShawn Bohrer ipv4_pktinfo_prepare(sk, skb); 293d826eb14SEric Dumazet if (sock_queue_rcv_skb(sk, skb) < 0) { 2941da177e4SLinus Torvalds kfree_skb(skb); 2951da177e4SLinus Torvalds return NET_RX_DROP; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds return NET_RX_SUCCESS; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds int raw_rcv(struct sock *sk, struct sk_buff *skb) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { 30433c732c3SWang Chen atomic_inc(&sk->sk_drops); 3051da177e4SLinus Torvalds kfree_skb(skb); 3061da177e4SLinus Torvalds return NET_RX_DROP; 3071da177e4SLinus Torvalds } 308895b5c9fSFlorian Westphal nf_reset_ct(skb); 3091da177e4SLinus Torvalds 310d56f90a7SArnaldo Carvalho de Melo skb_push(skb, skb->data - skb_network_header(skb)); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds raw_rcv_skb(sk, skb); 3131da177e4SLinus Torvalds return 0; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 31677968b78SDavid S. Miller static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4, 3177ae9abfdSAl Viro struct msghdr *msg, size_t length, 318c14ac945SSoheil Hassas Yeganeh struct rtable **rtp, unsigned int flags, 319c14ac945SSoheil Hassas Yeganeh const struct sockcm_cookie *sockc) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3220388b004SPavel Emelyanov struct net *net = sock_net(sk); 3231da177e4SLinus Torvalds struct iphdr *iph; 3241da177e4SLinus Torvalds struct sk_buff *skb; 325f844c74fSHerbert Xu unsigned int iphlen; 3261da177e4SLinus Torvalds int err; 3271789a640SEric Dumazet struct rtable *rt = *rtp; 32866088243SHerbert Xu int hlen, tlen; 3291da177e4SLinus Torvalds 330d8d1f30bSChangli Gao if (length > rt->dst.dev->mtu) { 33177968b78SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 332d8d1f30bSChangli Gao rt->dst.dev->mtu); 3331da177e4SLinus Torvalds return -EMSGSIZE; 3341da177e4SLinus Torvalds } 33586f4c90aSAlexander Potapenko if (length < sizeof(struct iphdr)) 33686f4c90aSAlexander Potapenko return -EINVAL; 33786f4c90aSAlexander Potapenko 3381da177e4SLinus Torvalds if (flags&MSG_PROBE) 3391da177e4SLinus Torvalds goto out; 3401da177e4SLinus Torvalds 34166088243SHerbert Xu hlen = LL_RESERVED_SPACE(rt->dst.dev); 34266088243SHerbert Xu tlen = rt->dst.dev->needed_tailroom; 343f5184d26SJohannes Berg skb = sock_alloc_send_skb(sk, 34466088243SHerbert Xu length + hlen + tlen + 15, 3451da177e4SLinus Torvalds flags & MSG_DONTWAIT, &err); 34651456b29SIan Morris if (!skb) 3471da177e4SLinus Torvalds goto error; 34866088243SHerbert Xu skb_reserve(skb, hlen); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds skb->priority = sk->sk_priority; 351c6af0c22SWillem de Bruijn skb->mark = sockc->mark; 352bc969a97SJesus Sanchez-Palencia skb->tstamp = sockc->transmit_time; 353d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 3541789a640SEric Dumazet *rtp = NULL; 3551da177e4SLinus Torvalds 3567e28ecc2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 357eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 3587e28ecc2SArnaldo Carvalho de Melo skb_put(skb, length); 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3611da177e4SLinus Torvalds 3628f932f76SWillem de Bruijn skb_setup_tx_timestamp(skb, sockc->tsflags); 36311878b40SWillem de Bruijn 3640dec879fSJulian Anastasov if (flags & MSG_CONFIRM) 3650dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 3660dec879fSJulian Anastasov 367b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 36855888dfbSNeil Horman err = -EFAULT; 36921226abbSAl Viro if (memcpy_from_msg(iph, msg, length)) 37055888dfbSNeil Horman goto error_free; 3711da177e4SLinus Torvalds 372f844c74fSHerbert Xu iphlen = iph->ihl * 4; 37355888dfbSNeil Horman 37455888dfbSNeil Horman /* 37555888dfbSNeil Horman * We don't want to modify the ip header, but we do need to 37655888dfbSNeil Horman * be sure that it won't cause problems later along the network 37755888dfbSNeil Horman * stack. Specifically we want to make sure that iph->ihl is a 37855888dfbSNeil Horman * sane value. If ihl points beyond the length of the buffer passed 37955888dfbSNeil Horman * in, reject the frame as invalid 38055888dfbSNeil Horman */ 38155888dfbSNeil Horman err = -EINVAL; 38255888dfbSNeil Horman if (iphlen > length) 38355888dfbSNeil Horman goto error_free; 38455888dfbSNeil Horman 38555888dfbSNeil Horman if (iphlen >= sizeof(*iph)) { 3861da177e4SLinus Torvalds if (!iph->saddr) 38777968b78SDavid S. Miller iph->saddr = fl4->saddr; 3881da177e4SLinus Torvalds iph->check = 0; 3891da177e4SLinus Torvalds iph->tot_len = htons(length); 3901da177e4SLinus Torvalds if (!iph->id) 391b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 394027ac58eSBen Cartwright-Cox skb->transport_header += iphlen; 395027ac58eSBen Cartwright-Cox if (iph->protocol == IPPROTO_ICMP && 396027ac58eSBen Cartwright-Cox length >= iphlen + sizeof(struct icmphdr)) 3970388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 39896793b48SDavid L Stevens skb_transport_header(skb))->type); 399027ac58eSBen Cartwright-Cox } 4001da177e4SLinus Torvalds 40129a26a56SEric W. Biederman err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, 40229a26a56SEric W. Biederman net, sk, skb, NULL, rt->dst.dev, 40313206b6bSEric W. Biederman dst_output); 4041da177e4SLinus Torvalds if (err > 0) 4056ce9e7b5SEric Dumazet err = net_xmit_errno(err); 4061da177e4SLinus Torvalds if (err) 4071da177e4SLinus Torvalds goto error; 4081da177e4SLinus Torvalds out: 4091da177e4SLinus Torvalds return 0; 4101da177e4SLinus Torvalds 41155888dfbSNeil Horman error_free: 4121da177e4SLinus Torvalds kfree_skb(skb); 4131da177e4SLinus Torvalds error: 4145e38e270SPavel Emelyanov IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 4156ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet->recverr) 4166ce9e7b5SEric Dumazet err = 0; 4171da177e4SLinus Torvalds return err; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 420c008ba5bSHerbert Xu static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4) 4211da177e4SLinus Torvalds { 42232b5913aSHerbert Xu int err; 4231da177e4SLinus Torvalds 42432b5913aSHerbert Xu if (fl4->flowi4_proto != IPPROTO_ICMP) 425a27b58feSHeiko Carstens return 0; 4261da177e4SLinus Torvalds 42732b5913aSHerbert Xu /* We only need the first two bytes. */ 428c008ba5bSHerbert Xu rfv->hlen = 2; 429c008ba5bSHerbert Xu 430b61e9dccSAl Viro err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen); 43132b5913aSHerbert Xu if (err) 43232b5913aSHerbert Xu return err; 4331da177e4SLinus Torvalds 434c008ba5bSHerbert Xu fl4->fl4_icmp_type = rfv->hdr.icmph.type; 435c008ba5bSHerbert Xu fl4->fl4_icmp_code = rfv->hdr.icmph.code; 4361da177e4SLinus Torvalds 437a27b58feSHeiko Carstens return 0; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 440c008ba5bSHerbert Xu static int raw_getfrag(void *from, char *to, int offset, int len, int odd, 441c008ba5bSHerbert Xu struct sk_buff *skb) 442c008ba5bSHerbert Xu { 443c008ba5bSHerbert Xu struct raw_frag_vec *rfv = from; 444c008ba5bSHerbert Xu 445c008ba5bSHerbert Xu if (offset < rfv->hlen) { 446c008ba5bSHerbert Xu int copy = min(rfv->hlen - offset, len); 447c008ba5bSHerbert Xu 448c008ba5bSHerbert Xu if (skb->ip_summed == CHECKSUM_PARTIAL) 449c008ba5bSHerbert Xu memcpy(to, rfv->hdr.c + offset, copy); 450c008ba5bSHerbert Xu else 451c008ba5bSHerbert Xu skb->csum = csum_block_add( 452c008ba5bSHerbert Xu skb->csum, 453c008ba5bSHerbert Xu csum_partial_copy_nocheck(rfv->hdr.c + offset, 454cc44c17bSAl Viro to, copy), 455c008ba5bSHerbert Xu odd); 456c008ba5bSHerbert Xu 457c008ba5bSHerbert Xu odd = 0; 458c008ba5bSHerbert Xu offset += copy; 459c008ba5bSHerbert Xu to += copy; 460c008ba5bSHerbert Xu len -= copy; 461c008ba5bSHerbert Xu 462c008ba5bSHerbert Xu if (!len) 463c008ba5bSHerbert Xu return 0; 464c008ba5bSHerbert Xu } 465c008ba5bSHerbert Xu 466c008ba5bSHerbert Xu offset -= rfv->hlen; 467c008ba5bSHerbert Xu 468f69e6d13SAl Viro return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 469c008ba5bSHerbert Xu } 470c008ba5bSHerbert Xu 4711b784140SYing Xue static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 474bb191c3eSDavid Ahern struct net *net = sock_net(sk); 4751da177e4SLinus Torvalds struct ipcm_cookie ipc; 4761da177e4SLinus Torvalds struct rtable *rt = NULL; 47777968b78SDavid S. Miller struct flowi4 fl4; 4781da177e4SLinus Torvalds int free = 0; 4793ca3c68eSAl Viro __be32 daddr; 480c1d18f9fSAl Viro __be32 saddr; 4811da177e4SLinus Torvalds u8 tos; 4821da177e4SLinus Torvalds int err; 483f6d8bd05SEric Dumazet struct ip_options_data opt_copy; 484c008ba5bSHerbert Xu struct raw_frag_vec rfv; 4858f659a03SMohamed Ghannam int hdrincl; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds err = -EMSGSIZE; 488926d4b81SJesper Juhl if (len > 0xFFFF) 4891da177e4SLinus Torvalds goto out; 4901da177e4SLinus Torvalds 4918f659a03SMohamed Ghannam /* hdrincl should be READ_ONCE(inet->hdrincl) 49220b50d79SNicolai Stange * but READ_ONCE() doesn't work with bit fields. 49320b50d79SNicolai Stange * Doing this indirectly yields the same result. 4948f659a03SMohamed Ghannam */ 4958f659a03SMohamed Ghannam hdrincl = inet->hdrincl; 49620b50d79SNicolai Stange hdrincl = READ_ONCE(hdrincl); 4971da177e4SLinus Torvalds /* 4981da177e4SLinus Torvalds * Check the flags. 4991da177e4SLinus Torvalds */ 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds err = -EOPNOTSUPP; 5021da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */ 5031da177e4SLinus Torvalds goto out; /* compatibility */ 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * Get and verify the address. 5071da177e4SLinus Torvalds */ 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds if (msg->msg_namelen) { 510342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name); 5111da177e4SLinus Torvalds err = -EINVAL; 5121da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(*usin)) 5131da177e4SLinus Torvalds goto out; 5141da177e4SLinus Torvalds if (usin->sin_family != AF_INET) { 515058bd4d2SJoe Perches pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n", 516058bd4d2SJoe Perches __func__, current->comm); 5171da177e4SLinus Torvalds err = -EAFNOSUPPORT; 5181da177e4SLinus Torvalds if (usin->sin_family) 5191da177e4SLinus Torvalds goto out; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds daddr = usin->sin_addr.s_addr; 5221da177e4SLinus Torvalds /* ANK: I did not forget to get protocol from port field. 5231da177e4SLinus Torvalds * I just do not know, who uses this weirdness. 5241da177e4SLinus Torvalds * IP_HDRINCL is much more convenient. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds } else { 5271da177e4SLinus Torvalds err = -EDESTADDRREQ; 5281da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 5291da177e4SLinus Torvalds goto out; 530c720c7e8SEric Dumazet daddr = inet->inet_daddr; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 53335178206SWillem de Bruijn ipcm_init_sk(&ipc, inet); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (msg->msg_controllen) { 53624025c46SSoheil Hassas Yeganeh err = ip_cmsg_send(sk, msg, &ipc, false); 53791948309SEric Dumazet if (unlikely(err)) { 53891948309SEric Dumazet kfree(ipc.opt); 5391da177e4SLinus Torvalds goto out; 54091948309SEric Dumazet } 5411da177e4SLinus Torvalds if (ipc.opt) 5421da177e4SLinus Torvalds free = 1; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds saddr = ipc.addr; 5461da177e4SLinus Torvalds ipc.addr = daddr; 5471da177e4SLinus Torvalds 548f6d8bd05SEric Dumazet if (!ipc.opt) { 549f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 550f6d8bd05SEric Dumazet 551f6d8bd05SEric Dumazet rcu_read_lock(); 552f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 553f6d8bd05SEric Dumazet if (inet_opt) { 554f6d8bd05SEric Dumazet memcpy(&opt_copy, inet_opt, 555f6d8bd05SEric Dumazet sizeof(*inet_opt) + inet_opt->opt.optlen); 556f6d8bd05SEric Dumazet ipc.opt = &opt_copy.opt; 557f6d8bd05SEric Dumazet } 558f6d8bd05SEric Dumazet rcu_read_unlock(); 559f6d8bd05SEric Dumazet } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if (ipc.opt) { 5621da177e4SLinus Torvalds err = -EINVAL; 5631da177e4SLinus Torvalds /* Linux does not mangle headers on raw sockets, 5641da177e4SLinus Torvalds * so that IP options + IP_HDRINCL is non-sense. 5651da177e4SLinus Torvalds */ 5668f659a03SMohamed Ghannam if (hdrincl) 5671da177e4SLinus Torvalds goto done; 568f6d8bd05SEric Dumazet if (ipc.opt->opt.srr) { 5691da177e4SLinus Torvalds if (!daddr) 5701da177e4SLinus Torvalds goto done; 571f6d8bd05SEric Dumazet daddr = ipc.opt->opt.faddr; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds } 574aa661581SFrancesco Fusco tos = get_rtconn_flags(&ipc, sk); 5751da177e4SLinus Torvalds if (msg->msg_flags & MSG_DONTROUTE) 5761da177e4SLinus Torvalds tos |= RTO_ONLINK; 5771da177e4SLinus Torvalds 578f97c1e0cSJoe Perches if (ipv4_is_multicast(daddr)) { 579854da991SRobert Shearman if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) 5801da177e4SLinus Torvalds ipc.oif = inet->mc_index; 5811da177e4SLinus Torvalds if (!saddr) 5821da177e4SLinus Torvalds saddr = inet->mc_addr; 5839515a2e0SDavid Ahern } else if (!ipc.oif) { 58476e21053SErich E. Hoover ipc.oif = inet->uc_index; 5859515a2e0SDavid Ahern } else if (ipv4_is_lbcast(daddr) && inet->uc_index) { 586645f0897SMiaohe Lin /* oif is set, packet is to local broadcast 5879515a2e0SDavid Ahern * and uc_index is set. oif is most likely set 5889515a2e0SDavid Ahern * by sk_bound_dev_if. If uc_index != oif check if the 5899515a2e0SDavid Ahern * oif is an L3 master and uc_index is an L3 slave. 5909515a2e0SDavid Ahern * If so, we want to allow the send using the uc_index. 5919515a2e0SDavid Ahern */ 5929515a2e0SDavid Ahern if (ipc.oif != inet->uc_index && 5939515a2e0SDavid Ahern ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), 5949515a2e0SDavid Ahern inet->uc_index)) { 5959515a2e0SDavid Ahern ipc.oif = inet->uc_index; 5969515a2e0SDavid Ahern } 5979515a2e0SDavid Ahern } 5981da177e4SLinus Torvalds 599c6af0c22SWillem de Bruijn flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, 600ef164ae3SDavid S. Miller RT_SCOPE_UNIVERSE, 6018f659a03SMohamed Ghannam hdrincl ? IPPROTO_RAW : sk->sk_protocol, 6020e0d44abSSteffen Klassert inet_sk_flowi_flags(sk) | 6038f659a03SMohamed Ghannam (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0), 604e2d118a1SLorenzo Colitti daddr, saddr, 0, 0, sk->sk_uid); 605ef164ae3SDavid S. Miller 6068f659a03SMohamed Ghannam if (!hdrincl) { 607b61e9dccSAl Viro rfv.msg = msg; 608c008ba5bSHerbert Xu rfv.hlen = 0; 609c008ba5bSHerbert Xu 610c008ba5bSHerbert Xu err = raw_probe_proto_opt(&rfv, &fl4); 611a27b58feSHeiko Carstens if (err) 612a27b58feSHeiko Carstens goto done; 613a27b58feSHeiko Carstens } 6141da177e4SLinus Torvalds 6153df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4)); 616bb191c3eSDavid Ahern rt = ip_route_output_flow(net, &fl4, sk); 617b23dd4feSDavid S. Miller if (IS_ERR(rt)) { 618b23dd4feSDavid S. Miller err = PTR_ERR(rt); 6194910ac6cSDavid S. Miller rt = NULL; 6201da177e4SLinus Torvalds goto done; 621b23dd4feSDavid S. Miller } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds err = -EACCES; 6241da177e4SLinus Torvalds if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST)) 6251da177e4SLinus Torvalds goto done; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds if (msg->msg_flags & MSG_CONFIRM) 6281da177e4SLinus Torvalds goto do_confirm; 6291da177e4SLinus Torvalds back_from_confirm: 6301da177e4SLinus Torvalds 6318f659a03SMohamed Ghannam if (hdrincl) 6327ae9abfdSAl Viro err = raw_send_hdrinc(sk, &fl4, msg, len, 633c14ac945SSoheil Hassas Yeganeh &rt, msg->msg_flags, &ipc.sockc); 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds else { 6361da177e4SLinus Torvalds if (!ipc.addr) 63777968b78SDavid S. Miller ipc.addr = fl4.daddr; 6381da177e4SLinus Torvalds lock_sock(sk); 639c008ba5bSHerbert Xu err = ip_append_data(sk, &fl4, raw_getfrag, 640c008ba5bSHerbert Xu &rfv, len, 0, 6412e77d89bSEric Dumazet &ipc, &rt, msg->msg_flags); 6421da177e4SLinus Torvalds if (err) 6431da177e4SLinus Torvalds ip_flush_pending_frames(sk); 6446ce9e7b5SEric Dumazet else if (!(msg->msg_flags & MSG_MORE)) { 64577968b78SDavid S. Miller err = ip_push_pending_frames(sk, &fl4); 6466ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet->recverr) 6476ce9e7b5SEric Dumazet err = 0; 6486ce9e7b5SEric Dumazet } 6491da177e4SLinus Torvalds release_sock(sk); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds done: 6521da177e4SLinus Torvalds if (free) 6531da177e4SLinus Torvalds kfree(ipc.opt); 6541da177e4SLinus Torvalds ip_rt_put(rt); 6551da177e4SLinus Torvalds 6565418c692SJesper Juhl out: 6575418c692SJesper Juhl if (err < 0) 6585418c692SJesper Juhl return err; 6595418c692SJesper Juhl return len; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds do_confirm: 6620dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 6630dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &fl4.daddr); 6641da177e4SLinus Torvalds if (!(msg->msg_flags & MSG_PROBE) || len) 6651da177e4SLinus Torvalds goto back_from_confirm; 6661da177e4SLinus Torvalds err = 0; 6671da177e4SLinus Torvalds goto done; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds static void raw_close(struct sock *sk, long timeout) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds /* 67325985edcSLucas De Marchi * Raw sockets may have direct kernel references. Kill them. 6741da177e4SLinus Torvalds */ 6751da177e4SLinus Torvalds ip_ra_control(sk, 0, NULL); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds sk_common_release(sk); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6807d06b2e0SBrian Haley static void raw_destroy(struct sock *sk) 68122dd4850SDenis V. Lunev { 68222dd4850SDenis V. Lunev lock_sock(sk); 68322dd4850SDenis V. Lunev ip_flush_pending_frames(sk); 68422dd4850SDenis V. Lunev release_sock(sk); 68522dd4850SDenis V. Lunev } 68622dd4850SDenis V. Lunev 6871da177e4SLinus Torvalds /* This gets rid of all the nasties in af_inet. -DaveM */ 6881da177e4SLinus Torvalds static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 6911da177e4SLinus Torvalds struct sockaddr_in *addr = (struct sockaddr_in *) uaddr; 6928ff978b8SRiccardo Paolo Bestetti struct net *net = sock_net(sk); 693cd2c0f45SDavid Ahern u32 tb_id = RT_TABLE_LOCAL; 6941da177e4SLinus Torvalds int ret = -EINVAL; 6951da177e4SLinus Torvalds int chk_addr_ret; 6961da177e4SLinus Torvalds 697153a0d18SEric Dumazet lock_sock(sk); 6981da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in)) 6991da177e4SLinus Torvalds goto out; 700cd2c0f45SDavid Ahern 701cd2c0f45SDavid Ahern if (sk->sk_bound_dev_if) 7028ff978b8SRiccardo Paolo Bestetti tb_id = l3mdev_fib_table_by_index(net, 703cd2c0f45SDavid Ahern sk->sk_bound_dev_if) ? : tb_id; 704cd2c0f45SDavid Ahern 7058ff978b8SRiccardo Paolo Bestetti chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id); 706cd2c0f45SDavid Ahern 7071da177e4SLinus Torvalds ret = -EADDRNOTAVAIL; 7088ff978b8SRiccardo Paolo Bestetti if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr, 7098ff978b8SRiccardo Paolo Bestetti chk_addr_ret)) 7101da177e4SLinus Torvalds goto out; 7118ff978b8SRiccardo Paolo Bestetti 712c720c7e8SEric Dumazet inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr; 7131da177e4SLinus Torvalds if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST) 714c720c7e8SEric Dumazet inet->inet_saddr = 0; /* Use device */ 7151da177e4SLinus Torvalds sk_dst_reset(sk); 7161da177e4SLinus Torvalds ret = 0; 717153a0d18SEric Dumazet out: 718153a0d18SEric Dumazet release_sock(sk); 719153a0d18SEric Dumazet return ret; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* 7231da177e4SLinus Torvalds * This should be easy, if there is something there 7241da177e4SLinus Torvalds * we return it, otherwise we block. 7251da177e4SLinus Torvalds */ 7261da177e4SLinus Torvalds 7271b784140SYing Xue static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 728ec095263SOliver Hartkopp int flags, int *addr_len) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 7311da177e4SLinus Torvalds size_t copied = 0; 7321da177e4SLinus Torvalds int err = -EOPNOTSUPP; 733342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 7341da177e4SLinus Torvalds struct sk_buff *skb; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds if (flags & MSG_OOB) 7371da177e4SLinus Torvalds goto out; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds if (flags & MSG_ERRQUEUE) { 74085fbaa75SHannes Frederic Sowa err = ip_recv_error(sk, msg, len, addr_len); 7411da177e4SLinus Torvalds goto out; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 744f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err); 7451da177e4SLinus Torvalds if (!skb) 7461da177e4SLinus Torvalds goto out; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds copied = skb->len; 7491da177e4SLinus Torvalds if (len < copied) { 7501da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 7511da177e4SLinus Torvalds copied = len; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 75451f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied); 7551da177e4SLinus Torvalds if (err) 7561da177e4SLinus Torvalds goto done; 7571da177e4SLinus Torvalds 7586fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* Copy the address. */ 7611da177e4SLinus Torvalds if (sin) { 7621da177e4SLinus Torvalds sin->sin_family = AF_INET; 763eddc9ec5SArnaldo Carvalho de Melo sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 764f59fc7f3STetsuo Handa sin->sin_port = 0; 7651da177e4SLinus Torvalds memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 766bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin); 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds if (inet->cmsg_flags) 7691da177e4SLinus Torvalds ip_cmsg_recv(msg, skb); 7701da177e4SLinus Torvalds if (flags & MSG_TRUNC) 7711da177e4SLinus Torvalds copied = skb->len; 7721da177e4SLinus Torvalds done: 7731da177e4SLinus Torvalds skb_free_datagram(sk, skb); 7745418c692SJesper Juhl out: 7755418c692SJesper Juhl if (err) 7765418c692SJesper Juhl return err; 7775418c692SJesper Juhl return copied; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7806897445fSMike Manning static int raw_sk_init(struct sock *sk) 7811da177e4SLinus Torvalds { 7821da177e4SLinus Torvalds struct raw_sock *rp = raw_sk(sk); 7831da177e4SLinus Torvalds 784c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num == IPPROTO_ICMP) 7851da177e4SLinus Torvalds memset(&rp->filter, 0, sizeof(rp->filter)); 7861da177e4SLinus Torvalds return 0; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 789a7b75c5aSChristoph Hellwig static int raw_seticmpfilter(struct sock *sk, sockptr_t optval, int optlen) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds if (optlen > sizeof(struct icmp_filter)) 7921da177e4SLinus Torvalds optlen = sizeof(struct icmp_filter); 793a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&raw_sk(sk)->filter, optval, optlen)) 7941da177e4SLinus Torvalds return -EFAULT; 7951da177e4SLinus Torvalds return 0; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds int len, ret = -EFAULT; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (get_user(len, optlen)) 8031da177e4SLinus Torvalds goto out; 8041da177e4SLinus Torvalds ret = -EINVAL; 8051da177e4SLinus Torvalds if (len < 0) 8061da177e4SLinus Torvalds goto out; 8071da177e4SLinus Torvalds if (len > sizeof(struct icmp_filter)) 8081da177e4SLinus Torvalds len = sizeof(struct icmp_filter); 8091da177e4SLinus Torvalds ret = -EFAULT; 8101da177e4SLinus Torvalds if (put_user(len, optlen) || 8111da177e4SLinus Torvalds copy_to_user(optval, &raw_sk(sk)->filter, len)) 8121da177e4SLinus Torvalds goto out; 8131da177e4SLinus Torvalds ret = 0; 8141da177e4SLinus Torvalds out: return ret; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8173fdadf7dSDmitry Mishin static int do_raw_setsockopt(struct sock *sk, int level, int optname, 818a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 8191da177e4SLinus Torvalds { 8201da177e4SLinus Torvalds if (optname == ICMP_FILTER) { 821c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMP) 8221da177e4SLinus Torvalds return -EOPNOTSUPP; 8231da177e4SLinus Torvalds else 8241da177e4SLinus Torvalds return raw_seticmpfilter(sk, optval, optlen); 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds return -ENOPROTOOPT; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8293fdadf7dSDmitry Mishin static int raw_setsockopt(struct sock *sk, int level, int optname, 830a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen) 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds if (level != SOL_RAW) 8333fdadf7dSDmitry Mishin return ip_setsockopt(sk, level, optname, optval, optlen); 8343fdadf7dSDmitry Mishin return do_raw_setsockopt(sk, level, optname, optval, optlen); 8353fdadf7dSDmitry Mishin } 8361da177e4SLinus Torvalds 8373fdadf7dSDmitry Mishin static int do_raw_getsockopt(struct sock *sk, int level, int optname, 8383fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 8393fdadf7dSDmitry Mishin { 8401da177e4SLinus Torvalds if (optname == ICMP_FILTER) { 841c720c7e8SEric Dumazet if (inet_sk(sk)->inet_num != IPPROTO_ICMP) 8421da177e4SLinus Torvalds return -EOPNOTSUPP; 8431da177e4SLinus Torvalds else 8441da177e4SLinus Torvalds return raw_geticmpfilter(sk, optval, optlen); 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds return -ENOPROTOOPT; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8493fdadf7dSDmitry Mishin static int raw_getsockopt(struct sock *sk, int level, int optname, 8503fdadf7dSDmitry Mishin char __user *optval, int __user *optlen) 8513fdadf7dSDmitry Mishin { 8523fdadf7dSDmitry Mishin if (level != SOL_RAW) 8533fdadf7dSDmitry Mishin return ip_getsockopt(sk, level, optname, optval, optlen); 8543fdadf7dSDmitry Mishin return do_raw_getsockopt(sk, level, optname, optval, optlen); 8553fdadf7dSDmitry Mishin } 8563fdadf7dSDmitry Mishin 8571da177e4SLinus Torvalds static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds switch (cmd) { 8601da177e4SLinus Torvalds case SIOCOUTQ: { 86131e6d363SEric Dumazet int amount = sk_wmem_alloc_get(sk); 86231e6d363SEric Dumazet 8631da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds case SIOCINQ: { 8661da177e4SLinus Torvalds struct sk_buff *skb; 8671da177e4SLinus Torvalds int amount = 0; 8681da177e4SLinus Torvalds 869e0f9f858SHerbert Xu spin_lock_bh(&sk->sk_receive_queue.lock); 8701da177e4SLinus Torvalds skb = skb_peek(&sk->sk_receive_queue); 87100db4124SIan Morris if (skb) 8721da177e4SLinus Torvalds amount = skb->len; 873e0f9f858SHerbert Xu spin_unlock_bh(&sk->sk_receive_queue.lock); 8741da177e4SLinus Torvalds return put_user(amount, (int __user *)arg); 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds default: 8781da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 8791da177e4SLinus Torvalds return ipmr_ioctl(sk, cmd, (void __user *)arg); 8801da177e4SLinus Torvalds #else 8811da177e4SLinus Torvalds return -ENOIOCTLCMD; 8821da177e4SLinus Torvalds #endif 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 886709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT 887709b46e8SEric W. Biederman static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg) 888709b46e8SEric W. Biederman { 889709b46e8SEric W. Biederman switch (cmd) { 890709b46e8SEric W. Biederman case SIOCOUTQ: 891709b46e8SEric W. Biederman case SIOCINQ: 892709b46e8SEric W. Biederman return -ENOIOCTLCMD; 893709b46e8SEric W. Biederman default: 894709b46e8SEric W. Biederman #ifdef CONFIG_IP_MROUTE 895709b46e8SEric W. Biederman return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg)); 896709b46e8SEric W. Biederman #else 897709b46e8SEric W. Biederman return -ENOIOCTLCMD; 898709b46e8SEric W. Biederman #endif 899709b46e8SEric W. Biederman } 900709b46e8SEric W. Biederman } 901709b46e8SEric W. Biederman #endif 902709b46e8SEric W. Biederman 903432490f9SCyrill Gorcunov int raw_abort(struct sock *sk, int err) 904432490f9SCyrill Gorcunov { 905432490f9SCyrill Gorcunov lock_sock(sk); 906432490f9SCyrill Gorcunov 907432490f9SCyrill Gorcunov sk->sk_err = err; 908e3ae2365SAlexander Aring sk_error_report(sk); 9093de864f8SCyrill Gorcunov __udp_disconnect(sk, 0); 910432490f9SCyrill Gorcunov 911432490f9SCyrill Gorcunov release_sock(sk); 912432490f9SCyrill Gorcunov 913432490f9SCyrill Gorcunov return 0; 914432490f9SCyrill Gorcunov } 915432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(raw_abort); 916432490f9SCyrill Gorcunov 9171da177e4SLinus Torvalds struct proto raw_prot = { 9181da177e4SLinus Torvalds .name = "RAW", 9191da177e4SLinus Torvalds .owner = THIS_MODULE, 9201da177e4SLinus Torvalds .close = raw_close, 92122dd4850SDenis V. Lunev .destroy = raw_destroy, 9221da177e4SLinus Torvalds .connect = ip4_datagram_connect, 923286c72deSEric Dumazet .disconnect = __udp_disconnect, 9241da177e4SLinus Torvalds .ioctl = raw_ioctl, 9256897445fSMike Manning .init = raw_sk_init, 9261da177e4SLinus Torvalds .setsockopt = raw_setsockopt, 9271da177e4SLinus Torvalds .getsockopt = raw_getsockopt, 9281da177e4SLinus Torvalds .sendmsg = raw_sendmsg, 9291da177e4SLinus Torvalds .recvmsg = raw_recvmsg, 9301da177e4SLinus Torvalds .bind = raw_bind, 9311da177e4SLinus Torvalds .backlog_rcv = raw_rcv_skb, 9328141ed9fSSteffen Klassert .release_cb = ip4_datagram_release_cb, 933fc8717baSPavel Emelyanov .hash = raw_hash_sk, 934fc8717baSPavel Emelyanov .unhash = raw_unhash_sk, 9351da177e4SLinus Torvalds .obj_size = sizeof(struct raw_sock), 9368c2bc895SDavid Windsor .useroffset = offsetof(struct raw_sock, filter), 9378c2bc895SDavid Windsor .usersize = sizeof_field(struct raw_sock, filter), 938fc8717baSPavel Emelyanov .h.raw_hash = &raw_v4_hashinfo, 939543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 940709b46e8SEric W. Biederman .compat_ioctl = compat_raw_ioctl, 941543d9cfeSArnaldo Carvalho de Melo #endif 942432490f9SCyrill Gorcunov .diag_destroy = raw_abort, 9431da177e4SLinus Torvalds }; 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9460daf07e5SEric Dumazet static struct sock *raw_get_first(struct seq_file *seq, int bucket) 9471da177e4SLinus Torvalds { 948359745d7SMuchun Song struct raw_hashinfo *h = pde_data(file_inode(seq->file)); 9491da177e4SLinus Torvalds struct raw_iter_state *state = raw_seq_private(seq); 9500daf07e5SEric Dumazet struct hlist_nulls_head *hlist; 9510daf07e5SEric Dumazet struct hlist_nulls_node *hnode; 9520daf07e5SEric Dumazet struct sock *sk; 9531da177e4SLinus Torvalds 9540daf07e5SEric Dumazet for (state->bucket = bucket; state->bucket < RAW_HTABLE_SIZE; 955b673e4dfSPavel Emelyanov ++state->bucket) { 9560daf07e5SEric Dumazet hlist = &h->ht[state->bucket]; 9570daf07e5SEric Dumazet hlist_nulls_for_each_entry(sk, hnode, hlist, sk_nulls_node) { 9581218854aSYOSHIFUJI Hideaki if (sock_net(sk) == seq_file_net(seq)) 9591da177e4SLinus Torvalds return sk; 9601da177e4SLinus Torvalds } 9610daf07e5SEric Dumazet } 9620daf07e5SEric Dumazet return NULL; 9630daf07e5SEric Dumazet } 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk) 9661da177e4SLinus Torvalds { 9671da177e4SLinus Torvalds struct raw_iter_state *state = raw_seq_private(seq); 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds do { 9700daf07e5SEric Dumazet sk = sk_nulls_next(sk); 9711218854aSYOSHIFUJI Hideaki } while (sk && sock_net(sk) != seq_file_net(seq)); 9721da177e4SLinus Torvalds 9730daf07e5SEric Dumazet if (!sk) 9740daf07e5SEric Dumazet return raw_get_first(seq, state->bucket + 1); 9751da177e4SLinus Torvalds return sk; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos) 9791da177e4SLinus Torvalds { 9800daf07e5SEric Dumazet struct sock *sk = raw_get_first(seq, 0); 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds if (sk) 9831da177e4SLinus Torvalds while (pos && (sk = raw_get_next(seq, sk)) != NULL) 9841da177e4SLinus Torvalds --pos; 9851da177e4SLinus Torvalds return pos ? NULL : sk; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 98842a73808SPavel Emelyanov void *raw_seq_start(struct seq_file *seq, loff_t *pos) 9890daf07e5SEric Dumazet __acquires(RCU) 9901da177e4SLinus Torvalds { 9910daf07e5SEric Dumazet rcu_read_lock(); 9921da177e4SLinus Torvalds return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; 9931da177e4SLinus Torvalds } 99442a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_start); 9951da177e4SLinus Torvalds 99642a73808SPavel Emelyanov void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos) 9971da177e4SLinus Torvalds { 9981da177e4SLinus Torvalds struct sock *sk; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 10010daf07e5SEric Dumazet sk = raw_get_first(seq, 0); 10021da177e4SLinus Torvalds else 10031da177e4SLinus Torvalds sk = raw_get_next(seq, v); 10041da177e4SLinus Torvalds ++*pos; 10051da177e4SLinus Torvalds return sk; 10061da177e4SLinus Torvalds } 100742a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_next); 10081da177e4SLinus Torvalds 100942a73808SPavel Emelyanov void raw_seq_stop(struct seq_file *seq, void *v) 10100daf07e5SEric Dumazet __releases(RCU) 10111da177e4SLinus Torvalds { 10120daf07e5SEric Dumazet rcu_read_unlock(); 10131da177e4SLinus Torvalds } 101442a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_stop); 10151da177e4SLinus Torvalds 10168cd850efSDenis V. Lunev static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sp); 1019c720c7e8SEric Dumazet __be32 dest = inet->inet_daddr, 1020c720c7e8SEric Dumazet src = inet->inet_rcv_saddr; 10211da177e4SLinus Torvalds __u16 destp = 0, 1022c720c7e8SEric Dumazet srcp = inet->inet_num; 10231da177e4SLinus Torvalds 10248cd850efSDenis V. Lunev seq_printf(seq, "%4d: %08X:%04X %08X:%04X" 1025ea9a0379SPatrick Talbert " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n", 10261da177e4SLinus Torvalds i, src, srcp, dest, destp, sp->sk_state, 102731e6d363SEric Dumazet sk_wmem_alloc_get(sp), 102831e6d363SEric Dumazet sk_rmem_alloc_get(sp), 1029a7cb5a49SEric W. Biederman 0, 0L, 0, 1030a7cb5a49SEric W. Biederman from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)), 1031a7cb5a49SEric W. Biederman 0, sock_i_ino(sp), 103241c6d650SReshetova, Elena refcount_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops)); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds static int raw_seq_show(struct seq_file *seq, void *v) 10361da177e4SLinus Torvalds { 10371da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 10388cd850efSDenis V. Lunev seq_printf(seq, " sl local_address rem_address st tx_queue " 10391da177e4SLinus Torvalds "rx_queue tr tm->when retrnsmt uid timeout " 1040cb61cb9bSEric Dumazet "inode ref pointer drops\n"); 10418cd850efSDenis V. Lunev else 10428cd850efSDenis V. Lunev raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket); 10431da177e4SLinus Torvalds return 0; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 1046f690808eSStephen Hemminger static const struct seq_operations raw_seq_ops = { 10471da177e4SLinus Torvalds .start = raw_seq_start, 10481da177e4SLinus Torvalds .next = raw_seq_next, 10491da177e4SLinus Torvalds .stop = raw_seq_stop, 10501da177e4SLinus Torvalds .show = raw_seq_show, 10511da177e4SLinus Torvalds }; 10521da177e4SLinus Torvalds 1053a308da16SPavel Emelyanov static __net_init int raw_init_net(struct net *net) 1054a308da16SPavel Emelyanov { 1055c3506372SChristoph Hellwig if (!proc_create_net_data("raw", 0444, net->proc_net, &raw_seq_ops, 1056c3506372SChristoph Hellwig sizeof(struct raw_iter_state), &raw_v4_hashinfo)) 1057a308da16SPavel Emelyanov return -ENOMEM; 1058a308da16SPavel Emelyanov 1059a308da16SPavel Emelyanov return 0; 1060a308da16SPavel Emelyanov } 1061a308da16SPavel Emelyanov 1062a308da16SPavel Emelyanov static __net_exit void raw_exit_net(struct net *net) 1063a308da16SPavel Emelyanov { 1064ece31ffdSGao feng remove_proc_entry("raw", net->proc_net); 1065a308da16SPavel Emelyanov } 1066a308da16SPavel Emelyanov 1067a308da16SPavel Emelyanov static __net_initdata struct pernet_operations raw_net_ops = { 1068a308da16SPavel Emelyanov .init = raw_init_net, 1069a308da16SPavel Emelyanov .exit = raw_exit_net, 1070a308da16SPavel Emelyanov }; 1071a308da16SPavel Emelyanov 10721da177e4SLinus Torvalds int __init raw_proc_init(void) 10731da177e4SLinus Torvalds { 10740daf07e5SEric Dumazet 1075a308da16SPavel Emelyanov return register_pernet_subsys(&raw_net_ops); 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds void __init raw_proc_exit(void) 10791da177e4SLinus Torvalds { 1080a308da16SPavel Emelyanov unregister_pernet_subsys(&raw_net_ops); 10811da177e4SLinus Torvalds } 108286d1d8b7SDavid Ahern #endif /* CONFIG_PROC_FS */ 10836897445fSMike Manning 10846897445fSMike Manning static void raw_sysctl_init_net(struct net *net) 10856897445fSMike Manning { 10866897445fSMike Manning #ifdef CONFIG_NET_L3_MASTER_DEV 10876897445fSMike Manning net->ipv4.sysctl_raw_l3mdev_accept = 1; 10886897445fSMike Manning #endif 10896897445fSMike Manning } 10906897445fSMike Manning 10916897445fSMike Manning static int __net_init raw_sysctl_init(struct net *net) 10926897445fSMike Manning { 10936897445fSMike Manning raw_sysctl_init_net(net); 10946897445fSMike Manning return 0; 10956897445fSMike Manning } 10966897445fSMike Manning 10976897445fSMike Manning static struct pernet_operations __net_initdata raw_sysctl_ops = { 10986897445fSMike Manning .init = raw_sysctl_init, 10996897445fSMike Manning }; 11006897445fSMike Manning 11016897445fSMike Manning void __init raw_init(void) 11026897445fSMike Manning { 11036897445fSMike Manning raw_sysctl_init_net(&init_net); 11046897445fSMike Manning if (register_pernet_subsys(&raw_sysctl_ops)) 11056897445fSMike Manning panic("RAW: failed to init sysctl parameters.\n"); 11066897445fSMike Manning } 1107