2
* lib/route/cls/ematch/nbyte.c Nbyte comparison
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation version 2.1
9
* Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
14
* @defgroup em_nbyte N-Byte Comparison
19
#include <netlink-local.h>
20
#include <netlink-tc.h>
21
#include <netlink/netlink.h>
22
#include <netlink/route/cls/ematch.h>
23
#include <netlink/route/cls/ematch/nbyte.h>
27
struct tcf_em_nbyte cfg;
31
void rtnl_ematch_nbyte_set_offset(struct rtnl_ematch *e, uint8_t layer,
34
struct nbyte_data *n = rtnl_ematch_data(e);
39
uint16_t rtnl_ematch_nbyte_get_offset(struct rtnl_ematch *e)
41
return ((struct nbyte_data *) rtnl_ematch_data(e))->cfg.off;
44
uint8_t rtnl_ematch_nbyte_get_layer(struct rtnl_ematch *e)
46
return ((struct nbyte_data *) rtnl_ematch_data(e))->cfg.layer;
49
void rtnl_ematch_nbyte_set_pattern(struct rtnl_ematch *e,
50
uint8_t *pattern, size_t len)
52
struct nbyte_data *n = rtnl_ematch_data(e);
61
uint8_t *rtnl_ematch_nbyte_get_pattern(struct rtnl_ematch *e)
63
return ((struct nbyte_data *) rtnl_ematch_data(e))->pattern;
66
size_t rtnl_ematch_nbyte_get_len(struct rtnl_ematch *e)
68
return ((struct nbyte_data *) rtnl_ematch_data(e))->cfg.len;
71
static const char *layer_txt(struct tcf_em_nbyte *nbyte)
73
switch (nbyte->layer) {
76
case TCF_LAYER_NETWORK:
78
case TCF_LAYER_TRANSPORT:
85
static int nbyte_parse(struct rtnl_ematch *e, void *data, size_t len)
87
struct nbyte_data *n = rtnl_ematch_data(e);
88
size_t hdrlen = sizeof(struct tcf_em_nbyte);
89
size_t plen = len - hdrlen;
91
memcpy(&n->cfg, data, hdrlen);
93
if (!(n->pattern = calloc(1, plen)))
96
memcpy(n->pattern, data + hdrlen, plen);
102
static void nbyte_dump(struct rtnl_ematch *e, struct nl_dump_params *p)
104
struct nbyte_data *n = rtnl_ematch_data(e);
107
nl_dump(p, "pattern(%u:[", n->cfg.len);
109
for (i = 0; i < n->cfg.len; i++) {
110
nl_dump(p, "%02x", n->pattern[i]);
111
if (i+1 < n->cfg.len)
115
nl_dump(p, "] at %s+%u)", layer_txt(&n->cfg), n->cfg.off);
118
static void nbyte_free(struct rtnl_ematch *e)
120
struct nbyte_data *n = rtnl_ematch_data(e);
124
static struct rtnl_ematch_ops nbyte_ops = {
125
.eo_kind = TCF_EM_NBYTE,
127
.eo_minlen = sizeof(struct tcf_em_nbyte),
128
.eo_datalen = sizeof(struct nbyte_data),
129
.eo_parse = nbyte_parse,
130
.eo_dump = nbyte_dump,
131
.eo_free = nbyte_free,
134
static void __init nbyte_init(void)
136
rtnl_ematch_register(&nbyte_ops);