ddp_usrreq.c revision 109623
1353944Sdim/*
2353944Sdim * Copyright (c) 1990,1994 Regents of The University of Michigan.
3353944Sdim * All Rights Reserved.  See COPYRIGHT.
4353944Sdim *
5353944Sdim * $FreeBSD: head/sys/netatalk/ddp_usrreq.c 109623 2003-01-21 08:56:16Z alfred $
6353944Sdim */
7353944Sdim
8353944Sdim#include <sys/param.h>
9353944Sdim#include <sys/systm.h>
10353944Sdim#include <sys/malloc.h>
11353944Sdim#include <sys/mbuf.h>
12353944Sdim#include <sys/socket.h>
13353944Sdim#include <sys/socketvar.h>
14353944Sdim#include <sys/protosw.h>
15353944Sdim#include <net/if.h>
16353944Sdim#include <net/route.h>
17353944Sdim#include <net/intrq.h>
18353944Sdim
19353944Sdim#include <netatalk/at.h>
20353944Sdim#include <netatalk/at_var.h>
21353944Sdim#include <netatalk/ddp_var.h>
22353944Sdim#include <netatalk/at_extern.h>
23353944Sdim
24353944Sdimstatic void at_pcbdisconnect( struct ddpcb *ddp );
25353944Sdimstatic void at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr);
26353944Sdimstatic int at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr,
27353944Sdim			  struct thread *td);
28353944Sdimstatic int at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr,
29353944Sdim			 struct thread *td);
30353944Sdimstatic void at_pcbdetach(struct socket *so, struct ddpcb *ddp);
31353944Sdimstatic int at_pcballoc(struct socket *so);
32353944Sdim
33353944Sdimstruct ddpcb	*ddp_ports[ ATPORT_LAST ];
34353944Sdimstruct ddpcb	*ddpcb = NULL;
35353944Sdimstatic u_long	ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */
36353944Sdimstatic u_long	ddp_recvspace = 10 * ( 587 + sizeof( struct sockaddr_at ));
37353944Sdim
38353944Sdim
39353944Sdimstatic int
40353944Sdimddp_attach(struct socket *so, int proto, struct thread *td)
41353944Sdim{
42353944Sdim	struct ddpcb	*ddp;
43353944Sdim	int		error = 0;
44353944Sdim	int		s;
45353944Sdim
46353944Sdim
47353944Sdim	ddp = sotoddpcb( so );
48353944Sdim	if ( ddp != NULL ) {
49353944Sdim	    return( EINVAL);
50353944Sdim	}
51353944Sdim
52353944Sdim	s = splnet();
53353944Sdim	error = at_pcballoc( so );
54353944Sdim	splx(s);
55353944Sdim	if (error) {
56353944Sdim	    return (error);
57353944Sdim	}
58353944Sdim	return (soreserve( so, ddp_sendspace, ddp_recvspace ));
59353944Sdim}
60353944Sdim
61353944Sdimstatic int
62353944Sdimddp_detach(struct socket *so)
63353944Sdim{
64353944Sdim	struct ddpcb	*ddp;
65353944Sdim	int		s;
66353944Sdim
67353944Sdim	ddp = sotoddpcb( so );
68353944Sdim	if ( ddp == NULL ) {
69353944Sdim	    return( EINVAL);
70353944Sdim	}
71353944Sdim	s = splnet();
72353944Sdim	at_pcbdetach( so, ddp );
73353944Sdim	splx(s);
74353944Sdim	return(0);
75353944Sdim}
76353944Sdim
77353944Sdimstatic int
78353944Sdimddp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
79353944Sdim{
80353944Sdim	struct ddpcb	*ddp;
81353944Sdim	int		error = 0;
82353944Sdim	int		s;
83353944Sdim
84353944Sdim	ddp = sotoddpcb( so );
85353944Sdim	if ( ddp == NULL ) {
86353944Sdim	    return( EINVAL);
87353944Sdim	}
88353944Sdim	s = splnet();
89353944Sdim	error = at_pcbsetaddr(ddp, nam, td);
90353944Sdim	splx(s);
91353944Sdim	return (error);
92353944Sdim}
93353944Sdim
94353944Sdimstatic int
95353944Sdimddp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
96353944Sdim{
97353944Sdim	struct ddpcb	*ddp;
98353944Sdim	int		error = 0;
99353944Sdim	int		s;
100353944Sdim
101353944Sdim	ddp = sotoddpcb( so );
102353944Sdim	if ( ddp == NULL ) {
103353944Sdim	    return( EINVAL);
104353944Sdim	}
105353944Sdim
106353944Sdim	if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
107353944Sdim	    return(EISCONN);
108353944Sdim	}
109353944Sdim
110353944Sdim	s = splnet();
111353944Sdim	error = at_pcbconnect( ddp, nam, td );
112353944Sdim	splx(s);
113353944Sdim	if ( error == 0 )
114353944Sdim	    soisconnected( so );
115353944Sdim	return(error);
116353944Sdim}
117353944Sdim
118353944Sdimstatic int
119353944Sdimddp_disconnect(struct socket *so)
120353944Sdim{
121353944Sdim
122353944Sdim	struct ddpcb	*ddp;
123353944Sdim	int		s;
124353944Sdim
125353944Sdim	ddp = sotoddpcb( so );
126353944Sdim	if ( ddp == NULL ) {
127353944Sdim	    return( EINVAL);
128353944Sdim	}
129353944Sdim	if ( ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE ) {
130353944Sdim	    return(ENOTCONN);
131353944Sdim	}
132353944Sdim
133353944Sdim	s = splnet();
134353944Sdim	at_pcbdisconnect( ddp );
135353944Sdim	ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
136353944Sdim	splx(s);
137353944Sdim	soisdisconnected( so );
138353944Sdim	return(0);
139353944Sdim}
140353944Sdim
141353944Sdimstatic int
142353944Sdimddp_shutdown(struct socket *so)
143353944Sdim{
144353944Sdim	struct ddpcb	*ddp;
145353944Sdim
146353944Sdim	ddp = sotoddpcb( so );
147353944Sdim	if ( ddp == NULL ) {
148353944Sdim		return( EINVAL);
149353944Sdim	}
150353944Sdim	socantsendmore( so );
151353944Sdim	return(0);
152353944Sdim}
153353944Sdim
154353944Sdimstatic int
155353944Sdimddp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
156353944Sdim            struct mbuf *control, struct thread *td)
157353944Sdim{
158353944Sdim	struct ddpcb	*ddp;
159353944Sdim	int		error = 0;
160353944Sdim	int		s;
161353944Sdim
162353944Sdim	ddp = sotoddpcb( so );
163353944Sdim	if ( ddp == NULL ) {
164353944Sdim		return(EINVAL);
165353944Sdim	}
166353944Sdim
167353944Sdim    	if ( control && control->m_len ) {
168353944Sdim		return(EINVAL);
169353944Sdim    	}
170353944Sdim
171353944Sdim	if ( addr ) {
172353944Sdim		if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
173353944Sdim			return(EISCONN);
174353944Sdim		}
175353944Sdim
176353944Sdim		s = splnet();
177353944Sdim		error = at_pcbconnect(ddp, addr, td);
178353944Sdim		splx( s );
179353944Sdim		if ( error ) {
180353944Sdim			return(error);
181353944Sdim		}
182353944Sdim	} else {
183353944Sdim		if ( ddp->ddp_fsat.sat_port == ATADDR_ANYPORT ) {
184353944Sdim			return(ENOTCONN);
185353944Sdim		}
186353944Sdim	}
187353944Sdim
188353944Sdim	s = splnet();
189353944Sdim	error = ddp_output( m, so );
190353944Sdim	if ( addr ) {
191353944Sdim	    at_pcbdisconnect( ddp );
192353944Sdim	}
193353944Sdim	splx(s);
194353944Sdim	return(error);
195353944Sdim}
196353944Sdim
197353944Sdimstatic int
198353944Sdimddp_abort(struct socket *so)
199353944Sdim{
200353944Sdim	struct ddpcb	*ddp;
201353944Sdim	int		s;
202353944Sdim
203353944Sdim	ddp = sotoddpcb( so );
204353944Sdim	if ( ddp == NULL ) {
205		return(EINVAL);
206	}
207	soisdisconnected( so );
208	s = splnet();
209	at_pcbdetach( so, ddp );
210	splx(s);
211	return(0);
212}
213
214
215static void
216at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr)
217{
218    *addr = dup_sockaddr((struct sockaddr *)&ddp->ddp_lsat, 0);
219}
220
221static int
222at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
223{
224    struct sockaddr_at	lsat, *sat;
225    struct at_ifaddr	*aa;
226    struct ddpcb	*ddpp;
227
228    if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT ) { /* shouldn't be bound */
229	return( EINVAL );
230    }
231
232    if (addr != 0) {			/* validate passed address */
233	sat = (struct sockaddr_at *)addr;
234	if (sat->sat_family != AF_APPLETALK) {
235	    return(EAFNOSUPPORT);
236	}
237
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 )) {
243		    break;
244		}
245	    }
246	    if ( !aa ) {
247		return( EADDRNOTAVAIL );
248	    }
249	}
250
251	if ( sat->sat_port != ATADDR_ANYPORT ) {
252	    if ( sat->sat_port < ATPORT_FIRST ||
253		    sat->sat_port >= ATPORT_LAST ) {
254		return( EINVAL );
255	    }
256	    if ( sat->sat_port < ATPORT_RESERVED &&
257		 suser(td) ) {
258		return( EACCES );
259	    }
260	}
261    } else {
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;
267	sat = &lsat;
268    }
269
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 );
274	}
275	sat->sat_addr = AA_SAT( at_ifaddr )->sat_addr;
276    }
277    ddp->ddp_lsat = *sat;
278
279    /*
280     * Choose port.
281     */
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 ) {
286		break;
287	    }
288	}
289	if ( sat->sat_port == ATPORT_LAST ) {
290	    return( EADDRNOTAVAIL );
291	}
292	ddp->ddp_lsat.sat_port = sat->sat_port;
293	ddp_ports[ sat->sat_port - 1 ] = ddp;
294    } else {
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 ) {
299		break;
300	    }
301	}
302	if ( ddpp != NULL ) {
303	    return( EADDRINUSE );
304	}
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;
309	}
310    }
311
312    return( 0 );
313}
314
315static int
316at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
317{
318    struct sockaddr_at	*sat = (struct sockaddr_at *)addr;
319    struct route	*ro;
320    struct at_ifaddr	*aa = 0;
321    struct ifnet	*ifp;
322    u_short		hintnet = 0, net;
323
324    if (sat->sat_family != AF_APPLETALK) {
325	return(EAFNOSUPPORT);
326    }
327
328    /*
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.
332     */
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 );
337	}
338	hintnet = ddp->ddp_lsat.sat_addr.s_net;
339    }
340
341    ro = &ddp->ddp_route;
342    /*
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.
346     */
347    if ( ro->ro_rt ) {
348	if ( hintnet ) {
349	    net = hintnet;
350	} else {
351	    net = sat->sat_addr.s_net;
352	}
353	aa = 0;
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 )) {
359		    break;
360		}
361	    }
362	}
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 )) {
367	    RTFREE( ro->ro_rt );
368	    ro->ro_rt = (struct rtentry *)0;
369	}
370    }
371
372    /*
373     * If we've got no route for this interface, try to find one.
374     */
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;
379	if ( hintnet ) {
380	    satosat( &ro->ro_dst )->sat_addr.s_net = hintnet;
381	} else {
382	    satosat( &ro->ro_dst )->sat_addr.s_net = sat->sat_addr.s_net;
383	}
384	satosat( &ro->ro_dst )->sat_addr.s_node = sat->sat_addr.s_node;
385	rtalloc( ro );
386    }
387
388    /*
389     * Make sure any route that we have has a valid interface.
390     */
391    aa = 0;
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 ) {
395		break;
396	    }
397	}
398    }
399    if ( aa == 0 ) {
400	return( ENETUNREACH );
401    }
402
403    ddp->ddp_fsat = *sat;
404    if ( ddp->ddp_lsat.sat_port == ATADDR_ANYPORT ) {
405	return(at_pcbsetaddr(ddp, (struct sockaddr *)0, td));
406    }
407    return( 0 );
408}
409
410static void
411at_pcbdisconnect( struct ddpcb	*ddp )
412{
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;
416}
417
418static int
419at_pcballoc( struct socket *so )
420{
421	struct ddpcb	*ddp;
422
423	MALLOC(ddp, struct ddpcb *, sizeof *ddp, M_PCB, M_ZERO);
424	ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
425
426	ddp->ddp_next = ddpcb;
427	ddp->ddp_prev = NULL;
428	ddp->ddp_pprev = NULL;
429	ddp->ddp_pnext = NULL;
430	if (ddpcb) {
431		ddpcb->ddp_prev = ddp;
432	}
433	ddpcb = ddp;
434
435	ddp->ddp_socket = so;
436	so->so_pcb = (caddr_t)ddp;
437	return(0);
438}
439
440static void
441at_pcbdetach( struct socket *so, struct ddpcb *ddp)
442{
443    soisdisconnected( so );
444    so->so_pcb = 0;
445    sotryfree(so);
446
447    /* remove ddp from ddp_ports list */
448    if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT &&
449	    ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] != NULL ) {
450	if ( ddp->ddp_pprev != NULL ) {
451	    ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext;
452	} else {
453	    ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] = ddp->ddp_pnext;
454	}
455	if ( ddp->ddp_pnext != NULL ) {
456	    ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
457	}
458    }
459
460    if ( ddp->ddp_route.ro_rt ) {
461	rtfree( ddp->ddp_route.ro_rt );
462    }
463
464    if ( ddp->ddp_prev ) {
465	ddp->ddp_prev->ddp_next = ddp->ddp_next;
466    } else {
467	ddpcb = ddp->ddp_next;
468    }
469    if ( ddp->ddp_next ) {
470	ddp->ddp_next->ddp_prev = ddp->ddp_prev;
471    }
472    FREE(ddp, M_PCB);
473}
474
475/*
476 * For the moment, this just find the pcb with the correct local address.
477 * In the future, this will actually do some real searching, so we can use
478 * the sender's address to do de-multiplexing on a single port to many
479 * sockets (pcbs).
480 */
481struct ddpcb *
482ddp_search( struct sockaddr_at *from, struct sockaddr_at *to,
483			struct at_ifaddr *aa)
484{
485    struct ddpcb	*ddp;
486
487    /*
488     * Check for bad ports.
489     */
490    if ( to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST ) {
491	return( NULL );
492    }
493
494    /*
495     * Make sure the local address matches the sent address.  What about
496     * the interface?
497     */
498    for ( ddp = ddp_ports[ to->sat_port - 1 ]; ddp; ddp = ddp->ddp_pnext ) {
499	/* XXX should we handle 0.YY? */
500
501	/* XXXX.YY to socket on destination interface */
502	if ( to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net &&
503		to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node ) {
504	    break;
505	}
506
507	/* 0.255 to socket on receiving interface */
508	if ( to->sat_addr.s_node == ATADDR_BCAST && ( to->sat_addr.s_net == 0 ||
509		to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net ) &&
510		ddp->ddp_lsat.sat_addr.s_net == AA_SAT( aa )->sat_addr.s_net ) {
511	    break;
512	}
513
514	/* XXXX.0 to socket on destination interface */
515	if ( to->sat_addr.s_net == aa->aa_firstnet &&
516		to->sat_addr.s_node == 0 &&
517		ntohs( ddp->ddp_lsat.sat_addr.s_net ) >=
518		ntohs( aa->aa_firstnet ) &&
519		ntohs( ddp->ddp_lsat.sat_addr.s_net ) <=
520		ntohs( aa->aa_lastnet )) {
521	    break;
522	}
523    }
524    return( ddp );
525}
526static int
527at_setpeeraddr(struct socket *so, struct sockaddr **nam)
528{
529	return(EOPNOTSUPP);
530}
531
532static int
533at_setsockaddr(struct socket *so, struct sockaddr **nam)
534{
535	struct ddpcb	*ddp;
536
537	ddp = sotoddpcb( so );
538	if ( ddp == NULL ) {
539	    return( EINVAL);
540	}
541	at_sockaddr( ddp, nam );
542	return(0);
543}
544
545
546void
547ddp_init(void )
548{
549    atintrq1.ifq_maxlen = IFQ_MAXLEN;
550    atintrq2.ifq_maxlen = IFQ_MAXLEN;
551    atintrq1_present = 1;
552    atintrq2_present = 1;
553    mtx_init(&atintrq1.ifq_mtx, "at1_inq", NULL, MTX_DEF);
554    mtx_init(&atintrq2.ifq_mtx, "at2_inq", NULL, MTX_DEF);
555}
556
557#if 0
558static void
559ddp_clean(void )
560{
561    struct ddpcb	*ddp;
562
563    for ( ddp = ddpcb; ddp; ddp = ddp->ddp_next ) {
564	at_pcbdetach( ddp->ddp_socket, ddp );
565    }
566}
567#endif
568
569struct pr_usrreqs ddp_usrreqs = {
570	ddp_abort,
571	pru_accept_notsupp,
572	ddp_attach,
573	ddp_bind,
574	ddp_connect,
575	pru_connect2_notsupp,
576	at_control,
577	ddp_detach,
578	ddp_disconnect,
579	pru_listen_notsupp,
580	at_setpeeraddr,
581	pru_rcvd_notsupp,
582	pru_rcvoob_notsupp,
583	ddp_send,
584	pru_sense_null,
585	ddp_shutdown,
586	at_setsockaddr,
587	sosend,
588	soreceive,
589	sopoll
590};
591