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