Subversion Repositories Kolibri OS

Rev

Rev 2960 | Blame | Last modification | View Log | Download | RSS feed

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