Home | History | Annotate | Line # | Download | only in netatalk
ddp_usrreq.c revision 1.37
      1 /*	$NetBSD: ddp_usrreq.c,v 1.37 2009/03/18 10:22:43 cegger Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1990,1991 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_usrreq.c,v 1.37 2009/03/18 10:22:43 cegger Exp $");
     31 
     32 #include "opt_mbuftrace.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/errno.h>
     36 #include <sys/systm.h>
     37 #include <sys/proc.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/queue.h>
     41 #include <sys/socket.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/protosw.h>
     44 #include <sys/kauth.h>
     45 #include <sys/sysctl.h>
     46 #include <net/if.h>
     47 #include <net/route.h>
     48 #include <net/if_ether.h>
     49 #include <net/net_stats.h>
     50 #include <netinet/in.h>
     51 
     52 #include <netatalk/at.h>
     53 #include <netatalk/at_var.h>
     54 #include <netatalk/ddp_var.h>
     55 #include <netatalk/ddp_private.h>
     56 #include <netatalk/aarp.h>
     57 #include <netatalk/at_extern.h>
     58 
     59 static void at_pcbdisconnect(struct ddpcb *);
     60 static void at_sockaddr(struct ddpcb *, struct mbuf *);
     61 static int at_pcbsetaddr(struct ddpcb *, struct mbuf *, struct lwp *);
     62 static int at_pcbconnect(struct ddpcb *, struct mbuf *, struct lwp *);
     63 static void at_pcbdetach(struct socket *, struct ddpcb *);
     64 static int at_pcballoc(struct socket *);
     65 
     66 struct ifqueue atintrq1, atintrq2;
     67 struct ddpcb   *ddp_ports[ATPORT_LAST];
     68 struct ddpcb   *ddpcb = NULL;
     69 percpu_t *ddpstat_percpu;
     70 struct at_ifaddrhead at_ifaddr;		/* Here as inited in this file */
     71 u_long ddp_sendspace = DDP_MAXSZ;	/* Max ddp size + 1 (ddp_type) */
     72 u_long ddp_recvspace = 25 * (587 + sizeof(struct sockaddr_at));
     73 
     74 #ifdef MBUFTRACE
     75 struct mowner atalk_rx_mowner = MOWNER_INIT("atalk", "rx");
     76 struct mowner atalk_tx_mowner = MOWNER_INIT("atalk", "tx");
     77 #endif
     78 
     79 /* ARGSUSED */
     80 int
     81 ddp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr, struct mbuf *rights, struct lwp *l)
     82 {
     83 	struct ddpcb   *ddp;
     84 	int             error = 0;
     85 
     86 	ddp = sotoddpcb(so);
     87 
     88 	if (req == PRU_CONTROL) {
     89 		return (at_control((long) m, (void *) addr,
     90 		    (struct ifnet *) rights, l));
     91 	}
     92 	if (req == PRU_PURGEIF) {
     93 		mutex_enter(softnet_lock);
     94 		at_purgeif((struct ifnet *) rights);
     95 		mutex_exit(softnet_lock);
     96 		return (0);
     97 	}
     98 	if (rights && rights->m_len) {
     99 		error = EINVAL;
    100 		goto release;
    101 	}
    102 	if (ddp == NULL && req != PRU_ATTACH) {
    103 		error = EINVAL;
    104 		goto release;
    105 	}
    106 	switch (req) {
    107 	case PRU_ATTACH:
    108 		if (ddp != NULL) {
    109 			error = EINVAL;
    110 			break;
    111 		}
    112 		sosetlock(so);
    113 		if ((error = at_pcballoc(so)) != 0) {
    114 			break;
    115 		}
    116 		error = soreserve(so, ddp_sendspace, ddp_recvspace);
    117 		break;
    118 
    119 	case PRU_DETACH:
    120 		at_pcbdetach(so, ddp);
    121 		break;
    122 
    123 	case PRU_BIND:
    124 		error = at_pcbsetaddr(ddp, addr, l);
    125 		break;
    126 
    127 	case PRU_SOCKADDR:
    128 		at_sockaddr(ddp, addr);
    129 		break;
    130 
    131 	case PRU_CONNECT:
    132 		if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT) {
    133 			error = EISCONN;
    134 			break;
    135 		}
    136 		error = at_pcbconnect(ddp, addr, l);
    137 		if (error == 0)
    138 			soisconnected(so);
    139 		break;
    140 
    141 	case PRU_DISCONNECT:
    142 		if (ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE) {
    143 			error = ENOTCONN;
    144 			break;
    145 		}
    146 		at_pcbdisconnect(ddp);
    147 		soisdisconnected(so);
    148 		break;
    149 
    150 	case PRU_SHUTDOWN:
    151 		socantsendmore(so);
    152 		break;
    153 
    154 	case PRU_SEND:{
    155 			int s = 0;
    156 
    157 			if (addr) {
    158 				if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT) {
    159 					error = EISCONN;
    160 					break;
    161 				}
    162 				s = splnet();
    163 				error = at_pcbconnect(ddp, addr, l);
    164 				if (error) {
    165 					splx(s);
    166 					break;
    167 				}
    168 			} else {
    169 				if (ddp->ddp_fsat.sat_port == ATADDR_ANYPORT) {
    170 					error = ENOTCONN;
    171 					break;
    172 				}
    173 			}
    174 
    175 			error = ddp_output(m, ddp);
    176 			m = NULL;
    177 			if (addr) {
    178 				at_pcbdisconnect(ddp);
    179 				splx(s);
    180 			}
    181 		}
    182 		break;
    183 
    184 	case PRU_ABORT:
    185 		soisdisconnected(so);
    186 		at_pcbdetach(so, ddp);
    187 		break;
    188 
    189 	case PRU_LISTEN:
    190 	case PRU_CONNECT2:
    191 	case PRU_ACCEPT:
    192 	case PRU_SENDOOB:
    193 	case PRU_FASTTIMO:
    194 	case PRU_SLOWTIMO:
    195 	case PRU_PROTORCV:
    196 	case PRU_PROTOSEND:
    197 		error = EOPNOTSUPP;
    198 		break;
    199 
    200 	case PRU_RCVD:
    201 	case PRU_RCVOOB:
    202 		/*
    203 		 * Don't mfree. Good architecture...
    204 		 */
    205 		return (EOPNOTSUPP);
    206 
    207 	case PRU_SENSE:
    208 		/*
    209 		 * 1. Don't return block size.
    210 		 * 2. Don't mfree.
    211 		 */
    212 		return (0);
    213 
    214 	default:
    215 		error = EOPNOTSUPP;
    216 	}
    217 
    218 release:
    219 	if (m != NULL) {
    220 		m_freem(m);
    221 	}
    222 	return (error);
    223 }
    224 
    225 static void
    226 at_sockaddr(struct ddpcb *ddp, struct mbuf *addr)
    227 {
    228 	struct sockaddr_at *sat;
    229 
    230 	addr->m_len = sizeof(struct sockaddr_at);
    231 	sat = mtod(addr, struct sockaddr_at *);
    232 	*sat = ddp->ddp_lsat;
    233 }
    234 
    235 static int
    236 at_pcbsetaddr(struct ddpcb *ddp, struct mbuf *addr, struct lwp *l)
    237 {
    238 	struct sockaddr_at lsat, *sat;
    239 	struct at_ifaddr *aa;
    240 	struct ddpcb   *ddpp;
    241 
    242 	if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT) {	/* shouldn't be bound */
    243 		return (EINVAL);
    244 	}
    245 	if (addr != 0) {	/* validate passed address */
    246 		sat = mtod(addr, struct sockaddr_at *);
    247 		if (addr->m_len != sizeof(*sat))
    248 			return (EINVAL);
    249 
    250 		if (sat->sat_family != AF_APPLETALK)
    251 			return (EAFNOSUPPORT);
    252 
    253 		if (sat->sat_addr.s_node != ATADDR_ANYNODE ||
    254 		    sat->sat_addr.s_net != ATADDR_ANYNET) {
    255 			TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
    256 				if ((sat->sat_addr.s_net ==
    257 				    AA_SAT(aa)->sat_addr.s_net) &&
    258 				    (sat->sat_addr.s_node ==
    259 				    AA_SAT(aa)->sat_addr.s_node))
    260 					break;
    261 			}
    262 			if (!aa)
    263 				return (EADDRNOTAVAIL);
    264 		}
    265 		if (sat->sat_port != ATADDR_ANYPORT) {
    266 			if (sat->sat_port < ATPORT_FIRST ||
    267 			    sat->sat_port >= ATPORT_LAST)
    268 				return (EINVAL);
    269 
    270 			if (sat->sat_port < ATPORT_RESERVED && l &&
    271 			    kauth_authorize_generic(l->l_cred,
    272 			    KAUTH_GENERIC_ISSUSER, NULL))
    273 				return (EACCES);
    274 		}
    275 	} else {
    276 		bzero((void *) & lsat, sizeof(struct sockaddr_at));
    277 		lsat.sat_len = sizeof(struct sockaddr_at);
    278 		lsat.sat_addr.s_node = ATADDR_ANYNODE;
    279 		lsat.sat_addr.s_net = ATADDR_ANYNET;
    280 		lsat.sat_family = AF_APPLETALK;
    281 		sat = &lsat;
    282 	}
    283 
    284 	if (sat->sat_addr.s_node == ATADDR_ANYNODE &&
    285 	    sat->sat_addr.s_net == ATADDR_ANYNET) {
    286 		if (TAILQ_EMPTY(&at_ifaddr))
    287 			return EADDRNOTAVAIL;
    288 		sat->sat_addr = AA_SAT(TAILQ_FIRST(&at_ifaddr))->sat_addr;
    289 	}
    290 	ddp->ddp_lsat = *sat;
    291 
    292 	/*
    293          * Choose port.
    294          */
    295 	if (sat->sat_port == ATADDR_ANYPORT) {
    296 		for (sat->sat_port = ATPORT_RESERVED;
    297 		     sat->sat_port < ATPORT_LAST; sat->sat_port++) {
    298 			if (ddp_ports[sat->sat_port - 1] == 0)
    299 				break;
    300 		}
    301 		if (sat->sat_port == ATPORT_LAST) {
    302 			return (EADDRNOTAVAIL);
    303 		}
    304 		ddp->ddp_lsat.sat_port = sat->sat_port;
    305 		ddp_ports[sat->sat_port - 1] = ddp;
    306 	} else {
    307 		for (ddpp = ddp_ports[sat->sat_port - 1]; ddpp;
    308 		     ddpp = ddpp->ddp_pnext) {
    309 			if (ddpp->ddp_lsat.sat_addr.s_net ==
    310 			    sat->sat_addr.s_net &&
    311 			    ddpp->ddp_lsat.sat_addr.s_node ==
    312 			    sat->sat_addr.s_node)
    313 				break;
    314 		}
    315 		if (ddpp != NULL)
    316 			return (EADDRINUSE);
    317 
    318 		ddp->ddp_pnext = ddp_ports[sat->sat_port - 1];
    319 		ddp_ports[sat->sat_port - 1] = ddp;
    320 		if (ddp->ddp_pnext)
    321 			ddp->ddp_pnext->ddp_pprev = ddp;
    322 	}
    323 
    324 	return 0;
    325 }
    326 
    327 static int
    328 at_pcbconnect(struct ddpcb *ddp, struct mbuf *addr, struct lwp *l)
    329 {
    330 	struct rtentry *rt;
    331 	const struct sockaddr_at *cdst;
    332 	struct sockaddr_at *sat = mtod(addr, struct sockaddr_at *);
    333 	struct route *ro;
    334 	struct at_ifaddr *aa;
    335 	struct ifnet   *ifp;
    336 	u_short         hintnet = 0, net;
    337 
    338 	if (addr->m_len != sizeof(*sat))
    339 		return EINVAL;
    340 	if (sat->sat_family != AF_APPLETALK) {
    341 		return EAFNOSUPPORT;
    342 	}
    343 	/*
    344          * Under phase 2, network 0 means "the network".  We take "the
    345          * network" to mean the network the control block is bound to.
    346          * If the control block is not bound, there is an error.
    347          */
    348 	if (sat->sat_addr.s_net == ATADDR_ANYNET
    349 	    && sat->sat_addr.s_node != ATADDR_ANYNODE) {
    350 		if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT) {
    351 			return EADDRNOTAVAIL;
    352 		}
    353 		hintnet = ddp->ddp_lsat.sat_addr.s_net;
    354 	}
    355 	ro = &ddp->ddp_route;
    356 	/*
    357          * If we've got an old route for this pcb, check that it is valid.
    358          * If we've changed our address, we may have an old "good looking"
    359          * route here.  Attempt to detect it.
    360          */
    361 	if ((rt = rtcache_validate(ro)) != NULL ||
    362 	    (rt = rtcache_update(ro, 1)) != NULL) {
    363 		if (hintnet) {
    364 			net = hintnet;
    365 		} else {
    366 			net = sat->sat_addr.s_net;
    367 		}
    368 		if ((ifp = rt->rt_ifp) != NULL) {
    369 			TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
    370 				if (aa->aa_ifp == ifp &&
    371 				    ntohs(net) >= ntohs(aa->aa_firstnet) &&
    372 				    ntohs(net) <= ntohs(aa->aa_lastnet)) {
    373 					break;
    374 				}
    375 			}
    376 		} else
    377 			aa = NULL;
    378 		cdst = satocsat(rtcache_getdst(ro));
    379 		if (aa == NULL || (cdst->sat_addr.s_net !=
    380 		    (hintnet ? hintnet : sat->sat_addr.s_net) ||
    381 		    cdst->sat_addr.s_node != sat->sat_addr.s_node)) {
    382 			rtcache_free(ro);
    383 			rt = NULL;
    384 		}
    385 	}
    386 	/*
    387          * If we've got no route for this interface, try to find one.
    388          */
    389 	if (rt == NULL) {
    390 		union {
    391 			struct sockaddr		dst;
    392 			struct sockaddr_at	dsta;
    393 		} u;
    394 
    395 		sockaddr_at_init(&u.dsta, &sat->sat_addr, 0);
    396 		if (hintnet)
    397 			u.dsta.sat_addr.s_net = hintnet;
    398 		rt = rtcache_lookup(ro, &u.dst);
    399 	}
    400 	/*
    401          * Make sure any route that we have has a valid interface.
    402          */
    403 	if (rt != NULL && (ifp = rt->rt_ifp) != NULL) {
    404 		TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
    405 			if (aa->aa_ifp == ifp)
    406 				break;
    407 		}
    408 	} else
    409 		aa = NULL;
    410 	if (aa == NULL)
    411 		return ENETUNREACH;
    412 	ddp->ddp_fsat = *sat;
    413 	if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT)
    414 		return at_pcbsetaddr(ddp, NULL, l);
    415 	return 0;
    416 }
    417 
    418 static void
    419 at_pcbdisconnect(struct ddpcb *ddp)
    420 {
    421 	ddp->ddp_fsat.sat_addr.s_net = ATADDR_ANYNET;
    422 	ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
    423 	ddp->ddp_fsat.sat_port = ATADDR_ANYPORT;
    424 }
    425 
    426 static int
    427 at_pcballoc(struct socket *so)
    428 {
    429 	struct ddpcb   *ddp;
    430 
    431 	ddp = malloc(sizeof(*ddp), M_PCB, M_WAITOK|M_ZERO);
    432 	if (!ddp)
    433 		panic("at_pcballoc");
    434 	ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
    435 
    436 	ddp->ddp_next = ddpcb;
    437 	ddp->ddp_prev = NULL;
    438 	ddp->ddp_pprev = NULL;
    439 	ddp->ddp_pnext = NULL;
    440 	if (ddpcb) {
    441 		ddpcb->ddp_prev = ddp;
    442 	}
    443 	ddpcb = ddp;
    444 
    445 	ddp->ddp_socket = so;
    446 	so->so_pcb = (void *) ddp;
    447 #ifdef MBUFTRACE
    448 	so->so_rcv.sb_mowner = &atalk_rx_mowner;
    449 	so->so_snd.sb_mowner = &atalk_tx_mowner;
    450 #endif
    451 	return 0;
    452 }
    453 
    454 static void
    455 at_pcbdetach(struct socket *so, struct ddpcb *ddp)
    456 {
    457 	soisdisconnected(so);
    458 	so->so_pcb = 0;
    459 	/* sofree drops the lock */
    460 	sofree(so);
    461 	mutex_enter(softnet_lock);
    462 
    463 	/* remove ddp from ddp_ports list */
    464 	if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT &&
    465 	    ddp_ports[ddp->ddp_lsat.sat_port - 1] != NULL) {
    466 		if (ddp->ddp_pprev != NULL) {
    467 			ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext;
    468 		} else {
    469 			ddp_ports[ddp->ddp_lsat.sat_port - 1] = ddp->ddp_pnext;
    470 		}
    471 		if (ddp->ddp_pnext != NULL) {
    472 			ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
    473 		}
    474 	}
    475 	rtcache_free(&ddp->ddp_route);
    476 	if (ddp->ddp_prev) {
    477 		ddp->ddp_prev->ddp_next = ddp->ddp_next;
    478 	} else {
    479 		ddpcb = ddp->ddp_next;
    480 	}
    481 	if (ddp->ddp_next) {
    482 		ddp->ddp_next->ddp_prev = ddp->ddp_prev;
    483 	}
    484 	free(ddp, M_PCB);
    485 }
    486 
    487 /*
    488  * For the moment, this just find the pcb with the correct local address.
    489  * In the future, this will actually do some real searching, so we can use
    490  * the sender's address to do de-multiplexing on a single port to many
    491  * sockets (pcbs).
    492  */
    493 struct ddpcb   *
    494 ddp_search(
    495     struct sockaddr_at *from,
    496     struct sockaddr_at *to,
    497     struct at_ifaddr *aa)
    498 {
    499 	struct ddpcb   *ddp;
    500 
    501 	/*
    502          * Check for bad ports.
    503          */
    504 	if (to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST)
    505 		return NULL;
    506 
    507 	/*
    508          * Make sure the local address matches the sent address.  What about
    509          * the interface?
    510          */
    511 	for (ddp = ddp_ports[to->sat_port - 1]; ddp; ddp = ddp->ddp_pnext) {
    512 		/* XXX should we handle 0.YY? */
    513 
    514 		/* XXXX.YY to socket on destination interface */
    515 		if (to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net &&
    516 		    to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node) {
    517 			break;
    518 		}
    519 		/* 0.255 to socket on receiving interface */
    520 		if (to->sat_addr.s_node == ATADDR_BCAST &&
    521 		    (to->sat_addr.s_net == 0 ||
    522 		    to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net) &&
    523 		ddp->ddp_lsat.sat_addr.s_net == AA_SAT(aa)->sat_addr.s_net) {
    524 			break;
    525 		}
    526 		/* XXXX.0 to socket on destination interface */
    527 		if (to->sat_addr.s_net == aa->aa_firstnet &&
    528 		    to->sat_addr.s_node == 0 &&
    529 		    ntohs(ddp->ddp_lsat.sat_addr.s_net) >=
    530 		    ntohs(aa->aa_firstnet) &&
    531 		    ntohs(ddp->ddp_lsat.sat_addr.s_net) <=
    532 		    ntohs(aa->aa_lastnet)) {
    533 			break;
    534 		}
    535 	}
    536 	return (ddp);
    537 }
    538 
    539 /*
    540  * Initialize all the ddp & appletalk stuff
    541  */
    542 void
    543 ddp_init(void)
    544 {
    545 
    546 	ddpstat_percpu = percpu_alloc(sizeof(uint64_t) * DDP_NSTATS);
    547 
    548 	TAILQ_INIT(&at_ifaddr);
    549 	atintrq1.ifq_maxlen = IFQ_MAXLEN;
    550 	atintrq2.ifq_maxlen = IFQ_MAXLEN;
    551 
    552 	MOWNER_ATTACH(&atalk_tx_mowner);
    553 	MOWNER_ATTACH(&atalk_rx_mowner);
    554 }
    555 
    556 #if 0
    557 static void
    558 ddp_clean(void)
    559 {
    560 	struct ddpcb   *ddp;
    561 
    562 	for (ddp = ddpcb; ddp; ddp = ddp->ddp_next)
    563 		at_pcbdetach(ddp->ddp_socket, ddp);
    564 }
    565 #endif
    566 
    567 static int
    568 sysctl_net_atalk_ddp_stats(SYSCTLFN_ARGS)
    569 {
    570 
    571 	return (NETSTAT_SYSCTL(ddpstat_percpu, DDP_NSTATS));
    572 }
    573 
    574 /*
    575  * Sysctl for DDP variables.
    576  */
    577 SYSCTL_SETUP(sysctl_net_atalk_ddp_setup, "sysctl net.atalk.ddp subtree setup")
    578 {
    579 
    580 	sysctl_createv(clog, 0, NULL, NULL,
    581 		       CTLFLAG_PERMANENT,
    582 		       CTLTYPE_NODE, "net", NULL,
    583 		       NULL, 0, NULL, 0,
    584 		       CTL_NET, CTL_EOL);
    585 	sysctl_createv(clog, 0, NULL, NULL,
    586 		       CTLFLAG_PERMANENT,
    587 		       CTLTYPE_NODE, "atalk", NULL,
    588 		       NULL, 0, NULL, 0,
    589 		       CTL_NET, PF_APPLETALK, CTL_EOL);
    590 	sysctl_createv(clog, 0, NULL, NULL,
    591 		       CTLFLAG_PERMANENT,
    592 		       CTLTYPE_NODE, "ddp",
    593 		       SYSCTL_DESCR("DDP related settings"),
    594 		       NULL, 0, NULL, 0,
    595 		       CTL_NET, PF_APPLETALK, ATPROTO_DDP, CTL_EOL);
    596 
    597 	sysctl_createv(clog, 0, NULL, NULL,
    598 		       CTLFLAG_PERMANENT,
    599 		       CTLTYPE_STRUCT, "stats",
    600 		       SYSCTL_DESCR("DDP statistics"),
    601 		       sysctl_net_atalk_ddp_stats, 0, NULL, 0,
    602 		       CTL_NET, PF_APPLETALK, ATPROTO_DDP, CTL_CREATE,
    603 		       CTL_EOL);
    604 }
    605