Subversion Repositories Kolibri OS

Rev

Rev 5565 | Details | Compare with Previous | Last modification | View Log | RSS feed

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