Subversion Repositories Kolibri OS

Rev

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

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