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