• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		RAW - implementation of IP "raw" sockets.
7  *
8  * Authors:	Ross Biro
9  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10  *
11  * Fixes:
12  *		Alan Cox	:	verify_area() fixed up
13  *		Alan Cox	:	ICMP error handling
14  *		Alan Cox	:	EMSGSIZE if you send too big a packet
15  *		Alan Cox	: 	Now uses generic datagrams and shared
16  *					skbuff library. No more peek crashes,
17  *					no more backlogs
18  *		Alan Cox	:	Checks sk->broadcast.
19  *		Alan Cox	:	Uses skb_free_datagram/skb_copy_datagram
20  *		Alan Cox	:	Raw passes ip options too
21  *		Alan Cox	:	Setsocketopt added
22  *		Alan Cox	:	Fixed error return for broadcasts
23  *		Alan Cox	:	Removed wake_up calls
24  *		Alan Cox	:	Use ttl/tos
25  *		Alan Cox	:	Cleaned up old debugging
26  *		Alan Cox	:	Use new kernel side addresses
27  *	Arnt Gulbrandsen	:	Fixed MSG_DONTROUTE in raw sockets.
28  *		Alan Cox	:	BSD style RAW socket demultiplexing.
29  *		Alan Cox	:	Beginnings of mrouted support.
30  *		Alan Cox	:	Added IP_HDRINCL option.
31  *		Alan Cox	:	Skip broadcast check if BSDism set.
32  *		David S. Miller	:	New socket lookup architecture.
33  *
34  *		This program is free software; you can redistribute it and/or
35  *		modify it under the terms of the GNU General Public License
36  *		as published by the Free Software Foundation; either version
37  *		2 of the License, or (at your option) any later version.
38  */
39 
40 #include <linux/types.h>
41 #include <linux/atomic.h>
42 #include <asm/byteorder.h>
43 #include <asm/current.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
46 #include <linux/stddef.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/aio.h>
50 #include <linux/kernel.h>
51 #include <linux/export.h>
52 #include <linux/spinlock.h>
53 #include <linux/sockios.h>
54 #include <linux/socket.h>
55 #include <linux/in.h>
56 #include <linux/mroute.h>
57 #include <linux/netdevice.h>
58 #include <linux/in_route.h>
59 #include <linux/route.h>
60 #include <linux/skbuff.h>
61 #include <linux/igmp.h>
62 #include <net/net_namespace.h>
63 #include <net/dst.h>
64 #include <net/sock.h>
65 #include <linux/ip.h>
66 #include <linux/net.h>
67 #include <net/ip.h>
68 #include <net/icmp.h>
69 #include <net/udp.h>
70 #include <net/raw.h>
71 #include <net/snmp.h>
72 #include <net/tcp_states.h>
73 #include <net/inet_common.h>
74 #include <net/checksum.h>
75 #include <net/xfrm.h>
76 #include <linux/rtnetlink.h>
77 #include <linux/proc_fs.h>
78 #include <linux/seq_file.h>
79 #include <linux/netfilter.h>
80 #include <linux/netfilter_ipv4.h>
81 #include <linux/compat.h>
82 
83 static struct raw_hashinfo raw_v4_hashinfo = {
84 	.lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
85 };
86 
raw_hash_sk(struct sock * sk)87 void raw_hash_sk(struct sock *sk)
88 {
89 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
90 	struct hlist_head *head;
91 
92 	head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
93 
94 	write_lock_bh(&h->lock);
95 	sk_add_node(sk, head);
96 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
97 	write_unlock_bh(&h->lock);
98 }
99 EXPORT_SYMBOL_GPL(raw_hash_sk);
100 
raw_unhash_sk(struct sock * sk)101 void raw_unhash_sk(struct sock *sk)
102 {
103 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
104 
105 	write_lock_bh(&h->lock);
106 	if (sk_del_node_init(sk))
107 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
108 	write_unlock_bh(&h->lock);
109 }
110 EXPORT_SYMBOL_GPL(raw_unhash_sk);
111 
__raw_v4_lookup(struct net * net,struct sock * sk,unsigned short num,__be32 raddr,__be32 laddr,int dif)112 static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
113 		unsigned short num, __be32 raddr, __be32 laddr, int dif)
114 {
115 	sk_for_each_from(sk) {
116 		struct inet_sock *inet = inet_sk(sk);
117 
118 		if (net_eq(sock_net(sk), net) && inet->inet_num == num	&&
119 		    !(inet->inet_daddr && inet->inet_daddr != raddr) 	&&
120 		    !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
121 		    !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
122 			goto found; /* gotcha */
123 	}
124 	sk = NULL;
125 found:
126 	return sk;
127 }
128 
129 /*
130  *	0 - deliver
131  *	1 - block
132  */
icmp_filter(const struct sock * sk,const struct sk_buff * skb)133 static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
134 {
135 	struct icmphdr _hdr;
136 	const struct icmphdr *hdr;
137 
138 	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
139 				 sizeof(_hdr), &_hdr);
140 	if (!hdr)
141 		return 1;
142 
143 	if (hdr->type < 32) {
144 		__u32 data = raw_sk(sk)->filter.data;
145 
146 		return ((1U << hdr->type) & data) != 0;
147 	}
148 
149 	/* Do not block unknown ICMP types */
150 	return 0;
151 }
152 
153 /* IP input processing comes here for RAW socket delivery.
154  * Caller owns SKB, so we must make clones.
155  *
156  * RFC 1122: SHOULD pass TOS value up to the transport layer.
157  * -> It does. And not only TOS, but all IP header.
158  */
raw_v4_input(struct sk_buff * skb,const struct iphdr * iph,int hash)159 static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
160 {
161 	struct sock *sk;
162 	struct hlist_head *head;
163 	int delivered = 0;
164 	struct net *net;
165 
166 	read_lock(&raw_v4_hashinfo.lock);
167 	head = &raw_v4_hashinfo.ht[hash];
168 	if (hlist_empty(head))
169 		goto out;
170 
171 	net = dev_net(skb->dev);
172 	sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
173 			     iph->saddr, iph->daddr,
174 			     skb->dev->ifindex);
175 
176 	while (sk) {
177 		delivered = 1;
178 		if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
179 		    ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
180 				   skb->dev->ifindex)) {
181 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
182 
183 			/* Not releasing hash table! */
184 			if (clone)
185 				raw_rcv(sk, clone);
186 		}
187 		sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
188 				     iph->saddr, iph->daddr,
189 				     skb->dev->ifindex);
190 	}
191 out:
192 	read_unlock(&raw_v4_hashinfo.lock);
193 	return delivered;
194 }
195 
raw_local_deliver(struct sk_buff * skb,int protocol)196 int raw_local_deliver(struct sk_buff *skb, int protocol)
197 {
198 	int hash;
199 	struct sock *raw_sk;
200 
201 	hash = protocol & (RAW_HTABLE_SIZE - 1);
202 	raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
203 
204 	/* If there maybe a raw socket we must check - if not we
205 	 * don't care less
206 	 */
207 	if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
208 		raw_sk = NULL;
209 
210 	return raw_sk != NULL;
211 
212 }
213 
raw_err(struct sock * sk,struct sk_buff * skb,u32 info)214 static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
215 {
216 	struct inet_sock *inet = inet_sk(sk);
217 	const int type = icmp_hdr(skb)->type;
218 	const int code = icmp_hdr(skb)->code;
219 	int err = 0;
220 	int harderr = 0;
221 
222 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
223 		ipv4_sk_update_pmtu(skb, sk, info);
224 	else if (type == ICMP_REDIRECT) {
225 		ipv4_sk_redirect(skb, sk);
226 		return;
227 	}
228 
229 	/* Report error on raw socket, if:
230 	   1. User requested ip_recverr.
231 	   2. Socket is connected (otherwise the error indication
232 	      is useless without ip_recverr and error is hard.
233 	 */
234 	if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
235 		return;
236 
237 	switch (type) {
238 	default:
239 	case ICMP_TIME_EXCEEDED:
240 		err = EHOSTUNREACH;
241 		break;
242 	case ICMP_SOURCE_QUENCH:
243 		return;
244 	case ICMP_PARAMETERPROB:
245 		err = EPROTO;
246 		harderr = 1;
247 		break;
248 	case ICMP_DEST_UNREACH:
249 		err = EHOSTUNREACH;
250 		if (code > NR_ICMP_UNREACH)
251 			break;
252 		err = icmp_err_convert[code].errno;
253 		harderr = icmp_err_convert[code].fatal;
254 		if (code == ICMP_FRAG_NEEDED) {
255 			harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
256 			err = EMSGSIZE;
257 		}
258 	}
259 
260 	if (inet->recverr) {
261 		const struct iphdr *iph = (const struct iphdr *)skb->data;
262 		u8 *payload = skb->data + (iph->ihl << 2);
263 
264 		if (inet->hdrincl)
265 			payload = skb->data;
266 		ip_icmp_error(sk, skb, err, 0, info, payload);
267 	}
268 
269 	if (inet->recverr || harderr) {
270 		sk->sk_err = err;
271 		sk->sk_error_report(sk);
272 	}
273 }
274 
raw_icmp_error(struct sk_buff * skb,int protocol,u32 info)275 void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
276 {
277 	int hash;
278 	struct sock *raw_sk;
279 	const struct iphdr *iph;
280 	struct net *net;
281 
282 	hash = protocol & (RAW_HTABLE_SIZE - 1);
283 
284 	read_lock(&raw_v4_hashinfo.lock);
285 	raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
286 	if (raw_sk != NULL) {
287 		iph = (const struct iphdr *)skb->data;
288 		net = dev_net(skb->dev);
289 
290 		while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
291 						iph->daddr, iph->saddr,
292 						skb->dev->ifindex)) != NULL) {
293 			raw_err(raw_sk, skb, info);
294 			raw_sk = sk_next(raw_sk);
295 			iph = (const struct iphdr *)skb->data;
296 		}
297 	}
298 	read_unlock(&raw_v4_hashinfo.lock);
299 }
300 
raw_rcv_skb(struct sock * sk,struct sk_buff * skb)301 static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
302 {
303 	/* Charge it to the socket. */
304 
305 	ipv4_pktinfo_prepare(sk, skb);
306 	if (sock_queue_rcv_skb(sk, skb) < 0) {
307 		kfree_skb(skb);
308 		return NET_RX_DROP;
309 	}
310 
311 	return NET_RX_SUCCESS;
312 }
313 
raw_rcv(struct sock * sk,struct sk_buff * skb)314 int raw_rcv(struct sock *sk, struct sk_buff *skb)
315 {
316 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
317 		atomic_inc(&sk->sk_drops);
318 		kfree_skb(skb);
319 		return NET_RX_DROP;
320 	}
321 	nf_reset(skb);
322 
323 	skb_push(skb, skb->data - skb_network_header(skb));
324 
325 	raw_rcv_skb(sk, skb);
326 	return 0;
327 }
328 
raw_send_hdrinc(struct sock * sk,struct flowi4 * fl4,void * from,size_t length,struct rtable ** rtp,unsigned int flags)329 static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
330 			   void *from, size_t length,
331 			   struct rtable **rtp,
332 			   unsigned int flags)
333 {
334 	struct inet_sock *inet = inet_sk(sk);
335 	struct net *net = sock_net(sk);
336 	struct iphdr *iph;
337 	struct sk_buff *skb;
338 	unsigned int iphlen;
339 	int err;
340 	struct rtable *rt = *rtp;
341 	int hlen, tlen;
342 
343 	if (length > rt->dst.dev->mtu) {
344 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
345 			       rt->dst.dev->mtu);
346 		return -EMSGSIZE;
347 	}
348 	if (length < sizeof(struct iphdr))
349 		return -EINVAL;
350 
351 	if (flags&MSG_PROBE)
352 		goto out;
353 
354 	hlen = LL_RESERVED_SPACE(rt->dst.dev);
355 	tlen = rt->dst.dev->needed_tailroom;
356 	skb = sock_alloc_send_skb(sk,
357 				  length + hlen + tlen + 15,
358 				  flags & MSG_DONTWAIT, &err);
359 	if (skb == NULL)
360 		goto error;
361 	skb_reserve(skb, hlen);
362 
363 	skb->priority = sk->sk_priority;
364 	skb->mark = sk->sk_mark;
365 	skb_dst_set(skb, &rt->dst);
366 	*rtp = NULL;
367 
368 	skb_reset_network_header(skb);
369 	iph = ip_hdr(skb);
370 	skb_put(skb, length);
371 
372 	skb->ip_summed = CHECKSUM_NONE;
373 
374 	sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
375 
376 	skb->transport_header = skb->network_header;
377 	err = -EFAULT;
378 	if (memcpy_fromiovecend((void *)iph, from, 0, length))
379 		goto error_free;
380 
381 	iphlen = iph->ihl * 4;
382 
383 	/*
384 	 * We don't want to modify the ip header, but we do need to
385 	 * be sure that it won't cause problems later along the network
386 	 * stack.  Specifically we want to make sure that iph->ihl is a
387 	 * sane value.  If ihl points beyond the length of the buffer passed
388 	 * in, reject the frame as invalid
389 	 */
390 	err = -EINVAL;
391 	if (iphlen > length)
392 		goto error_free;
393 
394 	if (iphlen >= sizeof(*iph)) {
395 		if (!iph->saddr)
396 			iph->saddr = fl4->saddr;
397 		iph->check   = 0;
398 		iph->tot_len = htons(length);
399 		if (!iph->id)
400 			ip_select_ident(skb, NULL);
401 
402 		iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
403 	}
404 	if (iph->protocol == IPPROTO_ICMP)
405 		icmp_out_count(net, ((struct icmphdr *)
406 			skb_transport_header(skb))->type);
407 
408 	err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
409 		      rt->dst.dev, dst_output);
410 	if (err > 0)
411 		err = net_xmit_errno(err);
412 	if (err)
413 		goto error;
414 out:
415 	return 0;
416 
417 error_free:
418 	kfree_skb(skb);
419 error:
420 	IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
421 	if (err == -ENOBUFS && !inet->recverr)
422 		err = 0;
423 	return err;
424 }
425 
raw_probe_proto_opt(struct flowi4 * fl4,struct msghdr * msg)426 static int raw_probe_proto_opt(struct flowi4 *fl4, struct msghdr *msg)
427 {
428 	struct iovec *iov;
429 	u8 __user *type = NULL;
430 	u8 __user *code = NULL;
431 	int probed = 0;
432 	unsigned int i;
433 
434 	if (!msg->msg_iov)
435 		return 0;
436 
437 	for (i = 0; i < msg->msg_iovlen; i++) {
438 		iov = &msg->msg_iov[i];
439 		if (!iov)
440 			continue;
441 
442 		switch (fl4->flowi4_proto) {
443 		case IPPROTO_ICMP:
444 			/* check if one-byte field is readable or not. */
445 			if (iov->iov_base && iov->iov_len < 1)
446 				break;
447 
448 			if (!type) {
449 				type = iov->iov_base;
450 				/* check if code field is readable or not. */
451 				if (iov->iov_len > 1)
452 					code = type + 1;
453 			} else if (!code)
454 				code = iov->iov_base;
455 
456 			if (type && code) {
457 				if (get_user(fl4->fl4_icmp_type, type) ||
458 				    get_user(fl4->fl4_icmp_code, code))
459 					return -EFAULT;
460 				probed = 1;
461 			}
462 			break;
463 		default:
464 			probed = 1;
465 			break;
466 		}
467 		if (probed)
468 			break;
469 	}
470 	return 0;
471 }
472 
raw_sendmsg(struct kiocb * iocb,struct sock * sk,struct msghdr * msg,size_t len)473 static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
474 		       size_t len)
475 {
476 	struct inet_sock *inet = inet_sk(sk);
477 	struct ipcm_cookie ipc;
478 	struct rtable *rt = NULL;
479 	struct flowi4 fl4;
480 	int free = 0;
481 	__be32 daddr;
482 	__be32 saddr;
483 	u8  tos;
484 	int err;
485 	struct ip_options_data opt_copy;
486 	int hdrincl;
487 
488 	err = -EMSGSIZE;
489 	if (len > 0xFFFF)
490 		goto out;
491 
492 	/* hdrincl should be READ_ONCE(inet->hdrincl)
493 	 * but READ_ONCE() doesn't work with bit fields
494 	 */
495 	hdrincl = inet->hdrincl;
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 	ipc.addr = inet->inet_saddr;
533 	ipc.opt = NULL;
534 	ipc.tx_flags = 0;
535 	ipc.ttl = 0;
536 	ipc.tos = -1;
537 	ipc.oif = sk->sk_bound_dev_if;
538 
539 	if (msg->msg_controllen) {
540 		err = ip_cmsg_send(sock_net(sk), msg, &ipc, false);
541 		if (err)
542 			goto out;
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_rtconn_flags(&ipc, sk);
577 	if (msg->msg_flags & MSG_DONTROUTE)
578 		tos |= RTO_ONLINK;
579 
580 	if (ipv4_is_multicast(daddr)) {
581 		if (!ipc.oif)
582 			ipc.oif = inet->mc_index;
583 		if (!saddr)
584 			saddr = inet->mc_addr;
585 	} else if (!ipc.oif)
586 		ipc.oif = inet->uc_index;
587 
588 	flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
589 			   RT_SCOPE_UNIVERSE,
590 			   hdrincl ? IPPROTO_RAW : sk->sk_protocol,
591 			   inet_sk_flowi_flags(sk) |
592 			    (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
593 			   daddr, saddr, 0, 0, sk->sk_uid);
594 
595 	if (!hdrincl) {
596 		err = raw_probe_proto_opt(&fl4, msg);
597 		if (err)
598 			goto done;
599 	}
600 
601 	security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
602 	rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
603 	if (IS_ERR(rt)) {
604 		err = PTR_ERR(rt);
605 		rt = NULL;
606 		goto done;
607 	}
608 
609 	err = -EACCES;
610 	if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
611 		goto done;
612 
613 	if (msg->msg_flags & MSG_CONFIRM)
614 		goto do_confirm;
615 back_from_confirm:
616 
617 	if (hdrincl)
618 		err = raw_send_hdrinc(sk, &fl4, msg->msg_iov, len,
619 				      &rt, msg->msg_flags);
620 
621 	 else {
622 		sock_tx_timestamp(sk, &ipc.tx_flags);
623 
624 		if (!ipc.addr)
625 			ipc.addr = fl4.daddr;
626 		lock_sock(sk);
627 		err = ip_append_data(sk, &fl4, ip_generic_getfrag,
628 				     msg->msg_iov, len, 0,
629 				     &ipc, &rt, msg->msg_flags);
630 		if (err)
631 			ip_flush_pending_frames(sk);
632 		else if (!(msg->msg_flags & MSG_MORE)) {
633 			err = ip_push_pending_frames(sk, &fl4);
634 			if (err == -ENOBUFS && !inet->recverr)
635 				err = 0;
636 		}
637 		release_sock(sk);
638 	}
639 done:
640 	if (free)
641 		kfree(ipc.opt);
642 	ip_rt_put(rt);
643 
644 out:
645 	if (err < 0)
646 		return err;
647 	return len;
648 
649 do_confirm:
650 	dst_confirm(&rt->dst);
651 	if (!(msg->msg_flags & MSG_PROBE) || len)
652 		goto back_from_confirm;
653 	err = 0;
654 	goto done;
655 }
656 
raw_close(struct sock * sk,long timeout)657 static void raw_close(struct sock *sk, long timeout)
658 {
659 	/*
660 	 * Raw sockets may have direct kernel references. Kill them.
661 	 */
662 	ip_ra_control(sk, 0, NULL);
663 
664 	sk_common_release(sk);
665 }
666 
raw_destroy(struct sock * sk)667 static void raw_destroy(struct sock *sk)
668 {
669 	lock_sock(sk);
670 	ip_flush_pending_frames(sk);
671 	release_sock(sk);
672 }
673 
674 /* This gets rid of all the nasties in af_inet. -DaveM */
raw_bind(struct sock * sk,struct sockaddr * uaddr,int addr_len)675 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
676 {
677 	struct inet_sock *inet = inet_sk(sk);
678 	struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
679 	int ret = -EINVAL;
680 	int chk_addr_ret;
681 
682 	if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
683 		goto out;
684 	chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
685 	ret = -EADDRNOTAVAIL;
686 	if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
687 	    chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
688 		goto out;
689 	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
690 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
691 		inet->inet_saddr = 0;  /* Use device */
692 	sk_dst_reset(sk);
693 	ret = 0;
694 out:	return ret;
695 }
696 
697 /*
698  *	This should be easy, if there is something there
699  *	we return it, otherwise we block.
700  */
701 
raw_recvmsg(struct kiocb * iocb,struct sock * sk,struct msghdr * msg,size_t len,int noblock,int flags,int * addr_len)702 static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
703 		       size_t len, int noblock, int flags, int *addr_len)
704 {
705 	struct inet_sock *inet = inet_sk(sk);
706 	size_t copied = 0;
707 	int err = -EOPNOTSUPP;
708 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
709 	struct sk_buff *skb;
710 
711 	if (flags & MSG_OOB)
712 		goto out;
713 
714 	if (flags & MSG_ERRQUEUE) {
715 		err = ip_recv_error(sk, msg, len, addr_len);
716 		goto out;
717 	}
718 
719 	skb = skb_recv_datagram(sk, flags, noblock, &err);
720 	if (!skb)
721 		goto out;
722 
723 	copied = skb->len;
724 	if (len < copied) {
725 		msg->msg_flags |= MSG_TRUNC;
726 		copied = len;
727 	}
728 
729 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
730 	if (err)
731 		goto done;
732 
733 	sock_recv_ts_and_drops(msg, sk, skb);
734 
735 	/* Copy the address. */
736 	if (sin) {
737 		sin->sin_family = AF_INET;
738 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
739 		sin->sin_port = 0;
740 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
741 		*addr_len = sizeof(*sin);
742 	}
743 	if (inet->cmsg_flags)
744 		ip_cmsg_recv(msg, skb);
745 	if (flags & MSG_TRUNC)
746 		copied = skb->len;
747 done:
748 	skb_free_datagram(sk, skb);
749 out:
750 	if (err)
751 		return err;
752 	return copied;
753 }
754 
raw_init(struct sock * sk)755 static int raw_init(struct sock *sk)
756 {
757 	struct raw_sock *rp = raw_sk(sk);
758 
759 	if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
760 		memset(&rp->filter, 0, sizeof(rp->filter));
761 	return 0;
762 }
763 
raw_seticmpfilter(struct sock * sk,char __user * optval,int optlen)764 static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
765 {
766 	if (optlen > sizeof(struct icmp_filter))
767 		optlen = sizeof(struct icmp_filter);
768 	if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
769 		return -EFAULT;
770 	return 0;
771 }
772 
raw_geticmpfilter(struct sock * sk,char __user * optval,int __user * optlen)773 static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
774 {
775 	int len, ret = -EFAULT;
776 
777 	if (get_user(len, optlen))
778 		goto out;
779 	ret = -EINVAL;
780 	if (len < 0)
781 		goto out;
782 	if (len > sizeof(struct icmp_filter))
783 		len = sizeof(struct icmp_filter);
784 	ret = -EFAULT;
785 	if (put_user(len, optlen) ||
786 	    copy_to_user(optval, &raw_sk(sk)->filter, len))
787 		goto out;
788 	ret = 0;
789 out:	return ret;
790 }
791 
do_raw_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)792 static int do_raw_setsockopt(struct sock *sk, int level, int optname,
793 			  char __user *optval, unsigned int optlen)
794 {
795 	if (optname == ICMP_FILTER) {
796 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
797 			return -EOPNOTSUPP;
798 		else
799 			return raw_seticmpfilter(sk, optval, optlen);
800 	}
801 	return -ENOPROTOOPT;
802 }
803 
raw_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)804 static int raw_setsockopt(struct sock *sk, int level, int optname,
805 			  char __user *optval, unsigned int optlen)
806 {
807 	if (level != SOL_RAW)
808 		return ip_setsockopt(sk, level, optname, optval, optlen);
809 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
810 }
811 
812 #ifdef CONFIG_COMPAT
compat_raw_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)813 static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
814 				 char __user *optval, unsigned int optlen)
815 {
816 	if (level != SOL_RAW)
817 		return compat_ip_setsockopt(sk, level, optname, optval, optlen);
818 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
819 }
820 #endif
821 
do_raw_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)822 static int do_raw_getsockopt(struct sock *sk, int level, int optname,
823 			  char __user *optval, int __user *optlen)
824 {
825 	if (optname == ICMP_FILTER) {
826 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
827 			return -EOPNOTSUPP;
828 		else
829 			return raw_geticmpfilter(sk, optval, optlen);
830 	}
831 	return -ENOPROTOOPT;
832 }
833 
raw_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)834 static int raw_getsockopt(struct sock *sk, int level, int optname,
835 			  char __user *optval, int __user *optlen)
836 {
837 	if (level != SOL_RAW)
838 		return ip_getsockopt(sk, level, optname, optval, optlen);
839 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
840 }
841 
842 #ifdef CONFIG_COMPAT
compat_raw_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)843 static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
844 				 char __user *optval, int __user *optlen)
845 {
846 	if (level != SOL_RAW)
847 		return compat_ip_getsockopt(sk, level, optname, optval, optlen);
848 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
849 }
850 #endif
851 
raw_ioctl(struct sock * sk,int cmd,unsigned long arg)852 static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
853 {
854 	switch (cmd) {
855 	case SIOCOUTQ: {
856 		int amount = sk_wmem_alloc_get(sk);
857 
858 		return put_user(amount, (int __user *)arg);
859 	}
860 	case SIOCINQ: {
861 		struct sk_buff *skb;
862 		int amount = 0;
863 
864 		spin_lock_bh(&sk->sk_receive_queue.lock);
865 		skb = skb_peek(&sk->sk_receive_queue);
866 		if (skb != NULL)
867 			amount = skb->len;
868 		spin_unlock_bh(&sk->sk_receive_queue.lock);
869 		return put_user(amount, (int __user *)arg);
870 	}
871 
872 	default:
873 #ifdef CONFIG_IP_MROUTE
874 		return ipmr_ioctl(sk, cmd, (void __user *)arg);
875 #else
876 		return -ENOIOCTLCMD;
877 #endif
878 	}
879 }
880 
881 #ifdef CONFIG_COMPAT
compat_raw_ioctl(struct sock * sk,unsigned int cmd,unsigned long arg)882 static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
883 {
884 	switch (cmd) {
885 	case SIOCOUTQ:
886 	case SIOCINQ:
887 		return -ENOIOCTLCMD;
888 	default:
889 #ifdef CONFIG_IP_MROUTE
890 		return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
891 #else
892 		return -ENOIOCTLCMD;
893 #endif
894 	}
895 }
896 #endif
897 
898 struct proto raw_prot = {
899 	.name		   = "RAW",
900 	.owner		   = THIS_MODULE,
901 	.close		   = raw_close,
902 	.destroy	   = raw_destroy,
903 	.connect	   = ip4_datagram_connect,
904 	.disconnect	   = udp_disconnect,
905 	.ioctl		   = raw_ioctl,
906 	.init		   = raw_init,
907 	.setsockopt	   = raw_setsockopt,
908 	.getsockopt	   = raw_getsockopt,
909 	.sendmsg	   = raw_sendmsg,
910 	.recvmsg	   = raw_recvmsg,
911 	.bind		   = raw_bind,
912 	.backlog_rcv	   = raw_rcv_skb,
913 	.release_cb	   = ip4_datagram_release_cb,
914 	.hash		   = raw_hash_sk,
915 	.unhash		   = raw_unhash_sk,
916 	.obj_size	   = sizeof(struct raw_sock),
917 	.h.raw_hash	   = &raw_v4_hashinfo,
918 #ifdef CONFIG_COMPAT
919 	.compat_setsockopt = compat_raw_setsockopt,
920 	.compat_getsockopt = compat_raw_getsockopt,
921 	.compat_ioctl	   = compat_raw_ioctl,
922 #endif
923 };
924 
925 #ifdef CONFIG_PROC_FS
raw_get_first(struct seq_file * seq)926 static struct sock *raw_get_first(struct seq_file *seq)
927 {
928 	struct sock *sk;
929 	struct raw_iter_state *state = raw_seq_private(seq);
930 
931 	for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
932 			++state->bucket) {
933 		sk_for_each(sk, &state->h->ht[state->bucket])
934 			if (sock_net(sk) == seq_file_net(seq))
935 				goto found;
936 	}
937 	sk = NULL;
938 found:
939 	return sk;
940 }
941 
raw_get_next(struct seq_file * seq,struct sock * sk)942 static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
943 {
944 	struct raw_iter_state *state = raw_seq_private(seq);
945 
946 	do {
947 		sk = sk_next(sk);
948 try_again:
949 		;
950 	} while (sk && sock_net(sk) != seq_file_net(seq));
951 
952 	if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
953 		sk = sk_head(&state->h->ht[state->bucket]);
954 		goto try_again;
955 	}
956 	return sk;
957 }
958 
raw_get_idx(struct seq_file * seq,loff_t pos)959 static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
960 {
961 	struct sock *sk = raw_get_first(seq);
962 
963 	if (sk)
964 		while (pos && (sk = raw_get_next(seq, sk)) != NULL)
965 			--pos;
966 	return pos ? NULL : sk;
967 }
968 
raw_seq_start(struct seq_file * seq,loff_t * pos)969 void *raw_seq_start(struct seq_file *seq, loff_t *pos)
970 {
971 	struct raw_iter_state *state = raw_seq_private(seq);
972 
973 	read_lock(&state->h->lock);
974 	return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
975 }
976 EXPORT_SYMBOL_GPL(raw_seq_start);
977 
raw_seq_next(struct seq_file * seq,void * v,loff_t * pos)978 void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
979 {
980 	struct sock *sk;
981 
982 	if (v == SEQ_START_TOKEN)
983 		sk = raw_get_first(seq);
984 	else
985 		sk = raw_get_next(seq, v);
986 	++*pos;
987 	return sk;
988 }
989 EXPORT_SYMBOL_GPL(raw_seq_next);
990 
raw_seq_stop(struct seq_file * seq,void * v)991 void raw_seq_stop(struct seq_file *seq, void *v)
992 {
993 	struct raw_iter_state *state = raw_seq_private(seq);
994 
995 	read_unlock(&state->h->lock);
996 }
997 EXPORT_SYMBOL_GPL(raw_seq_stop);
998 
raw_sock_seq_show(struct seq_file * seq,struct sock * sp,int i)999 static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
1000 {
1001 	struct inet_sock *inet = inet_sk(sp);
1002 	__be32 dest = inet->inet_daddr,
1003 	       src = inet->inet_rcv_saddr;
1004 	__u16 destp = 0,
1005 	      srcp  = inet->inet_num;
1006 
1007 	seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
1008 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
1009 		i, src, srcp, dest, destp, sp->sk_state,
1010 		sk_wmem_alloc_get(sp),
1011 		sk_rmem_alloc_get(sp),
1012 		0, 0L, 0,
1013 		from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1014 		0, sock_i_ino(sp),
1015 		atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
1016 }
1017 
raw_seq_show(struct seq_file * seq,void * v)1018 static int raw_seq_show(struct seq_file *seq, void *v)
1019 {
1020 	if (v == SEQ_START_TOKEN)
1021 		seq_printf(seq, "  sl  local_address rem_address   st tx_queue "
1022 				"rx_queue tr tm->when retrnsmt   uid  timeout "
1023 				"inode ref pointer drops\n");
1024 	else
1025 		raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
1026 	return 0;
1027 }
1028 
1029 static const struct seq_operations raw_seq_ops = {
1030 	.start = raw_seq_start,
1031 	.next  = raw_seq_next,
1032 	.stop  = raw_seq_stop,
1033 	.show  = raw_seq_show,
1034 };
1035 
raw_seq_open(struct inode * ino,struct file * file,struct raw_hashinfo * h,const struct seq_operations * ops)1036 int raw_seq_open(struct inode *ino, struct file *file,
1037 		 struct raw_hashinfo *h, const struct seq_operations *ops)
1038 {
1039 	int err;
1040 	struct raw_iter_state *i;
1041 
1042 	err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
1043 	if (err < 0)
1044 		return err;
1045 
1046 	i = raw_seq_private((struct seq_file *)file->private_data);
1047 	i->h = h;
1048 	return 0;
1049 }
1050 EXPORT_SYMBOL_GPL(raw_seq_open);
1051 
raw_v4_seq_open(struct inode * inode,struct file * file)1052 static int raw_v4_seq_open(struct inode *inode, struct file *file)
1053 {
1054 	return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
1055 }
1056 
1057 static const struct file_operations raw_seq_fops = {
1058 	.owner	 = THIS_MODULE,
1059 	.open	 = raw_v4_seq_open,
1060 	.read	 = seq_read,
1061 	.llseek	 = seq_lseek,
1062 	.release = seq_release_net,
1063 };
1064 
raw_init_net(struct net * net)1065 static __net_init int raw_init_net(struct net *net)
1066 {
1067 	if (!proc_create("raw", S_IRUGO, net->proc_net, &raw_seq_fops))
1068 		return -ENOMEM;
1069 
1070 	return 0;
1071 }
1072 
raw_exit_net(struct net * net)1073 static __net_exit void raw_exit_net(struct net *net)
1074 {
1075 	remove_proc_entry("raw", net->proc_net);
1076 }
1077 
1078 static __net_initdata struct pernet_operations raw_net_ops = {
1079 	.init = raw_init_net,
1080 	.exit = raw_exit_net,
1081 };
1082 
raw_proc_init(void)1083 int __init raw_proc_init(void)
1084 {
1085 	return register_pernet_subsys(&raw_net_ops);
1086 }
1087 
raw_proc_exit(void)1088 void __init raw_proc_exit(void)
1089 {
1090 	unregister_pernet_subsys(&raw_net_ops);
1091 }
1092 #endif /* CONFIG_PROC_FS */
1093