Subversion Repositories Kolibri OS

Rev

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