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