2 * This file is part of the ELKS TCP/IP stack
4 * (C) 2001 Harry Kalogirou (harkal@rainbow.cs.unipi.gr)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 * TODO : IP fragmentation and reassemply of fragmented IP packets
16 #include <sys/types.h>
26 #define IP_VERSION(s) ((s)->version_ihl>>4&0xf)
27 #define IP_IHL(s) ((s)->version_ihl&0xf)
28 #define IP_FLAGS(s) ((s)->frag_off>>13)
34 static char ipbuf
[1024];
42 __u16
ip_calc_chksum(char *data
, int len
)
46 __u16
*p
= (__u16
*) data
;
50 for (i
=0; i
< len
; i
++){
54 return ~((sum
& 0xffff) + ((sum
>> 16) & 0xffff));
58 /*__u16 ip_calc_chksum(char *data, int len)*/
60 .globl _ip_calc_chksum
94 void ip_print(struct iphdr_s
*head
)
100 printf("Version/IHL : %d/%d\n",IP_VERSION(head
),IP_IHL(head
));
101 printf("TypeOfService/Length : %d/%d\n",head
->tos
,ntohs(head
->tot_len
));
102 printf("Id/flags/fragment offset : %d/%d\n",head
->id
,head
->frag_off
);
103 printf("ttl : %d\n",head
->ttl
);
104 printf("Protocol : %d\n",head
->protocol
);
106 addr
= (__u8
*)&head
->saddr
;
107 printf("saddr : %d.%d.%d.%d \n",addr
[0],addr
[1],addr
[2],addr
[3]);
108 addr
= (__u8
*)&head
->daddr
;
109 printf("daddr : %d.%d.%d.%d \n",addr
[0],addr
[1],addr
[2],addr
[3]);
111 printf("check sum = %d\n",ip_calc_chksum(head
, 4 * IP_IHL(head
)));
113 addr
= (__u8
*)head
+ 4 * IP_IHL(head
);
114 for( i
= 0 ; i
< ntohs(head
->tot_len
) - 20 ; i
++ )
115 printf("%x ",addr
[i
]);
121 void ip_recvpacket(char *packet
,int size
)
123 struct iphdr_s
*iphdr
;
126 iphdr
= (struct iphdr_s
*)packet
;
128 /*printf("IP: Got packet of size : %d \n",size,*packet);
131 if(IP_VERSION(iphdr
) != 4){
133 printf("IP : Bad IP version\n");
138 if(IP_IHL(iphdr
) < 5){
140 printf("IP : Bad IHL\n");
145 data
= packet
+ 4 * IP_IHL(iphdr
);
147 switch (iphdr
->protocol
) {
150 printf("IP : ICMP packet\n");
153 icmp_process(iphdr
, data
);
158 printf("IP : TCP packet\n");
169 void ip_sendpacket(char *packet
,int len
,struct addr_pair
*apair
)
171 struct iphdr_s
*iph
= (struct iphdr_s
*)&ipbuf
;
174 iph
->version_ihl
= 0x45;
176 tlen
= 4 * IP_IHL(iph
);
179 iph
->tot_len
= htons(tlen
+ len
);
183 iph
->protocol
= apair
->protocol
;
184 iph
->daddr
= apair
->daddr
;
185 iph
->saddr
= apair
->saddr
;
188 iph
->check
= ip_calc_chksum((char *)iph
, tlen
);
190 memcpy(&ipbuf
[tlen
], packet
, len
);
193 if(iph
->daddr
== local_ip
&& iph
->daddr
== 0x0100007f) {
197 slip_send(&ipbuf
, tlen
+ len
);