ping.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 *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);
  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 (np->sndflow)
  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 = np->mcast_oif;
  99. else if (!oif)
  100. oif = 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, np);
  108. ipc6.sockc.tsflags = sk->sk_tsflags;
  109. ipc6.sockc.mark = READ_ONCE(sk->sk_mark);
  110. fl6.flowi6_oif = oif;
  111. if (msg->msg_controllen) {
  112. struct ipv6_txoptions opt = {};
  113. opt.tot_len = sizeof(opt);
  114. ipc6.opt = &opt;
  115. err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
  116. if (err < 0)
  117. return err;
  118. /* Changes to txoptions and flow info are not implemented, yet.
  119. * Drop the options.
  120. */
  121. ipc6.opt = NULL;
  122. }
  123. fl6.flowi6_proto = IPPROTO_ICMPV6;
  124. fl6.saddr = np->saddr;
  125. fl6.daddr = *daddr;
  126. fl6.flowi6_mark = ipc6.sockc.mark;
  127. fl6.flowi6_uid = sk->sk_uid;
  128. fl6.fl6_icmp_type = user_icmph.icmp6_type;
  129. fl6.fl6_icmp_code = user_icmph.icmp6_code;
  130. security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
  131. fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
  132. dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, false);
  133. if (IS_ERR(dst))
  134. return PTR_ERR(dst);
  135. rt = (struct rt6_info *) dst;
  136. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  137. fl6.flowi6_oif = np->mcast_oif;
  138. else if (!fl6.flowi6_oif)
  139. fl6.flowi6_oif = np->ucast_oif;
  140. pfh.icmph.type = user_icmph.icmp6_type;
  141. pfh.icmph.code = user_icmph.icmp6_code;
  142. pfh.icmph.checksum = 0;
  143. pfh.icmph.un.echo.id = inet->inet_sport;
  144. pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
  145. pfh.msg = msg;
  146. pfh.wcheck = 0;
  147. pfh.family = AF_INET6;
  148. if (ipc6.hlimit < 0)
  149. ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  150. lock_sock(sk);
  151. err = ip6_append_data(sk, ping_getfrag, &pfh, len,
  152. sizeof(struct icmp6hdr), &ipc6, &fl6, rt,
  153. MSG_DONTWAIT);
  154. if (err) {
  155. ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,
  156. ICMP6_MIB_OUTERRORS);
  157. ip6_flush_pending_frames(sk);
  158. } else {
  159. icmpv6_push_pending_frames(sk, &fl6,
  160. (struct icmp6hdr *)&pfh.icmph, len);
  161. }
  162. release_sock(sk);
  163. dst_release(dst);
  164. if (err)
  165. return err;
  166. return len;
  167. }
  168. struct proto pingv6_prot = {
  169. .name = "PINGv6",
  170. .owner = THIS_MODULE,
  171. .init = ping_init_sock,
  172. .close = ping_close,
  173. .pre_connect = ping_v6_pre_connect,
  174. .connect = ip6_datagram_connect_v6_only,
  175. .disconnect = __udp_disconnect,
  176. .setsockopt = ipv6_setsockopt,
  177. .getsockopt = ipv6_getsockopt,
  178. .sendmsg = ping_v6_sendmsg,
  179. .recvmsg = ping_recvmsg,
  180. .bind = ping_bind,
  181. .backlog_rcv = ping_queue_rcv_skb,
  182. .hash = ping_hash,
  183. .unhash = ping_unhash,
  184. .get_port = ping_get_port,
  185. .put_port = ping_unhash,
  186. .obj_size = sizeof(struct raw6_sock),
  187. };
  188. EXPORT_SYMBOL_GPL(pingv6_prot);
  189. static struct inet_protosw pingv6_protosw = {
  190. .type = SOCK_DGRAM,
  191. .protocol = IPPROTO_ICMPV6,
  192. .prot = &pingv6_prot,
  193. .ops = &inet6_sockraw_ops,
  194. .flags = INET_PROTOSW_REUSE,
  195. };
  196. #ifdef CONFIG_PROC_FS
  197. static void *ping_v6_seq_start(struct seq_file *seq, loff_t *pos)
  198. {
  199. return ping_seq_start(seq, pos, AF_INET6);
  200. }
  201. static int ping_v6_seq_show(struct seq_file *seq, void *v)
  202. {
  203. if (v == SEQ_START_TOKEN) {
  204. seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
  205. } else {
  206. int bucket = ((struct ping_iter_state *) seq->private)->bucket;
  207. struct inet_sock *inet = inet_sk(v);
  208. __u16 srcp = ntohs(inet->inet_sport);
  209. __u16 destp = ntohs(inet->inet_dport);
  210. ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
  211. }
  212. return 0;
  213. }
  214. static const struct seq_operations ping_v6_seq_ops = {
  215. .start = ping_v6_seq_start,
  216. .show = ping_v6_seq_show,
  217. .next = ping_seq_next,
  218. .stop = ping_seq_stop,
  219. };
  220. static int __net_init ping_v6_proc_init_net(struct net *net)
  221. {
  222. if (!proc_create_net("icmp6", 0444, net->proc_net, &ping_v6_seq_ops,
  223. sizeof(struct ping_iter_state)))
  224. return -ENOMEM;
  225. return 0;
  226. }
  227. static void __net_exit ping_v6_proc_exit_net(struct net *net)
  228. {
  229. remove_proc_entry("icmp6", net->proc_net);
  230. }
  231. static struct pernet_operations ping_v6_net_ops = {
  232. .init = ping_v6_proc_init_net,
  233. .exit = ping_v6_proc_exit_net,
  234. };
  235. #endif
  236. int __init pingv6_init(void)
  237. {
  238. #ifdef CONFIG_PROC_FS
  239. int ret = register_pernet_subsys(&ping_v6_net_ops);
  240. if (ret)
  241. return ret;
  242. #endif
  243. pingv6_ops.ipv6_recv_error = ipv6_recv_error;
  244. pingv6_ops.ip6_datagram_recv_common_ctl = ip6_datagram_recv_common_ctl;
  245. pingv6_ops.ip6_datagram_recv_specific_ctl =
  246. ip6_datagram_recv_specific_ctl;
  247. pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
  248. pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
  249. pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
  250. return inet6_register_protosw(&pingv6_protosw);
  251. }
  252. /* This never gets called because it's not possible to unload the ipv6 module,
  253. * but just in case.
  254. */
  255. void pingv6_exit(void)
  256. {
  257. pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
  258. pingv6_ops.ip6_datagram_recv_common_ctl = dummy_ip6_datagram_recv_ctl;
  259. pingv6_ops.ip6_datagram_recv_specific_ctl = dummy_ip6_datagram_recv_ctl;
  260. pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
  261. pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
  262. pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
  263. #ifdef CONFIG_PROC_FS
  264. unregister_pernet_subsys(&ping_v6_net_ops);
  265. #endif
  266. inet6_unregister_protosw(&pingv6_protosw);
  267. }