Subversion Repositories Kolibri OS

Rev

Rev 425 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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