Blame view

net/ipv6/ping.c 7.13 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
2
3
4
5
6
7
8
  /*
   * INET		An implementation of the TCP/IP protocol suite for the LINUX
   *		operating system.  INET is implemented using the  BSD Socket
   *		interface as the means of communication with the user level.
   *
   *		"Ping" sockets
   *
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
9
10
11
12
13
   * Based on ipv4/ping.c code.
   *
   * Authors:	Lorenzo Colitti (IPv6 support)
   *		Vasiliy Kulikov / Openwall (IPv4 implementation, for Linux 2.6),
   *		Pavel Kankovsky (IPv4 implementation, for Linux 2.4.32)
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
14
15
16
17
18
19
20
21
   */
  
  #include <net/addrconf.h>
  #include <net/ipv6.h>
  #include <net/ip6_route.h>
  #include <net/protocol.h>
  #include <net/udp.h>
  #include <net/transp_v6.h>
f45502216   Christoph Hellwig   ipv{4,6}/ping: si...
22
  #include <linux/proc_fs.h>
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
23
  #include <net/ping.h>
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
24
  /* Compatibility glue so we can support IPv6 when it's compiled as a module */
85fbaa750   Hannes Frederic Sowa   inet: fix addr_le...
25
26
  static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
  				 int *addr_len)
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
27
28
29
  {
  	return -EAFNOSUPPORT;
  }
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
30
  static void dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
a06a2d378   Wu Fengguang   net: ping_check_b...
31
  				       struct sk_buff *skb)
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
32
  {
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
33
  }
a06a2d378   Wu Fengguang   net: ping_check_b...
34
  static int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
35
36
37
  {
  	return -EAFNOSUPPORT;
  }
a06a2d378   Wu Fengguang   net: ping_check_b...
38
39
40
41
  static void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  				  __be16 port, u32 info, u8 *payload) {}
  static int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
  			       const struct net_device *dev, int strict)
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
42
43
44
  {
  	return 0;
  }
6579a023a   Haishuang Yan   net: ping: make p...
45
  static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
46
47
48
49
50
51
  {
  	struct inet_sock *inet = inet_sk(sk);
  	struct ipv6_pinfo *np = inet6_sk(sk);
  	struct icmp6hdr user_icmph;
  	int addr_type;
  	struct in6_addr *daddr;
5e4578969   Lorenzo Colitti   net: ipv6: Fix pi...
52
  	int oif = 0;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
53
54
  	struct flowi6 fl6;
  	int err;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
55
56
57
  	struct dst_entry *dst;
  	struct rt6_info *rt;
  	struct pingfakehdr pfh;
26879da58   Wei Wang   ipv6: add new str...
58
  	struct ipcm6_cookie ipc6;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
59
60
61
62
63
64
65
66
67
68
  
  	pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)
  ", inet, inet->inet_num);
  
  	err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
  				  sizeof(user_icmph));
  	if (err)
  		return err;
  
  	if (msg->msg_name) {
342dfc306   Steffen Hurrle   net: add build-ti...
69
  		DECLARE_SOCKADDR(struct sockaddr_in6 *, u, msg->msg_name);
9145736d4   Lorenzo Colitti   net: ping: Return...
70
  		if (msg->msg_namelen < sizeof(*u))
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
71
  			return -EINVAL;
9145736d4   Lorenzo Colitti   net: ping: Return...
72
73
  		if (u->sin6_family != AF_INET6) {
  			return -EAFNOSUPPORT;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
74
  		}
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
75
  		daddr = &(u->sin6_addr);
5e4578969   Lorenzo Colitti   net: ipv6: Fix pi...
76
77
  		if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr)))
  			oif = u->sin6_scope_id;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
78
79
80
  	} else {
  		if (sk->sk_state != TCP_ESTABLISHED)
  			return -EDESTADDRREQ;
efe4208f4   Eric Dumazet   ipv6: make lookup...
81
  		daddr = &sk->sk_v6_daddr;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
82
  	}
5e4578969   Lorenzo Colitti   net: ipv6: Fix pi...
83
84
85
86
87
88
89
90
91
92
  	if (!oif)
  		oif = sk->sk_bound_dev_if;
  
  	if (!oif)
  		oif = np->sticky_pktinfo.ipi6_ifindex;
  
  	if (!oif && ipv6_addr_is_multicast(daddr))
  		oif = np->mcast_oif;
  	else if (!oif)
  		oif = np->ucast_oif;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
93
94
  
  	addr_type = ipv6_addr_type(daddr);
