Subversion Repositories Kolibri OS

Rev

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

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