Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;;  IP.INC                                                         ;;
  4. ;;                                                                 ;;
  5. ;;  IP Processes for Menuet OS  TCP/IP stack                       ;;
  6. ;;                                                                 ;;
  7. ;;  Version 0.3  29 August 2002                                    ;;
  8. ;;                                                                 ;;
  9. ;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
  10. ;;                                                                 ;;
  11. ;;  See file COPYING for details                                   ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. $Revision: 593 $
  16.  
  17.  
  18. ; IP underlying protocols numbers
  19. PROTOCOL_ICMP     equ      1
  20. PROTOCOL_TCP      equ      6
  21. PROTOCOL_UDP      equ      17
  22.  
  23. struc IP_PACKET
  24. {  .VersionAndIHL           db   ?  ;+00 - Version[0-3 bits] and IHL(header length)[4-7 bits]
  25.    .TypeOfService           db   ?  ;+01
  26.    .TotalLength             dw   ?  ;+02
  27.    .Identification          dw   ?  ;+04
  28.    .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
  29.    .TimeToLive              db   ?  ;+08
  30.    .Protocol                db   ?  ;+09
  31.    .HeaderChecksum          dw   ?  ;+10
  32.    .SourceAddress           dd   ?  ;+12
  33.    .DestinationAddress      dd   ?  ;+16
  34.    .DataOrOptional          dd   ?  ;+20
  35. }
  36.  
  37. virtual at 0
  38.   IP_PACKET IP_PACKET
  39. end virtual
  40.  
  41.  
  42. ;*******************************************************************
  43. ;   Interface
  44. ;
  45. ;       ip_rx       processes all packets received by the network layer
  46. ;                   It calls the appropriate protocol handler
  47. ;
  48. ;
  49. ;
  50. ;*******************************************************************
  51.  
  52.  
  53. ;
  54. ;   IP Packet after reception - Normal IP packet format
  55. ;
  56. ;           0               1               2               3
  57. ;    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
  58. ;
  59. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  60. ;0  |Version|  IHL  |Type of Service|       Total Length            |
  61. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. ;4  |         Identification        |Flags|      Fragment Offset    |
  63. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  64. ;8  |  Time to Live |    Protocol   |         Header Checksum       |
  65. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  66. ;12 |                       Source Address                          |
  67. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  68. ;16 |                    Destination Address                        |
  69. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  70. ;20 |      Data                                                     |
  71. ;   +-+-+-..........                                               -+
  72. ;
  73. ;
  74. ;    [smb] attention! according to RFC 791 IP packet may have 'options' sections,
  75. ; so we can't simply think, that data have offset 20. We must calculate offset from
  76. ; IHL field
  77. ;
  78. macro   GET_IHL reg, header_addr
  79. {
  80.         movzx   reg, byte [header_addr]
  81.        
  82.         ; we need 4-7 bits, so....
  83.         and     reg, 0x0000000F
  84.        
  85.         ; IHL keeps number of octets, so we need to << 2 'reg'
  86.         shl     reg, 2
  87. }
  88.  
  89.  
  90. ;***************************************************************************
  91. ;   Function
  92. ;      ip_rx
  93. ;
  94. ;   Description
  95. ;       This is a kernel function, called by stack_handler
  96. ;       Processes all IP-packets received by the network layer
  97. ;       It calls the appropriate protocol handler
  98. ;
  99. ;***************************************************************************
  100. proc ip_rx stdcall
  101. local buffer_number dd ?
  102.  
  103.     ; Look for a buffer to tx
  104.     mov     eax, IPIN_QUEUE
  105.     call    dequeue
  106.     cmp     ax, NO_BUFFER
  107.     je      .exit         ; Exit if no buffer available
  108.  
  109.     mov     [buffer_number], eax    ;save buffer number
  110.  
  111.     ; convert buffer pointer eax to the absolute address
  112.     imul    eax, IPBUFFSIZE
  113.     add     eax, IPbuffs
  114.  
  115.     mov     ebx, eax  ; ebx=pointer to IP_PACKET
  116.  
  117.     ; Validate the IP checksum
  118.     mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
  119.     xchg    dh,dl          ; Get the checksum in intel format
  120.  
  121.     mov     [ebx + IP_PACKET.HeaderChecksum], word 0  ; clear checksum field - need to when
  122.                                 ; recalculating checksum
  123.     ;  this needs two data pointers and two size #.
  124.     ;  2nd pointer can be of length 0
  125.  
  126.     GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
  127.     stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
  128.     cmp     dx, ax
  129.  
  130.     mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
  131.     jnz     .dump  ;if CHECKSUM isn't valid then dump packet
  132.  
  133.     ; Validate the IP address, if it isn't broadcast
  134.     mov     eax, [stack_ip]
  135.     cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
  136.     je      @f
  137.  
  138.     ; If the IP address is 255.255.255.255, accept it
  139.     ; - it is a broadcast packet, which we need for dhcp
  140.     cmp     dword[ebx + IP_PACKET.DestinationAddress], 0xffffffff
  141.     jne     .dump
  142.  
  143.   @@:
  144.     mov     al, [ebx + IP_PACKET.VersionAndIHL]
  145.     and     al, 0x0f  ;get IHL(header length)
  146.     cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
  147.     jnz     .dump     ;then dump it
  148.  
  149.     cmp     byte[ebx + IP_PACKET.TimeToLive], byte 0
  150.     je      .dump     ;if TTL==0 then dump it
  151.  
  152.     mov     ax, word[ebx + IP_PACKET.FlagsAndFragmentOffset]
  153.     and     ax, 0xFFBF   ;get flags
  154.     cmp     ax, 0        ;if some flags was set then we dump this packet
  155.     jnz     .dump        ;the flags should be used for fragmented packets
  156.  
  157.     ; Check the protocol, and call the appropriate handler
  158.     ; Each handler will re-use or free the queue buffer as appropriate
  159.  
  160.     mov     al, [ebx + IP_PACKET.Protocol]
  161.  
  162.     cmp     al , PROTOCOL_TCP
  163.     jne     .not_tcp
  164.     DEBUGF  1,"K : ip_rx - TCP packet\n"
  165.     mov     eax, dword[buffer_number]
  166.     call    tcp_rx
  167.     jmp     .exit
  168.  
  169.   .not_tcp:
  170.     cmp     al, PROTOCOL_UDP
  171.     jne     .not_udp
  172.     DEBUGF  1,"K : ip_rx - UDP packet\n"
  173.     mov     eax, dword[buffer_number]
  174.     call    udp_rx
  175.     jmp     .exit
  176.  
  177.   .not_udp:
  178.     cmp     al , PROTOCOL_ICMP
  179.     jne     .dump              ;protocol ain't supported
  180.  
  181.     DEBUGF  1,"K : ip_rx - ICMP packet\n"
  182.     ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
  183.     mov     eax, dword[buffer_number]
  184.     stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
  185.     jmp     .exit
  186.  
  187.  
  188. .dump:
  189.     ; No protocol handler available, so
  190.     ; silently dump the packet, freeing up the queue buffer
  191.  
  192.     inc     dword [dumped_rx_count]
  193.  
  194.     mov     eax, dword[buffer_number]
  195.     call    freeBuff
  196.  
  197.   .exit:
  198.     ret
  199. endp
  200.  
  201.