Subversion Repositories Kolibri OS

Rev

Rev 3908 | Rev 5565 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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