2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2010 Maxim Ignatenko <gelraen.ua@gmail.com>
5 * Copyright (c) 2015 Dmitry Vagin <daemon.hammer@ya.ru>
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 THE AUTHOR 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 THE AUTHOR 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
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/endian.h>
35 #include <sys/malloc.h>
39 #include <net/ethernet.h>
41 #include <netgraph/ng_message.h>
42 #include <netgraph/ng_parse.h>
43 #include <netgraph/netgraph.h>
45 #include <netgraph/ng_patch.h>
48 struct ng_patch_priv {
51 uint8_t dlt; /* DLT_XXX from bpf.h */
52 struct ng_patch_stats stats;
53 struct ng_patch_config *conf;
56 typedef struct ng_patch_priv *priv_p;
58 /* Netgraph methods */
59 static ng_constructor_t ng_patch_constructor;
60 static ng_rcvmsg_t ng_patch_rcvmsg;
61 static ng_shutdown_t ng_patch_shutdown;
62 static ng_newhook_t ng_patch_newhook;
63 static ng_rcvdata_t ng_patch_rcvdata;
64 static ng_disconnect_t ng_patch_disconnect;
65 #define ERROUT(x) { error = (x); goto done; }
68 ng_patch_config_getlen(const struct ng_parse_type *type,
69 const u_char *start, const u_char *buf)
71 const struct ng_patch_config *conf;
73 conf = (const struct ng_patch_config *)(buf -
74 offsetof(struct ng_patch_config, ops));
79 static const struct ng_parse_struct_field ng_patch_op_type_fields[]
81 static const struct ng_parse_type ng_patch_op_type = {
82 &ng_parse_struct_type,
83 &ng_patch_op_type_fields
86 static const struct ng_parse_array_info ng_patch_ops_array_info = {
88 &ng_patch_config_getlen
90 static const struct ng_parse_type ng_patch_ops_array_type = {
92 &ng_patch_ops_array_info
95 static const struct ng_parse_struct_field ng_patch_config_type_fields[]
96 = NG_PATCH_CONFIG_TYPE;
97 static const struct ng_parse_type ng_patch_config_type = {
98 &ng_parse_struct_type,
99 &ng_patch_config_type_fields
102 static const struct ng_parse_struct_field ng_patch_stats_fields[]
103 = NG_PATCH_STATS_TYPE;
104 static const struct ng_parse_type ng_patch_stats_type = {
105 &ng_parse_struct_type,
106 &ng_patch_stats_fields
109 static const struct ng_cmdlist ng_patch_cmdlist[] = {
121 &ng_parse_uint8_type,
129 &ng_patch_config_type
135 &ng_patch_config_type,
154 NGM_PATCH_GETCLR_STATS,
162 static struct ng_type typestruct = {
163 .version = NG_ABI_VERSION,
164 .name = NG_PATCH_NODE_TYPE,
165 .constructor = ng_patch_constructor,
166 .rcvmsg = ng_patch_rcvmsg,
167 .shutdown = ng_patch_shutdown,
168 .newhook = ng_patch_newhook,
169 .rcvdata = ng_patch_rcvdata,
170 .disconnect = ng_patch_disconnect,
171 .cmdlist = ng_patch_cmdlist,
174 NETGRAPH_INIT(patch, &typestruct);
177 ng_patch_constructor(node_p node)
181 privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
182 privdata->dlt = DLT_RAW;
184 NG_NODE_SET_PRIVATE(node, privdata);
190 ng_patch_newhook(node_p node, hook_p hook, const char *name)
192 const priv_p privp = NG_NODE_PRIVATE(node);
194 if (strncmp(name, NG_PATCH_HOOK_IN, strlen(NG_PATCH_HOOK_IN)) == 0) {
196 } else if (strncmp(name, NG_PATCH_HOOK_OUT,
197 strlen(NG_PATCH_HOOK_OUT)) == 0) {
206 ng_patch_rcvmsg(node_p node, item_p item, hook_p lasthook)
208 const priv_p privp = NG_NODE_PRIVATE(node);
209 struct ng_patch_config *conf, *newconf;
211 struct ng_mesg *resp = NULL;
214 NGI_GET_MSG(item, msg);
216 if (msg->header.typecookie != NGM_PATCH_COOKIE)
219 switch (msg->header.cmd)
221 case NGM_PATCH_GETCONFIG:
222 if (privp->conf == NULL)
225 NG_MKRESPONSE(resp, msg,
226 NG_PATCH_CONF_SIZE(privp->conf->count), M_WAITOK);
231 bcopy(privp->conf, resp->data,
232 NG_PATCH_CONF_SIZE(privp->conf->count));
234 conf = (struct ng_patch_config *) resp->data;
236 for (i = 0; i < conf->count; i++) {
237 switch (conf->ops[i].length)
240 conf->ops[i].val.v8 = conf->ops[i].val.v1;
243 conf->ops[i].val.v8 = conf->ops[i].val.v2;
246 conf->ops[i].val.v8 = conf->ops[i].val.v4;
255 case NGM_PATCH_SETCONFIG:
256 conf = (struct ng_patch_config *) msg->data;
258 if (msg->header.arglen < sizeof(struct ng_patch_config) ||
259 msg->header.arglen < NG_PATCH_CONF_SIZE(conf->count))
262 for (i = 0; i < conf->count; i++) {
263 switch (conf->ops[i].length)
266 conf->ops[i].val.v1 = (uint8_t) conf->ops[i].val.v8;
269 conf->ops[i].val.v2 = (uint16_t) conf->ops[i].val.v8;
272 conf->ops[i].val.v4 = (uint32_t) conf->ops[i].val.v8;
281 conf->csum_flags &= NG_PATCH_CSUM_IPV4|NG_PATCH_CSUM_IPV6;
282 conf->relative_offset = !!conf->relative_offset;
284 newconf = malloc(NG_PATCH_CONF_SIZE(conf->count), M_NETGRAPH, M_WAITOK | M_ZERO);
286 bcopy(conf, newconf, NG_PATCH_CONF_SIZE(conf->count));
289 free(privp->conf, M_NETGRAPH);
291 privp->conf = newconf;
295 case NGM_PATCH_GET_STATS:
296 case NGM_PATCH_CLR_STATS:
297 case NGM_PATCH_GETCLR_STATS:
298 if (msg->header.cmd != NGM_PATCH_CLR_STATS) {
299 NG_MKRESPONSE(resp, msg, sizeof(struct ng_patch_stats), M_WAITOK);
304 bcopy(&(privp->stats), resp->data, sizeof(struct ng_patch_stats));
307 if (msg->header.cmd != NGM_PATCH_GET_STATS)
308 bzero(&(privp->stats), sizeof(struct ng_patch_stats));
312 case NGM_PATCH_GETDLT:
313 NG_MKRESPONSE(resp, msg, sizeof(uint8_t), M_WAITOK);
318 *((uint8_t *) resp->data) = privp->dlt;
322 case NGM_PATCH_SETDLT:
323 if (msg->header.arglen != sizeof(uint8_t))
326 switch (*(uint8_t *) msg->data)
330 privp->dlt = *(uint8_t *) msg->data;
344 NG_RESPOND_MSG(error, node, item, resp);
351 do_patch(priv_p privp, struct mbuf *m, int global_offset)
353 int i, offset, patched = 0;
354 union ng_patch_op_val val;
356 for (i = 0; i < privp->conf->count; i++) {
357 offset = global_offset + privp->conf->ops[i].offset;
359 if (offset + privp->conf->ops[i].length > m->m_pkthdr.len)
362 /* for "=" operation we don't need to copy data from mbuf */
363 if (privp->conf->ops[i].mode != NG_PATCH_MODE_SET)
364 m_copydata(m, offset, privp->conf->ops[i].length, (caddr_t) &val);
366 switch (privp->conf->ops[i].length)
369 switch (privp->conf->ops[i].mode)
371 case NG_PATCH_MODE_SET:
372 val.v1 = privp->conf->ops[i].val.v1;
374 case NG_PATCH_MODE_ADD:
375 val.v1 += privp->conf->ops[i].val.v1;
377 case NG_PATCH_MODE_SUB:
378 val.v1 -= privp->conf->ops[i].val.v1;
380 case NG_PATCH_MODE_MUL:
381 val.v1 *= privp->conf->ops[i].val.v1;
383 case NG_PATCH_MODE_DIV:
384 val.v1 /= privp->conf->ops[i].val.v1;
386 case NG_PATCH_MODE_NEG:
387 *((int8_t *) &val) = - *((int8_t *) &val);
389 case NG_PATCH_MODE_AND:
390 val.v1 &= privp->conf->ops[i].val.v1;
392 case NG_PATCH_MODE_OR:
393 val.v1 |= privp->conf->ops[i].val.v1;
395 case NG_PATCH_MODE_XOR:
396 val.v1 ^= privp->conf->ops[i].val.v1;
398 case NG_PATCH_MODE_SHL:
399 val.v1 <<= privp->conf->ops[i].val.v1;
401 case NG_PATCH_MODE_SHR:
402 val.v1 >>= privp->conf->ops[i].val.v1;
408 val.v2 = ntohs(val.v2);
410 switch (privp->conf->ops[i].mode)
412 case NG_PATCH_MODE_SET:
413 val.v2 = privp->conf->ops[i].val.v2;
415 case NG_PATCH_MODE_ADD:
416 val.v2 += privp->conf->ops[i].val.v2;
418 case NG_PATCH_MODE_SUB:
419 val.v2 -= privp->conf->ops[i].val.v2;
421 case NG_PATCH_MODE_MUL:
422 val.v2 *= privp->conf->ops[i].val.v2;
424 case NG_PATCH_MODE_DIV:
425 val.v2 /= privp->conf->ops[i].val.v2;
427 case NG_PATCH_MODE_NEG:
428 *((int16_t *) &val) = - *((int16_t *) &val);
430 case NG_PATCH_MODE_AND:
431 val.v2 &= privp->conf->ops[i].val.v2;
433 case NG_PATCH_MODE_OR:
434 val.v2 |= privp->conf->ops[i].val.v2;
436 case NG_PATCH_MODE_XOR:
437 val.v2 ^= privp->conf->ops[i].val.v2;
439 case NG_PATCH_MODE_SHL:
440 val.v2 <<= privp->conf->ops[i].val.v2;
442 case NG_PATCH_MODE_SHR:
443 val.v2 >>= privp->conf->ops[i].val.v2;
447 val.v2 = htons(val.v2);
452 val.v4 = ntohl(val.v4);
454 switch (privp->conf->ops[i].mode)
456 case NG_PATCH_MODE_SET:
457 val.v4 = privp->conf->ops[i].val.v4;
459 case NG_PATCH_MODE_ADD:
460 val.v4 += privp->conf->ops[i].val.v4;
462 case NG_PATCH_MODE_SUB:
463 val.v4 -= privp->conf->ops[i].val.v4;
465 case NG_PATCH_MODE_MUL:
466 val.v4 *= privp->conf->ops[i].val.v4;
468 case NG_PATCH_MODE_DIV:
469 val.v4 /= privp->conf->ops[i].val.v4;
471 case NG_PATCH_MODE_NEG:
472 *((int32_t *) &val) = - *((int32_t *) &val);
474 case NG_PATCH_MODE_AND:
475 val.v4 &= privp->conf->ops[i].val.v4;
477 case NG_PATCH_MODE_OR:
478 val.v4 |= privp->conf->ops[i].val.v4;
480 case NG_PATCH_MODE_XOR:
481 val.v4 ^= privp->conf->ops[i].val.v4;
483 case NG_PATCH_MODE_SHL:
484 val.v4 <<= privp->conf->ops[i].val.v4;
486 case NG_PATCH_MODE_SHR:
487 val.v4 >>= privp->conf->ops[i].val.v4;
491 val.v4 = htonl(val.v4);
496 val.v8 = be64toh(val.v8);
498 switch (privp->conf->ops[i].mode)
500 case NG_PATCH_MODE_SET:
501 val.v8 = privp->conf->ops[i].val.v8;
503 case NG_PATCH_MODE_ADD:
504 val.v8 += privp->conf->ops[i].val.v8;
506 case NG_PATCH_MODE_SUB:
507 val.v8 -= privp->conf->ops[i].val.v8;
509 case NG_PATCH_MODE_MUL:
510 val.v8 *= privp->conf->ops[i].val.v8;
512 case NG_PATCH_MODE_DIV:
513 val.v8 /= privp->conf->ops[i].val.v8;
515 case NG_PATCH_MODE_NEG:
516 *((int64_t *) &val) = - *((int64_t *) &val);
518 case NG_PATCH_MODE_AND:
519 val.v8 &= privp->conf->ops[i].val.v8;
521 case NG_PATCH_MODE_OR:
522 val.v8 |= privp->conf->ops[i].val.v8;
524 case NG_PATCH_MODE_XOR:
525 val.v8 ^= privp->conf->ops[i].val.v8;
527 case NG_PATCH_MODE_SHL:
528 val.v8 <<= privp->conf->ops[i].val.v8;
530 case NG_PATCH_MODE_SHR:
531 val.v8 >>= privp->conf->ops[i].val.v8;
535 val.v8 = htobe64(val.v8);
540 m_copyback(m, offset, privp->conf->ops[i].length, (caddr_t) &val);
545 privp->stats.patched++;
549 ng_patch_rcvdata(hook_p hook, item_p item)
551 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
557 priv->stats.received++;
561 #define PULLUP_CHECK(mbuf, length) do { \
562 pullup_len += length; \
563 if (((mbuf)->m_pkthdr.len < pullup_len) || \
564 (pullup_len > MHLEN)) { \
568 if ((mbuf)->m_len < pullup_len && \
569 (((mbuf) = m_pullup((mbuf), pullup_len)) == NULL)) { \
575 if (priv->conf && hook == priv->in &&
576 m && (m->m_flags & M_PKTHDR)) {
577 m = m_unshare(m, M_NOWAIT);
582 if (priv->conf->relative_offset) {
583 struct ether_header *eh;
584 struct ng_patch_vlan_header *vh;
590 PULLUP_CHECK(m, sizeof(struct ether_header));
591 eh = mtod(m, struct ether_header *);
592 etype = ntohs(eh->ether_type);
594 for (;;) { /* QinQ support */
600 PULLUP_CHECK(m, sizeof(struct ng_patch_vlan_header));
601 vh = (struct ng_patch_vlan_header *) mtodo(m,
602 pullup_len - sizeof(struct ng_patch_vlan_header));
603 etype = ntohs(vh->etype);
621 do_patch(priv, m, pullup_len);
623 m->m_pkthdr.csum_flags |= priv->conf->csum_flags;
631 if (hook == priv->in) {
632 /* return frames on 'in' hook if 'out' not connected */
633 out = priv->out ? priv->out : priv->in;
634 } else if (hook == priv->out && priv->in) {
635 /* pass frames on 'out' hook if 'in' connected */
642 NG_FWD_NEW_DATA(error, item, out, m);
651 priv->stats.dropped++;
657 ng_patch_shutdown(node_p node)
659 const priv_p privdata = NG_NODE_PRIVATE(node);
661 NG_NODE_SET_PRIVATE(node, NULL);
664 if (privdata->conf != NULL)
665 free(privdata->conf, M_NETGRAPH);
667 free(privdata, M_NETGRAPH);
673 ng_patch_disconnect(hook_p hook)
677 priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
679 if (hook == priv->in) {
683 if (hook == priv->out) {
687 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
688 NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) /* already shutting down? */
689 ng_rmnode_self(NG_HOOK_NODE(hook));