Subversion Repositories Kolibri OS

Rev

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