2 * $Id: ddp_input.c,v 1.4 2002-01-04 04:45:48 sibaz Exp $
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #include <sys/types.h>
13 #include <sys/param.h>
15 #include <sys/socket.h>
16 #include <sys/socketvar.h>
17 #include <atalk/logger.h>
19 #include <net/route.h>
33 extern u_short at_cksum();
36 * Could probably merge these two code segments a little better...
40 struct elaphdr *elhp, elh;
52 IF_DEQUEUE( &atintrq2, m );
54 IF_DEQUEUEIF( &atintrq2, m, ifp );
61 if ( m == 0 ) { /* no more queued packets */
66 ifp = m->m_pkthdr.rcvif;
68 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
69 if ( aa->aa_ifp == ifp && ( aa->aa_flags & AFA_PHASE2 )) {
73 if ( aa == NULL ) { /* ifp not an appletalk interface */
78 ddp_input( m, ifp, (struct elaphdr *)NULL, 2 );
87 IF_DEQUEUE( &atintrq1, m );
89 IF_DEQUEUEIF( &atintrq1, m, ifp );
96 if ( m == 0 ) { /* no more queued packets */
101 ifp = m->m_pkthdr.rcvif;
103 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
104 if ( aa->aa_ifp == ifp && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
108 if ( aa == NULL ) { /* ifp not an appletalk interface */
113 if ( m->m_len < SZ_ELAPHDR &&
114 (( m = m_pullup( m, SZ_ELAPHDR )) == 0 )) {
115 ddpstat.ddps_tooshort++;
119 elhp = mtod( m, struct elaphdr *);
120 m_adj( m, SZ_ELAPHDR );
122 if ( elhp->el_type == ELAP_DDPEXTEND ) {
123 ddp_input( m, ifp, (struct elaphdr *)NULL, 1 );
125 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR );
126 ddp_input( m, ifp, &elh, 1 );
134 ddp_input( m, ifp, elh, phase )
140 struct sockaddr_at from, to;
141 struct ddpshdr *dsh, ddps;
142 struct at_ifaddr *aa;
143 struct ddpehdr *deh, ddpe;
151 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
153 ddpstat.ddps_short++;
155 if ( m->m_len < sizeof( struct ddpshdr ) &&
156 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
157 ddpstat.ddps_tooshort++;
161 dsh = mtod( m, struct ddpshdr *);
162 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
163 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
166 to.sat_addr.s_net = 0;
167 to.sat_addr.s_node = elh->el_dnode;
168 to.sat_port = ddps.dsh_dport;
169 from.sat_addr.s_net = 0;
170 from.sat_addr.s_node = elh->el_snode;
171 from.sat_port = ddps.dsh_sport;
173 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
174 if ( aa->aa_ifp == ifp && ( aa->aa_flags & AFA_PHASE2 ) == 0 &&
175 ( AA_SAT( aa )->sat_addr.s_node == to.sat_addr.s_node ||
176 to.sat_addr.s_node == ATADDR_BCAST )) {
187 if ( m->m_len < sizeof( struct ddpehdr ) &&
188 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
189 ddpstat.ddps_tooshort++;
193 deh = mtod( m, struct ddpehdr *);
194 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
195 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
198 if (( cksum = ddpe.deh_sum ) == 0 ) {
199 ddpstat.ddps_nosum++;
202 from.sat_addr.s_net = ddpe.deh_snet;
203 from.sat_addr.s_node = ddpe.deh_snode;
204 from.sat_port = ddpe.deh_sport;
205 to.sat_addr.s_net = ddpe.deh_dnet;
206 to.sat_addr.s_node = ddpe.deh_dnode;
207 to.sat_port = ddpe.deh_dport;
209 if ( to.sat_addr.s_net == 0 ) {
210 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
211 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
214 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
217 if ( aa->aa_ifp == ifp &&
218 ( AA_SAT( aa )->sat_addr.s_node == to.sat_addr.s_node ||
219 to.sat_addr.s_node == ATADDR_BCAST ||
220 ( ifp->if_flags & IFF_LOOPBACK ))) {
225 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
226 if ( to.sat_addr.s_net == aa->aa_firstnet &&
227 to.sat_addr.s_node == 0 ) {
230 if (( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ) ||
231 ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )) &&
232 ( ntohs( to.sat_addr.s_net ) < 0xff00 ||
233 ntohs( to.sat_addr.s_net ) > 0xfffe)) {
236 if ( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node &&
237 to.sat_addr.s_node != ATADDR_BCAST ) {
246 * Adjust the length, removing any padding that may have been added
247 * at a link layer. We do this before we attempt to forward a packet,
248 * possibly on a different media.
251 mlen = m->m_pkthdr.len;
253 for ( mlen = 0, mp = m; mp; mp = mp->m_next ) {
258 ddpstat.ddps_toosmall++;
263 m_adj( m, dlen - mlen );
267 * XXX Should we deliver broadcasts locally, also, or rely on the
268 * link layer to give us a copy? For the moment, the latter.
270 if ( aa == NULL || ( to.sat_addr.s_node == ATADDR_BCAST &&
271 aa->aa_ifp != ifp && ( ifp->if_flags & IFF_LOOPBACK ) == 0 )) {
272 if ( ddp_forward == 0 ) {
276 if ( forwro.ro_rt && ( satosat( &forwro.ro_dst )->sat_addr.s_net !=
278 satosat( &forwro.ro_dst )->sat_addr.s_node !=
279 to.sat_addr.s_node )) {
281 rtfree( forwro.ro_rt );
283 RTFREE( forwro.ro_rt );
285 forwro.ro_rt = (struct rtentry *)0;
287 if ( forwro.ro_rt == (struct rtentry *)0 ||
288 forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
290 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;
298 if ( to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net &&
299 ddpe.deh_hops == DDP_MAXHOPS ) {
305 ( forwro.ro_rt == NULL || ( forwro.ro_rt->rt_ifp != ifp &&
306 forwro.ro_rt->rt_ifp != at_ifaddr->aa_ifp ))) {
312 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
313 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short ));
314 if ( ddp_route( m, &forwro )) {
315 ddpstat.ddps_cantforward++;
317 ddpstat.ddps_forward++;
323 from.sat_len = sizeof( struct sockaddr_at );
325 from.sat_family = AF_APPLETALK;
328 m_adj( m, sizeof( struct ddpshdr ));
330 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
331 ddpstat.ddps_badsum++;
335 m_adj( m, sizeof( struct ddpehdr ));
338 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
343 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
344 m, (struct mbuf *)0 ) == 0 ) {
345 ddpstat.ddps_nosockspace++;
349 sorwakeup( ddp->ddp_socket );
355 for (; m; m = m->m_next ) {
356 bprint( mtod( m, char * ), m->m_len );
363 char hexdig[] = "0123456789ABCDEF";
369 char xout[ BPXLEN ], aout[ BPALEN ];
372 bzero( xout, BPXLEN );
373 bzero( aout, BPALEN );
378 printf( "%s\t%s\n", xout, aout );
380 printf( "%s\n", "(end)" );
384 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
385 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
387 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
393 xout[ (i*3) + 2 ] = ' ';
399 if ( i > BPALEN - 2 ) {
400 printf( "%s\t%s\n", xout, aout );
401 bzero( xout, BPXLEN );
402 bzero( aout, BPALEN );