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