00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <ubixos/types.h>
00044 #include <ubixos/sched.h>
00045 #include <lib/kmalloc.h>
00046 #include <lib/kprintf.h>
00047 #include <sys/device.old.h>
00048 #include <isa/ne2k.h>
00049
00050
00051 #include "net/debug.h"
00052
00053 #include "net/opt.h"
00054 #include "net/def.h"
00055 #include "net/mem.h"
00056 #include "net/pbuf.h"
00057 #include "net/sys.h"
00058
00059 #include "netif/arp.h"
00060
00061
00062 #define IFNAME0 'e'
00063 #define IFNAME1 'd'
00064
00065 struct nicBuffer *tmpBuf = 0x0;
00066
00067 struct ethernetif {
00068 struct eth_addr *ethaddr;
00069
00070 };
00071
00072 static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
00073
00074
00075 static void ethernetif_input(struct netif *netif);
00076 static err_t ethernetif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr);
00077 static void ethernetif_thread();
00078 struct device *dev = 0x0;
00079
00080
00081 static void low_level_init(struct netif *netif) {
00082 struct ethernetif *ethernetif;
00083
00084 ethernetif = netif->state;
00085 dev = (struct device *)kmalloc(sizeof(struct device));
00086 dev->ioAddr = 0x280;
00087 dev->irq = 0x10;
00088
00089
00090 ethernetif->ethaddr->addr[0] = 0x00;
00091 ethernetif->ethaddr->addr[1] = 0x00;
00092 ethernetif->ethaddr->addr[2] = 0xC0;
00093 ethernetif->ethaddr->addr[3] = 0x97;
00094 ethernetif->ethaddr->addr[4] = 0xC6;
00095 ethernetif->ethaddr->addr[5] = 0x93;
00096
00097
00098 kprintf("NETIF: [0x%X:0x%X]\n",netif,ethernetif_thread);
00099 sys_thread_new(ethernetif_thread, netif);
00100
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 static err_t low_level_output(struct ethernetif *ethernetif, struct pbuf *p) {
00114 struct pbuf *q;
00115 char buf[1500];
00116 char *bufptr = 0x0;
00117
00118 dev->ioAddr = 0x280;
00119 dev->irq = 10;
00120
00121 bufptr = &buf[0];
00122
00123 for(q = p; q != NULL; q = q->next) {
00124 bcopy(q->payload, bufptr, q->len);
00125 bufptr += q->len;
00126 }
00127 PCtoNIC(dev,buf,p->tot_len);
00128
00129 return ERR_OK;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 static struct pbuf *low_level_input(struct ethernetif *ethernetif) {
00143 struct pbuf *p, *q;
00144 uInt16 len;
00145 char *bufptr;
00146 char *buf;
00147
00148 len = tmpBuf->length;
00149 bufptr = tmpBuf->buffer;
00150
00151
00152
00153 p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);
00154
00155 if(p != NULL) {
00156
00157
00158
00159 for(q = p; q != NULL; q = q->next) {
00160
00161
00162
00163
00164 bcopy(bufptr, q->payload, q->len);
00165 buf = q->payload;
00166 bufptr += q->len;
00167 }
00168
00169 } else {
00170
00171 }
00172 return p;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 static err_t ethernetif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr) {
00187 struct ethernetif *ethernetif;
00188 struct pbuf *q;
00189 struct eth_hdr *ethhdr;
00190 struct eth_addr *dest, mcastaddr;
00191 struct ip_addr *queryaddr;
00192 err_t err;
00193 uInt8 i;
00194
00195 ethernetif = netif->state;
00196
00197
00198
00199 if(pbuf_header(p, sizeof(struct eth_hdr)) != 0) {
00200
00201
00202 q = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr), PBUF_RAM);
00203 if(q == NULL) {
00204 return ERR_MEM;
00205 }
00206 pbuf_chain(q, p);
00207 p = q;
00208 }
00209
00210
00211
00212
00213
00214 queryaddr = ipaddr;
00215 if(ip_addr_isany(ipaddr) ||
00216 ip_addr_isbroadcast(ipaddr, &(netif->netmask))) {
00217 dest = (struct eth_addr *)ðbroadcast;
00218 } else if(ip_addr_ismulticast(ipaddr)) {
00219
00220 mcastaddr.addr[0] = 0x01;
00221 mcastaddr.addr[1] = 0x0;
00222 mcastaddr.addr[2] = 0x5e;
00223 mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
00224 mcastaddr.addr[4] = ip4_addr3(ipaddr);
00225 mcastaddr.addr[5] = ip4_addr4(ipaddr);
00226 dest = &mcastaddr;
00227 } else {
00228 if(ip_addr_maskcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
00229
00230
00231 queryaddr = ipaddr;
00232 } else {
00233
00234
00235 queryaddr = &(netif->gw);
00236 }
00237 dest = arp_lookup(queryaddr);
00238 }
00239
00240
00241
00242
00243 if(dest == NULL) {
00244 q = arp_query(netif, ethernetif->ethaddr, queryaddr);
00245 if(q != NULL) {
00246 err = low_level_output(ethernetif, q);
00247 pbuf_free(q);
00248 return err;
00249 }
00250 return ERR_MEM;
00251 }
00252 ethhdr = p->payload;
00253
00254 for(i = 0; i < 6; i++) {
00255 ethhdr->dest.addr[i] = dest->addr[i];
00256 ethhdr->src.addr[i] = ethernetif->ethaddr->addr[i];
00257 }
00258
00259 ethhdr->type = htons(ETHTYPE_IP);
00260
00261 return low_level_output(ethernetif, p);
00262
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 static void ethernetif_input(struct netif *netif) {
00276 struct ethernetif *ethernetif = 0x0;
00277 struct eth_hdr *ethhdr = 0x0;
00278 struct pbuf *p = 0x0;
00279
00280 ethernetif = netif->state;
00281
00282 p = low_level_input(ethernetif);
00283
00284 if(p != NULL) {
00285
00286 ethhdr = p->payload;
00287
00288 switch(htons(ethhdr->type)) {
00289 case ETHTYPE_IP:
00290 arp_ip_input(netif, p);
00291 pbuf_header(p, -14);
00292 netif->input(p, netif);
00293 break;
00294 case ETHTYPE_ARP:
00295 p = arp_arp_input(netif, ethernetif->ethaddr, p);
00296 if(p != NULL) {
00297 low_level_output(ethernetif, p);
00298 pbuf_free(p);
00299 }
00300 break;
00301 default:
00302 pbuf_free(p);
00303 break;
00304 }
00305 }
00306 }
00307
00308 static void
00309 arp_timer(void *arg)
00310 {
00311 arp_tmr();
00312 sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 void ethernetif_init(struct netif *netif) {
00326 struct ethernetif *ethernetif;
00327
00328 ethernetif = mem_malloc(sizeof(struct ethernetif));
00329 netif->state = ethernetif;
00330 netif->name[0] = IFNAME0;
00331 netif->name[1] = IFNAME1;
00332 netif->output = ethernetif_output;
00333 netif->linkoutput = (void *)low_level_output;
00334
00335 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
00336
00337 low_level_init(netif);
00338 arp_init();
00339
00340 sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
00341 }
00342
00343
00344
00345 void ethernetif_thread(void *arg) {
00346 struct netif *netif = 0x0;
00347
00348 netif = arg;
00349
00350 while (1) {
00351 tmpBuf = ne2kGetBuffer();
00352 if (tmpBuf && tmpBuf->length > 0x0) {
00353 ethernetif_input(netif);
00354 }
00355 }
00356 ne2kFreeBuffer(tmpBuf);
00357 }