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 $
10 #include <sys/kernel.h>
11 #include <net/netisr.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
16 #include <net/route.h>
17 #include <net/intrq.h>
19 #include <netatalk/at.h>
20 #include <netatalk/at_var.h>
21 #include <netatalk/ddp.h>
22 #include <netatalk/ddp_var.h>
23 #include <netatalk/at_extern.h>
25 static volatile int ddp_forward = 1;
26 static volatile int ddp_firewall = 0;
27 static struct ddpstat ddpstat;
28 static struct route forwro;
30 const int atintrq1_present = 1, atintrq2_present = 1;
32 static void ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
35 * Could probably merge these two code segments a little better...
40 struct elaphdr *elhp, elh;
46 * First pull off all the phase 2 packets.
51 IF_DEQUEUE( &atintrq2, m );
55 if ( m == 0 ) { /* no more queued packets */
59 ifp = m->m_pkthdr.rcvif;
60 ddp_input( m, ifp, (struct elaphdr *)NULL, 2 );
64 * Then pull off all the phase 1 packets.
69 IF_DEQUEUE( &atintrq1, m );
73 if ( m == 0 ) { /* no more queued packets */
77 ifp = m->m_pkthdr.rcvif;
79 if ( m->m_len < SZ_ELAPHDR &&
80 (( m = m_pullup( m, SZ_ELAPHDR )) == 0 )) {
81 ddpstat.ddps_tooshort++;
86 * this seems a little dubios, but I don't know phase 1 so leave it.
88 elhp = mtod( m, struct elaphdr *);
89 m_adj( m, SZ_ELAPHDR );
91 if ( elhp->el_type == ELAP_DDPEXTEND ) {
92 ddp_input( m, ifp, (struct elaphdr *)NULL, 1 );
94 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR );
95 ddp_input( m, ifp, &elh, 1 );
102 netisr_atalk_setup(void *dummy __unused)
105 register_netisr(NETISR_ATALK, atintr);
107 SYSINIT(atalk_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_atalk_setup, NULL);
110 ddp_input( m, ifp, elh, phase )
116 struct sockaddr_at from, to;
117 struct ddpshdr *dsh, ddps;
118 struct at_ifaddr *aa;
119 struct ddpehdr *deh = NULL, ddpe;
124 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
125 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
128 * Extract the information in the short header.
129 * netowrk information is defaulted to ATADDR_ANYNET
130 * and node information comes from the elh info.
131 * We must be phase 1.
133 ddpstat.ddps_short++;
135 if ( m->m_len < sizeof( struct ddpshdr ) &&
136 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
137 ddpstat.ddps_tooshort++;
141 dsh = mtod( m, struct ddpshdr *);
142 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
143 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
146 to.sat_addr.s_net = ATADDR_ANYNET;
147 to.sat_addr.s_node = elh->el_dnode;
148 to.sat_port = ddps.dsh_dport;
149 from.sat_addr.s_net = ATADDR_ANYNET;
150 from.sat_addr.s_node = elh->el_snode;
151 from.sat_port = ddps.dsh_sport;
154 * Make sure that we point to the phase1 ifaddr info
155 * and that it's valid for this packet.
157 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
158 if ( (aa->aa_ifp == ifp)
159 && ( (aa->aa_flags & AFA_PHASE2) == 0)
160 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
161 || (to.sat_addr.s_node == ATADDR_BCAST))) {
166 * maybe we got a broadcast not meant for us.. ditch it.
174 * There was no 'elh' passed on. This could still be
175 * either phase1 or phase2.
176 * We have a long header, but we may be running on a phase 1 net.
177 * Extract out all the info regarding this packet's src & dst.
181 if ( m->m_len < sizeof( struct ddpehdr ) &&
182 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
183 ddpstat.ddps_tooshort++;
187 deh = mtod( m, struct ddpehdr *);
188 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
189 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
192 if (( cksum = ddpe.deh_sum ) == 0 ) {
193 ddpstat.ddps_nosum++;
196 from.sat_addr.s_net = ddpe.deh_snet;
197 from.sat_addr.s_node = ddpe.deh_snode;
198 from.sat_port = ddpe.deh_sport;
199 to.sat_addr.s_net = ddpe.deh_dnet;
200 to.sat_addr.s_node = ddpe.deh_dnode;
201 to.sat_port = ddpe.deh_dport;
203 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
205 * The TO address doesn't specify a net,
206 * So by definition it's for this net.
207 * Try find ifaddr info with the right phase,
208 * the right interface, and either to our node, a broadcast,
209 * or looped back (though that SHOULD be covered in the other
212 * XXX If we have multiple interfaces, then the first with
213 * this node number will match (which may NOT be what we want,
214 * but it's probably safe in 99.999% of cases.
216 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
217 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
220 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
223 if ( (aa->aa_ifp == ifp)
224 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
225 || (to.sat_addr.s_node == ATADDR_BCAST)
226 || (ifp->if_flags & IFF_LOOPBACK))) {
232 * A destination network was given. We just try to find
233 * which ifaddr info matches it.
235 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
237 * This is a kludge. Accept packets that are
238 * for any router on a local netrange.
240 if ( to.sat_addr.s_net == aa->aa_firstnet &&
241 to.sat_addr.s_node == 0 ) {
245 * Don't use ifaddr info for which we are totally outside the
246 * netrange, and it's not a startup packet.
247 * Startup packets are always implicitly allowed on to
250 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
251 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
252 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
253 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
258 * Don't record a match either if we just don't have a match
259 * in the node address. This can have if the interface
260 * is in promiscuous mode for example.
262 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
263 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
272 * Adjust the length, removing any padding that may have been added
273 * at a link layer. We do this before we attempt to forward a packet,
274 * possibly on a different media.
276 mlen = m->m_pkthdr.len;
278 ddpstat.ddps_toosmall++;
283 m_adj( m, dlen - mlen );
287 * If it aint for a net on any of our interfaces,
288 * or it IS for a net on a different interface than it came in on,
289 * (and it is not looped back) then consider if we should forward it.
290 * As we are not really a router this is a bit cheeky, but it may be
294 || ( (to.sat_addr.s_node == ATADDR_BCAST)
295 && (aa->aa_ifp != ifp)
296 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
298 * If we've explicitly disabled it, don't route anything
300 if ( ddp_forward == 0 ) {
305 * If the cached forwarding route is still valid, use it.
308 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
309 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
310 RTFREE( forwro.ro_rt );
311 forwro.ro_rt = (struct rtentry *)0;
315 * If we don't have a cached one (any more) or it's useless,
316 * Then get a new route.
317 * XXX this could cause a 'route leak'. check this!
319 if ( forwro.ro_rt == (struct rtentry *)0
320 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
321 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
322 forwro.ro_dst.sa_family = AF_APPLETALK;
323 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
324 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
329 * If it's not going to get there on this hop, and it's
330 * already done too many hops, then throw it away.
332 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
333 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
339 * A ddp router might use the same interface
340 * to forward the packet, which this would not effect.
341 * Don't allow packets to cross from one interface to another however.
344 && ( (forwro.ro_rt == NULL)
345 || (forwro.ro_rt->rt_ifp != ifp))) {
352 * If it was a short header then it would have not gotten here,
353 * so we can assume there is room to drop the header in.
354 * XXX what about promiscuous mode, etc...
357 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
358 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
359 if ( ddp_route( m, &forwro )) {
360 ddpstat.ddps_cantforward++;
362 ddpstat.ddps_forward++;
368 * It was for us, and we have an ifaddr to use with it.
370 from.sat_len = sizeof( struct sockaddr_at );
371 from.sat_family = AF_APPLETALK;
374 * We are no longer interested in the link layer.
378 m_adj( m, sizeof( struct ddpshdr ));
380 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
381 ddpstat.ddps_badsum++;
385 m_adj( m, sizeof( struct ddpehdr ));
389 * Search for ddp protocol control blocks that match these
392 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
398 * If we found one, deliver th epacket to the socket
400 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
401 m, (struct mbuf *)0 ) == 0 ) {
403 * If the socket is full (or similar error) dump the packet.
405 ddpstat.ddps_nosockspace++;
410 * And wake up whatever might be waiting for it
412 sorwakeup( ddp->ddp_socket );
416 /* As if we haven't got enough of this sort of think floating
417 around the kernel :) */
422 char hexdig[] = "0123456789ABCDEF";
425 bprint( char *data, int len )
427 char xout[ BPXLEN ], aout[ BPALEN ];
430 bzero( xout, BPXLEN );
431 bzero( aout, BPALEN );
436 printf( "%s\t%s\n", xout, aout );
438 printf( "%s\n", "(end)" );
442 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
443 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
445 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
451 xout[ (i*3) + 2 ] = ' ';
457 if ( i > BPALEN - 2 ) {
458 printf( "%s\t%s\n", xout, aout );
459 bzero( xout, BPXLEN );
460 bzero( aout, BPALEN );
468 m_printm( struct mbuf *m )
470 for (; m; m = m->m_next ) {
471 bprint( mtod( m, char * ), m->m_len );