if_enc.c revision 1.12
1/*	$OpenBSD: if_enc.c,v 1.12 1999/10/29 05:22:41 angelos 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
56#include <netinet/ip_ipsp.h>
57#include <net/if_enc.h>
58
59#ifdef	INET
60#include <netinet/in.h>
61#include <netinet/in_systm.h>
62#include <netinet/in_var.h>
63#include <netinet/ip.h>
64#endif
65
66#ifdef ISO
67extern struct ifqueue clnlintrq;
68#endif
69
70#ifdef NS
71extern struct ifqueue nsintrq;
72#endif
73
74#include "bpfilter.h"
75#include "enc.h"
76
77struct enc_softc encif[NENC];
78
79void	encattach __P((int));
80int	encoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
81	    	       struct rtentry *));
82int	encioctl __P((struct ifnet *, u_long, caddr_t));
83void	encrtrequest __P((int, struct rtentry *, struct sockaddr *));
84void	encstart __P((struct ifnet *));
85
86extern int ifqmaxlen;
87
88void
89encattach(int nenc)
90{
91    struct ifnet *ifp;
92    int i;
93
94    bzero(encif, NENC * sizeof(encif));
95
96    for (i = 0; i < NENC; i++) {
97	ifp = &encif[i].sc_if;
98	sprintf(ifp->if_xname, "enc%d", i);
99	ifp->if_softc = &encif[i];
100	ifp->if_mtu = ENCMTU;
101	ifp->if_ioctl = encioctl;
102	ifp->if_output = encoutput;
103	ifp->if_start = encstart;
104	ifp->if_type = IFT_ENC;
105	ifp->if_snd.ifq_maxlen = ifqmaxlen;
106	ifp->if_hdrlen = ENC_HDRLEN;
107	if_attach(ifp);
108
109#if NBPFILTER > 0
110	bpfattach(&encif[i].sc_if.if_bpf, ifp, DLT_ENC, ENC_HDRLEN);
111#endif
112    }
113}
114
115/*
116 * Start output on the enc interface.
117 */
118void
119encstart(ifp)
120struct ifnet *ifp;
121{
122    /* XXX Code needed */
123}
124
125/*
126 * Shamelessly stolen from looutput()
127 */
128int
129encoutput(ifp, m, dst, rt)
130struct ifnet *ifp;
131register struct mbuf *m;
132struct sockaddr *dst;
133register struct rtentry *rt;
134{
135    register struct ifqueue *ifq = 0;
136    int s, isr;
137
138    if ((m->m_flags & M_PKTHDR) == 0)
139      panic("encoutput(): no HDR");
140
141    ifp->if_lastchange = time;
142    m->m_pkthdr.rcvif = ifp;
143
144    if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
145	m_freem(m);
146	return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
147		rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
148    }
149
150    ifp->if_opackets++;
151    ifp->if_obytes += m->m_pkthdr.len;
152
153    switch (dst->sa_family) {
154#ifdef INET
155	case AF_INET:
156	    ifq = &ipintrq;
157	    isr = NETISR_IP;
158	    break;
159#endif
160#ifdef NS
161	case AF_NS:
162	    ifq = &nsintrq;
163	    isr = NETISR_NS;
164	    break;
165#endif
166#ifdef ISO
167	case AF_ISO:
168	    ifq = &clnlintrq;
169	    isr = NETISR_ISO;
170	    break;
171#endif
172	default:
173	    m_freem(m);
174	    return (EAFNOSUPPORT);
175    }
176
177    s = splimp();
178    if (IF_QFULL(ifq)) {
179	IF_DROP(ifq);
180	m_freem(m);
181	splx(s);
182	return (ENOBUFS);
183    }
184
185    IF_ENQUEUE(ifq, m);
186    schednetisr(isr);
187
188    /* Statistics */
189    ifp->if_ipackets++;
190    ifp->if_ibytes += m->m_pkthdr.len;
191    splx(s);
192    return (0);
193}
194
195/* ARGSUSED */
196void
197encrtrequest(cmd, rt, sa)
198int cmd;
199struct rtentry *rt;
200struct sockaddr *sa;
201{
202    if (rt)
203      rt->rt_rmx.rmx_mtu = ENCMTU;
204}
205
206/* ARGSUSED */
207int
208encioctl(ifp, cmd, data)
209register struct ifnet *ifp;
210u_long cmd;
211caddr_t data;
212{
213    register struct ifaddr *ifa;
214    register int error = 0;
215
216    switch (cmd) {
217	case SIOCSIFADDR:
218	    /*
219	     * Everything else is done at a higher level.
220	     */
221	    ifp->if_flags |= IFF_UP;
222	    ifa = (struct ifaddr *) data;
223	    break;
224
225	default:
226	    error = EINVAL;
227	    break;
228    }
229    return (error);
230}
231