if_loop.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1982, 1986 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.1 cgd * @(#)if_loop.c 7.13 (Berkeley) 4/26/91
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd /*
37 1.1 cgd * Loopback interface driver for protocol testing and timing.
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd #include "param.h"
41 1.1 cgd #include "systm.h"
42 1.1 cgd #include "mbuf.h"
43 1.1 cgd #include "socket.h"
44 1.1 cgd #include "errno.h"
45 1.1 cgd #include "ioctl.h"
46 1.1 cgd
47 1.1 cgd #include "../net/if.h"
48 1.1 cgd #include "../net/if_types.h"
49 1.1 cgd #include "../net/netisr.h"
50 1.1 cgd #include "../net/route.h"
51 1.1 cgd
52 1.1 cgd #include "machine/mtpr.h"
53 1.1 cgd
54 1.1 cgd #ifdef INET
55 1.1 cgd #include "../netinet/in.h"
56 1.1 cgd #include "../netinet/in_systm.h"
57 1.1 cgd #include "../netinet/in_var.h"
58 1.1 cgd #include "../netinet/ip.h"
59 1.1 cgd #endif
60 1.1 cgd
61 1.1 cgd #ifdef NS
62 1.1 cgd #include "../netns/ns.h"
63 1.1 cgd #include "../netns/ns_if.h"
64 1.1 cgd #endif
65 1.1 cgd
66 1.1 cgd #ifdef ISO
67 1.1 cgd #include "../netiso/iso.h"
68 1.1 cgd #include "../netiso/iso_var.h"
69 1.1 cgd #endif
70 1.1 cgd
71 1.1 cgd #define LOMTU (1024+512)
72 1.1 cgd
73 1.1 cgd struct ifnet loif;
74 1.1 cgd int looutput(), loioctl();
75 1.1 cgd
76 1.1 cgd loattach()
77 1.1 cgd {
78 1.1 cgd register struct ifnet *ifp = &loif;
79 1.1 cgd
80 1.1 cgd ifp->if_name = "lo";
81 1.1 cgd ifp->if_mtu = LOMTU;
82 1.1 cgd ifp->if_flags = IFF_LOOPBACK;
83 1.1 cgd ifp->if_ioctl = loioctl;
84 1.1 cgd ifp->if_output = looutput;
85 1.1 cgd ifp->if_type = IFT_LOOP;
86 1.1 cgd ifp->if_hdrlen = 0;
87 1.1 cgd ifp->if_addrlen = 0;
88 1.1 cgd if_attach(ifp);
89 1.1 cgd }
90 1.1 cgd
91 1.1 cgd looutput(ifp, m, dst, rt)
92 1.1 cgd struct ifnet *ifp;
93 1.1 cgd register struct mbuf *m;
94 1.1 cgd struct sockaddr *dst;
95 1.1 cgd register struct rtentry *rt;
96 1.1 cgd {
97 1.1 cgd int s, isr;
98 1.1 cgd register struct ifqueue *ifq = 0;
99 1.1 cgd
100 1.1 cgd if ((m->m_flags & M_PKTHDR) == 0)
101 1.1 cgd panic("looutput no HDR");
102 1.1 cgd m->m_pkthdr.rcvif = ifp;
103 1.1 cgd
104 1.1 cgd if (rt && rt->rt_flags & RTF_REJECT) {
105 1.1 cgd m_freem(m);
106 1.1 cgd return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
107 1.1 cgd }
108 1.1 cgd ifp->if_opackets++;
109 1.1 cgd ifp->if_obytes += m->m_pkthdr.len;
110 1.1 cgd switch (dst->sa_family) {
111 1.1 cgd
112 1.1 cgd #ifdef INET
113 1.1 cgd case AF_INET:
114 1.1 cgd ifq = &ipintrq;
115 1.1 cgd isr = NETISR_IP;
116 1.1 cgd break;
117 1.1 cgd #endif
118 1.1 cgd #ifdef NS
119 1.1 cgd case AF_NS:
120 1.1 cgd ifq = &nsintrq;
121 1.1 cgd isr = NETISR_NS;
122 1.1 cgd break;
123 1.1 cgd #endif
124 1.1 cgd #ifdef ISO
125 1.1 cgd case AF_ISO:
126 1.1 cgd ifq = &clnlintrq;
127 1.1 cgd isr = NETISR_ISO;
128 1.1 cgd break;
129 1.1 cgd #endif
130 1.1 cgd default:
131 1.1 cgd printf("lo%d: can't handle af%d\n", ifp->if_unit,
132 1.1 cgd dst->sa_family);
133 1.1 cgd m_freem(m);
134 1.1 cgd return (EAFNOSUPPORT);
135 1.1 cgd }
136 1.1 cgd s = splimp();
137 1.1 cgd if (IF_QFULL(ifq)) {
138 1.1 cgd IF_DROP(ifq);
139 1.1 cgd m_freem(m);
140 1.1 cgd splx(s);
141 1.1 cgd return (ENOBUFS);
142 1.1 cgd }
143 1.1 cgd IF_ENQUEUE(ifq, m);
144 1.1 cgd schednetisr(isr);
145 1.1 cgd ifp->if_ipackets++;
146 1.1 cgd ifp->if_ibytes += m->m_pkthdr.len;
147 1.1 cgd splx(s);
148 1.1 cgd return (0);
149 1.1 cgd }
150 1.1 cgd
151 1.1 cgd /* ARGSUSED */
152 1.1 cgd lortrequest(cmd, rt, sa)
153 1.1 cgd struct rtentry *rt;
154 1.1 cgd struct sockaddr *sa;
155 1.1 cgd {
156 1.1 cgd if (rt)
157 1.1 cgd rt->rt_rmx.rmx_mtu = LOMTU;
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Process an ioctl request.
162 1.1 cgd */
163 1.1 cgd /* ARGSUSED */
164 1.1 cgd loioctl(ifp, cmd, data)
165 1.1 cgd register struct ifnet *ifp;
166 1.1 cgd int cmd;
167 1.1 cgd caddr_t data;
168 1.1 cgd {
169 1.1 cgd register struct ifaddr *ifa;
170 1.1 cgd int error = 0;
171 1.1 cgd
172 1.1 cgd switch (cmd) {
173 1.1 cgd
174 1.1 cgd case SIOCSIFADDR:
175 1.1 cgd ifp->if_flags |= IFF_UP;
176 1.1 cgd ifa = (struct ifaddr *)data;
177 1.1 cgd if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
178 1.1 cgd ifa->ifa_rtrequest = lortrequest;
179 1.1 cgd /*
180 1.1 cgd * Everything else is done at a higher level.
181 1.1 cgd */
182 1.1 cgd break;
183 1.1 cgd
184 1.1 cgd default:
185 1.1 cgd error = EINVAL;
186 1.1 cgd }
187 1.1 cgd return (error);
188 1.1 cgd }
189