PageRenderTime 94ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/net/core/datagram.c

https://gitlab.com/rychly/nst-linux-sources
C | 650 lines | 407 code | 79 blank | 164 comment | 99 complexity | 6c12335e5b0385129be5ac51efb334c5 MD5 | raw file
  1. /*
  2. * SUCS NET3:
  3. *
  4. * Generic datagram handling routines. These are generic for all
  5. * protocols. Possibly a generic IP version on top of these would
  6. * make sense. Not tonight however 8-).
  7. * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
  8. * NetROM layer all have identical poll code and mostly
  9. * identical recvmsg() code. So we share it here. The poll was
  10. * shared before but buried in udp.c so I moved it.
  11. *
  12. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
  13. * udp.c code)
  14. *
  15. * Fixes:
  16. * Alan Cox : NULL return from skb_peek_copy()
  17. * understood
  18. * Alan Cox : Rewrote skb_read_datagram to avoid the
  19. * skb_peek_copy stuff.
  20. * Alan Cox : Added support for SOCK_SEQPACKET.
  21. * IPX can no longer use the SO_TYPE hack
  22. * but AX.25 now works right, and SPX is
  23. * feasible.
  24. * Alan Cox : Fixed write poll of non IP protocol
  25. * crash.
  26. * Florian La Roche: Changed for my new skbuff handling.
  27. * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET.
  28. * Linus Torvalds : BSD semantic fixes.
  29. * Alan Cox : Datagram iovec handling
  30. * Darryl Miles : Fixed non-blocking SOCK_STREAM.
  31. * Alan Cox : POSIXisms
  32. * Pete Wyckoff : Unconnected accept() fix.
  33. *
  34. */
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/kernel.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #include <linux/mm.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/errno.h>
  43. #include <linux/sched.h>
  44. #include <linux/inet.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/rtnetlink.h>
  47. #include <linux/poll.h>
  48. #include <linux/highmem.h>
  49. #include <linux/spinlock.h>
  50. #include <net/protocol.h>
  51. #include <linux/skbuff.h>
  52. #include <net/checksum.h>
  53. #include <net/sock.h>
  54. #include <net/tcp_states.h>
  55. /*
  56. * Is a socket 'connection oriented' ?
  57. */
  58. static inline int connection_based(struct sock *sk)
  59. {
  60. return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
  61. }
  62. /*
  63. * Wait for a packet..
  64. */
  65. static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  66. {
  67. int error;
  68. DEFINE_WAIT(wait);
  69. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  70. /* Socket errors? */
  71. error = sock_error(sk);
  72. if (error)
  73. goto out_err;
  74. if (!skb_queue_empty(&sk->sk_receive_queue))
  75. goto out;
  76. /* Socket shut down? */
  77. if (sk->sk_shutdown & RCV_SHUTDOWN)
  78. goto out_noerr;
  79. /* Sequenced packets can come disconnected.
  80. * If so we report the problem
  81. */
  82. error = -ENOTCONN;
  83. if (connection_based(sk) &&
  84. !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
  85. goto out_err;
  86. /* handle signals */
  87. if (signal_pending(current))
  88. goto interrupted;
  89. error = 0;
  90. *timeo_p = schedule_timeout(*timeo_p);
  91. out:
  92. finish_wait(sk->sk_sleep, &wait);
  93. return error;
  94. interrupted:
  95. error = sock_intr_errno(*timeo_p);
  96. out_err:
  97. *err = error;
  98. goto out;
  99. out_noerr:
  100. *err = 0;
  101. error = 1;
  102. goto out;
  103. }
  104. /**
  105. * __skb_recv_datagram - Receive a datagram skbuff
  106. * @sk: socket
  107. * @flags: MSG_ flags
  108. * @peeked: returns non-zero if this packet has been seen before
  109. * @err: error code returned
  110. *
  111. * Get a datagram skbuff, understands the peeking, nonblocking wakeups
  112. * and possible races. This replaces identical code in packet, raw and
  113. * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
  114. * the long standing peek and read race for datagram sockets. If you
  115. * alter this routine remember it must be re-entrant.
  116. *
  117. * This function will lock the socket if a skb is returned, so the caller
  118. * needs to unlock the socket in that case (usually by calling
  119. * skb_free_datagram)
  120. *
  121. * * It does not lock socket since today. This function is
  122. * * free of race conditions. This measure should/can improve
  123. * * significantly datagram socket latencies at high loads,
  124. * * when data copying to user space takes lots of time.
  125. * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
  126. * * 8) Great win.)
  127. * * --ANK (980729)
  128. *
  129. * The order of the tests when we find no data waiting are specified
  130. * quite explicitly by POSIX 1003.1g, don't change them without having
  131. * the standard around please.
  132. */
  133. struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
  134. int *peeked, int *err)
  135. {
  136. struct sk_buff *skb;
  137. long timeo;
  138. /*
  139. * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
  140. */
  141. int error = sock_error(sk);
  142. if (error)
  143. goto no_packet;
  144. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  145. do {
  146. /* Again only user level code calls this function, so nothing
  147. * interrupt level will suddenly eat the receive_queue.
  148. *
  149. * Look at current nfs client by the way...
  150. * However, this function was corrent in any case. 8)
  151. */
  152. unsigned long cpu_flags;
  153. spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
  154. skb = skb_peek(&sk->sk_receive_queue);
  155. if (skb) {
  156. *peeked = skb->peeked;
  157. if (flags & MSG_PEEK) {
  158. skb->peeked = 1;
  159. atomic_inc(&skb->users);
  160. } else
  161. __skb_unlink(skb, &sk->sk_receive_queue);
  162. }
  163. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
  164. if (skb)
  165. return skb;
  166. /* User doesn't want to wait */
  167. error = -EAGAIN;
  168. if (!timeo)
  169. goto no_packet;
  170. } while (!wait_for_packet(sk, err, &timeo));
  171. return NULL;
  172. no_packet:
  173. *err = error;
  174. return NULL;
  175. }
  176. EXPORT_SYMBOL(__skb_recv_datagram);
  177. struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
  178. int noblock, int *err)
  179. {
  180. int peeked;
  181. return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  182. &peeked, err);
  183. }
  184. void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
  185. {
  186. kfree_skb(skb);
  187. sk_mem_reclaim_partial(sk);
  188. }
  189. /**
  190. * skb_kill_datagram - Free a datagram skbuff forcibly
  191. * @sk: socket
  192. * @skb: datagram skbuff
  193. * @flags: MSG_ flags
  194. *
  195. * This function frees a datagram skbuff that was received by
  196. * skb_recv_datagram. The flags argument must match the one
  197. * used for skb_recv_datagram.
  198. *
  199. * If the MSG_PEEK flag is set, and the packet is still on the
  200. * receive queue of the socket, it will be taken off the queue
  201. * before it is freed.
  202. *
  203. * This function currently only disables BH when acquiring the
  204. * sk_receive_queue lock. Therefore it must not be used in a
  205. * context where that lock is acquired in an IRQ context.
  206. *
  207. * It returns 0 if the packet was removed by us.
  208. */
  209. int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
  210. {
  211. int err = 0;
  212. if (flags & MSG_PEEK) {
  213. err = -ENOENT;
  214. spin_lock_bh(&sk->sk_receive_queue.lock);
  215. if (skb == skb_peek(&sk->sk_receive_queue)) {
  216. __skb_unlink(skb, &sk->sk_receive_queue);
  217. atomic_dec(&skb->users);
  218. err = 0;
  219. }
  220. spin_unlock_bh(&sk->sk_receive_queue.lock);
  221. }
  222. skb_free_datagram(sk, skb);
  223. return err;
  224. }
  225. EXPORT_SYMBOL(skb_kill_datagram);
  226. /**
  227. * skb_copy_datagram_iovec - Copy a datagram to an iovec.
  228. * @skb: buffer to copy
  229. * @offset: offset in the buffer to start copying from
  230. * @to: io vector to copy to
  231. * @len: amount of data to copy from buffer to iovec
  232. *
  233. * Note: the iovec is modified during the copy.
  234. */
  235. int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
  236. struct iovec *to, int len)
  237. {
  238. int start = skb_headlen(skb);
  239. int i, copy = start - offset;
  240. /* Copy header. */
  241. if (copy > 0) {
  242. if (copy > len)
  243. copy = len;
  244. if (memcpy_toiovec(to, skb->data + offset, copy))
  245. goto fault;
  246. if ((len -= copy) == 0)
  247. return 0;
  248. offset += copy;
  249. }
  250. /* Copy paged appendix. Hmm... why does this look so complicated? */
  251. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  252. int end;
  253. WARN_ON(start > offset + len);
  254. end = start + skb_shinfo(skb)->frags[i].size;
  255. if ((copy = end - offset) > 0) {
  256. int err;
  257. u8 *vaddr;
  258. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  259. struct page *page = frag->page;
  260. if (copy > len)
  261. copy = len;
  262. vaddr = kmap(page);
  263. err = memcpy_toiovec(to, vaddr + frag->page_offset +
  264. offset - start, copy);
  265. kunmap(page);
  266. if (err)
  267. goto fault;
  268. if (!(len -= copy))
  269. return 0;
  270. offset += copy;
  271. }
  272. start = end;
  273. }
  274. if (skb_shinfo(skb)->frag_list) {
  275. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  276. for (; list; list = list->next) {
  277. int end;
  278. WARN_ON(start > offset + len);
  279. end = start + list->len;
  280. if ((copy = end - offset) > 0) {
  281. if (copy > len)
  282. copy = len;
  283. if (skb_copy_datagram_iovec(list,
  284. offset - start,
  285. to, copy))
  286. goto fault;
  287. if ((len -= copy) == 0)
  288. return 0;
  289. offset += copy;
  290. }
  291. start = end;
  292. }
  293. }
  294. if (!len)
  295. return 0;
  296. fault:
  297. return -EFAULT;
  298. }
  299. /**
  300. * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
  301. * @skb: buffer to copy
  302. * @offset: offset in the buffer to start copying to
  303. * @from: io vector to copy to
  304. * @len: amount of data to copy to buffer from iovec
  305. *
  306. * Returns 0 or -EFAULT.
  307. * Note: the iovec is modified during the copy.
  308. */
  309. int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
  310. struct iovec *from, int len)
  311. {
  312. int start = skb_headlen(skb);
  313. int i, copy = start - offset;
  314. /* Copy header. */
  315. if (copy > 0) {
  316. if (copy > len)
  317. copy = len;
  318. if (memcpy_fromiovec(skb->data + offset, from, copy))
  319. goto fault;
  320. if ((len -= copy) == 0)
  321. return 0;
  322. offset += copy;
  323. }
  324. /* Copy paged appendix. Hmm... why does this look so complicated? */
  325. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  326. int end;
  327. WARN_ON(start > offset + len);
  328. end = start + skb_shinfo(skb)->frags[i].size;
  329. if ((copy = end - offset) > 0) {
  330. int err;
  331. u8 *vaddr;
  332. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  333. struct page *page = frag->page;
  334. if (copy > len)
  335. copy = len;
  336. vaddr = kmap(page);
  337. err = memcpy_fromiovec(vaddr + frag->page_offset +
  338. offset - start, from, copy);
  339. kunmap(page);
  340. if (err)
  341. goto fault;
  342. if (!(len -= copy))
  343. return 0;
  344. offset += copy;
  345. }
  346. start = end;
  347. }
  348. if (skb_shinfo(skb)->frag_list) {
  349. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  350. for (; list; list = list->next) {
  351. int end;
  352. WARN_ON(start > offset + len);
  353. end = start + list->len;
  354. if ((copy = end - offset) > 0) {
  355. if (copy > len)
  356. copy = len;
  357. if (skb_copy_datagram_from_iovec(list,
  358. offset - start,
  359. from, copy))
  360. goto fault;
  361. if ((len -= copy) == 0)
  362. return 0;
  363. offset += copy;
  364. }
  365. start = end;
  366. }
  367. }
  368. if (!len)
  369. return 0;
  370. fault:
  371. return -EFAULT;
  372. }
  373. EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
  374. static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
  375. u8 __user *to, int len,
  376. __wsum *csump)
  377. {
  378. int start = skb_headlen(skb);
  379. int pos = 0;
  380. int i, copy = start - offset;
  381. /* Copy header. */
  382. if (copy > 0) {
  383. int err = 0;
  384. if (copy > len)
  385. copy = len;
  386. *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
  387. *csump, &err);
  388. if (err)
  389. goto fault;
  390. if ((len -= copy) == 0)
  391. return 0;
  392. offset += copy;
  393. to += copy;
  394. pos = copy;
  395. }
  396. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  397. int end;
  398. WARN_ON(start > offset + len);
  399. end = start + skb_shinfo(skb)->frags[i].size;
  400. if ((copy = end - offset) > 0) {
  401. __wsum csum2;
  402. int err = 0;
  403. u8 *vaddr;
  404. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  405. struct page *page = frag->page;
  406. if (copy > len)
  407. copy = len;
  408. vaddr = kmap(page);
  409. csum2 = csum_and_copy_to_user(vaddr +
  410. frag->page_offset +
  411. offset - start,
  412. to, copy, 0, &err);
  413. kunmap(page);
  414. if (err)
  415. goto fault;
  416. *csump = csum_block_add(*csump, csum2, pos);
  417. if (!(len -= copy))
  418. return 0;
  419. offset += copy;
  420. to += copy;
  421. pos += copy;
  422. }
  423. start = end;
  424. }
  425. if (skb_shinfo(skb)->frag_list) {
  426. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  427. for (; list; list=list->next) {
  428. int end;
  429. WARN_ON(start > offset + len);
  430. end = start + list->len;
  431. if ((copy = end - offset) > 0) {
  432. __wsum csum2 = 0;
  433. if (copy > len)
  434. copy = len;
  435. if (skb_copy_and_csum_datagram(list,
  436. offset - start,
  437. to, copy,
  438. &csum2))
  439. goto fault;
  440. *csump = csum_block_add(*csump, csum2, pos);
  441. if ((len -= copy) == 0)
  442. return 0;
  443. offset += copy;
  444. to += copy;
  445. pos += copy;
  446. }
  447. start = end;
  448. }
  449. }
  450. if (!len)
  451. return 0;
  452. fault:
  453. return -EFAULT;
  454. }
  455. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
  456. {
  457. __sum16 sum;
  458. sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
  459. if (likely(!sum)) {
  460. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  461. netdev_rx_csum_fault(skb->dev);
  462. skb->ip_summed = CHECKSUM_UNNECESSARY;
  463. }
  464. return sum;
  465. }
  466. EXPORT_SYMBOL(__skb_checksum_complete_head);
  467. __sum16 __skb_checksum_complete(struct sk_buff *skb)
  468. {
  469. return __skb_checksum_complete_head(skb, skb->len);
  470. }
  471. EXPORT_SYMBOL(__skb_checksum_complete);
  472. /**
  473. * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
  474. * @skb: skbuff
  475. * @hlen: hardware length
  476. * @iov: io vector
  477. *
  478. * Caller _must_ check that skb will fit to this iovec.
  479. *
  480. * Returns: 0 - success.
  481. * -EINVAL - checksum failure.
  482. * -EFAULT - fault during copy. Beware, in this case iovec
  483. * can be modified!
  484. */
  485. int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  486. int hlen, struct iovec *iov)
  487. {
  488. __wsum csum;
  489. int chunk = skb->len - hlen;
  490. if (!chunk)
  491. return 0;
  492. /* Skip filled elements.
  493. * Pretty silly, look at memcpy_toiovec, though 8)
  494. */
  495. while (!iov->iov_len)
  496. iov++;
  497. if (iov->iov_len < chunk) {
  498. if (__skb_checksum_complete(skb))
  499. goto csum_error;
  500. if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
  501. goto fault;
  502. } else {
  503. csum = csum_partial(skb->data, hlen, skb->csum);
  504. if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
  505. chunk, &csum))
  506. goto fault;
  507. if (csum_fold(csum))
  508. goto csum_error;
  509. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  510. netdev_rx_csum_fault(skb->dev);
  511. iov->iov_len -= chunk;
  512. iov->iov_base += chunk;
  513. }
  514. return 0;
  515. csum_error:
  516. return -EINVAL;
  517. fault:
  518. return -EFAULT;
  519. }
  520. /**
  521. * datagram_poll - generic datagram poll
  522. * @file: file struct
  523. * @sock: socket
  524. * @wait: poll table
  525. *
  526. * Datagram poll: Again totally generic. This also handles
  527. * sequenced packet sockets providing the socket receive queue
  528. * is only ever holding data ready to receive.
  529. *
  530. * Note: when you _don't_ use this routine for this protocol,
  531. * and you use a different write policy from sock_writeable()
  532. * then please supply your own write_space callback.
  533. */
  534. unsigned int datagram_poll(struct file *file, struct socket *sock,
  535. poll_table *wait)
  536. {
  537. struct sock *sk = sock->sk;
  538. unsigned int mask;
  539. poll_wait(file, sk->sk_sleep, wait);
  540. mask = 0;
  541. /* exceptional events? */
  542. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  543. mask |= POLLERR;
  544. if (sk->sk_shutdown & RCV_SHUTDOWN)
  545. mask |= POLLRDHUP;
  546. if (sk->sk_shutdown == SHUTDOWN_MASK)
  547. mask |= POLLHUP;
  548. /* readable? */
  549. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  550. (sk->sk_shutdown & RCV_SHUTDOWN))
  551. mask |= POLLIN | POLLRDNORM;
  552. /* Connection-based need to check for termination and startup */
  553. if (connection_based(sk)) {
  554. if (sk->sk_state == TCP_CLOSE)
  555. mask |= POLLHUP;
  556. /* connection hasn't started yet? */
  557. if (sk->sk_state == TCP_SYN_SENT)
  558. return mask;
  559. }
  560. /* writable? */
  561. if (sock_writeable(sk))
  562. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  563. else
  564. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  565. return mask;
  566. }
  567. EXPORT_SYMBOL(datagram_poll);
  568. EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
  569. EXPORT_SYMBOL(skb_copy_datagram_iovec);
  570. EXPORT_SYMBOL(skb_free_datagram);
  571. EXPORT_SYMBOL(skb_recv_datagram);