datagram.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:11k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * SUCS NET3:
  3.  *
  4.  * Generic datagram handling routines. These are generic for all protocols. Possibly a generic IP version on top
  5.  * of these would make sense. Not tonight however 8-).
  6.  * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and NetROM layer all have identical poll code and mostly
  7.  * identical recvmsg() code. So we share it here. The poll was shared before but buried in udp.c so I moved it.
  8.  *
  9.  * Authors: Alan Cox <alan@redhat.com>. (datagram_poll() from old udp.c code)
  10.  *
  11.  * Fixes:
  12.  * Alan Cox : NULL return from skb_peek_copy() understood
  13.  * Alan Cox : Rewrote skb_read_datagram to avoid the skb_peek_copy stuff.
  14.  * Alan Cox : Added support for SOCK_SEQPACKET. IPX can no longer use the SO_TYPE hack but
  15.  * AX.25 now works right, and SPX is feasible.
  16.  * Alan Cox : Fixed write poll of non IP protocol crash.
  17.  * Florian  La Roche: Changed for my new skbuff handling.
  18.  * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET.
  19.  * Linus Torvalds : BSD semantic fixes.
  20.  * Alan Cox : Datagram iovec handling
  21.  * Darryl Miles : Fixed non-blocking SOCK_STREAM.
  22.  * Alan Cox : POSIXisms
  23.  * Pete Wyckoff    :       Unconnected accept() fix.
  24.  *
  25.  */
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/system.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/errno.h>
  33. #include <linux/sched.h>
  34. #include <linux/inet.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/rtnetlink.h>
  37. #include <linux/poll.h>
  38. #include <linux/highmem.h>
  39. #include <net/protocol.h>
  40. #include <linux/skbuff.h>
  41. #include <net/sock.h>
  42. #include <net/checksum.h>
  43. /*
  44.  * Is a socket 'connection oriented' ?
  45.  */
  46.  
  47. static inline int connection_based(struct sock *sk)
  48. {
  49. return (sk->type==SOCK_SEQPACKET || sk->type==SOCK_STREAM);
  50. }
  51. /*
  52.  * Wait for a packet..
  53.  */
  54. static int wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  55. {
  56. int error;
  57. DECLARE_WAITQUEUE(wait, current);
  58. __set_current_state(TASK_INTERRUPTIBLE);
  59. add_wait_queue_exclusive(sk->sleep, &wait);
  60. /* Socket errors? */
  61. error = sock_error(sk);
  62. if (error)
  63. goto out_err;
  64. if (!skb_queue_empty(&sk->receive_queue))
  65. goto ready;
  66. /* Socket shut down? */
  67. if (sk->shutdown & RCV_SHUTDOWN)
  68. goto out_noerr;
  69. /* Sequenced packets can come disconnected. If so we report the problem */
  70. error = -ENOTCONN;
  71. if(connection_based(sk) && !(sk->state==TCP_ESTABLISHED || sk->state==TCP_LISTEN))
  72. goto out_err;
  73. /* handle signals */
  74. if (signal_pending(current))
  75. goto interrupted;
  76. *timeo_p = schedule_timeout(*timeo_p);
  77. ready:
  78. current->state = TASK_RUNNING;
  79. remove_wait_queue(sk->sleep, &wait);
  80. return 0;
  81. interrupted:
  82. error = sock_intr_errno(*timeo_p);
  83. out_err:
  84. *err = error;
  85. out:
  86. current->state = TASK_RUNNING;
  87. remove_wait_queue(sk->sleep, &wait);
  88. return error;
  89. out_noerr:
  90. *err = 0;
  91. error = 1;
  92. goto out;
  93. }
  94. /*
  95.  * Get a datagram skbuff, understands the peeking, nonblocking wakeups and possible
  96.  * races. This replaces identical code in packet,raw and udp, as well as the IPX
  97.  * AX.25 and Appletalk. It also finally fixes the long standing peek and read
  98.  * race for datagram sockets. If you alter this routine remember it must be
  99.  * re-entrant.
  100.  *
  101.  * This function will lock the socket if a skb is returned, so the caller
  102.  * needs to unlock the socket in that case (usually by calling skb_free_datagram)
  103.  *
  104.  * * It does not lock socket since today. This function is
  105.  * * free of race conditions. This measure should/can improve
  106.  * * significantly datagram socket latencies at high loads,
  107.  * * when data copying to user space takes lots of time.
  108.  * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
  109.  * *  8) Great win.)
  110.  * *                     --ANK (980729)
  111.  *
  112.  * The order of the tests when we find no data waiting are specified
  113.  * quite explicitly by POSIX 1003.1g, don't change them without having
  114.  * the standard around please.
  115.  */
  116. struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock, int *err)
  117. {
  118. int error;
  119. struct sk_buff *skb;
  120. long timeo;
  121. /* Caller is allowed not to check sk->err before skb_recv_datagram() */
  122. error = sock_error(sk);
  123. if (error)
  124. goto no_packet;
  125. timeo = sock_rcvtimeo(sk, noblock);
  126. do {
  127. /* Again only user level code calls this function, so nothing interrupt level
  128.    will suddenly eat the receive_queue.
  129.    Look at current nfs client by the way...
  130.    However, this function was corrent in any case. 8)
  131.  */
  132. if (flags & MSG_PEEK)
  133. {
  134. unsigned long cpu_flags;
  135. spin_lock_irqsave(&sk->receive_queue.lock, cpu_flags);
  136. skb = skb_peek(&sk->receive_queue);
  137. if(skb!=NULL)
  138. atomic_inc(&skb->users);
  139. spin_unlock_irqrestore(&sk->receive_queue.lock, cpu_flags);
  140. } else
  141. skb = skb_dequeue(&sk->receive_queue);
  142. if (skb)
  143. return skb;
  144. /* User doesn't want to wait */
  145. error = -EAGAIN;
  146. if (!timeo)
  147. goto no_packet;
  148. } while (wait_for_packet(sk, err, &timeo) == 0);
  149. return NULL;
  150. no_packet:
  151. *err = error;
  152. return NULL;
  153. }
  154. void skb_free_datagram(struct sock * sk, struct sk_buff *skb)
  155. {
  156. kfree_skb(skb);
  157. }
  158. /*
  159.  * Copy a datagram to a linear buffer.
  160.  */
  161. int skb_copy_datagram(const struct sk_buff *skb, int offset, char *to, int size)
  162. {
  163. struct iovec iov = { to, size };
  164. return skb_copy_datagram_iovec(skb, offset, &iov, size);
  165. }
  166. /*
  167.  * Copy a datagram to an iovec.
  168.  * Note: the iovec is modified during the copy.
  169.  */
  170. int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, struct iovec *to,
  171.     int len)
  172. {
  173. int i, copy;
  174. int start = skb->len - skb->data_len;
  175. /* Copy header. */
  176. if ((copy = start-offset) > 0) {
  177. if (copy > len)
  178. copy = len;
  179. if (memcpy_toiovec(to, skb->data + offset, copy))
  180. goto fault;
  181. if ((len -= copy) == 0)
  182. return 0;
  183. offset += copy;
  184. }
  185. /* Copy paged appendix. Hmm... why does this look so complicated? */
  186. for (i=0; i<skb_shinfo(skb)->nr_frags; i++) {
  187. int end;
  188. BUG_TRAP(start <= offset+len);
  189. end = start + skb_shinfo(skb)->frags[i].size;
  190. if ((copy = end-offset) > 0) {
  191. int err;
  192. u8  *vaddr;
  193. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  194. struct page *page = frag->page;
  195. if (copy > len)
  196. copy = len;
  197. vaddr = kmap(page);
  198. err = memcpy_toiovec(to, vaddr + frag->page_offset +
  199.      offset-start, copy);
  200. kunmap(page);
  201. if (err)
  202. goto fault;
  203. if (!(len -= copy))
  204. return 0;
  205. offset += copy;
  206. }
  207. start = end;
  208. }
  209. if (skb_shinfo(skb)->frag_list) {
  210. struct sk_buff *list;
  211. for (list = skb_shinfo(skb)->frag_list; list; list=list->next) {
  212. int end;
  213. BUG_TRAP(start <= offset+len);
  214. end = start + list->len;
  215. if ((copy = end-offset) > 0) {
  216. if (copy > len)
  217. copy = len;
  218. if (skb_copy_datagram_iovec(list, offset-start, to, copy))
  219. goto fault;
  220. if ((len -= copy) == 0)
  221. return 0;
  222. offset += copy;
  223. }
  224. start = end;
  225. }
  226. }
  227. if (len == 0)
  228. return 0;
  229. fault:
  230. return -EFAULT;
  231. }
  232. int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int *csump)
  233. {
  234. int i, copy;
  235. int start = skb->len - skb->data_len;
  236. int pos = 0;
  237. /* Copy header. */
  238. if ((copy = start-offset) > 0) {
  239. int err = 0;
  240. if (copy > len)
  241. copy = len;
  242. *csump = csum_and_copy_to_user(skb->data+offset, to, copy, *csump, &err);
  243. if (err)
  244. goto fault;
  245. if ((len -= copy) == 0)
  246. return 0;
  247. offset += copy;
  248. to += copy;
  249. pos = copy;
  250. }
  251. for (i=0; i<skb_shinfo(skb)->nr_frags; i++) {
  252. int end;
  253. BUG_TRAP(start <= offset+len);
  254. end = start + skb_shinfo(skb)->frags[i].size;
  255. if ((copy = end-offset) > 0) {
  256. unsigned int csum2;
  257. int err = 0;
  258. u8  *vaddr;
  259. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  260. struct page *page = frag->page;
  261. if (copy > len)
  262. copy = len;
  263. vaddr = kmap(page);
  264. csum2 = csum_and_copy_to_user(vaddr + frag->page_offset +
  265.       offset-start, to, copy, 0, &err);
  266. kunmap(page);
  267. if (err)
  268. goto fault;
  269. *csump = csum_block_add(*csump, csum2, pos);
  270. if (!(len -= copy))
  271. return 0;
  272. offset += copy;
  273. to += copy;
  274. pos += copy;
  275. }
  276. start = end;
  277. }
  278. if (skb_shinfo(skb)->frag_list) {
  279. struct sk_buff *list;
  280. for (list = skb_shinfo(skb)->frag_list; list; list=list->next) {
  281. int end;
  282. BUG_TRAP(start <= offset+len);
  283. end = start + list->len;
  284. if ((copy = end-offset) > 0) {
  285. unsigned int csum2 = 0;
  286. if (copy > len)
  287. copy = len;
  288. if (skb_copy_and_csum_datagram(list, offset-start, to, copy, &csum2))
  289. goto fault;
  290. *csump = csum_block_add(*csump, csum2, pos);
  291. if ((len -= copy) == 0)
  292. return 0;
  293. offset += copy;
  294. to += copy;
  295. pos += copy;
  296. }
  297. start = end;
  298. }
  299. }
  300. if (len == 0)
  301. return 0;
  302. fault:
  303. return -EFAULT;
  304. }
  305. /* Copy and checkum skb to user iovec. Caller _must_ check that
  306.    skb will fit to this iovec.
  307.    Returns: 0       - success.
  308.             -EINVAL - checksum failure.
  309.     -EFAULT - fault during copy. Beware, in this case iovec can be
  310.               modified!
  311.  */
  312. int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb, int hlen, struct iovec *iov)
  313. {
  314. unsigned int csum;
  315. int chunk = skb->len - hlen;
  316. /* Skip filled elements. Pretty silly, look at memcpy_toiovec, though 8) */
  317. while (iov->iov_len == 0)
  318. iov++;
  319. if (iov->iov_len < chunk) {
  320. if ((unsigned short)csum_fold(skb_checksum(skb, 0, chunk+hlen, skb->csum)))
  321. goto csum_error;
  322. if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
  323. goto fault;
  324. } else {
  325. csum = csum_partial(skb->data, hlen, skb->csum);
  326. if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, chunk, &csum))
  327. goto fault;
  328. if ((unsigned short)csum_fold(csum))
  329. goto csum_error;
  330. iov->iov_len -= chunk;
  331. iov->iov_base += chunk;
  332. }
  333. return 0;
  334. csum_error:
  335. return -EINVAL;
  336. fault:
  337. return -EFAULT;
  338. }
  339. /*
  340.  * Datagram poll: Again totally generic. This also handles
  341.  * sequenced packet sockets providing the socket receive queue
  342.  * is only ever holding data ready to receive.
  343.  *
  344.  * Note: when you _don't_ use this routine for this protocol,
  345.  * and you use a different write policy from sock_writeable()
  346.  * then please supply your own write_space callback.
  347.  */
  348. unsigned int datagram_poll(struct file * file, struct socket *sock, poll_table *wait)
  349. {
  350. struct sock *sk = sock->sk;
  351. unsigned int mask;
  352. poll_wait(file, sk->sleep, wait);
  353. mask = 0;
  354. /* exceptional events? */
  355. if (sk->err || !skb_queue_empty(&sk->error_queue))
  356. mask |= POLLERR;
  357. if (sk->shutdown == SHUTDOWN_MASK)
  358. mask |= POLLHUP;
  359. /* readable? */
  360. if (!skb_queue_empty(&sk->receive_queue) || (sk->shutdown&RCV_SHUTDOWN))
  361. mask |= POLLIN | POLLRDNORM;
  362. /* Connection-based need to check for termination and startup */
  363. if (connection_based(sk)) {
  364. if (sk->state==TCP_CLOSE)
  365. mask |= POLLHUP;
  366. /* connection hasn't started yet? */
  367. if (sk->state == TCP_SYN_SENT)
  368. return mask;
  369. }
  370. /* writable? */
  371. if (sock_writeable(sk))
  372. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  373. else
  374. set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
  375. return mask;
  376. }