]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netatalk/ddp_input.c
Similar to r261991, for compiling the GENERIC kernel on sparc64,
[FreeBSD/FreeBSD.git] / sys / netatalk / ddp_input.c
1 /*-
2  * Copyright (c) 2004-2009 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * Copyright (c) 1990, 1994 Regents of The University of Michigan.
27  *
28  * Permission to use, copy, modify, and distribute this software and
29  * its documentation for any purpose and without fee is hereby granted,
30  * provided that the above copyright notice appears in all copies and
31  * that both that copyright notice and this permission notice appear
32  * in supporting documentation, and that the name of The University
33  * of Michigan not be used in advertising or publicity pertaining to
34  * distribution of the software without specific, written prior
35  * permission. This software is supplied as is without expressed or
36  * implied warranties of any kind.
37  *
38  * This product includes software developed by the University of
39  * California, Berkeley and its contributors.
40  *
41  *      Research Systems Unix Group
42  *      The University of Michigan
43  *      c/o Wesley Craig
44  *      535 W. William Street
45  *      Ann Arbor, Michigan
46  *      +1-313-764-2278
47  *      netatalk@umich.edu
48  *
49  * $FreeBSD$
50  */
51
52 #include <sys/param.h>
53 #include <sys/kernel.h>
54 #include <sys/lock.h>
55 #include <sys/mbuf.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sx.h>
60 #include <sys/systm.h>
61 #include <net/if.h>
62 #include <net/if_var.h>
63 #include <net/route.h>
64
65 #include <netatalk/at.h>
66 #include <netatalk/at_var.h>
67 #include <netatalk/ddp.h>
68 #include <netatalk/ddp_var.h>
69 #include <netatalk/ddp_pcb.h>
70 #include <netatalk/at_extern.h>
71
72 #include <security/mac/mac_framework.h>
73
74 static volatile int     ddp_forward = 1;
75 static volatile int     ddp_firewall = 0;
76 static struct ddpstat   ddpstat;
77
78 static struct route     forwro;
79
80 static void     ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int);
81
82 /*
83  * Could probably merge these two code segments a little better...
84  */
85 void
86 at2intr(struct mbuf *m)
87 {
88
89         /*
90          * Phase 2 packet handling .
91          */
92         ddp_input(m, m->m_pkthdr.rcvif, NULL, 2);
93 }
94
95 void
96 at1intr(struct mbuf *m)
97 {
98         struct elaphdr *elhp, elh;
99
100         /*
101          * Phase 1 packet handling 
102          */
103         if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) ==
104             NULL)) {
105                 ddpstat.ddps_tooshort++;
106                 return;
107         }
108
109         /*
110          * This seems a little dubious, but I don't know phase 1 so leave it.
111          */
112         elhp = mtod(m, struct elaphdr *);
113         m_adj(m, SZ_ELAPHDR);
114
115         if (elhp->el_type != ELAP_DDPEXTEND) {
116                 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR);
117                 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1);
118         } else
119                 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1);
120 }
121
122 static void
123 ddp_input(struct mbuf *m, struct ifnet *ifp, struct elaphdr *elh, int phase)
124 {
125         struct sockaddr_at from, to;
126         struct ddpshdr *dsh, ddps;
127         struct at_ifaddr *aa;
128         struct ddpehdr *deh = NULL, ddpe;
129         struct ddpcb *ddp;
130         int dlen, mlen;
131         u_short cksum = 0;
132
133         bzero((caddr_t)&from, sizeof(struct sockaddr_at));
134         bzero((caddr_t)&to, sizeof(struct sockaddr_at));
135         if (elh != NULL) {
136                 /*
137                  * Extract the information in the short header.  Network
138                  * information is defaulted to ATADDR_ANYNET and node
139                  * information comes from the elh info.  We must be phase 1.
140                  */
141                 ddpstat.ddps_short++;
142
143                 if (m->m_len < sizeof(struct ddpshdr) &&
144                     ((m = m_pullup(m, sizeof(struct ddpshdr))) == NULL)) {
145                         ddpstat.ddps_tooshort++;
146                         return;
147                 }
148
149                 dsh = mtod(m, struct ddpshdr *);
150                 bcopy((caddr_t)dsh, (caddr_t)&ddps, sizeof(struct ddpshdr));
151                 ddps.dsh_bytes = ntohl(ddps.dsh_bytes);
152                 dlen = ddps.dsh_len;
153
154                 to.sat_addr.s_net = ATADDR_ANYNET;
155                 to.sat_addr.s_node = elh->el_dnode;
156                 to.sat_port = ddps.dsh_dport;
157                 from.sat_addr.s_net = ATADDR_ANYNET;
158                 from.sat_addr.s_node = elh->el_snode;
159                 from.sat_port = ddps.dsh_sport;
160
161                 /* 
162                  * Make sure that we point to the phase1 ifaddr info and that
163                  * it's valid for this packet.
164                  */
165                 AT_IFADDR_RLOCK();
166                 TAILQ_FOREACH(aa, &at_ifaddrhead, aa_link) {
167                         if ((aa->aa_ifp == ifp)
168                             && ((aa->aa_flags & AFA_PHASE2) == 0)
169                             && ((to.sat_addr.s_node ==
170                             AA_SAT(aa)->sat_addr.s_node) ||
171                             (to.sat_addr.s_node == ATADDR_BCAST)))
172                                 break;
173                 }
174                 /* 
175                  * maybe we got a broadcast not meant for us.. ditch it.
176                  */
177                 if (aa == NULL) {
178                         AT_IFADDR_RUNLOCK();
179                         m_freem(m);
180                         return;
181                 }
182         } else {
183                 /*
184                  * There was no 'elh' passed on. This could still be either
185                  * phase1 or phase2.  We have a long header, but we may be
186                  * running on a phase 1 net.  Extract out all the info
187                  * regarding this packet's src & dst.
188                  */
189                 ddpstat.ddps_long++;
190
191                 if (m->m_len < sizeof(struct ddpehdr) &&
192                     ((m = m_pullup(m, sizeof(struct ddpehdr))) == NULL)) {
193                         AT_IFADDR_RUNLOCK();
194                         ddpstat.ddps_tooshort++;
195                         return;
196                 }
197
198                 deh = mtod(m, struct ddpehdr *);
199                 bcopy((caddr_t)deh, (caddr_t)&ddpe, sizeof(struct ddpehdr));
200                 ddpe.deh_bytes = ntohl(ddpe.deh_bytes);
201                 dlen = ddpe.deh_len;
202
203                 if ((cksum = ddpe.deh_sum) == 0)
204                         ddpstat.ddps_nosum++;
205
206                 from.sat_addr.s_net = ddpe.deh_snet;
207                 from.sat_addr.s_node = ddpe.deh_snode;
208                 from.sat_port = ddpe.deh_sport;
209                 to.sat_addr.s_net = ddpe.deh_dnet;
210                 to.sat_addr.s_node = ddpe.deh_dnode;
211                 to.sat_port = ddpe.deh_dport;
212
213                 AT_IFADDR_RLOCK();
214                 if (to.sat_addr.s_net == ATADDR_ANYNET) {
215                         /*
216                          * The TO address doesn't specify a net, so by
217                          * definition it's for this net.  Try find ifaddr
218                          * info with the right phase, the right interface,
219                          * and either to our node, a broadcast, or looped
220                          * back (though that SHOULD be covered in the other
221                          * cases).
222                          *
223                          * XXX If we have multiple interfaces, then the first
224                          * with this node number will match (which may NOT be
225                          * what we want, but it's probably safe in 99.999% of
226                          * cases.
227                          */
228                         TAILQ_FOREACH(aa, &at_ifaddrhead, aa_link) {
229                                 if (phase == 1 && (aa->aa_flags &
230                                     AFA_PHASE2))
231                                         continue;
232                                 if (phase == 2 && (aa->aa_flags &
233                                     AFA_PHASE2) == 0)
234                                         continue;
235                                 if ((aa->aa_ifp == ifp) &&
236                                     ((to.sat_addr.s_node ==
237                                     AA_SAT(aa)->sat_addr.s_node) ||
238                                     (to.sat_addr.s_node == ATADDR_BCAST) ||
239                                     (ifp->if_flags & IFF_LOOPBACK)))
240                                         break;
241                         }
242                 } else {
243                         /* 
244                          * A destination network was given.  We just try to
245                          * find which ifaddr info matches it.
246                          */
247                         TAILQ_FOREACH(aa, &at_ifaddrhead, aa_link) {
248                                 /*
249                                  * This is a kludge. Accept packets that are
250                                  * for any router on a local netrange.
251                                  */
252                                 if (to.sat_addr.s_net == aa->aa_firstnet &&
253                                     to.sat_addr.s_node == 0)
254                                         break;
255                                 /*
256                                  * Don't use ifaddr info for which we are
257                                  * totally outside the netrange, and it's not
258                                  * a startup packet.  Startup packets are
259                                  * always implicitly allowed on to the next
260                                  * test.
261                                  */
262                                 if (((ntohs(to.sat_addr.s_net) <
263                                     ntohs(aa->aa_firstnet)) ||
264                                     (ntohs(to.sat_addr.s_net) >
265                                     ntohs(aa->aa_lastnet))) &&
266                                     ((ntohs(to.sat_addr.s_net) < 0xff00) ||
267                                     (ntohs(to.sat_addr.s_net) > 0xfffe)))
268                                         continue;
269
270                                 /*
271                                  * Don't record a match either if we just
272                                  * don't have a match in the node address.
273                                  * This can have if the interface is in
274                                  * promiscuous mode for example.
275                                  */
276                                 if ((to.sat_addr.s_node !=
277                                     AA_SAT(aa)->sat_addr.s_node) &&
278                                     (to.sat_addr.s_node != ATADDR_BCAST))
279                                         continue;
280                                 break;
281                         }
282                 }
283         }
284         if (aa != NULL)
285                 ifa_ref(&aa->aa_ifa);
286         AT_IFADDR_RUNLOCK();
287
288         /*
289          * Adjust the length, removing any padding that may have been added
290          * at a link layer.  We do this before we attempt to forward a
291          * packet, possibly on a different media.
292          */
293         mlen = m->m_pkthdr.len;
294         if (mlen < dlen) {
295                 ddpstat.ddps_toosmall++;
296                 goto out;
297         }
298         if (mlen > dlen)
299                 m_adj(m, dlen - mlen);
300
301         /*
302          * If it isn't for a net on any of our interfaces, or it IS for a net
303          * on a different interface than it came in on, (and it is not looped
304          * back) then consider if we should forward it.  As we are not really
305          * a router this is a bit cheeky, but it may be useful some day.
306          */
307         if ((aa == NULL) || ((to.sat_addr.s_node == ATADDR_BCAST) &&
308             (aa->aa_ifp != ifp) && ((ifp->if_flags & IFF_LOOPBACK) == 0))) {
309                 /* 
310                  * If we've explicitly disabled it, don't route anything.
311                  */
312                 if (ddp_forward == 0)
313                         goto out;
314
315                 /* 
316                  * If the cached forwarding route is still valid, use it.
317                  *
318                  * XXXRW: Access to the cached route may not be properly
319                  * synchronized for parallel input handling.
320                  */
321                 if (forwro.ro_rt &&
322                     (satosat(&forwro.ro_dst)->sat_addr.s_net !=
323                     to.sat_addr.s_net ||
324                     satosat(&forwro.ro_dst)->sat_addr.s_node !=
325                     to.sat_addr.s_node)) {
326                         RTFREE(forwro.ro_rt);
327                         forwro.ro_rt = NULL;
328                 }
329
330                 /*
331                  * If we don't have a cached one (any more) or it's useless,
332                  * then get a new route.
333                  *
334                  * XXX this could cause a 'route leak'.  Check this!
335                  */
336                 if (forwro.ro_rt == NULL || forwro.ro_rt->rt_ifp == NULL) {
337                         forwro.ro_dst.sa_len = sizeof(struct sockaddr_at);
338                         forwro.ro_dst.sa_family = AF_APPLETALK;
339                         satosat(&forwro.ro_dst)->sat_addr.s_net =
340                             to.sat_addr.s_net;
341                         satosat(&forwro.ro_dst)->sat_addr.s_node =
342                             to.sat_addr.s_node;
343                         rtalloc(&forwro);
344                 }
345
346                 /* 
347                  * If it's not going to get there on this hop, and it's
348                  * already done too many hops, then throw it away.
349                  */
350                 if ((to.sat_addr.s_net !=
351                     satosat(&forwro.ro_dst)->sat_addr.s_net) &&
352                     (ddpe.deh_hops == DDP_MAXHOPS))
353                         goto out;
354
355                 /*
356                  * A ddp router might use the same interface to forward the
357                  * packet, which this would not effect.  Don't allow packets
358                  * to cross from one interface to another however.
359                  */
360                 if (ddp_firewall && ((forwro.ro_rt == NULL) ||
361                     (forwro.ro_rt->rt_ifp != ifp)))
362                         goto out;
363
364                 /*
365                  * Adjust the header.  If it was a short header then it would
366                  * have not gotten here, so we can assume there is room to
367                  * drop the header in.
368                  *
369                  * XXX what about promiscuous mode, etc...
370                  */
371                 ddpe.deh_hops++;
372                 ddpe.deh_bytes = htonl(ddpe.deh_bytes);
373                 /* XXX deh? */
374                 bcopy((caddr_t)&ddpe, (caddr_t)deh, sizeof(u_short));
375                 if (ddp_route(m, &forwro))
376                         ddpstat.ddps_cantforward++;
377                 else
378                         ddpstat.ddps_forward++;
379                 if (aa != NULL)
380                         ifa_free(&aa->aa_ifa);
381                 return;
382         }
383
384         /*
385          * It was for us, and we have an ifaddr to use with it.
386          */
387         from.sat_len = sizeof(struct sockaddr_at);
388         from.sat_family = AF_APPLETALK;
389
390         /* 
391          * We are no longer interested in the link layer so cut it off.
392          */
393         if (elh == NULL) {
394                 if (ddp_cksum && cksum && cksum !=
395                     at_cksum(m, sizeof(int))) {
396                         ddpstat.ddps_badsum++;
397                         goto out;
398                 }
399                 m_adj(m, sizeof(struct ddpehdr));
400         } else
401                 m_adj(m, sizeof(struct ddpshdr));
402
403         /* 
404          * Search for ddp protocol control blocks that match these addresses. 
405          */
406         DDP_LIST_SLOCK();
407         if ((ddp = ddp_search(&from, &to, aa)) == NULL)
408                 goto out_unlock;
409
410 #ifdef MAC
411         if (mac_socket_check_deliver(ddp->ddp_socket, m) != 0)
412                 goto out_unlock;
413 #endif
414
415         /* 
416          * If we found one, deliver the packet to the socket
417          */
418         SOCKBUF_LOCK(&ddp->ddp_socket->so_rcv);
419         if (sbappendaddr_locked(&ddp->ddp_socket->so_rcv,
420             (struct sockaddr *)&from, m, NULL) == 0) {
421                 SOCKBUF_UNLOCK(&ddp->ddp_socket->so_rcv);
422                 /* 
423                  * If the socket is full (or similar error) dump the packet.
424                  */
425                 ddpstat.ddps_nosockspace++;
426                 goto out_unlock;
427         }
428
429         /*
430          * And wake up whatever might be waiting for it
431          */
432         sorwakeup_locked(ddp->ddp_socket);
433         m = NULL;
434 out_unlock:
435         DDP_LIST_SUNLOCK();
436 out:
437         if (aa != NULL)
438                 ifa_free(&aa->aa_ifa);
439         if (m != NULL)
440                 m_freem(m);
441 }