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