Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5363 yogev_ezra 3
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved.    ;;
3545 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  IPv4.INC                                                       ;;
7
;;                                                                 ;;
8
;;  Part of the TCP/IP network stack for KolibriOS                 ;;
9
;;                                                                 ;;
10
;;  Based on the work of [Johnny_B] and [smb]                      ;;
11
;;                                                                 ;;
12
;;    Written by hidnplayr@kolibrios.org                           ;;
13
;;                                                                 ;;
14
;;          GNU GENERAL PUBLIC LICENSE                             ;;
15
;;             Version 2, June 1991                                ;;
16
;;                                                                 ;;
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
 
4850 mario79 19
$Revision: 5522 $
3545 hidnplayr 20
 
4053 hidnplayr 21
IPv4_MAX_FRAGMENTS              = 64
4387 hidnplayr 22
IPv4_MAX_ROUTES                 = 64
3545 hidnplayr 23
 
4387 hidnplayr 24
IPv4_ROUTE_FLAG_UP              = 1 shl 0
25
IPv4_ROUTE_FLAG_GATEWAY         = 1 shl 1
26
IPv4_ROUTE_FLAG_HOST            = 1 shl 2
27
IPv4_ROUTE_FLAG_D               = 1 shl 3       ; Route was created by a redirect
28
IPv4_ROUTE_FLAG_M               = 1 shl 4       ; Route was modified by a redirect
29
 
3545 hidnplayr 30
struct  IPv4_header
31
 
32
        VersionAndIHL           db ?    ; Version[0-3 bits] and IHL(header length)[4-7 bits]
33
        TypeOfService           db ?    ; precedence [7-5] minimize delay [4], maximize throughput [3], maximize riliability [2] minimize momentary cost [1] and zero [0]
34
        TotalLength             dw ?
35
        Identification          dw ?
36
        FlagsAndFragmentOffset  dw ?    ; Flags[0-2] and FragmentOffset[3-15]
37
        TimeToLive              db ?    ;
38
        Protocol                db ?
39
        HeaderChecksum          dw ?
40
        SourceAddress           dd ?
41
        DestinationAddress      dd ?
42
 
43
ends
44
 
4052 hidnplayr 45
struct  IPv4_FRAGMENT_slot
3545 hidnplayr 46
 
47
        ttl                     dw ?    ; Time to live for this entry, 0 for empty slot's
48
        id                      dw ?    ; Identification field from IP header
49
        SrcIP                   dd ?    ; .. from IP header
50
        DstIP                   dd ?    ; .. from IP header
51
        ptr                     dd ?    ; Pointer to first packet
52
 
53
ends
54
 
4052 hidnplayr 55
struct  IPv4_FRAGMENT_entry             ; This structure will replace the ethernet header in fragmented ip packets
3545 hidnplayr 56
 
57
        PrevPtr                 dd ?    ; Pointer to previous fragment entry  (-1 for first packet)
58
        NextPtr                 dd ?    ; Pointer to next fragment entry (-1 for last packet)
59
        Owner                   dd ?    ; Pointer to structure of driver
60
                                rb 2    ; to match ethernet header size         ;;; FIXME
61
                                        ; Ip header begins here (we will need the IP header to re-construct the complete packet)
62
ends
63
 
4387 hidnplayr 64
struct  IPv4_ROUTE
3545 hidnplayr 65
 
4387 hidnplayr 66
        Destination             dd ?
67
        Gateway                 dd ?
68
        Flags                   dd ?
69
        Use                     dd ?
70
        Interface               dd ?
71
 
72
ends
73
 
74
 
3698 hidnplayr 75
uglobal
3545 hidnplayr 76
align 4
77
 
3600 hidnplayr 78
        IP_LIST                 rd NET_DEVICES_MAX
79
        SUBNET_LIST             rd NET_DEVICES_MAX
80
        DNS_LIST                rd NET_DEVICES_MAX
81
        GATEWAY_LIST            rd NET_DEVICES_MAX
82
        BROADCAST_LIST          rd NET_DEVICES_MAX
3545 hidnplayr 83
 
4052 hidnplayr 84
        IPv4_packets_tx         rd NET_DEVICES_MAX
85
        IPv4_packets_rx         rd NET_DEVICES_MAX
86
        IPv4_packets_dumped     rd NET_DEVICES_MAX
3545 hidnplayr 87
 
4052 hidnplayr 88
        IPv4_FRAGMENT_LIST      rb IPv4_MAX_FRAGMENTS * sizeof.IPv4_FRAGMENT_slot
3698 hidnplayr 89
 
4387 hidnplayr 90
        IPv4_ROUTES             rd IPv4_MAX_ROUTES * sizeof.IPv4_ROUTE
91
 
3545 hidnplayr 92
endg
93
 
94
 
95
;-----------------------------------------------------------------
96
;
97
; IPv4_init
98
;
99
;  This function resets all IP variables
100
;
101
;-----------------------------------------------------------------
102
macro   IPv4_init {
103
 
104
        xor     eax, eax
105
        mov     edi, IP_LIST
4052 hidnplayr 106
        mov     ecx, 7*NET_DEVICES_MAX + (sizeof.IPv4_FRAGMENT_slot*IPv4_MAX_FRAGMENTS)/4
3711 clevermous 107
        rep stosd
3545 hidnplayr 108
 
109
}
110
 
111
 
