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