blob: 3bd87de793dad11dc2d66c1f2fc1466d989067a7 [file] [log] [blame]
Lorenzo Colittif7048202013-01-16 22:09:49 +00001/*
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 <net/ping.h>
28#include <linux/module.h>
29
30struct proto pingv6_prot = {
31 .name = "PINGv6",
32 .owner = THIS_MODULE,
33 .init = ping_init_sock,
34 .close = ping_close,
35 .connect = ip6_datagram_connect,
36 .disconnect = udp_disconnect,
37 .setsockopt = ipv6_setsockopt,
38 .getsockopt = ipv6_getsockopt,
39 .sendmsg = ping_v6_sendmsg,
40 .recvmsg = ping_recvmsg,
41 .bind = ping_bind,
42 .backlog_rcv = ping_queue_rcv_skb,
43 .hash = ping_hash,
44 .unhash = ping_unhash,
45 .get_port = ping_get_port,
46 .obj_size = sizeof(struct raw6_sock),
47};
48EXPORT_SYMBOL_GPL(pingv6_prot);
49
50static struct inet_protosw pingv6_protosw = {
51 .type = SOCK_DGRAM,
52 .protocol = IPPROTO_ICMPV6,
53 .prot = &pingv6_prot,
54 .ops = &inet6_dgram_ops,
55 .no_check = UDP_CSUM_DEFAULT,
56 .flags = INET_PROTOSW_REUSE,
57};
58
59
60/* Compatibility glue so we can support IPv6 when it's compiled as a module */
Ethan Chenc523abc2014-03-07 11:49:08 -080061int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
62 int *addr_len)
Lorenzo Colittif7048202013-01-16 22:09:49 +000063{
64 return -EAFNOSUPPORT;
65}
66int dummy_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
67 struct sk_buff *skb)
68{
69 return -EAFNOSUPPORT;
70}
71int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
72{
73 return -EAFNOSUPPORT;
74}
75void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
76 __be16 port, u32 info, u8 *payload) {}
77int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
78 struct net_device *dev, int strict)
79{
80 return 0;
81}
82
83int __init pingv6_init(void)
84{
85 pingv6_ops.ipv6_recv_error = ipv6_recv_error;
86 pingv6_ops.datagram_recv_ctl = datagram_recv_ctl;
87 pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
88 pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
89 pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
90 return inet6_register_protosw(&pingv6_protosw);
91}
92
93/* This never gets called because it's not possible to unload the ipv6 module,
94 * but just in case.
95 */
96void pingv6_exit(void)
97{
98 pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
99 pingv6_ops.datagram_recv_ctl = dummy_datagram_recv_ctl;
100 pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
101 pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
102 pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
103 inet6_unregister_protosw(&pingv6_protosw);
104}
105
106int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
107 size_t len)
108{
109 struct inet_sock *inet = inet_sk(sk);
110 struct ipv6_pinfo *np = inet6_sk(sk);
111 struct icmp6hdr user_icmph;
112 int addr_type;
113 struct in6_addr *daddr;
114 int iif = 0;
115 struct flowi6 fl6;
116 int err;
117 int hlimit;
118 struct dst_entry *dst;
119 struct rt6_info *rt;
120 struct pingfakehdr pfh;
121
122 pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
123
124 err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
125 sizeof(user_icmph));
126 if (err)
127 return err;
128
129 if (msg->msg_name) {
130 struct sockaddr_in6 *u = (struct sockaddr_in6 *) msg->msg_name;
131 if (msg->msg_namelen < sizeof(struct sockaddr_in6) ||
132 u->sin6_family != AF_INET6) {
133 return -EINVAL;
134 }
135 if (sk->sk_bound_dev_if &&
136 sk->sk_bound_dev_if != u->sin6_scope_id) {
137 return -EINVAL;
138 }
139 daddr = &(u->sin6_addr);
140 iif = u->sin6_scope_id;
141 } else {
142 if (sk->sk_state != TCP_ESTABLISHED)
143 return -EDESTADDRREQ;
144 daddr = &np->daddr;
145 }
146
147 if (!iif)
148 iif = sk->sk_bound_dev_if;
149
150 addr_type = ipv6_addr_type(daddr);
151 if (__ipv6_addr_needs_scope_id(addr_type) && !iif)
152 return -EINVAL;
153 if (addr_type & IPV6_ADDR_MAPPED)
154 return -EINVAL;
155
156 /* TODO: use ip6_datagram_send_ctl to get options from cmsg */
157
158 memset(&fl6, 0, sizeof(fl6));
159
160 fl6.flowi6_proto = IPPROTO_ICMPV6;
161 fl6.saddr = np->saddr;
162 fl6.daddr = *daddr;
Lorenzo Colitticfc667e2014-02-27 13:38:26 +0900163 fl6.flowi6_mark = sk->sk_mark;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000164 fl6.fl6_icmp_type = user_icmph.icmp6_type;
165 fl6.fl6_icmp_code = user_icmph.icmp6_code;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +0900166 fl6.flowi6_uid = sock_i_uid(sk);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000167 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
168
169 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
170 fl6.flowi6_oif = np->mcast_oif;
171 else if (!fl6.flowi6_oif)
172 fl6.flowi6_oif = np->ucast_oif;
173
174 dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, 1);
175 if (IS_ERR(dst))
176 return PTR_ERR(dst);
177 rt = (struct rt6_info *) dst;
178
179 np = inet6_sk(sk);
180 if (!np)
181 return -EBADF;
182
183 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
184 fl6.flowi6_oif = np->mcast_oif;
185 else if (!fl6.flowi6_oif)
186 fl6.flowi6_oif = np->ucast_oif;
187
188 pfh.icmph.type = user_icmph.icmp6_type;
189 pfh.icmph.code = user_icmph.icmp6_code;
190 pfh.icmph.checksum = 0;
191 pfh.icmph.un.echo.id = inet->inet_sport;
192 pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
193 pfh.iov = msg->msg_iov;
194 pfh.wcheck = 0;
195 pfh.family = AF_INET6;
196
197 if (ipv6_addr_is_multicast(&fl6.daddr))
198 hlimit = np->mcast_hops;
199 else
200 hlimit = np->hop_limit;
201 if (hlimit < 0)
202 hlimit = ip6_dst_hoplimit(dst);
203
Lorenzo Colitti9c487be2014-01-21 12:11:41 -0700204 lock_sock(sk);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000205 err = ip6_append_data(sk, ping_getfrag, &pfh, len,
206 0, hlimit,
207 np->tclass, NULL, &fl6, rt,
208 MSG_DONTWAIT, np->dontfrag);
209
210 if (err) {
211 ICMP6_INC_STATS_BH(sock_net(sk), rt->rt6i_idev,
212 ICMP6_MIB_OUTERRORS);
213 ip6_flush_pending_frames(sk);
214 } else {
215 err = icmpv6_push_pending_frames(sk, &fl6,
216 (struct icmp6hdr *) &pfh.icmph,
217 len);
218 }
Lorenzo Colitti9c487be2014-01-21 12:11:41 -0700219 release_sock(sk);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000220
Lorenzo Colittic5e55242014-01-21 12:13:56 -0700221 if (err)
222 return err;
223
224 return len;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000225}