ddp_input.c revision 1.18 1 /* $NetBSD: ddp_input.c,v 1.18 2008/04/23 15:17:42 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appears in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation, and that the name of The University
12 * of Michigan not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. This software is supplied as is without expressed or
15 * implied warranties of any kind.
16 *
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 *
20 * Research Systems Unix Group
21 * The University of Michigan
22 * c/o Wesley Craig
23 * 535 W. William Street
24 * Ann Arbor, Michigan
25 * +1-313-764-2278
26 * netatalk (at) umich.edu
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ddp_input.c,v 1.18 2008/04/23 15:17:42 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <net/netisr.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/syslog.h>
40 #include <net/if.h>
41 #include <net/route.h>
42 #include <net/if_ether.h>
43 #include <netinet/in.h>
44
45 #include <netatalk/at.h>
46 #include <netatalk/at_var.h>
47 #include <netatalk/ddp.h>
48 #include <netatalk/ddp_var.h>
49 #include <netatalk/ddp_private.h>
50 #include <netatalk/at_extern.h>
51
52 int ddp_forward = 1;
53 int ddp_firewall = 0;
54 extern int ddp_cksum;
55 void ddp_input __P((struct mbuf *, struct ifnet *,
56 struct elaphdr *, int));
57
58 /*
59 * Could probably merge these two code segments a little better...
60 */
61 void
62 atintr()
63 {
64 struct elaphdr *elhp, elh;
65 struct ifnet *ifp;
66 struct mbuf *m;
67 struct at_ifaddr *aa;
68 int s;
69
70 for (;;) {
71 s = splnet();
72
73 IF_DEQUEUE(&atintrq2, m);
74
75 splx(s);
76
77 if (m == 0) /* no more queued packets */
78 break;
79
80 m_claimm(m, &atalk_rx_mowner);
81 ifp = m->m_pkthdr.rcvif;
82 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
83 if (aa->aa_ifp == ifp && (aa->aa_flags & AFA_PHASE2))
84 break;
85 }
86 if (aa == NULL) { /* ifp not an appletalk interface */
87 m_freem(m);
88 continue;
89 }
90 ddp_input(m, ifp, (struct elaphdr *) NULL, 2);
91 }
92
93 for (;;) {
94 s = splnet();
95
96 IF_DEQUEUE(&atintrq1, m);
97
98 splx(s);
99
100 if (m == 0) /* no more queued packets */
101
102 break;
103
104 m_claimm(m, &atalk_rx_mowner);
105 ifp = m->m_pkthdr.rcvif;
106 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
107 if (aa->aa_ifp == ifp &&
108 (aa->aa_flags & AFA_PHASE2) == 0)
109 break;
110 }
111 if (aa == NULL) { /* ifp not an appletalk interface */
112 m_freem(m);
113 continue;
114 }
115 if (m->m_len < SZ_ELAPHDR &&
116 ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
117 DDP_STATINC(DDP_STAT_TOOSHORT);
118 continue;
119 }
120 elhp = mtod(m, struct elaphdr *);
121 m_adj(m, SZ_ELAPHDR);
122
123 if (elhp->el_type == ELAP_DDPEXTEND) {
124 ddp_input(m, ifp, (struct elaphdr *) NULL, 1);
125 } else {
126 bcopy((void *) elhp, (void *) & elh, SZ_ELAPHDR);
127 ddp_input(m, ifp, &elh, 1);
128 }
129 }
130 }
131
132 struct route forwro;
133
134 void
135 ddp_input(m, ifp, elh, phase)
136 struct mbuf *m;
137 struct ifnet *ifp;
138 struct elaphdr *elh;
139 int phase;
140 {
141 struct rtentry *rt;
142 struct sockaddr_at from, to;
143 struct ddpshdr *dsh, ddps;
144 struct at_ifaddr *aa;
145 struct ddpehdr *deh = NULL, ddpe;
146 struct ddpcb *ddp;
147 int dlen, mlen;
148 u_short cksum = 0;
149 union {
150 struct sockaddr dst;
151 struct sockaddr_at dsta;
152 } u;
153
154 bzero((void *) & from, sizeof(struct sockaddr_at));
155 if (elh) {
156 DDP_STATINC(DDP_STAT_SHORT);
157
158 if (m->m_len < sizeof(struct ddpshdr) &&
159 ((m = m_pullup(m, sizeof(struct ddpshdr))) == 0)) {
160 DDP_STATINC(DDP_STAT_TOOSHORT);
161 return;
162 }
163 dsh = mtod(m, struct ddpshdr *);
164 bcopy((void *) dsh, (void *) & ddps, sizeof(struct ddpshdr));
165 ddps.dsh_bytes = ntohl(ddps.dsh_bytes);
166 dlen = ddps.dsh_len;
167
168 to.sat_addr.s_net = ATADDR_ANYNET;
169 to.sat_addr.s_node = elh->el_dnode;
170 to.sat_port = ddps.dsh_dport;
171 from.sat_addr.s_net = ATADDR_ANYNET;
172 from.sat_addr.s_node = elh->el_snode;
173 from.sat_port = ddps.dsh_sport;
174
175 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
176 if (aa->aa_ifp == ifp &&
177 (aa->aa_flags & AFA_PHASE2) == 0 &&
178 (AA_SAT(aa)->sat_addr.s_node ==
179 to.sat_addr.s_node ||
180 to.sat_addr.s_node == ATADDR_BCAST))
181 break;
182 }
183 if (aa == NULL) {
184 m_freem(m);
185 return;
186 }
187 } else {
188 DDP_STATINC(DDP_STAT_LONG);
189
190 if (m->m_len < sizeof(struct ddpehdr) &&
191 ((m = m_pullup(m, sizeof(struct ddpehdr))) == 0)) {
192 DDP_STATINC(DDP_STAT_TOOSHORT);
193 return;
194 }
195 deh = mtod(m, struct ddpehdr *);
196 bcopy((void *) deh, (void *) & ddpe, sizeof(struct ddpehdr));
197 ddpe.deh_bytes = ntohl(ddpe.deh_bytes);
198 dlen = ddpe.deh_len;
199
200 if ((cksum = ddpe.deh_sum) == 0) {
201 DDP_STATINC(DDP_STAT_NOSUM);
202 }
203 from.sat_addr.s_net = ddpe.deh_snet;
204 from.sat_addr.s_node = ddpe.deh_snode;
205 from.sat_port = ddpe.deh_sport;
206 to.sat_addr.s_net = ddpe.deh_dnet;
207 to.sat_addr.s_node = ddpe.deh_dnode;
208 to.sat_port = ddpe.deh_dport;
209
210 if (to.sat_addr.s_net == ATADDR_ANYNET) {
211 for (aa = at_ifaddr.tqh_first; aa;
212 aa = aa->aa_list.tqe_next) {
213 if (phase == 1 && (aa->aa_flags & AFA_PHASE2))
214 continue;
215
216 if (phase == 2 &&
217 (aa->aa_flags & AFA_PHASE2) == 0)
218 continue;
219
220 if (aa->aa_ifp == ifp &&
221 (AA_SAT(aa)->sat_addr.s_node ==
222 to.sat_addr.s_node ||
223 to.sat_addr.s_node == ATADDR_BCAST ||
224 (ifp->if_flags & IFF_LOOPBACK)))
225 break;
226 }
227 } else {
228 for (aa = at_ifaddr.tqh_first; aa;
229 aa = aa->aa_list.tqe_next) {
230 if (to.sat_addr.s_net == aa->aa_firstnet &&
231 to.sat_addr.s_node == 0)
232 break;
233
234 if ((ntohs(to.sat_addr.s_net) <
235 ntohs(aa->aa_firstnet) ||
236 ntohs(to.sat_addr.s_net) >
237 ntohs(aa->aa_lastnet)) &&
238 (ntohs(to.sat_addr.s_net) < 0xff00 ||
239 ntohs(to.sat_addr.s_net) > 0xfffe))
240 continue;
241
242 if (to.sat_addr.s_node !=
243 AA_SAT(aa)->sat_addr.s_node &&
244 to.sat_addr.s_node != ATADDR_BCAST)
245 continue;
246
247 break;
248 }
249 }
250 }
251
252 /*
253 * Adjust the length, removing any padding that may have been added
254 * at a link layer. We do this before we attempt to forward a packet,
255 * possibly on a different media.
256 */
257 mlen = m->m_pkthdr.len;
258 if (mlen < dlen) {
259 DDP_STATINC(DDP_STAT_TOOSMALL);
260 m_freem(m);
261 return;
262 }
263 if (mlen > dlen) {
264 m_adj(m, dlen - mlen);
265 }
266 /*
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.
269 */
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) {
273 m_freem(m);
274 return;
275 }
276 sockaddr_at_init(&u.dsta, &to.sat_addr, 0);
277 rt = rtcache_lookup(&forwro, &u.dst);
278 #if 0 /* XXX The if-condition is always false. What was this
279 * actually trying to test?
280 */
281 if (to.sat_addr.s_net !=
282 satocsat(rtcache_getdst(&forwro))->sat_addr.s_net &&
283 ddpe.deh_hops == DDP_MAXHOPS) {
284 m_freem(m);
285 return;
286 }
287 #endif
288 if (ddp_firewall && (rt == NULL || rt->rt_ifp != ifp)) {
289 m_freem(m);
290 return;
291 }
292 ddpe.deh_hops++;
293 ddpe.deh_bytes = htonl(ddpe.deh_bytes);
294 bcopy((void *) & ddpe, (void *) deh, sizeof(u_short));/*XXX*/
295 if (ddp_route(m, &forwro)) {
296 DDP_STATINC(DDP_STAT_CANTFORWARD);
297 } else {
298 DDP_STATINC(DDP_STAT_FORWARD);
299 }
300 return;
301 }
302 from.sat_len = sizeof(struct sockaddr_at);
303 from.sat_family = AF_APPLETALK;
304
305 if (elh) {
306 m_adj(m, sizeof(struct ddpshdr));
307 } else {
308 if (ddp_cksum && cksum && cksum != at_cksum(m, sizeof(int))) {
309 DDP_STATINC(DDP_STAT_BADSUM);
310 m_freem(m);
311 return;
312 }
313 m_adj(m, sizeof(struct ddpehdr));
314 }
315
316 if ((ddp = ddp_search(&from, &to, aa)) == NULL) {
317 m_freem(m);
318 return;
319 }
320 if (sbappendaddr(&ddp->ddp_socket->so_rcv, (struct sockaddr *) & from,
321 m, (struct mbuf *) 0) == 0) {
322 DDP_STATINC(DDP_STAT_NOSOCKSPACE);
323 m_freem(m);
324 return;
325 }
326 #if IFA_STATS
327 if (aa)
328 aa->aa_ifa.ifa_data.ifad_inbytes += dlen;
329 #endif
330 sorwakeup(ddp->ddp_socket);
331 }
332
333 #if 0
334
335 #define BPXLEN 48
336 #define BPALEN 16
337 #include <ctype.h>
338
339 static void
340 bprint(data, len)
341 char *data;
342 int len;
343 {
344 char xout[BPXLEN], aout[BPALEN];
345 int i = 0;
346
347 bzero(xout, BPXLEN);
348 bzero(aout, BPALEN);
349
350 for (;;) {
351 if (len < 1) {
352 if (i != 0) {
353 printf("%s\t%s\n", xout, aout);
354 }
355 printf("%s\n", "(end)");
356 break;
357 }
358 xout[(i * 3)] = hexdigits[(*data & 0xf0) >> 4];
359 xout[(i * 3) + 1] = hexdigits[*data & 0x0f];
360
361 if ((u_char) * data < 0x7f && (u_char) * data > 0x20) {
362 aout[i] = *data;
363 } else {
364 aout[i] = '.';
365 }
366
367 xout[(i * 3) + 2] = ' ';
368
369 i++;
370 len--;
371 data++;
372
373 if (i > BPALEN - 2) {
374 printf("%s\t%s\n", xout, aout);
375 bzero(xout, BPXLEN);
376 bzero(aout, BPALEN);
377 i = 0;
378 continue;
379 }
380 }
381 }
382
383 static void
384 m_printm(m)
385 struct mbuf *m;
386 {
387 for (; m; m = m->m_next)
388 bprint(mtod(m, char *), m->m_len);
389 }
390 #endif
391