Sync psm/evdev/atkbd with FreeBSD
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      From: @(#)if_loop.c     8.1 (Berkeley) 6/10/93
30  * $FreeBSD: src/sys/net/if_disc.c,v 1.26.2.2 2001/12/20 10:30:16 ru Exp $
31  */
32
33 /*
34  * Discard interface driver for protocol testing and timing.
35  * (Based on the loopback.)
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45
46 #include <net/if.h>
47 #include <net/if_types.h>
48 #include <net/ifq_var.h>
49 #include <net/route.h>
50 #include <net/bpf.h>
51
52 #include "opt_inet.h"
53 #include "opt_inet6.h"
54
55 #ifdef TINY_DSMTU
56 #define DSMTU   (1024+512)
57 #else
58 #define DSMTU   65532
59 #endif
60
61 static void discattach (void);
62
63 static struct   ifnet discif;
64 static int discoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
65                     struct rtentry *);
66 static void discrtrequest(int cmd, struct rtentry *rt);
67 static int discioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
68
69 /* ARGSUSED */
70 static void
71 discattach(void)
72 {
73         struct ifnet *ifp = &discif;
74
75         if_initname(ifp, "ds", IF_DUNIT_NONE);
76         ifp->if_mtu = DSMTU;
77         ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
78         ifp->if_ioctl = discioctl;
79         ifp->if_output = discoutput;
80         ifp->if_type = IFT_LOOP;
81         ifp->if_hdrlen = 0;
82         ifp->if_addrlen = 0;
83         ifq_set_maxlen(&ifp->if_snd, 20);
84         if_attach(ifp, NULL);
85         bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
86 }
87
88 static int
89 disc_modevent(module_t mod, int type, void *data)
90 {
91         switch (type) {
92         case MOD_LOAD:
93                 discattach();
94                 break;
95         case MOD_UNLOAD:
96                 kprintf("if_disc module unload - not possible for this module type\n");
97                 return EINVAL;
98         }
99         return 0;
100 }
101
102 static moduledata_t disc_mod = {
103         "if_disc",
104         disc_modevent,
105         NULL
106 };
107
108 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
109
110 static int
111 discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
112            struct rtentry *rt)
113 {
114         if ((m->m_flags & M_PKTHDR) == 0)
115                 panic("discoutput no HDR");
116         /* BPF write needs to be handled specially */
117         if (dst->sa_family == AF_UNSPEC) {
118                 dst->sa_family = *(mtod(m, int *));
119                 m->m_len -= sizeof(int);
120                 m->m_pkthdr.len -= sizeof(int);
121                 m->m_data += sizeof(int);
122         }
123
124         if (discif.if_bpf) {
125                 bpf_gettoken();
126                 if (discif.if_bpf) {
127                         /*
128                          * We need to prepend the address family as
129                          * a four byte field.
130                          */
131                         uint32_t af = dst->sa_family;
132
133                         bpf_ptap(discif.if_bpf, m, &af, sizeof(af));
134                 }
135                 bpf_reltoken();
136         }
137         m->m_pkthdr.rcvif = ifp;
138
139         IFNET_STAT_INC(ifp, opackets, 1);
140         IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
141
142         m_freem(m);
143         return 0;
144 }
145
146 /* ARGSUSED */
147 static void
148 discrtrequest(int cmd, struct rtentry *rt)
149 {
150         if (rt)
151                 rt->rt_rmx.rmx_mtu = DSMTU;
152 }
153
154 /*
155  * Process an ioctl request.
156  */
157 /* ARGSUSED */
158 static int
159 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
160 {
161         struct ifaddr *ifa;
162         struct ifreq *ifr = (struct ifreq *)data;
163         int error = 0;
164
165         switch (cmd) {
166
167         case SIOCSIFADDR:
168                 ifp->if_flags |= IFF_UP;
169                 ifa = (struct ifaddr *)data;
170                 if (ifa != NULL)
171                         ifa->ifa_rtrequest = discrtrequest;
172                 /*
173                  * Everything else is done at a higher level.
174                  */
175                 break;
176
177         case SIOCADDMULTI:
178         case SIOCDELMULTI:
179                 if (ifr == NULL) {
180                         error = EAFNOSUPPORT;           /* XXX */
181                         break;
182                 }
183                 switch (ifr->ifr_addr.sa_family) {
184
185 #ifdef INET
186                 case AF_INET:
187                         break;
188 #endif
189 #ifdef INET6
190                 case AF_INET6:
191                         break;
192 #endif
193
194                 default:
195                         error = EAFNOSUPPORT;
196                         break;
197                 }
198                 break;
199
200         case SIOCSIFMTU:
201                 ifp->if_mtu = ifr->ifr_mtu;
202                 break;
203
204         default:
205                 error = EINVAL;
206         }
207         return (error);
208 }