Subversion Repositories Kolibri OS

Rev

Rev 1542 | 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. ;;  ICMP.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.  
  20. $Revision: 1544 $
  21.  
  22. ; ICMP types & codes
  23.  
  24. ICMP_ECHOREPLY                  equ 0               ; echo reply message
  25.  
  26. ICMP_UNREACH                    equ 3
  27. ICMP_UNREACH_NET                equ  0               ; bad net
  28. ICMP_UNREACH_HOST               equ  1               ; bad host
  29. ICMP_UNREACH_PROTOCOL           equ  2               ; bad protocol
  30. ICMP_UNREACH_PORT               equ  3               ; bad port
  31. ICMP_UNREACH_NEEDFRAG           equ  4               ; IP_DF caused drop
  32. ICMP_UNREACH_SRCFAIL            equ  5               ; src route failed
  33. ICMP_UNREACH_NET_UNKNOWN        equ  6               ; unknown net
  34. ICMP_UNREACH_HOST_UNKNOWN       equ  7               ; unknown host
  35. ICMP_UNREACH_ISOLATED           equ  8               ; src host isolated
  36. ICMP_UNREACH_NET_PROHIB         equ  9               ; prohibited access
  37. ICMP_UNREACH_HOST_PROHIB        equ 10              ; ditto
  38. ICMP_UNREACH_TOSNET             equ 11              ; bad tos for net
  39. ICMP_UNREACH_TOSHOST            equ 12              ; bad tos for host
  40. ICMP_UNREACH_FILTER_PROHIB      equ 13              ; admin prohib
  41. ICMP_UNREACH_HOST_PRECEDENCE    equ 14             ; host prec vio.
  42. ICMP_UNREACH_PRECEDENCE_CUTOFF  equ 15           ; prec cutoff
  43.  
  44. ICMP_SOURCEQUENCH               equ 4               ; Packet lost, slow down
  45.  
  46. ICMP_REDIRECT                   equ 5               ; shorter route, codes:
  47. ICMP_REDIRECT_NET               equ  0               ; for network
  48. ICMP_REDIRECT_HOST              equ  1               ; for host
  49. ICMP_REDIRECT_TOSNET            equ  2               ; for tos and net
  50. ICMP_REDIRECT_TOSHOST           equ  3               ; for tos and host
  51.  
  52. ICMP_ALTHOSTADDR                equ 6               ; alternate host address
  53. ICMP_ECHO                       equ  8               ; echo service
  54. ICMP_ROUTERADVERT               equ  9               ; router advertisement
  55. ICMP_ROUTERADVERT_NORMAL        equ  0                  ; normal advertisement
  56. ICMP_ROUTERADVERT_NOROUTE_COMMON equ 16         ; selective routing
  57.  
  58. ICMP_ROUTERSOLICIT              equ 10              ; router solicitation
  59. ICMP_TIMXCEED                   equ 11              ; time exceeded, code:
  60. ICMP_TIMXCEED_INTRANS           equ 0               ; ttl==0 in transit
  61. ICMP_TIMXCEED_REASS             equ 1               ; ttl==0 in reass
  62.  
  63. ICMP_PARAMPROB                  equ 12               ; ip header bad
  64. ICMP_PARAMPROB_ERRATPTR         equ 0            ; error at param ptr
  65. ICMP_PARAMPROB_OPTABSENT        equ 1            ; req. opt. absent
  66. ICMP_PARAMPROB_LENGTH           equ 2            ; bad length
  67.  
  68. ICMP_TSTAMP                     equ 13              ; timestamp request
  69. ICMP_TSTAMPREPLY                equ 14              ; timestamp reply
  70. ICMP_IREQ                       equ 15              ; information request
  71. ICMP_IREQREPLY                  equ 16              ; information reply
  72. ICMP_MASKREQ                    equ 17              ; address mask request
  73. ICMP_MASKREPLY                  equ 18              ; address mask reply
  74. ICMP_TRACEROUTE                 equ 30              ; traceroute
  75. ICMP_DATACONVERR                equ 31              ; data conversion error
  76. ICMP_MOBILE_REDIRECT            equ 32              ; mobile host redirect
  77. ICMP_IPV6_WHEREAREYOU           equ 33              ; IPv6 where-are-you
  78. ICMP_IPV6_IAMHERE               equ 34              ; IPv6 i-am-here
  79. ICMP_MOBILE_REGREQUEST          equ 35              ; mobile registration req
  80. ICMP_MOBILE_REGREPLY            equ 36              ; mobile registreation reply
  81. ICMP_SKIP                       equ 39              ; SKIP
  82.  
  83. ICMP_PHOTURIS                   equ 40              ; Photuris
  84. ICMP_PHOTURIS_UNKNOWN_INDEX     equ 1                ; unknown sec index
  85. ICMP_PHOTURIS_AUTH_FAILED       equ 2                ; auth failed
  86. ICMP_PHOTURIS_DECRYPT_FAILED    equ 3                ; decrypt failed
  87.  
  88.  
  89.  
  90. struct  ICMP_Packet
  91.         .Type           db   ?
  92.         .Code           db   ?
  93.         .Checksum       dw   ?
  94.         .Identifier     dw   ?
  95.         .SequenceNumber dw   ?
  96.         .Data:
  97. ends
  98.  
  99.  
  100. align 4
  101. uglobal
  102.         ICMP_PACKETS_TX         rd  MAX_IP
  103.         ICMP_PACKETS_RX         rd  MAX_IP
  104. endg
  105.  
  106.  
  107.  
  108. ;-----------------------------------------------------------------
  109. ;
  110. ; ICMP_init
  111. ;
  112. ;-----------------------------------------------------------------
  113.  
  114. macro ICMP_init {
  115.  
  116.         xor     eax, eax
  117.         mov     edi, ICMP_PACKETS_TX
  118.         mov     ecx, 2*MAX_IP
  119.         rep     stosd
  120.  
  121. }
  122.  
  123.  
  124. ;-----------------------------------------------------------------
  125. ;
  126. ; ICMP_input:
  127. ;
  128. ;  This procedure will send reply's to ICMP echo's
  129. ;   and insert packets into sockets when needed
  130. ;
  131. ;  IN:  Pointer to buffer in [esp]
  132. ;       size of buffer in [esp+4]
  133. ;       ebx = pointer to device struct
  134. ;       ecx = ICMP Packet size
  135. ;       edx = ptr to ICMP Packet data
  136. ;       esi = ipv4 source address
  137. ;       edi = ipv4 dest   address
  138. ;  OUT: /
  139. ;
  140. ;-----------------------------------------------------------------
  141. align 4
  142. ICMP_input:
  143.  
  144.         DEBUGF  1,"ICMP_input - start\n"
  145.  
  146. ; First, check the checksum (altough some implementations ignore it)
  147.  
  148.         push    edx esi ecx
  149.         push    [edx + ICMP_Packet.Checksum]
  150.         mov     [edx + ICMP_Packet.Checksum], 0
  151.         mov     esi, edx
  152.         xor     edx, edx
  153.         call    checksum_1
  154.         call    checksum_2
  155.         pop     si
  156.         cmp     dx, si
  157.         pop     ecx esi edx
  158.         jne     .checksum_mismatch
  159.  
  160.         cmp     byte [edx + ICMP_Packet.Type], ICMP_ECHO                    ; Is this an echo request?
  161.         jne     .check_sockets
  162.  
  163. ; We well re-use the packet sow e can create the response as fast as possible
  164. ; Notice: this only works on pure ethernet (however, IP packet options are not a problem this time :)
  165.  
  166.         DEBUGF  1,"ICMP_input - echo request\n"
  167.         mov     byte [edx + ICMP_Packet.Type], ICMP_ECHOREPLY               ; Change Packet type to reply
  168.  
  169. ; Update stats (and validate device ptr)
  170.         call    NET_ptr_to_num
  171.         cmp     edi,-1
  172.         je      .dump
  173.         inc     [ICMP_PACKETS_RX+4*edi]
  174.         inc     [ICMP_PACKETS_TX+4*edi]
  175.  
  176. ; exchange dest and source address in IP header
  177. ; exchange dest and source MAC in ETH header
  178.         mov     esi, [esp]                                                  ; Start of buffer
  179.         push    dword [esi + ETH_FRAME.DstMAC]
  180.         push    dword [esi + ETH_FRAME.SrcMAC]
  181.         pop     dword [esi + ETH_FRAME.DstMAC]
  182.         pop     dword [esi + ETH_FRAME.SrcMAC]
  183.         push    word [esi + ETH_FRAME.DstMAC + 4]
  184.         push    word [esi + ETH_FRAME.SrcMAC + 4]
  185.         pop     word [esi + ETH_FRAME.DstMAC + 4]
  186.         pop     word [esi + ETH_FRAME.SrcMAC + 4]
  187.  
  188.         add     esi, ETH_FRAME.Data
  189.         push    [esi + IPv4_Packet.SourceAddress]
  190.         push    [esi + IPv4_Packet.DestinationAddress]
  191.         pop     [esi + IPv4_Packet.SourceAddress]
  192.         pop     [esi + IPv4_Packet.DestinationAddress]
  193.  
  194. ; Recalculate ip header checksum
  195.         movzx   ecx, byte [esi + IPv4_Packet.VersionAndIHL]                 ; Calculate IP Header length by using IHL field
  196.         and     ecx, 0x0f
  197.         shl     cx, 2
  198.         mov     edi, ecx                                                    ; IP header length
  199.         mov     eax, edx                                                    ; ICMP packet start addr
  200.  
  201.         push    esi                                                         ; Calculate the IP checksum
  202.         xor     edx, edx                                                    ;
  203.         call    checksum_1                                                  ;
  204.         call    checksum_2                                                  ;
  205.         pop     esi                                                         ;
  206.         mov     word [esi + IPv4_Packet.HeaderChecksum], dx                 ;
  207.  
  208. ; Recalculate ICMP CheckSum
  209.         movzx   ecx, word[esi + IPv4_Packet.TotalLength]                    ; Find length of IP Packet
  210.         xchg    ch, cl                                                      ;
  211.         sub     ecx, edi                                                    ; IP packet length - IP header length = ICMP packet length
  212.  
  213.         mov     esi, eax                                                    ; Calculate ICMP checksum
  214.         xor     edx, edx                                                    ;
  215.         call    checksum_1                                                  ;
  216.         call    checksum_2                                                  ;
  217.         mov     word [eax + ICMP_Packet.Checksum], dx                       ;
  218.  
  219. ; Transmit the packet (notice that packet ptr and packet size have been on stack since start of the procedure!)
  220.         call    [ebx + NET_DEVICE.transmit]
  221.         ret
  222.  
  223.  
  224.  
  225.  
  226.        .check_sockets:
  227.         ; Look for an open ICMP socket
  228.         ; esi = sender ip
  229.  
  230.         mov     ebx, net_sockets
  231.   .try_more:
  232. ;        mov     ax , [edx + ICMP_Packet.Identifier]
  233.   .next_socket:
  234.         mov     ebx, [ebx + SOCKET.NextPtr]
  235.         or      ebx, ebx
  236.         jz      .dump
  237.  
  238.         cmp     [ebx + SOCKET.Domain], AF_INET4
  239.         jne     .next_socket
  240.  
  241.         cmp     [ebx + SOCKET.Protocol], IP_PROTO_ICMP
  242.         jne     .next_socket
  243.  
  244.         cmp     [ebx + IP_SOCKET.RemoteIP], esi
  245.         jne     .next_socket
  246.  
  247. ;        cmp     [esi + ICMP_SOCKET.Identifier], ax
  248. ;        jne     .next_socket
  249.  
  250. ;        call    IPv4_dest_to_dev
  251. ;        cmp     edi,-1
  252. ;        je      .dump
  253. ;        inc     [ICMP_PACKETS_RX+edi]
  254.  
  255.         DEBUGF 1,"Found valid ICMP packet for socket %x\n", ebx
  256.  
  257.         mov     eax, ebx
  258.         add     ebx, SOCKET.lock
  259.         call    wait_mutex
  260.  
  261.         mov     esi, edx
  262.         jmp     SOCKET_input
  263.  
  264.  
  265.   .checksum_mismatch:
  266.         DEBUGF  1,"ICMP_Handler - checksum mismatch\n"
  267.  
  268.   .dump:
  269.         DEBUGF  1,"ICMP_Handler - dumping\n"
  270.  
  271.         call    kernel_free
  272.         add     esp, 4 ; pop (balance stack)
  273.  
  274.         ret
  275.  
  276.  
  277. ;-----------------------------------------------------------------
  278. ;
  279. ; ICMP_output
  280. ;
  281. ; IN:  eax = dest ip
  282. ;      ebx = source ip
  283. ;      ecx = data length
  284. ;      dh  = type
  285. ;      dl  = code
  286. ;      high 16 bits of edx = fragment id (for IP header)
  287. ;      esi = data offset
  288. ;      edi = identifier shl 16 + sequence number
  289. ;
  290. ;-----------------------------------------------------------------
  291. align 4
  292. ICMP_output:
  293.  
  294.         DEBUGF  1,"Creating ICMP Packet\n"
  295.  
  296.         push    esi edi edx
  297.  
  298.         mov     ebx, [eax + IP_SOCKET.LocalIP]
  299.         mov     eax, [eax + IP_SOCKET.RemoteIP]
  300.         add     ecx, ICMP_Packet.Data
  301.         mov     di , IP_PROTO_ICMP SHL 8 + 128  ; TTL
  302.         shr     edx, 16
  303.  
  304.         call    IPv4_output
  305.         jz      .exit
  306.  
  307.         DEBUGF  1,"full icmp packet size: %u\n", edx
  308.  
  309.         pop     eax
  310.         mov     word [edi + ICMP_Packet.Type], ax       ; Write both type and code bytes at once
  311.         pop     eax
  312.         mov     [edi + ICMP_Packet.SequenceNumber], ax
  313.         shr     eax, 16
  314.         mov     [edi + ICMP_Packet.Identifier], ax
  315.         mov     [edi + ICMP_Packet.Checksum], 0
  316.  
  317.         push    eax ebx ecx edx
  318.         mov     esi, edi
  319.         xor     edx, edx
  320.         call    checksum_1
  321.         call    checksum_2
  322.         mov     [edi + ICMP_Packet.Checksum], dx
  323.         pop     edx ecx ebx eax esi
  324.  
  325.         sub     ecx, ICMP_Packet.Data
  326.         add     edi, ICMP_Packet.Data
  327.         push    cx
  328.         shr     cx , 2
  329.         rep     movsd
  330.         pop     cx
  331.         and     cx , 3
  332.         rep     movsb
  333.  
  334.         sub     edi, edx                                ;;; TODO: find a better way to remember start of packet
  335.         push    edx edi
  336.         DEBUGF  1,"Sending ICMP Packet\n"
  337.         call    [ebx + NET_DEVICE.transmit]
  338.         ret
  339.   .exit:
  340.         DEBUGF  1,"Creating ICMP Packet failed\n"
  341.         add     esp, 3*4
  342.         ret
  343.  
  344.  
  345.  
  346.  
  347. ;-----------------------------------------------------------------
  348. ;
  349. ; ICMP_output
  350. ;
  351. ; IN:  eax = socket ptr
  352. ;      ecx = data length
  353. ;      esi = data offset
  354. ;
  355. ;-----------------------------------------------------------------
  356. align 4
  357. ICMP_output_raw:
  358.  
  359.         DEBUGF  1,"Creating ICMP Packet for socket %x, data ptr=%x\n", eax, edx
  360.  
  361.         push    edx
  362.  
  363.         mov     di, IP_PROTO_ICMP SHL 8 + 128  ; TTL
  364.         shr     edx, 16
  365.         mov     ebx, [eax + IP_SOCKET.LocalIP]
  366.         mov     eax, [eax + IP_SOCKET.RemoteIP]
  367.         call    IPv4_output
  368.         jz      .exit
  369.  
  370.         pop     esi
  371.         push    edx
  372.         push    eax
  373.  
  374.         push    edi ecx
  375.         DEBUGF  1,"copying %u bytes from %x to %x\n", ecx, esi, edi
  376.         rep     movsb
  377.         pop     ecx edi
  378.  
  379.         mov     [edi + ICMP_Packet.Checksum], 0
  380.  
  381.         mov     esi, edi
  382.         xor     edx, edx
  383.         call    checksum_1
  384.         call    checksum_2
  385.         mov     [edi + ICMP_Packet.Checksum], dx
  386.  
  387.         DEBUGF  1,"Sending ICMP Packet\n"
  388.         call    [ebx + NET_DEVICE.transmit]
  389.         ret
  390.   .exit:
  391.         DEBUGF  1,"Creating ICMP Packet failed\n"
  392.         add     esp, 4
  393.         ret
  394.  
  395.  
  396.  
  397.  
  398. ;-----------------------------------------------------------------
  399. ;
  400. ; ICMP_API
  401. ;
  402. ; This function is called by system function 75
  403. ;
  404. ; IN:  subfunction number in bl
  405. ;      device number in bh
  406. ;      ecx, edx, .. depends on subfunction
  407. ;
  408. ; OUT:
  409. ;
  410. ;-----------------------------------------------------------------
  411. align 4
  412. ICMP_API:
  413.  
  414.         movzx   eax, bh
  415.         shl     eax, 2
  416.  
  417.         test    bl, bl
  418.         jz      .packets_tx     ; 0
  419.         dec     bl
  420.         jz      .packets_rx     ; 1
  421.  
  422. .error:
  423.         mov     eax, -1
  424.         ret
  425.  
  426. .packets_tx:
  427.         add     eax, ICMP_PACKETS_TX
  428.         mov     eax, [eax]
  429.         ret
  430.  
  431. .packets_rx:
  432.         add     eax, ICMP_PACKETS_RX
  433.         mov     eax, [eax]
  434.         ret
  435.