altq_conf.c revision 1.13 1 /* $NetBSD: altq_conf.c,v 1.13 2006/05/14 21:24:49 elad Exp $ */
2 /* $KAME: altq_conf.c,v 1.13 2002/01/29 10:16:01 kjc Exp $ */
3
4 /*
5 * Copyright (C) 1997-2000
6 * Sony Computer Science Laboratories Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: altq_conf.c,v 1.13 2006/05/14 21:24:49 elad Exp $");
32
33 #if defined(__FreeBSD__) || defined(__NetBSD__)
34 #include "opt_altq.h"
35 #if (__FreeBSD__ != 2)
36 #include "opt_inet.h"
37 #ifdef __FreeBSD__
38 #include "opt_inet6.h"
39 #endif
40 #endif
41 #endif /* __FreeBSD__ || __NetBSD__ */
42
43 /*
44 * altq device interface.
45 */
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/errno.h>
52 #if defined(__FreeBSD__) && (__FreeBSD_version < 400000) && defined(DEVFS)
53 #include <sys/devfsext.h>
54 #endif /*DEVFS*/
55 #include <net/if.h>
56
57 #include <altq/altq.h>
58 #include <altq/altqconf.h>
59 #include <altq/altq_conf.h>
60
61 #ifdef ALTQ_CBQ
62 altqdev_decl(cbq);
63 #endif
64 #ifdef ALTQ_WFQ
65 altqdev_decl(wfq);
66 #endif
67 #ifdef ALTQ_AFMAP
68 altqdev_decl(afm);
69 #endif
70 #ifdef ALTQ_FIFOQ
71 altqdev_decl(fifoq);
72 #endif
73 #ifdef ALTQ_RED
74 altqdev_decl(red);
75 #endif
76 #ifdef ALTQ_RIO
77 altqdev_decl(rio);
78 #endif
79 #ifdef ALTQ_LOCALQ
80 altqdev_decl(localq);
81 #endif
82 #ifdef ALTQ_HFSC
83 altqdev_decl(hfsc);
84 #endif
85 #ifdef ALTQ_CDNR
86 altqdev_decl(cdnr);
87 #endif
88 #ifdef ALTQ_BLUE
89 altqdev_decl(blue);
90 #endif
91 #ifdef ALTQ_PRIQ
92 altqdev_decl(priq);
93 #endif
94
95 /*
96 * altq minor device (discipline) table
97 */
98 static struct altqsw altqsw[] = { /* minor */
99 {"noq", noopen, noclose, noioctl}, /* 0 (reserved) */
100 #ifdef ALTQ_CBQ
101 {"cbq", cbqopen, cbqclose, cbqioctl}, /* 1 */
102 #else
103 {"noq", noopen, noclose, noioctl}, /* 1 */
104 #endif
105 #ifdef ALTQ_WFQ
106 {"wfq", wfqopen, wfqclose, wfqioctl}, /* 2 */
107 #else
108 {"noq", noopen, noclose, noioctl}, /* 2 */
109 #endif
110 #ifdef ALTQ_AFMAP
111 {"afm", afmopen, afmclose, afmioctl}, /* 3 */
112 #else
113 {"noq", noopen, noclose, noioctl}, /* 3 */
114 #endif
115 #ifdef ALTQ_FIFOQ
116 {"fifoq", fifoqopen, fifoqclose, fifoqioctl}, /* 4 */
117 #else
118 {"noq", noopen, noclose, noioctl}, /* 4 */
119 #endif
120 #ifdef ALTQ_RED
121 {"red", redopen, redclose, redioctl}, /* 5 */
122 #else
123 {"noq", noopen, noclose, noioctl}, /* 5 */
124 #endif
125 #ifdef ALTQ_RIO
126 {"rio", rioopen, rioclose, rioioctl}, /* 6 */
127 #else
128 {"noq", noopen, noclose, noioctl}, /* 6 */
129 #endif
130 #ifdef ALTQ_LOCALQ
131 {"localq",localqopen, localqclose, localqioctl}, /* 7 (local use) */
132 #else
133 {"noq", noopen, noclose, noioctl}, /* 7 (local use) */
134 #endif
135 #ifdef ALTQ_HFSC
136 {"hfsc",hfscopen, hfscclose, hfscioctl}, /* 8 */
137 #else
138 {"noq", noopen, noclose, noioctl}, /* 8 */
139 #endif
140 #ifdef ALTQ_CDNR
141 {"cdnr",cdnropen, cdnrclose, cdnrioctl}, /* 9 */
142 #else
143 {"noq", noopen, noclose, noioctl}, /* 9 */
144 #endif
145 #ifdef ALTQ_BLUE
146 {"blue",blueopen, blueclose, blueioctl}, /* 10 */
147 #else
148 {"noq", noopen, noclose, noioctl}, /* 10 */
149 #endif
150 #ifdef ALTQ_PRIQ
151 {"priq",priqopen, priqclose, priqioctl}, /* 11 */
152 #else
153 {"noq", noopen, noclose, noioctl}, /* 11 */
154 #endif
155 };
156
157 /*
158 * altq major device support
159 */
160 int naltqsw = sizeof (altqsw) / sizeof (altqsw[0]);
161
162 #if defined(__NetBSD__)
163 dev_type_open(altqopen);
164 dev_type_close(altqclose);
165 dev_type_ioctl(altqioctl);
166 #endif
167 #if defined(__OpenBSD__)
168 static d_open_t altqopen;
169 static d_close_t altqclose;
170 static d_ioctl_t altqioctl;
171 #endif
172 #ifdef __FreeBSD__
173 static void altq_drvinit __P((void *));
174 #else
175 void altqattach __P((int));
176 #endif
177
178 #if defined(__FreeBSD__)
179 #define CDEV_MAJOR 96 /* FreeBSD official number */
180 #elif defined(__OpenBSD__)
181 #if defined(__i386__)
182 #define CDEV_MAJOR 74 /* OpenBSD i386 (official) */
183 #elif defined(__alpha__)
184 #define CDEV_MAJOR 53 /* OpenBSD alpha (official) */
185 #else
186 #error arch not supported
187 #endif
188 #endif
189
190 #if defined(__FreeBSD__)
191 #if (__FreeBSD_version < 400000)
192 static struct cdevsw altq_cdevsw =
193 { altqopen, altqclose, noread, nowrite,
194 altqioctl, nostop, nullreset, nodevtotty,
195 seltrue, nommap, NULL, "altq", NULL, -1 };
196 #else
197 static struct cdevsw altq_cdevsw =
198 { altqopen, altqclose, noread, nowrite,
199 altqioctl, seltrue, nommap, nostrategy,
200 "altq", CDEV_MAJOR, nodump, nopsize, 0, -1 };
201 #endif
202 #endif
203
204 #if defined(__NetBSD__)
205 const struct cdevsw altq_cdevsw = {
206 altqopen, altqclose, noread, nowrite, altqioctl,
207 nostop, notty, nopoll, nommap, nokqfilter
208 };
209 #endif
210
211 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
212 static
213 #endif
214 int
215 altqopen(dev, flag, fmt, l)
216 dev_t dev;
217 int flag, fmt;
218 struct lwp *l;
219 {
220 int unit = minor(dev);
221
222 if (unit == 0)
223 return (0);
224 if (unit < naltqsw)
225 return (*altqsw[unit].d_open)(dev, flag, fmt, l);
226
227 return ENXIO;
228 }
229
230 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
231 static
232 #endif
233 int
234 altqclose(dev, flag, fmt, l)
235 dev_t dev;
236 int flag, fmt;
237 struct lwp *l;
238 {
239 int unit = minor(dev);
240
241 if (unit == 0)
242 return (0);
243 if (unit < naltqsw)
244 return (*altqsw[unit].d_close)(dev, flag, fmt, l);
245
246 return ENXIO;
247 }
248
249 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
250 static
251 #endif
252 int
253 altqioctl(dev, cmd, addr, flag, l)
254 dev_t dev;
255 ioctlcmd_t cmd;
256 caddr_t addr;
257 int flag;
258 struct lwp *l;
259 {
260 struct proc *p = l->l_proc;
261 int unit = minor(dev);
262
263 if (unit == 0) {
264 struct ifnet *ifp;
265 struct altqreq *typereq;
266 struct tbrreq *tbrreq;
267 int error;
268
269 switch (cmd) {
270 case ALTQGTYPE:
271 case ALTQTBRGET:
272 break;
273 default:
274 #if (__FreeBSD_version > 400000)
275 if ((error = suser(p)) != 0)
276 return (error);
277 #else
278 if ((error = kauth_authorize_generic(p->p_cred,
279 KAUTH_GENERIC_ISSUSER,
280 &p->p_acflag)) != 0)
281 return (error);
282 #endif
283 break;
284 }
285
286 switch (cmd) {
287 case ALTQGTYPE:
288 typereq = (struct altqreq *)addr;
289 if ((ifp = ifunit(typereq->ifname)) == NULL)
290 return (EINVAL);
291 typereq->arg = (u_long)ifp->if_snd.altq_type;
292 return (0);
293 case ALTQTBRSET:
294 tbrreq = (struct tbrreq *)addr;
295 if ((ifp = ifunit(tbrreq->ifname)) == NULL)
296 return (EINVAL);
297 return tbr_set(&ifp->if_snd, &tbrreq->tb_prof);
298 case ALTQTBRGET:
299 tbrreq = (struct tbrreq *)addr;
300 if ((ifp = ifunit(tbrreq->ifname)) == NULL)
301 return (EINVAL);
302 return tbr_get(&ifp->if_snd, &tbrreq->tb_prof);
303 default:
304 return (EINVAL);
305 }
306 }
307 if (unit < naltqsw)
308 return (*altqsw[unit].d_ioctl)(dev, cmd, addr, flag, l);
309
310 return ENXIO;
311 }
312
313 #if !defined(__NetBSD__)
314 static int altq_devsw_installed = 0;
315 #endif
316
317 #ifdef __FreeBSD__
318 #if (__FreeBSD_version < 400000)
319 #ifdef DEVFS
320 static void *altq_devfs_token[sizeof (altqsw) / sizeof (altqsw[0])];
321 #endif
322
323 static void
324 altq_drvinit(unused)
325 void *unused;
326 {
327 dev_t dev;
328 #ifdef DEVFS
329 int i;
330 #endif
331
332 if (!altq_devsw_installed) {
333 dev = makedev(CDEV_MAJOR,0);
334 cdevsw_add(&dev,&altq_cdevsw,NULL);
335 altq_devsw_installed = 1;
336 #ifdef DEVFS
337 for (i=0; i<naltqsw; i++)
338 altq_devfs_token[i] =
339 devfs_add_devswf(&altq_cdevsw, i, DV_CHR,
340 0, 0, 0644, altqsw[i].d_name);
341 #endif
342 printf("altq: major number is %d\n", CDEV_MAJOR);
343 }
344 }
345
346 #else /* FreeBSD 4.x */
347
348 static void
349 altq_drvinit(unused)
350 void *unused;
351 {
352 int unit;
353
354 cdevsw_add(&altq_cdevsw);
355 altq_devsw_installed = 1;
356 printf("altq: major number is %d\n", CDEV_MAJOR);
357
358 /* create minor devices */
359 for (unit = 0; unit < naltqsw; unit++)
360 make_dev(&altq_cdevsw, unit, 0, 0, 0644,
361 altqsw[unit].d_name);
362 }
363
364 #endif /* FreeBSD 4.x */
365
366 SYSINIT(altqdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,altq_drvinit,NULL)
367
368 #endif
369
370 #ifdef ALTQ_KLD
371 /*
372 * KLD support
373 */
374 static int altq_module_register __P((struct altq_module_data *));
375 static int altq_module_deregister __P((struct altq_module_data *));
376
377 static struct altq_module_data *altq_modules[ALTQT_MAX];
378 static struct altqsw noqdisc = {"noq", noopen, noclose, noioctl};
379
380 void altq_module_incref(type)
381 int type;
382 {
383 if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
384 return;
385
386 altq_modules[type]->ref++;
387 }
388
389 void altq_module_declref(type)
390 int type;
391 {
392 if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
393 return;
394
395 altq_modules[type]->ref--;
396 }
397
398 static int
399 altq_module_register(mdata)
400 struct altq_module_data *mdata;
401 {
402 int type = mdata->type;
403
404 if (type < 0 || type >= ALTQT_MAX)
405 return (EINVAL);
406 if (altqsw[type].d_open != noopen)
407 return (EBUSY);
408 altqsw[type] = *mdata->altqsw; /* set discipline functions */
409 altq_modules[type] = mdata; /* save module data pointer */
410 return (0);
411 }
412
413 static int
414 altq_module_deregister(mdata)
415 struct altq_module_data *mdata;
416 {
417 int type = mdata->type;
418
419 if (type < 0 || type >= ALTQT_MAX)
420 return (EINVAL);
421 if (mdata != altq_modules[type])
422 return (EINVAL);
423 if (altq_modules[type]->ref > 0)
424 return (EBUSY);
425 altqsw[type] = noqdisc;
426 altq_modules[type] = NULL;
427 return (0);
428 }
429
430 int
431 altq_module_handler(mod, cmd, arg)
432 module_t mod;
433 int cmd;
434 void * arg;
435 {
436 struct altq_module_data *data = (struct altq_module_data *)arg;
437 int error = 0;
438
439 switch (cmd) {
440 case MOD_LOAD:
441 error = altq_module_register(data);
442 break;
443
444 case MOD_UNLOAD:
445 error = altq_module_deregister(data);
446 break;
447
448 default:
449 error = EINVAL;
450 break;
451 }
452
453 return(error);
454 }
455
456 #endif /* ALTQ_KLD */
457