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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/* $OpenBSD: ns_error.c,v 1.4 2003/12/10 07:22:44 itojun Exp $ */
/* $NetBSD: ns_error.c,v 1.6 1996/02/13 22:13:53 christos Exp $ */
/*
* Copyright (c) 1984, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ns_error.c 8.1 (Berkeley) 6/10/93
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <net/if.h>
#include <net/route.h>
#include <netns/ns.h>
#include <netns/ns_pcb.h>
#include <netns/ns_if.h>
#include <netns/ns_var.h>
#include <netns/idp.h>
#include <netns/idp_var.h>
#include <netns/ns_error.h>
#include <netns/sp.h>
#include <netns/spidp.h>
#include <netns/spp_timer.h>
#include <netns/spp_var.h>
#ifdef lint
#define NS_ERRPRINTFS 1
#endif
#ifdef NS_ERRPRINTFS
/*
* NS_ERR routines: error generation, receive packet processing, and
* routines to turnaround packets back to the originator.
*/
int ns_errprintfs = 0;
#endif
int
ns_err_x(c)
int c;
{
u_short *w, *lim, *base = ns_errstat.ns_es_codes;
u_short x = c;
/*
* zero is a legit error code, handle specially
*/
if (x == 0)
return (0);
lim = base + NS_ERR_MAX - 1;
for (w = base + 1; w < lim; w++) {
if (*w == 0)
*w = x;
if (*w == x)
break;
}
return (w - base);
}
/*
* Generate an error packet of type error
* in response to bad packet.
*/
void
ns_error(om, type, param)
struct mbuf *om;
int type;
int param;
{
struct ns_epidp *ep;
struct mbuf *m;
struct idp *nip;
struct idp *oip = mtod(om, struct idp *);
extern int idpcksum;
/*
* If this packet was sent to the echo port,
* and nobody was there, just echo it.
* (Yes, this is a wart!)
*/
if (type == NS_ERR_NOSOCK &&
oip->idp_dna.x_port == htons(2) &&
(type = ns_echo(om))==0)
return;
#ifdef NS_ERRPRINTFS
if (ns_errprintfs)
printf("ns_err_error(%x, %d, %d)\n", oip, type, param);
#endif
/*
* Don't Generate error packets in response to multicasts.
*/
if (oip->idp_dna.x_host.c_host[0] & 1)
goto freeit;
ns_errstat.ns_es_error++;
/*
* Make sure that the old IDP packet had 30 bytes of data to return;
* if not, don't bother. Also don't EVER error if the old
* packet protocol was NS_ERR.
*/
if (oip->idp_len < sizeof(struct idp)) {
ns_errstat.ns_es_oldshort++;
goto freeit;
}
if (oip->idp_pt == NSPROTO_ERROR) {
ns_errstat.ns_es_oldns_err++;
goto freeit;
}
/*
* First, formulate ns_err message
*/
m = m_gethdr(M_DONTWAIT, MT_HEADER);
if (m == NULL)
goto freeit;
m->m_len = sizeof(*ep);
MH_ALIGN(m, m->m_len);
ep = mtod(m, struct ns_epidp *);
if ((u_int)type > NS_ERR_TOO_BIG)
panic("ns_err_error");
ns_errstat.ns_es_outhist[ns_err_x(type)]++;
ep->ns_ep_errp.ns_err_num = htons((u_short)type);
ep->ns_ep_errp.ns_err_param = htons((u_short)param);
bcopy((caddr_t)oip, (caddr_t)&ep->ns_ep_errp.ns_err_idp, 42);
nip = &ep->ns_ep_idp;
nip->idp_len = sizeof(*ep);
nip->idp_len = htons((u_short)nip->idp_len);
nip->idp_pt = NSPROTO_ERROR;
nip->idp_tc = 0;
nip->idp_dna = oip->idp_sna;
nip->idp_sna = oip->idp_dna;
if (idpcksum) {
nip->idp_sum = 0;
nip->idp_sum = ns_cksum(m, sizeof(*ep));
} else
nip->idp_sum = 0xffff;
(void) ns_output(m, (struct route *)0, 0);
freeit:
m_freem(om);
}
void
ns_printhost(p)
register struct ns_addr *p;
{
printf("<net:%x%x,host:%x%x%x,port:%x>",
p->x_net.s_net[0],
p->x_net.s_net[1],
p->x_host.s_host[0],
p->x_host.s_host[1],
p->x_host.s_host[2],
p->x_port);
}
/*
* Process a received NS_ERR message.
*/
void
ns_err_input(m)
struct mbuf *m;
{
struct ns_errp *ep;
#ifdef NS_ERRPRINTFS
struct ns_epidp *epidp = mtod(m, struct ns_epidp *);
#endif
int i;
int type, code, param;
/*
* Locate ns_err structure in mbuf, and check
* that not corrupted and of at least minimum length.
*/
#ifdef NS_ERRPRINTFS
if (ns_errprintfs) {
printf("ns_err_input from ");
ns_printhost(&epidp->ns_ep_idp.idp_sna);
printf("len %d\n", ntohs(epidp->ns_ep_idp.idp_len));
}
#endif
i = sizeof (struct ns_epidp);
if (((m->m_flags & M_EXT) || m->m_len < i) &&
(m = m_pullup(m, i)) == 0) {
ns_errstat.ns_es_tooshort++;
return;
}
ep = &(mtod(m, struct ns_epidp *)->ns_ep_errp);
type = ntohs(ep->ns_err_num);
param = ntohs(ep->ns_err_param);
ns_errstat.ns_es_inhist[ns_err_x(type)]++;
#ifdef NS_ERRPRINTFS
/*
* Message type specific processing.
*/
if (ns_errprintfs)
printf("ns_err_input, type %d param %d\n", type, param);
#endif
if (type >= NS_ERR_TOO_BIG) {
goto badcode;
}
ns_errstat.ns_es_outhist[ns_err_x(type)]++;
switch (type) {
case NS_ERR_UNREACH_HOST:
code = PRC_UNREACH_NET;
goto deliver;
case NS_ERR_TOO_OLD:
code = PRC_TIMXCEED_INTRANS;
goto deliver;
case NS_ERR_TOO_BIG:
code = PRC_MSGSIZE;
goto deliver;
case NS_ERR_FULLUP:
code = PRC_QUENCH;
goto deliver;
case NS_ERR_NOSOCK:
code = PRC_UNREACH_PORT;
goto deliver;
case NS_ERR_UNSPEC_T:
case NS_ERR_BADSUM_T:
case NS_ERR_BADSUM:
case NS_ERR_UNSPEC:
code = PRC_PARAMPROB;
goto deliver;
deliver:
/*
* Problem with datagram; advise higher level routines.
*/
#ifdef NS_ERRPRINTFS
if (ns_errprintfs)
printf("deliver to protocol %d\n",
ep->ns_err_idp.idp_pt);
#endif
switch(ep->ns_err_idp.idp_pt) {
case NSPROTO_SPP:
spp_ctlinput(code, NULL, ep);
break;
default:
idp_ctlinput(code, NULL, ep);
}
goto freeit;
default:
badcode:
ns_errstat.ns_es_badcode++;
goto freeit;
}
freeit:
m_freem(m);
}
#ifdef notdef
u_long
nstime()
{
int s = splclock();
u_long t;
t = (time.tv_sec % (24*60*60)) * 1000 + time.tv_usec / 1000;
splx(s);
return (htonl(t));
}
#endif
int
ns_echo(m)
struct mbuf *m;
{
struct idp *idp = mtod(m, struct idp *);
struct echo {
struct idp ec_idp;
u_short ec_op; /* Operation, 1 = request, 2 = reply */
} *ec = (struct echo *)idp;
struct ns_addr temp;
if (idp->idp_pt!=NSPROTO_ECHO) return(NS_ERR_NOSOCK);
if (ec->ec_op!=htons(1)) return(NS_ERR_UNSPEC);
ec->ec_op = htons(2);
temp = idp->idp_dna;
idp->idp_dna = idp->idp_sna;
idp->idp_sna = temp;
if (idp->idp_sum != 0xffff) {
idp->idp_sum = 0;
idp->idp_sum = ns_cksum(m,
(int)(((ntohs(idp->idp_len) - 1)|1)+1));
}
(void) ns_output(m, (struct route *)0, NS_FORWARDING);
return(0);
}
|