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