NetBSD/sys/altq/altq_afmap.c

385 lines
9.0 KiB
C
Raw Normal View History

2014-03-21 00:51:40 +04:00
/* $NetBSD: altq_afmap.c,v 1.20 2014/03/20 20:51:54 christos Exp $ */
/* $KAME: altq_afmap.c,v 1.12 2005/04/13 03:44:24 suz Exp $ */
2000-12-14 11:42:28 +03:00
/*
* Copyright (C) 1997-2002
2000-12-14 11:42:28 +03:00
* 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.
*/
/*
* experimental:
* mapping an ip flow to atm vpi/vci.
* this module is not related to queueing at all, but uses the altq
* flowinfo mechanism. it's just put in the altq framework since
* it is easy to add devices to altq.
*/
2001-11-13 02:08:56 +03:00
#include <sys/cdefs.h>
2014-03-21 00:51:40 +04:00
__KERNEL_RCSID(0, "$NetBSD: altq_afmap.c,v 1.20 2014/03/20 20:51:54 christos Exp $");
2001-11-13 02:08:56 +03:00
2004-04-21 22:40:37 +04:00
#ifdef _KERNEL_OPT
2000-12-14 11:42:28 +03:00
#include "opt_altq.h"
#include "opt_inet.h"
#endif
#ifdef ALTQ_AFMAP
2000-12-14 11:42:28 +03:00
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kernel.h>
2006-05-15 04:05:16 +04:00
#include <sys/kauth.h>
2000-12-14 11:42:28 +03:00
#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <altq/altq.h>
#include <altq/altq_conf.h>
#include <altq/altq_afmap.h>
#ifdef ALTQ3_COMPAT
2000-12-14 11:42:28 +03:00
LIST_HEAD(, afm_head) afhead_chain;
static struct afm *afm_match4(struct afm_head *, struct flowinfo_in *);
2000-12-14 11:42:28 +03:00
#ifdef INET6
static struct afm *afm_match6(struct afm_head *, struct flowinfo_in6 *);
2000-12-14 11:42:28 +03:00
#endif
/*
* rules to block interrupts: afm_match can be called from a net
* level interrupt so that other routines handling the lists should
* be called in splnet().
*/
int
afm_alloc(struct ifnet *ifp)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
2005-02-27 01:58:54 +03:00
head = malloc(sizeof(struct afm_head), M_DEVBUF, M_WAITOK|M_ZERO);
2000-12-14 11:42:28 +03:00
if (head == NULL)
panic("afm_alloc: malloc failed!");
/* initialize per interface afmap list */
LIST_INIT(&head->afh_head);
head->afh_ifp = ifp;
2005-02-27 01:58:54 +03:00
2000-12-14 11:42:28 +03:00
/* add this afm_head to the chain */
LIST_INSERT_HEAD(&afhead_chain, head, afh_chain);
2005-02-27 01:58:54 +03:00
2000-12-14 11:42:28 +03:00
return (0);
}
int
afm_dealloc(struct ifnet *ifp)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next)
if (head->afh_ifp == ifp)
break;
if (head == NULL)
return (-1);
afm_removeall(ifp);
LIST_REMOVE(head, afh_chain);
free(head, M_DEVBUF);
2000-12-14 11:42:28 +03:00
return 0;
}
struct afm *
afm_top(struct ifnet *ifp)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next)
if (head->afh_ifp == ifp)
break;
if (head == NULL)
return NULL;
2005-02-27 01:58:54 +03:00
2000-12-14 11:42:28 +03:00
return (head->afh_head.lh_first);
}
int
afm_add(struct ifnet *ifp, struct atm_flowmap *flowmap)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
struct afm *afm;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next)
if (head->afh_ifp == ifp)
break;
if (head == NULL)
return (-1);
if (flowmap->af_flowinfo.fi_family == AF_INET) {
if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in))
return (EINVAL);
#ifdef INET6
} else if (flowmap->af_flowinfo.fi_family == AF_INET6) {
if (flowmap->af_flowinfo.fi_len != sizeof(struct flowinfo_in6))
return (EINVAL);
#endif
} else
return (EINVAL);
afm = malloc(sizeof(struct afm), M_DEVBUF, M_WAITOK|M_ZERO);
2000-12-14 11:42:28 +03:00
if (afm == NULL)
return (ENOMEM);
afm->afm_vci = flowmap->af_vci;
afm->afm_vpi = flowmap->af_vpi;
(void)memcpy(&afm->afm_flowinfo, &flowmap->af_flowinfo,
2000-12-14 11:42:28 +03:00
flowmap->af_flowinfo.fi_len);
LIST_INSERT_HEAD(&head->afh_head, afm, afm_list);
return 0;
}
2005-02-27 01:58:54 +03:00
int
afm_remove(struct afm *afm)
2000-12-14 11:42:28 +03:00
{
LIST_REMOVE(afm, afm_list);
free(afm, M_DEVBUF);
2000-12-14 11:42:28 +03:00
return (0);
}
2005-02-27 01:58:54 +03:00
int
afm_removeall(struct ifnet *ifp)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
struct afm *afm;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next)
if (head->afh_ifp == ifp)
break;
if (head == NULL)
return (-1);
while ((afm = head->afh_head.lh_first) != NULL)
afm_remove(afm);
return (0);
}
struct afm *
afm_lookup(struct ifnet *ifp, int vpi, int vci)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
struct afm *afm;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next)
if (head->afh_ifp == ifp)
break;
if (head == NULL)
return NULL;
for (afm = head->afh_head.lh_first; afm != NULL;
afm = afm->afm_list.le_next)
if (afm->afm_vpi == vpi && afm->afm_vci == vci)
break;
return afm;
}
static struct afm *
afm_match4(struct afm_head *head, struct flowinfo_in *fp)
2000-12-14 11:42:28 +03:00
{
struct afm *afm;
for (afm = head->afh_head.lh_first; afm != NULL;
afm = afm->afm_list.le_next) {
if (afm->afm_flowinfo4.fi_dst.s_addr != 0 &&
afm->afm_flowinfo4.fi_dst.s_addr != fp->fi_dst.s_addr)
continue;
if (afm->afm_flowinfo4.fi_dport != 0 &&
afm->afm_flowinfo4.fi_dport != fp->fi_dport)
continue;
if (afm->afm_flowinfo4.fi_src.s_addr != 0 &&
afm->afm_flowinfo4.fi_src.s_addr != fp->fi_src.s_addr)
continue;
if (afm->afm_flowinfo4.fi_sport != 0 &&
afm->afm_flowinfo4.fi_sport != fp->fi_sport)
continue;
if (afm->afm_flowinfo4.fi_proto != 0 &&
afm->afm_flowinfo4.fi_proto != fp->fi_proto)
continue;
/* match found! */
return (afm);
}
return NULL;
}
#ifdef INET6
static struct afm *
afm_match6(struct afm_head *head, struct flowinfo_in6 *fp)
2000-12-14 11:42:28 +03:00
{
struct afm *afm;
for (afm = head->afh_head.lh_first; afm != NULL;
afm = afm->afm_list.le_next) {
if (afm->afm_flowinfo6.fi6_flowlabel != 0 &&
afm->afm_flowinfo6.fi6_flowlabel != fp->fi6_flowlabel)
continue;
#ifdef notyet
if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_dst) &&
!IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_dst,
&fp->fi6_dst))
continue;
if (afm->afm_flowinfo6.fi6_dport != 0 &&
afm->afm_flowinfo6.fi6_dport != fp->fi6_dport)
continue;
#endif
if (!IN6_IS_ADDR_UNSPECIFIED(&afm->afm_flowinfo6.fi6_src) &&
!IN6_ARE_ADDR_EQUAL(&afm->afm_flowinfo6.fi6_src,
&fp->fi6_src))
continue;
#ifdef notyet
if (afm->afm_flowinfo6.fi6_sport != 0 &&
afm->afm_flowinfo6.fi6_sport != fp->fi6_sport)
continue;
#endif
if (afm->afm_flowinfo6.fi6_proto != 0 &&
afm->afm_flowinfo6.fi6_proto != fp->fi6_proto)
continue;
/* match found! */
return (afm);
}
return NULL;
}
#endif
/* should be called in splnet() */
2000-12-14 11:42:28 +03:00
struct afm *
afm_match(struct ifnet *ifp, struct flowinfo *flow)
2000-12-14 11:42:28 +03:00
{
struct afm_head *head;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next)
if (head->afh_ifp == ifp)
break;
if (head == NULL)
return NULL;
switch (flow->fi_family) {
case AF_INET:
return (afm_match4(head, (struct flowinfo_in *)flow));
#ifdef INET6
case AF_INET6:
return (afm_match6(head, (struct flowinfo_in6 *)flow));
#endif
default:
return NULL;
}
}
/*
* afm device interface
*/
altqdev_decl(afm);
int
afmopen(dev_t dev, int flag, int fmt,
struct lwp *l)
2000-12-14 11:42:28 +03:00
{
return 0;
}
int
afmclose(dev_t dev, int flag, int fmt, struct lwp *l)
2000-12-14 11:42:28 +03:00
{
int err, error = 0;
struct atm_flowmap fmap;
struct afm_head *head;
for (head = afhead_chain.lh_first; head != NULL;
head = head->afh_chain.le_next) {
/* call interface to clean up maps */
2014-03-21 00:51:40 +04:00
snprintf(fmap.af_ifname, sizeof(fmap.af_ifname),
"%s", head->afh_ifp->if_xname);
err = afmioctl(dev, AFM_CLEANFMAP, (void *)&fmap, flag, l);
2000-12-14 11:42:28 +03:00
if (err && error == 0)
error = err;
}
return error;
}
int
afmioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
struct lwp *l)
2000-12-14 11:42:28 +03:00
{
int error = 0;
struct atm_flowmap *flowmap;
struct ifnet *ifp;
/* check cmd for superuser only */
switch (cmd) {
case AFM_GETFMAP:
break;
default:
#if (__FreeBSD_version > 400000)
error = suser(p);
#else
2006-10-21 01:55:56 +04:00
error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_ALTQ,
KAUTH_REQ_NETWORK_ALTQ_AFMAP, NULL, NULL, NULL);
2000-12-14 11:42:28 +03:00
#endif
if (error)
return (error);
break;
}
/* lookup interface */
flowmap = (struct atm_flowmap *)addr;
2005-02-27 01:58:54 +03:00
flowmap->af_ifname[IFNAMSIZ-1] = '\0';
2000-12-14 11:42:28 +03:00
ifp = ifunit(flowmap->af_ifname);
*** Summary *** When a link-layer address changes (e.g., ifconfig ex0 link 02:de:ad:be:ef:02 active), send a gratuitous ARP and/or a Neighbor Advertisement to update the network-/link-layer address bindings on our LAN peers. Refuse a change of ethernet address to the address 00:00:00:00:00:00 or to any multicast/broadcast address. (Thanks matt@.) Reorder ifnet ioctl operations so that driver ioctls may inherit the functions of their "class"---ether_ioctl(), fddi_ioctl(), et cetera---and the class ioctls may inherit from the generic ioctl, ifioctl_common(), but both driver- and class-ioctls may override the generic behavior. Make network drivers share more code. Distinguish a "factory" link-layer address from others for the purposes of both protecting that address from deletion and computing EUI64. Return consistent, appropriate error codes from network drivers. Improve readability. KNF. *** Details *** In if_attach(), always initialize the interface ioctl routine, ifnet->if_ioctl, if the driver has not already initialized it. Delete if_ioctl == NULL tests everywhere else, because it cannot happen. In the ioctl routines of network interfaces, inherit common ioctl behaviors by calling either ifioctl_common() or whichever ioctl routine is appropriate for the class of interface---e.g., ether_ioctl() for ethernets. Stop (ab)using SIOCSIFADDR and start to use SIOCINITIFADDR. In the user->kernel interface, SIOCSIFADDR's argument was an ifreq, but on the protocol->ifnet interface, SIOCSIFADDR's argument was an ifaddr. That was confusing, and it would work against me as I make it possible for a network interface to overload most ioctls. On the protocol->ifnet interface, replace SIOCSIFADDR with SIOCINITIFADDR. In ifioctl(), return EPERM if userland tries to invoke SIOCINITIFADDR. In ifioctl(), give the interface the first shot at handling most interface ioctls, and give the protocol the second shot, instead of the other way around. Finally, let compatibility code (COMPAT_OSOCK) take a shot. Pull device initialization out of switch statements under SIOCINITIFADDR. For example, pull ..._init() out of any switch statement that looks like this: switch (...->sa_family) { case ...: ..._init(); ... break; ... default: ..._init(); ... break; } Rewrite many if-else clauses that handle all permutations of IFF_UP and IFF_RUNNING to use a switch statement, switch (x & (IFF_UP|IFF_RUNNING)) { case 0: ... break; case IFF_RUNNING: ... break; case IFF_UP: ... break; case IFF_UP|IFF_RUNNING: ... break; } unifdef lots of code containing #ifdef FreeBSD, #ifdef NetBSD, and #ifdef SIOCSIFMTU, especially in fwip(4) and in ndis(4). In ipw(4), remove an if_set_sadl() call that is out of place. In nfe(4), reuse the jumbo MTU logic in ether_ioctl(). Let ethernets register a callback for setting h/w state such as promiscuous mode and the multicast filter in accord with a change in the if_flags: ether_set_ifflags_cb() registers a callback that returns ENETRESET if the caller should reset the ethernet by calling if_init(), 0 on success, != 0 on failure. Pull common code from ex(4), gem(4), nfe(4), sip(4), tlp(4), vge(4) into ether_ioctl(), and register if_flags callbacks for those drivers. Return ENOTTY instead of EINVAL for inappropriate ioctls. In zyd(4), use ENXIO instead of ENOTTY to indicate that the device is not any longer attached. Add to if_set_sadl() a boolean 'factory' argument that indicates whether a link-layer address was assigned by the factory or some other source. In a comment, recommend using the factory address for generating an EUI64, and update in6_get_hw_ifid() to prefer a factory address to any other link-layer address. Add a routing message, RTM_LLINFO_UPD, that tells protocols to update the binding of network-layer addresses to link-layer addresses. Implement this message in IPv4 and IPv6 by sending a gratuitous ARP or a neighbor advertisement, respectively. Generate RTM_LLINFO_UPD messages on a change of an interface's link-layer address. In ether_ioctl(), do not let SIOCALIFADDR set a link-layer address that is broadcast/multicast or equal to 00:00:00:00:00:00. Make ether_ioctl() call ifioctl_common() to handle ioctls that it does not understand. In gif(4), initialize if_softc and use it, instead of assuming that the gif_softc and ifp overlap. Let ifioctl_common() handle SIOCGIFADDR. Sprinkle rtcache_invariants(), which checks on DIAGNOSTIC kernels that certain invariants on a struct route are satisfied. In agr(4), rewrite agr_ioctl_filter() to be a bit more explicit about the ioctls that we do not allow on an agr(4) member interface. bzero -> memset. Delete unnecessary casts to void *. Use sockaddr_in_init() and sockaddr_in6_init(). Compare pointers with NULL instead of "testing truth". Replace some instances of (type *)0 with NULL. Change some K&R prototypes to ANSI C, and join lines.
2008-11-07 03:20:01 +03:00
if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0)
2000-12-14 11:42:28 +03:00
error = ENXIO;
else
error = ifp->if_ioctl(ifp, cmd, addr);
return error;
}
#endif /* ALTQ3_COMPAT */
#endif /* ALTQ_AFMAP */