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