Subversion Repositories Kolibri OS

Rev

Rev 3647 | Rev 3711 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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