112
;-----------------------------------------------------------------
113
;
114
; Decrease TimeToLive of all fragment slots
115
;
116
;-----------------------------------------------------------------
117
macro IPv4_decrease_fragment_ttls {
118
 
119
local   .loop, .next
120
 
4052 hidnplayr 121
        mov     esi, IPv4_FRAGMENT_LIST
122
        mov     ecx, IPv4_MAX_FRAGMENTS
3545 hidnplayr 123
  .loop:
4052 hidnplayr 124
        cmp     [esi + IPv4_FRAGMENT_slot.ttl], 0
3545 hidnplayr 125
        je      .next
4052 hidnplayr 126
        dec     [esi + IPv4_FRAGMENT_slot.ttl]
3545 hidnplayr 127
        jz      .died
128
  .next:
4052 hidnplayr 129
        add     esi, sizeof.IPv4_FRAGMENT_slot
3545 hidnplayr 130
        dec     ecx
131
        jnz     .loop
132
        jmp     .done
133
 
134
  .died:
3556 hidnplayr 135
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4 Fragment slot timed-out!\n"
3545 hidnplayr 136
;;; TODO: clear all entry's of timed-out slot
137
        jmp     .next
138
 
139
  .done:
140
}
141
 
142
 
143
 
144
macro IPv4_checksum ptr {
145
 
146
; This is the fast procedure to create or check an IP header without options
147
; To create a new checksum, the checksum field must be set to 0 before computation
148
; To check an existing checksum, leave the checksum as is, and it will be 0 after this procedure, if it was correct
149
 
150
        push    ebx
151
        xor     ebx, ebx
152
        add     bl, [ptr+1]
153
        adc     bh, [ptr+0]
154
 
155
        adc     bl, [ptr+3]
156
        adc     bh, [ptr+2]
157
 
158
        adc     bl, [ptr+5]
159
        adc     bh, [ptr+4]
160
 
161
        adc     bl, [ptr+7]
162
        adc     bh, [ptr+6]
163
 
164
        adc     bl, [ptr+9]
165
        adc     bh, [ptr+8]
166
 
167
; we skip 11th and 12th byte, they are the checksum bytes and should be 0 for re-calculation
168
 
169
        adc     bl, [ptr+13]
170
        adc     bh, [ptr+12]
171
 
172
        adc     bl, [ptr+15]
173
        adc     bh, [ptr+14]
174
 
175
        adc     bl, [ptr+17]
176
        adc     bh, [ptr+16]
177
 
178
        adc     bl, [ptr+19]
179
        adc     bh, [ptr+18]
180
 
181
        adc     ebx, 0
182
 
183
        push    ecx
184
        mov     ecx, ebx
185
        shr     ecx, 16
186
        and     ebx, 0xffff
187
        add     ebx, ecx
188
 
189
        mov     ecx, ebx
190
        shr     ecx, 16
191
        add     ebx, ecx
192
 
193
        not     bx
194
        jnz     .not_zero
195
        dec     bx
196
  .not_zero:
197
        xchg    bl, bh
198
        pop     ecx
199
 
200
        neg     word [ptr+10]           ; zero will stay zero so we just get the checksum
201
        add     word [ptr+10], bx       ;  , else we will get (new checksum - old checksum) in the end, wich should be 0 :)
202
        pop     ebx
203
 
204
}
205
 
206
 
207
 
208
;-----------------------------------------------------------------
209
;
210
; IPv4_input:
211
;
212
;  Will check if IPv4 Packet isnt damaged
213
;  and call appropriate handler. (TCP/UDP/ICMP/..)
214
;
215
;  It will also re-construct fragmented packets
216
;
217
;  IN:  Pointer to buffer in [esp]
218
;       size of buffer in [esp+4]
219
;       pointer to device struct in ebx
220
;       pointer to IPv4 header in edx
221
;       size of IPv4 packet in ecx
222
;  OUT: /
223
;
224
;-----------------------------------------------------------------
225
align 4
226
IPv4_input:                                                     ; TODO: add IPv4 raw sockets support
227
 
5522 hidnplayr 228
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: packet from %u.%u.%u.%u ",\
3545 hidnplayr 229
        [edx + IPv4_header.SourceAddress + 0]:1,[edx + IPv4_header.SourceAddress + 1]:1,\
