aboutsummaryrefslogtreecommitdiff
path: root/sys/gnu/isdn/if_ii.c
blob: 280023d017a6434c57524bec1b0553c1775290f0 (plain) (blame)
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
256
257
static char     _if_iiid[] = "@(#)$Id: if_ii.c,v 1.2 1995/02/15 06:28:26 jkh Exp $";
/*******************************************************************************
 *  II - Version 0.1 $Revision: 1.2 $   $State: Exp $
 *
 * Copyright 1994 Dietmar Friede
 *******************************************************************************
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *	jkr@saarlink.de or jkrause@guug.de
 *
 *******************************************************************************
 * $Log: if_ii.c,v $
 * Revision 1.2  1995/02/15  06:28:26  jkh
 * Fix up include paths, nuke some warnings.
 *
 * Revision 1.1  1995/02/14  15:00:27  jkh
 * An ISDN driver that supports the EDSS1 and the 1TR6 ISDN interfaces.
 * EDSS1 is the "Euro-ISDN", 1TR6 is the soon obsolete german ISDN Interface.
 * Obtained from: Dietmar Friede <dfriede@drnhh.neuhaus.de> and
 * 	Juergen Krause <jkr@saarlink.de>
 *
 * This is only one part - the rest to follow in a couple of hours.
 * This part is a benign import, since it doesn't affect anything else.
 *
 *
 ******************************************************************************/

/*
 * Copyright (c) 1994 Dietmar Friede (dietmar@friede.de) All rights reserved.
 * FSF/FSAG GNU Copyright applies
 *
 * A high level ip isdn driver.
 *
 * Uses loop driver as template. Small - and simple - is beautiful.
 */

#include "param.h"
#include "systm.h"
#include "mbuf.h"
#include "socket.h"
#include "errno.h"
#include "ioctl.h"
#include "protosw.h"

#include "net/if.h"
#include "net/if_types.h"
#include "net/netisr.h"
#include "net/route.h"

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

#include "ii.h"
#include "gnu/isdn/isdn_ioctl.h"

#define	IIMTU	1500

static struct ifnet ii_if[NII];
static int      applnr[NII];
static int      next_if = 0;
int             iioutput(), ii_ioctl();

int
iiattach(int ap)
{
	register struct ifnet *ifp;

	if (next_if >= NII)
		return -1;

	applnr[next_if] = ap;
	ifp = &ii_if[next_if];
	ifp->if_unit = next_if;
	ifp->if_name = "ii";
	ifp->if_mtu = IIMTU;
	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT ;
	ifp->if_ioctl = ii_ioctl;
	ifp->if_output = iioutput;
	ifp->if_type = IFT_ISDNBASIC;
	ifp->if_hdrlen = 0;
	ifp->if_addrlen = 0;
	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
	if_attach(ifp);
	/* ifp->if_flags |= IFF_RUNNING; */
	return next_if++;
}

int
iioutput(struct ifnet * ifp, struct mbuf * m, struct sockaddr * dst)
{
	int             s, isr;
	register struct ifqueue *ifq = 0;

	if (dst->sa_family != AF_INET)
	{
		m_freem(m);
		return EAFNOSUPPORT;
	}
	s = splhigh();
	if (IF_QFULL(&ifp->if_snd))
	{
		IF_DROP(&ifp->if_snd);
		m_freem(m);
		ifp->if_oerrors++;
		isdn_output(applnr[ifp->if_unit]);
		splx(s);
		return (ENOBUFS);
	}
	IF_ENQUEUE(&ifp->if_snd, m);

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

	isdn_output(applnr[ifp->if_unit]);
	splx(s);
	return (0);
}

int
ii_input(int no, int len, char *buf)
{
	int             error = 0;
	struct mbuf    *m;
	struct ifnet   *ifp = &(ii_if[no]);
	int             s;

	s = splhigh();
	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m == 0)
	{
		splx(s);
		return (0);
	}

	if (len >= MHLEN)
	{
		MCLGET(m, M_DONTWAIT);
		if ((m->m_flags & M_EXT) == 0)
		{
			(void) m_free(m);
			splx(s);
			return (0);
		}
	}
	bcopy((caddr_t) buf, mtod(m, caddr_t), len);
	m->m_pkthdr.rcvif = ifp;
	m->m_pkthdr.len = len;
	m->m_len = len;

	if (IF_QFULL(&ipintrq))
	{
		IF_DROP(&ipintrq);
		ifp->if_ierrors++;
		m_freem(m);
		splx(s);
		return(0);
	}
	IF_ENQUEUE(&ipintrq, m);
	ifp->if_ipackets++;
	schednetisr(NETISR_IP);
	splx(s);
	return(len);
}

void
ii_connect(int no)
{
	struct ifnet   *ifp = &ii_if[no];
	ifp->if_flags |= IFF_RUNNING;
}

void
ii_disconnect(int no)
{
	struct ifnet   *ifp = &ii_if[no];
	ifp->if_flags &= ~IFF_RUNNING;
}

int
ii_out(int no, char *buf, int len)
{
	struct ifnet   *ifp = &ii_if[no];
	struct mbuf    *m0, *m;
	int             l;

	IF_DEQUEUE(&ifp->if_snd, m);
	if (m == 0)
	{
		return (0);
	}
	/*
	 * Copy the mbuf chain into the transmit buf
	 */
	l = 0;
	for (m0 = m; m != 0; m = m->m_next)
	{
		if((l+= m->m_len) > len)
		{
			m_freem(m0);
			return(0);
		}
		bcopy(mtod(m, caddr_t), buf, m->m_len);
		buf += m->m_len;
	}
	m_freem(m0);

	return (l);
}

/*
 * Process an ioctl request.
 */
int
ii_ioctl(ifp, cmd, data)
	register struct ifnet *ifp;
	int             cmd;
	caddr_t         data;
{
	struct ifaddr *ifa = (struct ifaddr *) data;
	struct ifreq *ifr = (struct ifreq *) data;
	int s;

	switch (cmd)
	{
	case SIOCSIFDSTADDR:
	case SIOCAIFADDR:
	case SIOCSIFADDR:
		if (ifa->ifa_addr->sa_family != AF_INET)
			return(EAFNOSUPPORT);
		ifp->if_flags |= IFF_UP;
	/* FALLTHROUGH */
	case SIOCSIFFLAGS:
		s= splhigh();
		if((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING))
		{
			isdn_disconnect(applnr[ifp->if_unit],0);
			ifp->if_flags &= ~IFF_RUNNING;
		}
		break;
	case SIOCSIFMTU:
		ifr->ifr_metric = ifp->if_mtu;
		break;
	case SIOCGIFMTU:
		if(ifr->ifr_metric < 2048)
			return(EAFNOSUPPORT);
		ifp->if_mtu = ifr->ifr_metric;
		break;
	default:
printf("IIO %x",cmd);
		return(EINVAL);
	}
	return(0);
}