Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / disc / if_disc.c
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  * $FreeBSD: src/sys/net/if_disc.c,v 1.26.2.2 2001/12/20 10:30:16 ru 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/module.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53 #include <net/bpf.h>
54
55 #include "opt_inet.h"
56 #include "opt_inet6.h"
57
58 #ifdef TINY_DSMTU
59 #define DSMTU   (1024+512)
60 #else
61 #define DSMTU   65532
62 #endif
63
64 static void discattach __P((void));
65
66 static struct   ifnet discif;
67 static int discoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
68                     struct rtentry *);
69 static void discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info);
70 static int discioctl(struct ifnet *, u_long, caddr_t);
71
72 /* ARGSUSED */
73 static void
74 discattach()
75 {
76         register struct ifnet *ifp = &discif;
77
78         ifp->if_name = "ds";
79         ifp->if_mtu = DSMTU;
80         ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
81         ifp->if_ioctl = discioctl;
82         ifp->if_output = discoutput;
83         ifp->if_type = IFT_LOOP;
84         ifp->if_hdrlen = 0;
85         ifp->if_addrlen = 0;
86         ifp->if_snd.ifq_maxlen = 20;
87         if_attach(ifp);
88         bpfattach(ifp, DLT_NULL, sizeof(u_int));
89 }
90
91 static int
92 disc_modevent(module_t mod, int type, void *data) 
93
94         switch (type) { 
95         case MOD_LOAD: 
96                 discattach();
97                 break; 
98         case MOD_UNLOAD: 
99                 printf("if_disc module unload - not possible for this module type\n"); 
100                 return EINVAL; 
101         } 
102         return 0; 
103
104
105 static moduledata_t disc_mod = { 
106         "if_disc", 
107         disc_modevent, 
108         NULL
109 }; 
110
111 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
112
113 static int
114 discoutput(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("discoutput no HDR");
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 (discif.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(&discif, &m0);
146         }
147         m->m_pkthdr.rcvif = ifp;
148
149         ifp->if_opackets++;
150         ifp->if_obytes += m->m_pkthdr.len;
151
152         m_freem(m);
153         return 0;
154 }
155
156 /* ARGSUSED */
157 static void
158 discrtrequest(cmd, rt, info)
159         int cmd;
160         struct rtentry *rt;
161         struct rt_addrinfo *info;
162 {
163         if (rt)
164                 rt->rt_rmx.rmx_mtu = DSMTU;
165 }
166
167 /*
168  * Process an ioctl request.
169  */
170 /* ARGSUSED */
171 static int
172 discioctl(ifp, cmd, data)
173         register struct ifnet *ifp;
174         u_long cmd;
175         caddr_t data;
176 {
177         register struct ifaddr *ifa;
178         register struct ifreq *ifr = (struct ifreq *)data;
179         register int error = 0;
180
181         switch (cmd) {
182
183         case SIOCSIFADDR:
184                 ifp->if_flags |= IFF_UP;
185                 ifa = (struct ifaddr *)data;
186                 if (ifa != 0)
187                         ifa->ifa_rtrequest = discrtrequest;
188                 /*
189                  * Everything else is done at a higher level.
190                  */
191                 break;
192
193         case SIOCADDMULTI:
194         case SIOCDELMULTI:
195                 if (ifr == 0) {
196                         error = EAFNOSUPPORT;           /* XXX */
197                         break;
198                 }
199                 switch (ifr->ifr_addr.sa_family) {
200
201 #ifdef INET
202                 case AF_INET:
203                         break;
204 #endif
205 #ifdef INET6
206                 case AF_INET6:
207                         break;
208 #endif
209
210                 default:
211                         error = EAFNOSUPPORT;
212                         break;
213                 }
214                 break;
215
216         case SIOCSIFMTU:
217                 ifp->if_mtu = ifr->ifr_mtu;
218                 break;
219
220         default:
221                 error = EINVAL;
222         }
223         return (error);
224 }