2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
5 * $DragonFly: src/sys/netproto/atalk/ddp_usrreq.c,v 1.8 2004/06/06 19:16:12 dillon Exp $
11 #include <sys/malloc.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
15 #include <sys/protosw.h>
17 #include <net/netisr.h>
18 #include <net/route.h>
23 #include "at_extern.h"
25 static void at_pcbdisconnect( struct ddpcb *ddp );
26 static void at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr);
27 static int at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr,
29 static int at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr,
31 static void at_pcbdetach(struct socket *so, struct ddpcb *ddp);
32 static int at_pcballoc(struct socket *so);
34 struct ddpcb *ddp_ports[ ATPORT_LAST ];
35 struct ddpcb *ddpcb = NULL;
36 static u_long ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */
37 static u_long ddp_recvspace = 10 * ( 587 + sizeof( struct sockaddr_at ));
40 ddp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
47 ddp = sotoddpcb( so );
53 error = at_pcballoc( so );
58 return (soreserve( so, ddp_sendspace, ddp_recvspace, ai->sb_rlimit ));
62 ddp_detach(struct socket *so)
67 ddp = sotoddpcb( so );
72 at_pcbdetach( so, ddp );
78 ddp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
84 ddp = sotoddpcb( so );
89 error = at_pcbsetaddr(ddp, nam, td);
95 ddp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
101 ddp = sotoddpcb( so );
106 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
111 error = at_pcbconnect( ddp, nam, td );
119 ddp_disconnect(struct socket *so)
125 ddp = sotoddpcb( so );
129 if ( ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE ) {
134 at_pcbdisconnect( ddp );
135 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
137 soisdisconnected( so );
142 ddp_shutdown(struct socket *so)
146 ddp = sotoddpcb( so );
150 socantsendmore( so );
155 ddp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
156 struct mbuf *control, struct thread *td)
162 ddp = sotoddpcb( so );
167 if ( control && control->m_len ) {
172 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
177 error = at_pcbconnect(ddp, addr, td);
183 if ( ddp->ddp_fsat.sat_port == ATADDR_ANYPORT ) {
189 error = ddp_output( m, so );
191 at_pcbdisconnect( ddp );
198 ddp_abort(struct socket *so)
203 ddp = sotoddpcb( so );
207 soisdisconnected( so );
209 at_pcbdetach( so, ddp );
216 at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr)
218 *addr = dup_sockaddr((struct sockaddr *)&ddp->ddp_lsat);
222 at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
224 struct sockaddr_at lsat, *sat;
225 struct at_ifaddr *aa;
228 if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT ) { /* shouldn't be bound */
232 if (addr != 0) { /* validate passed address */
233 sat = (struct sockaddr_at *)addr;
234 if (sat->sat_family != AF_APPLETALK) {
235 return(EAFNOSUPPORT);
238 if ( sat->sat_addr.s_node != ATADDR_ANYNODE ||
239 sat->sat_addr.s_net != ATADDR_ANYNET ) {
240 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
241 if (( sat->sat_addr.s_net == AA_SAT( aa )->sat_addr.s_net ) &&
242 ( sat->sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node )) {
247 return( EADDRNOTAVAIL );
251 if ( sat->sat_port != ATADDR_ANYPORT ) {
252 if ( sat->sat_port < ATPORT_FIRST ||
253 sat->sat_port >= ATPORT_LAST ) {
256 if ( sat->sat_port < ATPORT_RESERVED &&
262 bzero( (caddr_t)&lsat, sizeof( struct sockaddr_at ));
263 lsat.sat_len = sizeof(struct sockaddr_at);
264 lsat.sat_addr.s_node = ATADDR_ANYNODE;
265 lsat.sat_addr.s_net = ATADDR_ANYNET;
266 lsat.sat_family = AF_APPLETALK;
270 if ( sat->sat_addr.s_node == ATADDR_ANYNODE &&
271 sat->sat_addr.s_net == ATADDR_ANYNET ) {
272 if ( at_ifaddr == NULL ) {
273 return( EADDRNOTAVAIL );
275 sat->sat_addr = AA_SAT( at_ifaddr )->sat_addr;
277 ddp->ddp_lsat = *sat;
282 if ( sat->sat_port == ATADDR_ANYPORT ) {
283 for ( sat->sat_port = ATPORT_RESERVED;
284 sat->sat_port < ATPORT_LAST; sat->sat_port++ ) {
285 if ( ddp_ports[ sat->sat_port - 1 ] == 0 ) {
289 if ( sat->sat_port == ATPORT_LAST ) {
290 return( EADDRNOTAVAIL );
292 ddp->ddp_lsat.sat_port = sat->sat_port;
293 ddp_ports[ sat->sat_port - 1 ] = ddp;
295 for ( ddpp = ddp_ports[ sat->sat_port - 1 ]; ddpp;
296 ddpp = ddpp->ddp_pnext ) {
297 if ( ddpp->ddp_lsat.sat_addr.s_net == sat->sat_addr.s_net &&
298 ddpp->ddp_lsat.sat_addr.s_node == sat->sat_addr.s_node ) {
302 if ( ddpp != NULL ) {
303 return( EADDRINUSE );
305 ddp->ddp_pnext = ddp_ports[ sat->sat_port - 1 ];
306 ddp_ports[ sat->sat_port - 1 ] = ddp;
307 if ( ddp->ddp_pnext ) {
308 ddp->ddp_pnext->ddp_pprev = ddp;
316 at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
318 struct sockaddr_at *sat = (struct sockaddr_at *)addr;
320 struct at_ifaddr *aa = 0;
322 u_short hintnet = 0, net;
324 if (sat->sat_family != AF_APPLETALK) {
325 return(EAFNOSUPPORT);
329 * Under phase 2, network 0 means "the network". We take "the
330 * network" to mean the network the control block is bound to.
331 * If the control block is not bound, there is an error.
333 if ( sat->sat_addr.s_net == ATADDR_ANYNET
334 && sat->sat_addr.s_node != ATADDR_ANYNODE ) {
335 if ( ddp->ddp_lsat.sat_port == ATADDR_ANYPORT ) {
336 return( EADDRNOTAVAIL );
338 hintnet = ddp->ddp_lsat.sat_addr.s_net;
341 ro = &ddp->ddp_route;
343 * If we've got an old route for this pcb, check that it is valid.
344 * If we've changed our address, we may have an old "good looking"
345 * route here. Attempt to detect it.
351 net = sat->sat_addr.s_net;
354 if ((ifp = ro->ro_rt->rt_ifp) != NULL) {
355 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
356 if ( aa->aa_ifp == ifp &&
357 ntohs( net ) >= ntohs( aa->aa_firstnet ) &&
358 ntohs( net ) <= ntohs( aa->aa_lastnet )) {
363 if ( aa == NULL || ( satosat( &ro->ro_dst )->sat_addr.s_net !=
364 ( hintnet ? hintnet : sat->sat_addr.s_net ) ||
365 satosat( &ro->ro_dst )->sat_addr.s_node !=
366 sat->sat_addr.s_node )) {
368 ro->ro_rt = (struct rtentry *)0;
373 * If we've got no route for this interface, try to find one.
375 if ( ro->ro_rt == (struct rtentry *)0 ||
376 ro->ro_rt->rt_ifp == (struct ifnet *)0 ) {
377 ro->ro_dst.sa_len = sizeof( struct sockaddr_at );
378 ro->ro_dst.sa_family = AF_APPLETALK;
380 satosat( &ro->ro_dst )->sat_addr.s_net = hintnet;
382 satosat( &ro->ro_dst )->sat_addr.s_net = sat->sat_addr.s_net;
384 satosat( &ro->ro_dst )->sat_addr.s_node = sat->sat_addr.s_node;
389 * Make sure any route that we have has a valid interface.
392 if ( ro->ro_rt && ( ifp = ro->ro_rt->rt_ifp )) {
393 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
394 if ( aa->aa_ifp == ifp ) {
400 return( ENETUNREACH );
403 ddp->ddp_fsat = *sat;
404 if ( ddp->ddp_lsat.sat_port == ATADDR_ANYPORT ) {
405 return(at_pcbsetaddr(ddp, (struct sockaddr *)0, td));
411 at_pcbdisconnect( struct ddpcb *ddp )
413 ddp->ddp_fsat.sat_addr.s_net = ATADDR_ANYNET;
414 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
415 ddp->ddp_fsat.sat_port = ATADDR_ANYPORT;
419 at_pcballoc( struct socket *so )
423 MALLOC(ddp, struct ddpcb *, sizeof *ddp, M_PCB, M_WAITOK);
424 bzero(ddp, sizeof *ddp);
425 ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
427 ddp->ddp_next = ddpcb;
428 ddp->ddp_prev = NULL;
429 ddp->ddp_pprev = NULL;
430 ddp->ddp_pnext = NULL;
432 ddpcb->ddp_prev = ddp;
436 ddp->ddp_socket = so;
437 so->so_pcb = (caddr_t)ddp;
442 at_pcbdetach( struct socket *so, struct ddpcb *ddp)
444 soisdisconnected( so );
448 /* remove ddp from ddp_ports list */
449 if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT &&
450 ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] != NULL ) {
451 if ( ddp->ddp_pprev != NULL ) {
452 ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext;
454 ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] = ddp->ddp_pnext;
456 if ( ddp->ddp_pnext != NULL ) {
457 ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
461 if ( ddp->ddp_route.ro_rt ) {
462 rtfree( ddp->ddp_route.ro_rt );
465 if ( ddp->ddp_prev ) {
466 ddp->ddp_prev->ddp_next = ddp->ddp_next;
468 ddpcb = ddp->ddp_next;
470 if ( ddp->ddp_next ) {
471 ddp->ddp_next->ddp_prev = ddp->ddp_prev;
477 * For the moment, this just find the pcb with the correct local address.
478 * In the future, this will actually do some real searching, so we can use
479 * the sender's address to do de-multiplexing on a single port to many
483 ddp_search( struct sockaddr_at *from, struct sockaddr_at *to,
484 struct at_ifaddr *aa)
489 * Check for bad ports.
491 if ( to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST ) {
496 * Make sure the local address matches the sent address. What about
499 for ( ddp = ddp_ports[ to->sat_port - 1 ]; ddp; ddp = ddp->ddp_pnext ) {
500 /* XXX should we handle 0.YY? */
502 /* XXXX.YY to socket on destination interface */
503 if ( to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net &&
504 to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node ) {
508 /* 0.255 to socket on receiving interface */
509 if ( to->sat_addr.s_node == ATADDR_BCAST && ( to->sat_addr.s_net == 0 ||
510 to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net ) &&
511 ddp->ddp_lsat.sat_addr.s_net == AA_SAT( aa )->sat_addr.s_net ) {
515 /* XXXX.0 to socket on destination interface */
516 if ( to->sat_addr.s_net == aa->aa_firstnet &&
517 to->sat_addr.s_node == 0 &&
518 ntohs( ddp->ddp_lsat.sat_addr.s_net ) >=
519 ntohs( aa->aa_firstnet ) &&
520 ntohs( ddp->ddp_lsat.sat_addr.s_net ) <=
521 ntohs( aa->aa_lastnet )) {
528 at_setpeeraddr(struct socket *so, struct sockaddr **nam)
534 at_setsockaddr(struct socket *so, struct sockaddr **nam)
538 ddp = sotoddpcb( so );
542 at_sockaddr( ddp, nam );
550 netisr_register(NETISR_ATALK1, cpu0_portfn, at1intr);
551 netisr_register(NETISR_ATALK2, cpu0_portfn, at2intr);
552 netisr_register(NETISR_AARP, cpu0_portfn, aarpintr);
561 for ( ddp = ddpcb; ddp; ddp = ddp->ddp_next ) {
562 at_pcbdetach( ddp->ddp_socket, ddp );
567 struct pr_usrreqs ddp_usrreqs = {
573 pru_connect2_notsupp,