ddp_input.c revision 193511
1/*- 2 * Copyright (c) 2004 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: head/sys/netatalk/ddp_input.c 193511 2009-06-05 14:55:22Z rwatson $ 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/route.h> 63 64#include <netatalk/at.h> 65#include <netatalk/at_var.h> 66#include <netatalk/ddp.h> 67#include <netatalk/ddp_var.h> 68#include <netatalk/ddp_pcb.h> 69#include <netatalk/at_extern.h> 70 71#include <security/mac/mac_framework.h> 72 73static volatile int ddp_forward = 1; 74static volatile int ddp_firewall = 0; 75static struct ddpstat ddpstat; 76 77static struct route forwro; 78 79static void ddp_input(struct mbuf *, struct ifnet *, struct elaphdr *, int); 80 81/* 82 * Could probably merge these two code segments a little better... 83 */ 84void 85at2intr(struct mbuf *m) 86{ 87 88 /* 89 * Phase 2 packet handling . 90 */ 91 ddp_input(m, m->m_pkthdr.rcvif, NULL, 2); 92} 93 94void 95at1intr(struct mbuf *m) 96{ 97 struct elaphdr *elhp, elh; 98 99 /* 100 * Phase 1 packet handling 101 */ 102 if (m->m_len < SZ_ELAPHDR && ((m = m_pullup(m, SZ_ELAPHDR)) == 103 NULL)) { 104 ddpstat.ddps_tooshort++; 105 return; 106 } 107 108 /* 109 * This seems a little dubious, but I don't know phase 1 so leave it. 110 */ 111 elhp = mtod(m, struct elaphdr *); 112 m_adj(m, SZ_ELAPHDR); 113 114 if (elhp->el_type != ELAP_DDPEXTEND) { 115 bcopy((caddr_t)elhp, (caddr_t)&elh, SZ_ELAPHDR); 116 ddp_input(m, m->m_pkthdr.rcvif, &elh, 1); 117 } else 118 ddp_input(m, m->m_pkthdr.rcvif, NULL, 1); 119} 120 121static void 122ddp_input(struct mbuf *m, struct ifnet *ifp, struct elaphdr *elh, int phase) 123{ 124 struct sockaddr_at from, to; 125 struct ddpshdr *dsh, ddps; 126 struct at_ifaddr *aa; 127 struct ddpehdr *deh = NULL, ddpe; 128 struct ddpcb *ddp; 129 int dlen, mlen; 130 u_short cksum = 0; 131 132 bzero((caddr_t)&from, sizeof(struct sockaddr_at)); 133 bzero((caddr_t)&to, sizeof(struct sockaddr_at)); 134 if (elh != NULL) { 135 /* 136 * Extract the information in the short header. Network 137 * information is defaulted to ATADDR_ANYNET and node 138 * information comes from the elh info. We must be phase 1. 139 */ 140 ddpstat.ddps_short++; 141 142 if (m->m_len < sizeof(struct ddpshdr) && 143 ((m = m_pullup(m, sizeof(struct ddpshdr))) == NULL)) { 144 ddpstat.ddps_tooshort++; 145 return; 146 } 147 148 dsh = mtod(m, struct ddpshdr *); 149 bcopy((caddr_t)dsh, (caddr_t)&ddps, sizeof(struct ddpshdr)); 150 ddps.dsh_bytes = ntohl(ddps.dsh_bytes); 151 dlen = ddps.dsh_len; 152 153 to.sat_addr.s_net = ATADDR_ANYNET; 154 to.sat_addr.s_node = elh->el_dnode; 155 to.sat_port = ddps.dsh_dport; 156 from.sat_addr.s_net = ATADDR_ANYNET; 157 from.sat_addr.s_node = elh->el_snode; 158 from.sat_port = ddps.dsh_sport; 159 160 /* 161 * Make sure that we point to the phase1 ifaddr info and that 162 * it's valid for this packet. 163 */ 164 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) { 165 if ((aa->aa_ifp == ifp) 166 && ((aa->aa_flags & AFA_PHASE2) == 0) 167 && ((to.sat_addr.s_node == 168 AA_SAT(aa)->sat_addr.s_node) || 169 (to.sat_addr.s_node == ATADDR_BCAST))) 170 break; 171 } 172 /* 173 * maybe we got a broadcast not meant for us.. ditch it. 174 */ 175 if (aa == NULL) { 176 m_freem(m); 177 return; 178 } 179 } else { 180 /* 181 * There was no 'elh' passed on. This could still be either 182 * phase1 or phase2. We have a long header, but we may be 183 * running on a phase 1 net. Extract out all the info 184 * regarding this packet's src & dst. 185 */ 186 ddpstat.ddps_long++; 187 188 if (m->m_len < sizeof(struct ddpehdr) && 189 ((m = m_pullup(m, sizeof(struct ddpehdr))) == NULL)) { 190 ddpstat.ddps_tooshort++; 191 return; 192 } 193 194 deh = mtod(m, struct ddpehdr *); 195 bcopy((caddr_t)deh, (caddr_t)&ddpe, sizeof(struct ddpehdr)); 196 ddpe.deh_bytes = ntohl(ddpe.deh_bytes); 197 dlen = ddpe.deh_len; 198 199 if ((cksum = ddpe.deh_sum) == 0) 200 ddpstat.ddps_nosum++; 201 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; 208 209 if (to.sat_addr.s_net == ATADDR_ANYNET) { 210 /* 211 * The TO address doesn't specify a net, so by 212 * definition it's for this net. Try find ifaddr 213 * info with the right phase, the right interface, 214 * and either to our node, a broadcast, or looped 215 * back (though that SHOULD be covered in the other 216 * cases). 217 * 218 * XXX If we have multiple interfaces, then the first 219 * with this node number will match (which may NOT be 220 * what we want, but it's probably safe in 99.999% of 221 * cases. 222 */ 223 for (aa = at_ifaddr_list; aa != NULL; 224 aa = aa->aa_next) { 225 if (phase == 1 && (aa->aa_flags & 226 AFA_PHASE2)) 227 continue; 228 if (phase == 2 && (aa->aa_flags & 229 AFA_PHASE2) == 0) 230 continue; 231 if ((aa->aa_ifp == ifp) && 232 ((to.sat_addr.s_node == 233 AA_SAT(aa)->sat_addr.s_node) || 234 (to.sat_addr.s_node == ATADDR_BCAST) || 235 (ifp->if_flags & IFF_LOOPBACK))) 236 break; 237 } 238 } else { 239 /* 240 * A destination network was given. We just try to 241 * find which ifaddr info matches it. 242 */ 243 for (aa = at_ifaddr_list; aa != NULL; 244 aa = aa->aa_next) { 245 /* 246 * This is a kludge. Accept packets that are 247 * for any router on a local netrange. 248 */ 249 if (to.sat_addr.s_net == aa->aa_firstnet && 250 to.sat_addr.s_node == 0) 251 break; 252 /* 253 * Don't use ifaddr info for which we are 254 * totally outside the netrange, and it's not 255 * a startup packet. Startup packets are 256 * always implicitly allowed on to the next 257 * test. 258 */ 259 if (((ntohs(to.sat_addr.s_net) < 260 ntohs(aa->aa_firstnet)) || 261 (ntohs(to.sat_addr.s_net) > 262 ntohs(aa->aa_lastnet))) && 263 ((ntohs(to.sat_addr.s_net) < 0xff00) || 264 (ntohs(to.sat_addr.s_net) > 0xfffe))) 265 continue; 266 267 /* 268 * Don't record a match either if we just 269 * don't have a match in the node address. 270 * This can have if the interface is in 271 * promiscuous mode for example. 272 */ 273 if ((to.sat_addr.s_node != 274 AA_SAT(aa)->sat_addr.s_node) && 275 (to.sat_addr.s_node != ATADDR_BCAST)) 276 continue; 277 break; 278 } 279 } 280 } 281 282 /* 283 * Adjust the length, removing any padding that may have been added 284 * at a link layer. We do this before we attempt to forward a 285 * packet, possibly on a different media. 286 */ 287 mlen = m->m_pkthdr.len; 288 if (mlen < dlen) { 289 ddpstat.ddps_toosmall++; 290 m_freem(m); 291 return; 292 } 293 if (mlen > dlen) 294 m_adj(m, dlen - mlen); 295 296 /* 297 * If it isn't for a net on any of our interfaces, or it IS for a net 298 * on a different interface than it came in on, (and it is not looped 299 * back) then consider if we should forward it. As we are not really 300 * a router this is a bit cheeky, but it may be useful some day. 301 */ 302 if ((aa == NULL) || ((to.sat_addr.s_node == ATADDR_BCAST) && 303 (aa->aa_ifp != ifp) && ((ifp->if_flags & IFF_LOOPBACK) == 0))) { 304 /* 305 * If we've explicitly disabled it, don't route anything. 306 */ 307 if (ddp_forward == 0) { 308 m_freem(m); 309 return; 310 } 311 312 /* 313 * If the cached forwarding route is still valid, use it. 314 * 315 * XXXRW: Access to the cached route may not be properly 316 * synchronized for parallel input handling. 317 */ 318 if (forwro.ro_rt && 319 (satosat(&forwro.ro_dst)->sat_addr.s_net != 320 to.sat_addr.s_net || 321 satosat(&forwro.ro_dst)->sat_addr.s_node != 322 to.sat_addr.s_node)) { 323 RTFREE(forwro.ro_rt); 324 forwro.ro_rt = NULL; 325 } 326 327 /* 328 * If we don't have a cached one (any more) or it's useless, 329 * then get a new route. 330 * 331 * XXX this could cause a 'route leak'. Check this! 332 */ 333 if (forwro.ro_rt == NULL || forwro.ro_rt->rt_ifp == NULL) { 334 forwro.ro_dst.sa_len = sizeof(struct sockaddr_at); 335 forwro.ro_dst.sa_family = AF_APPLETALK; 336 satosat(&forwro.ro_dst)->sat_addr.s_net = 337 to.sat_addr.s_net; 338 satosat(&forwro.ro_dst)->sat_addr.s_node = 339 to.sat_addr.s_node; 340 rtalloc(&forwro); 341 } 342 343 /* 344 * If it's not going to get there on this hop, and it's 345 * already done too many hops, then throw it away. 346 */ 347 if ((to.sat_addr.s_net != 348 satosat(&forwro.ro_dst)->sat_addr.s_net) && 349 (ddpe.deh_hops == DDP_MAXHOPS)) { 350 m_freem(m); 351 return; 352 } 353 354 /* 355 * A ddp router might use the same interface to forward the 356 * packet, which this would not effect. Don't allow packets 357 * to cross from one interface to another however. 358 */ 359 if (ddp_firewall && ((forwro.ro_rt == NULL) || 360 (forwro.ro_rt->rt_ifp != ifp))) { 361 m_freem(m); 362 return; 363 } 364 365 /* 366 * Adjust the header. If it was a short header then it would 367 * have not gotten here, so we can assume there is room to 368 * drop the header in. 369 * 370 * XXX what about promiscuous mode, etc... 371 */ 372 ddpe.deh_hops++; 373 ddpe.deh_bytes = htonl(ddpe.deh_bytes); 374 /* XXX deh? */ 375 bcopy((caddr_t)&ddpe, (caddr_t)deh, sizeof(u_short)); 376 if (ddp_route(m, &forwro)) 377 ddpstat.ddps_cantforward++; 378 else 379 ddpstat.ddps_forward++; 380 return; 381 } 382 383 /* 384 * It was for us, and we have an ifaddr to use with it. 385 */ 386 from.sat_len = sizeof(struct sockaddr_at); 387 from.sat_family = AF_APPLETALK; 388 389 /* 390 * We are no longer interested in the link layer so cut it off. 391 */ 392 if (elh == NULL) { 393 if (ddp_cksum && cksum && cksum != 394 at_cksum(m, sizeof(int))) { 395 ddpstat.ddps_badsum++; 396 m_freem(m); 397 return; 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; 409 410#ifdef MAC 411 if (mac_socket_check_deliver(ddp->ddp_socket, m) != 0) 412 goto out; 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; 427 } 428 429 /* 430 * And wake up whatever might be waiting for it 431 */ 432 sorwakeup_locked(ddp->ddp_socket); 433 m = NULL; 434out: 435 DDP_LIST_SUNLOCK(); 436 if (m != NULL) 437 m_freem(m); 438} 439