Subversion Repositories Kolibri OS

Rev

Rev 3643 | Rev 3698 | Go to most recent revision | Blame | Compare with Previous | 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. ;;  UDP.INC                                                        ;;
  7. ;;                                                                 ;;
  8. ;;  Part of the tcp/ip network stack for KolibriOS                 ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 2995 $
  18.  
  19.  
  20. struct  UDP_header
  21.  
  22.         SourcePort              dw  ?
  23.         DestinationPort         dw  ?
  24.         Length                  dw  ?  ; Length of (UDP Header + Data)
  25.         Checksum                dw  ?
  26.  
  27. ends
  28.  
  29.  
  30. align 4
  31. uglobal
  32.         UDP_PACKETS_TX          rd  NET_DEVICES_MAX
  33.         UDP_PACKETS_RX          rd  NET_DEVICES_MAX
  34. endg
  35.  
  36.  
  37. ;-----------------------------------------------------------------
  38. ;
  39. ; UDP_init
  40. ;
  41. ;  This function resets all UDP variables
  42. ;
  43. ;-----------------------------------------------------------------
  44. macro   UDP_init {
  45.  
  46.         xor     eax, eax
  47.         mov     edi, UDP_PACKETS_TX
  48.         mov     ecx, 2*NET_DEVICES_MAX
  49.         rep     stosd
  50. }
  51.  
  52.  
  53. macro   UDP_checksum    IP1, IP2  { ; esi = ptr to udp packet, ecx = packet size, destroys: ecx, edx
  54.  
  55. ; Pseudoheader
  56.         mov     edx, IP_PROTO_UDP
  57.  
  58.         add     dl, [IP1+1]
  59.         adc     dh, [IP1+0]
  60.         adc     dl, [IP1+3]
  61.         adc     dh, [IP1+2]
  62.  
  63.         adc     dl, [IP2+1]
  64.         adc     dh, [IP2+0]
  65.         adc     dl, [IP2+3]
  66.         adc     dh, [IP2+2]
  67.  
  68.         adc     dl, cl ; byte[esi+UDP_header.Length+1]
  69.         adc     dh, ch ; byte[esi+UDP_header.Length+0]
  70.  
  71. ; Done with pseudoheader, now do real header
  72.         adc     dl, byte[esi+UDP_header.SourcePort+1]
  73.         adc     dh, byte[esi+UDP_header.SourcePort+0]
  74.  
  75.         adc     dl, byte[esi+UDP_header.DestinationPort+1]
  76.         adc     dh, byte[esi+UDP_header.DestinationPort+0]
  77.  
  78.         adc     dl, byte[esi+UDP_header.Length+1]
  79.         adc     dh, byte[esi+UDP_header.Length+0]
  80.  
  81.         adc     edx, 0
  82.  
  83. ; Done with header, now do data
  84.         push    esi
  85.         movzx   ecx, [esi+UDP_header.Length]
  86.         rol     cx , 8
  87.         sub     cx , sizeof.UDP_header
  88.         add     esi, sizeof.UDP_header
  89.  
  90.         call    checksum_1
  91.         call    checksum_2
  92.         pop     esi
  93.  
  94.         add     [esi+UDP_header.Checksum], dx   ; this final instruction will set or clear ZF :)
  95.  
  96. }
  97.  
  98.  
  99. ;-----------------------------------------------------------------
  100. ;
  101. ; UDP_input:
  102. ;
  103. ;  Called by IPv4_input,
  104. ;  this procedure will inject the udp data diagrams in the application sockets.
  105. ;
  106. ;  IN:   [esp]  = Pointer to buffer
  107. ;       [esp+4] = size of buffer
  108. ;       ebx = ptr to device struct
  109. ;       ecx = UDP Packet size
  110. ;       esi = ptr to UDP header
  111. ;       edi = ptr to ipv4 source and dest address
  112. ;
  113. ;  OUT: /
  114. ;
  115. ;-----------------------------------------------------------------
  116. align 4
  117. UDP_input:
  118.  
  119.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_input: size=%u\n", ecx
  120.  
  121.         ; First validate, checksum
  122.  
  123.         neg     [esi + UDP_header.Checksum]     ; substract checksum from 0
  124.         jz      .no_checksum                    ; if checksum is zero, it is considered valid
  125.  
  126.         ; otherwise, we will re-calculate the checksum and add it to this value, thus creating 0 when it is correct
  127.  
  128.         UDP_checksum (edi), (edi+4)
  129.         jnz     .checksum_mismatch
  130.  
  131.   .no_checksum:
  132.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_input: checksum ok\n"
  133.  
  134.         ; Convert length to little endian
  135.  
  136.         rol     [esi + UDP_header.Length], 8
  137.  
  138.         ; Look for a socket where
  139.         ; IP Packet UDP Destination Port = local Port
  140.         ; IP Packet SA = Remote IP
  141.  
  142.         pusha
  143.         mov     ecx, socket_mutex
  144.         call    mutex_lock
  145.         popa
  146.  
  147.         mov     cx, [esi + UDP_header.SourcePort]
  148.         mov     dx, [esi + UDP_header.DestinationPort]
  149.         mov     edi, [edi + 4]                          ; ipv4 source address
  150.         mov     eax, net_sockets
  151.  
  152.   .next_socket:
  153.         mov     eax, [eax + SOCKET.NextPtr]
  154.         or      eax, eax
  155.         jz      .dump_
  156.  
  157.         cmp     [eax + SOCKET.Domain], AF_INET4
  158.         jne     .next_socket
  159.  
  160.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  161.         jne     .next_socket
  162.  
  163.         cmp     [eax + UDP_SOCKET.LocalPort], dx
  164.         jne     .next_socket
  165.  
  166.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_input: socket=%x\n", eax
  167.  
  168.         pusha
  169.         mov     ecx, socket_mutex
  170.         call    mutex_unlock
  171.         popa
  172.  
  173.         ;;; TODO: when packet is processed, check more sockets!
  174.  
  175. ;        cmp     [eax + IP_SOCKET.RemoteIP], 0xffffffff
  176. ;        je      @f
  177. ;        cmp     [eax + IP_SOCKET.RemoteIP], edi
  178. ;        jne     .next_socket
  179. ;       @@:
  180. ;
  181. ; FIXME: UDP should check remote IP, but not under all circumstances!
  182.  
  183.         cmp     [eax + UDP_SOCKET.firstpacket], 0
  184.         je      .updateport
  185.  
  186.         cmp     [eax + UDP_SOCKET.RemotePort], cx
  187.         jne     .dump
  188.  
  189.         pusha
  190.         lea     ecx, [eax + SOCKET.mutex]
  191.         call    mutex_lock
  192.         popa
  193.  
  194.   .updatesock:
  195.         call    NET_ptr_to_num4
  196.         inc     [UDP_PACKETS_RX + edi]
  197.  
  198.         movzx   ecx, [esi + UDP_header.Length]
  199.         sub     ecx, sizeof.UDP_header
  200.         add     esi, sizeof.UDP_header
  201.  
  202.         jmp     SOCKET_input
  203.  
  204.   .updateport:
  205.         pusha
  206.         lea     ecx, [eax + SOCKET.mutex]
  207.         call    mutex_lock
  208.         popa
  209.  
  210.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_input: new remote port=%x\n", cx ; FIXME: find a way to print big endian values with debugf
  211.         mov     [eax + UDP_SOCKET.RemotePort], cx
  212.         inc     [eax + UDP_SOCKET.firstpacket]
  213.  
  214.         jmp     .updatesock
  215.  
  216.   .dump_:
  217.  
  218.         pusha
  219.         mov     ecx, socket_mutex
  220.         call    mutex_unlock
  221.         popa
  222.  
  223.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_input: no socket found\n"
  224.  
  225.         jmp     .dump
  226.  
  227.   .checksum_mismatch:
  228.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_input: checksum mismatch\n"
  229.  
  230.   .dump:
  231.         call    kernel_free
  232.         add     esp, 4 ; pop (balance stack)
  233.         DEBUGF  DEBUG_NETWORK_VERBOSE,"UDP_input: dumping\n"
  234.  
  235.         ret
  236.  
  237.  
  238.  
  239.  
  240. ;-----------------------------------------------------------------
  241. ;
  242. ; UDP_output
  243. ;
  244. ; IN: eax = socket pointer
  245. ;     ecx = number of bytes to send
  246. ;     esi = pointer to data
  247. ;
  248. ;-----------------------------------------------------------------
  249.  
  250. align 4
  251. UDP_output:
  252.  
  253.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi
  254.  
  255.         mov     dx, [eax + UDP_SOCKET.RemotePort]
  256.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_output: remote port=%x, ", dx    ; FIXME: find a way to print big endian values with debugf
  257.         rol     edx, 16
  258.         mov     dx, [eax + UDP_SOCKET.LocalPort]
  259.         DEBUGF  DEBUG_NETWORK_VERBOSE, "local port=%x\n", dx
  260.  
  261.         sub     esp, 8                                          ; Data ptr and data size will be placed here
  262.         push    edx esi
  263.         mov     ebx, [eax + SOCKET.device]
  264.         mov     edx, [eax + IP_SOCKET.LocalIP]
  265.         mov     eax, [eax + IP_SOCKET.RemoteIP]
  266.         mov     di, IP_PROTO_UDP shl 8 + 128
  267.         add     ecx, sizeof.UDP_header
  268.         call    IPv4_output
  269.         jz      .fail
  270.         mov     [esp + 8], eax                                  ; pointer to buffer start
  271.         mov     [esp + 8 + 4], edx                              ; buffer size
  272.  
  273.         mov     [edi + UDP_header.Length], cx
  274.         rol     [edi + UDP_header.Length], 8
  275.  
  276.         pop     esi
  277.         push    edi ecx
  278.         sub     ecx, sizeof.UDP_header
  279.         add     edi, sizeof.UDP_header
  280.         shr     ecx, 2
  281.         rep     movsd
  282.         mov     ecx, [esp]
  283.         and     ecx, 3
  284.         rep     movsb
  285.         pop     ecx edi
  286.  
  287.         pop     dword [edi + UDP_header.SourcePort]
  288.  
  289. ; Checksum
  290.         mov     esi, edi
  291.         mov     [edi + UDP_header.Checksum], 0
  292.         UDP_checksum (edi-4), (edi-8)                           ; FIXME: IPv4 packet could have options..
  293.  
  294.         DEBUGF  DEBUG_NETWORK_VERBOSE, "UDP_output: sending with device %x\n", ebx
  295.         call    [ebx + NET_DEVICE.transmit]
  296.         test    eax, eax
  297.         jnz     @f
  298.         call    NET_ptr_to_num4
  299.         inc     [UDP_PACKETS_TX + edi]
  300.        @@:
  301.  
  302.         ret
  303.  
  304.   .fail:
  305.         DEBUGF  DEBUG_NETWORK_ERROR, "UDP_output: failed\n"
  306.         add     esp, 4+4+8
  307.         or      eax, -1
  308.         ret
  309.  
  310.  
  311.  
  312. ;---------------------------------------------------------------------------
  313. ;
  314. ; UDP_API
  315. ;
  316. ; This function is called by system function 75
  317. ;
  318. ; IN:  subfunction number in bl
  319. ;      device number in bh
  320. ;      ecx, edx, .. depends on subfunction
  321. ;
  322. ; OUT:
  323. ;
  324. ;---------------------------------------------------------------------------
  325.  
  326. align 4
  327. UDP_api:
  328.  
  329.         movzx   eax, bh
  330.         shl     eax, 2
  331.  
  332.         test    bl, bl
  333.         jz      .packets_tx     ; 0
  334.         dec     bl
  335.         jz      .packets_rx     ; 1
  336.  
  337.   .error:
  338.         mov     eax, -1
  339.         ret
  340.  
  341.   .packets_tx:
  342.         mov     eax, [UDP_PACKETS_TX + eax]
  343.         ret
  344.  
  345.   .packets_rx:
  346.         mov     eax, [UDP_PACKETS_RX + eax]
  347.         ret
  348.