FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_output.c
1 /* $NetBSD: ns_output.c,v 1.13 2004/04/19 00:10:48 matt 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.13 2004/04/19 00:10:48 matt 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 ns_output(struct mbuf *m0, ...)
63 {
64 struct route *ro;
65 int flags;
66 struct idp *idp = mtod(m0, struct idp *);
67 struct ifnet *ifp = 0;
68 int error = 0;
69 struct route idproute;
70 struct sockaddr_ns *dst;
71 va_list ap;
72
73 va_start(ap, m0);
74 ro = va_arg(ap, struct route *);
75 flags = va_arg(ap, int);
76 va_end(ap);
77
78 if (ns_hold_output) {
79 if (ns_lastout) {
80 (void)m_free(ns_lastout);
81 }
82 ns_lastout = m_copy(m0, 0, (int)M_COPYALL);
83 }
84 /*
85 * Route packet.
86 */
87 if (ro == 0) {
88 ro = &idproute;
89 bzero((caddr_t)ro, sizeof (*ro));
90 }
91 dst = satosns(&ro->ro_dst);
92 if (ro->ro_rt == 0) {
93 dst->sns_family = AF_NS;
94 dst->sns_len = sizeof (*dst);
95 dst->sns_addr = idp->idp_dna;
96 dst->sns_addr.x_port = 0;
97 /*
98 * If routing to interface only,
99 * short circuit routing lookup.
100 */
101 if (flags & NS_ROUTETOIF) {
102 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
103
104 if (ia == 0) {
105 error = ENETUNREACH;
106 goto bad;
107 }
108 ifp = ia->ia_ifp;
109 goto gotif;
110 }
111 rtalloc(ro);
112 } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
113 /*
114 * The old route has gone away; try for a new one.
115 */
116 rtfree(ro->ro_rt);
117 ro->ro_rt = NULL;
118 rtalloc(ro);
119 }
120 if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
121 error = ENETUNREACH;
122 goto bad;
123 }
124 ro->ro_rt->rt_use++;
125 if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
126 dst = satosns(ro->ro_rt->rt_gateway);
127 gotif:
128
129 /*
130 * Look for multicast addresses and
131 * and verify user is allowed to send
132 * such a packet.
133 */
134 if (dst->sns_addr.x_host.c_host[0]&1) {
135 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
136 error = EADDRNOTAVAIL;
137 goto bad;
138 }
139 if ((flags & NS_ALLOWBROADCAST) == 0) {
140 error = EACCES;
141 goto bad;
142 }
143 }
144
145 if (ntohs(idp->idp_len) <= ifp->if_mtu) {
146 ns_output_cnt++;
147 if (ns_copy_output) {
148 ns_watch_output(m0, ifp);
149 }
150 error = (*ifp->if_output)(ifp, m0, snstosa(dst), ro->ro_rt);
151 goto done;
152 } else error = EMSGSIZE;
153
154
155 bad:
156 if (ns_copy_output) {
157 ns_watch_output(m0, ifp);
158 }
159 m_freem(m0);
160 done:
161 if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt) {
162 RTFREE(ro->ro_rt);
163 ro->ro_rt = 0;
164 }
165 return (error);
166 }
Cache object: 984a32cc97e13d76a2dfbbe57af4368f
|