This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / sys / net / if_loop.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
70c5ff60 33 * from: @(#)if_loop.c 7.13 (Berkeley) 4/26/91
3228baa0 34 * $Id: if_loop.c,v 1.5 1993/11/25 01:34:03 wollman Exp $
15637ed4
RG
35 */
36
37/*
38 * Loopback interface driver for protocol testing and timing.
39 */
40
41#include "param.h"
42#include "systm.h"
43#include "mbuf.h"
44#include "socket.h"
45#include "errno.h"
46#include "ioctl.h"
3228baa0 47#include "kernel.h"
15637ed4
RG
48
49#include "../net/if.h"
50#include "../net/if_types.h"
51#include "../net/netisr.h"
52#include "../net/route.h"
53
54#include "machine/mtpr.h"
55
56#ifdef INET
57#include "../netinet/in.h"
58#include "../netinet/in_systm.h"
59#include "../netinet/in_var.h"
60#include "../netinet/ip.h"
61#endif
62
63#ifdef NS
64#include "../netns/ns.h"
65#include "../netns/ns_if.h"
66#endif
67
68#ifdef ISO
69#include "../netiso/iso.h"
70#include "../netiso/iso_var.h"
71#endif
72
73#include "bpfilter.h"
74#if NBPFILTER > 0
3228baa0
GW
75#include "sys/time.h"
76#include "net/bpf.h"
15637ed4
RG
77static caddr_t lo_bpf;
78#endif
79
5f18fc2f 80#ifdef TINY_LOMTU
15637ed4 81#define LOMTU (1024+512)
5f18fc2f
GW
82#else /* reasonable MTU */
83#define LOMTU 65535 /* maximum MTU for IP */
84#endif /* reasonable MTU */
15637ed4
RG
85
86struct ifnet loif;
3e8c7e28
GW
87int looutput(struct ifnet *, struct mbuf *, struct sockaddr *, struct rtentry *);
88int loioctl(struct ifnet *, int, caddr_t);
4c45483e 89void lortrequest(int, struct rtentry *, struct sockaddr *);
15637ed4 90
3e8c7e28
GW
91void
92loattach(void)
15637ed4
RG
93{
94 register struct ifnet *ifp = &loif;
95
96 ifp->if_name = "lo";
97 ifp->if_mtu = LOMTU;
98 ifp->if_flags = IFF_LOOPBACK;
99 ifp->if_ioctl = loioctl;
100 ifp->if_output = looutput;
101 ifp->if_type = IFT_LOOP;
102 ifp->if_hdrlen = 0;
103 ifp->if_addrlen = 0;
104 if_attach(ifp);
105#if NBPFILTER > 0
106 bpfattach(&lo_bpf, ifp, DLT_NULL, sizeof(u_int));
107#endif
108}
109
3228baa0
GW
110TEXT_SET(pseudo_set, loattach);
111
3e8c7e28 112int
15637ed4
RG
113looutput(ifp, m, dst, rt)
114 struct ifnet *ifp;
115 register struct mbuf *m;
116 struct sockaddr *dst;
117 register struct rtentry *rt;
118{
119 int s, isr;
120 register struct ifqueue *ifq = 0;
121
122 if ((m->m_flags & M_PKTHDR) == 0)
123 panic("looutput no HDR");
124#if NBPFILTER > 0
125 if (lo_bpf) {
126 /*
127 * We need to prepend the address family as
128 * a four byte field. Cons up a dummy header
129 * to pacify bpf. This is safe because bpf
130 * will only read from the mbuf (i.e., it won't
131 * try to free it or keep a pointer to it).
132 */
133 struct mbuf m0;
134 u_int af = dst->sa_family;
135
136 m0.m_next = m;
137 m0.m_len = 4;
138 m0.m_data = (char *)⁡
139
140 bpf_mtap(lo_bpf, &m0);
141 }
142#endif
143 m->m_pkthdr.rcvif = ifp;
144
145 if (rt && rt->rt_flags & RTF_REJECT) {
146 m_freem(m);
147 return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
148 }
149 ifp->if_opackets++;
150 ifp->if_obytes += m->m_pkthdr.len;
151 switch (dst->sa_family) {
152
153#ifdef INET
154 case AF_INET:
155 ifq = &ipintrq;
156 isr = NETISR_IP;
157 break;
158#endif
159#ifdef NS
160 case AF_NS:
161 ifq = &nsintrq;
162 isr = NETISR_NS;
163 break;
164#endif
165#ifdef ISO
166 case AF_ISO:
167 ifq = &clnlintrq;
168 isr = NETISR_ISO;
169 break;
170#endif
171 default:
172 printf("lo%d: can't handle af%d\n", ifp->if_unit,
173 dst->sa_family);
174 m_freem(m);
175 return (EAFNOSUPPORT);
176 }
177 s = splimp();
178 if (IF_QFULL(ifq)) {
179 IF_DROP(ifq);
180 m_freem(m);
181 splx(s);
182 return (ENOBUFS);
183 }
184 IF_ENQUEUE(ifq, m);
185 schednetisr(isr);
186 ifp->if_ipackets++;
187 ifp->if_ibytes += m->m_pkthdr.len;
188 splx(s);
189 return (0);
190}
191
192/* ARGSUSED */
4c45483e 193void
15637ed4 194lortrequest(cmd, rt, sa)
4c45483e
GW
195 int cmd;
196 struct rtentry *rt;
197 struct sockaddr *sa;
15637ed4 198{
3e8c7e28
GW
199 if (rt) {
200#ifdef DEBUG
201 printf("lo0: lortrequest: setting route MTU to %u\n",
202 LOMTU);
203#endif
15637ed4 204 rt->rt_rmx.rmx_mtu = LOMTU;
3e8c7e28 205 }
15637ed4
RG
206}
207
208/*
209 * Process an ioctl request.
210 */
211/* ARGSUSED */
3e8c7e28 212int
15637ed4
RG
213loioctl(ifp, cmd, data)
214 register struct ifnet *ifp;
215 int cmd;
216 caddr_t data;
217{
218 register struct ifaddr *ifa;
219 int error = 0;
220
221 switch (cmd) {
222
223 case SIOCSIFADDR:
224 ifp->if_flags |= IFF_UP;
225 ifa = (struct ifaddr *)data;
226 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
227 ifa->ifa_rtrequest = lortrequest;
228 /*
229 * Everything else is done at a higher level.
230 */
231 break;
232
233 default:
234 error = EINVAL;
235 }
236 return (error);
237}