ether.c

Go to the documentation of this file.
00001 /*      ether.c - Raw Ethernet stuff
00002  *                                                      Author: Kees J. Bot
00003  *                                                              16 Dec 2000
00004  */
00005 #include <sys/types.h>
00006 #include <fcntl.h>
00007 #include <string.h>
00008 #include <sys/asynchio.h>
00009 #include <net/hton.h>
00010 #include <net/gen/in.h>
00011 #include <net/gen/ether.h>
00012 #include <net/gen/eth_hdr.h>
00013 #include <net/gen/ip_hdr.h>
00014 #include <net/gen/icmp.h>
00015 #include <net/gen/icmp_hdr.h>
00016 #include <net/gen/oneCsum.h>
00017 #include <net/gen/udp.h>
00018 #include <net/gen/udp_hdr.h>
00019 #include <net/gen/dhcp.h>
00020 #include "arp.h"
00021 #include "dhcpd.h"
00022 
00023 static ether_addr_t BCAST_ETH = {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }};
00024 #define BCAST_IP        HTONL(0xFFFFFFFFUL)
00025 #define LOCALHOST       HTONL(0x7F000001UL)
00026 
00027 static u16_t udp_cksum(ipaddr_t src, ipaddr_t dst, udp_hdr_t *udp)
00028 {
00029     /* Compute the checksum of an UDP packet plus data. */
00030     struct udp_pseudo {
00031         ipaddr_t        src, dst;
00032         u8_t            zero, proto;
00033         u16_t           length;
00034     } pseudo;
00035     size_t len;
00036 
00037     /* Fill in the UDP pseudo header that must be prefixed to the UDP
00038      * packet to compute the checksum of the whole thing.
00039      */
00040     pseudo.src= src;
00041     pseudo.dst= dst;
00042     pseudo.zero= 0;
00043     pseudo.proto= IPPROTO_UDP;
00044     pseudo.length= udp->uh_length;
00045 
00046     len= ntohs(udp->uh_length);
00047     if (len & 1) {
00048         /* Length is odd?  Pad with a zero. */
00049         B(udp)[len++]= 0;
00050     }
00051     return oneC_sum(oneC_sum(0, &pseudo, sizeof(pseudo)), udp, len);
00052 }
00053 
00054 void udp2ether(buf_t *bp, network_t *np)
00055 {
00056     /* Transform a packet in UDP format to raw Ethernet.  Ignore the UDP
00057      * addresses, always broadcast from 0.0.0.0.
00058      */
00059     udp_io_hdr_t udpio;
00060 
00061     /* Save the UDP I/O header. */
00062     udpio= *bp->udpio;
00063 
00064     /* Fill in the Ethernet, IP and UDP headers. */
00065     bp->eth->eh_dst= BCAST_ETH;
00066     bp->eth->eh_src= np->eth;
00067     bp->eth->eh_proto= HTONS(ETH_IP_PROTO);
00068     bp->ip->ih_vers_ihl= 0x45;
00069     bp->ip->ih_tos= 0;
00070     bp->ip->ih_length= htons(sizeof(ip_hdr_t)
00071                         + sizeof(udp_hdr_t) + udpio.uih_data_len);
00072     bp->ip->ih_id= 0;
00073     bp->ip->ih_flags_fragoff= NTOHS(0x4000);
00074     bp->ip->ih_ttl= IP_MAX_TTL;
00075     bp->ip->ih_proto= IPPROTO_UDP;
00076     bp->ip->ih_hdr_chk= 0;
00077     bp->ip->ih_src= 0;
00078     bp->ip->ih_dst= BCAST_IP;
00079     bp->ip->ih_hdr_chk= ~oneC_sum(0, bp->ip, sizeof(*bp->ip));
00080     bp->udp->uh_src_port= udpio.uih_src_port;
00081     bp->udp->uh_dst_port= udpio.uih_dst_port;
00082     bp->udp->uh_length= htons(sizeof(udp_hdr_t) + udpio.uih_data_len);
00083     bp->udp->uh_chksum= 0;
00084     bp->udp->uh_chksum= ~udp_cksum(bp->ip->ih_src, bp->ip->ih_dst, bp->udp);
00085 }
00086 
00087 int ether2udp(buf_t *bp)
00088 {
00089     /* Transform an UDP packet read from raw Ethernet to normal UDP.
00090      * Return true iff the packet is indeed UDP and has no errors.
00091      */
00092     udp_io_hdr_t udpio;
00093 
00094     if (bp->eth->eh_proto != HTONS(ETH_IP_PROTO)
00095         || bp->ip->ih_vers_ihl != 0x45
00096         || bp->ip->ih_proto != IPPROTO_UDP
00097         || oneC_sum(0, bp->ip, 20) != (u16_t) ~0
00098         || udp_cksum(bp->ip->ih_src, bp->ip->ih_dst, bp->udp) != (u16_t) ~0
00099     ) {
00100         /* Not UDP/IP or checksums bad. */
00101         return 0;
00102     }
00103     udpio.uih_src_addr= bp->ip->ih_src;
00104     udpio.uih_dst_addr= bp->ip->ih_dst;
00105     udpio.uih_src_port= bp->udp->uh_src_port;
00106     udpio.uih_dst_port= bp->udp->uh_dst_port;
00107     udpio.uih_ip_opt_len= 0;
00108     udpio.uih_data_len= ntohs(bp->udp->uh_length) - sizeof(udp_hdr_t);
00109     *bp->udpio= udpio;
00110     return 1;
00111 }
00112 
00113 void make_arp(buf_t *bp, network_t *np)
00114 {
00115     /* Create an ARP packet to query for my IP address. */
00116     arp46_t *arp= (arp46_t *) bp->eth;
00117 
00118     memset(arp, 0, sizeof(*arp));
00119     arp->dstaddr= BCAST_ETH;
00120     arp->srcaddr= np->eth;
00121     arp->ethtype= HTONS(ETH_ARP_PROTO);
00122     arp->hdr= HTONS(ARP_ETHERNET);
00123     arp->pro= HTONS(ETH_IP_PROTO);
00124     arp->hln= 6;
00125     arp->pln= 4;
00126     arp->op= HTONS(ARP_REQUEST);
00127     arp->sha= np->eth;
00128     memcpy(arp->spa, &np->ip, sizeof(np->ip));
00129     memcpy(arp->tpa, &np->ip, sizeof(np->ip));
00130 }
00131 
00132 int is_arp_me(buf_t *bp, network_t *np)
00133 {
00134     /* True iff an ARP packet is a reply from someone else with an address I
00135      * thought was mine.  (That's like, bad.)
00136      */
00137     arp46_t *arp= (arp46_t *) bp->eth;
00138 
00139     if (arp->ethtype == HTONS(ETH_ARP_PROTO)
00140         && arp->hdr == HTONS(ARP_ETHERNET)
00141         && arp->pro == HTONS(ETH_IP_PROTO)
00142         && arp->op == HTONS(ARP_REPLY)
00143         && memcmp(&arp->spa, &np->ip, sizeof(np->ip)) == 0
00144         && memcmp(&arp->sha, &np->eth, sizeof(np->eth)) != 0
00145     ) {
00146         np->conflict= arp->sha;
00147         return 1;
00148     }
00149     return 0;
00150 }
00151 
00152 void icmp_solicit(buf_t *bp)
00153 {
00154     /* Fill in a router solicitation ICMP packet. */
00155     icmp_hdr_t *icmp= (icmp_hdr_t *) (bp->ip + 1);
00156 
00157     bp->ip->ih_vers_ihl= 0x45;
00158     bp->ip->ih_dst= BCAST_IP;
00159 
00160     icmp->ih_type= ICMP_TYPE_ROUTE_SOL;
00161     icmp->ih_code= 0;
00162     icmp->ih_hun.ihh_unused= 0;
00163     icmp->ih_chksum= 0;
00164     icmp->ih_chksum= ~oneC_sum(0, icmp, 8);
00165 }
00166 
00167 void icmp_advert(buf_t *bp, network_t *np)
00168 {
00169     /* Fill in a router advert to be sent to my own interface. */
00170     icmp_hdr_t *icmp= (icmp_hdr_t *) (bp->ip + 1);
00171 
00172     bp->ip->ih_vers_ihl= 0x45;
00173     bp->ip->ih_dst= LOCALHOST;
00174 
00175     icmp->ih_type= ICMP_TYPE_ROUTER_ADVER;
00176     icmp->ih_code= 0;
00177     icmp->ih_hun.ihh_ram.iram_na= 1;
00178     icmp->ih_hun.ihh_ram.iram_aes= 2;
00179     icmp->ih_hun.ihh_ram.iram_lt= htons(DELTA_ADV);
00180     ((u32_t *) icmp->ih_dun.uhd_data)[0] = np->gateway;
00181     ((u32_t *) icmp->ih_dun.uhd_data)[1] = HTONL((u32_t) -9999);
00182     icmp->ih_chksum= 0;
00183     icmp->ih_chksum= ~oneC_sum(0, icmp, 16);
00184 }
00185 
00186 ipaddr_t icmp_is_advert(buf_t *bp)
00187 {
00188     /* Check if an IP packet is a router advertisement, and if it's genuine,
00189      * i.e. the sender is mentioned in the packet.
00190      */
00191     icmp_hdr_t *icmp= (icmp_hdr_t *) (bp->ip + 1);
00192     int i;
00193 
00194     if (icmp->ih_type == ICMP_TYPE_ROUTER_ADVER) {
00195         for (i= 0; i < icmp->ih_hun.ihh_ram.iram_na; i++) {
00196             if (((u32_t *) icmp->ih_dun.uhd_data)[2*i] == bp->ip->ih_src) {
00197                 /* It's a router! */
00198                 return bp->ip->ih_src;
00199             }
00200         }
00201     }
00202     return 0;
00203 }

Generated on Fri Apr 14 22:56:47 2006 for minix by  doxygen 1.4.6