ping.c 21 KB

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