ping.c 7.1 KB

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