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.11 2006/05/20 06:32:41 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 /* msg was embedded in the mbuf, do not reply! */
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 /* msg was embedded in the mbuf, do not reply! */
87 ddp_input(struct mbuf *m, struct ifnet *ifp, struct elaphdr *elh, int phase)
89 struct sockaddr_at from, to;
90 struct ddpshdr *dsh, ddps;
92 struct ddpehdr *deh = NULL, ddpe;
97 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
98 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
101 * Extract the information in the short header.
102 * netowrk information is defaulted to ATADDR_ANYNET
103 * and node information comes from the elh info.
104 * We must be phase 1.
106 ddpstat.ddps_short++;
108 if ( m->m_len < sizeof( struct ddpshdr ) &&
109 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
110 ddpstat.ddps_tooshort++;
114 dsh = mtod( m, struct ddpshdr *);
115 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
116 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
119 to.sat_addr.s_net = ATADDR_ANYNET;
120 to.sat_addr.s_node = elh->el_dnode;
121 to.sat_port = ddps.dsh_dport;
122 from.sat_addr.s_net = ATADDR_ANYNET;
123 from.sat_addr.s_node = elh->el_snode;
124 from.sat_port = ddps.dsh_sport;
127 * Make sure that we point to the phase1 ifaddr info
128 * and that it's valid for this packet.
130 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
131 if ( (aa->aa_ifp == ifp)
132 && ( (aa->aa_flags & AFA_PHASE2) == 0)
133 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
134 || (to.sat_addr.s_node == ATADDR_BCAST))) {
139 * maybe we got a broadcast not meant for us.. ditch it.
147 * There was no 'elh' passed on. This could still be
148 * either phase1 or phase2.
149 * We have a long header, but we may be running on a phase 1 net.
150 * Extract out all the info regarding this packet's src & dst.
154 if ( m->m_len < sizeof( struct ddpehdr ) &&
155 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
156 ddpstat.ddps_tooshort++;
160 deh = mtod( m, struct ddpehdr *);
161 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
162 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
165 if (( cksum = ddpe.deh_sum ) == 0 ) {
166 ddpstat.ddps_nosum++;
169 from.sat_addr.s_net = ddpe.deh_snet;
170 from.sat_addr.s_node = ddpe.deh_snode;
171 from.sat_port = ddpe.deh_sport;
172 to.sat_addr.s_net = ddpe.deh_dnet;
173 to.sat_addr.s_node = ddpe.deh_dnode;
174 to.sat_port = ddpe.deh_dport;
176 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
178 * The TO address doesn't specify a net,
179 * So by definition it's for this net.
180 * Try find ifaddr info with the right phase,
181 * the right interface, and either to our node, a broadcast,
182 * or looped back (though that SHOULD be covered in the other
185 * XXX If we have multiple interfaces, then the first with
186 * this node number will match (which may NOT be what we want,
187 * but it's probably safe in 99.999% of cases.
189 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
190 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
193 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
196 if ( (aa->aa_ifp == ifp)
197 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
198 || (to.sat_addr.s_node == ATADDR_BCAST)
199 || (ifp->if_flags & IFF_LOOPBACK))) {
205 * A destination network was given. We just try to find
206 * which ifaddr info matches it.
208 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
210 * This is a kludge. Accept packets that are
211 * for any router on a local netrange.
213 if ( to.sat_addr.s_net == aa->aa_firstnet &&
214 to.sat_addr.s_node == 0 ) {
218 * Don't use ifaddr info for which we are totally outside the
219 * netrange, and it's not a startup packet.
220 * Startup packets are always implicitly allowed on to
223 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
224 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
225 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
226 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
231 * Don't record a match either if we just don't have a match
232 * in the node address. This can have if the interface
233 * is in promiscuous mode for example.
235 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
236 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
245 * Adjust the length, removing any padding that may have been added
246 * at a link layer. We do this before we attempt to forward a packet,
247 * possibly on a different media.
249 mlen = m->m_pkthdr.len;
251 ddpstat.ddps_toosmall++;
256 m_adj( m, dlen - mlen );
260 * If it aint for a net on any of our interfaces,
261 * or it IS for a net on a different interface than it came in on,
262 * (and it is not looped back) then consider if we should forward it.
263 * As we are not really a router this is a bit cheeky, but it may be
267 || ( (to.sat_addr.s_node == ATADDR_BCAST)
268 && (aa->aa_ifp != ifp)
269 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
271 * If we've explicitly disabled it, don't route anything
273 if ( ddp_forward == 0 ) {
278 * If the cached forwarding route is still valid, use it.
281 && ( satosat(&forwro.ro_dst)->sat_addr.s_net != to.sat_addr.s_net
282 || satosat(&forwro.ro_dst)->sat_addr.s_node != to.sat_addr.s_node )) {
283 RTFREE( forwro.ro_rt );
284 forwro.ro_rt = (struct rtentry *)0;
288 * If we don't have a cached one (any more) or it's useless,
289 * Then get a new route.
290 * XXX this could cause a 'route leak'. check this!
292 if ( forwro.ro_rt == (struct rtentry *)0
293 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
294 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
295 forwro.ro_dst.sa_family = AF_APPLETALK;
296 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
297 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
302 * If it's not going to get there on this hop, and it's
303 * already done too many hops, then throw it away.
305 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
306 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
312 * A ddp router might use the same interface
313 * to forward the packet, which this would not effect.
314 * Don't allow packets to cross from one interface to another however.
317 && ( (forwro.ro_rt == NULL)
318 || (forwro.ro_rt->rt_ifp != ifp))) {
325 * If it was a short header then it would have not gotten here,
326 * so we can assume there is room to drop the header in.
327 * XXX what about promiscuous mode, etc...
330 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
331 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
332 if ( ddp_route( m, &forwro )) {
333 ddpstat.ddps_cantforward++;
335 ddpstat.ddps_forward++;
341 * It was for us, and we have an ifaddr to use with it.
343 from.sat_len = sizeof( struct sockaddr_at );
344 from.sat_family = AF_APPLETALK;
347 * We are no longer interested in the link layer.
351 m_adj( m, sizeof( struct ddpshdr ));
353 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
354 ddpstat.ddps_badsum++;
358 m_adj( m, sizeof( struct ddpehdr ));
362 * Search for ddp protocol control blocks that match these
365 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
371 * If we found one, deliver th epacket to the socket
373 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
374 m, (struct mbuf *)0 ) == 0 ) {
376 * If the socket is full (or similar error) dump the packet.
378 ddpstat.ddps_nosockspace++;
383 * And wake up whatever might be waiting for it
385 sorwakeup( ddp->ddp_socket );
389 /* As if we haven't got enough of this sort of think floating
390 around the kernel :) */
395 char hexdig[] = "0123456789ABCDEF";
398 bprint(char *data, int len)
400 char xout[ BPXLEN ], aout[ BPALEN ];
403 bzero( xout, BPXLEN );
404 bzero( aout, BPALEN );
409 printf( "%s\t%s\n", xout, aout );
411 printf( "%s\n", "(end)" );
415 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
416 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
418 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
424 xout[ (i*3) + 2 ] = ' ';
430 if ( i > BPALEN - 2 ) {
431 printf( "%s\t%s\n", xout, aout );
432 bzero( xout, BPXLEN );
433 bzero( aout, BPALEN );
441 m_printm(struct mbuf *m)
443 for (; m; m = m->m_next ) {
444 bprint( mtod( m, char * ), m->m_len );