Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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/sysctl.h>
52 #include <sys/thread2.h>
53
54 #include <net/if.h>
55 #include <net/route.h>
56
57 #include <netinet/in.h>
58
59 #include "ipx.h"
60 #include "ipx_pcb.h"
61 #include "ipx_if.h"
62 #include "ipx_var.h"
63 #include "ipx_ip.h"
64
65 /*
66  * IPX protocol implementation.
67  */
68
69 static int ipxsendspace = IPXSNDQ;
70 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
71             &ipxsendspace, 0, "");
72 static int ipxrecvspace = IPXRCVQ;
73 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
74             &ipxrecvspace, 0, "");
75
76 static  int ipx_usr_abort(struct socket *so);
77 static  int ipx_attach(struct socket *so, int proto,
78                        struct pru_attach_info *ai);
79 static  int ipx_bind(struct socket *so, struct sockaddr *nam,
80                      struct thread *td);
81 static  int ipx_connect(struct socket *so, struct sockaddr *nam,
82                         struct thread *td);
83 static  int ipx_detach(struct socket *so);
84 static  int ipx_disconnect(struct socket *so);
85 static  int ipx_send(struct socket *so, int flags, struct mbuf *m,
86                      struct sockaddr *addr, struct mbuf *control, 
87                      struct thread *td);
88 static  int ipx_shutdown(struct socket *so);
89 static  int ripx_attach(struct socket *so, int proto,
90                         struct pru_attach_info *ai);
91 static  int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
92
93 struct  pr_usrreqs ipx_usrreqs = {
94         .pru_abort = ipx_usr_abort,
95         .pru_accept = pru_accept_notsupp,
96         .pru_attach = ipx_attach,
97         .pru_bind = ipx_bind,
98         .pru_connect = ipx_connect,
99         .pru_connect2 = pru_connect2_notsupp,
100         .pru_control = ipx_control,
101         .pru_detach = ipx_detach,
102         .pru_disconnect = ipx_disconnect,
103         .pru_listen = pru_listen_notsupp,
104         .pru_peeraddr = ipx_peeraddr,
105         .pru_rcvd = pru_rcvd_notsupp,
106         .pru_rcvoob = pru_rcvoob_notsupp,
107         .pru_send = ipx_send,
108         .pru_sense = pru_sense_null,
109         .pru_shutdown = ipx_shutdown,
110         .pru_sockaddr = ipx_sockaddr,
111         .pru_sosend = sosend,
112         .pru_soreceive = soreceive
113 };
114
115 struct  pr_usrreqs ripx_usrreqs = {
116         .pru_abort = ipx_usr_abort,
117         .pru_accept = pru_accept_notsupp,
118         .pru_attach = ripx_attach,
119         .pru_bind = ipx_bind,
120         .pru_connect = ipx_connect,
121         .pru_connect2 = pru_connect2_notsupp,
122         .pru_control = ipx_control,
123         .pru_detach = ipx_detach,
124         .pru_disconnect = ipx_disconnect,
125         .pru_listen = pru_listen_notsupp,
126         .pru_peeraddr = ipx_peeraddr,
127         .pru_rcvd = pru_rcvd_notsupp,
128         .pru_rcvoob = pru_rcvoob_notsupp,
129         .pru_send = ipx_send,
130         .pru_sense = pru_sense_null,
131         .pru_shutdown = ipx_shutdown,
132         .pru_sockaddr = ipx_sockaddr,
133         .pru_sosend = sosend,
134         .pru_soreceive = soreceive
135 };
136
137 /*
138  *  This may also be called for raw listeners.
139  */
140 void
141 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
142 {
143         struct ipx *ipx = mtod(m, struct ipx *);
144         struct ifnet *ifp = m->m_pkthdr.rcvif;
145         struct sockaddr_ipx ipx_ipx;
146
147         if (ipxp == NULL)
148                 panic("No ipxpcb");
149         /*
150          * Construct sockaddr format source address.
151          * Stuff source address and datagram in user buffer.
152          */
153         ipx_ipx.sipx_len = sizeof(ipx_ipx);
154         ipx_ipx.sipx_family = AF_IPX;
155         ipx_ipx.sipx_addr = ipx->ipx_sna;
156         ipx_ipx.sipx_zero[0] = '\0';
157         ipx_ipx.sipx_zero[1] = '\0';
158         if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
159                 struct ifaddr_container *ifac;
160
161                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
162                         struct ifaddr *ifa = ifac->ifa;
163
164                         if (ifa->ifa_addr->sa_family == AF_IPX) {
165                                 ipx_ipx.sipx_addr.x_net =
166                                         IA_SIPX(ifa)->sipx_addr.x_net;
167                                 break;
168                         }
169                 }
170         }
171         ipxp->ipxp_rpt = ipx->ipx_pt;
172         if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
173                 m->m_len -= sizeof(struct ipx);
174                 m->m_pkthdr.len -= sizeof(struct ipx);
175                 m->m_data += sizeof(struct ipx);
176         }
177         if (ssb_appendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
178             m, NULL) == 0)
179                 goto bad;
180         sorwakeup(ipxp->ipxp_socket);
181         return;
182 bad:
183         m_freem(m);
184 }
185
186 void
187 ipx_abort(struct ipxpcb *ipxp)
188 {
189         struct socket *so = ipxp->ipxp_socket;
190
191         soreference(so);
192         ipx_pcbdisconnect(ipxp);
193         soisdisconnected(so);
194         sofree(so);
195 }
196
197 /*
198  * Drop connection, reporting
199  * the specified error.
200  */
201 void
202 ipx_drop(struct ipxpcb *ipxp, int errno)
203 {
204         struct socket *so = ipxp->ipxp_socket;
205
206         /*
207          * someday, in the IPX world
208          * we will generate error protocol packets
209          * announcing that the socket has gone away.
210          *
211          * XXX Probably never. IPX does not have error packets.
212          */
213         /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
214                 tp->t_state = TCPS_CLOSED;
215                 tcp_output(tp);
216         }*/
217         so->so_error = errno;
218         soreference(so);
219         ipx_pcbdisconnect(ipxp);
220         soisdisconnected(so);
221         sofree(so);
222 }
223
224 static int
225 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
226 {
227         struct ipx *ipx;
228         struct socket *so;
229         int len = 0;
230         struct route *ro;
231         struct mbuf *m;
232         struct mbuf *mprev = NULL;
233
234         /*
235          * Calculate data length.
236          */
237         for (m = m0; m != NULL; m = m->m_next) {
238                 mprev = m;
239                 len += m->m_len;
240         }
241         /*
242          * Make sure packet is actually of even length.
243          */
244         
245         if (len & 1) {
246                 m = mprev;
247                 if ((m->m_flags & M_EXT) == 0 &&
248                         (m->m_len + m->m_data < &m->m_dat[MLEN])) {
249                         mtod(m, char*)[m->m_len++] = 0;
250                 } else {
251                         struct mbuf *m1 = m_get(MB_DONTWAIT, MT_DATA);
252
253                         if (m1 == NULL) {
254                                 m_freem(m0);
255                                 return (ENOBUFS);
256                         }
257                         m1->m_len = 1;
258                         * mtod(m1, char *) = 0;
259                         m->m_next = m1;
260                 }
261                 m0->m_pkthdr.len++;
262         }
263
264         /*
265          * Fill in mbuf with extended IPX header
266          * and addresses and length put into network format.
267          */
268         m = m0;
269         if (ipxp->ipxp_flags & IPXP_RAWOUT) {
270                 ipx = mtod(m, struct ipx *);
271         } else {
272                 M_PREPEND(m, sizeof(struct ipx), MB_DONTWAIT);
273                 if (m == NULL)
274                         return (ENOBUFS);
275                 ipx = mtod(m, struct ipx *);
276                 ipx->ipx_tc = 0;
277                 ipx->ipx_pt = ipxp->ipxp_dpt;
278                 ipx->ipx_sna = ipxp->ipxp_laddr;
279                 ipx->ipx_dna = ipxp->ipxp_faddr;
280                 len += sizeof(struct ipx);
281         }
282
283         ipx->ipx_len = htons((u_short)len);
284
285         if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
286                 ipx->ipx_sum = ipx_cksum(m, len);
287         } else
288                 ipx->ipx_sum = 0xffff;
289
290         /*
291          * Output datagram.
292          */
293         so = ipxp->ipxp_socket;
294         if (so->so_options & SO_DONTROUTE)
295                 return (ipx_outputfl(m, NULL,
296                     (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
297         /*
298          * Use cached route for previous datagram if
299          * possible.  If the previous net was the same
300          * and the interface was a broadcast medium, or
301          * if the previous destination was identical,
302          * then we are ok.
303          *
304          * NB: We don't handle broadcasts because that
305          *     would require 3 subroutine calls.
306          */
307         ro = &ipxp->ipxp_route;
308 #ifdef ancient_history
309         /*
310          * I think that this will all be handled in ipx_pcbconnect!
311          */
312         if (ro->ro_rt != NULL) {
313                 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
314                         /*
315                          * This assumes we have no GH type routes
316                          */
317                         if (ro->ro_rt->rt_flags & RTF_HOST) {
318                                 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
319                                         goto re_route;
320
321                         }
322                         if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
323                                 struct ipx_addr *dst =
324                                                 &satoipx_addr(ro->ro_dst);
325                                 dst->x_host = ipx->ipx_dna.x_host;
326                         }
327                         /* 
328                          * Otherwise, we go through the same gateway
329                          * and dst is already set up.
330                          */
331                 } else {
332                 re_route:
333                         RTFREE(ro->ro_rt);
334                         ro->ro_rt = NULL;
335                 }
336         }
337         ipxp->ipxp_lastdst = ipx->ipx_dna;
338 #endif /* ancient_history */
339         return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
340 }
341
342 int
343 ipx_ctloutput(struct socket *so, struct sockopt *sopt)
344 {
345         struct ipxpcb *ipxp = sotoipxpcb(so);
346         int mask, error, optval;
347         short soptval;
348         struct ipx ioptval;
349
350         error = 0;
351         if (ipxp == NULL)
352                 return (EINVAL);
353
354         switch (sopt->sopt_dir) {
355         case SOPT_GET:
356                 switch (sopt->sopt_name) {
357                 case SO_ALL_PACKETS:
358                         mask = IPXP_ALL_PACKETS;
359                         goto get_flags;
360
361                 case SO_HEADERS_ON_INPUT:
362                         mask = IPXP_RAWIN;
363                         goto get_flags;
364
365                 case SO_IPX_CHECKSUM:
366                         mask = IPXP_CHECKSUM;
367                         goto get_flags;
368                         
369                 case SO_HEADERS_ON_OUTPUT:
370                         mask = IPXP_RAWOUT;
371                 get_flags:
372                         soptval = ipxp->ipxp_flags & mask;
373                         error = sooptcopyout(sopt, &soptval, sizeof soptval);
374                         break;
375
376                 case SO_DEFAULT_HEADERS:
377                         ioptval.ipx_len = 0;
378                         ioptval.ipx_sum = 0;
379                         ioptval.ipx_tc = 0;
380                         ioptval.ipx_pt = ipxp->ipxp_dpt;
381                         ioptval.ipx_dna = ipxp->ipxp_faddr;
382                         ioptval.ipx_sna = ipxp->ipxp_laddr;
383                         error = sooptcopyout(sopt, &soptval, sizeof soptval);
384                         break;
385
386                 case SO_SEQNO:
387                         error = sooptcopyout(sopt, &ipx_pexseq, 
388                                              sizeof ipx_pexseq);
389                         ipx_pexseq++;
390                         break;
391
392                 default:
393                         error = EINVAL;
394                 }
395                 break;
396
397         case SOPT_SET:
398                 switch (sopt->sopt_name) {
399                 case SO_ALL_PACKETS:
400                         mask = IPXP_ALL_PACKETS;
401                         goto set_head;
402
403                 case SO_HEADERS_ON_INPUT:
404                         mask = IPXP_RAWIN;
405                         goto set_head;
406
407                 case SO_IPX_CHECKSUM:
408                         mask = IPXP_CHECKSUM;
409
410                 case SO_HEADERS_ON_OUTPUT:
411                         mask = IPXP_RAWOUT;
412                 set_head:
413                         error = sooptcopyin(sopt, &optval, sizeof optval,
414                                             sizeof optval);
415                         if (error)
416                                 break;
417                         if (optval)
418                                 ipxp->ipxp_flags |= mask;
419                         else
420                                 ipxp->ipxp_flags &= ~mask;
421                         break;
422
423                 case SO_DEFAULT_HEADERS:
424                         error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
425                                             sizeof ioptval);
426                         if (error)
427                                 break;
428                         ipxp->ipxp_dpt = ioptval.ipx_pt;
429                         break;
430 #ifdef IPXIP
431                 case SO_IPXIP_ROUTE:
432                         error = ipxip_route(so, sopt);
433                         break;
434 #endif /* IPXIP */
435 #ifdef IPTUNNEL
436 #if 0
437                 case SO_IPXTUNNEL_ROUTE:
438                         error = ipxtun_route(so, sopt);
439                         break;
440 #endif
441 #endif
442                 default:
443                         error = EINVAL;
444                 }
445                 break;
446         }
447         return (error);
448 }
449
450 /*
451  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
452  *       will sofree() it when we return.
453  */
454 static int
455 ipx_usr_abort(struct socket *so)
456 {
457         struct ipxpcb *ipxp = sotoipxpcb(so);
458
459         ipx_pcbdetach(ipxp);
460         soisdisconnected(so);
461
462         return (0);
463 }
464
465 static int
466 ipx_attach(struct socket *so, int proto, struct pru_attach_info *ai)
467 {
468         int error;
469         struct ipxpcb *ipxp = sotoipxpcb(so);
470
471         if (ipxp != NULL)
472                 return (EINVAL);
473         error = ipx_pcballoc(so, &ipxpcb);
474         if (error == 0)
475                 error = soreserve(so, ipxsendspace, ipxrecvspace,
476                                   ai->sb_rlimit);
477         return (error);
478 }
479
480 static int
481 ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
482 {
483         struct ipxpcb *ipxp = sotoipxpcb(so);
484
485         return (ipx_pcbbind(ipxp, nam, td));
486 }
487
488 static int
489 ipx_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
490 {
491         int error;
492         struct ipxpcb *ipxp = sotoipxpcb(so);
493
494         if (!ipx_nullhost(ipxp->ipxp_faddr))
495                 return (EISCONN);
496         error = ipx_pcbconnect(ipxp, nam, td);
497         if (error == 0)
498                 soisconnected(so);
499         return (error);
500 }
501
502 static int
503 ipx_detach(struct socket *so)
504 {
505         struct ipxpcb *ipxp = sotoipxpcb(so);
506
507         if (ipxp == NULL)
508                 return (ENOTCONN);
509         ipx_pcbdetach(ipxp);
510         return (0);
511 }
512
513 static int
514 ipx_disconnect(struct socket *so)
515 {
516         struct ipxpcb *ipxp = sotoipxpcb(so);
517
518         if (ipx_nullhost(ipxp->ipxp_faddr))
519                 return (ENOTCONN);
520         soreference(so);
521         ipx_pcbdisconnect(ipxp);
522         soisdisconnected(so);
523         sofree(so);
524
525         return (0);
526 }
527
528 int
529 ipx_peeraddr(struct socket *so, struct sockaddr **nam)
530 {
531         struct ipxpcb *ipxp = sotoipxpcb(so);
532
533         ipx_setpeeraddr(ipxp, nam);
534         return (0);
535 }
536
537 static int
538 ipx_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
539         struct mbuf *control, struct thread *td)
540 {
541         int error;
542         struct ipxpcb *ipxp = sotoipxpcb(so);
543         struct ipx_addr laddr;
544
545         if (nam != NULL) {
546                 laddr = ipxp->ipxp_laddr;
547                 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
548                         error = EISCONN;
549                         goto send_release;
550                 }
551                 /*
552                  * Must block input while temporarily connected.
553                  */
554                 error = ipx_pcbconnect(ipxp, nam, td);
555                 if (error) {
556                         goto send_release;
557                 }
558         } else {
559                 if (ipx_nullhost(ipxp->ipxp_faddr)) {
560                         error = ENOTCONN;
561                         goto send_release;
562                 }
563         }
564         error = ipx_output(ipxp, m);
565         m = NULL;
566         if (nam != NULL) {
567                 ipx_pcbdisconnect(ipxp);
568                 ipxp->ipxp_laddr = laddr;
569         }
570 send_release:
571         if (m != NULL)
572                 m_freem(m);
573         return (error);
574 }
575
576 static int
577 ipx_shutdown(struct socket *so)
578 {
579         socantsendmore(so);
580         return (0);
581 }
582
583 int
584 ipx_sockaddr(struct socket *so, struct sockaddr **nam)
585 {
586         struct ipxpcb *ipxp = sotoipxpcb(so);
587
588         ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */
589         return (0);
590 }
591
592 static int
593 ripx_attach(struct socket *so, int proto, struct pru_attach_info *ai)
594 {
595         int error = 0;
596         struct ipxpcb *ipxp;
597
598         if ((error = priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY)) != 0)
599                 return (error);
600         error = ipx_pcballoc(so, &ipxrawpcb);
601         if (error)
602                 return (error);
603         error = soreserve(so, ipxsendspace, ipxrecvspace, ai->sb_rlimit);
604         if (error)
605                 return (error);
606         ipxp = sotoipxpcb(so);
607         ipxp->ipxp_faddr.x_host = ipx_broadhost;
608         ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
609         return (error);
610 }