PageRenderTime 69ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/TVCam/ti-davinci/net/core/datagram.c

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