xref: /freebsd/sys/net/if_disc.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  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  *
33  *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
34  *	$Id: if_disc.c,v 1.10 1995/12/03 19:08:55 bde Exp $
35  */
36 
37 /*
38  * Discard interface driver for protocol testing and timing.
39  * (Based on the loopback.)
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
48 #include <sys/ioctl.h>
49 #include <sys/time.h>
50 #include <machine/cpu.h>
51 
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/bpf.h>
57 
58 #ifdef	INET
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #endif
64 
65 #ifdef NS
66 #include <netns/ns.h>
67 #include <netns/ns_if.h>
68 #endif
69 
70 #ifdef ISO
71 #include <netiso/iso.h>
72 #include <netiso/iso_var.h>
73 #endif
74 
75 #include "bpfilter.h"
76 
77 #ifdef TINY_DSMTU
78 #define	DSMTU	(1024+512)
79 #else
80 #define DSMTU	65532
81 #endif
82 
83 static void discattach __P((void *dummy));
84 PSEUDO_SET(discattach, if_disc);
85 
86 static struct	ifnet dsif;
87 static int dsoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
88 		    struct rtentry *);
89 static void dsrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa);
90 static int dsioctl(struct ifnet *, int, caddr_t);
91 
92 /* ARGSUSED */
93 static void
94 discattach(dummy)
95 	void *dummy;
96 {
97 	register struct ifnet *ifp = &dsif;
98 
99 	ifp->if_name = "ds";
100 	ifp->if_mtu = DSMTU;
101 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
102 	ifp->if_ioctl = dsioctl;
103 	ifp->if_output = dsoutput;
104 	ifp->if_type = IFT_LOOP;
105 	ifp->if_hdrlen = 0;
106 	ifp->if_addrlen = 0;
107 	if_attach(ifp);
108 #if NBPFILTER > 0
109 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
110 #endif
111 }
112 
113 static int
114 dsoutput(ifp, m, dst, rt)
115 	struct ifnet *ifp;
116 	register struct mbuf *m;
117 	struct sockaddr *dst;
118 	register struct rtentry *rt;
119 {
120 	if ((m->m_flags & M_PKTHDR) == 0)
121 		panic("dsoutput no HDR");
122 	ifp->if_lastchange = time;
123 #if NBPFILTER > 0
124 	/* BPF write needs to be handled specially */
125 	if (dst->sa_family == AF_UNSPEC) {
126 		dst->sa_family = *(mtod(m, int *));
127 		m->m_len -= sizeof(int);
128 		m->m_pkthdr.len -= sizeof(int);
129 		m->m_data += sizeof(int);
130 	}
131 
132 	if (dsif.if_bpf) {
133 		/*
134 		 * We need to prepend the address family as
135 		 * a four byte field.  Cons up a dummy header
136 		 * to pacify bpf.  This is safe because bpf
137 		 * will only read from the mbuf (i.e., it won't
138 		 * try to free it or keep a pointer a to it).
139 		 */
140 		struct mbuf m0;
141 		u_int af = dst->sa_family;
142 
143 		m0.m_next = m;
144 		m0.m_len = 4;
145 		m0.m_data = (char *)&af;
146 
147 		bpf_mtap(&dsif, &m0);
148 	}
149 #endif
150 	m->m_pkthdr.rcvif = ifp;
151 
152 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
153 		m_freem(m);
154 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
155 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
156 	}
157 	ifp->if_opackets++;
158 	ifp->if_obytes += m->m_pkthdr.len;
159 
160 	m_freem(m);
161 	return 0;
162 }
163 
164 /* ARGSUSED */
165 static void
166 dsrtrequest(cmd, rt, sa)
167 	int cmd;
168 	struct rtentry *rt;
169 	struct sockaddr *sa;
170 {
171 	if (rt)
172 		rt->rt_rmx.rmx_mtu = DSMTU;
173 }
174 
175 /*
176  * Process an ioctl request.
177  */
178 /* ARGSUSED */
179 static int
180 dsioctl(ifp, cmd, data)
181 	register struct ifnet *ifp;
182 	int cmd;
183 	caddr_t data;
184 {
185 	register struct ifaddr *ifa;
186 	register struct ifreq *ifr = (struct ifreq *)data;
187 	register int error = 0;
188 
189 	switch (cmd) {
190 
191 	case SIOCSIFADDR:
192 		ifp->if_flags |= IFF_UP;
193 		ifa = (struct ifaddr *)data;
194 		if (ifa != 0)
195 			ifa->ifa_rtrequest = dsrtrequest;
196 		/*
197 		 * Everything else is done at a higher level.
198 		 */
199 		break;
200 
201 	case SIOCADDMULTI:
202 	case SIOCDELMULTI:
203 		if (ifr == 0) {
204 			error = EAFNOSUPPORT;		/* XXX */
205 			break;
206 		}
207 		switch (ifr->ifr_addr.sa_family) {
208 
209 #ifdef INET
210 		case AF_INET:
211 			break;
212 #endif
213 
214 		default:
215 			error = EAFNOSUPPORT;
216 			break;
217 		}
218 		break;
219 
220 	case SIOCSIFMTU:
221 		ifp->if_mtu = ifr->ifr_mtu;
222 		break;
223 
224 	default:
225 		error = EINVAL;
226 	}
227 	return (error);
228 }
229