ping.c 8.1 KB

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