PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/net/ipv6/ping.c

https://gitlab.com/SerenityS/msm8974_G2_render_kernel
C | 230 lines | 172 code | 28 blank | 30 comment | 28 complexity | 1bc8f17d31a88f5c068fe2dd09b34594 MD5 | raw file
  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)
  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(struct sockaddr_in6) ||
  120. u->sin6_family != AF_INET6) {
  121. return -EINVAL;
  122. }
  123. if (sk->sk_bound_dev_if &&
  124. sk->sk_bound_dev_if != u->sin6_scope_id) {
  125. return -EINVAL;
  126. }
  127. daddr = &(u->sin6_addr);
  128. iif = u->sin6_scope_id;
  129. } else {
  130. if (sk->sk_state != TCP_ESTABLISHED)
  131. return -EDESTADDRREQ;
  132. daddr = &np->daddr;
  133. }
  134. if (!iif)
  135. iif = sk->sk_bound_dev_if;
  136. addr_type = ipv6_addr_type(daddr);
  137. if (__ipv6_addr_needs_scope_id(addr_type) && !iif)
  138. return -EINVAL;
  139. if (addr_type & IPV6_ADDR_MAPPED)
  140. return -EINVAL;
  141. /* TODO: use ip6_datagram_send_ctl to get options from cmsg */
  142. memset(&fl6, 0, sizeof(fl6));
  143. fl6.flowi6_proto = IPPROTO_ICMPV6;
  144. fl6.saddr = np->saddr;
  145. fl6.daddr = *daddr;
  146. fl6.fl6_icmp_type = user_icmph.icmp6_type;
  147. fl6.fl6_icmp_code = user_icmph.icmp6_code;
  148. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  149. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  150. fl6.flowi6_oif = np->mcast_oif;
  151. else if (!fl6.flowi6_oif)
  152. fl6.flowi6_oif = np->ucast_oif;
  153. dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, 1);
  154. if (IS_ERR(dst))
  155. return PTR_ERR(dst);
  156. rt = (struct rt6_info *) dst;
  157. np = inet6_sk(sk);
  158. if (!np)
  159. return -EBADF;
  160. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  161. fl6.flowi6_oif = np->mcast_oif;
  162. else if (!fl6.flowi6_oif)
  163. fl6.flowi6_oif = np->ucast_oif;
  164. pfh.icmph.type = user_icmph.icmp6_type;
  165. pfh.icmph.code = user_icmph.icmp6_code;
  166. pfh.icmph.checksum = 0;
  167. pfh.icmph.un.echo.id = inet->inet_sport;
  168. pfh.icmph.un.echo.sequence = user_icmph.icmp6_sequence;
  169. pfh.iov = msg->msg_iov;
  170. pfh.wcheck = 0;
  171. pfh.family = AF_INET6;
  172. if (ipv6_addr_is_multicast(&fl6.daddr))
  173. hlimit = np->mcast_hops;
  174. else
  175. hlimit = np->hop_limit;
  176. if (hlimit < 0)
  177. hlimit = ip6_dst_hoplimit(dst);
  178. /* 2013-11-25 hobbes.song LGP_DATA_CTS_IPV6_PINGTEST [START] */
  179. lock_sock(sk);
  180. /* 2013-11-25 hobbes.song LGP_DATA_CTS_IPV6_PINGTEST [END] */
  181. err = ip6_append_data(sk, ping_getfrag, &pfh, len,
  182. 0, hlimit,
  183. np->tclass, NULL, &fl6, rt,
  184. MSG_DONTWAIT, np->dontfrag);
  185. if (err) {
  186. ICMP6_INC_STATS_BH(sock_net(sk), rt->rt6i_idev,
  187. ICMP6_MIB_OUTERRORS);
  188. ip6_flush_pending_frames(sk);
  189. } else {
  190. err = icmpv6_push_pending_frames(sk, &fl6,
  191. (struct icmp6hdr *) &pfh.icmph,
  192. len);
  193. }
  194. /* 2013-11-25 hobbes.song LGP_DATA_CTS_IPV6_PINGTEST [START] */
  195. release_sock(sk);
  196. if(err)
  197. /* 2013-11-25 hobbes.song LGP_DATA_CTS_IPV6_PINGTEST [END] */
  198. return err;
  199. /* 2013-11-25 hobbes.song LGP_DATA_CTS_IPV6_PINGTEST [START] */
  200. return len;
  201. /* 2013-11-25 hobbes.song LGP_DATA_CTS_IPV6_PINGTEST [END] */
  202. }