HAMMER Utilities: MFC work to date.
[dragonfly.git] / sys / netproto / atalk / ddp_usrreq.c
blob62bee57c415b035c2e9a08e2deaa5810dba5b5e6
1 /*
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.12 2008/01/05 14:02:40 swildner Exp $
6 */
8 #include <sys/param.h>
9 #include <sys/systm.h>
10 #include <sys/proc.h>
11 #include <sys/malloc.h>
12 #include <sys/mbuf.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
15 #include <sys/protosw.h>
16 #include <sys/thread2.h>
17 #include <net/if.h>
18 #include <net/netisr.h>
19 #include <net/route.h>
21 #include "at.h"
22 #include "at_var.h"
23 #include "ddp_var.h"
24 #include "at_extern.h"
26 static void at_pcbdisconnect( struct ddpcb *ddp );
27 static void at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr);
28 static int at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr,
29 struct thread *td);
30 static int at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr,
31 struct thread *td);
32 static void at_pcbdetach(struct socket *so, struct ddpcb *ddp);
33 static int at_pcballoc(struct socket *so);
35 struct ddpcb *ddp_ports[ ATPORT_LAST ];
36 struct ddpcb *ddpcb = NULL;
37 static u_long ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */
38 static u_long ddp_recvspace = 10 * ( 587 + sizeof( struct sockaddr_at ));
40 static int
41 ddp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
43 struct ddpcb *ddp;
44 int error = 0;
47 ddp = sotoddpcb( so );
48 if ( ddp != NULL ) {
49 return( EINVAL);
52 crit_enter();
53 error = at_pcballoc( so );
54 crit_exit();
55 if (error) {
56 return (error);
58 return (soreserve( so, ddp_sendspace, ddp_recvspace, ai->sb_rlimit ));
61 static int
62 ddp_detach(struct socket *so)
64 struct ddpcb *ddp;
66 ddp = sotoddpcb( so );
67 if ( ddp == NULL ) {
68 return( EINVAL);
70 crit_enter();
71 at_pcbdetach( so, ddp );
72 crit_exit();
73 return(0);
76 static int
77 ddp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
79 struct ddpcb *ddp;
80 int error = 0;
82 ddp = sotoddpcb( so );
83 if ( ddp == NULL ) {
84 return( EINVAL);
86 crit_enter();
87 error = at_pcbsetaddr(ddp, nam, td);
88 crit_exit();
89 return (error);
92 static int
93 ddp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
95 struct ddpcb *ddp;
96 int error = 0;
98 ddp = sotoddpcb( so );
99 if ( ddp == NULL ) {
100 return( EINVAL);
103 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
104 return(EISCONN);
107 crit_enter();
108 error = at_pcbconnect( ddp, nam, td );
109 crit_exit();
110 if ( error == 0 )
111 soisconnected( so );
112 return(error);
115 static int
116 ddp_disconnect(struct socket *so)
119 struct ddpcb *ddp;
121 ddp = sotoddpcb( so );
122 if ( ddp == NULL ) {
123 return( EINVAL);
125 if ( ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE ) {
126 return(ENOTCONN);
129 crit_enter();
130 at_pcbdisconnect( ddp );
131 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
132 crit_exit();
133 soisdisconnected( so );
134 return(0);
137 static int
138 ddp_shutdown(struct socket *so)
140 struct ddpcb *ddp;
142 ddp = sotoddpcb( so );
143 if ( ddp == NULL ) {
144 return( EINVAL);
146 socantsendmore( so );
147 return(0);
150 static int
151 ddp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
152 struct mbuf *control, struct thread *td)
154 struct ddpcb *ddp;
155 int error = 0;
157 ddp = sotoddpcb( so );
158 if ( ddp == NULL ) {
159 return(EINVAL);
162 if ( control && control->m_len ) {
163 return(EINVAL);
166 if ( addr ) {
167 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
168 return(EISCONN);
171 crit_enter();
172 error = at_pcbconnect(ddp, addr, td);
173 crit_exit();
174 if ( error ) {
175 return(error);
177 } else {
178 if ( ddp->ddp_fsat.sat_port == ATADDR_ANYPORT ) {
179 return(ENOTCONN);
183 crit_enter();
184 error = ddp_output( m, so );
185 if ( addr ) {
186 at_pcbdisconnect( ddp );
188 crit_exit();
189 return(error);
192 static int
193 ddp_abort(struct socket *so)
195 struct ddpcb *ddp;
197 ddp = sotoddpcb( so );
198 if ( ddp == NULL ) {
199 return(EINVAL);
201 soisdisconnected( so );
202 crit_enter();
203 at_pcbdetach( so, ddp );
204 crit_exit();
205 return(0);
209 static void
210 at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr)
212 *addr = dup_sockaddr((struct sockaddr *)&ddp->ddp_lsat);
215 static int
216 at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
218 struct sockaddr_at lsat, *sat;
219 struct at_ifaddr *aa;
220 struct ddpcb *ddpp;
222 if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT ) { /* shouldn't be bound */
223 return( EINVAL );
226 if (addr != 0) { /* validate passed address */
227 sat = (struct sockaddr_at *)addr;
228 if (sat->sat_family != AF_APPLETALK) {
229 return(EAFNOSUPPORT);
232 if ( sat->sat_addr.s_node != ATADDR_ANYNODE ||
233 sat->sat_addr.s_net != ATADDR_ANYNET ) {
234 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
235 if (( sat->sat_addr.s_net == AA_SAT( aa )->sat_addr.s_net ) &&
236 ( sat->sat_addr.s_node == AA_SAT( aa )->sat_addr.s_node )) {
237 break;
240 if ( !aa ) {
241 return( EADDRNOTAVAIL );
245 if ( sat->sat_port != ATADDR_ANYPORT ) {
246 if ( sat->sat_port < ATPORT_FIRST ||
247 sat->sat_port >= ATPORT_LAST ) {
248 return( EINVAL );
250 if ( sat->sat_port < ATPORT_RESERVED &&
251 suser(td) ) {
252 return( EACCES );
255 } else {
256 bzero( (caddr_t)&lsat, sizeof( struct sockaddr_at ));
257 lsat.sat_len = sizeof(struct sockaddr_at);
258 lsat.sat_addr.s_node = ATADDR_ANYNODE;
259 lsat.sat_addr.s_net = ATADDR_ANYNET;
260 lsat.sat_family = AF_APPLETALK;
261 sat = &lsat;
264 if ( sat->sat_addr.s_node == ATADDR_ANYNODE &&
265 sat->sat_addr.s_net == ATADDR_ANYNET ) {
266 if ( at_ifaddr == NULL ) {
267 return( EADDRNOTAVAIL );
269 sat->sat_addr = AA_SAT( at_ifaddr )->sat_addr;
271 ddp->ddp_lsat = *sat;
274 * Choose port.
276 if ( sat->sat_port == ATADDR_ANYPORT ) {
277 for ( sat->sat_port = ATPORT_RESERVED;
278 sat->sat_port < ATPORT_LAST; sat->sat_port++ ) {
279 if ( ddp_ports[ sat->sat_port - 1 ] == 0 ) {
280 break;
283 if ( sat->sat_port == ATPORT_LAST ) {
284 return( EADDRNOTAVAIL );
286 ddp->ddp_lsat.sat_port = sat->sat_port;
287 ddp_ports[ sat->sat_port - 1 ] = ddp;
288 } else {
289 for ( ddpp = ddp_ports[ sat->sat_port - 1 ]; ddpp;
290 ddpp = ddpp->ddp_pnext ) {
291 if ( ddpp->ddp_lsat.sat_addr.s_net == sat->sat_addr.s_net &&
292 ddpp->ddp_lsat.sat_addr.s_node == sat->sat_addr.s_node ) {
293 break;
296 if ( ddpp != NULL ) {
297 return( EADDRINUSE );
299 ddp->ddp_pnext = ddp_ports[ sat->sat_port - 1 ];
300 ddp_ports[ sat->sat_port - 1 ] = ddp;
301 if ( ddp->ddp_pnext ) {
302 ddp->ddp_pnext->ddp_pprev = ddp;
306 return( 0 );
309 static int
310 at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
312 struct sockaddr_at *sat = (struct sockaddr_at *)addr;
313 struct route *ro;
314 struct at_ifaddr *aa = 0;
315 struct ifnet *ifp;
316 u_short hintnet = 0, net;
318 if (sat->sat_family != AF_APPLETALK) {
319 return(EAFNOSUPPORT);
323 * Under phase 2, network 0 means "the network". We take "the
324 * network" to mean the network the control block is bound to.
325 * If the control block is not bound, there is an error.
327 if ( sat->sat_addr.s_net == ATADDR_ANYNET
328 && sat->sat_addr.s_node != ATADDR_ANYNODE ) {
329 if ( ddp->ddp_lsat.sat_port == ATADDR_ANYPORT ) {
330 return( EADDRNOTAVAIL );
332 hintnet = ddp->ddp_lsat.sat_addr.s_net;
335 ro = &ddp->ddp_route;
337 * If we've got an old route for this pcb, check that it is valid.
338 * If we've changed our address, we may have an old "good looking"
339 * route here. Attempt to detect it.
341 if ( ro->ro_rt ) {
342 if ( hintnet ) {
343 net = hintnet;
344 } else {
345 net = sat->sat_addr.s_net;
347 aa = 0;
348 if ((ifp = ro->ro_rt->rt_ifp) != NULL) {
349 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
350 if ( aa->aa_ifp == ifp &&
351 ntohs( net ) >= ntohs( aa->aa_firstnet ) &&
352 ntohs( net ) <= ntohs( aa->aa_lastnet )) {
353 break;
357 if ( aa == NULL || ( satosat( &ro->ro_dst )->sat_addr.s_net !=
358 ( hintnet ? hintnet : sat->sat_addr.s_net ) ||
359 satosat( &ro->ro_dst )->sat_addr.s_node !=
360 sat->sat_addr.s_node )) {
361 RTFREE( ro->ro_rt );
362 ro->ro_rt = (struct rtentry *)0;
367 * If we've got no route for this interface, try to find one.
369 if ( ro->ro_rt == (struct rtentry *)0 ||
370 ro->ro_rt->rt_ifp == (struct ifnet *)0 ) {
371 ro->ro_dst.sa_len = sizeof( struct sockaddr_at );
372 ro->ro_dst.sa_family = AF_APPLETALK;
373 if ( hintnet ) {
374 satosat( &ro->ro_dst )->sat_addr.s_net = hintnet;
375 } else {
376 satosat( &ro->ro_dst )->sat_addr.s_net = sat->sat_addr.s_net;
378 satosat( &ro->ro_dst )->sat_addr.s_node = sat->sat_addr.s_node;
379 rtalloc( ro );
383 * Make sure any route that we have has a valid interface.
385 aa = 0;
386 if ( ro->ro_rt && ( ifp = ro->ro_rt->rt_ifp )) {
387 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
388 if ( aa->aa_ifp == ifp ) {
389 break;
393 if ( aa == 0 ) {
394 return( ENETUNREACH );
397 ddp->ddp_fsat = *sat;
398 if ( ddp->ddp_lsat.sat_port == ATADDR_ANYPORT ) {
399 return(at_pcbsetaddr(ddp, (struct sockaddr *)0, td));
401 return( 0 );
404 static void
405 at_pcbdisconnect( struct ddpcb *ddp )
407 ddp->ddp_fsat.sat_addr.s_net = ATADDR_ANYNET;
408 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
409 ddp->ddp_fsat.sat_port = ATADDR_ANYPORT;
412 static int
413 at_pcballoc( struct socket *so )
415 struct ddpcb *ddp;
417 MALLOC(ddp, struct ddpcb *, sizeof *ddp, M_PCB, M_WAITOK | M_ZERO);
418 ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
420 ddp->ddp_next = ddpcb;
421 ddp->ddp_prev = NULL;
422 ddp->ddp_pprev = NULL;
423 ddp->ddp_pnext = NULL;
424 if (ddpcb) {
425 ddpcb->ddp_prev = ddp;
427 ddpcb = ddp;
429 ddp->ddp_socket = so;
430 so->so_pcb = (caddr_t)ddp;
431 return(0);
434 static void
435 at_pcbdetach( struct socket *so, struct ddpcb *ddp)
437 soisdisconnected( so );
438 so->so_pcb = 0;
439 sofree( so );
441 /* remove ddp from ddp_ports list */
442 if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT &&
443 ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] != NULL ) {
444 if ( ddp->ddp_pprev != NULL ) {
445 ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext;
446 } else {
447 ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] = ddp->ddp_pnext;
449 if ( ddp->ddp_pnext != NULL ) {
450 ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
454 if ( ddp->ddp_route.ro_rt ) {
455 rtfree( ddp->ddp_route.ro_rt );
458 if ( ddp->ddp_prev ) {
459 ddp->ddp_prev->ddp_next = ddp->ddp_next;
460 } else {
461 ddpcb = ddp->ddp_next;
463 if ( ddp->ddp_next ) {
464 ddp->ddp_next->ddp_prev = ddp->ddp_prev;
466 FREE(ddp, M_PCB);
470 * For the moment, this just find the pcb with the correct local address.
471 * In the future, this will actually do some real searching, so we can use
472 * the sender's address to do de-multiplexing on a single port to many
473 * sockets (pcbs).
475 struct ddpcb *
476 ddp_search( struct sockaddr_at *from, struct sockaddr_at *to,
477 struct at_ifaddr *aa)
479 struct ddpcb *ddp;
482 * Check for bad ports.
484 if ( to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST ) {
485 return( NULL );
489 * Make sure the local address matches the sent address. What about
490 * the interface?
492 for ( ddp = ddp_ports[ to->sat_port - 1 ]; ddp; ddp = ddp->ddp_pnext ) {
493 /* XXX should we handle 0.YY? */
495 /* XXXX.YY to socket on destination interface */
496 if ( to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net &&
497 to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node ) {
498 break;
501 /* 0.255 to socket on receiving interface */
502 if ( to->sat_addr.s_node == ATADDR_BCAST && ( to->sat_addr.s_net == 0 ||
503 to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net ) &&
504 ddp->ddp_lsat.sat_addr.s_net == AA_SAT( aa )->sat_addr.s_net ) {
505 break;
508 /* XXXX.0 to socket on destination interface */
509 if ( to->sat_addr.s_net == aa->aa_firstnet &&
510 to->sat_addr.s_node == 0 &&
511 ntohs( ddp->ddp_lsat.sat_addr.s_net ) >=
512 ntohs( aa->aa_firstnet ) &&
513 ntohs( ddp->ddp_lsat.sat_addr.s_net ) <=
514 ntohs( aa->aa_lastnet )) {
515 break;
518 return( ddp );
520 static int
521 at_setpeeraddr(struct socket *so, struct sockaddr **nam)
523 return(EOPNOTSUPP);
526 static int
527 at_setsockaddr(struct socket *so, struct sockaddr **nam)
529 struct ddpcb *ddp;
531 ddp = sotoddpcb( so );
532 if ( ddp == NULL ) {
533 return( EINVAL);
535 at_sockaddr( ddp, nam );
536 return(0);
540 void
541 ddp_init(void)
543 netisr_register(NETISR_ATALK1, cpu0_portfn, at1intr);
544 netisr_register(NETISR_ATALK2, cpu0_portfn, at2intr);
545 netisr_register(NETISR_AARP, cpu0_portfn, aarpintr);
548 #if 0
549 static void
550 ddp_clean(void)
552 struct ddpcb *ddp;
554 for ( ddp = ddpcb; ddp; ddp = ddp->ddp_next ) {
555 at_pcbdetach( ddp->ddp_socket, ddp );
558 #endif
560 struct pr_usrreqs ddp_usrreqs = {
561 .pru_abort = ddp_abort,
562 .pru_accept = pru_accept_notsupp,
563 .pru_attach = ddp_attach,
564 .pru_bind = ddp_bind,
565 .pru_connect = ddp_connect,
566 .pru_connect2 = pru_connect2_notsupp,
567 .pru_control = at_control,
568 .pru_detach = ddp_detach,
569 .pru_disconnect = ddp_disconnect,
570 .pru_listen = pru_listen_notsupp,
571 .pru_peeraddr = at_setpeeraddr,
572 .pru_rcvd = pru_rcvd_notsupp,
573 .pru_rcvoob = pru_rcvoob_notsupp,
574 .pru_send = ddp_send,
575 .pru_sense = pru_sense_null,
576 .pru_shutdown = ddp_shutdown,
577 .pru_sockaddr = at_setsockaddr,
578 .pru_sosend = sosend,
579 .pru_soreceive = soreceive,
580 .pru_sopoll = sopoll