For kmalloc(), MALLOC() and contigmalloc(), use M_ZERO instead of
[dragonfly.git] / sys / netproto / atalk / ddp_usrreq.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
7b95be2a 4 *
e7b4468c 5 * $DragonFly: src/sys/netproto/atalk/ddp_usrreq.c,v 1.12 2008/01/05 14:02:40 swildner Exp $
984263bc
MD
6 */
7
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>
636ae7e8 16#include <sys/thread2.h>
984263bc 17#include <net/if.h>
08178808 18#include <net/netisr.h>
984263bc
MD
19#include <net/route.h>
20
1f2de5d4
MD
21#include "at.h"
22#include "at_var.h"
23#include "ddp_var.h"
24#include "at_extern.h"
984263bc
MD
25
26static void at_pcbdisconnect( struct ddpcb *ddp );
27static void at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr);
28static int at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr,
dadab5e9 29 struct thread *td);
984263bc 30static int at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr,
dadab5e9 31 struct thread *td);
984263bc
MD
32static void at_pcbdetach(struct socket *so, struct ddpcb *ddp);
33static int at_pcballoc(struct socket *so);
34
35struct ddpcb *ddp_ports[ ATPORT_LAST ];
36struct ddpcb *ddpcb = NULL;
37static u_long ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */
38static u_long ddp_recvspace = 10 * ( 587 + sizeof( struct sockaddr_at ));
39
984263bc 40static int
fbff4886 41ddp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
984263bc
MD
42{
43 struct ddpcb *ddp;
44 int error = 0;
984263bc
MD
45
46
47 ddp = sotoddpcb( so );
48 if ( ddp != NULL ) {
49 return( EINVAL);
50 }
51
636ae7e8 52 crit_enter();
984263bc 53 error = at_pcballoc( so );
636ae7e8 54 crit_exit();
984263bc
MD
55 if (error) {
56 return (error);
57 }
fbff4886 58 return (soreserve( so, ddp_sendspace, ddp_recvspace, ai->sb_rlimit ));
984263bc
MD
59}
60
61static int
62ddp_detach(struct socket *so)
63{
64 struct ddpcb *ddp;
984263bc
MD
65
66 ddp = sotoddpcb( so );
67 if ( ddp == NULL ) {
68 return( EINVAL);
69 }
636ae7e8 70 crit_enter();
984263bc 71 at_pcbdetach( so, ddp );
636ae7e8 72 crit_exit();
984263bc
MD
73 return(0);
74}
75
76static int
dadab5e9 77ddp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc
MD
78{
79 struct ddpcb *ddp;
80 int error = 0;
984263bc
MD
81
82 ddp = sotoddpcb( so );
83 if ( ddp == NULL ) {
84 return( EINVAL);
85 }
636ae7e8 86 crit_enter();
7b95be2a 87 error = at_pcbsetaddr(ddp, nam, td);
636ae7e8 88 crit_exit();
984263bc
MD
89 return (error);
90}
91
92static int
dadab5e9 93ddp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc
MD
94{
95 struct ddpcb *ddp;
96 int error = 0;
984263bc
MD
97
98 ddp = sotoddpcb( so );
99 if ( ddp == NULL ) {
100 return( EINVAL);
101 }
102
103 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
104 return(EISCONN);
105 }
106
636ae7e8 107 crit_enter();
7b95be2a 108 error = at_pcbconnect( ddp, nam, td );
636ae7e8 109 crit_exit();
984263bc
MD
110 if ( error == 0 )
111 soisconnected( so );
112 return(error);
113}
114
115static int
116ddp_disconnect(struct socket *so)
117{
118
119 struct ddpcb *ddp;
984263bc
MD
120
121 ddp = sotoddpcb( so );
122 if ( ddp == NULL ) {
123 return( EINVAL);
124 }
125 if ( ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE ) {
126 return(ENOTCONN);
127 }
128
636ae7e8 129 crit_enter();
984263bc
MD
130 at_pcbdisconnect( ddp );
131 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE;
636ae7e8 132 crit_exit();
984263bc
MD
133 soisdisconnected( so );
134 return(0);
135}
136
137static int
138ddp_shutdown(struct socket *so)
139{
140 struct ddpcb *ddp;
141
142 ddp = sotoddpcb( so );
143 if ( ddp == NULL ) {
144 return( EINVAL);
145 }
146 socantsendmore( so );
147 return(0);
148}
149
150static int
151ddp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
dadab5e9 152 struct mbuf *control, struct thread *td)
984263bc
MD
153{
154 struct ddpcb *ddp;
155 int error = 0;
984263bc
MD
156
157 ddp = sotoddpcb( so );
158 if ( ddp == NULL ) {
159 return(EINVAL);
160 }
161
162 if ( control && control->m_len ) {
163 return(EINVAL);
164 }
165
166 if ( addr ) {
167 if ( ddp->ddp_fsat.sat_port != ATADDR_ANYPORT ) {
168 return(EISCONN);
169 }
170
636ae7e8 171 crit_enter();
7b95be2a 172 error = at_pcbconnect(ddp, addr, td);
636ae7e8 173 crit_exit();
984263bc
MD
174 if ( error ) {
175 return(error);
176 }
177 } else {
178 if ( ddp->ddp_fsat.sat_port == ATADDR_ANYPORT ) {
179 return(ENOTCONN);
180 }
181 }
182
636ae7e8 183 crit_enter();
984263bc
MD
184 error = ddp_output( m, so );
185 if ( addr ) {
186 at_pcbdisconnect( ddp );
187 }
636ae7e8 188 crit_exit();
984263bc
MD
189 return(error);
190}
191
192static int
193ddp_abort(struct socket *so)
194{
195 struct ddpcb *ddp;
984263bc
MD
196
197 ddp = sotoddpcb( so );
198 if ( ddp == NULL ) {
199 return(EINVAL);
200 }
201 soisdisconnected( so );
636ae7e8 202 crit_enter();
984263bc 203 at_pcbdetach( so, ddp );
636ae7e8 204 crit_exit();
984263bc
MD
205 return(0);
206}
207
208
209static void
210at_sockaddr(struct ddpcb *ddp, struct sockaddr **addr)
211{
cfa2ba21 212 *addr = dup_sockaddr((struct sockaddr *)&ddp->ddp_lsat);
984263bc
MD
213}
214
215static int
dadab5e9 216at_pcbsetaddr(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
984263bc
MD
217{
218 struct sockaddr_at lsat, *sat;
219 struct at_ifaddr *aa;
220 struct ddpcb *ddpp;
221
222 if ( ddp->ddp_lsat.sat_port != ATADDR_ANYPORT ) { /* shouldn't be bound */
223 return( EINVAL );
224 }
225
226 if (addr != 0) { /* validate passed address */
227 sat = (struct sockaddr_at *)addr;
228 if (sat->sat_family != AF_APPLETALK) {
229 return(EAFNOSUPPORT);
230 }
231
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;
238 }
239 }
240 if ( !aa ) {
241 return( EADDRNOTAVAIL );
242 }
243 }
244
245 if ( sat->sat_port != ATADDR_ANYPORT ) {
246 if ( sat->sat_port < ATPORT_FIRST ||
247 sat->sat_port >= ATPORT_LAST ) {
248 return( EINVAL );
249 }
250 if ( sat->sat_port < ATPORT_RESERVED &&
dadab5e9 251 suser(td) ) {
984263bc
MD
252 return( EACCES );
253 }
254 }
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;
262 }
263
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 );
268 }
269 sat->sat_addr = AA_SAT( at_ifaddr )->sat_addr;
270 }
271 ddp->ddp_lsat = *sat;
272
273 /*
274 * Choose port.
275 */
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;
281 }
282 }
283 if ( sat->sat_port == ATPORT_LAST ) {
284 return( EADDRNOTAVAIL );
285 }
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;
294 }
295 }
296 if ( ddpp != NULL ) {
297 return( EADDRINUSE );
298 }
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;
303 }
304 }
305
306 return( 0 );
307}
308
309static int
dadab5e9 310at_pcbconnect(struct ddpcb *ddp, struct sockaddr *addr, struct thread *td)
984263bc
MD
311{
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;
317
318 if (sat->sat_family != AF_APPLETALK) {
319 return(EAFNOSUPPORT);
320 }
321
322 /*
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.
326 */
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 );
331 }
332 hintnet = ddp->ddp_lsat.sat_addr.s_net;
333 }
334
335 ro = &ddp->ddp_route;
336 /*
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.
340 */
341 if ( ro->ro_rt ) {
342 if ( hintnet ) {
343 net = hintnet;
344 } else {
345 net = sat->sat_addr.s_net;
346 }
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;
354 }
355 }
356 }
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;
363 }
364 }
365
366 /*
367 * If we've got no route for this interface, try to find one.
368 */
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;
377 }
378 satosat( &ro->ro_dst )->sat_addr.s_node = sat->sat_addr.s_node;
379 rtalloc( ro );
380 }
381
382 /*
383 * Make sure any route that we have has a valid interface.
384 */
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;
390 }
391 }
392 }
393 if ( aa == 0 ) {
394 return( ENETUNREACH );
395 }
396
397 ddp->ddp_fsat = *sat;
398 if ( ddp->ddp_lsat.sat_port == ATADDR_ANYPORT ) {
7b95be2a 399 return(at_pcbsetaddr(ddp, (struct sockaddr *)0, td));
984263bc
MD
400 }
401 return( 0 );
402}
403
404static void
405at_pcbdisconnect( struct ddpcb *ddp )
406{
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;
410}
411
412static int
413at_pcballoc( struct socket *so )
414{
415 struct ddpcb *ddp;
416
e7b4468c 417 MALLOC(ddp, struct ddpcb *, sizeof *ddp, M_PCB, M_WAITOK | M_ZERO);
984263bc
MD
418 ddp->ddp_lsat.sat_port = ATADDR_ANYPORT;
419
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;
426 }
427 ddpcb = ddp;
428
429 ddp->ddp_socket = so;
430 so->so_pcb = (caddr_t)ddp;
431 return(0);
432}
433
434static void
435at_pcbdetach( struct socket *so, struct ddpcb *ddp)
436{
437 soisdisconnected( so );
438 so->so_pcb = 0;
439 sofree( so );
440
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;
448 }
449 if ( ddp->ddp_pnext != NULL ) {
450 ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev;
451 }
452 }
453
454 if ( ddp->ddp_route.ro_rt ) {
455 rtfree( ddp->ddp_route.ro_rt );
456 }
457
458 if ( ddp->ddp_prev ) {
459 ddp->ddp_prev->ddp_next = ddp->ddp_next;
460 } else {
461 ddpcb = ddp->ddp_next;
462 }
463 if ( ddp->ddp_next ) {
464 ddp->ddp_next->ddp_prev = ddp->ddp_prev;
465 }
466 FREE(ddp, M_PCB);
467}
468
469/*
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).
474 */
475struct ddpcb *
476ddp_search( struct sockaddr_at *from, struct sockaddr_at *to,
477 struct at_ifaddr *aa)
478{
479 struct ddpcb *ddp;
480
481 /*
482 * Check for bad ports.
483 */
484 if ( to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST ) {
485 return( NULL );
486 }
487
488 /*
489 * Make sure the local address matches the sent address. What about
490 * the interface?
491 */
492 for ( ddp = ddp_ports[ to->sat_port - 1 ]; ddp; ddp = ddp->ddp_pnext ) {
493 /* XXX should we handle 0.YY? */
494
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;
499 }
500
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;
506 }
507
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;
516 }
517 }
518 return( ddp );
519}
520static int
521at_setpeeraddr(struct socket *so, struct sockaddr **nam)
522{
523 return(EOPNOTSUPP);
524}
525
526static int
527at_setsockaddr(struct socket *so, struct sockaddr **nam)
528{
529 struct ddpcb *ddp;
530
531 ddp = sotoddpcb( so );
532 if ( ddp == NULL ) {
533 return( EINVAL);
534 }
535 at_sockaddr( ddp, nam );
536 return(0);
537}
538
539
540void
9855a82b 541ddp_init(void)
984263bc 542{
bf82f9b7
MD
543 netisr_register(NETISR_ATALK1, cpu0_portfn, at1intr);
544 netisr_register(NETISR_ATALK2, cpu0_portfn, at2intr);
545 netisr_register(NETISR_AARP, cpu0_portfn, aarpintr);
984263bc
MD
546}
547
548#if 0
549static void
9855a82b 550ddp_clean(void)
984263bc
MD
551{
552 struct ddpcb *ddp;
553
554 for ( ddp = ddpcb; ddp; ddp = ddp->ddp_next ) {
555 at_pcbdetach( ddp->ddp_socket, ddp );
556 }
557}
558#endif
559
560struct pr_usrreqs ddp_usrreqs = {
fa5e758c
MD
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
984263bc 581};