blob: 9f97b9cbf7b37e41648cbc98fb994adeb63f43cb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * RAW - implementation of IP "raw" sockets.
8 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07009 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 *
12 * Fixes:
13 * Alan Cox : verify_area() fixed up
14 * Alan Cox : ICMP error handling
15 * Alan Cox : EMSGSIZE if you send too big a packet
16 * Alan Cox : Now uses generic datagrams and shared
17 * skbuff library. No more peek crashes,
18 * no more backlogs
19 * Alan Cox : Checks sk->broadcast.
20 * Alan Cox : Uses skb_free_datagram/skb_copy_datagram
21 * Alan Cox : Raw passes ip options too
22 * Alan Cox : Setsocketopt added
23 * Alan Cox : Fixed error return for broadcasts
24 * Alan Cox : Removed wake_up calls
25 * Alan Cox : Use ttl/tos
26 * Alan Cox : Cleaned up old debugging
27 * Alan Cox : Use new kernel side addresses
28 * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets.
29 * Alan Cox : BSD style RAW socket demultiplexing.
30 * Alan Cox : Beginnings of mrouted support.
31 * Alan Cox : Added IP_HDRINCL option.
32 * Alan Cox : Skip broadcast check if BSDism set.
33 * David S. Miller : New socket lookup architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
Dave Jonesbf0d5242006-09-22 14:00:29 -070035
Alan Cox715b49e2006-01-18 17:44:07 -080036#include <linux/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/byteorder.h>
39#include <asm/current.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/ioctls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/stddef.h>
43#include <linux/slab.h>
44#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040046#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/spinlock.h>
48#include <linux/sockios.h>
49#include <linux/socket.h>
50#include <linux/in.h>
51#include <linux/mroute.h>
52#include <linux/netdevice.h>
53#include <linux/in_route.h>
54#include <linux/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/skbuff.h>
Quentin Armitagef5220d62014-07-23 09:58:01 +010056#include <linux/igmp.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020057#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/dst.h>
59#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/ip.h>
61#include <linux/net.h>
62#include <net/ip.h>
63#include <net/icmp.h>
64#include <net/udp.h>
65#include <net/raw.h>
66#include <net/snmp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070067#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/inet_common.h>
69#include <net/checksum.h>
70#include <net/xfrm.h>
71#include <linux/rtnetlink.h>
72#include <linux/proc_fs.h>
73#include <linux/seq_file.h>
74#include <linux/netfilter.h>
75#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000076#include <linux/compat.h>
Herbert Xuc008ba5b2014-11-07 21:27:09 +080077#include <linux/uio.h>
78
79struct raw_frag_vec {
Al Virob61e9dc2014-11-24 10:52:29 -050080 struct msghdr *msg;
Herbert Xuc008ba5b2014-11-07 21:27:09 +080081 union {
82 struct icmphdr icmph;
83 char c[1];
84 } hdr;
85 int hlen;
86};
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Cyrill Gorcunov432490f2016-10-21 13:03:44 +030088struct raw_hashinfo raw_v4_hashinfo = {
Robert P. J. Day938b93a2008-03-18 00:59:23 -070089 .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -080090};
Cyrill Gorcunov432490f2016-10-21 13:03:44 +030091EXPORT_SYMBOL_GPL(raw_v4_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Craig Gallek086c6532016-02-10 11:50:35 -050093int raw_hash_sk(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Pavel Emelyanovfc8717b2008-03-22 16:56:51 -070095 struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
Pavel Emelyanov65b4c502007-11-19 22:37:24 -080096 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Eric Dumazetc720c7e82009-10-15 06:30:45 +000098 head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
Pavel Emelyanov65b4c502007-11-19 22:37:24 -080099
100 write_lock_bh(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 sk_add_node(sk, head);
Pavel Emelyanov65b4c502007-11-19 22:37:24 -0800102 write_unlock_bh(&h->lock);
Eric Dumazetb3cb7642021-11-15 09:11:50 -0800103 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Craig Gallek086c6532016-02-10 11:50:35 -0500104
105 return 0;
Pavel Emelyanov65b4c502007-11-19 22:37:24 -0800106}
107EXPORT_SYMBOL_GPL(raw_hash_sk);
108
Pavel Emelyanovfc8717b2008-03-22 16:56:51 -0700109void raw_unhash_sk(struct sock *sk)
Pavel Emelyanovab707682007-11-19 22:37:58 -0800110{
Pavel Emelyanovfc8717b2008-03-22 16:56:51 -0700111 struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
112
Pavel Emelyanovab707682007-11-19 22:37:58 -0800113 write_lock_bh(&h->lock);
114 if (sk_del_node_init(sk))
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700115 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Pavel Emelyanovab707682007-11-19 22:37:58 -0800116 write_unlock_bh(&h->lock);
117}
118EXPORT_SYMBOL_GPL(raw_unhash_sk);
119
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300120struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
David Ahern67359932017-08-07 08:44:18 -0700121 unsigned short num, __be32 raddr, __be32 laddr,
122 int dif, int sdif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800124 sk_for_each_from(sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct inet_sock *inet = inet_sk(sk);
126
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000127 if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
128 !(inet->inet_daddr && inet->inet_daddr != raddr) &&
129 !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
Duncan Eastoe70554202018-11-07 15:36:06 +0000130 raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto found; /* gotcha */
132 }
133 sk = NULL;
134found:
135 return sk;
136}
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300137EXPORT_SYMBOL_GPL(__raw_v4_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/*
140 * 0 - deliver
141 * 1 - block
142 */
Eric Dumazetab43ed82012-09-22 00:08:29 +0000143static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Eric Dumazetab43ed82012-09-22 00:08:29 +0000145 struct icmphdr _hdr;
146 const struct icmphdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Eric Dumazetab43ed82012-09-22 00:08:29 +0000148 hdr = skb_header_pointer(skb, skb_transport_offset(skb),
149 sizeof(_hdr), &_hdr);
150 if (!hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return 1;
152
Eric Dumazetab43ed82012-09-22 00:08:29 +0000153 if (hdr->type < 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 __u32 data = raw_sk(sk)->filter.data;
155
Eric Dumazetab43ed82012-09-22 00:08:29 +0000156 return ((1U << hdr->type) & data) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159 /* Do not block unknown ICMP types */
160 return 0;
161}
162
163/* IP input processing comes here for RAW socket delivery.
164 * Caller owns SKB, so we must make clones.
165 *
166 * RFC 1122: SHOULD pass TOS value up to the transport layer.
167 * -> It does. And not only TOS, but all IP header.
168 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000169static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
David Ahern67359932017-08-07 08:44:18 -0700171 int sdif = inet_sdif(skb);
David Ahern19e4e762019-05-07 20:44:59 -0700172 int dif = inet_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct sock *sk;
174 struct hlist_head *head;
Patrick McHardyd13964f2005-08-09 19:45:02 -0700175 int delivered = 0;
Pavel Emelyanovbe1858842008-01-14 05:35:31 -0800176 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800178 read_lock(&raw_v4_hashinfo.lock);
179 head = &raw_v4_hashinfo.ht[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (hlist_empty(head))
181 goto out;
Pavel Emelyanovbe1858842008-01-14 05:35:31 -0800182
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900183 net = dev_net(skb->dev);
Pavel Emelyanovbe1858842008-01-14 05:35:31 -0800184 sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
David Ahern19e4e762019-05-07 20:44:59 -0700185 iph->saddr, iph->daddr, dif, sdif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 while (sk) {
Patrick McHardyd13964f2005-08-09 19:45:02 -0700188 delivered = 1;
Quentin Armitagef5220d62014-07-23 09:58:01 +0100189 if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
190 ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
David Ahern60d9b032017-08-07 08:44:19 -0700191 skb->dev->ifindex, sdif)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
193
194 /* Not releasing hash table! */
195 if (clone)
196 raw_rcv(sk, clone);
197 }
Pavel Emelyanovbe1858842008-01-14 05:35:31 -0800198 sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 iph->saddr, iph->daddr,
Stephen Suryaputra38c73522019-06-24 20:14:06 -0400200 dif, sdif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202out:
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800203 read_unlock(&raw_v4_hashinfo.lock);
Patrick McHardyd13964f2005-08-09 19:45:02 -0700204 return delivered;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800207int raw_local_deliver(struct sk_buff *skb, int protocol)
208{
209 int hash;
210 struct sock *raw_sk;
211
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800212 hash = protocol & (RAW_HTABLE_SIZE - 1);
213 raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800214
215 /* If there maybe a raw socket we must check - if not we
216 * don't care less
217 */
218 if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
219 raw_sk = NULL;
220
221 return raw_sk != NULL;
222
223}
224
225static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300228 const int type = icmp_hdr(skb)->type;
229 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int err = 0;
231 int harderr = 0;
232
David S. Miller36393392012-06-14 22:21:46 -0700233 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
234 ipv4_sk_update_pmtu(skb, sk, info);
Duan Jiong8d65b112013-09-20 18:21:25 +0800235 else if (type == ICMP_REDIRECT) {
David S. Miller55be7a92012-07-11 21:27:49 -0700236 ipv4_sk_redirect(skb, sk);
Duan Jiong8d65b112013-09-20 18:21:25 +0800237 return;
238 }
David S. Miller36393392012-06-14 22:21:46 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Report error on raw socket, if:
241 1. User requested ip_recverr.
242 2. Socket is connected (otherwise the error indication
243 is useless without ip_recverr and error is hard.
244 */
245 if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
246 return;
247
248 switch (type) {
249 default:
250 case ICMP_TIME_EXCEEDED:
251 err = EHOSTUNREACH;
252 break;
253 case ICMP_SOURCE_QUENCH:
254 return;
255 case ICMP_PARAMETERPROB:
256 err = EPROTO;
257 harderr = 1;
258 break;
259 case ICMP_DEST_UNREACH:
260 err = EHOSTUNREACH;
261 if (code > NR_ICMP_UNREACH)
262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (code == ICMP_FRAG_NEEDED) {
264 harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
265 err = EMSGSIZE;
Miaohe Lin75511442020-08-24 07:15:04 -0400266 } else {
267 err = icmp_err_convert[code].errno;
268 harderr = icmp_err_convert[code].fatal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
271
272 if (inet->recverr) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000273 const struct iphdr *iph = (const struct iphdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 u8 *payload = skb->data + (iph->ihl << 2);
275
276 if (inet->hdrincl)
277 payload = skb->data;
278 ip_icmp_error(sk, skb, err, 0, info, payload);
279 }
280
281 if (inet->recverr || harderr) {
282 sk->sk_err = err;
Alexander Aringe3ae2362021-06-27 18:48:21 -0400283 sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800287void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
288{
289 int hash;
290 struct sock *raw_sk;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000291 const struct iphdr *iph;
Pavel Emelyanovbe1858842008-01-14 05:35:31 -0800292 struct net *net;
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800293
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800294 hash = protocol & (RAW_HTABLE_SIZE - 1);
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800295
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800296 read_lock(&raw_v4_hashinfo.lock);
297 raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
Ian Morris00db4122015-04-03 09:17:27 +0100298 if (raw_sk) {
David Ahern67359932017-08-07 08:44:18 -0700299 int dif = skb->dev->ifindex;
300 int sdif = inet_sdif(skb);
301
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000302 iph = (const struct iphdr *)skb->data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900303 net = dev_net(skb->dev);
Pavel Emelyanovbe1858842008-01-14 05:35:31 -0800304
305 while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
306 iph->daddr, iph->saddr,
David Ahern67359932017-08-07 08:44:18 -0700307 dif, sdif)) != NULL) {
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800308 raw_err(raw_sk, skb, info);
309 raw_sk = sk_next(raw_sk);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000310 iph = (const struct iphdr *)skb->data;
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800311 }
312 }
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800313 read_unlock(&raw_v4_hashinfo.lock);
Pavel Emelyanov7bc54c92007-11-19 22:35:07 -0800314}
315
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000316static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 /* Charge it to the socket. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900319
Shawn Bohrerfbf88662013-10-07 11:01:40 -0500320 ipv4_pktinfo_prepare(sk, skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +0000321 if (sock_queue_rcv_skb(sk, skb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 kfree_skb(skb);
323 return NET_RX_DROP;
324 }
325
326 return NET_RX_SUCCESS;
327}
328
329int raw_rcv(struct sock *sk, struct sk_buff *skb)
330{
331 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
Wang Chen33c732c2007-11-13 20:30:01 -0800332 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 kfree_skb(skb);
334 return NET_RX_DROP;
335 }
Florian Westphal895b5c92019-09-29 20:54:03 +0200336 nf_reset_ct(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700338 skb_push(skb, skb->data - skb_network_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 raw_rcv_skb(sk, skb);
341 return 0;
342}
343
David S. Miller77968b72011-05-08 17:12:19 -0700344static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
Al Viro7ae9abf2014-11-27 19:30:51 -0500345 struct msghdr *msg, size_t length,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400346 struct rtable **rtp, unsigned int flags,
347 const struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct inet_sock *inet = inet_sk(sk);
Pavel Emelyanov0388b002008-07-14 23:00:43 -0700350 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct iphdr *iph;
352 struct sk_buff *skb;
Herbert Xuf844c742008-01-05 23:14:44 -0800353 unsigned int iphlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int err;
Eric Dumazet1789a642010-06-03 22:23:57 +0000355 struct rtable *rt = *rtp;
Herbert Xu66088242011-11-18 02:20:04 +0000356 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Changli Gaod8d1f302010-06-10 23:31:35 -0700358 if (length > rt->dst.dev->mtu) {
David S. Miller77968b72011-05-08 17:12:19 -0700359 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
Changli Gaod8d1f302010-06-10 23:31:35 -0700360 rt->dst.dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -EMSGSIZE;
362 }
Alexander Potapenko86f4c902017-05-03 17:06:58 +0200363 if (length < sizeof(struct iphdr))
364 return -EINVAL;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (flags&MSG_PROBE)
367 goto out;
368
Herbert Xu66088242011-11-18 02:20:04 +0000369 hlen = LL_RESERVED_SPACE(rt->dst.dev);
370 tlen = rt->dst.dev->needed_tailroom;
Johannes Bergf5184d22008-05-12 20:48:31 -0700371 skb = sock_alloc_send_skb(sk,
Herbert Xu66088242011-11-18 02:20:04 +0000372 length + hlen + tlen + 15,
Johannes Bergf5184d22008-05-12 20:48:31 -0700373 flags & MSG_DONTWAIT, &err);
Ian Morris51456b22015-04-03 09:17:26 +0100374 if (!skb)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900375 goto error;
Herbert Xu66088242011-11-18 02:20:04 +0000376 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 skb->priority = sk->sk_priority;
Willem de Bruijnc6af0c22019-09-11 15:50:51 -0400379 skb->mark = sockc->mark;
Jesus Sanchez-Palenciabc969a92018-07-03 15:42:49 -0700380 skb->tstamp = sockc->transmit_time;
Changli Gaod8d1f302010-06-10 23:31:35 -0700381 skb_dst_set(skb, &rt->dst);
Eric Dumazet1789a642010-06-03 22:23:57 +0000382 *rtp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300384 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700385 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300386 skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 skb->ip_summed = CHECKSUM_NONE;
389
Willem de Bruijn8f932f72018-12-17 12:24:00 -0500390 skb_setup_tx_timestamp(skb, sockc->tsflags);
Willem de Bruijn11878b42014-07-14 17:55:06 -0400391
Julian Anastasov0dec8792017-02-06 23:14:16 +0200392 if (flags & MSG_CONFIRM)
393 skb_set_dst_pending_confirm(skb, 1);
394
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700395 skb->transport_header = skb->network_header;
Neil Horman55888df2009-10-28 08:59:47 +0000396 err = -EFAULT;
Al Viro21226ab2014-11-28 15:48:29 -0500397 if (memcpy_from_msg(iph, msg, length))
Neil Horman55888df2009-10-28 08:59:47 +0000398 goto error_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Herbert Xuf844c742008-01-05 23:14:44 -0800400 iphlen = iph->ihl * 4;
Neil Horman55888df2009-10-28 08:59:47 +0000401
402 /*
403 * We don't want to modify the ip header, but we do need to
404 * be sure that it won't cause problems later along the network
405 * stack. Specifically we want to make sure that iph->ihl is a
406 * sane value. If ihl points beyond the length of the buffer passed
407 * in, reject the frame as invalid
408 */
409 err = -EINVAL;
410 if (iphlen > length)
411 goto error_free;
412
413 if (iphlen >= sizeof(*iph)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!iph->saddr)
David S. Miller77968b72011-05-08 17:12:19 -0700415 iph->saddr = fl4->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 iph->check = 0;
417 iph->tot_len = htons(length);
418 if (!iph->id)
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100419 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
Ben Cartwright-Cox027ac582015-11-14 15:13:58 +0000422 skb->transport_header += iphlen;
423 if (iph->protocol == IPPROTO_ICMP &&
424 length >= iphlen + sizeof(struct icmphdr))
425 icmp_out_count(net, ((struct icmphdr *)
426 skb_transport_header(skb))->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500429 err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
430 net, sk, skb, NULL, rt->dst.dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500431 dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -0700433 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (err)
435 goto error;
436out:
437 return 0;
438
Neil Horman55888df2009-10-28 08:59:47 +0000439error_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 kfree_skb(skb);
441error:
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700442 IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -0700443 if (err == -ENOBUFS && !inet->recverr)
444 err = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800448static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Herbert Xu32b59132014-11-07 21:27:08 +0800450 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Herbert Xu32b59132014-11-07 21:27:08 +0800452 if (fl4->flowi4_proto != IPPROTO_ICMP)
Heiko Carstensa27b58f2006-10-30 15:06:12 -0800453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Herbert Xu32b59132014-11-07 21:27:08 +0800455 /* We only need the first two bytes. */
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800456 rfv->hlen = 2;
457
Al Virob61e9dc2014-11-24 10:52:29 -0500458 err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen);
Herbert Xu32b59132014-11-07 21:27:08 +0800459 if (err)
460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800462 fl4->fl4_icmp_type = rfv->hdr.icmph.type;
463 fl4->fl4_icmp_code = rfv->hdr.icmph.code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Heiko Carstensa27b58f2006-10-30 15:06:12 -0800465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800468static int raw_getfrag(void *from, char *to, int offset, int len, int odd,
469 struct sk_buff *skb)
470{
471 struct raw_frag_vec *rfv = from;
472
473 if (offset < rfv->hlen) {
474 int copy = min(rfv->hlen - offset, len);
475
476 if (skb->ip_summed == CHECKSUM_PARTIAL)
477 memcpy(to, rfv->hdr.c + offset, copy);
478 else
479 skb->csum = csum_block_add(
480 skb->csum,
481 csum_partial_copy_nocheck(rfv->hdr.c + offset,
Al Virocc44c172020-07-11 00:12:07 -0400482 to, copy),
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800483 odd);
484
485 odd = 0;
486 offset += copy;
487 to += copy;
488 len -= copy;
489
490 if (!len)
491 return 0;
492 }
493
494 offset -= rfv->hlen;
495
Al Virof69e6d12014-11-24 13:23:40 -0500496 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800497}
498
Ying Xue1b784142015-03-02 15:37:48 +0800499static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct inet_sock *inet = inet_sk(sk);
David Ahernbb191c32015-10-05 08:51:27 -0700502 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct ipcm_cookie ipc;
504 struct rtable *rt = NULL;
David S. Miller77968b72011-05-08 17:12:19 -0700505 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int free = 0;
Al Viro3ca3c682006-09-27 18:28:07 -0700507 __be32 daddr;
Al Viroc1d18f92006-09-27 18:28:28 -0700508 __be32 saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 u8 tos;
510 int err;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000511 struct ip_options_data opt_copy;
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800512 struct raw_frag_vec rfv;
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000513 int hdrincl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 err = -EMSGSIZE;
Jesper Juhl926d4b82005-06-18 23:00:00 -0700516 if (len > 0xFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto out;
518
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000519 /* hdrincl should be READ_ONCE(inet->hdrincl)
Nicolai Stange20b50d72018-01-08 15:54:44 +0100520 * but READ_ONCE() doesn't work with bit fields.
521 * Doing this indirectly yields the same result.
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000522 */
523 hdrincl = inet->hdrincl;
Nicolai Stange20b50d72018-01-08 15:54:44 +0100524 hdrincl = READ_ONCE(hdrincl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /*
526 * Check the flags.
527 */
528
529 err = -EOPNOTSUPP;
530 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
531 goto out; /* compatibility */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900534 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
536
537 if (msg->msg_namelen) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100538 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 err = -EINVAL;
540 if (msg->msg_namelen < sizeof(*usin))
541 goto out;
542 if (usin->sin_family != AF_INET) {
Joe Perches058bd4d2012-03-11 18:36:11 +0000543 pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
544 __func__, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 err = -EAFNOSUPPORT;
546 if (usin->sin_family)
547 goto out;
548 }
549 daddr = usin->sin_addr.s_addr;
550 /* ANK: I did not forget to get protocol from port field.
551 * I just do not know, who uses this weirdness.
552 * IP_HDRINCL is much more convenient.
553 */
554 } else {
555 err = -EDESTADDRREQ;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900556 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto out;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000558 daddr = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
Willem de Bruijn35178202018-07-06 10:12:54 -0400561 ipcm_init_sk(&ipc, inet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 if (msg->msg_controllen) {
Soheil Hassas Yeganeh24025c42016-04-02 23:08:10 -0400564 err = ip_cmsg_send(sk, msg, &ipc, false);
Eric Dumazet91948302016-02-04 06:23:28 -0800565 if (unlikely(err)) {
566 kfree(ipc.opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto out;
Eric Dumazet91948302016-02-04 06:23:28 -0800568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (ipc.opt)
570 free = 1;
571 }
572
573 saddr = ipc.addr;
574 ipc.addr = daddr;
575
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000576 if (!ipc.opt) {
577 struct ip_options_rcu *inet_opt;
578
579 rcu_read_lock();
580 inet_opt = rcu_dereference(inet->inet_opt);
581 if (inet_opt) {
582 memcpy(&opt_copy, inet_opt,
583 sizeof(*inet_opt) + inet_opt->opt.optlen);
584 ipc.opt = &opt_copy.opt;
585 }
586 rcu_read_unlock();
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (ipc.opt) {
590 err = -EINVAL;
591 /* Linux does not mangle headers on raw sockets,
592 * so that IP options + IP_HDRINCL is non-sense.
593 */
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000594 if (hdrincl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto done;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000596 if (ipc.opt->opt.srr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!daddr)
598 goto done;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000599 daddr = ipc.opt->opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 }
Francesco Fuscoaa661582013-09-24 15:43:09 +0200602 tos = get_rtconn_flags(&ipc, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (msg->msg_flags & MSG_DONTROUTE)
604 tos |= RTO_ONLINK;
605
Joe Perchesf97c1e02007-12-16 13:45:43 -0800606 if (ipv4_is_multicast(daddr)) {
Robert Shearman854da992018-10-01 09:40:23 +0100607 if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ipc.oif = inet->mc_index;
609 if (!saddr)
610 saddr = inet->mc_addr;
David Ahern9515a2e2018-01-24 19:37:38 -0800611 } else if (!ipc.oif) {
Erich E. Hoover76e21052012-02-08 09:11:07 +0000612 ipc.oif = inet->uc_index;
David Ahern9515a2e2018-01-24 19:37:38 -0800613 } else if (ipv4_is_lbcast(daddr) && inet->uc_index) {
Miaohe Lin645f0892020-08-27 07:27:49 -0400614 /* oif is set, packet is to local broadcast
David Ahern9515a2e2018-01-24 19:37:38 -0800615 * and uc_index is set. oif is most likely set
616 * by sk_bound_dev_if. If uc_index != oif check if the
617 * oif is an L3 master and uc_index is an L3 slave.
618 * If so, we want to allow the send using the uc_index.
619 */
620 if (ipc.oif != inet->uc_index &&
621 ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
622 inet->uc_index)) {
623 ipc.oif = inet->uc_index;
624 }
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Willem de Bruijnc6af0c22019-09-11 15:50:51 -0400627 flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos,
David S. Miller77968b72011-05-08 17:12:19 -0700628 RT_SCOPE_UNIVERSE,
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000629 hdrincl ? IPPROTO_RAW : sk->sk_protocol,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +0200630 inet_sk_flowi_flags(sk) |
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000631 (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900632 daddr, saddr, 0, 0, sk->sk_uid);
David S. Milleref164ae2011-03-31 04:53:51 -0700633
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000634 if (!hdrincl) {
Al Virob61e9dc2014-11-24 10:52:29 -0500635 rfv.msg = msg;
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800636 rfv.hlen = 0;
637
638 err = raw_probe_proto_opt(&rfv, &fl4);
David S. Miller77968b72011-05-08 17:12:19 -0700639 if (err)
David S. Millerb23dd4f2011-03-02 14:31:35 -0800640 goto done;
David S. Miller77968b72011-05-08 17:12:19 -0700641 }
642
Paul Moore3df98d72020-09-27 22:38:26 -0400643 security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4));
David Ahernbb191c32015-10-05 08:51:27 -0700644 rt = ip_route_output_flow(net, &fl4, sk);
David S. Miller77968b72011-05-08 17:12:19 -0700645 if (IS_ERR(rt)) {
646 err = PTR_ERR(rt);
647 rt = NULL;
648 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 err = -EACCES;
652 if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
653 goto done;
654
655 if (msg->msg_flags & MSG_CONFIRM)
656 goto do_confirm;
657back_from_confirm:
658
Mohamed Ghannam8f659a02017-12-10 03:50:58 +0000659 if (hdrincl)
Al Viro7ae9abf2014-11-27 19:30:51 -0500660 err = raw_send_hdrinc(sk, &fl4, msg, len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -0400661 &rt, msg->msg_flags, &ipc.sockc);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 else {
664 if (!ipc.addr)
David S. Miller77968b72011-05-08 17:12:19 -0700665 ipc.addr = fl4.daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 lock_sock(sk);
Herbert Xuc008ba5b2014-11-07 21:27:09 +0800667 err = ip_append_data(sk, &fl4, raw_getfrag,
668 &rfv, len, 0,
David S. Miller77968b72011-05-08 17:12:19 -0700669 &ipc, &rt, msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (err)
671 ip_flush_pending_frames(sk);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -0700672 else if (!(msg->msg_flags & MSG_MORE)) {
David S. Miller77968b72011-05-08 17:12:19 -0700673 err = ip_push_pending_frames(sk, &fl4);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -0700674 if (err == -ENOBUFS && !inet->recverr)
675 err = 0;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 release_sock(sk);
678 }
679done:
680 if (free)
681 kfree(ipc.opt);
682 ip_rt_put(rt);
683
Jesper Juhl5418c6922005-06-18 22:59:45 -0700684out:
685 if (err < 0)
686 return err;
687 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689do_confirm:
Julian Anastasov0dec8792017-02-06 23:14:16 +0200690 if (msg->msg_flags & MSG_PROBE)
691 dst_confirm_neigh(&rt->dst, &fl4.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!(msg->msg_flags & MSG_PROBE) || len)
693 goto back_from_confirm;
694 err = 0;
695 goto done;
696}
697
698static void raw_close(struct sock *sk, long timeout)
699{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900700 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300701 * Raw sockets may have direct kernel references. Kill them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
703 ip_ra_control(sk, 0, NULL);
704
705 sk_common_release(sk);
706}
707
Brian Haley7d06b2e2008-06-14 17:04:49 -0700708static void raw_destroy(struct sock *sk)
Denis V. Lunev22dd4852008-06-04 15:16:12 -0700709{
710 lock_sock(sk);
711 ip_flush_pending_frames(sk);
712 release_sock(sk);
Denis V. Lunev22dd4852008-06-04 15:16:12 -0700713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/* This gets rid of all the nasties in af_inet. -DaveM */
716static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
717{
718 struct inet_sock *inet = inet_sk(sk);
719 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
Riccardo Paolo Bestetti8ff978b2021-11-17 10:00:11 +0100720 struct net *net = sock_net(sk);
David Aherncd2c0f42016-11-03 09:25:00 -0700721 u32 tb_id = RT_TABLE_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int ret = -EINVAL;
723 int chk_addr_ret;
724
Eric Dumazet153a0d12022-01-26 16:51:16 -0800725 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
727 goto out;
David Aherncd2c0f42016-11-03 09:25:00 -0700728
729 if (sk->sk_bound_dev_if)
Riccardo Paolo Bestetti8ff978b2021-11-17 10:00:11 +0100730 tb_id = l3mdev_fib_table_by_index(net,
731 sk->sk_bound_dev_if) ? : tb_id;
David Aherncd2c0f42016-11-03 09:25:00 -0700732
Riccardo Paolo Bestetti8ff978b2021-11-17 10:00:11 +0100733 chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
David Aherncd2c0f42016-11-03 09:25:00 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ret = -EADDRNOTAVAIL;
Riccardo Paolo Bestetti8ff978b2021-11-17 10:00:11 +0100736 if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr,
737 chk_addr_ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto out;
Riccardo Paolo Bestetti8ff978b2021-11-17 10:00:11 +0100739
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000740 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000742 inet->inet_saddr = 0; /* Use device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 sk_dst_reset(sk);
744 ret = 0;
Eric Dumazet153a0d12022-01-26 16:51:16 -0800745out:
746 release_sock(sk);
747 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/*
751 * This should be easy, if there is something there
752 * we return it, otherwise we block.
753 */
754
Ying Xue1b784142015-03-02 15:37:48 +0800755static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
756 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct inet_sock *inet = inet_sk(sk);
759 size_t copied = 0;
760 int err = -EOPNOTSUPP;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100761 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct sk_buff *skb;
763
764 if (flags & MSG_OOB)
765 goto out;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (flags & MSG_ERRQUEUE) {
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100768 err = ip_recv_error(sk, msg, len, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto out;
770 }
771
772 skb = skb_recv_datagram(sk, flags, noblock, &err);
773 if (!skb)
774 goto out;
775
776 copied = skb->len;
777 if (len < copied) {
778 msg->msg_flags |= MSG_TRUNC;
779 copied = len;
780 }
781
David S. Miller51f3d022014-11-05 16:46:40 -0500782 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (err)
784 goto done;
785
Neil Horman3b885782009-10-12 13:26:31 -0700786 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Copy the address. */
789 if (sin) {
790 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700791 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Tetsuo Handaf59fc7f2006-07-25 17:05:35 -0700792 sin->sin_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowabceaa902013-11-18 04:20:45 +0100794 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 if (inet->cmsg_flags)
797 ip_cmsg_recv(msg, skb);
798 if (flags & MSG_TRUNC)
799 copied = skb->len;
800done:
801 skb_free_datagram(sk, skb);
Jesper Juhl5418c6922005-06-18 22:59:45 -0700802out:
803 if (err)
804 return err;
805 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Mike Manning68974452018-11-07 15:36:05 +0000808static int raw_sk_init(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct raw_sock *rp = raw_sk(sk);
811
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000812 if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 memset(&rp->filter, 0, sizeof(rp->filter));
814 return 0;
815}
816
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200817static int raw_seticmpfilter(struct sock *sk, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 if (optlen > sizeof(struct icmp_filter))
820 optlen = sizeof(struct icmp_filter);
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200821 if (copy_from_sockptr(&raw_sk(sk)->filter, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -EFAULT;
823 return 0;
824}
825
826static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
827{
828 int len, ret = -EFAULT;
829
830 if (get_user(len, optlen))
831 goto out;
832 ret = -EINVAL;
833 if (len < 0)
834 goto out;
835 if (len > sizeof(struct icmp_filter))
836 len = sizeof(struct icmp_filter);
837 ret = -EFAULT;
838 if (put_user(len, optlen) ||
839 copy_to_user(optval, &raw_sk(sk)->filter, len))
840 goto out;
841 ret = 0;
842out: return ret;
843}
844
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800845static int do_raw_setsockopt(struct sock *sk, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200846 sockptr_t optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (optname == ICMP_FILTER) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000849 if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return -EOPNOTSUPP;
851 else
852 return raw_seticmpfilter(sk, optval, optlen);
853 }
854 return -ENOPROTOOPT;
855}
856
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800857static int raw_setsockopt(struct sock *sk, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200858 sockptr_t optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 if (level != SOL_RAW)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800861 return ip_setsockopt(sk, level, optname, optval, optlen);
862 return do_raw_setsockopt(sk, level, optname, optval, optlen);
863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800865static int do_raw_getsockopt(struct sock *sk, int level, int optname,
866 char __user *optval, int __user *optlen)
867{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (optname == ICMP_FILTER) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000869 if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -EOPNOTSUPP;
871 else
872 return raw_geticmpfilter(sk, optval, optlen);
873 }
874 return -ENOPROTOOPT;
875}
876
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800877static int raw_getsockopt(struct sock *sk, int level, int optname,
878 char __user *optval, int __user *optlen)
879{
880 if (level != SOL_RAW)
881 return ip_getsockopt(sk, level, optname, optval, optlen);
882 return do_raw_getsockopt(sk, level, optname, optval, optlen);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
886{
887 switch (cmd) {
Joe Perches4500ebf2011-07-01 09:43:07 +0000888 case SIOCOUTQ: {
889 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -0700890
Joe Perches4500ebf2011-07-01 09:43:07 +0000891 return put_user(amount, (int __user *)arg);
892 }
893 case SIOCINQ: {
894 struct sk_buff *skb;
895 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Joe Perches4500ebf2011-07-01 09:43:07 +0000897 spin_lock_bh(&sk->sk_receive_queue.lock);
898 skb = skb_peek(&sk->sk_receive_queue);
Ian Morris00db4122015-04-03 09:17:27 +0100899 if (skb)
Joe Perches4500ebf2011-07-01 09:43:07 +0000900 amount = skb->len;
901 spin_unlock_bh(&sk->sk_receive_queue.lock);
902 return put_user(amount, (int __user *)arg);
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Joe Perches4500ebf2011-07-01 09:43:07 +0000905 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#ifdef CONFIG_IP_MROUTE
Joe Perches4500ebf2011-07-01 09:43:07 +0000907 return ipmr_ioctl(sk, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#else
Joe Perches4500ebf2011-07-01 09:43:07 +0000909 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#endif
911 }
912}
913
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000914#ifdef CONFIG_COMPAT
915static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
916{
917 switch (cmd) {
918 case SIOCOUTQ:
919 case SIOCINQ:
920 return -ENOIOCTLCMD;
921 default:
922#ifdef CONFIG_IP_MROUTE
923 return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
924#else
925 return -ENOIOCTLCMD;
926#endif
927 }
928}
929#endif
930
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300931int raw_abort(struct sock *sk, int err)
932{
933 lock_sock(sk);
934
935 sk->sk_err = err;
Alexander Aringe3ae2362021-06-27 18:48:21 -0400936 sk_error_report(sk);
Cyrill Gorcunov3de864f2016-11-01 23:05:00 +0300937 __udp_disconnect(sk, 0);
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300938
939 release_sock(sk);
940
941 return 0;
942}
943EXPORT_SYMBOL_GPL(raw_abort);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945struct proto raw_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800946 .name = "RAW",
947 .owner = THIS_MODULE,
948 .close = raw_close,
Denis V. Lunev22dd4852008-06-04 15:16:12 -0700949 .destroy = raw_destroy,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800950 .connect = ip4_datagram_connect,
Eric Dumazet286c72d2016-10-20 09:39:40 -0700951 .disconnect = __udp_disconnect,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800952 .ioctl = raw_ioctl,
Mike Manning68974452018-11-07 15:36:05 +0000953 .init = raw_sk_init,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800954 .setsockopt = raw_setsockopt,
955 .getsockopt = raw_getsockopt,
956 .sendmsg = raw_sendmsg,
957 .recvmsg = raw_recvmsg,
958 .bind = raw_bind,
959 .backlog_rcv = raw_rcv_skb,
Steffen Klassert8141ed92013-01-21 02:00:03 +0000960 .release_cb = ip4_datagram_release_cb,
Pavel Emelyanovfc8717b2008-03-22 16:56:51 -0700961 .hash = raw_hash_sk,
962 .unhash = raw_unhash_sk,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800963 .obj_size = sizeof(struct raw_sock),
David Windsor8c2bc892017-08-24 16:49:14 -0700964 .useroffset = offsetof(struct raw_sock, filter),
965 .usersize = sizeof_field(struct raw_sock, filter),
Pavel Emelyanovfc8717b2008-03-22 16:56:51 -0700966 .h.raw_hash = &raw_v4_hashinfo,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800967#ifdef CONFIG_COMPAT
Eric W. Biederman709b46e2011-01-29 16:15:56 +0000968 .compat_ioctl = compat_raw_ioctl,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800969#endif
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300970 .diag_destroy = raw_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971};
972
973#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static struct sock *raw_get_first(struct seq_file *seq)
975{
976 struct sock *sk;
Muchun Song359745d2022-01-21 22:14:23 -0800977 struct raw_hashinfo *h = pde_data(file_inode(seq->file));
Jianjun Kong5a5f3a82008-11-03 00:24:34 -0800978 struct raw_iter_state *state = raw_seq_private(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -0800980 for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
981 ++state->bucket) {
Christoph Hellwig93cb5a12018-04-10 22:08:28 +0200982 sk_for_each(sk, &h->ht[state->bucket])
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900983 if (sock_net(sk) == seq_file_net(seq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto found;
985 }
986 sk = NULL;
987found:
988 return sk;
989}
990
991static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
992{
Muchun Song359745d2022-01-21 22:14:23 -0800993 struct raw_hashinfo *h = pde_data(file_inode(seq->file));
Jianjun Kong5a5f3a82008-11-03 00:24:34 -0800994 struct raw_iter_state *state = raw_seq_private(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 do {
997 sk = sk_next(sk);
998try_again:
999 ;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001000 } while (sk && sock_net(sk) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Pavel Emelyanovb673e4d2007-11-19 22:36:45 -08001002 if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
Christoph Hellwig93cb5a12018-04-10 22:08:28 +02001003 sk = sk_head(&h->ht[state->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 goto try_again;
1005 }
1006 return sk;
1007}
1008
1009static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
1010{
1011 struct sock *sk = raw_get_first(seq);
1012
1013 if (sk)
1014 while (pos && (sk = raw_get_next(seq, sk)) != NULL)
1015 --pos;
1016 return pos ? NULL : sk;
1017}
1018
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001019void *raw_seq_start(struct seq_file *seq, loff_t *pos)
Jules Irenge0d8a42c2020-03-11 01:09:02 +00001020 __acquires(&h->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Muchun Song359745d2022-01-21 22:14:23 -08001022 struct raw_hashinfo *h = pde_data(file_inode(seq->file));
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001023
Christoph Hellwig93cb5a12018-04-10 22:08:28 +02001024 read_lock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1026}
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001027EXPORT_SYMBOL_GPL(raw_seq_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001029void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 struct sock *sk;
1032
1033 if (v == SEQ_START_TOKEN)
1034 sk = raw_get_first(seq);
1035 else
1036 sk = raw_get_next(seq, v);
1037 ++*pos;
1038 return sk;
1039}
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001040EXPORT_SYMBOL_GPL(raw_seq_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001042void raw_seq_stop(struct seq_file *seq, void *v)
Jules Irenge0d8a42c2020-03-11 01:09:02 +00001043 __releases(&h->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Muchun Song359745d2022-01-21 22:14:23 -08001045 struct raw_hashinfo *h = pde_data(file_inode(seq->file));
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001046
Christoph Hellwig93cb5a12018-04-10 22:08:28 +02001047 read_unlock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
Pavel Emelyanov42a73802007-11-19 22:38:33 -08001049EXPORT_SYMBOL_GPL(raw_seq_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Denis V. Lunev8cd850e2008-01-31 03:46:43 -08001051static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct inet_sock *inet = inet_sk(sp);
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001054 __be32 dest = inet->inet_daddr,
1055 src = inet->inet_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 __u16 destp = 0,
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001057 srcp = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Denis V. Lunev8cd850e2008-01-31 03:46:43 -08001059 seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
Patrick Talbertea9a0372019-05-17 17:11:28 +02001060 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001061 i, src, srcp, dest, destp, sp->sk_state,
Eric Dumazet31e6d362009-06-17 19:05:41 -07001062 sk_wmem_alloc_get(sp),
1063 sk_rmem_alloc_get(sp),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06001064 0, 0L, 0,
1065 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1066 0, sock_i_ino(sp),
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001067 refcount_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070static int raw_seq_show(struct seq_file *seq, void *v)
1071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (v == SEQ_START_TOKEN)
Denis V. Lunev8cd850e2008-01-31 03:46:43 -08001073 seq_printf(seq, " sl local_address rem_address st tx_queue "
1074 "rx_queue tr tm->when retrnsmt uid timeout "
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001075 "inode ref pointer drops\n");
Denis V. Lunev8cd850e2008-01-31 03:46:43 -08001076 else
1077 raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return 0;
1079}
1080
Stephen Hemmingerf6908082007-03-12 14:34:29 -07001081static const struct seq_operations raw_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 .start = raw_seq_start,
1083 .next = raw_seq_next,
1084 .stop = raw_seq_stop,
1085 .show = raw_seq_show,
1086};
1087
Pavel Emelyanova308da12008-01-14 05:36:50 -08001088static __net_init int raw_init_net(struct net *net)
1089{
Christoph Hellwigc3506372018-04-10 19:42:55 +02001090 if (!proc_create_net_data("raw", 0444, net->proc_net, &raw_seq_ops,
1091 sizeof(struct raw_iter_state), &raw_v4_hashinfo))
Pavel Emelyanova308da12008-01-14 05:36:50 -08001092 return -ENOMEM;
1093
1094 return 0;
1095}
1096
1097static __net_exit void raw_exit_net(struct net *net)
1098{
Gao fengece31ff2013-02-18 01:34:56 +00001099 remove_proc_entry("raw", net->proc_net);
Pavel Emelyanova308da12008-01-14 05:36:50 -08001100}
1101
1102static __net_initdata struct pernet_operations raw_net_ops = {
1103 .init = raw_init_net,
1104 .exit = raw_exit_net,
1105};
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107int __init raw_proc_init(void)
1108{
Pavel Emelyanova308da12008-01-14 05:36:50 -08001109 return register_pernet_subsys(&raw_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112void __init raw_proc_exit(void)
1113{
Pavel Emelyanova308da12008-01-14 05:36:50 -08001114 unregister_pernet_subsys(&raw_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
David Ahern86d1d8b2018-11-27 20:52:24 -08001116#endif /* CONFIG_PROC_FS */
Mike Manning68974452018-11-07 15:36:05 +00001117
1118static void raw_sysctl_init_net(struct net *net)
1119{
1120#ifdef CONFIG_NET_L3_MASTER_DEV
1121 net->ipv4.sysctl_raw_l3mdev_accept = 1;
1122#endif
1123}
1124
1125static int __net_init raw_sysctl_init(struct net *net)
1126{
1127 raw_sysctl_init_net(net);
1128 return 0;
1129}
1130
1131static struct pernet_operations __net_initdata raw_sysctl_ops = {
1132 .init = raw_sysctl_init,
1133};
1134
1135void __init raw_init(void)
1136{
1137 raw_sysctl_init_net(&init_net);
1138 if (register_pernet_subsys(&raw_sysctl_ops))
1139 panic("RAW: failed to init sysctl parameters.\n");
1140}