230
        [edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1
5522 hidnplayr 231
        DEBUGF  DEBUG_NETWORK_VERBOSE, "to %u.%u.%u.%u\n",\
3545 hidnplayr 232
        [edx + IPv4_header.DestinationAddress + 0]:1,[edx + IPv4_header.DestinationAddress + 1]:1,\
233
        [edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1
234
 
235
;-------------------------------
236
; re-calculate the checksum
237
 
238
        IPv4_checksum edx
239
        jnz     .dump                                           ; if checksum isn't valid then dump packet
240
 
3556 hidnplayr 241
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Checksum ok\n"
3545 hidnplayr 242
 
243
;-----------------------------------
244
; Check if destination IP is correct
245
 
3643 hidnplayr 246
        call    NET_ptr_to_num4
3545 hidnplayr 247
 
248
        ; check if it matches local ip (Using RFC1122 strong end system model)
249
 
250
        mov     eax, [edx + IPv4_header.DestinationAddress]
251
        cmp     eax, [IP_LIST + edi]
252
        je      .ip_ok
253
 
254
        ; check for broadcast (IP or (not SUBNET))
255
 
256
        cmp     eax, [BROADCAST_LIST + edi]
257
        je      .ip_ok
258
 
259
        ; or a special broadcast (255.255.255.255)
260
 
261
        cmp     eax, 0xffffffff
262
        je      .ip_ok
263
 
264
        ; maybe it's a multicast (224.0.0.0/4)
265
 
266
        and     eax, 0x0fffffff
267
        cmp     eax, 224
268
        je      .ip_ok
269
 
4976 hidnplayr 270
        ; maybe we just dont have an IP yet and should accept everything on the IP level
3545 hidnplayr 271
 
4976 hidnplayr 272
        cmp     [IP_LIST + edi], 0
3545 hidnplayr 273
        je      .ip_ok
274
 
275
        ; or it's just not meant for us.. :(
276
 
3556 hidnplayr 277
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Destination address does not match!\n"
3545 hidnplayr 278
        jmp     .dump
279
 
280
;------------------------
281
; Now we can update stats
282
 
283
  .ip_ok:
4052 hidnplayr 284
        inc     [IPv4_packets_rx + edi]
3545 hidnplayr 285
 
286
;----------------------------------
287
; Check if the packet is fragmented
288
 
289
        test    [edx + IPv4_header.FlagsAndFragmentOffset], 1 shl 5     ; Is 'more fragments' flag set ?
290
        jnz     .has_fragments                                          ; If so, we definately have a fragmented packet
291
 
292
        test    [edx + IPv4_header.FlagsAndFragmentOffset], 0xff1f      ; If flag is not set, but there is a fragment offset, the packet is last in series of fragmented packets
293
        jnz     .is_last_fragment
294
 
295
;-------------------------------------------------------------------
296
; No, it's just a regular IP packet, pass it to the higher protocols
297
 
298
  .handle_it:                                                   ; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
299
 
300
        movzx   esi, [edx + IPv4_header.VersionAndIHL]          ; Calculate Header length by using IHL field
301
        and     esi, 0x0000000f                                 ;
302
        shl     esi, 2                                          ;
303
 
304
        movzx   ecx, [edx + IPv4_header.TotalLength]            ; Calculate length of encapsulated Packet
305
        xchg    cl, ch                                          ;
306
        sub     ecx, esi                                        ;
307
 
308
        lea     edi, [edx + IPv4_header.SourceAddress]          ; make edi ptr to source and dest IPv4 address
309
        mov     al, [edx + IPv4_header.Protocol]
310
        add     esi, edx                                        ; make esi ptr to data
311
 
312
        cmp     al, IP_PROTO_TCP
313
        je      TCP_input
314
 
315
        cmp     al, IP_PROTO_UDP
316
        je      UDP_input
317
 
318
        cmp     al, IP_PROTO_ICMP
319
        je      ICMP_input
320
 
3556 hidnplayr 321
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: unknown protocol %u\n", al
3545 hidnplayr 322
 
323
  .dump:
3556 hidnplayr 324
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: dumping\n"
4052 hidnplayr 325
        inc     [IPv4_packets_dumped]                           ; FIXME: use correct interface
5522 hidnplayr 326
        call    NET_BUFF_free
3545 hidnplayr 327
        ret
328
 
329
 
330
;---------------------------
331
; Fragmented packet handler
332
 
333
 
334
  .has_fragments:
335
        movzx   eax, [edx + IPv4_header.FlagsAndFragmentOffset]
336
        xchg    al, ah
337
        shl     ax, 3
338
 
4258 hidnplayr 339
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: fragmented packet offset=%u id=%x ptr=0x%x\n", ax, [edx + IPv4_header.Identification]:4, edx
3545 hidnplayr 340
 
341
        test    ax, ax                                          ; Is this the first packet of the fragment?
342
        jz      .is_first_fragment
343
 
344
 
345
;-------------------------------------------------------
346
; We have a fragmented IP packet, but it's not the first
347
 
3556 hidnplayr 348
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Middle fragment packet received!\n"
3545 hidnplayr 349
 
350
        call    IPv4_find_fragment_slot
351
        cmp     esi, -1
352
        je      .dump
353
 
4052 hidnplayr 354
        mov     [esi + IPv4_FRAGMENT_slot.ttl], 15              ; Reset the ttl
355
        mov     esi, [esi + IPv4_FRAGMENT_slot.ptr]
3545 hidnplayr 356
        or      edi, -1
357
  .find_last_entry:                                             ; The following routine will try to find the last entry
4052 hidnplayr 358
        cmp     edi, [esi + IPv4_FRAGMENT_entry.PrevPtr]
3545 hidnplayr 359
        jne     .destroy_slot                                   ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
360
        mov     edi, esi
4052 hidnplayr 361
        mov     esi, [esi + IPv4_FRAGMENT_entry.NextPtr]
3545 hidnplayr 362
        cmp     esi, -1
363
        jne     .find_last_entry
364
                                                                ; We found the last entry (pointer is now in edi)
365
                                                                ; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
366
 
367
        pop     eax                                             ; pointer to packet
4052 hidnplayr 368
        mov     [edi + IPv4_FRAGMENT_entry.NextPtr], eax        ; update pointer of previous entry to the new entry
369
        mov     [eax + IPv4_FRAGMENT_entry.NextPtr], -1
370
        mov     [eax + IPv4_FRAGMENT_entry.PrevPtr], edi
371
        mov     [eax + IPv4_FRAGMENT_entry.Owner], ebx
3545 hidnplayr 372
 
373
        ret
374
 
375
 
376
;------------------------------------
377
; We have received the first fragment
378
 
379
  .is_first_fragment:
3556 hidnplayr 380
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: First fragment packet received!\n"
3545 hidnplayr 381
                                                                ; try to locate a free slot..
4052 hidnplayr 382
        mov     ecx, IPv4_MAX_FRAGMENTS
383
        mov     esi, IPv4_FRAGMENT_LIST
3545 hidnplayr 384
  .find_free_slot:
4052 hidnplayr 385
        cmp     word [esi + IPv4_FRAGMENT_slot.ttl], 0
3545 hidnplayr 386
        je      .found_free_slot
4052 hidnplayr 387
        add     esi, sizeof.IPv4_FRAGMENT_slot
3545 hidnplayr 388
        loop    .find_free_slot
389
        jmp     .dump                                           ; If no free slot was found, dump the packet
390
 
391
  .found_free_slot:                                             ; We found a free slot, let's fill in the FRAGMENT_slot structure
4052 hidnplayr 392
        mov     [esi + IPv4_FRAGMENT_slot.ttl], 15              ; RFC recommends 15 secs as ttl
3545 hidnplayr 393
        mov     ax, [edx + IPv4_header.Identification]
4052 hidnplayr 394
        mov     [esi + IPv4_FRAGMENT_slot.id], ax
3545 hidnplayr 395
        mov     eax, [edx + IPv4_header.SourceAddress]
4052 hidnplayr 396
        mov     [esi + IPv4_FRAGMENT_slot.SrcIP], eax
3545 hidnplayr 397
        mov     eax, [edx + IPv4_header.DestinationAddress]
4052 hidnplayr 398
        mov     [esi + IPv4_FRAGMENT_slot.DstIP], eax
3545 hidnplayr 399
        pop     eax
4052 hidnplayr 400
        mov     [esi + IPv4_FRAGMENT_slot.ptr], eax
3545 hidnplayr 401
                                                                ; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
4052 hidnplayr 402
        mov     [eax + IPv4_FRAGMENT_entry.NextPtr], -1
403
        mov     [eax + IPv4_FRAGMENT_entry.PrevPtr], -1
404
        mov     [eax + IPv4_FRAGMENT_entry.Owner], ebx
3545 hidnplayr 405
 
406
        ret
407
 
408
 
409
;-----------------------------------
410
; We have received the last fragment
411
 
412
  .is_last_fragment:
3556 hidnplayr 413
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Last fragment packet received!\n"
3545 hidnplayr 414
 
415
        call    IPv4_find_fragment_slot
416
        cmp     esi, -1
417
        je      .dump
418
 
4052 hidnplayr 419
        mov     esi, [esi + IPv4_FRAGMENT_slot.ptr]                     ; We found the first entry, let's calculate total size of the packet in eax, so we can allocate a buffer
3545 hidnplayr 420
        push    esi
421
        xor     eax, eax
422
        or      edi, -1
423
 
424
  .count_bytes:
4052 hidnplayr 425
        cmp     [esi + IPv4_FRAGMENT_entry.PrevPtr], edi
426
        jne     .destroy_slot_pop                                                       ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
427
        mov     cx, [esi + sizeof.IPv4_FRAGMENT_entry + IPv4_header.TotalLength]        ; Add total length
3545 hidnplayr 428
        xchg    cl, ch
3556 hidnplayr 429
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Packet size=%u\n", cx
3545 hidnplayr 430
        add     ax, cx
4052 hidnplayr 431
        movzx   cx, [esi + sizeof.IPv4_FRAGMENT_entry + IPv4_header.VersionAndIHL]      ; Sub Header length
3545 hidnplayr 432
        and     cx, 0x000F
433
        shl     cx, 2
3556 hidnplayr 434
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Header size=%u\n", cx
3545 hidnplayr 435
        sub     ax, cx
436
        mov     edi, esi
4052 hidnplayr 437
        mov     esi, [esi + IPv4_FRAGMENT_entry.NextPtr]
3545 hidnplayr 438
        cmp     esi, -1
439
        jne     .count_bytes
440
 
441
        mov     esi, [esp+4]
4052 hidnplayr 442
        mov     [edi + IPv4_FRAGMENT_entry.NextPtr], esi                                ; Add this packet to the chain, this simplifies the following code
443
        mov     [esi + IPv4_FRAGMENT_entry.NextPtr], -1
444
        mov     [esi + IPv4_FRAGMENT_entry.PrevPtr], edi
445
        mov     [esi + IPv4_FRAGMENT_entry.Owner], ebx
3545 hidnplayr 446
 
4052 hidnplayr 447
        mov     cx, [edx + IPv4_header.TotalLength]                                     ; Note: This time we dont substract Header length
3545 hidnplayr 448
        xchg    cl, ch
3556 hidnplayr 449
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Packet size=%u\n", cx
3545 hidnplayr 450
        add     ax, cx
3556 hidnplayr 451
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Total Received data size=%u\n", eax
3545 hidnplayr 452
 
453
        push    eax
454
        mov     ax, [edx + IPv4_header.FlagsAndFragmentOffset]
455
        xchg    al, ah
456
        shl     ax, 3
457
        add     cx, ax
458
        pop     eax
3556 hidnplayr 459
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Total Fragment size=%u\n", ecx
3545 hidnplayr 460
 
461
        cmp     ax, cx
462
        jne     .destroy_slot_pop
463
 
464
        push    eax
465
        push    eax
466
        call    kernel_alloc
467
        test    eax, eax
468
        je      .destroy_slot_pop                                                       ; If we dont have enough space to allocate the buffer, discard all packets in slot
469
        mov     edx, [esp+4]                                                            ; Get pointer to first fragment entry back in edx
470
 
471
  .rebuild_packet_loop:
4052 hidnplayr 472
        movzx   ecx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.FlagsAndFragmentOffset] ; Calculate the fragment offset
3545 hidnplayr 473
        xchg    cl, ch                                                                  ;  intel byte order
474
        shl     cx, 3                                                                   ;   multiply by 8 and clear first 3 bits
3556 hidnplayr 475
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Fragment offset=%u\n", cx
3545 hidnplayr 476
 
477
        lea     edi, [eax + ecx]                                                        ; Notice that edi will be equal to eax for first fragment
4258 hidnplayr 478
        movzx   ebx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.VersionAndIHL]     ; Find header size (in ebx) of fragment
