Subversion Repositories Kolibri OS

Rev

Rev 1196 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  IP.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.  
  19. $Revision: 922 $
  20.  
  21. ; IP underlying protocols numbers
  22.  
  23. ETHER_IPv4      equ 0x0008      ; Reversed from 0800 for intel
  24.  
  25. MAX_FRAGMENTS   equ 16
  26. MAX_IP          equ MAX_NET_DEVICES
  27.  
  28. struct  IPv4_Packet
  29.         .VersionAndIHL          db  ?  ; Version[0-3 bits] and IHL(header length)[4-7 bits]
  30.         .TypeOfService          db  ?
  31.         .TotalLength            dw  ?
  32.         .Identification         dw  ?
  33.         .FlagsAndFragmentOffset dw  ?  ; Flags[0-2] and FragmentOffset[3-15]
  34.         .TimeToLive             db  ?  ;
  35.         .Protocol               db  ?
  36.         .HeaderChecksum         dw  ?
  37.         .SourceAddress          dd  ?
  38.         .DestinationAddress     dd  ?
  39.         .DataOrOptional:
  40. ends
  41.  
  42. struct  FRAGMENT_slot
  43.         .ttl                    dw  ?  ; Time to live for this entry, 0 for empty slot's
  44.         .id                     dw  ?  ; Identification field from IP header
  45.         .SrcIP                  dd  ?  ; .. from IP header
  46.         .DstIP                  dd  ?  ; .. from IP header
  47.         .ptr                    dd  ?  ; Pointer to first packet
  48.         .size:
  49. ends
  50.  
  51. struct  FRAGMENT_entry                 ; This structure will replace the ethernet header in fragmented ip packets
  52.         .PrevPtr                dd  ?  ; Pointer to previous fragment entry  (-1 for first packet)
  53.         .NextPtr                dd  ?  ; Pointer to next fragment entry (-1 for last packet)
  54.         .Owner                  dd  ?  ; Pointer to structure of driver
  55.                                 rb  2  ; to match ethernet header size
  56.         .Data:                         ; Ip header begins here (we will need the IP header to re-construct the complete packet)
  57. ends
  58.  
  59. align 4
  60. uglobal
  61.         BROADCAST       dd  ?
  62.         IP_LIST         rd  MAX_IP
  63.         SUBNET_LIST     rd  MAX_IP
  64.         DNS_LIST        rd  MAX_IP
  65.         GATEWAY_LIST    rd  MAX_IP
  66.         IP_PACKETS_TX   rd  MAX_IP
  67.         IP_PACKETS_RX   rd  MAX_IP
  68.         FRAGMENT_LIST   rb  MAX_FRAGMENTS*FRAGMENT_slot.size
  69. endg
  70.  
  71.  
  72. ;-----------------------------------------------------------------
  73. ;
  74. ; IPv4_init
  75. ;
  76. ;  This function resets all IP variables
  77. ;
  78. ;  IN:  /
  79. ;  OUT: /
  80. ;
  81. ;-----------------------------------------------------------------
  82.  
  83. align 4
  84. IPv4_init:
  85.  
  86.         or      eax, -1
  87.         mov     edi, BROADCAST
  88.         mov     ecx, 4*MAX_IP+1
  89.         rep     stosd
  90.  
  91.         xor     eax, eax
  92.         mov     edi, FRAGMENT_LIST
  93.         mov     ecx, FRAGMENT_slot.size*MAX_FRAGMENTS/4 + 2*MAX_IP
  94.         rep     stosd
  95.  
  96.         ret
  97.  
  98.  
  99.  
  100. ;-----------------------------------------------------------------
  101. ;
  102. ; IP_Handler:
  103. ;
  104. ;  Called by eth_handler,
  105. ;  will check if IP Packet isnt damaged
  106. ;  and call appropriate handler. (TCP/UDP/ICMP/..)
  107. ;
  108. ;  It will also re-construct fragmented packets
  109. ;
  110. ;  IN:  Pointer to buffer in [esp]
  111. ;       size of buffer in [esp+4]
  112. ;       pointer to device struct in ebx
  113. ;       pointer to IP Packet data in edx
  114. ;  OUT: /
  115. ;
  116. ;-----------------------------------------------------------------
  117.  
  118. align 4
  119. IPv4_handler:
  120.  
  121.         DEBUGF  1,"IP_Handler - start\n"
  122.         mov     cx , [edx + IPv4_Packet.HeaderChecksum]
  123.         xchg    ch , cl                                         ; Get the checksum in intel format
  124.  
  125.         mov     word [edx + IPv4_Packet.HeaderChecksum], 0      ; Clear checksum field to recalculating checksum
  126.  
  127.         movzx   eax, byte [edx + IPv4_Packet.VersionAndIHL]     ; Calculate Header length by using IHL field
  128.         and     eax, 0x0000000F  ;
  129.         shl     eax, 2           ;
  130.  
  131.         push    edx
  132.         stdcall checksum_jb, edx, eax                           ; buf_ptr, buf_size
  133.         pop     edx
  134.         cmp     cx , ax
  135.         jnz     .dump                                           ; if CHECKSUM isn't valid then dump Packet
  136.  
  137.         mov     eax, [edx + IPv4_Packet.DestinationAddress]
  138.         mov     edi, BROADCAST
  139.         mov     ecx, MAX_IP+1
  140.         repnz   scasd
  141.         jz      .ip_ok
  142.  
  143.         not     eax
  144.         test    eax, 127 shl 24 ; 127.x.x.x
  145.         jz      .ip_ok
  146.  
  147. ;  TODO: we need to check for broadcasts (other then 255.255.255.255)
  148.  
  149.         jmp     .dump
  150.  
  151.   .ip_ok:
  152.         call    ETH_struc2dev                                   ; TODO: make this work on other protocols too!
  153.         inc     [IP_PACKETS_RX+4*edi]
  154.         DEBUGF  1,"IP_Handler - packet from %u.%u.%u.%u\n",\
  155.         [edx + IPv4_Packet.SourceAddress]:1,[edx + IPv4_Packet.SourceAddress + 1]:1,[edx + IPv4_Packet.SourceAddress + 2]:1,[edx + IPv4_Packet.SourceAddress + 3]:1
  156.  
  157.         mov     al , [edx + IPv4_Packet.VersionAndIHL]
  158.         and     al , 0x0f                                       ; get IHL(header length)
  159.         cmp     al , 0x05                                       ; IHL!= 5*4(20 bytes)
  160.         jnz     .dump                                           ; TODO: dont dump packets wich have optional fiels !!!                   /!\
  161.  
  162.         cmp     byte [edx + IPv4_Packet.TimeToLive], 0
  163.         je      .dump
  164.  
  165.         movzx   eax, word [edx + IPv4_Packet.FlagsAndFragmentOffset]
  166.         xchg    al , ah
  167.  
  168.         test    ax , 1 shl 13                                   ; Is 'more fragments' flag set ?
  169.         jnz     .yes_fragments                                  ; If so, we definately have a fragmented packet
  170.  
  171.         test    ax , 0x1fff                                     ; If flag is not set, but there is a fragment offset, the packet is last in series of fragmented packets
  172.         jnz     .last_fragment
  173.  
  174.    .handle_it:                                                  ; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
  175.         movzx   eax, byte [edx + IPv4_Packet.VersionAndIHL]     ; Calculate Header length by using IHL field
  176.         and     eax, 0x0000000F                                 ;
  177.         shl     eax, 2                                          ;
  178.  
  179.         movzx   ecx, word [edx + IPv4_Packet.TotalLength]       ; Calculate length of encapsulated Packet
  180.         xchg    cl , ch                                         ;
  181.         sub     ecx, eax                                        ;
  182.  
  183.         add     eax, edx
  184.         push    eax
  185.         mov     al , [edx + IPv4_Packet.Protocol]
  186.         pop     edx                                             ; Offset to data (tcp/udp/icmp/.. Packet)
  187.  
  188.         cmp     al , IP_PROTO_TCP
  189. ;        je      TCP_handler
  190.  
  191.         cmp     al , IP_PROTO_UDP
  192.         je      UDP_handler
  193.  
  194.         cmp     al , IP_PROTO_ICMP
  195.         je      ICMP_handler
  196.  
  197.         DEBUGF  1,"IP_Handler - unknown protocol:%u\n",al
  198.  
  199.   .dump:
  200.         DEBUGF  1,"IP_Handler - done\n"
  201. ;        inc     [dumped_rx_count]
  202.         call    kernel_free
  203.         add     esp, 4                                          ; pop (balance stack)
  204.         ret
  205.  
  206.  
  207.   .yes_fragments:
  208.         shl     ax , 3
  209.         DEBUGF  1,"Fragmented packet, offset:%u, id:%x\n", ax, [edx + IPv4_Packet.Identification]:4
  210.  
  211.         test    ax , ax                                         ; Is this the first packet of the fragment?
  212.         jnz     .not_first_fragment
  213.  
  214.         DEBUGF  1,"First fragmented packet received!\n"
  215.                                                                 ; try to locate a free slot..
  216.         mov     ecx, MAX_FRAGMENTS
  217.         mov     esi, FRAGMENT_LIST
  218.      .find_free_slot:
  219.         cmp     word [esi + FRAGMENT_slot.ttl], 0
  220.         je      .found_free_slot
  221.         add     esi, FRAGMENT_slot.size
  222.         loop    .find_free_slot
  223.         jmp     .dump                                           ; If no free slot was found, dump the packet
  224.  
  225.      .found_free_slot:                                          ; We found a free slot, let's fill in the FRAGMENT_slot structure
  226.         mov     word [esi + FRAGMENT_slot.ttl], 15              ; RFC recommends 15 secs as ttl
  227.         mov     ax , word [edx + IPv4_Packet.Identification]
  228.         mov     word [esi + FRAGMENT_slot.id], ax
  229.         mov     eax, dword [edx + IPv4_Packet.SourceAddress]
  230.         mov     dword [esi + FRAGMENT_slot.SrcIP], eax
  231.         mov     eax, dword [edx + IPv4_Packet.DestinationAddress]
  232.         mov     dword [esi + FRAGMENT_slot.DstIP], eax
  233.         pop     eax
  234.         mov     dword [esi + FRAGMENT_slot.ptr], eax
  235.                                                                 ; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
  236.         mov     [eax + FRAGMENT_entry.NextPtr], -1
  237.         mov     [eax + FRAGMENT_entry.PrevPtr], -1
  238.         mov     [eax + FRAGMENT_entry.Owner], ebx
  239.  
  240.         add     esp, 4                                          ; balance stack and exit
  241.         ret
  242.  
  243.  
  244.  
  245.   .not_first_fragment:
  246.         DEBUGF  1,"Middle fragmented packet received!\n"
  247.  
  248.         call    .find_fragment_slot
  249.         cmp     esi, -1
  250.         je      .dump
  251.  
  252.         mov     word [esi + FRAGMENT_slot.ttl], 15              ; Reset the ttl
  253.         mov     esi, [esi + FRAGMENT_slot.ptr]
  254.         or      edi, -1
  255.      .find_last_entry:                                          ; The following routine will try to find the last entry
  256.         cmp     edi, [esi + FRAGMENT_entry.PrevPtr]
  257.         jne     .destroy_slot                                   ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
  258.         mov     edi, esi
  259.         mov     esi, [esi + FRAGMENT_entry.NextPtr]
  260.         cmp     esi, -1
  261.         jne     .find_last_entry
  262.                                                                 ; We found the last entry (pointer is noww in edi)
  263.                                                                 ; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
  264.  
  265.         pop     eax                                             ; pointer to packet
  266.         mov     [edi + FRAGMENT_entry.NextPtr], eax             ; update pointer of previous entry to the new entry
  267.         mov     [eax + FRAGMENT_entry.NextPtr], -1
  268.         mov     [eax + FRAGMENT_entry.PrevPtr], edi
  269.         mov     [eax + FRAGMENT_entry.Owner], ebx
  270.  
  271.         add     esp, 4
  272.         ret
  273.  
  274.  
  275.  
  276.   .last_fragment:
  277.         DEBUGF  1,"Last fragmented packet received!\n"
  278.         call    .find_fragment_slot
  279.         cmp     esi, -1
  280.         je      .dump
  281.  
  282.         mov     esi, [esi + FRAGMENT_slot.ptr]                  ; We found the first entry, let's calculate total size of the packet in eax, so we can allocate a buffer
  283.         push    esi
  284.         xor     eax, eax                                        ;
  285.         or      edi, -1
  286.      .count_bytes:
  287.         cmp     [esi + FRAGMENT_entry.PrevPtr], edi
  288.         jne     .destroy_slot_pop                                               ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
  289.         mov     cx, word [esi + FRAGMENT_entry.Data + IPv4_Packet.TotalLength]    ; Add total length
  290.         xchg    cl, ch
  291.         DEBUGF  1,"Packet size: %u\n", cx
  292.         add     ax, cx
  293.         movzx   cx, byte [esi + FRAGMENT_entry.Data + IPv4_Packet.VersionAndIHL]  ; Sub Header length
  294.         and     cx, 0x000F
  295.         shl     cx, 2
  296.         DEBUGF  1,"Header size: %u\n", cx
  297.         sub     ax, cx
  298.         mov     edi, esi
  299.         mov     esi, [esi + FRAGMENT_entry.NextPtr]
  300.         cmp     esi, -1
  301.         jne     .count_bytes
  302.  
  303.         mov     esi, [esp+4]  ;;;
  304.         mov     [edi + FRAGMENT_entry.NextPtr], esi                            ; Add this packet to the chain, this simplifies the following code
  305.         mov     [esi + FRAGMENT_entry.NextPtr], -1
  306.         mov     [esi + FRAGMENT_entry.PrevPtr], edi
  307.         mov     [esi + FRAGMENT_entry.Owner], ebx
  308.  
  309.         mov     cx, [edx + IPv4_Packet.TotalLength]                            ; Note: This time we dont substract Header length
  310.         xchg    cl , ch
  311.         DEBUGF  1,"Packet size: %u\n", cx
  312.         add     ax , cx
  313.         DEBUGF  1,"Total Received data size: %u\n", eax
  314.  
  315.         push    eax
  316.         mov     ax , [edx + IPv4_Packet.FlagsAndFragmentOffset]
  317.         xchg    al , ah
  318.         shl     ax , 3
  319.         add     cx , ax
  320.         pop     eax
  321.         DEBUGF  1,"Total Fragment size: %u\n", ecx
  322.  
  323.         cmp     ax, cx
  324.         jne     .destroy_slot_pop
  325.  
  326.         push    eax
  327.         push    eax
  328.         call    kernel_alloc
  329.         test    eax, eax
  330.         je      .destroy_slot_pop                                                       ; If we dont have enough space to allocate the buffer, discard all packets in slot
  331.         mov     edx, [esp+4]                                                            ; Get pointer to first fragment entry back in edx
  332.  
  333.      .rebuild_packet_loop:
  334.         movzx   ecx, word [edx + FRAGMENT_entry.Data + IPv4_Packet.FlagsAndFragmentOffset]      ; Calculate the fragment offset
  335.         xchg    cl , ch                                                                         ;   intel byte order
  336.         shl     cx , 3                                                                          ;   multiply by 8 and clear first 3 bits
  337.         DEBUGF  1,"Fragment offset: %u\n", cx
  338.  
  339.         lea     edi, [eax + ecx]                                                                ; Notice that edi will be equal to eax for first fragment
  340.         movzx   ebx, byte [edx + FRAGMENT_entry.Data + IPv4_Packet.VersionAndIHL]               ; Find header size (in ebx) of fragment
  341.         and     bx , 0x000F                                                                     ;
  342.         shl     bx , 2                                                                          ;
  343.  
  344.         lea     esi, [edx + FRAGMENT_entry.Data]                                                ; Set esi to the correct begin of fragment
  345.         movzx   ecx, word [edx + FRAGMENT_entry.Data + IPv4_Packet.TotalLength]                 ; Calculate total length of fragment
  346.         xchg    cl, ch                                                                          ;  intel byte order
  347.  
  348.         cmp     edi, eax                                                                        ; Is this packet the first fragment ?
  349.         je      .first_fragment
  350.         sub     cx, bx                                                                          ; If not, dont copy the header
  351.         add     esi, ebx                                                                        ;
  352.      .first_fragment:
  353.  
  354.         push    cx                                                                              ; First copy dword-wise, then byte-wise
  355.         shr     cx, 2                                                                           ;
  356.         rep     movsd                                                                           ;
  357.         pop     cx                                                                              ;
  358.         and     cx, 3                                                                           ;
  359.         rep     movsb                                                                           ;
  360.  
  361.         push    eax
  362.         push    edx                                                                             ; Push pointer to fragment onto stack
  363.         mov     edx, [edx + FRAGMENT_entry.NextPtr]                                             ; Set edx to the next pointer
  364.         call    kernel_free                                                                     ; free the previous fragment buffer (this uses the value from stack)
  365.         pop     eax
  366.         cmp     edx, -1                                                                         ; Check if it is last fragment in chain
  367.         jne     .rebuild_packet_loop
  368.  
  369.         pop     ecx                                                                             ;
  370.         xchg    cl, ch
  371.         mov     edx, eax
  372.         mov     word [edx + IPv4_Packet.TotalLength], cx
  373.         add     esp, 8
  374.  
  375.         xchg    cl, ch             ;  This prints the IP packet to the debug board (usefull when using serial output debug..)
  376.         push    ecx  ;;;;
  377.         push    eax     ;;;;
  378. ;        mov     esi, edx           ;
  379. ;                                   ;
  380. ;       @@:                         ;
  381. ;        lodsb                      ;
  382. ;        DEBUGF  1,"%x ", eax:2     ;
  383. ;        loop    @r                 ;
  384.  
  385.         movzx   eax, byte [edx + IPv4_Packet.VersionAndIHL]     ; Calculate Header length by using IHL field
  386.         and     ax, 0x000F                                      ;
  387.         shl     ax, 2                                           ;
  388.         sub     ecx, eax                                        ;
  389.         add     eax, edx
  390.         push    eax
  391.         mov     al , [edx + IPv4_Packet.Protocol]
  392.         pop     edx                                             ; Offset to data (tcp/udp/icmp/.. Packet)
  393.  
  394.         cmp     al , IP_PROTO_TCP
  395. ;        je      TCP_handler
  396.  
  397.         cmp     al , IP_PROTO_UDP
  398.         je      UDP_handler
  399.  
  400.         cmp     al , IP_PROTO_ICMP
  401.         je      ICMP_handler_fragments
  402.  
  403.         DEBUGF  1,"IP_Handler - unknown protocol:%u\n",al
  404.  
  405.         call    kernel_free
  406.         add     esp, 8                                          ; pop (balance stack)
  407.  
  408.         ret
  409.  
  410.  
  411.   .destroy_slot_pop:
  412.         add     esp, 4
  413.   .destroy_slot:
  414.         DEBUGF  1,"Destroy fragment slot!\n"
  415.         ; TODO!
  416.         jmp     .dump
  417.  
  418.  
  419.  
  420. ;-----------------------------------------------------------------
  421. ;
  422. ; find fragment slot
  423. ;
  424. ; IN: pointer to fragmented packet in edx         ; TODO: the RFC says we should check protocol too
  425. ; OUT: pointer to slot in edi, -1 on error
  426. ;
  427. ;-----------------------------------------------------------------
  428.  
  429.   .find_fragment_slot:
  430.  
  431.         push    eax ebx ecx edx
  432.         mov     ax , word [edx + IPv4_Packet.Identification]
  433.         mov     ecx, MAX_FRAGMENTS
  434.         mov     esi, FRAGMENT_LIST
  435.         mov     ebx, dword [edx + IPv4_Packet.SourceAddress]
  436.         mov     edx, dword [edx + IPv4_Packet.DestinationAddress]
  437.   .find_slot:
  438.         cmp     word [esi + FRAGMENT_slot.id], ax
  439.         jne     .try_next
  440.         cmp     dword [esi + FRAGMENT_slot.SrcIP], ebx
  441.         jne     .try_next
  442.         cmp     dword [esi + FRAGMENT_slot.DstIP], edx
  443.         je      .found_slot
  444.   .try_next:
  445.         add     esi, FRAGMENT_slot.size
  446.         loop    .find_slot
  447.  ;       pop     edx ebx
  448.         or      esi, -1
  449. ;        ret
  450.  
  451.   .found_slot:
  452.         pop     edx ecx ebx eax
  453.         ret
  454.  
  455.  
  456. ;-----------------------------------------------------------------
  457. ;
  458. ; Decrease TimeToLive of all fragment slots
  459. ;
  460. ; IN: /
  461. ; OUT: /
  462. ;
  463. ;-----------------------------------------------------------------
  464.  
  465. align 4
  466. IPv4_decrease_fragment_ttls:
  467.  
  468.         mov     esi, FRAGMENT_LIST
  469.         mov     ecx, MAX_FRAGMENTS
  470.   .loop:
  471.         cmp     [esi + FRAGMENT_slot.ttl], 0
  472.         je      .try_next
  473.         dec     [esi + FRAGMENT_slot.ttl]
  474.         jnz     .try_next
  475.         DEBUGF 1,"Fragment slot timed-out!\n"
  476.         ; TODO: clear all entry's of timed-out slot
  477.   .try_next:
  478.         add     esi, 4
  479.         loop    .loop
  480.         ret
  481.  
  482.  
  483.  
  484.  
  485.  
  486. ;-----------------------------------------------------------------
  487. ;
  488. ; Create_IPv4_Packet
  489. ;
  490. ; IN: eax = dest ip
  491. ;     ebx = source ip
  492. ;     ecx = data length
  493. ;     dx  = fragment id
  494. ;     di  = protocol
  495. ;
  496. ; OUT: eax points to buffer start
  497. ;      ebx is size of complete buffer
  498. ;      edi = pointer to start of data (-1 on error)
  499. ;      ecx = unchanged (packet size of embedded data)
  500. ;      edx = pointer to device struct (needed for sending procedure)
  501. ;      esi = pointer to sending procedure
  502. ;
  503. ;-----------------------------------------------------------------
  504.  
  505. ;;; TODO: create fragmented packets
  506.  
  507. align 4
  508. IPv4_create_packet:
  509.  
  510.         DEBUGF 1,"Create IPv4 Packet\n"
  511.  
  512.         cmp     ecx, 1514
  513.         jg      .exit_
  514.  
  515.         cmp     eax, -1
  516.         je      .broadcast                ; If it is broadcast, just send
  517.  
  518.         call    ARP_IP_to_MAC
  519.  
  520.         cmp     eax, -1
  521.         jne     .found
  522.  
  523.         DEBUGF 1,"Create IPv4 Packet - ARP entry not found!\n"
  524.  
  525.         ; TODO: QUEUE!
  526.         or      edi, -1
  527.  
  528.         ret
  529.  
  530.   .found:
  531.         push    ax
  532.         push    ebx
  533.  
  534.         jmp     .send
  535.  
  536.   .broadcast:
  537.         push    word -1
  538.         push    dword -1
  539.  
  540.  
  541.   .send:
  542.         push    ecx eax ebx dx di
  543.         call    IPv4_dest_to_dev
  544.         inc     [IP_PACKETS_TX+4*edi]
  545.         mov     edi, [ETH_DRV_LIST + 4*edi]
  546.         lea     eax, [edi + ETH_DEVICE.mac]
  547.         lea     ebx, [esp+16]
  548.         mov     ecx, [esp+12]
  549.         add     ecx, IPv4_Packet.DataOrOptional
  550.         mov     edx, edi ;;;
  551.         mov     di , ETHER_IPv4
  552.         call    ETH_create_Packet                  ; TODO: figure out a way to make this work with other protocols too
  553.         cmp     edi, -1
  554.         je      .exit
  555.  
  556.         mov     [edi + IPv4_Packet.VersionAndIHL], 0x45   ; IPv4, normal length (no Optional header)
  557.         mov     [edi + IPv4_Packet.TypeOfService], 0
  558.         xchg    ch, cl
  559.         mov     [edi + IPv4_Packet.TotalLength], cx
  560.         mov     [edi + IPv4_Packet.FlagsAndFragmentOffset], 0x0000
  561.         mov     [edi + IPv4_Packet.TimeToLive], 128
  562.         mov     [edi + IPv4_Packet.HeaderChecksum], 0
  563.         pop     cx
  564.         mov     [edi + IPv4_Packet.Protocol], cl
  565.         pop     cx
  566.         mov     [edi + IPv4_Packet.Identification], cx
  567.         pop     ecx
  568.         mov     [edi + IPv4_Packet.SourceAddress], ecx
  569.         pop     ecx
  570.         mov     [edi + IPv4_Packet.DestinationAddress], ecx
  571.  
  572.         push    eax
  573.         stdcall checksum_jb, edi, IPv4_Packet.DataOrOptional ; buf_ptr, buf_size
  574.         xchg    al, ah
  575.         mov     [edi + IPv4_Packet.HeaderChecksum], ax
  576.         pop     eax ecx
  577.         add     edi, IPv4_Packet.DataOrOptional
  578.  
  579.         DEBUGF 1,"IPv4 Packet for device %x created successfully\n", edx
  580.  
  581.         add     esp, 6
  582.  
  583.         ret
  584.  
  585.   .exit:
  586.         add     esp, 16+6
  587.   .exit_:
  588.         DEBUGF 1,"Create IPv4 Packet - failed\n"
  589.         or      edi, -1
  590.         ret
  591.  
  592.  
  593.  
  594. ;---------------------------------------------------------------------------
  595. ;
  596. ; IPv4_dest_to_dev
  597. ;
  598. ; IN: Destination IP in eax
  599. ; OUT: device id in edi
  600. ;
  601. ;---------------------------------------------------------------------------
  602.  
  603. align 4
  604. IPv4_dest_to_dev:
  605.  
  606.         DEBUGF 1,"IPv4 destination to device: "
  607.  
  608.         xor     edi, edi
  609.         mov     ecx, MAX_IP
  610.  
  611.   .loop:
  612.         mov     ebx, [IP_LIST+edi]              ; we dont need to worry about non exisiting ip interfaces
  613.         and     ebx, [SUBNET_LIST+edi]          ; they have IP and SUBNET set to all one's, so they will have no match except 255.255.255.255
  614.                                                 ; (only a moron would insert that ip into this function..)
  615.         mov     edx, eax
  616.         and     edx, [SUBNET_LIST+edi]
  617.  
  618.         cmp     ebx, edx
  619.         je      .found_it
  620.  
  621.         add     edi, 4
  622.         loop    .loop
  623.  
  624.         xor     edi, edi        ; if none found, use device 0 as default device
  625.  
  626.   .found_it:
  627.         shr     edi, 2
  628.  
  629.         DEBUGF 1,"%u\n",edi
  630.  
  631.         ret
  632.  
  633.  
  634.  
  635. ;---------------------------------------------------------------------------
  636. ;
  637. ; IPv4_get_frgmnt_num
  638. ;
  639. ; IN: /
  640. ; OUT: fragment number in ax
  641. ;
  642. ;---------------------------------------------------------------------------
  643.  
  644. align 4
  645. IPv4_get_frgmnt_num:
  646.         xor     ax, ax  ;;; TODO: replace this with real code
  647.  
  648.         ret
  649.  
  650.  
  651. ;---------------------------------------------------------------------------
  652. ;
  653. ; IPv4_API
  654. ;
  655. ; This function is called by system function 75
  656. ;
  657. ; IN:  subfunction number in bl
  658. ;      device number in bh
  659. ;      ecx, edx, .. depends on subfunction
  660. ;
  661. ; OUT:
  662. ;
  663. ;---------------------------------------------------------------------------
  664.  
  665. align 4
  666. IPv4_API:
  667.  
  668.         movzx   eax, bh
  669.         shl     eax, 2
  670.  
  671.         test    bl, bl
  672.         jz      .packets_tx     ; 0
  673.         dec     bl
  674.         jz      .packets_rx     ; 1
  675.         dec     bl
  676.         jz      .read_ip        ; 2
  677.         dec     bl
  678.         jz      .write_ip       ; 3
  679.         dec     bl
  680.         jz      .read_dns       ; 4
  681.         dec     bl
  682.         jz      .write_dns      ; 5
  683.         dec     bl
  684.         jz      .read_subnet    ; 6
  685.         dec     bl
  686.         jz      .write_subnet   ; 7
  687.         dec     bl
  688.         jz      .read_gateway   ; 8
  689.         dec     bl
  690.         jz      .write_gateway  ; 9
  691.  
  692. .error:
  693.         mov     eax, -1
  694.         ret
  695.  
  696. .packets_tx:
  697.         add     eax, IP_PACKETS_TX
  698.         mov     eax, [eax]
  699.         ret
  700.  
  701. .packets_rx:
  702.         add     eax, IP_PACKETS_RX
  703.         mov     eax, [eax]
  704.         ret
  705.  
  706. .read_ip:
  707.         add     eax, IP_LIST
  708.         mov     eax, [eax]
  709.         ret
  710.  
  711. .write_ip:
  712.         add     eax, IP_LIST
  713.         mov     [eax], ecx
  714.         xor     eax, eax
  715.         ret
  716.  
  717. .read_dns:
  718.         add     eax, DNS_LIST
  719.         mov     eax, [eax]
  720.         ret
  721.  
  722. .write_dns:
  723.         add     eax, DNS_LIST
  724.         mov     [eax], ecx
  725.         xor     eax, eax
  726.         ret
  727.  
  728. .read_subnet:
  729.         add     eax, SUBNET_LIST
  730.         mov     eax, [eax]
  731.         ret
  732.  
  733. .write_subnet:
  734.         add     eax, SUBNET_LIST
  735.         mov     [eax], ecx
  736.         xor     eax, eax
  737.         ret
  738.  
  739. .read_gateway:
  740.         add     eax, GATEWAY_LIST
  741.         mov     eax, [eax]
  742.         ret
  743.  
  744. .write_gateway:
  745.         add     eax, GATEWAY_LIST
  746.         mov     [eax], ecx
  747.         xor     eax, eax
  748.         ret
  749.  
  750.  
  751.  
  752.  
  753.