1*076e3579Sriastradh /* $NetBSD: if_srt.c,v 1.32 2021/06/16 00:21:19 riastradh Exp $ */
2ebc746c1Smouse /* This file is in the public domain. */
3ebc746c1Smouse
4456279dfSlukem #include <sys/cdefs.h>
5*076e3579Sriastradh __KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.32 2021/06/16 00:21:19 riastradh Exp $");
6456279dfSlukem
71c4a50f1Spooka #ifdef _KERNEL_OPT
8ebc746c1Smouse #include "opt_inet.h"
91c4a50f1Spooka #endif
10ebc746c1Smouse
11ebc746c1Smouse #if !defined(INET) && !defined(INET6)
12ebc746c1Smouse #error "srt without INET/INET6?"
13ebc746c1Smouse #endif
14ebc746c1Smouse
15ebc746c1Smouse #ifndef SRT_MAXUNIT
16ebc746c1Smouse #define SRT_MAXUNIT 255
17ebc746c1Smouse #endif
18ebc746c1Smouse
199707fdffSmouse /* include-file bug workarounds */
209707fdffSmouse #include <sys/types.h> /* sys/conf.h */
21a4392067Sdyoung #include <sys/resource.h> /* sys/resourcevar.h
22a4392067Sdyoung * (uvm/uvm_param.h, sys/mbuf.h)
23a4392067Sdyoung */
249707fdffSmouse #include <netinet/in.h> /* netinet/ip.h */
259707fdffSmouse #include <sys/param.h> /* sys/mbuf.h */
269707fdffSmouse #include <netinet/in_systm.h> /* netinet/ip.h */
279707fdffSmouse
28ebc746c1Smouse #include <sys/conf.h>
29ebc746c1Smouse #include <sys/mbuf.h>
30ebc746c1Smouse #include <sys/errno.h>
31ebc746c1Smouse #include <sys/fcntl.h>
32ebc746c1Smouse #include <sys/param.h>
33ebc746c1Smouse #include <sys/ioctl.h>
341d8e08d4Schristos #include <sys/module.h>
351d8e08d4Schristos #include <sys/device.h>
36ebc746c1Smouse #include <netinet/ip.h>
37ebc746c1Smouse #include <netinet/ip6.h>
384fbae14dSkre #include <netinet6/in6_var.h>
393f909d17Sozaki-r #include <netinet6/ip6_var.h>
404fbae14dSkre #include <netinet6/nd6.h>
414fbae14dSkre #include <netinet6/scope6_var.h>
42ebc746c1Smouse #include <net/if_types.h>
43ebc746c1Smouse
44ebc746c1Smouse #include "if_srt.h"
45ebc746c1Smouse
46ebc746c1Smouse /* until we know what to pass to bpfattach.... */
4710fe49d7Spooka /* #define BPFILTER_NOW_AVAILABLE */
48ebc746c1Smouse
49a4392067Sdyoung struct srt_softc {
50ebc746c1Smouse struct ifnet intf; /* XXX interface botch */
51ebc746c1Smouse int unit;
52ebc746c1Smouse int nrt;
53a4392067Sdyoung struct srt_rt **rts;
54ebc746c1Smouse unsigned int flags; /* SSF_* values from if_srt.h */
55ebc746c1Smouse #define SSF_UCHG (SSF_MTULOCK) /* userland-changeable bits */
56ebc746c1Smouse unsigned int kflags; /* bits private to this file */
57ebc746c1Smouse #define SKF_CDEVOPEN 0x00000001
58ebc746c1Smouse };
59ebc746c1Smouse
601d8e08d4Schristos #include "ioconf.h"
61a4392067Sdyoung
62a4392067Sdyoung static struct srt_softc *softcv[SRT_MAXUNIT+1];
63ebc746c1Smouse static unsigned int global_flags;
64ebc746c1Smouse
651d8e08d4Schristos static u_int srt_count;
661d8e08d4Schristos
670a2e0c92Spgoyette #ifdef _MODULE
680a2e0c92Spgoyette devmajor_t srt_bmajor = -1, srt_cmajor = -1;
690a2e0c92Spgoyette #endif
700a2e0c92Spgoyette
710a2e0c92Spgoyette static int srt_open(dev_t, int, int, struct lwp *);
720a2e0c92Spgoyette static int srt_close(dev_t, int, int, struct lwp *);
730a2e0c92Spgoyette static int srt_ioctl(dev_t, u_long, void *, int, struct lwp *);
740a2e0c92Spgoyette
750a2e0c92Spgoyette const struct cdevsw srt_cdevsw = {
760a2e0c92Spgoyette .d_open = srt_open,
770a2e0c92Spgoyette .d_close = srt_close,
780a2e0c92Spgoyette .d_read = nullread,
790a2e0c92Spgoyette .d_write = nullwrite,
800a2e0c92Spgoyette .d_ioctl = srt_ioctl,
810a2e0c92Spgoyette .d_stop = nullstop,
820a2e0c92Spgoyette .d_tty = notty,
830a2e0c92Spgoyette .d_poll = nullpoll,
840a2e0c92Spgoyette .d_mmap = nommap,
850a2e0c92Spgoyette .d_kqfilter = nullkqfilter,
860a2e0c92Spgoyette .d_discard = nodiscard,
870a2e0c92Spgoyette .d_flag = D_OTHER
880a2e0c92Spgoyette };
890a2e0c92Spgoyette
90ebc746c1Smouse /* Internal routines. */
91ebc746c1Smouse
92a4392067Sdyoung static unsigned int ipv4_masks[33] = {
93a4392067Sdyoung 0x00000000, /* /0 */
94ebc746c1Smouse 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, /* /1 - /4 */
95ebc746c1Smouse 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000, /* /5 - /8 */
96ebc746c1Smouse 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, /* /9 - /12 */
97ebc746c1Smouse 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, /* /13 - /16 */
98ebc746c1Smouse 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000, /* /17 - /20 */
99ebc746c1Smouse 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, /* /21 - /24 */
100ebc746c1Smouse 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, /* /25 - /28 */
101a4392067Sdyoung 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff /* /29 - /32 */
102a4392067Sdyoung };
103ebc746c1Smouse
104a4392067Sdyoung static void
update_mtu(struct srt_softc * sc)105a4392067Sdyoung update_mtu(struct srt_softc *sc)
106ebc746c1Smouse {
107ebc746c1Smouse int mtu;
108ebc746c1Smouse int i;
109a4392067Sdyoung struct srt_rt *r;
110ebc746c1Smouse
111a4392067Sdyoung if (sc->flags & SSF_MTULOCK)
112a4392067Sdyoung return;
113ebc746c1Smouse mtu = 65535;
114a4392067Sdyoung for (i = sc->nrt-1; i>=0; i--) {
115a4392067Sdyoung r = sc->rts[i];
116a4392067Sdyoung if (r->u.dstifp->if_mtu < mtu)
117a4392067Sdyoung mtu = r->u.dstifp->if_mtu;
118ebc746c1Smouse }
119ebc746c1Smouse sc->intf.if_mtu = mtu;
120ebc746c1Smouse }
121ebc746c1Smouse
122a4392067Sdyoung static struct srt_rt *
find_rt(struct srt_softc * sc,int af,...)123a4392067Sdyoung find_rt(struct srt_softc *sc, int af, ...)
124ebc746c1Smouse {
125ebc746c1Smouse int i;
126a4392067Sdyoung struct srt_rt *r;
127ebc746c1Smouse struct in_addr ia;
128ebc746c1Smouse struct in6_addr ia6;
129ebc746c1Smouse va_list ap;
130ebc746c1Smouse
131ebc746c1Smouse ia.s_addr = 0; ia6.s6_addr[0] = 0; /* shut up incorrect -Wuninitialized */
132ebc746c1Smouse va_start(ap,af);
133a4392067Sdyoung switch (af) {
134a4392067Sdyoung case AF_INET:
135ebc746c1Smouse ia = va_arg(ap,struct in_addr);
136ebc746c1Smouse break;
137ebc746c1Smouse case AF_INET6:
138ebc746c1Smouse ia6 = va_arg(ap,struct in6_addr);
139ebc746c1Smouse break;
140ebc746c1Smouse default:
141ebc746c1Smouse panic("if_srt find_rt: impossible address family");
142ebc746c1Smouse break;
143ebc746c1Smouse }
144ebc746c1Smouse va_end(ap);
145a4392067Sdyoung for (i=0; i < sc->nrt; i++) {
146a4392067Sdyoung r = sc->rts[i];
147a4392067Sdyoung if (r->af != af)
148a4392067Sdyoung continue;
149a4392067Sdyoung switch (af) {
150a4392067Sdyoung case AF_INET:
151cb72c101Stls if ((ia.s_addr & htonl(ipv4_masks[r->srcmask])) ==
152a4392067Sdyoung r->srcmatch.v4.s_addr)
153a4392067Sdyoung return r;
154ebc746c1Smouse break;
155ebc746c1Smouse case AF_INET6:
156a4392067Sdyoung if ((r->srcmask >= 8) &&
157a4392067Sdyoung memcmp(&ia6,&r->srcmatch.v6,r->srcmask / 8) != 0)
158a4392067Sdyoung continue;
159ebc746c1Smouse if ((r->srcmask % 8) &&
160ebc746c1Smouse ((ia6.s6_addr[r->srcmask / 8] ^
161ebc746c1Smouse r->srcmatch.v6.s6_addr[r->srcmask / 8]) &
162a4392067Sdyoung 0xff & (0xff00 >> (r->srcmask % 8))))
163a4392067Sdyoung continue;
164a4392067Sdyoung return r;
165ebc746c1Smouse default:
166ebc746c1Smouse panic("if_srt find_rt: impossible address family 2");
167ebc746c1Smouse break;
168ebc746c1Smouse }
169ebc746c1Smouse }
170a4392067Sdyoung return 0;
171ebc746c1Smouse }
172ebc746c1Smouse
173ebc746c1Smouse /* Network device interface. */
174ebc746c1Smouse
175a4392067Sdyoung static int
srt_if_ioctl(struct ifnet * ifp,u_long cmd,void * data)176a4392067Sdyoung srt_if_ioctl(struct ifnet *ifp, u_long cmd, void *data)
177ebc746c1Smouse {
178ebc746c1Smouse struct ifaddr *ifa;
179ebc746c1Smouse int s;
180ebc746c1Smouse int err;
181ebc746c1Smouse
182ebc746c1Smouse err = 0;
183ebc746c1Smouse s = splnet();
184a4392067Sdyoung switch (cmd) {
185a4392067Sdyoung case SIOCINITIFADDR:
186ebc746c1Smouse ifa = (void *) data;
187a4392067Sdyoung switch (ifa->ifa_addr->sa_family) {
188ebc746c1Smouse #ifdef INET
189ebc746c1Smouse case AF_INET:
190a4392067Sdyoung break;
191ebc746c1Smouse #endif
192ebc746c1Smouse #ifdef INET6
193ebc746c1Smouse case AF_INET6:
194ebc746c1Smouse break;
195a4392067Sdyoung #endif
196ebc746c1Smouse default:
197ebc746c1Smouse err = EAFNOSUPPORT;
198ebc746c1Smouse break;
199ebc746c1Smouse }
200ebc746c1Smouse break;
201de87fe67Sdyoung default:
202a4392067Sdyoung if ((err = ifioctl_common(ifp, cmd, data)) == ENETRESET)
2032ccede0aSdyoung err = 0;
204ebc746c1Smouse break;
205ebc746c1Smouse }
206ebc746c1Smouse splx(s);
207a4392067Sdyoung return err;
208ebc746c1Smouse }
209ebc746c1Smouse
210a4392067Sdyoung static int
srt_if_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * to,const struct rtentry * rtp)211a4392067Sdyoung srt_if_output(
212a4392067Sdyoung struct ifnet *ifp,
213ebc746c1Smouse struct mbuf *m,
2145493f188Sdyoung const struct sockaddr *to,
2152cf7873bSozaki-r const struct rtentry *rtp)
216ebc746c1Smouse {
217a4392067Sdyoung struct srt_softc *sc;
218a4392067Sdyoung struct srt_rt *r;
219ebc746c1Smouse
220a4392067Sdyoung sc = ifp->if_softc;
221a4392067Sdyoung if (! (ifp->if_flags & IFF_UP)) {
222a4392067Sdyoung m_freem(m);
223a4392067Sdyoung return ENETDOWN;
224ebc746c1Smouse }
225a4392067Sdyoung switch (to->sa_family) {
226ebc746c1Smouse #ifdef INET
227a4392067Sdyoung case AF_INET: {
228a4392067Sdyoung struct ip *ip;
229ebc746c1Smouse ip = mtod(m,struct ip *);
230ebc746c1Smouse r = find_rt(sc,AF_INET,ip->ip_src);
231ebc746c1Smouse break;
232a4392067Sdyoung }
233ebc746c1Smouse #endif
234a4392067Sdyoung #ifdef INET6
235a4392067Sdyoung case AF_INET6: {
236a4392067Sdyoung struct ip6_hdr *ip;
237ebc746c1Smouse ip = mtod(m,struct ip6_hdr *);
238ebc746c1Smouse r = find_rt(sc,AF_INET6,ip->ip6_src);
239a4392067Sdyoung break;
240ebc746c1Smouse }
241a4392067Sdyoung #endif
242ebc746c1Smouse default:
243a4392067Sdyoung IF_DROP(&ifp->if_snd);
244ebc746c1Smouse m_freem(m);
245a4392067Sdyoung return EAFNOSUPPORT;
246ebc746c1Smouse }
247ebc746c1Smouse /* XXX Do we need to bpf_tap? Or do higher layers now handle that? */
248ebc746c1Smouse /* if_gif.c seems to imply the latter. */
249d99f8f36Sthorpej if_statinc(ifp, if_opackets);
250a4392067Sdyoung if (! r) {
251d99f8f36Sthorpej if_statinc(ifp, if_oerrors);
252ebc746c1Smouse m_freem(m);
253a4392067Sdyoung return 0;
254ebc746c1Smouse }
255a4392067Sdyoung if (! (m->m_flags & M_PKTHDR)) {
256a4392067Sdyoung printf("srt_if_output no PKTHDR\n");
257ebc746c1Smouse m_freem(m);
258a4392067Sdyoung return 0;
259ebc746c1Smouse }
260d99f8f36Sthorpej if_statadd(ifp, if_obytes, m->m_pkthdr.len);
261a4392067Sdyoung if (! (r->u.dstifp->if_flags & IFF_UP)) {
262a4392067Sdyoung m_freem(m);
263a4392067Sdyoung return 0; /* XXX ENETDOWN? */
264ebc746c1Smouse }
265ebc746c1Smouse /* XXX is 0 the right last arg here? */
2664fbae14dSkre if (to->sa_family == AF_INET6)
2673f909d17Sozaki-r return ip6_if_output(r->u.dstifp, r->u.dstifp, m, &r->dst.sin6, 0);
26895fc1456Sknakahara return if_output_lock(r->u.dstifp, r->u.dstifp, m, &r->dst.sa, 0);
269ebc746c1Smouse }
270ebc746c1Smouse
271a4392067Sdyoung static int
srt_clone_create(struct if_clone * cl,int unit)272a4392067Sdyoung srt_clone_create(struct if_clone *cl, int unit)
273ebc746c1Smouse {
274a4392067Sdyoung struct srt_softc *sc;
275ebc746c1Smouse
276a4392067Sdyoung if (unit < 0 || unit > SRT_MAXUNIT)
277a4392067Sdyoung return ENXIO;
278a4392067Sdyoung if (softcv[unit])
279a4392067Sdyoung return EBUSY;
280a4392067Sdyoung sc = malloc(sizeof(struct srt_softc), M_DEVBUF, M_WAITOK | M_ZERO);
281ebc746c1Smouse sc->unit = unit;
282ebc746c1Smouse sc->nrt = 0;
283ebc746c1Smouse sc->rts = 0;
284ebc746c1Smouse sc->flags = 0;
285ebc746c1Smouse sc->kflags = 0;
286bc168f27Schristos if_initname(&sc->intf,cl->ifc_name,unit);
287ebc746c1Smouse sc->intf.if_softc = sc;
288ebc746c1Smouse sc->intf.if_mtu = 65535;
289ebc746c1Smouse sc->intf.if_flags = IFF_POINTOPOINT;
290ebc746c1Smouse sc->intf.if_type = IFT_OTHER;
291ebc746c1Smouse sc->intf.if_ioctl = &srt_if_ioctl;
292ebc746c1Smouse sc->intf.if_output = &srt_if_output;
293ebc746c1Smouse sc->intf.if_dlt = DLT_RAW;
294*076e3579Sriastradh if_attach(&sc->intf);
295ebc746c1Smouse if_alloc_sadl(&sc->intf);
29610fe49d7Spooka #ifdef BPFILTER_NOW_AVAILABLE
29758e86755Sjoerg bpf_attach(&sc->intf, 0, 0);
298ebc746c1Smouse #endif
299ebc746c1Smouse softcv[unit] = sc;
3001d8e08d4Schristos atomic_inc_uint(&srt_count);
301a4392067Sdyoung return 0;
302ebc746c1Smouse }
303ebc746c1Smouse
304a4392067Sdyoung static int
srt_clone_destroy(struct ifnet * ifp)305a4392067Sdyoung srt_clone_destroy(struct ifnet *ifp)
306ebc746c1Smouse {
307a4392067Sdyoung struct srt_softc *sc;
308ebc746c1Smouse
309a4392067Sdyoung sc = ifp->if_softc;
310a4392067Sdyoung if ((ifp->if_flags & IFF_UP) || (sc->kflags & SKF_CDEVOPEN))
311a4392067Sdyoung return EBUSY;
31210fe49d7Spooka #ifdef BPFILTER_NOW_AVAILABLE
31358e86755Sjoerg bpf_detach(ifp);
314ebc746c1Smouse #endif
315a4392067Sdyoung if_detach(ifp);
316a4392067Sdyoung if (sc->unit < 0 || sc->unit > SRT_MAXUNIT) {
317a4392067Sdyoung panic("srt_clone_destroy: impossible unit %d\n",sc->unit);
318ebc746c1Smouse }
319a4392067Sdyoung if (softcv[sc->unit] != sc) {
320a4392067Sdyoung panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
321ebc746c1Smouse sc->unit, (void *)softcv[sc->unit], (void *)sc);
322ebc746c1Smouse }
323ebc746c1Smouse softcv[sc->unit] = 0;
324ebc746c1Smouse free(sc, M_DEVBUF);
3251d8e08d4Schristos atomic_inc_uint(&srt_count);
326a4392067Sdyoung return 0;
327ebc746c1Smouse }
328ebc746c1Smouse
329ebc746c1Smouse struct if_clone srt_clone =
330ebc746c1Smouse IF_CLONE_INITIALIZER("srt", &srt_clone_create, &srt_clone_destroy);
331ebc746c1Smouse
332a4392067Sdyoung void
srtattach(int n)3331d8e08d4Schristos srtattach(int n)
3341d8e08d4Schristos {
3351d8e08d4Schristos
3361d8e08d4Schristos /*
3371d8e08d4Schristos * Nothing to do here, initialization is handled by the
3381d8e08d4Schristos * module initialization code in srtinit() below).
3391d8e08d4Schristos */
3401d8e08d4Schristos }
3411d8e08d4Schristos
3421d8e08d4Schristos static void
srtinit(void)3431d8e08d4Schristos srtinit(void)
344ebc746c1Smouse {
345ebc746c1Smouse int i;
346ebc746c1Smouse
347a4392067Sdyoung for (i = SRT_MAXUNIT; i >= 0; i--)
348a4392067Sdyoung softcv[i] = 0;
349ebc746c1Smouse global_flags = 0;
350ebc746c1Smouse if_clone_attach(&srt_clone);
3510a2e0c92Spgoyette #ifdef _MODULE
3520a2e0c92Spgoyette devsw_attach("srt", NULL, &srt_bmajor, &srt_cdevsw, &srt_cmajor);
3530a2e0c92Spgoyette #endif
354ebc746c1Smouse }
355ebc746c1Smouse
3561d8e08d4Schristos static int
srtdetach(void)3571d8e08d4Schristos srtdetach(void)
3581d8e08d4Schristos {
3591d8e08d4Schristos int error = 0;
3601d8e08d4Schristos int i;
3611d8e08d4Schristos
3620a2e0c92Spgoyette if_clone_detach(&srt_clone);
3630a2e0c92Spgoyette #ifdef _MODULE
3640a2e0c92Spgoyette devsw_detach(NULL, &srt_cdevsw);
3650a2e0c92Spgoyette if (error != 0) {
3660a2e0c92Spgoyette if_clone_attach(&srt_clone);
3670a2e0c92Spgoyette return error;
3680a2e0c92Spgoyette }
3690a2e0c92Spgoyette #endif
3700a2e0c92Spgoyette
3711d8e08d4Schristos for (i = SRT_MAXUNIT; i >= 0; i--)
3721d8e08d4Schristos if(softcv[i]) {
3731d8e08d4Schristos error = EBUSY;
3740a2e0c92Spgoyette #ifdef _MODULE
3750a2e0c92Spgoyette devsw_attach("srt", NULL, &srt_bmajor,
3760a2e0c92Spgoyette &srt_cdevsw, &srt_cmajor);
3770a2e0c92Spgoyette #endif
3780a2e0c92Spgoyette if_clone_attach(&srt_clone);
3791d8e08d4Schristos break;
3801d8e08d4Schristos }
3811d8e08d4Schristos
3821d8e08d4Schristos return error;
3831d8e08d4Schristos }
3841d8e08d4Schristos
385ebc746c1Smouse /* Special-device interface. */
386ebc746c1Smouse
387a4392067Sdyoung static int
srt_open(dev_t dev,int flag,int mode,struct lwp * l)388a4392067Sdyoung srt_open(dev_t dev, int flag, int mode, struct lwp *l)
389ebc746c1Smouse {
390ebc746c1Smouse int unit;
391a4392067Sdyoung struct srt_softc *sc;
392ebc746c1Smouse
393ebc746c1Smouse unit = minor(dev);
394a4392067Sdyoung if (unit < 0 || unit > SRT_MAXUNIT)
395a4392067Sdyoung return ENXIO;
396ebc746c1Smouse sc = softcv[unit];
39721ccb941Spgoyette if (sc == NULL) {
39821ccb941Spgoyette (void)srt_clone_create(&srt_clone, minor(dev));
39921ccb941Spgoyette sc = softcv[unit];
40021ccb941Spgoyette }
401a4392067Sdyoung if (! sc)
402a4392067Sdyoung return ENXIO;
403ebc746c1Smouse sc->kflags |= SKF_CDEVOPEN;
404a4392067Sdyoung return 0;
405ebc746c1Smouse }
406ebc746c1Smouse
407a4392067Sdyoung static int
srt_close(dev_t dev,int flag,int mode,struct lwp * l)408a4392067Sdyoung srt_close(dev_t dev, int flag, int mode, struct lwp *l)
409ebc746c1Smouse {
410ebc746c1Smouse int unit;
411a4392067Sdyoung struct srt_softc *sc;
412ebc746c1Smouse
413ebc746c1Smouse unit = minor(dev);
414a4392067Sdyoung if (unit < 0 || unit > SRT_MAXUNIT)
415a4392067Sdyoung return ENXIO;
416ebc746c1Smouse sc = softcv[unit];
417a4392067Sdyoung if (! sc)
418a4392067Sdyoung return ENXIO;
419ebc746c1Smouse sc->kflags &= ~SKF_CDEVOPEN;
420a4392067Sdyoung return 0;
421ebc746c1Smouse }
422ebc746c1Smouse
423a4392067Sdyoung static int
srt_ioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)424a4392067Sdyoung srt_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
425ebc746c1Smouse {
426a4392067Sdyoung unsigned int f, i, n, o;
427a4392067Sdyoung struct srt_softc *sc;
428a4392067Sdyoung struct srt_rt *dr;
429a4392067Sdyoung struct srt_rt *scr;
430a4392067Sdyoung struct ifnet *ifp;
431ebc746c1Smouse char nbuf[IFNAMSIZ];
432ebc746c1Smouse
433ebc746c1Smouse sc = softcv[minor(dev)];
434a4392067Sdyoung if (! sc)
435a4392067Sdyoung panic("srt_ioctl: softc disappeared");
436a4392067Sdyoung switch (cmd) {
437a4392067Sdyoung case SRT_GETNRT:
438a4392067Sdyoung if (! (flag & FREAD))
439a4392067Sdyoung return EBADF;
440ebc746c1Smouse *(unsigned int *)data = sc->nrt;
441a4392067Sdyoung return 0;
442ebc746c1Smouse case SRT_GETRT:
443a4392067Sdyoung if (! (flag & FREAD))
444a4392067Sdyoung return EBADF;
445a4392067Sdyoung dr = (struct srt_rt *) data;
446a4392067Sdyoung if (dr->inx >= sc->nrt)
447a4392067Sdyoung return EDOM;
448ebc746c1Smouse scr = sc->rts[dr->inx];
449ebc746c1Smouse dr->af = scr->af;
450ebc746c1Smouse dr->srcmatch = scr->srcmatch;
451ebc746c1Smouse dr->srcmask = scr->srcmask;
452351c2b72Smsaitoh strlcpy(&dr->u.dstifn[0], &scr->u.dstifp->if_xname[0],
453351c2b72Smsaitoh IFNAMSIZ);
454ebc746c1Smouse memcpy(&dr->dst, &scr->dst, scr->dst.sa.sa_len);
455a4392067Sdyoung return 0;
456ebc746c1Smouse case SRT_SETRT:
457a4392067Sdyoung if (! (flag & FWRITE))
458a4392067Sdyoung return EBADF;
459a4392067Sdyoung dr = (struct srt_rt *) data;
460a4392067Sdyoung if (dr->inx > sc->nrt)
461a4392067Sdyoung return EDOM;
462fe2925feSmaya strlcpy(&nbuf[0], &dr->u.dstifn[0], IFNAMSIZ);
463ebc746c1Smouse nbuf[IFNAMSIZ-1] = '\0';
464a4392067Sdyoung if (dr->dst.sa.sa_family != dr->af)
465a4392067Sdyoung return EIO;
466a4392067Sdyoung switch (dr->af) {
467ebc746c1Smouse #ifdef INET
468ebc746c1Smouse case AF_INET:
469a4392067Sdyoung if (dr->dst.sa.sa_len != sizeof(dr->dst.sin))
470a4392067Sdyoung return EIO;
471a4392067Sdyoung if (dr->srcmask > 32)
472a4392067Sdyoung return EIO;
473ebc746c1Smouse break;
474ebc746c1Smouse #endif
475ebc746c1Smouse #ifdef INET6
476ebc746c1Smouse case AF_INET6:
477a4392067Sdyoung if (dr->dst.sa.sa_len != sizeof(dr->dst.sin6))
478a4392067Sdyoung return EIO;
479a4392067Sdyoung if (dr->srcmask > 128)
480a4392067Sdyoung return EIO;
481ebc746c1Smouse break;
482ebc746c1Smouse #endif
483ebc746c1Smouse default:
484a4392067Sdyoung return EAFNOSUPPORT;
485ebc746c1Smouse }
486a4392067Sdyoung ifp = ifunit(&nbuf[0]);
487a4392067Sdyoung if (ifp == 0)
488a4392067Sdyoung return ENXIO; /* needs translation */
489a4392067Sdyoung if (dr->inx == sc->nrt) {
490a4392067Sdyoung struct srt_rt **tmp;
491a4392067Sdyoung tmp = malloc((sc->nrt+1)*sizeof(*tmp), M_DEVBUF,
492a4392067Sdyoung M_WAITOK);
493a4392067Sdyoung if (tmp == 0)
494a4392067Sdyoung return ENOBUFS;
495ebc746c1Smouse tmp[sc->nrt] = 0;
496a4392067Sdyoung if (sc->nrt > 0) {
497a4392067Sdyoung memcpy(tmp, sc->rts, sc->nrt*sizeof(*tmp));
498ebc746c1Smouse free(sc->rts, M_DEVBUF);
499ebc746c1Smouse }
500ebc746c1Smouse sc->rts = tmp;
501ebc746c1Smouse sc->nrt ++;
502ebc746c1Smouse }
503ebc746c1Smouse scr = sc->rts[dr->inx];
504a4392067Sdyoung if (scr == 0) {
505a4392067Sdyoung scr = malloc(sizeof(struct srt_rt), M_DEVBUF,M_WAITOK);
506ebc746c1Smouse if (scr == 0)
507a4392067Sdyoung return ENOBUFS;
508ebc746c1Smouse scr->inx = dr->inx;
509ebc746c1Smouse scr->af = AF_UNSPEC;
510ebc746c1Smouse sc->rts[dr->inx] = scr;
511ebc746c1Smouse }
512ebc746c1Smouse scr->af = dr->af;
513ebc746c1Smouse scr->srcmatch = dr->srcmatch;
514ebc746c1Smouse scr->srcmask = dr->srcmask;
515a4392067Sdyoung scr->u.dstifp = ifp;
516ebc746c1Smouse memcpy(&scr->dst,&dr->dst,dr->dst.sa.sa_len);
5174fbae14dSkre if (dr->af == AF_INET6)
5184fbae14dSkre in6_setzoneid(&scr->dst.sin6.sin6_addr, ifp->if_index);
519ebc746c1Smouse update_mtu(sc);
520a4392067Sdyoung return 0;
521ebc746c1Smouse case SRT_DELRT:
522a4392067Sdyoung if (! (flag & FWRITE))
523a4392067Sdyoung return EBADF;
524ebc746c1Smouse i = *(unsigned int *)data;
525a4392067Sdyoung if (i >= sc->nrt)
526a4392067Sdyoung return EDOM;
527ebc746c1Smouse scr = sc->rts[i];
528ebc746c1Smouse sc->rts[i] = 0;
529ebc746c1Smouse free(scr, M_DEVBUF);
530ebc746c1Smouse sc->nrt--;
531a4392067Sdyoung if (i < sc->nrt) {
532a4392067Sdyoung memcpy(sc->rts+i, sc->rts+i+1,
533a4392067Sdyoung (sc->nrt-i) * sizeof(*sc->rts));
534a4392067Sdyoung }
535a4392067Sdyoung if (sc->nrt == 0) {
536a4392067Sdyoung free(sc->rts, M_DEVBUF);
537ebc746c1Smouse sc->rts = 0;
538ebc746c1Smouse sc->intf.if_flags &= ~IFF_UP;
539ebc746c1Smouse }
540ebc746c1Smouse update_mtu(sc);
541a4392067Sdyoung return 0;
542ebc746c1Smouse case SRT_SFLAGS:
543a4392067Sdyoung if (! (flag & FWRITE))
544a4392067Sdyoung return EBADF;
545ebc746c1Smouse f = *(unsigned int *)data & SSF_UCHG;
546ebc746c1Smouse global_flags = (global_flags & ~SSF_UCHG) | (f & SSF_GLOBAL);
547ebc746c1Smouse sc->flags = (sc->flags & ~SSF_UCHG) | (f & ~SSF_GLOBAL);
548a4392067Sdyoung return 0;
549ebc746c1Smouse case SRT_GFLAGS:
550a4392067Sdyoung if (! (flag & FREAD))
551a4392067Sdyoung return EBADF;
552ebc746c1Smouse *(unsigned int *)data = sc->flags | global_flags;
553a4392067Sdyoung return 0;
554ebc746c1Smouse case SRT_SGFLAGS:
555a4392067Sdyoung if ((flag & (FWRITE | FREAD)) != (FWRITE | FREAD))
556a4392067Sdyoung return EBADF;
557ebc746c1Smouse o = sc->flags | global_flags;
558ebc746c1Smouse n = *(unsigned int *)data & SSF_UCHG;
559ebc746c1Smouse global_flags = (global_flags & ~SSF_UCHG) | (n & SSF_GLOBAL);
560ebc746c1Smouse sc->flags = (sc->flags & ~SSF_UCHG) | (n & ~SSF_GLOBAL);
561ebc746c1Smouse *(unsigned int *)data = o;
562a4392067Sdyoung return 0;
563ebc746c1Smouse case SRT_DEBUG:
564a4392067Sdyoung return 0;
565ebc746c1Smouse break;
566ebc746c1Smouse }
567a4392067Sdyoung return ENOTTY;
568ebc746c1Smouse }
569ebc746c1Smouse
5701d8e08d4Schristos /*
5711d8e08d4Schristos * Module infrastructure
5721d8e08d4Schristos */
5731d8e08d4Schristos #include "if_module.h"
5741d8e08d4Schristos
575befc8be5Spgoyette IF_MODULE(MODULE_CLASS_DRIVER, srt, NULL)
576