| Commit | Line | Data |
|---|---|---|
| b06ebda0 MD |
1 | /* |
| 2 | * ng_hole.c | |
| 3 | */ | |
| 4 | ||
| 5 | /*- | |
| 6 | * Copyright (c) 1996-1999 Whistle Communications, Inc. | |
| 7 | * All rights reserved. | |
| 8 | * | |
| 9 | * Subject to the following obligations and disclaimer of warranty, use and | |
| 10 | * redistribution of this software, in source or object code forms, with or | |
| 11 | * without modifications are expressly permitted by Whistle Communications; | |
| 12 | * provided, however, that: | |
| 13 | * 1. Any and all reproductions of the source or object code must include the | |
| 14 | * copyright notice above and the following disclaimer of warranties; and | |
| 15 | * 2. No rights are granted, in any manner or form, to use Whistle | |
| 16 | * Communications, Inc. trademarks, including the mark "WHISTLE | |
| 17 | * COMMUNICATIONS" on advertising, endorsements, or otherwise except as | |
| 18 | * such appears in the above copyright notice or in the software. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND | |
| 21 | * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO | |
| 22 | * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, | |
| 23 | * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF | |
| 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. | |
| 25 | * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY | |
| 26 | * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS | |
| 27 | * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. | |
| 28 | * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES | |
| 29 | * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING | |
| 30 | * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | |
| 31 | * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 32 | * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY | |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 35 | * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY | |
| 36 | * OF SUCH DAMAGE. | |
| 37 | * | |
| 38 | * Author: Julian Elisher <julian@freebsd.org> | |
| 39 | * | |
| 40 | * $FreeBSD: src/sys/netgraph/ng_hole.c,v 1.15 2005/12/09 07:09:44 ru Exp $ | |
| 5a975a3d | 41 | * $DragonFly: src/sys/netgraph7/ng_hole.c,v 1.2 2008/06/26 23:05:35 dillon Exp $ |
| b06ebda0 MD |
42 | * $Whistle: ng_hole.c,v 1.10 1999/11/01 09:24:51 julian Exp $ |
| 43 | */ | |
| 44 | ||
| 45 | /* | |
| 46 | * This node is a 'black hole' that simply discards everything it receives | |
| 47 | */ | |
| 48 | ||
| 49 | #include <sys/param.h> | |
| 50 | #include <sys/systm.h> | |
| 51 | #include <sys/kernel.h> | |
| 52 | #include <sys/malloc.h> | |
| 53 | #include <sys/mbuf.h> | |
| 5a975a3d MD |
54 | #include "ng_message.h" |
| 55 | #include "netgraph.h" | |
| 56 | #include "ng_parse.h" | |
| 57 | #include "ng_hole.h" | |
| b06ebda0 MD |
58 | |
| 59 | /* Per hook private info. */ | |
| 60 | struct ng_hole_hookinfo { | |
| 61 | struct ng_hole_hookstat stats; | |
| 62 | }; | |
| 63 | typedef struct ng_hole_hookinfo *hinfo_p; | |
| 64 | ||
| 65 | /* Parse type for struct ng_hole_hookstat. */ | |
| 66 | static const struct ng_parse_struct_field ng_hole_hookstat_type_fields[] = | |
| 67 | NG_HOLE_HOOKSTAT_TYPE_INFO; | |
| 68 | static const struct ng_parse_type ng_hole_hookstat_type = { | |
| 69 | &ng_parse_struct_type, | |
| 70 | &ng_hole_hookstat_type_fields | |
| 71 | }; | |
| 72 | ||
| 73 | /* List of commands and how to convert arguments to/from ASCII. */ | |
| 74 | static const struct ng_cmdlist ng_hole_cmdlist[] = { | |
| 75 | { | |
| 76 | NGM_HOLE_COOKIE, | |
| 77 | NGM_HOLE_GET_STATS, | |
| 78 | "getstats", | |
| 79 | &ng_parse_hookbuf_type, | |
| 80 | &ng_hole_hookstat_type | |
| 81 | }, | |
| 82 | { | |
| 83 | NGM_HOLE_COOKIE, | |
| 84 | NGM_HOLE_CLR_STATS, | |
| 85 | "clrstats", | |
| 86 | &ng_parse_hookbuf_type, | |
| 87 | NULL | |
| 88 | }, | |
| 89 | { | |
| 90 | NGM_HOLE_COOKIE, | |
| 91 | NGM_HOLE_GETCLR_STATS, | |
| 92 | "getclrstats", | |
| 93 | &ng_parse_hookbuf_type, | |
| 94 | &ng_hole_hookstat_type | |
| 95 | }, | |
| 96 | { 0 } | |
| 97 | }; | |
| 98 | ||
| 99 | /* Netgraph methods */ | |
| 100 | static ng_constructor_t ngh_cons; | |
| 101 | static ng_rcvmsg_t ngh_rcvmsg; | |
| 102 | static ng_newhook_t ngh_newhook; | |
| 103 | static ng_rcvdata_t ngh_rcvdata; | |
| 104 | static ng_disconnect_t ngh_disconnect; | |
| 105 | ||
| 106 | static struct ng_type typestruct = { | |
| 107 | .version = NG_ABI_VERSION, | |
| 108 | .name = NG_HOLE_NODE_TYPE, | |
| 109 | .constructor = ngh_cons, | |
| 110 | .rcvmsg = ngh_rcvmsg, | |
| 111 | .newhook = ngh_newhook, | |
| 112 | .rcvdata = ngh_rcvdata, | |
| 113 | .disconnect = ngh_disconnect, | |
| 114 | .cmdlist = ng_hole_cmdlist, | |
| 115 | }; | |
| 116 | NETGRAPH_INIT(hole, &typestruct); | |
| 117 | ||
| 118 | /* | |
| 119 | * Be obliging. but no work to do. | |
| 120 | */ | |
| 121 | static int | |
| 122 | ngh_cons(node_p node) | |
| 123 | { | |
| 124 | return(0); | |
| 125 | } | |
| 126 | ||
| 127 | /* | |
| 128 | * Add a hook. | |
| 129 | */ | |
| 130 | static int | |
| 131 | ngh_newhook(node_p node, hook_p hook, const char *name) | |
| 132 | { | |
| 133 | hinfo_p hip; | |
| 134 | ||
| 135 | /* Create hook private structure. */ | |
| fc025606 | 136 | hip = kmalloc(sizeof(*hip), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO); |
| b06ebda0 MD |
137 | if (hip == NULL) |
| 138 | return (ENOMEM); | |
| 139 | NG_HOOK_SET_PRIVATE(hook, hip); | |
| 140 | return (0); | |
| 141 | } | |
| 142 | ||
| 143 | /* | |
| 144 | * Receive a control message. | |
| 145 | */ | |
| 146 | static int | |
| 147 | ngh_rcvmsg(node_p node, item_p item, hook_p lasthook) | |
| 148 | { | |
| 149 | struct ng_mesg *msg; | |
| 150 | struct ng_mesg *resp = NULL; | |
| 151 | int error = 0; | |
| 152 | struct ng_hole_hookstat *stats; | |
| 153 | hook_p hook; | |
| 154 | ||
| 155 | NGI_GET_MSG(item, msg); | |
| 156 | switch (msg->header.typecookie) { | |
| 157 | case NGM_HOLE_COOKIE: | |
| 158 | switch (msg->header.cmd) { | |
| 159 | case NGM_HOLE_GET_STATS: | |
| 160 | case NGM_HOLE_CLR_STATS: | |
| 161 | case NGM_HOLE_GETCLR_STATS: | |
| 162 | /* Sanity check. */ | |
| 163 | if (msg->header.arglen != NG_HOOKSIZ) { | |
| 164 | error = EINVAL; | |
| 165 | break; | |
| 166 | } | |
| 167 | /* Find hook. */ | |
| 168 | hook = ng_findhook(node, (char *)msg->data); | |
| 169 | if (hook == NULL) { | |
| 170 | error = ENOENT; | |
| 171 | break; | |
| 172 | } | |
| 173 | stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats; | |
| 174 | /* Build response (if desired). */ | |
| 175 | if (msg->header.cmd != NGM_HOLE_CLR_STATS) { | |
| 176 | NG_MKRESPONSE(resp, msg, sizeof(*stats), | |
| 5a975a3d | 177 | M_WAITOK | M_NULLOK); |
| b06ebda0 MD |
178 | if (resp == NULL) { |
| 179 | error = ENOMEM; | |
| 180 | break; | |
| 181 | } | |
| 182 | bcopy(stats, resp->data, sizeof(*stats)); | |
| 183 | } | |
| 184 | /* Clear stats (if desired). */ | |
| 185 | if (msg->header.cmd != NGM_HOLE_GET_STATS) | |
| 186 | bzero(stats, sizeof(*stats)); | |
| 187 | break; | |
| 188 | default: /* Unknown command. */ | |
| 189 | error = EINVAL; | |
| 190 | break; | |
| 191 | } | |
| 192 | break; | |
| 193 | default: /* Unknown type cookie. */ | |
| 194 | error = EINVAL; | |
| 195 | break; | |
| 196 | } | |
| 197 | NG_RESPOND_MSG(error, node, item, resp); | |
| 198 | NG_FREE_MSG(msg); | |
| 199 | return (error); | |
| 200 | } | |
| 201 | ||
| 202 | /* | |
| 203 | * Receive data | |
| 204 | */ | |
| 205 | static int | |
| 206 | ngh_rcvdata(hook_p hook, item_p item) | |
| 207 | { | |
| 208 | const hinfo_p hip = NG_HOOK_PRIVATE(hook); | |
| 209 | ||
| 210 | hip->stats.frames++; | |
| 211 | hip->stats.octets += NGI_M(item)->m_pkthdr.len; | |
| 212 | NG_FREE_ITEM(item); | |
| 213 | return 0; | |
| 214 | } | |
| 215 | ||
| 216 | /* | |
| 217 | * Hook disconnection | |
| 218 | */ | |
| 219 | static int | |
| 220 | ngh_disconnect(hook_p hook) | |
| 221 | { | |
| 222 | ||
| fc025606 | 223 | kfree(NG_HOOK_PRIVATE(hook), M_NETGRAPH); |
| b06ebda0 MD |
224 | NG_HOOK_SET_PRIVATE(hook, NULL); |
| 225 | if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) | |
| 226 | ng_rmnode_self(NG_HOOK_NODE(hook)); | |
| 227 | return (0); | |
| 228 | } |