Subversion Repositories Kolibri OS

Rev

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

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