ethersim_netport.c
4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*
* ethersim_netport.c --
*
* serviceNetPort --
*
* Service a request that arrives at the network interface tap port.
*
* Results:
* -1 if problem occur with
*
* Side effects:
*
*/
#include "ethersim.h"
#ifdef sgi
int
serviceNetPort(void)
{
struct etherpacket ep;
struct ether_header *hdrPtr;
int n;
u_long inetAddr;
Hash_Entry *entryPtr;
HostInfoRecord *hostInfoRecord;
int xfersize;
n = read(netfd,(char *) &ep,sizeof(ep));
if (n < 0) {
perror("read of serviceNetPort");
return -1;
}
if ((n < ETHERMIN+sizeof(struct ether_header)+sizeof(struct snoopheader) + ETHERHDRPAD)
||
(n > ETHERMTU+sizeof(struct ether_header)+sizeof(struct snoopheader) + ETHERHDRPAD)) {
fprintf(stderr, "Ethernet: Bad packet size %d\n", n);
return -1;
}
n -= sizeof(struct snoopheader) + ETHERHDRPAD;
/*
* Get rid of snoop header and pad to make the curPacket.
*/
hdrPtr = (struct ether_header *) &(ep.ether);
if (htons(hdrPtr->ether_type) != ETHERTYPE_IP) {
fprintf(stderr,"ether_type != ip\n");
return 0;
}
if (verbose) {
printf("Packet received on ethernet:\n");
dispEtherPacket(n, hdrPtr, 2);
}
bcopy((void *) (((char *) &(ep.data)) + offsetof(struct ip, ip_dst)),
(void *) &inetAddr, sizeof(inetAddr));
entryPtr = Hash_FindEntry(&ipaddrTable, (Address)inetAddr);
if (entryPtr == NULL) {
fprintf(stderr,"Dhost not in ipaddrTable\n");
return 0;
}
hostInfoRecord = (HostInfoRecord *) Hash_GetValue(entryPtr);
if (verbose) {
printf(" Sending packet on UDP to 0x%x\n",
((struct sockaddr_in *) &(hostInfoRecord->fromaddr))->sin_addr.s_addr);
}
xfersize = sendto(simfd, (char *)&(ep.ether), n, 0x0,
(struct sockaddr *) &hostInfoRecord->fromaddr,
hostInfoRecord->fromlen);
if (xfersize < 0) {
perror("sendto");
}
return 0;
}
#endif /* sgi */
#ifdef sparc
int
serviceNetPort()
{
struct ether_header *hdrPtr;
int n;
u_long inetAddr;
Hash_Entry *entryPtr;
HostInfoRecord *hostInfoRecord;
int xfersize;
n = read(netfd, curPacket, sizeof(curPacket));
if (n < 0) {
perror("read of serviceNetPort");
return -1;
}
if ((n < ETHERMIN + sizeof(struct ether_header)) ||
(n > ETHERMTU + sizeof(struct ether_header))) {
fprintf(stderr, "Bad packet size %d\n", n);
return -1;
}
hdrPtr = (struct ether_header *) curPacket;
if (htons(hdrPtr->ether_type) != ETHERTYPE_IP) {
return 0;
}
bcopy(curPacket + sizeof(struct ether_header) + offsetof(struct ip, ip_dst),
(char *) &inetAddr, sizeof(inetAddr));
entryPtr = Hash_FindEntry(&ipaddrTable, (Address)inetAddr);
if (entryPtr == NULL) {
return 0;
}
hostInfoRecord = (HostInfoRecord *) Hash_GetValue(entryPtr);
xfersize = sendto(simfd, curPacket, n, 0x0,
(struct sockaddr *) &hostInfoRecord->fromaddr,
hostInfoRecord->fromlen);
if (xfersize < 0) {
perror("sendto");
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* validPacket --
*
* Check to see if we should accept this packet.
*
* Results:
* 1 if packet ok.
* 0 if we should request it.
*
* Side effects:
*
*----------------------------------------------------------------------
*/
int
validPacket(packet, size, fromAddr, fromlen)
char *packet; /* The contents of the packet. */
int size; /* The size of the packet. */
struct sockaddr *fromAddr; /* Where the packet was from. */
int fromlen; /* The length of fromAddr. */
{
struct ether_header *hdrPtr;
struct sockaddr_in *inaddrPtr;
u_long srcAddr;
hdrPtr = (struct ether_header *) packet;
if ((size < ETHERMIN + sizeof(struct ether_header)) ||
(size > ETHERMTU + sizeof(struct ether_header))) {
fprintf(stderr, "Bad packet size %d\n", size);
return 0;
}
/*
* XXX - Do security checking here. Currently we reject all packets
* from hosts outside our network. We only if we are forwarding
* packets.
*/
if (!noipforwarding) {
if ((fromAddr->sa_family != AF_INET) ||
(fromlen < offsetof(struct sockaddr_in, sin_zero))) {
fprintf(stderr, "Packet from bad fromaddr\n");
return 0;
}
inaddrPtr = (struct sockaddr_in *) fromAddr;
srcAddr = inet_netof(inaddrPtr->sin_addr);
if ((srcAddr != mynetaddr) && (srcAddr != IN_LOOPBACKNET)) {
fprintf(stderr, "Security violation: Packet from host %s\n",
inet_ntoa(inaddrPtr->sin_addr));
return 0;
}
}
return 1;
}
#endif /* sparc */