PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/release/src-rt/linux/linux-2.6/net/core/datagram.c

https://gitlab.com/envieidoc/tomato
C | 551 lines | 336 code | 65 blank | 150 comment | 81 complexity | aace3104a37b754af43ae24f0d189629 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@redhat.com>. (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. * @noblock: blocking operation?
  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 noblock, 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, noblock);
  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. if (flags & MSG_PEEK) {
  153. unsigned long cpu_flags;
  154. spin_lock_irqsave(&sk->sk_receive_queue.lock,
  155. cpu_flags);
  156. skb = skb_peek(&sk->sk_receive_queue);
  157. if (skb)
  158. atomic_inc(&skb->users);
  159. spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
  160. cpu_flags);
  161. } else
  162. skb = skb_dequeue(&sk->sk_receive_queue);
  163. if (skb)
  164. return skb;
  165. /* User doesn't want to wait */
  166. error = -EAGAIN;
  167. if (!timeo)
  168. goto no_packet;
  169. } while (!wait_for_packet(sk, err, &timeo));
  170. return NULL;
  171. no_packet:
  172. *err = error;
  173. return NULL;
  174. }
  175. void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
  176. {
  177. kfree_skb(skb);
  178. }
  179. EXPORT_SYMBOL(skb_free_datagram);
  180. void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
  181. {
  182. lock_sock(sk);
  183. skb_free_datagram(sk, skb);
  184. release_sock(sk);
  185. }
  186. EXPORT_SYMBOL(skb_free_datagram_locked);
  187. /**
  188. * skb_kill_datagram - Free a datagram skbuff forcibly
  189. * @sk: socket
  190. * @skb: datagram skbuff
  191. * @flags: MSG_ flags
  192. *
  193. * This function frees a datagram skbuff that was received by
  194. * skb_recv_datagram. The flags argument must match the one
  195. * used for skb_recv_datagram.
  196. *
  197. * If the MSG_PEEK flag is set, and the packet is still on the
  198. * receive queue of the socket, it will be taken off the queue
  199. * before it is freed.
  200. *
  201. * This function currently only disables BH when acquiring the
  202. * sk_receive_queue lock. Therefore it must not be used in a
  203. * context where that lock is acquired in an IRQ context.
  204. */
  205. void skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
  206. {
  207. if (flags & MSG_PEEK) {
  208. spin_lock_bh(&sk->sk_receive_queue.lock);
  209. if (skb == skb_peek(&sk->sk_receive_queue)) {
  210. __skb_unlink(skb, &sk->sk_receive_queue);
  211. atomic_dec(&skb->users);
  212. }
  213. spin_unlock_bh(&sk->sk_receive_queue.lock);
  214. }
  215. kfree_skb(skb);
  216. }
  217. EXPORT_SYMBOL(skb_kill_datagram);
  218. /**
  219. * skb_copy_datagram_iovec - Copy a datagram to an iovec.
  220. * @skb: buffer to copy
  221. * @offset: offset in the buffer to start copying from
  222. * @to: io vector to copy to
  223. * @len: amount of data to copy from buffer to iovec
  224. *
  225. * Note: the iovec is modified during the copy.
  226. */
  227. int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
  228. struct iovec *to, int len)
  229. {
  230. int start = skb_headlen(skb);
  231. int i, copy = start - offset;
  232. /* Copy header. */
  233. if (copy > 0) {
  234. if (copy > len)
  235. copy = len;
  236. if (memcpy_toiovec(to, skb->data + offset, copy))
  237. goto fault;
  238. if ((len -= copy) == 0)
  239. return 0;
  240. offset += copy;
  241. }
  242. /* Copy paged appendix. Hmm... why does this look so complicated? */
  243. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  244. int end;
  245. BUG_TRAP(start <= offset + len);
  246. end = start + skb_shinfo(skb)->frags[i].size;
  247. if ((copy = end - offset) > 0) {
  248. int err;
  249. u8 *vaddr;
  250. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  251. struct page *page = frag->page;
  252. if (copy > len)
  253. copy = len;
  254. vaddr = kmap(page);
  255. err = memcpy_toiovec(to, vaddr + frag->page_offset +
  256. offset - start, copy);
  257. kunmap(page);
  258. if (err)
  259. goto fault;
  260. if (!(len -= copy))
  261. return 0;
  262. offset += copy;
  263. }
  264. start = end;
  265. }
  266. if (skb_shinfo(skb)->frag_list) {
  267. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  268. for (; list; list = list->next) {
  269. int end;
  270. BUG_TRAP(start <= offset + len);
  271. end = start + list->len;
  272. if ((copy = end - offset) > 0) {
  273. if (copy > len)
  274. copy = len;
  275. if (skb_copy_datagram_iovec(list,
  276. offset - start,
  277. to, copy))
  278. goto fault;
  279. if ((len -= copy) == 0)
  280. return 0;
  281. offset += copy;
  282. }
  283. start = end;
  284. }
  285. }
  286. if (!len)
  287. return 0;
  288. fault:
  289. return -EFAULT;
  290. }
  291. static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
  292. u8 __user *to, int len,
  293. __wsum *csump)
  294. {
  295. int start = skb_headlen(skb);
  296. int pos = 0;
  297. int i, copy = start - offset;
  298. /* Copy header. */
  299. if (copy > 0) {
  300. int err = 0;
  301. if (copy > len)
  302. copy = len;
  303. *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
  304. *csump, &err);
  305. if (err)
  306. goto fault;
  307. if ((len -= copy) == 0)
  308. return 0;
  309. offset += copy;
  310. to += copy;
  311. pos = copy;
  312. }
  313. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  314. int end;
  315. BUG_TRAP(start <= offset + len);
  316. end = start + skb_shinfo(skb)->frags[i].size;
  317. if ((copy = end - offset) > 0) {
  318. __wsum csum2;
  319. int err = 0;
  320. u8 *vaddr;
  321. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  322. struct page *page = frag->page;
  323. if (copy > len)
  324. copy = len;
  325. vaddr = kmap(page);
  326. csum2 = csum_and_copy_to_user(vaddr +
  327. frag->page_offset +
  328. offset - start,
  329. to, copy, 0, &err);
  330. kunmap(page);
  331. if (err)
  332. goto fault;
  333. *csump = csum_block_add(*csump, csum2, pos);
  334. if (!(len -= copy))
  335. return 0;
  336. offset += copy;
  337. to += copy;
  338. pos += copy;
  339. }
  340. start = end;
  341. }
  342. if (skb_shinfo(skb)->frag_list) {
  343. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  344. for (; list; list=list->next) {
  345. int end;
  346. BUG_TRAP(start <= offset + len);
  347. end = start + list->len;
  348. if ((copy = end - offset) > 0) {
  349. __wsum csum2 = 0;
  350. if (copy > len)
  351. copy = len;
  352. if (skb_copy_and_csum_datagram(list,
  353. offset - start,
  354. to, copy,
  355. &csum2))
  356. goto fault;
  357. *csump = csum_block_add(*csump, csum2, pos);
  358. if ((len -= copy) == 0)
  359. return 0;
  360. offset += copy;
  361. to += copy;
  362. pos += copy;
  363. }
  364. start = end;
  365. }
  366. }
  367. if (!len)
  368. return 0;
  369. fault:
  370. return -EFAULT;
  371. }
  372. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
  373. {
  374. __sum16 sum;
  375. sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
  376. if (likely(!sum)) {
  377. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  378. netdev_rx_csum_fault(skb->dev);
  379. skb->ip_summed = CHECKSUM_UNNECESSARY;
  380. }
  381. return sum;
  382. }
  383. EXPORT_SYMBOL(__skb_checksum_complete_head);
  384. __sum16 __skb_checksum_complete(struct sk_buff *skb)
  385. {
  386. return __skb_checksum_complete_head(skb, skb->len);
  387. }
  388. EXPORT_SYMBOL(__skb_checksum_complete);
  389. /**
  390. * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
  391. * @skb: skbuff
  392. * @hlen: hardware length
  393. * @iov: io vector
  394. *
  395. * Caller _must_ check that skb will fit to this iovec.
  396. *
  397. * Returns: 0 - success.
  398. * -EINVAL - checksum failure.
  399. * -EFAULT - fault during copy. Beware, in this case iovec
  400. * can be modified!
  401. */
  402. int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  403. int hlen, struct iovec *iov)
  404. {
  405. __wsum csum;
  406. int chunk = skb->len - hlen;
  407. if (!chunk)
  408. return 0;
  409. /* Skip filled elements.
  410. * Pretty silly, look at memcpy_toiovec, though 8)
  411. */
  412. while (!iov->iov_len)
  413. iov++;
  414. if (iov->iov_len < chunk) {
  415. if (__skb_checksum_complete(skb))
  416. goto csum_error;
  417. if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
  418. goto fault;
  419. } else {
  420. csum = csum_partial(skb->data, hlen, skb->csum);
  421. if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
  422. chunk, &csum))
  423. goto fault;
  424. if (csum_fold(csum))
  425. goto csum_error;
  426. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  427. netdev_rx_csum_fault(skb->dev);
  428. iov->iov_len -= chunk;
  429. iov->iov_base += chunk;
  430. }
  431. return 0;
  432. csum_error:
  433. return -EINVAL;
  434. fault:
  435. return -EFAULT;
  436. }
  437. /**
  438. * datagram_poll - generic datagram poll
  439. * @file: file struct
  440. * @sock: socket
  441. * @wait: poll table
  442. *
  443. * Datagram poll: Again totally generic. This also handles
  444. * sequenced packet sockets providing the socket receive queue
  445. * is only ever holding data ready to receive.
  446. *
  447. * Note: when you _don't_ use this routine for this protocol,
  448. * and you use a different write policy from sock_writeable()
  449. * then please supply your own write_space callback.
  450. */
  451. unsigned int datagram_poll(struct file *file, struct socket *sock,
  452. poll_table *wait)
  453. {
  454. struct sock *sk = sock->sk;
  455. unsigned int mask;
  456. poll_wait(file, sk->sk_sleep, wait);
  457. mask = 0;
  458. /* exceptional events? */
  459. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  460. mask |= POLLERR;
  461. if (sk->sk_shutdown & RCV_SHUTDOWN)
  462. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  463. if (sk->sk_shutdown == SHUTDOWN_MASK)
  464. mask |= POLLHUP;
  465. /* readable? */
  466. if (!skb_queue_empty(&sk->sk_receive_queue))
  467. mask |= POLLIN | POLLRDNORM;
  468. /* Connection-based need to check for termination and startup */
  469. if (connection_based(sk)) {
  470. if (sk->sk_state == TCP_CLOSE)
  471. mask |= POLLHUP;
  472. /* connection hasn't started yet? */
  473. if (sk->sk_state == TCP_SYN_SENT)
  474. return mask;
  475. }
  476. /* writable? */
  477. if (sock_writeable(sk))
  478. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  479. else
  480. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  481. return mask;
  482. }
  483. EXPORT_SYMBOL(datagram_poll);
  484. EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
  485. EXPORT_SYMBOL(skb_copy_datagram_iovec);
  486. EXPORT_SYMBOL(skb_recv_datagram);