xref: /linux/net/ipv6/ping.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
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  *		"Ping" sockets
8  *
9  * Based on ipv4/ping.c code.
10  *
11  * Authors:	Lorenzo Colitti (IPv6 support)
12  *		Vasiliy Kulikov / Openwall (IPv4 implementation, for Linux 2.6),
13  *		Pavel Kankovsky (IPv4 implementation, for Linux 2.4.32)
14  */
15 
16 #include <net/addrconf.h>
17 #include <net/ipv6.h>
18 #include <net/ip6_route.h>
19 #include <net/protocol.h>
20 #include <net/udp.h>
21 #include <net/transp_v6.h>
22 #include <linux/proc_fs.h>
23 #include <net/ping.h>
24 
25 static void ping_v6_destroy(struct sock *sk)
26 {
27 	inet6_destroy_sock(sk);
28 }
29 
30 /* Compatibility glue so we can support IPv6 when it's compiled as a module */
31 static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
32 				 int *addr_len)
33 {
34 	return -EAFNOSUPPORT;
35 }
36 static void dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
37 				       struct sk_buff *skb)
38 {
39 }
40 static int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
41 {
42 	return -EAFNOSUPPORT;
43 }
44 static void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
45 				  __be16 port, u32 info, u8 *payload) {}
46 static int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
47 			       const struct net_device *dev, int strict)
48 {
49 	return 0;
50 }
51 
52 static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
53 {
54 	struct inet_sock *inet = inet_sk(sk);
55 	struct ipv6_pinfo *np = inet6_sk(sk);
56 	struct icmp6hdr user_icmph;
57 	int addr_type;
58 	struct in6_addr *daddr;
59 	int oif = 0;
60 	struct flowi6 fl6;
61 	int err;
62 	struct dst_entry *dst;
63 	struct rt6_info *rt;
64 	struct pingfakehdr pfh;
65 	struct ipcm6_cookie ipc6;
66 
67 	err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
68 				  sizeof(user_icmph));
69 	if (err)
70 		return err;
71 
72 	memset(&fl6, 0, sizeof(fl6));
73 
74 	if (msg->msg_name) {
75 		DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name);
76 		if (msg->msg_namelen < sizeof(*u))
77 			return -EINVAL;
78 		if (u->sin6_family != AF_INET6) {
79 			return -EAFNOSUPPORT;
80 		}
81 		daddr = &(u->sin6_addr);
82 		if (np->sndflow)
83 			fl6.flowlabel = u->sin6_flowinfo & IPV6_FLOWINFO_MASK;
84 		if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr)))
85 			oif = u->sin6_scope_id;
86 	} else {
87 		if (sk->sk_state != TCP_ESTABLISHED)
88 			return -EDESTADDRREQ;
89 		daddr = &sk->sk_v6_daddr;
90 		fl6.flowlabel = np->flow_label;
91 	}
92 
93 	if (!oif)
94 		oif = sk->sk_bound_dev_if;
95 
96 	if (!oif)
97 		oif = np->sticky_pktinfo.ipi6_ifindex;
98 
99 	if (!oif && ipv6_addr_is_multicast(daddr))
100 		oif = np->mcast_oif;
101 	else if (!oif)
102 		oif = np->ucast_oif;
103 
104 	addr_type = ipv6_addr_type(daddr);
105 	if ((__ipv6_addr_needs_scope_id(addr_type) && !oif) ||
106 	    (addr_type & IPV6_ADDR_MAPPED) ||
107 	    (oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if))
108 		return -EINVAL;
109 
110 	ipcm6_init_sk(&ipc6, np);
111 	ipc6.sockc.tsflags = sk->sk_tsflags;
112 	ipc6.sockc.mark = sk->sk_mark;
113 
114 	fl6.flowi6_oif = oif;
115 
116 	if (msg->msg_controllen) {
117 		struct ipv6_txoptions opt = {};
118 
119 		opt.tot_len = sizeof(opt);
120 		ipc6.opt = &opt;
121 
122 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
123 		if (err < 0)
124 			return err;
125 
126 		/* Changes to txoptions and flow info are not implemented, yet.
127 		 * Drop the options.
128 		 */
129 		ipc6.opt = NULL;
130 	}
131 
132 	fl6.flowi6_proto = IPPROTO_ICMPV6;
133 	fl6.saddr = np->saddr;
134 	fl6.daddr = *daddr;
135 	fl6.flowi6_mark = ipc6.sockc.mark;
136 	fl6.flowi6_uid = sk->sk_uid;
137 	fl6.fl6_icmp_type = user_icmph.icmp6_type;
138 	fl6.fl6_icmp_code = user_icmph.icmp6_code;
139 	security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
140 
141 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
142 
143 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, false);
144 	if (IS_ERR(dst))
145 		return PTR_ERR(dst);
146 	rt = (struct rt6_info *) dst;
147 
148 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
149 		fl6.flowi6_oif = np->mcast_oif;
150 	else if (!fl6.flowi6_oif)
151 		fl6.flowi6_oif = np->ucast_oif;
152 
153 	pfh.icmph.type = user_icmph.icmp6_type;
154 	pfh.icmph.code = user_icmph.icmp6_code;
155 	pfh.icmph.checksum = 0;
156 	pfh.icmph.un.echo.id = inet->inet_sport;
157 	pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
158 	pfh.msg = msg;
159 	pfh.wcheck = 0;
160 	pfh.family = AF_INET6;
161 
162 	if (ipc6.hlimit < 0)
163 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
164 
165 	lock_sock(sk);
166 	err = ip6_append_data(sk, ping_getfrag, &pfh, len,
167 			      0, &ipc6, &fl6, rt,
168 			      MSG_DONTWAIT);
169 
170 	if (err) {
171 		ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
172 				ICMP6_MIB_OUTERRORS);
173 		ip6_flush_pending_frames(sk);
174 	} else {
175 		icmpv6_push_pending_frames(sk, &fl6,
176 					   (struct icmp6hdr *)&pfh.icmph, len);
177 	}
178 	release_sock(sk);
179 
180 	dst_release(dst);
181 
182 	if (err)
183 		return err;
184 
185 	return len;
186 }
187 
188 struct proto pingv6_prot = {
189 	.name =		"PINGv6",
190 	.owner =	THIS_MODULE,
191 	.init =		ping_init_sock,
192 	.close =	ping_close,
193 	.destroy =	ping_v6_destroy,
194 	.connect =	ip6_datagram_connect_v6_only,
195 	.disconnect =	__udp_disconnect,
196 	.setsockopt =	ipv6_setsockopt,
197 	.getsockopt =	ipv6_getsockopt,
198 	.sendmsg =	ping_v6_sendmsg,
199 	.recvmsg =	ping_recvmsg,
200 	.bind =		ping_bind,
201 	.backlog_rcv =	ping_queue_rcv_skb,
202 	.hash =		ping_hash,
203 	.unhash =	ping_unhash,
204 	.get_port =	ping_get_port,
205 	.put_port =	ping_unhash,
206 	.obj_size =	sizeof(struct raw6_sock),
207 };
208 EXPORT_SYMBOL_GPL(pingv6_prot);
209 
210 static struct inet_protosw pingv6_protosw = {
211 	.type =      SOCK_DGRAM,
212 	.protocol =  IPPROTO_ICMPV6,
213 	.prot =      &pingv6_prot,
214 	.ops =       &inet6_sockraw_ops,
215 	.flags =     INET_PROTOSW_REUSE,
216 };
217 
218 #ifdef CONFIG_PROC_FS
219 static void *ping_v6_seq_start(struct seq_file *seq, loff_t *pos)
220 {
221 	return ping_seq_start(seq, pos, AF_INET6);
222 }
223 
224 static int ping_v6_seq_show(struct seq_file *seq, void *v)
225 {
226 	if (v == SEQ_START_TOKEN) {
227 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
228 	} else {
229 		int bucket = ((struct ping_iter_state *) seq->private)->bucket;
230 		struct inet_sock *inet = inet_sk(v);
231 		__u16 srcp = ntohs(inet->inet_sport);
232 		__u16 destp = ntohs(inet->inet_dport);
233 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
234 	}
235 	return 0;
236 }
237 
238 static const struct seq_operations ping_v6_seq_ops = {
239 	.start		= ping_v6_seq_start,
240 	.show		= ping_v6_seq_show,
241 	.next		= ping_seq_next,
242 	.stop		= ping_seq_stop,
243 };
244 
245 static int __net_init ping_v6_proc_init_net(struct net *net)
246 {
247 	if (!proc_create_net("icmp6", 0444, net->proc_net, &ping_v6_seq_ops,
248 			sizeof(struct ping_iter_state)))
249 		return -ENOMEM;
250 	return 0;
251 }
252 
253 static void __net_exit ping_v6_proc_exit_net(struct net *net)
254 {
255 	remove_proc_entry("icmp6", net->proc_net);
256 }
257 
258 static struct pernet_operations ping_v6_net_ops = {
259 	.init = ping_v6_proc_init_net,
260 	.exit = ping_v6_proc_exit_net,
261 };
262 #endif
263 
264 int __init pingv6_init(void)
265 {
266 #ifdef CONFIG_PROC_FS
267 	int ret = register_pernet_subsys(&ping_v6_net_ops);
268 	if (ret)
269 		return ret;
270 #endif
271 	pingv6_ops.ipv6_recv_error = ipv6_recv_error;
272 	pingv6_ops.ip6_datagram_recv_common_ctl = ip6_datagram_recv_common_ctl;
273 	pingv6_ops.ip6_datagram_recv_specific_ctl =
274 		ip6_datagram_recv_specific_ctl;
275 	pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
276 	pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
277 	pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
278 	return inet6_register_protosw(&pingv6_protosw);
279 }
280 
281 /* This never gets called because it's not possible to unload the ipv6 module,
282  * but just in case.
283  */
284 void pingv6_exit(void)
285 {
286 	pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
287 	pingv6_ops.ip6_datagram_recv_common_ctl = dummy_ip6_datagram_recv_ctl;
288 	pingv6_ops.ip6_datagram_recv_specific_ctl = dummy_ip6_datagram_recv_ctl;
289 	pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
290 	pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
291 	pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
292 #ifdef CONFIG_PROC_FS
293 	unregister_pernet_subsys(&ping_v6_net_ops);
294 #endif
295 	inet6_unregister_protosw(&pingv6_protosw);
296 }
297