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.5 2003/09/16 05:03:13 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/route.h>
17 #include <net/intrq.h>
23 #include "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...
38 at2intr(struct mbuf *m)
42 * Phase 2 packet handling
44 ddp_input(m, m->m_pkthdr.rcvif, NULL, 2);
49 at1intr(struct mbuf *m)
51 struct elaphdr *elhp, elh;
54 * Phase 1 packet handling
56 if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
57 ddpstat.ddps_tooshort++;
62 * This seems a little dubious, but I don't know phase 1 so leave it.
64 elhp = mtod(m, struct elaphdr *);
67 if (elhp->el_type == ELAP_DDPEXTEND) {
68 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1);
70 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR);
71 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1);
77 ddp_input( m, ifp, elh, phase )
83 struct sockaddr_at from, to;
84 struct ddpshdr *dsh, ddps;
86 struct ddpehdr *deh = NULL, ddpe;
91 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
92 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
95 * Extract the information in the short header.
96 * netowrk information is defaulted to ATADDR_ANYNET
97 * and node information comes from the elh info.
100 ddpstat.ddps_short++;
102 if ( m->m_len < sizeof( struct ddpshdr ) &&
103 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
104 ddpstat.ddps_tooshort++;
108 dsh = mtod( m, struct ddpshdr *);
109 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
110 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
113 to.sat_addr.s_net = ATADDR_ANYNET;
114 to.sat_addr.s_node = elh->el_dnode;
115 to.sat_port = ddps.dsh_dport;
116 from.sat_addr.s_net = ATADDR_ANYNET;
117 from.sat_addr.s_node = elh->el_snode;
118 from.sat_port = ddps.dsh_sport;
121 * Make sure that we point to the phase1 ifaddr info
122 * and that it's valid for this packet.
124 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
125 if ( (aa->aa_ifp == ifp)
126 && ( (aa->aa_flags & AFA_PHASE2) == 0)
127 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
128 || (to.sat_addr.s_node == ATADDR_BCAST))) {
133 * maybe we got a broadcast not meant for us.. ditch it.
141 * There was no 'elh' passed on. This could still be
142 * either phase1 or phase2.
143 * We have a long header, but we may be running on a phase 1 net.
144 * Extract out all the info regarding this packet's src & dst.
148 if ( m->m_len < sizeof( struct ddpehdr ) &&
149 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
150 ddpstat.ddps_tooshort++;
154 deh = mtod( m, struct ddpehdr *);
155 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
156 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
159 if (( cksum = ddpe.deh_sum ) == 0 ) {
160 ddpstat.ddps_nosum++;
163 from.sat_addr.s_net = ddpe.deh_snet;
164 from.sat_addr.s_node = ddpe.deh_snode;
165 from.sat_port = ddpe.deh_sport;
166 to.sat_addr.s_net = ddpe.deh_dnet;
167 to.sat_addr.s_node = ddpe.deh_dnode;
168 to.sat_port = ddpe.deh_dport;
170 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
172 * The TO address doesn't specify a net,
173 * So by definition it's for this net.
174 * Try find ifaddr info with the right phase,
175 * the right interface, and either to our node, a broadcast,
176 * or looped back (though that SHOULD be covered in the other
179 * XXX If we have multiple interfaces, then the first with
180 * this node number will match (which may NOT be what we want,
181 * but it's probably safe in 99.999% of cases.
183 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
184 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
187 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
190 if ( (aa->aa_ifp == ifp)
191 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
192 || (to.sat_addr.s_node == ATADDR_BCAST)
193 || (ifp->if_flags & IFF_LOOPBACK))) {
199 * A destination network was given. We just try to find
200 * which ifaddr info matches it.
202 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
204 * This is a kludge. Accept packets that are
205 * for any router on a local netrange.
207 if ( to.sat_addr.s_net == aa->aa_firstnet &&
208 to.sat_addr.s_node == 0 ) {
212 * Don't use ifaddr info for which we are totally outside the
213 * netrange, and it's not a startup packet.
214 * Startup packets are always implicitly allowed on to
217 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
218 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
219 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
220 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
225 * Don't record a match either if we just don't have a match
226 * in the node address. This can have if the interface
227 * is in promiscuous mode for example.
229 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
230 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
239 * Adjust the length, removing any padding that may have been added
240 * at a link layer. We do this before we attempt to forward a packet,
241 * possibly on a different media.
243 mlen = m->m_pkthdr.len;
245 ddpstat.ddps_toosmall++;
250 m_adj( m, dlen - mlen );
254 * If it aint for a net on any of our interfaces,
255 * or it IS for a net on a different interface than it came in on,
256 * (and it is not looped back) then consider if we should forward it.
257 * As we are not really a router this is a bit cheeky, but it may be
261 || ( (to.sat_addr.s_node == ATADDR_BCAST)
262 && (aa->aa_ifp != ifp)
263 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
265 * If we've explicitly disabled it, don't route anything
267 if ( ddp_forward == 0 ) {
272 * If the cached forwarding route is still valid, use it.
275 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
276 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
277 RTFREE( forwro.ro_rt );
278 forwro.ro_rt = (struct rtentry *)0;
282 * If we don't have a cached one (any more) or it's useless,
283 * Then get a new route.
284 * XXX this could cause a 'route leak'. check this!
286 if ( forwro.ro_rt == (struct rtentry *)0
287 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
288 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
289 forwro.ro_dst.sa_family = AF_APPLETALK;
290 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
291 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
296 * If it's not going to get there on this hop, and it's
297 * already done too many hops, then throw it away.
299 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
300 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
306 * A ddp router might use the same interface
307 * to forward the packet, which this would not effect.
308 * Don't allow packets to cross from one interface to another however.
311 && ( (forwro.ro_rt == NULL)
312 || (forwro.ro_rt->rt_ifp != ifp))) {
319 * If it was a short header then it would have not gotten here,
320 * so we can assume there is room to drop the header in.
321 * XXX what about promiscuous mode, etc...
324 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
325 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
326 if ( ddp_route( m, &forwro )) {
327 ddpstat.ddps_cantforward++;
329 ddpstat.ddps_forward++;
335 * It was for us, and we have an ifaddr to use with it.
337 from.sat_len = sizeof( struct sockaddr_at );
338 from.sat_family = AF_APPLETALK;
341 * We are no longer interested in the link layer.
345 m_adj( m, sizeof( struct ddpshdr ));
347 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
348 ddpstat.ddps_badsum++;
352 m_adj( m, sizeof( struct ddpehdr ));
356 * Search for ddp protocol control blocks that match these
359 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
365 * If we found one, deliver th epacket to the socket
367 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
368 m, (struct mbuf *)0 ) == 0 ) {
370 * If the socket is full (or similar error) dump the packet.
372 ddpstat.ddps_nosockspace++;
377 * And wake up whatever might be waiting for it
379 sorwakeup( ddp->ddp_socket );
383 /* As if we haven't got enough of this sort of think floating
384 around the kernel :) */
389 char hexdig[] = "0123456789ABCDEF";
392 bprint( char *data, int len )
394 char xout[ BPXLEN ], aout[ BPALEN ];
397 bzero( xout, BPXLEN );
398 bzero( aout, BPALEN );
403 printf( "%s\t%s\n", xout, aout );
405 printf( "%s\n", "(end)" );
409 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
410 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
412 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
418 xout[ (i*3) + 2 ] = ' ';
424 if ( i > BPALEN - 2 ) {
425 printf( "%s\t%s\n", xout, aout );
426 bzero( xout, BPXLEN );
427 bzero( aout, BPALEN );
435 m_printm( struct mbuf *m )
437 for (; m; m = m->m_next ) {
438 bprint( mtod( m, char * ), m->m_len );