net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 1 | /* |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2 | * m_pedit.c generic packet editor actions module |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +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 | * |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 9 | * Authors: J Hadi Salim (hadi@cyberus.ca) |
| 10 | * |
| 11 | * TODO: |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 12 | * 1) Big endian broken in some spots |
| 13 | * 2) A lot of this stuff was added on the fly; get a big double-double |
| 14 | * and clean it up at some point. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 15 | * |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <syslog.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
| 25 | #include <arpa/inet.h> |
| 26 | #include <string.h> |
| 27 | #include <dlfcn.h> |
| 28 | #include "utils.h" |
| 29 | #include "tc_util.h" |
| 30 | #include "m_pedit.h" |
| 31 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 32 | static struct m_pedit_util *pedit_list; |
WANG Cong | 1c9af05 | 2014-01-29 15:12:59 -0800 | [diff] [blame] | 33 | static int pedit_debug; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 34 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 35 | static void explain(void) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 36 | { |
Phil Sutter | 1672f42 | 2016-03-22 15:48:33 +0100 | [diff] [blame] | 37 | fprintf(stderr, "Usage: ... pedit munge <MUNGE> [CONTROL]\n"); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 38 | fprintf(stderr, |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 39 | "Where: MUNGE := <RAW>|<LAYERED>\n" |
Stephen Hemminger | 8b62517 | 2016-06-14 14:31:37 -0700 | [diff] [blame] | 40 | "\t<RAW>:= <OFFSETC>[ATC]<CMD>\n \t\tOFFSETC:= offset <offval> <u8|u16|u32>\n" |
| 41 | "\t\tATC:= at <atval> offmask <maskval> shift <shiftval>\n" |
| 42 | "\t\tNOTE: offval is byte offset, must be multiple of 4\n" |
| 43 | "\t\tNOTE: maskval is a 32 bit hex number\n \t\tNOTE: shiftval is a is a shift value\n" |
| 44 | "\t\tCMD:= clear | invert | set <setval>| retain\n" |
| 45 | "\t<LAYERED>:= ip <ipdata> | ip6 <ip6data>\n" |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 46 | " \t\t| udp <udpdata> | tcp <tcpdata> | icmp <icmpdata>\n" |
Phil Sutter | 1672f42 | 2016-03-22 15:48:33 +0100 | [diff] [blame] | 47 | "\tCONTROL:= reclassify | pipe | drop | continue | pass\n" |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 48 | "For Example usage look at the examples directory\n"); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 49 | |
| 50 | } |
| 51 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 52 | static void usage(void) |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 53 | { |
| 54 | explain(); |
| 55 | exit(-1); |
| 56 | } |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 57 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 58 | static int pedit_parse_nopopt(int *argc_p, char ***argv_p, |
| 59 | struct tc_pedit_sel *sel, |
| 60 | struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 61 | { |
| 62 | int argc = *argc_p; |
| 63 | char **argv = *argv_p; |
| 64 | |
| 65 | if (argc) { |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 66 | fprintf(stderr, |
| 67 | "Unknown action hence option \"%s\" is unparsable\n", |
| 68 | *argv); |
| 69 | return -1; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | |
| 74 | } |
| 75 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 76 | static struct m_pedit_util *get_pedit_kind(const char *str) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 77 | { |
2!tgraf | fe102f6 | 2005-01-18 00:09:02 +0000 | [diff] [blame] | 78 | static void *pBODY; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 79 | void *dlh; |
| 80 | char buf[256]; |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 81 | struct m_pedit_util *p; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 82 | |
| 83 | for (p = pedit_list; p; p = p->next) { |
| 84 | if (strcmp(p->id, str) == 0) |
| 85 | return p; |
| 86 | } |
| 87 | |
| 88 | snprintf(buf, sizeof(buf), "p_%s.so", str); |
| 89 | dlh = dlopen(buf, RTLD_LAZY); |
| 90 | if (dlh == NULL) { |
| 91 | dlh = pBODY; |
| 92 | if (dlh == NULL) { |
| 93 | dlh = pBODY = dlopen(NULL, RTLD_LAZY); |
| 94 | if (dlh == NULL) |
| 95 | goto noexist; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | snprintf(buf, sizeof(buf), "p_pedit_%s", str); |
| 100 | p = dlsym(dlh, buf); |
| 101 | if (p == NULL) |
| 102 | goto noexist; |
| 103 | |
| 104 | reg: |
| 105 | p->next = pedit_list; |
| 106 | pedit_list = p; |
| 107 | return p; |
| 108 | |
| 109 | noexist: |
Phil Sutter | f89bb02 | 2016-07-18 16:48:43 +0200 | [diff] [blame] | 110 | p = calloc(1, sizeof(*p)); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 111 | if (p) { |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 112 | strncpy(p->id, str, sizeof(p->id) - 1); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 113 | p->parse_peopt = pedit_parse_nopopt; |
| 114 | goto reg; |
| 115 | } |
| 116 | return p; |
| 117 | } |
| 118 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 119 | int pack_key(struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 120 | { |
| 121 | int hwm = sel->nkeys; |
| 122 | |
| 123 | if (hwm >= MAX_OFFS) |
| 124 | return -1; |
| 125 | |
| 126 | if (tkey->off % 4) { |
| 127 | fprintf(stderr, "offsets MUST be in 32 bit boundaries\n"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | sel->keys[hwm].val = tkey->val; |
| 132 | sel->keys[hwm].mask = tkey->mask; |
| 133 | sel->keys[hwm].off = tkey->off; |
| 134 | sel->keys[hwm].at = tkey->at; |
| 135 | sel->keys[hwm].offmask = tkey->offmask; |
| 136 | sel->keys[hwm].shift = tkey->shift; |
| 137 | sel->nkeys++; |
| 138 | return 0; |
| 139 | } |
| 140 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 141 | int pack_key32(__u32 retain, struct tc_pedit_sel *sel, |
| 142 | struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 143 | { |
| 144 | if (tkey->off > (tkey->off & ~3)) { |
| 145 | fprintf(stderr, |
| 146 | "pack_key32: 32 bit offsets must begin in 32bit boundaries\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | tkey->val = htonl(tkey->val & retain); |
| 151 | tkey->mask = htonl(tkey->mask | ~retain); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 152 | return pack_key(sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 155 | int pack_key16(__u32 retain, struct tc_pedit_sel *sel, |
| 156 | struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 157 | { |
Maciej Żenczykowski | 0bbca04 | 2015-06-25 02:03:02 -0700 | [diff] [blame] | 158 | int ind, stride; |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 159 | __u32 m[4] = { 0x0000FFFF, 0xFF0000FF, 0xFFFF0000 }; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 160 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 161 | if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) { |
| 162 | fprintf(stderr, "pack_key16 bad value\n"); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | ind = tkey->off & 3; |
| 167 | |
Maciej Żenczykowski | 0bbca04 | 2015-06-25 02:03:02 -0700 | [diff] [blame] | 168 | if (ind == 3) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 169 | fprintf(stderr, "pack_key16 bad index value %d\n", ind); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 170 | return -1; |
| 171 | } |
| 172 | |
Phil Sutter | 77bed40 | 2016-03-22 15:16:22 +0100 | [diff] [blame] | 173 | stride = 8 * (2 - ind); |
| 174 | tkey->val = htonl((tkey->val & retain) << stride); |
| 175 | tkey->mask = htonl(((tkey->mask | ~retain) << stride) | m[ind]); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 176 | |
| 177 | tkey->off &= ~3; |
| 178 | |
| 179 | if (pedit_debug) |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 180 | printf("pack_key16: Final val %08x mask %08x\n", |
| 181 | tkey->val, tkey->mask); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 182 | return pack_key(sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 183 | |
| 184 | } |
| 185 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 186 | int pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 187 | { |
Maciej Żenczykowski | 0bbca04 | 2015-06-25 02:03:02 -0700 | [diff] [blame] | 188 | int ind, stride; |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 189 | __u32 m[4] = { 0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00 }; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 190 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 191 | if (tkey->val > 0xFF || tkey->mask > 0xFF) { |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 192 | fprintf(stderr, "pack_key8 bad value (val %x mask %x\n", |
| 193 | tkey->val, tkey->mask); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | ind = tkey->off & 3; |
Maciej Żenczykowski | 0bbca04 | 2015-06-25 02:03:02 -0700 | [diff] [blame] | 198 | |
Phil Sutter | 77bed40 | 2016-03-22 15:16:22 +0100 | [diff] [blame] | 199 | stride = 8 * (3 - ind); |
| 200 | tkey->val = htonl((tkey->val & retain) << stride); |
| 201 | tkey->mask = htonl(((tkey->mask | ~retain) << stride) | m[ind]); |
Maciej Żenczykowski | 0bbca04 | 2015-06-25 02:03:02 -0700 | [diff] [blame] | 202 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 203 | tkey->off &= ~3; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 204 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 205 | if (pedit_debug) |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 206 | printf("pack_key8: Final word off %d val %08x mask %08x\n", |
| 207 | tkey->off, tkey->val, tkey->mask); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 208 | return pack_key(sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Stephen Hemminger | 8b62517 | 2016-06-14 14:31:37 -0700 | [diff] [blame] | 211 | int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 212 | { |
| 213 | int argc = *argc_p; |
| 214 | char **argv = *argv_p; |
| 215 | |
| 216 | if (argc <= 0) |
| 217 | return -1; |
| 218 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 219 | if (type == TINT) |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 220 | return get_integer((int *)val, *argv, 0); |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 221 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 222 | if (type == TU32) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 223 | return get_u32(val, *argv, 0); |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 224 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 225 | if (type == TIPV4) { |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 226 | inet_prefix addr; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 227 | |
Stephen Hemminger | 8b62517 | 2016-06-14 14:31:37 -0700 | [diff] [blame] | 228 | if (get_prefix_1(&addr, *argv, AF_INET)) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 229 | return -1; |
Stephen Hemminger | 8b62517 | 2016-06-14 14:31:37 -0700 | [diff] [blame] | 230 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 231 | *val = addr.data[0]; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |
Stephen Hemminger | 8b62517 | 2016-06-14 14:31:37 -0700 | [diff] [blame] | 234 | |
| 235 | if (type == TIPV6) |
| 236 | return -1; /* not implemented yet */ |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 237 | |
| 238 | return -1; |
| 239 | } |
| 240 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 241 | int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, |
| 242 | struct tc_pedit_sel *sel, struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 243 | { |
| 244 | __u32 mask = 0, val = 0; |
| 245 | __u32 o = 0xFF; |
| 246 | int res = -1; |
| 247 | int argc = *argc_p; |
| 248 | char **argv = *argv_p; |
| 249 | |
| 250 | if (argc <= 0) |
| 251 | return -1; |
| 252 | |
| 253 | if (pedit_debug) |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 254 | printf("parse_cmd argc %d %s offset %d length %d\n", |
| 255 | argc, *argv, tkey->off, len); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 256 | |
| 257 | if (len == 2) |
| 258 | o = 0xFFFF; |
| 259 | if (len == 4) |
| 260 | o = 0xFFFFFFFF; |
| 261 | |
| 262 | if (matches(*argv, "invert") == 0) { |
Phil Sutter | f440e9d | 2016-03-02 12:20:30 +0100 | [diff] [blame] | 263 | val = mask = o; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 264 | } else if (matches(*argv, "set") == 0) { |
| 265 | NEXT_ARG(); |
| 266 | if (parse_val(&argc, &argv, &val, type)) |
| 267 | return -1; |
| 268 | } else if (matches(*argv, "preserve") == 0) { |
Phil Sutter | f440e9d | 2016-03-02 12:20:30 +0100 | [diff] [blame] | 269 | retain = 0; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 270 | } else { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 271 | if (matches(*argv, "clear") != 0) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 275 | argc--; |
| 276 | argv++; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 277 | |
| 278 | if (argc && matches(*argv, "retain") == 0) { |
| 279 | NEXT_ARG(); |
| 280 | if (parse_val(&argc, &argv, &retain, TU32)) |
| 281 | return -1; |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 282 | argc--; |
| 283 | argv++; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | tkey->val = val; |
Phil Sutter | f440e9d | 2016-03-02 12:20:30 +0100 | [diff] [blame] | 287 | tkey->mask = mask; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 288 | |
Phil Sutter | 77bed40 | 2016-03-22 15:16:22 +0100 | [diff] [blame] | 289 | if (type == TIPV4) |
| 290 | tkey->val = ntohl(tkey->val); |
| 291 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 292 | if (len == 1) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 293 | res = pack_key8(retain, sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 294 | goto done; |
| 295 | } |
| 296 | if (len == 2) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 297 | res = pack_key16(retain, sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 298 | goto done; |
| 299 | } |
| 300 | if (len == 4) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 301 | res = pack_key32(retain, sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 302 | goto done; |
| 303 | } |
| 304 | |
| 305 | return -1; |
| 306 | done: |
| 307 | if (pedit_debug) |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 308 | printf("parse_cmd done argc %d %s offset %d length %d\n", |
| 309 | argc, *argv, tkey->off, len); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 310 | *argc_p = argc; |
| 311 | *argv_p = argv; |
| 312 | return res; |
| 313 | |
| 314 | } |
| 315 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 316 | int parse_offset(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, |
| 317 | struct tc_pedit_key *tkey) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 318 | { |
| 319 | int off; |
| 320 | __u32 len, retain; |
| 321 | int argc = *argc_p; |
| 322 | char **argv = *argv_p; |
| 323 | int res = -1; |
| 324 | |
| 325 | if (argc <= 0) |
| 326 | return -1; |
| 327 | |
| 328 | if (get_integer(&off, *argv, 0)) |
| 329 | return -1; |
| 330 | tkey->off = off; |
| 331 | |
| 332 | argc--; |
| 333 | argv++; |
| 334 | |
| 335 | if (argc <= 0) |
| 336 | return -1; |
| 337 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 338 | if (matches(*argv, "u32") == 0) { |
| 339 | len = 4; |
| 340 | retain = 0xFFFFFFFF; |
| 341 | goto done; |
| 342 | } |
| 343 | if (matches(*argv, "u16") == 0) { |
| 344 | len = 2; |
Phil Sutter | a33786b | 2016-03-22 15:16:23 +0100 | [diff] [blame] | 345 | retain = 0xffff; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 346 | goto done; |
| 347 | } |
| 348 | if (matches(*argv, "u8") == 0) { |
| 349 | len = 1; |
Phil Sutter | a33786b | 2016-03-22 15:16:23 +0100 | [diff] [blame] | 350 | retain = 0xff; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 351 | goto done; |
| 352 | } |
| 353 | |
| 354 | return -1; |
| 355 | |
| 356 | done: |
| 357 | |
| 358 | NEXT_ARG(); |
| 359 | |
| 360 | /* [at <someval> offmask <maskval> shift <shiftval>] */ |
| 361 | if (matches(*argv, "at") == 0) { |
| 362 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 363 | __u32 atv = 0, offmask = 0x0, shift = 0; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 364 | |
| 365 | NEXT_ARG(); |
| 366 | if (get_u32(&atv, *argv, 0)) |
| 367 | return -1; |
| 368 | tkey->at = atv; |
| 369 | |
| 370 | NEXT_ARG(); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 371 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 372 | if (get_u32(&offmask, *argv, 16)) |
| 373 | return -1; |
| 374 | tkey->offmask = offmask; |
| 375 | |
| 376 | NEXT_ARG(); |
| 377 | |
| 378 | if (get_u32(&shift, *argv, 0)) |
| 379 | return -1; |
| 380 | tkey->shift = shift; |
| 381 | |
| 382 | NEXT_ARG(); |
| 383 | } |
| 384 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 385 | res = parse_cmd(&argc, &argv, len, TU32, retain, sel, tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 386 | |
| 387 | *argc_p = argc; |
| 388 | *argv_p = argv; |
| 389 | return res; |
| 390 | } |
| 391 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 392 | static int parse_munge(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 393 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 394 | struct tc_pedit_key tkey = {}; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 395 | int argc = *argc_p; |
| 396 | char **argv = *argv_p; |
| 397 | int res = -1; |
| 398 | |
| 399 | if (argc <= 0) |
| 400 | return -1; |
| 401 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 402 | if (matches(*argv, "offset") == 0) { |
| 403 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 404 | res = parse_offset(&argc, &argv, sel, &tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 405 | goto done; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 406 | } else { |
| 407 | char k[16]; |
| 408 | struct m_pedit_util *p = NULL; |
| 409 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 410 | strncpy(k, *argv, sizeof(k) - 1); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 411 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 412 | if (argc > 0) { |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 413 | p = get_pedit_kind(k); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 414 | if (p == NULL) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 415 | goto bad_val; |
Phil Sutter | ec0ceee | 2016-03-02 12:20:29 +0100 | [diff] [blame] | 416 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 417 | res = p->parse_peopt(&argc, &argv, sel, &tkey); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 418 | if (res < 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 419 | fprintf(stderr, "bad pedit parsing\n"); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 420 | goto bad_val; |
| 421 | } |
| 422 | goto done; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | bad_val: |
| 427 | return -1; |
| 428 | |
| 429 | done: |
| 430 | |
| 431 | *argc_p = argc; |
| 432 | *argv_p = argv; |
| 433 | return res; |
| 434 | } |
| 435 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 436 | int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, |
| 437 | struct nlmsghdr *n) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 438 | { |
| 439 | struct { |
| 440 | struct tc_pedit_sel sel; |
| 441 | struct tc_pedit_key keys[MAX_OFFS]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 442 | } sel = {}; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 443 | |
| 444 | int argc = *argc_p; |
| 445 | char **argv = *argv_p; |
| 446 | int ok = 0, iok = 0; |
| 447 | struct rtattr *tail; |
| 448 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 449 | while (argc > 0) { |
| 450 | if (pedit_debug > 1) |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 451 | fprintf(stderr, "while pedit (%d:%s)\n", argc, *argv); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 452 | if (matches(*argv, "pedit") == 0) { |
| 453 | NEXT_ARG(); |
| 454 | ok++; |
| 455 | continue; |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 456 | } else if (matches(*argv, "help") == 0) { |
| 457 | usage(); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 458 | } else if (matches(*argv, "munge") == 0) { |
| 459 | if (!ok) { |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 460 | fprintf(stderr, "Bad pedit construct (%s)\n", |
| 461 | *argv); |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 462 | explain(); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 463 | return -1; |
| 464 | } |
| 465 | NEXT_ARG(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 466 | if (parse_munge(&argc, &argv, &sel.sel)) { |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 467 | fprintf(stderr, "Bad pedit construct (%s)\n", |
| 468 | *argv); |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 469 | explain(); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 470 | return -1; |
| 471 | } |
| 472 | ok++; |
| 473 | } else { |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | } |
| 478 | |
| 479 | if (!ok) { |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 480 | explain(); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 481 | return -1; |
| 482 | } |
| 483 | |
Phil Sutter | 69f5aff | 2016-07-23 13:28:09 +0200 | [diff] [blame] | 484 | if (argc && !action_a2n(*argv, &sel.sel.action, false)) |
| 485 | NEXT_ARG(); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 486 | |
| 487 | if (argc) { |
| 488 | if (matches(*argv, "index") == 0) { |
| 489 | NEXT_ARG(); |
| 490 | if (get_u32(&sel.sel.index, *argv, 10)) { |
| 491 | fprintf(stderr, "Pedit: Illegal \"index\"\n"); |
| 492 | return -1; |
| 493 | } |
| 494 | argc--; |
| 495 | argv++; |
| 496 | iok++; |
| 497 | } |
| 498 | } |
| 499 | |
2!tgraf | fc78a8e | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 500 | tail = NLMSG_TAIL(n); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 501 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 502 | addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel, |
| 503 | sizeof(sel.sel) + |
| 504 | sel.sel.nkeys * sizeof(struct tc_pedit_key)); |
| 505 | tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail; |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 506 | |
| 507 | *argc_p = argc; |
| 508 | *argv_p = argv; |
| 509 | return 0; |
| 510 | } |
| 511 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 512 | int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 513 | { |
| 514 | struct tc_pedit_sel *sel; |
| 515 | struct rtattr *tb[TCA_PEDIT_MAX + 1]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 516 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 517 | if (arg == NULL) |
| 518 | return -1; |
| 519 | |
2!tgraf | 7893400 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 520 | parse_rtattr_nested(tb, TCA_PEDIT_MAX, arg); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 521 | |
| 522 | if (tb[TCA_PEDIT_PARMS] == NULL) { |
| 523 | fprintf(f, "[NULL pedit parameters]"); |
| 524 | return -1; |
| 525 | } |
| 526 | sel = RTA_DATA(tb[TCA_PEDIT_PARMS]); |
| 527 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 528 | fprintf(f, " pedit action %s keys %d\n ", |
Phil Sutter | 7093200 | 2016-07-23 13:28:10 +0200 | [diff] [blame] | 529 | action_n2a(sel->action), sel->nkeys); |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 530 | fprintf(f, "\t index %d ref %d bind %d", sel->index, sel->refcnt, |
| 531 | sel->bindcnt); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 532 | |
| 533 | if (show_stats) { |
| 534 | if (tb[TCA_PEDIT_TM]) { |
| 535 | struct tcf_t *tm = RTA_DATA(tb[TCA_PEDIT_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 536 | |
| 537 | print_tm(f, tm); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | if (sel->nkeys) { |
| 541 | int i; |
| 542 | struct tc_pedit_key *key = sel->keys; |
| 543 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 544 | for (i = 0; i < sel->nkeys; i++, key++) { |
| 545 | fprintf(f, "\n\t key #%d", i); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 546 | fprintf(f, " at %d: val %08x mask %08x", |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 547 | (unsigned int)key->off, |
| 548 | (unsigned int)ntohl(key->val), |
| 549 | (unsigned int)ntohl(key->mask)); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 550 | } |
| 551 | } else { |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 552 | fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index, |
| 553 | sel->nkeys); |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 554 | } |
| 555 | |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 556 | fprintf(f, "\n "); |
| 557 | return 0; |
| 558 | } |
| 559 | |
Jamal Hadi Salim | d8694a3 | 2016-06-12 17:40:34 -0400 | [diff] [blame] | 560 | int pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats) |
net[shemminger]!shemminger | 7e7c737 | 2005-01-17 23:32:51 +0000 | [diff] [blame] | 561 | { |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | struct action_util pedit_action_util = { |
| 566 | .id = "pedit", |
| 567 | .parse_aopt = parse_pedit, |
| 568 | .print_aopt = print_pedit, |
| 569 | }; |