altq_afmap.c revision 1.9.12.1 1 /* $NetBSD: altq_afmap.c,v 1.9.12.1 2006/03/18 12:08:18 peter Exp $ */
2 /* $KAME: altq_afmap.c,v 1.12 2005/04/13 03:44:24 suz Exp $ */
3
4 /*
5 * Copyright (C) 1997-2002
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 /*
31 * experimental:
32 * mapping an ip flow to atm vpi/vci.
33 * this module is not related to queueing at all, but uses the altq
34 * flowinfo mechanism. it's just put in the altq framework since
35 * it is easy to add devices to altq.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: altq_afmap.c,v 1.9.12.1 2006/03/18 12:08:18 peter Exp $");
40
41 #ifdef _KERNEL_OPT
42 #include "opt_altq.h"
43 #include "opt_inet.h"
44 #endif
45
46 #ifdef ALTQ_AFMAP
47
48 #include <sys/param.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/uio.h>
52 #include <sys/socket.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/errno.h>
56 #include <sys/time.h>
57 #include <sys/kernel.h>
58
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <netinet/in.h>
62
63 #include <altq/altq.h>
64 #include <altq/altq_conf.h>
65 #include <altq/altq_afmap.h>
66
67 #ifdef ALTQ3_COMPAT
68
69 LIST_HEAD(, afm_head) afhead_chain;
70
71 static struct afm *afm_match4(struct afm_head *, struct flowinfo_in *);
72 #ifdef INET6
73 static struct afm *afm_match6(struct afm_head *, struct flowinfo_in6 *);
74 #endif
75
76 /*
77 * rules to block interrupts: afm_match can be called from a net
78 * level interrupt so that other routines handling the lists should
79 * be called in splnet().
80 */
81 int
82 afm_alloc(ifp)
83 struct ifnet *ifp;
84 {
85 struct afm_head *head;
86
87 MALLOC(head, struct afm_head *, sizeof(struct afm_head),
88 M_DEVBUF, M_WAITOK);
89 if (head == NULL)
90 panic("afm_alloc: malloc failed!");
91 (void)memset(head, 0, sizeof(struct afm_head));
92
93 /* initialize per interface afmap list */
94 LIST_INIT(&head->afh_head);
95
96 head->afh_ifp = ifp;
97
98 /* add this afm_head to the chain */
99 LIST_INSERT_HEAD(&afhead_chain, head, afh_chain);
100
101 return (0);
102 }
103
104 int
105 afm_dealloc(ifp)
106 struct ifnet *ifp;
107 {
108 struct afm_head *head;
109
110 for (head = afhead_chain.lh_first; head != NULL;
111 head = head->afh_chain.le_next)
112 if (head->afh_ifp == ifp)
113 break;
114 if (head == NULL)
115 return (-1);
116
117 afm_removeall(ifp);
118
119 LIST_REMOVE(head, afh_chain);
120
121 FREE(head, M_DEVBUF);
122 return 0;
123 }
124
125 struct afm *
126 afm_top(ifp)
127 struct ifnet *ifp;
128 {
129 struct afm_head *head;
130
131 for (head = afhead_chain.lh_first; head != NULL;
132 head = head->afh_chain.le_next)
133 if (head->afh_ifp == ifp)
134 break;
135 if (head == NULL)
136 return NULL;
137
138 return (head->afh_head.lh_first);
139 }
140
141 int afm_add(ifp, flowmap)
142 struct ifnet *ifp;
143 struct atm_flowmap *flowmap;
144 {
145 struct afm_head *head;
146 struct afm *afm;
147
148 for (head = afhead_chain.lh_first; head != NULL;
149 head = head->afh_chain.le_next)
150 if (head->afh_ifp == ifp)
151 break;
152 if (head == NULL)
153 return (-1);
154
155 if (flowmap->af_flowinfo.fi_family == AF_INET) {
156 if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in))
157 return (EINVAL);
158 #ifdef INET6
159 } else if (flowmap->af_flowinfo.fi_family == AF_INET6) {
160 if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in6))
161 return (EINVAL);
162 #endif
163 } else
164 return (EINVAL);
165
166 MALLOC(afm, struct afm *, sizeof(struct afm),
167 M_DEVBUF, M_WAITOK);
168 if (afm == NULL)
169 return (ENOMEM);
170 (void)memset(afm, 0, sizeof(struct afm));
171
172 afm->afm_vci = flowmap->af_vci;
173 afm->afm_vpi = flowmap->af_vpi;
174 (void)memcpy(&afm->afm_flowinfo, &flowmap->af_flowinfo,
175 flowmap->af_flowinfo.fi_len);
176
177 LIST_INSERT_HEAD(&head->afh_head, afm, afm_list);
178 return 0;
179 }
180
181 int
182 afm_remove(afm)
183 struct afm *afm;
184 {
185 LIST_REMOVE(afm, afm_list);
186 FREE(afm, M_DEVBUF);
187 return (0);
188 }
189
190 int
191 afm_removeall(ifp)
192 struct ifnet *ifp;
193 {
194 struct afm_head *head;
195 struct afm *afm;
196
197 for (head = afhead_chain.lh_first; head != NULL;
198 head = head->afh_chain.le_next)
199 if (head->afh_ifp == ifp)
200 break;
201 if (head == NULL)
202 return (-1);
203
204 while ((afm = head->afh_head.lh_first) != NULL)
205 afm_remove(afm);
206 return (0);
207 }
208
209 struct afm *
210 afm_lookup(ifp, vpi, vci)
211 struct ifnet *ifp;
212 int vpi, vci;
213 {
214 struct afm_head *head;
215 struct afm *afm;
216
217 for (head = afhead_chain.lh_first; head != NULL;
218 head = head->afh_chain.le_next)
219 if (head->afh_ifp == ifp)
220 break;
221 if (head == NULL)
222 return NULL;
223
224 for (afm = head->afh_head.lh_first; afm != NULL;
225 afm = afm->afm_list.le_next)
226 if (afm->afm_vpi == vpi && afm->afm_vci == vci)
227 break;
228 return afm;
229 }
230
231 static struct afm *
232 afm_match4(head, fp)
233 struct afm_head *head;
234 struct flowinfo_in *fp;
235 {
236 struct afm *afm;
237
238 for (afm = head->afh_head.lh_first; afm != NULL;
239 afm = afm->afm_list.le_next) {
240 if (afm->afm_flowinfo4.fi_dst.s_addr != 0 &&
241 afm->afm_flowinfo4.fi_dst.s_addr != fp->fi_dst.s_addr)
242 continue;
243 if (afm->afm_flowinfo4.fi_dport != 0 &&
244 afm->afm_flowinfo4.fi_dport != fp->fi_dport)
245 continue;
246 if (afm->afm_flowinfo4.fi_src.s_addr != 0 &&
247 afm->afm_flowinfo4.fi_src.s_addr != fp->fi_src.s_addr)
248 continue;
249 if (afm->afm_flowinfo4.fi_sport != 0 &&
250 afm->afm_flowinfo4.fi_sport != fp->fi_sport)
251 continue;
252 if (afm->afm_flowinfo4.fi_proto != 0 &&
253 afm->afm_flowinfo4.fi_proto != fp->fi_proto)
254 continue;
255 /* match found! */
256 return (afm);
257 }
258 return NULL;
259 }
260
261 #ifdef INET6
262 static struct afm *
263 afm_match6(head, fp)
264 struct afm_head *head;
265 struct flowinfo_in6 *fp;
266 {
267 struct afm *afm;
268
269 for (afm = head->afh_head.lh_first; afm != NULL;
270 afm = afm->afm_list.le_next) {
271 if (afm->afm_flowinfo6.fi6_flowlabel != 0 &&
272 afm->afm_flowinfo6.fi6_flowlabel != fp->fi6_flowlabel)
273 continue;
274 #ifdef notyet
275 if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_dst) &&
276 !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_dst,
277 &fp->fi6_dst))
278 continue;
279 if (afm->afm_flowinfo6.fi6_dport != 0 &&
280 afm->afm_flowinfo6.fi6_dport != fp->fi6_dport)
281 continue;
282 #endif
283 if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_src) &&
284 !IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_src,
285 &fp->fi6_src))
286 continue;
287 #ifdef notyet
288 if (afm->afm_flowinfo6.fi6_sport != 0 &&
289 afm->afm_flowinfo6.fi6_sport != fp->fi6_sport)
290 continue;
291 #endif
292 if (afm->afm_flowinfo6.fi6_proto != 0 &&
293 afm->afm_flowinfo6.fi6_proto != fp->fi6_proto)
294 continue;
295 /* match found! */
296 return (afm);
297 }
298 return NULL;
299 }
300 #endif
301
302 /* should be called in splnet() */
303 struct afm *
304 afm_match(ifp, flow)
305 struct ifnet *ifp;
306 struct flowinfo *flow;
307 {
308 struct afm_head *head;
309
310 for (head = afhead_chain.lh_first; head != NULL;
311 head = head->afh_chain.le_next)
312 if (head->afh_ifp == ifp)
313 break;
314 if (head == NULL)
315 return NULL;
316
317 switch (flow->fi_family) {
318 case AF_INET:
319 return (afm_match4(head, (struct flowinfo_in *)flow));
320
321 #ifdef INET6
322 case AF_INET6:
323 return (afm_match6(head, (struct flowinfo_in6 *)flow));
324 #endif
325
326 default:
327 return NULL;
328 }
329 }
330
331 /*
332 * afm device interface
333 */
334 altqdev_decl(afm);
335
336 int
337 afmopen(dev, flag, fmt, l)
338 dev_t dev;
339 int flag, fmt;
340 struct lwp *l;
341 {
342 return 0;
343 }
344
345 int
346 afmclose(dev, flag, fmt, l)
347 dev_t dev;
348 int flag, fmt;
349 struct lwp *l;
350 {
351 int err, error = 0;
352 struct atm_flowmap fmap;
353 struct afm_head *head;
354
355 for (head = afhead_chain.lh_first; head != NULL;
356 head = head->afh_chain.le_next) {
357
358 /* call interface to clean up maps */
359 sprintf(fmap.af_ifname, "%s", head->afh_ifp->if_xname);
360 err = afmioctl(dev, AFM_CLEANFMAP, (caddr_t)&fmap, flag, l);
361 if (err && error == 0)
362 error = err;
363 }
364
365 return error;
366 }
367
368 int
369 afmioctl(dev, cmd, addr, flag, l)
370 dev_t dev;
371 ioctlcmd_t cmd;
372 caddr_t addr;
373 int flag;
374 struct lwp *l;
375 {
376 int error = 0;
377 struct atm_flowmap *flowmap;
378 struct ifnet *ifp;
379 struct proc *p = l->l_proc;
380
381 /* check cmd for superuser only */
382 switch (cmd) {
383 case AFM_GETFMAP:
384 break;
385 default:
386 #if (__FreeBSD_version > 400000)
387 error = suser(p);
388 #else
389 error = suser(p->p_ucred, &p->p_acflag);
390 #endif
391 if (error)
392 return (error);
393 break;
394 }
395
396 /* lookup interface */
397 flowmap = (struct atm_flowmap *)addr;
398 flowmap->af_ifname[IFNAMSIZ-1] = '\0';
399 ifp = ifunit(flowmap->af_ifname);
400 if (ifp == NULL || ifp->if_ioctl == NULL ||
401 (ifp->if_flags & IFF_RUNNING) == 0)
402 error = ENXIO;
403 else
404 error = ifp->if_ioctl(ifp, cmd, addr);
405
406 return error;
407 }
408
409 #endif /* ALTQ3_COMPAT */
410 #endif /* ALTQ_AFMAP */
411