Subversion Repositories Kolibri OS

Rev

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

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