Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;;  ICMP.INC
  4. ;;
  5. ;;  Internet Control Message Protocol ( RFC 792 )
  6. ;;
  7. ;;  Last revision: 11.11.2006
  8. ;;
  9. ;;  This file contains the following:
  10. ;;   icmp_rx - processes ICMP-packets received by the IP layer
  11. ;;
  12. ;;  Changes history:
  13. ;;   22.09.2003 - [Mike Hibbett] : mikeh@oceanfree.net
  14. ;;   11.11.2006 - [Johnny_B] and [smb]
  15. ;;
  16. ;;   Current status:
  17. ;;      This implemetation of ICMP proto supports message of ECHO type.
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19.  
  20.  
  21. struc ICMP_PACKET
  22. {  .Type            db   ?  ;+00
  23.    .Code            db   ?  ;+01
  24.    .Checksum        dw   ?  ;+02
  25.    .Identifier      dw   ?  ;+04
  26.    .SequenceNumber  dw   ?  ;+06
  27.    .Data            db   ?  ;+08
  28. }
  29.  
  30. virtual at 0
  31.   ICMP_PACKET ICMP_PACKET
  32. end virtual
  33.  
  34.  
  35. ;   Example:
  36. ;       ECHO message format
  37. ;
  38. ;
  39. ;           0               1               2               3
  40. ;    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  41. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  42. ;   |     Type      |     Code      |          Checksum             |
  43. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  44. ;   |           Identifier          |        Sequence Number        |
  45. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  46. ;   |     Data ...
  47. ;   +-+-+-+-+-
  48. ;
  49.  
  50. ;
  51. ; ICMP types & codes, RFC 792 and FreeBSD's ICMP sources
  52. ;
  53.  
  54. ICMP_ECHOREPLY          equ     0               ; echo reply message
  55.  
  56. ICMP_UNREACH            equ     3
  57.         ICMP_UNREACH_NET        equ     0               ; bad net
  58.         ICMP_UNREACH_HOST       equ     1               ; bad host
  59.         ICMP_UNREACH_PROTOCOL   equ     2               ; bad protocol
  60.         ICMP_UNREACH_PORT       equ     3               ; bad port
  61.         ICMP_UNREACH_NEEDFRAG   equ     4               ; IP_DF caused drop
  62.         ICMP_UNREACH_SRCFAIL    equ     5               ; src route failed
  63.         ICMP_UNREACH_NET_UNKNOWN equ    6               ; unknown net
  64.         ICMP_UNREACH_HOST_UNKNOWN equ   7               ; unknown host
  65.         ICMP_UNREACH_ISOLATED   equ     8               ; src host isolated
  66.         ICMP_UNREACH_NET_PROHIB equ     9               ; prohibited access
  67.         ICMP_UNREACH_HOST_PROHIB equ    10              ; ditto
  68.         ICMP_UNREACH_TOSNET     equ     11              ; bad tos for net
  69.         ICMP_UNREACH_TOSHOST    equ     12              ; bad tos for host
  70.         ICMP_UNREACH_FILTER_PROHIB equ  13              ; admin prohib
  71.         ICMP_UNREACH_HOST_PRECEDENCE equ 14             ; host prec vio.
  72.         ICMP_UNREACH_PRECEDENCE_CUTOFF equ 15           ; prec cutoff
  73.  
  74. ICMP_SOURCEQUENCH       equ     4               ; packet lost, slow down
  75.  
  76. ICMP_REDIRECT           equ     5               ; shorter route, codes:
  77.         ICMP_REDIRECT_NET       equ     0               ; for network
  78.         ICMP_REDIRECT_HOST      equ     1               ; for host
  79.         ICMP_REDIRECT_TOSNET    equ     2               ; for tos and net
  80.         ICMP_REDIRECT_TOSHOST   equ     3               ; for tos and host
  81.        
  82. ICMP_ALTHOSTADDR        equ     6               ; alternate host address
  83. ICMP_ECHO               equ     8               ; echo service
  84. ICMP_ROUTERADVERT       equ     9               ; router advertisement
  85.         ICMP_ROUTERADVERT_NORMAL equ 0                  ; normal advertisement
  86.         ICMP_ROUTERADVERT_NOROUTE_COMMON equ 16         ; selective routing
  87.  
  88. ICMP_ROUTERSOLICIT      equ     10              ; router solicitation
  89. ICMP_TIMXCEED           equ     11              ; time exceeded, code:
  90.     ICMP_TIMXCEED_INTRANS       equ     0               ; ttl==0 in transit
  91.     ICMP_TIMXCEED_REASS equ     1               ; ttl==0 in reass
  92.  
  93. ICMP_PARAMPROB            equ  12               ; ip header bad
  94.     ICMP_PARAMPROB_ERRATPTR   equ  0            ; error at param ptr
  95.     ICMP_PARAMPROB_OPTABSENT  equ  1            ; req. opt. absent
  96.     ICMP_PARAMPROB_LENGTH     equ  2            ; bad length
  97.        
  98. ICMP_TSTAMP             equ     13              ; timestamp request
  99. ICMP_TSTAMPREPLY        equ     14              ; timestamp reply
  100. ICMP_IREQ               equ     15              ; information request
  101. ICMP_IREQREPLY          equ     16              ; information reply
  102. ICMP_MASKREQ            equ     17              ; address mask request
  103. ICMP_MASKREPLY          equ     18              ; address mask reply
  104. ICMP_TRACEROUTE         equ     30              ; traceroute
  105. ICMP_DATACONVERR        equ     31              ; data conversion error
  106. ICMP_MOBILE_REDIRECT    equ     32              ; mobile host redirect
  107. ICMP_IPV6_WHEREAREYOU   equ     33              ; IPv6 where-are-you
  108.  ICMP_IPV6_IAMHERE      equ     34              ; IPv6 i-am-here
  109. ICMP_MOBILE_REGREQUEST  equ     35              ; mobile registration req
  110. ICMP_MOBILE_REGREPLY    equ     36              ; mobile registreation reply
  111. ICMP_SKIP               equ     39              ; SKIP
  112.  
  113. ICMP_PHOTURIS           equ     40              ; Photuris
  114.     ICMP_PHOTURIS_UNKNOWN_INDEX   equ  1                ; unknown sec index
  115.     ICMP_PHOTURIS_AUTH_FAILED     equ  2                ; auth failed
  116.     ICMP_PHOTURIS_DECRYPT_FAILED  equ  3                ; decrypt failed
  117.  
  118.  
  119. ;***************************************************************************
  120. ;   Function
  121. ;      icmp_rx  [by Johnny_B]
  122. ;
  123. ;   Description
  124. ;       ICMP protocol handler
  125. ;       This is a kernel function, called by ip_rx
  126. ;
  127. ;  IN:
  128. ;    buffer_number  - # of IP-buffer. This buffer must be reused or marked as empty afterwards
  129. ;    IPPacketBase   - IP_PACKET base address
  130. ;    IPHeaderLength - Header length of IP_PACKET
  131. ;
  132. ;  OUT:
  133. ;    EAX=not defined
  134. ;
  135. ;           All used registers will be saved
  136. ;
  137. ;***************************************************************************
  138. proc icmp_rx  stdcall  uses ebx esi edi,\
  139.     buffer_number:DWORD,IPPacketBase:DWORD,IPHeaderLength:DWORD
  140.  
  141.     mov     esi,[IPPacketBase]   ;esi=IP_PACKET base address
  142.     mov     edi, esi
  143.     add     edi,[IPHeaderLength] ;edi=ICMP_PACKET base address
  144.  
  145.     cmp     byte[edi + ICMP_PACKET.Type], ICMP_ECHO ; Is this an echo request? discard if not
  146.     jz      .icmp_echo
  147.  
  148.     mov     eax, [buffer_number]
  149.     call    freeBuff
  150.     jmp     .exit
  151.  
  152.   .icmp_echo:
  153.  
  154.     ; swap the source and destination addresses
  155.     mov     ecx, [esi + IP_PACKET.DestinationAddress]
  156.     mov     ebx, [esi + IP_PACKET.SourceAddress]
  157.     mov     [esi + IP_PACKET.DestinationAddress], ebx
  158.     mov     [esi + IP_PACKET.SourceAddress], ecx
  159.  
  160.     ; recalculate the IP header checksum
  161.     mov     eax,[IPHeaderLength]
  162.     stdcall checksum_jb,esi,eax   ;buf_ptr,buf_size
  163.  
  164.     mov     byte[esi + IP_PACKET.HeaderChecksum], ah
  165.     mov     byte[esi + IP_PACKET.HeaderChecksum + 1], al      ; ?? correct byte order?
  166.  
  167.     mov     byte[edi + ICMP_PACKET.Type], ICMP_ECHOREPLY  ; change the request to a response
  168.     mov     word[edi + ICMP_PACKET.Checksum], 0  ; clear ICMP checksum prior to re-calc
  169.  
  170.     ; Calculate the length of the ICMP data ( IP payload)
  171.     xor     eax, eax
  172.     mov     ah, byte[esi + IP_PACKET.TotalLength]
  173.     mov     al, byte[esi + IP_PACKET.TotalLength + 1]
  174.     sub     ax, word[IPHeaderLength]  ;ax=ICMP-packet length
  175.  
  176.     stdcall checksum_jb,edi,eax   ;buf_ptr,buf_size
  177.  
  178.     mov     byte[edi + ICMP_PACKET.Checksum], ah
  179.     mov     byte[edi + ICMP_PACKET.Checksum + 1], al
  180.  
  181.     ; Queue packet for transmission
  182.     mov     ebx, [buffer_number]
  183.     mov     eax, NET1OUT_QUEUE
  184.     call    queue
  185.  
  186.   .exit:
  187.     ret
  188. endp