Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. 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: 2914 $
  20.  
  21. MAX_FRAGMENTS                   = 64
  22. MAX_IP                          = MAX_NET_DEVICES
  23. IP_MAX_INTERFACES               = MAX_IP
  24.  
  25. struct  IPv4_header
  26.  
  27.         VersionAndIHL           db ?    ; Version[0-3 bits] and IHL(header length)[4-7 bits]
  28.         TypeOfService           db ?    ; precedence [7-5] minimize delay [4], maximize throughput [3], maximize riliability [2] minimize momentary cost [1] and zero [0]
  29.         TotalLength             dw ?
  30.         Identification          dw ?
  31.         FlagsAndFragmentOffset  dw ?    ; Flags[0-2] and FragmentOffset[3-15]
  32.         TimeToLive              db ?    ;
  33.         Protocol                db ?
  34.         HeaderChecksum          dw ?
  35.         SourceAddress           dd ?
  36.         DestinationAddress      dd ?
  37.  
  38. ends
  39.  
  40. struct  FRAGMENT_slot
  41.  
  42.         ttl                     dw ?    ; Time to live for this entry, 0 for empty slot's
  43.         id                      dw ?    ; Identification field from IP header
  44.         SrcIP                   dd ?    ; .. from IP header
  45.         DstIP                   dd ?    ; .. from IP header
  46.         ptr                     dd ?    ; Pointer to first packet
  47.  
  48. ends
  49.  
  50. struct  FRAGMENT_entry                  ; This structure will replace the ethernet header in fragmented ip packets
  51.  
  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         ;;; FIXME
  56.                                         ; Ip header begins here (we will need the IP header to re-construct the complete packet)
  57. ends
  58.  
  59.  
  60. align 4
  61. uglobal
  62.  
  63.         IP_LIST         rd MAX_IP
  64.         SUBNET_LIST     rd MAX_IP
  65.         DNS_LIST        rd MAX_IP
  66.         GATEWAY_LIST    rd MAX_IP
  67.         BROADCAST_LIST  rd MAX_IP
  68.  
  69.         IP_PACKETS_TX   rd MAX_IP
  70.         IP_PACKETS_RX   rd MAX_IP
  71.  
  72.         FRAGMENT_LIST   rb MAX_FRAGMENTS * sizeof.FRAGMENT_slot
  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*MAX_IP + (sizeof.FRAGMENT_slot*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, FRAGMENT_LIST
  103.         mov     ecx, MAX_FRAGMENTS
  104.   .loop:
  105.         cmp     [esi + FRAGMENT_slot.ttl], 0
  106.         je      .next
  107.         dec     [esi + FRAGMENT_slot.ttl]
  108.         jz      .died
  109.   .next:
  110.         add     esi, sizeof.FRAGMENT_slot
  111.         dec     ecx
  112.         jnz     .loop
  113.         jmp     .done
  114.  
  115.   .died:
  116.         DEBUGF 1,"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  1,"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  1,"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  1,"IPv4_input: Checksum ok\n"
  223.  
  224. ;-----------------------------------
  225. ; Check if destination IP is correct
  226.  
  227.         call    NET_ptr_to_num
  228.         shl     edi, 2
  229.  
  230.         ; check if it matches local ip
  231.  
  232.         mov     eax, [edx + IPv4_header.DestinationAddress]
  233.         cmp     eax, [IP_LIST+edi]
  234.         je      .ip_ok
  235.  
  236.         ; check for broadcast (IP or (not SUBNET))
  237.  
  238.         cmp     eax, [BROADCAST_LIST+edi]
  239.         je      .ip_ok
  240.  
  241.         ; or a special broadcast (255.255.255.255)
  242.  
  243.         cmp     eax, 0xffffffff
  244.         je      .ip_ok
  245.  
  246.         ; maybe it's a multicast (224.0.0.0/4)
  247.  
  248.         and     eax, 0x0fffffff
  249.         cmp     eax, 224
  250.         je      .ip_ok
  251.  
  252.         ; or a loopback address (127.0.0.0/8)
  253.  
  254.         and     eax, 0x00ffffff
  255.         cmp     eax, 127
  256.         je      .ip_ok
  257.  
  258.         ; or it's just not meant for us.. :(
  259.  
  260.         DEBUGF  2,"IPv4_input: Destination address does not match!\n"
  261.         jmp     .dump
  262.  
  263. ;------------------------
  264. ; Now we can update stats
  265.  
  266.   .ip_ok:
  267.         inc     [IP_PACKETS_RX+edi]
  268.  
  269. ;----------------------------------
  270. ; Check if the packet is fragmented
  271.  
  272.         test    [edx + IPv4_header.FlagsAndFragmentOffset], 1 shl 5     ; Is 'more fragments' flag set ?
  273.         jnz     .has_fragments                                          ; If so, we definately have a fragmented packet
  274.  
  275.         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
  276.         jnz     .is_last_fragment
  277.  
  278. ;-------------------------------------------------------------------
  279. ; No, it's just a regular IP packet, pass it to the higher protocols
  280.  
  281.   .handle_it:                                                   ; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
  282.  
  283.         movzx   esi, [edx + IPv4_header.VersionAndIHL]          ; Calculate Header length by using IHL field
  284.         and     esi, 0x0000000f                                 ;
  285.         shl     esi, 2                                          ;
  286.  
  287.         movzx   ecx, [edx + IPv4_header.TotalLength]            ; Calculate length of encapsulated Packet
  288.         xchg    cl, ch                                          ;
  289.         sub     ecx, esi                                        ;
  290.  
  291.         lea     edi, [edx + IPv4_header.SourceAddress]          ; make edi ptr to source and dest IPv4 address
  292.         mov     al, [edx + IPv4_header.Protocol]
  293.         add     esi, edx                                        ; make esi ptr to data
  294.  
  295.         cmp     al, IP_PROTO_TCP
  296.         je      TCP_input
  297.  
  298.         cmp     al, IP_PROTO_UDP
  299.         je      UDP_input
  300.  
  301.         cmp     al, IP_PROTO_ICMP
  302.         je      ICMP_input
  303.  
  304.         DEBUGF  2,"IPv4_input: unknown protocol %u\n", al
  305.  
  306.   .dump:
  307.         DEBUGF  2,"IPv4_input: dumping\n"
  308. ;        inc     [dumped_rx_count]                              ;;; TODO
  309.         call    kernel_free
  310.         add     esp, 4                                          ; pop (balance stack)
  311.         ret
  312.  
  313.  
  314. ;---------------------------
  315. ; Fragmented packet handler
  316.  
  317.  
  318.   .has_fragments:
  319.         movzx   eax, [edx + IPv4_header.FlagsAndFragmentOffset]
  320.         xchg    al , ah
  321.         shl     ax , 3
  322.  
  323.         DEBUGF  1,"IPv4_input: fragmented packet offset=%u id=%x\n", ax, [edx + IPv4_header.Identification]:4
  324.  
  325.         test    ax , ax                                         ; Is this the first packet of the fragment?
  326.         jz      .is_first_fragment
  327.  
  328.  
  329. ;-------------------------------------------------------
  330. ; We have a fragmented IP packet, but it's not the first
  331.  
  332.         DEBUGF  1,"IPv4_input: Middle fragment packet received!\n"
  333.  
  334.         call    IPv4_find_fragment_slot
  335.         cmp     esi, -1
  336.         je      .dump
  337.  
  338.         mov     [esi + FRAGMENT_slot.ttl], 15                   ; Reset the ttl
  339.         mov     esi, [esi + FRAGMENT_slot.ptr]
  340.         or      edi, -1
  341.   .find_last_entry:                                             ; The following routine will try to find the last entry
  342.         cmp     edi, [esi + FRAGMENT_entry.PrevPtr]
  343.         jne     .destroy_slot                                   ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
  344.         mov     edi, esi
  345.         mov     esi, [esi + FRAGMENT_entry.NextPtr]
  346.         cmp     esi, -1
  347.         jne     .find_last_entry
  348.                                                                 ; We found the last entry (pointer is now in edi)
  349.                                                                 ; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
  350.  
  351.         pop     eax                                             ; pointer to packet
  352.         mov     [edi + FRAGMENT_entry.NextPtr], eax             ; update pointer of previous entry to the new entry
  353.         mov     [eax + FRAGMENT_entry.NextPtr], -1
  354.         mov     [eax + FRAGMENT_entry.PrevPtr], edi
  355.         mov     [eax + FRAGMENT_entry.Owner], ebx
  356.  
  357.         add     esp, 4
  358.         ret
  359.  
  360.  
  361. ;------------------------------------
  362. ; We have received the first fragment
  363.  
  364.   .is_first_fragment:
  365.         DEBUGF  1,"IPv4_input: First fragment packet received!\n"
  366.                                                                 ; try to locate a free slot..
  367.         mov     ecx, MAX_FRAGMENTS
  368.         mov     esi, FRAGMENT_LIST
  369.   .find_free_slot:
  370.         cmp     word [esi + FRAGMENT_slot.ttl], 0
  371.         je      .found_free_slot
  372.         add     esi, sizeof.FRAGMENT_slot
  373.         loop    .find_free_slot
  374.         jmp     .dump                                           ; If no free slot was found, dump the packet
  375.  
  376.   .found_free_slot:                                             ; We found a free slot, let's fill in the FRAGMENT_slot structure
  377.         mov     [esi + FRAGMENT_slot.ttl], 15                   ; RFC recommends 15 secs as ttl
  378.         mov     ax , [edx + IPv4_header.Identification]
  379.         mov     [esi + FRAGMENT_slot.id], ax
  380.         mov     eax,[edx + IPv4_header.SourceAddress]
  381.         mov     [esi + FRAGMENT_slot.SrcIP], eax
  382.         mov     eax, [edx + IPv4_header.DestinationAddress]
  383.         mov     [esi + FRAGMENT_slot.DstIP], eax
  384.         pop     eax
  385.         mov     [esi + FRAGMENT_slot.ptr], eax
  386.                                                                 ; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
  387.         mov     [eax + FRAGMENT_entry.NextPtr], -1
  388.         mov     [eax + FRAGMENT_entry.PrevPtr], -1
  389.         mov     [eax + FRAGMENT_entry.Owner], ebx
  390.  
  391.         add     esp, 4                                          ; balance stack and exit
  392.         ret
  393.  
  394.  
  395. ;-----------------------------------
  396. ; We have received the last fragment
  397.  
  398.   .is_last_fragment:
  399.         DEBUGF  1,"IPv4_input: Last fragment packet received!\n"
  400.  
  401.         call    IPv4_find_fragment_slot
  402.         cmp     esi, -1
  403.         je      .dump
  404.  
  405.         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
  406.         push    esi
  407.         xor     eax, eax
  408.         or      edi, -1
  409.  
  410.   .count_bytes:
  411.         cmp     [esi + FRAGMENT_entry.PrevPtr], edi
  412.         jne     .destroy_slot_pop                                               ; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
  413.         mov     cx, [esi + sizeof.FRAGMENT_entry + IPv4_header.TotalLength]       ; Add total length
  414.         xchg    cl, ch
  415.         DEBUGF  1,"IPv4_input: Packet size=%u\n", cx
  416.         add     ax, cx
  417.         movzx   cx, [esi + sizeof.FRAGMENT_entry + IPv4_header.VersionAndIHL]     ; Sub Header length
  418.         and     cx, 0x000F
  419.         shl     cx, 2
  420.         DEBUGF  1,"IPv4_input: Header size=%u\n", cx
  421.         sub     ax, cx
  422.         mov     edi, esi
  423.         mov     esi, [esi + FRAGMENT_entry.NextPtr]
  424.         cmp     esi, -1
  425.         jne     .count_bytes
  426.  
  427.         mov     esi, [esp+4]
  428.         mov     [edi + FRAGMENT_entry.NextPtr], esi                            ; Add this packet to the chain, this simplifies the following code
  429.         mov     [esi + FRAGMENT_entry.NextPtr], -1
  430.         mov     [esi + FRAGMENT_entry.PrevPtr], edi
  431.         mov     [esi + FRAGMENT_entry.Owner], ebx
  432.  
  433.         mov     cx, [edx + IPv4_header.TotalLength]                            ; Note: This time we dont substract Header length
  434.         xchg    cl , ch
  435.         DEBUGF  1,"IPv4_input: Packet size=%u\n", cx
  436.         add     ax , cx
  437.         DEBUGF  1,"IPv4_input: Total Received data size=%u\n", eax
  438.  
  439.         push    eax
  440.         mov     ax , [edx + IPv4_header.FlagsAndFragmentOffset]
  441.         xchg    al , ah
  442.         shl     ax , 3
  443.         add     cx , ax
  444.         pop     eax
  445.         DEBUGF  1,"IPv4_input: Total Fragment size=%u\n", ecx
  446.  
  447.         cmp     ax, cx
  448.         jne     .destroy_slot_pop
  449.  
  450.         push    eax
  451.         push    eax
  452.         call    kernel_alloc
  453.         test    eax, eax
  454.         je      .destroy_slot_pop                                                       ; If we dont have enough space to allocate the buffer, discard all packets in slot
  455.         mov     edx, [esp+4]                                                            ; Get pointer to first fragment entry back in edx
  456.  
  457.   .rebuild_packet_loop:
  458.         movzx   ecx, [edx + sizeof.FRAGMENT_entry + IPv4_header.FlagsAndFragmentOffset]   ; Calculate the fragment offset
  459.         xchg    cl , ch                                                                 ;  intel byte order
  460.         shl     cx , 3                                                                  ;   multiply by 8 and clear first 3 bits
  461.         DEBUGF  1,"IPv4_input: Fragment offset=%u\n", cx
  462.  
  463.         lea     edi, [eax + ecx]                                                        ; Notice that edi will be equal to eax for first fragment
  464.         movzx   ebx, [edx + sizeof.FRAGMENT_entry + IPv4_header.VersionAndIHL]            ; Find header size (in ebx) of fragment
  465.         and     bx , 0x000F                                                             ;
  466.         shl     bx , 2                                                                  ;
  467.  
  468.         lea     esi, [edx + sizeof.FRAGMENT_entry]                                        ; Set esi to the correct begin of fragment
  469.         movzx   ecx, [edx + sizeof.FRAGMENT_entry + IPv4_header.TotalLength]              ; Calculate total length of fragment
  470.         xchg    cl, ch                                                                  ;  intel byte order
  471.  
  472.         cmp     edi, eax                                                                ; Is this packet the first fragment ?
  473.         je      .first_fragment
  474.         sub     cx, bx                                                                  ; If not, dont copy the header
  475.         add     esi, ebx                                                                ;
  476.   .first_fragment:
  477.  
  478.         push    cx                                                                      ; First copy dword-wise, then byte-wise
  479.         shr     cx, 2                                                                   ;
  480.         rep     movsd                                                                   ;
  481.         pop     cx                                                                      ;
  482.         and     cx, 3                                                                   ;
  483.         rep     movsb                                                                   ;
  484.  
  485.         push    eax
  486.         push    edx                                                                     ; Push pointer to fragment onto stack
  487.         mov     ebx, [edx + FRAGMENT_entry.Owner]                                       ; we need to remeber the owner, in case this is the last packet
  488.         mov     edx, [edx + FRAGMENT_entry.NextPtr]                                     ; Set edx to the next pointer
  489.         call    kernel_free                                                             ; free the previous fragment buffer (this uses the value from stack)
  490.         pop     eax
  491.         cmp     edx, -1                                                                 ; Check if it is last fragment in chain
  492.         jne     .rebuild_packet_loop
  493.  
  494.         pop     ecx
  495.         xchg    cl, ch
  496.         mov     edx, eax
  497.         mov     [edx + IPv4_header.TotalLength], cx
  498.         add     esp, 8
  499.         xchg    cl, ch
  500.         push    ecx
  501.  
  502.         push    eax
  503.         jmp     .handle_it          ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr
  504.  
  505.   .destroy_slot_pop:
  506.         add     esp, 4
  507.   .destroy_slot:
  508.         DEBUGF  1,"IPv4_input: Destroy fragment slot!\n"
  509.         ; TODO!
  510.         jmp     .dump
  511.  
  512.  
  513.  
  514.  
  515.  
  516. ;-----------------------------------------------------------------
  517. ;
  518. ; find fragment slot
  519. ;
  520. ; IN: pointer to fragmented packet in edx
  521. ; OUT: pointer to slot in esi, -1 on error
  522. ;
  523. ;-----------------------------------------------------------------
  524. align 4
  525. IPv4_find_fragment_slot:
  526.  
  527. ;;; TODO: the RFC says we should check protocol number too
  528.  
  529.         push    eax ebx ecx edx
  530.         mov     ax , [edx + IPv4_header.Identification]
  531.         mov     ecx, MAX_FRAGMENTS
  532.         mov     esi, FRAGMENT_LIST
  533.         mov     ebx, [edx + IPv4_header.SourceAddress]
  534.         mov     edx, [edx + IPv4_header.DestinationAddress]
  535.   .find_slot:
  536.         cmp     [esi + FRAGMENT_slot.id], ax
  537.         jne     .try_next
  538.         cmp     [esi + FRAGMENT_slot.SrcIP], ebx
  539.         jne     .try_next
  540.         cmp     [esi + FRAGMENT_slot.DstIP], edx
  541.         je      .found_slot
  542.   .try_next:
  543.         add     esi, sizeof.FRAGMENT_slot
  544.         loop    .find_slot
  545. ;        pop     edx ebx
  546.         or      esi, -1
  547. ;        ret
  548.  
  549.   .found_slot:
  550.         pop     edx ecx ebx eax
  551.         ret
  552.  
  553.  
  554. ;------------------------------------------------------------------
  555. ;
  556. ; IPv4_output
  557. ;
  558. ; IN: eax = dest ip
  559. ;     ebx = output device ptr/0 for automatic choice
  560. ;     ecx = data length
  561. ;     edx = source ip
  562. ;     di  = TTL shl 8 + protocol
  563. ;
  564. ; OUT: eax = pointer to buffer start
  565. ;      ebx = pointer to device struct (needed for sending procedure)
  566. ;      ecx = unchanged (packet size of embedded data)
  567. ;      edx = size of complete buffer
  568. ;      edi = pointer to start of data (0 on error)
  569. ;
  570. ;------------------------------------------------------------------
  571. align 4
  572. IPv4_output:
  573.  
  574.         DEBUGF  1,"IPv4_output: size=%u\n", ecx
  575.  
  576.         cmp     ecx, 65500              ; Max IPv4 packet size
  577.         ja      .too_large
  578.  
  579.         push    ecx eax edx di
  580.  
  581.         cmp     eax, 1 shl 24 + 127
  582.         je      .loopback
  583.  
  584.         call    IPv4_dest_to_dev        ; outputs device number in edi, dest ip in eax
  585.         call    ARP_IP_to_MAC
  586.         test    eax, 0xffff0000         ; error bits
  587.         jnz     .arp_error
  588.         push    ebx                     ; push the mac onto the stack
  589.         push    ax
  590.  
  591.         inc     [IP_PACKETS_TX + edi]   ; update stats
  592.  
  593.         mov     ebx, [NET_DRV_LIST + edi]
  594.         lea     eax, [ebx + ETH_DEVICE.mac]
  595.         mov     edx, esp
  596.         mov     ecx, [esp + 10 + 6]
  597.         add     ecx, sizeof.IPv4_header
  598.         mov     di, ETHER_IPv4
  599.         call    ETH_output
  600.         jz      .eth_error
  601.         add     esp, 6                  ; pop the mac out of the stack
  602.  
  603.   .continue:
  604.         xchg    cl, ch                                  ; internet byte order
  605.         mov     [edi + IPv4_header.VersionAndIHL], 0x45 ; IPv4, normal length (no Optional header)
  606.         mov     [edi + IPv4_header.TypeOfService], 0    ; nothing special, just plain ip packet
  607.         mov     [edi + IPv4_header.TotalLength], cx
  608.         mov     [edi + IPv4_header.Identification], 0   ; fragment id: FIXME
  609.         mov     [edi + IPv4_header.FlagsAndFragmentOffset], 0
  610.         pop     word [edi + IPv4_header.TimeToLive]     ; ttl shl 8 + protocol
  611. ;               [edi + IPv4_header.Protocol]
  612.         mov     [edi + IPv4_header.HeaderChecksum], 0
  613.         popd    [edi + IPv4_header.SourceAddress]
  614.         popd    [edi + IPv4_header.DestinationAddress]
  615.  
  616.         pop     ecx
  617.  
  618.         IPv4_checksum edi
  619.         add     edi, sizeof.IPv4_header
  620.         DEBUGF  1,"IPv4_output: success!\n"
  621.         ret
  622.  
  623.   .eth_error:
  624.         DEBUGF  1,"IPv4_output: ethernet error\n"
  625.         add     esp, 3*4+2+6
  626.         xor     edi, edi
  627.         ret
  628.  
  629.   .arp_error:
  630.         DEBUGF  1,"IPv4_output: ARP error=%x\n", eax
  631.         add     esp, 3*4+2
  632.         xor     edi, edi
  633.         ret
  634.  
  635.   .too_large:
  636.         DEBUGF  1,"IPv4_output: Packet too large!\n"
  637.         xor     edi, edi
  638.         ret
  639.  
  640.   .loopback:
  641.         mov     dword [esp + 2], eax
  642.         add     ecx, sizeof.IPv4_header
  643.         mov     di, ETHER_IPv4
  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_dest_to_dev
  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     [IP_PACKETS_TX + edi]
  682.         mov     ebx, [NET_DRV_LIST + 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_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  1,"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  1,"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 1,"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 1,"Ipv4_fragment: new fragment"
  777.  
  778.  
  779.         mov     eax, [esp + 3*4]
  780.         lea     ebx, [esp + 4*4]
  781.         mov     di , ETHER_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 1,"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 1,"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 1,"Ipv4_fragment: failed\n"
  845.       .done:
  846.         add     esp, 12 + 4 + 6
  847.       .err2:
  848.         DEBUGF 1,"Ipv4_fragment: dumping\n"
  849.         call    kernel_free
  850.         add     esp, 4
  851.  
  852.         ret
  853.  
  854.  
  855.  
  856. ;---------------------------------------------------------------------------
  857. ;
  858. ; IPv4_dest_to_dev
  859. ;
  860. ; IN:   eax = Destination IP
  861. ; OUT:  edi = device id * 4
  862. ;       eax = ip of gateway if nescessary, unchanged otherwise
  863. ;
  864. ;---------------------------------------------------------------------------
  865. align 4
  866. IPv4_dest_to_dev:
  867.  
  868.         cmp     eax, 0xffffffff
  869.         je      .broadcast
  870.  
  871.         xor     edi, edi
  872.         mov     ecx, MAX_IP
  873.   .loop:
  874.         mov     ebx, [IP_LIST+edi]
  875.         and     ebx, [SUBNET_LIST+edi]
  876.         jz      .next
  877.         mov     edx, eax
  878.         and     edx, [SUBNET_LIST+edi]
  879.  
  880.         cmp     ebx, edx
  881.         je      .found_it
  882.   .next:
  883.         add     edi, 4
  884.         dec     ecx
  885.         jnz     .loop
  886.  
  887.   .invalid:
  888.         xor     edi, edi                        ; if none found, use device 0 as default
  889.         mov     eax, [GATEWAY_LIST]
  890.  
  891.   .found_it:
  892.         DEBUGF  1,"IPv4_dest_to_dev: %u\n", edi
  893.         ret
  894.  
  895.   .broadcast:
  896.         xor     edi, edi
  897.         ret
  898.  
  899.  
  900.  
  901. ;---------------------------------------------------------------------------
  902. ;
  903. ; IPv4_get_frgmnt_num
  904. ;
  905. ; IN: /
  906. ; OUT: fragment number in ax
  907. ;
  908. ;---------------------------------------------------------------------------
  909. align 4
  910. IPv4_get_frgmnt_num:
  911.         xor     ax, ax  ;;; TODO: replace this with real code
  912.  
  913.         ret
  914.  
  915.  
  916. ;---------------------------------------------------------------------------
  917. ;
  918. ; IPv4_API
  919. ;
  920. ; This function is called by system function 75
  921. ;
  922. ; IN:  subfunction number in bl
  923. ;      device number in bh
  924. ;      ecx, edx, .. depends on subfunction
  925. ;
  926. ; OUT:
  927. ;
  928. ;---------------------------------------------------------------------------
  929. align 4
  930. IPv4_api:
  931.  
  932.         movzx   eax, bh
  933.         shl     eax, 2
  934.  
  935.         and     ebx, 0x000000ff
  936.         cmp     ebx, .number
  937.         ja      .error
  938.         jmp     dword [.table + 4*ebx]
  939.  
  940.   .table:
  941.         dd      .packets_tx     ; 0
  942.         dd      .packets_rx     ; 1
  943.         dd      .read_ip        ; 2
  944.         dd      .write_ip       ; 3
  945.         dd      .read_dns       ; 4
  946.         dd      .write_dns      ; 5
  947.         dd      .read_subnet    ; 6
  948.         dd      .write_subnet   ; 7
  949.         dd      .read_gateway   ; 8
  950.         dd      .write_gateway  ; 9
  951.   .number = ($ - .table) / 4 - 1
  952.  
  953.   .error:
  954.         mov     eax, -1
  955.         ret
  956.  
  957.   .packets_tx:
  958.         mov     eax, [IP_PACKETS_TX + eax]
  959.         ret
  960.  
  961.   .packets_rx:
  962.         mov     eax, [IP_PACKETS_RX + eax]
  963.         ret
  964.  
  965.   .read_ip:
  966.         mov     eax, [IP_LIST + eax]
  967.         ret
  968.  
  969.   .write_ip:
  970.         mov     [IP_LIST + eax], ecx
  971.  
  972.         ; pre-calculate the local broadcast address
  973.         mov     ebx, [SUBNET_LIST + eax]
  974.         not     ebx
  975.         or      ecx, ebx
  976.         mov     [BROADCAST_LIST + eax], ecx
  977.  
  978.         xor     eax, eax
  979.         ret
  980.  
  981.   .read_dns:
  982.         mov     eax, [DNS_LIST + eax]
  983.         ret
  984.  
  985.   .write_dns:
  986.         mov     [DNS_LIST + eax], ecx
  987.         xor     eax, eax
  988.         ret
  989.  
  990.   .read_subnet:
  991.         mov     eax, [SUBNET_LIST + eax]
  992.         ret
  993.  
  994.   .write_subnet:
  995.         mov     [SUBNET_LIST + eax], ecx
  996.  
  997.         ; pre-calculate the local broadcast address
  998.         mov     ebx, [IP_LIST + eax]
  999.         not     ecx
  1000.         or      ecx, ebx
  1001.         mov     [BROADCAST_LIST + eax], ecx
  1002.  
  1003.         xor     eax, eax
  1004.         ret
  1005.  
  1006.   .read_gateway:
  1007.         mov     eax, [GATEWAY_LIST + eax]
  1008.         ret
  1009.  
  1010.   .write_gateway:
  1011.         mov     [GATEWAY_LIST + eax], ecx
  1012.         xor     eax, eax
  1013.         ret