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.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * Based on ipv4/udp.c code.
15 * Authors: Vasiliy Kulikov / Openwall (for Linux 2.6),
16 * Pavel Kankovsky (for Linux 2.4.32)
18 * Pavel gave all rights to bugs to Vasiliy,
19 * none of the bugs are Pavel's now.
23 #include <asm/system.h>
24 #include <linux/uaccess.h>
25 #include <asm/ioctls.h>
26 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sockios.h>
31 #include <linux/errno.h>
32 #include <linux/timer.h>
34 #include <linux/inet.h>
35 #include <linux/netdevice.h>
40 #include <net/protocol.h>
41 #include <linux/skbuff.h>
42 #include <linux/proc_fs.h>
47 #include <net/route.h>
48 #include <net/inet_common.h>
49 #include <net/checksum.h>
52 struct ping_table ping_table __read_mostly;
56 static inline int ping_hashfn(struct net *net, unsigned num, unsigned mask)
58 int res = (num + net_hash_mix(net)) & mask;
59 pr_debug("hash(%d) = %d\n", num, res);
63 static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
64 struct net *net, unsigned num)
66 return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
69 static int ping_v4_get_port(struct sock *sk, unsigned short ident)
71 struct hlist_nulls_node *node;
72 struct hlist_nulls_head *hlist;
73 struct inet_sock *isk, *isk2;
74 struct sock *sk2 = NULL;
77 write_lock_bh(&ping_table.lock);
80 u16 result = ping_port_rover + 1;
82 for (i = 0; i < (1L << 16); i++, result++) {
84 result++; /* avoid zero */
85 hlist = ping_hashslot(&ping_table, sock_net(sk),
87 ping_portaddr_for_each_entry(sk2, node, hlist) {
90 if (isk2->inet_num == result)
95 ping_port_rover = ident = result;
103 hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
104 ping_portaddr_for_each_entry(sk2, node, hlist) {
107 if ((isk2->inet_num == ident) &&
109 (!sk2->sk_reuse || !sk->sk_reuse))
114 pr_debug("found port/ident = %d\n", ident);
115 isk->inet_num = ident;
116 if (sk_unhashed(sk)) {
117 pr_debug("was not hashed\n");
119 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
120 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
122 write_unlock_bh(&ping_table.lock);
126 write_unlock_bh(&ping_table.lock);
130 static void ping_v4_hash(struct sock *sk)
132 pr_debug("ping_v4_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
133 BUG(); /* "Please do not press this button again." */
136 static void ping_v4_unhash(struct sock *sk)
138 struct inet_sock *isk = inet_sk(sk);
139 pr_debug("ping_v4_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
141 struct hlist_nulls_head *hslot;
143 hslot = ping_hashslot(&ping_table, sock_net(sk), isk->inet_num);
144 write_lock_bh(&ping_table.lock);
145 hlist_nulls_del(&sk->sk_nulls_node);
147 isk->inet_num = isk->inet_sport = 0;
148 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
149 write_unlock_bh(&ping_table.lock);
153 struct sock *ping_v4_lookup(struct net *net, u32 saddr, u32 daddr,
156 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
157 struct sock *sk = NULL;
158 struct inet_sock *isk;
159 struct hlist_nulls_node *hnode;
161 pr_debug("try to find: num = %d, daddr = %ld, dif = %d\n",
162 (int)ident, (unsigned long)daddr, dif);
163 read_lock_bh(&ping_table.lock);
165 ping_portaddr_for_each_entry(sk, hnode, hslot) {
168 pr_debug("found: %p: num = %d, daddr = %ld, dif = %d\n", sk,
169 (int)isk->inet_num, (unsigned long)isk->inet_rcv_saddr,
170 sk->sk_bound_dev_if);
172 pr_debug("iterate\n");
173 if (isk->inet_num != ident)
175 if (isk->inet_rcv_saddr && isk->inet_rcv_saddr != daddr)
177 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
186 read_unlock_bh(&ping_table.lock);
191 static int ping_init_sock(struct sock *sk)
193 struct net *net = sock_net(sk);
194 gid_t group = current_egid();
196 struct group_info *group_info = get_current_groups();
197 int i, j, count = group_info->ngroups;
199 inet_get_ping_group_range_net(net, range, range+1);
200 if (range[0] <= group && group <= range[1])
203 for (i = 0; i < group_info->nblocks; i++) {
204 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
206 for (j = 0; j < cp_count; j++) {
207 group = group_info->blocks[i][j];
208 if (range[0] <= group && group <= range[1])
218 static void ping_close(struct sock *sk, long timeout)
220 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
221 inet_sk(sk), inet_sk(sk)->inet_num);
222 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
224 sk_common_release(sk);
228 * We need our own bind because there are no privileged id's == local ports.
229 * Moreover, we don't allow binding to multi- and broadcast addresses.
232 static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
234 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
235 struct inet_sock *isk = inet_sk(sk);
240 if (addr_len < sizeof(struct sockaddr_in))
243 pr_debug("ping_v4_bind(sk=%p,sa_addr=%08x,sa_port=%d)\n",
244 sk, addr->sin_addr.s_addr, ntohs(addr->sin_port));
246 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
247 if (addr->sin_addr.s_addr == INADDR_ANY)
248 chk_addr_ret = RTN_LOCAL;
250 if ((sysctl_ip_nonlocal_bind == 0 &&
251 isk->freebind == 0 && isk->transparent == 0 &&
252 chk_addr_ret != RTN_LOCAL) ||
253 chk_addr_ret == RTN_MULTICAST ||
254 chk_addr_ret == RTN_BROADCAST)
255 return -EADDRNOTAVAIL;
260 if (isk->inet_num != 0)
264 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
265 snum = ntohs(addr->sin_port);
266 if (ping_v4_get_port(sk, snum) != 0) {
267 isk->inet_saddr = isk->inet_rcv_saddr = 0;
271 pr_debug("after bind(): num = %d, daddr = %ld, dif = %d\n",
273 (unsigned long) isk->inet_rcv_saddr,
274 (int)sk->sk_bound_dev_if);
277 if (isk->inet_rcv_saddr)
278 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
280 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
281 isk->inet_sport = htons(isk->inet_num);
287 pr_debug("ping_v4_bind -> %d\n", err);
292 * Is this a supported type of ICMP message?
295 static inline int ping_supported(int type, int code)
297 if (type == ICMP_ECHO && code == 0)
303 * This routine is called by the ICMP module when it gets some
304 * sort of error condition.
307 static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
309 void ping_err(struct sk_buff *skb, u32 info)
311 struct iphdr *iph = (struct iphdr *)skb->data;
312 struct icmphdr *icmph = (struct icmphdr *)(skb->data+(iph->ihl<<2));
313 struct inet_sock *inet_sock;
314 int type = icmph->type;
315 int code = icmph->code;
316 struct net *net = dev_net(skb->dev);
321 /* We assume the packet has already been checked by icmp_unreach */
323 if (!ping_supported(icmph->type, icmph->code))
326 pr_debug("ping_err(type=%04x,code=%04x,id=%04x,seq=%04x)\n", type,
327 code, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
329 sk = ping_v4_lookup(net, iph->daddr, iph->saddr,
330 ntohs(icmph->un.echo.id), skb->dev->ifindex);
332 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
333 pr_debug("no socket, dropping\n");
334 return; /* No socket for error */
336 pr_debug("err on socket %p\n", sk);
340 inet_sock = inet_sk(sk);
344 case ICMP_TIME_EXCEEDED:
347 case ICMP_SOURCE_QUENCH:
348 /* This is not a real error but ping wants to see it.
349 * Report it with some fake errno. */
352 case ICMP_PARAMETERPROB:
356 case ICMP_DEST_UNREACH:
357 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
358 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
366 if (code <= NR_ICMP_UNREACH) {
367 harderr = icmp_err_convert[code].fatal;
368 err = icmp_err_convert[code].errno;
372 /* See ICMP_SOURCE_QUENCH */
378 * RFC1122: OK. Passes ICMP errors back to application, as per
381 if (!inet_sock->recverr) {
382 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
385 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
389 sk->sk_error_report(sk);
395 * Copy and checksum an ICMP Echo packet from user space into a buffer.
399 struct icmphdr icmph;
404 static int ping_getfrag(void *from, char * to,
405 int offset, int fraglen, int odd, struct sk_buff *skb)
407 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
410 if (fraglen < sizeof(struct icmphdr))
412 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
413 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
419 if (offset < sizeof(struct icmphdr))
421 if (csum_partial_copy_fromiovecend
422 (to, pfh->iov, offset - sizeof(struct icmphdr),
423 fraglen, &pfh->wcheck))
428 static int ping_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh, struct flowi4 *fl4)
430 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
432 pfh->wcheck = csum_partial((char *)&pfh->icmph,
433 sizeof(struct icmphdr), pfh->wcheck);
434 pfh->icmph.checksum = csum_fold(pfh->wcheck);
435 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
436 skb->ip_summed = CHECKSUM_NONE;
437 return ip_push_pending_frames(sk, fl4);
440 int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
443 struct net *net = sock_net(sk);
445 struct inet_sock *inet = inet_sk(sk);
446 struct ipcm_cookie ipc;
447 struct icmphdr user_icmph;
448 struct pingfakehdr pfh;
449 struct rtable *rt = NULL;
450 struct ip_options_data opt_copy;
452 u32 saddr, daddr, faddr;
456 pr_debug("ping_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
466 /* Mirror BSD error message compatibility */
467 if (msg->msg_flags & MSG_OOB)
471 * Fetch the ICMP header provided by the userland.
475 if (memcpy_fromiovec((u8 *)&user_icmph, msg->msg_iov,
476 sizeof(struct icmphdr)))
478 if (!ping_supported(user_icmph.type, user_icmph.code))
482 * Get and verify the address.
486 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
487 if (msg->msg_namelen < sizeof(*usin))
489 if (usin->sin_family != AF_INET)
491 daddr = usin->sin_addr.s_addr;
494 if (sk->sk_state != TCP_ESTABLISHED)
495 return -EDESTADDRREQ;
496 daddr = inet->inet_daddr;
500 ipc.addr = inet->inet_saddr;
502 ipc.oif = sk->sk_bound_dev_if;
504 err = sock_tx_timestamp(sk, &ipc.tx_flags);
508 if (msg->msg_controllen) {
509 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
516 struct ip_options_rcu *inet_opt;
519 inet_opt = rcu_dereference(inet->inet_opt);
521 memcpy(&opt_copy, inet_opt,
522 sizeof(*inet_opt) + inet_opt->opt.optlen);
523 ipc.opt = &opt_copy.opt;
529 ipc.addr = faddr = daddr;
531 if (ipc.opt && ipc.opt->opt.srr) {
534 faddr = ipc.opt->opt.faddr;
536 tos = RT_TOS(inet->tos);
537 if (sock_flag(sk, SOCK_LOCALROUTE) ||
538 (msg->msg_flags & MSG_DONTROUTE) ||
539 (ipc.opt && ipc.opt->opt.is_strictroute)) {
543 if (ipv4_is_multicast(daddr)) {
545 ipc.oif = inet->mc_index;
547 saddr = inet->mc_addr;
550 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
551 RT_SCOPE_UNIVERSE, sk->sk_protocol,
552 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
554 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
555 rt = ip_route_output_flow(net, &fl4, sk);
559 if (err == -ENETUNREACH)
560 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
565 if ((rt->rt_flags & RTCF_BROADCAST) &&
566 !sock_flag(sk, SOCK_BROADCAST))
569 if (msg->msg_flags & MSG_CONFIRM)
574 ipc.addr = fl4.daddr;
578 pfh.icmph.type = user_icmph.type; /* already checked */
579 pfh.icmph.code = user_icmph.code; /* ditto */
580 pfh.icmph.checksum = 0;
581 pfh.icmph.un.echo.id = inet->inet_sport;
582 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
583 pfh.iov = msg->msg_iov;
586 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
587 0, &ipc, &rt, msg->msg_flags);
589 ip_flush_pending_frames(sk);
591 err = ping_push_pending_frames(sk, &pfh, &fl4);
599 icmp_out_count(sock_net(sk), user_icmph.type);
605 dst_confirm(&rt->dst);
606 if (!(msg->msg_flags & MSG_PROBE) || len)
607 goto back_from_confirm;
613 * IOCTL requests applicable to the UDP^H^H^HICMP protocol
616 int ping_ioctl(struct sock *sk, int cmd, unsigned long arg)
618 pr_debug("ping_ioctl(sk=%p,sk->num=%u,cmd=%d,arg=%lu)\n",
619 inet_sk(sk), inet_sk(sk)->inet_num, cmd, arg);
623 return udp_ioctl(sk, cmd, arg);
629 int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
630 size_t len, int noblock, int flags, int *addr_len)
632 struct inet_sock *isk = inet_sk(sk);
633 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
637 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
643 *addr_len = sizeof(*sin);
645 if (flags & MSG_ERRQUEUE)
646 return ip_recv_error(sk, msg, len);
648 skb = skb_recv_datagram(sk, flags, noblock, &err);
654 msg->msg_flags |= MSG_TRUNC;
658 /* Don't bother checking the checksum */
659 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
663 sock_recv_timestamp(msg, sk, skb);
665 /* Copy the address. */
667 sin->sin_family = AF_INET;
668 sin->sin_port = 0 /* skb->h.uh->source */;
669 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
670 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
673 ip_cmsg_recv(msg, skb);
677 skb_free_datagram(sk, skb);
679 pr_debug("ping_recvmsg -> %d\n", err);
683 static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
685 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
686 inet_sk(sk), inet_sk(sk)->inet_num, skb);
687 if (sock_queue_rcv_skb(sk, skb) < 0) {
688 ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_INERRORS);
690 pr_debug("ping_queue_rcv_skb -> failed\n");
698 * All we need to do is get the socket.
701 void ping_rcv(struct sk_buff *skb)
704 struct net *net = dev_net(skb->dev);
705 struct iphdr *iph = ip_hdr(skb);
706 struct icmphdr *icmph = icmp_hdr(skb);
707 u32 saddr = iph->saddr;
708 u32 daddr = iph->daddr;
710 /* We assume the packet has already been checked by icmp_rcv */
712 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
713 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
715 /* Push ICMP header back */
716 skb_push(skb, skb->data - (u8 *)icmph);
718 sk = ping_v4_lookup(net, saddr, daddr, ntohs(icmph->un.echo.id),
721 pr_debug("rcv on socket %p\n", sk);
722 ping_queue_rcv_skb(sk, skb_get(skb));
726 pr_debug("no socket, dropping\n");
728 /* We're called from icmp_rcv(). kfree_skb() is done there. */
731 struct proto ping_prot = {
733 .owner = THIS_MODULE,
734 .init = ping_init_sock,
736 .connect = ip4_datagram_connect,
737 .disconnect = udp_disconnect,
739 .setsockopt = ip_setsockopt,
740 .getsockopt = ip_getsockopt,
741 .sendmsg = ping_sendmsg,
742 .recvmsg = ping_recvmsg,
744 .backlog_rcv = ping_queue_rcv_skb,
745 .hash = ping_v4_hash,
746 .unhash = ping_v4_unhash,
747 .get_port = ping_v4_get_port,
748 .obj_size = sizeof(struct inet_sock),
750 EXPORT_SYMBOL(ping_prot);
752 #ifdef CONFIG_PROC_FS
754 static struct sock *ping_get_first(struct seq_file *seq, int start)
757 struct ping_iter_state *state = seq->private;
758 struct net *net = seq_file_net(seq);
760 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
762 struct hlist_nulls_node *node;
763 struct hlist_nulls_head *hslot = &ping_table.hash[state->bucket];
765 if (hlist_nulls_empty(hslot))
768 sk_nulls_for_each(sk, node, hslot) {
769 if (net_eq(sock_net(sk), net))
778 static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
780 struct ping_iter_state *state = seq->private;
781 struct net *net = seq_file_net(seq);
784 sk = sk_nulls_next(sk);
785 } while (sk && (!net_eq(sock_net(sk), net)));
788 return ping_get_first(seq, state->bucket + 1);
792 static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
794 struct sock *sk = ping_get_first(seq, 0);
797 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
799 return pos ? NULL : sk;
802 static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
804 struct ping_iter_state *state = seq->private;
807 read_lock_bh(&ping_table.lock);
809 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
812 static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
816 if (v == SEQ_START_TOKEN)
817 sk = ping_get_idx(seq, 0);
819 sk = ping_get_next(seq, v);
825 static void ping_seq_stop(struct seq_file *seq, void *v)
827 read_unlock_bh(&ping_table.lock);
830 static void ping_format_sock(struct sock *sp, struct seq_file *f,
831 int bucket, int *len)
833 struct inet_sock *inet = inet_sk(sp);
834 __be32 dest = inet->inet_daddr;
835 __be32 src = inet->inet_rcv_saddr;
836 __u16 destp = ntohs(inet->inet_dport);
837 __u16 srcp = ntohs(inet->inet_sport);
839 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
840 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
841 bucket, src, srcp, dest, destp, sp->sk_state,
842 sk_wmem_alloc_get(sp),
843 sk_rmem_alloc_get(sp),
844 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
845 atomic_read(&sp->sk_refcnt), sp,
846 atomic_read(&sp->sk_drops), len);
849 static int ping_seq_show(struct seq_file *seq, void *v)
851 if (v == SEQ_START_TOKEN)
852 seq_printf(seq, "%-127s\n",
853 " sl local_address rem_address st tx_queue "
854 "rx_queue tr tm->when retrnsmt uid timeout "
855 "inode ref pointer drops");
857 struct ping_iter_state *state = seq->private;
860 ping_format_sock(v, seq, state->bucket, &len);
861 seq_printf(seq, "%*s\n", 127 - len, "");
866 static const struct seq_operations ping_seq_ops = {
867 .show = ping_seq_show,
868 .start = ping_seq_start,
869 .next = ping_seq_next,
870 .stop = ping_seq_stop,
873 static int ping_seq_open(struct inode *inode, struct file *file)
875 return seq_open_net(inode, file, &ping_seq_ops,
876 sizeof(struct ping_iter_state));
879 static const struct file_operations ping_seq_fops = {
880 .open = ping_seq_open,
883 .release = seq_release_net,
886 static int ping_proc_register(struct net *net)
888 struct proc_dir_entry *p;
891 p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
897 static void ping_proc_unregister(struct net *net)
899 proc_net_remove(net, "icmp");
903 static int __net_init ping_proc_init_net(struct net *net)
905 return ping_proc_register(net);
908 static void __net_exit ping_proc_exit_net(struct net *net)
910 ping_proc_unregister(net);
913 static struct pernet_operations ping_net_ops = {
914 .init = ping_proc_init_net,
915 .exit = ping_proc_exit_net,
918 int __init ping_proc_init(void)
920 return register_pernet_subsys(&ping_net_ops);
923 void ping_proc_exit(void)
925 unregister_pernet_subsys(&ping_net_ops);
930 void __init ping_init(void)
934 for (i = 0; i < PING_HTABLE_SIZE; i++)
935 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
936 rwlock_init(&ping_table.lock);