Subversion Repositories Kolibri OS

Rev

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