Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
1514 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
1159 hidnplayr 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
 
1206 hidnplayr 17
$Revision: 2220 $
1159 hidnplayr 18
 
19
struct	ETH_FRAME
1514 hidnplayr 20
	.DstMAC 	dp  ?  ; destination MAC-address
21
	.SrcMAC 	dp  ?  ; source MAC-address
22
	.Type		dw  ?  ; type of the upper-layer protocol
23
	.Data:		       ; data (46-1500 bytes for a normal packet)
1159 hidnplayr 24
ends
25
 
1514 hidnplayr 26
virtual at NET_DEVICE.end
27
 
28
	ETH_DEVICE:
1519 hidnplayr 29
 
1159 hidnplayr 30
	.set_mode	dd ?
31
	.get_mode	dd ?
32
 
1519 hidnplayr 33
	.set_MAC	dd ?
34
	.get_MAC	dd ?
35
 
1514 hidnplayr 36
	.mode		dd ?
1159 hidnplayr 37
	.mac		dp ?
38
 
1514 hidnplayr 39
end virtual
1159 hidnplayr 40
 
41
align 4
1196 hidnplayr 42
iglobal
43
 
44
	ETH_BROADCAST	dp  0xffffffffffff
45
endg
46
 
1257 hidnplayr 47
;-----------------------------------------------------------------
1159 hidnplayr 48
;
1529 hidnplayr 49
; ETH_input
1159 hidnplayr 50
;
51
;  This function is called by ethernet drivers,
52
;  It pushes the received ethernet packets onto the eth_in_queue
53
;
1514 hidnplayr 54
;  IN:   [esp]  = Pointer to buffer
1529 hidnplayr 55
;       [esp+4] = size of buffer
1514 hidnplayr 56
;         ebx   = pointer to eth_device
1159 hidnplayr 57
;  OUT: /
58
;
1257 hidnplayr 59
;-----------------------------------------------------------------
1159 hidnplayr 60
align 4
1529 hidnplayr 61
ETH_input:
1473 hidnplayr 62
	mov	eax, [esp]
63
	mov	ecx, [esp+4]
1249 hidnplayr 64
 
1529 hidnplayr 65
	DEBUGF	1,"ETH_input - size: %u\n", ecx
1159 hidnplayr 66
	cmp	ecx, 60    ; check packet length
67
	jl	.dump
68
	sub	ecx, ETH_FRAME.Data
69
 
70
	lea	edx, [eax + ETH_FRAME.Data]
71
	mov	ax , [eax + ETH_FRAME.Type]
72
 
73
	cmp	ax, ETHER_IPv4
1529 hidnplayr 74
	je	IPv4_input
1159 hidnplayr 75
 
76
	cmp	ax, ETHER_ARP
1529 hidnplayr 77
	je	ARP_input
1159 hidnplayr 78
 
1519 hidnplayr 79
;        cmp     ax, ETHER_PPP_DISCOVERY
80
;        je      PPPOE_discovery
81
 
1473 hidnplayr 82
	DEBUGF	2,"Unknown ethernet packet type %x\n", ax
1159 hidnplayr 83
 
84
  .dump:
1529 hidnplayr 85
	DEBUGF	2,"ETH_input - dumping\n"
1159 hidnplayr 86
	call	kernel_free
87
	add	esp, 4
1249 hidnplayr 88
	ret
1159 hidnplayr 89
 
1249 hidnplayr 90
;-----------------------------------------------------------------
91
;
1529 hidnplayr 92
; ETH_output
1159 hidnplayr 93
;
1514 hidnplayr 94
; IN: eax = pointer to source mac
1529 hidnplayr 95
;     ebx = device ptr
1514 hidnplayr 96
;     ecx = packet size
1529 hidnplayr 97
;     edx = pointer to destination mac
1514 hidnplayr 98
;      di = protocol
1159 hidnplayr 99
;
1514 hidnplayr 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
1159 hidnplayr 105
;
1257 hidnplayr 106
;-----------------------------------------------------------------
1159 hidnplayr 107
align 4
1529 hidnplayr 108
ETH_output:
1159 hidnplayr 109
 
1529 hidnplayr 110
	DEBUGF	1,"ETH_output: size=%u device:%x\n", ecx, ebx
1159 hidnplayr 111
 
1529 hidnplayr 112
	cmp	ecx, [ebx + NET_DEVICE.mtu]
1159 hidnplayr 113
	jg	.exit
114
 
1529 hidnplayr 115
	push	ecx			; << 1
116
	push	di eax edx		; << 2
117
	add	ecx, ETH_FRAME.Data
1159 hidnplayr 118
 
1529 hidnplayr 119
	push	ecx			; << 3
120
 
121
	push	ecx			; << 4
122
	call	kernel_alloc		; >> 4
123
	test	eax, eax
124
	jz	.out_of_ram
1514 hidnplayr 125
	mov	edi, eax
1159 hidnplayr 126
 
1529 hidnplayr 127
	pop	ecx			; >> 3
