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