3545 hidnplayr 479
        and     bx, 0x000F                                                              ;
480
        shl     bx, 2                                                                   ;
481
 
4258 hidnplayr 482
        lea     esi, [edx + sizeof.IPv4_FRAGMENT_entry]                                 ; Set esi to the correct begin of fragment
483
        movzx   ecx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.TotalLength]       ; Calculate total length of fragment
3545 hidnplayr 484
        xchg    cl, ch                                                                  ;  intel byte order
485
 
486
        cmp     edi, eax                                                                ; Is this packet the first fragment ?
487
        je      .first_fragment
488
        sub     cx, bx                                                                  ; If not, dont copy the header
489
        add     esi, ebx                                                                ;
490
  .first_fragment:
491
 
4258 hidnplayr 492
 
493
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Copying %u bytes from 0x%x to 0x%x\n", ecx, esi, edi
3545 hidnplayr 494
        push    cx                                                                      ; First copy dword-wise, then byte-wise
495
        shr     cx, 2                                                                   ;
3711 clevermous 496
        rep movsd                                                                       ;
3545 hidnplayr 497
        pop     cx                                                                      ;
498
        and     cx, 3                                                                   ;
3711 clevermous 499
        rep movsb                                                                       ;
3545 hidnplayr 500
 
501
        push    eax
