ddp_usrreq.c revision 1.59 1 1.59 rtr /* $NetBSD: ddp_usrreq.c,v 1.59 2014/08/05 07:55:31 rtr 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.59 rtr __KERNEL_RCSID(0, "$NetBSD: ddp_usrreq.c,v 1.59 2014/08/05 07:55:31 rtr 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.55 rtr static int at_pcbsetaddr(struct ddpcb *, struct mbuf *);
62 1.56 rtr static int at_pcbconnect(struct ddpcb *, struct mbuf *);
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.53 rtr KASSERT(req != PRU_ACCEPT);
88 1.55 rtr KASSERT(req != PRU_BIND);
89 1.55 rtr KASSERT(req != PRU_LISTEN);
90 1.56 rtr KASSERT(req != PRU_CONNECT);
91 1.57 rtr KASSERT(req != PRU_DISCONNECT);
92 1.57 rtr KASSERT(req != PRU_SHUTDOWN);
93 1.57 rtr KASSERT(req != PRU_ABORT);
94 1.45 rtr KASSERT(req != PRU_CONTROL);
95 1.48 rtr KASSERT(req != PRU_SENSE);
96 1.52 rtr KASSERT(req != PRU_PEERADDR);
97 1.52 rtr KASSERT(req != PRU_SOCKADDR);
98 1.54 rtr KASSERT(req != PRU_RCVOOB);
99 1.59 rtr KASSERT(req != PRU_SEND);
100 1.54 rtr KASSERT(req != PRU_SENDOOB);
101 1.45 rtr
102 1.1 christos ddp = sotoddpcb(so);
103 1.1 christos
104 1.4 thorpej if (req == PRU_PURGEIF) {
105 1.32 ad mutex_enter(softnet_lock);
106 1.4 thorpej at_purgeif((struct ifnet *) rights);
107 1.32 ad mutex_exit(softnet_lock);
108 1.3 thorpej return (0);
109 1.1 christos }
110 1.1 christos if (rights && rights->m_len) {
111 1.1 christos error = EINVAL;
112 1.1 christos goto release;
113 1.1 christos }
114 1.43 rmind if (ddp == NULL) {
115 1.1 christos error = EINVAL;
116 1.1 christos goto release;
117 1.1 christos }
118 1.1 christos switch (req) {
119 1.1 christos case PRU_CONNECT2:
120 1.1 christos case PRU_FASTTIMO:
121 1.1 christos case PRU_SLOWTIMO:
122 1.1 christos case PRU_PROTORCV:
123 1.1 christos case PRU_PROTOSEND:
124 1.1 christos error = EOPNOTSUPP;
125 1.1 christos break;
126 1.1 christos
127 1.1 christos case PRU_RCVD:
128 1.1 christos /*
129 1.1 christos * Don't mfree. Good architecture...
130 1.1 christos */
131 1.1 christos return (EOPNOTSUPP);
132 1.1 christos
133 1.1 christos default:
134 1.1 christos error = EOPNOTSUPP;
135 1.1 christos }
136 1.1 christos
137 1.1 christos release:
138 1.1 christos if (m != NULL) {
139 1.1 christos m_freem(m);
140 1.1 christos }
141 1.1 christos return (error);
142 1.1 christos }
143 1.1 christos
144 1.1 christos static void
145 1.36 dsl at_sockaddr(struct ddpcb *ddp, struct mbuf *addr)
146 1.1 christos {
147 1.1 christos struct sockaddr_at *sat;
148 1.1 christos
149 1.1 christos addr->m_len = sizeof(struct sockaddr_at);
150 1.1 christos sat = mtod(addr, struct sockaddr_at *);
151 1.1 christos *sat = ddp->ddp_lsat;
152 1.1 christos }
153 1.1 christos
154 1.1 christos static int
155 1.55 rtr at_pcbsetaddr(struct ddpcb *ddp, struct mbuf *addr)
156 1.1 christos {
157 1.1 christos struct sockaddr_at lsat, *sat;
158 1.1 christos struct at_ifaddr *aa;
159 1.1 christos struct ddpcb *ddpp;
160 1.1 christos
161 1.1 christos if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT) { /* shouldn't be bound */
162 1.1 christos return (EINVAL);
163 1.1 christos }
164 1.1 christos if (addr != 0) { /* validate passed address */
165 1.1 christos sat = mtod(addr, struct sockaddr_at *);
166 1.1 christos if (addr->m_len != sizeof(*sat))
167 1.1 christos return (EINVAL);
168 1.1 christos
169 1.1 christos if (sat->sat_family != AF_APPLETALK)
170 1.1 christos return (EAFNOSUPPORT);
171 1.1 christos
172 1.1 christos if (sat->sat_addr.s_node != ATADDR_ANYNODE ||
173 1.1 christos sat->sat_addr.s_net != ATADDR_ANYNET) {
174 1.25 dyoung TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
175 1.1 christos if ((sat->sat_addr.s_net ==
176 1.1 christos AA_SAT(aa)->sat_addr.s_net) &&
177 1.1 christos (sat->sat_addr.s_node ==
178 1.1 christos AA_SAT(aa)->sat_addr.s_node))
179 1.1 christos break;
180 1.1 christos }
181 1.1 christos if (!aa)
182 1.1 christos return (EADDRNOTAVAIL);
183 1.1 christos }
184 1.1 christos if (sat->sat_port != ATADDR_ANYPORT) {
185 1.39 elad int error;
186 1.39 elad
187 1.1 christos if (sat->sat_port < ATPORT_FIRST ||
188 1.1 christos sat->sat_port >= ATPORT_LAST)
189 1.1 christos return (EINVAL);
190 1.1 christos
191 1.55 rtr if (sat->sat_port < ATPORT_RESERVED &&
192 1.55 rtr (error = kauth_authorize_network(curlwp->l_cred,
193 1.39 elad KAUTH_NETWORK_BIND, KAUTH_REQ_NETWORK_BIND_PRIVPORT,
194 1.39 elad ddpcb->ddp_socket, sat, NULL)) != 0)
195 1.39 elad return (error);
196 1.1 christos }
197 1.1 christos } else {
198 1.38 cegger memset((void *) & lsat, 0, sizeof(struct sockaddr_at));
199 1.1 christos lsat.sat_len = sizeof(struct sockaddr_at);
200 1.1 christos lsat.sat_addr.s_node = ATADDR_ANYNODE;
201 1.1 christos lsat.sat_addr.s_net = ATADDR_ANYNET;
202 1.1 christos lsat.sat_family = AF_APPLETALK;
203 1.1 christos sat = &lsat;
204 1.1 christos }
205 1.1 christos
206 1.1 christos if (sat->sat_addr.s_node == ATADDR_ANYNODE &&
207 1.1 christos sat->sat_addr.s_net == ATADDR_ANYNET) {
208 1.25 dyoung if (TAILQ_EMPTY(&at_ifaddr))
209 1.25 dyoung return EADDRNOTAVAIL;
210 1.25 dyoung sat->sat_addr = AA_SAT(TAILQ_FIRST(&at_ifaddr))->sat_addr;
211 1.1 christos }
212 1.1 christos ddp->ddp_lsat = *sat;
213 1.1 christos
214 1.1 christos /*
215 1.1 christos * Choose port.
216 1.1 christos */
217 1.1 christos if (sat->sat_port == ATADDR_ANYPORT) {
218 1.1 christos for (sat->sat_port = ATPORT_RESERVED;
219 1.1 christos sat->sat_port < ATPORT_LAST; sat->sat_port++) {
220 1.1 christos if (ddp_ports[sat->sat_port - 1] == 0)
221 1.1 christos break;
222 1.1 christos }
223 1.1 christos if (sat->sat_port == ATPORT_LAST) {
224 1.1 christos return (EADDRNOTAVAIL);
225 1.1 christos }
226 1.1 christos ddp->ddp_lsat.sat_port = sat->sat_port;
227 1.1 christos ddp_ports[sat->sat_port - 1] = ddp;
228 1.1 christos } else {
229 1.1 christos for (ddpp = ddp_ports[sat->sat_port - 1]; ddpp;
230 1.1 christos ddpp = ddpp->ddp_pnext) {
231 1.13 perry if (ddpp->ddp_lsat.sat_addr.s_net ==
232 1.1 christos sat->sat_addr.s_net &&
233 1.1 christos ddpp->ddp_lsat.sat_addr.s_node ==
234 1.1 christos sat->sat_addr.s_node)
235 1.1 christos break;
236 1.1 christos }
237 1.1 christos if (ddpp != NULL)
238 1.1 christos return (EADDRINUSE);
239 1.1 christos
240 1.1 christos ddp->ddp_pnext = ddp_ports[sat->sat_port - 1];
241 1.1 christos ddp_ports[sat->sat_port - 1] = ddp;
242 1.1 christos if (ddp->ddp_pnext)
243 1.1 christos ddp->ddp_pnext->ddp_pprev = ddp;
244 1.1 christos }
245 1.1 christos
246 1.1 christos return 0;
247 1.1 christos }
248 1.1 christos
249 1.1 christos static int
250 1.56 rtr at_pcbconnect(struct ddpcb *ddp, struct mbuf *addr)
251 1.1 christos {
252 1.26 dyoung struct rtentry *rt;
253 1.25 dyoung const struct sockaddr_at *cdst;
254 1.1 christos struct sockaddr_at *sat = mtod(addr, struct sockaddr_at *);
255 1.23 dyoung struct route *ro;
256 1.25 dyoung struct at_ifaddr *aa;
257 1.1 christos struct ifnet *ifp;
258 1.1 christos u_short hintnet = 0, net;
259 1.1 christos
260 1.1 christos if (addr->m_len != sizeof(*sat))
261 1.25 dyoung return EINVAL;
262 1.1 christos if (sat->sat_family != AF_APPLETALK) {
263 1.25 dyoung return EAFNOSUPPORT;
264 1.1 christos }
265 1.1 christos /*
266 1.1 christos * Under phase 2, network 0 means "the network". We take "the
267 1.1 christos * network" to mean the network the control block is bound to.
268 1.1 christos * If the control block is not bound, there is an error.
269 1.1 christos */
270 1.1 christos if (sat->sat_addr.s_net == ATADDR_ANYNET
271 1.1 christos && sat->sat_addr.s_node != ATADDR_ANYNODE) {
272 1.1 christos if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT) {
273 1.25 dyoung return EADDRNOTAVAIL;
274 1.1 christos }
275 1.1 christos hintnet = ddp->ddp_lsat.sat_addr.s_net;
276 1.1 christos }
277 1.1 christos ro = &ddp->ddp_route;
278 1.1 christos /*
279 1.1 christos * If we've got an old route for this pcb, check that it is valid.
280 1.1 christos * If we've changed our address, we may have an old "good looking"
281 1.1 christos * route here. Attempt to detect it.
282 1.1 christos */
283 1.28 dyoung if ((rt = rtcache_validate(ro)) != NULL ||
284 1.28 dyoung (rt = rtcache_update(ro, 1)) != NULL) {
285 1.1 christos if (hintnet) {
286 1.1 christos net = hintnet;
287 1.1 christos } else {
288 1.1 christos net = sat->sat_addr.s_net;
289 1.1 christos }
290 1.26 dyoung if ((ifp = rt->rt_ifp) != NULL) {
291 1.25 dyoung TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
292 1.1 christos if (aa->aa_ifp == ifp &&
293 1.1 christos ntohs(net) >= ntohs(aa->aa_firstnet) &&
294 1.1 christos ntohs(net) <= ntohs(aa->aa_lastnet)) {
295 1.1 christos break;
296 1.1 christos }
297 1.1 christos }
298 1.25 dyoung } else
299 1.25 dyoung aa = NULL;
300 1.25 dyoung cdst = satocsat(rtcache_getdst(ro));
301 1.25 dyoung if (aa == NULL || (cdst->sat_addr.s_net !=
302 1.1 christos (hintnet ? hintnet : sat->sat_addr.s_net) ||
303 1.29 dyoung cdst->sat_addr.s_node != sat->sat_addr.s_node)) {
304 1.21 joerg rtcache_free(ro);
305 1.29 dyoung rt = NULL;
306 1.29 dyoung }
307 1.1 christos }
308 1.1 christos /*
309 1.1 christos * If we've got no route for this interface, try to find one.
310 1.1 christos */
311 1.29 dyoung if (rt == NULL) {
312 1.25 dyoung union {
313 1.25 dyoung struct sockaddr dst;
314 1.25 dyoung struct sockaddr_at dsta;
315 1.25 dyoung } u;
316 1.25 dyoung
317 1.25 dyoung sockaddr_at_init(&u.dsta, &sat->sat_addr, 0);
318 1.25 dyoung if (hintnet)
319 1.25 dyoung u.dsta.sat_addr.s_net = hintnet;
320 1.30 dyoung rt = rtcache_lookup(ro, &u.dst);
321 1.1 christos }
322 1.1 christos /*
323 1.1 christos * Make sure any route that we have has a valid interface.
324 1.1 christos */
325 1.27 dyoung if (rt != NULL && (ifp = rt->rt_ifp) != NULL) {
326 1.25 dyoung TAILQ_FOREACH(aa, &at_ifaddr, aa_list) {
327 1.25 dyoung if (aa->aa_ifp == ifp)
328 1.1 christos break;
329 1.1 christos }
330 1.25 dyoung } else
331 1.25 dyoung aa = NULL;
332 1.25 dyoung if (aa == NULL)
333 1.25 dyoung return ENETUNREACH;
334 1.1 christos ddp->ddp_fsat = *sat;
335 1.25 dyoung if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT)
336 1.55 rtr return at_pcbsetaddr(ddp, NULL);
337 1.25 dyoung return 0;
338 1.1 christos }
339 1.1 christos
340 1.1 christos static void
341 1.36 dsl at_pcbdisconnect(struct ddpcb *ddp)
342 1.1 christos {
343 1.1 christos ddp->ddp_fsat.sat_addr.s_net = ATADDR_ANYNET;
344 1.1 christos ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
345 1.1 christos ddp->ddp_fsat.sat_port = ATADDR_ANYPORT;
346 1.1 christos }
347 1.1 christos
348 1.1 christos static int
349 1.43 rmind ddp_attach(struct socket *so, int proto)
350 1.1 christos {
351 1.43 rmind struct ddpcb *ddp;
352 1.43 rmind int error;
353 1.43 rmind
354 1.43 rmind KASSERT(sotoddpcb(so) == NULL);
355 1.43 rmind sosetlock(so);
356 1.43 rmind #ifdef MBUFTRACE
357 1.43 rmind so->so_rcv.sb_mowner = &atalk_rx_mowner;
358 1.43 rmind so->so_snd.sb_mowner = &atalk_tx_mowner;
359 1.43 rmind #endif
360 1.43 rmind error = soreserve(so, ddp_sendspace, ddp_recvspace);
361 1.43 rmind if (error) {
362 1.43 rmind return error;
363 1.43 rmind }
364 1.1 christos
365 1.43 rmind ddp = kmem_zalloc(sizeof(*ddp), KM_SLEEP);
366 1.1 christos ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
367 1.1 christos
368 1.1 christos ddp->ddp_next = ddpcb;
369 1.1 christos ddp->ddp_prev = NULL;
370 1.1 christos ddp->ddp_pprev = NULL;
371 1.1 christos ddp->ddp_pnext = NULL;
372 1.1 christos if (ddpcb) {
373 1.1 christos ddpcb->ddp_prev = ddp;
374 1.1 christos }
375 1.1 christos ddpcb = ddp;
376 1.1 christos
377 1.1 christos ddp->ddp_socket = so;
378 1.43 rmind so->so_pcb = ddp;
379 1.25 dyoung return 0;
380 1.1 christos }
381 1.1 christos
382 1.1 christos static void
383 1.43 rmind ddp_detach(struct socket *so)
384 1.1 christos {
385 1.43 rmind struct ddpcb *ddp = sotoddpcb(so);
386 1.43 rmind
387 1.1 christos soisdisconnected(so);
388 1.43 rmind so->so_pcb = NULL;
389 1.32 ad /* sofree drops the lock */
390 1.1 christos sofree(so);
391 1.32 ad mutex_enter(softnet_lock);
392 1.1 christos
393 1.1 christos /* remove ddp from ddp_ports list */
394 1.1 christos if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT &&
395 1.1 christos ddp_ports[ddp->ddp_lsat.sat_port - 1] != NULL) {
396 1.1 christos if (ddp->ddp_pprev != NULL) {
397 1.1 christos ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext;
398 1.1 christos } else {
399 1.1 christos ddp_ports[ddp->ddp_lsat.sat_port - 1] = ddp->ddp_pnext;
400 1.1 christos }
401 1.1 christos if (ddp->ddp_pnext != NULL) {
402 1.1 christos ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
403 1.1 christos }
404 1.1 christos }
405 1.25 dyoung rtcache_free(&ddp->ddp_route);
406 1.1 christos if (ddp->ddp_prev) {
407 1.1 christos ddp->ddp_prev->ddp_next = ddp->ddp_next;
408 1.1 christos } else {
409 1.1 christos ddpcb = ddp->ddp_next;
410 1.1 christos }
411 1.1 christos if (ddp->ddp_next) {
412 1.1 christos ddp->ddp_next->ddp_prev = ddp->ddp_prev;
413 1.1 christos }
414 1.43 rmind kmem_free(ddp, sizeof(*ddp));
415 1.1 christos }
416 1.1 christos
417 1.45 rtr static int
418 1.53 rtr ddp_accept(struct socket *so, struct mbuf *nam)
419 1.53 rtr {
420 1.53 rtr KASSERT(solocked(so));
421 1.53 rtr
422 1.53 rtr return EOPNOTSUPP;
423 1.53 rtr }
424 1.53 rtr
425 1.53 rtr static int
426 1.58 rtr ddp_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
427 1.55 rtr {
428 1.55 rtr KASSERT(solocked(so));
429 1.55 rtr KASSERT(sotoddpcb(so) != NULL);
430 1.55 rtr
431 1.55 rtr return at_pcbsetaddr(sotoddpcb(so), nam);
432 1.55 rtr }
433 1.55 rtr
434 1.55 rtr static int
435 1.58 rtr ddp_listen(struct socket *so, struct lwp *l)
436 1.55 rtr {
437 1.55 rtr KASSERT(solocked(so));
438 1.55 rtr
439 1.55 rtr return EOPNOTSUPP;
440 1.55 rtr }
441 1.55 rtr
442 1.55 rtr static int
443 1.58 rtr ddp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
444 1.56 rtr {
445 1.56 rtr struct ddpcb *ddp = sotoddpcb(so);
446 1.56 rtr int error = 0;
447 1.56 rtr
448 1.56 rtr KASSERT(solocked(so));
449 1.56 rtr KASSERT(ddp != NULL);
450 1.56 rtr KASSERT(nam != NULL);
451 1.56 rtr
452 1.56 rtr if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT)
453 1.56 rtr return EISCONN;
454 1.56 rtr error = at_pcbconnect(ddp, nam);
455 1.56 rtr if (error == 0)
456 1.56 rtr soisconnected(so);
457 1.56 rtr
458 1.56 rtr return error;
459 1.56 rtr }
460 1.56 rtr
461 1.56 rtr static int
462 1.57 rtr ddp_disconnect(struct socket *so)
463 1.57 rtr {
464 1.57 rtr struct ddpcb *ddp = sotoddpcb(so);
465 1.57 rtr
466 1.57 rtr KASSERT(solocked(so));
467 1.57 rtr KASSERT(ddp != NULL);
468 1.57 rtr
469 1.57 rtr if (ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE)
470 1.57 rtr return ENOTCONN;
471 1.57 rtr
472 1.57 rtr at_pcbdisconnect(ddp);
473 1.57 rtr soisdisconnected(so);
474 1.57 rtr return 0;
475 1.57 rtr }
476 1.57 rtr
477 1.57 rtr static int
478 1.57 rtr ddp_shutdown(struct socket *so)
479 1.57 rtr {
480 1.57 rtr KASSERT(solocked(so));
481 1.57 rtr
482 1.57 rtr socantsendmore(so);
483 1.57 rtr return 0;
484 1.57 rtr }
485 1.57 rtr
486 1.57 rtr static int
487 1.57 rtr ddp_abort(struct socket *so)
488 1.57 rtr {
489 1.57 rtr KASSERT(solocked(so));
490 1.57 rtr
491 1.57 rtr soisdisconnected(so);
492 1.57 rtr ddp_detach(so);
493 1.57 rtr return 0;
494 1.57 rtr }
495 1.57 rtr
496 1.57 rtr static int
497 1.47 rtr ddp_ioctl(struct socket *so, u_long cmd, void *addr, struct ifnet *ifp)
498 1.45 rtr {
499 1.47 rtr return at_control(cmd, addr, ifp);
500 1.45 rtr }
501 1.45 rtr
502 1.48 rtr static int
503 1.48 rtr ddp_stat(struct socket *so, struct stat *ub)
504 1.48 rtr {
505 1.51 rtr KASSERT(solocked(so));
506 1.51 rtr
507 1.50 rtr /* stat: don't bother with a blocksize. */
508 1.50 rtr return 0;
509 1.48 rtr }
510 1.48 rtr
511 1.52 rtr static int
512 1.52 rtr ddp_peeraddr(struct socket *so, struct mbuf *nam)
513 1.52 rtr {
514 1.52 rtr KASSERT(solocked(so));
515 1.52 rtr
516 1.52 rtr return EOPNOTSUPP;
517 1.52 rtr }
518 1.52 rtr
519 1.52 rtr static int
520 1.52 rtr ddp_sockaddr(struct socket *so, struct mbuf *nam)
521 1.52 rtr {
522 1.52 rtr KASSERT(solocked(so));
523 1.52 rtr KASSERT(sotoddpcb(so) != NULL);
524 1.52 rtr KASSERT(nam != NULL);
525 1.52 rtr
526 1.52 rtr at_sockaddr(sotoddpcb(so), nam);
527 1.52 rtr return 0;
528 1.52 rtr }
529 1.52 rtr
530 1.54 rtr static int
531 1.54 rtr ddp_recvoob(struct socket *so, struct mbuf *m, int flags)
532 1.54 rtr {
533 1.54 rtr KASSERT(solocked(so));
534 1.54 rtr
535 1.54 rtr return EOPNOTSUPP;
536 1.54 rtr }
537 1.54 rtr
538 1.54 rtr static int
539 1.59 rtr ddp_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
540 1.59 rtr struct mbuf *control, struct lwp *l)
541 1.59 rtr {
542 1.59 rtr struct ddpcb *ddp = sotoddpcb(so);
543 1.59 rtr int error = 0;
544 1.59 rtr int s;
545 1.59 rtr
546 1.59 rtr KASSERT(solocked(so));
547 1.59 rtr KASSERT(ddp != NULL);
548 1.59 rtr KASSERT(nam != NULL);
549 1.59 rtr
550 1.59 rtr if (nam) {
551 1.59 rtr if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT)
552 1.59 rtr return EISCONN;
553 1.59 rtr s = splnet();
554 1.59 rtr error = at_pcbconnect(ddp, nam);
555 1.59 rtr if (error) {
556 1.59 rtr splx(s);
557 1.59 rtr return error;
558 1.59 rtr }
559 1.59 rtr } else {
560 1.59 rtr if (ddp->ddp_fsat.sat_port == ATADDR_ANYPORT)
561 1.59 rtr return ENOTCONN;
562 1.59 rtr }
563 1.59 rtr
564 1.59 rtr error = ddp_output(m, ddp);
565 1.59 rtr m = NULL;
566 1.59 rtr if (nam) {
567 1.59 rtr at_pcbdisconnect(ddp);
568 1.59 rtr splx(s);
569 1.59 rtr }
570 1.59 rtr
571 1.59 rtr return error;
572 1.59 rtr }
573 1.59 rtr
574 1.59 rtr static int
575 1.54 rtr ddp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
576 1.54 rtr {
577 1.54 rtr KASSERT(solocked(so));
578 1.54 rtr
579 1.54 rtr if (m)
580 1.54 rtr m_freem(m);
581 1.54 rtr
582 1.54 rtr return EOPNOTSUPP;
583 1.54 rtr }
584 1.54 rtr
585 1.1 christos /*
586 1.1 christos * For the moment, this just find the pcb with the correct local address.
587 1.1 christos * In the future, this will actually do some real searching, so we can use
588 1.1 christos * the sender's address to do de-multiplexing on a single port to many
589 1.1 christos * sockets (pcbs).
590 1.1 christos */
591 1.1 christos struct ddpcb *
592 1.19 christos ddp_search(
593 1.20 christos struct sockaddr_at *from,
594 1.19 christos struct sockaddr_at *to,
595 1.19 christos struct at_ifaddr *aa)
596 1.1 christos {
597 1.1 christos struct ddpcb *ddp;
598 1.1 christos
599 1.1 christos /*
600 1.1 christos * Check for bad ports.
601 1.1 christos */
602 1.25 dyoung if (to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST)
603 1.25 dyoung return NULL;
604 1.25 dyoung
605 1.1 christos /*
606 1.1 christos * Make sure the local address matches the sent address. What about
607 1.1 christos * the interface?
608 1.1 christos */
609 1.1 christos for (ddp = ddp_ports[to->sat_port - 1]; ddp; ddp = ddp->ddp_pnext) {
610 1.1 christos /* XXX should we handle 0.YY? */
611 1.1 christos
612 1.1 christos /* XXXX.YY to socket on destination interface */
613 1.1 christos if (to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net &&
614 1.1 christos to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node) {
615 1.1 christos break;
616 1.1 christos }
617 1.1 christos /* 0.255 to socket on receiving interface */
618 1.1 christos if (to->sat_addr.s_node == ATADDR_BCAST &&
619 1.1 christos (to->sat_addr.s_net == 0 ||
620 1.1 christos to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net) &&
621 1.1 christos ddp->ddp_lsat.sat_addr.s_net == AA_SAT(aa)->sat_addr.s_net) {
622 1.1 christos break;
623 1.1 christos }
624 1.1 christos /* XXXX.0 to socket on destination interface */
625 1.1 christos if (to->sat_addr.s_net == aa->aa_firstnet &&
626 1.1 christos to->sat_addr.s_node == 0 &&
627 1.1 christos ntohs(ddp->ddp_lsat.sat_addr.s_net) >=
628 1.1 christos ntohs(aa->aa_firstnet) &&
629 1.1 christos ntohs(ddp->ddp_lsat.sat_addr.s_net) <=
630 1.1 christos ntohs(aa->aa_lastnet)) {
631 1.1 christos break;
632 1.1 christos }
633 1.1 christos }
634 1.1 christos return (ddp);
635 1.1 christos }
636 1.1 christos
637 1.1 christos /*
638 1.1 christos * Initialize all the ddp & appletalk stuff
639 1.1 christos */
640 1.1 christos void
641 1.31 thorpej ddp_init(void)
642 1.1 christos {
643 1.31 thorpej
644 1.31 thorpej ddpstat_percpu = percpu_alloc(sizeof(uint64_t) * DDP_NSTATS);
645 1.31 thorpej
646 1.1 christos TAILQ_INIT(&at_ifaddr);
647 1.1 christos atintrq1.ifq_maxlen = IFQ_MAXLEN;
648 1.1 christos atintrq2.ifq_maxlen = IFQ_MAXLEN;
649 1.8 matt
650 1.8 matt MOWNER_ATTACH(&atalk_tx_mowner);
651 1.8 matt MOWNER_ATTACH(&atalk_rx_mowner);
652 1.40 bouyer MOWNER_ATTACH(&aarp_mowner);
653 1.1 christos }
654 1.1 christos
655 1.44 rmind PR_WRAP_USRREQS(ddp)
656 1.44 rmind #define ddp_attach ddp_attach_wrapper
657 1.44 rmind #define ddp_detach ddp_detach_wrapper
658 1.53 rtr #define ddp_accept ddp_accept_wrapper
659 1.55 rtr #define ddp_bind ddp_bind_wrapper
660 1.55 rtr #define ddp_listen ddp_listen_wrapper
661 1.56 rtr #define ddp_connect ddp_connect_wrapper
662 1.57 rtr #define ddp_disconnect ddp_disconnect_wrapper
663 1.57 rtr #define ddp_shutdown ddp_shutdown_wrapper
664 1.57 rtr #define ddp_abort ddp_abort_wrapper
665 1.45 rtr #define ddp_ioctl ddp_ioctl_wrapper
666 1.48 rtr #define ddp_stat ddp_stat_wrapper
667 1.52 rtr #define ddp_peeraddr ddp_peeraddr_wrapper
668 1.52 rtr #define ddp_sockaddr ddp_sockaddr_wrapper
669 1.54 rtr #define ddp_recvoob ddp_recvoob_wrapper
670 1.59 rtr #define ddp_send ddp_send_wrapper
671 1.54 rtr #define ddp_sendoob ddp_sendoob_wrapper
672 1.42 rmind #define ddp_usrreq ddp_usrreq_wrapper
673 1.1 christos
674 1.42 rmind const struct pr_usrreqs ddp_usrreqs = {
675 1.43 rmind .pr_attach = ddp_attach,
676 1.43 rmind .pr_detach = ddp_detach,
677 1.53 rtr .pr_accept = ddp_accept,
678 1.55 rtr .pr_bind = ddp_bind,
679 1.55 rtr .pr_listen = ddp_listen,
680 1.56 rtr .pr_connect = ddp_connect,
681 1.57 rtr .pr_disconnect = ddp_disconnect,
682 1.57 rtr .pr_shutdown = ddp_shutdown,
683 1.57 rtr .pr_abort = ddp_abort,
684 1.45 rtr .pr_ioctl = ddp_ioctl,
685 1.48 rtr .pr_stat = ddp_stat,
686 1.52 rtr .pr_peeraddr = ddp_peeraddr,
687 1.52 rtr .pr_sockaddr = ddp_sockaddr,
688 1.54 rtr .pr_recvoob = ddp_recvoob,
689 1.59 rtr .pr_send = ddp_send,
690 1.54 rtr .pr_sendoob = ddp_sendoob,
691 1.42 rmind .pr_generic = ddp_usrreq,
692 1.42 rmind };
693 1.31 thorpej
694 1.31 thorpej static int
695 1.31 thorpej sysctl_net_atalk_ddp_stats(SYSCTLFN_ARGS)
696 1.31 thorpej {
697 1.31 thorpej
698 1.33 thorpej return (NETSTAT_SYSCTL(ddpstat_percpu, DDP_NSTATS));
699 1.31 thorpej }
700 1.31 thorpej
701 1.31 thorpej /*
702 1.31 thorpej * Sysctl for DDP variables.
703 1.31 thorpej */
704 1.31 thorpej SYSCTL_SETUP(sysctl_net_atalk_ddp_setup, "sysctl net.atalk.ddp subtree setup")
705 1.31 thorpej {
706 1.31 thorpej
707 1.31 thorpej sysctl_createv(clog, 0, NULL, NULL,
708 1.31 thorpej CTLFLAG_PERMANENT,
709 1.31 thorpej CTLTYPE_NODE, "atalk", NULL,
710 1.31 thorpej NULL, 0, NULL, 0,
711 1.31 thorpej CTL_NET, PF_APPLETALK, CTL_EOL);
712 1.31 thorpej sysctl_createv(clog, 0, NULL, NULL,
713 1.31 thorpej CTLFLAG_PERMANENT,
714 1.31 thorpej CTLTYPE_NODE, "ddp",
715 1.31 thorpej SYSCTL_DESCR("DDP related settings"),
716 1.31 thorpej NULL, 0, NULL, 0,
717 1.31 thorpej CTL_NET, PF_APPLETALK, ATPROTO_DDP, CTL_EOL);
718 1.31 thorpej
719 1.31 thorpej sysctl_createv(clog, 0, NULL, NULL,
720 1.31 thorpej CTLFLAG_PERMANENT,
721 1.31 thorpej CTLTYPE_STRUCT, "stats",
722 1.31 thorpej SYSCTL_DESCR("DDP statistics"),
723 1.31 thorpej sysctl_net_atalk_ddp_stats, 0, NULL, 0,
724 1.31 thorpej CTL_NET, PF_APPLETALK, ATPROTO_DDP, CTL_CREATE,
725 1.31 thorpej CTL_EOL);
726 1.31 thorpej }
727