Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3555 Serge 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
 
3725 Serge 35
iglobal
3555 Serge 36
align 4
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
 
3589 Serge 59
        DEBUGF  DEBUG_NETWORK_VERBOSE,"ETH_input: size=%u\n", ecx
3725 Serge 60
        sub     ecx, sizeof.ETH_header
3555 Serge 61
        jb      .dump
62
 
63
        lea     edx, [eax + sizeof.ETH_header]
64
        mov     ax, [eax + ETH_header.Type]
65
 
3626 Serge 66
        cmp     ax, ETHER_PROTO_IPv4
3555 Serge 67
        je      IPv4_input
68
 
3626 Serge 69
        cmp     ax, ETHER_PROTO_ARP
3555 Serge 70
        je      ARP_input
71
 
3626 Serge 72
        cmp     ax, ETHER_PROTO_IPv6
3555 Serge 73
        je      IPv6_input
74
 
3626 Serge 75
        cmp     ax, ETHER_PROTO_PPP_DISCOVERY
3555 Serge 76
        je      PPPoE_discovery_input
77
 
3626 Serge 78
        cmp     ax, ETHER_PROTO_PPP_SESSION
3555 Serge 79
        je      PPPoE_session_input
80
 
3589 Serge 81
        DEBUGF  DEBUG_NETWORK_ERROR, "ETH_input: Unknown packet type=%x\n", ax
3555 Serge 82
 
83
  .dump:
3589 Serge 84
        DEBUGF  DEBUG_NETWORK_VERBOSE,"ETH_input: dumping\n"
3555 Serge 85
        call    kernel_free
86
        add     esp, 4
87
        ret
88
 
89
;-----------------------------------------------------------------
90
;
91
; ETH_output
92
;
93
; IN: eax = pointer to source mac
94
;     ebx = device ptr
95
;     ecx = packet size
96
;     edx = pointer to destination mac
97
;      di = protocol
98
;
99
; OUT: edi = 0 on error, pointer to buffer otherwise
100
;      eax = buffer start
101
;      ebx = to device structure
102
;      ecx = unchanged (packet size of embedded data)
103
;      edx = size of complete buffer
104
;
105
;-----------------------------------------------------------------
106
align 4
107
ETH_output:
108
 
3589 Serge 109
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx
3555 Serge 110
 
111
        cmp     ecx, [ebx + NET_DEVICE.mtu]
112
        ja      .exit
113
 
114
        push    ecx
115
        push    di eax edx
116
 
117
        add     ecx, sizeof.ETH_header
118
        stdcall kernel_alloc, ecx
119
        test    eax, eax
120
        jz      .out_of_ram
121
        mov     edi, eax
122
 
123
        pop     esi
124
        movsd
125
        movsw
126
        pop     esi
127
        movsd
128
        movsw
129
        pop     ax
130
        stosw
131
 
132
        lea     eax, [edi - sizeof.ETH_header]  ; Set eax to buffer start
133
        pop     ecx
134
        lea     edx, [ecx + sizeof.ETH_header]  ; Set edx to complete buffer size
135
 
136
        cmp     edx, ETH_FRAME_MINIMUM
137
        jbe     .adjust_size
138
  .done:
3589 Serge 139
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ETH_output: ptr=%x size=%u\n", eax, edx
3555 Serge 140
        ret
141
 
142
  .adjust_size:
143
        mov     edx, ETH_FRAME_MINIMUM
144
        test    edx, edx        ; clear zero flag
145
        jmp     .done
146
 
147
  .out_of_ram:
3589 Serge 148
        DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Out of ram!\n"
3555 Serge 149
        add     esp, 4+4+2+4
150
        sub     edi, edi
151
        ret
152
 
153
  .exit:
3589 Serge 154
        DEBUGF  DEBUG_NETWORK_ERROR, "ETH_output: Packet too large!\n"
3555 Serge 155
        sub     edi, edi
156
        ret
157
 
158
 
159
 
160
;-----------------------------------------------------------------
161
;
162
; ETH_API
163
;
3626 Serge 164
; This function is called by system function 76
3555 Serge 165
;
166
; IN:  subfunction number in bl
167
;      device number in bh
168
;      ecx, edx, .. depends on subfunction
169
;
170
; OUT:
171
;
172
;-----------------------------------------------------------------
173
align 4
174
ETH_api:
175
 
3626 Serge 176
        cmp     bh, NET_DEVICES_MAX
3555 Serge 177
        ja      .error
178
        movzx   eax, bh
179
        mov     eax, dword [NET_DRV_LIST + 4*eax]
3626 Serge 180
        cmp     [eax + NET_DEVICE.device_type], NET_DEVICE_ETH
3555 Serge 181
        jne     .error
182
 
183
        and     ebx, 0xff
184
        cmp     ebx, .number
185
        ja      .error
186
        jmp     dword [.table + 4*ebx]
187
 
188
  .table:
3626 Serge 189
        dd      .read_mac       ; 0
3555 Serge 190
  .number = ($ - .table) / 4 - 1
191
 
192
  .error:
193
        or      eax, -1
194
        ret
195
 
196
 
197
  .read_mac:
198
        movzx   ebx, word [eax + ETH_DEVICE.mac]
199
        mov     eax, dword [eax + ETH_DEVICE.mac + 2]
200
        mov     [esp+20+4], ebx                         ; TODO: fix this ugly code
201
        ret
202