kernel tree reorganization stage 1: Major cvs repository work (not logged as
[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.4 2003/08/07 21:17:38 dillon 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 #ifdef lint
55 #define NS_ERRPRINTFS 1
56 #endif
57
58 #ifdef NS_ERRPRINTFS
59 /*
60  * NS_ERR routines: error generation, receive packet processing, and
61  * routines to turnaround packets back to the originator.
62  */
63 int     ns_errprintfs = 0;
64 #endif
65
66 ns_err_x(c)
67 {
68         u_short *w, *lim, *base = ns_errstat.ns_es_codes;
69         u_short x = c;
70
71         /*
72          * zero is a legit error code, handle specially
73          */
74         if (x == 0)
75                 return (0);
76         lim = base + NS_ERR_MAX - 1;
77         for (w = base + 1; w < lim; w++) {
78                 if (*w == 0)
79                         *w = x;
80                 if (*w == x)
81                         break;
82         }
83         return (w - base);
84 }
85
86 /*
87  * Generate an error packet of type error
88  * in response to bad packet.
89  */
90
91 ns_error(om, type, param)
92         struct mbuf *om;
93         int type;
94 {
95         struct ns_epidp *ep;
96         struct mbuf *m;
97         struct idp *nip;
98         struct idp *oip = mtod(om, struct idp *);
99         extern int idpcksum;
100
101         /*
102          * If this packet was sent to the echo port,
103          * and nobody was there, just echo it.
104          * (Yes, this is a wart!)
105          */
106         if (type == NS_ERR_NOSOCK &&
107             oip->idp_dna.x_port == htons(2) &&
108             (type = ns_echo(om))==0)
109                 return;
110
111 #ifdef NS_ERRPRINTFS
112         if (ns_errprintfs)
113                 printf("ns_err_error(%x, %d, %d)\n", oip, type, param);
114 #endif
115         /*
116          * Don't Generate error packets in response to multicasts.
117          */
118         if (oip->idp_dna.x_host.c_host[0] & 1)
119                 goto freeit;
120
121         ns_errstat.ns_es_error++;
122         /*
123          * Make sure that the old IDP packet had 30 bytes of data to return;
124          * if not, don't bother.  Also don't EVER error if the old
125          * packet protocol was NS_ERR.
126          */
127         if (oip->idp_len < sizeof(struct idp)) {
128                 ns_errstat.ns_es_oldshort++;
129                 goto freeit;
130         }
131         if (oip->idp_pt == NSPROTO_ERROR) {
132                 ns_errstat.ns_es_oldns_err++;
133                 goto freeit;
134         }
135
136         /*
137          * First, formulate ns_err message
138          */
139         m = m_gethdr(M_DONTWAIT, MT_HEADER);
140         if (m == NULL)
141                 goto freeit;
142         m->m_len = sizeof(*ep);
143         MH_ALIGN(m, m->m_len);
144         ep = mtod(m, struct ns_epidp *);
145         if ((u_int)type > NS_ERR_TOO_BIG)
146                 panic("ns_err_error");
147         ns_errstat.ns_es_outhist[ns_err_x(type)]++;
148         ep->ns_ep_errp.ns_err_num = htons((u_short)type);
149         ep->ns_ep_errp.ns_err_param = htons((u_short)param);
150         bcopy((caddr_t)oip, (caddr_t)&ep->ns_ep_errp.ns_err_idp, 42);
151         nip = &ep->ns_ep_idp;
152         nip->idp_len = sizeof(*ep);
153         nip->idp_len = htons((u_short)nip->idp_len);
154         nip->idp_pt = NSPROTO_ERROR;
155         nip->idp_tc = 0;
156         nip->idp_dna = oip->idp_sna;
157         nip->idp_sna = oip->idp_dna;
158         if (idpcksum) {
159                 nip->idp_sum = 0;
160                 nip->idp_sum = ns_cksum(m, sizeof(*ep));
161         } else
162                 nip->idp_sum = 0xffff;
163         (void) ns_output(m, (struct route *)0, 0);
164
165 freeit:
166         m_freem(om);
167 }
168
169 ns_printhost(p)
170 struct ns_addr *p;
171 {
172
173         printf("<net:%x%x,host:%x%x%x,port:%x>",
174                         p->x_net.s_net[0],
175                         p->x_net.s_net[1],
176                         p->x_host.s_host[0],
177                         p->x_host.s_host[1],
178                         p->x_host.s_host[2],
179                         p->x_port);
180
181 }
182
183 /*
184  * Process a received NS_ERR message.
185  */
186 ns_err_input(m)
187         struct mbuf *m;
188 {
189         struct ns_errp *ep;
190         struct ns_epidp *epidp = mtod(m, struct ns_epidp *);
191         int i;
192         int type, code, param;
193
194         /*
195          * Locate ns_err structure in mbuf, and check
196          * that not corrupted and of at least minimum length.
197          */
198 #ifdef NS_ERRPRINTFS
199         if (ns_errprintfs) {
200                 printf("ns_err_input from ");
201                 ns_printhost(&epidp->ns_ep_idp.idp_sna);
202                 printf("len %d\n", ntohs(epidp->ns_ep_idp.idp_len));
203         }
204 #endif
205         i = sizeof (struct ns_epidp);
206         if (((m->m_flags & M_EXT) || m->m_len < i) &&
207                 (m = m_pullup(m, i)) == 0)  {
208                 ns_errstat.ns_es_tooshort++;
209                 return;
210         }
211         ep = &(mtod(m, struct ns_epidp *)->ns_ep_errp);
212         type = ntohs(ep->ns_err_num);
213         param = ntohs(ep->ns_err_param);
214         ns_errstat.ns_es_inhist[ns_err_x(type)]++;
215
216 #ifdef NS_ERRPRINTFS
217         /*
218          * Message type specific processing.
219          */
220         if (ns_errprintfs)
221                 printf("ns_err_input, type %d param %d\n", type, param);
222 #endif
223         if (type >= NS_ERR_TOO_BIG) {
224                 goto badcode;
225         }
226         ns_errstat.ns_es_outhist[ns_err_x(type)]++;
227         switch (type) {
228
229         case NS_ERR_UNREACH_HOST:
230                 code = PRC_UNREACH_NET;
231                 goto deliver;
232
233         case NS_ERR_TOO_OLD:
234                 code = PRC_TIMXCEED_INTRANS;
235                 goto deliver;
236
237         case NS_ERR_TOO_BIG:
238                 code = PRC_MSGSIZE;
239                 goto deliver;
240
241         case NS_ERR_FULLUP:
242                 code = PRC_QUENCH;
243                 goto deliver;
244
245         case NS_ERR_NOSOCK:
246                 code = PRC_UNREACH_PORT;
247                 goto deliver;
248
249         case NS_ERR_UNSPEC_T:
250         case NS_ERR_BADSUM_T:
251         case NS_ERR_BADSUM:
252         case NS_ERR_UNSPEC:
253                 code = PRC_PARAMPROB;
254                 goto deliver;
255
256         deliver:
257                 /*
258                  * Problem with datagram; advise higher level routines.
259                  */
260 #ifdef NS_ERRPRINTFS
261                 if (ns_errprintfs)
262                         printf("deliver to protocol %d\n",
263                                        ep->ns_err_idp.idp_pt);
264 #endif
265                 switch(ep->ns_err_idp.idp_pt) {
266                 case NSPROTO_SPP:
267                         spp_ctlinput(code, (caddr_t)ep);
268                         break;
269
270                 default:
271                         idp_ctlinput(code, (caddr_t)ep);
272                 }
273
274                 goto freeit;
275
276         default:
277         badcode:
278                 ns_errstat.ns_es_badcode++;
279                 goto freeit;
280
281         }
282 freeit:
283         m_freem(m);
284 }
285
286 #ifdef notdef
287 u_long
288 nstime()
289 {
290         int s = splclock();
291         u_long t;
292
293         t = (time.tv_sec % (24*60*60)) * 1000 + time.tv_usec / 1000;
294         splx(s);
295         return (htonl(t));
296 }
297 #endif
298
299 ns_echo(m)
300 struct mbuf *m;
301 {
302         struct idp *idp = mtod(m, struct idp *);
303         struct echo {
304             struct idp  ec_idp;
305             u_short             ec_op; /* Operation, 1 = request, 2 = reply */
306         } *ec = (struct echo *)idp;
307         struct ns_addr temp;
308
309         if (idp->idp_pt!=NSPROTO_ECHO) return(NS_ERR_NOSOCK);
310         if (ec->ec_op!=htons(1)) return(NS_ERR_UNSPEC);
311
312         ec->ec_op = htons(2);
313
314         temp = idp->idp_dna;
315         idp->idp_dna = idp->idp_sna;
316         idp->idp_sna = temp;
317
318         if (idp->idp_sum != 0xffff) {
319                 idp->idp_sum = 0;
320                 idp->idp_sum = ns_cksum(m,
321                     (int)(((ntohs(idp->idp_len) - 1)|1)+1));
322         }
323         (void) ns_output(m, (struct route *)0, NS_FORWARDING);
324         return(0);
325 }