2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)idp_usrreq.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/netns/idp_usrreq.c,v 1.9 1999/08/28 00:49:47 peter Exp $
35 * $DragonFly: src/sys/netproto/ns/idp_usrreq.c,v 1.9 2004/06/07 07:04:33 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/errno.h>
49 #include <net/route.h>
58 extern int idpcksum; /* from ns_input.c */
59 extern long ns_pexseq; /* from ns_input.c */
60 extern struct nspcb nsrawpcb; /* from ns_input.c */
62 struct idpstat idpstat;
65 * IDP protocol implementation.
68 struct sockaddr_ns idp_ns = { sizeof(idp_ns), AF_NS };
71 * This may also be called for raw listeners.
78 struct idp *idp = mtod(m, struct idp *);
79 struct ifnet *ifp = m->m_pkthdr.rcvif;
84 * Construct sockaddr format source address.
85 * Stuff source address and datagram in user buffer.
87 idp_ns.sns_addr = idp->idp_sna;
88 if (ns_neteqnn(idp->idp_sna.x_net, ns_zeronet) && ifp) {
93 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
94 if (ifa->ifa_addr->sa_family == AF_NS) {
95 idp_ns.sns_addr.x_net =
96 IA_SNS(ifa)->sns_addr.x_net;
101 nsp->nsp_rpt = idp->idp_pt;
102 if ( ! (nsp->nsp_flags & NSP_RAWIN) ) {
103 m->m_len -= sizeof (struct idp);
104 m->m_pkthdr.len -= sizeof (struct idp);
105 m->m_data += sizeof (struct idp);
107 if (sbappendaddr(&nsp->nsp_socket->so_rcv, (struct sockaddr *)&idp_ns,
108 m, (struct mbuf *)0) == 0)
110 sorwakeup(nsp->nsp_socket);
120 struct socket *so = nsp->nsp_socket;
122 ns_pcbdisconnect(nsp);
123 soisdisconnected(so);
126 * Drop connection, reporting
127 * the specified error.
134 struct socket *so = nsp->nsp_socket;
137 * someday, in the xerox world
138 * we will generate error protocol packets
139 * announcing that the socket has gone away.
141 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
142 tp->t_state = TCPS_CLOSED;
143 (void) tcp_output(tp);
145 so->so_error = errno;
146 ns_pcbdisconnect(nsp);
147 soisdisconnected(so);
162 struct mbuf *mprev = NULL;
165 * Calculate data length.
167 for (m = m0; m; m = m->m_next) {
172 * Make sure packet is actually of even length.
177 if ((m->m_flags & M_EXT) == 0 &&
178 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
181 struct mbuf *m1 = m_get(MB_DONTWAIT, MT_DATA);
188 * mtod(m1, char *) = 0;
195 * Fill in mbuf with extended IDP header
196 * and addresses and length put into network format.
199 if (nsp->nsp_flags & NSP_RAWOUT) {
200 idp = mtod(m, struct idp *);
202 M_PREPEND(m, sizeof (struct idp), MB_DONTWAIT);
205 idp = mtod(m, struct idp *);
207 idp->idp_pt = nsp->nsp_dpt;
208 idp->idp_sna = nsp->nsp_laddr;
209 idp->idp_dna = nsp->nsp_faddr;
210 len += sizeof (struct idp);
213 idp->idp_len = htons((u_short)len);
217 len = ((len - 1) | 1) + 1;
218 idp->idp_sum = ns_cksum(m, len);
220 idp->idp_sum = 0xffff;
225 so = nsp->nsp_socket;
226 if (so->so_options & SO_DONTROUTE)
227 return (ns_output(m, (struct route *)0,
228 (so->so_options & SO_BROADCAST) | NS_ROUTETOIF));
230 * Use cached route for previous datagram if
231 * possible. If the previous net was the same
232 * and the interface was a broadcast medium, or
233 * if the previous destination was identical,
236 * NB: We don't handle broadcasts because that
237 * would require 3 subroutine calls.
239 ro = &nsp->nsp_route;
240 #ifdef ancient_history
242 * I think that this will all be handled in ns_pcbconnect!
245 if(ns_neteq(nsp->nsp_lastdst, idp->idp_dna)) {
247 * This assumes we have no GH type routes
249 if (ro->ro_rt->rt_flags & RTF_HOST) {
250 if (!ns_hosteq(nsp->nsp_lastdst, idp->idp_dna))
254 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
255 struct ns_addr *dst =
256 &satons_addr(ro->ro_dst);
257 dst->x_host = idp->idp_dna.x_host;
260 * Otherwise, we go through the same gateway
261 * and dst is already set up.
266 ro->ro_rt = (struct rtentry *)0;
269 nsp->nsp_lastdst = idp->idp_dna;
270 #endif /* ancient_history */
271 if (noIdpRoute) ro = 0;
272 return (ns_output(m, ro, so->so_options & SO_BROADCAST));
277 idp_ctloutput(req, so, level, name, value)
284 struct nspcb *nsp = sotonspcb(so);
295 m = m_get(MB_DONTWAIT, MT_DATA);
301 mask = NSP_ALL_PACKETS;
304 case SO_HEADERS_ON_INPUT:
308 case SO_HEADERS_ON_OUTPUT:
311 m->m_len = sizeof(short);
312 *mtod(m, short *) = nsp->nsp_flags & mask;
315 case SO_DEFAULT_HEADERS:
316 m->m_len = sizeof(struct idp);
318 struct idp *idp = mtod(m, struct idp *);
322 idp->idp_pt = nsp->nsp_dpt;
323 idp->idp_dna = nsp->nsp_faddr;
324 idp->idp_sna = nsp->nsp_laddr;
329 m->m_len = sizeof(long);
330 *mtod(m, long *) = ns_pexseq++;
344 mask = NSP_ALL_PACKETS;
347 case SO_HEADERS_ON_INPUT:
351 case SO_HEADERS_ON_OUTPUT:
354 if (value && *value) {
355 ok = mtod(*value, int *);
357 nsp->nsp_flags |= mask;
359 nsp->nsp_flags &= ~mask;
360 } else error = EINVAL;
363 case SO_DEFAULT_HEADERS:
366 = mtod(*value, struct idp *);
367 nsp->nsp_dpt = idp->idp_pt;
373 error = nsip_route(*value);
388 * IDP_USRREQ PROCEDURES
392 idp_usr_abort(struct socket *so)
394 struct nspcb *nsp = sotonspcb(so);
400 soisdisconnected(so);
409 idp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
411 struct nspcb *nsp = sotonspcb(so);
416 if ((error = ns_pcballoc(so, &nspcb)) != 0)
418 error = soreserve(so, 2048, 2048, ai->sb_rlimit);
423 idp_raw_attach(struct socket *so, int proto, struct pru_attach_info *ai)
425 struct nspcb *nsp = sotonspcb(so);
428 #ifdef NS_PRIV_SOCKETS
429 if ((so->so_state & SS_PRIV) == 0)
434 if ((error = ns_pcballoc(so, &nsrawpcb)) != 0)
436 if ((error = soreserve(so, 2048, 2048, ai->sb_rlimit)) != 0)
439 nsp->nsp_faddr.x_host = ns_broadhost;
440 nsp->nsp_flags = NSP_RAWIN | NSP_RAWOUT;
445 idp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
447 struct nspcb *nsp = sotonspcb(so);
451 error = ns_pcbbind(nsp, nam);
458 idp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 struct nspcb *nsp = sotonspcb(so);
464 if (!ns_nullhost(nsp->nsp_faddr))
466 else if ((error = ns_pcbconnect(nsp, nam)) == 0)
475 idp_detach(struct socket *so)
477 struct nspcb *nsp = sotonspcb(so);
490 idp_usr_disconnect(struct socket *so)
492 struct nspcb *nsp = sotonspcb(so);
496 if (ns_nullhost(nsp->nsp_faddr)) {
500 ns_pcbdisconnect(nsp);
501 soisdisconnected(so);
510 idp_peeraddr(struct socket *so, struct sockaddr **pnam)
512 struct nspcb *nsp = sotonspcb(so);
516 ns_setpeeraddr(nsp, pnam);
525 idp_send(struct socket *so, int flags, struct mbuf *m,
526 struct sockaddr *addr, struct mbuf *control,
529 struct nspcb *nsp = sotonspcb(so);
530 struct ns_addr laddr;
536 if (control && control->m_len) {
543 laddr = nsp->nsp_laddr;
544 if (!ns_nullhost(nsp->nsp_faddr))
547 error = ns_pcbconnect(nsp, addr);
549 if (ns_nullhost(nsp->nsp_faddr))
555 error = idp_output(nsp, m);
558 ns_pcbdisconnect(nsp);
559 nsp->nsp_laddr.x_host = laddr.x_host;
560 nsp->nsp_laddr.x_port = laddr.x_port;
573 idp_sockaddr(struct socket *so, struct sockaddr **pnam)
575 struct nspcb *nsp = sotonspcb(so);
579 ns_setsockaddr(nsp, pnam);
588 idp_shutdown(struct socket *so)
594 struct pr_usrreqs idp_usrreqs = {
595 idp_usr_abort, pru_accept_notsupp, idp_attach, idp_bind,
596 idp_connect, pru_connect2_notsupp, ns_control, idp_detach,
597 idp_usr_disconnect, pru_listen_notsupp, idp_peeraddr, pru_rcvd_notsupp,
598 pru_rcvoob_notsupp, idp_send, pru_sense_null, idp_shutdown,
599 idp_sockaddr, sosend, soreceive, sopoll
602 struct pr_usrreqs idp_raw_usrreqs = {
603 idp_usr_abort, pru_accept_notsupp, idp_raw_attach, idp_bind,
604 idp_connect, pru_connect2_notsupp, ns_control, idp_detach,
605 idp_usr_disconnect, pru_listen_notsupp, idp_peeraddr, pru_rcvd_notsupp,
606 pru_rcvoob_notsupp, idp_send, pru_sense_null, idp_shutdown,
607 idp_sockaddr, sosend, soreceive, sopoll