shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 1 | /* |
Lionel Elie Mamane | bc45ded | 2007-10-12 10:56:43 +0200 | [diff] [blame] | 2 | * em_cmp.c Simple comparison Ematch |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can distribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Thomas Graf <tgraf@suug.ch> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> |
| 20 | #include <string.h> |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | |
| 23 | #include "m_ematch.h" |
| 24 | #include <linux/tc_ematch/tc_em_cmp.h> |
| 25 | |
| 26 | extern struct ematch_util cmp_ematch_util; |
| 27 | |
| 28 | static void cmp_print_usage(FILE *fd) |
| 29 | { |
| 30 | fprintf(fd, |
| 31 | "Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \ |
| 32 | "where: ALIGN := { u8 | u16 | u32 }\n" \ |
| 33 | " ATTRS := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \ |
Lionel Elie Mamane | bc45ded | 2007-10-12 10:56:43 +0200 | [diff] [blame] | 34 | " LAYER := { link | network | transport | 0..%d }\n" \ |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 35 | "\n" \ |
| 36 | "Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n", |
| 37 | TCF_LAYER_MAX); |
| 38 | } |
| 39 | |
| 40 | static int cmp_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr, |
| 41 | struct bstr *args) |
| 42 | { |
| 43 | struct bstr *a; |
| 44 | int align, opnd = 0; |
| 45 | unsigned long offset = 0, layer = TCF_LAYER_NETWORK, mask = 0, value = 0; |
| 46 | int offset_present = 0, value_present = 0; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 47 | struct tcf_em_cmp cmp = {}; |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 48 | |
| 49 | #define PARSE_ERR(CARG, FMT, ARGS...) \ |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 50 | em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT, ##ARGS) |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 51 | |
| 52 | if (args == NULL) |
| 53 | return PARSE_ERR(args, "cmp: missing arguments"); |
| 54 | |
| 55 | if (!bstrcmp(args, "u8")) |
| 56 | align = TCF_EM_ALIGN_U8; |
| 57 | else if (!bstrcmp(args, "u16")) |
| 58 | align = TCF_EM_ALIGN_U16; |
| 59 | else if (!bstrcmp(args, "u32")) |
| 60 | align = TCF_EM_ALIGN_U32; |
| 61 | else |
| 62 | return PARSE_ERR(args, "cmp: invalid alignment"); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 63 | |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 64 | for (a = bstr_next(args); a; a = bstr_next(a)) { |
| 65 | if (!bstrcmp(a, "at")) { |
| 66 | if (a->next == NULL) |
| 67 | return PARSE_ERR(a, "cmp: missing argument"); |
| 68 | a = bstr_next(a); |
| 69 | |
| 70 | offset = bstrtoul(a); |
| 71 | if (offset == ULONG_MAX) |
| 72 | return PARSE_ERR(a, "cmp: invalid offset, " \ |
| 73 | "must be numeric"); |
| 74 | |
| 75 | offset_present = 1; |
| 76 | } else if (!bstrcmp(a, "layer")) { |
| 77 | if (a->next == NULL) |
| 78 | return PARSE_ERR(a, "cmp: missing argument"); |
| 79 | a = bstr_next(a); |
| 80 | |
| 81 | layer = parse_layer(a); |
| 82 | if (layer == INT_MAX) { |
| 83 | layer = bstrtoul(a); |
| 84 | if (layer == ULONG_MAX) |
| 85 | return PARSE_ERR(a, "cmp: invalid " \ |
| 86 | "layer"); |
| 87 | } |
| 88 | |
| 89 | if (layer > TCF_LAYER_MAX) |
| 90 | return PARSE_ERR(a, "cmp: illegal layer, " \ |
| 91 | "must be in 0..%d", TCF_LAYER_MAX); |
| 92 | } else if (!bstrcmp(a, "mask")) { |
| 93 | if (a->next == NULL) |
| 94 | return PARSE_ERR(a, "cmp: missing argument"); |
| 95 | a = bstr_next(a); |
| 96 | |
| 97 | mask = bstrtoul(a); |
| 98 | if (mask == ULONG_MAX) |
| 99 | return PARSE_ERR(a, "cmp: invalid mask"); |
| 100 | } else if (!bstrcmp(a, "trans")) { |
| 101 | cmp.flags |= TCF_EM_CMP_TRANS; |
| 102 | } else if (!bstrcmp(a, "eq") || !bstrcmp(a, "gt") || |
| 103 | !bstrcmp(a, "lt")) { |
| 104 | |
| 105 | if (!bstrcmp(a, "eq")) |
| 106 | opnd = TCF_EM_OPND_EQ; |
| 107 | else if (!bstrcmp(a, "gt")) |
| 108 | opnd = TCF_EM_OPND_GT; |
| 109 | else if (!bstrcmp(a, "lt")) |
| 110 | opnd = TCF_EM_OPND_LT; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 111 | |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 112 | if (a->next == NULL) |
| 113 | return PARSE_ERR(a, "cmp: missing argument"); |
| 114 | a = bstr_next(a); |
| 115 | |
| 116 | value = bstrtoul(a); |
| 117 | if (value == ULONG_MAX) |
| 118 | return PARSE_ERR(a, "cmp: invalid value"); |
| 119 | |
| 120 | value_present = 1; |
| 121 | } else |
| 122 | return PARSE_ERR(a, "nbyte: unknown parameter"); |
| 123 | } |
| 124 | |
| 125 | if (offset_present == 0 || value_present == 0) |
| 126 | return PARSE_ERR(a, "cmp: offset and value required"); |
| 127 | |
| 128 | cmp.val = (__u32) value; |
| 129 | cmp.mask = (__u32) mask; |
| 130 | cmp.off = (__u16) offset; |
| 131 | cmp.align = (__u8) align; |
| 132 | cmp.layer = (__u8) layer; |
| 133 | cmp.opnd = (__u8) opnd; |
| 134 | |
| 135 | addraw_l(n, MAX_MSG, hdr, sizeof(*hdr)); |
| 136 | addraw_l(n, MAX_MSG, &cmp, sizeof(cmp)); |
| 137 | |
| 138 | #undef PARSE_ERR |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, |
| 143 | int data_len) |
| 144 | { |
| 145 | struct tcf_em_cmp *cmp = data; |
| 146 | |
| 147 | if (data_len < sizeof(*cmp)) { |
| 148 | fprintf(stderr, "CMP header size mismatch\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | if (cmp->align == TCF_EM_ALIGN_U8) |
| 153 | fprintf(fd, "u8 "); |
| 154 | else if (cmp->align == TCF_EM_ALIGN_U16) |
| 155 | fprintf(fd, "u16 "); |
Thomas Jarosch | 6d5ee98 | 2011-10-03 05:22:56 +0000 | [diff] [blame] | 156 | else if (cmp->align == TCF_EM_ALIGN_U32) |
shemminger | 311b414 | 2005-06-23 20:25:16 +0000 | [diff] [blame] | 157 | fprintf(fd, "u32 "); |
| 158 | |
| 159 | fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer); |
| 160 | |
| 161 | if (cmp->mask) |
| 162 | fprintf(fd, "mask 0x%x ", cmp->mask); |
| 163 | |
| 164 | if (cmp->flags & TCF_EM_CMP_TRANS) |
| 165 | fprintf(fd, "trans "); |
| 166 | |
| 167 | if (cmp->opnd == TCF_EM_OPND_EQ) |
| 168 | fprintf(fd, "eq "); |
| 169 | else if (cmp->opnd == TCF_EM_OPND_LT) |
| 170 | fprintf(fd, "lt "); |
| 171 | else if (cmp->opnd == TCF_EM_OPND_GT) |
| 172 | fprintf(fd, "gt "); |
| 173 | |
| 174 | fprintf(fd, "%d", cmp->val); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | struct ematch_util cmp_ematch_util = { |
| 180 | .kind = "cmp", |
| 181 | .kind_num = TCF_EM_CMP, |
| 182 | .parse_eopt = cmp_parse_eopt, |
| 183 | .print_eopt = cmp_print_eopt, |
| 184 | .print_usage = cmp_print_usage |
| 185 | }; |