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 and the to be released IPX layer all have identical select code and mostly 7 * identical recvfrom() code. So we share it here. The select was shared before but buried in udp.c so I moved it. 8 * 9 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>. (datagram_select() 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 select 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 * 22 * Note: 23 * A lot of this will change when the protocol/socket separation 24 * occurs. Using this will make things reasonably clean. 25 */ 26
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <asm/segment.h>
30 #include <asm/system.h>
31 #include <linux/mm.h>
32 #include <linux/interrupt.h>
33 #include <linux/in.h>
34 #include <linux/errno.h>
35 #include <linux/sched.h>
36 #include <linux/inet.h>
37 #include <linux/netdevice.h>
38 #include <net/ip.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
41 #include <net/tcp.h>
42 #include <net/udp.h>
43 #include <linux/skbuff.h>
44 #include <net/sock.h>
45
46
47 /* 48 * Get a datagram skbuff, understands the peeking, nonblocking wakeups and possible 49 * races. This replaces identical code in packet,raw and udp, as well as the IPX 50 * AX.25 and Appletalk. It also finally fixes the long standing peek and read 51 * race for datagram sockets. If you alter this routine remember it must be 52 * re-entrant. 53 */ 54
55 structsk_buff *skb_recv_datagram(structsock *sk, unsignedflags, intnoblock, int *err)
/* */ 56 { 57 structsk_buff *skb;
58 unsignedlongintflags;
59
60 /* Socket is inuse - so the timer doesn't attack it */ 61 save_flags(intflags);
62 restart:
63 sk->inuse = 1;
64 while(skb_peek(&sk->receive_queue) == NULL) /* No data */ 65 { 66 /* If we are shutdown then no more data is going to appear. We are done */ 67 if (sk->shutdown & RCV_SHUTDOWN)
68 { 69 release_sock(sk);
70 *err=0;
71 returnNULL;
72 } 73
74 if(sk->err)
75 { 76 release_sock(sk);
77 *err=sock_error(sk);
78 restore_flags(intflags);
79 returnNULL;
80 } 81
82 /* Sequenced packets can come disconnected. If so we report the problem */ 83 if(sk->type==SOCK_SEQPACKET && sk->state!=TCP_ESTABLISHED)
84 { 85 release_sock(sk);
86 *err=-ENOTCONN;
87 returnNULL;
88 } 89
90 /* User doesn't want to wait */ 91 if (noblock)
92 { 93 release_sock(sk);
94 *err=-EAGAIN;
95 returnNULL;
96 } 97 release_sock(sk);
98
99 /* Interrupts off so that no packet arrives before we begin sleeping. 100 Otherwise we might miss our wake up */ 101 cli();
102 if (skb_peek(&sk->receive_queue) == NULL)
103 { 104 interruptible_sleep_on(sk->sleep);
105 /* Signals may need a restart of the syscall */ 106 if (current->signal & ~current->blocked)
107 { 108 restore_flags(intflags);;
109 *err=-ERESTARTSYS;
110 return(NULL);
111 } 112 if(sk->err != 0) /* Error while waiting for packet 113 eg an icmp sent earlier by the 114 peer has finally turned up now */ 115 { 116 *err = sock_error(sk);
117 restore_flags(intflags);
118 returnNULL;
119 } 120 } 121 sk->inuse = 1;
122 restore_flags(intflags);
123 } 124 /* Again only user level code calls this function, so nothing interrupt level 125 will suddenly eat the receive_queue */ 126 if (!(flags & MSG_PEEK))
127 { 128 skb=skb_dequeue(&sk->receive_queue);
129 if(skb!=NULL)
130 skb->users++;
131 else 132 gotorestart; /* Avoid race if someone beats us to the data */ 133 } 134 else 135 { 136 cli();
137 skb=skb_peek(&sk->receive_queue);
138 if(skb!=NULL)
139 skb->users++;
140 restore_flags(intflags);
141 if(skb==NULL) /* shouldn't happen but .. */ 142 *err=-EAGAIN;
143 } 144 returnskb;
145 } 146
147 voidskb_free_datagram(structsk_buff *skb)
/* */ 148 { 149 unsignedlongflags;
150
151 save_flags(flags);
152 cli();
153 skb->users--;
154 if(skb->users>0)
155 { 156 restore_flags(flags);
157 return;
158 } 159 /* See if it needs destroying */ 160 if(!skb->next && !skb->prev) /* Been dequeued by someone - ie it's read */ 161 kfree_skb(skb,FREE_READ);
162 restore_flags(flags);
163 } 164
165 /* 166 * Copy a datagram to a linear buffer. 167 */ 168
169 voidskb_copy_datagram(structsk_buff *skb, intoffset, char *to, intsize)
/* */ 170 { 171 memcpy_tofs(to,skb->h.raw+offset,size);
172 } 173
174
175 /* 176 * Copy a datagram to an iovec. 177 */ 178
179 voidskb_copy_datagram_iovec(structsk_buff *skb, intoffset, structiovec *to, intsize)
/* */ 180 { 181 memcpy_toiovec(to,skb->h.raw+offset,size);
182 } 183
184 /* 185 * Datagram select: Again totally generic. Moved from udp.c 186 * Now does seqpacket. 187 */ 188
189 intdatagram_select(structsock *sk, intsel_type, select_table *wait)
/* */ 190 { 191 select_wait(sk->sleep, wait);
192 switch(sel_type)
193 { 194 caseSEL_IN:
195 if (sk->err)
196 return 1;
197 if (sk->shutdown & RCV_SHUTDOWN)
198 return 1;
199 if (sk->type==SOCK_SEQPACKET && sk->state==TCP_CLOSE)
200 { 201 /* Connection closed: Wake up */ 202 return(1);
203 } 204 if (skb_peek(&sk->receive_queue) != NULL)
205 {/* This appears to be consistent 206 with other stacks */ 207 return(1);
208 } 209 return(0);
210
211 caseSEL_OUT:
212 if (sk->err)
213 return 1;
214 if (sk->shutdown & SEND_SHUTDOWN)
215 return 1;
216 if (sk->type==SOCK_SEQPACKET && sk->state==TCP_SYN_SENT)
217 { 218 /* Connection still in progress */ 219 break;
220 } 221 if (sk->prot && sock_wspace(sk) >= MIN_WRITE_SPACE)
222 { 223 return(1);
224 } 225 if (sk->prot==NULL && sk->sndbuf-sk->wmem_alloc >= MIN_WRITE_SPACE)
226 { 227 return(1);
228 } 229 return(0);
230
231 caseSEL_EX:
232 if (sk->err)
233 return(1); /* Socket has gone into error state (eg icmp error) */ 234 return(0);
235 } 236 return(0);
237 }