Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2013. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  ETHERNET.INC                                                   ;;
7
;;                                                                 ;;
8
;;  Ethernet network layer for KolibriOS                           ;;
9
;;                                                                 ;;
10
;;    Written by hidnplayr@kolibrios.org                           ;;
11
;;                                                                 ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;             Version 2, June 1991                                ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
 
17
$Revision: 3346 $
18
 
19
ETH_FRAME_MINIMUM       = 60
20
 
21
struct  ETH_header
22
 
23
        DstMAC          dp  ?  ; destination MAC-address
24
        SrcMAC          dp  ?  ; source MAC-address
25
        Type            dw  ?  ; type of the upper-layer protocol
26
 
27
ends
28
 
29
struct  ETH_DEVICE      NET_DEVICE
30
 
31
        mac             dp ?
32
 
33
ends
34
 
35
align 4
36
iglobal
37
 
38
        ETH_BROADCAST   dp  0xffffffffffff
39
endg
40
 
41
;-----------------------------------------------------------------
42
;
43
; ETH_input
44
;
45
;  This function is called by ethernet drivers,
46
;  It pushes the received ethernet packets onto the eth_in_queue
47
;
48
;  IN:   [esp]  = Pointer to buffer
49
;       [esp+4] = size of buffer
50
;         ebx   = pointer to eth_device
51
;  OUT: /
52
;
53
;-----------------------------------------------------------------
54
align 4
55
ETH_input:
56
        mov     eax, [esp]
57
        mov     ecx, [esp+4]
58
 
3556 hidnplayr 59
        DEBUGF  DEBUG_NETWORK_VERBOSE,"ETH_input: size=%u\n", ecx
3545 hidnplayr 60
        cmp     ecx, ETH_FRAME_MINIMUM
61
        jb      .dump
62
        sub     ecx, sizeof.ETH_header
63
 
64
        lea     edx, [eax + sizeof.ETH_header]
65
        mov     ax, [eax + ETH_header.Type]
66
 
3600 hidnplayr 67
        cmp     ax, ETHER_PROTO_IPv4
3545 hidnplayr 68
        je      IPv4_input
69
 
3601 hidnplayr 70
        cmp     ax, ETHER_PROTO_ARP
3545 hidnplayr 71
        je      ARP_input
72
 
3601 hidnplayr 73
        cmp     ax, ETHER_PROTO_IPv6
3545 hidnplayr 74
        je      IPv6_input
75
 
3600 hidnplayr 76
        cmp     ax, ETHER_PROTO_PPP_DISCOVERY
3545 hidnplayr 77
        je      PPPoE_discovery_input
78
 
3600 hidnplayr 79
        cmp     ax, ETHER_PROTO_PPP_SESSION
3545 hidnplayr 80
        je      PPPoE_session_input
81
 
3556 hidnplayr 82
        DEBUGF  DEBUG_NETWORK_ERROR, "ETH_input: Unknown packet type=%x\n", ax
3545 hidnplayr 83
 
84
  .dump:
3556 hidnplayr 85
        DEBUGF  DEBUG_NETWORK_VERBOSE,"ETH_input: dumping\n"
3545 hidnplayr 86
        call    kernel_free
87
        add     esp, 4
88
        ret
89
 
90
;-----------------------------------------------------------------
91
;
92
; ETH_output
93
;
94
; IN: eax = pointer to source mac
95
;     ebx = device ptr
96
;     ecx = packet size
97
;     edx = pointer to destination mac
98
;      di = protocol
99
;
100
; OUT: edi = 0 on error, pointer to buffer otherwise
101
;      eax = buffer start
102
;      ebx = to device structure
103
;      ecx = unchanged (packet size of embedded data)
104
;      edx = size of complete buffer
105
;
106
;-----------------------------------------------------------------
107
align 4
108
ETH_output:
109
 
3556 hidnplayr 110
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx
3545 hidnplayr 111
 
112
        cmp     ecx, [ebx + NET_DEVICE.mtu]
113
        ja      .exit
114
 
115
        push    ecx
116
        push    di eax edx
117
 
118
        add     ecx, sizeof.ETH_header
119
        stdcall kernel_alloc, ecx
120
        test    eax, eax
121
        jz      .out_of_ram
122
        mov     edi, eax
123
 
124
        pop     esi
125
        movsd
126
        movsw
127
        pop     esi
128
        movsd
129
        movsw
130
        pop     ax
131
        stosw
132
 
133
        lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
134
        pop     ecx
135
        lea     edx, [ecx + sizeof.ETH_header]  ; Set edx to complete buffer size
136
 
137
        cmp     edx, ETH_FRAME_MINIMUM
138
        jbe     .adjust_size
139
  .done:
3556 hidnplayr 140
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx
3545 hidnplayr 141
        ret
142
 
143
  .adjust_size:
144
        mov     edx, ETH_FRAME_MINIMUM
145
        test    edx, edx        ; clear zero flag
146
        jmp     .done
147
 
148
  .out_of_ram:
3556 hidnplayr 149
        DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Out of ram!\n"
3545 hidnplayr 150
        add     esp, 4+4+2+4
151
        sub     edi, edi
152
        ret
153
 
154
  .exit:
3556 hidnplayr 155
        DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Packet too large!\n"
3545 hidnplayr 156
        sub     edi, edi
157
        ret
158
 
159
 
160
 
161
;-----------------------------------------------------------------
162
;
163
; ETH_API
164
;
3601 hidnplayr 165
; This function is called by system function 76
3545 hidnplayr 166
;
167
; IN:  subfunction number in bl
168
;      device number in bh
169
;      ecx, edx, .. depends on subfunction
170
;
171
; OUT:
172
;
173
;-----------------------------------------------------------------
174
align 4
175
ETH_api:
176
 
3600 hidnplayr 177
        cmp     bh, NET_DEVICES_MAX
3545 hidnplayr 178
        ja      .error
179
        movzx   eax, bh
180
        mov     eax, dword [NET_DRV_LIST + 4*eax]
3600 hidnplayr 181
        cmp     [eax + NET_DEVICE.device_type], NET_DEVICE_ETH
3545 hidnplayr 182
        jne     .error
183
 
184
        and     ebx, 0xff
185
        cmp     ebx, .number
186
        ja      .error
187
        jmp     dword [.table + 4*ebx]
188
 
189
  .table:
3601 hidnplayr 190
        dd      .read_mac       ; 0
3545 hidnplayr 191
  .number = ($ - .table) / 4 - 1
192
 
193
  .error:
194
        or      eax, -1
195
        ret
196
 
197
 
198
  .read_mac:
199
        movzx   ebx, word [eax + ETH_DEVICE.mac]
200
        mov     eax, dword [eax + ETH_DEVICE.mac + 2]
201
        mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
202
        ret
203