ping.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <linux/export.h>
  21. #include <net/addrconf.h>
  22. #include <net/ipv6.h>
  23. #include <net/ip6_route.h>
  24. #include <net/protocol.h>
  25. #include <net/udp.h>
  26. #include <net/transp_v6.h>
  27. #include <net/ping.h>
  28. struct proto pingv6_prot = {
  29. .name = "PINGv6",
  30. .owner = THIS_MODULE,
  31. .init = ping_init_sock,
  32. .close = ping_close,
  33. .connect = ip6_datagram_connect,
  34. .disconnect = udp_disconnect,
  35. .setsockopt = ipv6_setsockopt,
  36. .getsockopt = ipv6_getsockopt,
  37. .sendmsg = ping_v6_sendmsg,
  38. .recvmsg = ping_recvmsg,
  39. .bind = ping_bind,
  40. .backlog_rcv = ping_queue_rcv_skb,
  41. .hash = ping_hash,
  42. .unhash = ping_unhash,
  43. .get_port = ping_get_port,
  44. .obj_size = sizeof(struct raw6_sock),
  45. };
  46. EXPORT_SYMBOL_GPL(pingv6_prot);
  47. static struct inet_protosw pingv6_protosw = {
  48. .type = SOCK_DGRAM,
  49. .protocol = IPPROTO_ICMPV6,
  50. .prot = &pingv6_prot,
  51. .ops = &inet6_dgram_ops,
  52. .no_check = UDP_CSUM_DEFAULT,
  53. .flags = INET_PROTOSW_REUSE,
  54. };
  55. /* Compatibility glue so we can support IPv6 when it's compiled as a module */
  56. int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
  57. {
  58. return -EAFNOSUPPORT;
  59. }
  60. int dummy_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
  61. struct sk_buff *skb)
  62. {
  63. return -EAFNOSUPPORT;
  64. }
  65. int dummy_icmpv6_err_convert(u8 type, u8 code, int *err)
  66. {
  67. return -EAFNOSUPPORT;
  68. }
  69. void dummy_ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  70. __be16 port, u32 info, u8 *payload) {}
  71. int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
  72. struct net_device *dev, int strict)
  73. {
  74. return 0;
  75. }
  76. int __init pingv6_init(void)
  77. {
  78. pingv6_ops.ipv6_recv_error = ipv6_recv_error;
  79. pingv6_ops.datagram_recv_ctl = datagram_recv_ctl;
  80. pingv6_ops.icmpv6_err_convert = icmpv6_err_convert;
  81. pingv6_ops.ipv6_icmp_error = ipv6_icmp_error;
  82. pingv6_ops.ipv6_chk_addr = ipv6_chk_addr;
  83. return inet6_register_protosw(&pingv6_protosw);
  84. }
  85. /* This never gets called because it's not possible to unload the ipv6 module,
  86. * but just in case.
  87. */
  88. void pingv6_exit(void)
  89. {
  90. pingv6_ops.ipv6_recv_error = dummy_ipv6_recv_error;
  91. pingv6_ops.datagram_recv_ctl = dummy_datagram_recv_ctl;
  92. pingv6_ops.icmpv6_err_convert = dummy_icmpv6_err_convert;
  93. pingv6_ops.ipv6_icmp_error = dummy_ipv6_icmp_error;
  94. pingv6_ops.ipv6_chk_addr = dummy_ipv6_chk_addr;
  95. inet6_unregister_protosw(&pingv6_protosw);
  96. }
  97. int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  98. size_t len)
  99. {
  100. struct inet_sock *inet = inet_sk(sk);
  101. struct ipv6_pinfo *np = inet6_sk(sk);
  102. struct icmp6hdr user_icmph;
  103. int addr_type;
  104. struct in6_addr *daddr;
  105. int iif = 0;
  106. struct flowi6 fl6;
  107. int err;
  108. int hlimit;
  109. struct dst_entry *dst;
  110. struct rt6_info *rt;
  111. struct pingfakehdr pfh;
  112. pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
  113. err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
  114. sizeof(user_icmph));
  115. if (err)
  116. return err;
  117. if (msg->msg_name) {
  118. struct sockaddr_in6 *u = (struct sockaddr_in6 *) msg->msg_name;
  119. if (msg->msg_namelen < sizeof(*u))
  120. return -EINVAL;
  121. if (u->sin6_family != AF_INET6) {
  122. return -EAFNOSUPPORT;
  123. }
  124. if (sk->sk_bound_dev_if &&
  125. sk->sk_bound_dev_if != u->sin6_scope_id) {
  126. return -EINVAL;
  127. }
  128. daddr = &(u->sin6_addr);
  129. iif = u->sin6_scope_id;
  130. } else {
  131. if (sk->sk_state != TCP_ESTABLISHED)
  132. return -EDESTADDRREQ;
  133. daddr = &np->daddr;
  134. }
  135. if (!iif)
  136. iif = sk->sk_bound_dev_if;
  137. addr_type = ipv6_addr_type(daddr);
  138. if (__ipv6_addr_needs_scope_id(addr_type) && !iif)
  139. return -EINVAL;
  140. if (addr_type & IPV6_ADDR_MAPPED)
  141. return -EINVAL;
  142. /* TODO: use ip6_datagram_send_ctl to get options from cmsg */
  143. memset(&fl6, 0, sizeof(fl6));
  144. fl6.flowi6_proto = IPPROTO_ICMPV6;
  145. fl6.saddr = np->saddr;
  146. fl6.daddr = *daddr;
  147. fl6.flowi6_mark = sk->sk_mark;
  148. fl6.flowi6_uid = sk->sk_uid;
  149. fl6.fl6_icmp_type = user_icmph.icmp6_type;
  150. fl6.fl6_icmp_code = user_icmph.icmp6_code;
  151. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  152. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  153. fl6.flowi6_oif = np->mcast_oif;
  154. else if (!fl6.flowi6_oif)
  155. fl6.flowi6_oif = np->ucast_oif;
  156. dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, 1);
  157. if (IS_ERR(dst))
  158. return PTR_ERR(dst);
  159. rt = (struct rt6_info *) dst;
  160. np = inet6_sk(sk);
  161. if (!np)
  162. return -EBADF;
  163. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  164. fl6.flowi6_oif = np->mcast_oif;
  165. else if (!fl6.flowi6_oif)
  166. fl6.flowi6_oif = np->ucast_oif;
  167. pfh.icmph.type = user_icmph.icmp6_type;
  168. pfh.icmph.code = user_icmph.icmp6_code;
  169. pfh.icmph.checksum = 0;
  170. pfh.icmph.un.echo.id = inet->inet_sport;
  171. pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
  172. pfh.iov = msg->msg_iov;
  173. pfh.wcheck = 0;
  174. pfh.family = AF_INET6;
  175. if (ipv6_addr_is_multicast(&fl6.daddr))
  176. hlimit = np->mcast_hops;
  177. else
  178. hlimit = np->hop_limit;
  179. if (hlimit < 0)
  180. hlimit = ip6_dst_hoplimit(dst);
  181. lock_sock(sk);
  182. err = ip6_append_data(sk, ping_getfrag, &pfh, len,
  183. 0, hlimit,
  184. np->tclass, NULL, &fl6, rt,
  185. MSG_DONTWAIT, np->dontfrag);
  186. if (err) {
  187. ICMP6_INC_STATS_BH(sock_net(sk), rt->rt6i_idev,
  188. ICMP6_MIB_OUTERRORS);
  189. ip6_flush_pending_frames(sk);
  190. } else {
  191. err = icmpv6_push_pending_frames(sk, &fl6,
  192. (struct icmp6hdr *) &pfh.icmph,
  193. len);
  194. }
  195. release_sock(sk);
  196. if (err)
  197. return err;
  198. return len;
  199. }