net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 1 | /* |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 2 | * m_egress.c ingress/egress packet mirror/redir actions module |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +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 | * |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 11 | * TODO: Add Ingress support |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <unistd.h> |
| 18 | #include <syslog.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <arpa/inet.h> |
| 23 | #include <string.h> |
| 24 | #include "utils.h" |
| 25 | #include "tc_util.h" |
site!shemminger | c1027a7 | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 26 | #include "tc_common.h" |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 27 | #include <linux/tc_act/tc_mirred.h> |
| 28 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 29 | static void |
| 30 | explain(void) |
| 31 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 32 | fprintf(stderr, "Usage: mirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME>\n"); |
| 33 | fprintf(stderr, "where:\n"); |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 34 | fprintf(stderr, "\tDIRECTION := <ingress | egress>\n"); |
| 35 | fprintf(stderr, "\tACTION := <mirror | redirect>\n"); |
| 36 | fprintf(stderr, "\tINDEX is the specific policy instance id\n"); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 37 | fprintf(stderr, "\tDEVICENAME is the devicename\n"); |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 38 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 41 | static void |
| 42 | usage(void) |
| 43 | { |
| 44 | explain(); |
| 45 | exit(-1); |
| 46 | } |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 47 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 48 | static const char *mirred_n2a(int action) |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 49 | { |
| 50 | switch (action) { |
| 51 | case TCA_EGRESS_REDIR: |
| 52 | return "Egress Redirect"; |
| 53 | case TCA_INGRESS_REDIR: |
| 54 | return "Ingress Redirect"; |
| 55 | case TCA_EGRESS_MIRROR: |
| 56 | return "Egress Mirror"; |
| 57 | case TCA_INGRESS_MIRROR: |
| 58 | return "Ingress Mirror"; |
| 59 | default: |
| 60 | return "unknown"; |
| 61 | } |
| 62 | } |
| 63 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 64 | static int |
| 65 | parse_egress(struct action_util *a, int *argc_p, char ***argv_p, |
| 66 | int tca_id, struct nlmsghdr *n) |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 67 | { |
| 68 | |
| 69 | int argc = *argc_p; |
| 70 | char **argv = *argv_p; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 71 | int ok = 0, iok = 0, mirror = 0, redir = 0; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 72 | struct tc_mirred p; |
| 73 | struct rtattr *tail; |
| 74 | char d[16]; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 75 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 76 | memset(d, 0, sizeof(d)-1); |
| 77 | memset(&p, 0, sizeof(struct tc_mirred)); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 78 | |
| 79 | while (argc > 0) { |
| 80 | |
| 81 | if (matches(*argv, "action") == 0) { |
| 82 | break; |
| 83 | } else if (matches(*argv, "egress") == 0) { |
| 84 | NEXT_ARG(); |
| 85 | ok++; |
| 86 | continue; |
| 87 | } else { |
| 88 | |
| 89 | if (matches(*argv, "index") == 0) { |
| 90 | NEXT_ARG(); |
| 91 | if (get_u32(&p.index, *argv, 10)) { |
| 92 | fprintf(stderr, "Illegal \"index\"\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | iok++; |
| 96 | if (!ok) { |
| 97 | argc--; |
| 98 | argv++; |
| 99 | break; |
| 100 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 101 | } else if (!ok) { |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 102 | fprintf(stderr, "was expecting egress (%s)\n", *argv); |
| 103 | break; |
| 104 | |
| 105 | } else if (!mirror && matches(*argv, "mirror") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 106 | mirror = 1; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 107 | if (redir) { |
Stephen Hemminger | b8a4589 | 2013-08-04 15:10:05 -0700 | [diff] [blame] | 108 | fprintf(stderr, "Can't have both mirror and redir\n"); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | p.eaction = TCA_EGRESS_MIRROR; |
| 112 | p.action = TC_ACT_PIPE; |
| 113 | ok++; |
| 114 | } else if (!redir && matches(*argv, "redirect") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 115 | redir = 1; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 116 | if (mirror) { |
Stephen Hemminger | b8a4589 | 2013-08-04 15:10:05 -0700 | [diff] [blame] | 117 | fprintf(stderr, "Can't have both mirror and redir\n"); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | p.eaction = TCA_EGRESS_REDIR; |
| 121 | p.action = TC_ACT_STOLEN; |
| 122 | ok++; |
| 123 | } else if ((redir || mirror) && matches(*argv, "dev") == 0) { |
| 124 | NEXT_ARG(); |
| 125 | if (strlen(d)) |
| 126 | duparg("dev", *argv); |
| 127 | |
| 128 | strncpy(d, *argv, sizeof(d)-1); |
| 129 | argc--; |
| 130 | argv++; |
| 131 | |
| 132 | break; |
| 133 | |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | NEXT_ARG(); |
| 138 | } |
| 139 | |
| 140 | if (!ok && !iok) { |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | if (d[0]) { |
| 147 | int idx; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 148 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 149 | ll_init_map(&rth); |
| 150 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 151 | if ((idx = ll_name_to_index(d)) == 0) { |
| 152 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | p.ifindex = idx; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | if (argc && p.eaction == TCA_EGRESS_MIRROR) { |
| 161 | |
| 162 | if (matches(*argv, "reclassify") == 0) { |
| 163 | p.action = TC_POLICE_RECLASSIFY; |
| 164 | NEXT_ARG(); |
| 165 | } else if (matches(*argv, "pipe") == 0) { |
| 166 | p.action = TC_POLICE_PIPE; |
| 167 | NEXT_ARG(); |
| 168 | } else if (matches(*argv, "drop") == 0 || |
| 169 | matches(*argv, "shot") == 0) { |
| 170 | p.action = TC_POLICE_SHOT; |
| 171 | NEXT_ARG(); |
| 172 | } else if (matches(*argv, "continue") == 0) { |
| 173 | p.action = TC_POLICE_UNSPEC; |
| 174 | NEXT_ARG(); |
| 175 | } else if (matches(*argv, "pass") == 0) { |
| 176 | p.action = TC_POLICE_OK; |
| 177 | NEXT_ARG(); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | if (argc) { |
| 183 | if (iok && matches(*argv, "index") == 0) { |
| 184 | fprintf(stderr, "mirred: Illegal double index\n"); |
| 185 | return -1; |
| 186 | } else { |
| 187 | if (matches(*argv, "index") == 0) { |
| 188 | NEXT_ARG(); |
| 189 | if (get_u32(&p.index, *argv, 10)) { |
| 190 | fprintf(stderr, "mirred: Illegal \"index\"\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | argc--; |
| 194 | argv++; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
3!tgraf | 60a383a | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 199 | tail = NLMSG_TAIL(n); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 200 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 201 | addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p)); |
3!tgraf | 60a383a | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 202 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 203 | |
| 204 | *argc_p = argc; |
| 205 | *argv_p = argv; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 210 | static int |
| 211 | parse_mirred(struct action_util *a, int *argc_p, char ***argv_p, |
| 212 | int tca_id, struct nlmsghdr *n) |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 213 | { |
| 214 | |
| 215 | int argc = *argc_p; |
| 216 | char **argv = *argv_p; |
| 217 | |
| 218 | if (argc < 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 219 | fprintf(stderr, "mirred bad argument count %d\n", argc); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | if (matches(*argv, "mirred") == 0) { |
| 224 | NEXT_ARG(); |
| 225 | } else { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 226 | fprintf(stderr, "mirred bad argument %s\n", *argv); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | if (matches(*argv, "egress") == 0 || matches(*argv, "index") == 0) { |
| 232 | int ret = parse_egress(a, &argc, &argv, tca_id, n); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 233 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 234 | if (ret == 0) { |
| 235 | *argc_p = argc; |
| 236 | *argv_p = argv; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | } else if (matches(*argv, "ingress") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 241 | fprintf(stderr, "mirred ingress not supported at the moment\n"); |
Jamal Hadi Salim | ebf3208 | 2006-08-08 12:10:08 -0700 | [diff] [blame] | 242 | } else if (matches(*argv, "help") == 0) { |
| 243 | usage(); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 244 | } else { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 245 | fprintf(stderr, "mirred option not supported %s\n", *argv); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return -1; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 249 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 252 | static int |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 253 | print_mirred(struct action_util *au, FILE * f, struct rtattr *arg) |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 254 | { |
| 255 | struct tc_mirred *p; |
| 256 | struct rtattr *tb[TCA_MIRRED_MAX + 1]; |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 257 | const char *dev; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 258 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 259 | SPRINT_BUF(b1); |
| 260 | |
| 261 | if (arg == NULL) |
| 262 | return -1; |
| 263 | |
3!tgraf | 5cb5ee3 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 264 | parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 265 | |
| 266 | if (tb[TCA_MIRRED_PARMS] == NULL) { |
| 267 | fprintf(f, "[NULL mirred parameters]"); |
| 268 | return -1; |
| 269 | } |
| 270 | p = RTA_DATA(tb[TCA_MIRRED_PARMS]); |
| 271 | |
osdl.net!shemminger | 6ce88ca | 2005-03-30 18:43:30 +0000 | [diff] [blame] | 272 | /* |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 273 | ll_init_map(&rth); |
osdl.net!shemminger | 6ce88ca | 2005-03-30 18:43:30 +0000 | [diff] [blame] | 274 | */ |
| 275 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 276 | |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 277 | if ((dev = ll_index_to_name(p->ifindex)) == 0) { |
| 278 | fprintf(stderr, "Cannot find device %d\n", p->ifindex); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 279 | return -1; |
| 280 | } |
| 281 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 282 | fprintf(f, "mirred (%s to device %s) %s", mirred_n2a(p->eaction), dev, action_n2a(p->action, b1, sizeof (b1))); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 283 | |
| 284 | fprintf(f, "\n "); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 285 | fprintf(f, "\tindex %d ref %d bind %d", p->index, p->refcnt, p->bindcnt); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 286 | |
| 287 | if (show_stats) { |
| 288 | if (tb[TCA_MIRRED_TM]) { |
| 289 | struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 290 | |
| 291 | print_tm(f, tm); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | fprintf(f, "\n "); |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
osdl.net!shemminger | 6ce88ca | 2005-03-30 18:43:30 +0000 | [diff] [blame] | 298 | struct action_util mirred_action_util = { |
net[shemminger]!shemminger | 00fa848 | 2004-12-08 20:13:56 +0000 | [diff] [blame] | 299 | .id = "mirred", |
| 300 | .parse_aopt = parse_mirred, |
| 301 | .print_aopt = print_mirred, |
| 302 | }; |