summaryrefslogtreecommitdiff
path: root/sys/net/if_enc.c
blob: 6cd97361fc55f6c33a2420752a709e6fcb7739bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*	$OpenBSD: if_enc.c,v 1.6 1997/11/04 09:10:55 provos Exp $	*/

/*
 * The author of this code is John Ioannidis, ji@tla.org,
 * 	(except when noted otherwise).
 *
 * This code was written for BSD/OS in Athens, Greece, in November 1995.
 *
 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
 * by Angelos D. Keromytis, kermit@forthnet.gr.
 *
 * Additional transforms and features in 1997 by Angelos D. Keromytis and
 * Niels Provos.
 *
 * Copyright (C) 1995, 1996, 1997 by John Ioannidis, Angelos D. Keromytis
 * and Niels Provos.
 *	
 * Permission to use, copy, and modify this software without fee
 * is hereby granted, provided that this entire notice is included in
 * all copies of any software which is or includes a copy or
 * modification of this software.
 *
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTY. IN PARTICULAR, NEITHER AUTHOR MAKES ANY
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
 * PURPOSE.
 */

/*
 * Encapsulation interface driver.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <machine/cpu.h>

#include <net/if.h>
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/bpf.h>

#ifdef	INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#endif

#ifdef ISO
extern struct ifqueue clnlintrq;
#endif

#ifdef NS
extern struct ifqueue nsintrq;
#endif

#include "bpfilter.h"

#define	ENCMTU	(1024+512)

struct ifnet enc_softc;

void	encattach __P((int));
int	encoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
	    	       struct rtentry *));
int	encioctl __P((struct ifnet *, u_long, caddr_t));
void	encrtrequest __P((int, struct rtentry *, struct sockaddr *));

void
encattach(int nenc)
{
    struct ifaddr *ifa;

    bzero(&enc_softc, sizeof(struct ifnet));

    /* We only need one interface anyway under the new mode of operation */
    enc_softc.if_index = 0;

    sprintf(enc_softc.if_xname, "enc0");

    enc_softc.if_list.tqe_next = NULL;
    enc_softc.if_mtu = ENCMTU;
    enc_softc.if_flags = IFF_LOOPBACK;
    enc_softc.if_type = IFT_ENC;
    enc_softc.if_ioctl = encioctl;
    enc_softc.if_output = encoutput;
    enc_softc.if_hdrlen = 0;
    enc_softc.if_addrlen = 0;

    if_attach(&enc_softc);

#if NBPFILTER > 0
    bpfattach(&(enc_softc.if_bpf), &enc_softc, DLT_NULL, sizeof(u_int32_t));
#endif

    /* Just a bogus entry */
    ifa = (struct ifaddr *) malloc(sizeof(struct ifaddr) + 
			           sizeof(struct sockaddr), M_IFADDR, M_WAITOK);
    bzero(ifa, sizeof(struct ifaddr) + sizeof(struct sockaddr));
    ifa->ifa_addr = ifa->ifa_dstaddr = (struct sockaddr *) (ifa + 1);
    ifa->ifa_ifp = &enc_softc;
    TAILQ_INSERT_HEAD(&(enc_softc.if_addrlist), ifa, ifa_list);
}

/*
 * Shamelessly stolen from looutput()
 */
int
encoutput(ifp, m, dst, rt)
struct ifnet *ifp;
register struct mbuf *m;
struct sockaddr *dst;
register struct rtentry *rt;
{
    register struct ifqueue *ifq = 0;
    int s, isr;

    if ((m->m_flags & M_PKTHDR) == 0)
      panic("encoutput(): no HDR");

    ifp->if_lastchange = time;

#if NBPFILTER > 0
    if (ifp->if_bpf) 
    {
	/*
	 * We need to prepend the address family as
	 * a four byte field.  Cons up a dummy header
	 * to pacify bpf.  This is safe because bpf
	 * will only read from the mbuf (i.e., it won't
	 * try to free it or keep a pointer a to it).
	 */
	struct mbuf m0;
	u_int af = dst->sa_family;

	m0.m_next = m;
	m0.m_len = 4;
	m0.m_data = (char *) &af;
	
	bpf_mtap(ifp->if_bpf, &m0);
    }
#endif

    m->m_pkthdr.rcvif = ifp;
    
    if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) 
    {
	m_freem(m);
	return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
		rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    }

    ifp->if_opackets++;
    ifp->if_obytes += m->m_pkthdr.len;

    switch (dst->sa_family) 
    {
#ifdef INET
	case AF_INET:
	    ifq = &ipintrq;
	    isr = NETISR_IP;
	    break;
#endif
#ifdef NS
	case AF_NS:
	    ifq = &nsintrq;
	    isr = NETISR_NS;
	    break;
#endif
#ifdef ISO
	case AF_ISO:
	    ifq = &clnlintrq;
	    isr = NETISR_ISO;
		break;
#endif
	default:
	    m_freem(m);
	    return (EAFNOSUPPORT);
    }
    
    s = splimp();

    if (IF_QFULL(ifq)) 
    {
	IF_DROP(ifq);
	m_freem(m);
	splx(s);
	return (ENOBUFS);
    }
	
    IF_ENQUEUE(ifq, m);
    schednetisr(isr);

    /* Statistics */
    ifp->if_ipackets++;
    ifp->if_ibytes += m->m_pkthdr.len;

    splx(s);

    return (0);
}

/* ARGSUSED */
void
encrtrequest(cmd, rt, sa)
int cmd;
struct rtentry *rt;
struct sockaddr *sa;
{
    if (rt)
      rt->rt_rmx.rmx_mtu = ENCMTU;
}


/*
 * Process an ioctl request.
 * Also shamelessly stolen from loioctl()
 */

/* ARGSUSED */
int
encioctl(ifp, cmd, data)
register struct ifnet *ifp;
u_long cmd;
caddr_t data;
{
    register struct ifaddr *ifa;
    register int error = 0;

    switch (cmd)
    {
	case SIOCSIFADDR:
	    /*
	     * Everything else is done at a higher level.
	     */

	    ifp->if_flags |= IFF_UP;
	    ifa = (struct ifaddr *) data;

	    break;

	default:
	    error = EINVAL;
    }

    return error;
}