Subversion Repositories Kolibri OS

Rev

Rev 2960 | 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.asm - PPPoE dialer for KolibriOS                         ;;
  7. ;;                                                                 ;;
  8. ;;  Written by hidnplayr@kolibrios.org                             ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. format binary as ""
  16.  
  17. use32
  18.  
  19.         db      'MENUET01'      ; signature
  20.         dd      1               ; header version
  21.         dd      start           ; entry point
  22.         dd      i_end           ; initialized size
  23.         dd      mem             ; required memory
  24.         dd      mem             ; stack pointer
  25.         dd      0               ; parameters
  26.         dd      0               ; path
  27.  
  28. include '../macros.inc'
  29. purge mov,add,sub
  30. include '../proc32.inc'
  31. include '../dll.inc'
  32. include '../network.inc'
  33. include '../struct.inc'
  34.  
  35. ; Ethernet protocol numbers
  36. ETHER_PPP_DISCOVERY     = 0x6388
  37. ETHER_PPP_SESSION       = 0x6488
  38.  
  39. ; PPP protocol numbers
  40. PPP_IPv4                = 0x2100
  41. PPP_LCP                 = 0x21c0
  42.  
  43. ; PPP Active Discovery...
  44. PPPoE_PADI      = 0x09  ; .. Initiation
  45. PPPoE_PADO      = 0x07  ; .. Offer
  46. PPPoE_PADR      = 0x19  ; .. Request
  47. PPPoE_PADS      = 0x65  ; .. Session-confirmation
  48. PPPoE_PADT      = 0xa7  ; .. Terminate
  49.  
  50. TAG_EOL         = 0x0000
  51. TAG_SERVICE_NAME= 0x0101
  52. TAG_AC_NAME     = 0x0201
  53. TAG_HOST_UNIQ   = 0x0301
  54. TAG_AC_COOKIE   = 0x0401
  55.  
  56. LCP_config_request      = 1
  57. LCP_config_ack          = 2
  58. LCP_config_nak          = 3
  59. LCP_config_reject       = 4
  60. LCP_terminate_request   = 5
  61. LCP_terminate_ack       = 6
  62. LCP_code_reject         = 7
  63. LCP_protocol_reject     = 8
  64. LCP_echo_request        = 9
  65. LCP_echo_reply          = 10
  66. LCP_discard_request     = 11
  67.  
  68. struct  ETH_frame
  69.         DestMac         dp ?
  70.         SrcMac          dp ?
  71.         Type            dw ?
  72. ends
  73.  
  74. struct  PPPoE_frame     ETH_frame
  75.         VersionAndType  db ?
  76.         Code            db ?
  77.         SessionID       dw ?
  78.         Length          dw ?            ; Length of payload, does NOT include the length PPPoE header.
  79.         Payload         rb 0
  80. ends
  81.  
  82. struct  PPP_frame       PPPoE_frame
  83.         Protocol        dw ?
  84. ends
  85.  
  86. struct  LCP_frame       PPP_frame
  87.         LCP_Code        db ?
  88.         LCP_Identifier  db ?
  89.         LCP_Length      dw ?
  90.         LCP_Data        rb 0
  91. ends
  92.  
  93. ; entry point
  94. start:
  95. ; load libraries
  96.         stdcall dll.Load, @IMPORT
  97.         test    eax, eax
  98.         jnz     exit
  99. ; initialize console
  100.         push    1
  101.         call    [con_start]
  102.         push    title
  103.         push    25
  104.         push    80
  105.         push    25
  106.         push    80
  107.         call    [con_init]
  108.  
  109. main:
  110.         mcall   40,  1 shl 7
  111.  
  112.         call    [con_cls]
  113. ; Welcome user
  114.         push    str1
  115.         call    [con_write_asciiz]
  116.  
  117.         mcall   socket, 777, 3, 666
  118.         mov     [socketnum], eax
  119.         mcall   send, [socketnum], PADI, PADI.length, 0
  120.  
  121. mainloop:
  122.         mcall   10
  123.  
  124.         call    [con_get_flags]
  125.         test    eax, 0x200                      ; con window closed?
  126.         jnz     close_conn
  127.  
  128.         mcall   recv, [socketnum], buffer, 4096
  129.         cmp     eax, sizeof.PPPoE_frame
  130.         jb      mainloop
  131.  
  132.         cmp     word [buffer + ETH_frame.Type], ETHER_PPP_SESSION
  133.         je      LCP_input
  134.  
  135.         cmp     word [buffer + ETH_frame.Type], ETHER_PPP_DISCOVERY
  136.         jne     mainloop
  137.  
  138.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADO
  139.         je      pado
  140.  
  141.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADS
  142.         je      pads
  143.  
  144.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADT
  145.         je      padt
  146.  
  147.         jmp     mainloop
  148.  
  149. pado:
  150.  
  151.         push    str2
  152.         call    [con_write_asciiz]
  153.  
  154.         lea     esi, [buffer + ETH_frame.SrcMac]                ; source mac -> dest mac
  155.         lea     edi, [buffer + ETH_frame.DestMac]
  156.         movsw
  157.         movsd
  158.  
  159.         mov     byte [buffer + PPPoE_frame.Code], PPPoE_PADR    ; change packet type to PADR
  160.  
  161.         mov     al, byte [buffer + PPPoE_frame.Length + 1]      ; get packet size
  162.         mov     ah, byte [buffer + PPPoE_frame.Length + 0]
  163.         movzx   esi, ax
  164.         add     esi, sizeof.PPPoE_frame
  165.  
  166.         mcall   send, [socketnum], buffer, , 0  ; now send it!
  167.  
  168.         jmp     mainloop
  169.  
  170.  
  171. pads:
  172.  
  173.         push    str3
  174.         call    [con_write_asciiz]
  175.  
  176.         mov     edx, dword [buffer + ETH_frame.SrcMac]                ; source mac -> dest mac
  177.         mov      si, word [buffer + ETH_frame.SrcMac + 4]
  178.         mov     dword [PADT.mac], edx
  179.         mov     word [PADT.mac + 4], si
  180.  
  181.         mov     cx, word [buffer + PPPoE_frame.SessionID]       ; and Session ID
  182.         mov     [PADT.sid], cx
  183.  
  184.         mcall   76, API_PPPOE + 0               ; Start PPPoE session
  185.  
  186.         jmp     mainloop
  187.  
  188. padt:
  189.  
  190.         push    str4
  191.         call    [con_write_asciiz]
  192.  
  193.         mcall   76, API_PPPOE + 1
  194.  
  195. exit:
  196.         mcall   close, [socketnum]
  197.         mcall   -1
  198.  
  199.  
  200. close_conn:
  201.  
  202.         mcall   send, [socketnum], PADT, 14 + 6, 0
  203.         jmp     exit
  204.  
  205.  
  206. LCP_input:
  207.  
  208.         cmp     word [buffer + PPP_frame.Protocol], PPP_LCP
  209.         jne     mainloop
  210.  
  211.         cmp     [buffer + LCP_frame.LCP_Code], LCP_echo_request
  212.         je      .echo
  213.  
  214.   .dump:
  215.         jmp     mainloop
  216.  
  217.   .echo:
  218.         mov     [buffer + LCP_frame.LCP_Code], LCP_echo_reply
  219.  
  220.         push    dword [buffer + ETH_frame.DestMac]
  221.         push    dword [buffer + ETH_frame.SrcMac]
  222.         pop     dword [buffer + ETH_frame.DestMac]
  223.         pop     dword [buffer + ETH_frame.SrcMac]
  224.         push    word [buffer + ETH_frame.DestMac + 4]
  225.         push    word [buffer + ETH_frame.SrcMac + 4]
  226.         pop     word [buffer + ETH_frame.DestMac + 4]
  227.         pop     word [buffer + ETH_frame.SrcMac + 4]
  228.  
  229.         mov     esi, eax
  230.         mcall   send, [socketnum], buffer, , 0  ; now send it!
  231.  
  232.         jmp     mainloop
  233.  
  234. ; data
  235. title   db      'PPPoE',0
  236. str1    db      'Sending PADI',13,10,0
  237. str2    db      'Got PADO',13,10,'Sending PADR',13,10,0
  238. str3    db      'Got PADS',13,10,'starting PPPoE session',13,10,0
  239. str4    db      'Got PADT - connection terminated by Access Concentrator',13,10,0
  240.  
  241.  
  242. PADI:
  243.         dp      -1              ; dest mac
  244.         dp      0               ; source mac (overwritten by kernel)
  245.         dw      0               ; type       (overwritten by kernel)
  246.  
  247.         db      0x11
  248.         db      PPPoE_PADI
  249.         dw      0               ; session ID
  250.         dw      20 shl 8
  251.  
  252.         dw      TAG_SERVICE_NAME
  253.         dw      0x0000
  254.  
  255.         dw      TAG_HOST_UNIQ
  256.         dw      0x0c00          ; 12 bytes long
  257.         dd      0xdead          ; some random id
  258.         dd      0xbeef
  259.         dd      0x1337
  260.  
  261.         .length = $ - PADI
  262.  
  263. PADT:
  264.  
  265.   .mac  dp      0
  266.         dp      0
  267.         dw      0
  268.  
  269.         db      0x11
  270.         db      PPPoE_PADT
  271.   .sid  dw      0
  272.         dw      0
  273.  
  274.  
  275. ; import
  276. align 4
  277. @IMPORT:
  278.  
  279. library console, 'console.obj'
  280. import  console,        \
  281.         con_start,      'START',        \
  282.         con_init,       'con_init',     \
  283.         con_write_asciiz,       'con_write_asciiz',     \
  284.         con_exit,       'con_exit',     \
  285.         con_gets,       'con_gets',\
  286.         con_cls,        'con_cls',\
  287.         con_getch2,     'con_getch2',\
  288.         con_set_cursor_pos, 'con_set_cursor_pos',\
  289.         con_write_string, 'con_write_string',\
  290.         con_get_flags,  'con_get_flags'
  291.  
  292.  
  293. i_end:
  294.  
  295. socketnum       dd ?
  296. sid             dw ?
  297. buffer          rb 4096
  298.                 rb 4096    ; stack
  299. mem:
  300.