Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | 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
 
15
 
16
;*******************************************************************
17
;   Interface
18
;
19
;       ip_rx       processes all packets received by the network layer
20
;                   It calls the appropriate protocol handler
21
;
22
;
23
;
24
;*******************************************************************
25
 
26
 
27
;***************************************************************************
28
;   Function
29
;      ip_rx
30
;
31
;   Description
32
;       Handles received IP packets
33
;       This is a kernel function, called by stack_handler
34
;
35
;***************************************************************************
36
ip_rx:
37
    ; Look for a buffer to tx
38
    mov     eax, IPIN_QUEUE
39
    call    dequeue
40
    cmp     ax, NO_BUFFER
41
    je      ipr_exit         ; Exit if no buffer available
42
 
43
    push    eax
44
 
45
    ; convert buffer pointer eax to the absolute address
46
    mov     ecx, IPBUFFSIZE
47
    mul     ecx
48
    add     eax, IPbuffs
49
 
50
    mov     edx, eax  ; Save the address in  edx for use by future processes
51
 
52
    ; Validate the IP checksum
53
    mov     ebx, edx
54
    mov     ah, [ebx + 10]
55
    mov     al, [ebx + 11]      ; Get the checksum in intel format
56
    mov     [ebx + 10], word 0  ; clear checksum field - need to when
57
                                    ; recalculating checksum
58
 
59
    ;  this needs two data pointers and two size #.
60
    ;  2nd pointer can be of length 0
61
    mov     ebx, edx
62
    mov     [checkAdd1], ebx
63
    mov     [checkSize1], word 20
64
    mov     [checkAdd2], dword 0
65
    mov     [checkSize2], word 0
66
 
67
    call    checksum           ; Recalculate IP checksum
68
    cmp     ax, [checkResult]
69
    jnz     ipr_dump
70
 
71
    ; If the IP address is 255.255.255.255, accept it
72
    ; - it is a broadcast packet, which we need for dhcp
73
    mov     eax, [edx + 16]
74
    cmp     eax, 0xffffffff
75
    je      ipr_p0
76
 
77
    ; Validate the IP address, if it isn't broadcast
78
    cmp     eax, [stack_ip]
79
    jnz     ipr_dump
80
 
81
ipr_p0:
82
    mov     al, [edx]
83
    and     al, 0x0f
84
    cmp     al, 0x05
85
    jnz     ipr_dump
86
 
87
    cmp     [edx+8], byte 0
88
    jz      ipr_dump
89
 
90
    mov     ax, [edx + 6]
91
    and     ax, 0xFFBF
92
    cmp     ax, 0
93
    jnz     ipr_dump
94
 
95
    ; Check the protocol, and call the appropriate handler
96
    ; Each handler will re-use or free the queue buffer as appropriate
97
    mov     al, [edx + 9]
98
    cmp     al , PROTOCOL_ICMP
99
    jnz     ipr_p1
100
    pop     eax
101
    call    icmp_rx
102
    jmp     ipr_exit
103
 
104
ipr_p1:
105
    cmp     al , PROTOCOL_TCP
106
    jnz     ipr_p2
107
    pop     eax
108
    call    tcp_rx
109
    jmp     ipr_exit
110
 
111
ipr_p2:
112
    cmp     al , PROTOCOL_UDP
113
    jnz     ipr_dump
114
    pop     eax
115
    call    udp_rx
116
    jmp     ipr_exit
117
 
118
ipr_dump:
119
    ; No protocol handler available, so
120
    ; silently dump the packet, freeing up the queue buffer
121
 
122
;    inc     dword [dumped_rx_count]
123
 
124
    pop     eax
125
    call    freeBuff
126
 
127
ipr_exit:
128
    ret
129
 
130
 
131
 
132
;***************************************************************************
133
;   Function
134
;      icmp_rx
135
;
136
;   Description
137
;       ICMP protocol handler
138
;       This is a kernel function, called by ip_rx
139
;       edx contains the address of the buffer in use.
140
;       This buffer must be reused or marked as empty afterwards
141
;
142
;***************************************************************************
143
icmp_rx:
144
    cmp     [edx + 20], byte 8 ; Is this an echo request? discard if not
145
    jz      icmp_echo
146
 
147
    call    freeBuff
148
    jmp     icmp_exit
149
 
150
icmp_echo:
151
    push    eax
152
    mov     [edx + 10], word 0  ; I think this was already done by IP rx
153
 
154
    ; swap the source and destination addresses
155
    mov     ecx, [edx + 16]
156
    mov     eax, [edx + 12]
157
    mov     [edx + 16], eax
158
    mov     [edx + 12], ecx
159
 
160
    ; recaluculate the IP header checksum
161
 
162
    mov     ebx, edx
163
    mov     [checkAdd1], ebx
164
    mov     [checkSize1], word 20
165
    mov     [checkAdd2], dword 0
166
    mov     [checkSize2], word 0
167
 
168
    call    checksum
169
    mov     ax, [checkResult]
170
    mov     [edx + 10], ah
171
    mov     [edx + 11], al      ; ?? correct byte order?
172
 
173
    mov     [edx + 20], byte 0  ; change the request to a response
174
    mov     [edx + 22], word 0  ; clear ICMP checksum prior to re-calc
175
 
176
    ; Calculate the length of the ICMP data ( IP payload)
177
    mov     ah, [edx + 2]
178
    mov     al, [edx + 3]
179
    sub     ax, 20
180
 
181
    mov     [checkSize1], ax
182
    mov     ebx, edx
183
    add     ebx, 20
184
 
185
    mov     [checkAdd1], ebx
186
    mov     [checkAdd2], dword 0
187
    mov     [checkSize2], word 0
188
 
189
    call    checksum
190
 
191
    mov     ax, [checkResult]
192
    mov     [edx + 22], ah
193
    mov     [edx + 23], al
194
 
195
    ; Queue packet for transmission
196
 
197
    pop     ebx
198
    mov     eax, NET1OUT_QUEUE
199
    call    queue
200
 
201
icmp_exit:
202
    ret