]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netns/ns_error.c
ea71eee6d6053ae233caf7eadce2159253e89c8a
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1984, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ns_error.c 8.1 (Berkeley) 6/10/93
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/malloc.h>
61 #include <sys/protosw.h>
62 #include <sys/socket.h>
64 #include <sys/kernel.h>
66 #include <net/route.h>
69 #include <netns/ns_pcb.h>
70 #include <netns/idp.h>
71 #include <netns/ns_error.h>
74 #define NS_ERRPRINTFS 1
79 * NS_ERR routines: error generation, receive packet processing, and
80 * routines to turnaround packets back to the originator.
82 int ns_errprintfs
= 0;
87 register u_short
*w
, *lim
, *base
= ns_errstat
.ns_es_codes
;
91 * zero is a legit error code, handle specially
95 lim
= base
+ NS_ERR_MAX
- 1;
96 for (w
= base
+ 1; w
< lim
; w
++) {
106 * Generate an error packet of type error
107 * in response to bad packet.
110 ns_error(om
, type
, param
)
114 register struct ns_epidp
*ep
;
117 register struct idp
*oip
= mtod(om
, struct idp
*);
121 * If this packet was sent to the echo port,
122 * and nobody was there, just echo it.
123 * (Yes, this is a wart!)
125 if (type
== NS_ERR_NOSOCK
&&
126 oip
->idp_dna
.x_port
== htons(2) &&
127 (type
= ns_echo(om
))==0)
132 printf("ns_err_error(%x, %d, %d)\n", oip
, type
, param
);
135 * Don't Generate error packets in response to multicasts.
137 if (oip
->idp_dna
.x_host
.c_host
[0] & 1)
140 ns_errstat
.ns_es_error
++;
142 * Make sure that the old IDP packet had 30 bytes of data to return;
143 * if not, don't bother. Also don't EVER error if the old
144 * packet protocol was NS_ERR.
146 if (oip
->idp_len
< sizeof(struct idp
)) {
147 ns_errstat
.ns_es_oldshort
++;
150 if (oip
->idp_pt
== NSPROTO_ERROR
) {
151 ns_errstat
.ns_es_oldns_err
++;
156 * First, formulate ns_err message
158 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
161 m
->m_len
= sizeof(*ep
);
162 MH_ALIGN(m
, m
->m_len
);
163 ep
= mtod(m
, struct ns_epidp
*);
164 if ((u_int
)type
> NS_ERR_TOO_BIG
)
165 panic("ns_err_error");
166 ns_errstat
.ns_es_outhist
[ns_err_x(type
)]++;
167 ep
->ns_ep_errp
.ns_err_num
= htons((u_short
)type
);
168 ep
->ns_ep_errp
.ns_err_param
= htons((u_short
)param
);
169 bcopy((caddr_t
)oip
, (caddr_t
)&ep
->ns_ep_errp
.ns_err_idp
, 42);
170 nip
= &ep
->ns_ep_idp
;
171 nip
->idp_len
= sizeof(*ep
);
172 nip
->idp_len
= htons((u_short
)nip
->idp_len
);
173 nip
->idp_pt
= NSPROTO_ERROR
;
175 nip
->idp_dna
= oip
->idp_sna
;
176 nip
->idp_sna
= oip
->idp_dna
;
179 nip
->idp_sum
= ns_cksum(m
, sizeof(*ep
));
181 nip
->idp_sum
= 0xffff;
182 (void) ns_output(m
, (struct route
*)0, 0);
189 register struct ns_addr
*p
;
192 printf("<net:%x%x,host:%x%x%x,port:%x>",
203 * Process a received NS_ERR message.
208 register struct ns_errp
*ep
;
209 register struct ns_epidp
*epidp
= mtod(m
, struct ns_epidp
*);
211 int type
, code
, param
;
214 * Locate ns_err structure in mbuf, and check
215 * that not corrupted and of at least minimum length.
219 printf("ns_err_input from ");
220 ns_printhost(&epidp
->ns_ep_idp
.idp_sna
);
221 printf("len %d\n", ntohs(epidp
->ns_ep_idp
.idp_len
));
224 i
= sizeof (struct ns_epidp
);
225 if (((m
->m_flags
& M_EXT
) || m
->m_len
< i
) &&
226 (m
= m_pullup(m
, i
)) == 0) {
227 ns_errstat
.ns_es_tooshort
++;
230 ep
= &(mtod(m
, struct ns_epidp
*)->ns_ep_errp
);
231 type
= ntohs(ep
->ns_err_num
);
232 param
= ntohs(ep
->ns_err_param
);
233 ns_errstat
.ns_es_inhist
[ns_err_x(type
)]++;
237 * Message type specific processing.
240 printf("ns_err_input, type %d param %d\n", type
, param
);
242 if (type
>= NS_ERR_TOO_BIG
) {
245 ns_errstat
.ns_es_outhist
[ns_err_x(type
)]++;
248 case NS_ERR_UNREACH_HOST
:
249 code
= PRC_UNREACH_NET
;
253 code
= PRC_TIMXCEED_INTRANS
;
265 code
= PRC_UNREACH_PORT
;
268 case NS_ERR_UNSPEC_T
:
269 case NS_ERR_BADSUM_T
:
272 code
= PRC_PARAMPROB
;
277 * Problem with datagram; advise higher level routines.
281 printf("deliver to protocol %d\n",
282 ep
->ns_err_idp
.idp_pt
);
284 switch(ep
->ns_err_idp
.idp_pt
) {
286 spp_ctlinput(code
, (caddr_t
)ep
);
290 idp_ctlinput(code
, (caddr_t
)ep
);
297 ns_errstat
.ns_es_badcode
++;
312 t
= (time
.tv_sec
% (24*60*60)) * 1000 + time
.tv_usec
/ 1000;
321 register struct idp
*idp
= mtod(m
, struct idp
*);
322 register struct echo
{
324 u_short ec_op
; /* Operation, 1 = request, 2 = reply */
325 } *ec
= (struct echo
*)idp
;
328 if (idp
->idp_pt
!=NSPROTO_ECHO
) return(NS_ERR_NOSOCK
);
329 if (ec
->ec_op
!=htons(1)) return(NS_ERR_UNSPEC
);
331 ec
->ec_op
= htons(2);
334 idp
->idp_dna
= idp
->idp_sna
;
337 if (idp
->idp_sum
!= 0xffff) {
339 idp
->idp_sum
= ns_cksum(m
,
340 (int)(((ntohs(idp
->idp_len
) - 1)|1)+1));
342 (void) ns_output(m
, (struct route
*)0, NS_FORWARDING
);