2 * Copyright (c) 2010 Maxim Ignatenko <gelraen.ua@gmail.com>
3 * Copyright (c) 2015 Dmitry Vagin <daemon.hammer@ya.ru>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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;
67 #define ERROUT(x) { error = (x); goto done; }
70 ng_patch_config_getlen(const struct ng_parse_type *type,
71 const u_char *start, const u_char *buf)
73 const struct ng_patch_config *conf;
75 conf = (const struct ng_patch_config *)(buf -
76 offsetof(struct ng_patch_config, ops));
81 static const struct ng_parse_struct_field ng_patch_op_type_fields[]
83 static const struct ng_parse_type ng_patch_op_type = {
84 &ng_parse_struct_type,
85 &ng_patch_op_type_fields
88 static const struct ng_parse_array_info ng_patch_ops_array_info = {
90 &ng_patch_config_getlen
92 static const struct ng_parse_type ng_patch_ops_array_type = {
94 &ng_patch_ops_array_info
97 static const struct ng_parse_struct_field ng_patch_config_type_fields[]
98 = NG_PATCH_CONFIG_TYPE;
99 static const struct ng_parse_type ng_patch_config_type = {
100 &ng_parse_struct_type,
101 &ng_patch_config_type_fields
104 static const struct ng_parse_struct_field ng_patch_stats_fields[]
105 = NG_PATCH_STATS_TYPE;
106 static const struct ng_parse_type ng_patch_stats_type = {
107 &ng_parse_struct_type,
108 &ng_patch_stats_fields
111 static const struct ng_cmdlist ng_patch_cmdlist[] = {
123 &ng_parse_uint8_type,
131 &ng_patch_config_type
137 &ng_patch_config_type,
156 NGM_PATCH_GETCLR_STATS,
164 static struct ng_type typestruct = {
165 .version = NG_ABI_VERSION,
166 .name = NG_PATCH_NODE_TYPE,
167 .constructor = ng_patch_constructor,
168 .rcvmsg = ng_patch_rcvmsg,
169 .shutdown = ng_patch_shutdown,
170 .newhook = ng_patch_newhook,
171 .rcvdata = ng_patch_rcvdata,
172 .disconnect = ng_patch_disconnect,
173 .cmdlist = ng_patch_cmdlist,
176 NETGRAPH_INIT(patch, &typestruct);
179 ng_patch_constructor(node_p node)
183 privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
184 privdata->dlt = DLT_RAW;
186 NG_NODE_SET_PRIVATE(node, privdata);
192 ng_patch_newhook(node_p node, hook_p hook, const char *name)
194 const priv_p privp = NG_NODE_PRIVATE(node);
196 if (strncmp(name, NG_PATCH_HOOK_IN, strlen(NG_PATCH_HOOK_IN)) == 0) {
198 } else if (strncmp(name, NG_PATCH_HOOK_OUT,
199 strlen(NG_PATCH_HOOK_OUT)) == 0) {
208 ng_patch_rcvmsg(node_p node, item_p item, hook_p lasthook)
210 const priv_p privp = NG_NODE_PRIVATE(node);
211 struct ng_patch_config *conf, *newconf;
213 struct ng_mesg *resp = NULL;
216 NGI_GET_MSG(item, msg);
218 if (msg->header.typecookie != NGM_PATCH_COOKIE)
221 switch (msg->header.cmd)
223 case NGM_PATCH_GETCONFIG:
224 if (privp->conf == NULL)
227 NG_MKRESPONSE(resp, msg,
228 NG_PATCH_CONF_SIZE(privp->conf->count), M_WAITOK);
233 bcopy(privp->conf, resp->data,
234 NG_PATCH_CONF_SIZE(privp->conf->count));
236 conf = (struct ng_patch_config *) resp->data;
238 for (i = 0; i < conf->count; i++) {
239 switch (conf->ops[i].length)
242 conf->ops[i].val.v8 = conf->ops[i].val.v1;
245 conf->ops[i].val.v8 = conf->ops[i].val.v2;
248 conf->ops[i].val.v8 = conf->ops[i].val.v4;
257 case NGM_PATCH_SETCONFIG:
258 conf = (struct ng_patch_config *) msg->data;
260 if (msg->header.arglen < sizeof(struct ng_patch_config) ||
261 msg->header.arglen < NG_PATCH_CONF_SIZE(conf->count))
264 for (i = 0; i < conf->count; i++) {
265 switch (conf->ops[i].length)
268 conf->ops[i].val.v1 = (uint8_t) conf->ops[i].val.v8;
271 conf->ops[i].val.v2 = (uint16_t) conf->ops[i].val.v8;
274 conf->ops[i].val.v4 = (uint32_t) conf->ops[i].val.v8;
283 conf->csum_flags &= NG_PATCH_CSUM_IPV4|NG_PATCH_CSUM_IPV6;
284 conf->relative_offset = !!conf->relative_offset;
286 newconf = malloc(NG_PATCH_CONF_SIZE(conf->count), M_NETGRAPH, M_WAITOK | M_ZERO);
288 bcopy(conf, newconf, NG_PATCH_CONF_SIZE(conf->count));
291 free(privp->conf, M_NETGRAPH);
293 privp->conf = newconf;
297 case NGM_PATCH_GET_STATS:
298 case NGM_PATCH_CLR_STATS:
299 case NGM_PATCH_GETCLR_STATS:
300 if (msg->header.cmd != NGM_PATCH_CLR_STATS) {
301 NG_MKRESPONSE(resp, msg, sizeof(struct ng_patch_stats), M_WAITOK);
306 bcopy(&(privp->stats), resp->data, sizeof(struct ng_patch_stats));
309 if (msg->header.cmd != NGM_PATCH_GET_STATS)
310 bzero(&(privp->stats), sizeof(struct ng_patch_stats));
314 case NGM_PATCH_GETDLT:
315 NG_MKRESPONSE(resp, msg, sizeof(uint8_t), M_WAITOK);
320 *((uint8_t *) resp->data) = privp->dlt;
324 case NGM_PATCH_SETDLT:
325 if (msg->header.arglen != sizeof(uint8_t))
328 switch (*(uint8_t *) msg->data)
332 privp->dlt = *(uint8_t *) msg->data;
346 NG_RESPOND_MSG(error, node, item, resp);
353 do_patch(priv_p privp, struct mbuf *m, int global_offset)
355 int i, offset, patched = 0;
356 union ng_patch_op_val val;
358 for (i = 0; i < privp->conf->count; i++) {
359 offset = global_offset + privp->conf->ops[i].offset;
361 if (offset + privp->conf->ops[i].length > m->m_pkthdr.len)
364 /* for "=" operation we don't need to copy data from mbuf */
365 if (privp->conf->ops[i].mode != NG_PATCH_MODE_SET)
366 m_copydata(m, offset, privp->conf->ops[i].length, (caddr_t) &val);
368 switch (privp->conf->ops[i].length)
371 switch (privp->conf->ops[i].mode)
373 case NG_PATCH_MODE_SET:
374 val.v1 = privp->conf->ops[i].val.v1;
376 case NG_PATCH_MODE_ADD:
377 val.v1 += privp->conf->ops[i].val.v1;
379 case NG_PATCH_MODE_SUB:
380 val.v1 -= privp->conf->ops[i].val.v1;
382 case NG_PATCH_MODE_MUL:
383 val.v1 *= privp->conf->ops[i].val.v1;
385 case NG_PATCH_MODE_DIV:
386 val.v1 /= privp->conf->ops[i].val.v1;
388 case NG_PATCH_MODE_NEG:
389 *((int8_t *) &val) = - *((int8_t *) &val);
391 case NG_PATCH_MODE_AND:
392 val.v1 &= privp->conf->ops[i].val.v1;
394 case NG_PATCH_MODE_OR:
395 val.v1 |= privp->conf->ops[i].val.v1;
397 case NG_PATCH_MODE_XOR:
398 val.v1 ^= privp->conf->ops[i].val.v1;
400 case NG_PATCH_MODE_SHL:
401 val.v1 <<= privp->conf->ops[i].val.v1;
403 case NG_PATCH_MODE_SHR:
404 val.v1 >>= privp->conf->ops[i].val.v1;
410 val.v2 = ntohs(val.v2);
412 switch (privp->conf->ops[i].mode)
414 case NG_PATCH_MODE_SET:
415 val.v2 = privp->conf->ops[i].val.v2;
417 case NG_PATCH_MODE_ADD:
418 val.v2 += privp->conf->ops[i].val.v2;
420 case NG_PATCH_MODE_SUB:
421 val.v2 -= privp->conf->ops[i].val.v2;
423 case NG_PATCH_MODE_MUL:
424 val.v2 *= privp->conf->ops[i].val.v2;
426 case NG_PATCH_MODE_DIV:
427 val.v2 /= privp->conf->ops[i].val.v2;
429 case NG_PATCH_MODE_NEG:
430 *((int16_t *) &val) = - *((int16_t *) &val);
432 case NG_PATCH_MODE_AND:
433 val.v2 &= privp->conf->ops[i].val.v2;
435 case NG_PATCH_MODE_OR:
436 val.v2 |= privp->conf->ops[i].val.v2;
438 case NG_PATCH_MODE_XOR:
439 val.v2 ^= privp->conf->ops[i].val.v2;
441 case NG_PATCH_MODE_SHL:
442 val.v2 <<= privp->conf->ops[i].val.v2;
444 case NG_PATCH_MODE_SHR:
445 val.v2 >>= privp->conf->ops[i].val.v2;
449 val.v2 = htons(val.v2);
454 val.v4 = ntohl(val.v4);
456 switch (privp->conf->ops[i].mode)
458 case NG_PATCH_MODE_SET:
459 val.v4 = privp->conf->ops[i].val.v4;
461 case NG_PATCH_MODE_ADD:
462 val.v4 += privp->conf->ops[i].val.v4;
464 case NG_PATCH_MODE_SUB:
465 val.v4 -= privp->conf->ops[i].val.v4;
467 case NG_PATCH_MODE_MUL:
468 val.v4 *= privp->conf->ops[i].val.v4;
470 case NG_PATCH_MODE_DIV:
471 val.v4 /= privp->conf->ops[i].val.v4;
473 case NG_PATCH_MODE_NEG:
474 *((int32_t *) &val) = - *((int32_t *) &val);
476 case NG_PATCH_MODE_AND:
477 val.v4 &= privp->conf->ops[i].val.v4;
479 case NG_PATCH_MODE_OR:
480 val.v4 |= privp->conf->ops[i].val.v4;
482 case NG_PATCH_MODE_XOR:
483 val.v4 ^= privp->conf->ops[i].val.v4;
485 case NG_PATCH_MODE_SHL:
486 val.v4 <<= privp->conf->ops[i].val.v4;
488 case NG_PATCH_MODE_SHR:
489 val.v4 >>= privp->conf->ops[i].val.v4;
493 val.v4 = htonl(val.v4);
498 val.v8 = be64toh(val.v8);
500 switch (privp->conf->ops[i].mode)
502 case NG_PATCH_MODE_SET:
503 val.v8 = privp->conf->ops[i].val.v8;
505 case NG_PATCH_MODE_ADD:
506 val.v8 += privp->conf->ops[i].val.v8;
508 case NG_PATCH_MODE_SUB:
509 val.v8 -= privp->conf->ops[i].val.v8;
511 case NG_PATCH_MODE_MUL:
512 val.v8 *= privp->conf->ops[i].val.v8;
514 case NG_PATCH_MODE_DIV:
515 val.v8 /= privp->conf->ops[i].val.v8;
517 case NG_PATCH_MODE_NEG:
518 *((int64_t *) &val) = - *((int64_t *) &val);
520 case NG_PATCH_MODE_AND:
521 val.v8 &= privp->conf->ops[i].val.v8;
523 case NG_PATCH_MODE_OR:
524 val.v8 |= privp->conf->ops[i].val.v8;
526 case NG_PATCH_MODE_XOR:
527 val.v8 ^= privp->conf->ops[i].val.v8;
529 case NG_PATCH_MODE_SHL:
530 val.v8 <<= privp->conf->ops[i].val.v8;
532 case NG_PATCH_MODE_SHR:
533 val.v8 >>= privp->conf->ops[i].val.v8;
537 val.v8 = htobe64(val.v8);
542 m_copyback(m, offset, privp->conf->ops[i].length, (caddr_t) &val);
547 privp->stats.patched++;
551 ng_patch_rcvdata(hook_p hook, item_p item)
553 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
559 priv->stats.received++;
563 #define PULLUP_CHECK(mbuf, length) do { \
564 pullup_len += length; \
565 if (((mbuf)->m_pkthdr.len < pullup_len) || \
566 (pullup_len > MHLEN)) { \
570 if ((mbuf)->m_len < pullup_len && \
571 (((mbuf) = m_pullup((mbuf), pullup_len)) == NULL)) { \
577 if (priv->conf && hook == priv->in &&
578 m && (m->m_flags & M_PKTHDR)) {
580 m = m_unshare(m, M_NOWAIT);
585 if (priv->conf->relative_offset) {
586 struct ether_header *eh;
587 struct ng_patch_vlan_header *vh;
593 PULLUP_CHECK(m, sizeof(struct ether_header));
594 eh = mtod(m, struct ether_header *);
595 etype = ntohs(eh->ether_type);
597 for (;;) { /* QinQ support */
603 PULLUP_CHECK(m, sizeof(struct ng_patch_vlan_header));
604 vh = (struct ng_patch_vlan_header *) mtodo(m,
605 pullup_len - sizeof(struct ng_patch_vlan_header));
606 etype = ntohs(vh->etype);
624 do_patch(priv, m, pullup_len);
626 m->m_pkthdr.csum_flags |= priv->conf->csum_flags;
634 if (hook == priv->in) {
635 /* return frames on 'in' hook if 'out' not connected */
636 out = priv->out ? priv->out : priv->in;
637 } else if (hook == priv->out && priv->in) {
638 /* pass frames on 'out' hook if 'in' connected */
645 NG_FWD_NEW_DATA(error, item, out, m);
654 priv->stats.dropped++;
660 ng_patch_shutdown(node_p node)
662 const priv_p privdata = NG_NODE_PRIVATE(node);
664 NG_NODE_SET_PRIVATE(node, NULL);
667 if (privdata->conf != NULL)
668 free(privdata->conf, M_NETGRAPH);
670 free(privdata, M_NETGRAPH);
676 ng_patch_disconnect(hook_p hook)
680 priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
682 if (hook == priv->in) {
686 if (hook == priv->out) {
690 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 &&
691 NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) /* already shutting down? */
692 ng_rmnode_self(NG_HOOK_NODE(hook));