if_enc.c revision 1.10
1/*	$OpenBSD: if_enc.c,v 1.10 1999/05/16 21:35:54 niklas Exp $	*/
2
3/*
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
7 *
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
18 * and Niels Provos.
19 *
20 * Permission to use, copy, and modify this software without fee
21 * is hereby granted, provided that this entire notice is included in
22 * all copies of any software which is or includes a copy or
23 * modification of this software.
24 * You may use this code under the GNU public license if you so wish. Please
25 * contribute changes back to the authors under this freer than GPL license
26 * so that we may further the use of strong encryption without limitations to
27 * all.
28 *
29 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
30 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
31 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
32 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
33 * PURPOSE.
34 */
35
36/*
37 * Encapsulation interface driver.
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/errno.h>
46#include <sys/ioctl.h>
47#include <sys/time.h>
48#include <machine/cpu.h>
49
50#include <net/if.h>
51#include <net/if_types.h>
52#include <net/netisr.h>
53#include <net/route.h>
54#include <net/bpf.h>
55#include <net/if_enc.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 ISO
65extern struct ifqueue clnlintrq;
66#endif
67
68#ifdef NS
69extern struct ifqueue nsintrq;
70#endif
71
72#include "bpfilter.h"
73
74struct ifnet enc_softc;
75
76void	encattach __P((int));
77int	encoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
78	    	       struct rtentry *));
79int	encioctl __P((struct ifnet *, u_long, caddr_t));
80void	encrtrequest __P((int, struct rtentry *, struct sockaddr *));
81
82void
83encattach(int nenc)
84{
85	struct ifaddr *ifa;
86
87	bzero(&enc_softc, sizeof(struct ifnet));
88
89	/* We only need one interface anyway under the new mode of operation */
90	enc_softc.if_index = 0;
91
92	sprintf(enc_softc.if_xname, "enc0");
93	enc_softc.if_list.tqe_next = NULL;
94	enc_softc.if_mtu = ENCMTU;
95	enc_softc.if_flags = 0;
96	enc_softc.if_type = IFT_ENC;
97	enc_softc.if_ioctl = encioctl;
98	enc_softc.if_output = encoutput;
99	enc_softc.if_hdrlen = ENC_HDRLEN;
100	enc_softc.if_addrlen = 0;
101	if_attach(&enc_softc);
102
103#if NBPFILTER > 0
104	bpfattach(&enc_softc.if_bpf, &enc_softc, DLT_ENC, ENC_HDRLEN);
105#endif
106
107	/* Just a bogus entry */
108	ifa = (struct ifaddr *) malloc(sizeof(struct ifaddr) +
109	    sizeof(struct sockaddr), M_IFADDR, M_WAITOK);
110	bzero(ifa, sizeof(struct ifaddr) + sizeof(struct sockaddr));
111	ifa->ifa_addr = ifa->ifa_dstaddr = (struct sockaddr *) (ifa + 1);
112	ifa->ifa_ifp = &enc_softc;
113	TAILQ_INSERT_HEAD(&(enc_softc.if_addrlist), ifa, ifa_list);
114}
115
116/*
117 * Shamelessly stolen from looutput()
118 */
119int
120encoutput(ifp, m, dst, rt)
121struct ifnet *ifp;
122register struct mbuf *m;
123struct sockaddr *dst;
124register struct rtentry *rt;
125{
126	register struct ifqueue *ifq = 0;
127	int s, isr;
128
129	if ((m->m_flags & M_PKTHDR) == 0)
130		panic("encoutput(): no HDR");
131
132	ifp->if_lastchange = time;
133	m->m_pkthdr.rcvif = ifp;
134
135	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
136		m_freem(m);
137		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
138		    rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
139	}
140
141	ifp->if_opackets++;
142	ifp->if_obytes += m->m_pkthdr.len;
143
144	switch (dst->sa_family) {
145#ifdef INET
146	case AF_INET:
147		ifq = &ipintrq;
148		isr = NETISR_IP;
149		break;
150#endif
151#ifdef NS
152	case AF_NS:
153		ifq = &nsintrq;
154		isr = NETISR_NS;
155		break;
156#endif
157#ifdef ISO
158	case AF_ISO:
159		ifq = &clnlintrq;
160		isr = NETISR_ISO;
161		break;
162#endif
163	default:
164		m_freem(m);
165		return (EAFNOSUPPORT);
166	}
167
168	s = splimp();
169	if (IF_QFULL(ifq)) {
170		IF_DROP(ifq);
171		m_freem(m);
172		splx(s);
173		return (ENOBUFS);
174	}
175
176	IF_ENQUEUE(ifq, m);
177	schednetisr(isr);
178
179	/* Statistics */
180	ifp->if_ipackets++;
181	ifp->if_ibytes += m->m_pkthdr.len;
182	splx(s);
183	return (0);
184}
185
186/* ARGSUSED */
187void
188encrtrequest(cmd, rt, sa)
189	int cmd;
190	struct rtentry *rt;
191	struct sockaddr *sa;
192{
193	if (rt)
194		rt->rt_rmx.rmx_mtu = ENCMTU;
195}
196
197/* ARGSUSED */
198int
199encioctl(ifp, cmd, data)
200	register struct ifnet *ifp;
201	u_long cmd;
202	caddr_t data;
203{
204	register struct ifaddr *ifa;
205	register int error = 0;
206
207	switch (cmd) {
208	case SIOCSIFADDR:
209		/*
210		 * Everything else is done at a higher level.
211		 */
212		ifp->if_flags |= IFF_UP;
213		ifa = (struct ifaddr *) data;
214		break;
215
216	default:
217		error = EINVAL;
218		break;
219	}
220	return (error);
221}
222