root/net/inet/eth.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. eth_print
  2. eth_setup
  3. eth_dump
  4. eth_header
  5. eth_rebuild_header
  6. eth_add_arp
  7. eth_type_trans

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              Ethernet-type device handling.
   7  *
   8  * Version:     @(#)eth.c       1.0.7   05/25/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Mark Evans, <evansmp@uhura.aston.ac.uk>
  13  * 
  14  * Fixes:
  15  *              Mr Linux        : Arp problems
  16  *              Alan Cox        : Generic queue tidyup (very tiny here)
  17  *              Alan Cox        : eth_header ntohs should be htons
  18  *              Alan Cox        : eth_rebuild_header missing an htons and
  19  *                                minor other things.
  20  *
  21  *              This program is free software; you can redistribute it and/or
  22  *              modify it under the terms of the GNU General Public License
  23  *              as published by the Free Software Foundation; either version
  24  *              2 of the License, or (at your option) any later version.
  25  */
  26 #include <asm/segment.h>
  27 #include <asm/system.h>
  28 #include <linux/types.h>
  29 #include <linux/kernel.h>
  30 #include <linux/sched.h>
  31 #include <linux/string.h>
  32 #include <linux/mm.h>
  33 #include <linux/socket.h>
  34 #include <linux/in.h>
  35 #include "inet.h"
  36 #include "dev.h"
  37 #include "eth.h"
  38 #include "ip.h"
  39 #include "route.h"
  40 #include "protocol.h"
  41 #include "tcp.h"
  42 #include "skbuff.h"
  43 #include "sock.h"
  44 #include <linux/errno.h>
  45 #include "arp.h"
  46 
  47 
  48 /* Display an Ethernet address in readable format. */
  49 char *eth_print(unsigned char *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51   static char buff[64];
  52 
  53   if (ptr == NULL) return("[NONE]");
  54   sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
  55         (ptr[0] & 255), (ptr[1] & 255), (ptr[2] & 255),
  56         (ptr[3] & 255), (ptr[4] & 255), (ptr[5] & 255)
  57   );
  58   return(buff);
  59 }
  60 
  61 void eth_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63         struct device *d = dev_base;
  64 
  65         if (!str || !*str)
  66                 return;
  67         while (d) {
  68                 if (!strcmp(str,d->name)) {
  69                         if (ints[0] > 0)
  70                                 d->irq=ints[1];
  71                         if (ints[0] > 1)
  72                                 d->base_addr=ints[2];
  73                         if (ints[0] > 2)
  74                                 d->mem_start=ints[3];
  75                         if (ints[0] > 3)
  76                                 d->mem_end=ints[4];
  77                         break;
  78                 }
  79                 d=d->next;
  80         }
  81 }
  82 
  83 /* Display the contents of the Ethernet MAC header. */
  84 void
  85 eth_dump(struct ethhdr *eth)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87   if (inet_debug != DBG_ETH) return;
  88 
  89   printk("eth: SRC = %s ", eth_print(eth->h_source));
  90   printk("DST = %s ", eth_print(eth->h_dest));
  91   printk("TYPE = %04X\n", ntohs(eth->h_proto));
  92 }
  93 
  94 
  95 /* Create the Ethernet MAC header. */
  96 int
  97 eth_header(unsigned char *buff, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
  98            unsigned long daddr, unsigned long saddr, unsigned len)
  99 {
 100   struct ethhdr *eth;
 101 
 102   DPRINTF((DBG_DEV, "ETH: header(%s, ", in_ntoa(saddr)));
 103   DPRINTF((DBG_DEV, "%s, 0x%X)\n", in_ntoa(daddr), type));
 104 
 105   /* Fill in the basic Ethernet MAC header. */
 106   eth = (struct ethhdr *) buff;
 107   eth->h_proto = htons(type);
 108 
 109   /* We don't ARP for the LOOPBACK device... */
 110   if (dev->flags & IFF_LOOPBACK) {
 111         DPRINTF((DBG_DEV, "ETH: No header for loopback\n"));
 112         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 113         memset(eth->h_dest, 0, dev->addr_len);
 114         return(dev->hard_header_len);
 115   }
 116 
 117   /* Check if we can use the MAC BROADCAST address. */
 118   if (chk_addr(daddr) == IS_BROADCAST) {
 119         DPRINTF((DBG_DEV, "ETH: Using MAC Broadcast\n"));
 120         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 121         memcpy(eth->h_dest, dev->broadcast, dev->addr_len);
 122         return(dev->hard_header_len);
 123   }
 124   cli();
 125   memcpy(eth->h_source, &saddr, 4);
 126   /* No. Ask ARP to resolve the Ethernet address. */
 127   if (arp_find(eth->h_dest, daddr, dev, saddr)) 
 128   {
 129         sti();
 130         if(type!=ETH_P_IP)
 131                 printk("Erk: protocol %X got into an arp request state!\n",type);
 132         return(-dev->hard_header_len);
 133   } 
 134   else
 135   {
 136         memcpy(eth->h_source,dev->dev_addr,dev->addr_len);      /* This was missing causing chaos if the
 137                                                                    header built correctly! */
 138         sti();
 139         return(dev->hard_header_len);
 140   }
 141 }
 142 
 143 
 144 /* Rebuild the Ethernet MAC header. */
 145 int
 146 eth_rebuild_header(void *buff, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148   struct ethhdr *eth;
 149   unsigned long src, dst;
 150 
 151   DPRINTF((DBG_DEV, "ETH: Using MAC Broadcast\n"));
 152   eth = (struct ethhdr *) buff;
 153   src = *(unsigned long *) eth->h_source;
 154   dst = *(unsigned long *) eth->h_dest;
 155   DPRINTF((DBG_DEV, "ETH: RebuildHeader: SRC=%s ", in_ntoa(src)));
 156   DPRINTF((DBG_DEV, "DST=%s\n", in_ntoa(dst)));
 157   if(eth->h_proto!=htons(ETH_P_ARP))    /* This ntohs kind of helps a bit! */
 158           if (arp_find(eth->h_dest, dst, dev, src)) return(1);
 159   memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 160   return(0);
 161 }
 162 
 163 
 164 /* Add an ARP entry for a host on this interface. */
 165 void
 166 eth_add_arp(unsigned long addr, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168   struct ethhdr *eth;
 169 
 170   eth = (struct ethhdr *) (skb + 1);
 171   arp_add(addr, eth->h_source, dev);
 172 }
 173 
 174 
 175 /* Determine the packet's protocol ID. */
 176 unsigned short
 177 eth_type_trans(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179   struct ethhdr *eth;
 180 
 181   eth = (struct ethhdr *) (skb + 1);
 182 
 183   if(ntohs(eth->h_proto)<1536)
 184         return(htons(ETH_P_802_3));
 185   return(eth->h_proto);
 186 }

/* [previous][next][first][last][top][bottom][index][help] */