Subversion Repositories Kolibri OS

Rev

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