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