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