Subversion Repositories Kolibri OS

Rev

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

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