Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * m_skbedit.c SKB Editing module |
| 3 | * |
| 4 | * Copyright (c) 2008, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
Stephen Hemminger | 4d98ab0 | 2013-12-06 15:05:07 -0800 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 17 | * |
| 18 | * Authors: Alexander Duyck <alexander.h.duyck@intel.com> |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | #include <string.h> |
| 26 | #include "utils.h" |
| 27 | #include "tc_util.h" |
| 28 | #include <linux/tc_act/tc_skbedit.h> |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 29 | #include <linux/if_packet.h> |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 30 | |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 31 | static void explain(void) |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 32 | { |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 33 | fprintf(stderr, "Usage: ... skbedit <[QM] [PM] [MM] [PT]>\n" |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 34 | "QM = queue_mapping QUEUE_MAPPING\n" |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 35 | "PM = priority PRIORITY\n" |
| 36 | "MM = mark MARK\n" |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 37 | "PT = ptype PACKETYPE\n" |
| 38 | "PACKETYPE = is one of:\n" |
| 39 | " host, otherhost, broadcast, multicast\n" |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 40 | "QUEUE_MAPPING = device transmit queue to use\n" |
| 41 | "PRIORITY = classID to assign to priority field\n" |
| 42 | "MARK = firewall mark to set\n"); |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void |
| 46 | usage(void) |
| 47 | { |
| 48 | explain(); |
| 49 | exit(-1); |
| 50 | } |
| 51 | |
| 52 | static int |
| 53 | parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, |
| 54 | struct nlmsghdr *n) |
| 55 | { |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 56 | int argc = *argc_p; |
| 57 | char **argv = *argv_p; |
| 58 | int ok = 0; |
| 59 | struct rtattr *tail; |
| 60 | unsigned int tmp; |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 61 | __u16 queue_mapping, ptype; |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 62 | __u32 flags = 0, priority, mark; |
Stephen Hemminger | 46a6573 | 2009-02-19 08:59:06 -0800 | [diff] [blame] | 63 | struct tc_skbedit sel = { 0 }; |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 64 | |
| 65 | if (matches(*argv, "skbedit") != 0) |
| 66 | return -1; |
| 67 | |
| 68 | NEXT_ARG(); |
| 69 | |
| 70 | while (argc > 0) { |
| 71 | if (matches(*argv, "queue_mapping") == 0) { |
| 72 | flags |= SKBEDIT_F_QUEUE_MAPPING; |
| 73 | NEXT_ARG(); |
| 74 | if (get_unsigned(&tmp, *argv, 10) || tmp > 65535) { |
| 75 | fprintf(stderr, "Illegal queue_mapping\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | queue_mapping = tmp; |
| 79 | ok++; |
| 80 | } else if (matches(*argv, "priority") == 0) { |
| 81 | flags |= SKBEDIT_F_PRIORITY; |
| 82 | NEXT_ARG(); |
| 83 | if (get_tc_classid(&priority, *argv)) { |
| 84 | fprintf(stderr, "Illegal priority\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | ok++; |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 88 | } else if (matches(*argv, "mark") == 0) { |
| 89 | flags |= SKBEDIT_F_MARK; |
| 90 | NEXT_ARG(); |
jamal | e906975 | 2010-02-15 01:51:01 +0000 | [diff] [blame] | 91 | if (get_u32(&mark, *argv, 0)) { |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 92 | fprintf(stderr, "Illegal mark\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | ok++; |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 96 | } else if (matches(*argv, "ptype") == 0) { |
| 97 | |
| 98 | NEXT_ARG(); |
| 99 | if (matches(*argv, "host") == 0) { |
| 100 | ptype = PACKET_HOST; |
| 101 | } else if (matches(*argv, "broadcast") == 0) { |
| 102 | ptype = PACKET_BROADCAST; |
| 103 | } else if (matches(*argv, "multicast") == 0) { |
| 104 | ptype = PACKET_MULTICAST; |
| 105 | } else if (matches(*argv, "otherhost") == 0) { |
| 106 | ptype = PACKET_OTHERHOST; |
| 107 | } else { |
| 108 | fprintf(stderr, "Illegal ptype (%s)\n", |
| 109 | *argv); |
| 110 | return -1; |
| 111 | } |
| 112 | flags |= SKBEDIT_F_PTYPE; |
| 113 | ok++; |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 114 | } else if (matches(*argv, "help") == 0) { |
| 115 | usage(); |
| 116 | } else { |
| 117 | break; |
| 118 | } |
| 119 | argc--; |
| 120 | argv++; |
| 121 | } |
| 122 | |
Jamal Hadi Salim | 64b7db4 | 2013-12-21 16:38:36 -0500 | [diff] [blame] | 123 | sel.action = TC_ACT_PIPE; |
Phil Sutter | 69f5aff | 2016-07-23 13:28:09 +0200 | [diff] [blame] | 124 | if (argc && !action_a2n(*argv, &sel.action, false)) |
| 125 | NEXT_ARG(); |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 126 | |
| 127 | if (argc) { |
| 128 | if (matches(*argv, "index") == 0) { |
| 129 | NEXT_ARG(); |
| 130 | if (get_u32(&sel.index, *argv, 10)) { |
| 131 | fprintf(stderr, "Pedit: Illegal \"index\"\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | argc--; |
| 135 | argv++; |
| 136 | ok++; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (!ok) { |
| 141 | explain(); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | tail = NLMSG_TAIL(n); |
| 147 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 148 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_PARMS, &sel, sizeof(sel)); |
| 149 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
| 150 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_QUEUE_MAPPING, |
| 151 | &queue_mapping, sizeof(queue_mapping)); |
| 152 | if (flags & SKBEDIT_F_PRIORITY) |
| 153 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_PRIORITY, |
| 154 | &priority, sizeof(priority)); |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 155 | if (flags & SKBEDIT_F_MARK) |
| 156 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK, |
| 157 | &mark, sizeof(mark)); |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 158 | if (flags & SKBEDIT_F_PTYPE) |
| 159 | addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE, |
| 160 | &ptype, sizeof(ptype)); |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 161 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 162 | |
| 163 | *argc_p = argc; |
| 164 | *argv_p = argv; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg) |
| 169 | { |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 170 | struct rtattr *tb[TCA_SKBEDIT_MAX + 1]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 171 | |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 172 | SPRINT_BUF(b1); |
| 173 | __u32 *priority; |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 174 | __u32 *mark; |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 175 | __u16 *queue_mapping, *ptype; |
Jamal Hadi Salim | 02b1d34 | 2013-12-21 16:38:37 -0500 | [diff] [blame] | 176 | struct tc_skbedit *p = NULL; |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 177 | |
| 178 | if (arg == NULL) |
| 179 | return -1; |
| 180 | |
| 181 | parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg); |
| 182 | |
| 183 | if (tb[TCA_SKBEDIT_PARMS] == NULL) { |
| 184 | fprintf(f, "[NULL skbedit parameters]"); |
| 185 | return -1; |
| 186 | } |
Jamal Hadi Salim | 02b1d34 | 2013-12-21 16:38:37 -0500 | [diff] [blame] | 187 | p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]); |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 188 | |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 189 | fprintf(f, " skbedit"); |
| 190 | |
| 191 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { |
| 192 | queue_mapping = RTA_DATA(tb[TCA_SKBEDIT_QUEUE_MAPPING]); |
| 193 | fprintf(f, " queue_mapping %u", *queue_mapping); |
| 194 | } |
| 195 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { |
| 196 | priority = RTA_DATA(tb[TCA_SKBEDIT_PRIORITY]); |
| 197 | fprintf(f, " priority %s", sprint_tc_classid(*priority, b1)); |
| 198 | } |
Jamal Hadi Salim | e04dd30 | 2009-12-26 11:12:43 -0800 | [diff] [blame] | 199 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
| 200 | mark = RTA_DATA(tb[TCA_SKBEDIT_MARK]); |
| 201 | fprintf(f, " mark %d", *mark); |
| 202 | } |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 203 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
| 204 | ptype = RTA_DATA(tb[TCA_SKBEDIT_PTYPE]); |
| 205 | if (*ptype == PACKET_HOST) |
| 206 | fprintf(f, " ptype host"); |
| 207 | else if (*ptype == PACKET_BROADCAST) |
| 208 | fprintf(f, " ptype broadcast"); |
| 209 | else if (*ptype == PACKET_MULTICAST) |
| 210 | fprintf(f, " ptype multicast"); |
| 211 | else if (*ptype == PACKET_OTHERHOST) |
| 212 | fprintf(f, " ptype otherhost"); |
| 213 | else |
| 214 | fprintf(f, " ptype %d", *ptype); |
| 215 | } |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 216 | |
Jamal Hadi Salim | 1d1e0fd | 2016-07-02 07:14:33 -0400 | [diff] [blame] | 217 | fprintf(f, "\n\t index %d ref %d bind %d", |
| 218 | p->index, p->refcnt, p->bindcnt); |
Jamal Hadi Salim | 02b1d34 | 2013-12-21 16:38:37 -0500 | [diff] [blame] | 219 | |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 220 | if (show_stats) { |
| 221 | if (tb[TCA_SKBEDIT_TM]) { |
| 222 | struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 223 | |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 224 | print_tm(f, tm); |
| 225 | } |
| 226 | } |
| 227 | |
Jamal Hadi Salim | 02b1d34 | 2013-12-21 16:38:37 -0500 | [diff] [blame] | 228 | fprintf(f, "\n "); |
| 229 | |
Alexander Duyck | f72a7aa | 2009-01-06 19:27:03 -0800 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | struct action_util skbedit_action_util = { |
| 234 | .id = "skbedit", |
| 235 | .parse_aopt = parse_skbedit, |
| 236 | .print_aopt = print_skbedit, |
| 237 | }; |