ethersim_output.c
5.05 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
/*
* 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_output.c --
*
* outputPacket --
*
*
* Results:
* None.
*
* Side effects:
*
*/
#include "ethersim.h"
extern u_long proxyaddr;
#ifdef sgi
int
outputPacket (
char *packet, /* The contents of the packet. */
int size /* The size of the packet. */
)
{
struct etherpacket ep;
struct ether_header *hdrPtr;
struct sockaddr protoheader;
struct in_addr dstaddr,srcaddr;
int cc;
int i;
/*
* Fill in the correct ethernet address source.
*/
hdrPtr = (struct ether_header *) packet;
for ( i = 0; i < 6; i++)
hdrPtr->ether_shost[i] = myetheraddr[i];
bcopy(packet + sizeof(struct ether_header) + offsetof(struct ip, ip_src),
(char *) &srcaddr, sizeof(srcaddr));
bcopy(packet + sizeof(struct ether_header) + offsetof(struct ip, ip_dst),
(char *) &dstaddr, sizeof(dstaddr));
/*
printf("D<%s,0x%x,%x:%x:%x:%x:%x:%x>",
inet_ntoa(dstaddr),dstaddr.s_addr,A(0), A(1), A(2), A(3), A(4), A(5));
printf("S<%s,0x%x,%x:%x:%x:%x:%x:%x>\n",
inet_ntoa(srcaddr),srcaddr.s_addr,B(0), B(1), B(2), B(3), B(4), B(5));
*/
srcaddr = ntohl(srcaddr);
dstaddr = ntohl(dstaddr);
/*
* Security:
* 1) Don't broadcast or send to all zeros.
* 2) Must be of type IP.
* 3) Must have a ip_src address on the simulated subnet.
*/
if (((A(0) == 0xff) && (A(1) == 0xff) && (A(2) == 0xff) &&
(A(3) == 0xff) && (A(4) == 0xff) && (A(5) == 0xff)) ||
((A(0) == 0) && (A(1) == 0) && (A(2) == 0) &&
(A(3) == 0) && (A(4) == 0) && (A(5) == 0)) ||
(htons(hdrPtr->ether_type) != ETHERTYPE_IP) ||
#ifdef SIM_PROXY
(((srcaddr.s_addr & mynetmask) != subnetaddr ) &&
proxyaddr == 0)) {
#else
((srcaddr.s_addr & mynetmask) != subnetaddr)) {
#endif
fprintf(stderr,
"outputPacket: Tossing packet sent to %x:%x:%x:%x:%x:%x\n",
A(0), A(1), A(2), A(3), A(4), A(5));
return 0;
}
/*
* Send the packet to the snoop device.
* Output on a snoop socket, using write or send, takes a buffer
* address pointing at the link-layer packet to be transmitted.
* Output buffers may need to begin with RAW_HDRPAD bytes of padding
* to ensure addressability of structured data, but such padding is
* not passed to write.
*/
if(verbose) {
printf("outputPacket: sending to 0x%x (%x:%x:%x:%x:%x:%x)\n",
dstaddr.s_addr, B(0), B(1), B(2), B(3), B(4), B(5));
}
if ( write(netfd,packet,size ) < size ) {
perror("outputPacket:send");
return 0;
}
#ifdef notdef
fprintf(stderr, "Not sending packet to %x:%x:%x:%x:%x:%x from %x:%x:%x:%x:%x:%x\n",
A(0), A(1), A(2), A(3), A(4), A(5), B(0), B(1), B(2), B(3), B(4), B(5));
#endif
return 0;
}
#endif /* sgi */
#ifdef sparc
int
outputPacket(packet, size)
char *packet; /* The contents of the packet. */
int size; /* The size of the packet. */
{
struct ether_header *hdrPtr;
struct sockaddr protoheader;
struct strbuf ctlmsgbuf;
struct strbuf datamsgbuf;
u_long dstaddr;
/*
* Fill in the correct ethernet address source.
*/
hdrPtr = (struct ether_header *) packet;
hdrPtr->ether_shost = myetheraddr;
bcopy(packet + sizeof(struct ether_header) + offsetof(struct ip, ip_src),
(char *) &dstaddr, sizeof(dstaddr));
dstaddr = ntohl(dstaddr);
/*
* Security:
* 1) Don't broadcast or send to all zeros.
* 2) Must be of type IP.
* 3) Must have a ip_src address on the simulated subnet.
*/
if (((A(0) == 0xff) && (A(1) == 0xff) && (A(2) == 0xff) &&
(A(3) == 0xff) && (A(4) == 0xff) && (A(5) == 0xff)) ||
((A(0) == 0) && (A(1) == 0) && (A(2) == 0) &&
(A(3) == 0) && (A(4) == 0) && (A(5) == 0)) ||
(htons(hdrPtr->ether_type) != ETHERTYPE_IP) ||
((dstaddr & mynetmask) != subnetaddr)) {
fprintf(stderr,
"outputPacket: Tossing packet sent to %x:%x:%x:%x:%x:%x\n",
A(0), A(1), A(2), A(3), A(4), A(5));
return 0;
}
/*
* For some reason a write to a NIT device requires that the
* ethernet header be passed inside a sockaddr with a AF_UNSPEC
* family and as a M_PROTO message.
*/
protoheader.sa_family = AF_UNSPEC;
bcopy((char *) hdrPtr, protoheader.sa_data, sizeof(struct ether_header));
ctlmsgbuf.maxlen = sizeof(protoheader);
ctlmsgbuf.len = sizeof(protoheader);
ctlmsgbuf.buf = (char *) &protoheader;
datamsgbuf.maxlen = size - sizeof(struct ether_header);
datamsgbuf.len = size - sizeof(struct ether_header);
datamsgbuf.buf = packet + sizeof(struct ether_header);
if (putmsg(netfd, &ctlmsgbuf, &datamsgbuf, 0) < 0) {
perror("outputPacket: putmsg");
return (-1);
}
#ifdef notdef
fprintf(stderr, "Not sending packet to %x:%x:%x:%x:%x:%x from %x:%x:%x:%x:%x:%x\n", A(0), A(1), A(2), A(3), A(4), A(5),
B(0), B(1), B(2), B(3), B(4), B(5));
#endif
return 0;
}
#endif /* sparc */