1 /* $NetBSD: altq_afmap.c,v 1.18 2007/03/04 05:59:00 christos Exp $ */
2 /* $KAME: altq_afmap.c,v 1.12 2005/04/13 03:44:24 suz Exp $ */
5 * Copyright (C) 1997-2002
6 * Sony Computer Science Laboratories Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: altq_afmap.c,v 1.18 2007/03/04 05:59:00 christos Exp $");
48 #include <sys/param.h>
49 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 #include <sys/systm.h>
55 #include <sys/errno.h>
57 #include <sys/kernel.h>
58 #include <sys/kauth.h>
61 #include <net/if_types.h>
62 #include <netinet/in.h>
64 #include <altq/altq.h>
65 #include <altq/altq_conf.h>
66 #include <altq/altq_afmap.h>
70 LIST_HEAD(, afm_head
) afhead_chain
;
72 static struct afm
*afm_match4(struct afm_head
*, struct flowinfo_in
*);
74 static struct afm
*afm_match6(struct afm_head
*, struct flowinfo_in6
*);
78 * rules to block interrupts: afm_match can be called from a net
79 * level interrupt so that other routines handling the lists should
80 * be called in splnet().
83 afm_alloc(struct ifnet
*ifp
)
85 struct afm_head
*head
;
87 head
= malloc(sizeof(struct afm_head
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
89 panic("afm_alloc: malloc failed!");
91 /* initialize per interface afmap list */
92 LIST_INIT(&head
->afh_head
);
96 /* add this afm_head to the chain */
97 LIST_INSERT_HEAD(&afhead_chain
, head
, afh_chain
);
103 afm_dealloc(struct ifnet
*ifp
)
105 struct afm_head
*head
;
107 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
108 head
= head
->afh_chain
.le_next
)
109 if (head
->afh_ifp
== ifp
)
116 LIST_REMOVE(head
, afh_chain
);
118 free(head
, M_DEVBUF
);
123 afm_top(struct ifnet
*ifp
)
125 struct afm_head
*head
;
127 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
128 head
= head
->afh_chain
.le_next
)
129 if (head
->afh_ifp
== ifp
)
134 return (head
->afh_head
.lh_first
);
138 afm_add(struct ifnet
*ifp
, struct atm_flowmap
*flowmap
)
140 struct afm_head
*head
;
143 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
144 head
= head
->afh_chain
.le_next
)
145 if (head
->afh_ifp
== ifp
)
150 if (flowmap
->af_flowinfo
.fi_family
== AF_INET
) {
151 if (flowmap
->af_flowinfo
.fi_len
!= sizeof(struct flowinfo_in
))
154 } else if (flowmap
->af_flowinfo
.fi_family
== AF_INET6
) {
155 if (flowmap
->af_flowinfo
.fi_len
!= sizeof(struct flowinfo_in6
))
161 afm
= malloc(sizeof(struct afm
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
165 afm
->afm_vci
= flowmap
->af_vci
;
166 afm
->afm_vpi
= flowmap
->af_vpi
;
167 (void)memcpy(&afm
->afm_flowinfo
, &flowmap
->af_flowinfo
,
168 flowmap
->af_flowinfo
.fi_len
);
170 LIST_INSERT_HEAD(&head
->afh_head
, afm
, afm_list
);
175 afm_remove(struct afm
*afm
)
177 LIST_REMOVE(afm
, afm_list
);
183 afm_removeall(struct ifnet
*ifp
)
185 struct afm_head
*head
;
188 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
189 head
= head
->afh_chain
.le_next
)
190 if (head
->afh_ifp
== ifp
)
195 while ((afm
= head
->afh_head
.lh_first
) != NULL
)
201 afm_lookup(struct ifnet
*ifp
, int vpi
, int vci
)
203 struct afm_head
*head
;
206 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
207 head
= head
->afh_chain
.le_next
)
208 if (head
->afh_ifp
== ifp
)
213 for (afm
= head
->afh_head
.lh_first
; afm
!= NULL
;
214 afm
= afm
->afm_list
.le_next
)
215 if (afm
->afm_vpi
== vpi
&& afm
->afm_vci
== vci
)
221 afm_match4(struct afm_head
*head
, struct flowinfo_in
*fp
)
225 for (afm
= head
->afh_head
.lh_first
; afm
!= NULL
;
226 afm
= afm
->afm_list
.le_next
) {
227 if (afm
->afm_flowinfo4
.fi_dst
.s_addr
!= 0 &&
228 afm
->afm_flowinfo4
.fi_dst
.s_addr
!= fp
->fi_dst
.s_addr
)
230 if (afm
->afm_flowinfo4
.fi_dport
!= 0 &&
231 afm
->afm_flowinfo4
.fi_dport
!= fp
->fi_dport
)
233 if (afm
->afm_flowinfo4
.fi_src
.s_addr
!= 0 &&
234 afm
->afm_flowinfo4
.fi_src
.s_addr
!= fp
->fi_src
.s_addr
)
236 if (afm
->afm_flowinfo4
.fi_sport
!= 0 &&
237 afm
->afm_flowinfo4
.fi_sport
!= fp
->fi_sport
)
239 if (afm
->afm_flowinfo4
.fi_proto
!= 0 &&
240 afm
->afm_flowinfo4
.fi_proto
!= fp
->fi_proto
)
250 afm_match6(struct afm_head
*head
, struct flowinfo_in6
*fp
)
254 for (afm
= head
->afh_head
.lh_first
; afm
!= NULL
;
255 afm
= afm
->afm_list
.le_next
) {
256 if (afm
->afm_flowinfo6
.fi6_flowlabel
!= 0 &&
257 afm
->afm_flowinfo6
.fi6_flowlabel
!= fp
->fi6_flowlabel
)
260 if (!IN6_IS_ADDR_UNSPECIFIED(&afm
->afm_flowinfo6
.fi6_dst
) &&
261 !IN6_ARE_ADDR_EQUAL(&afm
->afm_flowinfo6
.fi6_dst
,
264 if (afm
->afm_flowinfo6
.fi6_dport
!= 0 &&
265 afm
->afm_flowinfo6
.fi6_dport
!= fp
->fi6_dport
)
268 if (!IN6_IS_ADDR_UNSPECIFIED(&afm
->afm_flowinfo6
.fi6_src
) &&
269 !IN6_ARE_ADDR_EQUAL(&afm
->afm_flowinfo6
.fi6_src
,
273 if (afm
->afm_flowinfo6
.fi6_sport
!= 0 &&
274 afm
->afm_flowinfo6
.fi6_sport
!= fp
->fi6_sport
)
277 if (afm
->afm_flowinfo6
.fi6_proto
!= 0 &&
278 afm
->afm_flowinfo6
.fi6_proto
!= fp
->fi6_proto
)
287 /* should be called in splnet() */
289 afm_match(struct ifnet
*ifp
, struct flowinfo
*flow
)
291 struct afm_head
*head
;
293 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
294 head
= head
->afh_chain
.le_next
)
295 if (head
->afh_ifp
== ifp
)
300 switch (flow
->fi_family
) {
302 return (afm_match4(head
, (struct flowinfo_in
*)flow
));
306 return (afm_match6(head
, (struct flowinfo_in6
*)flow
));
315 * afm device interface
320 afmopen(dev_t dev
, int flag
, int fmt
,
327 afmclose(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
330 struct atm_flowmap fmap
;
331 struct afm_head
*head
;
333 for (head
= afhead_chain
.lh_first
; head
!= NULL
;
334 head
= head
->afh_chain
.le_next
) {
336 /* call interface to clean up maps */
337 sprintf(fmap
.af_ifname
, "%s", head
->afh_ifp
->if_xname
);
338 err
= afmioctl(dev
, AFM_CLEANFMAP
, (void *)&fmap
, flag
, l
);
339 if (err
&& error
== 0)
347 afmioctl(dev_t dev
, ioctlcmd_t cmd
, void *addr
, int flag
,
351 struct atm_flowmap
*flowmap
;
354 /* check cmd for superuser only */
359 #if (__FreeBSD_version > 400000)
362 error
= kauth_authorize_network(l
->l_cred
, KAUTH_NETWORK_ALTQ
,
363 KAUTH_REQ_NETWORK_ALTQ_AFMAP
, NULL
, NULL
, NULL
);
370 /* lookup interface */
371 flowmap
= (struct atm_flowmap
*)addr
;
372 flowmap
->af_ifname
[IFNAMSIZ
-1] = '\0';
373 ifp
= ifunit(flowmap
->af_ifname
);
374 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_RUNNING
) == 0)
377 error
= ifp
->if_ioctl(ifp
, cmd
, addr
);
382 #endif /* ALTQ3_COMPAT */
383 #endif /* ALTQ_AFMAP */