Subversion Repositories Kolibri OS

Rev

Rev 2950 | 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.  
  19.         VersionAndType  db ?
  20.         Code            db ?
  21.         SessionID       dw ?
  22.         Length          dw ?            ; Length of payload, does NOT include the length PPPoE header.
  23.         Payload         rb 0
  24.  
  25. ends
  26.  
  27.  
  28. uglobal
  29.  
  30.         PPPoE_SID       dw ?
  31.         PPPoE_MAC       dp ?
  32.  
  33. endg
  34.  
  35.  
  36. ;-----------------------------------------------------------------
  37. ;
  38. ; IPv4_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  2,"PPPoE_discovery_input\n"
  69.  
  70. ; First, find open PPPoE socket
  71.  
  72.         mov     eax, net_sockets
  73.  
  74.   .next_socket:
  75.         mov     eax, [eax + SOCKET.NextPtr]
  76.         or      eax, eax
  77.         jz      .dump
  78.  
  79.         cmp     [eax + SOCKET.Domain], AF_PPP
  80.         jne     .next_socket
  81.  
  82.         cmp     [eax + SOCKET.Protocol], PPP_PROTO_ETHERNET
  83.         jne     .next_socket
  84.  
  85. ; Now, send it to the this socket
  86.  
  87.         mov     ecx, [esp + 4]
  88.         mov     esi, [esp]
  89.  
  90.         jmp     SOCKET_input
  91.  
  92.   .dump:
  93.         DEBUGF 1,'PPPoE_discovery_input: dumping\n'
  94.         call    kernel_free
  95.         add     esp, 4
  96.         ret
  97.  
  98.  
  99. ;--------------------------------------
  100. ;
  101. ; Send discovery packet
  102. ;
  103. ; IN: eax = socket pointer
  104. ;     ecx = number of bytes to send
  105. ;     esi = pointer to data
  106. ;
  107. ;--------------------------------------
  108.  
  109. align 4
  110. PPPoE_discovery_output:
  111.  
  112.         DEBUGF  2,"PPPoE_discovery_output: socket=%x buffer=%x size=%d\n", eax, esi, ecx
  113.  
  114. ; RFC2516: An entire PADI packet (including the PPPoE header) MUST NOT
  115. ; exceed 1484 octets.
  116.         cmp     ecx, 1484 + 14
  117.         ja      .bad
  118.  
  119. ; Check that device exists and is ethernet device
  120.         mov     ebx, [eax + SOCKET.device]
  121.  
  122.         cmp     ebx, MAX_NET_DEVICES
  123.         ja      .bad
  124.  
  125.         mov     ebx, [NET_DRV_LIST + 4*ebx]
  126.         test    ebx, ebx
  127.         jz      .bad
  128.  
  129.         cmp     [ebx + NET_DEVICE.type], NET_TYPE_ETH
  130.         jne     .bad
  131.  
  132.         DEBUGF  2,"PPPoE_discovery_output: device=%x\n", ebx
  133.  
  134. ; Create packet.
  135.         push    ecx esi
  136.         stdcall kernel_alloc, 1500
  137.         pop     esi ecx
  138.         test    eax, eax
  139.         jz      .bad
  140.  
  141.         mov     edx, ecx
  142.         mov     edi, eax
  143.         rep     movsb
  144.  
  145.         cmp     edx, 60         ; Min ETH size
  146.         ja      @f
  147.         mov     edx, 60
  148.        @@:
  149.  
  150.         push    edx eax         ; size and packet ptr for driver send proc
  151.  
  152. ; Overwrite source MAC and protocol type
  153.         lea     edi, [eax + ETH_header.SrcMAC]
  154.         lea     esi, [ebx + ETH_DEVICE.mac]
  155.         movsd
  156.         movsw
  157.         mov     ax, ETHER_PPP_DISCOVERY
  158.         stosw
  159.  
  160. ; And send the packet
  161.         call    [ebx + NET_DEVICE.transmit]
  162.  
  163.         xor     eax, eax
  164.         ret
  165.  
  166.   .bad:
  167.         or      eax, -1
  168.         ret
  169.  
  170.  
  171. ;-----------------------------------------------------------------
  172. ;
  173. ; PPPoE session input
  174. ;
  175. ; Handler of received Ethernet packet with type = Session
  176. ;
  177. ;
  178. ;  IN:  Pointer to buffer in [esp]
  179. ;       size of buffer in [esp+4]
  180. ;       pointer to device struct in ebx
  181. ;       pointer to PPP header in edx
  182. ;       size of PPP packet in ecx
  183. ;  OUT: /
  184. ;
  185. ;-----------------------------------------------------------------
  186. align 4
  187. PPPoE_session_input:
  188.  
  189.         cmp     [edx + PPPoE_frame.VersionAndType], 0x11
  190.         jne     .dump
  191.  
  192.         cmp     [edx + PPPoE_frame.Code], 0x00
  193.         jne     .dump
  194.  
  195.         movzx   ecx, [edx + PPPoE_frame.Length]
  196.         xchg    cl, ch
  197.  
  198.         mov     ax, [edx + PPPoE_frame.SessionID]
  199.         DEBUGF  2,"PPPoE_input: session ID=%x, length=%u\n", ax, cx
  200.         cmp     ax, [PPPoE_SID]
  201.         jne     .dump
  202.  
  203.         mov     ax, word [edx + PPPoE_frame.Payload]
  204.         add     edx, PPPoE_frame.Payload + 2
  205.  
  206.         cmp     ax, PPP_IPv4
  207.         je      IPv4_input
  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.  
  277. align 4
  278. PPPoE_start_connection:
  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.         xor     eax, eax
  299.         mov     [PPPoE_SID], ax
  300.         mov     dword [PPPoE_MAC], eax
  301.         mov     word [PPPoE_MAC + 4], ax
  302.  
  303.         ret
  304.  
  305.  
  306. ;---------------------------------------------------------------------------
  307. ;
  308. ; PPPoE API
  309. ;
  310. ; This function is called by system function 75
  311. ;
  312. ; IN:  subfunction number in bl
  313. ;      device number in bh
  314. ;      ecx, edx, .. depends on subfunction
  315. ;
  316. ; OUT:
  317. ;
  318. ;---------------------------------------------------------------------------
  319. align 4
  320. PPPoE_api:
  321.  
  322.         movzx   eax, bh
  323.         shl     eax, 2
  324.  
  325.         and     ebx, 0xff
  326.         cmp     ebx, .number
  327.         ja      .error
  328.         jmp     dword [.table + 4*ebx]
  329.  
  330.   .table:
  331.         dd      PPPoE_start_connection  ; 0
  332.         dd      PPPoE_stop_connection   ; 1
  333.   .number = ($ - .table) / 4 - 1
  334.  
  335.   .error:
  336.         mov     eax, -1
  337.         ret
  338.