Subversion Repositories Kolibri OS

Rev

Rev 129 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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