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