Subversion Repositories Kolibri OS

Rev

Rev 5522 | Rev 7679 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2012-2015. 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: 6011 $
  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_DRV_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.         push    ecx esi
  145. ;;;; FIXME        stdcall kernel_alloc, 1500
  146.         pop     esi ecx
  147.         test    eax, eax
  148.         jz      .bad
  149.  
  150.         mov     edx, ecx
  151.         mov     edi, eax
  152.         rep movsb
  153.  
  154.         cmp     edx, 60         ; Min ETH size
  155.         ja      @f
  156.         mov     edx, 60
  157.        @@:
  158.  
  159.         push    edx eax         ; size and packet ptr for driver send proc
  160.  
  161. ; Overwrite source MAC and protocol type
  162.         lea     edi, [eax + ETH_header.SrcMAC]
  163.         lea     esi, [ebx + ETH_DEVICE.mac]
  164.         movsd
  165.         movsw
  166.         cmp     word[edi], ETHER_PROTO_PPP_SESSION      ; Allow only PPP_discovery, or LCP
  167.         je      @f
  168.         mov     ax, ETHER_PROTO_PPP_DISCOVERY
  169.         stosw
  170.        @@:
  171.  
  172. ; And send the packet
  173.         call    [ebx + NET_DEVICE.transmit]
  174.  
  175.         xor     eax, eax
  176.         ret
  177.  
  178.   .bad:
  179.         or      eax, -1
  180.         ret
  181.  
  182.  
  183. ;-----------------------------------------------------------------;
  184. ;                                                                 ;
  185. ; pppoe_session_input                                             ;
  186. ;                                                                 ;
  187. ;  IN:  [esp] = ptr to buffer                                     ;
  188. ;       [esp+4] = size of buffer                                  ;
  189. ;       ebx = ptr to device struct                                ;
  190. ;       edx = ptr to PPP header                                   ;
  191. ;       ecx = size of PPP packet                                  ;
  192. ;                                                                 ;
  193. ;  OUT: /                                                         ;
  194. ;                                                                 ;
  195. ;-----------------------------------------------------------------;
  196. align 4
  197. pppoe_session_input:
  198.  
  199.         cmp     [edx + PPPoE_frame.VersionAndType], 0x11
  200.         jne     .dump
  201.  
  202.         cmp     [edx + PPPoE_frame.Code], 0x00
  203.         jne     .dump
  204.  
  205.         movzx   ecx, [edx + PPPoE_frame.Length]
  206.         xchg    cl, ch
  207.  
  208.         mov     ax, [edx + PPPoE_frame.SessionID]
  209.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: session ID=%x, length=%u\n", ax, cx
  210.         cmp     ax, [PPPoE_SID]
  211.         jne     .dump
  212.  
  213.         mov     ax, word [edx + PPPoE_frame.Payload]
  214.         add     edx, PPPoE_frame.Payload + 2
  215.  
  216.         cmp     ax, PPP_PROTO_IPv4
  217.         je      ipv4_input
  218.  
  219. ;        cmp     ax, PPP_PROTO_IPv6
  220. ;        je      ipv6_input
  221.  
  222.         jmp     pppoe_discovery_input   ; Send LCP,CHAP,CBCP,... packets to the PPP dialer
  223.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: Unknown protocol=%x\n", ax
  224.  
  225.   .dump:
  226.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: dumping\n"
  227.         call    net_buff_free
  228.         ret
  229.  
  230.  
  231.  
  232. ;-----------------------------------------------------------------;
  233. ;                                                                 ;
  234. ; pppoe_output                                                    ;
  235. ;                                                                 ;
  236. ;  IN:  ax = protocol                                             ;
  237. ;       ebx = device ptr                                          ;
  238. ;       ecx = packet size                                         ;
  239. ;                                                                 ;
  240. ; OUT:  eax = buffer start                                        ;
  241. ;       eax = 0 on error                                          ;
  242. ;       ebx = device ptr                                          ;
  243. ;       ecx = packet size                                         ;
  244. ;       edx = size of complete buffer                             ;
  245. ;       edi = start of PPP payload                                ;
  246. ;                                                                 ;
  247. ;-----------------------------------------------------------------;
  248. align 4
  249. pppoe_output:
  250.  
  251.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_output: size=%u device=%x\n", ecx, ebx
  252.  
  253.         pushw   ax
  254.         pushw   [PPPoE_SID]
  255.  
  256.         mov     ax, ETHER_PROTO_PPP_SESSION
  257.         add     ecx, PPPoE_frame.Payload + 2
  258.         lea     edx, [PPPoE_MAC]
  259.         call    eth_output
  260.         jz      .eth_error
  261.  
  262.         sub     ecx, PPPoE_frame.Payload
  263.         mov     [edi + PPPoE_frame.VersionAndType], 0x11
  264.         mov     [edi + PPPoE_frame.Code], 0
  265.         popw    [edi + PPPoE_frame.SessionID]
  266.         xchg    cl, ch
  267.         mov     [edi + PPPoE_frame.Length], cx
  268.         xchg    cl, ch
  269.  
  270.         pop     word [edi + PPPoE_frame.Payload]
  271.  
  272.         sub     ecx, 2
  273.         add     edi, PPPoE_frame.Payload + 2
  274.  
  275.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_output: success!\n"
  276.         ret
  277.  
  278.  
  279.   .eth_error:
  280.         add     esp, 4
  281.         xor     eax, eax
  282.         ret
  283.  
  284. align 4
  285. pppoe_start_connection:
  286.  
  287.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_start_connection: %x\n", cx
  288.  
  289.         cmp     [PPPoE_SID], 0
  290.         jne     .fail
  291.  
  292.         mov     [PPPoE_SID], cx
  293.         mov     dword [PPPoE_MAC], edx
  294.         mov     word [PPPoE_MAC + 4], si
  295.  
  296.         xor     eax, eax
  297.         ret
  298.  
  299.   .fail:
  300.         or      eax, -1
  301.         ret
  302.  
  303.  
  304. align 4
  305. pppoe_stop_connection:
  306.  
  307.         DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_stop_connection\n"
  308.  
  309.         xor     eax, eax
  310.         mov     [PPPoE_SID], ax
  311.         mov     dword [PPPoE_MAC], eax
  312.         mov     word [PPPoE_MAC + 4], ax
  313.  
  314.         ret
  315.  
  316.  
  317. ;-----------------------------------------------------------------;
  318. ;                                                                 ;
  319. ; pppoe_api: Part of system function 76                           ;
  320. ;                                                                 ;
  321. ; IN:  subfunction number in bl                                   ;
  322. ;      device number in bh                                        ;
  323. ;      ecx, edx, .. depends on subfunction                        ;
  324. ;                                                                 ;
  325. ; OUT:                                                            ;
  326. ;                                                                 ;
  327. ;-----------------------------------------------------------------;
  328. align 4
  329. pppoe_api:
  330.  
  331.         movzx   eax, bh
  332.         shl     eax, 2
  333.  
  334.         and     ebx, 0xff
  335.         cmp     ebx, .number
  336.         ja      .error
  337.         jmp     dword [.table + 4*ebx]
  338.  
  339.   .table:
  340.         dd      pppoe_start_connection  ; 0
  341.         dd      pppoe_stop_connection   ; 1
  342.   .number = ($ - .table) / 4 - 1
  343.  
  344.   .error:
  345.         mov     eax, -1
  346.         ret
  347.