4259 hidnplayr 502
        push    [edx + IPv4_FRAGMENT_entry.Owner]                                       ; we need to remeber the owner, in case this is the last packet
503
        push    [edx + IPv4_FRAGMENT_entry.NextPtr]                                     ; Set edx to the next pointer
3545 hidnplayr 504
        push    edx                                                                     ; Push pointer to fragment onto stack
4258 hidnplayr 505
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Next Fragment: 0x%x\n", edx
5522 hidnplayr 506
        call    NET_BUFF_free                                                          ; free the previous fragment buffer (this uses the value from stack)
4258 hidnplayr 507
        pop     edx ebx eax
3545 hidnplayr 508
        cmp     edx, -1                                                                 ; Check if it is last fragment in chain
509
        jne     .rebuild_packet_loop
510
 
511
        pop     ecx
512
        xchg    cl, ch
513
        mov     edx, eax
514
        mov     [edx + IPv4_header.TotalLength], cx
4259 hidnplayr 515
        add     esp, 12
3545 hidnplayr 516
        xchg    cl, ch
4259 hidnplayr 517
        push    ecx edx                 ; size and pointer
518
        jmp     .handle_it              ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr
3545 hidnplayr 519
 
520
  .destroy_slot_pop:
521
        add     esp, 4
522
  .destroy_slot:
3556 hidnplayr 523
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Destroy fragment slot!\n"
3545 hidnplayr 524
        ; TODO!
525
        jmp     .dump
526
 
527
 
528
 
529
 
530
 
531
;-----------------------------------------------------------------
532
;
533
; find fragment slot
534
;
535
; IN: pointer to fragmented packet in edx
536
; OUT: pointer to slot in esi, -1 on error
537
;
538
;-----------------------------------------------------------------
539
align 4
540
IPv4_find_fragment_slot:
541
 
542
;;; TODO: the RFC says we should check protocol number too
543
 
544
        push    eax ebx ecx edx
545
        mov     ax, [edx + IPv4_header.Identification]
4052 hidnplayr 546
        mov     ecx, IPv4_MAX_FRAGMENTS
547
        mov     esi, IPv4_FRAGMENT_LIST
3545 hidnplayr 548
        mov     ebx, [edx + IPv4_header.SourceAddress]
549
        mov     edx, [edx + IPv4_header.DestinationAddress]
550
  .find_slot:
4052 hidnplayr 551
        cmp     [esi + IPv4_FRAGMENT_slot.id], ax
3545 hidnplayr 552
        jne     .try_next
4052 hidnplayr 553
        cmp     [esi + IPv4_FRAGMENT_slot.SrcIP], ebx
3545 hidnplayr 554
        jne     .try_next
4052 hidnplayr 555
        cmp     [esi + IPv4_FRAGMENT_slot.DstIP], edx
3545 hidnplayr 556
        je      .found_slot
557
  .try_next:
4052 hidnplayr 558
        add     esi, sizeof.IPv4_FRAGMENT_slot
3545 hidnplayr 559
        loop    .find_slot
560
 
561
        or      esi, -1
562
  .found_slot:
563
        pop     edx ecx ebx eax
564
        ret
565
 
566
 
567
;------------------------------------------------------------------
568
;
569
; IPv4_output
570
;
4052 hidnplayr 571
; IN:   eax = Destination IP
572
;       ecx = data length
573
;       edx = Source IP
5015 hidnplayr 574
;        di = TTL shl 8 + protocol
3545 hidnplayr 575
;
5015 hidnplayr 576
; OUT:  eax = pointer to buffer start / 0 on error
577
;       ebx = device ptr (send packet through this device)
578
;       ecx = data length
579
;       edx = size of complete frame
580
;       edi = start of IPv4 payload
3545 hidnplayr 581
;
582
;------------------------------------------------------------------
583
align 4
584
IPv4_output:
585
 
3601 hidnplayr 586
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_output: size=%u ip=0x%x\n", ecx, eax
3545 hidnplayr 587
 
588
        cmp     ecx, 65500              ; Max IPv4 packet size
589
        ja      .too_large
590
 
4052 hidnplayr 591
        push    ecx di eax
592
        call    IPv4_route              ; outputs device number in edi, dest ip in eax, source IP in edx
593
        push    edx
3610 hidnplayr 594
        test    edi, edi
595
        jz      .loopback
596
 
3545 hidnplayr 597
        call    ARP_IP_to_MAC
598
        test    eax, 0xffff0000         ; error bits
599
        jnz     .arp_error
600
        push    ebx                     ; push the mac onto the stack
601
        push    ax
602
 
4052 hidnplayr 603
        inc     [IPv4_packets_tx + edi]   ; update stats
3545 hidnplayr 604
 
5015 hidnplayr 605
        mov     ax, ETHER_PROTO_IPv4
3638 hidnplayr 606
        mov     ebx, [NET_DRV_LIST + edi]
4052 hidnplayr 607
        mov     ecx, [esp + 6 + 8 + 2]
3545 hidnplayr 608
        add     ecx, sizeof.IPv4_header
5015 hidnplayr 609
        mov     edx, esp
3545 hidnplayr 610
        call    ETH_output
611
        jz      .eth_error
612
        add     esp, 6                  ; pop the mac out of the stack
613
 
614
  .continue:
615
        xchg    cl, ch                                  ; internet byte order
