Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
9049 hidnplayr 3
;; Copyright (C) KolibriOS team 2012-2021. All rights reserved. ;;
3545 hidnplayr 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
 
4850 mario79 17
$Revision: 9049 $
18
 
19
 
3545 hidnplayr 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
3698 hidnplayr 29
align 4
30
 
3545 hidnplayr 31
        PPPoE_SID       dw ?
32
        PPPoE_MAC       dp ?
3698 hidnplayr 33
 
3545 hidnplayr 34
endg
35
 
6011 hidnplayr 36
;-----------------------------------------------------------------;
37
;                                                                 ;
38
; pppoe_init: Reset all pppoe variables                           ;
39
;                                                                 ;
40
;-----------------------------------------------------------------;
41
macro   pppoe_init {
3545 hidnplayr 42
 
6011 hidnplayr 43
        call    pppoe_stop_connection
3545 hidnplayr 44
 
45
}
46
 
47
 
6011 hidnplayr 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
;-----------------------------------------------------------------;
3545 hidnplayr 61
align 4
6011 hidnplayr 62
pppoe_discovery_input:
3545 hidnplayr 63
 
3556 hidnplayr 64
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_discovery_input\n"
3545 hidnplayr 65
 
66
; First, find open PPPoE socket
67
 
3647 hidnplayr 68
        pusha
69
        mov     ecx, socket_mutex
70
        call    mutex_lock
71
        popa
72
 
3545 hidnplayr 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
 
3647 hidnplayr 86
        pusha
87
        mov     ecx, socket_mutex
88
        call    mutex_unlock
89
        popa
90
 
3545 hidnplayr 91
; Now, send it to the this socket
92
 
93
        mov     ecx, [esp + 4]
94
        mov     esi, [esp]
95
 
6011 hidnplayr 96
        jmp     socket_input
3545 hidnplayr 97
 
98
  .dump:
3647 hidnplayr 99
        pusha
100
        mov     ecx, socket_mutex
101
        call    mutex_unlock
102
        popa
103
 
3556 hidnplayr 104
        DEBUGF  DEBUG_NETWORK_VERBOSE, 'PPPoE_discovery_input: dumping\n'
6011 hidnplayr 105
        call    net_buff_free
3545 hidnplayr 106
        ret
107
 
108
 
6011 hidnplayr 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
;-----------------------------------------------------------------;
3545 hidnplayr 118
align 4
6011 hidnplayr 119
pppoe_discovery_output:
3545 hidnplayr 120
 
3556 hidnplayr 121
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_discovery_output: socket=%x buffer=%x size=%d\n", eax, esi, ecx
3545 hidnplayr 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
 
3600 hidnplayr 131
        cmp     ebx, NET_DEVICES_MAX
3545 hidnplayr 132
        ja      .bad
133
 
7679 hidnplayr 134
        mov     ebx, [net_device_list + 4*ebx]
3545 hidnplayr 135
        test    ebx, ebx
136
        jz      .bad
137
 
3600 hidnplayr 138
        cmp     [ebx + NET_DEVICE.device_type], NET_DEVICE_ETH
3545 hidnplayr 139
        jne     .bad
140
 
3556 hidnplayr 141
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_discovery_output: device=%x\n", ebx
3545 hidnplayr 142
 
143
; Create packet.
9049 hidnplayr 144
        stdcall net_buff_alloc, 1514 + NET_BUFF.data
3545 hidnplayr 145
        test    eax, eax
146
        jz      .bad
147
 
9049 hidnplayr 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
3545 hidnplayr 154
        mov     edx, ecx
9049 hidnplayr 155
        lea     edi, [eax + NET_BUFF.data]
3711 clevermous 156
        rep movsb
3545 hidnplayr 157
 
9049 hidnplayr 158
; Packet size
159
        cmp     edx, 60
3545 hidnplayr 160
        ja      @f
161
        mov     edx, 60
9049 hidnplayr 162
  @@:
163
        mov     [eax + NET_BUFF.length], edx
3545 hidnplayr 164
 