5e4578969   Lorenzo Colitti   net: ipv6: Fix pi...
95
96
97
  	if ((__ipv6_addr_needs_scope_id(addr_type) && !oif) ||
  	    (addr_type & IPV6_ADDR_MAPPED) ||
  	    (oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if))
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
98
99
100
101
102
103
104
105
106
  		return -EINVAL;
  
  	/* TODO: use ip6_datagram_send_ctl to get options from cmsg */
  
  	memset(&fl6, 0, sizeof(fl6));
  
  	fl6.flowi6_proto = IPPROTO_ICMPV6;
  	fl6.saddr = np->saddr;
  	fl6.daddr = *daddr;
5e4578969   Lorenzo Colitti   net: ipv6: Fix pi...
107
  	fl6.flowi6_oif = oif;
bf439b315   Lorenzo Colitti   net: ipv6: ping: ...
108
  	fl6.flowi6_mark = sk->sk_mark;
e2d118a1c   Lorenzo Colitti   net: inet: Suppor...
109
  	fl6.flowi6_uid = sk->sk_uid;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
110
111
112
  	fl6.fl6_icmp_type = user_icmph.icmp6_type;
  	fl6.fl6_icmp_code = user_icmph.icmp6_code;
  	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
b515430ac   Willem de Bruijn   ipv6: ipcm6_cooki...
113
  	ipcm6_init_sk(&ipc6, np);
cd8700e45   Willem de Bruijn   ipv6/ping: set sk...
114
  	ipc6.sockc.mark = sk->sk_mark;
38b7097b5   Hannes Frederic Sowa   ipv6: use TOS mar...
115
  	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
96818159c   Alexey Kodanev   ipv6: allow to ca...
116
  	dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, false);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
117
118
119
  	if (IS_ERR(dst))
  		return PTR_ERR(dst);
  	rt = (struct rt6_info *) dst;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
120
121
122
123
124
125
126
127
128
129
  	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  		fl6.flowi6_oif = np->mcast_oif;
  	else if (!fl6.flowi6_oif)
  		fl6.flowi6_oif = np->ucast_oif;
  
  	pfh.icmph.type = user_icmph.icmp6_type;
  	pfh.icmph.code = user_icmph.icmp6_code;
  	pfh.icmph.checksum = 0;
  	pfh.icmph.un.echo.id = inet->inet_sport;
  	pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
cacdc7d2f   Al Viro   ip: stash a point...
130
  	pfh.msg = msg;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
131
132
  	pfh.wcheck = 0;
  	pfh.family = AF_INET6;
26879da58   Wei Wang   ipv6: add new str...
133
  	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
134

a1bdc4558   Lorenzo Colitti   net: ipv6: add mi...
135
  	lock_sock(sk);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
136
  	err = ip6_append_data(sk, ping_getfrag, &pfh, len,
26879da58   Wei Wang   ipv6: add new str...
137
  			      0, &ipc6, &fl6, rt,
5fdaa88df   Willem de Bruijn   ipv6: fold sockcm...
138
  			      MSG_DONTWAIT);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
139
140
  
  	if (err) {
43a43b604   Hannes Frederic Sowa   ipv6: some ipv6 s...
141
142
  		ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
  				ICMP6_MIB_OUTERRORS);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
143
144
  		ip6_flush_pending_frames(sk);
  	} else {
4e64b1ed1   Joe Perches   net/ipv6: Convert...
145
146
  		icmpv6_push_pending_frames(sk, &fl6,
  					   (struct icmp6hdr *)&pfh.icmph, len);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
147
  	}
a1bdc4558   Lorenzo Colitti   net: ipv6: add mi...
148
  	release_sock(sk);
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
149

03c2778a9   Dave Jones   ipv6: release dst...
150
  	dst_release(dst);
fbfe80c89   Lorenzo Colitti   net: ipv6: fix wr...
151
152
153
154
  	if (err)
  		return err;
  
  	return len;
6d0bfe226   Lorenzo Colitti   net: ipv6: Add IP...
155
  }
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
156