616
        mov     [edi + IPv4_header.VersionAndIHL], 0x45 ; IPv4, normal length (no Optional header)
617
        mov     [edi + IPv4_header.TypeOfService], 0    ; nothing special, just plain ip packet
618
        mov     [edi + IPv4_header.TotalLength], cx
619
        mov     [edi + IPv4_header.Identification], 0   ; fragment id: FIXME
620
        mov     [edi + IPv4_header.FlagsAndFragmentOffset], 0
4052 hidnplayr 621
 
3545 hidnplayr 622
        mov     [edi + IPv4_header.HeaderChecksum], 0
623
        popd    [edi + IPv4_header.SourceAddress]
624
        popd    [edi + IPv4_header.DestinationAddress]
625
 
4052 hidnplayr 626
        pop     word[edi + IPv4_header.TimeToLive]      ; ttl shl 8 + protocol
627
;               [edi + IPv4_header.Protocol]
628
 
3545 hidnplayr 629
        pop     ecx
630
 
631
        IPv4_checksum edi
632
        add     edi, sizeof.IPv4_header
3556 hidnplayr 633
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_output: success!\n"
3545 hidnplayr 634
        ret
635
 
636
  .eth_error:
3603 hidnplayr 637
        DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output: ethernet error\n"
3545 hidnplayr 638
        add     esp, 3*4+2+6
5015 hidnplayr 639
        xor     eax, eax
3545 hidnplayr 640
        ret
641
 
642
  .arp_error:
3603 hidnplayr 643
        DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output: ARP error=%x\n", eax
3545 hidnplayr 644
        add     esp, 3*4+2
5015 hidnplayr 645
        xor     eax, eax
3545 hidnplayr 646
        ret
647
 
648
  .too_large:
3556 hidnplayr 649
        DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output: Packet too large!\n"
5015 hidnplayr 650
        xor     eax, eax
3545 hidnplayr 651
        ret
652
 
653
  .loopback:
5522 hidnplayr 654
        mov     dword [esp], eax                        ; set source IP to dest IP
3610 hidnplayr 655
        mov     ecx, [esp + 10]
3545 hidnplayr 656
        add     ecx, sizeof.IPv4_header
3600 hidnplayr 657
        mov     edi, AF_INET4
3545 hidnplayr 658
        call    LOOP_output
659
        jmp     .continue
660
 
661
 
662
 
663
 
664
;------------------------------------------------------------------
665
;
666
; IPv4_output_raw
667
;
668
; IN: eax = socket ptr
669
;     ecx = data length
670
;     esi = data ptr
671
;
5015 hidnplayr 672
; OUT: eax = -1 on error
3545 hidnplayr 673
;
674
;------------------------------------------------------------------
675
align 4
676
IPv4_output_raw:
677
 
678
        DEBUGF 1,"IPv4_output_raw: size=%u ptr=%x socket=%x\n", ecx, esi, eax
679
 
680
        cmp     ecx, 1480               ;;;;; FIXME
681
        ja      .too_large
682
 
683
        sub     esp, 8
684
        push    esi eax
685
 
686
        call    IPv4_route
687
        call    ARP_IP_to_MAC
688
 
689
        test    eax, 0xffff0000         ; error bits
690
        jnz     .arp_error
691
 
692
        push    ebx                     ; push the mac
693
        push    ax
694
 
4052 hidnplayr 695
        inc     [IPv4_packets_tx + 4*edi]
5015 hidnplayr 696
        mov     ax, ETHER_PROTO_IPv4
3601 hidnplayr 697
        mov     ebx, [NET_DRV_LIST + 4*edi]
3545 hidnplayr 698
        mov     ecx, [esp + 6 + 4]
699
        add     ecx, sizeof.IPv4_header
5015 hidnplayr 700
        mov     edx, esp
3545 hidnplayr 701
        call    ETH_output
702
        jz      .error
703
        add     esp, 6  ; pop the mac
704
 
705
        mov     dword[esp+4+4], edx
706
        mov     dword[esp+4+4+4], eax
707
 
708
        pop     eax esi
709
;; todo: check socket options if we should add header, or just compute checksum
710
 
711
        push    edi ecx
3711 clevermous 712
        rep movsb
3545 hidnplayr 713
        pop     ecx edi
714
 
715
;        [edi + IPv4_header.VersionAndIHL]              ; IPv4, normal length (no Optional header)
716
;        [edi + IPv4_header.TypeOfService]              ; nothing special, just plain ip packet
717
;        [edi + IPv4_header.TotalLength]
718
;        [edi + IPv4_header.TotalLength]                ; internet byte order
719
;        [edi + IPv4_header.FlagsAndFragmentOffset]
720
 
721
        mov     [edi + IPv4_header.HeaderChecksum], 0
722
 
723
;        [edi + IPv4_header.TimeToLive]                 ; ttl shl 8 + protocol
724
;        [edi + IPv4_header.Protocol]
725
;        [edi + IPv4_header.Identification]             ; fragment id
726
;        [edi + IPv4_header.SourceAddress]
727
;        [edi + IPv4_header.DestinationAddress]
728
 
729
        IPv4_checksum edi                       ;;;; todo: checksum for IP packet with options!
730
        add     edi, sizeof.IPv4_header
3556 hidnplayr 731
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_output_raw: device=%x\n", ebx
3545 hidnplayr 732
        call    [ebx + NET_DEVICE.transmit]
733
        ret
734
 
735
  .error:
736
        add     esp, 6
737
  .arp_error:
738
        add     esp, 8+4+4
739
  .too_large:
3556 hidnplayr 740
        DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output_raw: Failed\n"
5015 hidnplayr 741
        or      eax, -1
3545 hidnplayr 742
        ret
743
 
744
 
