1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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  *
9  * Authors:	Ross Biro
10  *		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.
34  */
35 
36 #include <linux/types.h>
37 #include <linux/atomic.h>
38 #include <asm/byteorder.h>
39 #include <asm/current.h>
40 #include <linux/uaccess.h>
41 #include <asm/ioctls.h>
42 #include <linux/stddef.h>
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/export.h>
47 #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>
55 #include <linux/skbuff.h>
56 #include <linux/igmp.h>
57 #include <net/net_namespace.h>
58 #include <net/dst.h>
59 #include <net/sock.h>
60 #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>
67 #include <net/tcp_states.h>
68 #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>
76 #include <linux/compat.h>
77 #include <linux/uio.h>
78 
79 struct raw_frag_vec {
80 	struct msghdr *msg;
81 	union {
82 		struct icmphdr icmph;
83 		char c[1];
84 	} hdr;
85 	int hlen;
86 };
87 
88 struct raw_hashinfo raw_v4_hashinfo;
89 EXPORT_SYMBOL_GPL(raw_v4_hashinfo);
90 
raw_hash_sk(struct sock * sk)91 int raw_hash_sk(struct sock *sk)
92 {
93 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
94 	struct hlist_head *hlist;
95 
96 	hlist = &h->ht[raw_hashfunc(sock_net(sk), inet_sk(sk)->inet_num)];
97 
98 	spin_lock(&h->lock);
99 	sk_add_node_rcu(sk, hlist);
100 	sock_set_flag(sk, SOCK_RCU_FREE);
101 	spin_unlock(&h->lock);
102 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
103 
104 	return 0;
105 }
106 EXPORT_SYMBOL_GPL(raw_hash_sk);
107 
raw_unhash_sk(struct sock * sk)108 void raw_unhash_sk(struct sock *sk)
109 {
110 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
111 
112 	spin_lock(&h->lock);
113 	if (sk_del_node_init_rcu(sk))
114 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
115 	spin_unlock(&h->lock);
116 }
117 EXPORT_SYMBOL_GPL(raw_unhash_sk);
118 
raw_v4_match(struct net * net,const struct sock * sk,unsigned short num,__be32 raddr,__be32 laddr,int dif,int sdif)119 bool raw_v4_match(struct net *net, const struct sock *sk, unsigned short num,
120 		  __be32 raddr, __be32 laddr, int dif, int sdif)
121 {
122 	const struct inet_sock *inet = inet_sk(sk);
123 
124 	if (net_eq(sock_net(sk), net) && inet->inet_num == num	&&
125 	    !(inet->inet_daddr && inet->inet_daddr != raddr) 	&&
126 	    !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
127 	    raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
128 		return true;
129 	return false;
130 }
131 EXPORT_SYMBOL_GPL(raw_v4_match);
132 
133 /*
134  *	0 - deliver
135  *	1 - block
136  */
icmp_filter(const struct sock * sk,const struct sk_buff * skb)137 static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
138 {
139 	struct icmphdr _hdr;
140 	const struct icmphdr *hdr;
141 
142 	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
143 				 sizeof(_hdr), &_hdr);
144 	if (!hdr)
145 		return 1;
146 
147 	if (hdr->type < 32) {
148 		__u32 data = raw_sk(sk)->filter.data;
149 
150 		return ((1U << hdr->type) & data) != 0;
151 	}
152 
153 	/* Do not block unknown ICMP types */
154 	return 0;
155 }
156 
157 /* IP input processing comes here for RAW socket delivery.
158  * Caller owns SKB, so we must make clones.
159  *
160  * RFC 1122: SHOULD pass TOS value up to the transport layer.
161  * -> It does. And not only TOS, but all IP header.
162  */
raw_v4_input(struct net * net,struct sk_buff * skb,const struct iphdr * iph,int hash)163 static int raw_v4_input(struct net *net, struct sk_buff *skb,
164 			const struct iphdr *iph, int hash)
165 {
166 	int sdif = inet_sdif(skb);
167 	struct hlist_head *hlist;
168 	int dif = inet_iif(skb);
169 	int delivered = 0;
170 	struct sock *sk;
171 
172 	hlist = &raw_v4_hashinfo.ht[hash];
173 	rcu_read_lock();
174 	sk_for_each_rcu(sk, hlist) {
175 		if (!raw_v4_match(net, sk, iph->protocol,
176 				  iph->saddr, iph->daddr, dif, sdif))
177 			continue;
178 		delivered = 1;
179 		if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
180 		    ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
181 				   skb->dev->ifindex, sdif)) {
182 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
183 
184 			/* Not releasing hash table! */
185 			if (clone)
186 				raw_rcv(sk, clone);
187 		}
188 	}
189 	rcu_read_unlock();
190 	return delivered;
191 }
192 
raw_local_deliver(struct sk_buff * skb,int protocol)193 int raw_local_deliver(struct sk_buff *skb, int protocol)
194 {
195 	struct net *net = dev_net(skb->dev);
196 
197 	return raw_v4_input(net, skb, ip_hdr(skb),
198 			    raw_hashfunc(net, protocol));
199 }
200 
raw_err(struct sock * sk,struct sk_buff * skb,u32 info)201 static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
202 {
203 	struct inet_sock *inet = inet_sk(sk);
204 	const int type = icmp_hdr(skb)->type;
205 	const int code = icmp_hdr(skb)->code;
206 	int harderr = 0;
207 	bool recverr;
208 	int err = 0;
209 
210 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
211 		ipv4_sk_update_pmtu(skb, sk, info);
212 	else if (type == ICMP_REDIRECT) {
213 		ipv4_sk_redirect(skb, sk);
214 		return;
215 	}
216 
217 	/* Report error on raw socket, if:
218 	   1. User requested ip_recverr.
219 	   2. Socket is connected (otherwise the error indication
220 	      is useless without ip_recverr and error is hard.
221 	 */
222 	recverr = inet_test_bit(RECVERR, sk);
223 	if (!recverr && sk->sk_state != TCP_ESTABLISHED)
224 		return;
225 
226 	switch (type) {
227 	default:
228 	case ICMP_TIME_EXCEEDED:
229 		err = EHOSTUNREACH;
230 		break;
231 	case ICMP_SOURCE_QUENCH:
232 		return;
233 	case ICMP_PARAMETERPROB:
234 		err = EPROTO;
235 		harderr = 1;
236 		break;
237 	case ICMP_DEST_UNREACH:
238 		err = EHOSTUNREACH;
239 		if (code > NR_ICMP_UNREACH)
240 			break;
241 		if (code == ICMP_FRAG_NEEDED) {
242 			harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
243 			err = EMSGSIZE;
244 		} else {
245 			err = icmp_err_convert[code].errno;
246 			harderr = icmp_err_convert[code].fatal;
247 		}
248 	}
249 
250 	if (recverr) {
251 		const struct iphdr *iph = (const struct iphdr *)skb->data;
252 		u8 *payload = skb->data + (iph->ihl << 2);
253 
254 		if (inet_test_bit(HDRINCL, sk))
255 			payload = skb->data;
256 		ip_icmp_error(sk, skb, err, 0, info, payload);
257 	}
258 
259 	if (recverr || harderr) {
260 		sk->sk_err = err;
261 		sk_error_report(sk);
262 	}
263 }
264 
raw_icmp_error(struct sk_buff * skb,int protocol,u32 info)265 void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
266 {
267 	struct net *net = dev_net(skb->dev);
268 	int dif = skb->dev->ifindex;
269 	int sdif = inet_sdif(skb);
270 	struct hlist_head *hlist;
271 	const struct iphdr *iph;
272 	struct sock *sk;
273 	int hash;
274 
275 	hash = raw_hashfunc(net, protocol);
276 	hlist = &raw_v4_hashinfo.ht[hash];
277 
278 	rcu_read_lock();
279 	sk_for_each_rcu(sk, hlist) {
280 		iph = (const struct iphdr *)skb->data;
281 		if (!raw_v4_match(net, sk, iph->protocol,
282 				  iph->daddr, iph->saddr, dif, sdif))
283 			continue;
284 		raw_err(sk, skb, info);
285 	}
286 	rcu_read_unlock();
287 }
288 
raw_rcv_skb(struct sock * sk,struct sk_buff * skb)289 static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
290 {
291 	enum skb_drop_reason reason;
292 
293 	/* Charge it to the socket. */
294 
295 	ipv4_pktinfo_prepare(sk, skb, true);
296 	if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) {
297 		kfree_skb_reason(skb, reason);
298 		return NET_RX_DROP;
299 	}
300 
301 	return NET_RX_SUCCESS;
302 }
303 
raw_rcv(struct sock * sk,struct sk_buff * skb)304 int raw_rcv(struct sock *sk, struct sk_buff *skb)
305 {
306 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
307 		atomic_inc(&sk->sk_drops);
308 		kfree_skb_reason(skb, SKB_DROP_REASON_XFRM_POLICY);
309 		return NET_RX_DROP;
310 	}
311 	nf_reset_ct(skb);
312 
313 	skb_push(skb, skb->data - skb_network_header(skb));
314 
315 	raw_rcv_skb(sk, skb);
316 	return 0;
317 }
318 
raw_send_hdrinc(struct sock * sk,struct flowi4 * fl4,struct msghdr * msg,size_t length,struct rtable ** rtp,unsigned int flags,const struct sockcm_cookie * sockc)319 static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
320 			   struct msghdr *msg, size_t length,
321 			   struct rtable **rtp, unsigned int flags,
322 			   const struct sockcm_cookie *sockc)
323 {
324 	struct inet_sock *inet = inet_sk(sk);
325 	struct net *net = sock_net(sk);
326 	struct iphdr *iph;
327 	struct sk_buff *skb;
328 	unsigned int iphlen;
329 	int err;
330 	struct rtable *rt = *rtp;
331 	int hlen, tlen;
332 
333 	if (length > rt->dst.dev->mtu) {
334 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
335 			       rt->dst.dev->mtu);
336 		return -EMSGSIZE;
337 	}
338 	if (length < sizeof(struct iphdr))
339 		return -EINVAL;
340 
341 	if (flags&MSG_PROBE)
342 		goto out;
343 
344 	hlen = LL_RESERVED_SPACE(rt->dst.dev);
345 	tlen = rt->dst.dev->needed_tailroom;
346 	skb = sock_alloc_send_skb(sk,
347 				  length + hlen + tlen + 15,
348 				  flags & MSG_DONTWAIT, &err);
349 	if (!skb)
350 		goto error;
351 	skb_reserve(skb, hlen);
352 
353 	skb->priority = READ_ONCE(sk->sk_priority);
354 	skb->mark = sockc->mark;
355 	skb->tstamp = sockc->transmit_time;
356 	skb_dst_set(skb, &rt->dst);
357 	*rtp = NULL;
358 
359 	skb_reset_network_header(skb);
360 	iph = ip_hdr(skb);
361 	skb_put(skb, length);
362 
363 	skb->ip_summed = CHECKSUM_NONE;
364 
365 	skb_setup_tx_timestamp(skb, sockc->tsflags);
366 
367 	if (flags & MSG_CONFIRM)
368 		skb_set_dst_pending_confirm(skb, 1);
369 
370 	skb->transport_header = skb->network_header;
371 	err = -EFAULT;
372 	if (memcpy_from_msg(iph, msg, length))
373 		goto error_free;
374 
375 	iphlen = iph->ihl * 4;
376 
377 	/*
378 	 * We don't want to modify the ip header, but we do need to
379 	 * be sure that it won't cause problems later along the network
380 	 * stack.  Specifically we want to make sure that iph->ihl is a
381 	 * sane value.  If ihl points beyond the length of the buffer passed
382 	 * in, reject the frame as invalid
383 	 */
384 	err = -EINVAL;
385 	if (iphlen > length)
386 		goto error_free;
387 
388 	if (iphlen >= sizeof(*iph)) {
389 		if (!iph->saddr)
390 			iph->saddr = fl4->saddr;
391 		iph->check   = 0;
392 		iph->tot_len = htons(length);
393 		if (!iph->id)
394 			ip_select_ident(net, skb, NULL);
395 
396 		iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
397 		skb->transport_header += iphlen;
398 		if (iph->protocol == IPPROTO_ICMP &&
399 		    length >= iphlen + sizeof(struct icmphdr))
400 			icmp_out_count(net, ((struct icmphdr *)
401 				skb_transport_header(skb))->type);
402 	}
403 
404 	err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
405 		      net, sk, skb, NULL, rt->dst.dev,
406 		      dst_output);
407 	if (err > 0)
408 		err = net_xmit_errno(err);
409 	if (err)
410 		goto error;
411 out:
412 	return 0;
413 
414 error_free:
415 	kfree_skb(skb);
416 error:
417 	IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
418 	if (err == -ENOBUFS && !inet_test_bit(RECVERR, sk))
419 		err = 0;
420 	return err;
421 }
422 
raw_probe_proto_opt(struct raw_frag_vec * rfv,struct flowi4 * fl4)423 static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4)
424 {
425 	int err;
426 
427 	if (fl4->flowi4_proto != IPPROTO_ICMP)
428 		return 0;
429 
430 	/* We only need the first two bytes. */
431 	rfv->hlen = 2;
432 
433 	err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen);
434 	if (err)
435 		return err;
436 
437 	fl4->fl4_icmp_type = rfv->hdr.icmph.type;
438 	fl4->fl4_icmp_code = rfv->hdr.icmph.code;
439 
440 	return 0;
441 }
442 
raw_getfrag(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)443 static int raw_getfrag(void *from, char *to, int offset, int len, int odd,
444 		       struct sk_buff *skb)
445 {
446 	struct raw_frag_vec *rfv = from;
447 
448 	if (offset < rfv->hlen) {
449 		int copy = min(rfv->hlen - offset, len);
450 
451 		if (skb->ip_summed == CHECKSUM_PARTIAL)
452 			memcpy(to, rfv->hdr.c + offset, copy);
453 		else
454 			skb->csum = csum_block_add(
455 				skb->csum,
456 				csum_partial_copy_nocheck(rfv->hdr.c + offset,
457 							  to, copy),
458 				odd);
459 
460 		odd = 0;
461 		offset += copy;
462 		to += copy;
463 		len -= copy;
464 
465 		if (!len)
466 			return 0;
467 	}
468 
469 	offset -= rfv->hlen;
470 
471 	return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
472 }
473 
raw_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)474 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
475 {
476 	struct inet_sock *inet = inet_sk(sk);
477 	struct net *net = sock_net(sk);
478 	struct ipcm_cookie ipc;
479 	struct rtable *rt = NULL;
480 	struct flowi4 fl4;
481 	u8 tos, scope;
482 	int free = 0;
483 	__be32 daddr;
484 	__be32 saddr;
485 	int err;
486 	struct ip_options_data opt_copy;
487 	struct raw_frag_vec rfv;
488 	int hdrincl;
489 
490 	err = -EMSGSIZE;
491 	if (len > 0xFFFF)
492 		goto out;
493 
494 	hdrincl = inet_test_bit(HDRINCL, sk);
495 
496 	/*
497 	 *	Check the flags.
498 	 */
499 
500 	err = -EOPNOTSUPP;
501 	if (msg->msg_flags & MSG_OOB)	/* Mirror BSD error message */
502 		goto out;               /* compatibility */
503 
504 	/*
505 	 *	Get and verify the address.
506 	 */
507 
508 	if (msg->msg_namelen) {
509 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
510 		err = -EINVAL;
511 		if (msg->msg_namelen < sizeof(*usin))
512 			goto out;
513 		if (usin->sin_family != AF_INET) {
514 			pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
515 				     __func__, current->comm);
516 			err = -EAFNOSUPPORT;
517 			if (usin->sin_family)
518 				goto out;
519 		}
520 		daddr = usin->sin_addr.s_addr;
521 		/* ANK: I did not forget to get protocol from port field.
522 		 * I just do not know, who uses this weirdness.
523 		 * IP_HDRINCL is much more convenient.
524 		 */
525 	} else {
526 		err = -EDESTADDRREQ;
527 		if (sk->sk_state != TCP_ESTABLISHED)
528 			goto out;
529 		daddr = inet->inet_daddr;
530 	}
531 
532 	ipcm_init_sk(&ipc, inet);
533 	/* Keep backward compat */
534 	if (hdrincl)
535 		ipc.protocol = IPPROTO_RAW;
536 
537 	if (msg->msg_controllen) {
538 		err = ip_cmsg_send(sk, msg, &ipc, false);
539 		if (unlikely(err)) {
540 			kfree(ipc.opt);
541 			goto out;
542 		}
543 		if (ipc.opt)
544 			free = 1;
545 	}
546 
547 	saddr = ipc.addr;
548 	ipc.addr = daddr;
549 
550 	if (!ipc.opt) {
551 		struct ip_options_rcu *inet_opt;
552 
553 		rcu_read_lock();
554 		inet_opt = rcu_dereference(inet->inet_opt);
555 		if (inet_opt) {
556 			memcpy(&opt_copy, inet_opt,
557 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
558 			ipc.opt = &opt_copy.opt;
559 		}
560 		rcu_read_unlock();
561 	}
562 
563 	if (ipc.opt) {
564 		err = -EINVAL;
565 		/* Linux does not mangle headers on raw sockets,
566 		 * so that IP options + IP_HDRINCL is non-sense.
567 		 */
568 		if (hdrincl)
569 			goto done;
570 		if (ipc.opt->opt.srr) {
571 			if (!daddr)
572 				goto done;
573 			daddr = ipc.opt->opt.faddr;
574 		}
575 	}
576 	tos = get_rttos(&ipc, inet);
577 	scope = ip_sendmsg_scope(inet, &ipc, msg);
578 
579 	if (ipv4_is_multicast(daddr)) {
580 		if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
581 			ipc.oif = inet->mc_index;
582 		if (!saddr)
583 			saddr = inet->mc_addr;
584 	} else if (!ipc.oif) {
585 		ipc.oif = inet->uc_index;
586 	} else if (ipv4_is_lbcast(daddr) && inet->uc_index) {
587 		/* oif is set, packet is to local broadcast
588 		 * and uc_index is set. oif is most likely set
589 		 * by sk_bound_dev_if. If uc_index != oif check if the
590 		 * oif is an L3 master and uc_index is an L3 slave.
591 		 * If so, we want to allow the send using the uc_index.
592 		 */
593 		if (ipc.oif != inet->uc_index &&
594 		    ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
595 							      inet->uc_index)) {
596 			ipc.oif = inet->uc_index;
597 		}
598 	}
599 
600 	flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope,
601 			   hdrincl ? ipc.protocol : sk->sk_protocol,
602 			   inet_sk_flowi_flags(sk) |
603 			    (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
604 			   daddr, saddr, 0, 0, sk->sk_uid);
605 
606 	if (!hdrincl) {
607 		rfv.msg = msg;
608 		rfv.hlen = 0;
609 
610 		err = raw_probe_proto_opt(&rfv, &fl4);
611 		if (err)
612 			goto done;
613 	}
614 
615 	security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4));
616 	rt = ip_route_output_flow(net, &fl4, sk);
617 	if (IS_ERR(rt)) {
618 		err = PTR_ERR(rt);
619 		rt = NULL;
620 		goto done;
621 	}
622 
623 	err = -EACCES;
624 	if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
625 		goto done;
626 
627 	if (msg->msg_flags & MSG_CONFIRM)
628 		goto do_confirm;
629 back_from_confirm:
630 
631 	if (hdrincl)
632 		err = raw_send_hdrinc(sk, &fl4, msg, len,
633 				      &rt, msg->msg_flags, &ipc.sockc);
634 
635 	 else {
636 		if (!ipc.addr)
637 			ipc.addr = fl4.daddr;
638 		lock_sock(sk);
639 		err = ip_append_data(sk, &fl4, raw_getfrag,
640 				     &rfv, len, 0,
641 				     &ipc, &rt, msg->msg_flags);
642 		if (err)
643 			ip_flush_pending_frames(sk);
644 		else if (!(msg->msg_flags & MSG_MORE)) {
645 			err = ip_push_pending_frames(sk, &fl4);
646 			if (err == -ENOBUFS && !inet_test_bit(RECVERR, sk))
647 				err = 0;
648 		}
649 		release_sock(sk);
650 	}
651 done:
652 	if (free)
653 		kfree(ipc.opt);
654 	ip_rt_put(rt);
655 
656 out:
657 	if (err < 0)
658 		return err;
659 	return len;
660 
661 do_confirm:
662 	if (msg->msg_flags & MSG_PROBE)
663 		dst_confirm_neigh(&rt->dst, &fl4.daddr);
664 	if (!(msg->msg_flags & MSG_PROBE) || len)
665 		goto back_from_confirm;
666 	err = 0;
667 	goto done;
668 }
669 
raw_close(struct sock * sk,long timeout)670 static void raw_close(struct sock *sk, long timeout)
671 {
672 	/*
673 	 * Raw sockets may have direct kernel references. Kill them.
674 	 */
675 	ip_ra_control(sk, 0, NULL);
676 
677 	sk_common_release(sk);
678 }
679 
raw_destroy(struct sock * sk)680 static void raw_destroy(struct sock *sk)
681 {
682 	lock_sock(sk);
683 	ip_flush_pending_frames(sk);
684 	release_sock(sk);
685 }
686 
687 /* This gets rid of all the nasties in af_inet. -DaveM */
raw_bind(struct sock * sk,struct sockaddr * uaddr,int addr_len)688 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
689 {
690 	struct inet_sock *inet = inet_sk(sk);
691 	struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
692 	struct net *net = sock_net(sk);
693 	u32 tb_id = RT_TABLE_LOCAL;
694 	int ret = -EINVAL;
695 	int chk_addr_ret;
696 
697 	lock_sock(sk);
698 	if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
699 		goto out;
700 
701 	if (sk->sk_bound_dev_if)
702 		tb_id = l3mdev_fib_table_by_index(net,
703 						  sk->sk_bound_dev_if) ? : tb_id;
704 
705 	chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
706 
707 	ret = -EADDRNOTAVAIL;
708 	if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr,
709 					 chk_addr_ret))
710 		goto out;
711 
712 	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
713 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
714 		inet->inet_saddr = 0;  /* Use device */
715 	sk_dst_reset(sk);
716 	ret = 0;
717 out:
718 	release_sock(sk);
719 	return ret;
720 }
721 
722 /*
723  *	This should be easy, if there is something there
724  *	we return it, otherwise we block.
725  */
726 
raw_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)727 static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
728 		       int flags, int *addr_len)
729 {
730 	struct inet_sock *inet = inet_sk(sk);
731 	size_t copied = 0;
732 	int err = -EOPNOTSUPP;
733 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
734 	struct sk_buff *skb;
735 
736 	if (flags & MSG_OOB)
737 		goto out;
738 
739 	if (flags & MSG_ERRQUEUE) {
740 		err = ip_recv_error(sk, msg, len, addr_len);
741 		goto out;
742 	}
743 
744 	skb = skb_recv_datagram(sk, flags, &err);
745 	if (!skb)
746 		goto out;
747 
748 	copied = skb->len;
749 	if (len < copied) {
750 		msg->msg_flags |= MSG_TRUNC;
751 		copied = len;
752 	}
753 
754 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
755 	if (err)
756 		goto done;
757 
758 	sock_recv_cmsgs(msg, sk, skb);
759 
760 	/* Copy the address. */
761 	if (sin) {
762 		sin->sin_family = AF_INET;
763 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
764 		sin->sin_port = 0;
765 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
766 		*addr_len = sizeof(*sin);
767 	}
768 	if (inet_cmsg_flags(inet))
769 		ip_cmsg_recv(msg, skb);
770 	if (flags & MSG_TRUNC)
771 		copied = skb->len;
772 done:
773 	skb_free_datagram(sk, skb);
774 out:
775 	if (err)
776 		return err;
777 	return copied;
778 }
779 
raw_sk_init(struct sock * sk)780 static int raw_sk_init(struct sock *sk)
781 {
782 	struct raw_sock *rp = raw_sk(sk);
783 
784 	if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
785 		memset(&rp->filter, 0, sizeof(rp->filter));
786 	return 0;
787 }
788 
raw_seticmpfilter(struct sock * sk,sockptr_t optval,int optlen)789 static int raw_seticmpfilter(struct sock *sk, sockptr_t optval, int optlen)
790 {
791 	if (optlen > sizeof(struct icmp_filter))
792 		optlen = sizeof(struct icmp_filter);
793 	if (copy_from_sockptr(&raw_sk(sk)->filter, optval, optlen))
794 		return -EFAULT;
795 	return 0;
796 }
797 
raw_geticmpfilter(struct sock * sk,char __user * optval,int __user * optlen)798 static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
799 {
800 	int len, ret = -EFAULT;
801 
802 	if (get_user(len, optlen))
803 		goto out;
804 	ret = -EINVAL;
805 	if (len < 0)
806 		goto out;
807 	if (len > sizeof(struct icmp_filter))
808 		len = sizeof(struct icmp_filter);
809 	ret = -EFAULT;
810 	if (put_user(len, optlen) ||
811 	    copy_to_user(optval, &raw_sk(sk)->filter, len))
812 		goto out;
813 	ret = 0;
814 out:	return ret;
815 }
816 
do_raw_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)817 static int do_raw_setsockopt(struct sock *sk, int level, int optname,
818 			     sockptr_t optval, unsigned int optlen)
819 {
820 	if (optname == ICMP_FILTER) {
821 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
822 			return -EOPNOTSUPP;
823 		else
824 			return raw_seticmpfilter(sk, optval, optlen);
825 	}
826 	return -ENOPROTOOPT;
827 }
828 
raw_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)829 static int raw_setsockopt(struct sock *sk, int level, int optname,
830 			  sockptr_t optval, unsigned int optlen)
831 {
832 	if (level != SOL_RAW)
833 		return ip_setsockopt(sk, level, optname, optval, optlen);
834 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
835 }
836 
do_raw_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)837 static int do_raw_getsockopt(struct sock *sk, int level, int optname,
838 			  char __user *optval, int __user *optlen)
839 {
840 	if (optname == ICMP_FILTER) {
841 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
842 			return -EOPNOTSUPP;
843 		else
844 			return raw_geticmpfilter(sk, optval, optlen);
845 	}
846 	return -ENOPROTOOPT;
847 }
848 
raw_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)849 static int raw_getsockopt(struct sock *sk, int level, int optname,
850 			  char __user *optval, int __user *optlen)
851 {
852 	if (level != SOL_RAW)
853 		return ip_getsockopt(sk, level, optname, optval, optlen);
854 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
855 }
856 
raw_ioctl(struct sock * sk,int cmd,int * karg)857 static int raw_ioctl(struct sock *sk, int cmd, int *karg)
858 {
859 	switch (cmd) {
860 	case SIOCOUTQ: {
861 		*karg = sk_wmem_alloc_get(sk);
862 		return 0;
863 	}
864 	case SIOCINQ: {
865 		struct sk_buff *skb;
866 
867 		spin_lock_bh(&sk->sk_receive_queue.lock);
868 		skb = skb_peek(&sk->sk_receive_queue);
869 		if (skb)
870 			*karg = skb->len;
871 		else
872 			*karg = 0;
873 		spin_unlock_bh(&sk->sk_receive_queue.lock);
874 		return 0;
875 	}
876 
877 	default:
878 #ifdef CONFIG_IP_MROUTE
879 		return ipmr_ioctl(sk, cmd, karg);
880 #else
881 		return -ENOIOCTLCMD;
882 #endif
883 	}
884 }
885 
886 #ifdef CONFIG_COMPAT
compat_raw_ioctl(struct sock * sk,unsigned int cmd,unsigned long arg)887 static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
888 {
889 	switch (cmd) {
890 	case SIOCOUTQ:
891 	case SIOCINQ:
892 		return -ENOIOCTLCMD;
893 	default:
894 #ifdef CONFIG_IP_MROUTE
895 		return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
896 #else
897 		return -ENOIOCTLCMD;
898 #endif
899 	}
900 }
901 #endif
902 
raw_abort(struct sock * sk,int err)903 int raw_abort(struct sock *sk, int err)
904 {
905 	lock_sock(sk);
906 
907 	sk->sk_err = err;
908 	sk_error_report(sk);
909 	__udp_disconnect(sk, 0);
910 
911 	release_sock(sk);
912 
913 	return 0;
914 }
915 EXPORT_SYMBOL_GPL(raw_abort);
916 
917 struct proto raw_prot = {
918 	.name		   = "RAW",
919 	.owner		   = THIS_MODULE,
920 	.close		   = raw_close,
921 	.destroy	   = raw_destroy,
922 	.connect	   = ip4_datagram_connect,
923 	.disconnect	   = __udp_disconnect,
924 	.ioctl		   = raw_ioctl,
925 	.init		   = raw_sk_init,
926 	.setsockopt	   = raw_setsockopt,
927 	.getsockopt	   = raw_getsockopt,
928 	.sendmsg	   = raw_sendmsg,
929 	.recvmsg	   = raw_recvmsg,
930 	.bind		   = raw_bind,
931 	.backlog_rcv	   = raw_rcv_skb,
932 	.release_cb	   = ip4_datagram_release_cb,
933 	.hash		   = raw_hash_sk,
934 	.unhash		   = raw_unhash_sk,
935 	.obj_size	   = sizeof(struct raw_sock),
936 	.useroffset	   = offsetof(struct raw_sock, filter),
937 	.usersize	   = sizeof_field(struct raw_sock, filter),
938 	.h.raw_hash	   = &raw_v4_hashinfo,
939 #ifdef CONFIG_COMPAT
940 	.compat_ioctl	   = compat_raw_ioctl,
941 #endif
942 	.diag_destroy	   = raw_abort,
943 };
944 
945 #ifdef CONFIG_PROC_FS
raw_get_first(struct seq_file * seq,int bucket)946 static struct sock *raw_get_first(struct seq_file *seq, int bucket)
947 {
948 	struct raw_hashinfo *h = pde_data(file_inode(seq->file));
949 	struct raw_iter_state *state = raw_seq_private(seq);
950 	struct hlist_head *hlist;
951 	struct sock *sk;
952 
953 	for (state->bucket = bucket; state->bucket < RAW_HTABLE_SIZE;
954 			++state->bucket) {
955 		hlist = &h->ht[state->bucket];
956 		sk_for_each(sk, hlist) {
957 			if (sock_net(sk) == seq_file_net(seq))
958 				return sk;
959 		}
960 	}
961 	return NULL;
962 }
963 
raw_get_next(struct seq_file * seq,struct sock * sk)964 static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
965 {
966 	struct raw_iter_state *state = raw_seq_private(seq);
967 
968 	do {
969 		sk = sk_next(sk);
970 	} while (sk && sock_net(sk) != seq_file_net(seq));
971 
972 	if (!sk)
973 		return raw_get_first(seq, state->bucket + 1);
974 	return sk;
975 }
976 
raw_get_idx(struct seq_file * seq,loff_t pos)977 static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
978 {
979 	struct sock *sk = raw_get_first(seq, 0);
980 
981 	if (sk)
982 		while (pos && (sk = raw_get_next(seq, sk)) != NULL)
983 			--pos;
984 	return pos ? NULL : sk;
985 }
986 
raw_seq_start(struct seq_file * seq,loff_t * pos)987 void *raw_seq_start(struct seq_file *seq, loff_t *pos)
988 	__acquires(&h->lock)
989 {
990 	struct raw_hashinfo *h = pde_data(file_inode(seq->file));
991 
992 	spin_lock(&h->lock);
993 
994 	return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
995 }
996 EXPORT_SYMBOL_GPL(raw_seq_start);
997 
raw_seq_next(struct seq_file * seq,void * v,loff_t * pos)998 void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
999 {
1000 	struct sock *sk;
1001 
1002 	if (v == SEQ_START_TOKEN)
1003 		sk = raw_get_first(seq, 0);
1004 	else
1005 		sk = raw_get_next(seq, v);
1006 	++*pos;
1007 	return sk;
1008 }
1009 EXPORT_SYMBOL_GPL(raw_seq_next);
1010 
raw_seq_stop(struct seq_file * seq,void * v)1011 void raw_seq_stop(struct seq_file *seq, void *v)
1012 	__releases(&h->lock)
1013 {
1014 	struct raw_hashinfo *h = pde_data(file_inode(seq->file));
1015 
1016 	spin_unlock(&h->lock);
1017 }
1018 EXPORT_SYMBOL_GPL(raw_seq_stop);
1019 
raw_sock_seq_show(struct seq_file * seq,struct sock * sp,int i)1020 static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
1021 {
1022 	struct inet_sock *inet = inet_sk(sp);
1023 	__be32 dest = inet->inet_daddr,
1024 	       src = inet->inet_rcv_saddr;
1025 	__u16 destp = 0,
1026 	      srcp  = inet->inet_num;
1027 
1028 	seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
1029 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u\n",
1030 		i, src, srcp, dest, destp, sp->sk_state,
1031 		sk_wmem_alloc_get(sp),
1032 		sk_rmem_alloc_get(sp),
1033 		0, 0L, 0,
1034 		from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1035 		0, sock_i_ino(sp),
1036 		refcount_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
1037 }
1038 
raw_seq_show(struct seq_file * seq,void * v)1039 static int raw_seq_show(struct seq_file *seq, void *v)
1040 {
1041 	if (v == SEQ_START_TOKEN)
1042 		seq_printf(seq, "  sl  local_address rem_address   st tx_queue "
1043 				"rx_queue tr tm->when retrnsmt   uid  timeout "
1044 				"inode ref pointer drops\n");
1045 	else
1046 		raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
1047 	return 0;
1048 }
1049 
1050 static const struct seq_operations raw_seq_ops = {
1051 	.start = raw_seq_start,
1052 	.next  = raw_seq_next,
1053 	.stop  = raw_seq_stop,
1054 	.show  = raw_seq_show,
1055 };
1056 
raw_init_net(struct net * net)1057 static __net_init int raw_init_net(struct net *net)
1058 {
1059 	if (!proc_create_net_data("raw", 0444, net->proc_net, &raw_seq_ops,
1060 			sizeof(struct raw_iter_state), &raw_v4_hashinfo))
1061 		return -ENOMEM;
1062 
1063 	return 0;
1064 }
1065 
raw_exit_net(struct net * net)1066 static __net_exit void raw_exit_net(struct net *net)
1067 {
1068 	remove_proc_entry("raw", net->proc_net);
1069 }
1070 
1071 static __net_initdata struct pernet_operations raw_net_ops = {
1072 	.init = raw_init_net,
1073 	.exit = raw_exit_net,
1074 };
1075 
raw_proc_init(void)1076 int __init raw_proc_init(void)
1077 {
1078 
1079 	return register_pernet_subsys(&raw_net_ops);
1080 }
1081 
raw_proc_exit(void)1082 void __init raw_proc_exit(void)
1083 {
1084 	unregister_pernet_subsys(&raw_net_ops);
1085 }
1086 #endif /* CONFIG_PROC_FS */
1087 
raw_sysctl_init_net(struct net * net)1088 static void raw_sysctl_init_net(struct net *net)
1089 {
1090 #ifdef CONFIG_NET_L3_MASTER_DEV
1091 	net->ipv4.sysctl_raw_l3mdev_accept = 1;
1092 #endif
1093 }
1094 
raw_sysctl_init(struct net * net)1095 static int __net_init raw_sysctl_init(struct net *net)
1096 {
1097 	raw_sysctl_init_net(net);
1098 	return 0;
1099 }
1100 
1101 static struct pernet_operations __net_initdata raw_sysctl_ops = {
1102 	.init	= raw_sysctl_init,
1103 };
1104 
raw_init(void)1105 void __init raw_init(void)
1106 {
1107 	raw_sysctl_init_net(&init_net);
1108 	if (register_pernet_subsys(&raw_sysctl_ops))
1109 		panic("RAW: failed to init sysctl parameters.\n");
1110 }
1111