Subversion Repositories Kolibri OS

Rev

Rev 261 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ha 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;;  IP.INC                                                         ;;
4
;;                                                                 ;;
5
;;  IP Processes for Menuet OS  TCP/IP stack                       ;;
6
;;                                                                 ;;
7
;;  Version 0.3  29 August 2002                                    ;;
8
;;                                                                 ;;
9
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
10
;;                                                                 ;;
11
;;  See file COPYING for details                                   ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
261 hidnplayr 15
; IP underlying protocols numbers
16
PROTOCOL_ICMP     equ      1
17
PROTOCOL_TCP      equ      6
18
PROTOCOL_UDP      equ      17
1 ha 19
 
261 hidnplayr 20
struc IP_PACKET
21
{  .VersionAndIHL           db   ?  ;+00 - Version[0-3 bits] and IHL(header length)[4-7 bits]
22
   .TypeOfService           db   ?  ;+01
23
   .TotalLength             dw   ?  ;+02
24
   .Identification          dw   ?  ;+04
25
   .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
26
   .TimeToLive              db   ?  ;+08
27
   .Protocol                db   ?  ;+09
28
   .HeaderChecksum          dw   ?  ;+10
29
   .SourceAddress           dd   ?  ;+12
30
   .DestinationAddress      dd   ?  ;+16
31
   .DataOrOptional          dd   ?  ;+20
32
}
33
 
34
virtual at 0
35
  IP_PACKET IP_PACKET
36
end virtual
37
 
38
 
1 ha 39
;*******************************************************************
40
;   Interface
41
;
42
;       ip_rx       processes all packets received by the network layer
43
;                   It calls the appropriate protocol handler
44
;
45
;
46
;
47
;*******************************************************************
48
 
49
 
261 hidnplayr 50
;
51
;   IP Packet after reception - Normal IP packet format
52
;
53
;           0               1               2               3
54
;    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
55
;
56
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57
;0  |Version|  IHL  |Type of Service|       Total Length            |
58
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59
;4  |         Identification        |Flags|      Fragment Offset    |
60
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61
;8  |  Time to Live |    Protocol   |         Header Checksum       |
62
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63
;12 |                       Source Address                          |
64
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65
;16 |                    Destination Address                        |
66
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67
;20 |      Data                                                     |
68
;   +-+-+-..........                                               -+
69
;
70
;
71
;    [smb] attention! according to RFC 791 IP packet may have 'options' sections,
72
; so we can't simply think, that data have offset 20. We must calculate offset from
73
; IHL field
74
;
75
macro	GET_IHL reg, header_addr
76
{
77
	movzx	reg, byte [header_addr]
78
 
79
	; we need 4-7 bits, so....
80
	and	reg, 0x0000000F
81
 
82
	; IHL keeps number of octets, so we need to << 2 'reg'
83
	shl     reg, 2
84
}
85
 
86
 
1 ha 87
;***************************************************************************
88
;   Function
89
;      ip_rx
90
;
91
;   Description
92
;       This is a kernel function, called by stack_handler
261 hidnplayr 93
;       Processes all IP-packets received by the network layer
94
;       It calls the appropriate protocol handler
1 ha 95
;
96
;***************************************************************************
261 hidnplayr 97
proc ip_rx stdcall
98
local buffer_number dd ?
99
 
1 ha 100
    ; Look for a buffer to tx
101
    mov     eax, IPIN_QUEUE
102
    call    dequeue
103
    cmp     ax, NO_BUFFER
261 hidnplayr 104
    je      .exit         ; Exit if no buffer available
1 ha 105
 
261 hidnplayr 106
    mov     [buffer_number], eax    ;save buffer number
1 ha 107
 
108
    ; convert buffer pointer eax to the absolute address
261 hidnplayr 109
    imul    eax, IPBUFFSIZE
1 ha 110
    add     eax, IPbuffs
111
 