745
;--------------------------------------------------------
746
;
747
;
748
; IN: dword [esp] = pointer to buffer containing ipv4 packet to be fragmented
749
;     esi = pointer to ip header in that buffer
750
;     ecx = max size of fragments
751
;
752
; OUT: /
753
;
754
;--------------------------------------------------------
755
 
756
align 4
757
IPv4_fragment:
758
 
3556 hidnplayr 759
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_fragment\n"
3545 hidnplayr 760
 
761
        and     ecx, not 111b   ; align 4
762
 
763
        cmp     ecx, sizeof.IPv4_header + 8     ; must be able to put at least 8 bytes
764
        jb      .err2
765
 
766
        push    esi ecx
767
        mov     eax, [esi + IPv4_header.DestinationAddress]
768
        call    ARP_IP_to_MAC
769
        pop     ecx esi
770
        cmp     eax, -1
771
        jz      .err2
772
 
773
        push    ebx
774
        push    ax
775
 
776
        mov     ebx, [NET_DRV_LIST]
777
        lea     eax, [ebx + ETH_DEVICE.mac]
778
        push    eax
779
 
780
 
781
        push    esi                             ; ptr to ip header
782
        sub     ecx, sizeof.IPv4_header         ; substract header size
783
        push    ecx                             ; max data size
784
        push    dword 0                         ; offset
785
 
786
  .new_fragment:
3556 hidnplayr 787
        DEBUGF  DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: new fragment"
3545 hidnplayr 788
 
5015 hidnplayr 789
        mov     ax, ETHER_PROTO_IPv4
3545 hidnplayr 790
        lea     ebx, [esp + 4*4]
791
        call    ETH_output
792
        jz      .err
793
 
794
; copy header
795
        mov     esi, [esp + 2*4]
796
        mov     ecx, 5  ; 5 dwords: TODO: use IHL field of the header!
3711 clevermous 797
        rep movsd
3545 hidnplayr 798
 
799
; copy data
800
        mov     esi, [esp + 2*4]
801
        add     esi, sizeof.IPv4_header
802
        add     esi, [esp]      ; offset
803
 
804
        mov     ecx, [esp + 1*4]
3556 hidnplayr 805
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_fragment: copying %u bytes\n", ecx
3711 clevermous 806
        rep movsb
3545 hidnplayr 807
 
808
; now, correct header
809
        mov     ecx, [esp + 1*4]
810
        add     ecx, sizeof.IPv4_header
811
        xchg    cl, ch
812
        mov     [edi + IPv4_header.TotalLength], cx
813
 
814
        mov     ecx, [esp]              ; offset
815
        xchg    cl, ch
816
 
817
;        cmp     dword[esp + 4*4], 0     ; last fragment?;<<<<<<
818
;        je      .last_fragment
819
        or      cx, 1 shl 2             ; more fragments
820
;  .last_fragment:
821
        mov     [edi + IPv4_header.FlagsAndFragmentOffset], cx
822
 
823
        mov     [edi + IPv4_header.HeaderChecksum], 0
824
 
825
        ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<< send the packet
826
        mov     ecx, [esp + 1*4]
827
 
828
        push    edx eax
829
        IPv4_checksum edi
830
 
831
        call    [ebx + NET_DEVICE.transmit]
832
        ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
833
 
3711 clevermous 834
        mov     ecx, [esp+4]
3545 hidnplayr 835
        add     [esp], ecx
836
 
837
        mov     ecx, [esp+3*4+6+4]      ; ptr to begin of buff
838
        add     ecx, [esp+3*4+6+4+4]    ; buff size
839
        sub     ecx, [esp+2*4]          ; ptr to ip header
840
        add     ecx, [esp]              ; offset
841
 
3556 hidnplayr 842
        DEBUGF  DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: %u bytes remaining\n", ecx
3545 hidnplayr 843
 
844
        cmp     ecx, [esp+1*4]
845
        jae     .new_fragment
846
 
847
        mov     [esp+4], ecx            ; set fragment size to remaining packet size
848
        jmp     .new_fragment
849
 
850
      .err:
3556 hidnplayr 851
        DEBUGF  DEBUG_NETWORK_ERROR, "Ipv4_fragment: failed\n"
3545 hidnplayr 852
      .done:
853
        add     esp, 12 + 4 + 6
854
      .err2:
3556 hidnplayr 855
        DEBUGF  DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: dumping\n"
5522 hidnplayr 856
        call    NET_BUFF_free
3545 hidnplayr 857
        ret
858
 
859
 
860
 
861
;---------------------------------------------------------------------------
862
;
863
; IPv4_route
864
;
865
; IN:   eax = Destination IP
4052 hidnplayr 866
;       edx = Source IP
867
; OUT:  eax = Destination IP (or gateway IP)
868
;       edx = Source IP
869
;       edi = device number*4
870
; DESTROYED:
871
;       ecx
3545 hidnplayr 872
;
873
;---------------------------------------------------------------------------
874
align 4
4052 hidnplayr 875
IPv4_route:     ; TODO: return error if no valid route found
3545 hidnplayr 876
 
877
        cmp     eax, 0xffffffff
878
        je      .broadcast
879
 
880
        xor     edi, edi
881
  .loop:
4052 hidnplayr 882
        mov     ebx, [IP_LIST + edi]
883
        and     ebx, [SUBNET_LIST + edi]
3545 hidnplayr 884
        jz      .next
4052 hidnplayr 885
        mov     ecx, eax
886
        and     ecx, [SUBNET_LIST + edi]
887
        cmp     ebx, ecx
888
        je      .got_it
3545 hidnplayr 889
  .next:
890
        add     edi, 4
4052 hidnplayr 891
        cmp     edi, 4*NET_DEVICES_MAX
892
        jb      .loop
3545 hidnplayr 893
 
4052 hidnplayr 894
        mov     eax, [GATEWAY_LIST + 4]         ; TODO: let user (or a user space daemon) configure default route
3545 hidnplayr 895
  .broadcast:
