Subversion Repositories Kolibri OS

Rev

Rev 5015 | Rev 5522 | 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. ;;  PPPoE.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: 5363 $
  18.  
  19.  
  20. struct  PPPoE_frame
  21.         VersionAndType  db ?
  22.         Code            db ?
  23.         SessionID       dw ?
  24.         Length          dw ?            ; Length of payload, does NOT include the length PPPoE header.
  25.         Payload         rb 0
  26. ends
  27.  
  28. uglobal
  29. align 4
  30.  
  31.         PPPoE_SID       dw ?
  32.         PPPoE_MAC       dp ?
  33.  
  34. endg
  35.  
  36. ;-----------------------------------------------------------------
  37. ;
  38. ; PPPoE_init
  39. ;
  40. ;  This function resets all IP variables
  41. ;
  42. ;-----------------------------------------------------------------
  43. macro   PPPoE_init {
  44.  
  45.         call    PPPoE_stop_connection
  46.  
  47. }
  48.  
  49.  
  50. ;-----------------------------------------------------------------
  51. ;
  52. ; PPPoE discovery input
  53. ;
  54. ; Handler of received Ethernet packet with type = Discovery
  55. ;
  56. ;
  57. ;  IN:  Pointer to buffer in [esp]
  58. ;       size of buffer in [esp+4]
  59. ;       pointer to device struct in ebx
  60. ;       pointer to PPP header in edx
  61. ;       size of PPP packet in ecx
  62. ;  OUT: /
  63. ;
  64. ;-----------------------------------------------------------------
  65. align 4
  66. PPPoE_discovery_input:
  67.  
  68.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_discovery_input\n"
  69.  
  70. ; First, find open PPPoE socket
  71.  
  72.         pusha
  73.         mov     ecx, socket_mutex
  74.         call    mutex_lock
  75.         popa
  76.  
  77.         mov     eax, net_sockets
  78.  
  79.   .next_socket:
  80.         mov     eax, [eax + SOCKET.NextPtr]
  81.         or      eax, eax
  82.         jz      .dump
  83.  
  84.         cmp     [eax + SOCKET.Domain], AF_PPP
  85.         jne     .next_socket
  86.  
  87.         cmp     [eax + SOCKET.Protocol], PPP_PROTO_ETHERNET
  88.         jne     .next_socket
  89.  
  90.         pusha
  91.         mov     ecx, socket_mutex
  92.         call    mutex_unlock
  93.         popa
  94.  
  95. ; Now, send it to the this socket
  96.  
  97.         mov     ecx, [esp + 4]
  98.         mov     esi, [esp]
  99.  
  100.         jmp     SOCKET_input
  101.  
  102.   .dump:
  103.         pusha
  104.         mov     ecx, socket_mutex
  105.         call    mutex_unlock
  106.         popa
  107.  
  108.         DEBUGF  DEBUG_NETWORK_VERBOSE, 'PPPoE_discovery_input: dumping\n'
  109.         call    NET_packet_free
  110.         add     esp, 4
  111.         ret
  112.  
  113.  
  114. ;--------------------------------------
  115. ;
  116. ; Send discovery packet
  117. ;
  118. ; IN: eax = socket pointer
  119. ;     ecx = number of bytes to send
  120. ;     esi = pointer to data
  121. ;
  122. ;--------------------------------------
  123.  
  124. align 4
  125. PPPoE_discovery_output:
  126.  
  127.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_discovery_output: socket=%x buffer=%x size=%d\n", eax, esi, ecx
  128.  
  129. ; RFC2516: An entire PADI packet (including the PPPoE header) MUST NOT
  130. ; exceed 1484 octets.
  131.         cmp     ecx, 1484 + 14
  132.         ja      .bad
  133.  
  134. ; Check that device exists and is ethernet device
  135.         mov     ebx, [eax + SOCKET.device]
  136.  
  137.         cmp     ebx, NET_DEVICES_MAX
  138.         ja      .bad
  139.  
  140.         mov     ebx, [NET_DRV_LIST + 4*ebx]
  141.         test    ebx, ebx
  142.         jz      .bad
  143.  
  144.         cmp     [ebx + NET_DEVICE.device_type], NET_DEVICE_ETH
  145.         jne     .bad
  146.  
  147.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_discovery_output: device=%x\n", ebx
  148.  
  149. ; Create packet.
  150.         push    ecx esi
  151.         stdcall kernel_alloc, 1500
  152.         pop     esi ecx
  153.         test    eax, eax
  154.         jz      .bad
  155.  
  156.         mov     edx, ecx
  157.         mov     edi, eax
  158.         rep movsb
  159.  
  160.         cmp     edx, 60         ; Min ETH size
  161.         ja      @f
  162.         mov     edx, 60
  163.        @@:
  164.  
  165.         push    edx eax         ; size and packet ptr for driver send proc
  166.  
  167. ; Overwrite source MAC and protocol type
  168.         lea     edi, [eax + ETH_header.SrcMAC]
  169.         lea     esi, [ebx + ETH_DEVICE.mac]
  170.         movsd
  171.         movsw
  172.         cmp     word[edi], ETHER_PROTO_PPP_SESSION      ; Allow only PPP_discovery, or LCP
  173.         je      @f
  174.         mov     ax, ETHER_PROTO_PPP_DISCOVERY
  175.         stosw
  176.        @@:
  177.  
  178. ; And send the packet
  179.         call    [ebx + NET_DEVICE.transmit]
  180.  
  181.         xor     eax, eax
  182.         ret
  183.  
  184.   .bad:
  185.         or      eax, -1
  186.         ret
  187.  
  188.  
  189. ;-----------------------------------------------------------------
  190. ;
  191. ; PPPoE session input
  192. ;
  193. ; Handler of received Ethernet packet with type = Session
  194. ;
  195. ;
  196. ;  IN:  Pointer to buffer in [esp]
  197. ;       size of buffer in [esp+4]
  198. ;       pointer to device struct in ebx
  199. ;       pointer to PPP header in edx
  200. ;       size of PPP packet in ecx
  201. ;  OUT: /
  202. ;
  203. ;-----------------------------------------------------------------
  204. align 4
  205. PPPoE_session_input:
  206.  
  207.         cmp     [edx + PPPoE_frame.VersionAndType], 0x11
  208.         jne     .dump
  209.  
  210.         cmp     [edx + PPPoE_frame.Code], 0x00
  211.         jne     .dump
  212.  
  213.         movzx   ecx, [edx + PPPoE_frame.Length]
  214.         xchg    cl, ch
  215.  
  216.         mov     ax, [edx + PPPoE_frame.SessionID]
  217.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: session ID=%x, length=%u\n", ax, cx
  218.         cmp     ax, [PPPoE_SID]
  219.         jne     .dump
  220.  
  221.         mov     ax, word [edx + PPPoE_frame.Payload]
  222.         add     edx, PPPoE_frame.Payload + 2
  223.  
  224.         cmp     ax, PPP_PROTO_IPv4
  225.         je      IPv4_input
  226.  
  227. ;        cmp     ax, PPP_PROTO_IPv6
  228. ;        je      IPv6_input
  229.  
  230.         jmp     PPPoE_discovery_input   ; Send LCP,CHAP,CBCP,... packets to the PPP dialer
  231.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: Unknown protocol=%x\n", ax
  232.  
  233.   .dump:
  234.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: dumping\n"
  235.         call    NET_packet_free
  236.         add     esp, 4
  237.         ret
  238.  
  239.  
  240.  
  241.  
  242. ;-----------------------------------------------------------------
  243. ;
  244. ; PPPoE_output
  245. ;
  246. ; IN:  ax = protocol
  247. ;     ebx = device ptr
  248. ;     ecx = packet size
  249. ;
  250. ; OUT: eax = buffer start / 0 on error
  251. ;      ebx = device ptr
  252. ;      ecx = packet size
  253. ;      edx = size of complete buffer
  254. ;      edi = start of PPP payload
  255. ;
  256. ;-----------------------------------------------------------------
  257. align 4
  258. PPPoE_output:
  259.  
  260.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_output: size=%u device=%x\n", ecx, ebx
  261.  
  262.         pushw   ax
  263.         pushw   [PPPoE_SID]
  264.  
  265.         mov     ax, ETHER_PROTO_PPP_SESSION
  266.         add     ecx, PPPoE_frame.Payload + 2
  267.         lea     edx, [PPPoE_MAC]
  268.         call    ETH_output
  269.         jz      .eth_error
  270.  
  271.         sub     ecx, PPPoE_frame.Payload
  272.         mov     [edi + PPPoE_frame.VersionAndType], 0x11
  273.         mov     [edi + PPPoE_frame.Code], 0
  274.         popw    [edi + PPPoE_frame.SessionID]
  275.         xchg    cl, ch
  276.         mov     [edi + PPPoE_frame.Length], cx
  277.         xchg    cl, ch
  278.  
  279.         pop     word [edi + PPPoE_frame.Payload]
  280.  
  281.         sub     ecx, 2
  282.         add     edi, PPPoE_frame.Payload + 2
  283.  
  284.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_output: success!\n"
  285.         ret
  286.  
  287.  
  288.   .eth_error:
  289.         add     esp, 4
  290.         xor     eax, eax
  291.         ret
  292.  
  293.  
  294. PPPoE_start_connection:
  295.  
  296.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_start_connection: %x\n", cx
  297.  
  298.         cmp     [PPPoE_SID], 0
  299.         jne     .fail
  300.  
  301.         mov     [PPPoE_SID], cx
  302.         mov     dword [PPPoE_MAC], edx
  303.         mov     word [PPPoE_MAC + 4], si
  304.  
  305.         xor     eax, eax
  306.         ret
  307.  
  308.   .fail:
  309.         or      eax, -1
  310.         ret
  311.  
  312.  
  313. align 4
  314. PPPoE_stop_connection:
  315.  
  316.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_stop_connection\n"
  317.  
  318.         xor     eax, eax
  319.         mov     [PPPoE_SID], ax
  320.         mov     dword [PPPoE_MAC], eax
  321.         mov     word [PPPoE_MAC + 4], ax
  322.  
  323.         ret
  324.  
  325.  
  326. ;---------------------------------------------------------------------------
  327. ;
  328. ; PPPoE API
  329. ;
  330. ; This function is called by system function 75
  331. ;
  332. ; IN:  subfunction number in bl
  333. ;      device number in bh
  334. ;      ecx, edx, .. depends on subfunction
  335. ;
  336. ; OUT:
  337. ;
  338. ;---------------------------------------------------------------------------
  339. align 4
  340. PPPoE_api:
  341.  
  342.         movzx   eax, bh
  343.         shl     eax, 2
  344.  
  345.         and     ebx, 0xff
  346.         cmp     ebx, .number
  347.         ja      .error
  348.         jmp     dword [.table + 4*ebx]
  349.  
  350.   .table:
  351.         dd      PPPoE_start_connection  ; 0
  352.         dd      PPPoE_stop_connection   ; 1
  353.   .number = ($ - .table) / 4 - 1
  354.  
  355.   .error:
  356.         mov     eax, -1
  357.         ret
  358.