ping.c 21 KB

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