2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
38 * Discard interface driver for protocol testing and timing.
39 * (Based on the loopback.)
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
56 #include "opt_inet6.h"
59 #define DSMTU (1024+512)
64 static void discattach(void);
66 static struct ifnet discif;
67 static int discoutput(struct ifnet *, struct mbuf *,
68 struct sockaddr *, struct rtentry *);
69 static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
70 static int discioctl(struct ifnet *, u_long, caddr_t);
75 struct ifnet *ifp = &discif;
79 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
80 ifp->if_ioctl = discioctl;
81 ifp->if_output = discoutput;
82 ifp->if_type = IFT_LOOP;
85 ifp->if_snd.ifq_maxlen = 20;
87 bpfattach(ifp, DLT_NULL, sizeof(u_int));
91 disc_modevent(module_t mod, int type, void *data)
98 printf("if_disc module unload - not possible for this module type\n");
104 static moduledata_t disc_mod = {
110 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
113 discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
116 if ((m->m_flags & M_PKTHDR) == 0)
117 panic("discoutput no HDR");
118 /* BPF write needs to be handled specially */
119 if (dst->sa_family == AF_UNSPEC) {
120 dst->sa_family = *(mtod(m, int *));
121 m->m_len -= sizeof(int);
122 m->m_pkthdr.len -= sizeof(int);
123 m->m_data += sizeof(int);
128 * We need to prepend the address family as
129 * a four byte field. Cons up a dummy header
130 * to pacify bpf. This is safe because bpf
131 * will only read from the mbuf (i.e., it won't
132 * try to free it or keep a pointer a to it).
135 u_int af = dst->sa_family;
139 m0.m_data = (char *)⁡
141 bpf_mtap(&discif, &m0);
143 m->m_pkthdr.rcvif = ifp;
146 ifp->if_obytes += m->m_pkthdr.len;
154 discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
157 rt->rt_rmx.rmx_mtu = DSMTU;
161 * Process an ioctl request.
164 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
167 struct ifreq *ifr = (struct ifreq *)data;
173 ifp->if_flags |= IFF_UP;
174 ifa = (struct ifaddr *)data;
176 ifa->ifa_rtrequest = discrtrequest;
178 * Everything else is done at a higher level.
185 error = EAFNOSUPPORT; /* XXX */
188 switch (ifr->ifr_addr.sa_family) {
200 error = EAFNOSUPPORT;
206 ifp->if_mtu = ifr->ifr_mtu;