revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / net / if_loop.c
blob8341cde3249914dc54230618e16e9a69e026d6c6
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 - 2007 The AROS Dev Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
24 * Copyright (c) 1982, 1986 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
55 * @(#)if_loop.c 7.13 (Berkeley) 4/26/91
59 * Loopback interface driver for protocol testing and timing.
62 #include <conf.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/mbuf.h>
67 #include <sys/socket.h>
68 #include <sys/errno.h>
69 #include <sys/sockio.h>
70 #include <sys/synch.h>
72 #include <net/if.h>
73 #include <net/if_types.h>
74 #include <net/netisr.h>
75 #include <net/route.h>
78 #if INET
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip.h>
83 #endif
85 #if NS
86 #include <netns/ns.h>
87 #include <netns/ns_if.h>
88 #endif
90 #if ISO
91 #include <netiso/iso.h>
92 #include <netiso/iso_var.h>
93 #endif
95 #include <net/if_protos.h>
96 #include <net/if_loop_protos.h>
98 #define LOMTU (1024+512)
100 struct ifnet loif = {0};
102 void
103 loattach()
105 register struct ifnet *ifp = &loif;
107 ifp->if_name = "lo";
108 ifp->if_mtu = LOMTU;
109 ifp->if_flags = IFF_LOOPBACK;
110 ifp->if_ioctl = loioctl;
111 ifp->if_output = looutput;
112 ifp->if_type = IFT_LOOP;
113 ifp->if_hdrlen = 0;
114 ifp->if_addrlen = 0;
115 if_attach(ifp);
118 void loconfig()
120 struct ifreq ifr = { };
121 struct sockaddr_in *ifr_saddr = (struct sockaddr_in *)&ifr.ifr_addr;
122 ifr.ifr_addr.sa_len = sizeof(struct sockaddr_in);
123 ifr.ifr_addr.sa_family = AF_INET;
124 ifr_saddr->sin_addr.s_addr = htonl(0x7F000001);
125 in_control(NULL, SIOCSIFADDR, &ifr, &loif);
129 looutput(ifp, m, dst, rt)
130 struct ifnet *ifp;
131 register struct mbuf *m;
132 struct sockaddr *dst;
133 register struct rtentry *rt;
135 int isr;
136 spl_t s;
137 register struct ifqueue *ifq = 0;
139 if ((m->m_flags & M_PKTHDR) == 0)
140 panic("looutput no HDR");
141 m->m_pkthdr.rcvif = ifp;
143 if (rt && rt->rt_flags & RTF_REJECT) {
144 m_freem(m);
145 DROUTE(log(LOG_DEBUG,"lo0: packet rejected");)
146 return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
148 ifp->if_opackets++;
149 ifp->if_obytes += m->m_pkthdr.len;
150 switch (dst->sa_family) {
152 #if INET
153 case AF_INET:
154 ifq = &ipintrq;
155 isr = NETISR_IP;
156 break;
157 #endif
158 #if NS
159 case AF_NS:
160 ifq = &nsintrq;
161 isr = NETISR_NS;
162 break;
163 #endif
164 #if ISO
165 case AF_ISO:
166 ifq = &clnlintrq;
167 isr = NETISR_ISO;
168 break;
169 #endif
170 default:
171 printf("lo%ld: can't handle af%ld\n", (long)ifp->if_unit,
172 (long)dst->sa_family);
173 m_freem(m);
174 return (EAFNOSUPPORT);
176 s = splimp();
177 if (IF_QFULL(ifq)) {
178 IF_DROP(ifq);
179 m_freem(m);
180 splx(s);
181 return (ENOBUFS);
183 IF_ENQUEUE(ifq, m);
184 schednetisr(isr);
185 ifp->if_ipackets++;
186 ifp->if_ibytes += m->m_pkthdr.len;
187 splx(s);
188 return (0);
191 void lortrequest(int cmd,
192 struct rtentry *rt,
193 struct sockaddr *sa)
195 if (rt)
196 rt->rt_rmx.rmx_mtu = LOMTU;
200 * Process an ioctl request.
203 loioctl(register struct ifnet *ifp, int cmd, caddr_t data)
205 register struct ifaddr *ifa;
206 int error = 0;
208 switch (cmd) {
210 case SIOCSIFADDR:
211 ifp->if_flags |= IFF_UP;
212 ifa = (struct ifaddr *)data;
213 if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
214 ifa->ifa_rtrequest = lortrequest;
216 * Everything else is done at a higher level.
218 break;
220 default:
221 error = EINVAL;
223 return (error);