Subversion Repositories Kolibri OS

Rev

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