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/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/endian.h>
36 #include <sys/malloc.h>
40 #include <net/ethernet.h>
42 #include <netgraph/ng_message.h>
43 #include <netgraph/ng_parse.h>
44 #include <netgraph/netgraph.h>
46 #include <netgraph/ng_patch.h>
49 struct ng_patch_priv {
52 uint8_t dlt; /* DLT_XXX from bpf.h */
53 struct ng_patch_stats stats;
54 struct ng_patch_config *conf;
57 typedef struct ng_patch_priv *priv_p;
59 /* Netgraph methods */
60 static ng_constructor_t ng_patch_constructor;
61 static ng_rcvmsg_t ng_patch_rcvmsg;
62 static ng_shutdown_t ng_patch_shutdown;
63 static ng_newhook_t ng_patch_newhook;
64 static ng_rcvdata_t ng_patch_rcvdata;
65 static ng_disconnect_t ng_patch_disconnect;
66 #define ERROUT(x) { error = (x); goto done; }
69 ng_patch_config_getlen(const struct ng_parse_type *type,
70 const u_char *start, const u_char *buf)
72 const struct ng_patch_config *conf;
74 conf = (const struct ng_patch_config *)(buf -
75 offsetof(struct ng_patch_config, ops));
80 static const struct ng_parse_struct_field ng_patch_op_type_fields[]
82 static const struct ng_parse_type ng_patch_op_type = {
83 &ng_parse_struct_type,
84 &ng_patch_op_type_fields
87 static const struct ng_parse_array_info ng_patch_ops_array_info = {
89 &ng_patch_config_getlen
91 static const struct ng_parse_type ng_patch_ops_array_type = {
93 &ng_patch_ops_array_info
96 static const struct ng_parse_struct_field ng_patch_config_type_fields[]
97 = NG_PATCH_CONFIG_TYPE;
98 static const struct ng_parse_type ng_patch_config_type = {
99 &ng_parse_struct_type,
100 &ng_patch_config_type_fields
103 static const struct ng_parse_struct_field ng_patch_stats_fields[]
104 = NG_PATCH_STATS_TYPE;
105 static const struct ng_parse_type ng_patch_stats_type = {
106 &ng_parse_struct_type,
107 &ng_patch_stats_fields
110 static const struct ng_cmdlist ng_patch_cmdlist[] = {
122 &ng_parse_uint8_type,
130 &ng_patch_config_type
136 &ng_patch_config_type,
155 NGM_PATCH_GETCLR_STATS,
163 static struct ng_type typestruct = {
164 .version = NG_ABI_VERSION,
165 .name = NG_PATCH_NODE_TYPE,
166 .constructor = ng_patch_constructor,
167 .rcvmsg = ng_patch_rcvmsg,
168 .shutdown = ng_patch_shutdown,
169 .newhook = ng_patch_newhook,
170 .rcvdata = ng_patch_rcvdata,
171 .disconnect = ng_patch_disconnect,
172 .cmdlist = ng_patch_cmdlist,
175 NETGRAPH_INIT(patch, &typestruct);
178 ng_patch_constructor(node_p node)
182 privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
183 privdata->dlt = DLT_RAW;
185 NG_NODE_SET_PRIVATE(node, privdata);
191 ng_patch_newhook(node_p node, hook_p hook, const char *name)
193 const priv_p privp = NG_NODE_PRIVATE(node);
195 if (strncmp(name, NG_PATCH_HOOK_IN, strlen(NG_PATCH_HOOK_IN)) == 0) {
197 } else if (strncmp(name, NG_PATCH_HOOK_OUT,
198 strlen(NG_PATCH_HOOK_OUT)) == 0) {
207 ng_patch_rcvmsg(node_p node, item_p item, hook_p lasthook)
209 const priv_p privp = NG_NODE_PRIVATE(node);
210 struct ng_patch_config *conf, *newconf;
212 struct ng_mesg *resp = NULL;
215 NGI_GET_MSG(item, msg);
217 if (msg->header.typecookie != NGM_PATCH_COOKIE)
220 switch (msg->header.cmd)
222 case NGM_PATCH_GETCONFIG:
223 if (privp->conf == NULL)
226 NG_MKRESPONSE(resp, msg,
227 NG_PATCH_CONF_SIZE(privp->conf->count), M_WAITOK);
232 bcopy(privp->conf, resp->data,
233 NG_PATCH_CONF_SIZE(privp->conf->count));
235 conf = (struct ng_patch_config *) resp->data;
237 for (i = 0; i < conf->count; i++) {
238 switch (conf->ops[i].length)
241 conf->ops[i].val.v8 = conf->ops[i].val.v1;
244 conf->ops[i].val.v8 = conf->ops[i].val.v2;
247 conf->ops[i].val.v8 = conf->ops[i].val.v4;
256 case NGM_PATCH_SETCONFIG:
257 conf = (struct ng_patch_config *) msg->data;
259 if (msg->header.arglen < sizeof(struct ng_patch_config) ||
260 msg->header.arglen < NG_PATCH_CONF_SIZE(conf->count))
263 for (i = 0; i < conf->count; i++) {
264 switch (conf->ops[i].length)
267 conf->ops[i].val.v1 = (uint8_t) conf->ops[i].val.v8;
270 conf->ops[i].val.v2 = (uint16_t) conf->ops[i].val.v8;
273 conf->ops[i].val.v4 = (uint32_t) conf->ops[i].val.v8;
282 conf->csum_flags &= NG_PATCH_CSUM_IPV4|NG_PATCH_CSUM_IPV6;
283 conf->relative_offset = !!conf->relative_offset;
285 newconf = malloc(NG_PATCH_CONF_SIZE(conf->count), M_NETGRAPH, M_WAITOK | M_ZERO);
287 bcopy(conf, newconf, NG_PATCH_CONF_SIZE(conf->count));
290 free(privp->conf, M_NETGRAPH);
292 privp->conf = newconf;
296 case NGM_PATCH_GET_STATS:
297 case NGM_PATCH_CLR_STATS:
298 case NGM_PATCH_GETCLR_STATS:
299 if (msg->header.cmd != NGM_PATCH_CLR_STATS) {
300 NG_MKRESPONSE(resp, msg, sizeof(struct ng_patch_stats), M_WAITOK);
305 bcopy(&(privp->stats), resp->data, sizeof(struct ng_patch_stats));
308 if (msg->header.cmd != NGM_PATCH_GET_STATS)
309 bzero(&(privp->stats), sizeof(struct ng_patch_stats));
313 case NGM_PATCH_GETDLT:
314 NG_MKRESPONSE(resp, msg, sizeof(uint8_t), M_WAITOK);
319 *((uint8_t *) resp->data) = privp->dlt;
323 case NGM_PATCH_SETDLT:
324 if (msg->header.arglen != sizeof(uint8_t))
327 switch (*(uint8_t *) msg->data)
331 privp->dlt = *(uint8_t *) msg->data;
345 NG_RESPOND_MSG(error, node, item, resp);
352 do_patch(priv_p privp, struct mbuf *m, int global_offset)
354 int i, offset, patched = 0;
355 union ng_patch_op_val val;
357 for (i = 0; i < privp->conf->count; i++) {
358 offset = global_offset + privp->conf->ops[i].offset;
360 if (offset + privp->conf->ops[i].length > m->m_pkthdr.len)
363 /* for "=" operation we don't need to copy data from mbuf */
364 if (privp->conf->ops[i].mode != NG_PATCH_MODE_SET)
365 m_copydata(m, offset, privp->conf->ops[i].length, (caddr_t) &val);
367 switch (privp->conf->ops[i].length)
370 switch (privp->conf->ops[i].mode)
372 case NG_PATCH_MODE_SET:
373 val.v1 = privp->conf->ops[i].val.v1;
375 case NG_PATCH_MODE_ADD:
376 val.v1 += privp->conf->ops[i].val.v1;
378 case NG_PATCH_MODE_SUB:
379 val.v1 -= privp->conf->ops[i].val.v1;
381 case NG_PATCH_MODE_MUL:
382 val.v1 *= privp->conf->ops[i].val.v1;
384 case NG_PATCH_MODE_DIV:
385 val.v1 /= privp->conf->ops[i].val.v1;
387 case NG_PATCH_MODE_NEG:
388 *((int8_t *) &val) = - *((int8_t *) &val);
390 case NG_PATCH_MODE_AND:
391 val.v1 &= privp->conf->ops[i].val.v1;
393 case NG_PATCH_MODE_OR:
394 val.v1 |= privp->conf->ops[i].val.v1;
396 case NG_PATCH_MODE_XOR:
397 val.v1 ^= privp->conf->ops[i].val.v1;
399 case NG_PATCH_MODE_SHL:
400 val.v1 <<= privp->conf->ops[i].val.v1;
402 case NG_PATCH_MODE_SHR:
403 val.v1 >>= privp->conf->ops[i].val.v1;
409 val.v2 = ntohs(val.v2);
411 switch (privp->conf->ops[i].mode)
413 case NG_PATCH_MODE_SET:
414 val.v2 = privp->conf->ops[i].val.v2;
416 case NG_PATCH_MODE_ADD:
417 val.v2 += privp->conf->ops[i].val.v2;
419 case NG_PATCH_MODE_SUB:
420 val.v2 -= privp->conf->ops[i].val.v2;
422 case NG_PATCH_MODE_MUL:
423 val.v2 *= privp->conf->ops[i].val.v2;
425 case NG_PATCH_MODE_DIV:
426 val.v2 /= privp->conf->ops[i].val.v2;
428 case NG_PATCH_MODE_NEG:
429 *((int16_t *) &val) = - *((int16_t *) &val);
431 case NG_PATCH_MODE_AND:
432 val.v2 &= privp->conf->ops[i].val.v2;
434 case NG_PATCH_MODE_OR:
435 val.v2 |= privp->conf->ops[i].val.v2;
437 case NG_PATCH_MODE_XOR:
438 val.v2 ^= privp->conf->ops[i].val.v2;
440 case NG_PATCH_MODE_SHL:
441 val.v2 <<= privp->conf->ops[i].val.v2;
443 case NG_PATCH_MODE_SHR:
444 val.v2 >>= privp->conf->ops[i].val.v2;
448 val.v2 = htons(val.v2);
453 val.v4 = ntohl(val.v4);
455 switch (privp->conf->ops[i].mode)
457 case NG_PATCH_MODE_SET:
458 val.v4 = privp->conf->ops[i].val.v4;
460 case NG_PATCH_MODE_ADD:
461 val.v4 += privp->conf->ops[i].val.v4;
463 case NG_PATCH_MODE_SUB:
464 val.v4 -= privp->conf->ops[i].val.v4;
466 case NG_PATCH_MODE_MUL:
467 val.v4 *= privp->conf->ops[i].val.v4;
469 case NG_PATCH_MODE_DIV:
470 val.v4 /= privp->conf->ops[i].val.v4;
472 case NG_PATCH_MODE_NEG:
473 *((int32_t *) &val) = - *((int32_t *) &val);
475 case NG_PATCH_MODE_AND:
476 val.v4 &= privp->conf->ops[i].val.v4;
478 case NG_PATCH_MODE_OR:
479 val.v4 |= privp->conf->ops[i].val.v4;
481 case NG_PATCH_MODE_XOR:
482 val.v4 ^= privp->conf->ops[i].val.v4;
484 case NG_PATCH_MODE_SHL:
485 val.v4 <<= privp->conf->ops[i].val.v4;
487 case NG_PATCH_MODE_SHR:
488 val.v4 >>= privp->conf->ops[i].val.v4;
492 val.v4 = htonl(val.v4);
497 val.v8 = be64toh(val.v8);
499 switch (privp->conf->ops[i].mode)
501 case NG_PATCH_MODE_SET:
502 val.v8 = privp->conf->ops[i].val.v8;
504 case NG_PATCH_MODE_ADD:
505 val.v8 += privp->conf->ops[i].val.v8;
507 case NG_PATCH_MODE_SUB:
508 val.v8 -= privp->conf->ops[i].val.v8;
510 case NG_PATCH_MODE_MUL:
511 val.v8 *= privp->conf->ops[i].val.v8;
513 case NG_PATCH_MODE_DIV:
514 val.v8 /= privp->conf->ops[i].val.v8;
516 case NG_PATCH_MODE_NEG:
517 *((int64_t *) &val) = - *((int64_t *) &val);
519 case NG_PATCH_MODE_AND:
520 val.v8 &= privp->conf->ops[i].val.v8;
522 case NG_PATCH_MODE_OR:
523 val.v8 |= privp->conf->ops[i].val.v8;
525 case NG_PATCH_MODE_XOR:
526 val.v8 ^= privp->conf->ops[i].val.v8;
528 case NG_PATCH_MODE_SHL:
529 val.v8 <<= privp->conf->ops[i].val.v8;
531 case NG_PATCH_MODE_SHR:
532 val.v8 >>= privp->conf->ops[i].val.v8;
536 val.v8 = htobe64(val.v8);
541 m_copyback(m, offset, privp->conf->ops[i].length, (caddr_t) &val);
546 privp->stats.patched++;
550 ng_patch_rcvdata(hook_p hook, item_p item)
552 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
558 priv->stats.received++;
562 #define PULLUP_CHECK(mbuf, length) do { \
563 pullup_len += length; \
564 if (((mbuf)->m_pkthdr.len < pullup_len) || \
565 (pullup_len > MHLEN)) { \
569 if ((mbuf)->m_len < pullup_len && \
570 (((mbuf) = m_pullup((mbuf), pullup_len)) == NULL)) { \
576 if (priv->conf && hook == priv->in &&
577 m && (m->m_flags & M_PKTHDR)) {
578 m = m_unshare(m, M_NOWAIT);
583 if (priv->conf->relative_offset) {
584 struct ether_header *eh;
585 struct ng_patch_vlan_header *vh;
591 PULLUP_CHECK(m, sizeof(struct ether_header));
592 eh = mtod(m, struct ether_header *);
593 etype = ntohs(eh->ether_type);
595 for (;;) { /* QinQ support */
601 PULLUP_CHECK(m, sizeof(struct ng_patch_vlan_header));
602 vh = (struct ng_patch_vlan_header *) mtodo(m,
603 pullup_len - sizeof(struct ng_patch_vlan_header));
604 etype = ntohs(vh->etype);
622 do_patch(priv, m, pullup_len);
624 m->m_pkthdr.csum_flags |= priv->conf->csum_flags;
632 if (hook == priv->in) {
633 /* return frames on 'in' hook if 'out' not connected */
634 out = priv->out ? priv->out : priv->in;
635 } else if (hook == priv->out && priv->in) {
636 /* pass frames on 'out' hook if 'in' connected */
643 NG_FWD_NEW_DATA(error, item, out, m);
652 priv->stats.dropped++;
658 ng_patch_shutdown(node_p node)
660 const priv_p privdata = NG_NODE_PRIVATE(node);
662 NG_NODE_SET_PRIVATE(node, NULL);
665 if (privdata->conf != NULL)
666 free(privdata->conf, M_NETGRAPH);
668 free(privdata, M_NETGRAPH);
674 ng_patch_disconnect(hook_p hook)
678 priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
680 if (hook == priv->in) {
684 if (hook == priv->out) {
688 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
689 NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) /* already shutting down? */
690 ng_rmnode_self(NG_HOOK_NODE(hook));