18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * INET		An implementation of the TCP/IP protocol suite for the LINUX
48c2ecf20Sopenharmony_ci *		operating system.  INET is implemented using the  BSD Socket
58c2ecf20Sopenharmony_ci *		interface as the means of communication with the user level.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *		"Ping" sockets
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based on ipv4/ping.c code.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Authors:	Lorenzo Colitti (IPv6 support)
128c2ecf20Sopenharmony_ci *		Vasiliy Kulikov / Openwall (IPv4 implementation, for Linux 2.6),
138c2ecf20Sopenharmony_ci *		Pavel Kankovsky (IPv4 implementation, for Linux 2.4.32)
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <net/addrconf.h>
178c2ecf20Sopenharmony_ci#include <net/ipv6.h>
188c2ecf20Sopenharmony_ci#include <net/ip6_route.h>
198c2ecf20Sopenharmony_ci#include <net/protocol.h>
208c2ecf20Sopenharmony_ci#include <net/udp.h>
218c2ecf20Sopenharmony_ci#include <net/transp_v6.h>
228c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
238c2ecf20Sopenharmony_ci#include <net/ping.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Compatibility glue so we can support IPv6 when it's compiled as a module */
268c2ecf20Sopenharmony_cistatic int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
278c2ecf20Sopenharmony_ci				 int *addr_len)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	return -EAFNOSUPPORT;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_cistatic void dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
328c2ecf20Sopenharmony_ci				       struct sk_buff *skb)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_cistatic int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	return -EAFNOSUPPORT;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_cistatic void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
408c2ecf20Sopenharmony_ci				  __be16 port, u32 info, u8 *payload) {}
418c2ecf20Sopenharmony_cistatic int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
428c2ecf20Sopenharmony_ci			       const struct net_device *dev, int strict)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	return 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct inet_sock *inet = inet_sk(sk);
508c2ecf20Sopenharmony_ci	struct ipv6_pinfo *np = inet6_sk(sk);
518c2ecf20Sopenharmony_ci	struct icmp6hdr user_icmph;
528c2ecf20Sopenharmony_ci	int addr_type;
538c2ecf20Sopenharmony_ci	struct in6_addr *daddr;
548c2ecf20Sopenharmony_ci	int oif = 0;
558c2ecf20Sopenharmony_ci	struct flowi6 fl6;
568c2ecf20Sopenharmony_ci	int err;
578c2ecf20Sopenharmony_ci	struct dst_entry *dst;
588c2ecf20Sopenharmony_ci	struct rt6_info *rt;
598c2ecf20Sopenharmony_ci	struct pingfakehdr pfh;
608c2ecf20Sopenharmony_ci	struct ipcm6_cookie ipc6;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
658c2ecf20Sopenharmony_ci				  sizeof(user_icmph));
668c2ecf20Sopenharmony_ci	if (err)
678c2ecf20Sopenharmony_ci		return err;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (msg->msg_name) {
708c2ecf20Sopenharmony_ci		DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name);
718c2ecf20Sopenharmony_ci		if (msg->msg_namelen < sizeof(*u))
728c2ecf20Sopenharmony_ci			return -EINVAL;
738c2ecf20Sopenharmony_ci		if (u->sin6_family != AF_INET6) {
748c2ecf20Sopenharmony_ci			return -EAFNOSUPPORT;
758c2ecf20Sopenharmony_ci		}
768c2ecf20Sopenharmony_ci		daddr = &(u->sin6_addr);
778c2ecf20Sopenharmony_ci		if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr)))
788c2ecf20Sopenharmony_ci			oif = u->sin6_scope_id;
798c2ecf20Sopenharmony_ci	} else {
808c2ecf20Sopenharmony_ci		if (sk->sk_state != TCP_ESTABLISHED)
818c2ecf20Sopenharmony_ci			return -EDESTADDRREQ;
828c2ecf20Sopenharmony_ci		daddr = &sk->sk_v6_daddr;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (!oif)
868c2ecf20Sopenharmony_ci		oif = sk->sk_bound_dev_if;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (!oif)
898c2ecf20Sopenharmony_ci		oif = np->sticky_pktinfo.ipi6_ifindex;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (!oif && ipv6_addr_is_multicast(daddr))
928c2ecf20Sopenharmony_ci		oif = np->mcast_oif;
938c2ecf20Sopenharmony_ci	else if (!oif)
948c2ecf20Sopenharmony_ci		oif = np->ucast_oif;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	addr_type = ipv6_addr_type(daddr);
978c2ecf20Sopenharmony_ci	if ((__ipv6_addr_needs_scope_id(addr_type) && !oif) ||
988c2ecf20Sopenharmony_ci	    (addr_type & IPV6_ADDR_MAPPED) ||
998c2ecf20Sopenharmony_ci	    (oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if &&
1008c2ecf20Sopenharmony_ci	     l3mdev_master_ifindex_by_index(sock_net(sk), oif) != sk->sk_bound_dev_if))
1018c2ecf20Sopenharmony_ci		return -EINVAL;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* TODO: use ip6_datagram_send_ctl to get options from cmsg */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	memset(&fl6, 0, sizeof(fl6));
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	fl6.flowi6_proto = IPPROTO_ICMPV6;
1088c2ecf20Sopenharmony_ci	fl6.saddr = np->saddr;
1098c2ecf20Sopenharmony_ci	fl6.daddr = *daddr;
1108c2ecf20Sopenharmony_ci	fl6.flowi6_oif = oif;
1118c2ecf20Sopenharmony_ci	fl6.flowi6_mark = sk->sk_mark;
1128c2ecf20Sopenharmony_ci	fl6.flowi6_uid = sk->sk_uid;
1138c2ecf20Sopenharmony_ci	fl6.fl6_icmp_type = user_icmph.icmp6_type;
1148c2ecf20Sopenharmony_ci	fl6.fl6_icmp_code = user_icmph.icmp6_code;
1158c2ecf20Sopenharmony_ci	security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	ipcm6_init_sk(&ipc6, np);
1188c2ecf20Sopenharmony_ci	ipc6.sockc.mark = sk->sk_mark;
1198c2ecf20Sopenharmony_ci	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, false);
1228c2ecf20Sopenharmony_ci	if (IS_ERR(dst))
1238c2ecf20Sopenharmony_ci		return PTR_ERR(dst);
1248c2ecf20Sopenharmony_ci	rt = (struct rt6_info *) dst;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
1278c2ecf20Sopenharmony_ci		fl6.flowi6_oif = np->mcast_oif;
1288c2ecf20Sopenharmony_ci	else if (!fl6.flowi6_oif)
1298c2ecf20Sopenharmony_ci		fl6.flowi6_oif = np->ucast_oif;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	pfh.icmph.type = user_icmph.icmp6_type;
1328c2ecf20Sopenharmony_ci	pfh.icmph.code = user_icmph.icmp6_code;
1338c2ecf20Sopenharmony_ci	pfh.icmph.checksum = 0;
1348c2ecf20Sopenharmony_ci	pfh.icmph.un.echo.id = inet->inet_sport;
1358c2ecf20Sopenharmony_ci	pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
1368c2ecf20Sopenharmony_ci	pfh.msg = msg;
1378c2ecf20Sopenharmony_ci	pfh.wcheck = 0;
1388c2ecf20Sopenharmony_ci	pfh.family = AF_INET6;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	lock_sock(sk);
1438c2ecf20Sopenharmony_ci	err = ip6_append_data(sk, ping_getfrag, &pfh, len,
1448c2ecf20Sopenharmony_ci			      0, &ipc6, &fl6, rt,
1458c2ecf20Sopenharmony_ci			      MSG_DONTWAIT);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (err) {
1488c2ecf20Sopenharmony_ci		ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
1498c2ecf20Sopenharmony_ci				ICMP6_MIB_OUTERRORS);
1508c2ecf20Sopenharmony_ci		ip6_flush_pending_frames(sk);
1518c2ecf20Sopenharmony_ci	} else {
1528c2ecf20Sopenharmony_ci		icmpv6_push_pending_frames(sk, &fl6,
1538c2ecf20Sopenharmony_ci					   (struct icmp6hdr *)&pfh.icmph, len);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	release_sock(sk);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	dst_release(dst);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (err)
1608c2ecf20Sopenharmony_ci		return err;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return len;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistruct proto pingv6_prot = {
1668c2ecf20Sopenharmony_ci	.name =		"PINGv6",
1678c2ecf20Sopenharmony_ci	.owner =	THIS_MODULE,
1688c2ecf20Sopenharmony_ci	.init =		ping_init_sock,
1698c2ecf20Sopenharmony_ci	.close =	ping_close,
1708c2ecf20Sopenharmony_ci	.connect =	ip6_datagram_connect_v6_only,
1718c2ecf20Sopenharmony_ci	.disconnect =	__udp_disconnect,
1728c2ecf20Sopenharmony_ci	.setsockopt =	ipv6_setsockopt,
1738c2ecf20Sopenharmony_ci	.getsockopt =	ipv6_getsockopt,
1748c2ecf20Sopenharmony_ci	.sendmsg =	ping_v6_sendmsg,
1758c2ecf20Sopenharmony_ci	.recvmsg =	ping_recvmsg,
1768c2ecf20Sopenharmony_ci	.bind =		ping_bind,
1778c2ecf20Sopenharmony_ci	.backlog_rcv =	ping_queue_rcv_skb,
1788c2ecf20Sopenharmony_ci	.hash =		ping_hash,
1798c2ecf20Sopenharmony_ci	.unhash =	ping_unhash,
1808c2ecf20Sopenharmony_ci	.get_port =	ping_get_port,
1818c2ecf20Sopenharmony_ci	.obj_size =	sizeof(struct raw6_sock),
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pingv6_prot);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic struct inet_protosw pingv6_protosw = {
1868c2ecf20Sopenharmony_ci	.type =      SOCK_DGRAM,
1878c2ecf20Sopenharmony_ci	.protocol =  IPPROTO_ICMPV6,
1888c2ecf20Sopenharmony_ci	.prot =      &pingv6_prot,
1898c2ecf20Sopenharmony_ci	.ops =       &inet6_sockraw_ops,
1908c2ecf20Sopenharmony_ci	.flags =     INET_PROTOSW_REUSE,
1918c2ecf20Sopenharmony_ci};
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
1948c2ecf20Sopenharmony_cistatic void *ping_v6_seq_start(struct seq_file *seq, loff_t *pos)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	return ping_seq_start(seq, pos, AF_INET6);
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int ping_v6_seq_show(struct seq_file *seq, void *v)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	if (v == SEQ_START_TOKEN) {
2028c2ecf20Sopenharmony_ci		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
2038c2ecf20Sopenharmony_ci	} else {
2048c2ecf20Sopenharmony_ci		int bucket = ((struct ping_iter_state *) seq->private)->bucket;
2058c2ecf20Sopenharmony_ci		struct inet_sock *inet = inet_sk(v);
2068c2ecf20Sopenharmony_ci		__u16 srcp = ntohs(inet->inet_sport);
2078c2ecf20Sopenharmony_ci		__u16 destp = ntohs(inet->inet_dport);
2088c2ecf20Sopenharmony_ci		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci	return 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic const struct seq_operations ping_v6_seq_ops = {
2148c2ecf20Sopenharmony_ci	.start		= ping_v6_seq_start,
2158c2ecf20Sopenharmony_ci	.show		= ping_v6_seq_show,
2168c2ecf20Sopenharmony_ci	.next		= ping_seq_next,
2178c2ecf20Sopenharmony_ci	.stop		= ping_seq_stop,
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic int __net_init ping_v6_proc_init_net(struct net *net)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	if (!proc_create_net("icmp6", 0444, net->proc_net, &ping_v6_seq_ops,
2238c2ecf20Sopenharmony_ci			sizeof(struct ping_iter_state)))
2248c2ecf20Sopenharmony_ci		return -ENOMEM;
2258c2ecf20Sopenharmony_ci	return 0;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic void __net_exit ping_v6_proc_exit_net(struct net *net)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	remove_proc_entry("icmp6", net->proc_net);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic struct pernet_operations ping_v6_net_ops = {
2348c2ecf20Sopenharmony_ci	.init = ping_v6_proc_init_net,
2358c2ecf20Sopenharmony_ci	.exit = ping_v6_proc_exit_net,
2368c2ecf20Sopenharmony_ci};
2378c2ecf20Sopenharmony_ci#endif
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ciint __init pingv6_init(void)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
2428c2ecf20Sopenharmony_ci	int ret = register_pernet_subsys(&ping_v6_net_ops);
2438c2ecf20Sopenharmony_ci	if (ret)
2448c2ecf20Sopenharmony_ci		return ret;
2458c2ecf20Sopenharmony_ci#endif
2468c2ecf20Sopenharmony_ci	pingv6_ops.ipv6_recv_error = ipv6_recv_error;
2478c2ecf20Sopenharmony_ci	pingv6_ops.ip6_datagram_recv_common_ctl = ip6_datagram_recv_common_ctl;
2488c2ecf20Sopenharmony_ci	pingv6_ops.ip6_datagram_recv_specific_ctl =
2498c2ecf20Sopenharmony_ci		ip6_datagram_recv_specific_ctl;
2508c2ecf20Sopenharmony_ci	pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
2518c2ecf20Sopenharmony_ci	pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
2528c2ecf20Sopenharmony_ci	pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
2538c2ecf20Sopenharmony_ci	return inet6_register_protosw(&pingv6_protosw);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/* This never gets called because it's not possible to unload the ipv6 module,
2578c2ecf20Sopenharmony_ci * but just in case.
2588c2ecf20Sopenharmony_ci */
2598c2ecf20Sopenharmony_civoid pingv6_exit(void)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
2628c2ecf20Sopenharmony_ci	pingv6_ops.ip6_datagram_recv_common_ctl = dummy_ip6_datagram_recv_ctl;
2638c2ecf20Sopenharmony_ci	pingv6_ops.ip6_datagram_recv_specific_ctl = dummy_ip6_datagram_recv_ctl;
2648c2ecf20Sopenharmony_ci	pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
2658c2ecf20Sopenharmony_ci	pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
2668c2ecf20Sopenharmony_ci	pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
2678c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
2688c2ecf20Sopenharmony_ci	unregister_pernet_subsys(&ping_v6_net_ops);
2698c2ecf20Sopenharmony_ci#endif
2708c2ecf20Sopenharmony_ci	inet6_unregister_protosw(&pingv6_protosw);
2718c2ecf20Sopenharmony_ci}
272