Subversion Repositories Kolibri OS

Rev

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