Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3555 Serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2012-2013. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  IPv6.INC                                                       ;;
7
;;                                                                 ;;
8
;;  Part of the tcp/ip network stack for KolibriOS                 ;;
9
;;                                                                 ;;
10
;;    Written by hidnplayr@kolibrios.org                           ;;
11
;;                                                                 ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;             Version 2, June 1991                                ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
 
17
$Revision: 3251 $
18
 
19
 
20
struct  IPv6_header
21
 
22
        VersionTrafficFlow      dd ?    ; Version[0-3], Traffic class[4-11], Flow Label [12-31]
23
        PayloadLength           dw ?    ; 16 bits, unsigned length of payload (extension headers are part of this)
24
        NextHeader              db ?    ; Values are same as in IPv4 'Protocol' field
25
        HopLimit                db ?    ; Decremented by every node, packet is discarded when it reaches 0
26
        SourceAddress           rd 4    ; 128-bit addresses
27
        DestinationAddress      rd 4    ;
28
        Payload                 rb 0
29
 
30
ends
31
 
32
 
3725 Serge 33
uglobal
3555 Serge 34
align 4
35
 
36
        IPv6:
3626 Serge 37
        .addresses      rd 4*NET_DEVICES_MAX
38
        .subnet         rd 4*NET_DEVICES_MAX
39
        .dns            rd 4*NET_DEVICES_MAX
40
        .gateway        rd 4*NET_DEVICES_MAX
3555 Serge 41
 
3626 Serge 42
        .packets_tx     rd NET_DEVICES_MAX
43
        .packets_rx     rd NET_DEVICES_MAX
3555 Serge 44
 
45
endg
46
 
47
 
48
;-----------------------------------------------------------------
49
;
50
; IPv6_init
51
;
52
;  This function resets all IP variables
53
;
54
;-----------------------------------------------------------------
55
macro   IPv6_init {
56
 
57
        xor     eax, eax
58
        mov     edi, IPv6
59
        mov     ecx, (4*4*4+2*4)MAX_IP
3725 Serge 60
        rep stosd
3555 Serge 61
 
62
}
63
 
64
 
65
 
66
;-----------------------------------------------------------------
67
;
68
; IPv6_input:
69
;
70
;  Will check if IPv6 Packet isnt damaged
71
;  and call appropriate handler. (TCP/UDP/ICMP/..)
72
;
73
;  It will also re-construct fragmented packets
74
;
75
;  IN:  Pointer to buffer in [esp]
76
;       size of buffer in [esp+4]
77
;       pointer to device struct in ebx
78
;       pointer to IPv6 header in edx
79
;       size of IPv6 packet in ecx
80
;  OUT: /
81
;
82
;-----------------------------------------------------------------
83
align 4
84
IPv6_input:
85
 
3589 Serge 86
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input from: %x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x\n",\
3555 Serge 87
        [edx + IPv6_header.SourceAddress + 0]:2,[edx + IPv6_header.SourceAddress + 1]:2,\
88
        [edx + IPv6_header.SourceAddress + 2]:2,[edx + IPv6_header.SourceAddress + 3]:2,\
89
        [edx + IPv6_header.SourceAddress + 4]:2,[edx + IPv6_header.SourceAddress + 5]:2,\
90
        [edx + IPv6_header.SourceAddress + 6]:2,[edx + IPv6_header.SourceAddress + 7]:2,\
91
        [edx + IPv6_header.SourceAddress + 8]:2,[edx + IPv6_header.SourceAddress + 9]:2,\
92
        [edx + IPv6_header.SourceAddress + 10]:2,[edx + IPv6_header.SourceAddress + 11]:2,\
93
        [edx + IPv6_header.SourceAddress + 12]:2,[edx + IPv6_header.SourceAddress + 13]:2,\
94
        [edx + IPv6_header.SourceAddress + 14]:2,[edx + IPv6_header.SourceAddress + 15]:2
95
 
3589 Serge 96
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input to: %x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x\n",\
3555 Serge 97
        [edx + IPv6_header.DestinationAddress + 0]:2,[edx + IPv6_header.DestinationAddress + 1]:2,\
98
        [edx + IPv6_header.DestinationAddress + 2]:2,[edx + IPv6_header.DestinationAddress + 3]:2,\
99
        [edx + IPv6_header.DestinationAddress + 4]:2,[edx + IPv6_header.DestinationAddress + 5]:2,\
100
        [edx + IPv6_header.DestinationAddress + 6]:2,[edx + IPv6_header.DestinationAddress + 7]:2,\
101
        [edx + IPv6_header.DestinationAddress + 8]:2,[edx + IPv6_header.DestinationAddress + 9]:2,\
102
        [edx + IPv6_header.DestinationAddress + 10]:2,[edx + IPv6_header.DestinationAddress + 11]:2,\
103
        [edx + IPv6_header.DestinationAddress + 12]:2,[edx + IPv6_header.DestinationAddress + 13]:2,\
104
        [edx + IPv6_header.DestinationAddress + 14]:2,[edx + IPv6_header.DestinationAddress + 15]:2
105
 
106
        sub     ecx, sizeof.IPv6_header
107
        jb      .dump
108
 
109
        cmp     cx, [edx + IPv6_header.PayloadLength]
110
        jb      .dump
111
 
112
;-------------------------------------------------------------------
113
; No, it's just a regular IP packet, pass it to the higher protocols
114
 
115
  .handle_it:
