Subversion Repositories Kolibri OS

Rev

Rev 4259 | Rev 4850 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved.    ;;
  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.  
  19. $Revision: 3515 $
  20.  
  21. IPv4_MAX_FRAGMENTS              = 64
  22. IPv4_MAX_ROUTES                 = 64
  23.  
  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.  
  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.  
  45. struct  IPv4_FRAGMENT_slot
  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.  
  55. struct  IPv4_FRAGMENT_entry             ; This structure will replace the ethernet header in fragmented ip packets
  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.  
  64. struct  IPv4_ROUTE
  65.  
  66.         Destination             dd ?
  67.         Gateway                 dd ?
  68.         Flags                   dd ?
  69.         Use                     dd ?
  70.         Interface               dd ?
  71.  
  72. ends
  73.  
  74.  
  75. uglobal
  76. align 4
  77.  
  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
  83.  
  84.         IPv4_packets_tx         rd NET_DEVICES_MAX
  85.         IPv4_packets_rx         rd NET_DEVICES_MAX
  86.         IPv4_packets_dumped     rd NET_DEVICES_MAX
  87.  
  88.         IPv4_FRAGMENT_LIST      rb IPv4_MAX_FRAGMENTS * sizeof.IPv4_FRAGMENT_slot
  89.  
  90.         IPv4_ROUTES             rd IPv4_MAX_ROUTES * sizeof.IPv4_ROUTE
  91.  
  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
  106.         mov     ecx, 7*NET_DEVICES_MAX + (sizeof.IPv4_FRAGMENT_slot*IPv4_MAX_FRAGMENTS)/4
  107.         rep stosd
  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.  
  121.         mov     esi, IPv4_FRAGMENT_LIST
  122.         mov     ecx, IPv4_MAX_FRAGMENTS
  123.   .loop:
  124.         cmp     [esi + IPv4_FRAGMENT_slot.ttl], 0
  125.         je      .next
  126.         dec     [esi + IPv4_FRAGMENT_slot.ttl]
  127.         jz      .died
  128.   .next:
  129.         add     esi, sizeof.IPv4_FRAGMENT_slot
  130.         dec     ecx
  131.         jnz     .loop
  132.         jmp     .done
  133.  
  134.   .died:
  135.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4 Fragment slot timed-out!\n"
  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.  
  228.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input, packet from: %u.%u.%u.%u ",\
  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
  231.         DEBUGF  DEBUG_NETWORK_VERBOSE, "to: %u.%u.%u.%u\n",\
  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.  
  241.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Checksum ok\n"
  242.  
  243. ;-----------------------------------
  244. ; Check if destination IP is correct
  245.  
  246.         call    NET_ptr_to_num4
  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.  
  270.         ; or a loopback address (127.0.0.0/8)
  271.  
  272.         and     eax, 0x00ffffff
  273.         cmp     eax, 127
  274.         je      .ip_ok
  275.  
  276.         ; or it's just not meant for us.. :(
  277.  
  278.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Destination address does not match!\n"
  279.         jmp     .dump
  280.  
  281. ;------------------------
  282. ; Now we can update stats
  283.  
  284.   .ip_ok:
  285.         inc     [IPv4_packets_rx + edi]
  286.  
  287. ;----------------------------------
  288. ; Check if the packet is fragmented
  289.  
  290.         test    [edx + IPv4_header.FlagsAndFragmentOffset], 1 shl 5     ; Is 'more fragments' flag set ?
  291.         jnz     .has_fragments                                          ; If so, we definately have a fragmented packet
  292.  
  293.         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
  294.         jnz     .is_last_fragment
  295.  
  296. ;-------------------------------------------------------------------
  297. ; No, it's just a regular IP packet, pass it to the higher protocols
  298.  
  299.   .handle_it:                                                   ; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
  300.  
  301.         movzx   esi, [edx + IPv4_header.VersionAndIHL]          ; Calculate Header length by using IHL field
  302.         and     esi, 0x0000000f                                 ;
  303.         shl     esi, 2                                          ;
  304.  
  305.         movzx   ecx, [edx + IPv4_header.TotalLength]            ; Calculate length of encapsulated Packet
  306.         xchg    cl, ch                                          ;
  307.         sub     ecx, esi                                        ;
  308.  
  309.         lea     edi, [edx + IPv4_header.SourceAddress]          ; make edi ptr to source and dest IPv4 address
  310.         mov     al, [edx + IPv4_header.Protocol]
  311.         add     esi, edx                                        ; make esi ptr to data
  312.  
  313.         cmp     al, IP_PROTO_TCP
  314.         je      TCP_input
  315.  
  316.         cmp     al, IP_PROTO_UDP
  317.         je      UDP_input
  318.  
  319.         cmp     al, IP_PROTO_ICMP
  320.         je      ICMP_input
  321.  
  322.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: unknown protocol %u\n", al
  323.  
  324.   .dump:
  325.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: dumping\n"
  326.         inc     [IPv4_packets_dumped]                           ; FIXME: use correct interface
  327.         call    NET_packet_free
  328.         add     esp, 4                                          ; pop (balance stack)
  329.         ret
  330.  
  331.  
  332. ;---------------------------
  333. ; Fragmented packet handler
  334.  
  335.  
  336.   .has_fragments:
  337.         movzx   eax, [edx + IPv4_header.FlagsAndFragmentOffset]
  338.         xchg    al, ah
  339.         shl     ax, 3
  340.  
  341.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: fragmented packet offset=%u id=%x ptr=0x%x\n", ax, [edx + IPv4_header.Identification]:4, edx
  342.  
  343.         test    ax, ax                                          ; Is this the first packet of the fragment?
  344.         jz      .is_first_fragment
  345.  
  346.  
  347. ;-------------------------------------------------------
  348. ; We have a fragmented IP packet, but it's not the first
  349.  
  350.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Middle fragment packet received!\n"
  351.  
  352.         call    IPv4_find_fragment_slot
  353.         cmp     esi, -1
  354.         je      .dump
  355.  
  356.         mov     [esi + IPv4_FRAGMENT_slot.ttl], 15              ; Reset the ttl
  357.         mov     esi, [esi + IPv4_FRAGMENT_slot.ptr]
  358.         or      edi, -1
  359.   .find_last_entry:                                             ; The following routine will try to find the last entry
  360.         cmp     edi, [esi + IPv4_FRAGMENT_entry.PrevPtr]
  361.         jne     .destroy_slot                                   ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
  362.         mov     edi, esi
  363.         mov     esi, [esi + IPv4_FRAGMENT_entry.NextPtr]
  364.         cmp     esi, -1
  365.         jne     .find_last_entry
  366.                                                                 ; We found the last entry (pointer is now in edi)
  367.                                                                 ; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
  368.  
  369.         pop     eax                                             ; pointer to packet
  370.         mov     [edi + IPv4_FRAGMENT_entry.NextPtr], eax        ; update pointer of previous entry to the new entry
  371.         mov     [eax + IPv4_FRAGMENT_entry.NextPtr], -1
  372.         mov     [eax + IPv4_FRAGMENT_entry.PrevPtr], edi
  373.         mov     [eax + IPv4_FRAGMENT_entry.Owner], ebx
  374.  
  375.         add     esp, 4
  376.         ret
  377.  
  378.  
  379. ;------------------------------------
  380. ; We have received the first fragment
  381.  
  382.   .is_first_fragment:
  383.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: First fragment packet received!\n"
  384.                                                                 ; try to locate a free slot..
  385.         mov     ecx, IPv4_MAX_FRAGMENTS
  386.         mov     esi, IPv4_FRAGMENT_LIST
  387.   .find_free_slot:
  388.         cmp     word [esi + IPv4_FRAGMENT_slot.ttl], 0
  389.         je      .found_free_slot
  390.         add     esi, sizeof.IPv4_FRAGMENT_slot
  391.         loop    .find_free_slot
  392.         jmp     .dump                                           ; If no free slot was found, dump the packet
  393.  
  394.   .found_free_slot:                                             ; We found a free slot, let's fill in the FRAGMENT_slot structure
  395.         mov     [esi + IPv4_FRAGMENT_slot.ttl], 15              ; RFC recommends 15 secs as ttl
  396.         mov     ax, [edx + IPv4_header.Identification]
  397.         mov     [esi + IPv4_FRAGMENT_slot.id], ax
  398.         mov     eax, [edx + IPv4_header.SourceAddress]
  399.         mov     [esi + IPv4_FRAGMENT_slot.SrcIP], eax
  400.         mov     eax, [edx + IPv4_header.DestinationAddress]
  401.         mov     [esi + IPv4_FRAGMENT_slot.DstIP], eax
  402.         pop     eax
  403.         mov     [esi + IPv4_FRAGMENT_slot.ptr], eax
  404.                                                                 ; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
  405.         mov     [eax + IPv4_FRAGMENT_entry.NextPtr], -1
  406.         mov     [eax + IPv4_FRAGMENT_entry.PrevPtr], -1
  407.         mov     [eax + IPv4_FRAGMENT_entry.Owner], ebx
  408.  
  409.         add     esp, 4                                          ; balance stack and exit
  410.         ret
  411.  
  412.  
  413. ;-----------------------------------
  414. ; We have received the last fragment
  415.  
  416.   .is_last_fragment:
  417.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Last fragment packet received!\n"
  418.  
  419.         call    IPv4_find_fragment_slot
  420.         cmp     esi, -1
  421.         je      .dump
  422.  
  423.         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
  424.         push    esi
  425.         xor     eax, eax
  426.         or      edi, -1
  427.  
  428.   .count_bytes:
  429.         cmp     [esi + IPv4_FRAGMENT_entry.PrevPtr], edi
  430.         jne     .destroy_slot_pop                                                       ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
  431.         mov     cx, [esi + sizeof.IPv4_FRAGMENT_entry + IPv4_header.TotalLength]        ; Add total length
  432.         xchg    cl, ch
  433.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Packet size=%u\n", cx
  434.         add     ax, cx
  435.         movzx   cx, [esi + sizeof.IPv4_FRAGMENT_entry + IPv4_header.VersionAndIHL]      ; Sub Header length
  436.         and     cx, 0x000F
  437.         shl     cx, 2
  438.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Header size=%u\n", cx
  439.         sub     ax, cx
  440.         mov     edi, esi
  441.         mov     esi, [esi + IPv4_FRAGMENT_entry.NextPtr]
  442.         cmp     esi, -1
  443.         jne     .count_bytes
  444.  
  445.         mov     esi, [esp+4]
  446.         mov     [edi + IPv4_FRAGMENT_entry.NextPtr], esi                                ; Add this packet to the chain, this simplifies the following code
  447.         mov     [esi + IPv4_FRAGMENT_entry.NextPtr], -1
  448.         mov     [esi + IPv4_FRAGMENT_entry.PrevPtr], edi
  449.         mov     [esi + IPv4_FRAGMENT_entry.Owner], ebx
  450.  
  451.         mov     cx, [edx + IPv4_header.TotalLength]                                     ; Note: This time we dont substract Header length
  452.         xchg    cl, ch
  453.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Packet size=%u\n", cx
  454.         add     ax, cx
  455.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Total Received data size=%u\n", eax
  456.  
  457.         push    eax
  458.         mov     ax, [edx + IPv4_header.FlagsAndFragmentOffset]
  459.         xchg    al, ah
  460.         shl     ax, 3
  461.         add     cx, ax
  462.         pop     eax
  463.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Total Fragment size=%u\n", ecx
  464.  
  465.         cmp     ax, cx
  466.         jne     .destroy_slot_pop
  467.  
  468.         push    eax
  469.         push    eax
  470.         call    kernel_alloc
  471.         test    eax, eax
  472.         je      .destroy_slot_pop                                                       ; If we dont have enough space to allocate the buffer, discard all packets in slot
  473.         mov     edx, [esp+4]                                                            ; Get pointer to first fragment entry back in edx
  474.  
  475.   .rebuild_packet_loop:
  476.         movzx   ecx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.FlagsAndFragmentOffset] ; Calculate the fragment offset
  477.         xchg    cl, ch                                                                  ;  intel byte order
  478.         shl     cx, 3                                                                   ;   multiply by 8 and clear first 3 bits
  479.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Fragment offset=%u\n", cx
  480.  
  481.         lea     edi, [eax + ecx]                                                        ; Notice that edi will be equal to eax for first fragment
  482.         movzx   ebx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.VersionAndIHL]     ; Find header size (in ebx) of fragment
  483.         and     bx, 0x000F                                                              ;
  484.         shl     bx, 2                                                                   ;
  485.  
  486.         lea     esi, [edx + sizeof.IPv4_FRAGMENT_entry]                                 ; Set esi to the correct begin of fragment
  487.         movzx   ecx, [edx + sizeof.IPv4_FRAGMENT_entry + IPv4_header.TotalLength]       ; Calculate total length of fragment
  488.         xchg    cl, ch                                                                  ;  intel byte order
  489.  
  490.         cmp     edi, eax                                                                ; Is this packet the first fragment ?
  491.         je      .first_fragment
  492.         sub     cx, bx                                                                  ; If not, dont copy the header
  493.         add     esi, ebx                                                                ;
  494.   .first_fragment:
  495.  
  496.  
  497.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Copying %u bytes from 0x%x to 0x%x\n", ecx, esi, edi
  498.         push    cx                                                                      ; First copy dword-wise, then byte-wise
  499.         shr     cx, 2                                                                   ;
  500.         rep movsd                                                                       ;
  501.         pop     cx                                                                      ;
  502.         and     cx, 3                                                                   ;
  503.         rep movsb                                                                       ;
  504.  
  505.         push    eax
  506.         push    [edx + IPv4_FRAGMENT_entry.Owner]                                       ; we need to remeber the owner, in case this is the last packet
  507.         push    [edx + IPv4_FRAGMENT_entry.NextPtr]                                     ; Set edx to the next pointer
  508.         push    edx                                                                     ; Push pointer to fragment onto stack
  509.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Next Fragment: 0x%x\n", edx
  510.         call    NET_packet_free                                                         ; free the previous fragment buffer (this uses the value from stack)
  511.         pop     edx ebx eax
  512.         cmp     edx, -1                                                                 ; Check if it is last fragment in chain
  513.         jne     .rebuild_packet_loop
  514.  
  515.         pop     ecx
  516.         xchg    cl, ch
  517.         mov     edx, eax
  518.         mov     [edx + IPv4_header.TotalLength], cx
  519.         add     esp, 12
  520.         xchg    cl, ch
  521.         push    ecx edx                 ; size and pointer
  522.         jmp     .handle_it              ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr
  523.  
  524.   .destroy_slot_pop:
  525.         add     esp, 4
  526.   .destroy_slot:
  527.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_input: Destroy fragment slot!\n"
  528.         ; TODO!
  529.         jmp     .dump
  530.  
  531.  
  532.  
  533.  
  534.  
  535. ;-----------------------------------------------------------------
  536. ;
  537. ; find fragment slot
  538. ;
  539. ; IN: pointer to fragmented packet in edx
  540. ; OUT: pointer to slot in esi, -1 on error
  541. ;
  542. ;-----------------------------------------------------------------
  543. align 4
  544. IPv4_find_fragment_slot:
  545.  
  546. ;;; TODO: the RFC says we should check protocol number too
  547.  
  548.         push    eax ebx ecx edx
  549.         mov     ax, [edx + IPv4_header.Identification]
  550.         mov     ecx, IPv4_MAX_FRAGMENTS
  551.         mov     esi, IPv4_FRAGMENT_LIST
  552.         mov     ebx, [edx + IPv4_header.SourceAddress]
  553.         mov     edx, [edx + IPv4_header.DestinationAddress]
  554.   .find_slot:
  555.         cmp     [esi + IPv4_FRAGMENT_slot.id], ax
  556.         jne     .try_next
  557.         cmp     [esi + IPv4_FRAGMENT_slot.SrcIP], ebx
  558.         jne     .try_next
  559.         cmp     [esi + IPv4_FRAGMENT_slot.DstIP], edx
  560.         je      .found_slot
  561.   .try_next:
  562.         add     esi, sizeof.IPv4_FRAGMENT_slot
  563.         loop    .find_slot
  564.  
  565.         or      esi, -1
  566.   .found_slot:
  567.         pop     edx ecx ebx eax
  568.         ret
  569.  
  570.  
  571. ;------------------------------------------------------------------
  572. ;
  573. ; IPv4_output
  574. ;
  575. ; IN:   eax = Destination IP
  576. ;       ecx = data length
  577. ;       edx = Source IP
  578. ;       di  = TTL shl 8 + protocol
  579. ;
  580. ; OUT:  eax = pointer to buffer start
  581. ;       ebx = pointer to device struct (needed for sending procedure)
  582. ;       ecx = unchanged (packet size of embedded data)
  583. ;       edx = size of complete buffer
  584. ;       edi = pointer to start of data (0 on error)
  585. ;
  586. ;------------------------------------------------------------------
  587. align 4
  588. IPv4_output:
  589.  
  590.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_output: size=%u ip=0x%x\n", ecx, eax
  591.  
  592.         cmp     ecx, 65500              ; Max IPv4 packet size
  593.         ja      .too_large
  594.  
  595.         push    ecx di eax
  596.         call    IPv4_route              ; outputs device number in edi, dest ip in eax, source IP in edx
  597.         push    edx
  598.  
  599.         test    edi, edi
  600.         jz      .loopback
  601.  
  602.         call    ARP_IP_to_MAC
  603.         test    eax, 0xffff0000         ; error bits
  604.         jnz     .arp_error
  605.         push    ebx                     ; push the mac onto the stack
  606.         push    ax
  607.  
  608.         inc     [IPv4_packets_tx + edi]   ; update stats
  609.  
  610.         mov     ebx, [NET_DRV_LIST + edi]
  611.         lea     eax, [ebx + ETH_DEVICE.mac]
  612.         mov     edx, esp
  613.         mov     ecx, [esp + 6 + 8 + 2]
  614.         add     ecx, sizeof.IPv4_header
  615.         mov     di, ETHER_PROTO_IPv4
  616.         call    ETH_output
  617.         jz      .eth_error
  618.         add     esp, 6                  ; pop the mac out of the stack
  619.  
  620.   .continue:
  621.         xchg    cl, ch                                  ; internet byte order
  622.         mov     [edi + IPv4_header.VersionAndIHL], 0x45 ; IPv4, normal length (no Optional header)
  623.         mov     [edi + IPv4_header.TypeOfService], 0    ; nothing special, just plain ip packet
  624.         mov     [edi + IPv4_header.TotalLength], cx
  625.         mov     [edi + IPv4_header.Identification], 0   ; fragment id: FIXME
  626.         mov     [edi + IPv4_header.FlagsAndFragmentOffset], 0
  627.  
  628.         mov     [edi + IPv4_header.HeaderChecksum], 0
  629.         popd    [edi + IPv4_header.SourceAddress]
  630.         popd    [edi + IPv4_header.DestinationAddress]
  631.  
  632.         pop     word[edi + IPv4_header.TimeToLive]      ; ttl shl 8 + protocol
  633. ;               [edi + IPv4_header.Protocol]
  634.  
  635.         pop     ecx
  636.  
  637.         IPv4_checksum edi
  638.         add     edi, sizeof.IPv4_header
  639.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_output: success!\n"
  640.         ret
  641.  
  642.   .eth_error:
  643.         DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output: ethernet error\n"
  644.         add     esp, 3*4+2+6
  645.         xor     edi, edi
  646.         ret
  647.  
  648.   .arp_error:
  649.         DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output: ARP error=%x\n", eax
  650.         add     esp, 3*4+2
  651.         xor     edi, edi
  652.         ret
  653.  
  654.   .too_large:
  655.         DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output: Packet too large!\n"
  656.         xor     edi, edi
  657.         ret
  658.  
  659.   .loopback:
  660.         mov     dword [esp + 2], eax            ; change source IP to dest IP
  661.         mov     ecx, [esp + 10]
  662.         add     ecx, sizeof.IPv4_header
  663.         mov     edi, AF_INET4
  664.         call    LOOP_output
  665.         jmp     .continue
  666.  
  667.  
  668.  
  669.  
  670. ;------------------------------------------------------------------
  671. ;
  672. ; IPv4_output_raw
  673. ;
  674. ; IN: eax = socket ptr
  675. ;     ecx = data length
  676. ;     esi = data ptr
  677. ;
  678. ; OUT: /
  679. ;
  680. ;------------------------------------------------------------------
  681. align 4
  682. IPv4_output_raw:
  683.  
  684.         DEBUGF 1,"IPv4_output_raw: size=%u ptr=%x socket=%x\n", ecx, esi, eax
  685.  
  686.         cmp     ecx, 1480               ;;;;; FIXME
  687.         ja      .too_large
  688.  
  689.         sub     esp, 8
  690.         push    esi eax
  691.  
  692.         call    IPv4_route
  693.         call    ARP_IP_to_MAC
  694.  
  695.         test    eax, 0xffff0000         ; error bits
  696.         jnz     .arp_error
  697.  
  698.         push    ebx                     ; push the mac
  699.         push    ax
  700.  
  701.         inc     [IPv4_packets_tx + 4*edi]
  702.         mov     ebx, [NET_DRV_LIST + 4*edi]
  703.         lea     eax, [ebx + ETH_DEVICE.mac]
  704.         mov     edx, esp
  705.         mov     ecx, [esp + 6 + 4]
  706.         add     ecx, sizeof.IPv4_header
  707.         mov     di, ETHER_PROTO_IPv4
  708.         call    ETH_output
  709.         jz      .error
  710.  
  711.         add     esp, 6  ; pop the mac
  712.  
  713.         mov     dword[esp+4+4], edx
  714.         mov     dword[esp+4+4+4], eax
  715.  
  716.         pop     eax esi
  717. ;; todo: check socket options if we should add header, or just compute checksum
  718.  
  719.         push    edi ecx
  720.         rep movsb
  721.         pop     ecx edi
  722.  
  723. ;        [edi + IPv4_header.VersionAndIHL]              ; IPv4, normal length (no Optional header)
  724. ;        [edi + IPv4_header.TypeOfService]              ; nothing special, just plain ip packet
  725. ;        [edi + IPv4_header.TotalLength]
  726. ;        [edi + IPv4_header.TotalLength]                ; internet byte order
  727. ;        [edi + IPv4_header.FlagsAndFragmentOffset]
  728.  
  729.         mov     [edi + IPv4_header.HeaderChecksum], 0
  730.  
  731. ;        [edi + IPv4_header.TimeToLive]                 ; ttl shl 8 + protocol
  732. ;        [edi + IPv4_header.Protocol]
  733. ;        [edi + IPv4_header.Identification]             ; fragment id
  734. ;        [edi + IPv4_header.SourceAddress]
  735. ;        [edi + IPv4_header.DestinationAddress]
  736.  
  737.         IPv4_checksum edi                       ;;;; todo: checksum for IP packet with options!
  738.         add     edi, sizeof.IPv4_header
  739.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_output_raw: device=%x\n", ebx
  740.         call    [ebx + NET_DEVICE.transmit]
  741.         ret
  742.  
  743.   .error:
  744.         add     esp, 6
  745.   .arp_error:
  746.         add     esp, 8+4+4
  747.   .too_large:
  748.         DEBUGF  DEBUG_NETWORK_ERROR, "IPv4_output_raw: Failed\n"
  749.         sub     edi, edi
  750.         ret
  751.  
  752.  
  753. ;--------------------------------------------------------
  754. ;
  755. ;
  756. ; IN: dword [esp] = pointer to buffer containing ipv4 packet to be fragmented
  757. ;     dword [esp+4] = buffer size
  758. ;     esi = pointer to ip header in that buffer
  759. ;     ecx = max size of fragments
  760. ;
  761. ; OUT: /
  762. ;
  763. ;--------------------------------------------------------
  764.  
  765. align 4
  766. IPv4_fragment:
  767.  
  768.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_fragment\n"
  769.  
  770.         and     ecx, not 111b   ; align 4
  771.  
  772.         cmp     ecx, sizeof.IPv4_header + 8     ; must be able to put at least 8 bytes
  773.         jb      .err2
  774.  
  775.         push    esi ecx
  776.         mov     eax, [esi + IPv4_header.DestinationAddress]
  777.         call    ARP_IP_to_MAC
  778.         pop     ecx esi
  779.         cmp     eax, -1
  780.         jz      .err2
  781.  
  782.         push    ebx
  783.         push    ax
  784.  
  785.         mov     ebx, [NET_DRV_LIST]
  786.         lea     eax, [ebx + ETH_DEVICE.mac]
  787.         push    eax
  788.  
  789.  
  790.         push    esi                             ; ptr to ip header
  791.         sub     ecx, sizeof.IPv4_header         ; substract header size
  792.         push    ecx                             ; max data size
  793.         push    dword 0                         ; offset
  794.  
  795.   .new_fragment:
  796.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: new fragment"
  797.  
  798.  
  799.         mov     eax, [esp + 3*4]
  800.         lea     ebx, [esp + 4*4]
  801.         mov     di , ETHER_PROTO_IPv4
  802.         call    ETH_output
  803.  
  804.         cmp     edi, -1
  805.         jz      .err
  806.  
  807. ; copy header
  808.         mov     esi, [esp + 2*4]
  809.         mov     ecx, 5  ; 5 dwords: TODO: use IHL field of the header!
  810.         rep movsd
  811.  
  812. ; copy data
  813.         mov     esi, [esp + 2*4]
  814.         add     esi, sizeof.IPv4_header
  815.         add     esi, [esp]      ; offset
  816.  
  817.         mov     ecx, [esp + 1*4]
  818.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_fragment: copying %u bytes\n", ecx
  819.         rep movsb
  820.  
  821. ; now, correct header
  822.         mov     ecx, [esp + 1*4]
  823.         add     ecx, sizeof.IPv4_header
  824.         xchg    cl, ch
  825.         mov     [edi + IPv4_header.TotalLength], cx
  826.  
  827.         mov     ecx, [esp]              ; offset
  828.         xchg    cl, ch
  829.  
  830. ;        cmp     dword[esp + 4*4], 0     ; last fragment?;<<<<<<
  831. ;        je      .last_fragment
  832.         or      cx, 1 shl 2             ; more fragments
  833. ;  .last_fragment:
  834.         mov     [edi + IPv4_header.FlagsAndFragmentOffset], cx
  835.  
  836.         mov     [edi + IPv4_header.HeaderChecksum], 0
  837.  
  838.         ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<< send the packet
  839.         mov     ecx, [esp + 1*4]
  840.  
  841.         push    edx eax
  842.         IPv4_checksum edi
  843.  
  844.         call    [ebx + NET_DEVICE.transmit]
  845.         ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  846.  
  847.         mov     ecx, [esp+4]
  848.         add     [esp], ecx
  849.  
  850.         mov     ecx, [esp+3*4+6+4]      ; ptr to begin of buff
  851.         add     ecx, [esp+3*4+6+4+4]    ; buff size
  852.         sub     ecx, [esp+2*4]          ; ptr to ip header
  853.         add     ecx, [esp]              ; offset
  854.  
  855.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: %u bytes remaining\n", ecx
  856.  
  857.         cmp     ecx, [esp+1*4]
  858.         jae     .new_fragment
  859.  
  860.         mov     [esp+4], ecx            ; set fragment size to remaining packet size
  861.         jmp     .new_fragment
  862.  
  863.       .err:
  864.         DEBUGF  DEBUG_NETWORK_ERROR, "Ipv4_fragment: failed\n"
  865.       .done:
  866.         add     esp, 12 + 4 + 6
  867.       .err2:
  868.         DEBUGF  DEBUG_NETWORK_VERBOSE, "Ipv4_fragment: dumping\n"
  869.         call    NET_packet_free
  870.         add     esp, 4
  871.  
  872.         ret
  873.  
  874.  
  875.  
  876. ;---------------------------------------------------------------------------
  877. ;
  878. ; IPv4_route
  879. ;
  880. ; IN:   eax = Destination IP
  881. ;       edx = Source IP
  882. ; OUT:  eax = Destination IP (or gateway IP)
  883. ;       edx = Source IP
  884. ;       edi = device number*4
  885. ; DESTROYED:
  886. ;       ecx
  887. ;
  888. ;---------------------------------------------------------------------------
  889. align 4
  890. IPv4_route:     ; TODO: return error if no valid route found
  891.  
  892.         cmp     eax, 0xffffffff
  893.         je      .broadcast
  894.  
  895.         xor     edi, edi
  896.   .loop:
  897.         mov     ebx, [IP_LIST + edi]
  898.         and     ebx, [SUBNET_LIST + edi]
  899.         jz      .next
  900.         mov     ecx, eax
  901.         and     ecx, [SUBNET_LIST + edi]
  902.         cmp     ebx, ecx
  903.         je      .got_it
  904.   .next:
  905.         add     edi, 4
  906.         cmp     edi, 4*NET_DEVICES_MAX
  907.         jb      .loop
  908.  
  909.         mov     eax, [GATEWAY_LIST + 4]         ; TODO: let user (or a user space daemon) configure default route
  910.   .broadcast:
  911.         mov     edi, 4                          ; TODO: same as above
  912.   .got_it:
  913.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv4_route: %u\n", edi
  914.         test    edx, edx
  915.         jnz     @f
  916.         mov     edx, [IP_LIST + edi]
  917.   @@:
  918.  
  919.         ret
  920.  
  921.  
  922.  
  923. ;---------------------------------------------------------------------------
  924. ;
  925. ; IPv4_get_frgmnt_num
  926. ;
  927. ; IN: /
  928. ; OUT: fragment number in ax
  929. ;
  930. ;---------------------------------------------------------------------------
  931. align 4
  932. IPv4_get_frgmnt_num:
  933.         xor     ax, ax  ;;; TODO: replace this with real code
  934.  
  935.         ret
  936.  
  937.  
  938. ;-----------------------------------------------------------------
  939. ;
  940. ; IPv4_connect
  941. ;
  942. ;   IN: eax = socket pointer
  943. ;  OUT: eax = 0 ok / -1 error
  944. ;       ebx = error code
  945. ;
  946. ;-------------------------
  947. align 4
  948. IPv4_connect:
  949.  
  950.         push    eax edx
  951.         lea     ecx, [eax + SOCKET.mutex]
  952.         call    mutex_lock
  953.         pop     edx eax
  954.  
  955. ; Fill in local IP
  956.         cmp     [eax + IP_SOCKET.LocalIP], 0
  957.         jne     @f
  958.         push    [IP_LIST + 4]                                   ; FIXME: use correct local IP
  959.         pop     [eax + IP_SOCKET.LocalIP]
  960.  
  961. ; Fill in remote IP
  962.         pushd   [edx + 4]
  963.         pop     [eax + IP_SOCKET.RemoteIP]
  964.  
  965. ; Set up data receiving queue
  966.         push    eax
  967.         init_queue (eax + SOCKET_QUEUE_LOCATION)
  968.         pop     eax
  969.  
  970.         lea     ecx, [eax + SOCKET.mutex]
  971.         call    mutex_unlock
  972.  
  973.         xor     eax, eax
  974.         ret
  975.  
  976.  
  977. ;---------------------------------------------------------------------------
  978. ;
  979. ; IPv4_API
  980. ;
  981. ; This function is called by system function 75
  982. ;
  983. ; IN:  subfunction number in bl
  984. ;      device number in bh
  985. ;      ecx, edx, .. depends on subfunction
  986. ;
  987. ; OUT:
  988. ;
  989. ;---------------------------------------------------------------------------
  990. align 4
  991. IPv4_api:
  992.  
  993.         movzx   eax, bh
  994.         shl     eax, 2
  995.  
  996.         and     ebx, 0x000000ff
  997.         cmp     ebx, .number
  998.         ja      .error
  999.         jmp     dword [.table + 4*ebx]
  1000.  
  1001.   .table:
  1002.         dd      .packets_tx     ; 0
  1003.         dd      .packets_rx     ; 1
  1004.         dd      .read_ip        ; 2
  1005.         dd      .write_ip       ; 3
  1006.         dd      .read_dns       ; 4
  1007.         dd      .write_dns      ; 5
  1008.         dd      .read_subnet    ; 6
  1009.         dd      .write_subnet   ; 7
  1010.         dd      .read_gateway   ; 8
  1011.         dd      .write_gateway  ; 9
  1012.   .number = ($ - .table) / 4 - 1
  1013.  
  1014.   .error:
  1015.         mov     eax, -1
  1016.         ret
  1017.  
  1018.   .packets_tx:
  1019.         mov     eax, [IPv4_packets_tx + eax]
  1020.         ret
  1021.  
  1022.   .packets_rx:
  1023.         mov     eax, [IPv4_packets_rx + eax]
  1024.         ret
  1025.  
  1026.   .read_ip:
  1027.         mov     eax, [IP_LIST + eax]
  1028.         ret
  1029.  
  1030.   .write_ip:
  1031.         mov     [IP_LIST + eax], ecx
  1032.         mov     edi, eax                        ; device number, we'll need it for ARP
  1033.  
  1034.         ; pre-calculate the local broadcast address
  1035.         mov     ebx, [SUBNET_LIST + eax]
  1036.         not     ebx
  1037.         or      ebx, ecx
  1038.         mov     [BROADCAST_LIST + eax], ebx
  1039.  
  1040.         mov     ebx, [NET_DRV_LIST + eax]
  1041.         mov     eax, [IP_LIST + eax]
  1042.         call    ARP_output_request              ; now send a gratuitous ARP
  1043.  
  1044.         call    NET_send_event
  1045.         xor     eax, eax
  1046.         ret
  1047.  
  1048.   .read_dns:
  1049.         mov     eax, [DNS_LIST + eax]
  1050.         ret
  1051.  
  1052.   .write_dns:
  1053.         mov     [DNS_LIST + eax], ecx
  1054.         call    NET_send_event
  1055.         xor     eax, eax
  1056.         ret
  1057.  
  1058.   .read_subnet:
  1059.         mov     eax, [SUBNET_LIST + eax]
  1060.         ret
  1061.  
  1062.   .write_subnet:
  1063.         mov     [SUBNET_LIST + eax], ecx
  1064.  
  1065.         ; pre-calculate the local broadcast address
  1066.         mov     ebx, [IP_LIST + eax]
  1067.         not     ecx
  1068.         or      ecx, ebx
  1069.         mov     [BROADCAST_LIST + eax], ecx
  1070.  
  1071.         call    NET_send_event
  1072.         xor     eax, eax
  1073.         ret
  1074.  
  1075.   .read_gateway:
  1076.         mov     eax, [GATEWAY_LIST + eax]
  1077.         ret
  1078.  
  1079.   .write_gateway:
  1080.         mov     [GATEWAY_LIST + eax], ecx
  1081.  
  1082.         call    NET_send_event
  1083.         xor     eax, eax
  1084.         ret