2 * Copyright (c) 2004-2009 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Copyright (c) 1990, 1994 Regents of The University of Michigan.
27 * All Rights Reserved.
29 * Permission to use, copy, modify, and distribute this software and
30 * its documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appears in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation, and that the name of The University
34 * of Michigan not be used in advertising or publicity pertaining to
35 * distribution of the software without specific, written prior
36 * permission. This software is supplied as is without expressed or
37 * implied warranties of any kind.
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
42 * Research Systems Unix Group
43 * The University of Michigan
45 * 535 W. William Street
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/protosw.h>
61 #include <net/route.h>
62 #include <net/netisr.h>
64 #include <netatalk/at.h>
65 #include <netatalk/at_var.h>
66 #include <netatalk/ddp_var.h>
67 #include <netatalk/ddp_pcb.h>
68 #include <netatalk/at_extern.h>
70 static u_long ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */
71 static u_long ddp_recvspace = 10 * (587 + sizeof(struct sockaddr_at));
73 static const struct netisr_handler atalk1_nh = {
75 .nh_handler = at1intr,
76 .nh_proto = NETISR_ATALK1,
77 .nh_policy = NETISR_POLICY_SOURCE,
80 static const struct netisr_handler atalk2_nh = {
82 .nh_handler = at2intr,
83 .nh_proto = NETISR_ATALK2,
84 .nh_policy = NETISR_POLICY_SOURCE,
87 static const struct netisr_handler aarp_nh = {
89 .nh_handler = aarpintr,
90 .nh_proto = NETISR_AARP,
91 .nh_policy = NETISR_POLICY_SOURCE,
95 ddp_attach(struct socket *so, int proto, struct thread *td)
99 KASSERT(sotoddpcb(so) == NULL, ("ddp_attach: ddp != NULL"));
102 * Allocate socket buffer space first so that it's present
105 error = soreserve(so, ddp_sendspace, ddp_recvspace);
110 error = at_pcballoc(so);
116 ddp_detach(struct socket *so)
121 KASSERT(ddp != NULL, ("ddp_detach: ddp == NULL"));
125 at_pcbdetach(so, ddp);
130 ddp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
136 KASSERT(ddp != NULL, ("ddp_bind: ddp == NULL"));
140 error = at_pcbsetaddr(ddp, nam, td);
147 ddp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
153 KASSERT(ddp != NULL, ("ddp_connect: ddp == NULL"));
157 if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT) {
163 error = at_pcbconnect( ddp, nam, td );
172 ddp_disconnect(struct socket *so)
177 KASSERT(ddp != NULL, ("ddp_disconnect: ddp == NULL"));
180 if (ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE) {
185 at_pcbdisconnect(ddp);
186 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
188 soisdisconnected(so);
193 ddp_shutdown(struct socket *so)
196 KASSERT(sotoddpcb(so) != NULL, ("ddp_shutdown: ddp == NULL"));
203 ddp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
204 struct mbuf *control, struct thread *td)
210 KASSERT(ddp != NULL, ("ddp_send: ddp == NULL"));
212 if (control && control->m_len)
218 if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT) {
223 error = at_pcbconnect(ddp, addr, td);
225 error = ddp_output(m, so);
226 at_pcbdisconnect(ddp);
233 if (ddp->ddp_fsat.sat_port == ATADDR_ANYPORT)
236 error = ddp_output(m, so);
243 * XXXRW: This is never called because we only invoke abort on stream
247 ddp_abort(struct socket *so)
252 KASSERT(ddp != NULL, ("ddp_abort: ddp == NULL"));
255 at_pcbdisconnect(ddp);
257 soisdisconnected(so);
261 ddp_close(struct socket *so)
266 KASSERT(ddp != NULL, ("ddp_close: ddp == NULL"));
269 at_pcbdisconnect(ddp);
271 soisdisconnected(so);
278 DDP_LIST_LOCK_INIT();
279 TAILQ_INIT(&at_ifaddrhead);
280 netisr_register(&atalk1_nh);
281 netisr_register(&atalk2_nh);
282 netisr_register(&aarp_nh);
291 for (ddp = ddpcb_list; ddp != NULL; ddp = ddp->ddp_next)
292 at_pcbdetach(ddp->ddp_socket, ddp);
293 DDP_LIST_LOCK_DESTROY();
298 at_getpeeraddr(struct socket *so, struct sockaddr **nam)
305 at_getsockaddr(struct socket *so, struct sockaddr **nam)
310 KASSERT(ddp != NULL, ("at_getsockaddr: ddp == NULL"));
313 at_sockaddr(ddp, nam);
318 struct pr_usrreqs ddp_usrreqs = {
319 .pru_abort = ddp_abort,
320 .pru_attach = ddp_attach,
321 .pru_bind = ddp_bind,
322 .pru_connect = ddp_connect,
323 .pru_control = at_control,
324 .pru_detach = ddp_detach,
325 .pru_disconnect = ddp_disconnect,
326 .pru_peeraddr = at_getpeeraddr,
327 .pru_send = ddp_send,
328 .pru_shutdown = ddp_shutdown,
329 .pru_sockaddr = at_getsockaddr,
330 .pru_close = ddp_close,