summaryrefslogtreecommitdiff
path: root/net/ipv4.c
blob: f1d4043d86c71e28dfb16c926db39d44338f9f0a (plain)
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
#include "ipv4.h"
#include "net_sys.h"
#include "inet.h"
#include "eth.h"
#include "icmp.h"

bool ipv4_incoming(struct netdev *dev,struct eth_hdr *hdr)
{
    struct ipv4_hdr *ipv4=hdr->payload;
//    klog("ipv4 incoming with checksum: 0x%04X",ntohs(ipv4->csum));
    ipv4->csum=0;
//    klog("expected checksum: 0x%04X",checksum(ipv4,ntohs(ipv4->len)));
//    klog("ipv4 header len=%d",ipv4->ihl);

    if(ipv4->proto==IPV4_P_ICMP)
    {
	icmp_incoming(dev,hdr);
    }

    return true;
}

uint32_t ipv4_generic(struct netdev *dev, uint32_t ip, uint8_t type, uint8_t* pos,uint8_t *end)
{

    pos-=20; // we need 20 bytes for the ipv4 header.
    struct ipv4_hdr *ipv4=pos;
    ipv4->ihl=5; // 20 bytes
    ipv4->version=4; // ipv 4

    ipv4->tos=0;	 // type of service?
    ipv4->len=htons(end-pos);	 // total length of ip datagramm in bytes
    ipv4->id=0;		 // index of datagram for fragmentation

    //ipv4->flags=0x2;	 // various control flags 0x20 do not fragment
    //ipv4->frag_offset=0; // fragment offset 0 - first
    ipv4->flags_and_offset=0; // TODO! 0x2?

    ipv4->ttl=64;	 // time to live - countdown decreased by each receiver
    ipv4->proto=type;    // payload protocol / 1-icmp, 16 - udp , 6 -tcp
    ipv4->csum=0;	 // header checksum
    ipv4->saddr=dev->ip; // source ip address
    ipv4->daddr=ip;	    // destination ip address

    //ipv4->csum=checksum(ipv4,ntohs(ipv4->len));
    ipv4->csum=checksum(ipv4,20);

    return eth_generic(dev,ip,ETH_P_IPV4,pos,end);
}