Subversion Repositories Kolibri OS

Rev

Rev 3562 | Go to most recent revision | Blame | Compare with Previous | 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_LCP                 = 0x21c0        ; Link Configure Protocol
  41. PPP_CBCP                = 0x29c0        ; CallBack Control Protocol
  42. PPP_PAP                 = 0x23c0        ; Password Authenication Protocol packet
  43. PPP_CHAP                = 0x23c2        ; Challenge Handshake Authentication Protocol
  44. PPP_IPCP                = 0x2180        ; Internet Protocol Configure Protocol (maybe this should be in kernel?)
  45. PPP_CCP                 = 0xfd80        ; Compression Configure Protocol
  46.  
  47. ; PPP Active Discovery...
  48. PPPoE_PADI      = 0x09  ; .. Initiation
  49. PPPoE_PADO      = 0x07  ; .. Offer
  50. PPPoE_PADR      = 0x19  ; .. Request
  51. PPPoE_PADS      = 0x65  ; .. Session-confirmation
  52. PPPoE_PADT      = 0xa7  ; .. Terminate
  53.  
  54. TAG_EOL         = 0x0000
  55. TAG_SERVICE_NAME= 0x0101
  56. TAG_AC_NAME     = 0x0201
  57. TAG_HOST_UNIQ   = 0x0301
  58. TAG_AC_COOKIE   = 0x0401
  59.  
  60. LCP_config_request      = 1
  61. LCP_config_ack          = 2
  62. LCP_config_nak          = 3
  63. LCP_config_reject       = 4
  64. LCP_terminate_request   = 5
  65. LCP_terminate_ack       = 6
  66. LCP_code_reject         = 7
  67. LCP_protocol_reject     = 8
  68. LCP_echo_request        = 9
  69. LCP_echo_reply          = 10
  70. LCP_discard_request     = 11
  71.  
  72. struct  ETH_frame
  73.         DestMac         dp ?
  74.         SrcMac          dp ?
  75.         Type            dw ?
  76. ends
  77.  
  78. struct  PPPoE_frame     ETH_frame
  79.         VersionAndType  db ?
  80.         Code            db ?
  81.         SessionID       dw ?
  82.         Length          dw ?            ; Length of payload, does NOT include the length PPPoE header.
  83.         Payload         rb 0
  84. ends
  85.  
  86. struct  PPP_frame       PPPoE_frame
  87.         Protocol        dw ?
  88. ends
  89.  
  90. struct  LCP_frame       PPP_frame
  91.         LCP_Code        db ?
  92.         LCP_Identifier  db ?
  93.         LCP_Length      dw ?
  94.         LCP_Data        rb 0
  95. ends
  96.  
  97. ; entry point
  98. start:
  99. ; load libraries
  100.         stdcall dll.Load, @IMPORT
  101.         test    eax, eax
  102.         jnz     exit
  103. ; initialize console
  104.         push    1
  105.         call    [con_start]
  106.         push    title
  107.         push    25
  108.         push    80
  109.         push    25
  110.         push    80
  111.         call    [con_init]
  112.  
  113. main:
  114.         mcall   40,  1 shl 7
  115.  
  116.         call    [con_cls]
  117. ; Welcome user
  118.         push    str1
  119.         call    [con_write_asciiz]
  120.  
  121.         mcall   socket, 777, 3, 666
  122.         mov     [socketnum], eax
  123.         mcall   send, [socketnum], PADI, PADI.length, 0
  124.  
  125. mainloop:
  126.         mcall   10
  127.  
  128.         call    [con_get_flags]
  129.         test    eax, 0x200                      ; con window closed?
  130.         jnz     close_conn
  131.  
  132.         mcall   recv, [socketnum], buffer, 4096
  133.         cmp     eax, sizeof.PPPoE_frame
  134.         jb      mainloop
  135.  
  136.         cmp     word [buffer + ETH_frame.Type], ETHER_PPP_SESSION
  137.         je      SESSION_input
  138.  
  139.         cmp     word [buffer + ETH_frame.Type], ETHER_PPP_DISCOVERY
  140.         jne     mainloop
  141.  
  142.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADO
  143.         je      pado
  144.  
  145.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADS
  146.         je      pads
  147.  
  148.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADT
  149.         je      padt
  150.  
  151.         jmp     mainloop
  152.  
  153. pado:
  154.  
  155.         push    str2
  156.         call    [con_write_asciiz]
  157.  
  158.         lea     esi, [buffer + ETH_frame.SrcMac]                ; source mac -> dest mac
  159.         lea     edi, [buffer + ETH_frame.DestMac]
  160.         movsw
  161.         movsd
  162.  
  163.         mov     byte [buffer + PPPoE_frame.Code], PPPoE_PADR    ; change packet type to PADR
  164.  
  165.         mov     al, byte [buffer + PPPoE_frame.Length + 1]      ; get packet size
  166.         mov     ah, byte [buffer + PPPoE_frame.Length + 0]
  167.         movzx   esi, ax
  168.         add     esi, sizeof.PPPoE_frame
  169.         mcall   send, [socketnum], buffer, , 0                  ; now send it!
  170.  
  171.         jmp     mainloop
  172.  
  173.  
  174. pads:
  175.  
  176.         push    str3
  177.         call    [con_write_asciiz]
  178.  
  179.         mov     edx, dword [buffer + ETH_frame.SrcMac]          ; source mac -> dest mac
  180.         mov      si, word [buffer + ETH_frame.SrcMac + 4]
  181.         mov     dword [PADT.mac], edx
  182.         mov     word [PADT.mac + 4], si
  183.  
  184.         mov     cx, word [buffer + PPPoE_frame.SessionID]       ; and Session ID
  185.         mov     [PADT.sid], cx
  186.  
  187.         mcall   76, API_PPPOE + 0               ; Start PPPoE session
  188.  
  189.         jmp     mainloop
  190.  
  191. padt:
  192.  
  193.         push    str4
  194.         call    [con_write_asciiz]
  195.  
  196.         mcall   76, API_PPPOE + 1               ; Stop PPPoE session
  197.  
  198. exit:
  199.         mcall   close, [socketnum]
  200.         mcall   -1
  201.  
  202.  
  203. close_conn:
  204.  
  205.         mcall   send, [socketnum], PADT, PADT.length, 0
  206.         jmp     exit
  207.  
  208.  
  209. SESSION_input:
  210.  
  211.         mov     ax, word[buffer + PPP_frame.Protocol]
  212.  
  213.         cmp     ax, PPP_LCP
  214.         je      LCP_input
  215.  
  216.         cmp     ax, PPP_CBCP
  217.         je      CBCP_input
  218.  
  219.         cmp     ax, PPP_PAP
  220.         je      PAP_input
  221.  
  222.         cmp     ax, PPP_CHAP
  223.         je      CHAP_input
  224.  
  225.         cmp     ax, PPP_IPCP
  226.         je      IPCP_input
  227.  
  228.         cmp     ax, PPP_CCP
  229.         je      CCP_input
  230.  
  231.         jmp     mainloop
  232.  
  233.  
  234.  
  235. LCP_input:
  236.  
  237.         stdcall con_write_asciiz, str_lcp
  238.  
  239.         cmp     [buffer + LCP_frame.LCP_Code], LCP_echo_request
  240.         je      .echo
  241.  
  242.   .dump:
  243.         jmp     mainloop
  244.  
  245.   .echo:
  246.         mov     [buffer + LCP_frame.LCP_Code], LCP_echo_reply
  247.  
  248.         lea     esi, [buffer + ETH_frame.SrcMac]        ; source mac -> dest mac
  249.         lea     edi, [buffer + ETH_frame.DestMac]
  250.         movsw
  251.         movsd
  252.  
  253.         mov     esi, eax
  254.         mcall   send, [socketnum], buffer, , 0          ; now send it back!
  255.  
  256.         jmp     mainloop
  257.  
  258. CBCP_input:
  259.  
  260.         stdcall con_write_asciiz, str_cbcp
  261.  
  262.         jmp     mainloop
  263.  
  264. PAP_input:
  265.  
  266.         stdcall con_write_asciiz, str_pap
  267.  
  268.         jmp     mainloop
  269.  
  270. CHAP_input:
  271.  
  272.         stdcall con_write_asciiz, str_chap
  273.  
  274.         jmp     mainloop
  275.  
  276. IPCP_input:
  277.  
  278.         stdcall con_write_asciiz, str_ipcp
  279.  
  280.         jmp     mainloop
  281.  
  282. CCP_input:
  283.  
  284.         stdcall con_write_asciiz, str_ccp
  285.  
  286.         jmp     mainloop
  287.  
  288. ; data
  289. title   db      'PPPoE',0
  290. str1    db      'Sending PADI',13,10,0
  291. str2    db      'Got PADO',13,10,'Sending PADR',13,10,0
  292. str3    db      'Got PADS',13,10,'starting PPPoE session',13,10,0
  293. str4    db      'Got PADT - connection terminated by Access Concentrator',13,10,0
  294. str_lcp db      'Got LCP packet',13,10,0
  295. str_cbcp db     'got CBCP packet',13,10,0
  296. str_pap db      'got PAP packet',13,10,0
  297. str_chap db     'got CHAP packet',13,10,0
  298. str_ipcp db     'got IPCP packet',13,10,0
  299. str_ccp db      'got CCP packet',13,10,0
  300.  
  301.  
  302. PADI:
  303.         dp      0xffffffffffff          ; dest mac: broadcast
  304.         dp      0                       ; source mac (overwritten by kernel)
  305.         dw      ETHER_PPP_DISCOVERY     ; type
  306.  
  307.         db      0x11                    ; Version and Type
  308.         db      PPPoE_PADI              ; Code
  309.         dw      0                       ; session ID
  310.         dw      20 shl 8                ; Payload Length
  311.  
  312.         dw      TAG_SERVICE_NAME        ; tag
  313.         dw      0x0000                  ; length
  314.  
  315.         dw      TAG_HOST_UNIQ           ; tag
  316.         dw      0x0c00                  ; length = 12 bytes
  317.  
  318.         dd      0xdead                  ; some random id
  319.         dd      0xbeef
  320.         dd      0x1337
  321.  
  322.         .length = $ - PADI
  323.  
  324. PADT:
  325.  
  326.   .mac  dp      0                       ; Dest mac, to be filled in
  327.         dp      0                       ; source mac (overwritten by kernel)
  328.         dw      ETHER_PPP_DISCOVERY     ; Type
  329.  
  330.         db      0x11                    ; Version and Type
  331.         db      PPPoE_PADT              ; Code: terminate connection
  332.   .sid  dw      0                       ; session id, to be filled in
  333.         dw      0                       ; PAyload length = 0
  334.  
  335.         .length = $ - PADT
  336.  
  337.  
  338. ; import
  339. align 4
  340. @IMPORT:
  341.  
  342. library console, 'console.obj'
  343. import  console,        \
  344.         con_start,      'START',        \
  345.         con_init,       'con_init',     \
  346.         con_write_asciiz,       'con_write_asciiz',     \
  347.         con_exit,       'con_exit',     \
  348.         con_gets,       'con_gets',\
  349.         con_cls,        'con_cls',\
  350.         con_getch2,     'con_getch2',\
  351.         con_set_cursor_pos, 'con_set_cursor_pos',\
  352.         con_write_string, 'con_write_string',\
  353.         con_get_flags,  'con_get_flags'
  354.  
  355.  
  356. i_end:
  357.  
  358. socketnum       dd ?
  359.  
  360. buffer          rb 4096
  361.                 rb 4096    ; stack
  362. mem:
  363.