6579a023a   Haishuang Yan   net: ping: make p...
157
158
159
160
161
162
  struct proto pingv6_prot = {
  	.name =		"PINGv6",
  	.owner =	THIS_MODULE,
  	.init =		ping_init_sock,
  	.close =	ping_close,
  	.connect =	ip6_datagram_connect_v6_only,
286c72dea   Eric Dumazet   udp: must lock th...
163
  	.disconnect =	__udp_disconnect,
6579a023a   Haishuang Yan   net: ping: make p...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  	.setsockopt =	ipv6_setsockopt,
  	.getsockopt =	ipv6_getsockopt,
  	.sendmsg =	ping_v6_sendmsg,
  	.recvmsg =	ping_recvmsg,
  	.bind =		ping_bind,
  	.backlog_rcv =	ping_queue_rcv_skb,
  	.hash =		ping_hash,
  	.unhash =	ping_unhash,
  	.get_port =	ping_get_port,
  	.obj_size =	sizeof(struct raw6_sock),
  };
  EXPORT_SYMBOL_GPL(pingv6_prot);
  
  static struct inet_protosw pingv6_protosw = {
  	.type =      SOCK_DGRAM,
  	.protocol =  IPPROTO_ICMPV6,
  	.prot =      &pingv6_prot,
77d4b1d36   Eric Dumazet   net: ping: do not...
181
  	.ops =       &inet6_sockraw_ops,
6579a023a   Haishuang Yan   net: ping: make p...
182
183
  	.flags =     INET_PROTOSW_REUSE,
  };
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
184
185
186
187
188
  #ifdef CONFIG_PROC_FS
  static void *ping_v6_seq_start(struct seq_file *seq, loff_t *pos)
  {
  	return ping_seq_start(seq, pos, AF_INET6);
  }
a06a2d378   Wu Fengguang   net: ping_check_b...
189
  static int ping_v6_seq_show(struct seq_file *seq, void *v)
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
190
191
192
193
194
195
196
197
198
199
200
201
  {
  	if (v == SEQ_START_TOKEN) {
  		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
  	} else {
  		int bucket = ((struct ping_iter_state *) seq->private)->bucket;
  		struct inet_sock *inet = inet_sk(v);
  		__u16 srcp = ntohs(inet->inet_sport);
  		__u16 destp = ntohs(inet->inet_dport);
  		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
  	}
  	return 0;
  }
f45502216   Christoph Hellwig   ipv{4,6}/ping: si...
202
203
204
205
206
207
  static const struct seq_operations ping_v6_seq_ops = {
  	.start		= ping_v6_seq_start,
  	.show		= ping_v6_seq_show,
  	.next		= ping_seq_next,
  	.stop		= ping_seq_stop,
  };
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
208
209
  static int __net_init ping_v6_proc_init_net(struct net *net)
  {
c35063722   Christoph Hellwig   proc: introduce p...
210
211
  	if (!proc_create_net("icmp6", 0444, net->proc_net, &ping_v6_seq_ops,
  			sizeof(struct ping_iter_state)))
f45502216   Christoph Hellwig   ipv{4,6}/ping: si...
212
213
  		return -ENOMEM;
  	return 0;
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
214
  }
d23dbc479   Christophe JAILLET   ipv6: Fix the lin...
215
  static void __net_exit ping_v6_proc_exit_net(struct net *net)
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
216
  {
f45502216   Christoph Hellwig   ipv{4,6}/ping: si...
217
  	remove_proc_entry("icmp6", net->proc_net);
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  }
  
  static struct pernet_operations ping_v6_net_ops = {
  	.init = ping_v6_proc_init_net,
  	.exit = ping_v6_proc_exit_net,
  };
  #endif
  
  int __init pingv6_init(void)
  {
  #ifdef CONFIG_PROC_FS
  	int ret = register_pernet_subsys(&ping_v6_net_ops);
  	if (ret)
  		return ret;
  #endif
  	pingv6_ops.ipv6_recv_error = ipv6_recv_error;
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
234
235
236
  	pingv6_ops.ip6_datagram_recv_common_ctl = ip6_datagram_recv_common_ctl;
  	pingv6_ops.ip6_datagram_recv_specific_ctl =
  		ip6_datagram_recv_specific_ctl;
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
237
238
239
240
241
242
243
244
245
246
247
248
  	pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
  	pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
  	pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
  	return inet6_register_protosw(&pingv6_protosw);
  }
  
  /* This never gets called because it's not possible to unload the ipv6 module,
   * but just in case.
   */
  void pingv6_exit(void)
  {
  	pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
4b261c75a   Hannes Frederic Sowa   ipv6: make IPV6_R...
249
250
  	pingv6_ops.ip6_datagram_recv_common_ctl = dummy_ip6_datagram_recv_ctl;
  	pingv6_ops.ip6_datagram_recv_specific_ctl = dummy_ip6_datagram_recv_ctl;
d862e5461   Lorenzo Colitti   net: ipv6: Implem...
251
252
253
254
255
256
257
258
  	pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
  	pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
  	pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
  #ifdef CONFIG_PROC_FS
  	unregister_pernet_subsys(&ping_v6_net_ops);
  #endif
  	inet6_unregister_protosw(&pingv6_protosw);
  }