source: trunk/minix/commands/dhcpd/ether.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 5.6 KB
RevLine 
[9]1/* ether.c - Raw Ethernet stuff
2 * Author: Kees J. Bot
3 * 16 Dec 2000
4 */
5#include <sys/types.h>
6#include <fcntl.h>
7#include <string.h>
8#include <sys/asynchio.h>
9#include <net/hton.h>
10#include <net/gen/in.h>
11#include <net/gen/ether.h>
12#include <net/gen/eth_hdr.h>
13#include <net/gen/ip_hdr.h>
14#include <net/gen/icmp.h>
15#include <net/gen/icmp_hdr.h>
16#include <net/gen/oneCsum.h>
17#include <net/gen/udp.h>
18#include <net/gen/udp_hdr.h>
19#include <net/gen/dhcp.h>
20#include "arp.h"
21#include "dhcpd.h"
22
23static ether_addr_t BCAST_ETH = {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }};
24#define BCAST_IP HTONL(0xFFFFFFFFUL)
25#define LOCALHOST HTONL(0x7F000001UL)
26
27static u16_t udp_cksum(ipaddr_t src, ipaddr_t dst, udp_hdr_t *udp)
28{
29 /* Compute the checksum of an UDP packet plus data. */
30 struct udp_pseudo {
31 ipaddr_t src, dst;
32 u8_t zero, proto;
33 u16_t length;
34 } pseudo;
35 size_t len;
36
37 /* Fill in the UDP pseudo header that must be prefixed to the UDP
38 * packet to compute the checksum of the whole thing.
39 */
40 pseudo.src= src;
41 pseudo.dst= dst;
42 pseudo.zero= 0;
43 pseudo.proto= IPPROTO_UDP;
44 pseudo.length= udp->uh_length;
45
46 len= ntohs(udp->uh_length);
47 if (len & 1) {
48 /* Length is odd? Pad with a zero. */
49 B(udp)[len++]= 0;
50 }
51 return oneC_sum(oneC_sum(0, &pseudo, sizeof(pseudo)), udp, len);
52}
53
54void udp2ether(buf_t *bp, network_t *np)
55{
56 /* Transform a packet in UDP format to raw Ethernet. Ignore the UDP
57 * addresses, always broadcast from 0.0.0.0.
58 */
59 udp_io_hdr_t udpio;
60
61 /* Save the UDP I/O header. */
62 udpio= *bp->udpio;
63
64 /* Fill in the Ethernet, IP and UDP headers. */
65 bp->eth->eh_dst= BCAST_ETH;
66 bp->eth->eh_src= np->eth;
67 bp->eth->eh_proto= HTONS(ETH_IP_PROTO);
68 bp->ip->ih_vers_ihl= 0x45;
69 bp->ip->ih_tos= 0;
70 bp->ip->ih_length= htons(sizeof(ip_hdr_t)
71 + sizeof(udp_hdr_t) + udpio.uih_data_len);
72 bp->ip->ih_id= 0;
73 bp->ip->ih_flags_fragoff= NTOHS(0x4000);
74 bp->ip->ih_ttl= IP_MAX_TTL;
75 bp->ip->ih_proto= IPPROTO_UDP;
76 bp->ip->ih_hdr_chk= 0;
77 bp->ip->ih_src= 0;
78 bp->ip->ih_dst= BCAST_IP;
79 bp->ip->ih_hdr_chk= ~oneC_sum(0, bp->ip, sizeof(*bp->ip));
80 bp->udp->uh_src_port= udpio.uih_src_port;
81 bp->udp->uh_dst_port= udpio.uih_dst_port;
82 bp->udp->uh_length= htons(sizeof(udp_hdr_t) + udpio.uih_data_len);
83 bp->udp->uh_chksum= 0;
84 bp->udp->uh_chksum= ~udp_cksum(bp->ip->ih_src, bp->ip->ih_dst, bp->udp);
85}
86
87int ether2udp(buf_t *bp)
88{
89 /* Transform an UDP packet read from raw Ethernet to normal UDP.
90 * Return true iff the packet is indeed UDP and has no errors.
91 */
92 udp_io_hdr_t udpio;
93
94 if (bp->eth->eh_proto != HTONS(ETH_IP_PROTO)
95 || bp->ip->ih_vers_ihl != 0x45
96 || bp->ip->ih_proto != IPPROTO_UDP
97 || oneC_sum(0, bp->ip, 20) != (u16_t) ~0
98 || udp_cksum(bp->ip->ih_src, bp->ip->ih_dst, bp->udp) != (u16_t) ~0
99 ) {
100 /* Not UDP/IP or checksums bad. */
101 return 0;
102 }
103 udpio.uih_src_addr= bp->ip->ih_src;
104 udpio.uih_dst_addr= bp->ip->ih_dst;
105 udpio.uih_src_port= bp->udp->uh_src_port;
106 udpio.uih_dst_port= bp->udp->uh_dst_port;
107 udpio.uih_ip_opt_len= 0;
108 udpio.uih_data_len= ntohs(bp->udp->uh_length) - sizeof(udp_hdr_t);
109 *bp->udpio= udpio;
110 return 1;
111}
112
113void make_arp(buf_t *bp, network_t *np)
114{
115 /* Create an ARP packet to query for my IP address. */
116 arp46_t *arp= (arp46_t *) bp->eth;
117
118 memset(arp, 0, sizeof(*arp));
119 arp->dstaddr= BCAST_ETH;
120 arp->srcaddr= np->eth;
121 arp->ethtype= HTONS(ETH_ARP_PROTO);
122 arp->hdr= HTONS(ARP_ETHERNET);
123 arp->pro= HTONS(ETH_IP_PROTO);
124 arp->hln= 6;
125 arp->pln= 4;
126 arp->op= HTONS(ARP_REQUEST);
127 arp->sha= np->eth;
128 memcpy(arp->spa, &np->ip, sizeof(np->ip));
129 memcpy(arp->tpa, &np->ip, sizeof(np->ip));
130}
131
132int is_arp_me(buf_t *bp, network_t *np)
133{
134 /* True iff an ARP packet is a reply from someone else with an address I
135 * thought was mine. (That's like, bad.)
136 */
137 arp46_t *arp= (arp46_t *) bp->eth;
138
139 if (arp->ethtype == HTONS(ETH_ARP_PROTO)
140 && arp->hdr == HTONS(ARP_ETHERNET)
141 && arp->pro == HTONS(ETH_IP_PROTO)
142 && arp->op == HTONS(ARP_REPLY)
143 && memcmp(&arp->spa, &np->ip, sizeof(np->ip)) == 0
144 && memcmp(&arp->sha, &np->eth, sizeof(np->eth)) != 0
145 ) {
146 np->conflict= arp->sha;
147 return 1;
148 }
149 return 0;
150}
151
152void icmp_solicit(buf_t *bp)
153{
154 /* Fill in a router solicitation ICMP packet. */
155 icmp_hdr_t *icmp= (icmp_hdr_t *) (bp->ip + 1);
156
157 bp->ip->ih_vers_ihl= 0x45;
158 bp->ip->ih_dst= BCAST_IP;
159
160 icmp->ih_type= ICMP_TYPE_ROUTE_SOL;
161 icmp->ih_code= 0;
162 icmp->ih_hun.ihh_unused= 0;
163 icmp->ih_chksum= 0;
164 icmp->ih_chksum= ~oneC_sum(0, icmp, 8);
165}
166
167void icmp_advert(buf_t *bp, network_t *np)
168{
169 /* Fill in a router advert to be sent to my own interface. */
170 icmp_hdr_t *icmp= (icmp_hdr_t *) (bp->ip + 1);
171
172 bp->ip->ih_vers_ihl= 0x45;
173 bp->ip->ih_dst= LOCALHOST;
174
175 icmp->ih_type= ICMP_TYPE_ROUTER_ADVER;
176 icmp->ih_code= 0;
177 icmp->ih_hun.ihh_ram.iram_na= 1;
178 icmp->ih_hun.ihh_ram.iram_aes= 2;
179 icmp->ih_hun.ihh_ram.iram_lt= htons(DELTA_ADV);
180 ((u32_t *) icmp->ih_dun.uhd_data)[0] = np->gateway;
181 ((u32_t *) icmp->ih_dun.uhd_data)[1] = HTONL((u32_t) -9999);
182 icmp->ih_chksum= 0;
183 icmp->ih_chksum= ~oneC_sum(0, icmp, 16);
184}
185
186ipaddr_t icmp_is_advert(buf_t *bp)
187{
188 /* Check if an IP packet is a router advertisement, and if it's genuine,
189 * i.e. the sender is mentioned in the packet.
190 */
191 icmp_hdr_t *icmp= (icmp_hdr_t *) (bp->ip + 1);
192 int i;
193
194 if (icmp->ih_type == ICMP_TYPE_ROUTER_ADVER) {
195 for (i= 0; i < icmp->ih_hun.ihh_ram.iram_na; i++) {
196 if (((u32_t *) icmp->ih_dun.uhd_data)[2*i] == bp->ip->ih_src) {
197 /* It's a router! */
198 return bp->ip->ih_src;
199 }
200 }
201 }
202 return 0;
203}
Note: See TracBrowser for help on using the repository browser.