This source file includes following definitions.
- sock_setsockopt
- sock_getsockopt
- sock_wmalloc
- sock_rmalloc
- sock_rspace
- sock_wspace
- sock_wfree
- sock_rfree
- sock_alloc_send_skb
- release_sock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 #include <linux/config.h>
78 #include <linux/errno.h>
79 #include <linux/types.h>
80 #include <linux/socket.h>
81 #include <linux/in.h>
82 #include <linux/kernel.h>
83 #include <linux/major.h>
84 #include <linux/sched.h>
85 #include <linux/timer.h>
86 #include <linux/string.h>
87 #include <linux/sockios.h>
88 #include <linux/net.h>
89 #include <linux/fcntl.h>
90 #include <linux/mm.h>
91 #include <linux/interrupt.h>
92
93 #include <asm/segment.h>
94 #include <asm/system.h>
95
96 #include <linux/inet.h>
97 #include <linux/netdevice.h>
98 #include <net/ip.h>
99 #include <net/protocol.h>
100 #include <net/arp.h>
101 #include <net/rarp.h>
102 #include <net/route.h>
103 #include <net/tcp.h>
104 #include <net/udp.h>
105 #include <linux/skbuff.h>
106 #include <net/sock.h>
107 #include <net/raw.h>
108 #include <net/icmp.h>
109
110 #define min(a,b) ((a)<(b)?(a):(b))
111
112
113
114
115
116
117 int sock_setsockopt(struct sock *sk, int level, int optname,
118 char *optval, int optlen)
119 {
120 int val;
121 int valbool;
122 int err;
123 struct linger ling;
124
125 if (optval == NULL)
126 return(-EINVAL);
127
128 err=verify_area(VERIFY_READ, optval, sizeof(int));
129 if(err)
130 return err;
131
132 val = get_user((int *)optval);
133 valbool = val?1:0;
134
135 switch(optname)
136 {
137 case SO_DEBUG:
138 if(val && !suser())
139 return(-EPERM);
140 sk->debug=valbool;
141 return 0;
142 case SO_REUSEADDR:
143 sk->reuse = valbool;
144 return(0);
145 case SO_TYPE:
146 case SO_ERROR:
147 return(-ENOPROTOOPT);
148 case SO_DONTROUTE:
149 sk->localroute=valbool;
150 return 0;
151 case SO_BROADCAST:
152 sk->broadcast=valbool;
153 return 0;
154 case SO_SNDBUF:
155 if(val>32767)
156 val=32767;
157 if(val<256)
158 val=256;
159 sk->sndbuf=val;
160 return 0;
161
162 case SO_RCVBUF:
163 if(val>32767)
164 val=32767;
165 if(val<256)
166 val=256;
167 sk->rcvbuf=val;
168 return(0);
169
170 case SO_KEEPALIVE:
171 sk->keepopen = valbool;
172 return(0);
173
174 case SO_OOBINLINE:
175 sk->urginline = valbool;
176 return(0);
177
178 case SO_NO_CHECK:
179 sk->no_check = valbool;
180 return(0);
181
182 case SO_PRIORITY:
183 if (val >= 0 && val < DEV_NUMBUFFS)
184 {
185 sk->priority = val;
186 }
187 else
188 {
189 return(-EINVAL);
190 }
191 return(0);
192
193
194 case SO_LINGER:
195 err=verify_area(VERIFY_READ,optval,sizeof(ling));
196 if(err)
197 return err;
198 memcpy_fromfs(&ling,optval,sizeof(ling));
199 if(ling.l_onoff==0)
200 sk->linger=0;
201 else
202 {
203 sk->lingertime=ling.l_linger;
204 sk->linger=1;
205 }
206 return 0;
207
208
209 default:
210 return(-ENOPROTOOPT);
211 }
212 }
213
214
215 int sock_getsockopt(struct sock *sk, int level, int optname,
216 char *optval, int *optlen)
217 {
218 int val;
219 int err;
220 struct linger ling;
221
222 switch(optname)
223 {
224 case SO_DEBUG:
225 val = sk->debug;
226 break;
227
228 case SO_DONTROUTE:
229 val = sk->localroute;
230 break;
231
232 case SO_BROADCAST:
233 val= sk->broadcast;
234 break;
235
236 case SO_SNDBUF:
237 val=sk->sndbuf;
238 break;
239
240 case SO_RCVBUF:
241 val =sk->rcvbuf;
242 break;
243
244 case SO_REUSEADDR:
245 val = sk->reuse;
246 break;
247
248 case SO_KEEPALIVE:
249 val = sk->keepopen;
250 break;
251
252 case SO_TYPE:
253 val = sk->type;
254 break;
255
256 case SO_ERROR:
257 val = sk->err;
258 sk->err = 0;
259 break;
260
261 case SO_OOBINLINE:
262 val = sk->urginline;
263 break;
264
265 case SO_NO_CHECK:
266 val = sk->no_check;
267 break;
268
269 case SO_PRIORITY:
270 val = sk->priority;
271 break;
272
273 case SO_LINGER:
274 err=verify_area(VERIFY_WRITE,optval,sizeof(ling));
275 if(err)
276 return err;
277 err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
278 if(err)
279 return err;
280 put_fs_long(sizeof(ling),(unsigned long *)optlen);
281 ling.l_onoff=sk->linger;
282 ling.l_linger=sk->lingertime;
283 memcpy_tofs(optval,&ling,sizeof(ling));
284 return 0;
285
286
287
288 default:
289 return(-ENOPROTOOPT);
290 }
291 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
292 if(err)
293 return err;
294 put_fs_long(sizeof(int),(unsigned long *) optlen);
295
296 err=verify_area(VERIFY_WRITE, optval, sizeof(int));
297 if(err)
298 return err;
299 put_fs_long(val,(unsigned long *)optval);
300
301 return(0);
302 }
303
304
305 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, int priority)
306 {
307 if (sk)
308 {
309 if (sk->wmem_alloc + size < sk->sndbuf || force)
310 {
311 struct sk_buff * c = alloc_skb(size, priority);
312 if (c)
313 {
314 unsigned long flags;
315 save_flags(flags);
316 cli();
317 sk->wmem_alloc+= c->truesize;
318 restore_flags(flags);
319 }
320 return c;
321 }
322 return(NULL);
323 }
324 return(alloc_skb(size, priority));
325 }
326
327
328 struct sk_buff *sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
329 {
330 if (sk)
331 {
332 if (sk->rmem_alloc + size < sk->rcvbuf || force)
333 {
334 struct sk_buff *c = alloc_skb(size, priority);
335 if (c)
336 {
337 unsigned long flags;
338 save_flags(flags);
339 cli();
340 sk->rmem_alloc += c->truesize;
341 restore_flags(flags);
342 }
343 return(c);
344 }
345 return(NULL);
346 }
347 return(alloc_skb(size, priority));
348 }
349
350
351 unsigned long sock_rspace(struct sock *sk)
352 {
353 int amt;
354
355 if (sk != NULL)
356 {
357 if (sk->rmem_alloc >= sk->rcvbuf-2*MIN_WINDOW)
358 return(0);
359 amt = min((sk->rcvbuf-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
360 if (amt < 0)
361 return(0);
362 return(amt);
363 }
364 return(0);
365 }
366
367
368 unsigned long sock_wspace(struct sock *sk)
369 {
370 if (sk != NULL)
371 {
372 if (sk->shutdown & SEND_SHUTDOWN)
373 return(0);
374 if (sk->wmem_alloc >= sk->sndbuf)
375 return(0);
376 return(sk->sndbuf-sk->wmem_alloc );
377 }
378 return(0);
379 }
380
381
382 void sock_wfree(struct sock *sk, struct sk_buff *skb)
383 {
384 int s=skb->truesize;
385 #if CONFIG_SKB_CHECK
386 IS_SKB(skb);
387 #endif
388 kfree_skbmem(skb);
389 if (sk)
390 {
391 unsigned long flags;
392 save_flags(flags);
393 cli();
394 sk->wmem_alloc -= s;
395 restore_flags(flags);
396
397 sk->write_space(sk);
398 return;
399 }
400 }
401
402
403 void sock_rfree(struct sock *sk, struct sk_buff *skb)
404 {
405 int s=skb->truesize;
406 #if CONFIG_SKB_CHECK
407 IS_SKB(skb);
408 #endif
409 kfree_skbmem(skb);
410 if (sk)
411 {
412 unsigned long flags;
413 save_flags(flags);
414 cli();
415 sk->rmem_alloc -= s;
416 restore_flags(flags);
417 }
418 }
419
420
421
422
423
424 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size, int noblock, int *errcode)
425 {
426 struct sk_buff *skb;
427 int err;
428
429 sk->inuse=1;
430
431 do
432 {
433 if(sk->err!=0)
434 {
435 cli();
436 err= -sk->err;
437 sk->err=0;
438 sti();
439 *errcode=err;
440 return NULL;
441 }
442
443 if(sk->shutdown&SEND_SHUTDOWN)
444 {
445 *errcode=-EPIPE;
446 return NULL;
447 }
448
449 skb = sock_wmalloc(sk, size, 0, GFP_KERNEL);
450
451 if(skb==NULL)
452 {
453 unsigned long tmp;
454
455 sk->socket->flags |= SO_NOSPACE;
456 if(noblock)
457 {
458 *errcode=-EAGAIN;
459 return NULL;
460 }
461 if(sk->shutdown&SEND_SHUTDOWN)
462 {
463 *errcode=-EPIPE;
464 return NULL;
465 }
466 tmp = sk->wmem_alloc;
467 cli();
468 if(sk->shutdown&SEND_SHUTDOWN)
469 {
470 sti();
471 *errcode=-EPIPE;
472 return NULL;
473 }
474
475 if( tmp <= sk->wmem_alloc)
476 {
477 sk->socket->flags &= ~SO_NOSPACE;
478 interruptible_sleep_on(sk->sleep);
479 if (current->signal & ~current->blocked)
480 {
481 sti();
482 *errcode = -ERESTARTSYS;
483 return NULL;
484 }
485 }
486 sti();
487 }
488 }
489 while(skb==NULL);
490
491 return skb;
492 }
493
494
495 void release_sock(struct sock *sk)
496 {
497 unsigned long flags;
498 #ifdef CONFIG_INET
499 struct sk_buff *skb;
500 #endif
501
502 if (!sk->prot)
503 return;
504
505
506
507
508
509
510
511 save_flags(flags);
512 cli();
513 if (sk->blog)
514 {
515 restore_flags(flags);
516 return;
517 }
518 sk->blog=1;
519 sk->inuse = 1;
520 restore_flags(flags);
521 #ifdef CONFIG_INET
522
523 while((skb = skb_dequeue(&sk->back_log)) != NULL)
524 {
525 sk->blog = 1;
526 if (sk->prot->rcv)
527 sk->prot->rcv(skb, skb->dev, sk->opt,
528 skb->saddr, skb->len, skb->daddr, 1,
529
530 (struct inet_protocol *)sk->pair);
531 }
532 #endif
533 sk->blog = 0;
534 sk->inuse = 0;
535 #ifdef CONFIG_INET
536 if (sk->dead && sk->state == TCP_CLOSE)
537 {
538
539 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
540 }
541 #endif
542 }
543
544