altq_afmap.c revision 1.3 1 1.3 thorpej /* $NetBSD: altq_afmap.c,v 1.3 2001/04/13 23:29:55 thorpej Exp $ */
2 1.1 thorpej /* $KAME: altq_afmap.c,v 1.7 2000/12/14 08:12:45 thorpej Exp $ */
3 1.1 thorpej
4 1.1 thorpej /*
5 1.1 thorpej * Copyright (C) 1997-2000
6 1.1 thorpej * Sony Computer Science Laboratories Inc. All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * Redistribution and use in source and binary forms, with or without
9 1.1 thorpej * modification, are permitted provided that the following conditions
10 1.1 thorpej * are met:
11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer.
13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.1 thorpej * documentation and/or other materials provided with the distribution.
16 1.1 thorpej *
17 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 thorpej * SUCH DAMAGE.
28 1.1 thorpej */
29 1.1 thorpej
30 1.1 thorpej /*
31 1.1 thorpej * experimental:
32 1.1 thorpej * mapping an ip flow to atm vpi/vci.
33 1.1 thorpej * this module is not related to queueing at all, but uses the altq
34 1.1 thorpej * flowinfo mechanism. it's just put in the altq framework since
35 1.1 thorpej * it is easy to add devices to altq.
36 1.1 thorpej */
37 1.1 thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
38 1.1 thorpej #include "opt_altq.h"
39 1.1 thorpej #if (__FreeBSD__ != 2)
40 1.1 thorpej #include "opt_inet.h"
41 1.1 thorpej #ifdef __FreeBSD__
42 1.1 thorpej #include "opt_inet6.h"
43 1.1 thorpej #endif
44 1.1 thorpej #endif
45 1.1 thorpej #endif /* __FreeBSD__ || __NetBSD__ */
46 1.1 thorpej #ifdef ALTQ_AFMAP
47 1.1 thorpej
48 1.1 thorpej #include <sys/types.h>
49 1.1 thorpej #include <sys/param.h>
50 1.1 thorpej #include <sys/malloc.h>
51 1.1 thorpej #include <sys/mbuf.h>
52 1.1 thorpej #include <sys/uio.h>
53 1.1 thorpej #include <sys/socket.h>
54 1.1 thorpej #include <sys/systm.h>
55 1.1 thorpej #include <sys/proc.h>
56 1.1 thorpej #include <sys/errno.h>
57 1.1 thorpej #include <sys/time.h>
58 1.1 thorpej #include <sys/kernel.h>
59 1.1 thorpej
60 1.1 thorpej #include <net/if.h>
61 1.1 thorpej #include <net/if_types.h>
62 1.1 thorpej #include <netinet/in.h>
63 1.1 thorpej
64 1.1 thorpej #include <altq/altq.h>
65 1.1 thorpej #include <altq/altq_conf.h>
66 1.1 thorpej #include <altq/altq_afmap.h>
67 1.1 thorpej
68 1.1 thorpej LIST_HEAD(, afm_head) afhead_chain;
69 1.1 thorpej
70 1.1 thorpej static struct afm *afm_match4 __P((struct afm_head *, struct flowinfo_in *));
71 1.1 thorpej #ifdef INET6
72 1.1 thorpej static struct afm *afm_match6 __P((struct afm_head *, struct flowinfo_in6 *));
73 1.1 thorpej #endif
74 1.1 thorpej
75 1.1 thorpej /*
76 1.1 thorpej * rules to block interrupts: afm_match can be called from a net
77 1.1 thorpej * level interrupt so that other routines handling the lists should
78 1.1 thorpej * be called in splnet().
79 1.1 thorpej */
80 1.1 thorpej int
81 1.1 thorpej afm_alloc(ifp)
82 1.1 thorpej struct ifnet *ifp;
83 1.1 thorpej {
84 1.1 thorpej struct afm_head *head;
85 1.1 thorpej
86 1.1 thorpej MALLOC(head, struct afm_head *, sizeof(struct afm_head),
87 1.1 thorpej M_DEVBUF, M_WAITOK);
88 1.1 thorpej if (head == NULL)
89 1.1 thorpej panic("afm_alloc: malloc failed!");
90 1.1 thorpej bzero(head, sizeof(struct afm_head));
91 1.1 thorpej
92 1.1 thorpej /* initialize per interface afmap list */
93 1.1 thorpej LIST_INIT(&head->afh_head);
94 1.1 thorpej
95 1.1 thorpej head->afh_ifp = ifp;
96 1.1 thorpej
97 1.1 thorpej /* add this afm_head to the chain */
98 1.1 thorpej LIST_INSERT_HEAD(&afhead_chain, head, afh_chain);
99 1.1 thorpej
100 1.1 thorpej return (0);
101 1.1 thorpej }
102 1.1 thorpej
103 1.1 thorpej int
104 1.1 thorpej afm_dealloc(ifp)
105 1.1 thorpej struct ifnet *ifp;
106 1.1 thorpej {
107 1.1 thorpej struct afm_head *head;
108 1.1 thorpej
109 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
110 1.1 thorpej head = head->afh_chain.le_next)
111 1.1 thorpej if (head->afh_ifp == ifp)
112 1.1 thorpej break;
113 1.1 thorpej if (head == NULL)
114 1.1 thorpej return (-1);
115 1.1 thorpej
116 1.1 thorpej afm_removeall(ifp);
117 1.1 thorpej
118 1.1 thorpej LIST_REMOVE(head, afh_chain);
119 1.1 thorpej
120 1.1 thorpej FREE(head, M_DEVBUF);
121 1.1 thorpej return 0;
122 1.1 thorpej }
123 1.1 thorpej
124 1.1 thorpej struct afm *
125 1.1 thorpej afm_top(ifp)
126 1.1 thorpej struct ifnet *ifp;
127 1.1 thorpej {
128 1.1 thorpej struct afm_head *head;
129 1.1 thorpej
130 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
131 1.1 thorpej head = head->afh_chain.le_next)
132 1.1 thorpej if (head->afh_ifp == ifp)
133 1.1 thorpej break;
134 1.1 thorpej if (head == NULL)
135 1.1 thorpej return NULL;
136 1.1 thorpej
137 1.1 thorpej return (head->afh_head.lh_first);
138 1.1 thorpej }
139 1.1 thorpej
140 1.1 thorpej int afm_add(ifp, flowmap)
141 1.1 thorpej struct ifnet *ifp;
142 1.1 thorpej struct atm_flowmap *flowmap;
143 1.1 thorpej {
144 1.1 thorpej struct afm_head *head;
145 1.1 thorpej struct afm *afm;
146 1.1 thorpej
147 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
148 1.1 thorpej head = head->afh_chain.le_next)
149 1.1 thorpej if (head->afh_ifp == ifp)
150 1.1 thorpej break;
151 1.1 thorpej if (head == NULL)
152 1.1 thorpej return (-1);
153 1.1 thorpej
154 1.1 thorpej if (flowmap->af_flowinfo.fi_family == AF_INET) {
155 1.1 thorpej if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in))
156 1.1 thorpej return (EINVAL);
157 1.1 thorpej #ifdef INET6
158 1.1 thorpej } else if (flowmap->af_flowinfo.fi_family == AF_INET6) {
159 1.1 thorpej if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in6))
160 1.1 thorpej return (EINVAL);
161 1.1 thorpej #endif
162 1.1 thorpej } else
163 1.1 thorpej return (EINVAL);
164 1.1 thorpej
165 1.1 thorpej MALLOC(afm, struct afm *, sizeof(struct afm),
166 1.1 thorpej M_DEVBUF, M_WAITOK);
167 1.1 thorpej if (afm == NULL)
168 1.1 thorpej return (ENOMEM);
169 1.1 thorpej bzero(afm, sizeof(struct afm));
170 1.1 thorpej
171 1.1 thorpej afm->afm_vci = flowmap->af_vci;
172 1.1 thorpej afm->afm_vpi = flowmap->af_vpi;
173 1.1 thorpej bcopy(&flowmap->af_flowinfo, &afm->afm_flowinfo,
174 1.1 thorpej flowmap->af_flowinfo.fi_len);
175 1.1 thorpej
176 1.1 thorpej LIST_INSERT_HEAD(&head->afh_head, afm, afm_list);
177 1.1 thorpej return 0;
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej int
181 1.1 thorpej afm_remove(afm)
182 1.1 thorpej struct afm *afm;
183 1.1 thorpej {
184 1.1 thorpej LIST_REMOVE(afm, afm_list);
185 1.1 thorpej FREE(afm, M_DEVBUF);
186 1.1 thorpej return (0);
187 1.1 thorpej }
188 1.1 thorpej
189 1.1 thorpej int
190 1.1 thorpej afm_removeall(ifp)
191 1.1 thorpej struct ifnet *ifp;
192 1.1 thorpej {
193 1.1 thorpej struct afm_head *head;
194 1.1 thorpej struct afm *afm;
195 1.1 thorpej
196 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
197 1.1 thorpej head = head->afh_chain.le_next)
198 1.1 thorpej if (head->afh_ifp == ifp)
199 1.1 thorpej break;
200 1.1 thorpej if (head == NULL)
201 1.1 thorpej return (-1);
202 1.1 thorpej
203 1.1 thorpej while ((afm = head->afh_head.lh_first) != NULL)
204 1.1 thorpej afm_remove(afm);
205 1.1 thorpej return (0);
206 1.1 thorpej }
207 1.1 thorpej
208 1.1 thorpej struct afm *
209 1.1 thorpej afm_lookup(ifp, vpi, vci)
210 1.1 thorpej struct ifnet *ifp;
211 1.1 thorpej int vpi, vci;
212 1.1 thorpej {
213 1.1 thorpej struct afm_head *head;
214 1.1 thorpej struct afm *afm;
215 1.1 thorpej
216 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
217 1.1 thorpej head = head->afh_chain.le_next)
218 1.1 thorpej if (head->afh_ifp == ifp)
219 1.1 thorpej break;
220 1.1 thorpej if (head == NULL)
221 1.1 thorpej return NULL;
222 1.1 thorpej
223 1.1 thorpej for (afm = head->afh_head.lh_first; afm != NULL;
224 1.1 thorpej afm = afm->afm_list.le_next)
225 1.1 thorpej if (afm->afm_vpi == vpi && afm->afm_vci == vci)
226 1.1 thorpej break;
227 1.1 thorpej return afm;
228 1.1 thorpej }
229 1.1 thorpej
230 1.1 thorpej static struct afm *
231 1.1 thorpej afm_match4(head, fp)
232 1.1 thorpej struct afm_head *head;
233 1.1 thorpej struct flowinfo_in *fp;
234 1.1 thorpej {
235 1.1 thorpej struct afm *afm;
236 1.1 thorpej
237 1.1 thorpej for (afm = head->afh_head.lh_first; afm != NULL;
238 1.1 thorpej afm = afm->afm_list.le_next) {
239 1.1 thorpej if (afm->afm_flowinfo4.fi_dst.s_addr != 0 &&
240 1.1 thorpej afm->afm_flowinfo4.fi_dst.s_addr != fp->fi_dst.s_addr)
241 1.1 thorpej continue;
242 1.1 thorpej if (afm->afm_flowinfo4.fi_dport != 0 &&
243 1.1 thorpej afm->afm_flowinfo4.fi_dport != fp->fi_dport)
244 1.1 thorpej continue;
245 1.1 thorpej if (afm->afm_flowinfo4.fi_src.s_addr != 0 &&
246 1.1 thorpej afm->afm_flowinfo4.fi_src.s_addr != fp->fi_src.s_addr)
247 1.1 thorpej continue;
248 1.1 thorpej if (afm->afm_flowinfo4.fi_sport != 0 &&
249 1.1 thorpej afm->afm_flowinfo4.fi_sport != fp->fi_sport)
250 1.1 thorpej continue;
251 1.1 thorpej if (afm->afm_flowinfo4.fi_proto != 0 &&
252 1.1 thorpej afm->afm_flowinfo4.fi_proto != fp->fi_proto)
253 1.1 thorpej continue;
254 1.1 thorpej /* match found! */
255 1.1 thorpej return (afm);
256 1.1 thorpej }
257 1.1 thorpej return NULL;
258 1.1 thorpej }
259 1.1 thorpej
260 1.1 thorpej #ifdef INET6
261 1.1 thorpej static struct afm *
262 1.1 thorpej afm_match6(head, fp)
263 1.1 thorpej struct afm_head *head;
264 1.1 thorpej struct flowinfo_in6 *fp;
265 1.1 thorpej {
266 1.1 thorpej struct afm *afm;
267 1.1 thorpej
268 1.1 thorpej for (afm = head->afh_head.lh_first; afm != NULL;
269 1.1 thorpej afm = afm->afm_list.le_next) {
270 1.1 thorpej if (afm->afm_flowinfo6.fi6_flowlabel != 0 &&
271 1.1 thorpej afm->afm_flowinfo6.fi6_flowlabel != fp->fi6_flowlabel)
272 1.1 thorpej continue;
273 1.1 thorpej #ifdef notyet
274 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_dst) &&
275 1.1 thorpej !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_dst,
276 1.1 thorpej &fp->fi6_dst))
277 1.1 thorpej continue;
278 1.1 thorpej if (afm->afm_flowinfo6.fi6_dport != 0 &&
279 1.1 thorpej afm->afm_flowinfo6.fi6_dport != fp->fi6_dport)
280 1.1 thorpej continue;
281 1.1 thorpej #endif
282 1.1 thorpej if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_src) &&
283 1.1 thorpej !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_src,
284 1.1 thorpej &fp->fi6_src))
285 1.1 thorpej continue;
286 1.1 thorpej #ifdef notyet
287 1.1 thorpej if (afm->afm_flowinfo6.fi6_sport != 0 &&
288 1.1 thorpej afm->afm_flowinfo6.fi6_sport != fp->fi6_sport)
289 1.1 thorpej continue;
290 1.1 thorpej #endif
291 1.1 thorpej if (afm->afm_flowinfo6.fi6_proto != 0 &&
292 1.1 thorpej afm->afm_flowinfo6.fi6_proto != fp->fi6_proto)
293 1.1 thorpej continue;
294 1.1 thorpej /* match found! */
295 1.1 thorpej return (afm);
296 1.1 thorpej }
297 1.1 thorpej return NULL;
298 1.1 thorpej }
299 1.1 thorpej #endif
300 1.1 thorpej
301 1.3 thorpej /* should be called in splnet() */
302 1.1 thorpej struct afm *
303 1.1 thorpej afm_match(ifp, flow)
304 1.1 thorpej struct ifnet *ifp;
305 1.1 thorpej struct flowinfo *flow;
306 1.1 thorpej {
307 1.1 thorpej struct afm_head *head;
308 1.1 thorpej
309 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
310 1.1 thorpej head = head->afh_chain.le_next)
311 1.1 thorpej if (head->afh_ifp == ifp)
312 1.1 thorpej break;
313 1.1 thorpej if (head == NULL)
314 1.1 thorpej return NULL;
315 1.1 thorpej
316 1.1 thorpej switch (flow->fi_family) {
317 1.1 thorpej case AF_INET:
318 1.1 thorpej return (afm_match4(head, (struct flowinfo_in *)flow));
319 1.1 thorpej
320 1.1 thorpej #ifdef INET6
321 1.1 thorpej case AF_INET6:
322 1.1 thorpej return (afm_match6(head, (struct flowinfo_in6 *)flow));
323 1.1 thorpej #endif
324 1.1 thorpej
325 1.1 thorpej default:
326 1.1 thorpej return NULL;
327 1.1 thorpej }
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej /*
331 1.1 thorpej * afm device interface
332 1.1 thorpej */
333 1.1 thorpej altqdev_decl(afm);
334 1.1 thorpej
335 1.1 thorpej int
336 1.1 thorpej afmopen(dev, flag, fmt, p)
337 1.1 thorpej dev_t dev;
338 1.1 thorpej int flag, fmt;
339 1.1 thorpej struct proc *p;
340 1.1 thorpej {
341 1.1 thorpej return 0;
342 1.1 thorpej }
343 1.1 thorpej
344 1.1 thorpej int
345 1.1 thorpej afmclose(dev, flag, fmt, p)
346 1.1 thorpej dev_t dev;
347 1.1 thorpej int flag, fmt;
348 1.1 thorpej struct proc *p;
349 1.1 thorpej {
350 1.1 thorpej int err, error = 0;
351 1.1 thorpej struct atm_flowmap fmap;
352 1.1 thorpej struct afm_head *head;
353 1.1 thorpej
354 1.1 thorpej for (head = afhead_chain.lh_first; head != NULL;
355 1.1 thorpej head = head->afh_chain.le_next) {
356 1.1 thorpej
357 1.1 thorpej /* call interface to clean up maps */
358 1.1 thorpej #if defined(__NetBSD__) || defined(__OpenBSD__)
359 1.1 thorpej sprintf(fmap.af_ifname, "%s", head->afh_ifp->if_xname);
360 1.1 thorpej #else
361 1.1 thorpej sprintf(fmap.af_ifname, "%s%d",
362 1.1 thorpej head->afh_ifp->if_name, head->afh_ifp->if_unit);
363 1.1 thorpej #endif
364 1.1 thorpej err = afmioctl(dev, AFM_CLEANFMAP, (caddr_t)&fmap, flag, p);
365 1.1 thorpej if (err && error == 0)
366 1.1 thorpej error = err;
367 1.1 thorpej }
368 1.1 thorpej
369 1.1 thorpej return error;
370 1.1 thorpej }
371 1.1 thorpej
372 1.1 thorpej int
373 1.1 thorpej afmioctl(dev, cmd, addr, flag, p)
374 1.1 thorpej dev_t dev;
375 1.1 thorpej ioctlcmd_t cmd;
376 1.1 thorpej caddr_t addr;
377 1.1 thorpej int flag;
378 1.1 thorpej struct proc *p;
379 1.1 thorpej {
380 1.1 thorpej int error = 0;
381 1.1 thorpej struct atm_flowmap *flowmap;
382 1.1 thorpej struct ifnet *ifp;
383 1.1 thorpej
384 1.1 thorpej /* check cmd for superuser only */
385 1.1 thorpej switch (cmd) {
386 1.1 thorpej case AFM_GETFMAP:
387 1.1 thorpej break;
388 1.1 thorpej default:
389 1.1 thorpej #if (__FreeBSD_version > 400000)
390 1.1 thorpej error = suser(p);
391 1.1 thorpej #else
392 1.1 thorpej error = suser(p->p_ucred, &p->p_acflag);
393 1.1 thorpej #endif
394 1.1 thorpej if (error)
395 1.1 thorpej return (error);
396 1.1 thorpej break;
397 1.1 thorpej }
398 1.1 thorpej
399 1.1 thorpej /* lookup interface */
400 1.1 thorpej flowmap = (struct atm_flowmap *)addr;
401 1.1 thorpej flowmap->af_ifname[IFNAMSIZ-1] = '\0';
402 1.1 thorpej ifp = ifunit(flowmap->af_ifname);
403 1.1 thorpej if (ifp == NULL || ifp->if_ioctl == NULL ||
404 1.1 thorpej (ifp->if_flags & IFF_RUNNING) == 0)
405 1.1 thorpej error = ENXIO;
406 1.1 thorpej else
407 1.1 thorpej error = ifp->if_ioctl(ifp, cmd, addr);
408 1.1 thorpej
409 1.1 thorpej return error;
410 1.1 thorpej }
411 1.1 thorpej
412 1.1 thorpej #endif /* ALTQ_AFMAP */
413