FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_output.c
1 /* $NetBSD: ns_output.c,v 1.12 2003/08/07 16:33:46 agc Exp $ */
2
3 /*
4 * Copyright (c) 1984, 1985, 1986, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ns_output.c 8.1 (Berkeley) 6/10/93
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ns_output.c,v 1.12 2003/08/07 16:33:46 agc Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/errno.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44
45 #include <net/if.h>
46 #include <net/route.h>
47
48 #include <netns/ns.h>
49 #include <netns/ns_if.h>
50 #include <netns/ns_var.h>
51 #include <netns/idp.h>
52 #include <netns/idp_var.h>
53
54 #include <machine/stdarg.h>
55
56 int ns_hold_output = 0;
57 int ns_copy_output = 0;
58 int ns_output_cnt = 0;
59 struct mbuf *ns_lastout;
60
61 int
62 #if __STDC__
63 ns_output(struct mbuf *m0, ...)
64 #else
65 ns_output(m0, va_alist)
66 struct mbuf *m0;
67 va_dcl
68 #endif
69 {
70 struct route *ro;
71 int flags;
72 struct idp *idp = mtod(m0, struct idp *);
73 struct ifnet *ifp = 0;
74 int error = 0;
75 struct route idproute;
76 struct sockaddr_ns *dst;
77 va_list ap;
78
79 va_start(ap, m0);
80 ro = va_arg(ap, struct route *);
81 flags = va_arg(ap, int);
82 va_end(ap);
83
84 if (ns_hold_output) {
85 if (ns_lastout) {
86 (void)m_free(ns_lastout);
87 }
88 ns_lastout = m_copy(m0, 0, (int)M_COPYALL);
89 }
90 /*
91 * Route packet.
92 */
93 if (ro == 0) {
94 ro = &idproute;
95 bzero((caddr_t)ro, sizeof (*ro));
96 }
97 dst = satosns(&ro->ro_dst);
98 if (ro->ro_rt == 0) {
99 dst->sns_family = AF_NS;
100 dst->sns_len = sizeof (*dst);
101 dst->sns_addr = idp->idp_dna;
102 dst->sns_addr.x_port = 0;
103 /*
104 * If routing to interface only,
105 * short circuit routing lookup.
106 */
107 if (flags & NS_ROUTETOIF) {
108 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
109
110 if (ia == 0) {
111 error = ENETUNREACH;
112 goto bad;
113 }
114 ifp = ia->ia_ifp;
115 goto gotif;
116 }
117 rtalloc(ro);
118 } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
119 /*
120 * The old route has gone away; try for a new one.
121 */
122 rtfree(ro->ro_rt);
123 ro->ro_rt = NULL;
124 rtalloc(ro);
125 }
126 if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
127 error = ENETUNREACH;
128 goto bad;
129 }
130 ro->ro_rt->rt_use++;
131 if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
132 dst = satosns(ro->ro_rt->rt_gateway);
133 gotif:
134
135 /*
136 * Look for multicast addresses and
137 * and verify user is allowed to send
138 * such a packet.
139 */
140 if (dst->sns_addr.x_host.c_host[0]&1) {
141 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
142 error = EADDRNOTAVAIL;
143 goto bad;
144 }
145 if ((flags & NS_ALLOWBROADCAST) == 0) {
146 error = EACCES;
147 goto bad;
148 }
149 }
150
151 if (ntohs(idp->idp_len) <= ifp->if_mtu) {
152 ns_output_cnt++;
153 if (ns_copy_output) {
154 ns_watch_output(m0, ifp);
155 }
156 error = (*ifp->if_output)(ifp, m0, snstosa(dst), ro->ro_rt);
157 goto done;
158 } else error = EMSGSIZE;
159
160
161 bad:
162 if (ns_copy_output) {
163 ns_watch_output(m0, ifp);
164 }
165 m_freem(m0);
166 done:
167 if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt) {
168 RTFREE(ro->ro_rt);
169 ro->ro_rt = 0;
170 }
171 return (error);
172 }
Cache object: abe1cd37f7aa3e9b43a3815fd3bfc984
|