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