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