116
        movzx   ecx, [edx + IPv6_header.PayloadLength]
117
        lea     edi, [edx + IPv6_header.SourceAddress]          ; make edi ptr to source and dest IPv6 address
118
        lea     esi, [edx + IPv6_header.Payload]                ; make esi ptr to data
119
        mov     al, [edx + IPv6_header.NextHeader]
120
 
121
  .scan:
122
        cmp     al, 59  ; no next
123
        je      .dump
124
 
125
        cmp     al, 0
126
        je      .hop_by_hop
127
 
128
        cmp     al, 43
129
        je      .routing
130
 
131
        cmp     al, 44
132
        je      .fragment
133
 
134
        cmp     al, 60
135
        je      .dest_opts
136
 
137
;        cmp     al, IP_PROTO_TCP
138
;        je      TCP_input
139
 
140
;        cmp     al, IP_PROTO_UDP
141
;        je      UDP_input
142
 
143
;        cmp     al, 58
144
;        je      ICMP6_input
145
 
3589 Serge 146
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - unknown protocol: %u\n", al
3555 Serge 147
 
148
  .dump:
3589 Serge 149
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - dumping\n"
3555 Serge 150
        call    kernel_free
151
        add     esp, 4
152
 
153
        ret
154
 
155
  .dump_options:
156
        add     esp, 2+4+4
157
        jmp     .dump
158
 
159
  .nextheader:
160
        pop     esi
161
        pop     ecx
162
        pop     ax
163
        jmp     .scan
164
 
165
;-------------------------
166
; Hop-by-Hop
167
 
168
  .hop_by_hop:
3589 Serge 169
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - hop by hop\n"
3555 Serge 170
        pushw   [esi]                   ; 8 bit identifier for option type
171
        movzx   eax, byte[esi + 1]      ; Hdr Ext Len
172
        inc     eax                     ; first 8 octets not counted
173
        shl     eax, 3                  ; * 8
174
        sub     ecx, eax
175
        push    ecx
176
        add     eax, esi
177
        push    eax
178
        inc     esi
179
        inc     esi
180
 
181
        mov     al, [esi]
182
 
183
        cmp     al, 0
184
        je      .pad_1
185
 
186
        cmp     al, 1
187
        je      .pad_n
188
 
189
        ; TODO: check with other known options
190
 
191
; unknown option.. discard packet or not?
192
; check highest two bits
193
        test    al, 0xc0        ; discard packet
194
        jnz     .dump_options
195
 
196
  .pad_n:
197
        movzx   eax, byte[esi + 1]
3589 Serge 198
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - pad %u\n", eax
3555 Serge 199
        inc     esi
200
        inc     esi
201
        add     esi, eax
202
        sub     ecx, eax
203
        jmp     .hop_by_hop
204
 
205
  .pad_1:
3589 Serge 206
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - pad 1\n"
3555 Serge 207
        inc     esi
208
        dec     ecx
209
        jmp     .hop_by_hop
210
 
211
 
212
 
213
  .dest_opts:
3589 Serge 214
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - dest opts\n"
3555 Serge 215
        jmp     .nextheader
216
 
217
  .routing:
3589 Serge 218
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - routing\n"
3555 Serge 219
        pushw   [esi]                   ; 8 bit identifier for option type
220
        movzx   eax, byte[esi + 1]      ; Hdr Ext Len
221
        inc     eax                     ; first 8 octets not counted
222
        shl     eax, 3                  ; * 8
223
        sub     ecx, eax
224
        push    ecx
225
        add     eax, esi
226
        push    eax
227
        inc     esi
228
        inc     esi
229
 
230
        cmp     al, 0
231
        je      .pad_1
232
 
233
        cmp     al, 1
234
        je      .pad_n
235
 
236
        mov     al, [esi]       ; routing type
237
 
238
        jmp     .nextheader
239
 
240
  .fragment:
3589 Serge 241
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - fragment\n"
3555 Serge 242
 
243
        jmp     .nextheader
244
 
245
 
246
 
247
 
248
 
249
 
250
;---------------------------------------------------------------------------
251
;
252
; IPv6_API
253
;
254
; This function is called by system function 75
255
;
256
; IN:  subfunction number in bl
257
;      device number in bh
258
;      ecx, edx, .. depends on subfunction
259
;
260
; OUT:
261
;
262
;---------------------------------------------------------------------------
263
align 4
264
IPv6_api:
265
 
266
        movzx   eax, bh
267
        shl     eax, 2
268
 
269
        and     ebx, 0x000000ff
270
        cmp     ebx, .number
271
        ja      .error
272
        jmp     dword [.table + 4*ebx]
273
 
274
  .table:
275
        dd      .packets_tx     ; 0
276
        dd      .packets_rx     ; 1
277
;        dd      .read_ip        ; 2
278
;        dd      .write_ip       ; 3
279
;        dd      .read_dns       ; 4
280
;        dd      .write_dns      ; 5
281
;        dd      .read_subnet    ; 6
282
;        dd      .write_subnet   ; 7
283
;        dd      .read_gateway   ; 8
284
;        dd      .write_gateway  ; 9
285
  .number = ($ - .table) / 4 - 1
286
 
287
  .error:
288
        mov     eax, -1
289
        ret
290
 
291
  .packets_tx:
292
        mov     eax, [IPv6.packets_tx + eax]
293
        ret
294
 
295
  .packets_rx:
296
        mov     eax, [IPv6.packets_rx + eax]
297
        ret
298