Main Page | Directories | File List | Globals

eth_core.c

Go to the documentation of this file.
00001 /*
00002  * kernel_net/layer_2/eth/eth_core.c
00003  *
00004  * Copyright (c) 2003-2005 Lukasz Dembinski <dembol@nasa.com.pl>
00005  * All Rights Reserved
00006  * 
00007  * Date:        2005/06
00008  * Author:      Lukasz Dembinski
00009  * Info:        eth_core.c core file
00010  * Contact:     mailto: <dembol@nasa.com.pl>
00011  *
00012  */
00013 
00014 #include <agnix/agnix.h>
00015 #include <agnix/adi/adi.h>
00016 #include <agnix/init.h>
00017 #include <agnix/net/net.h>
00018 #include <agnix/memory.h>
00019 #include <agnix/console.h>
00020 #include <agnix/sprintf.h>
00021 
00022 #define ETH_CORE_DEBUG  0
00023 #define MOD_NAME        "NET: "
00024 
00025 int eth_device_nr = 0;
00026 
00027 int eth_header_build(struct frb_s *frb, u8 *mac_dest, u8 *mac_source, u16 proto)
00028 {
00029     struct eth_hdr_s *eth_hdr;
00030     
00031     eth_hdr = (struct eth_hdr_s *)frb_pop(frb, sizeof(struct eth_hdr_s));
00032     memcpy((void *)eth_hdr->mac_dest, mac_dest, NET_LAYER_2_ETH_MAC_LEN);
00033 
00034     if (mac_source != NULL)
00035         memcpy((void *)eth_hdr->mac_source, mac_source, NET_LAYER_2_ETH_MAC_LEN);
00036     else
00037         memcpy((void *)eth_hdr->mac_source, frb->frb_dev->if_layer_2.if_addr, NET_LAYER_2_ETH_MAC_LEN);
00038         
00039     eth_hdr->proto = htons(proto);
00040 
00041     return 0;
00042 }
00043 
00044 int eth_header_parse(struct frb_s *frb)
00045 {
00046     struct eth_hdr_s *ethhdr = (struct eth_hdr_s *)frb_data(frb);
00047 #if ETH_CORE_DEBUG
00048     int i;
00049 
00050     u8 *ptr = (u8 *)frb->frb_data;
00051 
00052     for (i = 0; i < 64; i++) {
00053         printk("%02X ", ptr[i]);
00054     }
00055     printk("\n");
00056 
00057 #endif
00058 
00059     if (ntohs(ethhdr->proto) > 1536)
00060         frb->frb_proto_type = ethhdr->proto;
00061     else
00062         frb->frb_proto_type = ETH_P_802_3;
00063     
00064     if (frb->frb_data[0] & 0x01) {
00065         if (!memcmp(frb->frb_data, frb->frb_dev->if_layer_2.if_addr_broadcast, NET_LAYER_2_ETH_MAC_LEN))
00066             frb->frb_dest_type = FRB_DEST_TYPE_BROADCAST;
00067     } else
00068     if (!memcmp(frb->frb_data, frb->frb_dev->if_layer_2.if_addr, NET_LAYER_2_ETH_MAC_LEN))
00069         frb->frb_dest_type = FRB_DEST_TYPE_LOCAL;
00070     else
00071         frb->frb_dest_type = FRB_DEST_TYPE_OTHER;
00072 
00073     frb_push(frb, sizeof(struct eth_hdr_s));
00074 
00075     return 0;
00076 }
00077 
00078 int eth_set_mtu(struct adi_netdev_s *netdev, int mtu)
00079 {
00080     return 0;
00081 }
00082 
00083 int eth_set_mac_addr(struct adi_netdev_s *netdev, u8 *mac_addr)
00084 {
00085     return 0;
00086 }
00087 
00088 int eth_set_broadcast_addr(struct adi_netdev_s *netdev, u8 *broadcast_addr)
00089 {
00090     memcpy(netdev->if_layer_2.if_addr_broadcast, broadcast_addr, NET_LAYER_2_ETH_MAC_LEN);
00091 
00092     return 0;
00093 }
00094 
00095 int eth_device_compose_name(struct adi_netdev_s *netdev)
00096 {
00097     sprintf(netdev->if_name, "eth%d", eth_device_nr);    
00098     eth_device_nr++;
00099 
00100     return 0;
00101 }
00102 
00103 int eth_device_init(struct adi_netdev_s *netdev)
00104 {
00105     netdev->if_layer_2.if_addr_len      = NET_LAYER_2_ETH_MAC_LEN;
00106     netdev->if_layer_2.if_mtu           = NET_LAYER_2_ETH_MTU;
00107     netdev->if_layer_2.if_type          = IF_TYPE_ETH;
00108     netdev->if_ops->set_mac_addr        = eth_set_mac_addr;
00109     netdev->if_ops->set_broadcast_addr  = eth_set_broadcast_addr;
00110     netdev->if_ops->set_mtu             = eth_set_mtu;
00111     netdev->if_ops->header_build        = eth_header_build;
00112     netdev->if_ops->header_parse        = eth_header_parse;
00113 
00114     eth_set_broadcast_addr(netdev, NET_LAYER_2_ETH_BROADCAST);
00115     
00116     return 0;
00117 }
00118 
00119 int net_layer_2_eth_init(void)
00120 {
00121     printk(", eth");
00122     
00123     return 0;
00124 }
Dokumentacje wygenerowano programem Doxygen 1.4.2 dla projektu Agnix