we do not want to shift by the block size, which is much larger than
[dragonfly.git] / sys / netproto / atalk / ddp_usrreq.c
blob57e6ea4afed7c10c29d2d407752ad0848bde737a
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.11 2007/04/21 02:26:48 dillon 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);
418 bzero(ddp, sizeof *ddp);
419 ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
421 ddp->ddp_next = ddpcb;
422 ddp->ddp_prev = NULL;
423 ddp->ddp_pprev = NULL;
424 ddp->ddp_pnext = NULL;
425 if (ddpcb) {
426 ddpcb->ddp_prev = ddp;
428 ddpcb = ddp;
430 ddp->ddp_socket = so;
431 so->so_pcb = (caddr_t)ddp;
432 return(0);
435 static void
436 at_pcbdetach( struct socket *so, struct ddpcb *ddp)
438 soisdisconnected( so );
439 so->so_pcb = 0;
440 sofree( so );
442 /* remove ddp from ddp_ports list */
443 if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT &&
444 ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] != NULL ) {
445 if ( ddp->ddp_pprev != NULL ) {
446 ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext;
447 } else {
448 ddp_ports[ ddp->ddp_lsat.sat_port - 1 ] = ddp->ddp_pnext;
450 if ( ddp->ddp_pnext != NULL ) {
451 ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
455 if ( ddp->ddp_route.ro_rt ) {
456 rtfree( ddp->ddp_route.ro_rt );
459 if ( ddp->ddp_prev ) {
460 ddp->ddp_prev->ddp_next = ddp->ddp_next;
461 } else {
462 ddpcb = ddp->ddp_next;
464 if ( ddp->ddp_next ) {
465 ddp->ddp_next->ddp_prev = ddp->ddp_prev;
467 FREE(ddp, M_PCB);
471 * For the moment, this just find the pcb with the correct local address.
472 * In the future, this will actually do some real searching, so we can use
473 * the sender's address to do de-multiplexing on a single port to many
474 * sockets (pcbs).
476 struct ddpcb *
477 ddp_search( struct sockaddr_at *from, struct sockaddr_at *to,
478 struct at_ifaddr *aa)
480 struct ddpcb *ddp;
483 * Check for bad ports.
485 if ( to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST ) {
486 return( NULL );
490 * Make sure the local address matches the sent address. What about
491 * the interface?
493 for ( ddp = ddp_ports[ to->sat_port - 1 ]; ddp; ddp = ddp->ddp_pnext ) {
494 /* XXX should we handle 0.YY? */
496 /* XXXX.YY to socket on destination interface */
497 if ( to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net &&
498 to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node ) {
499 break;
502 /* 0.255 to socket on receiving interface */
503 if ( to->sat_addr.s_node == ATADDR_BCAST && ( to->sat_addr.s_net == 0 ||
504 to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net ) &&
505 ddp->ddp_lsat.sat_addr.s_net == AA_SAT( aa )->sat_addr.s_net ) {
506 break;
509 /* XXXX.0 to socket on destination interface */
510 if ( to->sat_addr.s_net == aa->aa_firstnet &&
511 to->sat_addr.s_node == 0 &&
512 ntohs( ddp->ddp_lsat.sat_addr.s_net ) >=
513 ntohs( aa->aa_firstnet ) &&
514 ntohs( ddp->ddp_lsat.sat_addr.s_net ) <=
515 ntohs( aa->aa_lastnet )) {
516 break;
519 return( ddp );
521 static int
522 at_setpeeraddr(struct socket *so, struct sockaddr **nam)
524 return(EOPNOTSUPP);
527 static int
528 at_setsockaddr(struct socket *so, struct sockaddr **nam)
530 struct ddpcb *ddp;
532 ddp = sotoddpcb( so );
533 if ( ddp == NULL ) {
534 return( EINVAL);
536 at_sockaddr( ddp, nam );
537 return(0);
541 void
542 ddp_init(void)
544 netisr_register(NETISR_ATALK1, cpu0_portfn, at1intr);
545 netisr_register(NETISR_ATALK2, cpu0_portfn, at2intr);
546 netisr_register(NETISR_AARP, cpu0_portfn, aarpintr);
549 #if 0
550 static void
551 ddp_clean(void)
553 struct ddpcb *ddp;
555 for ( ddp = ddpcb; ddp; ddp = ddp->ddp_next ) {
556 at_pcbdetach( ddp->ddp_socket, ddp );
559 #endif
561 struct pr_usrreqs ddp_usrreqs = {
562 .pru_abort = ddp_abort,
563 .pru_accept = pru_accept_notsupp,
564 .pru_attach = ddp_attach,
565 .pru_bind = ddp_bind,
566 .pru_connect = ddp_connect,
567 .pru_connect2 = pru_connect2_notsupp,
568 .pru_control = at_control,
569 .pru_detach = ddp_detach,
570 .pru_disconnect = ddp_disconnect,
571 .pru_listen = pru_listen_notsupp,
572 .pru_peeraddr = at_setpeeraddr,
573 .pru_rcvd = pru_rcvd_notsupp,
574 .pru_rcvoob = pru_rcvoob_notsupp,
575 .pru_send = ddp_send,
576 .pru_sense = pru_sense_null,
577 .pru_shutdown = ddp_shutdown,
578 .pru_sockaddr = at_setsockaddr,
579 .pru_sosend = sosend,
580 .pru_soreceive = soreceive,
581 .pru_sopoll = sopoll