pdq_ifsubr.c revision 71999
1/*-
2 * Copyright (c) 1995, 1996 Matt Thomas <matt@3am-software.com>
3 * 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. The name of the author may not be used to endorse or promote products
11 *    derived from this software withough specific prior written permission
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * $FreeBSD: head/sys/dev/pdq/pdq_ifsubr.c 71999 2001-02-04 13:13:25Z phk $
25 *
26 */
27
28/*
29 * DEC PDQ FDDI Controller; code for BSD derived operating systems
30 *
31 *	This module provide bus independent BSD specific O/S functions.
32 *	(ie. it provides an ifnet interface to the rest of the system)
33 */
34
35
36#include "opt_inet.h"
37
38#include <sys/param.h>
39#include <sys/socket.h>
40#include <sys/sockio.h>
41#if defined(__bsdi__) || defined(__NetBSD__)
42#include <sys/device.h>
43#endif
44
45#include <net/if.h>
46#include <net/if_dl.h>
47
48#include <net/bpf.h>
49
50#if defined(__FreeBSD__)
51#ifdef INET
52#include <netinet/in.h>
53#include <netinet/if_ether.h>
54#endif
55#include <netinet/if_fddi.h>
56#else
57#include <net/if_fddi.h>
58#endif
59
60#if defined(__bsdi__)
61#include <i386/isa/isavar.h>
62#endif
63
64#ifdef NS
65#include <netns/ns.h>
66#include <netns/ns_if.h>
67#endif
68
69#if defined(__FreeBSD__)
70#include <dev/pdq/pdqvar.h>
71#include <dev/pdq/pdqreg.h>
72#else
73#include "pdqvar.h"
74#include "pdqreg.h"
75#endif
76
77#if defined(__bsdi__) && _BSDI_VERSION < 199506 /* XXX */
78static void
79arp_ifinit(
80    struct arpcom *ac,
81    struct ifaddr *ifa)
82{
83    sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
84    arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
85#if _BSDI_VERSION >= 199401
86    ifa->ifa_rtrequest = arp_rtrequest;
87    ifa->ifa_flags |= RTF_CLONING;
88#endif
89}
90#endif
91
92
93void
94pdq_ifinit(
95    pdq_softc_t *sc)
96{
97    if (sc->sc_if.if_flags & IFF_UP) {
98	sc->sc_if.if_flags |= IFF_RUNNING;
99	if (sc->sc_if.if_flags & IFF_PROMISC) {
100	    sc->sc_pdq->pdq_flags |= PDQ_PROMISC;
101	} else {
102	    sc->sc_pdq->pdq_flags &= ~PDQ_PROMISC;
103	}
104	if (sc->sc_if.if_flags & IFF_ALLMULTI) {
105	    sc->sc_pdq->pdq_flags |= PDQ_ALLMULTI;
106	} else {
107	    sc->sc_pdq->pdq_flags &= ~PDQ_ALLMULTI;
108	}
109	if (sc->sc_if.if_flags & IFF_LINK1) {
110	    sc->sc_pdq->pdq_flags |= PDQ_PASS_SMT;
111	} else {
112	    sc->sc_pdq->pdq_flags &= ~PDQ_PASS_SMT;
113	}
114	sc->sc_pdq->pdq_flags |= PDQ_RUNNING;
115	pdq_run(sc->sc_pdq);
116    } else {
117	sc->sc_if.if_flags &= ~IFF_RUNNING;
118	sc->sc_pdq->pdq_flags &= ~PDQ_RUNNING;
119	pdq_stop(sc->sc_pdq);
120    }
121}
122
123void
124pdq_ifwatchdog(
125    struct ifnet *ifp)
126{
127    /*
128     * No progress was made on the transmit queue for PDQ_OS_TX_TRANSMIT
129     * seconds.  Remove all queued packets.
130     */
131
132    ifp->if_flags &= ~IFF_OACTIVE;
133    ifp->if_timer = 0;
134    IF_DRAIN(&ifp->if_snd);
135}
136
137ifnet_ret_t
138pdq_ifstart(
139    struct ifnet *ifp)
140{
141    pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
142    struct ifqueue *ifq = &ifp->if_snd;
143    struct mbuf *m;
144    int tx = 0;
145
146    if ((ifp->if_flags & IFF_RUNNING) == 0)
147	return;
148
149    if (sc->sc_if.if_timer == 0)
150	sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
151
152    if ((sc->sc_pdq->pdq_flags & PDQ_TXOK) == 0) {
153	sc->sc_if.if_flags |= IFF_OACTIVE;
154	return;
155    }
156    for (;; tx = 1) {
157	IF_DEQUEUE(ifq, m);
158	if (m == NULL)
159	    break;
160
161	if (pdq_queue_transmit_data(sc->sc_pdq, m) == PDQ_FALSE) {
162	    ifp->if_flags |= IFF_OACTIVE;
163	    IF_PREPEND(ifq, m);
164	    break;
165	}
166    }
167    if (tx)
168	PDQ_DO_TYPE2_PRODUCER(sc->sc_pdq);
169}
170
171void
172pdq_os_receive_pdu(
173    pdq_t *pdq,
174    struct mbuf *m,
175    size_t pktlen)
176{
177    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
178    struct fddi_header *fh = mtod(m, struct fddi_header *);
179
180    sc->sc_if.if_ipackets++;
181    if (sc->sc_bpf != NULL)
182	PDQ_BPF_MTAP(sc, m);
183    if ((fh->fddi_fc & (FDDIFC_L|FDDIFC_F)) != FDDIFC_LLC_ASYNC) {
184	m_freem(m);
185	return;
186    }
187
188    m->m_data += sizeof(struct fddi_header);
189    m->m_len  -= sizeof(struct fddi_header);
190    m->m_pkthdr.len = pktlen - sizeof(struct fddi_header);
191    m->m_pkthdr.rcvif = &sc->sc_if;
192    fddi_input(&sc->sc_if, fh, m);
193}
194
195void
196pdq_os_restart_transmitter(
197    pdq_t *pdq)
198{
199    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
200    sc->sc_if.if_flags &= ~IFF_OACTIVE;
201    if (sc->sc_if.if_snd.ifq_head != NULL) {
202	sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
203	pdq_ifstart(&sc->sc_if);
204    } else {
205	sc->sc_if.if_timer = 0;
206    }
207}
208
209void
210pdq_os_transmit_done(
211    pdq_t *pdq,
212    struct mbuf *m)
213{
214    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
215    if (sc->sc_bpf != NULL)
216	PDQ_BPF_MTAP(sc, m);
217    m_freem(m);
218    sc->sc_if.if_opackets++;
219}
220
221void
222pdq_os_addr_fill(
223    pdq_t *pdq,
224    pdq_lanaddr_t *addr,
225    size_t num_addrs)
226{
227    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
228    struct ifmultiaddr *ifma;
229
230    for (ifma = LIST_FIRST(&sc->sc_if.if_multiaddrs); ifma && num_addrs > 0;
231	 ifma = LIST_NEXT(ifma, ifma_link)) {
232	    char *mcaddr;
233	    if (ifma->ifma_addr->sa_family != AF_LINK)
234		    continue;
235	    mcaddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
236	    ((u_short *) addr->lanaddr_bytes)[0] = ((u_short *) mcaddr)[0];
237	    ((u_short *) addr->lanaddr_bytes)[1] = ((u_short *) mcaddr)[1];
238	    ((u_short *) addr->lanaddr_bytes)[2] = ((u_short *) mcaddr)[2];
239	    addr++;
240	    num_addrs--;
241    }
242}
243
244int
245pdq_ifioctl(
246    struct ifnet *ifp,
247    ioctl_cmd_t cmd,
248    caddr_t data)
249{
250    pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
251    int s, error = 0;
252
253    s = splimp();
254
255    switch (cmd) {
256	case SIOCSIFADDR: {
257	    struct ifaddr *ifa = (struct ifaddr *)data;
258
259	    ifp->if_flags |= IFF_UP;
260	    switch(ifa->ifa_addr->sa_family) {
261#if defined(INET)
262		case AF_INET: {
263		    pdq_ifinit(sc);
264		    arp_ifinit(&sc->sc_ac, ifa);
265		    break;
266		}
267#endif /* INET */
268
269#if defined(NS)
270		/* This magic copied from if_is.c; I don't use XNS,
271		 * so I have no way of telling if this actually
272		 * works or not.
273		 */
274		case AF_NS: {
275		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
276		    if (ns_nullhost(*ina)) {
277			ina->x_host = *(union ns_host *)(sc->sc_ac.ac_enaddr);
278		    } else {
279			ifp->if_flags &= ~IFF_RUNNING;
280			bcopy((caddr_t)ina->x_host.c_host,
281			      (caddr_t)sc->sc_ac.ac_enaddr,
282			      sizeof sc->sc_ac.ac_enaddr);
283		    }
284
285		    pdq_ifinit(sc);
286		    break;
287		}
288#endif /* NS */
289
290		default: {
291		    pdq_ifinit(sc);
292		    break;
293		}
294	    }
295	    break;
296	}
297	case SIOCGIFADDR: {
298	    struct ifreq *ifr = (struct ifreq *)data;
299	    bcopy((caddr_t) sc->sc_ac.ac_enaddr,
300		  (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
301		  6);
302	    break;
303	}
304
305	case SIOCSIFFLAGS: {
306	    pdq_ifinit(sc);
307	    break;
308	}
309
310	case SIOCADDMULTI:
311	case SIOCDELMULTI:
312		/*
313		 * Update multicast listeners
314		 */
315		if (sc->sc_if.if_flags & IFF_RUNNING)
316			pdq_run(sc->sc_pdq);
317		error = 0;
318		break;
319
320#if defined(SIOCSIFMTU)
321#if !defined(ifr_mtu)
322#define ifr_mtu ifr_metric
323#endif
324	case SIOCSIFMTU: {
325	    struct ifreq *ifr = (struct ifreq *)data;
326	    /*
327	     * Set the interface MTU.
328	     */
329	    if (ifr->ifr_mtu > FDDIMTU) {
330		error = EINVAL;
331		break;
332	    }
333	    ifp->if_mtu = ifr->ifr_mtu;
334	    break;
335	}
336#endif /* SIOCSIFMTU */
337
338	default: {
339	    error = EINVAL;
340	    break;
341	}
342    }
343
344    splx(s);
345    return error;
346}
347
348#ifndef IFF_NOTRAILERS
349#define	IFF_NOTRAILERS	0
350#endif
351
352void
353pdq_ifattach(
354    pdq_softc_t *sc,
355    ifnet_ret_t (*ifwatchdog)(int unit))
356{
357    struct ifnet *ifp = &sc->sc_if;
358
359    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
360
361#if (defined(__FreeBSD__) && BSD >= 199506) || defined(__NetBSD__)
362    ifp->if_watchdog = pdq_ifwatchdog;
363#else
364    ifp->if_watchdog = ifwatchdog;
365#endif
366
367    ifp->if_ioctl = pdq_ifioctl;
368    ifp->if_output = fddi_output;
369    ifp->if_start = pdq_ifstart;
370    ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
371#warning "Implement fddi_resolvemulti!"
372/*    ifp->if_resolvemulti = ether_resolvemulti; XXX */
373
374    if_attach(ifp);
375    fddi_ifattach(ifp);
376    PDQ_BPFATTACH(sc, DLT_FDDI, sizeof(struct fddi_header));
377}
378