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.6 2004/03/06 01:58:56 hsu Exp $
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
16 #include <net/netisr.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...
39 at2intr(struct netmsg *msg)
41 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
44 * Phase 2 packet handling
46 ddp_input(m, m->m_pkthdr.rcvif, NULL, 2);
51 at1intr(struct netmsg *msg)
53 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
54 struct elaphdr *elhp, elh;
57 * Phase 1 packet handling
59 if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
60 ddpstat.ddps_tooshort++;
65 * This seems a little dubious, but I don't know phase 1 so leave it.
67 elhp = mtod(m, struct elaphdr *);
70 if (elhp->el_type == ELAP_DDPEXTEND) {
71 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1);
73 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR);
74 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1);
80 ddp_input( m, ifp, elh, phase )
86 struct sockaddr_at from, to;
87 struct ddpshdr *dsh, ddps;
89 struct ddpehdr *deh = NULL, ddpe;
94 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
95 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
98 * Extract the information in the short header.
99 * netowrk information is defaulted to ATADDR_ANYNET
100 * and node information comes from the elh info.
101 * We must be phase 1.
103 ddpstat.ddps_short++;
105 if ( m->m_len < sizeof( struct ddpshdr ) &&
106 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
107 ddpstat.ddps_tooshort++;
111 dsh = mtod( m, struct ddpshdr *);
112 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
113 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
116 to.sat_addr.s_net = ATADDR_ANYNET;
117 to.sat_addr.s_node = elh->el_dnode;
118 to.sat_port = ddps.dsh_dport;
119 from.sat_addr.s_net = ATADDR_ANYNET;
120 from.sat_addr.s_node = elh->el_snode;
121 from.sat_port = ddps.dsh_sport;
124 * Make sure that we point to the phase1 ifaddr info
125 * and that it's valid for this packet.
127 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
128 if ( (aa->aa_ifp == ifp)
129 && ( (aa->aa_flags & AFA_PHASE2) == 0)
130 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
131 || (to.sat_addr.s_node == ATADDR_BCAST))) {
136 * maybe we got a broadcast not meant for us.. ditch it.
144 * There was no 'elh' passed on. This could still be
145 * either phase1 or phase2.
146 * We have a long header, but we may be running on a phase 1 net.
147 * Extract out all the info regarding this packet's src & dst.
151 if ( m->m_len < sizeof( struct ddpehdr ) &&
152 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
153 ddpstat.ddps_tooshort++;
157 deh = mtod( m, struct ddpehdr *);
158 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
159 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
162 if (( cksum = ddpe.deh_sum ) == 0 ) {
163 ddpstat.ddps_nosum++;
166 from.sat_addr.s_net = ddpe.deh_snet;
167 from.sat_addr.s_node = ddpe.deh_snode;
168 from.sat_port = ddpe.deh_sport;
169 to.sat_addr.s_net = ddpe.deh_dnet;
170 to.sat_addr.s_node = ddpe.deh_dnode;
171 to.sat_port = ddpe.deh_dport;
173 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
175 * The TO address doesn't specify a net,
176 * So by definition it's for this net.
177 * Try find ifaddr info with the right phase,
178 * the right interface, and either to our node, a broadcast,
179 * or looped back (though that SHOULD be covered in the other
182 * XXX If we have multiple interfaces, then the first with
183 * this node number will match (which may NOT be what we want,
184 * but it's probably safe in 99.999% of cases.
186 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
187 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
190 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
193 if ( (aa->aa_ifp == ifp)
194 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
195 || (to.sat_addr.s_node == ATADDR_BCAST)
196 || (ifp->if_flags & IFF_LOOPBACK))) {
202 * A destination network was given. We just try to find
203 * which ifaddr info matches it.
205 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
207 * This is a kludge. Accept packets that are
208 * for any router on a local netrange.
210 if ( to.sat_addr.s_net == aa->aa_firstnet &&
211 to.sat_addr.s_node == 0 ) {
215 * Don't use ifaddr info for which we are totally outside the
216 * netrange, and it's not a startup packet.
217 * Startup packets are always implicitly allowed on to
220 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
221 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
222 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
223 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
228 * Don't record a match either if we just don't have a match
229 * in the node address. This can have if the interface
230 * is in promiscuous mode for example.
232 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
233 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
242 * Adjust the length, removing any padding that may have been added
243 * at a link layer. We do this before we attempt to forward a packet,
244 * possibly on a different media.
246 mlen = m->m_pkthdr.len;
248 ddpstat.ddps_toosmall++;
253 m_adj( m, dlen - mlen );
257 * If it aint for a net on any of our interfaces,
258 * or it IS for a net on a different interface than it came in on,
259 * (and it is not looped back) then consider if we should forward it.
260 * As we are not really a router this is a bit cheeky, but it may be
264 || ( (to.sat_addr.s_node == ATADDR_BCAST)
265 && (aa->aa_ifp != ifp)
266 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
268 * If we've explicitly disabled it, don't route anything
270 if ( ddp_forward == 0 ) {
275 * If the cached forwarding route is still valid, use it.
278 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
279 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
280 RTFREE( forwro.ro_rt );
281 forwro.ro_rt = (struct rtentry *)0;
285 * If we don't have a cached one (any more) or it's useless,
286 * Then get a new route.
287 * XXX this could cause a 'route leak'. check this!
289 if ( forwro.ro_rt == (struct rtentry *)0
290 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
291 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
292 forwro.ro_dst.sa_family = AF_APPLETALK;
293 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
294 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
299 * If it's not going to get there on this hop, and it's
300 * already done too many hops, then throw it away.
302 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
303 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
309 * A ddp router might use the same interface
310 * to forward the packet, which this would not effect.
311 * Don't allow packets to cross from one interface to another however.
314 && ( (forwro.ro_rt == NULL)
315 || (forwro.ro_rt->rt_ifp != ifp))) {
322 * If it was a short header then it would have not gotten here,
323 * so we can assume there is room to drop the header in.
324 * XXX what about promiscuous mode, etc...
327 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
328 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
329 if ( ddp_route( m, &forwro )) {
330 ddpstat.ddps_cantforward++;
332 ddpstat.ddps_forward++;
338 * It was for us, and we have an ifaddr to use with it.
340 from.sat_len = sizeof( struct sockaddr_at );
341 from.sat_family = AF_APPLETALK;
344 * We are no longer interested in the link layer.
348 m_adj( m, sizeof( struct ddpshdr ));
350 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
351 ddpstat.ddps_badsum++;
355 m_adj( m, sizeof( struct ddpehdr ));
359 * Search for ddp protocol control blocks that match these
362 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
368 * If we found one, deliver th epacket to the socket
370 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
371 m, (struct mbuf *)0 ) == 0 ) {
373 * If the socket is full (or similar error) dump the packet.
375 ddpstat.ddps_nosockspace++;
380 * And wake up whatever might be waiting for it
382 sorwakeup( ddp->ddp_socket );
386 /* As if we haven't got enough of this sort of think floating
387 around the kernel :) */
392 char hexdig[] = "0123456789ABCDEF";
395 bprint( char *data, int len )
397 char xout[ BPXLEN ], aout[ BPALEN ];
400 bzero( xout, BPXLEN );
401 bzero( aout, BPALEN );
406 printf( "%s\t%s\n", xout, aout );
408 printf( "%s\n", "(end)" );
412 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
413 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
415 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
421 xout[ (i*3) + 2 ] = ' ';
427 if ( i > BPALEN - 2 ) {
428 printf( "%s\t%s\n", xout, aout );
429 bzero( xout, BPXLEN );
430 bzero( aout, BPALEN );
438 m_printm( struct mbuf *m )
440 for (; m; m = m->m_next ) {
441 bprint( mtod( m, char * ), m->m_len );