Subversion Repositories Kolibri OS

Rev

Rev 5522 | 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: 6011 $
  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: Resets all IPv6 variables                            ;
  51. ;                                                                 ;
  52. ;-----------------------------------------------------------------;
  53. macro   ipv6_init {
  54.  
  55.         xor     eax, eax
  56.         mov     edi, IPv6
  57.         mov     ecx, (4*4*4+2*4)MAX_IP
  58.         rep stosd
  59.  
  60. }
  61.  
  62.  
  63.  
  64. ;-----------------------------------------------------------------;
  65. ;                                                                 ;
  66. ; ipv6_input: Check if IPv6 Packet isnt damaged and call          ;
  67. ; appropriate handler. (TCP/UDP/ICMP/..)                          ;
  68. ; We will also re-construct fragmented packets                    ;
  69. ;                                                                 ;
  70. ;   IN: [esp] = ptr to buffer                                     ;
  71. ;       [esp+4] = size of buffer                                  ;
  72. ;       ebx = ptr to device struct                                ;
  73. ;       edx = ptr to IPv6 header                                  ;
  74. ;       ecx = size of IPv6 packet                                 ;
  75. ;                                                                 ;
  76. ;  OUT: /                                                         ;
  77. ;                                                                 ;
  78. ;-----------------------------------------------------------------;
  79. align 4
  80. ipv6_input:
  81.  
  82.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input from: %x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x\n",\
  83.         [edx + IPv6_header.SourceAddress + 0]:2,[edx + IPv6_header.SourceAddress + 1]:2,\
  84.         [edx + IPv6_header.SourceAddress + 2]:2,[edx + IPv6_header.SourceAddress + 3]:2,\
  85.         [edx + IPv6_header.SourceAddress + 4]:2,[edx + IPv6_header.SourceAddress + 5]:2,\
  86.         [edx + IPv6_header.SourceAddress + 6]:2,[edx + IPv6_header.SourceAddress + 7]:2,\
  87.         [edx + IPv6_header.SourceAddress + 8]:2,[edx + IPv6_header.SourceAddress + 9]:2,\
  88.         [edx + IPv6_header.SourceAddress + 10]:2,[edx + IPv6_header.SourceAddress + 11]:2,\
  89.         [edx + IPv6_header.SourceAddress + 12]:2,[edx + IPv6_header.SourceAddress + 13]:2,\
  90.         [edx + IPv6_header.SourceAddress + 14]:2,[edx + IPv6_header.SourceAddress + 15]:2
  91.  
  92.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input to: %x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x\n",\
  93.         [edx + IPv6_header.DestinationAddress + 0]:2,[edx + IPv6_header.DestinationAddress + 1]:2,\
  94.         [edx + IPv6_header.DestinationAddress + 2]:2,[edx + IPv6_header.DestinationAddress + 3]:2,\
  95.         [edx + IPv6_header.DestinationAddress + 4]:2,[edx + IPv6_header.DestinationAddress + 5]:2,\
  96.         [edx + IPv6_header.DestinationAddress + 6]:2,[edx + IPv6_header.DestinationAddress + 7]:2,\
  97.         [edx + IPv6_header.DestinationAddress + 8]:2,[edx + IPv6_header.DestinationAddress + 9]:2,\
  98.         [edx + IPv6_header.DestinationAddress + 10]:2,[edx + IPv6_header.DestinationAddress + 11]:2,\
  99.         [edx + IPv6_header.DestinationAddress + 12]:2,[edx + IPv6_header.DestinationAddress + 13]:2,\
  100.         [edx + IPv6_header.DestinationAddress + 14]:2,[edx + IPv6_header.DestinationAddress + 15]:2
  101.  
  102.         sub     ecx, sizeof.IPv6_header
  103.         jb      .dump
  104.  
  105.         cmp     cx, [edx + IPv6_header.PayloadLength]
  106.         jb      .dump
  107.  
  108. ;-------------------------------------------------------------------
  109. ; No, it's just a regular IP packet, pass it to the higher protocols
  110.  
  111.   .handle_it:
  112.         movzx   ecx, [edx + IPv6_header.PayloadLength]
  113.         lea     edi, [edx + IPv6_header.SourceAddress]          ; make edi ptr to source and dest IPv6 address
  114.         lea     esi, [edx + IPv6_header.Payload]                ; make esi ptr to data
  115.         mov     al, [edx + IPv6_header.NextHeader]
  116.  
  117.   .scan:
  118.         cmp     al, 59  ; no next
  119.         je      .dump
  120.  
  121.         cmp     al, 0
  122.         je      .hop_by_hop
  123.  
  124.         cmp     al, 43
  125.         je      .routing
  126.  
  127.         cmp     al, 44
  128.         je      .fragment
  129.  
  130.         cmp     al, 60
  131.         je      .dest_opts
  132.  
  133. ;        cmp     al, IP_PROTO_TCP
  134. ;        je      TCP_input
  135.  
  136. ;        cmp     al, IP_PROTO_UDP
  137. ;        je      UDP_input
  138.  
  139. ;        cmp     al, 58
  140. ;        je      ICMP6_input
  141.  
  142.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - unknown protocol: %u\n", al
  143.  
  144.   .dump:
  145.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - dumping\n"
  146.         call    net_buff_free
  147.         ret
  148.  
  149.   .dump_options:
  150.         add     esp, 2+4+4
  151.         jmp     .dump
  152.  
  153.   .nextheader:
  154.         pop     esi
  155.         pop     ecx
  156.         pop     ax
  157.         jmp     .scan
  158.  
  159. ;-------------------------
  160. ; Hop-by-Hop
  161.  
  162.   .hop_by_hop:
  163.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - hop by hop\n"
  164.         pushw   [esi]                   ; 8 bit identifier for option type
  165.         movzx   eax, byte[esi + 1]      ; Hdr Ext Len
  166.         inc     eax                     ; first 8 octets not counted
  167.         shl     eax, 3                  ; * 8
  168.         sub     ecx, eax
  169.         push    ecx
  170.         add     eax, esi
  171.         push    eax
  172.         inc     esi
  173.         inc     esi
  174.  
  175.         mov     al, [esi]
  176.  
  177.         cmp     al, 0
  178.         je      .pad_1
  179.  
  180.         cmp     al, 1
  181.         je      .pad_n
  182.  
  183.         ; TODO: check with other known options
  184.  
  185. ; unknown option.. discard packet or not?
  186. ; check highest two bits
  187.         test    al, 0xc0        ; discard packet
  188.         jnz     .dump_options
  189.  
  190.   .pad_n:
  191.         movzx   eax, byte[esi + 1]
  192.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - pad %u\n", eax
  193.         inc     esi
  194.         inc     esi
  195.         add     esi, eax
  196.         sub     ecx, eax
  197.         jmp     .hop_by_hop
  198.  
  199.   .pad_1:
  200.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - pad 1\n"
  201.         inc     esi
  202.         dec     ecx
  203.         jmp     .hop_by_hop
  204.  
  205.  
  206.  
  207.   .dest_opts:
  208.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - dest opts\n"
  209.         jmp     .nextheader
  210.  
  211.   .routing:
  212.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - routing\n"
  213.         pushw   [esi]                   ; 8 bit identifier for option type
  214.         movzx   eax, byte[esi + 1]      ; Hdr Ext Len
  215.         inc     eax                     ; first 8 octets not counted
  216.         shl     eax, 3                  ; * 8
  217.         sub     ecx, eax
  218.         push    ecx
  219.         add     eax, esi
  220.         push    eax
  221.         inc     esi
  222.         inc     esi
  223.  
  224.         cmp     al, 0
  225.         je      .pad_1
  226.  
  227.         cmp     al, 1
  228.         je      .pad_n
  229.  
  230.         mov     al, [esi]       ; routing type
  231.  
  232.         jmp     .nextheader
  233.  
  234.   .fragment:
  235.         DEBUGF  DEBUG_NETWORK_VERBOSE, "IPv6_input - fragment\n"
  236.  
  237.         jmp     .nextheader
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. ;-----------------------------------------------------------------;
  245. ;                                                                 ;
  246. ; ipv6_api: Part of system function 76                            ;
  247. ;                                                                 ;
  248. ;  IN:  bl = subfunction number                                   ;
  249. ;       bh = device number                                        ;
  250. ;       ecx, edx, .. depends on subfunction                       ;
  251. ;                                                                 ;
  252. ; OUT:  depends on subfunction                                    ;
  253. ;                                                                 ;
  254. ;-----------------------------------------------------------------;
  255. align 4
  256. ipv6_api:
  257.  
  258.         movzx   eax, bh
  259.         shl     eax, 2
  260.  
  261.         and     ebx, 0x000000ff
  262.         cmp     ebx, .number
  263.         ja      .error
  264.         jmp     dword [.table + 4*ebx]
  265.  
  266.   .table:
  267.         dd      .packets_tx     ; 0
  268.         dd      .packets_rx     ; 1
  269. ;        dd      .read_ip        ; 2
  270. ;        dd      .write_ip       ; 3
  271. ;        dd      .read_dns       ; 4
  272. ;        dd      .write_dns      ; 5
  273. ;        dd      .read_subnet    ; 6
  274. ;        dd      .write_subnet   ; 7
  275. ;        dd      .read_gateway   ; 8
  276. ;        dd      .write_gateway  ; 9
  277.   .number = ($ - .table) / 4 - 1
  278.  
  279.   .error:
  280.         mov     eax, -1
  281.         ret
  282.  
  283.   .packets_tx:
  284.         mov     eax, [IPv6.packets_tx + eax]
  285.         ret
  286.  
  287.   .packets_rx:
  288.         mov     eax, [IPv6.packets_rx + eax]
  289.         ret
  290.  
  291.