Subversion Repositories Kolibri OS

Rev

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