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