261 hidnplayr 112
    mov     ebx, eax  ; ebx=pointer to IP_PACKET
1 ha 113
 
114
    ; Validate the IP checksum
261 hidnplayr 115
    mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
116
    xchg    dh,dl          ; Get the checksum in intel format
117
 
118
    mov     [ebx + IP_PACKET.HeaderChecksum], word 0  ; clear checksum field - need to when
119
                                ; recalculating checksum
1 ha 120
    ;  this needs two data pointers and two size #.
121
    ;  2nd pointer can be of length 0
122
 
261 hidnplayr 123
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
124
    stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
125
    cmp     dx, ax
1 ha 126
 
261 hidnplayr 127
    mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
128
    jnz     .dump  ;if CHECKSUM isn't valid then dump packet
129
 
130
    ; Validate the IP address, if it isn't broadcast
131
    mov     eax, [stack_ip]
132
    cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
133
    je      @f
134
 
1 ha 135
    ; If the IP address is 255.255.255.255, accept it
136
    ; - it is a broadcast packet, which we need for dhcp
261 hidnplayr 137
    cmp     dword[ebx + IP_PACKET.DestinationAddress], 0xffffffff
138
    jne     .dump
1 ha 139
 
261 hidnplayr 140
  @@:
141
    mov     al, [ebx + IP_PACKET.VersionAndIHL]
142
    and     al, 0x0f  ;get IHL(header length)
143
    cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
144
    jnz     .dump     ;then dump it
1 ha 145
 
261 hidnplayr 146
    cmp     byte[ebx + IP_PACKET.TimeToLive], byte 0
147
    je      .dump     ;if TTL==0 then dump it
1 ha 148
 
261 hidnplayr 149
    mov     ax, word[ebx + IP_PACKET.FlagsAndFragmentOffset]
150
    and     ax, 0xFFBF   ;get flags
151
    cmp     ax, 0        ;if some flags was set then we dump this packet
152
    jnz     .dump        ;the flags should be used for fragmented packets
1 ha 153
 
154
    ; Check the protocol, and call the appropriate handler
155
    ; Each handler will re-use or free the queue buffer as appropriate
156
 
261 hidnplayr 157
    mov     al, [ebx + IP_PACKET.Protocol]
158
 
1 ha 159
    cmp     al , PROTOCOL_TCP
261 hidnplayr 160
    jne     .not_tcp
373 mikedld 161
    DEBUGF  1,"K : ip_rx - TCP packet\n"
261 hidnplayr 162
    mov     eax, dword[buffer_number]
1 ha 163
    call    tcp_rx
261 hidnplayr 164
    jmp     .exit
1 ha 165
 
261 hidnplayr 166
  .not_tcp:
167
    cmp     al, PROTOCOL_UDP
168
    jne     .not_udp
373 mikedld 169
    DEBUGF  1,"K : ip_rx - UDP packet\n"
261 hidnplayr 170
    mov     eax, dword[buffer_number]
1 ha 171
    call    udp_rx
261 hidnplayr 172
    jmp     .exit
1 ha 173
 
261 hidnplayr 174
  .not_udp:
175
    cmp     al , PROTOCOL_ICMP
176
    jne     .dump              ;protocol ain't supported
177
 
373 mikedld 178
    DEBUGF  1,"K : ip_rx - ICMP packet\n"
261 hidnplayr 179
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
180
    mov     eax, dword[buffer_number]
181
    stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
182
    jmp     .exit
183
 
184
 
185
.dump:
1 ha 186
    ; No protocol handler available, so
187
    ; silently dump the packet, freeing up the queue buffer
188
 
261 hidnplayr 189
    inc     dword [dumped_rx_count]
1 ha 190
 
261 hidnplayr 191
    mov     eax, dword[buffer_number]
1 ha 192
    call    freeBuff
193
 
261 hidnplayr 194
  .exit:
1 ha 195
    ret
261 hidnplayr 196
endp
1 ha 197