Leftover netisr consolidation cleanups.
[dragonfly.git] / sys / netproto / atalk / ddp_input.c
1 /*
2  * Copyright (c) 1990,1994 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  *
5  * $FreeBSD: src/sys/netatalk/ddp_input.c,v 1.12 2000/02/13 03:31:58 peter Exp $
6  * $DragonFly: src/sys/netproto/atalk/ddp_input.c,v 1.5 2003/09/16 05:03:13 hsu Exp $
7  */
8
9 #include <sys/param.h>
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
12 #include <sys/mbuf.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
15 #include <net/if.h>
16 #include <net/route.h>
17 #include <net/intrq.h>
18
19 #include "at.h"
20 #include "at_var.h"
21 #include "ddp.h"
22 #include "ddp_var.h"
23 #include "at_extern.h"
24
25 static volatile int     ddp_forward = 1;
26 static volatile int     ddp_firewall = 0;
27 static struct ddpstat   ddpstat;
28 static struct route     forwro;
29
30 const int atintrq1_present = 1, atintrq2_present = 1;
31
32 static void     ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
33
34 /*
35  * Could probably merge these two code segments a little better...
36  */
37 void
38 at2intr(struct mbuf *m)
39 {
40
41         /*
42          * Phase 2 packet handling 
43          */
44         ddp_input(m, m->m_pkthdr.rcvif, NULL, 2);
45         return;
46 }
47
48 void
49 at1intr(struct mbuf *m)
50 {
51         struct elaphdr *elhp, elh;
52
53         /*
54          * Phase 1 packet handling 
55          */
56         if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
57                 ddpstat.ddps_tooshort++;
58                 return;
59         }
60
61         /*
62          * This seems a little dubious, but I don't know phase 1 so leave it.
63          */
64         elhp = mtod(m, struct elaphdr *);
65         m_adj(m, SZ_ELAPHDR);
66
67         if (elhp->el_type == ELAP_DDPEXTEND) {
68                 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1);
69         } else {
70                 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR);
71                 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1);
72         }
73         return;
74 }
75
76 static void
77 ddp_input( m, ifp, elh, phase )
78     struct mbuf         *m;
79     struct ifnet        *ifp;
80     struct elaphdr      *elh;
81     int                 phase;
82 {
83     struct sockaddr_at  from, to;
84     struct ddpshdr      *dsh, ddps;
85     struct at_ifaddr    *aa;
86     struct ddpehdr      *deh = NULL, ddpe;
87     struct ddpcb        *ddp;
88     int                 dlen, mlen;
89     u_short             cksum = 0;
90
91     bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
92     bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
93     if ( elh ) {
94         /*
95          * Extract the information in the short header.
96          * netowrk information is defaulted to ATADDR_ANYNET
97          * and node information comes from the elh info.
98          * We must be phase 1.
99          */
100         ddpstat.ddps_short++;
101
102         if ( m->m_len < sizeof( struct ddpshdr ) &&
103                 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
104             ddpstat.ddps_tooshort++;
105             return;
106         }
107
108         dsh = mtod( m, struct ddpshdr *);
109         bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
110         ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
111         dlen = ddps.dsh_len;
112
113         to.sat_addr.s_net = ATADDR_ANYNET;
114         to.sat_addr.s_node = elh->el_dnode;
115         to.sat_port = ddps.dsh_dport;
116         from.sat_addr.s_net = ATADDR_ANYNET;
117         from.sat_addr.s_node = elh->el_snode;
118         from.sat_port = ddps.dsh_sport;
119
120         /* 
121          * Make sure that we point to the phase1 ifaddr info 
122          * and that it's valid for this packet.
123          */
124         for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
125             if ( (aa->aa_ifp == ifp)
126             && ( (aa->aa_flags & AFA_PHASE2) == 0)
127             && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
128               || (to.sat_addr.s_node == ATADDR_BCAST))) {
129                 break;
130             }
131         }
132         /* 
133          * maybe we got a broadcast not meant for us.. ditch it.
134          */
135         if ( aa == NULL ) {
136             m_freem( m );
137             return;
138         }
139     } else {
140         /*
141          * There was no 'elh' passed on. This could still be
142          * either phase1 or phase2.
143          * We have a long header, but we may be running on a phase 1 net.
144          * Extract out all the info regarding this packet's src & dst.
145          */
146         ddpstat.ddps_long++;
147
148         if ( m->m_len < sizeof( struct ddpehdr ) &&
149                 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
150             ddpstat.ddps_tooshort++;
151             return;
152         }
153
154         deh = mtod( m, struct ddpehdr *);
155         bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
156         ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
157         dlen = ddpe.deh_len;
158
159         if (( cksum = ddpe.deh_sum ) == 0 ) {
160             ddpstat.ddps_nosum++;
161         }
162
163         from.sat_addr.s_net = ddpe.deh_snet;
164         from.sat_addr.s_node = ddpe.deh_snode;
165         from.sat_port = ddpe.deh_sport;
166         to.sat_addr.s_net = ddpe.deh_dnet;
167         to.sat_addr.s_node = ddpe.deh_dnode;
168         to.sat_port = ddpe.deh_dport;
169
170         if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
171             /*
172              * The TO address doesn't specify a net,
173              * So by definition it's for this net.
174              * Try find ifaddr info with the right phase, 
175              * the right interface, and either to our node, a broadcast,
176              * or looped back (though that SHOULD be covered in the other
177              * cases).
178              *
179              * XXX If we have multiple interfaces, then the first with
180              * this node number will match (which may NOT be what we want,
181              * but it's probably safe in 99.999% of cases.
182              */
183             for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
184                 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
185                     continue;
186                 }
187                 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
188                     continue;
189                 }
190                 if ( (aa->aa_ifp == ifp)
191                 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
192                   || (to.sat_addr.s_node == ATADDR_BCAST)
193                   || (ifp->if_flags & IFF_LOOPBACK))) {
194                     break;
195                 }
196             }
197         } else {
198             /* 
199              * A destination network was given. We just try to find 
200              * which ifaddr info matches it.
201              */
202             for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
203                 /*
204                  * This is a kludge. Accept packets that are
205                  * for any router on a local netrange.
206                  */
207                 if ( to.sat_addr.s_net == aa->aa_firstnet &&
208                         to.sat_addr.s_node == 0 ) {
209                     break;
210                 }
211                 /*
212                  * Don't use ifaddr info for which we are totally outside the
213                  * netrange, and it's not a startup packet.
214                  * Startup packets are always implicitly allowed on to
215                  * the next test.
216                  */
217                 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
218                     || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
219                  && (( ntohs( to.sat_addr.s_net ) < 0xff00)
220                     || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
221                     continue;
222                 }
223
224                 /*
225                  * Don't record a match either if we just don't have a match
226                  * in the node address. This can have if the interface
227                  * is in promiscuous mode for example.
228                  */
229                 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
230                 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
231                     continue;
232                 }
233                 break;
234             }
235         }
236     }
237
238     /*
239      * Adjust the length, removing any padding that may have been added
240      * at a link layer.  We do this before we attempt to forward a packet,
241      * possibly on a different media.
242      */
243     mlen = m->m_pkthdr.len;
244     if ( mlen < dlen ) {
245         ddpstat.ddps_toosmall++;
246         m_freem( m );
247         return;
248     }
249     if ( mlen > dlen ) {
250         m_adj( m, dlen - mlen );
251     }
252
253     /*
254      * If it aint for a net on any of our interfaces,
255      * or it IS for a net on a different interface than it came in on,
256      * (and it is not looped back) then consider if we should forward it.
257      * As we are not really a router this is a bit cheeky, but it may be
258      * useful some day.
259      */
260     if ( (aa == NULL)
261     || ( (to.sat_addr.s_node == ATADDR_BCAST)
262       && (aa->aa_ifp != ifp)
263       && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
264         /* 
265          * If we've explicitly disabled it, don't route anything
266          */
267         if ( ddp_forward == 0 ) {
268             m_freem( m );
269             return;
270         }
271         /* 
272          * If the cached forwarding route is still valid, use it.
273          */
274         if ( forwro.ro_rt
275         && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
276           || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
277             RTFREE( forwro.ro_rt );
278             forwro.ro_rt = (struct rtentry *)0;
279         }
280
281         /*
282          * If we don't have a cached one (any more) or it's useless,
283          * Then get a new route.
284          * XXX this could cause a 'route leak'. check this!
285          */
286         if ( forwro.ro_rt == (struct rtentry *)0
287         || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
288             forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
289             forwro.ro_dst.sa_family = AF_APPLETALK;
290             satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
291             satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
292             rtalloc(&forwro);
293         }
294
295         /* 
296          * If it's not going to get there on this hop, and it's
297          * already done too many hops, then throw it away.
298          */
299         if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
300         && (ddpe.deh_hops == DDP_MAXHOPS) ) {
301             m_freem( m );
302             return;
303         }
304
305         /*
306          * A ddp router might use the same interface
307          * to forward the packet, which this would not effect.
308          * Don't allow packets to cross from one interface to another however.
309          */
310         if ( ddp_firewall
311         && ( (forwro.ro_rt == NULL)
312           || (forwro.ro_rt->rt_ifp != ifp))) {
313             m_freem( m );
314             return;
315         }
316
317         /*
318          * Adjust the header.
319          * If it was a short header then it would have not gotten here,
320          * so we can assume there is room to drop the header in.
321          * XXX what about promiscuous mode, etc...
322          */
323         ddpe.deh_hops++;
324         ddpe.deh_bytes = htonl( ddpe.deh_bytes );
325         bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
326         if ( ddp_route( m, &forwro )) {
327             ddpstat.ddps_cantforward++;
328         } else {
329             ddpstat.ddps_forward++;
330         }
331         return;
332     }
333
334     /*
335      * It was for us, and we have an ifaddr to use with it.
336      */
337     from.sat_len = sizeof( struct sockaddr_at );
338     from.sat_family = AF_APPLETALK;
339
340     /* 
341      * We are no longer interested in the link layer.
342      * so cut it off.
343      */
344     if ( elh ) {
345         m_adj( m, sizeof( struct ddpshdr ));
346     } else {
347         if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
348             ddpstat.ddps_badsum++;
349             m_freem( m );
350             return;
351         }
352         m_adj( m, sizeof( struct ddpehdr ));
353     }
354
355     /* 
356      * Search for ddp protocol control blocks that match these
357      * addresses. 
358      */
359     if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
360         m_freem( m );
361         return;
362     }
363
364     /* 
365      * If we found one, deliver th epacket to the socket
366      */
367     if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
368             m, (struct mbuf *)0 ) == 0 ) {
369         /* 
370          * If the socket is full (or similar error) dump the packet.
371          */
372         ddpstat.ddps_nosockspace++;
373         m_freem( m );
374         return;
375     }
376     /*
377      * And wake up whatever might be waiting for it
378      */
379     sorwakeup( ddp->ddp_socket );
380 }
381
382 #if 0
383 /* As if we haven't got enough of this sort of think floating
384 around the kernel :) */
385
386 #define BPXLEN  48
387 #define BPALEN  16
388 #include <ctype.h>
389 char    hexdig[] = "0123456789ABCDEF";
390
391 static void
392 bprint( char *data, int len )
393 {
394     char        xout[ BPXLEN ], aout[ BPALEN ];
395     int         i = 0;
396
397     bzero( xout, BPXLEN );
398     bzero( aout, BPALEN );
399
400     for ( ;; ) {
401         if ( len < 1 ) {
402             if ( i != 0 ) {
403                 printf( "%s\t%s\n", xout, aout );
404             }
405             printf( "%s\n", "(end)" );
406             break;
407         }
408
409         xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
410         xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
411
412         if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
413             aout[ i ] = *data;
414         } else {
415             aout[ i ] = '.';
416         }
417
418         xout[ (i*3) + 2 ] = ' ';
419
420         i++;
421         len--;
422         data++;
423
424         if ( i > BPALEN - 2 ) {
425             printf( "%s\t%s\n", xout, aout );
426             bzero( xout, BPXLEN );
427             bzero( aout, BPALEN );
428             i = 0;
429             continue;
430         }
431     }
432 }
433
434 static void
435 m_printm( struct mbuf *m )
436 {
437     for (; m; m = m->m_next ) {
438         bprint( mtod( m, char * ), m->m_len );
439     }
440 }
441 #endif