48a151da619faf4833f87d24e9b69449f64bef13
[dragonfly.git] / sys / netproto / ipx / ipx_usrreq.c
1 /*
2  * Copyright (c) 1995, Mike Mitchell
3  * Copyright (c) 1984, 1985, 1986, 1987, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)ipx_usrreq.c
35  *
36  * $FreeBSD: src/sys/netipx/ipx_usrreq.c,v 1.26.2.1 2001/02/22 09:44:18 bp Exp $
37  * $DragonFly: src/sys/netproto/ipx/ipx_usrreq.c,v 1.13 2008/03/07 11:34:21 sephe Exp $
38  */
39
40 #include "opt_ipx.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/mbuf.h>
46 #include <sys/proc.h>
47 #include <sys/priv.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/socketvar2.h>
52 #include <sys/sysctl.h>
53
54 #include <sys/thread2.h>
55 #include <sys/msgport2.h>
56
57 #include <net/if.h>
58 #include <net/route.h>
59
60 #include <netinet/in.h>
61
62 #include "ipx.h"
63 #include "ipx_pcb.h"
64 #include "ipx_if.h"
65 #include "ipx_var.h"
66 #include "ipx_ip.h"
67
68 /*
69  * IPX protocol implementation.
70  */
71
72 static int ipxsendspace = IPXSNDQ;
73 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
74             &ipxsendspace, 0, "");
75 static int ipxrecvspace = IPXRCVQ;
76 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
77             &ipxrecvspace, 0, "");
78
79 static void ipx_usr_abort(netmsg_t msg);
80 static void ipx_attach(netmsg_t msg);
81 static void ipx_bind(netmsg_t msg);
82 static void ipx_connect(netmsg_t msg);
83 /*static void ipx_control(netmsg_t msg);*/
84 static void ipx_detach(netmsg_t msg);
85 static void ipx_disconnect(netmsg_t msg);
86 /*static void ipx_peeraddr(netmsg_t msg);*/
87 static void ipx_send(netmsg_t msg);
88 static void ipx_shutdown(netmsg_t msg);
89 /*static void ipx_sockaddr(netmsg_t msg);*/
90 static void ripx_attach(netmsg_t msg);
91
92 struct  pr_usrreqs ipx_usrreqs = {
93         .pru_abort = ipx_usr_abort,
94         .pru_accept = pr_generic_notsupp,
95         .pru_attach = ipx_attach,
96         .pru_bind = ipx_bind,
97         .pru_connect = ipx_connect,
98         .pru_connect2 = pr_generic_notsupp,
99         .pru_control = ipx_control,
100         .pru_detach = ipx_detach,
101         .pru_disconnect = ipx_disconnect,
102         .pru_listen = pr_generic_notsupp,
103         .pru_peeraddr = ipx_peeraddr,
104         .pru_rcvd = pr_generic_notsupp,
105         .pru_rcvoob = pr_generic_notsupp,
106         .pru_send = ipx_send,
107         .pru_sense = pru_sense_null,
108         .pru_shutdown = ipx_shutdown,
109         .pru_sockaddr = ipx_sockaddr,
110         .pru_sosend = sosend,
111         .pru_soreceive = soreceive
112 };
113
114 struct  pr_usrreqs ripx_usrreqs = {
115         .pru_abort = ipx_usr_abort,
116         .pru_accept = pr_generic_notsupp,
117         .pru_attach = ripx_attach,
118         .pru_bind = ipx_bind,
119         .pru_connect = ipx_connect,
120         .pru_connect2 = pr_generic_notsupp,
121         .pru_control = ipx_control,
122         .pru_detach = ipx_detach,
123         .pru_disconnect = ipx_disconnect,
124         .pru_listen = pr_generic_notsupp,
125         .pru_peeraddr = ipx_peeraddr,
126         .pru_rcvd = pr_generic_notsupp,
127         .pru_rcvoob = pr_generic_notsupp,
128         .pru_send = ipx_send,
129         .pru_sense = pru_sense_null,
130         .pru_shutdown = ipx_shutdown,
131         .pru_sockaddr = ipx_sockaddr,
132         .pru_sosend = sosend,
133         .pru_soreceive = soreceive
134 };
135
136 /*
137  *  This may also be called for raw listeners.
138  */
139 void
140 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
141 {
142         struct ipx *ipx = mtod(m, struct ipx *);
143         struct ifnet *ifp = m->m_pkthdr.rcvif;
144         struct sockaddr_ipx ipx_ipx;
145
146         if (ipxp == NULL)
147                 panic("No ipxpcb");
148         /*
149          * Construct sockaddr format source address.
150          * Stuff source address and datagram in user buffer.
151          */
152         ipx_ipx.sipx_len = sizeof(ipx_ipx);
153         ipx_ipx.sipx_family = AF_IPX;
154         ipx_ipx.sipx_addr = ipx->ipx_sna;
155         ipx_ipx.sipx_zero[0] = '\0';
156         ipx_ipx.sipx_zero[1] = '\0';
157         if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
158                 struct ifaddr_container *ifac;
159
160                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
161                         struct ifaddr *ifa = ifac->ifa;
162
163                         if (ifa->ifa_addr->sa_family == AF_IPX) {
164                                 ipx_ipx.sipx_addr.x_net =
165                                         IA_SIPX(ifa)->sipx_addr.x_net;
166                                 break;
167                         }
168                 }
169         }
170         ipxp->ipxp_rpt = ipx->ipx_pt;
171         if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
172                 m->m_len -= sizeof(struct ipx);
173                 m->m_pkthdr.len -= sizeof(struct ipx);
174                 m->m_data += sizeof(struct ipx);
175         }
176         if (ssb_appendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
177             m, NULL) == 0)
178                 goto bad;
179         sorwakeup(ipxp->ipxp_socket);
180         return;
181 bad:
182         m_freem(m);
183 }
184
185 void
186 ipx_abort(struct ipxpcb *ipxp)
187 {
188         struct socket *so = ipxp->ipxp_socket;
189
190         soreference(so);
191         ipx_pcbdisconnect(ipxp);
192         soisdisconnected(so);
193         sofree(so);
194 }
195
196 /*
197  * Drop connection, reporting
198  * the specified error.
199  */
200 void
201 ipx_drop(struct ipxpcb *ipxp, int errno)
202 {
203         struct socket *so = ipxp->ipxp_socket;
204
205         /*
206          * someday, in the IPX world
207          * we will generate error protocol packets
208          * announcing that the socket has gone away.
209          *
210          * XXX Probably never. IPX does not have error packets.
211          */
212         /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
213                 tp->t_state = TCPS_CLOSED;
214                 tcp_output(tp);
215         }*/
216         so->so_error = errno;
217         soreference(so);
218         ipx_pcbdisconnect(ipxp);
219         soisdisconnected(so);
220         sofree(so);
221 }
222
223 static int
224 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
225 {
226         struct ipx *ipx;
227         struct socket *so;
228         int len = 0;
229         struct route *ro;
230         struct mbuf *m;
231         struct mbuf *mprev = NULL;
232
233         /*
234          * Calculate data length.
235          */
236         for (m = m0; m != NULL; m = m->m_next) {
237                 mprev = m;
238                 len += m->m_len;
239         }
240         /*
241          * Make sure packet is actually of even length.
242          */
243         
244         if (len & 1) {
245                 m = mprev;
246                 if ((m->m_flags & M_EXT) == 0 &&
247                         (m->m_len + m->m_data < &m->m_dat[MLEN])) {
248                         mtod(m, char*)[m->m_len++] = 0;
249                 } else {
250                         struct mbuf *m1 = m_get(MB_DONTWAIT, MT_DATA);
251
252                         if (m1 == NULL) {
253                                 m_freem(m0);
254                                 return (ENOBUFS);
255                         }
256                         m1->m_len = 1;
257                         * mtod(m1, char *) = 0;
258                         m->m_next = m1;
259                 }
260                 m0->m_pkthdr.len++;
261         }
262
263         /*
264          * Fill in mbuf with extended IPX header
265          * and addresses and length put into network format.
266          */
267         m = m0;
268         if (ipxp->ipxp_flags & IPXP_RAWOUT) {
269                 ipx = mtod(m, struct ipx *);
270         } else {
271                 M_PREPEND(m, sizeof(struct ipx), MB_DONTWAIT);
272                 if (m == NULL)
273                         return (ENOBUFS);
274                 ipx = mtod(m, struct ipx *);
275                 ipx->ipx_tc = 0;
276                 ipx->ipx_pt = ipxp->ipxp_dpt;
277                 ipx->ipx_sna = ipxp->ipxp_laddr;
278                 ipx->ipx_dna = ipxp->ipxp_faddr;
279                 len += sizeof(struct ipx);
280         }
281
282         ipx->ipx_len = htons((u_short)len);
283
284         if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
285                 ipx->ipx_sum = ipx_cksum(m, len);
286         } else
287                 ipx->ipx_sum = 0xffff;
288
289         /*
290          * Output datagram.
291          */
292         so = ipxp->ipxp_socket;
293         if (so->so_options & SO_DONTROUTE)
294                 return (ipx_outputfl(m, NULL,
295                     (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
296         /*
297          * Use cached route for previous datagram if
298          * possible.  If the previous net was the same
299          * and the interface was a broadcast medium, or
300          * if the previous destination was identical,
301          * then we are ok.
302          *
303          * NB: We don't handle broadcasts because that
304          *     would require 3 subroutine calls.
305          */
306         ro = &ipxp->ipxp_route;
307 #ifdef ancient_history
308         /*
309          * I think that this will all be handled in ipx_pcbconnect!
310          */
311         if (ro->ro_rt != NULL) {
312                 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
313                         /*
314                          * This assumes we have no GH type routes
315                          */
316                         if (ro->ro_rt->rt_flags & RTF_HOST) {
317                                 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
318                                         goto re_route;
319
320                         }
321                         if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
322                                 struct ipx_addr *dst =
323                                                 &satoipx_addr(ro->ro_dst);
324                                 dst->x_host = ipx->ipx_dna.x_host;
325                         }
326                         /* 
327                          * Otherwise, we go through the same gateway
328                          * and dst is already set up.
329                          */
330                 } else {
331                 re_route:
332                         RTFREE(ro->ro_rt);
333                         ro->ro_rt = NULL;
334                 }
335         }
336         ipxp->ipxp_lastdst = ipx->ipx_dna;
337 #endif /* ancient_history */
338         return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
339 }
340
341 void
342 ipx_ctloutput(netmsg_t msg)
343 {
344         struct socket *so = msg->base.nm_so;
345         struct sockopt *sopt = msg->ctloutput.nm_sopt;
346         struct ipxpcb *ipxp = sotoipxpcb(so);
347         int mask, error, optval;
348         short soptval;
349         struct ipx ioptval;
350
351         error = 0;
352         if (ipxp == NULL) {
353                 error = EINVAL;
354                 goto out;
355         }
356
357         switch (sopt->sopt_dir) {
358         case SOPT_GET:
359                 switch (sopt->sopt_name) {
360                 case SO_ALL_PACKETS:
361                         mask = IPXP_ALL_PACKETS;
362                         goto get_flags;
363
364                 case SO_HEADERS_ON_INPUT:
365                         mask = IPXP_RAWIN;
366                         goto get_flags;
367
368                 case SO_IPX_CHECKSUM:
369                         mask = IPXP_CHECKSUM;
370                         goto get_flags;
371                         
372                 case SO_HEADERS_ON_OUTPUT:
373                         mask = IPXP_RAWOUT;
374                 get_flags:
375                         soptval = ipxp->ipxp_flags & mask;
376                         error = sooptcopyout(sopt, &soptval, sizeof soptval);
377                         break;
378
379                 case SO_DEFAULT_HEADERS:
380                         ioptval.ipx_len = 0;
381                         ioptval.ipx_sum = 0;
382                         ioptval.ipx_tc = 0;
383                         ioptval.ipx_pt = ipxp->ipxp_dpt;
384                         ioptval.ipx_dna = ipxp->ipxp_faddr;
385                         ioptval.ipx_sna = ipxp->ipxp_laddr;
386                         error = sooptcopyout(sopt, &soptval, sizeof soptval);
387                         break;
388
389                 case SO_SEQNO:
390                         error = sooptcopyout(sopt, &ipx_pexseq, 
391                                              sizeof ipx_pexseq);
392                         ipx_pexseq++;
393                         break;
394
395                 default:
396                         error = EINVAL;
397                 }
398                 break;
399
400         case SOPT_SET:
401                 switch (sopt->sopt_name) {
402                 case SO_ALL_PACKETS:
403                         mask = IPXP_ALL_PACKETS;
404                         goto set_head;
405
406                 case SO_HEADERS_ON_INPUT:
407                         mask = IPXP_RAWIN;
408                         goto set_head;
409
410                 case SO_IPX_CHECKSUM:
411                         mask = IPXP_CHECKSUM;
412
413                 case SO_HEADERS_ON_OUTPUT:
414                         mask = IPXP_RAWOUT;
415                 set_head:
416                         error = sooptcopyin(sopt, &optval, sizeof optval,
417                                             sizeof optval);
418                         if (error)
419                                 break;
420                         if (optval)
421                                 ipxp->ipxp_flags |= mask;
422                         else
423                                 ipxp->ipxp_flags &= ~mask;
424                         break;
425
426                 case SO_DEFAULT_HEADERS:
427                         error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
428                                             sizeof ioptval);
429                         if (error)
430                                 break;
431                         ipxp->ipxp_dpt = ioptval.ipx_pt;
432                         break;
433 #ifdef IPXIP
434                 case SO_IPXIP_ROUTE:
435                         error = ipxip_route(so, sopt);
436                         break;
437 #endif /* IPXIP */
438 #ifdef IPTUNNEL
439 #if 0
440                 case SO_IPXTUNNEL_ROUTE:
441                         error = ipxtun_route(so, sopt);
442                         break;
443 #endif
444 #endif
445                 default:
446                         error = EINVAL;
447                 }
448                 break;
449         }
450 out:
451         lwkt_replymsg(&msg->lmsg, error);
452 }
453
454 /*
455  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
456  *       will sofree() it when we return.
457  */
458 static void
459 ipx_usr_abort(netmsg_t msg)
460 {
461         struct socket *so = msg->base.nm_so;
462         struct ipxpcb *ipxp = sotoipxpcb(so);
463
464         ipx_pcbdetach(ipxp);
465         soisdisconnected(so);
466
467         lwkt_replymsg(&msg->lmsg, 0);
468 }
469
470 static void
471 ipx_attach(netmsg_t msg)
472 {
473         struct socket *so = msg->base.nm_so;
474         struct pru_attach_info *ai = msg->attach.nm_ai;
475         struct ipxpcb *ipxp = sotoipxpcb(so);
476         int error;
477
478         if (ipxp != NULL) {
479                 error = EINVAL;
480         } else {
481                 error = ipx_pcballoc(so, &ipxpcb_list);
482                 if (error == 0) {
483                         error = soreserve(so, ipxsendspace, ipxrecvspace,
484                                           ai->sb_rlimit);
485                 }
486         }
487         lwkt_replymsg(&msg->lmsg, error);
488 }
489
490 static void
491 ipx_bind(netmsg_t msg)
492 {
493         struct socket *so = msg->base.nm_so;
494         struct ipxpcb *ipxp = sotoipxpcb(so);
495         int error;
496
497         error = ipx_pcbbind(ipxp, msg->bind.nm_nam, msg->bind.nm_td);
498         lwkt_replymsg(&msg->lmsg, error);
499 }
500
501 static void
502 ipx_connect(netmsg_t msg)
503 {
504         struct socket *so = msg->base.nm_so;
505         struct ipxpcb *ipxp = sotoipxpcb(so);
506         int error;
507
508         if (ipx_nullhost(ipxp->ipxp_faddr)) {
509                 error = ipx_pcbconnect(ipxp,
510                                        msg->connect.nm_nam,
511                                        msg->connect.nm_td);
512                 if (error == 0)
513                         soisconnected(so);
514         } else {
515                 error = EISCONN;
516         }
517         lwkt_replymsg(&msg->lmsg, error);
518 }
519
520 void
521 ipx_control(netmsg_t msg)
522 {
523         int error;
524
525         error = ipx_control_oncpu(msg->base.nm_so,
526                                   msg->control.nm_cmd,
527                                   msg->control.nm_data,
528                                   msg->control.nm_ifp,
529                                   msg->control.nm_td);
530         lwkt_replymsg(&msg->lmsg, error);
531 }
532
533 static void
534 ipx_detach(netmsg_t msg)
535 {
536         struct socket *so = msg->base.nm_so;
537         struct ipxpcb *ipxp = sotoipxpcb(so);
538         int error;
539
540         if (ipxp) {
541                 ipx_pcbdetach(ipxp);
542                 error = 0;
543         } else {
544                 error = ENOTCONN;
545         }
546         lwkt_replymsg(&msg->lmsg, error);
547 }
548
549 static void
550 ipx_disconnect(netmsg_t msg)
551 {
552         struct socket *so = msg->base.nm_so;
553         struct ipxpcb *ipxp = sotoipxpcb(so);
554         int error;
555
556         if (ipx_nullhost(ipxp->ipxp_faddr)) {
557                 error= ENOTCONN;
558         } else {
559                 soreference(so);
560                 ipx_pcbdisconnect(ipxp);
561                 soisdisconnected(so);
562                 sofree(so);
563                 error = 0;
564         }
565         lwkt_replymsg(&msg->lmsg, error);
566 }
567
568 void
569 ipx_peeraddr(netmsg_t msg)
570 {
571         struct socket *so = msg->base.nm_so;
572         struct ipxpcb *ipxp = sotoipxpcb(so);
573
574         ipx_setpeeraddr(ipxp, msg->peeraddr.nm_nam);
575         lwkt_replymsg(&msg->lmsg, 0);
576 }
577
578 static void
579 ipx_send(netmsg_t msg)
580 {
581         struct socket *so = msg->base.nm_so;
582         struct mbuf *m = msg->send.nm_m;
583         struct sockaddr *nam = msg->send.nm_addr;
584         struct mbuf *control = msg->send.nm_control;
585         struct thread *td = msg->send.nm_td;
586         struct ipxpcb *ipxp = sotoipxpcb(so);
587         struct ipx_addr laddr;
588         int error;
589
590         if (control) {
591                 m_freem(control);
592                 control = NULL;
593         }
594
595         if (nam != NULL) {
596                 laddr = ipxp->ipxp_laddr;
597                 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
598                         error = EISCONN;
599                         goto send_release;
600                 }
601                 /*
602                  * Must block input while temporarily connected.
603                  */
604                 error = ipx_pcbconnect(ipxp, nam, td);
605                 if (error) {
606                         goto send_release;
607                 }
608         } else {
609                 if (ipx_nullhost(ipxp->ipxp_faddr)) {
610                         error = ENOTCONN;
611                         goto send_release;
612                 }
613         }
614         error = ipx_output(ipxp, m);
615         m = NULL;
616         if (nam != NULL) {
617                 ipx_pcbdisconnect(ipxp);
618                 ipxp->ipxp_laddr = laddr;
619         }
620 send_release:
621         if (m != NULL)
622                 m_freem(m);
623         lwkt_replymsg(&msg->lmsg, error);
624 }
625
626 static void
627 ipx_shutdown(netmsg_t msg)
628 {
629         struct socket *so = msg->base.nm_so;
630
631         socantsendmore(so);
632
633         lwkt_replymsg(&msg->lmsg, 0);
634 }
635
636 void
637 ipx_sockaddr(netmsg_t msg)
638 {
639         struct socket *so = msg->base.nm_so;
640         struct ipxpcb *ipxp = sotoipxpcb(so);
641
642         /* XXX what if alloc fails? */
643         ipx_setsockaddr(ipxp, msg->sockaddr.nm_nam);
644         lwkt_replymsg(&msg->lmsg, 0);
645 }
646
647 static void
648 ripx_attach(netmsg_t msg)
649 {
650         struct socket *so = msg->base.nm_so;
651         struct pru_attach_info *ai = msg->attach.nm_ai;
652         struct ipxpcb *ipxp;
653         int error;
654
655         error = priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY);
656         if (error)
657                 goto out;
658         error = ipx_pcballoc(so, &ipxrawpcb_list);
659         if (error)
660                 goto out;
661         error = soreserve(so, ipxsendspace, ipxrecvspace, ai->sb_rlimit);
662         if (error)
663                 goto out;
664         ipxp = sotoipxpcb(so);
665         ipxp->ipxp_faddr.x_host = ipx_broadhost;
666         ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
667 out:
668         lwkt_replymsg(&msg->lmsg, error);
669 }