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.2 2003/06/17 04:28:53 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>
52 #include <netns/ns_pcb.h>
53 #include <netns/ns_if.h>
54 #include <netns/idp.h>
55 #include <netns/idp_var.h>
56 #include <netns/ns_error.h>
59 * IDP protocol implementation.
62 struct sockaddr_ns idp_ns = { sizeof(idp_ns), AF_NS };
65 * This may also be called for raw listeners.
69 register struct nspcb *nsp;
71 register struct idp *idp = mtod(m, struct idp *);
72 struct ifnet *ifp = m->m_pkthdr.rcvif;
77 * Construct sockaddr format source address.
78 * Stuff source address and datagram in user buffer.
80 idp_ns.sns_addr = idp->idp_sna;
81 if (ns_neteqnn(idp->idp_sna.x_net, ns_zeronet) && ifp) {
82 register struct ifaddr *ifa;
84 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
85 if (ifa->ifa_addr->sa_family == AF_NS) {
86 idp_ns.sns_addr.x_net =
87 IA_SNS(ifa)->sns_addr.x_net;
92 nsp->nsp_rpt = idp->idp_pt;
93 if ( ! (nsp->nsp_flags & NSP_RAWIN) ) {
94 m->m_len -= sizeof (struct idp);
95 m->m_pkthdr.len -= sizeof (struct idp);
96 m->m_data += sizeof (struct idp);
98 if (sbappendaddr(&nsp->nsp_socket->so_rcv, (struct sockaddr *)&idp_ns,
99 m, (struct mbuf *)0) == 0)
101 sorwakeup(nsp->nsp_socket);
110 struct socket *so = nsp->nsp_socket;
112 ns_pcbdisconnect(nsp);
113 soisdisconnected(so);
116 * Drop connection, reporting
117 * the specified error.
121 register struct nspcb *nsp;
124 struct socket *so = nsp->nsp_socket;
127 * someday, in the xerox world
128 * we will generate error protocol packets
129 * announcing that the socket has gone away.
131 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
132 tp->t_state = TCPS_CLOSED;
133 (void) tcp_output(tp);
135 so->so_error = errno;
136 ns_pcbdisconnect(nsp);
137 soisdisconnected(so);
145 register struct mbuf *m;
146 register struct idp *idp;
147 register struct socket *so;
148 register int len = 0;
149 register struct route *ro;
154 * Calculate data length.
156 for (m = m0; m; m = m->m_next) {
161 * Make sure packet is actually of even length.
166 if ((m->m_flags & M_EXT) == 0 &&
167 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
170 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
177 * mtod(m1, char *) = 0;
184 * Fill in mbuf with extended IDP header
185 * and addresses and length put into network format.
188 if (nsp->nsp_flags & NSP_RAWOUT) {
189 idp = mtod(m, struct idp *);
191 M_PREPEND(m, sizeof (struct idp), M_DONTWAIT);
194 idp = mtod(m, struct idp *);
196 idp->idp_pt = nsp->nsp_dpt;
197 idp->idp_sna = nsp->nsp_laddr;
198 idp->idp_dna = nsp->nsp_faddr;
199 len += sizeof (struct idp);
202 idp->idp_len = htons((u_short)len);
206 len = ((len - 1) | 1) + 1;
207 idp->idp_sum = ns_cksum(m, len);
209 idp->idp_sum = 0xffff;
214 so = nsp->nsp_socket;
215 if (so->so_options & SO_DONTROUTE)
216 return (ns_output(m, (struct route *)0,
217 (so->so_options & SO_BROADCAST) | NS_ROUTETOIF));
219 * Use cached route for previous datagram if
220 * possible. If the previous net was the same
221 * and the interface was a broadcast medium, or
222 * if the previous destination was identical,
225 * NB: We don't handle broadcasts because that
226 * would require 3 subroutine calls.
228 ro = &nsp->nsp_route;
229 #ifdef ancient_history
231 * I think that this will all be handled in ns_pcbconnect!
234 if(ns_neteq(nsp->nsp_lastdst, idp->idp_dna)) {
236 * This assumes we have no GH type routes
238 if (ro->ro_rt->rt_flags & RTF_HOST) {
239 if (!ns_hosteq(nsp->nsp_lastdst, idp->idp_dna))
243 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
244 register struct ns_addr *dst =
245 &satons_addr(ro->ro_dst);
246 dst->x_host = idp->idp_dna.x_host;
249 * Otherwise, we go through the same gateway
250 * and dst is already set up.
255 ro->ro_rt = (struct rtentry *)0;
258 nsp->nsp_lastdst = idp->idp_dna;
259 #endif /* ancient_history */
260 if (noIdpRoute) ro = 0;
261 return (ns_output(m, ro, so->so_options & SO_BROADCAST));
264 idp_ctloutput(req, so, level, name, value)
270 register struct mbuf *m;
271 struct nspcb *nsp = sotonspcb(so);
273 extern long ns_pexseq;
283 m = m_get(M_DONTWAIT, MT_DATA);
289 mask = NSP_ALL_PACKETS;
292 case SO_HEADERS_ON_INPUT:
296 case SO_HEADERS_ON_OUTPUT:
299 m->m_len = sizeof(short);
300 *mtod(m, short *) = nsp->nsp_flags & mask;
303 case SO_DEFAULT_HEADERS:
304 m->m_len = sizeof(struct idp);
306 register struct idp *idp = mtod(m, struct idp *);
310 idp->idp_pt = nsp->nsp_dpt;
311 idp->idp_dna = nsp->nsp_faddr;
312 idp->idp_sna = nsp->nsp_laddr;
317 m->m_len = sizeof(long);
318 *mtod(m, long *) = ns_pexseq++;
332 mask = NSP_ALL_PACKETS;
335 case SO_HEADERS_ON_INPUT:
339 case SO_HEADERS_ON_OUTPUT:
342 if (value && *value) {
343 ok = mtod(*value, int *);
345 nsp->nsp_flags |= mask;
347 nsp->nsp_flags &= ~mask;
348 } else error = EINVAL;
351 case SO_DEFAULT_HEADERS:
353 register struct idp *idp
354 = mtod(*value, struct idp *);
355 nsp->nsp_dpt = idp->idp_pt;
361 error = nsip_route(*value);
375 idp_usrreq(so, req, m, nam, control)
378 struct mbuf *m, *nam, *control;
380 struct nspcb *nsp = sotonspcb(so);
383 if (req == PRU_CONTROL)
384 return (ns_control(so, (int)m, (caddr_t)nam,
385 (struct ifnet *)control));
386 if (control && control->m_len) {
390 if (nsp == NULL && req != PRU_ATTACH) {
401 error = ns_pcballoc(so, &nspcb);
404 error = soreserve(so, (u_long) 2048, (u_long) 2048);
418 error = ns_pcbbind(nsp, nam);
426 if (!ns_nullhost(nsp->nsp_faddr)) {
430 error = ns_pcbconnect(nsp, nam);
444 if (ns_nullhost(nsp->nsp_faddr)) {
448 ns_pcbdisconnect(nsp);
449 soisdisconnected(so);
458 struct ns_addr laddr;
462 laddr = nsp->nsp_laddr;
463 if (!ns_nullhost(nsp->nsp_faddr)) {
468 * Must block input while temporarily connected.
471 error = ns_pcbconnect(nsp, nam);
477 if (ns_nullhost(nsp->nsp_faddr)) {
482 error = idp_output(nsp, m);
485 ns_pcbdisconnect(nsp);
487 nsp->nsp_laddr.x_host = laddr.x_host;
488 nsp->nsp_laddr.x_port = laddr.x_port;
496 soisdisconnected(so);
500 ns_setsockaddr(nsp, nam);
504 ns_setpeeraddr(nsp, nam);
509 * stat: don't bother with a blocksize.
524 return (EOPNOTSUPP); /* do not free mbuf's */
537 idp_raw_usrreq(so, req, m, nam, control)
540 struct mbuf *m, *nam, *control;
543 struct nspcb *nsp = sotonspcb(so);
544 extern struct nspcb nsrawpcb;
550 if (!(so->so_state & SS_PRIV) || (nsp != NULL)) {
554 error = ns_pcballoc(so, &nsrawpcb);
557 error = soreserve(so, (u_long) 2048, (u_long) 2048);
561 nsp->nsp_faddr.x_host = ns_broadhost;
562 nsp->nsp_flags = NSP_RAWIN | NSP_RAWOUT;
565 error = idp_usrreq(so, req, m, nam, control);