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