9049 hidnplayr 165
; Overwrite ETH source MAC with our own
3545 hidnplayr 166
        lea     esi, [ebx + ETH_DEVICE.mac]
9049 hidnplayr 167
        lea     edi, [eax + NET_BUFF.data + ETH_header.SrcMAC]
3545 hidnplayr 168
        movsd
169
        movsw
9049 hidnplayr 170
 
171
; Allow only PPP_discovery, or LCP
172
        cmp     word[edi], ETHER_PROTO_PPP_SESSION
3545 hidnplayr 173
        je      @f
9049 hidnplayr 174
        mov     word[edi], ETHER_PROTO_PPP_DISCOVERY
3545 hidnplayr 175
       @@:
176
 
177
; And send the packet
9049 hidnplayr 178
        stdcall [ebx + NET_DEVICE.transmit], eax
3545 hidnplayr 179
        ret
180
 
181
  .bad:
182
        or      eax, -1
183
        ret
184
 
185
 
6011 hidnplayr 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
;-----------------------------------------------------------------;
3545 hidnplayr 199
align 4
6011 hidnplayr 200
pppoe_session_input:
3545 hidnplayr 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]
3556 hidnplayr 212
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: session ID=%x, length=%u\n", ax, cx
3545 hidnplayr 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
 
3600 hidnplayr 219
        cmp     ax, PPP_PROTO_IPv4
6011 hidnplayr 220
        je      ipv4_input
3545 hidnplayr 221
 
3600 hidnplayr 222
;        cmp     ax, PPP_PROTO_IPv6
6011 hidnplayr 223
;        je      ipv6_input
3545 hidnplayr 224
 
6011 hidnplayr 225
        jmp     pppoe_discovery_input   ; Send LCP,CHAP,CBCP,... packets to the PPP dialer
3556 hidnplayr 226
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: Unknown protocol=%x\n", ax
3545 hidnplayr 227
 
228
  .dump:
3556 hidnplayr 229
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_input: dumping\n"
6011 hidnplayr 230
        call    net_buff_free
3545 hidnplayr 231
        ret
232
 
233
 
234
 
6011 hidnplayr 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
;-----------------------------------------------------------------;
3545 hidnplayr 251
align 4
6011 hidnplayr 252
pppoe_output:
3545 hidnplayr 253
 
3556 hidnplayr 254
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_output: size=%u device=%x\n", ecx, ebx
3545 hidnplayr 255
 
5015 hidnplayr 256
        pushw   ax
3545 hidnplayr 257
        pushw   [PPPoE_SID]
258
 
5015 hidnplayr 259
        mov     ax, ETHER_PROTO_PPP_SESSION
260
        add     ecx, PPPoE_frame.Payload + 2
3545 hidnplayr 261
        lea     edx, [PPPoE_MAC]
6011 hidnplayr 262
        call    eth_output
3545 hidnplayr 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
 
3556 hidnplayr 278
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_output: success!\n"
3545 hidnplayr 279
        ret
280
 
281
 
282
  .eth_error:
283
        add     esp, 4
5015 hidnplayr 284
        xor     eax, eax
3545 hidnplayr 285
        ret
286
 
6011 hidnplayr 287
align 4
288
pppoe_start_connection:
3545 hidnplayr 289
 
3556 hidnplayr 290
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_start_connection: %x\n", cx
3545 hidnplayr 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
6011 hidnplayr 308
pppoe_stop_connection:
3545 hidnplayr 309
 
3556 hidnplayr 310
        DEBUGF  DEBUG_NETWORK_VERBOSE, "PPPoE_stop_connection\n"
3545 hidnplayr 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
 
6011 hidnplayr 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
;-----------------------------------------------------------------;
3545 hidnplayr 331
align 4
6011 hidnplayr 332
pppoe_api:
3545 hidnplayr 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:
6011 hidnplayr 343
        dd      pppoe_start_connection  ; 0
344
        dd      pppoe_stop_connection   ; 1
3545 hidnplayr 345
  .number = ($ - .table) / 4 - 1
346
 
347
  .error:
348
        mov     eax, -1
349
        ret