ping.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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,
  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_dgram_ops,
  51. .no_check = UDP_CSUM_DEFAULT,
  52. .flags = INET_PROTOSW_REUSE,
  53. };
  54. /* Compatibility glue so we can support IPv6 when it's compiled as a module */
  55. int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
  56. {
  57. return -EAFNOSUPPORT;
  58. }
  59. int dummy_ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
  60. struct sk_buff *skb)
  61. {
  62. return -EAFNOSUPPORT;
  63. }
  64. int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
  65. {
  66. return -EAFNOSUPPORT;
  67. }
  68. void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  69. __be16 port, u32 info, u8 *payload) {}
  70. int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
  71. struct net_device *dev, int strict)
  72. {
  73. return 0;
  74. }
  75. int __init pingv6_init(void)
  76. {
  77. pingv6_ops.ipv6_recv_error = ipv6_recv_error;
  78. pingv6_ops.ip6_datagram_recv_ctl = ip6_datagram_recv_ctl;
  79. pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
  80. pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
  81. pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
  82. return inet6_register_protosw(&pingv6_protosw);
  83. }
  84. /* This never gets called because it's not possible to unload the ipv6 module,
  85. * but just in case.
  86. */
  87. void pingv6_exit(void)
  88. {
  89. pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
  90. pingv6_ops.ip6_datagram_recv_ctl = dummy_ip6_datagram_recv_ctl;
  91. pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
  92. pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
  93. pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
  94. inet6_unregister_protosw(&pingv6_protosw);
  95. }
  96. int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  97. size_t len)
  98. {
  99. struct inet_sock *inet = inet_sk(sk);
  100. struct ipv6_pinfo *np = inet6_sk(sk);
  101. struct icmp6hdr user_icmph;
  102. int addr_type;
  103. struct in6_addr *daddr;
  104. int iif = 0;
  105. struct flowi6 fl6;
  106. int err;
  107. int hlimit;
  108. struct dst_entry *dst;
  109. struct rt6_info *rt;
  110. struct pingfakehdr pfh;
  111. pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
  112. err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
  113. sizeof(user_icmph));
  114. if (err)
  115. return err;
  116. if (msg->msg_name) {
  117. struct sockaddr_in6 *u = (struct sockaddr_in6 *) msg->msg_name;
  118. if (msg->msg_namelen < sizeof(struct sockaddr_in6) ||
  119. u->sin6_family != AF_INET6) {
  120. return -EINVAL;
  121. }
  122. if (sk->sk_bound_dev_if &&
  123. sk->sk_bound_dev_if != u->sin6_scope_id) {
  124. return -EINVAL;
  125. }
  126. daddr = &(u->sin6_addr);
  127. iif = u->sin6_scope_id;
  128. } else {
  129. if (sk->sk_state != TCP_ESTABLISHED)
  130. return -EDESTADDRREQ;
  131. daddr = &np->daddr;
  132. }
  133. if (!iif)
  134. iif = sk->sk_bound_dev_if;
  135. addr_type = ipv6_addr_type(daddr);
  136. if (__ipv6_addr_needs_scope_id(addr_type) && !iif)
  137. return -EINVAL;
  138. if (addr_type & IPV6_ADDR_MAPPED)
  139. return -EINVAL;
  140. /* TODO: use ip6_datagram_send_ctl to get options from cmsg */
  141. memset(&fl6, 0, sizeof(fl6));
  142. fl6.flowi6_proto = IPPROTO_ICMPV6;
  143. fl6.saddr = np->saddr;
  144. fl6.daddr = *daddr;
  145. fl6.fl6_icmp_type = user_icmph.icmp6_type;
  146. fl6.fl6_icmp_code = user_icmph.icmp6_code;
  147. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  148. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  149. fl6.flowi6_oif = np->mcast_oif;
  150. else if (!fl6.flowi6_oif)
  151. fl6.flowi6_oif = np->ucast_oif;
  152. dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, 1);
  153. if (IS_ERR(dst))
  154. return PTR_ERR(dst);
  155. rt = (struct rt6_info *) dst;
  156. np = inet6_sk(sk);
  157. if (!np)
  158. return -EBADF;
  159. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  160. fl6.flowi6_oif = np->mcast_oif;
  161. else if (!fl6.flowi6_oif)
  162. fl6.flowi6_oif = np->ucast_oif;
  163. pfh.icmph.type = user_icmph.icmp6_type;
  164. pfh.icmph.code = user_icmph.icmp6_code;
  165. pfh.icmph.checksum = 0;
  166. pfh.icmph.un.echo.id = inet->inet_sport;
  167. pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
  168. pfh.iov = msg->msg_iov;
  169. pfh.wcheck = 0;
  170. pfh.family = AF_INET6;
  171. if (ipv6_addr_is_multicast(&fl6.daddr))
  172. hlimit = np->mcast_hops;
  173. else
  174. hlimit = np->hop_limit;
  175. if (hlimit < 0)
  176. hlimit = ip6_dst_hoplimit(dst);
  177. err = ip6_append_data(sk, ping_getfrag, &pfh, len,
  178. 0, hlimit,
  179. np->tclass, NULL, &fl6, rt,
  180. MSG_DONTWAIT, np->dontfrag);
  181. if (err) {
  182. ICMP6_INC_STATS_BH(sock_net(sk), rt->rt6i_idev,
  183. ICMP6_MIB_OUTERRORS);
  184. ip6_flush_pending_frames(sk);
  185. } else {
  186. err = icmpv6_push_pending_frames(sk, &fl6,
  187. (struct icmp6hdr *) &pfh.icmph,
  188. len);
  189. }
  190. return err;
  191. }