ping.c 7.4 KB

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