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