Subversion Repositories Kolibri OS

Rev

Rev 5363 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2012-2015. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  IPv6.INC                                                       ;;
  7. ;;                                                                 ;;
  8. ;;  Part of the tcp/ip network stack for KolibriOS                 ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 5522 $
  18.  
  19.  
  20. struct  IPv6_header
  21.  
  22.         VersionTrafficFlow      dd ?    ; Version[0-3], Traffic class[4-11], Flow Label [12-31]
  23.         PayloadLength           dw ?    ; 16 bits, unsigned length of payload (extension headers are part of this)
  24.         NextHeader              db ?    ; Values are same as in IPv4 'Protocol' field
  25.         HopLimit                db ?    ; Decremented by every node, packet is discarded when it reaches 0
  26.         SourceAddress           rd 4    ; 128-bit addresses
  27.         DestinationAddress      rd 4    ;
  28.         Payload                 rb 0
  29.  
  30. ends
  31.  
  32.  
  33. uglobal
  34. align 4
  35.  
  36.         IPv6:
  37.         .addresses      rd 4*NET_DEVICES_MAX
  38.         .subnet         rd 4*NET_DEVICES_MAX
  39.         .dns            rd 4*NET_DEVICES_MAX
  40.         .gateway        rd 4*NET_DEVICES_MAX
  41.  
  42.         .packets_tx     rd NET_DEVICES_MAX
  43.         .packets_rx     rd NET_DEVICES_MAX
  44.  
  45. endg
  46.  
  47.  
  48. ;-----------------------------------------------------------------
  49. ;
  50. ; IPv6_init
  51. ;
  52. ;  This function resets all IP variables
  53. ;
  54. ;-----------------------------------------------------------------
  55. macro   IPv6_init {
  56.  
  57.         xor     eax, eax
  58.         mov     edi, IPv6
  59.         mov     ecx, (4*4*4+2*4)MAX_IP
  60.         rep stosd
  61.  
  62. }
  63.  
  64.  
  65.  
  66. ;-----------------------------------------------------------------
  67. ;
  68. ; IPv6_input:
  69. ;
  70. ;  Will check if IPv6 Packet isnt damaged
  71. ;  and call appropriate handler. (TCP/UDP/ICMP/..)
  72. ;
  73. ;  It will also re-construct fragmented packets
  74. ;
  75. ;  IN:  Pointer to buffer in [esp]
  76. ;       size of buffer in [esp+4]
  77. ;       pointer to device struct in ebx
  78. ;       pointer to IPv6 header in edx
  79. ;       size of IPv6 packet in ecx
  80. ;  OUT: /
  81. ;
  82. ;-----------------------------------------------------------------
  83. align 4
  84. IPv6_input:
  85.  
  86.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input from: %x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x\n",\
  87.         [edx + IPv6_header.SourceAddress + 0]:2,[edx + IPv6_header.SourceAddress + 1]:2,\
  88.         [edx + IPv6_header.SourceAddress + 2]:2,[edx + IPv6_header.SourceAddress + 3]:2,\
  89.         [edx + IPv6_header.SourceAddress + 4]:2,[edx + IPv6_header.SourceAddress + 5]:2,\
  90.         [edx + IPv6_header.SourceAddress + 6]:2,[edx + IPv6_header.SourceAddress + 7]:2,\
  91.         [edx + IPv6_header.SourceAddress + 8]:2,[edx + IPv6_header.SourceAddress + 9]:2,\
  92.         [edx + IPv6_header.SourceAddress + 10]:2,[edx + IPv6_header.SourceAddress + 11]:2,\
  93.         [edx + IPv6_header.SourceAddress + 12]:2,[edx + IPv6_header.SourceAddress + 13]:2,\
  94.         [edx + IPv6_header.SourceAddress + 14]:2,[edx + IPv6_header.SourceAddress + 15]:2
  95.  
  96.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input to: %x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x\n",\
  97.         [edx + IPv6_header.DestinationAddress + 0]:2,[edx + IPv6_header.DestinationAddress + 1]:2,\
  98.         [edx + IPv6_header.DestinationAddress + 2]:2,[edx + IPv6_header.DestinationAddress + 3]:2,\
  99.         [edx + IPv6_header.DestinationAddress + 4]:2,[edx + IPv6_header.DestinationAddress + 5]:2,\
  100.         [edx + IPv6_header.DestinationAddress + 6]:2,[edx + IPv6_header.DestinationAddress + 7]:2,\
  101.         [edx + IPv6_header.DestinationAddress + 8]:2,[edx + IPv6_header.DestinationAddress + 9]:2,\
  102.         [edx + IPv6_header.DestinationAddress + 10]:2,[edx + IPv6_header.DestinationAddress + 11]:2,\
  103.         [edx + IPv6_header.DestinationAddress + 12]:2,[edx + IPv6_header.DestinationAddress + 13]:2,\
  104.         [edx + IPv6_header.DestinationAddress + 14]:2,[edx + IPv6_header.DestinationAddress + 15]:2
  105.  
  106.         sub     ecx, sizeof.IPv6_header
  107.         jb      .dump
  108.  
  109.         cmp     cx, [edx + IPv6_header.PayloadLength]
  110.         jb      .dump
  111.  
  112. ;-------------------------------------------------------------------
  113. ; No, it's just a regular IP packet, pass it to the higher protocols
  114.  
  115.   .handle_it:
  116.         movzx   ecx, [edx + IPv6_header.PayloadLength]
  117.         lea     edi, [edx + IPv6_header.SourceAddress]          ; make edi ptr to source and dest IPv6 address
  118.         lea     esi, [edx + IPv6_header.Payload]                ; make esi ptr to data
  119.         mov     al, [edx + IPv6_header.NextHeader]
  120.  
  121.   .scan:
  122.         cmp     al, 59  ; no next
  123.         je      .dump
  124.  
  125.         cmp     al, 0
  126.         je      .hop_by_hop
  127.  
  128.         cmp     al, 43
  129.         je      .routing
  130.  
  131.         cmp     al, 44
  132.         je      .fragment
  133.  
  134.         cmp     al, 60
  135.         je      .dest_opts
  136.  
  137. ;        cmp     al, IP_PROTO_TCP
  138. ;        je      TCP_input
  139.  
  140. ;        cmp     al, IP_PROTO_UDP
  141. ;        je      UDP_input
  142.  
  143. ;        cmp     al, 58
  144. ;        je      ICMP6_input
  145.  
  146.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - unknown protocol: %u\n", al
  147.  
  148.   .dump:
  149.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - dumping\n"
  150.         call    NET_BUFF_free
  151.         ret
  152.  
  153.   .dump_options:
  154.         add     esp, 2+4+4
  155.         jmp     .dump
  156.  
  157.   .nextheader:
  158.         pop     esi
  159.         pop     ecx
  160.         pop     ax
  161.         jmp     .scan
  162.  
  163. ;-------------------------
  164. ; Hop-by-Hop
  165.  
  166.   .hop_by_hop:
  167.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - hop by hop\n"
  168.         pushw   [esi]                   ; 8 bit identifier for option type
  169.         movzx   eax, byte[esi + 1]      ; Hdr Ext Len
  170.         inc     eax                     ; first 8 octets not counted
  171.         shl     eax, 3                  ; * 8
  172.         sub     ecx, eax
  173.         push    ecx
  174.         add     eax, esi
  175.         push    eax
  176.         inc     esi
  177.         inc     esi
  178.  
  179.         mov     al, [esi]
  180.  
  181.         cmp     al, 0
  182.         je      .pad_1
  183.  
  184.         cmp     al, 1
  185.         je      .pad_n
  186.  
  187.         ; TODO: check with other known options
  188.  
  189. ; unknown option.. discard packet or not?
  190. ; check highest two bits
  191.         test    al, 0xc0        ; discard packet
  192.         jnz     .dump_options
  193.  
  194.   .pad_n:
  195.         movzx   eax, byte[esi + 1]
  196.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - pad %u\n", eax
  197.         inc     esi
  198.         inc     esi
  199.         add     esi, eax
  200.         sub     ecx, eax
  201.         jmp     .hop_by_hop
  202.  
  203.   .pad_1:
  204.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - pad 1\n"
  205.         inc     esi
  206.         dec     ecx
  207.         jmp     .hop_by_hop
  208.  
  209.  
  210.  
  211.   .dest_opts:
  212.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - dest opts\n"
  213.         jmp     .nextheader
  214.  
  215.   .routing:
  216.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - routing\n"
  217.         pushw   [esi]                   ; 8 bit identifier for option type
  218.         movzx   eax, byte[esi + 1]      ; Hdr Ext Len
  219.         inc     eax                     ; first 8 octets not counted
  220.         shl     eax, 3                  ; * 8
  221.         sub     ecx, eax
  222.         push    ecx
  223.         add     eax, esi
  224.         push    eax
  225.         inc     esi
  226.         inc     esi
  227.  
  228.         cmp     al, 0
  229.         je      .pad_1
  230.  
  231.         cmp     al, 1
  232.         je      .pad_n
  233.  
  234.         mov     al, [esi]       ; routing type
  235.  
  236.         jmp     .nextheader
  237.  
  238.   .fragment:
  239.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - fragment\n"
  240.  
  241.         jmp     .nextheader
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. ;---------------------------------------------------------------------------
  249. ;
  250. ; IPv6_API
  251. ;
  252. ; This function is called by system function 75
  253. ;
  254. ; IN:  subfunction number in bl
  255. ;      device number in bh
  256. ;      ecx, edx, .. depends on subfunction
  257. ;
  258. ; OUT:
  259. ;
  260. ;---------------------------------------------------------------------------
  261. align 4
  262. IPv6_api:
  263.  
  264.         movzx   eax, bh
  265.         shl     eax, 2
  266.  
  267.         and     ebx, 0x000000ff
  268.         cmp     ebx, .number
  269.         ja      .error
  270.         jmp     dword [.table + 4*ebx]
  271.  
  272.   .table:
  273.         dd      .packets_tx     ; 0
  274.         dd      .packets_rx     ; 1
  275. ;        dd      .read_ip        ; 2
  276. ;        dd      .write_ip       ; 3
  277. ;        dd      .read_dns       ; 4
  278. ;        dd      .write_dns      ; 5
  279. ;        dd      .read_subnet    ; 6
  280. ;        dd      .write_subnet   ; 7
  281. ;        dd      .read_gateway   ; 8
  282. ;        dd      .write_gateway  ; 9
  283.   .number = ($ - .table) / 4 - 1
  284.  
  285.   .error:
  286.         mov     eax, -1
  287.         ret
  288.  
  289.   .packets_tx:
  290.         mov     eax, [IPv6.packets_tx + eax]
  291.         ret
  292.  
  293.   .packets_rx:
  294.         mov     eax, [IPv6.packets_rx + eax]
  295.         ret
  296.  
  297.