2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
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 static void ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
33 * Could probably merge these two code segments a little better...
38 struct elaphdr *elhp, elh;
44 * First pull off all the phase 2 packets.
49 IF_DEQUEUE( &atintrq2, m );
53 if ( m == 0 ) { /* no more queued packets */
57 ifp = m->m_pkthdr.rcvif;
58 ddp_input( m, ifp, (struct elaphdr *)NULL, 2 );
62 * Then pull off all the phase 1 packets.
67 IF_DEQUEUE( &atintrq1, m );
71 if ( m == 0 ) { /* no more queued packets */
75 ifp = m->m_pkthdr.rcvif;
77 if ( m->m_len < SZ_ELAPHDR &&
78 (( m = m_pullup( m, SZ_ELAPHDR )) == 0 )) {
79 ddpstat.ddps_tooshort++;
84 * this seems a little dubios, but I don't know phase 1 so leave it.
86 elhp = mtod( m, struct elaphdr *);
87 m_adj( m, SZ_ELAPHDR );
89 if ( elhp->el_type == ELAP_DDPEXTEND ) {
90 ddp_input( m, ifp, (struct elaphdr *)NULL, 1 );
92 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR );
93 ddp_input( m, ifp, &elh, 1 );
100 netisr_atalk_setup(void *dummy __unused)
103 register_netisr(NETISR_ATALK, atintr);
105 SYSINIT(atalk_setup, SI_SUB_CPU, SI_ORDER_ANY, netisr_atalk_setup, NULL);
108 ddp_input( m, ifp, elh, phase )
114 struct sockaddr_at from, to;
115 struct ddpshdr *dsh, ddps;
116 struct at_ifaddr *aa;
117 struct ddpehdr *deh = NULL, ddpe;
122 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
123 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
126 * Extract the information in the short header.
127 * netowrk information is defaulted to ATADDR_ANYNET
128 * and node information comes from the elh info.
129 * We must be phase 1.
131 ddpstat.ddps_short++;
133 if ( m->m_len < sizeof( struct ddpshdr ) &&
134 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
135 ddpstat.ddps_tooshort++;
139 dsh = mtod( m, struct ddpshdr *);
140 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
141 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
144 to.sat_addr.s_net = ATADDR_ANYNET;
145 to.sat_addr.s_node = elh->el_dnode;
146 to.sat_port = ddps.dsh_dport;
147 from.sat_addr.s_net = ATADDR_ANYNET;
148 from.sat_addr.s_node = elh->el_snode;
149 from.sat_port = ddps.dsh_sport;
152 * Make sure that we point to the phase1 ifaddr info
153 * and that it's valid for this packet.
155 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
156 if ( (aa->aa_ifp == ifp)
157 && ( (aa->aa_flags & AFA_PHASE2) == 0)
158 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
159 || (to.sat_addr.s_node == ATADDR_BCAST))) {
164 * maybe we got a broadcast not meant for us.. ditch it.
172 * There was no 'elh' passed on. This could still be
173 * either phase1 or phase2.
174 * We have a long header, but we may be running on a phase 1 net.
175 * Extract out all the info regarding this packet's src & dst.
179 if ( m->m_len < sizeof( struct ddpehdr ) &&
180 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
181 ddpstat.ddps_tooshort++;
185 deh = mtod( m, struct ddpehdr *);
186 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
187 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
190 if (( cksum = ddpe.deh_sum ) == 0 ) {
191 ddpstat.ddps_nosum++;
194 from.sat_addr.s_net = ddpe.deh_snet;
195 from.sat_addr.s_node = ddpe.deh_snode;
196 from.sat_port = ddpe.deh_sport;
197 to.sat_addr.s_net = ddpe.deh_dnet;
198 to.sat_addr.s_node = ddpe.deh_dnode;
199 to.sat_port = ddpe.deh_dport;
201 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
203 * The TO address doesn't specify a net,
204 * So by definition it's for this net.
205 * Try find ifaddr info with the right phase,
206 * the right interface, and either to our node, a broadcast,
207 * or looped back (though that SHOULD be covered in the other
210 * XXX If we have multiple interfaces, then the first with
211 * this node number will match (which may NOT be what we want,
212 * but it's probably safe in 99.999% of cases.
214 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
215 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
218 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
221 if ( (aa->aa_ifp == ifp)
222 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
223 || (to.sat_addr.s_node == ATADDR_BCAST)
224 || (ifp->if_flags & IFF_LOOPBACK))) {
230 * A destination network was given. We just try to find
231 * which ifaddr info matches it.
233 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
235 * This is a kludge. Accept packets that are
236 * for any router on a local netrange.
238 if ( to.sat_addr.s_net == aa->aa_firstnet &&
239 to.sat_addr.s_node == 0 ) {
243 * Don't use ifaddr info for which we are totally outside the
244 * netrange, and it's not a startup packet.
245 * Startup packets are always implicitly allowed on to
248 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
249 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
250 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
251 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
256 * Don't record a match either if we just don't have a match
257 * in the node address. This can have if the interface
258 * is in promiscuous mode for example.
260 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
261 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
270 * Adjust the length, removing any padding that may have been added
271 * at a link layer. We do this before we attempt to forward a packet,
272 * possibly on a different media.
274 mlen = m->m_pkthdr.len;
276 ddpstat.ddps_toosmall++;
281 m_adj( m, dlen - mlen );
285 * If it aint for a net on any of our interfaces,
286 * or it IS for a net on a different interface than it came in on,
287 * (and it is not looped back) then consider if we should forward it.
288 * As we are not really a router this is a bit cheeky, but it may be
292 || ( (to.sat_addr.s_node == ATADDR_BCAST)
293 && (aa->aa_ifp != ifp)
294 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
296 * If we've explicitly disabled it, don't route anything
298 if ( ddp_forward == 0 ) {
303 * If the cached forwarding route is still valid, use it.
306 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
307 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
308 RTFREE( forwro.ro_rt );
309 forwro.ro_rt = (struct rtentry *)0;
313 * If we don't have a cached one (any more) or it's useless,
314 * Then get a new route.
315 * XXX this could cause a 'route leak'. check this!
317 if ( forwro.ro_rt == (struct rtentry *)0
318 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
319 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
320 forwro.ro_dst.sa_family = AF_APPLETALK;
321 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
322 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
327 * If it's not going to get there on this hop, and it's
328 * already done too many hops, then throw it away.
330 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
331 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
337 * A ddp router might use the same interface
338 * to forward the packet, which this would not effect.
339 * Don't allow packets to cross from one interface to another however.
342 && ( (forwro.ro_rt == NULL)
343 || (forwro.ro_rt->rt_ifp != ifp))) {
350 * If it was a short header then it would have not gotten here,
351 * so we can assume there is room to drop the header in.
352 * XXX what about promiscuous mode, etc...
355 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
356 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
357 if ( ddp_route( m, &forwro )) {
358 ddpstat.ddps_cantforward++;
360 ddpstat.ddps_forward++;
366 * It was for us, and we have an ifaddr to use with it.
368 from.sat_len = sizeof( struct sockaddr_at );
369 from.sat_family = AF_APPLETALK;
372 * We are no longer interested in the link layer.
376 m_adj( m, sizeof( struct ddpshdr ));
378 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
379 ddpstat.ddps_badsum++;
383 m_adj( m, sizeof( struct ddpehdr ));
387 * Search for ddp protocol control blocks that match these
390 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
396 * If we found one, deliver th epacket to the socket
398 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
399 m, (struct mbuf *)0 ) == 0 ) {
401 * If the socket is full (or similar error) dump the packet.
403 ddpstat.ddps_nosockspace++;
408 * And wake up whatever might be waiting for it
410 sorwakeup( ddp->ddp_socket );
414 /* As if we haven't got enough of this sort of think floating
415 around the kernel :) */
420 char hexdig[] = "0123456789ABCDEF";
423 bprint( char *data, int len )
425 char xout[ BPXLEN ], aout[ BPALEN ];
428 bzero( xout, BPXLEN );
429 bzero( aout, BPALEN );
434 printf( "%s\t%s\n", xout, aout );
436 printf( "%s\n", "(end)" );
440 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
441 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
443 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
449 xout[ (i*3) + 2 ] = ' ';
455 if ( i > BPALEN - 2 ) {
456 printf( "%s\t%s\n", xout, aout );
457 bzero( xout, BPXLEN );
458 bzero( aout, BPALEN );
466 m_printm( struct mbuf *m )
468 for (; m; m = m->m_next ) {
469 bprint( mtod( m, char * ), m->m_len );