ping.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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/udp.c code.
  14. *
  15. * Authors: Vasiliy Kulikov / Openwall (for Linux 2.6),
  16. * Pavel Kankovsky (for Linux 2.4.32)
  17. *
  18. * Pavel gave all rights to bugs to Vasiliy,
  19. * none of the bugs are Pavel's now.
  20. *
  21. */
  22. #include <asm/system.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/ioctls.h>
  25. #include <linux/types.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/socket.h>
  28. #include <linux/sockios.h>
  29. #include <linux/in.h>
  30. #include <linux/errno.h>
  31. #include <linux/timer.h>
  32. #include <linux/mm.h>
  33. #include <linux/inet.h>
  34. #include <linux/netdevice.h>
  35. #include <net/snmp.h>
  36. #include <net/ip.h>
  37. #include <net/ipv6.h>
  38. #include <net/icmp.h>
  39. #include <net/protocol.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/proc_fs.h>
  42. #include <net/sock.h>
  43. #include <net/ping.h>
  44. #include <net/icmp.h>
  45. #include <net/udp.h>
  46. #include <net/route.h>
  47. #include <net/inet_common.h>
  48. #include <net/checksum.h>
  49. struct ping_table ping_table __read_mostly;
  50. u16 ping_port_rover;
  51. static inline int ping_hashfn(struct net *net, unsigned num, unsigned mask)
  52. {
  53. int res = (num + net_hash_mix(net)) & mask;
  54. pr_debug("hash(%d) = %d\n", num, res);
  55. return res;
  56. }
  57. static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
  58. struct net *net, unsigned num)
  59. {
  60. return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
  61. }
  62. static int ping_v4_get_port(struct sock *sk, unsigned short ident)
  63. {
  64. struct hlist_nulls_node *node;
  65. struct hlist_nulls_head *hlist;
  66. struct inet_sock *isk, *isk2;
  67. struct sock *sk2 = NULL;
  68. isk = inet_sk(sk);
  69. write_lock_bh(&ping_table.lock);
  70. if (ident == 0) {
  71. u32 i;
  72. u16 result = ping_port_rover + 1;
  73. for (i = 0; i < (1L << 16); i++, result++) {
  74. if (!result)
  75. result++; /* avoid zero */
  76. hlist = ping_hashslot(&ping_table, sock_net(sk),
  77. result);
  78. ping_portaddr_for_each_entry(sk2, node, hlist) {
  79. isk2 = inet_sk(sk2);
  80. if (isk2->inet_num == result)
  81. goto next_port;
  82. }
  83. /* found */
  84. ping_port_rover = ident = result;
  85. break;
  86. next_port:
  87. ;
  88. }
  89. if (i >= (1L << 16))
  90. goto fail;
  91. } else {
  92. hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
  93. ping_portaddr_for_each_entry(sk2, node, hlist) {
  94. isk2 = inet_sk(sk2);
  95. if ((isk2->inet_num == ident) &&
  96. (sk2 != sk) &&
  97. (!sk2->sk_reuse || !sk->sk_reuse))
  98. goto fail;
  99. }
  100. }
  101. pr_debug("found port/ident = %d\n", ident);
  102. isk->inet_num = ident;
  103. if (sk_unhashed(sk)) {
  104. pr_debug("was not hashed\n");
  105. sock_hold(sk);
  106. hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
  107. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  108. }
  109. write_unlock_bh(&ping_table.lock);
  110. return 0;
  111. fail:
  112. write_unlock_bh(&ping_table.lock);
  113. return 1;
  114. }
  115. static void ping_v4_hash(struct sock *sk)
  116. {
  117. pr_debug("ping_v4_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
  118. BUG(); /* "Please do not press this button again." */
  119. }
  120. static void ping_v4_unhash(struct sock *sk)
  121. {
  122. struct inet_sock *isk = inet_sk(sk);
  123. pr_debug("ping_v4_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
  124. if (sk_hashed(sk)) {
  125. struct hlist_nulls_head *hslot;
  126. hslot = ping_hashslot(&ping_table, sock_net(sk), isk->inet_num);
  127. write_lock_bh(&ping_table.lock);
  128. hlist_nulls_del(&sk->sk_nulls_node);
  129. sock_put(sk);
  130. isk->inet_num = isk->inet_sport = 0;
  131. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  132. write_unlock_bh(&ping_table.lock);
  133. }
  134. }
  135. struct sock *ping_v4_lookup(struct net *net, u32 saddr, u32 daddr,
  136. u16 ident, int dif)
  137. {
  138. struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
  139. struct sock *sk = NULL;
  140. struct inet_sock *isk;
  141. struct hlist_nulls_node *hnode;
  142. pr_debug("try to find: num = %d, daddr = %ld, dif = %d\n",
  143. (int)ident, (unsigned long)daddr, dif);
  144. read_lock_bh(&ping_table.lock);
  145. ping_portaddr_for_each_entry(sk, hnode, hslot) {
  146. isk = inet_sk(sk);
  147. pr_debug("found: %p: num = %d, daddr = %ld, dif = %d\n", sk,
  148. (int)isk->inet_num, (unsigned long)isk->inet_rcv_saddr,
  149. sk->sk_bound_dev_if);
  150. pr_debug("iterate\n");
  151. if (isk->inet_num != ident)
  152. continue;
  153. if (isk->inet_rcv_saddr && isk->inet_rcv_saddr != daddr)
  154. continue;
  155. if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
  156. continue;
  157. sock_hold(sk);
  158. goto exit;
  159. }
  160. sk = NULL;
  161. exit:
  162. read_unlock_bh(&ping_table.lock);
  163. return sk;
  164. }
  165. static int ping_init_sock(struct sock *sk)
  166. {
  167. struct net *net = sock_net(sk);
  168. gid_t group = current_egid();
  169. gid_t range[2];
  170. struct group_info *group_info = get_current_groups();
  171. int i, j, count = group_info->ngroups;
  172. inet_get_ping_group_range_net(net, range, range+1);
  173. if (range[0] <= group && group <= range[1])
  174. return 0;
  175. for (i = 0; i < group_info->nblocks; i++) {
  176. int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
  177. for (j = 0; j < cp_count; j++) {
  178. group = group_info->blocks[i][j];
  179. if (range[0] <= group && group <= range[1])
  180. return 0;
  181. }
  182. count -= cp_count;
  183. }
  184. return -EACCES;
  185. }
  186. static void ping_close(struct sock *sk, long timeout)
  187. {
  188. pr_debug("ping_close(sk=%p,sk->num=%u)\n",
  189. inet_sk(sk), inet_sk(sk)->inet_num);
  190. pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
  191. sk_common_release(sk);
  192. }
  193. /*
  194. * We need our own bind because there are no privileged id's == local ports.
  195. * Moreover, we don't allow binding to multi- and broadcast addresses.
  196. */
  197. static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  198. {
  199. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  200. struct inet_sock *isk = inet_sk(sk);
  201. unsigned short snum;
  202. int chk_addr_ret;
  203. int err;
  204. if (addr_len < sizeof(struct sockaddr_in))
  205. return -EINVAL;
  206. pr_debug("ping_v4_bind(sk=%p,sa_addr=%08x,sa_port=%d)\n",
  207. sk, addr->sin_addr.s_addr, ntohs(addr->sin_port));
  208. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  209. if (addr->sin_addr.s_addr == INADDR_ANY)
  210. chk_addr_ret = RTN_LOCAL;
  211. if ((sysctl_ip_nonlocal_bind == 0 &&
  212. isk->freebind == 0 && isk->transparent == 0 &&
  213. chk_addr_ret != RTN_LOCAL) ||
  214. chk_addr_ret == RTN_MULTICAST ||
  215. chk_addr_ret == RTN_BROADCAST)
  216. return -EADDRNOTAVAIL;
  217. lock_sock(sk);
  218. err = -EINVAL;
  219. if (isk->inet_num != 0)
  220. goto out;
  221. err = -EADDRINUSE;
  222. isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
  223. snum = ntohs(addr->sin_port);
  224. if (ping_v4_get_port(sk, snum) != 0) {
  225. isk->inet_saddr = isk->inet_rcv_saddr = 0;
  226. goto out;
  227. }
  228. pr_debug("after bind(): num = %d, daddr = %ld, dif = %d\n",
  229. (int)isk->inet_num,
  230. (unsigned long) isk->inet_rcv_saddr,
  231. (int)sk->sk_bound_dev_if);
  232. err = 0;
  233. if (isk->inet_rcv_saddr)
  234. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  235. if (snum)
  236. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  237. isk->inet_sport = htons(isk->inet_num);
  238. isk->inet_daddr = 0;
  239. isk->inet_dport = 0;
  240. sk_dst_reset(sk);
  241. out:
  242. release_sock(sk);
  243. pr_debug("ping_v4_bind -> %d\n", err);
  244. return err;
  245. }
  246. /*
  247. * Is this a supported type of ICMP message?
  248. */
  249. static inline int ping_supported(int type, int code)
  250. {
  251. if (type == ICMP_ECHO && code == 0)
  252. return 1;
  253. return 0;
  254. }
  255. /*
  256. * This routine is called by the ICMP module when it gets some
  257. * sort of error condition.
  258. */
  259. static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
  260. void ping_err(struct sk_buff *skb, u32 info)
  261. {
  262. struct iphdr *iph = (struct iphdr *)skb->data;
  263. struct icmphdr *icmph = (struct icmphdr *)(skb->data+(iph->ihl<<2));
  264. struct inet_sock *inet_sock;
  265. int type = icmph->type;
  266. int code = icmph->code;
  267. struct net *net = dev_net(skb->dev);
  268. struct sock *sk;
  269. int harderr;
  270. int err;
  271. /* We assume the packet has already been checked by icmp_unreach */
  272. if (!ping_supported(icmph->type, icmph->code))
  273. return;
  274. pr_debug("ping_err(type=%04x,code=%04x,id=%04x,seq=%04x)\n", type,
  275. code, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
  276. sk = ping_v4_lookup(net, iph->daddr, iph->saddr,
  277. ntohs(icmph->un.echo.id), skb->dev->ifindex);
  278. if (sk == NULL) {
  279. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  280. pr_debug("no socket, dropping\n");
  281. return; /* No socket for error */
  282. }
  283. pr_debug("err on socket %p\n", sk);
  284. err = 0;
  285. harderr = 0;
  286. inet_sock = inet_sk(sk);
  287. switch (type) {
  288. default:
  289. case ICMP_TIME_EXCEEDED:
  290. err = EHOSTUNREACH;
  291. break;
  292. case ICMP_SOURCE_QUENCH:
  293. /* This is not a real error but ping wants to see it.
  294. * Report it with some fake errno. */
  295. err = EREMOTEIO;
  296. break;
  297. case ICMP_PARAMETERPROB:
  298. err = EPROTO;
  299. harderr = 1;
  300. break;
  301. case ICMP_DEST_UNREACH:
  302. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  303. if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
  304. err = EMSGSIZE;
  305. harderr = 1;
  306. break;
  307. }
  308. goto out;
  309. }
  310. err = EHOSTUNREACH;
  311. if (code <= NR_ICMP_UNREACH) {
  312. harderr = icmp_err_convert[code].fatal;
  313. err = icmp_err_convert[code].errno;
  314. }
  315. break;
  316. case ICMP_REDIRECT:
  317. /* See ICMP_SOURCE_QUENCH */
  318. err = EREMOTEIO;
  319. break;
  320. }
  321. /*
  322. * RFC1122: OK. Passes ICMP errors back to application, as per
  323. * 4.1.3.3.
  324. */
  325. if (!inet_sock->recverr) {
  326. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  327. goto out;
  328. } else {
  329. ip_icmp_error(sk, skb, err, 0 /* no remote port */,
  330. info, (u8 *)icmph);
  331. }
  332. sk->sk_err = err;
  333. sk->sk_error_report(sk);
  334. out:
  335. sock_put(sk);
  336. }
  337. /*
  338. * Copy and checksum an ICMP Echo packet from user space into a buffer.
  339. */
  340. struct pingfakehdr {
  341. struct icmphdr icmph;
  342. struct iovec *iov;
  343. u32 wcheck;
  344. };
  345. static int ping_getfrag(void *from, char * to,
  346. int offset, int fraglen, int odd, struct sk_buff *skb)
  347. {
  348. struct pingfakehdr *pfh = (struct pingfakehdr *)from;
  349. if (offset == 0) {
  350. if (fraglen < sizeof(struct icmphdr))
  351. BUG();
  352. if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
  353. pfh->iov, 0, fraglen - sizeof(struct icmphdr),
  354. &pfh->wcheck))
  355. return -EFAULT;
  356. return 0;
  357. }
  358. if (offset < sizeof(struct icmphdr))
  359. BUG();
  360. if (csum_partial_copy_fromiovecend
  361. (to, pfh->iov, offset - sizeof(struct icmphdr),
  362. fraglen, &pfh->wcheck))
  363. return -EFAULT;
  364. return 0;
  365. }
  366. static int ping_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh, struct flowi4 *fl4)
  367. {
  368. struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
  369. pfh->wcheck = csum_partial((char *)&pfh->icmph,
  370. sizeof(struct icmphdr), pfh->wcheck);
  371. pfh->icmph.checksum = csum_fold(pfh->wcheck);
  372. memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
  373. skb->ip_summed = CHECKSUM_NONE;
  374. return ip_push_pending_frames(sk, fl4);
  375. }
  376. int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  377. size_t len)
  378. {
  379. struct net *net = sock_net(sk);
  380. struct flowi4 fl4;
  381. struct inet_sock *inet = inet_sk(sk);
  382. struct ipcm_cookie ipc;
  383. struct icmphdr user_icmph;
  384. struct pingfakehdr pfh;
  385. struct rtable *rt = NULL;
  386. struct ip_options_data opt_copy;
  387. int free = 0;
  388. u32 saddr, daddr, faddr;
  389. u8 tos;
  390. int err;
  391. pr_debug("ping_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
  392. if (len > 0xFFFF)
  393. return -EMSGSIZE;
  394. /*
  395. * Check the flags.
  396. */
  397. /* Mirror BSD error message compatibility */
  398. if (msg->msg_flags & MSG_OOB)
  399. return -EOPNOTSUPP;
  400. /*
  401. * Fetch the ICMP header provided by the userland.
  402. * iovec is modified!
  403. */
  404. if (memcpy_fromiovec((u8 *)&user_icmph, msg->msg_iov,
  405. sizeof(struct icmphdr)))
  406. return -EFAULT;
  407. if (!ping_supported(user_icmph.type, user_icmph.code))
  408. return -EINVAL;
  409. /*
  410. * Get and verify the address.
  411. */
  412. if (msg->msg_name) {
  413. struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
  414. if (msg->msg_namelen < sizeof(*usin))
  415. return -EINVAL;
  416. if (usin->sin_family != AF_INET)
  417. return -EINVAL;
  418. daddr = usin->sin_addr.s_addr;
  419. /* no remote port */
  420. } else {
  421. if (sk->sk_state != TCP_ESTABLISHED)
  422. return -EDESTADDRREQ;
  423. daddr = inet->inet_daddr;
  424. /* no remote port */
  425. }
  426. ipc.addr = inet->inet_saddr;
  427. ipc.opt = NULL;
  428. ipc.oif = sk->sk_bound_dev_if;
  429. ipc.tx_flags = 0;
  430. err = sock_tx_timestamp(sk, &ipc.tx_flags);
  431. if (err)
  432. return err;
  433. if (msg->msg_controllen) {
  434. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  435. if (err)
  436. return err;
  437. if (ipc.opt)
  438. free = 1;
  439. }
  440. if (!ipc.opt) {
  441. struct ip_options_rcu *inet_opt;
  442. rcu_read_lock();
  443. inet_opt = rcu_dereference(inet->inet_opt);
  444. if (inet_opt) {
  445. memcpy(&opt_copy, inet_opt,
  446. sizeof(*inet_opt) + inet_opt->opt.optlen);
  447. ipc.opt = &opt_copy.opt;
  448. }
  449. rcu_read_unlock();
  450. }
  451. saddr = ipc.addr;
  452. ipc.addr = faddr = daddr;
  453. if (ipc.opt && ipc.opt->opt.srr) {
  454. if (!daddr)
  455. return -EINVAL;
  456. faddr = ipc.opt->opt.faddr;
  457. }
  458. tos = RT_TOS(inet->tos);
  459. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  460. (msg->msg_flags & MSG_DONTROUTE) ||
  461. (ipc.opt && ipc.opt->opt.is_strictroute)) {
  462. tos |= RTO_ONLINK;
  463. }
  464. if (ipv4_is_multicast(daddr)) {
  465. if (!ipc.oif)
  466. ipc.oif = inet->mc_index;
  467. if (!saddr)
  468. saddr = inet->mc_addr;
  469. }
  470. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  471. RT_SCOPE_UNIVERSE, sk->sk_protocol,
  472. inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
  473. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  474. rt = ip_route_output_flow(net, &fl4, sk);
  475. if (IS_ERR(rt)) {
  476. err = PTR_ERR(rt);
  477. rt = NULL;
  478. if (err == -ENETUNREACH)
  479. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  480. goto out;
  481. }
  482. err = -EACCES;
  483. if ((rt->rt_flags & RTCF_BROADCAST) &&
  484. !sock_flag(sk, SOCK_BROADCAST))
  485. goto out;
  486. if (msg->msg_flags & MSG_CONFIRM)
  487. goto do_confirm;
  488. back_from_confirm:
  489. if (!ipc.addr)
  490. ipc.addr = fl4.daddr;
  491. lock_sock(sk);
  492. pfh.icmph.type = user_icmph.type; /* already checked */
  493. pfh.icmph.code = user_icmph.code; /* ditto */
  494. pfh.icmph.checksum = 0;
  495. pfh.icmph.un.echo.id = inet->inet_sport;
  496. pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
  497. pfh.iov = msg->msg_iov;
  498. pfh.wcheck = 0;
  499. err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
  500. 0, &ipc, &rt, msg->msg_flags);
  501. if (err)
  502. ip_flush_pending_frames(sk);
  503. else
  504. err = ping_push_pending_frames(sk, &pfh, &fl4);
  505. release_sock(sk);
  506. out:
  507. ip_rt_put(rt);
  508. if (free)
  509. kfree(ipc.opt);
  510. if (!err) {
  511. icmp_out_count(sock_net(sk), user_icmph.type);
  512. return len;
  513. }
  514. return err;
  515. do_confirm:
  516. dst_confirm(&rt->dst);
  517. if (!(msg->msg_flags & MSG_PROBE) || len)
  518. goto back_from_confirm;
  519. err = 0;
  520. goto out;
  521. }
  522. /*
  523. * IOCTL requests applicable to the UDP^H^H^HICMP protocol
  524. */
  525. int ping_ioctl(struct sock *sk, int cmd, unsigned long arg)
  526. {
  527. pr_debug("ping_ioctl(sk=%p,sk->num=%u,cmd=%d,arg=%lu)\n",
  528. inet_sk(sk), inet_sk(sk)->inet_num, cmd, arg);
  529. switch (cmd) {
  530. case SIOCOUTQ:
  531. case SIOCINQ:
  532. return udp_ioctl(sk, cmd, arg);
  533. default:
  534. return -ENOIOCTLCMD;
  535. }
  536. }
  537. int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  538. size_t len, int noblock, int flags, int *addr_len)
  539. {
  540. struct inet_sock *isk = inet_sk(sk);
  541. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  542. struct sk_buff *skb;
  543. int copied, err;
  544. pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
  545. if (flags & MSG_OOB)
  546. goto out;
  547. if (addr_len)
  548. *addr_len = sizeof(*sin);
  549. if (flags & MSG_ERRQUEUE)
  550. return ip_recv_error(sk, msg, len);
  551. skb = skb_recv_datagram(sk, flags, noblock, &err);
  552. if (!skb)
  553. goto out;
  554. copied = skb->len;
  555. if (copied > len) {
  556. msg->msg_flags |= MSG_TRUNC;
  557. copied = len;
  558. }
  559. /* Don't bother checking the checksum */
  560. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  561. if (err)
  562. goto done;
  563. sock_recv_timestamp(msg, sk, skb);
  564. /* Copy the address. */
  565. if (sin) {
  566. sin->sin_family = AF_INET;
  567. sin->sin_port = 0 /* skb->h.uh->source */;
  568. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  569. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  570. }
  571. if (isk->cmsg_flags)
  572. ip_cmsg_recv(msg, skb);
  573. err = copied;
  574. done:
  575. skb_free_datagram(sk, skb);
  576. out:
  577. pr_debug("ping_recvmsg -> %d\n", err);
  578. return err;
  579. }
  580. static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  581. {
  582. pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
  583. inet_sk(sk), inet_sk(sk)->inet_num, skb);
  584. if (sock_queue_rcv_skb(sk, skb) < 0) {
  585. ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_INERRORS);
  586. kfree_skb(skb);
  587. pr_debug("ping_queue_rcv_skb -> failed\n");
  588. return -1;
  589. }
  590. return 0;
  591. }
  592. /*
  593. * All we need to do is get the socket.
  594. */
  595. void ping_rcv(struct sk_buff *skb)
  596. {
  597. struct sock *sk;
  598. struct net *net = dev_net(skb->dev);
  599. struct iphdr *iph = ip_hdr(skb);
  600. struct icmphdr *icmph = icmp_hdr(skb);
  601. u32 saddr = iph->saddr;
  602. u32 daddr = iph->daddr;
  603. /* We assume the packet has already been checked by icmp_rcv */
  604. pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
  605. skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
  606. /* Push ICMP header back */
  607. skb_push(skb, skb->data - (u8 *)icmph);
  608. sk = ping_v4_lookup(net, saddr, daddr, ntohs(icmph->un.echo.id),
  609. skb->dev->ifindex);
  610. if (sk != NULL) {
  611. pr_debug("rcv on socket %p\n", sk);
  612. ping_queue_rcv_skb(sk, skb_get(skb));
  613. sock_put(sk);
  614. return;
  615. }
  616. pr_debug("no socket, dropping\n");
  617. /* We're called from icmp_rcv(). kfree_skb() is done there. */
  618. }
  619. struct proto ping_prot = {
  620. .name = "PING",
  621. .owner = THIS_MODULE,
  622. .init = ping_init_sock,
  623. .close = ping_close,
  624. .connect = ip4_datagram_connect,
  625. .disconnect = udp_disconnect,
  626. .ioctl = ping_ioctl,
  627. .setsockopt = ip_setsockopt,
  628. .getsockopt = ip_getsockopt,
  629. .sendmsg = ping_sendmsg,
  630. .recvmsg = ping_recvmsg,
  631. .bind = ping_bind,
  632. .backlog_rcv = ping_queue_rcv_skb,
  633. .hash = ping_v4_hash,
  634. .unhash = ping_v4_unhash,
  635. .get_port = ping_v4_get_port,
  636. .obj_size = sizeof(struct inet_sock),
  637. };
  638. EXPORT_SYMBOL(ping_prot);
  639. #ifdef CONFIG_PROC_FS
  640. static struct sock *ping_get_first(struct seq_file *seq, int start)
  641. {
  642. struct sock *sk;
  643. struct ping_iter_state *state = seq->private;
  644. struct net *net = seq_file_net(seq);
  645. for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
  646. ++state->bucket) {
  647. struct hlist_nulls_node *node;
  648. struct hlist_nulls_head *hslot = &ping_table.hash[state->bucket];
  649. if (hlist_nulls_empty(hslot))
  650. continue;
  651. sk_nulls_for_each(sk, node, hslot) {
  652. if (net_eq(sock_net(sk), net))
  653. goto found;
  654. }
  655. }
  656. sk = NULL;
  657. found:
  658. return sk;
  659. }
  660. static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
  661. {
  662. struct ping_iter_state *state = seq->private;
  663. struct net *net = seq_file_net(seq);
  664. do {
  665. sk = sk_nulls_next(sk);
  666. } while (sk && (!net_eq(sock_net(sk), net)));
  667. if (!sk)
  668. return ping_get_first(seq, state->bucket + 1);
  669. return sk;
  670. }
  671. static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
  672. {
  673. struct sock *sk = ping_get_first(seq, 0);
  674. if (sk)
  675. while (pos && (sk = ping_get_next(seq, sk)) != NULL)
  676. --pos;
  677. return pos ? NULL : sk;
  678. }
  679. static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
  680. {
  681. struct ping_iter_state *state = seq->private;
  682. state->bucket = 0;
  683. read_lock_bh(&ping_table.lock);
  684. return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  685. }
  686. static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  687. {
  688. struct sock *sk;
  689. if (v == SEQ_START_TOKEN)
  690. sk = ping_get_idx(seq, 0);
  691. else
  692. sk = ping_get_next(seq, v);
  693. ++*pos;
  694. return sk;
  695. }
  696. static void ping_seq_stop(struct seq_file *seq, void *v)
  697. {
  698. read_unlock_bh(&ping_table.lock);
  699. }
  700. static void ping_format_sock(struct sock *sp, struct seq_file *f,
  701. int bucket, int *len)
  702. {
  703. struct inet_sock *inet = inet_sk(sp);
  704. __be32 dest = inet->inet_daddr;
  705. __be32 src = inet->inet_rcv_saddr;
  706. __u16 destp = ntohs(inet->inet_dport);
  707. __u16 srcp = ntohs(inet->inet_sport);
  708. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  709. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
  710. bucket, src, srcp, dest, destp, sp->sk_state,
  711. sk_wmem_alloc_get(sp),
  712. sk_rmem_alloc_get(sp),
  713. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  714. atomic_read(&sp->sk_refcnt), sp,
  715. atomic_read(&sp->sk_drops), len);
  716. }
  717. static int ping_seq_show(struct seq_file *seq, void *v)
  718. {
  719. if (v == SEQ_START_TOKEN)
  720. seq_printf(seq, "%-127s\n",
  721. " sl local_address rem_address st tx_queue "
  722. "rx_queue tr tm->when retrnsmt uid timeout "
  723. "inode ref pointer drops");
  724. else {
  725. struct ping_iter_state *state = seq->private;
  726. int len;
  727. ping_format_sock(v, seq, state->bucket, &len);
  728. seq_printf(seq, "%*s\n", 127 - len, "");
  729. }
  730. return 0;
  731. }
  732. static const struct seq_operations ping_seq_ops = {
  733. .show = ping_seq_show,
  734. .start = ping_seq_start,
  735. .next = ping_seq_next,
  736. .stop = ping_seq_stop,
  737. };
  738. static int ping_seq_open(struct inode *inode, struct file *file)
  739. {
  740. return seq_open_net(inode, file, &ping_seq_ops,
  741. sizeof(struct ping_iter_state));
  742. }
  743. static const struct file_operations ping_seq_fops = {
  744. .open = ping_seq_open,
  745. .read = seq_read,
  746. .llseek = seq_lseek,
  747. .release = seq_release_net,
  748. };
  749. static int ping_proc_register(struct net *net)
  750. {
  751. struct proc_dir_entry *p;
  752. int rc = 0;
  753. p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
  754. if (!p)
  755. rc = -ENOMEM;
  756. return rc;
  757. }
  758. static void ping_proc_unregister(struct net *net)
  759. {
  760. proc_net_remove(net, "icmp");
  761. }
  762. static int __net_init ping_proc_init_net(struct net *net)
  763. {
  764. return ping_proc_register(net);
  765. }
  766. static void __net_exit ping_proc_exit_net(struct net *net)
  767. {
  768. ping_proc_unregister(net);
  769. }
  770. static struct pernet_operations ping_net_ops = {
  771. .init = ping_proc_init_net,
  772. .exit = ping_proc_exit_net,
  773. };
  774. int __init ping_proc_init(void)
  775. {
  776. return register_pernet_subsys(&ping_net_ops);
  777. }
  778. void ping_proc_exit(void)
  779. {
  780. unregister_pernet_subsys(&ping_net_ops);
  781. }
  782. #endif
  783. void __init ping_init(void)
  784. {
  785. int i;
  786. for (i = 0; i < PING_HTABLE_SIZE; i++)
  787. INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
  788. rwlock_init(&ping_table.lock);
  789. }