|
|
1.1 root 1: /* tcp.c */
2: /*
3: Copyright (C) 1992 Ross Biro
4:
5: This program is free software; you can redistribute it and/or modify
6: it under the terms of the GNU General Public License as published by
7: the Free Software Foundation; either version 2, or (at your option)
8: any later version.
9:
10: This program is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public License
16: along with this program; if not, write to the Free Software
17: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18:
19: The Author may be reached as [email protected] or
20: C/O Department of Mathematics; Stanford University; Stanford, CA 94305
21: */
22:
23: #include <linux/types.h>
24: #include <linux/sched.h>
25: #include <linux/mm.h>
26: #include <asm/memory.h>
27: #include <linux/socket.h>
28: #include <netinet/in.h>
29: #include <linux/fcntl.h>
30: #include "timer.h"
31: #include "ip.h"
32: #include "icmp.h"
33: #include "tcp.h"
34: #include "sock.h"
35: #include <linux/errno.h>
36: #include <linux/timer.h>
37: #include <asm/system.h>
38: #include <asm/segment.h>
39: /* #include <signal.h>*/
40: #include "../kern_sock.h" /* for PRINTK */
41:
42: #define tmax(a,b) (before ((a),(b)) ? (b) : (a))
43: #define swap(a,b) {unsigned long c; c=a; a=b; b=c;}
44:
45: extern struct proto tcp_prot;
46:
47: static int
48: min (unsigned int a, unsigned int b)
49: {
50: if (a < b) return (a);
51: return (b);
52: }
53:
54: void
55: print_th (struct tcp_header *th)
56: {
57: unsigned char *ptr;
58: ptr = (unsigned char *)(th + 1);
59: PRINTK ("tcp header:\n");
60: PRINTK (" source=%d, dest=%d, seq =%d, ack_seq = %d\n",
61: net16(th->source), net16(th->dest), net32(th->seq),
62: net32(th->ack_seq));
63: PRINTK (" fin=%d, syn=%d, rst=%d, psh=%d, ack=%d, urg=%d res1=%d res2=%d\n"
64: ,th->fin, th->syn, th->rst, th->psh, th->ack, th->urg,
65: th->res1, th->res2);
66: PRINTK (" window = %d, check = %d urg_ptr = %d\n",
67: net16(th->window), net16(th->check), net16(th->urg_ptr));
68: PRINTK (" doff = %d\n",th->doff);
69: PRINTK ("options = %d %d %d %d\n", ptr[0], ptr[1], ptr[2], ptr[3]);
70: }
71:
72: /* This routine grabs the first thing off of a rcv queue. */
73: static struct sk_buff *
74: get_firstr(volatile struct sock *sk)
75: {
76: struct sk_buff *skb;
77: skb = sk->rqueue;
78: if (skb == NULL) return (NULL);
79: sk->rqueue = skb->next;
80: if (sk->rqueue == skb)
81: {
82: sk->rqueue = NULL;
83: }
84: else
85: {
86: sk->rqueue->prev=skb->prev;
87: sk->rqueue->prev->next = sk->rqueue;
88: }
89: return (skb);
90: }
91:
92: static long
93: diff (unsigned long seq1, unsigned long seq2)
94: {
95: long d;
96: d=seq1-seq2;
97: if (d > 0) return (d);
98: /* I hope this returns what I want. */
99: return (~d+1);
100: }
101:
102: /* enter the time wait state. */
103: static void
104: tcp_time_wait (volatile struct sock *sk)
105: {
106: sk->state = TCP_TIME_WAIT;
107: sk->time_wait.len = TCP_TIMEWAIT_LEN;
108: sk->timeout = TIME_CLOSE;
109: reset_timer ((struct timer *)&sk->time_wait);
110: }
111:
112: static void
113: tcp_retransmit (volatile struct sock *sk, int all)
114: {
115: if (all)
116: {
117: ip_retransmit (sk, all);
118: return;
119: }
120: sk->rtt *= 2; /* exponential back off. */
121: if (sk->cong_window > 1)
122: sk->cong_window = sk->cong_window / 2;
123: sk->exp_growth = 0;
124:
125: /* do the actuall retransmit. */
126: ip_retransmit (sk, all);
127:
128: }
129:
130: /* this routine is called by the icmp module when it gets some
131: sort of error condition. If err < 0 then the socket should
132: be closed and the error returned to the user. If err > 0
133: it's just the icmp type << 8 | icmp code.
134: header points to the first 8 bytes of the tcp header. We need
135: to find the appropriate port. */
136:
137: void
138: tcp_err (int err, unsigned char *header, unsigned long daddr,
139: unsigned long saddr, struct ip_protocol *protocol)
140: {
141: struct tcp_header *th;
142: volatile struct sock *sk;
143:
144: th = (struct tcp_header *)header;
145: sk = get_sock (&tcp_prot, net16(th->dest), saddr, th->source, daddr);
146:
147: if (sk == NULL) return;
148:
149: if (err & 0xff00 == (ICMP_SOURCE_QUENCH << 8))
150: {
151: /* for now we will just trigger a linear backoff. The slow start
152: code should cause a real backoff here. */
153:
154: if (sk->cong_window > 1)
155: sk->cong_window --;
156:
157: return;
158: }
159:
160: sk->err = icmp_err_convert[err & 0xff].errno;
161: if (icmp_err_convert[err & 0xff].fatal)
162: {
163: if (sk->state != TCP_ESTABLISHED)
164: sk->state = TCP_CLOSE;
165: sk->prot->close(sk, 0);
166: }
167:
168: return;
169:
170: }
171:
172: static int
173: tcp_select (volatile struct sock *sk, int sel_type, select_table *wait)
174: {
175: switch (sel_type)
176: {
177: case SEL_IN:
178: select_wait (sk->sleep, wait);
179: if (sk->rqueue != NULL &&
180: (between (sk->copied_seq, sk->rqueue->next->h.th->seq - 1,
181: sk->rqueue->next->h.th->seq + sk->rqueue->next->len) ||
182: sk->state == TCP_LISTEN))
183: {
184: return (1);
185: }
186:
187: switch (sk->state)
188: {
189: case TCP_LISTEN:
190: case TCP_ESTABLISHED:
191: case TCP_SYN_SENT:
192: case TCP_SYN_RECV:
193: return (0);
194: default:
195: return (1);
196: }
197:
198: case SEL_OUT:
199: select_wait (sk->sleep, wait);
200: if (sk->state != TCP_ESTABLISHED) return (1);
201: /* hack so it will probably be able to write something
202: if it says it's ok to write. */
203: if (sk->prot->wspace(sk) >= MIN_WRITE_SPACE) return (1);
204: return (0);
205:
206: case SEL_EX:
207: select_wait(sk->sleep,wait);
208: if (sk->err) return (1);
209: if (sk->state == TCP_FIN_WAIT1 ||
210: sk->state == TCP_FIN_WAIT2 ||
211: sk->state == TCP_TIME_WAIT ||
212: sk->state == TCP_LAST_ACK)
213: return (1);
214: return (0);
215: }
216: return (0);
217: }
218:
219: static int
220: tcp_ioctl (volatile struct sock *sk, int cmd, unsigned long arg)
221: {
222: switch (cmd)
223: {
224: default:
225: return (-EINVAL);
226: #if 0
227: case SIOCATMARK:
228: /* try to figure out if we need to read some urgent data. */
229: if (sk->rqueue && sk->rqueue->next->th.urg)
230: {
231: int offset;
232: struct sk_buff *skb;
233: skb = sk->rqueue->next;
234: offset = sk->copied_seq +1 - skb->th.seq - skb->th.syn;
235: /* now we know we are at the urgent data. */
236: if (offset >= skb->len)
237: {
238: verify_area ((void *)arg, sizeof (int));
239: put_fs_long(1, (unsigned long *)arg);
240: return (0);
241: }
242: }
243: verify_area ((void *)arg, sizeof (int));
244: put_fs_long(0, (unsigned long *)arg);
245: return (0);
246: #endif
247: }
248: }
249:
250:
251: /* this routine computes a tcp checksum */
252: static unsigned short
253: tcp_check (struct tcp_header *th, int len, unsigned long saddr,
254: unsigned long daddr)
255: {
256: unsigned long sum;
257:
258: if (saddr == 0) saddr = MY_IP_ADDR;
259: print_th (th);
260: __asm__("\t addl %%ecx,%%ebx\n"
261: "\t adcl %%edx,%%ebx\n"
262: "\t adcl $0, %%ebx\n"
263: : "=b" (sum)
264: : "0" (daddr), "c" (saddr), "d" ((net16(len) << 16) + IP_TCP*256)
265: : "cx","bx","dx" );
266:
267: if (len > 3)
268: {
269: __asm__(
270: "\tclc\n"
271: "1:\n"
272: "\t lodsl\n"
273: "\t adcl %%eax, %%ebx\n"
274: "\t loop 1b\n"
275: "\t adcl $0, %%ebx\n"
276: : "=b" (sum) , "=S" (th)
277: : "0" (sum), "c" (len/4) ,"1" (th)
278: : "ax", "cx", "bx", "si" );
279: }
280:
281: /* convert from 32 bits to 16 bits. */
282: __asm__(
283: "\t movl %%ebx, %%ecx\n"
284: "\t shrl $16,%%ecx\n"
285: "\t addw %%cx, %%bx\n"
286: "\t adcw $0, %%bx\n"
287: : "=b" (sum)
288: : "0" (sum)
289: : "bx", "cx");
290:
291: /* check for an extra word. */
292: if ((len & 2) != 0)
293: {
294: __asm__("\t lodsw\n"
295: "\t addw %%ax,%%bx\n"
296: "\t adcw $0, %%bx\n"
297: : "=b" (sum), "=S" (th)
298: : "0" (sum) ,"1" (th)
299: : "si", "ax", "bx");
300: }
301:
302: /* now check for the extra byte. */
303: if ((len & 1) != 0)
304: {
305: __asm__("\t lodsb\n"
306: "\t movb $0,%%ah\n"
307: "\t addw %%ax,%%bx\n"
308: "\t adcw $0, %%bx\n"
309: : "=b" (sum)
310: : "0" (sum) ,"S" (th)
311: : "si", "ax", "bx");
312: }
313:
314: /* we only want the bottom 16 bits, but we never cleared
315: the top 16. */
316: return ((~sum) & 0xffff);
317: }
318:
319:
320: static void
321: tcp_send_check (struct tcp_header *th, unsigned long saddr,
322: unsigned long daddr, int len, volatile struct sock *sk)
323: {
324:
325: th->check = 0;
326: if (sk && sk->no_check) return;
327: th->check = tcp_check (th, len, saddr, daddr);
328: return;
329: }
330:
331: /* This routine sends an ack and also updates the window. */
332: static void
333: tcp_send_ack (unsigned long sequence, unsigned long ack,
334: volatile struct sock *sk,
335: struct tcp_header *th, unsigned long daddr)
336: {
337: struct sk_buff *buff;
338: struct tcp_header *t1;
339: struct device *dev=NULL;
340: int tmp;
341:
342: /* we need to grab some memory, and put together an ack, and then
343: put it into the queue to be sent. */
344:
345: buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
346: if (buff == NULL)
347: {
348: /* force it to send an ack. */
349: sk->ack_backlog++;
350: if (sk->timeout != TIME_WRITE && sk->state < TCP_CLOSING)
351: {
352: sk->timeout = TIME_WRITE;
353: sk->time_wait.len = 10; /* got to do it quickly. */
354: reset_timer ((struct timer *)&sk->time_wait);
355: }
356: return;
357: }
358:
359: buff->mem_addr = buff;
360: buff->mem_len = MAX_ACK_SIZE;
361: buff->len=sizeof (struct tcp_header);
362: buff->sk = sk;
363: t1 = (struct tcp_header *)(buff + 1);
364: /* put in the ip_header and routing stuff. */
365: tmp = sk->prot->build_header (buff, sk->saddr, daddr, &dev,
366: IP_TCP, sk->opt, MAX_ACK_SIZE);
367: if (tmp < 0)
368: {
369: sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
370: return;
371: }
372: buff->len += tmp;
373: t1 = (struct tcp_header *)((char *)t1 +tmp);
374:
375: memcpy (t1, th, sizeof (*t1)); /* this should probably be removed. */
376:
377: /* swap the send and the receive. */
378: t1->dest = th->source;
379: t1->source = th->dest;
380: t1->seq = net32(sequence);
381: t1->ack = 1;
382: sk->window = sk->prot->rspace(sk);
383: t1->window = net16(sk->window);
384: t1->res1=0;
385: t1->res2=0;
386: t1->rst = 0;
387: t1->urg = 0;
388: t1->syn = 0;
389: t1->psh = 0;
390: t1->fin = 0;
391: if (ack == sk->acked_seq)
392: {
393: sk->ack_backlog = 0;
394: sk->bytes_rcv = 0;
395: sk->ack_timed = 0;
396: if (sk->send_head == NULL &&
397: sk->wfront == NULL)
398: {
399: delete_timer((struct timer *)&sk->time_wait);
400: sk->timeout = 0;
401: }
402:
403: }
404: t1->ack_seq = net32(ack);
405: t1->doff = sizeof (*t1)/4;
406: tcp_send_check (t1, sk->saddr, daddr, sizeof (*t1), sk);
407: sk->prot->queue_xmit(sk, dev, buff, 1);
408: }
409:
410: /* this routine builds a generic tcp header. */
411: static int
412: tcp_build_header(struct tcp_header *th, volatile struct sock *sk, int push)
413: {
414:
415: /* want to get rid of this. */
416: memcpy (th,(void *) &(sk->dummy_th), sizeof (*th));
417: th->seq = net32(sk->send_seq);
418: th->psh = (push == 0) ? 1 : 0;
419: th->doff = sizeof (*th)/4;
420: th->ack = 1;
421: th->fin = 0;
422: sk->ack_backlog = 0;
423: sk->bytes_rcv = 0;
424: sk->ack_timed = 0;
425: th->ack_seq = net32(sk->acked_seq);
426: sk->window = sk->prot->rspace(sk);
427: th->window = net16(sk->window);
428:
429: return (sizeof (*th));
430: }
431:
432: /* This routine copies from a user buffer into a socket, and starts
433: the transmit system. */
434:
435: static int
436: tcp_write(volatile struct sock *sk, unsigned char *from,
437: int len, int nonblock, unsigned flags)
438: {
439: int copied=0;
440: int copy;
441: int tmp;
442: struct sk_buff *skb;
443: unsigned char *buff;
444: struct proto *prot;
445: struct device *dev=NULL;
446:
447: PRINTK ("in TCP_WRITE sk = %X:\n",sk);
448: print_sk (sk);
449:
450: sk->inuse = 1; /* no one else will use this socket. */
451: prot = sk->prot;
452: while (len > 0)
453: {
454: /* first thing we do is make sure that we are established. */
455:
456: while (sk->state != TCP_ESTABLISHED)
457: {
458: if (sk->state != TCP_SYN_SENT &&
459: sk->state != TCP_SYN_RECV)
460: {
461: release_sock (sk);
462: if (sk->keepopen)
463: {
464: send_sig (SIGPIPE, current, 0);
465: return (-EINTR);
466: }
467: if (copied) return (copied);
468: if (sk->err) return (-sk->err);
469: return (-ENOTCONN);
470: }
471:
472: if (nonblock)
473: {
474: release_sock (sk);
475: return (-EAGAIN);
476: }
477:
478: /* now here is a race condition.
479: release_sock could cause the connection to
480: enter the established mode, if that is the
481: case, then we will block here for ever, because
482: we will have gotten our wakeup call before we
483: go to sleep. */
484: release_sock (sk);
485: cli();
486: if (sk->state != TCP_ESTABLISHED)
487: {
488: interruptible_sleep_on (sk->sleep);
489: if (current->signal & ~current->blocked)
490: {
491: sti();
492: if (copied) return (copied);
493: return (-ERESTARTSYS);
494: }
495: }
496: sti();
497: sk->inuse = 1;
498: }
499: /* we also need to worry about the window. The smallest we
500: will send is about 200 bytes. */
501:
502: copy = min (sk->mtu, diff(sk->window_seq, sk->send_seq));
503: if (copy < 200) copy = sk->mtu;
504: copy = min (copy, len);
505:
506: skb=prot->wmalloc (sk, copy + prot->max_header+sizeof (*skb),0);
507:
508: /* if we didn't get any memory, we need to sleep. */
509: if (skb == NULL)
510: {
511: if (nonblock || copied)
512: {
513: break;
514: }
515: /* here is another race condition. */
516: tmp = sk->wmem_alloc;
517: release_sock (sk);
518: /* again we will try to avoid it. */
519: cli ();
520: if (tmp <= sk->wmem_alloc)
521: {
522: interruptible_sleep_on (sk->sleep);
523: if (current->signal & ~current->blocked)
524: {
525: sti();
526: if (copied) return (copied);
527: return (-ERESTARTSYS);
528: }
529: }
530: sti();
531: sk->inuse = 1;
532: continue;
533: }
534: skb->mem_addr = skb;
535: skb->mem_len = copy+prot->max_header+sizeof (*skb);
536: skb->len = 0;
537: skb->sk = sk;
538: buff =(unsigned char *)( skb+1);
539: /* we need to optimize this. Perhaps some hints here
540: would be good. */
541:
542: tmp = prot->build_header (skb, sk->saddr, sk->daddr, &dev,
543: IP_TCP, sk->opt, skb->mem_len);
544: if (tmp < 0 )
545: {
546: prot->wfree (sk, skb->mem_addr, skb->mem_len);
547: release_sock (sk);
548: return (tmp);
549: }
550: skb->len += tmp;
551: skb->dev = dev;
552: buff+=tmp;
553: tmp = tcp_build_header((struct tcp_header *)buff, sk, len-copy);
554: if (tmp < 0)
555: {
556: prot->wfree (sk, skb->mem_addr, skb->mem_len);
557: release_sock (sk);
558: return (tmp);
559: }
560:
561: if (flags & MSG_OOB)
562: {
563: ((struct tcp_header *)buff)->urg = 1;
564: ((struct tcp_header *)buff)->urg_ptr = copy;
565: }
566: skb->len += tmp;
567: memcpy_fromfs (buff+tmp, from, copy);
568:
569: tcp_send_check ((struct tcp_header *)buff, sk->saddr, sk->daddr,
570: copy +sizeof (struct tcp_header), sk);
571:
572: from += copy;
573: copied += copy;
574: len -= copy;
575: skb->len += copy;
576: skb->free = 0;
577: sk->send_seq += copy;
578: skb->h.seq = sk->send_seq;
579: if (after (sk->send_seq , sk->window_seq) ||
580: sk->packets_out >= sk->cong_window)
581: {
582: PRINTK ("sk->cong_window = %d, sk->packets_out = %d\n",
583: sk->cong_window, sk->packets_out);
584: PRINTK ("sk->send_seq = %d, sk->window_seq = %d\n",
585: sk->send_seq, sk->window_seq);
586: skb->next = NULL;
587: if (sk->wback == NULL)
588: {
589: sk->wfront=skb;
590: }
591: else
592: {
593: sk->wback->next = skb;
594: }
595: sk->wback = skb;
596: }
597: else
598: {
599: prot->queue_xmit (sk, dev, skb,0);
600: }
601: }
602: sk->err = 0;
603: release_sock (sk);
604: return (copied);
605: }
606:
607:
608: static void
609: tcp_read_wakeup(volatile struct sock *sk)
610: {
611: int tmp;
612: struct device *dev = NULL;
613: struct tcp_header *t1;
614: struct sk_buff *buff;
615:
616: if (!sk->ack_backlog ) return;
617: PRINTK ("in tcp read wakeup\n");
618: /* we need to put code here to prevent this routine from being called. */
619: /* being called once in a while is ok, so only check if this is the
620: second time in a row. */
621:
622: /* we need to grab some memory, and put together an ack, and then
623: put it into the queue to be sent. */
624:
625: buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
626: if (buff == NULL)
627: {
628: /* try again real soon. */
629: sk->timeout = TIME_WRITE;
630: sk->time_wait.len = 10;
631: reset_timer((struct timer *) &sk->time_wait);
632: return;
633: }
634:
635: buff->mem_addr = buff;
636: buff->mem_len = MAX_ACK_SIZE;
637: buff->len=sizeof (struct tcp_header);
638: buff->sk = sk;
639:
640: /* put in the ip_header and routing stuff. */
641: tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
642: IP_TCP, sk->opt, MAX_ACK_SIZE);
643: if (tmp < 0)
644: {
645: sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
646: return;
647: }
648:
649: buff->len += tmp;
650: t1 = (struct tcp_header *)((char *)(buff+1) +tmp);
651:
652: memcpy (t1,(void *) &sk->dummy_th, sizeof (*t1));
653: t1->seq = net32(sk->send_seq);
654: t1->ack = 1;
655: t1->res1=0;
656: t1->res2=0;
657: t1->rst = 0;
658: t1->urg = 0;
659: t1->syn = 0;
660: t1->psh = 0;
661: sk->ack_backlog = 0;
662: sk->bytes_rcv = 0;
663: sk->window = sk->prot->rspace(sk);
664: t1->window = net16(sk->window);
665: t1->ack_seq = net32(sk->acked_seq);
666: t1->doff = sizeof (*t1)/4;
667: tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
668: sk->prot->queue_xmit(sk, dev, buff, 1);
669: }
670:
671:
672: /* This routine frees used buffers. */
673: /* It should consider sending an ack to let the
674: other end know we now have a bigger window. */
675:
676: static void
677: cleanup_rbuf (volatile struct sock *sk)
678: {
679: PRINTK ("cleaning rbuf for sk=%X\n",sk);
680: /* we have to loop through all the buffer headers, and
681: try to free up all the space we can. */
682: while (sk->rqueue != NULL )
683: {
684: struct sk_buff *skb;
685: skb=sk->rqueue->next;
686: if (!skb->used) break;
687: if (sk->rqueue == skb)
688: {
689: sk->rqueue = NULL;
690: }
691: else
692: {
693: skb->next->prev = skb->prev;
694: skb->prev->next = skb->next;
695: }
696: skb->sk = sk;
697: free_skb (skb, FREE_READ);
698: }
699: /* at this point we should send an ack if the difference in
700: the window, and the amount of space is bigger than
701: TCP_WINDOW_DIFF */
702: PRINTK ("sk->window left = %d, sk->prot->rspace(sk)=%d\n",
703: sk->window - sk->bytes_rcv, sk->prot->rspace(sk));
704:
705: if ((sk->prot->rspace(sk) >
706: (sk->window - sk->bytes_rcv + TCP_WINDOW_DIFF)) ||
707: (sk->window - sk->bytes_rcv < 2*sk->mtu))
708: {
709: /* force it to send an ack. */
710: sk->ack_backlog++;
711: if (sk->timeout != TIME_WRITE && sk->state == TCP_ESTABLISHED)
712: {
713: sk->time_wait.len = TCP_ACK_TIME;
714: sk->timeout=TIME_WRITE;
715: reset_timer ((struct timer *)&sk->time_wait);
716: }
717: }
718:
719: }
720:
721: /* handle reading urgent data. */
722: static int
723: tcp_read_urg(volatile struct sock * sk,
724: unsigned char *to, int len, unsigned flags)
725: {
726: int copied = 0;
727: struct sk_buff *skb;
728: PRINTK ("tcp_read_urg(sk=%X, to=%X, len=%d, flags=%X)\n",
729: sk, to, len, flags);
730: print_sk(sk);
731: while (len > 0)
732: {
733: sk->inuse = 1;
734: while (sk->urg==0 || sk->rqueue == NULL)
735: {
736: /* now at this point, we may have gotten some data. */
737: release_sock (sk);
738: if (sk->state > TCP_CLOSING)
739: {
740: if (copied) return (copied);
741: return (-ENOTCONN);
742: }
743: cli();
744: if (sk->urg == 0 || sk->rqueue == NULL)
745: {
746: interruptible_sleep_on (sk->sleep);
747: if (current->signal & ~current->blocked)
748: {
749: sti();
750: if (copied) return (copied);
751: return (-ERESTARTSYS);
752: }
753: }
754: sti();
755: sk->inuse = 1;
756: }
757: /* now we have some urgent data, we must find it.*/
758: for (skb = sk->rqueue->next; skb->next != sk->rqueue;
759: skb = skb->next)
760: {
761: int offset;
762: int amt;
763: if (!skb->h.th->urg) continue;
764: offset = 0;
765: amt = min(skb->h.th->urg_ptr,len);
766: verify_area (to, amt);
767: memcpy_tofs (to, (unsigned char *)(skb->h.th) +
768: skb->h.th->doff*4
769: + offset, amt);
770:
771: if (!(flags & MSG_PEEK))
772: {
773: skb->urg_used = 1;
774: sk->urg --;
775: }
776: release_sock (sk);
777: copied += amt;
778: return (copied);
779: }
780: }
781: return (0);
782: }
783:
784: /* This routine copies from a sock struct into the user buffer. */
785: static int
786: tcp_read(volatile struct sock *sk, unsigned char *to,
787: int len, int nonblock, unsigned flags)
788: {
789: int copied=0; /* will be used to say how much has been copied. */
790: struct sk_buff *skb;
791: unsigned long offset;
792: unsigned long used;
793:
794: if (len == 0) return (0);
795: if (len < 0)
796: {
797: return (-EINVAL);
798: }
799:
800: /* this error should be checked. */
801: if (sk->state == TCP_LISTEN) return (-ENOTCONN);
802:
803: /* urgent data needs to be handled specially. */
804: if ((flags & MSG_OOB))
805: return (tcp_read_urg (sk, to, len, flags));
806:
807: /* so no-one else will use this socket. */
808: sk->inuse = 1;
809: if (sk->rqueue != NULL)
810: skb=sk->rqueue->next;
811: else
812: skb = NULL;
813:
814: while ( len > 0)
815: {
816: PRINTK("tcp_read (sk=%X, to=%X, len=%d, nonblock=%d, flags=%X)\n",
817: sk, to, len, nonblock, flags);
818: while ( skb == NULL || before (sk->copied_seq+1, skb->h.th->seq) ||
819: skb->used) /* skb->used just checks to see if we've
820: gone all the way around. */
821: {
822:
823: PRINTK("skb = %X:\n",skb);
824: print_skb(skb);
825: print_sk (sk);
826:
827: cleanup_rbuf(sk);
828:
829: if (nonblock || ((flags & MSG_PEEK) && copied))
830: {
831: release_sock (sk);
832: if (copied) return (copied);
833: return (-EAGAIN);
834: }
835:
836: release_sock (sk); /* now we may have some data waiting. */
837:
838:
839: PRINTK ("tcp_read about to sleep. state = %d\n",sk->state);
840: cli();
841:
842: if (sk->state == TCP_CLOSE || sk->state == TCP_TIME_WAIT)
843: {
844: sti();
845: if (copied) return (copied);
846: if (sk->err) return (-sk->err);
847: if (!sk->done)
848: {
849: sk->done = 1;
850: return (0);
851: }
852: return (-ENOTCONN);
853: }
854:
855: if ( sk->rqueue == NULL ||
856: before (sk->copied_seq+1, sk->rqueue->next->h.th->seq))
857: {
858: interruptible_sleep_on (sk->sleep);
859: if (current->signal & ~current->blocked)
860: {
861: sti ();
862: if (copied) return (copied);
863: return (-ERESTARTSYS);
864: }
865: }
866: sti();
867: PRINTK ("tcp_read woke up. \n");
868:
869: sk->inuse = 1;
870:
871: if (sk->rqueue != NULL)
872: skb=sk->rqueue->next;
873: else
874: skb = NULL;
875:
876: }
877:
878: /* Copy anything from the current block that needs to go
879: into the user buffer. */
880:
881: offset = sk->copied_seq+1 - skb->h.th->seq;
882:
883: if (skb->h.th->syn) offset --;
884: if (offset < skb->len )
885: {
886: /* if there is urgent data we must either return or skip
887: over it. */
888: if (skb->h.th->urg)
889: {
890: if (skb->urg_used)
891: {
892: if (flags & MSG_PEEK) break;
893: sk->copied_seq += skb->h.th->urg_ptr;
894: offset += skb->h.th->urg_ptr;
895: if (offset > skb->len)
896: {
897: skb->used = 1;
898: skb=skb->next;
899: continue;
900: }
901: }
902: else
903: {
904: break;
905: }
906: }
907: used = min(skb->len - offset, len);
908:
909: verify_area (to, used);
910: memcpy_tofs(to, ((unsigned char *)skb->h.th) +
911: skb->h.th->doff*4 +
912: offset,
913: used);
914: copied += used;
915: len -= used;
916: to += used;
917: if (!(flags & MSG_PEEK))
918: sk->copied_seq += used;
919:
920: /* mark this data used if we are really reading it, and if
921: it doesn't contain any urgent data. And we have used all
922: the data. */
923: if (!(flags & MSG_PEEK) &&
924: (!skb->h.th->urg || skb->urg_used) &&
925: (used + offset >= skb->len) )
926: skb->used = 1;
927:
928: /* see if this is the end of a message or if the remaining data
929: is urgent. */
930: if ( skb->h.th->psh || skb->h.th->urg)
931: {
932: break;
933: }
934: }
935: else /* already used this data, must be a retransmit. */
936: {
937: skb->used = 1;
938: }
939: skb=skb->next;
940: }
941: cleanup_rbuf (sk);
942: release_sock (sk);
943: if (copied == 0 && nonblock) return (-EAGAIN);
944: return (copied);
945: }
946:
947: /* this routine will send a reset to the other tcp. */
948: static void
949: tcp_reset(unsigned long saddr, unsigned long daddr, struct tcp_header *th,
950: struct proto *prot, struct options *opt, struct device *dev)
951: {
952: /* we need to grab some memory, and put together a reset, and then
953: put it into the queue to be sent. */
954: struct sk_buff *buff;
955: struct tcp_header *t1;
956: int tmp;
957: buff=prot->wmalloc(NULL, MAX_RESET_SIZE,1);
958: if (buff == NULL) return;
959:
960: PRINTK("tcp_reset buff = %X\n", buff);
961: buff->mem_addr = buff;
962: buff->mem_len = MAX_RESET_SIZE;
963: buff->len = sizeof (*t1);
964: buff->sk = NULL;
965: buff->dev = dev;
966:
967: t1=(struct tcp_header *)(buff + 1);
968: /* put in the ip_header and routing stuff. */
969: tmp = prot->build_header (buff, saddr, daddr, &dev, IP_TCP, opt,
970: sizeof(struct tcp_header));
971: if (tmp < 0)
972: {
973: prot->wfree (NULL,buff->mem_addr, buff->mem_len);
974: return;
975: }
976: t1 = (struct tcp_header *)((char *)t1 +tmp);
977: buff->len += tmp;
978: memcpy (t1, th, sizeof (*t1));
979: /* swap the send and the receive. */
980: t1->dest = th->source;
981: t1->source = th->dest;
982: t1->seq = th->ack_seq; /* add one so it will be in
983: the right range.*/
984: t1->rst = 1;
985: t1->ack = 0;
986: t1->syn = 0;
987: t1->urg = 0;
988: t1->fin = 0;
989: t1->psh = 0;
990: t1->doff = sizeof (*t1)/4;
991: tcp_send_check (t1, saddr, daddr, sizeof (*t1), NULL);
992: prot->queue_xmit(NULL, dev, buff, 1);
993:
994: }
995:
996:
997: /* This routine handles a connection request. This should make sure
998: we haven't already responded. */
999: /* Because of the way BSD works, we have to send a syn/ack now. This also
1000: means it will be harder to close a socket which is listening. */
1001:
1002: static void
1003: tcp_conn_request(volatile struct sock *sk, struct sk_buff *skb,
1004: unsigned long daddr,
1005: unsigned long saddr, struct options *opt, struct device *dev)
1006: {
1007: struct sk_buff *buff;
1008: struct tcp_header *t1;
1009: unsigned char *ptr;
1010: volatile struct sock *newsk;
1011: struct tcp_header *th;
1012: int tmp;
1013: th = skb->h.th;
1014:
1015: PRINTK ("tcp_conn_request (sk = %X, skb = %X, daddr = %X, sadd4= %X, \n"
1016: " opt = %X, dev = %X)\n",
1017: sk, skb, daddr, saddr, opt, dev);
1018:
1019: /* if the socket is dead, don't accept the connection. */
1020: if (!sk->dead)
1021: {
1022: wake_up(sk->sleep);
1023: }
1024: else
1025: {
1026: PRINTK ("tcp_conn_request on dead socket\n");
1027: tcp_reset (daddr, saddr, th, sk->prot, opt, dev);
1028: free_skb (skb, FREE_READ);
1029: return;
1030: }
1031:
1032:
1033: /* we need to build a new sock struct. */
1034: /* It is sort of bad to have a socket without an inode attached to
1035: it, but the wake_up's will just wake up the listening socket,
1036: and if the listening socket is destroyed before this is taken
1037: off of the queue, this will take care of it. */
1038:
1039: newsk = malloc(sizeof (struct sock));
1040: if (newsk == NULL)
1041: {
1042: /* just ignore the syn. It will get retransmitted. */
1043: free_skb (skb, FREE_READ);
1044: return;
1045: }
1046:
1047:
1048: PRINTK ("newsk = %X\n", newsk);
1049: memcpy ((void *)newsk, (void *)sk, sizeof (*newsk));
1050: newsk->wback = NULL;
1051: newsk->wfront = NULL;
1052: newsk->rqueue = NULL;
1053: newsk->send_head = NULL;
1054: newsk->send_tail = NULL;
1055: newsk->back_log = NULL;
1056: newsk->blog = 0;
1057: newsk->intr = 0;
1058: newsk->proc = 0;
1059: newsk->done = 0;
1060:
1061: newsk->pair = NULL;
1062: newsk->wmem_alloc = 0;
1063: newsk->rmem_alloc = 0;
1064:
1065: newsk->max_unacked = MAX_WINDOW - TCP_WINDOW_DIFF;
1066:
1067: newsk->err = 0;
1068: newsk->shutdown = 0;
1069: newsk->ack_backlog = 0;
1070: newsk->acked_seq = skb->h.th->seq+1;
1071: newsk->fin_seq = skb->h.th->seq;
1072: newsk->copied_seq = skb->h.th->seq;
1073: newsk->state = TCP_SYN_RECV;
1074: newsk->timeout = 0;
1075: newsk->send_seq = timer_seq*SEQ_TICK-seq_offset;
1076: newsk->rcv_ack_seq = newsk->send_seq;
1077: newsk->urg =0;
1078: newsk->retransmits = 0;
1079: newsk->destroy = 0;
1080: newsk->time_wait.sk = newsk;
1081: newsk->time_wait.next = NULL;
1082: newsk->dummy_th.source = skb->h.th->dest;
1083: newsk->dummy_th.dest = skb->h.th->source;
1084: /* swap these two, they are from our point of view. */
1085: newsk->daddr=saddr;
1086: newsk->saddr=daddr;
1087:
1088: put_sock (newsk->num,newsk);
1089: newsk->dummy_th.res1=0;
1090: newsk->dummy_th.doff=6;
1091: newsk->dummy_th.fin=0;
1092: newsk->dummy_th.syn=0;
1093: newsk->dummy_th.rst=0;
1094: newsk->dummy_th.psh=0;
1095: newsk->dummy_th.ack=0;
1096: newsk->dummy_th.urg=0;
1097: newsk->dummy_th.res2=0;
1098: newsk->acked_seq = skb->h.th->seq+1;
1099: newsk->copied_seq = skb->h.th->seq;
1100:
1101: if (skb->h.th->doff == 5)
1102: {
1103: newsk->mtu=dev->mtu-HEADER_SIZE;
1104: }
1105: else
1106: {
1107: ptr = (unsigned char *)(skb+1);
1108: if (ptr[0] != 2 || ptr[1] != 4)
1109: {
1110: newsk->mtu=576-HEADER_SIZE;
1111: }
1112: else
1113: {
1114: newsk->mtu = min (ptr[2]*256+ptr[3]-HEADER_SIZE,
1115: dev->mtu-HEADER_SIZE);
1116: }
1117: }
1118:
1119: print_sk (newsk);
1120: buff=newsk->prot->wmalloc(newsk,MAX_SYN_SIZE,1);
1121: if (buff == NULL)
1122: {
1123: sk->err = -ENOMEM;
1124: newsk->dead = 1;
1125: release_sock (newsk);
1126: free_skb (skb, FREE_READ);
1127: return;
1128: }
1129:
1130: buff->mem_addr = buff;
1131: buff->mem_len = MAX_SYN_SIZE;
1132: buff->len=sizeof (struct tcp_header)+4;
1133: buff->sk = newsk;
1134:
1135: t1=(struct tcp_header *)(buff + 1);
1136: /* put in the ip_header and routing stuff. */
1137:
1138: tmp = sk->prot->build_header (buff, newsk->saddr, newsk->daddr, &dev,
1139: IP_TCP, NULL, MAX_SYN_SIZE);
1140:
1141: /* something went wrong. */
1142: if (tmp < 0)
1143: {
1144: sk->err = tmp;
1145: sk->prot->wfree(newsk, buff->mem_addr, buff->mem_len);
1146: newsk->dead = 1;
1147: release_sock (newsk);
1148: free_skb (skb, FREE_READ);
1149: return;
1150: }
1151:
1152: buff->len += tmp;
1153: t1 = (struct tcp_header *)((char *)t1 +tmp);
1154:
1155: memcpy (t1, skb->h.th, sizeof (*t1));
1156: buff->h.seq = newsk->send_seq;
1157: /* swap the send and the receive. */
1158: t1->dest = skb->h.th->source;
1159: t1->source = newsk->dummy_th.source;
1160: t1->seq = net32(newsk->send_seq++);
1161: t1->ack = 1;
1162: newsk->window = sk->prot->rspace(newsk);
1163: t1->window = net16(newsk->window);
1164: t1->res1=0;
1165: t1->res2=0;
1166: t1->rst = 0;
1167: t1->urg = 0;
1168: t1->psh = 0;
1169: t1->syn = 1;
1170: t1->ack_seq = net32(skb->h.th->seq+1);
1171: t1->doff = sizeof (*t1)/4+1;
1172:
1173: ptr = (unsigned char *)(t1+1);
1174: ptr[0]=2;
1175: ptr[1]=4;
1176: ptr[2]=((dev->mtu - HEADER_SIZE) >> 8) & 0xff;
1177: ptr[3]=(dev->mtu - HEADER_SIZE) & 0xff;
1178:
1179: tcp_send_check (t1, daddr, saddr, sizeof (*t1)+4, newsk);
1180: newsk->prot->queue_xmit(newsk, dev, buff, 0);
1181:
1182: newsk->time_wait.len = TCP_CONNECT_TIME;
1183: PRINTK ("newsk->time_wait.sk = %X\n", newsk->time_wait.sk);
1184: reset_timer ((struct timer *)&newsk->time_wait);
1185: skb->sk = newsk;
1186: /* charge the sock_buff to newsk. */
1187: sk->rmem_alloc -= skb->mem_len;
1188: newsk->rmem_alloc += skb->mem_len;
1189:
1190: if (sk->rqueue == NULL)
1191: {
1192: skb->next = skb;
1193: skb->prev = skb;
1194: sk->rqueue = skb;
1195: }
1196: else
1197: {
1198: skb->next = sk->rqueue;
1199: skb->prev = sk->rqueue->prev;
1200: sk->rqueue->prev = skb;
1201: skb->prev->next = skb;
1202: }
1203: release_sock (newsk);
1204: }
1205:
1206: static void
1207: tcp_close (volatile struct sock *sk, int timeout)
1208: {
1209: /* we need to grab some memory, and put together a fin, and then
1210: put it into the queue to be sent. */
1211: struct sk_buff *buff;
1212: int need_reset = 0;
1213: struct tcp_header *t1,*th;
1214: struct proto *prot;
1215: struct device *dev=NULL;
1216: int tmp;
1217: PRINTK ("tcp_close ((struct sock *)%X, %d)\n",sk, timeout);
1218: sk->inuse = 1;
1219: sk->keepopen = 0;
1220: sk->shutdown = SHUTDOWN_MASK;
1221:
1222: if (!sk->dead)
1223: wake_up (sk->sleep);
1224:
1225:
1226: /* we need to flush the recv. buffs. */
1227:
1228: if (sk->rqueue != NULL)
1229: {
1230: struct sk_buff *skb;
1231: struct sk_buff *skb2;
1232: skb = sk->rqueue;
1233: do {
1234: skb2=skb->next;
1235: free_skb (skb, FREE_READ);
1236: skb=skb2;
1237: } while (skb != sk->rqueue);
1238: need_reset = 1;
1239: }
1240: sk->rqueue = NULL;
1241:
1242:
1243: switch (sk->state)
1244: {
1245: case TCP_FIN_WAIT1:
1246: case TCP_FIN_WAIT2:
1247: case TCP_LAST_ACK:
1248: if (timeout)
1249: tcp_time_wait(sk);
1250: release_sock (sk);
1251: if (!need_reset)
1252: return;
1253: break;
1254:
1255: case TCP_TIME_WAIT:
1256: if (timeout)
1257: sk->state = TCP_CLOSE;
1258: release_sock (sk);
1259: return;
1260:
1261: case TCP_LISTEN:
1262: sk->state = TCP_CLOSE;
1263: release_sock(sk);
1264: return;
1265:
1266: case TCP_CLOSE:
1267:
1268: release_sock(sk);
1269: return;
1270:
1271:
1272: case TCP_ESTABLISHED:
1273: case TCP_SYN_SENT:
1274: case TCP_SYN_RECV:
1275:
1276: prot = (struct proto *)sk->prot;
1277: th=(struct tcp_header *)&sk->dummy_th;
1278:
1279: buff=prot->wmalloc(sk, MAX_FIN_SIZE,1);
1280: if (buff == NULL)
1281: {
1282: /* this will force it to try again later. */
1283: sk->state = TCP_ESTABLISHED;
1284: sk->timeout = TIME_CLOSE;
1285: sk->time_wait.len = 100; /* wait a second. */
1286: reset_timer ((struct timer *)&sk->time_wait);
1287: return;
1288: }
1289:
1290: buff->mem_addr = buff;
1291: buff->mem_len = MAX_FIN_SIZE;
1292: buff->sk = sk;
1293: buff->len = sizeof (*t1);
1294: t1=(struct tcp_header *)(buff + 1);
1295: /* put in the ip_header and routing stuff. */
1296: tmp = prot->build_header (buff,sk->saddr, sk->daddr, &dev,
1297: IP_TCP, sk->opt,
1298: sizeof(struct tcp_header));
1299: if (tmp < 0)
1300: {
1301: prot->wfree (sk,buff->mem_addr, buff->mem_len);
1302: PRINTK ("Unable to build header for fin.\n");
1303: release_sock(sk);
1304: return;
1305: }
1306: t1 = (struct tcp_header *)((char *)t1 +tmp);
1307: buff ->len += tmp;
1308: buff->dev = dev;
1309: memcpy (t1, th, sizeof (*t1));
1310: t1->seq = net32(sk->send_seq);
1311: sk->send_seq++;
1312: buff->h.seq = sk->send_seq;
1313: t1->ack = 1;
1314: /* ack everything immediately from now on. */
1315: sk->delay_acks = 0;
1316: t1->ack_seq = net32(sk->acked_seq);
1317: t1->window = net16(sk->prot->rspace(sk));
1318: t1->fin = 1;
1319: t1->rst = need_reset;
1320: t1->doff = sizeof (*t1)/4;
1321: tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
1322:
1323: if (sk->wfront == NULL)
1324: {
1325: prot->queue_xmit(sk, dev, buff, 0);
1326: }
1327: else
1328: {
1329: sk->time_wait.len = sk->rtt;
1330: sk->timeout = TIME_WRITE;
1331: reset_timer ((struct timer *)&sk->time_wait);
1332: buff->next = NULL;
1333: if (sk->wback == NULL)
1334: {
1335: sk->wfront=buff;
1336: }
1337: else
1338: {
1339: sk->wback->next = buff;
1340: }
1341: sk->wback = buff;
1342:
1343: }
1344: sk->state = TCP_FIN_WAIT1;
1345: }
1346: release_sock (sk);
1347: }
1348:
1349:
1350: /* This routine takes stuff off of the write queue, and puts it in the
1351: xmit queue. */
1352: static void
1353: tcp_write_xmit (volatile struct sock *sk)
1354: {
1355: struct sk_buff *skb;
1356: while (sk->wfront != NULL && before (sk->wfront->h.seq, sk->window_seq) &&
1357: sk->packets_out < sk->cong_window)
1358: {
1359: skb = sk->wfront;
1360: sk->wfront = skb->next;
1361: if (sk->wfront == NULL)
1362: sk->wback = NULL;
1363: sk->prot->queue_xmit (sk, skb->dev, skb, skb->free);
1364: }
1365: }
1366:
1367:
1368:
1369: /* This routine deals with incoming acks, but not outgoing ones. */
1370:
1371: static int
1372: tcp_ack (volatile struct sock *sk, struct tcp_header *th, unsigned long saddr)
1373: {
1374: unsigned long ack;
1375: ack = net32(th->ack_seq);
1376:
1377: if (!between (ack , sk->rcv_ack_seq, sk->send_seq))
1378: {
1379: if (after (ack, sk->send_seq) || sk->state != TCP_ESTABLISHED)
1380: {
1381: return (0);
1382: }
1383: if (sk->keepopen)
1384: reset_timer ((struct timer *)&sk->time_wait);
1385: sk->retransmits = 0;
1386: return (1);
1387: }
1388:
1389: sk->window_seq = ack + net16(th->window);
1390:
1391:
1392: /* we don't want too many packets out there. */
1393: if (sk->cong_window < 2048 && ack != sk->rcv_ack_seq)
1394: {
1395: if (sk->exp_growth)
1396: sk->cong_window *= 2;
1397: else
1398: sk->cong_window++;
1399: }
1400:
1401: sk->rcv_ack_seq = ack;
1402:
1403: /* see if we can take anything off of the retransmit queue. */
1404: while (sk->send_head != NULL)
1405: {
1406: if (before (sk->send_head->h.seq, ack+1))
1407: {
1408: struct sk_buff *oskb;
1409: /* we have one less packet out there. */
1410: sk->packets_out --;
1411: cli();
1412: oskb = sk->send_head;
1413: /* estimate the rtt. */
1414: sk->rtt += ((jiffies - oskb->when) - sk->rtt)/2;
1415: if (sk->rtt < 30) sk->rtt = 30;
1416: sk->send_head = oskb->link3;
1417: if (sk->send_head == NULL)
1418: {
1419: sk->send_tail = NULL;
1420: }
1421: /* we may need to remove this from the dev send list. */
1422: if (oskb->next != NULL)
1423: {
1424: if (oskb->next != oskb)
1425: {
1426: oskb->next->prev = oskb->prev;
1427: oskb->prev->next = oskb->next;
1428: }
1429: else
1430: {
1431: int i;
1432: for (i = 0; i < DEV_NUMBUFFS; i++)
1433: {
1434: if (oskb->dev->buffs[i] = oskb)
1435: {
1436: oskb->dev->buffs[i]= NULL;
1437: break;
1438: }
1439: }
1440: }
1441: }
1442: free_skb (oskb, FREE_WRITE); /* write. */
1443: sti();
1444: if (!sk->dead)
1445: wake_up(sk->sleep);
1446: }
1447: else
1448: {
1449: break;
1450: }
1451:
1452: }
1453:
1454:
1455: /* at this point we need to check to see if we have anything
1456: which needs to be retransmiteed. If we have failed to get
1457: some acks i.e. had to retransmit something, and we succeded, we
1458: should then attempt to retransmit everything right now. */
1459:
1460: if (sk->retransmits && sk->send_head != NULL)
1461: {
1462: sk->prot->retransmit (sk,1);
1463: }
1464: sk->retransmits = 0;
1465:
1466: /* maybe we can take some stuff off of the write queue, and put it onto
1467: the xmit queue. */
1468: if (sk->wfront != NULL && sk->packets_out < sk->cong_window)
1469: {
1470: if (after (sk->window_seq, sk->wfront->h.seq))
1471: {
1472: tcp_write_xmit (sk);
1473: }
1474: }
1475: else
1476: {
1477: if (sk->send_head == NULL && sk->ack_backlog == 0 &&
1478: sk->state != TCP_TIME_WAIT)
1479: {
1480: delete_timer((struct timer *)&sk->time_wait);
1481: sk->timeout = 0;
1482: }
1483: else
1484: {
1485: if (sk->state == TCP_TIME_WAIT)
1486: {
1487: sk->time_wait.len = TCP_TIMEWAIT_LEN;
1488: sk->timeout = TIME_CLOSE;
1489: }
1490: reset_timer ((struct timer *)&sk->time_wait);
1491: }
1492: }
1493:
1494: /* see if we are done. */
1495: if ( sk->state == TCP_TIME_WAIT)
1496: {
1497: if (sk->rcv_ack_seq == sk->send_seq &&
1498: sk->acked_seq == sk->fin_seq);
1499: if (!sk->dead) wake_up (sk->sleep);
1500: sk->state = TCP_CLOSE;
1501: }
1502:
1503: if (sk->state == TCP_FIN_WAIT1)
1504: {
1505: if (sk->rcv_ack_seq == sk->send_seq)
1506: sk->state = TCP_FIN_WAIT2;
1507: }
1508:
1509: if (sk->state == TCP_LAST_ACK)
1510: {
1511: if (sk->rcv_ack_seq == sk->send_seq)
1512: {
1513: if (sk->acked_seq != sk->fin_seq)
1514: {
1515: tcp_time_wait(sk);
1516: }
1517: else
1518: {
1519: sk->state = TCP_CLOSE;
1520: }
1521: }
1522: if (!sk->dead) wake_up (sk->sleep);
1523: }
1524:
1525: return (1);
1526: }
1527:
1528: /* This routine handles the data. If there is room in the buffer, it
1529: will be have already been moved into it. If there is no room,
1530: then we will just have to discard the packet. */
1531:
1532: static int
1533: tcp_data (struct sk_buff *skb, volatile struct sock *sk,
1534: unsigned long saddr, unsigned short len)
1535: {
1536: struct sk_buff *skb1, *skb2;
1537: struct tcp_header *th;
1538:
1539: th = skb->h.th;
1540: print_th (th);
1541: skb->len = len - (th->doff*4);
1542:
1543: PRINTK("tcp_data len = %d sk = %X:\n",skb->len, sk);
1544: print_sk(sk);
1545:
1546: sk->bytes_rcv += skb->len;
1547:
1548: if (skb->len == 0 && !th->fin && !th->urg && !th->psh)
1549: {
1550: /* don't want to keep passing ack's back and fourth. */
1551: if (!th->ack)
1552: tcp_send_ack (sk->send_seq, sk->acked_seq,sk, th, saddr);
1553: free_skb(skb, FREE_READ);
1554: return (0);
1555: }
1556:
1557: if (sk->shutdown & RCV_SHUTDOWN)
1558: {
1559: /* just ack everything. */
1560: sk->acked_seq = th->seq + skb->len + th->syn + th->fin;
1561: tcp_send_ack (sk->send_seq, sk->acked_seq, sk, skb->h.th, saddr);
1562: free_skb (skb, FREE_READ);
1563: if (sk->state == TCP_TIME_WAIT && sk->acked_seq == sk->fin_seq)
1564: {
1565: if (!sk->dead) wake_up (sk->sleep);
1566: sk->state = TCP_CLOSE;
1567: }
1568: return (0);
1569: }
1570:
1571: /* now we have to walk the chain, and figure out where this one
1572: goes into it. This is set up so that the last packet we received
1573: will be the first one we look at, that way if everything comes
1574: in order, there will be no performance loss, and if they come
1575: out of order we will be able to fit things in nicely. */
1576:
1577: if (sk->rqueue == NULL)
1578: {
1579: PRINTK ("tcp_data: skb = %X:\n",skb);
1580: print_skb (skb);
1581:
1582: sk->rqueue = skb;
1583: skb->next = skb;
1584: skb->prev = skb;
1585: skb1= NULL;
1586: }
1587: else
1588: {
1589: PRINTK ("tcp_data adding to chain sk = %X:\n",sk);
1590: print_sk (sk);
1591:
1592: for (skb1=sk->rqueue; ; skb1=skb1->prev)
1593: {
1594: PRINTK ("skb1=%X\n",skb1);
1595: print_skb(skb1);
1596: PRINTK ("skb1->h.th->seq = %d\n", skb1->h.th->seq);
1597: if (after ( th->seq+1, skb1->h.th->seq))
1598: {
1599: skb->prev = skb1;
1600: skb->next = skb1->next;
1601: skb->next->prev = skb;
1602: skb1->next = skb;
1603: if (skb1 == sk->rqueue)
1604: sk->rqueue = skb;
1605: break;
1606: }
1607: if ( skb1->prev == sk->rqueue)
1608: {
1609: skb->next= skb1;
1610: skb->prev = skb1->prev;
1611: skb->prev->next = skb;
1612: skb1->prev = skb;
1613: skb1 = NULL; /* so we know we might be able to ack stuff. */
1614: break;
1615: }
1616: }
1617:
1618: PRINTK ("skb = %X:\n",skb);
1619: print_skb (skb);
1620: PRINTK ("sk now equals:\n");
1621: print_sk (sk);
1622:
1623: }
1624:
1625: th->ack_seq = th->seq + skb->len;
1626: if (th->syn) th->ack_seq ++;
1627: if (th->fin) th->ack_seq ++;
1628:
1629: if (before (sk->acked_seq, sk->copied_seq))
1630: {
1631: printk ("*** tcp.c:tcp_data bug acked < copied\n");
1632: sk->acked_seq = sk->copied_seq;
1633: }
1634:
1635: /* now figure out if we can ack anything. */
1636: if (skb1 == NULL || skb1->acked || before (th->seq, sk->acked_seq+1))
1637: {
1638: if (before (th->seq, sk->acked_seq+1))
1639: {
1640: sk->acked_seq = th->ack_seq;
1641: skb->acked = 1;
1642:
1643: for (skb2=skb->next; skb2 != sk->rqueue->next; skb2=skb2->next)
1644: {
1645: if (before(skb2->h.th->seq, sk->acked_seq+1))
1646: {
1647: sk->acked_seq = skb2->h.th->ack_seq;
1648: skb2->acked = 1;
1649: /* force an immediate ack. */
1650: sk->ack_backlog = sk->max_ack_backlog;
1651: }
1652: else
1653: break;
1654: }
1655:
1656: /* this also takes care of updating the window. */
1657: /* this if statement needs to be simplified. */
1658:
1659: if (!sk->delay_acks ||
1660: sk->ack_backlog >= sk->max_ack_backlog ||
1661: sk->window < 2*sk->mtu + sk->bytes_rcv ||
1662: sk->bytes_rcv > sk->max_unacked ||
1663: th->fin)
1664: {
1665: tcp_send_ack (sk->send_seq, sk->acked_seq,sk,th, saddr);
1666: }
1667: else
1668: {
1669: sk->ack_backlog++;
1670: sk->time_wait.len = TCP_ACK_TIME;
1671: sk->timeout = TIME_WRITE;
1672: reset_timer ((struct timer *)&sk->time_wait);
1673: }
1674: }
1675: }
1676: else
1677: {
1678: /* we missed a packet. Send an ack to try to resync things. */
1679: tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
1680: }
1681:
1682: /* now tell the user we may have some data. */
1683: if (!sk->dead)
1684: {
1685: wake_up (sk->sleep);
1686: }
1687: else
1688: {
1689: PRINTK ("data received on dead socket. \n");
1690: }
1691:
1692: if (sk->state > TCP_CLOSING && sk->acked_seq == sk->fin_seq)
1693: {
1694: sk->state = TCP_CLOSE;
1695: }
1696:
1697: return (0);
1698: }
1699:
1700: static int
1701: tcp_urg (volatile struct sock *sk, struct tcp_header *th, unsigned long saddr)
1702: {
1703: extern int kill_pg (int pg, int sig, int priv);
1704: extern int kill_proc (int pid, int sig, int priv);
1705:
1706: if (!sk->dead)
1707: wake_up(sk->sleep);
1708:
1709: if (sk->urginline)
1710: {
1711: th->urg = 0;
1712: th->psh = 1;
1713: return (0);
1714: }
1715:
1716: sk->urg++;
1717:
1718: if (!sk->urg)
1719: {
1720: /* so if we get more urgent data, we don't
1721: signal the user again. */
1722: if (sk->proc == 0) return (0);
1723: if (sk->proc > 0)
1724: {
1725: kill_proc (sk->proc, SIGURG, 1);
1726: }
1727: else
1728: {
1729: kill_pg (-sk->proc, SIGURG, 1);
1730: }
1731: }
1732: return (0);
1733: }
1734:
1735: /* this deals with incoming fins. */
1736: static int
1737: tcp_fin (volatile struct sock *sk, struct tcp_header *th,
1738: unsigned long saddr, struct device *dev)
1739: {
1740: struct sk_buff *buff;
1741: struct tcp_header *t1;
1742: int tmp;
1743: PRINTK ("tcp_fin (sk=%X, th=%X, saddr=%X, dev=%X)\n",
1744: sk, th, saddr, dev);
1745:
1746: if (!sk->dead)
1747: {
1748: wake_up (sk->sleep);
1749: }
1750:
1751: /* after sending the fin, we aren't allowed to write anymore. */
1752: sk->shutdown |= SEND_SHUTDOWN;
1753:
1754: sk->err = 0;
1755: switch (sk->state)
1756: {
1757: case TCP_SYN_RECV:
1758: case TCP_SYN_SENT:
1759: case TCP_ESTABLISHED:
1760: sk->state = TCP_LAST_ACK;
1761: break;
1762:
1763: default:
1764: case TCP_FIN_WAIT1:
1765: case TCP_TIME_WAIT:
1766: sk->state = TCP_LAST_ACK;
1767: /* start the timers. */
1768: sk->time_wait.len = TCP_TIMEWAIT_LEN;
1769: sk->timeout = TIME_CLOSE;
1770: reset_timer ((struct timer *)&sk->time_wait);
1771: return (0);
1772:
1773: case TCP_FIN_WAIT2:
1774: sk->state = TCP_CLOSE;
1775: return (0);
1776: }
1777:
1778: /* send an ack and our own fin. */
1779: buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
1780: if (buff == NULL)
1781: {
1782: /* we will ignore the fin. That way it will be sent again. */
1783: return (1);
1784: }
1785:
1786: buff->mem_addr = buff;
1787: buff->mem_len = MAX_ACK_SIZE;
1788: buff->len=sizeof (struct tcp_header);
1789: buff->sk = sk;
1790:
1791: t1 = (struct tcp_header *)(buff + 1);
1792: /* put in the ip_header and routing stuff. */
1793: tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
1794: IP_TCP, sk->opt, MAX_ACK_SIZE);
1795: if (tmp < 0)
1796: {
1797: sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
1798: return (0);
1799: }
1800:
1801: buff->len += tmp;
1802: t1 = (struct tcp_header *)((char *)t1 +tmp);
1803:
1804: memcpy (t1, th, sizeof (*t1));
1805:
1806: /* swap the send and the receive. */
1807: t1->dest = th->source;
1808: t1->source = th->dest;
1809:
1810:
1811: t1->seq = net32(sk->send_seq++);
1812:
1813: /* contains the one that needs to be acked. */
1814: sk->fin_seq = th->seq+1;
1815:
1816: buff->h.seq = sk->send_seq;
1817: t1->window = net16(sk->prot->rspace(sk));
1818:
1819: t1->res1=0;
1820: t1->res2=0;
1821: t1->rst = 0;
1822: t1->urg = 0;
1823: t1->syn = 0;
1824: t1->psh = 0;
1825: t1->ack = 1;
1826: t1->fin = 1;
1827: t1->ack_seq = net32(sk->acked_seq);
1828:
1829: t1->doff = sizeof (*t1)/4;
1830: tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
1831:
1832: /* can't just queue this up. It should go at the end of
1833: the write queue. */
1834: if (sk->wback != NULL)
1835: {
1836: buff->next = NULL;
1837: sk->wback->next = buff;
1838: sk->wback = buff;
1839: }
1840: else
1841: {
1842: sk->prot->queue_xmit (sk, dev, buff,0);
1843: }
1844:
1845: return (0);
1846: }
1847:
1848:
1849: /* this will accept the next outstanding connection. */
1850:
1851: static volatile struct sock *
1852: tcp_accept (volatile struct sock *sk, int flags)
1853: {
1854: volatile struct sock *newsk;
1855: struct sk_buff *skb;
1856:
1857: PRINTK ("tcp_accept(sk=%X, flags=%X)\n", sk, flags);
1858: print_sk(sk);
1859: /* we need to make sure that this socket is listening, and that
1860: it has something pending. */
1861:
1862: if (sk->state != TCP_LISTEN)
1863: {
1864: sk->err = EINVAL;
1865: return (NULL);
1866: }
1867: /* avoid the race. */
1868:
1869: sk->inuse = 1;
1870: cli();
1871: while ( (skb = get_firstr(sk)) == NULL )
1872: {
1873: if (flags & O_NONBLOCK)
1874: {
1875: sti();
1876: release_sock (sk);
1877: sk->err = EAGAIN;
1878: return (NULL);
1879: }
1880:
1881: release_sock (sk);
1882: interruptible_sleep_on (sk->sleep);
1883: if (current->signal & ~current->blocked)
1884: {
1885: sti();
1886: sk->err = ERESTARTSYS;
1887: return (NULL);
1888: }
1889:
1890: sk->inuse = 1;
1891: }
1892: sti();
1893:
1894: /* now all we need to do is return skb->sk. */
1895: newsk = skb->sk;
1896: free_skb (skb, FREE_READ);
1897: release_sock (sk);
1898: return (newsk);
1899: }
1900:
1901:
1902:
1903: /* this will initiate an outgoing connection. */
1904: static int
1905: tcp_connect (volatile struct sock *sk, struct sockaddr_in *usin, int addr_len)
1906: {
1907: struct sk_buff *buff;
1908: struct sockaddr_in sin;
1909: struct device *dev=NULL;
1910: unsigned char *ptr;
1911: int tmp;
1912: struct tcp_header *t1;
1913: if (sk->state != TCP_CLOSE) return (-EISCONN);
1914: if (addr_len < 8) return (-EINVAL);
1915:
1916: verify_area (usin, addr_len);
1917: memcpy_fromfs (&sin,usin, min(sizeof (sin), addr_len));
1918:
1919: if (sin.sin_family && sin.sin_family != AF_INET) return (-EAFNOSUPPORT);
1920:
1921: sk->daddr = sin.sin_addr.s_addr;
1922: sk->send_seq = timer_seq*SEQ_TICK-seq_offset;
1923: sk->rcv_ack_seq = sk->send_seq -1;
1924: sk->err = 0;
1925: sk->dummy_th.dest = sin.sin_port;
1926:
1927: buff=sk->prot->wmalloc(sk,MAX_SYN_SIZE,0);
1928: if (buff == NULL)
1929: {
1930: return (-ENOMEM);
1931: }
1932: sk->inuse = 1;
1933: buff->mem_addr = buff;
1934: buff->mem_len = MAX_SYN_SIZE;
1935: buff->len=24;
1936: buff->sk = sk;
1937: t1=(struct tcp_header *)(buff + 1);
1938: /* put in the ip_header and routing stuff. */
1939: /* We need to build the routing stuff fromt the things saved
1940: in skb. */
1941: tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
1942: IP_TCP, NULL, MAX_SYN_SIZE);
1943: if (tmp < 0)
1944: {
1945: sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
1946: release_sock (sk);
1947: return (-ENETUNREACH);
1948: }
1949: buff->len += tmp;
1950: t1 = (struct tcp_header *)((char *)t1 +tmp);
1951:
1952: memcpy (t1, (void *)&(sk->dummy_th), sizeof (*t1));
1953: t1->seq = net32(sk->send_seq++);
1954: buff->h.seq = sk->send_seq;
1955: t1->ack = 0;
1956: t1->window = 2;
1957: t1->res1=0;
1958: t1->res2=0;
1959: t1->rst = 0;
1960: t1->urg = 0;
1961: t1->psh = 0;
1962: t1->syn = 1;
1963: t1->urg_ptr = 0;
1964: t1->doff =6;
1965: /* put in the tcp options to say mtu. */
1966: ptr=(unsigned char *)(t1+1);
1967: ptr[0]=2;
1968: ptr[1]=4;
1969: ptr[2]=(dev->mtu- HEADER_SIZE) >> 8;
1970: ptr[3]=(dev->mtu- HEADER_SIZE) & 0xff;
1971: sk->mtu = dev->mtu - HEADER_SIZE;
1972: tcp_send_check (t1, sk->saddr, sk->daddr,
1973: sizeof (struct tcp_header) + 4, sk);
1974: /* this must go first otherwise a really quick response will
1975: get reset. */
1976: sk->state = TCP_SYN_SENT;
1977:
1978: sk->prot->queue_xmit(sk, dev, buff, 0);
1979:
1980: sk->time_wait.len = TCP_CONNECT_TIME;
1981: reset_timer ((struct timer *)&sk->time_wait);
1982: sk->retransmits = TCP_RETR1 - TCP_SYN_RETRIES;
1983: release_sock (sk);
1984: return (0);
1985: }
1986:
1987:
1988: /* this functions checks to see if the tcp header is actually
1989: acceptible. */
1990:
1991: static int
1992: tcp_sequence (volatile struct sock *sk, struct tcp_header *th, short len,
1993: struct options *opt, unsigned long saddr)
1994: {
1995: /* this isn't quite right. sk->acked_seq could be more recent
1996: than sk->window. This is however close enough. We will accept
1997: slightly more packets than we should, but it should not cause
1998: problems unless someone is trying to forge packets. */
1999:
2000: if (between(th->seq, sk->acked_seq, sk->acked_seq + sk->window)||
2001: between(th->seq + len-sizeof (*th), sk->acked_seq+1,
2002: sk->acked_seq + sk->window))
2003: {
2004: return (1);
2005: }
2006:
2007: /* if it's too far ahead, send an ack to let the other end
2008: know what we expect. */
2009: if (after (th->seq, sk->acked_seq + sk->window))
2010: {
2011: tcp_send_ack (sk->send_seq, sk->acked_seq, sk, th, saddr);
2012: return (0);
2013: }
2014:
2015: if (!th->rst)
2016: {
2017: if (len != th->doff*4 || th->fin || th->syn)
2018: {
2019: sk->delay_acks = 0;
2020: }
2021:
2022: /* try to resync things. */
2023: tcp_send_ack (net32(th->ack_seq), sk->acked_seq, sk, th, saddr);
2024: }
2025:
2026: /* in case it's just a late ack, let it through */
2027: if (th->ack && len == th->doff*4 && after (th->seq, sk->acked_seq - 4096) &&
2028: !th->fin && !th->syn) return (1);
2029:
2030: return (0);
2031: }
2032:
2033: /* This deals with the tcp option. It isn't very general yet. */
2034: static void
2035: tcp_options (volatile struct sock *sk, struct tcp_header *th)
2036: {
2037: unsigned char *ptr;
2038: ptr = (unsigned char *)(th + 1);
2039: if (ptr[0] != 2 || ptr[1] != 4)
2040: {
2041: sk->mtu = min (sk->mtu, 576-HEADER_SIZE);
2042: return;
2043: }
2044: sk->mtu = min (sk->mtu, ptr[2]*256 + ptr[3] - HEADER_SIZE);
2045: }
2046:
2047: int
2048: tcp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
2049: unsigned long daddr, unsigned short len,
2050: unsigned long saddr, int redo, struct ip_protocol * protocol)
2051: {
2052: struct tcp_header *th;
2053: volatile struct sock *sk;
2054:
2055: th = skb->h.th;
2056: /* find the socket. */
2057: sk=get_sock(&tcp_prot, net16(th->dest), saddr, th->source, daddr);
2058: PRINTK("<<\n");
2059: PRINTK("len = %d, redo = %d, skb=%X\n", len, redo, skb);
2060:
2061: if (sk)
2062: {
2063: PRINTK ("sk = %X:\n",sk);
2064: print_sk (sk);
2065: }
2066:
2067: if (!redo)
2068: {
2069: if (th->check && tcp_check (th, len, saddr, daddr ))
2070: {
2071: skb->sk = NULL;
2072: free_skb (skb, 0);
2073: /* we don't release the socket because it was never
2074: marked in use. */
2075: return (0);
2076: }
2077:
2078: /*See if we know about the socket. */
2079: if (sk == NULL)
2080: {
2081: if (!th->rst)
2082: tcp_reset (daddr, saddr, th, &tcp_prot, opt,dev);
2083: skb->sk = NULL;
2084: free_skb (skb, 0);
2085: return (0);
2086: }
2087:
2088: skb->len = len;
2089: skb->sk = sk;
2090: skb->acked = 0;
2091: skb->used = 0;
2092: skb->free = 0;
2093: skb->urg_used = 0;
2094: skb->saddr = daddr;
2095: skb->daddr = saddr;
2096:
2097: th->seq = net32(th->seq);
2098:
2099: cli();
2100:
2101: /* we may need to add it to the backlog here. */
2102: if (sk->inuse)
2103: {
2104: if (sk->back_log == NULL)
2105: {
2106: sk->back_log = skb;
2107: skb->next = skb;
2108: skb->prev = skb;
2109: }
2110: else
2111: {
2112: skb->next = sk->back_log;
2113: skb->prev = sk->back_log->prev;
2114: skb->prev->next = skb;
2115: skb->next->prev = skb;
2116: }
2117: sti();
2118: return (0);
2119: }
2120: sk->inuse = 1;
2121: sti();
2122: }
2123:
2124: /* charge the memory to the socket. */
2125: if (sk->rmem_alloc + skb->mem_len >= SK_RMEM_MAX)
2126: {
2127: skb->sk = NULL;
2128: free_skb (skb, 0);
2129: release_sock (sk);
2130: return (0);
2131: }
2132:
2133: sk->rmem_alloc += skb->mem_len;
2134:
2135: PRINTK ("About to do switch. \n");
2136:
2137: /* now deal with it. */
2138:
2139: switch (sk->state)
2140: {
2141: /* this should close the system down if it's waiting for an
2142: ack that is never going to be sent. */
2143: case TCP_LAST_ACK:
2144: if (th->rst)
2145: {
2146: sk->err = ECONNRESET;
2147: sk->state = TCP_CLOSE;
2148: if (!sk->dead)
2149: {
2150: wake_up (sk->sleep);
2151: }
2152: free_skb (skb, FREE_READ);
2153: release_sock(sk);
2154: return (0);
2155: }
2156:
2157: case TCP_ESTABLISHED:
2158: case TCP_FIN_WAIT1:
2159: case TCP_FIN_WAIT2:
2160: case TCP_TIME_WAIT:
2161:
2162: if (!tcp_sequence (sk, th, len, opt, saddr))
2163: {
2164: free_skb (skb, FREE_READ);
2165: release_sock(sk);
2166: return (0);
2167: }
2168: if (th->rst)
2169: {
2170: sk->err = ECONNRESET;
2171: sk->state = TCP_CLOSE;
2172: if (!sk->dead)
2173: {
2174: wake_up (sk->sleep);
2175: }
2176: free_skb (skb, FREE_READ);
2177: release_sock(sk);
2178: return (0);
2179: }
2180:
2181: if (opt->security != 0 || opt->compartment != 0 || th->syn)
2182: {
2183: sk->err = ECONNRESET;
2184: sk->state = TCP_CLOSE;
2185: tcp_reset (daddr, saddr, th, sk->prot, opt,dev);
2186: if (!sk->dead)
2187: {
2188: wake_up (sk->sleep);
2189: }
2190: free_skb (skb, FREE_READ);
2191: release_sock(sk);
2192: return (0);
2193: }
2194:
2195: if (th->ack)
2196: {
2197: if(!tcp_ack (sk, th, saddr))
2198: {
2199: free_skb (skb, FREE_READ);
2200: release_sock(sk);
2201: return (0);
2202: }
2203: }
2204: if (th->urg)
2205: {
2206: if (tcp_urg (sk, th, saddr))
2207: {
2208: free_skb (skb, FREE_READ);
2209: release_sock(sk);
2210: return (0);
2211: }
2212: }
2213:
2214: if ( tcp_data (skb, sk, saddr, len))
2215: {
2216: free_skb (skb, FREE_READ);
2217: release_sock(sk);
2218: return (0);
2219: }
2220:
2221: if (!th->fin)
2222: {
2223: release_sock(sk);
2224: return (0);
2225: }
2226:
2227: tcp_fin (sk, th, saddr, dev);
2228: release_sock(sk);
2229: return (0);
2230:
2231: case TCP_CLOSE:
2232:
2233: if (sk->dead || sk->daddr)
2234: {
2235: PRINTK ("packet received for closed,dead socket\n");
2236: free_skb (skb, FREE_READ);
2237: release_sock (sk);
2238: return (0);
2239: }
2240:
2241: if (!th->rst)
2242: {
2243: if (!th->ack)
2244: th->ack_seq=0;
2245: tcp_reset (daddr, saddr, th, sk->prot, opt,dev);
2246: }
2247: free_skb (skb, FREE_READ);
2248: release_sock(sk);
2249: return (0);
2250:
2251: case TCP_LISTEN:
2252: if (th->rst)
2253: {
2254: free_skb (skb, FREE_READ);
2255: release_sock(sk);
2256: return (0);
2257: }
2258: if (th->ack)
2259: {
2260: tcp_reset (daddr, saddr, th, sk->prot, opt,dev );
2261: free_skb (skb, FREE_READ);
2262: release_sock(sk);
2263: return (0);
2264: }
2265:
2266: if (th->syn)
2267: {
2268: /* if (opt->security != 0 || opt->compartment != 0)
2269: {
2270: tcp_reset (daddr, saddr, th, prot, opt,dev);
2271: release_sock(sk);
2272: return (0);
2273: } */
2274:
2275: /* now we just put the whole thing including the header
2276: and saddr, and protocol pointer into the buffer.
2277: We can't respond until the user tells us to accept
2278: the connection. */
2279:
2280: tcp_conn_request (sk, skb, daddr, saddr, opt, dev);
2281:
2282: release_sock(sk);
2283: return (0);
2284: }
2285:
2286: free_skb (skb, FREE_READ);
2287: release_sock(sk);
2288: return (0);
2289:
2290: default:
2291: if (!tcp_sequence (sk, th, len, opt, saddr))
2292: {
2293: free_skb (skb, FREE_READ);
2294: release_sock(sk);
2295: return (0);
2296: }
2297:
2298: case TCP_SYN_SENT:
2299: if (th->rst)
2300: {
2301: sk->err = ECONNREFUSED;
2302: sk->state = TCP_CLOSE;
2303: if (!sk->dead)
2304: {
2305: wake_up (sk->sleep);
2306: }
2307: free_skb (skb, FREE_READ);
2308: release_sock(sk);
2309: return (0);
2310: }
2311: /* if (opt->security != 0 || opt->compartment != 0 )
2312: {
2313: sk->err = ECONNRESET;
2314: sk->state = TCP_CLOSE;
2315: tcp_reset (daddr, saddr, th, sk->prot, opt, dev);
2316: if (!sk->dead)
2317: {
2318: wake_up (sk->sleep);
2319: }
2320: free_skb (skb, FREE_READ);
2321: release_sock(sk);
2322: return (0);
2323: } */
2324:
2325: if (!th->ack)
2326: {
2327: if (th->syn)
2328: {
2329: sk->state = TCP_SYN_RECV;
2330: }
2331:
2332: free_skb (skb, FREE_READ);
2333: release_sock(sk);
2334: return (0);
2335: }
2336:
2337: switch (sk->state)
2338: {
2339: case TCP_SYN_SENT:
2340: if (!tcp_ack(sk, th, saddr))
2341: {
2342: tcp_reset(daddr, saddr, th, sk->prot, opt,dev);
2343: free_skb (skb, FREE_READ);
2344: release_sock(sk);
2345: return (0);
2346: }
2347:
2348: /* if the syn bit is also set, switch to tcp_syn_recv,
2349: and then to established. */
2350:
2351: if (!th->syn)
2352: {
2353: free_skb (skb, FREE_READ);
2354: release_sock (sk);
2355: return (0);
2356: }
2357:
2358: /* ack the syn and fall through. */
2359: sk->acked_seq = th->seq+1;
2360: sk->fin_seq = th->seq;
2361: tcp_send_ack (sk->send_seq, th->seq+1, sk,
2362: th, sk->daddr);
2363:
2364: case TCP_SYN_RECV:
2365: if (!tcp_ack(sk, th, saddr))
2366: {
2367: tcp_reset(daddr, saddr, th, sk->prot, opt, dev);
2368: free_skb (skb, FREE_READ);
2369: release_sock(sk);
2370: return (0);
2371: }
2372:
2373: sk->state = TCP_ESTABLISHED;
2374: /* now we need to finish filling out some of the tcp
2375: header. */
2376:
2377: /* we need to check for mtu info. */
2378: tcp_options(sk, th);
2379: sk->dummy_th.dest = th->source;
2380: sk->copied_seq = sk->acked_seq-1;
2381: if (!sk->dead)
2382: {
2383: wake_up (sk->sleep);
2384: }
2385:
2386: /* now process the rest like we were already in the established
2387: state. */
2388: if (th->urg)
2389: if (tcp_urg (sk, th, saddr))
2390: {
2391: free_skb (skb, FREE_READ);
2392: release_sock(sk);
2393: return (0);
2394: }
2395: if (tcp_data (skb, sk, saddr, len))
2396: free_skb (skb, FREE_READ);
2397:
2398: if (th->fin)
2399: tcp_fin(sk, th, saddr, dev);
2400:
2401: release_sock(sk);
2402: return (0);
2403: }
2404:
2405: if (th->urg)
2406: {
2407: if (tcp_urg (sk, th, saddr))
2408: {
2409: free_skb (skb, FREE_READ);
2410: release_sock (sk);
2411: return (0);
2412: }
2413: }
2414:
2415: if (tcp_data (skb, sk, saddr, len))
2416: {
2417: free_skb (skb, FREE_READ);
2418: release_sock (sk);
2419: return (0);
2420: }
2421:
2422: if (!th->fin)
2423: {
2424: release_sock(sk);
2425: return (0);
2426: }
2427: tcp_fin (sk, th, saddr, dev);
2428: release_sock(sk);
2429: return (0);
2430: }
2431: }
2432:
2433:
2434: /* this routine sends a packet with an out of date sequence number. It
2435: assumes the other end will try to ack it. */
2436:
2437: static void
2438: tcp_write_wakeup(volatile struct sock *sk)
2439: {
2440: struct sk_buff *buff;
2441: struct tcp_header *t1;
2442: struct device *dev=NULL;
2443: int tmp;
2444: if (sk -> state != TCP_ESTABLISHED) return;
2445:
2446: buff=sk->prot->wmalloc(sk,MAX_ACK_SIZE,1);
2447: /* no big loss. */
2448: if (buff == NULL) return;
2449:
2450: buff->mem_addr = buff;
2451: buff->mem_len = MAX_ACK_SIZE;
2452: buff->len=sizeof (struct tcp_header);
2453: buff->free = 1;
2454: buff->sk = sk;
2455: PRINTK ("in tcp_write_wakeup\n");
2456: t1=(struct tcp_header *)(buff + 1);
2457:
2458: /* put in the ip_header and routing stuff. */
2459: tmp = sk->prot->build_header (buff, sk->saddr, sk->daddr, &dev,
2460: IP_TCP, sk->opt, MAX_ACK_SIZE);
2461: if (tmp < 0)
2462: {
2463: sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
2464: return;
2465: }
2466:
2467: buff->len += tmp;
2468: t1 = (struct tcp_header *)((char *)t1 +tmp);
2469:
2470: memcpy (t1,(void *) &sk->dummy_th, sizeof (*t1));
2471:
2472: /* use a previous sequence. This should cause the other end
2473: to send an ack. */
2474: t1->seq = net32(sk->send_seq-1);
2475: t1->ack = 1;
2476: t1->res1= 0;
2477: t1->res2= 0;
2478: t1->rst = 0;
2479: t1->urg = 0;
2480: t1->psh = 0;
2481: t1->fin = 0;
2482: t1->syn = 0;
2483: t1->ack_seq = net32(sk->acked_seq);
2484: t1->window = net16(sk->prot->rspace(sk));
2485: t1->doff = sizeof (*t1)/4;
2486: tcp_send_check (t1, sk->saddr, sk->daddr, sizeof (*t1), sk);
2487: /* send it and free it. This will prevent the timer from
2488: automatically being restarted. */
2489: sk->prot->queue_xmit(sk, dev, buff, 1);
2490:
2491: }
2492:
2493: struct proto tcp_prot =
2494: {
2495: sock_wmalloc,
2496: sock_rmalloc,
2497: sock_wfree,
2498: sock_rfree,
2499: sock_rspace,
2500: sock_wspace,
2501: tcp_close,
2502: tcp_read,
2503: tcp_write,
2504: NULL,
2505: NULL,
2506: ip_build_header,
2507: tcp_connect,
2508: tcp_accept,
2509: ip_queue_xmit,
2510: tcp_retransmit,
2511: tcp_write_wakeup,
2512: tcp_read_wakeup,
2513: tcp_rcv,
2514: tcp_select,
2515: tcp_ioctl,
2516: NULL,
2517: 128,
2518: 0,
2519: {NULL,}
2520: };
2521:
2522:
2523:
2524:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.