FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_error.c
1 /* $NetBSD: ns_error.c,v 1.17 2005/02/26 22:39:50 perry Exp $ */
2
3 /*
4 * Copyright (c) 1984, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ns_error.c 8.2 (Berkeley) 9/22/94
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ns_error.c,v 1.17 2005/02/26 22:39:50 perry Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45
46 #include <net/if.h>
47 #include <net/route.h>
48
49 #include <netns/ns.h>
50 #include <netns/ns_pcb.h>
51 #include <netns/ns_if.h>
52 #include <netns/ns_var.h>
53 #include <netns/idp.h>
54 #include <netns/idp_var.h>
55 #include <netns/ns_error.h>
56 #include <netns/sp.h>
57 #include <netns/spidp.h>
58 #include <netns/spp_timer.h>
59 #include <netns/spp_var.h>
60
61 #ifdef lint
62 #define NS_ERRPRINTFS 1
63 #endif
64
65 #ifdef NS_ERRPRINTFS
66 /*
67 * NS_ERR routines: error generation, receive packet processing, and
68 * routines to turnaround packets back to the originator.
69 */
70 int ns_errprintfs = 0;
71 #endif
72
73 struct ns_errstat ns_errstat;
74
75 int
76 ns_err_x(int c)
77 {
78 u_int16_t *w, *lim, *base = ns_errstat.ns_es_codes;
79 u_int16_t x = c;
80
81 /*
82 * zero is a legit error code, handle specially
83 */
84 if (x == 0)
85 return (0);
86 lim = base + NS_ERR_MAX - 1;
87 for (w = base + 1; w < lim; w++) {
88 if (*w == 0)
89 *w = x;
90 if (*w == x)
91 break;
92 }
93 return (w - base);
94 }
95
96 /*
97 * Generate an error packet of type error
98 * in response to bad packet.
99 */
100 void
101 ns_error(struct mbuf *om, int type, int param)
102 {
103 struct ns_epidp *ep;
104 struct mbuf *m;
105 struct idp *nip;
106 struct idp *oip = mtod(om, struct idp *);
107 extern int idpcksum;
108
109 /*
110 * If this packet was sent to the echo port,
111 * and nobody was there, just echo it.
112 * (Yes, this is a wart!)
113 */
114 if (type == NS_ERR_NOSOCK &&
115 oip->idp_dna.x_port == htons(2) &&
116 (type = ns_echo(om))==0)
117 return;
118
119 #ifdef NS_ERRPRINTFS
120 if (ns_errprintfs)
121 printf("ns_err_error(%x, %d, %d)\n", oip, type, param);
122 #endif
123 /*
124 * Don't Generate error packets in response to multicasts.
125 */
126 if (oip->idp_dna.x_host.c_host[0] & 1)
127 goto freeit;
128
129 ns_errstat.ns_es_error++;
130 /*
131 * Make sure that the old IDP packet had 30 bytes of data to return;
132 * if not, don't bother. Also don't EVER error if the old
133 * packet protocol was NS_ERR.
134 */
135 if (oip->idp_len < sizeof(struct idp)) {
136 ns_errstat.ns_es_oldshort++;
137 goto freeit;
138 }
139 if (oip->idp_pt == NSPROTO_ERROR) {
140 ns_errstat.ns_es_oldns_err++;
141 goto freeit;
142 }
143
144 /*
145 * First, formulate ns_err message
146 */
147 m = m_gethdr(M_DONTWAIT, MT_HEADER);
148 if (m == NULL)
149 goto freeit;
150 m->m_len = sizeof(*ep);
151 MH_ALIGN(m, m->m_len);
152 ep = mtod(m, struct ns_epidp *);
153 if ((u_int)type > NS_ERR_TOO_BIG)
154 panic("ns_err_error");
155 ns_errstat.ns_es_outhist[ns_err_x(type)]++;
156 ep->ns_ep_errp.ns_err_num = htons((u_int16_t)type);
157 ep->ns_ep_errp.ns_err_param = htons((u_int16_t)param);
158 bcopy((caddr_t)oip, (caddr_t)&ep->ns_ep_errp.ns_err_idp, 42);
159 nip = &ep->ns_ep_idp;
160 nip->idp_len = sizeof(*ep);
161 nip->idp_len = htons((u_int16_t)nip->idp_len);
162 nip->idp_pt = NSPROTO_ERROR;
163 nip->idp_tc = 0;
164 nip->idp_dna = oip->idp_sna;
165 nip->idp_sna = oip->idp_dna;
166 if (idpcksum) {
167 nip->idp_sum = 0;
168 nip->idp_sum = ns_cksum(m, sizeof(*ep));
169 } else
170 nip->idp_sum = 0xffff;
171 (void) ns_output(m, (struct route *)0, 0);
172
173 freeit:
174 m_freem(om);
175 }
176
177 void
178 ns_printhost(struct ns_addr *p)
179 {
180
181 printf("<net:%x%x,host:%x%x%x,port:%x>",
182 p->x_net.s_net[0],
183 p->x_net.s_net[1],
184 p->x_host.s_host[0],
185 p->x_host.s_host[1],
186 p->x_host.s_host[2],
187 p->x_port);
188
189 }
190
191 /*
192 * Process a received NS_ERR message.
193 */
194 void
195 ns_err_input(struct mbuf *m)
196 {
197 struct ns_errp *ep;
198 #ifdef NS_ERRPRINTFS
199 struct ns_epidp *epidp = mtod(m, struct ns_epidp *);
200 int param;
201 #endif
202 int i;
203 int type, code;
204
205 /*
206 * Locate ns_err structure in mbuf, and check
207 * that not corrupted and of at least minimum length.
208 */
209 #ifdef NS_ERRPRINTFS
210 if (ns_errprintfs) {
211 printf("ns_err_input from ");
212 ns_printhost(&epidp->ns_ep_idp.idp_sna);
213 printf("len %d\n", ntohs(epidp->ns_ep_idp.idp_len));
214 }
215 #endif
216 i = sizeof (struct ns_epidp);
217 if (((m->m_flags & M_EXT) || m->m_len < i) &&
218 (m = m_pullup(m, i)) == 0) {
219 ns_errstat.ns_es_tooshort++;
220 return;
221 }
222 ep = &(mtod(m, struct ns_epidp *)->ns_ep_errp);
223 type = ntohs(ep->ns_err_num);
224 #ifdef NS_ERRPRINTFS
225 param = ntohs(ep->ns_err_param);
226 #endif
227 ns_errstat.ns_es_inhist[ns_err_x(type)]++;
228
229 #ifdef NS_ERRPRINTFS
230 /*
231 * Message type specific processing.
232 */
233 if (ns_errprintfs)
234 printf("ns_err_input, type %d param %d\n", type, param);
235 #endif
236 if (type >= NS_ERR_TOO_BIG) {
237 goto badcode;
238 }
239 ns_errstat.ns_es_outhist[ns_err_x(type)]++;
240 switch (type) {
241
242 case NS_ERR_UNREACH_HOST:
243 code = PRC_UNREACH_NET;
244 goto deliver;
245
246 case NS_ERR_TOO_OLD:
247 code = PRC_TIMXCEED_INTRANS;
248 goto deliver;
249
250 case NS_ERR_TOO_BIG:
251 code = PRC_MSGSIZE;
252 goto deliver;
253
254 case NS_ERR_FULLUP:
255 code = PRC_QUENCH;
256 goto deliver;
257
258 case NS_ERR_NOSOCK:
259 code = PRC_UNREACH_PORT;
260 goto deliver;
261
262 case NS_ERR_UNSPEC_T:
263 case NS_ERR_BADSUM_T:
264 case NS_ERR_BADSUM:
265 case NS_ERR_UNSPEC:
266 code = PRC_PARAMPROB;
267 goto deliver;
268
269 deliver:
270 /*
271 * Problem with datagram; advise higher level routines.
272 */
273 #ifdef NS_ERRPRINTFS
274 if (ns_errprintfs)
275 printf("deliver to protocol %d\n",
276 ep->ns_err_idp.idp_pt);
277 #endif
278 switch(ep->ns_err_idp.idp_pt) {
279 case NSPROTO_SPP:
280 spp_ctlinput(code, NULL, ep);
281 break;
282
283 default:
284 idp_ctlinput(code, NULL, ep);
285 }
286
287 goto freeit;
288
289 default:
290 badcode:
291 ns_errstat.ns_es_badcode++;
292 goto freeit;
293
294 }
295 freeit:
296 m_freem(m);
297 }
298
299 #ifdef notdef
300 u_int32_t
301 nstime(void)
302 {
303 int s = splclock();
304 u_int32_t t;
305
306 t = (time.tv_sec % (24*60*60)) * 1000 + time.tv_usec / 1000;
307 splx(s);
308 return (htonl(t));
309 }
310 #endif
311
312 int
313 ns_echo(struct mbuf *m)
314 {
315 struct idp *idp = mtod(m, struct idp *);
316 struct echo {
317 struct idp ec_idp;
318 u_int16_t ec_op; /* Operation, 1 = request, 2 = reply */
319 } *ec = (struct echo *)idp;
320 struct ns_addr temp;
321
322 if (idp->idp_pt!=NSPROTO_ECHO) return(NS_ERR_NOSOCK);
323 if (ec->ec_op!=htons(1)) return(NS_ERR_UNSPEC);
324
325 ec->ec_op = htons(2);
326
327 temp = idp->idp_dna;
328 idp->idp_dna = idp->idp_sna;
329 idp->idp_sna = temp;
330
331 if (idp->idp_sum != 0xffff) {
332 idp->idp_sum = 0;
333 idp->idp_sum = ns_cksum(m,
334 (int)(((ntohs(idp->idp_len) - 1)|1)+1));
335 }
336 (void) ns_output(m, (struct route *)0, NS_FORWARDING);
337 return(0);
338 }
Cache object: 0fff78ff4b6e6a6985f4261abf38ea90
|