4052 hidnplayr 896
        mov     edi, 4                          ; TODO: same as above
897
  .got_it:
898
        DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_route: %u\n", edi
899
        test    edx, edx
900
        jnz     @f
901
        mov     edx, [IP_LIST + edi]
902
  @@:
903
 
3545 hidnplayr 904
        ret
905
 
906
 
907
 
908
;---------------------------------------------------------------------------
909
;
910
; IPv4_get_frgmnt_num
911
;
912
; IN: /
913
; OUT: fragment number in ax
914
;
915
;---------------------------------------------------------------------------
916
align 4
917
IPv4_get_frgmnt_num:
918
        xor     ax, ax  ;;; TODO: replace this with real code
919
 
920
        ret
921
 
922
 
4030 hidnplayr 923
;-----------------------------------------------------------------
924
;
925
; IPv4_connect
926
;
927
;   IN: eax = socket pointer
928
;  OUT: eax = 0 ok / -1 error
929
;       ebx = error code
930
;
931
;-------------------------
932
align 4
933
IPv4_connect:
934
 
4035 hidnplayr 935
        push    eax edx
4030 hidnplayr 936
        lea     ecx, [eax + SOCKET.mutex]
937
        call    mutex_lock
4035 hidnplayr 938
        pop     edx eax
4030 hidnplayr 939
 
940
; Fill in local IP
941
        cmp     [eax + IP_SOCKET.LocalIP], 0
942
        jne     @f
943
        push    [IP_LIST + 4]                                   ; FIXME: use correct local IP
944
        pop     [eax + IP_SOCKET.LocalIP]
945
 
946
; Fill in remote IP
947
        pushd   [edx + 4]
948
        pop     [eax + IP_SOCKET.RemoteIP]
949
 
4035 hidnplayr 950
; Set up data receiving queue
4030 hidnplayr 951
        push    eax
4035 hidnplayr 952
        init_queue (eax + SOCKET_QUEUE_LOCATION)
4030 hidnplayr 953
        pop     eax
954
 
955
        lea     ecx, [eax + SOCKET.mutex]
956
        call    mutex_unlock
957
 
958
        xor     eax, eax
959
        ret
960
 
961
 
3545 hidnplayr 962
;---------------------------------------------------------------------------
963
;
964
; IPv4_API
965
;
966
; This function is called by system function 75
967
;
968
; IN:  subfunction number in bl
969
;      device number in bh
970
;      ecx, edx, .. depends on subfunction
971
;
972
; OUT:
973
;
974
;---------------------------------------------------------------------------
975
align 4
976
IPv4_api:
977
 
978
        movzx   eax, bh
979
        shl     eax, 2
980
 
981
        and     ebx, 0x000000ff
982
        cmp     ebx, .number
983
        ja      .error
984
        jmp     dword [.table + 4*ebx]
985
 
986
  .table:
987
        dd      .packets_tx     ; 0
988
        dd      .packets_rx     ; 1
989
        dd      .read_ip        ; 2
990
        dd      .write_ip       ; 3
991
        dd      .read_dns       ; 4
992
        dd      .write_dns      ; 5
993
        dd      .read_subnet    ; 6
994
        dd      .write_subnet   ; 7
995
        dd      .read_gateway   ; 8
996
        dd      .write_gateway  ; 9
997
  .number = ($ - .table) / 4 - 1
998
 
999
  .error:
1000
        mov     eax, -1
1001
        ret
1002
 
1003
  .packets_tx:
4052 hidnplayr 1004
        mov     eax, [IPv4_packets_tx + eax]
3545 hidnplayr 1005
        ret
1006
 
1007
  .packets_rx:
4052 hidnplayr 1008
        mov     eax, [IPv4_packets_rx + eax]
3545 hidnplayr 1009
        ret
1010
 
1011
  .read_ip:
1012
        mov     eax, [IP_LIST + eax]
1013
        ret
1014
 
1015
  .write_ip:
1016
        mov     [IP_LIST + eax], ecx
1017
        mov     edi, eax                        ; device number, we'll need it for ARP
1018
 
1019
        ; pre-calculate the local broadcast address
1020
        mov     ebx, [SUBNET_LIST + eax]
1021
        not     ebx
1022
        or      ebx, ecx
1023
        mov     [BROADCAST_LIST + eax], ebx
1024
 
3601 hidnplayr 1025
        mov     ebx, [NET_DRV_LIST + eax]
1026
        mov     eax, [IP_LIST + eax]
3545 hidnplayr 1027
        call    ARP_output_request              ; now send a gratuitous ARP
1028
 
1029
        call    NET_send_event
1030
        xor     eax, eax
1031
        ret
1032
 
1033
  .read_dns:
1034
        mov     eax, [DNS_LIST + eax]
1035
        ret
1036
 
1037
  .write_dns:
1038
        mov     [DNS_LIST + eax], ecx
1039
        call    NET_send_event
1040
        xor     eax, eax
1041
        ret
1042
 
1043
  .read_subnet:
1044
        mov     eax, [SUBNET_LIST + eax]
1045
        ret
1046
 
1047
  .write_subnet:
1048
        mov     [SUBNET_LIST + eax], ecx
1049
 
1050
        ; pre-calculate the local broadcast address
1051
        mov     ebx, [IP_LIST + eax]
1052
        not     ecx
1053
        or      ecx, ebx
1054
        mov     [BROADCAST_LIST + eax], ecx
1055
 
1056
        call    NET_send_event
1057
        xor     eax, eax
1058
        ret
1059
 
1060
  .read_gateway:
1061
        mov     eax, [GATEWAY_LIST + eax]
1062
        ret
1063
 
1064
  .write_gateway:
1065
        mov     [GATEWAY_LIST + eax], ecx
1066
 
1067
        call    NET_send_event
1068
        xor     eax, eax
1069
        ret