Subversion Repositories Kolibri OS

Rev

Rev 7679 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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