Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1 ha 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2465 Serge 3
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;                                                                 ;;
1 ha 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
 
593 mikedld 19
$Revision: 2465 $
20
 
21
 
261 hidnplayr 22
; IP underlying protocols numbers
2434 Serge 23
PROTOCOL_ICMP     equ      1
24
PROTOCOL_TCP      equ      6
25
PROTOCOL_UDP      equ      17
1 ha 26
 
261 hidnplayr 27
struc IP_PACKET
2434 Serge 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
261 hidnplayr 39
}
40
 
41
virtual at 0
42
  IP_PACKET IP_PACKET
43
end virtual
44
 
45
 
1 ha 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
 
261 hidnplayr 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
;
2434 Serge 82
macro   GET_IHL reg, header_addr
261 hidnplayr 83
{
2434 Serge 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
261 hidnplayr 91
}
92
 
93
 
907 mikedld 94
include "tcp.inc"
95
include "udp.inc"
96
include "icmp.inc"
97
 
1 ha 98
;***************************************************************************
99
;   Function
100
;      ip_rx
101
;
102
;   Description
103
;       This is a kernel function, called by stack_handler
261 hidnplayr 104
;       Processes all IP-packets received by the network layer
105
;       It calls the appropriate protocol handler
1 ha 106
;
107
;***************************************************************************
261 hidnplayr 108
proc ip_rx stdcall
109
local buffer_number dd ?
110
 
1 ha 111
    ; Look for a buffer to tx
2434 Serge 112
        mov     eax, IPIN_QUEUE
113
        call    dequeue
114
        cmp     ax, NO_BUFFER
115
        je      .exit     ; Exit if no buffer available
1 ha 116
 
2434 Serge 117
        mov     [buffer_number], eax;save buffer number
1 ha 118
 
119
    ; convert buffer pointer eax to the absolute address
2434 Serge 120
        imul    eax, IPBUFFSIZE
121
        add     eax, IPbuffs
1 ha 122
 
2434 Serge 123
        mov     ebx, eax; ebx=pointer to IP_PACKET
1 ha 124
 
907 mikedld 125
;        DEBUGF  1, "K : ip_rx - proto: %u\n", [ebx + IP_PACKET.Protocol]:1
126
 
1 ha 127
    ; Validate the IP checksum
2434 Serge 128
        mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
129
        xchg    dh, dl     ; Get the checksum in intel format
261 hidnplayr 130
 
2434 Serge 131
        mov     [ebx + IP_PACKET.HeaderChecksum], 0; clear checksum field - need to when
132
                                ; recalculating checksum
1 ha 133
    ;  this needs two data pointers and two size #.
134
    ;  2nd pointer can be of length 0
135
 
261 hidnplayr 136
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
2434 Serge 137
        stdcall checksum_jb, ebx, ecx;buf_ptr, buf_size
138
        cmp     dx, ax
1 ha 139
 
907 mikedld 140
;        DEBUGF  1, "K : ip_rx - checksums: %x - %x\n", dx, ax
141
 
2434 Serge 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!!!
261 hidnplayr 144
 
907 mikedld 145
;        DEBUGF  1, "K : ip_rx - dest: %x - %x\n", [ebx + IP_PACKET.DestinationAddress], [stack_ip]
146
 
261 hidnplayr 147
    ; Validate the IP address, if it isn't broadcast
2434 Serge 148
        mov     eax, [stack_ip]
149
        cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
150
        je      @f
261 hidnplayr 151
 
1 ha 152
    ; If the IP address is 255.255.255.255, accept it
153
    ; - it is a broadcast packet, which we need for dhcp
154
 
2434 Serge 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
907 mikedld 167
 
261 hidnplayr 168
  @@:
2434 Serge 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)
907 mikedld 172
;        DEBUGF  1, "K : ip_rx - ihl: %x - 05\n", al
2434 Serge 173
        jnz     .dump.3 ;then dump it
1 ha 174
 
907 mikedld 175
;        DEBUGF  1, "K : ip_rx - ttl: %x - 00\n", [ebx + IP_PACKET.TimeToLive]:2
1 ha 176
 
2434 Serge 177
        cmp     [ebx + IP_PACKET.TimeToLive], 0
178
        je      .dump.4 ;if TTL==0 then dump it
1 ha 179
 
2434 Serge 180
        mov     ax, [ebx + IP_PACKET.FlagsAndFragmentOffset]
181
        and     ax, 0xFFBF;get flags
907 mikedld 182
;        DEBUGF  1, "K : ip_rx - flags: %x - 0000\n", ax
2434 Serge 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
907 mikedld 185
 
1 ha 186
    ; Check the protocol, and call the appropriate handler
187
    ; Each handler will re-use or free the queue buffer as appropriate
188
 
2434 Serge 189
        mov     al, [ebx + IP_PACKET.Protocol]
261 hidnplayr 190
 
2434 Serge 191
        cmp     al , PROTOCOL_TCP
192
        jne     .not_tcp
907 mikedld 193
;    DEBUGF  1,"K : ip_rx - TCP packet\n"
2434 Serge 194
        mov     eax, dword[buffer_number]
195
        call    tcp_rx
196
        jmp     .exit
1 ha 197
 
261 hidnplayr 198
  .not_tcp:
2434 Serge 199
        cmp     al, PROTOCOL_UDP
200
        jne     .not_udp
907 mikedld 201
;    DEBUGF  1,"K : ip_rx - UDP packet\n"
2434 Serge 202
        mov     eax, dword[buffer_number]
203
        call    udp_rx
204
        jmp     .exit
1 ha 205
 
261 hidnplayr 206
  .not_udp:
2434 Serge 207
        cmp     al, PROTOCOL_ICMP
208
        jne     .dump.6          ;protocol ain't supported
261 hidnplayr 209
 
907 mikedld 210
;    DEBUGF  1,"K : ip_rx - ICMP packet\n"
261 hidnplayr 211
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
2434 Serge 212
        mov     eax, dword[buffer_number]
213
        stdcall icmp_rx, eax, ebx, ecx;buffer_number,IPPacketBase,IPHeaderLength
214
        jmp     .exit
261 hidnplayr 215
 
216
 
907 mikedld 217
  .dump.1:
2434 Serge 218
        DEBUGF  1, "K : ip_rx - dumped (checksum: 0x%x-0x%x)\n", dx, ax
219
        jmp     .dump.x
1 ha 220
 
907 mikedld 221
  .dump.2:
2434 Serge 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
1 ha 224
 
907 mikedld 225
  .dump.3:
2434 Serge 226
        DEBUGF  1, "K : ip_rx - dumped (ihl: %u)\n", al
227
        jmp     .dump.x
907 mikedld 228
 
229
  .dump.4:
2434 Serge 230
        DEBUGF  1, "K : ip_rx - dumped (ttl: %u)\n", [ebx + IP_PACKET.TimeToLive]
231
        jmp     .dump.x
907 mikedld 232
 
233
  .dump.5:
2434 Serge 234
        DEBUGF  1, "K : ip_rx - dumped (flags: 0x%x)\n", ax
235
        jmp     .dump.x
907 mikedld 236
 
237
  .dump.6:
2434 Serge 238
        DEBUGF  1, "K : ip_rx - dumped (proto: %u)\n", [ebx + IP_PACKET.Protocol]:1
907 mikedld 239
 
240
  .dump.x:
2434 Serge 241
        inc     dword[dumped_rx_count]
242
        mov     eax, [buffer_number]
243
        call    freeBuff
1 ha 244
 
261 hidnplayr 245
  .exit:
2434 Serge 246
        ret
261 hidnplayr 247
endp
1 ha 248