pdq_ifsubr.c revision 26640
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 * $Id: pdq_ifsubr.c,v 1.4 1997/03/24 11:32:26 bde Exp $
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 <sys/param.h>
37#include <sys/socket.h>
38#include <sys/sockio.h>
39#if defined(__bsdi__) || defined(__NetBSD__)
40#include <sys/device.h>
41#endif
42
43#include <net/if.h>
44#include <net/if_dl.h>
45
46#include "bpfilter.h"
47#if NBPFILTER > 0
48#include <net/bpf.h>
49#endif
50
51#ifdef INET
52#include <netinet/in.h>
53#include <netinet/if_ether.h>
54#endif
55#if defined(__FreeBSD__)
56#include <netinet/if_fddi.h>
57#else
58#include <net/if_fddi.h>
59#endif
60
61#if defined(__bsdi__)
62#include <i386/isa/isavar.h>
63#endif
64
65#ifdef NS
66#include <netns/ns.h>
67#include <netns/ns_if.h>
68#endif
69
70#if defined(__FreeBSD__)
71#include <dev/pdq/pdqvar.h>
72#include <dev/pdq/pdqreg.h>
73#else
74#include "pdqvar.h"
75#include "pdqreg.h"
76#endif
77
78#if defined(__bsdi__) && _BSDI_VERSION < 199506 /* XXX */
79static void
80arp_ifinit(
81    struct arpcom *ac,
82    struct ifaddr *ifa)
83{
84    sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
85    arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
86#if _BSDI_VERSION >= 199401
87    ifa->ifa_rtrequest = arp_rtrequest;
88    ifa->ifa_flags |= RTF_CLONING;
89#endif
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    for (;;) {
135	struct mbuf *m;
136	IF_DEQUEUE(&ifp->if_snd, m);
137	if (m == NULL)
138	    return;
139	m_freem(m);
140    }
141}
142
143ifnet_ret_t
144pdq_ifstart(
145    struct ifnet *ifp)
146{
147    pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
148    struct ifqueue *ifq = &ifp->if_snd;
149    struct mbuf *m;
150    int tx = 0;
151
152    if ((ifp->if_flags & IFF_RUNNING) == 0)
153	return;
154
155    if (sc->sc_if.if_timer == 0)
156	sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
157
158    if ((sc->sc_pdq->pdq_flags & PDQ_TXOK) == 0) {
159	sc->sc_if.if_flags |= IFF_OACTIVE;
160	return;
161    }
162    for (;; tx = 1) {
163	IF_DEQUEUE(ifq, m);
164	if (m == NULL)
165	    break;
166
167	if (pdq_queue_transmit_data(sc->sc_pdq, m) == PDQ_FALSE) {
168	    ifp->if_flags |= IFF_OACTIVE;
169	    IF_PREPEND(ifq, m);
170	    break;
171	}
172    }
173    if (tx)
174	PDQ_DO_TYPE2_PRODUCER(sc->sc_pdq);
175}
176
177void
178pdq_os_receive_pdu(
179    pdq_t *pdq,
180    struct mbuf *m,
181    size_t pktlen)
182{
183    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
184    struct fddi_header *fh = mtod(m, struct fddi_header *);
185
186    sc->sc_if.if_ipackets++;
187#if NBPFILTER > 0
188    if (sc->sc_bpf != NULL)
189	PDQ_BPF_MTAP(sc, m);
190    if ((fh->fddi_fc & (FDDIFC_L|FDDIFC_F)) != FDDIFC_LLC_ASYNC) {
191	m_freem(m);
192	return;
193    }
194#endif
195
196    m->m_data += sizeof(struct fddi_header);
197    m->m_len  -= sizeof(struct fddi_header);
198    m->m_pkthdr.len = pktlen - sizeof(struct fddi_header);
199    m->m_pkthdr.rcvif = &sc->sc_if;
200    fddi_input(&sc->sc_if, fh, m);
201}
202
203void
204pdq_os_restart_transmitter(
205    pdq_t *pdq)
206{
207    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
208    sc->sc_if.if_flags &= ~IFF_OACTIVE;
209    if (sc->sc_if.if_snd.ifq_head != NULL) {
210	sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
211	pdq_ifstart(&sc->sc_if);
212    } else {
213	sc->sc_if.if_timer = 0;
214    }
215}
216
217void
218pdq_os_transmit_done(
219    pdq_t *pdq,
220    struct mbuf *m)
221{
222    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
223#if NBPFILTER > 0
224    if (sc->sc_bpf != NULL)
225	PDQ_BPF_MTAP(sc, m);
226#endif
227    m_freem(m);
228    sc->sc_if.if_opackets++;
229}
230
231void
232pdq_os_addr_fill(
233    pdq_t *pdq,
234    pdq_lanaddr_t *addr,
235    size_t num_addrs)
236{
237    pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
238    struct ifmultiaddr *ifma;
239
240    for (ifma = sc->sc_if.if_multiaddrs.lh_first; ifma && num_addrs > 0;
241	 ifma = ifma->ifma_link.le_next) {
242	    char *mcaddr;
243	    if (ifma->ifma_addr->sa_family != AF_LINK)
244		    continue;
245	    mcaddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
246	    ((u_short *) addr->lanaddr_bytes)[0] = ((u_short *) mcaddr)[0];
247	    ((u_short *) addr->lanaddr_bytes)[1] = ((u_short *) mcaddr)[1];
248	    ((u_short *) addr->lanaddr_bytes)[2] = ((u_short *) mcaddr)[2];
249	    addr++;
250	    num_addrs--;
251    }
252}
253
254int
255pdq_ifioctl(
256    struct ifnet *ifp,
257    ioctl_cmd_t cmd,
258    caddr_t data)
259{
260    pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
261    int s, error = 0;
262
263    s = splimp();
264
265    switch (cmd) {
266	case SIOCSIFADDR: {
267	    struct ifaddr *ifa = (struct ifaddr *)data;
268
269	    ifp->if_flags |= IFF_UP;
270	    switch(ifa->ifa_addr->sa_family) {
271#if defined(INET)
272		case AF_INET: {
273		    pdq_ifinit(sc);
274		    arp_ifinit(&sc->sc_ac, ifa);
275		    break;
276		}
277#endif /* INET */
278
279#if defined(NS)
280		/* This magic copied from if_is.c; I don't use XNS,
281		 * so I have no way of telling if this actually
282		 * works or not.
283		 */
284		case AF_NS: {
285		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
286		    if (ns_nullhost(*ina)) {
287			ina->x_host = *(union ns_host *)(sc->sc_ac.ac_enaddr);
288		    } else {
289			ifp->if_flags &= ~IFF_RUNNING;
290			bcopy((caddr_t)ina->x_host.c_host,
291			      (caddr_t)sc->sc_ac.ac_enaddr,
292			      sizeof sc->sc_ac.ac_enaddr);
293		    }
294
295		    pdq_ifinit(sc);
296		    break;
297		}
298#endif /* NS */
299
300		default: {
301		    pdq_ifinit(sc);
302		    break;
303		}
304	    }
305	    break;
306	}
307	case SIOCGIFADDR: {
308	    struct ifreq *ifr = (struct ifreq *)data;
309	    bcopy((caddr_t) sc->sc_ac.ac_enaddr,
310		  (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
311		  6);
312	    break;
313	}
314
315	case SIOCSIFFLAGS: {
316	    pdq_ifinit(sc);
317	    break;
318	}
319
320	case SIOCADDMULTI:
321	case SIOCDELMULTI:
322		/*
323		 * Update multicast listeners
324		 */
325		if (sc->sc_if.if_flags & IFF_RUNNING)
326			pdq_run(sc->sc_pdq);
327		error = 0;
328		break;
329
330#if defined(SIOCSIFMTU)
331#if !defined(ifr_mtu)
332#define ifr_mtu ifr_metric
333#endif
334	case SIOCSIFMTU: {
335	    struct ifreq *ifr = (struct ifreq *)data;
336	    /*
337	     * Set the interface MTU.
338	     */
339	    if (ifr->ifr_mtu > FDDIMTU) {
340		error = EINVAL;
341		break;
342	    }
343	    ifp->if_mtu = ifr->ifr_mtu;
344	    break;
345	}
346#endif /* SIOCSIFMTU */
347
348	default: {
349	    error = EINVAL;
350	    break;
351	}
352    }
353
354    splx(s);
355    return error;
356}
357
358#ifndef IFF_NOTRAILERS
359#define	IFF_NOTRAILERS	0
360#endif
361
362void
363pdq_ifattach(
364    pdq_softc_t *sc,
365    ifnet_ret_t (*ifwatchdog)(int unit))
366{
367    struct ifnet *ifp = &sc->sc_if;
368
369    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
370
371#if (defined(__FreeBSD__) && BSD >= 199506) || defined(__NetBSD__)
372    ifp->if_watchdog = pdq_ifwatchdog;
373#else
374    ifp->if_watchdog = ifwatchdog;
375#endif
376
377    ifp->if_ioctl = pdq_ifioctl;
378    ifp->if_output = fddi_output;
379    ifp->if_start = pdq_ifstart;
380#warning "Implement fddi_resolvemulti!"
381/*    ifp->if_resolvemulti = ether_resolvemulti; XXX */
382
383    if_attach(ifp);
384    fddi_ifattach(ifp);
385#if NBPFILTER > 0
386    PDQ_BPFATTACH(sc, DLT_FDDI, sizeof(struct fddi_header));
387#endif
388}
389