/*
* Copyright (C) 1997-2003
* Sony Computer Science Laboratories Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
tbr = ifq->altq_tbr;
if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
/* if this is a remove after poll, bypass tbr check */
} else {
/* update token only when it is negative */
if (tbr->tbr_token <= 0) {
now = read_machclk();
interval = now - tbr->tbr_last;
if (interval >= tbr->tbr_filluptime)
tbr->tbr_token = tbr->tbr_depth;
else {
tbr->tbr_token += interval * tbr->tbr_rate;
if (tbr->tbr_token > tbr->tbr_depth)
tbr->tbr_token = tbr->tbr_depth;
}
tbr->tbr_last = now;
}
/* if token is still negative, don't allow dequeue */
if (tbr->tbr_token <= 0)
return NULL;
}
if (ALTQ_IS_ENABLED(ifq))
m = (*ifq->altq_dequeue)(ifq, op);
else {
if (op == ALTDQ_POLL)
IF_POLL(ifq, m);
else
IF_DEQUEUE(ifq, m);
}
if (m != NULL && op == ALTDQ_REMOVE)
tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
tbr->tbr_lastop = op;
return (m);
}
/*
* set a token bucket regulator.
* if the specified rate is zero, the token bucket regulator is deleted.
*/
int
tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
{
struct tb_regulator *tbr, *otbr;
if (machclk_freq == 0)
init_machclk();
if (machclk_freq == 0) {
printf("tbr_set: no CPU clock available!\n");
return ENXIO;
}
if (profile->rate == 0) {
/* delete this tbr */
if ((tbr = ifq->altq_tbr) == NULL)
return ENOENT;
ifq->altq_tbr = NULL;
free(tbr, M_DEVBUF);
return 0;
}
/*
* tbr_timeout goes through the interface list, and kicks the drivers
* if necessary.
*/
static void
tbr_timeout(void *arg)
{
struct ifnet *ifp;
int active, s;
active = 0;
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifp) {
struct psref psref;
if (!TBR_IS_ENABLED(&ifp->if_snd))
continue;
if_acquire(ifp, &psref);
pserialize_read_exit(s);
active++;
if (!IFQ_IS_EMPTY(&ifp->if_snd) && ifp->if_start != NULL) {
int _s = splnet();
if_start_lock(ifp);
splx(_s);
}
s = pserialize_read_enter();
if_release(ifp, &psref);
}
pserialize_read_exit(s);
if (active > 0)
CALLOUT_RESET(&tbr_callout, 1, tbr_timeout, (void *)0);
else
tbr_timer = 0; /* don't need tbr_timer anymore */
}
/*
* get token bucket regulator profile
*/
int
tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
{
struct tb_regulator *tbr;
#if NPF > 0
/*
* attach a discipline to the interface. if one already exists, it is
* overridden.
*/
int
altq_pfattach(struct pf_altq *a)
{
int error = 0;
switch (a->scheduler) {
case ALTQT_NONE:
break;
#ifdef ALTQ_CBQ
case ALTQT_CBQ:
error = cbq_pfattach(a);
break;
#endif
#ifdef ALTQ_PRIQ
case ALTQT_PRIQ:
error = priq_pfattach(a);
break;
#endif
#ifdef ALTQ_HFSC
case ALTQT_HFSC:
error = hfsc_pfattach(a);
break;
#endif
default:
error = ENXIO;
}
return error;
}
/*
* detach a discipline from the interface.
* it is possible that the discipline was already overridden by another
* discipline.
*/
int
altq_pfdetach(struct pf_altq *a)
{
struct ifnet *ifp;
int s, error = 0;
if ((ifp = ifunit(a->ifname)) == NULL)
return EINVAL;
/* if this discipline is no longer referenced, just return */
if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
return 0;
s = splnet();
if (ALTQ_IS_ENABLED(&ifp->if_snd))
error = altq_disable(&ifp->if_snd);
if (error == 0)
error = altq_detach(&ifp->if_snd);
splx(s);
return error;
}
/*
* add a discipline or a queue
*/
int
altq_add(struct pf_altq *a)
{
int error = 0;
if (a->qname[0] != 0)
return (altq_add_queue(a));
if (machclk_freq == 0)
init_machclk();
if (machclk_freq == 0)
panic("altq_add: no CPU clock");
/*
* Always emulate 1GiHz counter using bintime(9)
* since it has enough resolution via timecounter(9).
* Using machine dependent cpu_counter() is not MP safe
* and it won't work even on UP with Speedstep etc.
*/
machclk_freq = 1024 * 1024 * 1024; /* 2^30 to emulate ~1GHz */
machclk_per_tick = machclk_freq / hz;
#ifdef ALTQ_DEBUG
printf("altq: emulate %uHz CPU clock\n", machclk_freq);
#endif
}
/*
* extract flow information from a given packet.
* filt_mask shows flowinfo fields required.
* we assume the ip header is in one mbuf, and addresses and ports are
* in network byte order.
*/
int
altq_extractflow(struct mbuf *m, int af, struct flowinfo *flow,
u_int32_t filt_bmask)
{
switch (af) {
case PF_INET: {
struct flowinfo_in *fin;
struct ip *ip;
if ((filt_bmask & FIMB6_PORTS) ||
((filt_bmask & FIMB6_PROTO)
&& ip6->ip6_nxt > IPPROTO_IPV6))
/*
* if port info is required, or proto is required
* but there are option headers, extract port
* and protocol numbers.
*/
extract_ports6(m, ip6, fin6);
else {
fin6->fi6_sport = 0;
fin6->fi6_dport = 0;
fin6->fi6_gpi = 0;
}
return 1;
}
#endif /* INET6 */
/* locate the mbuf containing the protocol header */
for (m0 = m; m0 != NULL; m0 = m0->m_next)
if (((char *)ip6 >= m0->m_data) &&
((char *)ip6 < m0->m_data + m0->m_len))
break;
if (m0 == NULL) {
#ifdef ALTQ_DEBUG
printf("extract_ports6: can't locate header! ip6=%p\n", ip6);
#endif
return 0;
}
off = ((char *)ip6 - m0->m_data) + sizeof(struct ip6_hdr);
proto = ip6->ip6_nxt;
do {
while (off >= m0->m_len) {
off -= m0->m_len;
m0 = m0->m_next;
if (m0 == NULL)
return 0;
}
if (m0->m_len < off + 4)
return 0;
switch (proto) {
case IPPROTO_TCP:
case IPPROTO_UDP: {
struct udphdr *udp;
i = ACC_WILDCARD_INDEX;
if (filter->ff_flow.fi_family == AF_INET) {
struct flow_filter *filter4 = &afp->f_filter;
/*
* if address is 0, it's a wildcard. if address mask
* isn't set, use full mask.
*/
if (filter4->ff_flow.fi_dst.s_addr == 0)
filter4->ff_mask.mask_dst.s_addr = 0;
else if (filter4->ff_mask.mask_dst.s_addr == 0)
filter4->ff_mask.mask_dst.s_addr = 0xffffffff;
if (filter4->ff_flow.fi_src.s_addr == 0)
filter4->ff_mask.mask_src.s_addr = 0;
else if (filter4->ff_mask.mask_src.s_addr == 0)
filter4->ff_mask.mask_src.s_addr = 0xffffffff;
/* clear extra bits in addresses */
filter4->ff_flow.fi_dst.s_addr &=
filter4->ff_mask.mask_dst.s_addr;
filter4->ff_flow.fi_src.s_addr &=
filter4->ff_mask.mask_src.s_addr;
/*
* if dst address is a wildcard, use hash-entry
* ACC_WILDCARD_INDEX.
*/
if (filter4->ff_mask.mask_dst.s_addr != 0xffffffff)
i = ACC_WILDCARD_INDEX;
else
i = ACC_GET_HASH_INDEX(filter4->ff_flow.fi_dst.s_addr);
}
#ifdef INET6
else if (filter->ff_flow.fi_family == AF_INET6) {
struct flow_filter6 *filter6 =
(struct flow_filter6 *)&afp->f_filter;
#ifndef IN6MASK0 /* taken from kame ipv6 */
#define IN6MASK0 {{{ 0, 0, 0, 0 }}}
#define IN6MASK128 {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}}
const struct in6_addr in6mask0 = IN6MASK0;
const struct in6_addr in6mask128 = IN6MASK128;
#endif
if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_dst))
filter6->ff_mask6.mask6_dst = in6mask0;
else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_dst))
filter6->ff_mask6.mask6_dst = in6mask128;
if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_flow6.fi6_src))
filter6->ff_mask6.mask6_src = in6mask0;
else if (IN6_IS_ADDR_UNSPECIFIED(&filter6->ff_mask6.mask6_src))
filter6->ff_mask6.mask6_src = in6mask128;
/* clear extra bits in addresses */
for (i = 0; i < 16; i++)
filter6->ff_flow6.fi6_dst.s6_addr[i] &=
filter6->ff_mask6.mask6_dst.s6_addr[i];
for (i = 0; i < 16; i++)
filter6->ff_flow6.fi6_src.s6_addr[i] &=
filter6->ff_mask6.mask6_src.s6_addr[i];
if (filter6->ff_flow6.fi6_flowlabel == 0)
i = ACC_WILDCARD_INDEX;
else
i = ACC_GET_HASH_INDEX(filter6->ff_flow6.fi6_flowlabel);
}
#endif /* INET6 */
/*
* add this filter to the filter list.
* filters are ordered from the highest rule number.
*/
s = splnet();
prev = NULL;
LIST_FOREACH(tmp, &classifier->acc_filters[i], f_chain) {
if (tmp->f_filter.ff_ruleno > afp->f_filter.ff_ruleno)
prev = tmp;
else
break;
}
if (prev == NULL)
LIST_INSERT_HEAD(&classifier->acc_filters[i], afp, f_chain);
else
LIST_INSERT_AFTER(prev, afp, f_chain);
splx(s);
*phandle = afp->f_handle;
return 0;
}
int
acc_delete_filter(struct acc_classifier *classifier, u_long handle)
{
struct acc_filter *afp;
int s;
if ((afp = filth_to_filtp(classifier, handle)) == NULL)
return EINVAL;
s = splnet();
LIST_REMOVE(afp, f_chain);
splx(s);
free(afp, M_DEVBUF);
/* todo: update filt_bmask */
return 0;
}
/*
* delete filters referencing to the specified class.
* if the all flag is not 0, delete all the filters.
*/
int
acc_discard_filters(struct acc_classifier *classifier, void *class, int all)
{
struct acc_filter *afp;
int i, s;
s = splnet();
for (i = 0; i < ACC_FILTER_TABLESIZE; i++) {
do {
LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
if (all || afp->f_class == class) {
LIST_REMOVE(afp, f_chain);
free(afp, M_DEVBUF);
/* start again from the head */
break;
}
} while (afp != NULL);
}
splx(s);
if ((classifier->acc_fbmask & FIMB4_ALL) == FIMB4_TOS) {
/* only tos is used */
LIST_FOREACH(afp,
&classifier->acc_filters[ACC_WILDCARD_INDEX],
f_chain)
if (apply_tosfilter4(afp->f_fbmask,
&afp->f_filter, fp))
/* filter matched */
return (afp->f_class);
} else if ((classifier->acc_fbmask &
(~(FIMB4_PROTO|FIMB4_SPORT|FIMB4_DPORT) & FIMB4_ALL))
== 0) {
/* only proto and ports are used */
LIST_FOREACH(afp,
&classifier->acc_filters[ACC_WILDCARD_INDEX],
f_chain)
if (apply_ppfilter4(afp->f_fbmask,
&afp->f_filter, fp))
/* filter matched */
return (afp->f_class);
} else {
/* get the filter hash entry from its dest address */
i = ACC_GET_HASH_INDEX(fp->fi_dst.s_addr);
do {
/*
* go through this loop twice. first for dst
* hash, second for wildcards.
*/
LIST_FOREACH(afp, &classifier->acc_filters[i],
f_chain)
if (apply_filter4(afp->f_fbmask,
&afp->f_filter, fp))
/* filter matched */
return (afp->f_class);
/*
* check again for filters with a dst addr
* wildcard.
* (daddr == 0 || dmask != 0xffffffff).
*/
if (i != ACC_WILDCARD_INDEX)
i = ACC_WILDCARD_INDEX;
else
break;
} while (1);
}
}
#ifdef INET6
else if (flow.fi_family == AF_INET6) {
struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)&flow;
/* get the filter hash entry from its flow ID */
if (fp6->fi6_flowlabel != 0)
i = ACC_GET_HASH_INDEX(fp6->fi6_flowlabel);
else
/* flowlable can be zero */
i = ACC_WILDCARD_INDEX;
/* go through this loop twice. first for flow hash, second
for wildcards. */
do {
LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
if (apply_filter6(afp->f_fbmask,
(struct flow_filter6 *)&afp->f_filter,
fp6))
/* filter matched */
return (afp->f_class);
/*
* check again for filters with a wildcard.
*/
if (i != ACC_WILDCARD_INDEX)
i = ACC_WILDCARD_INDEX;
else
break;
} while (1);
}
#endif /* INET6 */
/* no filter matched */
return NULL;
}
static int
apply_filter4(u_int32_t fbmask, struct flow_filter *filt,
struct flowinfo_in *pkt)
{
if (filt->ff_flow.fi_family != AF_INET)
return 0;
if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
return 0;
if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
return 0;
if ((fbmask & FIMB4_DADDR) &&
filt->ff_flow.fi_dst.s_addr !=
(pkt->fi_dst.s_addr & filt->ff_mask.mask_dst.s_addr))
return 0;
if ((fbmask & FIMB4_SADDR) &&
filt->ff_flow.fi_src.s_addr !=
(pkt->fi_src.s_addr & filt->ff_mask.mask_src.s_addr))
return 0;
if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
return 0;
if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
(pkt->fi_tos & filt->ff_mask.mask_tos))
return 0;
if ((fbmask & FIMB4_GPI) && filt->ff_flow.fi_gpi != (pkt->fi_gpi))
return 0;
/* match */
return 1;
}
/*
* filter matching function optimized for a common case that checks
* only protocol and port numbers
*/
static int
apply_ppfilter4(u_int32_t fbmask, struct flow_filter *filt,
struct flowinfo_in *pkt)
{
if (filt->ff_flow.fi_family != AF_INET)
return 0;
if ((fbmask & FIMB4_SPORT) && filt->ff_flow.fi_sport != pkt->fi_sport)
return 0;
if ((fbmask & FIMB4_DPORT) && filt->ff_flow.fi_dport != pkt->fi_dport)
return 0;
if ((fbmask & FIMB4_PROTO) && filt->ff_flow.fi_proto != pkt->fi_proto)
return 0;
/* match */
return 1;
}
/*
* filter matching function only for tos field.
*/
static int
apply_tosfilter4(u_int32_t fbmask, struct flow_filter *filt,
struct flowinfo_in *pkt)
{
if (filt->ff_flow.fi_family != AF_INET)
return 0;
if ((fbmask & FIMB4_TOS) && filt->ff_flow.fi_tos !=
(pkt->fi_tos & filt->ff_mask.mask_tos))
return 0;
/* match */
return 1;
}
#ifdef INET6
static int
apply_filter6(u_int32_t fbmask, struct flow_filter6 *filt,
struct flowinfo_in6 *pkt)
{
int i;
if (filt->ff_flow6.fi6_family != AF_INET6)
return 0;
if ((fbmask & FIMB6_FLABEL) &&
filt->ff_flow6.fi6_flowlabel != pkt->fi6_flowlabel)
return 0;
if ((fbmask & FIMB6_PROTO) &&
filt->ff_flow6.fi6_proto != pkt->fi6_proto)
return 0;
if ((fbmask & FIMB6_SPORT) &&
filt->ff_flow6.fi6_sport != pkt->fi6_sport)
return 0;
if ((fbmask & FIMB6_DPORT) &&
filt->ff_flow6.fi6_dport != pkt->fi6_dport)
return 0;
if (fbmask & FIMB6_SADDR) {
for (i = 0; i < 4; i++)
if (filt->ff_flow6.fi6_src.s6_addr32[i] !=
(pkt->fi6_src.s6_addr32[i] &
filt->ff_mask6.mask6_src.s6_addr32[i]))
return 0;
}
if (fbmask & FIMB6_DADDR) {
for (i = 0; i < 4; i++)
if (filt->ff_flow6.fi6_dst.s6_addr32[i] !=
(pkt->fi6_dst.s6_addr32[i] &
filt->ff_mask6.mask6_dst.s6_addr32[i]))
return 0;
}
if ((fbmask & FIMB6_TCLASS) &&
filt->ff_flow6.fi6_tclass !=
(pkt->fi6_tclass & filt->ff_mask6.mask6_tclass))
return 0;
if ((fbmask & FIMB6_GPI) &&
filt->ff_flow6.fi6_gpi != pkt->fi6_gpi)
return 0;
/* match */
return 1;
}
#endif /* INET6 */
/*
* filter handle:
* bit 20-28: index to the filter hash table
* bit 0-19: unique id in the hash bucket.
*/
static u_long
get_filt_handle(struct acc_classifier *classifier, int i)
{
static u_long handle_number = 1;
u_long handle;
struct acc_filter *afp;
while (1) {
handle = handle_number++ & 0x000fffff;
if (LIST_EMPTY(&classifier->acc_filters[i]))
break;
LIST_FOREACH(afp, &classifier->acc_filters[i], f_chain)
if ((afp->f_handle & 0x000fffff) == handle)
break;
if (afp == NULL)
break;
/* this handle is already used, try again */
}
switch (filt->ff_flow.fi_family) {
case AF_INET:
if (filt->ff_flow.fi_proto != 0)
mask |= FIMB4_PROTO;
if (filt->ff_flow.fi_tos != 0)
mask |= FIMB4_TOS;
if (filt->ff_flow.fi_dst.s_addr != 0)
mask |= FIMB4_DADDR;
if (filt->ff_flow.fi_src.s_addr != 0)
mask |= FIMB4_SADDR;
if (filt->ff_flow.fi_sport != 0)
mask |= FIMB4_SPORT;
if (filt->ff_flow.fi_dport != 0)
mask |= FIMB4_DPORT;
if (filt->ff_flow.fi_gpi != 0)
mask |= FIMB4_GPI;
break;
#ifdef INET6
case AF_INET6:
filt6 = (struct flow_filter6 *)filt;
if (filt6->ff_flow6.fi6_proto != 0)
mask |= FIMB6_PROTO;
if (filt6->ff_flow6.fi6_tclass != 0)
mask |= FIMB6_TCLASS;
if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_dst))
mask |= FIMB6_DADDR;
if (!IN6_IS_ADDR_UNSPECIFIED(&filt6->ff_flow6.fi6_src))
mask |= FIMB6_SADDR;
if (filt6->ff_flow6.fi6_sport != 0)
mask |= FIMB6_SPORT;
if (filt6->ff_flow6.fi6_dport != 0)
mask |= FIMB6_DPORT;
if (filt6->ff_flow6.fi6_gpi != 0)
mask |= FIMB6_GPI;
if (filt6->ff_flow6.fi6_flowlabel != 0)
mask |= FIMB6_FLABEL;
break;
#endif /* INET6 */
}
return mask;
}
/*
* helper functions to handle IPv4 fragments.
* currently only in-sequence fragments are handled.
* - fragment info is cached in a LRU list.
* - when a first fragment is found, cache its flow info.
* - when a non-first fragment is found, lookup the cache.
*/
/* reclaim an entry at the tail, put it at the head */
fp = TAILQ_LAST(&ip4f_list, ip4f_list);
TAILQ_REMOVE(&ip4f_list, fp, ip4f_chain);
fp->ip4f_valid = 1;
TAILQ_INSERT_HEAD(&ip4f_list, fp, ip4f_chain);
return fp;
}