Subversion Repositories Kolibri OS

Rev

Rev 2311 | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2010. 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: 2366 $
  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  MAX_IP
  33.         UDP_PACKETS_RX          rd  MAX_IP
  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*MAX_IP
  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  1,"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 and we continue processing
  125.                                                 ; otherwise, we will re-calculate the checksum and add it to this value, thus creating 0 when it is correct
  126.  
  127.         push    esi
  128.         UDP_checksum (edi), (edi+4)
  129.         pop     edx
  130.         jnz     .checksum_mismatch
  131.  
  132.   .no_checksum:
  133.         DEBUGF  1,"UDP Checksum is correct\n"
  134.  
  135.         ; Convert port numbers to intel format
  136.  
  137.         rol     [edx + UDP_header.DestinationPort], 8
  138.         rol     [edx + UDP_header.SourcePort], 8
  139.         rol     [edx + UDP_header.Length], 8
  140.  
  141.         ; Look for a socket where
  142.         ; IP Packet UDP Destination Port = local Port
  143.         ; IP Packet SA = Remote IP
  144.  
  145.         mov     si, [edx + UDP_header.DestinationPort]
  146.         mov     cx, [edx + UDP_header.SourcePort]
  147.         mov     edi, [edi + 4]                          ; ipv4 source address
  148.         mov     eax, net_sockets
  149.  
  150.   .next_socket:
  151.         mov     eax, [eax + SOCKET.NextPtr]
  152.         or      eax, eax
  153.         jz      .dump
  154.  
  155.         cmp     [eax + SOCKET.Domain], AF_INET4
  156.         jne     .next_socket
  157.  
  158.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  159.         jne     .next_socket
  160.  
  161.         cmp     [eax + UDP_SOCKET.LocalPort], si
  162.         jne     .next_socket
  163.  
  164.         DEBUGF  1,"using socket: %x\n", eax
  165.  
  166.         ;;; TODO: when packet is processed, check more sockets!
  167.  
  168. ;        cmp     [eax + IP_SOCKET.RemoteIP], 0xffffffff
  169. ;        je      @f
  170. ;        cmp     [eax + IP_SOCKET.RemoteIP], edi
  171. ;        jne     .next_socket
  172. ;       @@:
  173. ;
  174. ; FIXME: UDP should check remote IP, but not under all circumstances!
  175.  
  176.         cmp     [eax + UDP_SOCKET.firstpacket], 0
  177.         je      .updateport
  178.  
  179.         cmp     [eax + UDP_SOCKET.RemotePort], cx
  180.         jne     .dump
  181.  
  182.         push    ebx
  183.         lea     ebx, [eax + SOCKET.lock]
  184.         call    wait_mutex
  185.         pop     ebx
  186.  
  187.   .updatesock:
  188.         inc     [UDP_PACKETS_RX]
  189.         DEBUGF  1,"Found valid UDP packet for socket %x\n", eax
  190.         lea     esi, [edx + sizeof.UDP_header]
  191.         movzx   ecx, [edx + UDP_header.Length]
  192.         sub     ecx, sizeof.UDP_header
  193.  
  194.         jmp     SOCKET_input
  195.  
  196.   .updateport:
  197.         push    ebx
  198.         lea     ebx, [eax + SOCKET.lock]
  199.         call    wait_mutex
  200.         pop     ebx
  201.  
  202.         DEBUGF  1,"Changing remote port to: %u\n", cx
  203.         mov     [eax + UDP_SOCKET.RemotePort], cx
  204.         inc     [eax + UDP_SOCKET.firstpacket]
  205.  
  206.         jmp     .updatesock
  207.  
  208.  
  209.   .checksum_mismatch:
  210.  
  211.         DEBUGF  2,"UDP_Handler - checksum mismatch\n"
  212.  
  213.   .dump:
  214.         call    kernel_free
  215.         add     esp, 4 ; pop (balance stack)
  216.         DEBUGF  2,"UDP_Handler - dumping\n"
  217.  
  218.         ret
  219.  
  220.  
  221.  
  222.  
  223. ;-----------------------------------------------------------------
  224. ;
  225. ; UDP_output
  226. ;
  227. ; IN: eax = socket pointer
  228. ;     ecx = number of bytes to send
  229. ;     esi = pointer to data
  230. ;
  231. ;-----------------------------------------------------------------
  232.  
  233. align 4
  234. UDP_output:
  235.  
  236.         DEBUGF  1,"UDP_output: socket:%x, bytes: %u, data ptr: %x\n", eax, ecx, esi
  237.  
  238.         mov     dx, [eax + UDP_SOCKET.RemotePort]
  239.         DEBUGF  1,"remote port: %u\n", dx
  240.         rol     dx, 8
  241.         rol     edx, 16
  242.         mov     dx, [eax + UDP_SOCKET.LocalPort]
  243.         DEBUGF  1,"local port: %u\n", dx
  244.         rol     dx, 8
  245.  
  246.         mov     ebx, [eax + IP_SOCKET.LocalIP]
  247.         mov     eax, [eax + IP_SOCKET.RemoteIP]
  248.  
  249.         mov     di, IP_PROTO_UDP shl 8 + 128
  250.         sub     esp, 8                                          ; Data ptr and data size will be placed here
  251.         add     ecx, sizeof.UDP_header
  252.  
  253. ;;; TODO: fragment id
  254.         push    edx esi
  255.         call    IPv4_output
  256.         jz      .fail
  257.  
  258.         mov     [esp + 8], eax                                  ; pointer to buffer start
  259.         mov     [esp + 8 + 4], edx                              ; buffer size
  260.  
  261.         mov     [edi + UDP_header.Length], cx
  262.         rol     [edi + UDP_header.Length], 8
  263.  
  264.         pop     esi
  265.         push    edi ecx
  266.         sub     ecx, sizeof.UDP_header
  267.         add     edi, sizeof.UDP_header
  268.         shr     ecx, 2
  269.         rep     movsd
  270.         mov     ecx, [esp]
  271.         and     ecx, 3
  272.         rep     movsb
  273.         pop     ecx edi
  274.  
  275.         pop     dword [edi + UDP_header.SourcePort]
  276.  
  277. ; Checksum
  278.         mov     esi, edi
  279.         mov     [edi + UDP_header.Checksum], 0
  280.         UDP_checksum (edi-4), (edi-8)                           ; TODO: fix this, IPv4 packet could have options..
  281.  
  282.         inc     [UDP_PACKETS_TX]
  283.  
  284.         DEBUGF  1,"Sending UDP Packet to device %x\n", ebx
  285.  
  286.         call    [ebx + NET_DEVICE.transmit]
  287.         ret
  288.  
  289.   .fail:
  290.         DEBUGF  1,"UDP_output: failed\n"
  291.         add     esp, 4+4+8
  292.         xor     eax, eax
  293.         ret
  294.  
  295.  
  296.  
  297. ;---------------------------------------------------------------------------
  298. ;
  299. ; UDP_API
  300. ;
  301. ; This function is called by system function 75
  302. ;
  303. ; IN:  subfunction number in bl
  304. ;      device number in bh
  305. ;      ecx, edx, .. depends on subfunction
  306. ;
  307. ; OUT:
  308. ;
  309. ;---------------------------------------------------------------------------
  310.  
  311. align 4
  312. UDP_API:
  313.  
  314.         movzx   eax, bh
  315.         shl     eax, 2
  316.  
  317.         test    bl, bl
  318.         jz      .packets_tx     ; 0
  319.         dec     bl
  320.         jz      .packets_rx     ; 1
  321.  
  322. .error:
  323.         mov     eax, -1
  324.         ret
  325.  
  326. .packets_tx:
  327.         mov     eax, [UDP_PACKETS_TX + eax]
  328.         ret
  329.  
  330. .packets_rx:
  331.         mov     eax, [UDP_PACKETS_RX + eax]
  332.         ret
  333.