Dispatch upper-half protocol request handling.
[dragonfly.git] / sys / netproto / ns / ns_input.c
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987, 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_input.c  8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netns/ns_input.c,v 1.13 2000/02/13 03:32:04 peter Exp $
35  * $DragonFly: src/sys/netproto/ns/ns_input.c,v 1.9 2004/03/06 01:58:57 hsu 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/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52 #include <net/raw_cb.h>
53 #include <net/netisr.h>
54
55 #include "ns.h"
56 #include "ns_if.h"
57 #include "ns_pcb.h"
58 #include "idp.h"
59 #include "idp_var.h"
60 #include "ns_error.h"
61
62 extern void spp_input(struct mbuf *, struct nspcb *);   /* spp_usrreq.c XXX */
63
64 /*
65  * NS initialization.
66  */
67 union ns_host   ns_thishost;
68 union ns_host   ns_zerohost;
69 union ns_host   ns_broadhost;
70 union ns_net    ns_zeronet;
71 union ns_net    ns_broadnet;
72 struct sockaddr_ns ns_netmask, ns_hostmask;
73
74 static u_short allones[] = {-1, -1, -1};
75
76 struct nspcb nsrawpcb;
77
78 int     idpcksum = 1;
79 long    ns_pexseq;
80
81 static void nsintr(struct netmsg *msg);
82
83 void
84 ns_init()
85 {
86         ns_broadhost = * (union ns_host *) allones;
87         ns_broadnet = * (union ns_net *) allones;
88         nspcb.nsp_next = nspcb.nsp_prev = &nspcb;
89         nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb;
90         ns_pexseq = tick;
91         ns_netmask.sns_len = 6;
92         ns_netmask.sns_addr.x_net = ns_broadnet;
93         ns_hostmask.sns_len = 12;
94         ns_hostmask.sns_addr.x_net = ns_broadnet;
95         ns_hostmask.sns_addr.x_host = ns_broadhost;
96         netisr_register(NETISR_NS, cpu0_portfn, nsintr);
97 }
98
99 /*
100  * Idp input routine.  Pass to next level.
101  */
102 int nsintr_getpck = 0;
103 int nsintr_swtch = 0;
104
105 static void
106 nsintr(struct netmsg *msg)
107 {
108         struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
109         struct idp *idp;
110         struct nspcb *nsp;
111         int i;
112         int len, error;
113         char oddpacketp;
114
115         /*
116          * Get IDP header in first mbuf.
117          */
118         nsintr_getpck++;
119         if ((m->m_flags & M_EXT || m->m_len < sizeof (struct idp)) &&
120             (m = m_pullup(m, sizeof (struct idp))) == 0) {
121                 idpstat.idps_toosmall++;
122                 return;
123         }
124
125         /*
126          * Give any raw listeners a crack at the packet
127          */
128         for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
129                 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
130                 if (m1) idp_input(m1, nsp);
131         }
132
133         idp = mtod(m, struct idp *);
134         len = ntohs(idp->idp_len);
135         if ((oddpacketp = (len & 1))) {
136                 len++;          /* If this packet is of odd length,
137                                    preserve garbage byte for checksum */
138         }
139
140         /*
141          * Check that the amount of data in the buffers
142          * is as at least much as the IDP header would have us expect.
143          * Trim mbufs if longer than we expect.
144          * Drop packet if shorter than we expect.
145          */
146         if (m->m_pkthdr.len < len) {
147                 idpstat.idps_tooshort++;
148                 goto bad;
149         }
150         if (m->m_pkthdr.len > len) {
151                 if (m->m_len == m->m_pkthdr.len) {
152                         m->m_len = len;
153                         m->m_pkthdr.len = len;
154                 } else
155                         m_adj(m, len - m->m_pkthdr.len);
156         }
157         if (idpcksum && ((i = idp->idp_sum)!=0xffff)) {
158                 idp->idp_sum = 0;
159                 if (i != (idp->idp_sum = ns_cksum(m, len))) {
160                         idpstat.idps_badsum++;
161                         idp->idp_sum = i;
162                         if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
163                                 error = NS_ERR_BADSUM;
164                         else
165                                 error = NS_ERR_BADSUM_T;
166                         ns_error(m, error, 0);
167                         return;
168                 }
169         }
170         /*
171          * Is this a directed broadcast?
172          */
173         if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) {
174                 if ((!ns_neteq(idp->idp_dna, idp->idp_sna)) &&
175                     (!ns_neteqnn(idp->idp_dna.x_net, ns_broadnet)) &&
176                     (!ns_neteqnn(idp->idp_sna.x_net, ns_zeronet)) &&
177                     (!ns_neteqnn(idp->idp_dna.x_net, ns_zeronet)) ) {
178                         /*
179                          * Look to see if I need to eat this packet.
180                          * Algorithm is to forward all young packets
181                          * and prematurely age any packets which will
182                          * by physically broadcasted.
183                          * Any very old packets eaten without forwarding
184                          * would die anyway.
185                          *
186                          * Suggestion of Bill Nesheim, Cornell U.
187                          */
188                         if (idp->idp_tc < NS_MAXHOPS) {
189                                 idp_forward(m);
190                                 return;
191                         }
192                 }
193         /*
194          * Is this our packet? If not, forward.
195          */
196         } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) {
197                 idp_forward(m);
198                 return;
199         }
200         /*
201          * Locate pcb for datagram.
202          */
203         nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
204         /*
205          * Switch out to protocol's input routine.
206          */
207         nsintr_swtch++;
208         if (nsp) {
209                 if (oddpacketp) {
210                         m_adj(m, -1);
211                 }
212                 if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0)
213                         switch (idp->idp_pt) {
214
215                             case NSPROTO_SPP:
216                                     spp_input(m, nsp);
217                                     return;
218
219                             case NSPROTO_ERROR:
220                                     ns_err_input(m);
221                                     return;
222                         }
223                 idp_input(m, nsp);
224         } else {
225                 ns_error(m, NS_ERR_NOSOCK, 0);
226         }
227         return;
228
229 bad:
230         m_freem(m);
231         return;
232 }
233
234 u_char nsctlerrmap[PRC_NCMDS] = {
235         ECONNABORTED,   ECONNABORTED,   0,              0,
236         0,              0,              EHOSTDOWN,      EHOSTUNREACH,
237         ENETUNREACH,    EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
238         EMSGSIZE,       0,              0,              0,
239         0,              0,              0,              0
240 };
241
242 int idp_donosocks = 1;
243
244 void
245 idp_ctlinput(cmd, arg)
246         int cmd;
247         caddr_t arg;
248 {
249         struct ns_addr *ns;
250         struct nspcb *nsp;
251         struct ns_errp *errp = (struct ns_errp *)arg;   /* XXX */
252         int type;
253
254         if (cmd < 0 || cmd > PRC_NCMDS)
255                 return;
256         if (nsctlerrmap[cmd] == 0)
257                 return;         /* XXX */
258         type = NS_ERR_UNREACH_HOST;
259         switch (cmd) {
260                 struct sockaddr_ns *sns;
261
262         case PRC_IFDOWN:
263         case PRC_HOSTDEAD:
264         case PRC_HOSTUNREACH:
265                 sns = (struct sockaddr_ns *)arg;
266                 if (sns->sns_family != AF_NS)
267                         return;
268                 ns = &sns->sns_addr;
269                 break;
270
271         default:
272                 errp = (struct ns_errp *)arg;
273                 ns = &errp->ns_err_idp.idp_dna;
274                 type = errp->ns_err_num;
275                 type = ntohs((u_short)type);
276         }
277         switch (type) {
278
279         case NS_ERR_UNREACH_HOST:
280                 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0);
281                 break;
282
283         case NS_ERR_NOSOCK:
284                 nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port,
285                         NS_WILDCARD);
286                 if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr))
287                         (void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
288         }
289 }
290
291 int     idpprintfs = 0;
292 int     idpforwarding = 1;
293 /*
294  * Forward a packet.  If some error occurs return the sender
295  * an error packet.  Note we can't always generate a meaningful
296  * error message because the NS errors don't have a large enough repetoire
297  * of codes and types.
298  */
299 struct route idp_droute;
300 struct route idp_sroute;
301
302 void
303 idp_forward(m)
304 struct mbuf *m;
305 {
306         struct idp *idp = mtod(m, struct idp *);
307         int error, type, code;
308         struct mbuf *mcopy = NULL;
309         int agedelta = 1;
310         int flags = NS_FORWARDING;
311         int ok_there = 0;
312         int ok_back = 0;
313
314         if (idpprintfs) {
315                 printf("forward: src ");
316                 ns_printhost(&idp->idp_sna);
317                 printf(", dst ");
318                 ns_printhost(&idp->idp_dna);
319                 printf("hop count %d\n", idp->idp_tc);
320         }
321         if (idpforwarding == 0) {
322                 /* can't tell difference between net and host */
323                 type = NS_ERR_UNREACH_HOST, code = 0;
324                 goto senderror;
325         }
326         idp->idp_tc++;
327         if (idp->idp_tc > NS_MAXHOPS) {
328                 type = NS_ERR_TOO_OLD, code = 0;
329                 goto senderror;
330         }
331         /*
332          * Save at most 42 bytes of the packet in case
333          * we need to generate an NS error message to the src.
334          */
335         mcopy = m_copy(m, 0, imin((int)ntohs(idp->idp_len), 42));
336
337         if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
338                 type = NS_ERR_UNREACH_HOST, code = 0;
339                 goto senderror;
340         }
341         /*
342          * Here we think about  forwarding  broadcast packets,
343          * so we try to insure that it doesn't go back out
344          * on the interface it came in on.  Also, if we
345          * are going to physically broadcast this, let us
346          * age the packet so we can eat it safely the second time around.
347          */
348         if (idp->idp_dna.x_host.c_host[0] & 0x1) {
349                 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
350                 struct ifnet *ifp;
351                 if (ia) {
352                         /* I'm gonna hafta eat this packet */
353                         agedelta += NS_MAXHOPS - idp->idp_tc;
354                         idp->idp_tc = NS_MAXHOPS;
355                 }
356                 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) {
357                         /* error = ENETUNREACH; He'll never get it! */
358                         m_freem(m);
359                         goto cleanup;
360                 }
361                 if (idp_droute.ro_rt &&
362                     (ifp=idp_droute.ro_rt->rt_ifp) &&
363                     idp_sroute.ro_rt &&
364                     (ifp!=idp_sroute.ro_rt->rt_ifp)) {
365                         flags |= NS_ALLOWBROADCAST;
366                 } else {
367                         type = NS_ERR_UNREACH_HOST, code = 0;
368                         goto senderror;
369                 }
370         }
371         /* need to adjust checksum */
372         if (idp->idp_sum!=0xffff) {
373                 union bytes {
374                         u_char c[4];
375                         u_short s[2];
376                         long l;
377                 } x;
378                 int shift;
379                 x.l = 0; x.c[0] = agedelta;
380                 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
381                 x.l = idp->idp_sum + (x.s[0] << shift);
382                 x.l = x.s[0] + x.s[1];
383                 x.l = x.s[0] + x.s[1];
384                 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
385         }
386         if ((error = ns_output(m, &idp_droute, flags)) &&
387             (mcopy!=NULL)) {
388                 idp = mtod(mcopy, struct idp *);
389                 type = NS_ERR_UNSPEC_T, code = 0;
390                 switch (error) {
391
392                 case ENETUNREACH:
393                 case EHOSTDOWN:
394                 case EHOSTUNREACH:
395                 case ENETDOWN:
396                 case EPERM:
397                         type = NS_ERR_UNREACH_HOST;
398                         break;
399
400                 case EMSGSIZE:
401                         type = NS_ERR_TOO_BIG;
402                         code = 576; /* too hard to figure out mtu here */
403                         break;
404
405                 case ENOBUFS:
406                         type = NS_ERR_UNSPEC_T;
407                         break;
408                 }
409                 mcopy = NULL;
410         senderror:
411                 ns_error(m, type, code);
412         }
413 cleanup:
414         if (ok_there)
415                 idp_undo_route(&idp_droute);
416         if (ok_back)
417                 idp_undo_route(&idp_sroute);
418         if (mcopy != NULL)
419                 m_freem(mcopy);
420 }
421
422 int
423 idp_do_route(src, ro)
424 struct ns_addr *src;
425 struct route *ro;
426 {
427
428         struct sockaddr_ns *dst;
429
430         bzero((caddr_t)ro, sizeof (*ro));
431         dst = (struct sockaddr_ns *)&ro->ro_dst;
432
433         dst->sns_len = sizeof(*dst);
434         dst->sns_family = AF_NS;
435         dst->sns_addr = *src;
436         dst->sns_addr.x_port = 0;
437         rtalloc(ro);
438         if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) {
439                 return (0);
440         }
441         ro->ro_rt->rt_use++;
442         return (1);
443 }
444
445 void
446 idp_undo_route(ro)
447 struct route *ro;
448 {
449         if (ro->ro_rt) {RTFREE(ro->ro_rt);}
450 }
451
452 void
453 ns_watch_output(m, ifp)
454 struct mbuf *m;
455 struct ifnet *ifp;
456 {
457         struct nspcb *nsp;
458         struct ifaddr *ifa;
459         /*
460          * Give any raw listeners a crack at the packet
461          */
462         for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
463                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
464                 if (m0) {
465                         struct idp *idp;
466
467                         M_PREPEND(m0, sizeof (*idp), M_DONTWAIT);
468                         if (m0 == NULL)
469                                 continue;
470                         idp = mtod(m0, struct idp *);
471                         idp->idp_sna.x_net = ns_zeronet;
472                         idp->idp_sna.x_host = ns_thishost;
473                         if (ifp && (ifp->if_flags & IFF_POINTOPOINT))
474                                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
475                                 if (ifa->ifa_addr->sa_family==AF_NS) {
476                                     idp->idp_sna = IA_SNS(ifa)->sns_addr;
477                                     break;
478                                 }
479                         idp->idp_len = ntohl(m0->m_pkthdr.len);
480                         idp_input(m0, nsp);
481                 }
482         }
483 }