shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * f_basic.c Basic Classifier |
| 3 | * |
Stephen Hemminger | 84d6688 | 2008-02-07 22:10:14 -0800 | [diff] [blame] | 4 | * This program is free software; you can distribute it and/or |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 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 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <arpa/inet.h> |
| 21 | #include <string.h> |
| 22 | #include <linux/if.h> |
| 23 | |
| 24 | #include "utils.h" |
| 25 | #include "tc_util.h" |
| 26 | #include "m_ematch.h" |
| 27 | |
| 28 | static void explain(void) |
| 29 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 30 | fprintf(stderr, "Usage: ... basic [ match EMATCH_TREE ]\n"); |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 31 | fprintf(stderr, " [ action ACTION_SPEC ] [ classid CLASSID ]\n"); |
| 32 | fprintf(stderr, "\n"); |
| 33 | fprintf(stderr, "Where: SELECTOR := SAMPLE SAMPLE ...\n"); |
| 34 | fprintf(stderr, " FILTERID := X:Y:Z\n"); |
Jamal Hadi Salim | 863ecb0 | 2014-10-06 07:41:21 -0400 | [diff] [blame] | 35 | fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n"); |
PJ Waskiewicz | e9acc24 | 2008-02-13 03:49:09 -0800 | [diff] [blame] | 36 | fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n"); |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int basic_parse_opt(struct filter_util *qu, char *handle, |
| 40 | int argc, char **argv, struct nlmsghdr *n) |
| 41 | { |
| 42 | struct tcmsg *t = NLMSG_DATA(n); |
| 43 | struct rtattr *tail; |
| 44 | long h = 0; |
| 45 | |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 46 | if (handle) { |
| 47 | h = strtol(handle, NULL, 0); |
| 48 | if (h == LONG_MIN || h == LONG_MAX) { |
| 49 | fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n", |
| 50 | handle); |
| 51 | return -1; |
| 52 | } |
| 53 | } |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 54 | t->tcm_handle = h; |
| 55 | |
WANG Cong | 285e776 | 2015-05-05 15:30:20 -0700 | [diff] [blame] | 56 | if (argc == 0) |
| 57 | return 0; |
| 58 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 59 | tail = (struct rtattr *)(((void *)n)+NLMSG_ALIGN(n->nlmsg_len)); |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 60 | addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
| 61 | |
| 62 | while (argc > 0) { |
| 63 | if (matches(*argv, "match") == 0) { |
| 64 | NEXT_ARG(); |
| 65 | if (parse_ematch(&argc, &argv, TCA_BASIC_EMATCHES, n)) { |
| 66 | fprintf(stderr, "Illegal \"ematch\"\n"); |
| 67 | return -1; |
| 68 | } |
| 69 | continue; |
| 70 | } else if (matches(*argv, "classid") == 0 || |
| 71 | strcmp(*argv, "flowid") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 72 | unsigned int handle; |
| 73 | |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 74 | NEXT_ARG(); |
| 75 | if (get_tc_classid(&handle, *argv)) { |
| 76 | fprintf(stderr, "Illegal \"classid\"\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | addattr_l(n, MAX_MSG, TCA_BASIC_CLASSID, &handle, 4); |
| 80 | } else if (matches(*argv, "action") == 0) { |
| 81 | NEXT_ARG(); |
| 82 | if (parse_action(&argc, &argv, TCA_BASIC_ACT, n)) { |
| 83 | fprintf(stderr, "Illegal \"action\"\n"); |
| 84 | return -1; |
| 85 | } |
| 86 | continue; |
| 87 | |
| 88 | } else if (matches(*argv, "police") == 0) { |
| 89 | NEXT_ARG(); |
| 90 | if (parse_police(&argc, &argv, TCA_BASIC_POLICE, n)) { |
| 91 | fprintf(stderr, "Illegal \"police\"\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | continue; |
| 95 | } else if (strcmp(*argv, "help") == 0) { |
| 96 | explain(); |
| 97 | return -1; |
| 98 | } else { |
| 99 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 100 | explain(); |
| 101 | return -1; |
| 102 | } |
| 103 | argc--; argv++; |
| 104 | } |
| 105 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 106 | tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail; |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int basic_print_opt(struct filter_util *qu, FILE *f, |
| 111 | struct rtattr *opt, __u32 handle) |
| 112 | { |
| 113 | struct rtattr *tb[TCA_BASIC_MAX+1]; |
| 114 | |
| 115 | if (opt == NULL) |
| 116 | return 0; |
| 117 | |
| 118 | parse_rtattr_nested(tb, TCA_BASIC_MAX, opt); |
| 119 | |
| 120 | if (handle) |
| 121 | fprintf(f, "handle 0x%x ", handle); |
| 122 | |
| 123 | if (tb[TCA_BASIC_CLASSID]) { |
| 124 | SPRINT_BUF(b1); |
| 125 | fprintf(f, "flowid %s ", |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 126 | sprint_tc_classid(rta_getattr_u32(tb[TCA_BASIC_CLASSID]), b1)); |
shemminger | c428e91 | 2005-06-23 20:29:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (tb[TCA_BASIC_EMATCHES]) |
| 130 | print_ematch(f, tb[TCA_BASIC_EMATCHES]); |
| 131 | |
| 132 | if (tb[TCA_BASIC_POLICE]) { |
| 133 | fprintf(f, "\n"); |
| 134 | tc_print_police(f, tb[TCA_BASIC_POLICE]); |
| 135 | } |
| 136 | |
| 137 | if (tb[TCA_BASIC_ACT]) { |
| 138 | tc_print_action(f, tb[TCA_BASIC_ACT]); |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | struct filter_util basic_filter_util = { |
| 145 | .id = "basic", |
| 146 | .parse_fopt = basic_parse_opt, |
| 147 | .print_fopt = basic_print_opt, |
| 148 | }; |