if_srt.c revision 1.3.2.2 1 1.3.2.2 ad /* $NetBSD: if_srt.c,v 1.3.2.2 2007/01/12 01:04:12 ad Exp $ */
2 1.3.2.2 ad /* This file is in the public domain. */
3 1.3.2.2 ad
4 1.3.2.2 ad #include "opt_inet.h"
5 1.3.2.2 ad
6 1.3.2.2 ad #if !defined(INET) && !defined(INET6)
7 1.3.2.2 ad #error "srt without INET/INET6?"
8 1.3.2.2 ad #endif
9 1.3.2.2 ad
10 1.3.2.2 ad #ifndef SRT_MAXUNIT
11 1.3.2.2 ad #define SRT_MAXUNIT 255
12 1.3.2.2 ad #endif
13 1.3.2.2 ad
14 1.3.2.2 ad /* include-file bug workarounds */
15 1.3.2.2 ad #include <sys/types.h> /* sys/conf.h */
16 1.3.2.2 ad #include <sys/resource.h> /* sys/resourcevar.h (uvm/uvm_param.h, sys/mbuf.h) */
17 1.3.2.2 ad #include <netinet/in.h> /* netinet/ip.h */
18 1.3.2.2 ad #include <sys/param.h> /* sys/mbuf.h */
19 1.3.2.2 ad #include <netinet/in_systm.h> /* netinet/ip.h */
20 1.3.2.2 ad
21 1.3.2.2 ad #include <sys/conf.h>
22 1.3.2.2 ad #include <sys/mbuf.h>
23 1.3.2.2 ad #include <sys/errno.h>
24 1.3.2.2 ad #include <sys/fcntl.h>
25 1.3.2.2 ad #include <sys/param.h>
26 1.3.2.2 ad #include <sys/ioctl.h>
27 1.3.2.2 ad #include <netinet/ip.h>
28 1.3.2.2 ad #include <netinet/ip6.h>
29 1.3.2.2 ad #include <net/if_types.h>
30 1.3.2.2 ad #include <machine/stdarg.h>
31 1.3.2.2 ad
32 1.3.2.2 ad #include "if_srt.h"
33 1.3.2.2 ad #include "bpfilter.h"
34 1.3.2.2 ad
35 1.3.2.2 ad /* until we know what to pass to bpfattach.... */
36 1.3.2.2 ad #undef NBPFILTER
37 1.3.2.2 ad #define NBPFILTER 0
38 1.3.2.2 ad
39 1.3.2.2 ad typedef struct srt_rt RT;
40 1.3.2.2 ad typedef struct softc SOFTC;
41 1.3.2.2 ad
42 1.3.2.2 ad struct softc {
43 1.3.2.2 ad struct ifnet intf; /* XXX interface botch */
44 1.3.2.2 ad int unit;
45 1.3.2.2 ad int nrt;
46 1.3.2.2 ad RT **rts;
47 1.3.2.2 ad unsigned int flags; /* SSF_* values from if_srt.h */
48 1.3.2.2 ad #define SSF_UCHG (SSF_MTULOCK) /* userland-changeable bits */
49 1.3.2.2 ad unsigned int kflags; /* bits private to this file */
50 1.3.2.2 ad #define SKF_CDEVOPEN 0x00000001
51 1.3.2.2 ad } ;
52 1.3.2.2 ad
53 1.3.2.2 ad static SOFTC *softcv[SRT_MAXUNIT+1];
54 1.3.2.2 ad static unsigned int global_flags;
55 1.3.2.2 ad
56 1.3.2.2 ad /* Internal routines. */
57 1.3.2.2 ad
58 1.3.2.2 ad static unsigned int ipv4_masks[33]
59 1.3.2.2 ad = { 0x00000000, /* /0 */
60 1.3.2.2 ad 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, /* /1 - /4 */
61 1.3.2.2 ad 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000, /* /5 - /8 */
62 1.3.2.2 ad 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, /* /9 - /12 */
63 1.3.2.2 ad 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, /* /13 - /16 */
64 1.3.2.2 ad 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000, /* /17 - /20 */
65 1.3.2.2 ad 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, /* /21 - /24 */
66 1.3.2.2 ad 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, /* /25 - /28 */
67 1.3.2.2 ad 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff /* /29 - /32 */ };
68 1.3.2.2 ad
69 1.3.2.2 ad static void update_mtu(SOFTC *sc)
70 1.3.2.2 ad {
71 1.3.2.2 ad int mtu;
72 1.3.2.2 ad int i;
73 1.3.2.2 ad RT *r;
74 1.3.2.2 ad
75 1.3.2.2 ad if (sc->flags & SSF_MTULOCK) return;
76 1.3.2.2 ad mtu = 65535;
77 1.3.2.2 ad for (i=sc->nrt-1;i>=0;i--)
78 1.3.2.2 ad { r = sc->rts[i];
79 1.3.2.2 ad if (r->u.dstifp->if_mtu < mtu) mtu = r->u.dstifp->if_mtu;
80 1.3.2.2 ad }
81 1.3.2.2 ad sc->intf.if_mtu = mtu;
82 1.3.2.2 ad }
83 1.3.2.2 ad
84 1.3.2.2 ad static RT *find_rt(SOFTC *sc, int af, ...)
85 1.3.2.2 ad {
86 1.3.2.2 ad int i;
87 1.3.2.2 ad RT *r;
88 1.3.2.2 ad struct in_addr ia;
89 1.3.2.2 ad struct in6_addr ia6;
90 1.3.2.2 ad va_list ap;
91 1.3.2.2 ad
92 1.3.2.2 ad ia.s_addr = 0; ia6.s6_addr[0] = 0; /* shut up incorrect -Wuninitialized */
93 1.3.2.2 ad va_start(ap,af);
94 1.3.2.2 ad switch (af)
95 1.3.2.2 ad { case AF_INET:
96 1.3.2.2 ad ia = va_arg(ap,struct in_addr);
97 1.3.2.2 ad break;
98 1.3.2.2 ad case AF_INET6:
99 1.3.2.2 ad ia6 = va_arg(ap,struct in6_addr);
100 1.3.2.2 ad break;
101 1.3.2.2 ad default:
102 1.3.2.2 ad panic("if_srt find_rt: impossible address family");
103 1.3.2.2 ad break;
104 1.3.2.2 ad }
105 1.3.2.2 ad va_end(ap);
106 1.3.2.2 ad for (i=0;i<sc->nrt;i++)
107 1.3.2.2 ad { r = sc->rts[i];
108 1.3.2.2 ad if (r->af != af) continue;
109 1.3.2.2 ad switch (af)
110 1.3.2.2 ad { case AF_INET:
111 1.3.2.2 ad if ((ia.s_addr & ipv4_masks[r->srcmask]) == r->srcmatch.v4.s_addr) return(r);
112 1.3.2.2 ad break;
113 1.3.2.2 ad case AF_INET6:
114 1.3.2.2 ad if ((r->srcmask >= 8) && bcmp(&ia6,&r->srcmatch.v6,r->srcmask/8)) continue;
115 1.3.2.2 ad if ( (r->srcmask % 8) &&
116 1.3.2.2 ad ( ( ia6.s6_addr[r->srcmask/8] ^
117 1.3.2.2 ad r->srcmatch.v6.s6_addr[r->srcmask/8] ) &
118 1.3.2.2 ad 0xff & (0xff00 >> (r->srcmask%8)) ) ) continue;
119 1.3.2.2 ad return(r);
120 1.3.2.2 ad break;
121 1.3.2.2 ad default:
122 1.3.2.2 ad panic("if_srt find_rt: impossible address family 2");
123 1.3.2.2 ad break;
124 1.3.2.2 ad }
125 1.3.2.2 ad }
126 1.3.2.2 ad return(0);
127 1.3.2.2 ad }
128 1.3.2.2 ad
129 1.3.2.2 ad /* Network device interface. */
130 1.3.2.2 ad
131 1.3.2.2 ad static int srt_if_ioctl(struct ifnet *intf, u_long cmd, caddr_t data)
132 1.3.2.2 ad {
133 1.3.2.2 ad struct ifaddr *ifa;
134 1.3.2.2 ad struct ifreq *ifr;
135 1.3.2.2 ad int s;
136 1.3.2.2 ad int err;
137 1.3.2.2 ad
138 1.3.2.2 ad err = 0;
139 1.3.2.2 ad s = splnet();
140 1.3.2.2 ad switch (cmd)
141 1.3.2.2 ad { case SIOCSIFADDR:
142 1.3.2.2 ad case SIOCSIFDSTADDR:
143 1.3.2.2 ad ifa = (void *) data;
144 1.3.2.2 ad switch (ifa->ifa_addr->sa_family)
145 1.3.2.2 ad {
146 1.3.2.2 ad #ifdef INET
147 1.3.2.2 ad case AF_INET:
148 1.3.2.2 ad #endif
149 1.3.2.2 ad #ifdef INET6
150 1.3.2.2 ad case AF_INET6:
151 1.3.2.2 ad #endif
152 1.3.2.2 ad break;
153 1.3.2.2 ad default:
154 1.3.2.2 ad err = EAFNOSUPPORT;
155 1.3.2.2 ad break;
156 1.3.2.2 ad }
157 1.3.2.2 ad /* XXX do we need to do more here for either of these? */
158 1.3.2.2 ad break;
159 1.3.2.2 ad case SIOCSIFMTU:
160 1.3.2.2 ad ifr = (void *) data;
161 1.3.2.2 ad ((SOFTC *)intf->if_softc)->intf.if_mtu = ifr->ifr_mtu;
162 1.3.2.2 ad break;
163 1.3.2.2 ad case SIOCGIFMTU:
164 1.3.2.2 ad ifr = (void *) data;
165 1.3.2.2 ad ifr->ifr_mtu = intf->if_mtu;
166 1.3.2.2 ad break;
167 1.3.2.2 ad default:
168 1.3.2.2 ad err = EINVAL;
169 1.3.2.2 ad break;
170 1.3.2.2 ad }
171 1.3.2.2 ad splx(s);
172 1.3.2.2 ad return(err);
173 1.3.2.2 ad }
174 1.3.2.2 ad
175 1.3.2.2 ad static int srt_if_output(
176 1.3.2.2 ad struct ifnet *intf,
177 1.3.2.2 ad struct mbuf *m,
178 1.3.2.2 ad struct sockaddr *to,
179 1.3.2.2 ad struct rtentry *rtp )
180 1.3.2.2 ad {
181 1.3.2.2 ad SOFTC *sc;
182 1.3.2.2 ad RT *r;
183 1.3.2.2 ad
184 1.3.2.2 ad sc = intf->if_softc;
185 1.3.2.2 ad if (! (intf->if_flags & IFF_UP))
186 1.3.2.2 ad { m_freem(m);
187 1.3.2.2 ad return(ENETDOWN);
188 1.3.2.2 ad }
189 1.3.2.2 ad switch (to->sa_family)
190 1.3.2.2 ad {
191 1.3.2.2 ad #ifdef INET
192 1.3.2.2 ad case AF_INET:
193 1.3.2.2 ad #endif
194 1.3.2.2 ad { struct ip *ip;
195 1.3.2.2 ad ip = mtod(m,struct ip *);
196 1.3.2.2 ad r = find_rt(sc,AF_INET,ip->ip_src);
197 1.3.2.2 ad }
198 1.3.2.2 ad break;
199 1.3.2.2 ad #ifdef INET6
200 1.3.2.2 ad case AF_INET6:
201 1.3.2.2 ad #endif
202 1.3.2.2 ad { struct ip6_hdr *ip;
203 1.3.2.2 ad ip = mtod(m,struct ip6_hdr *);
204 1.3.2.2 ad r = find_rt(sc,AF_INET6,ip->ip6_src);
205 1.3.2.2 ad }
206 1.3.2.2 ad break;
207 1.3.2.2 ad default:
208 1.3.2.2 ad IF_DROP(&intf->if_snd);
209 1.3.2.2 ad m_freem(m);
210 1.3.2.2 ad return(EAFNOSUPPORT);
211 1.3.2.2 ad break;
212 1.3.2.2 ad }
213 1.3.2.2 ad /* XXX Do we need to bpf_tap? Or do higher layers now handle that? */
214 1.3.2.2 ad /* if_gif.c seems to imply the latter. */
215 1.3.2.2 ad intf->if_opackets ++;
216 1.3.2.2 ad if (! r)
217 1.3.2.2 ad { intf->if_oerrors ++;
218 1.3.2.2 ad m_freem(m);
219 1.3.2.2 ad return(0);
220 1.3.2.2 ad }
221 1.3.2.2 ad if (! (m->m_flags & M_PKTHDR))
222 1.3.2.2 ad { printf("srt_if_output no PKTHDR\n");
223 1.3.2.2 ad m_freem(m);
224 1.3.2.2 ad return(0);
225 1.3.2.2 ad }
226 1.3.2.2 ad intf->if_obytes += m->m_pkthdr.len;
227 1.3.2.2 ad if (! (r->u.dstifp->if_flags & IFF_UP))
228 1.3.2.2 ad { m_freem(m);
229 1.3.2.2 ad return(0); /* XXX ENETDOWN? */
230 1.3.2.2 ad }
231 1.3.2.2 ad /* XXX is 0 the right last arg here? */
232 1.3.2.2 ad return((*r->u.dstifp->if_output)(r->u.dstifp,m,&r->dst.sa,0));
233 1.3.2.2 ad }
234 1.3.2.2 ad
235 1.3.2.2 ad static int srt_clone_create(struct if_clone *cl, int unit)
236 1.3.2.2 ad {
237 1.3.2.2 ad SOFTC *sc;
238 1.3.2.2 ad
239 1.3.2.2 ad if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
240 1.3.2.2 ad if (softcv[unit]) return(EBUSY);
241 1.3.2.2 ad sc = malloc(sizeof(SOFTC),M_DEVBUF,M_WAIT);
242 1.3.2.2 ad bzero(&sc->intf,sizeof(sc->intf)); /* XXX */
243 1.3.2.2 ad sc->unit = unit;
244 1.3.2.2 ad sc->nrt = 0;
245 1.3.2.2 ad sc->rts = 0;
246 1.3.2.2 ad sc->flags = 0;
247 1.3.2.2 ad sc->kflags = 0;
248 1.3.2.2 ad snprintf(&sc->intf.if_xname[0],sizeof(sc->intf.if_xname),"%s%d",cl->ifc_name,unit);
249 1.3.2.2 ad sc->intf.if_softc = sc;
250 1.3.2.2 ad sc->intf.if_mtu = 65535;
251 1.3.2.2 ad sc->intf.if_flags = IFF_POINTOPOINT;
252 1.3.2.2 ad sc->intf.if_type = IFT_OTHER;
253 1.3.2.2 ad sc->intf.if_ioctl = &srt_if_ioctl;
254 1.3.2.2 ad sc->intf.if_output = &srt_if_output;
255 1.3.2.2 ad sc->intf.if_dlt = DLT_RAW;
256 1.3.2.2 ad if_attach(&sc->intf);
257 1.3.2.2 ad if_alloc_sadl(&sc->intf);
258 1.3.2.2 ad #if NBPFILTER > 0 /* see comment near top */
259 1.3.2.2 ad bpfattach(&sc->intf,0/*???*/,0/*???*/);
260 1.3.2.2 ad #endif
261 1.3.2.2 ad softcv[unit] = sc;
262 1.3.2.2 ad return(0);
263 1.3.2.2 ad }
264 1.3.2.2 ad
265 1.3.2.2 ad static int srt_clone_destroy(struct ifnet *intf)
266 1.3.2.2 ad {
267 1.3.2.2 ad SOFTC *sc;
268 1.3.2.2 ad
269 1.3.2.2 ad sc = intf->if_softc;
270 1.3.2.2 ad if ((intf->if_flags & IFF_UP) || (sc->kflags & SKF_CDEVOPEN)) return(EBUSY);
271 1.3.2.2 ad #if NBPFILTER > 0
272 1.3.2.2 ad bpfdetach(intf);
273 1.3.2.2 ad #endif
274 1.3.2.2 ad if_detach(intf);
275 1.3.2.2 ad if ((sc->unit < 0) || (sc->unit > SRT_MAXUNIT))
276 1.3.2.2 ad { panic("srt_clone_destroy: impossible unit %d\n",sc->unit);
277 1.3.2.2 ad }
278 1.3.2.2 ad if (softcv[sc->unit] != sc)
279 1.3.2.2 ad { panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
280 1.3.2.2 ad sc->unit,(void *)softcv[sc->unit],(void *)sc);
281 1.3.2.2 ad }
282 1.3.2.2 ad softcv[sc->unit] = 0;
283 1.3.2.2 ad free(sc,M_DEVBUF);
284 1.3.2.2 ad return(0);
285 1.3.2.2 ad }
286 1.3.2.2 ad
287 1.3.2.2 ad struct if_clone srt_clone =
288 1.3.2.2 ad IF_CLONE_INITIALIZER("srt",&srt_clone_create,&srt_clone_destroy);
289 1.3.2.2 ad
290 1.3.2.2 ad extern void srtattach(void);
291 1.3.2.2 ad void srtattach(void)
292 1.3.2.2 ad {
293 1.3.2.2 ad int i;
294 1.3.2.2 ad
295 1.3.2.2 ad for (i=SRT_MAXUNIT;i>=0;i--) softcv[i] = 0;
296 1.3.2.2 ad global_flags = 0;
297 1.3.2.2 ad if_clone_attach(&srt_clone);
298 1.3.2.2 ad }
299 1.3.2.2 ad
300 1.3.2.2 ad /* Special-device interface. */
301 1.3.2.2 ad
302 1.3.2.2 ad static int srt_open(dev_t dev, int flag, int mode, struct lwp *l)
303 1.3.2.2 ad {
304 1.3.2.2 ad int unit;
305 1.3.2.2 ad SOFTC *sc;
306 1.3.2.2 ad
307 1.3.2.2 ad unit = minor(dev);
308 1.3.2.2 ad if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
309 1.3.2.2 ad sc = softcv[unit];
310 1.3.2.2 ad if (! sc) return(ENXIO);
311 1.3.2.2 ad sc->kflags |= SKF_CDEVOPEN;
312 1.3.2.2 ad return(0);
313 1.3.2.2 ad }
314 1.3.2.2 ad
315 1.3.2.2 ad static int srt_close(dev_t dev, int flag, int mode, struct lwp *l)
316 1.3.2.2 ad {
317 1.3.2.2 ad int unit;
318 1.3.2.2 ad SOFTC *sc;
319 1.3.2.2 ad
320 1.3.2.2 ad unit = minor(dev);
321 1.3.2.2 ad if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
322 1.3.2.2 ad sc = softcv[unit];
323 1.3.2.2 ad if (! sc) return(ENXIO);
324 1.3.2.2 ad sc->kflags &= ~SKF_CDEVOPEN;
325 1.3.2.2 ad return(0);
326 1.3.2.2 ad }
327 1.3.2.2 ad
328 1.3.2.2 ad static int srt_ioctl(
329 1.3.2.2 ad dev_t dev,
330 1.3.2.2 ad u_long cmd,
331 1.3.2.2 ad caddr_t data,
332 1.3.2.2 ad int flag,
333 1.3.2.2 ad struct lwp *l )
334 1.3.2.2 ad {
335 1.3.2.2 ad SOFTC *sc;
336 1.3.2.2 ad RT *dr;
337 1.3.2.2 ad RT *scr;
338 1.3.2.2 ad struct ifnet *intf;
339 1.3.2.2 ad char nbuf[IFNAMSIZ];
340 1.3.2.2 ad
341 1.3.2.2 ad sc = softcv[minor(dev)];
342 1.3.2.2 ad if (! sc) panic("srt_ioctl: softc disappeared");
343 1.3.2.2 ad switch (cmd)
344 1.3.2.2 ad { case SRT_GETNRT:
345 1.3.2.2 ad if (! (flag & FREAD)) return(EBADF);
346 1.3.2.2 ad *(unsigned int *)data = sc->nrt;
347 1.3.2.2 ad return(0);
348 1.3.2.2 ad break;
349 1.3.2.2 ad case SRT_GETRT:
350 1.3.2.2 ad if (! (flag & FREAD)) return(EBADF);
351 1.3.2.2 ad dr = (RT *) data;
352 1.3.2.2 ad if (dr->inx >= sc->nrt) return(EDOM);
353 1.3.2.2 ad scr = sc->rts[dr->inx];
354 1.3.2.2 ad dr->af = scr->af;
355 1.3.2.2 ad dr->srcmatch = scr->srcmatch;
356 1.3.2.2 ad dr->srcmask = scr->srcmask;
357 1.3.2.2 ad strncpy(&dr->u.dstifn[0],&scr->u.dstifp->if_xname[0],IFNAMSIZ);
358 1.3.2.2 ad memcpy(&dr->dst,&scr->dst,scr->dst.sa.sa_len);
359 1.3.2.2 ad return(0);
360 1.3.2.2 ad break;
361 1.3.2.2 ad case SRT_SETRT:
362 1.3.2.2 ad if (! (flag & FWRITE)) return(EBADF);
363 1.3.2.2 ad dr = (RT *) data;
364 1.3.2.2 ad if (dr->inx > sc->nrt) return(EDOM);
365 1.3.2.2 ad strncpy(&nbuf[0],&dr->u.dstifn[0],IFNAMSIZ);
366 1.3.2.2 ad nbuf[IFNAMSIZ-1] = '\0';
367 1.3.2.2 ad if (dr->dst.sa.sa_family != dr->af) return(EIO);
368 1.3.2.2 ad switch (dr->af)
369 1.3.2.2 ad {
370 1.3.2.2 ad #ifdef INET
371 1.3.2.2 ad case AF_INET:
372 1.3.2.2 ad if (dr->dst.sa.sa_len != sizeof(dr->dst.sin)) return(EIO);
373 1.3.2.2 ad if (dr->srcmask > 32) return(EIO);
374 1.3.2.2 ad break;
375 1.3.2.2 ad #endif
376 1.3.2.2 ad #ifdef INET6
377 1.3.2.2 ad case AF_INET6:
378 1.3.2.2 ad if (dr->dst.sa.sa_len != sizeof(dr->dst.sin6)) return(EIO);
379 1.3.2.2 ad if (dr->srcmask > 128) return(EIO);
380 1.3.2.2 ad break;
381 1.3.2.2 ad #endif
382 1.3.2.2 ad break;
383 1.3.2.2 ad default:
384 1.3.2.2 ad return(EAFNOSUPPORT);
385 1.3.2.2 ad break;
386 1.3.2.2 ad }
387 1.3.2.2 ad intf = ifunit(&nbuf[0]);
388 1.3.2.2 ad if (intf == 0) return(ENXIO); /* needs translation */
389 1.3.2.2 ad if (dr->inx == sc->nrt)
390 1.3.2.2 ad { RT **tmp;
391 1.3.2.2 ad tmp = malloc((sc->nrt+1)*sizeof(*tmp),M_DEVBUF,M_WAITOK);
392 1.3.2.2 ad if (tmp == 0) return(ENOBUFS);
393 1.3.2.2 ad tmp[sc->nrt] = 0;
394 1.3.2.2 ad if (sc->nrt > 0)
395 1.3.2.2 ad { memcpy(tmp,sc->rts,sc->nrt*sizeof(*tmp));
396 1.3.2.2 ad free(sc->rts,M_DEVBUF);
397 1.3.2.2 ad }
398 1.3.2.2 ad sc->rts = tmp;
399 1.3.2.2 ad sc->nrt ++;
400 1.3.2.2 ad }
401 1.3.2.2 ad scr = sc->rts[dr->inx];
402 1.3.2.2 ad if (scr == 0)
403 1.3.2.2 ad { scr = malloc(sizeof(RT),M_DEVBUF,M_WAITOK);
404 1.3.2.2 ad if (scr == 0) return(ENOBUFS);
405 1.3.2.2 ad scr->inx = dr->inx;
406 1.3.2.2 ad scr->af = AF_UNSPEC;
407 1.3.2.2 ad sc->rts[dr->inx] = scr;
408 1.3.2.2 ad }
409 1.3.2.2 ad scr->af = dr->af;
410 1.3.2.2 ad scr->srcmatch = dr->srcmatch;
411 1.3.2.2 ad scr->srcmask = dr->srcmask;
412 1.3.2.2 ad scr->u.dstifp = intf;
413 1.3.2.2 ad memcpy(&scr->dst,&dr->dst,dr->dst.sa.sa_len);
414 1.3.2.2 ad update_mtu(sc);
415 1.3.2.2 ad return(0);
416 1.3.2.2 ad break;
417 1.3.2.2 ad case SRT_DELRT:
418 1.3.2.2 ad { unsigned int i;
419 1.3.2.2 ad if (! (flag & FWRITE)) return(EBADF);
420 1.3.2.2 ad i = *(unsigned int *)data;
421 1.3.2.2 ad if (i >= sc->nrt) return(EDOM);
422 1.3.2.2 ad scr = sc->rts[i];
423 1.3.2.2 ad sc->rts[i] = 0;
424 1.3.2.2 ad free(scr,M_DEVBUF);
425 1.3.2.2 ad sc->nrt --;
426 1.3.2.2 ad if (i < sc->nrt) memcpy(sc->rts+i,sc->rts+i+1,(sc->nrt-i)*sizeof(*sc->rts));
427 1.3.2.2 ad if (sc->nrt == 0)
428 1.3.2.2 ad { free(sc->rts,M_DEVBUF);
429 1.3.2.2 ad sc->rts = 0;
430 1.3.2.2 ad sc->intf.if_flags &= ~IFF_UP;
431 1.3.2.2 ad }
432 1.3.2.2 ad }
433 1.3.2.2 ad update_mtu(sc);
434 1.3.2.2 ad return(0);
435 1.3.2.2 ad break;
436 1.3.2.2 ad case SRT_SFLAGS:
437 1.3.2.2 ad { unsigned int f;
438 1.3.2.2 ad if (! (flag & FWRITE)) return(EBADF);
439 1.3.2.2 ad f = *(unsigned int *)data & SSF_UCHG;
440 1.3.2.2 ad global_flags = (global_flags & ~SSF_UCHG) | (f & SSF_GLOBAL);
441 1.3.2.2 ad sc->flags = (sc->flags & ~SSF_UCHG) | (f & ~SSF_GLOBAL);
442 1.3.2.2 ad }
443 1.3.2.2 ad return(0);
444 1.3.2.2 ad break;
445 1.3.2.2 ad case SRT_GFLAGS:
446 1.3.2.2 ad if (! (flag & FREAD)) return(EBADF);
447 1.3.2.2 ad *(unsigned int *)data = sc->flags | global_flags;
448 1.3.2.2 ad return(0);
449 1.3.2.2 ad break;
450 1.3.2.2 ad case SRT_SGFLAGS:
451 1.3.2.2 ad { unsigned int o;
452 1.3.2.2 ad unsigned int n;
453 1.3.2.2 ad if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD)) return(EBADF);
454 1.3.2.2 ad o = sc->flags | global_flags;
455 1.3.2.2 ad n = *(unsigned int *)data & SSF_UCHG;
456 1.3.2.2 ad global_flags = (global_flags & ~SSF_UCHG) | (n & SSF_GLOBAL);
457 1.3.2.2 ad sc->flags = (sc->flags & ~SSF_UCHG) | (n & ~SSF_GLOBAL);
458 1.3.2.2 ad *(unsigned int *)data = o;
459 1.3.2.2 ad }
460 1.3.2.2 ad return(0);
461 1.3.2.2 ad break;
462 1.3.2.2 ad case SRT_DEBUG:
463 1.3.2.2 ad return(0);
464 1.3.2.2 ad break;
465 1.3.2.2 ad }
466 1.3.2.2 ad return(ENOTTY);
467 1.3.2.2 ad }
468 1.3.2.2 ad
469 1.3.2.2 ad const struct cdevsw srt_cdevsw
470 1.3.2.2 ad = { &srt_open,
471 1.3.2.2 ad &srt_close,
472 1.3.2.2 ad nullread,
473 1.3.2.2 ad nullwrite,
474 1.3.2.2 ad &srt_ioctl,
475 1.3.2.2 ad nullstop,
476 1.3.2.2 ad notty,
477 1.3.2.2 ad nullpoll,
478 1.3.2.2 ad nommap,
479 1.3.2.2 ad nullkqfilter,
480 1.3.2.2 ad D_OTHER };
481