Subversion Repositories Kolibri OS

Rev

Rev 4429 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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