ping.c 21 KB

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