1 /*
2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
4 */
5
6 #include <sys/param.h>
7 #include <sys/systm.h>
8 #include <sys/kernel.h>
9 #include <net/netisr.h>
10 #include <sys/mbuf.h>
11 #include <sys/socket.h>
12 #include <sys/socketvar.h>
13 #include <net/if.h>
14 #include <net/route.h>
15
16 #include <netatalk/at.h>
17 #include <netatalk/at_var.h>
18 #include <netatalk/ddp.h>
19 #include <netatalk/ddp_var.h>
20 #include <netatalk/at_extern.h>
21
22 struct ifqueue atintrq1, atintrq2;
23
24 static volatile int ddp_forward = 1;
25 static volatile int ddp_firewall = 0;
26 static struct ddpstat ddpstat;
27 static struct route forwro;
28
29 static void ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
30
31 /*
32 * Could probably merge these two code segments a little better...
33 */
34 static void
35 atintr( void )
36 {
37 struct elaphdr *elhp, elh;
38 struct ifnet *ifp;
39 struct mbuf *m;
40 int s;
41
42 /*
43 * First pull off all the phase 2 packets.
44 */
45 for (;;) {
46 s = splimp();
47
48 IF_DEQUEUE( &atintrq2, m );
49
50 splx( s );
51
52 if ( m == 0 ) { /* no more queued packets */
53 break;
54 }
55
56 ifp = m->m_pkthdr.rcvif;
57 ddp_input( m, ifp, (struct elaphdr *)NULL, 2 );
58 }
59
60 /*
61 * Then pull off all the phase 1 packets.
62 */
63 for (;;) {
64 s = splimp();
65
66 IF_DEQUEUE( &atintrq1, m );
67
68 splx( s );
69
70 if ( m == 0 ) { /* no more queued packets */
71 break;
72 }
73
74 ifp = m->m_pkthdr.rcvif;
75
76 if ( m->m_len < SZ_ELAPHDR &&
77 (( m = m_pullup( m, SZ_ELAPHDR )) == 0 )) {
78 ddpstat.ddps_tooshort++;
79 continue;
80 }
81
82 /*
83 * this seems a little dubios, but I don't know phase 1 so leave it.
84 */
85 elhp = mtod( m, struct elaphdr *);
86 m_adj( m, SZ_ELAPHDR );
87
88 if ( elhp->el_type == ELAP_DDPEXTEND ) {
89 ddp_input( m, ifp, (struct elaphdr *)NULL, 1 );
90 } else {
91 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR );
92 ddp_input( m, ifp, &elh, 1 );
93 }
94 }
95 return;
96 }
97
98 NETISR_SET(NETISR_ATALK, atintr);
99
100 static void
101 ddp_input( m, ifp, elh, phase )
102 struct mbuf *m;
103 struct ifnet *ifp;
104 struct elaphdr *elh;
105 int phase;
106 {
107 struct sockaddr_at from, to;
108 struct ddpshdr *dsh, ddps;
109 struct at_ifaddr *aa;
110 struct ddpehdr *deh = NULL, ddpe;
111 struct ddpcb *ddp;
112 int dlen, mlen;
113 u_short cksum = 0;
114
115 bzero( (caddr_t)&from, sizeof( struct sockaddr_at ));
116 bzero( (caddr_t)&to, sizeof( struct sockaddr_at ));
117 if ( elh ) {
118 /*
119 * Extract the information in the short header.
120 * netowrk information is defaulted to ATADDR_ANYNET
121 * and node information comes from the elh info.
122 * We must be phase 1.
123 */
124 ddpstat.ddps_short++;
125
126 if ( m->m_len < sizeof( struct ddpshdr ) &&
127 (( m = m_pullup( m, sizeof( struct ddpshdr ))) == 0 )) {
128 ddpstat.ddps_tooshort++;
129 return;
130 }
131
132 dsh = mtod( m, struct ddpshdr *);
133 bcopy( (caddr_t)dsh, (caddr_t)&ddps, sizeof( struct ddpshdr ));
134 ddps.dsh_bytes = ntohl( ddps.dsh_bytes );
135 dlen = ddps.dsh_len;
136
137 to.sat_addr.s_net = ATADDR_ANYNET;
138 to.sat_addr.s_node = elh->el_dnode;
139 to.sat_port = ddps.dsh_dport;
140 from.sat_addr.s_net = ATADDR_ANYNET;
141 from.sat_addr.s_node = elh->el_snode;
142 from.sat_port = ddps.dsh_sport;
143
144 /*
145 * Make sure that we point to the phase1 ifaddr info
146 * and that it's valid for this packet.
147 */
148 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
149 if ( (aa->aa_ifp == ifp)
150 && ( (aa->aa_flags & AFA_PHASE2) == 0)
151 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
152 || (to.sat_addr.s_node == ATADDR_BCAST))) {
153 break;
154 }
155 }
156 /*
157 * maybe we got a broadcast not meant for us.. ditch it.
158 */
159 if ( aa == NULL ) {
160 m_freem( m );
161 return;
162 }
163 } else {
164 /*
165 * There was no 'elh' passed on. This could still be
166 * either phase1 or phase2.
167 * We have a long header, but we may be running on a phase 1 net.
168 * Extract out all the info regarding this packet's src & dst.
169 */
170 ddpstat.ddps_long++;
171
172 if ( m->m_len < sizeof( struct ddpehdr ) &&
173 (( m = m_pullup( m, sizeof( struct ddpehdr ))) == 0 )) {
174 ddpstat.ddps_tooshort++;
175 return;
176 }
177
178 deh = mtod( m, struct ddpehdr *);
179 bcopy( (caddr_t)deh, (caddr_t)&ddpe, sizeof( struct ddpehdr ));
180 ddpe.deh_bytes = ntohl( ddpe.deh_bytes );
181 dlen = ddpe.deh_len;
182
183 if (( cksum = ddpe.deh_sum ) == 0 ) {
184 ddpstat.ddps_nosum++;
185 }
186
187 from.sat_addr.s_net = ddpe.deh_snet;
188 from.sat_addr.s_node = ddpe.deh_snode;
189 from.sat_port = ddpe.deh_sport;
190 to.sat_addr.s_net = ddpe.deh_dnet;
191 to.sat_addr.s_node = ddpe.deh_dnode;
192 to.sat_port = ddpe.deh_dport;
193
194 if ( to.sat_addr.s_net == ATADDR_ANYNET ) {
195 /*
196 * The TO address doesn't specify a net,
197 * So by definition it's for this net.
198 * Try find ifaddr info with the right phase,
199 * the right interface, and either to our node, a broadcast,
200 * or looped back (though that SHOULD be covered in the other
201 * cases).
202 *
203 * XXX If we have multiple interfaces, then the first with
204 * this node number will match (which may NOT be what we want,
205 * but it's probably safe in 99.999% of cases.
206 */
207 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
208 if ( phase == 1 && ( aa->aa_flags & AFA_PHASE2 )) {
209 continue;
210 }
211 if ( phase == 2 && ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
212 continue;
213 }
214 if ( (aa->aa_ifp == ifp)
215 && ( (to.sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node)
216 || (to.sat_addr.s_node == ATADDR_BCAST)
217 || (ifp->if_flags & IFF_LOOPBACK))) {
218 break;
219 }
220 }
221 } else {
222 /*
223 * A destination network was given. We just try to find
224 * which ifaddr info matches it.
225 */
226 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
227 /*
228 * This is a kludge. Accept packets that are
229 * for any router on a local netrange.
230 */
231 if ( to.sat_addr.s_net == aa->aa_firstnet &&
232 to.sat_addr.s_node == 0 ) {
233 break;
234 }
235 /*
236 * Don't use ifaddr info for which we are totally outside the
237 * netrange, and it's not a startup packet.
238 * Startup packets are always implicitly allowed on to
239 * the next test.
240 */
241 if ((( ntohs( to.sat_addr.s_net ) < ntohs( aa->aa_firstnet ))
242 || (ntohs( to.sat_addr.s_net ) > ntohs( aa->aa_lastnet )))
243 && (( ntohs( to.sat_addr.s_net ) < 0xff00)
244 || (ntohs( to.sat_addr.s_net ) > 0xfffe ))) {
245 continue;
246 }
247
248 /*
249 * Don't record a match either if we just don't have a match
250 * in the node address. This can have if the interface
251 * is in promiscuous mode for example.
252 */
253 if (( to.sat_addr.s_node != AA_SAT( aa )->sat_addr.s_node)
254 && (to.sat_addr.s_node != ATADDR_BCAST) ) {
255 continue;
256 }
257 break;
258 }
259 }
260 }
261
262 /*
263 * Adjust the length, removing any padding that may have been added
264 * at a link layer. We do this before we attempt to forward a packet,
265 * possibly on a different media.
266 */
267 mlen = m->m_pkthdr.len;
268 if ( mlen < dlen ) {
269 ddpstat.ddps_toosmall++;
270 m_freem( m );
271 return;
272 }
273 if ( mlen > dlen ) {
274 m_adj( m, dlen - mlen );
275 }
276
277 /*
278 * If it aint for a net on any of our interfaces,
279 * or it IS for a net on a different interface than it came in on,
280 * (and it is not looped back) then consider if we should forward it.
281 * As we are not really a router this is a bit cheeky, but it may be
282 * useful some day.
283 */
284 if ( (aa == NULL)
285 || ( (to.sat_addr.s_node == ATADDR_BCAST)
286 && (aa->aa_ifp != ifp)
287 && (( ifp->if_flags & IFF_LOOPBACK ) == 0 ))) {
288 /*
289 * If we've explicitly disabled it, don't route anything
290 */
291 if ( ddp_forward == 0 ) {
292 m_freem( m );
293 return;
294 }
295 /*
296 * If the cached forwarding route is still valid, use it.
297 */
298 if ( forwro.ro_rt
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 )) {
301 RTFREE( forwro.ro_rt );
302 forwro.ro_rt = (struct rtentry *)0;
303 }
304
305 /*
306 * If we don't have a cached one (any more) or it's useless,
307 * Then get a new route.
308 * XXX this could cause a 'route leak'. check this!
309 */
310 if ( forwro.ro_rt == (struct rtentry *)0
311 || forwro.ro_rt->rt_ifp == (struct ifnet *)0 ) {
312 forwro.ro_dst.sa_len = sizeof( struct sockaddr_at );
313 forwro.ro_dst.sa_family = AF_APPLETALK;
314 satosat(&forwro.ro_dst)->sat_addr.s_net = to.sat_addr.s_net;
315 satosat(&forwro.ro_dst)->sat_addr.s_node = to.sat_addr.s_node;
316 rtalloc(&forwro);
317 }
318
319 /*
320 * If it's not going to get there on this hop, and it's
321 * already done too many hops, then throw it away.
322 */
323 if ( (to.sat_addr.s_net != satosat( &forwro.ro_dst )->sat_addr.s_net)
324 && (ddpe.deh_hops == DDP_MAXHOPS) ) {
325 m_freem( m );
326 return;
327 }
328
329 /*
330 * A ddp router might use the same interface
331 * to forward the packet, which this would not effect.
332 * Don't allow packets to cross from one interface to another however.
333 */
334 if ( ddp_firewall
335 && ( (forwro.ro_rt == NULL)
336 || (forwro.ro_rt->rt_ifp != ifp))) {
337 m_freem( m );
338 return;
339 }
340
341 /*
342 * Adjust the header.
343 * If it was a short header then it would have not gotten here,
344 * so we can assume there is room to drop the header in.
345 * XXX what about promiscuous mode, etc...
346 */
347 ddpe.deh_hops++;
348 ddpe.deh_bytes = htonl( ddpe.deh_bytes );
349 bcopy( (caddr_t)&ddpe, (caddr_t)deh, sizeof( u_short )); /* XXX deh? */
350 if ( ddp_route( m, &forwro )) {
351 ddpstat.ddps_cantforward++;
352 } else {
353 ddpstat.ddps_forward++;
354 }
355 return;
356 }
357
358 /*
359 * It was for us, and we have an ifaddr to use with it.
360 */
361 from.sat_len = sizeof( struct sockaddr_at );
362 from.sat_family = AF_APPLETALK;
363
364 /*
365 * We are no longer interested in the link layer.
366 * so cut it off.
367 */
368 if ( elh ) {
369 m_adj( m, sizeof( struct ddpshdr ));
370 } else {
371 if ( ddp_cksum && cksum && cksum != at_cksum( m, sizeof( int ))) {
372 ddpstat.ddps_badsum++;
373 m_freem( m );
374 return;
375 }
376 m_adj( m, sizeof( struct ddpehdr ));
377 }
378
379 /*
380 * Search for ddp protocol control blocks that match these
381 * addresses.
382 */
383 if (( ddp = ddp_search( &from, &to, aa )) == NULL ) {
384 m_freem( m );
385 return;
386 }
387
388 /*
389 * If we found one, deliver th epacket to the socket
390 */
391 if ( sbappendaddr( &ddp->ddp_socket->so_rcv, (struct sockaddr *)&from,
392 m, (struct mbuf *)0 ) == 0 ) {
393 /*
394 * If the socket is full (or similar error) dump the packet.
395 */
396 ddpstat.ddps_nosockspace++;
397 m_freem( m );
398 return;
399 }
400 /*
401 * And wake up whatever might be waiting for it
402 */
403 sorwakeup( ddp->ddp_socket );
404 }
405
406 #if 0
407 /* As if we haven't got enough of this sort of think floating
408 around the kernel :) */
409
410 #define BPXLEN 48
411 #define BPALEN 16
412 #include <ctype.h>
413 char hexdig[] = "0123456789ABCDEF";
414
415 static void
416 bprint( char *data, int len )
417 {
418 char xout[ BPXLEN ], aout[ BPALEN ];
419 int i = 0;
420
421 bzero( xout, BPXLEN );
422 bzero( aout, BPALEN );
423
424 for ( ;; ) {
425 if ( len < 1 ) {
426 if ( i != 0 ) {
427 printf( "%s\t%s\n", xout, aout );
428 }
429 printf( "%s\n", "(end)" );
430 break;
431 }
432
433 xout[ (i*3) ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
434 xout[ (i*3) + 1 ] = hexdig[ *data & 0x0f ];
435
436 if ( (u_char)*data < 0x7f && (u_char)*data > 0x20 ) {
437 aout[ i ] = *data;
438 } else {
439 aout[ i ] = '.';
440 }
441
442 xout[ (i*3) + 2 ] = ' ';
443
444 i++;
445 len--;
446 data++;
447
448 if ( i > BPALEN - 2 ) {
449 printf( "%s\t%s\n", xout, aout );
450 bzero( xout, BPXLEN );
451 bzero( aout, BPALEN );
452 i = 0;
453 continue;
454 }
455 }
456 }
457
458 static void
459 m_printm( struct mbuf *m )
460 {
461 for (; m; m = m->m_next ) {
462 bprint( mtod( m, char * ), m->m_len );
463 }
464 }
465 #endif
Cache object: 1c92ff19adcbd08a6dba0aeebc0e86f1
|