ping.c 21 KB

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