Subversion Repositories Kolibri OS

Rev

Rev 5522 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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