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.9 2004/04/21 18:13:59 dillon Exp $
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
16 #include <sys/thread2.h>
17 #include <sys/msgport2.h>
20 #include <net/netisr.h>
21 #include <net/route.h>
22 #include <net/intrq.h>
28 #include "at_extern.h"
30 static volatile int ddp_forward = 1;
31 static volatile int ddp_firewall = 0;
32 static struct ddpstat ddpstat;
33 static struct route forwro;
35 const int atintrq1_present = 1, atintrq2_present = 1;
37 static void ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
40 * Could probably merge these two code segments a little better...
43 at2intr(struct netmsg *msg)
45 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
48 * Phase 2 packet handling
50 ddp_input(m, m->m_pkthdr.rcvif, NULL, 2);
51 lwkt_replymsg(&msg->nm_lmsg, 0);
56 at1intr(struct netmsg *msg)
58 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
59 struct elaphdr *elhp, elh;
62 * Phase 1 packet handling
64 if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
65 ddpstat.ddps_tooshort++;
70 * This seems a little dubious, but I don't know phase 1 so leave it.
72 elhp = mtod(m, struct elaphdr *);
75 if (elhp->el_type == ELAP_DDPEXTEND) {
76 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1);
78 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR);
79 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1);
82 lwkt_replymsg(&msg->nm_lmsg, 0);
87 ddp_input( m, ifp, elh, phase )
93 struct sockaddr_at from, to;
94 struct ddpshdr *dsh, ddps;
96 struct ddpehdr *deh = NULL, ddpe;
101 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
102 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
105 * Extract the information in the short header.
106 * netowrk information is defaulted to ATADDR_ANYNET
107 * and node information comes from the elh info.
108 * We must be phase 1.
110 ddpstat.ddps_short++;
112 if ( m->m_len < sizeof( struct ddpshdr ) &&
113 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
114 ddpstat.ddps_tooshort++;
118 dsh = mtod( m, struct ddpshdr *);
119 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
120 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
123 to.sat_addr.s_net = ATADDR_ANYNET;
124 to.sat_addr.s_node = elh->el_dnode;
125 to.sat_port = ddps.dsh_dport;
126 from.sat_addr.s_net = ATADDR_ANYNET;
127 from.sat_addr.s_node = elh->el_snode;
128 from.sat_port = ddps.dsh_sport;
131 * Make sure that we point to the phase1 ifaddr info
132 * and that it's valid for this packet.
134 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
135 if ( (aa->aa_ifp == ifp)
136 && ( (aa->aa_flags & AFA_PHASE2) == 0)
137 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
138 || (to.sat_addr.s_node == ATADDR_BCAST))) {
143 * maybe we got a broadcast not meant for us.. ditch it.
151 * There was no 'elh' passed on. This could still be
152 * either phase1 or phase2.
153 * We have a long header, but we may be running on a phase 1 net.
154 * Extract out all the info regarding this packet's src & dst.
158 if ( m->m_len < sizeof( struct ddpehdr ) &&
159 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
160 ddpstat.ddps_tooshort++;
164 deh = mtod( m, struct ddpehdr *);
165 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
166 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
169 if (( cksum = ddpe.deh_sum ) == 0 ) {
170 ddpstat.ddps_nosum++;
173 from.sat_addr.s_net = ddpe.deh_snet;
174 from.sat_addr.s_node = ddpe.deh_snode;
175 from.sat_port = ddpe.deh_sport;
176 to.sat_addr.s_net = ddpe.deh_dnet;
177 to.sat_addr.s_node = ddpe.deh_dnode;
178 to.sat_port = ddpe.deh_dport;
180 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
182 * The TO address doesn't specify a net,
183 * So by definition it's for this net.
184 * Try find ifaddr info with the right phase,
185 * the right interface, and either to our node, a broadcast,
186 * or looped back (though that SHOULD be covered in the other
189 * XXX If we have multiple interfaces, then the first with
190 * this node number will match (which may NOT be what we want,
191 * but it's probably safe in 99.999% of cases.
193 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
194 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
197 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
200 if ( (aa->aa_ifp == ifp)
201 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
202 || (to.sat_addr.s_node == ATADDR_BCAST)
203 || (ifp->if_flags & IFF_LOOPBACK))) {
209 * A destination network was given. We just try to find
210 * which ifaddr info matches it.
212 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
214 * This is a kludge. Accept packets that are
215 * for any router on a local netrange.
217 if ( to.sat_addr.s_net == aa->aa_firstnet &&
218 to.sat_addr.s_node == 0 ) {
222 * Don't use ifaddr info for which we are totally outside the
223 * netrange, and it's not a startup packet.
224 * Startup packets are always implicitly allowed on to
227 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
228 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
229 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
230 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
235 * Don't record a match either if we just don't have a match
236 * in the node address. This can have if the interface
237 * is in promiscuous mode for example.
239 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
240 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
249 * Adjust the length, removing any padding that may have been added
250 * at a link layer. We do this before we attempt to forward a packet,
251 * possibly on a different media.
253 mlen = m->m_pkthdr.len;
255 ddpstat.ddps_toosmall++;
260 m_adj( m, dlen - mlen );
264 * If it aint for a net on any of our interfaces,
265 * or it IS for a net on a different interface than it came in on,
266 * (and it is not looped back) then consider if we should forward it.
267 * As we are not really a router this is a bit cheeky, but it may be
271 || ( (to.sat_addr.s_node == ATADDR_BCAST)
272 && (aa->aa_ifp != ifp)
273 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
275 * If we've explicitly disabled it, don't route anything
277 if ( ddp_forward == 0 ) {
282 * If the cached forwarding route is still valid, use it.
285 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
286 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
287 RTFREE( forwro.ro_rt );
288 forwro.ro_rt = (struct rtentry *)0;
292 * If we don't have a cached one (any more) or it's useless,
293 * Then get a new route.
294 * XXX this could cause a 'route leak'. check this!
296 if ( forwro.ro_rt == (struct rtentry *)0
297 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
298 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
299 forwro.ro_dst.sa_family = AF_APPLETALK;
300 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
301 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
306 * If it's not going to get there on this hop, and it's
307 * already done too many hops, then throw it away.
309 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
310 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
316 * A ddp router might use the same interface
317 * to forward the packet, which this would not effect.
318 * Don't allow packets to cross from one interface to another however.
321 && ( (forwro.ro_rt == NULL)
322 || (forwro.ro_rt->rt_ifp != ifp))) {
329 * If it was a short header then it would have not gotten here,
330 * so we can assume there is room to drop the header in.
331 * XXX what about promiscuous mode, etc...
334 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
335 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
336 if ( ddp_route( m, &forwro )) {
337 ddpstat.ddps_cantforward++;
339 ddpstat.ddps_forward++;
345 * It was for us, and we have an ifaddr to use with it.
347 from.sat_len = sizeof( struct sockaddr_at );
348 from.sat_family = AF_APPLETALK;
351 * We are no longer interested in the link layer.
355 m_adj( m, sizeof( struct ddpshdr ));
357 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
358 ddpstat.ddps_badsum++;
362 m_adj( m, sizeof( struct ddpehdr ));
366 * Search for ddp protocol control blocks that match these
369 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
375 * If we found one, deliver th epacket to the socket
377 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
378 m, (struct mbuf *)0 ) == 0 ) {
380 * If the socket is full (or similar error) dump the packet.
382 ddpstat.ddps_nosockspace++;
387 * And wake up whatever might be waiting for it
389 sorwakeup( ddp->ddp_socket );
393 /* As if we haven't got enough of this sort of think floating
394 around the kernel :) */
399 char hexdig[] = "0123456789ABCDEF";
402 bprint( char *data, int len )
404 char xout[ BPXLEN ], aout[ BPALEN ];
407 bzero( xout, BPXLEN );
408 bzero( aout, BPALEN );
413 printf( "%s\t%s\n", xout, aout );
415 printf( "%s\n", "(end)" );
419 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
420 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
422 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
428 xout[ (i*3) + 2 ] = ' ';
434 if ( i > BPALEN - 2 ) {
435 printf( "%s\t%s\n", xout, aout );
436 bzero( xout, BPXLEN );
437 bzero( aout, BPALEN );
445 m_printm( struct mbuf *m )
447 for (; m; m = m->m_next ) {
448 bprint( mtod( m, char * ), m->m_len );