2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
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.3 2003/08/07 21:17:33 dillon Exp $
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
12 #include <net/netisr.h>
14 #include <sys/socket.h>
15 #include <sys/socketvar.h>
17 #include <net/route.h>
18 #include <net/intrq.h>
24 #include "at_extern.h"
26 static volatile int ddp_forward = 1;
27 static volatile int ddp_firewall = 0;
28 static struct ddpstat ddpstat;
29 static struct route forwro;
31 const int atintrq1_present = 1, atintrq2_present = 1;
33 static void ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
36 * Could probably merge these two code segments a little better...
41 struct elaphdr *elhp, elh;
47 * First pull off all the phase 2 packets.
52 IF_DEQUEUE( &atintrq2, m );
56 if ( m == 0 ) { /* no more queued packets */
60 ifp = m->m_pkthdr.rcvif;
61 ddp_input( m, ifp, (struct elaphdr *)NULL, 2 );
65 * Then pull off all the phase 1 packets.
70 IF_DEQUEUE( &atintrq1, m );
74 if ( m == 0 ) { /* no more queued packets */
78 ifp = m->m_pkthdr.rcvif;
80 if ( m->m_len < SZ_ELAPHDR &&
81 (( m = m_pullup( m, SZ_ELAPHDR )) == 0 )) {
82 ddpstat.ddps_tooshort++;
87 * this seems a little dubios, but I don't know phase 1 so leave it.
89 elhp = mtod( m, struct elaphdr *);
90 m_adj( m, SZ_ELAPHDR );
92 if ( elhp->el_type == ELAP_DDPEXTEND ) {
93 ddp_input( m, ifp, (struct elaphdr *)NULL, 1 );
95 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR );
96 ddp_input( m, ifp, &elh, 1 );
103 netisr_atalk_setup(void *dummy __unused)
106 register_netisr(NETISR_ATALK, atintr);
108 SYSINIT(atalk_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_atalk_setup, NULL);
111 ddp_input( m, ifp, elh, phase )
117 struct sockaddr_at from, to;
118 struct ddpshdr *dsh, ddps;
119 struct at_ifaddr *aa;
120 struct ddpehdr *deh = NULL, ddpe;
125 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
126 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
129 * Extract the information in the short header.
130 * netowrk information is defaulted to ATADDR_ANYNET
131 * and node information comes from the elh info.
132 * We must be phase 1.
134 ddpstat.ddps_short++;
136 if ( m->m_len < sizeof( struct ddpshdr ) &&
137 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
138 ddpstat.ddps_tooshort++;
142 dsh = mtod( m, struct ddpshdr *);
143 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
144 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
147 to.sat_addr.s_net = ATADDR_ANYNET;
148 to.sat_addr.s_node = elh->el_dnode;
149 to.sat_port = ddps.dsh_dport;
150 from.sat_addr.s_net = ATADDR_ANYNET;
151 from.sat_addr.s_node = elh->el_snode;
152 from.sat_port = ddps.dsh_sport;
155 * Make sure that we point to the phase1 ifaddr info
156 * and that it's valid for this packet.
158 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
159 if ( (aa->aa_ifp == ifp)
160 && ( (aa->aa_flags & AFA_PHASE2) == 0)
161 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
162 || (to.sat_addr.s_node == ATADDR_BCAST))) {
167 * maybe we got a broadcast not meant for us.. ditch it.
175 * There was no 'elh' passed on. This could still be
176 * either phase1 or phase2.
177 * We have a long header, but we may be running on a phase 1 net.
178 * Extract out all the info regarding this packet's src & dst.
182 if ( m->m_len < sizeof( struct ddpehdr ) &&
183 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
184 ddpstat.ddps_tooshort++;
188 deh = mtod( m, struct ddpehdr *);
189 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
190 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
193 if (( cksum = ddpe.deh_sum ) == 0 ) {
194 ddpstat.ddps_nosum++;
197 from.sat_addr.s_net = ddpe.deh_snet;
198 from.sat_addr.s_node = ddpe.deh_snode;
199 from.sat_port = ddpe.deh_sport;
200 to.sat_addr.s_net = ddpe.deh_dnet;
201 to.sat_addr.s_node = ddpe.deh_dnode;
202 to.sat_port = ddpe.deh_dport;
204 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
206 * The TO address doesn't specify a net,
207 * So by definition it's for this net.
208 * Try find ifaddr info with the right phase,
209 * the right interface, and either to our node, a broadcast,
210 * or looped back (though that SHOULD be covered in the other
213 * XXX If we have multiple interfaces, then the first with
214 * this node number will match (which may NOT be what we want,
215 * but it's probably safe in 99.999% of cases.
217 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
218 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
221 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
224 if ( (aa->aa_ifp == ifp)
225 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
226 || (to.sat_addr.s_node == ATADDR_BCAST)
227 || (ifp->if_flags & IFF_LOOPBACK))) {
233 * A destination network was given. We just try to find
234 * which ifaddr info matches it.
236 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
238 * This is a kludge. Accept packets that are
239 * for any router on a local netrange.
241 if ( to.sat_addr.s_net == aa->aa_firstnet &&
242 to.sat_addr.s_node == 0 ) {
246 * Don't use ifaddr info for which we are totally outside the
247 * netrange, and it's not a startup packet.
248 * Startup packets are always implicitly allowed on to
251 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
252 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
253 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
254 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
259 * Don't record a match either if we just don't have a match
260 * in the node address. This can have if the interface
261 * is in promiscuous mode for example.
263 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
264 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
273 * Adjust the length, removing any padding that may have been added
274 * at a link layer. We do this before we attempt to forward a packet,
275 * possibly on a different media.
277 mlen = m->m_pkthdr.len;
279 ddpstat.ddps_toosmall++;
284 m_adj( m, dlen - mlen );
288 * If it aint for a net on any of our interfaces,
289 * or it IS for a net on a different interface than it came in on,
290 * (and it is not looped back) then consider if we should forward it.
291 * As we are not really a router this is a bit cheeky, but it may be
295 || ( (to.sat_addr.s_node == ATADDR_BCAST)
296 && (aa->aa_ifp != ifp)
297 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
299 * If we've explicitly disabled it, don't route anything
301 if ( ddp_forward == 0 ) {
306 * If the cached forwarding route is still valid, use it.
309 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
310 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
311 RTFREE( forwro.ro_rt );
312 forwro.ro_rt = (struct rtentry *)0;
316 * If we don't have a cached one (any more) or it's useless,
317 * Then get a new route.
318 * XXX this could cause a 'route leak'. check this!
320 if ( forwro.ro_rt == (struct rtentry *)0
321 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
322 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
323 forwro.ro_dst.sa_family = AF_APPLETALK;
324 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
325 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
330 * If it's not going to get there on this hop, and it's
331 * already done too many hops, then throw it away.
333 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
334 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
340 * A ddp router might use the same interface
341 * to forward the packet, which this would not effect.
342 * Don't allow packets to cross from one interface to another however.
345 && ( (forwro.ro_rt == NULL)
346 || (forwro.ro_rt->rt_ifp != ifp))) {
353 * If it was a short header then it would have not gotten here,
354 * so we can assume there is room to drop the header in.
355 * XXX what about promiscuous mode, etc...
358 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
359 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
360 if ( ddp_route( m, &forwro )) {
361 ddpstat.ddps_cantforward++;
363 ddpstat.ddps_forward++;
369 * It was for us, and we have an ifaddr to use with it.
371 from.sat_len = sizeof( struct sockaddr_at );
372 from.sat_family = AF_APPLETALK;
375 * We are no longer interested in the link layer.
379 m_adj( m, sizeof( struct ddpshdr ));
381 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
382 ddpstat.ddps_badsum++;
386 m_adj( m, sizeof( struct ddpehdr ));
390 * Search for ddp protocol control blocks that match these
393 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
399 * If we found one, deliver th epacket to the socket
401 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
402 m, (struct mbuf *)0 ) == 0 ) {
404 * If the socket is full (or similar error) dump the packet.
406 ddpstat.ddps_nosockspace++;
411 * And wake up whatever might be waiting for it
413 sorwakeup( ddp->ddp_socket );
417 /* As if we haven't got enough of this sort of think floating
418 around the kernel :) */
423 char hexdig[] = "0123456789ABCDEF";
426 bprint( char *data, int len )
428 char xout[ BPXLEN ], aout[ BPALEN ];
431 bzero( xout, BPXLEN );
432 bzero( aout, BPALEN );
437 printf( "%s\t%s\n", xout, aout );
439 printf( "%s\n", "(end)" );
443 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
444 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
446 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
452 xout[ (i*3) + 2 ] = ' ';
458 if ( i > BPALEN - 2 ) {
459 printf( "%s\t%s\n", xout, aout );
460 bzero( xout, BPXLEN );
461 bzero( aout, BPALEN );
469 m_printm( struct mbuf *m )
471 for (; m; m = m->m_next ) {
472 bprint( mtod( m, char * ), m->m_len );