]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netatalk/ddp_pcb.c
Introduce support for Mandatory Access Control and extensible
[FreeBSD/FreeBSD.git] / sys / netatalk / ddp_pcb.c
1 /*
2  * Copyright (c) 1990,1994 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  *
5  * $FreeBSD$
6  */
7
8 #include <sys/param.h>
9 #include <sys/systm.h>
10 #include <sys/malloc.h>
11 #include <sys/mbuf.h>
12 #include <sys/socket.h>
13 #include <sys/socketvar.h>
14 #include <sys/protosw.h>
15 #include <net/if.h>
16 #include <net/route.h>
17 #include <net/intrq.h>
18
19 #include <netatalk/at.h>
20 #include <netatalk/at_var.h>
21 #include <netatalk/ddp_var.h>
22 #include <netatalk/at_extern.h>
23
24 static void at_pcbdisconnect( struct ddpcb *ddp );
25 static void at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr);
26 static int at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr,
27                           struct thread *td);
28 static int at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr, 
29                          struct thread *td);
30 static void at_pcbdetach(struct socket *so, struct ddpcb *ddp);
31 static int at_pcballoc(struct socket *so);
32
33 struct ddpcb    *ddp_ports[ ATPORT_LAST ];
34 struct ddpcb    *ddpcb = NULL;
35 static u_long   ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */
36 static u_long   ddp_recvspace = 10 * ( 587 + sizeof( struct sockaddr_at ));
37
38
39 static int
40 ddp_attach(struct socket *so, int proto, struct thread *td)
41 {
42         struct ddpcb    *ddp;
43         int             error = 0;
44         int             s;
45         
46
47         ddp = sotoddpcb( so );
48         if ( ddp != NULL ) {
49             return( EINVAL);
50         }
51
52         s = splnet();
53         error = at_pcballoc( so );
54         splx(s);
55         if (error) {
56             return (error);
57         }
58         return (soreserve( so, ddp_sendspace, ddp_recvspace ));
59 }
60
61 static int
62 ddp_detach(struct socket *so)
63 {
64         struct ddpcb    *ddp;
65         int             s;
66         
67         ddp = sotoddpcb( so );
68         if ( ddp == NULL ) {
69             return( EINVAL);
70         }
71         s = splnet();
72         at_pcbdetach( so, ddp );
73         splx(s);
74         return(0);
75 }
76
77 static int      
78 ddp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
79 {
80         struct ddpcb    *ddp;
81         int             error = 0;
82         int             s;
83         
84         ddp = sotoddpcb( so );
85         if ( ddp == NULL ) {
86             return( EINVAL);
87         }
88         s = splnet();
89         error = at_pcbsetaddr(ddp, nam, td);
90         splx(s);
91         return (error);
92 }
93     
94 static int
95 ddp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
96 {
97         struct ddpcb    *ddp;
98         int             error = 0;
99         int             s;
100         
101         ddp = sotoddpcb( so );
102         if ( ddp == NULL ) {
103             return( EINVAL);
104         }
105
106         if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
107             return(EISCONN);
108         }
109
110         s = splnet();
111         error = at_pcbconnect( ddp, nam, td );
112         splx(s);
113         if ( error == 0 )
114             soisconnected( so );
115         return(error);
116 }
117
118 static int
119 ddp_disconnect(struct socket *so)
120 {
121
122         struct ddpcb    *ddp;
123         int             s;
124         
125         ddp = sotoddpcb( so );
126         if ( ddp == NULL ) {
127             return( EINVAL);
128         }
129         if ( ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE ) {
130             return(ENOTCONN);
131         }
132
133         s = splnet();
134         at_pcbdisconnect( ddp );
135         ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
136         splx(s);
137         soisdisconnected( so );
138         return(0);
139 }
140
141 static int
142 ddp_shutdown(struct socket *so)
143 {
144         struct ddpcb    *ddp;
145
146         ddp = sotoddpcb( so );
147         if ( ddp == NULL ) {
148                 return( EINVAL);
149         }
150         socantsendmore( so );
151         return(0);
152 }
153
154 static int
155 ddp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
156             struct mbuf *control, struct thread *td)
157 {
158         struct ddpcb    *ddp;
159         int             error = 0;
160         int             s;
161         
162         ddp = sotoddpcb( so );
163         if ( ddp == NULL ) {
164                 return(EINVAL);
165         }
166
167         if ( control && control->m_len ) {
168                 return(EINVAL);
169         }
170
171         if ( addr ) {
172                 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
173                         return(EISCONN);
174                 }
175
176                 s = splnet();
177                 error = at_pcbconnect(ddp, addr, td);
178                 splx( s );
179                 if ( error ) {
180                         return(error);
181                 }
182         } else {
183                 if ( ddp->ddp_fsat.sat_port == ATADDR_ANYPORT ) {
184                         return(ENOTCONN);
185                 }
186         }
187
188         s = splnet();
189         error = ddp_output( m, so );
190         if ( addr ) {
191             at_pcbdisconnect( ddp );
192         }
193         splx(s);
194         return(error);
195 }
196
197 static int
198 ddp_abort(struct socket *so)
199 {
200         struct ddpcb    *ddp;
201         int             s;
202         
203         ddp = sotoddpcb( so );
204         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
215 static void
216 at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr)
217 {
218     *addr = dup_sockaddr((struct sockaddr *)&ddp->ddp_lsat, 0);
219 }
220
221 static int 
222 at_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
315 static int
316 at_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
410 static void 
411 at_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
418 static int
419 at_pcballoc( struct socket *so )
420 {
421         struct ddpcb    *ddp;
422
423         MALLOC(ddp, struct ddpcb *, sizeof *ddp, M_PCB, M_WAITOK | 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
440 static void
441 at_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  */
481 struct ddpcb *
482 ddp_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 }
526 static int
527 at_setpeeraddr(struct socket *so, struct sockaddr **nam)
528 {
529         return(EOPNOTSUPP);
530 }
531
532 static int
533 at_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
546 void 
547 ddp_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
558 static void 
559 ddp_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
569 struct 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 };