ping.c 7.5 KB

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