/*-1* ng_etf.c Ethertype filter2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2001, FreeBSD Incorporated8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice unmodified, this list of conditions, and the following15* disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19*20* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*32* Author: Julian Elischer <[email protected]>33*/3435#include <sys/param.h>36#include <sys/systm.h>37#include <sys/kernel.h>38#include <sys/mbuf.h>39#include <sys/malloc.h>40#include <sys/ctype.h>41#include <sys/errno.h>42#include <sys/queue.h>43#include <sys/syslog.h>4445#include <net/ethernet.h>4647#include <netgraph/ng_message.h>48#include <netgraph/ng_parse.h>49#include <netgraph/ng_etf.h>50#include <netgraph/netgraph.h>5152/* If you do complicated mallocs you may want to do this */53/* and use it for your mallocs */54#ifdef NG_SEPARATE_MALLOC55static MALLOC_DEFINE(M_NETGRAPH_ETF, "netgraph_etf", "netgraph etf node ");56#else57#define M_NETGRAPH_ETF M_NETGRAPH58#endif5960/*61* This section contains the netgraph method declarations for the62* etf node. These methods define the netgraph 'type'.63*/6465static ng_constructor_t ng_etf_constructor;66static ng_rcvmsg_t ng_etf_rcvmsg;67static ng_shutdown_t ng_etf_shutdown;68static ng_newhook_t ng_etf_newhook;69static ng_rcvdata_t ng_etf_rcvdata; /* note these are both ng_rcvdata_t */70static ng_disconnect_t ng_etf_disconnect;7172/* Parse type for struct ng_etfstat */73static const struct ng_parse_struct_field ng_etf_stat_type_fields[]74= NG_ETF_STATS_TYPE_INFO;75static const struct ng_parse_type ng_etf_stat_type = {76&ng_parse_struct_type,77&ng_etf_stat_type_fields78};79/* Parse type for struct ng_setfilter */80static const struct ng_parse_struct_field ng_etf_filter_type_fields[]81= NG_ETF_FILTER_TYPE_INFO;82static const struct ng_parse_type ng_etf_filter_type = {83&ng_parse_struct_type,84&ng_etf_filter_type_fields85};8687/* List of commands and how to convert arguments to/from ASCII */88static const struct ng_cmdlist ng_etf_cmdlist[] = {89{90NGM_ETF_COOKIE,91NGM_ETF_GET_STATUS,92"getstatus",93NULL,94&ng_etf_stat_type,95},96{97NGM_ETF_COOKIE,98NGM_ETF_SET_FLAG,99"setflag",100&ng_parse_int32_type,101NULL102},103{104NGM_ETF_COOKIE,105NGM_ETF_SET_FILTER,106"setfilter",107&ng_etf_filter_type,108NULL109},110{ 0 }111};112113/* Netgraph node type descriptor */114static struct ng_type typestruct = {115.version = NG_ABI_VERSION,116.name = NG_ETF_NODE_TYPE,117.constructor = ng_etf_constructor,118.rcvmsg = ng_etf_rcvmsg,119.shutdown = ng_etf_shutdown,120.newhook = ng_etf_newhook,121.rcvdata = ng_etf_rcvdata,122.disconnect = ng_etf_disconnect,123.cmdlist = ng_etf_cmdlist,124};125NETGRAPH_INIT(etf, &typestruct);126127/* Information we store for each hook on each node */128struct ETF_hookinfo {129hook_p hook;130};131132struct filter {133LIST_ENTRY(filter) next;134u_int16_t ethertype; /* network order ethertype */135hook_p match_hook; /* Hook to use on a match */136};137138#define HASHSIZE 16 /* Dont change this without changing HASH() */139#define HASH(et) ((((et)>>12)+((et)>>8)+((et)>>4)+(et)) & 0x0f)140LIST_HEAD(filterhead, filter);141142/* Information we store for each node */143struct ETF {144struct ETF_hookinfo downstream_hook;145struct ETF_hookinfo nomatch_hook;146node_p node; /* back pointer to node */147u_int packets_in; /* packets in from downstream */148u_int packets_out; /* packets out towards downstream */149u_int32_t flags;150struct filterhead hashtable[HASHSIZE];151};152typedef struct ETF *etf_p;153154static struct filter *155ng_etf_findentry(etf_p etfp, u_int16_t ethertype)156{157struct filterhead *chain = etfp->hashtable + HASH(ethertype);158struct filter *fil;159160LIST_FOREACH(fil, chain, next) {161if (fil->ethertype == ethertype) {162return (fil);163}164}165return (NULL);166}167168/*169* Allocate the private data structure. The generic node has already170* been created. Link them together. We arrive with a reference to the node171* i.e. the reference count is incremented for us already.172*/173static int174ng_etf_constructor(node_p node)175{176etf_p privdata;177int i;178179/* Initialize private descriptor */180privdata = malloc(sizeof(*privdata), M_NETGRAPH_ETF, M_WAITOK | M_ZERO);181for (i = 0; i < HASHSIZE; i++) {182LIST_INIT((privdata->hashtable + i));183}184185/* Link structs together; this counts as our one reference to node */186NG_NODE_SET_PRIVATE(node, privdata);187privdata->node = node;188return (0);189}190191/*192* Give our ok for a hook to be added...193* All names are ok. Two names are special.194*/195static int196ng_etf_newhook(node_p node, hook_p hook, const char *name)197{198const etf_p etfp = NG_NODE_PRIVATE(node);199struct ETF_hookinfo *hpriv;200201if (strcmp(name, NG_ETF_HOOK_DOWNSTREAM) == 0) {202etfp->downstream_hook.hook = hook;203NG_HOOK_SET_PRIVATE(hook, &etfp->downstream_hook);204etfp->packets_in = 0;205etfp->packets_out = 0;206} else if (strcmp(name, NG_ETF_HOOK_NOMATCH) == 0) {207etfp->nomatch_hook.hook = hook;208NG_HOOK_SET_PRIVATE(hook, &etfp->nomatch_hook);209} else {210/*211* Any other hook name is valid and can212* later be associated with a filter rule.213*/214hpriv = malloc(sizeof(*hpriv),215M_NETGRAPH_ETF, M_NOWAIT | M_ZERO);216if (hpriv == NULL) {217return (ENOMEM);218}219220NG_HOOK_SET_PRIVATE(hook, hpriv);221hpriv->hook = hook;222}223return(0);224}225226/*227* Get a netgraph control message.228* We actually receive a queue item that has a pointer to the message.229* If we free the item, the message will be freed too, unless we remove230* it from the item using NGI_GET_MSG();231* The return address is also stored in the item, as an ng_ID_t,232* accessible as NGI_RETADDR(item);233* Check it is one we understand. If needed, send a response.234* We could save the address for an async action later, but don't here.235* Always free the message.236* The response should be in a malloc'd region that the caller can 'free'.237* The NG_MKRESPONSE macro does all this for us.238* A response is not required.239* Theoretically you could respond defferently to old message types if240* the cookie in the header didn't match what we consider to be current241* (so that old userland programs could continue to work).242*/243static int244ng_etf_rcvmsg(node_p node, item_p item, hook_p lasthook)245{246const etf_p etfp = NG_NODE_PRIVATE(node);247struct ng_mesg *resp = NULL;248int error = 0;249struct ng_mesg *msg;250251NGI_GET_MSG(item, msg);252/* Deal with message according to cookie and command */253switch (msg->header.typecookie) {254case NGM_ETF_COOKIE:255switch (msg->header.cmd) {256case NGM_ETF_GET_STATUS:257{258struct ng_etfstat *stats;259260NG_MKRESPONSE(resp, msg, sizeof(*stats), M_NOWAIT);261if (!resp) {262error = ENOMEM;263break;264}265stats = (struct ng_etfstat *) resp->data;266stats->packets_in = etfp->packets_in;267stats->packets_out = etfp->packets_out;268break;269}270case NGM_ETF_SET_FLAG:271if (msg->header.arglen != sizeof(u_int32_t)) {272error = EINVAL;273break;274}275etfp->flags = *((u_int32_t *) msg->data);276break;277case NGM_ETF_SET_FILTER:278{279struct ng_etffilter *f;280struct filter *fil;281hook_p hook;282283/* Check message long enough for this command */284if (msg->header.arglen != sizeof(*f)) {285error = EINVAL;286break;287}288289/* Make sure hook referenced exists */290f = (struct ng_etffilter *)msg->data;291hook = ng_findhook(node, f->matchhook);292if (hook == NULL) {293error = ENOENT;294break;295}296297/* and is not the downstream hook */298if (hook == etfp->downstream_hook.hook) {299error = EINVAL;300break;301}302303/* Check we don't already trap this ethertype */304if (ng_etf_findentry(etfp,305htons(f->ethertype))) {306error = EEXIST;307break;308}309310/*311* Ok, make the filter and put it in the312* hashtable ready for matching.313*/314fil = malloc(sizeof(*fil),315M_NETGRAPH_ETF, M_NOWAIT | M_ZERO);316if (fil == NULL) {317error = ENOMEM;318break;319}320321fil->match_hook = hook;322fil->ethertype = htons(f->ethertype);323LIST_INSERT_HEAD( etfp->hashtable324+ HASH(fil->ethertype),325fil, next);326}327break;328default:329error = EINVAL; /* unknown command */330break;331}332break;333default:334error = EINVAL; /* unknown cookie type */335break;336}337338/* Take care of synchronous response, if any */339NG_RESPOND_MSG(error, node, item, resp);340/* Free the message and return */341NG_FREE_MSG(msg);342return(error);343}344345/*346* Receive data, and do something with it.347* Actually we receive a queue item which holds the data.348* If we free the item it will also free the data unless we have previously349* disassociated it using the NGI_GET_etf() macro.350* Possibly send it out on another link after processing.351* Possibly do something different if it comes from different352* hooks. The caller will never free m , so if we use up this data353* or abort we must free it.354*355* If we want, we may decide to force this data to be queued and reprocessed356* at the netgraph NETISR time.357* We would do that by setting the HK_QUEUE flag on our hook. We would do that358* in the connect() method.359*/360static int361ng_etf_rcvdata(hook_p hook, item_p item )362{363const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));364struct ether_header *eh;365int error = 0;366struct mbuf *m;367u_int16_t ethertype;368struct filter *fil;369370if (NG_HOOK_PRIVATE(hook) == NULL) { /* Shouldn't happen but.. */371NG_FREE_ITEM(item);372}373374/*375* Everything not from the downstream hook goes to the376* downstream hook. But only if it matches the ethertype377* of the source hook. Un matching must go to/from 'nomatch'.378*/379380/* Make sure we have an entire header */381NGI_GET_M(item, m);382if (m->m_len < sizeof(*eh) ) {383m = m_pullup(m, sizeof(*eh));384if (m == NULL) {385NG_FREE_ITEM(item);386return(EINVAL);387}388}389390eh = mtod(m, struct ether_header *);391ethertype = eh->ether_type;392fil = ng_etf_findentry(etfp, ethertype);393394/*395* if from downstream, select between a match hook or396* the nomatch hook397*/398if (hook == etfp->downstream_hook.hook) {399etfp->packets_in++;400if (fil && fil->match_hook) {401NG_FWD_NEW_DATA(error, item, fil->match_hook, m);402} else {403NG_FWD_NEW_DATA(error, item,etfp->nomatch_hook.hook, m);404}405} else {406/*407* It must be heading towards the downstream.408* Check that it's ethertype matches409* the filters for it's input hook.410* If it doesn't have one, check it's from nomatch.411*/412if ((fil && (fil->match_hook != hook))413|| ((fil == NULL) && (hook != etfp->nomatch_hook.hook))) {414NG_FREE_ITEM(item);415NG_FREE_M(m);416return (EPROTOTYPE);417}418NG_FWD_NEW_DATA( error, item, etfp->downstream_hook.hook, m);419if (error == 0) {420etfp->packets_out++;421}422}423return (error);424}425426/*427* Do local shutdown processing..428* All our links and the name have already been removed.429*/430static int431ng_etf_shutdown(node_p node)432{433const etf_p privdata = NG_NODE_PRIVATE(node);434435NG_NODE_SET_PRIVATE(node, NULL);436NG_NODE_UNREF(privdata->node);437free(privdata, M_NETGRAPH_ETF);438return (0);439}440441/*442* Hook disconnection443*444* For this type, removal of the last link destroys the node445*/446static int447ng_etf_disconnect(hook_p hook)448{449const etf_p etfp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));450int i;451struct filter *fil1, *fil2;452453/* purge any rules that refer to this filter */454for (i = 0; i < HASHSIZE; i++) {455fil1 = LIST_FIRST(&etfp->hashtable[i]);456while (fil1 != NULL) {457fil2 = LIST_NEXT(fil1, next);458if (fil1->match_hook == hook) {459LIST_REMOVE(fil1, next);460free(fil1, M_NETGRAPH_ETF);461}462fil1 = fil2;463}464}465466/* If it's not one of the special hooks, then free it */467if (hook == etfp->downstream_hook.hook) {468etfp->downstream_hook.hook = NULL;469} else if (hook == etfp->nomatch_hook.hook) {470etfp->nomatch_hook.hook = NULL;471} else {472if (NG_HOOK_PRIVATE(hook)) /* Paranoia */473free(NG_HOOK_PRIVATE(hook), M_NETGRAPH_ETF);474}475476NG_HOOK_SET_PRIVATE(hook, NULL);477478if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)479&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) /* already shutting down? */480ng_rmnode_self(NG_HOOK_NODE(hook));481return (0);482}483484485