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.15 2007/11/16 05:07:36 sephe 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>
18 #include <sys/mplock2.h>
21 #include <net/netisr.h>
22 #include <net/route.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...
45 struct mbuf *m = msg->packet.nm_packet;
48 * Phase 2 packet handling
51 ddp_input(m, m->m_pkthdr.rcvif, NULL, 2);
53 /* msg was embedded in the mbuf, do not reply! */
59 struct mbuf *m = msg->packet.nm_packet;
60 struct elaphdr *elhp, elh;
65 * Phase 1 packet handling
67 if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
68 ddpstat.ddps_tooshort++;
73 * This seems a little dubious, but I don't know phase 1 so leave it.
75 elhp = mtod(m, struct elaphdr *);
78 if (elhp->el_type == ELAP_DDPEXTEND) {
79 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1);
81 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR);
82 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1);
86 /* msg was embedded in the mbuf, do not reply! */
90 ddp_input(struct mbuf *m, struct ifnet *ifp, struct elaphdr *elh, int phase)
92 struct sockaddr_at from, to;
93 struct ddpshdr *dsh, ddps;
95 struct ddpehdr *deh = NULL, ddpe;
100 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
101 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
104 * Extract the information in the short header.
105 * netowrk information is defaulted to ATADDR_ANYNET
106 * and node information comes from the elh info.
107 * We must be phase 1.
109 ddpstat.ddps_short++;
111 if ( m->m_len < sizeof( struct ddpshdr ) &&
112 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
113 ddpstat.ddps_tooshort++;
117 dsh = mtod( m, struct ddpshdr *);
118 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
119 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
122 to.sat_addr.s_net = ATADDR_ANYNET;
123 to.sat_addr.s_node = elh->el_dnode;
124 to.sat_port = ddps.dsh_dport;
125 from.sat_addr.s_net = ATADDR_ANYNET;
126 from.sat_addr.s_node = elh->el_snode;
127 from.sat_port = ddps.dsh_sport;
130 * Make sure that we point to the phase1 ifaddr info
131 * and that it's valid for this packet.
133 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
134 if ( (aa->aa_ifp == ifp)
135 && ( (aa->aa_flags & AFA_PHASE2) == 0)
136 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
137 || (to.sat_addr.s_node == ATADDR_BCAST))) {
142 * maybe we got a broadcast not meant for us.. ditch it.
150 * There was no 'elh' passed on. This could still be
151 * either phase1 or phase2.
152 * We have a long header, but we may be running on a phase 1 net.
153 * Extract out all the info regarding this packet's src & dst.
157 if ( m->m_len < sizeof( struct ddpehdr ) &&
158 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
159 ddpstat.ddps_tooshort++;
163 deh = mtod( m, struct ddpehdr *);
164 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
165 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
168 if (( cksum = ddpe.deh_sum ) == 0 ) {
169 ddpstat.ddps_nosum++;
172 from.sat_addr.s_net = ddpe.deh_snet;
173 from.sat_addr.s_node = ddpe.deh_snode;
174 from.sat_port = ddpe.deh_sport;
175 to.sat_addr.s_net = ddpe.deh_dnet;
176 to.sat_addr.s_node = ddpe.deh_dnode;
177 to.sat_port = ddpe.deh_dport;
179 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
181 * The TO address doesn't specify a net,
182 * So by definition it's for this net.
183 * Try find ifaddr info with the right phase,
184 * the right interface, and either to our node, a broadcast,
185 * or looped back (though that SHOULD be covered in the other
188 * XXX If we have multiple interfaces, then the first with
189 * this node number will match (which may NOT be what we want,
190 * but it's probably safe in 99.999% of cases.
192 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
193 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
196 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
199 if ( (aa->aa_ifp == ifp)
200 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
201 || (to.sat_addr.s_node == ATADDR_BCAST)
202 || (ifp->if_flags & IFF_LOOPBACK))) {
208 * A destination network was given. We just try to find
209 * which ifaddr info matches it.
211 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
213 * This is a kludge. Accept packets that are
214 * for any router on a local netrange.
216 if ( to.sat_addr.s_net == aa->aa_firstnet &&
217 to.sat_addr.s_node == 0 ) {
221 * Don't use ifaddr info for which we are totally outside the
222 * netrange, and it's not a startup packet.
223 * Startup packets are always implicitly allowed on to
226 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
227 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
228 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
229 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
234 * Don't record a match either if we just don't have a match
235 * in the node address. This can have if the interface
236 * is in promiscuous mode for example.
238 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
239 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
248 * Adjust the length, removing any padding that may have been added
249 * at a link layer. We do this before we attempt to forward a packet,
250 * possibly on a different media.
252 mlen = m->m_pkthdr.len;
254 ddpstat.ddps_toosmall++;
259 m_adj( m, dlen - mlen );
263 * If it aint for a net on any of our interfaces,
264 * or it IS for a net on a different interface than it came in on,
265 * (and it is not looped back) then consider if we should forward it.
266 * As we are not really a router this is a bit cheeky, but it may be
270 || ( (to.sat_addr.s_node == ATADDR_BCAST)
271 && (aa->aa_ifp != ifp)
272 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
274 * If we've explicitly disabled it, don't route anything
276 if ( ddp_forward == 0 ) {
281 * If the cached forwarding route is still valid, use it.
284 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
285 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
286 RTFREE( forwro.ro_rt );
291 * If we don't have a cached one (any more) or it's useless,
292 * Then get a new route.
293 * XXX this could cause a 'route leak'. check this!
295 if ( forwro.ro_rt == NULL
296 || forwro.ro_rt->rt_ifp == NULL ) {
297 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
298 forwro.ro_dst.sa_family = AF_APPLETALK;
299 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
300 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
305 * If it's not going to get there on this hop, and it's
306 * already done too many hops, then throw it away.
308 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
309 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
315 * A ddp router might use the same interface
316 * to forward the packet, which this would not effect.
317 * Don't allow packets to cross from one interface to another however.
320 && ( (forwro.ro_rt == NULL)
321 || (forwro.ro_rt->rt_ifp != ifp))) {
328 * If it was a short header then it would have not gotten here,
329 * so we can assume there is room to drop the header in.
330 * XXX what about promiscuous mode, etc...
333 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
334 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
335 if ( ddp_route( m, &forwro )) {
336 ddpstat.ddps_cantforward++;
338 ddpstat.ddps_forward++;
344 * It was for us, and we have an ifaddr to use with it.
346 from.sat_len = sizeof( struct sockaddr_at );
347 from.sat_family = AF_APPLETALK;
350 * We are no longer interested in the link layer.
354 m_adj( m, sizeof( struct ddpshdr ));
356 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
357 ddpstat.ddps_badsum++;
361 m_adj( m, sizeof( struct ddpehdr ));
365 * Search for ddp protocol control blocks that match these
368 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
374 * If we found one, deliver th epacket to the socket
376 if (ssb_appendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
379 * If the socket is full (or similar error) dump the packet.
381 ddpstat.ddps_nosockspace++;
386 * And wake up whatever might be waiting for it
388 sorwakeup( ddp->ddp_socket );
392 /* As if we haven't got enough of this sort of think floating
393 around the kernel :) */
398 char hexdig[] = "0123456789ABCDEF";
401 bprint(char *data, int len)
403 char xout[ BPXLEN ], aout[ BPALEN ];
406 bzero( xout, BPXLEN );
407 bzero( aout, BPALEN );
412 kprintf( "%s\t%s\n", xout, aout );
414 kprintf( "%s\n", "(end)" );
418 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
419 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
421 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
427 xout[ (i*3) + 2 ] = ' ';
433 if ( i > BPALEN - 2 ) {
434 kprintf( "%s\t%s\n", xout, aout );
435 bzero( xout, BPXLEN );
436 bzero( aout, BPALEN );
444 m_printm(struct mbuf *m)
446 for (; m; m = m->m_next ) {
447 bprint( mtod( m, char * ), m->m_len );