Subversion Repositories Kolibri OS

Rev

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

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