1159 hidnplayr 128
 
1529 hidnplayr 129
	pop	esi			; >> 2
1159 hidnplayr 130
	movsd
131
	movsw
1529 hidnplayr 132
	pop	esi			; >> 2
1159 hidnplayr 133
	movsd
134
	movsw
1529 hidnplayr 135
	pop	ax			; >> 2
1159 hidnplayr 136
	stosw
137
 
138
	lea	eax, [edi - ETH_FRAME.Data]  ; Set eax to buffer start
1529 hidnplayr 139
	mov	edx, ecx		     ; Set edx to complete buffer size
1159 hidnplayr 140
 
1529 hidnplayr 141
	pop	ecx			; >> 1
1159 hidnplayr 142
 
1529 hidnplayr 143
	cmp	edx, 60-1		; minimum ethernet packet size
144
	jle	.adjust_size
145
	DEBUGF	1,"ETH_output: done: %x total size: %u\n", eax, edx
146
	ret
1206 hidnplayr 147
 
1529 hidnplayr 148
  .adjust_size:
149
	mov	edx, 60
1536 hidnplayr 150
	test	edx, edx	; clear zero flag
1159 hidnplayr 151
	ret
152
 
1529 hidnplayr 153
  .out_of_ram:
154
	DEBUGF	2,"ETH_output: Out of ram space!!\n"
155
	add	esp, 3*4+2+4
156
	sub	edi, edi
1206 hidnplayr 157
	ret
158
 
1159 hidnplayr 159
  .exit:
1529 hidnplayr 160
	DEBUGF	2,"ETH_output: Packet too large!\n"
161
	sub	edi, edi
162
;;;        dec     edi
1159 hidnplayr 163
	ret
164
 
165
 
166
 
1257 hidnplayr 167
;-----------------------------------------------------------------
1159 hidnplayr 168
;
169
; ETH_API
170
;
171
; This function is called by system function 75
172
;
173
; IN:  subfunction number in bl
174
;      device number in bh
175
;      ecx, edx, .. depends on subfunction
176
;
177
; OUT:
178
;
1257 hidnplayr 179
;-----------------------------------------------------------------
1159 hidnplayr 180
align 4
181
ETH_API:
182
 
1514 hidnplayr 183
	cmp	bh, MAX_NET_DEVICES
184
	jg	.error
1159 hidnplayr 185
	movzx	eax, bh
186
	shl	eax, 2
187
 
1514 hidnplayr 188
	mov	eax, dword [NET_DRV_LIST + eax]
189
	cmp	[eax + NET_DEVICE.type], NET_TYPE_ETH
190
	jne	.error
191
 
1159 hidnplayr 192
	test	bl, bl
193
	jz	.packets_tx	; 0
194
	dec	bl
195
	jz	.packets_rx	; 1
196
	dec	bl
197
	jz	.bytes_tx	; 2
198
	dec	bl
199
	jz	.bytes_rx	; 3
200
	dec	bl
201
	jz	.read_mac	; 4
202
	dec	bl
203
	jz	.write_mac	; 5
204
 
1514 hidnplayr 205
  .error:
206
	DEBUGF	2,"Device is not ethernet type\n"
207
	or	eax, -1
1159 hidnplayr 208
	ret
209
 
210
.packets_tx:
1519 hidnplayr 211
	mov	eax, dword [eax + NET_DEVICE.packets_tx]
1171 hidnplayr 212
 
1159 hidnplayr 213
	ret
214
 
215
.packets_rx:
1519 hidnplayr 216
	mov	eax, dword [eax + NET_DEVICE.packets_rx]
1159 hidnplayr 217
	ret
218
 
219
.bytes_tx:
1519 hidnplayr 220
	mov	ebx, dword [eax + NET_DEVICE.bytes_tx + 4]
221
	mov	eax, dword [eax + NET_DEVICE.bytes_tx]
1174 hidnplayr 222
	mov	[esp+20+4], ebx 			; TODO: fix this ugly code
1159 hidnplayr 223
	ret
224
 
225
.bytes_rx:
1519 hidnplayr 226
	mov	ebx, dword [eax + NET_DEVICE.bytes_rx + 4]
227
	mov	eax, dword [eax + NET_DEVICE.bytes_rx]
1174 hidnplayr 228
	mov	[esp+20+4], ebx 			; TODO: fix this ugly code
1159 hidnplayr 229
	ret
230
 
1174 hidnplayr 231
 
1159 hidnplayr 232
.read_mac:
233
	movzx	ebx, word [eax + ETH_DEVICE.mac]
234
	mov	eax, dword [eax + ETH_DEVICE.mac + 2]
1171 hidnplayr 235
	mov	[esp+20+4], ebx 			; TODO: fix this ugly code
1159 hidnplayr 236
	ret
237
 
238
.write_mac:
239
	push	ecx
240
	push	dx
1519 hidnplayr 241
	call	[eax + ETH_DEVICE.set_MAC]
1159 hidnplayr 242
	ret
243