ping.c 7.3 KB

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