osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * f_fw.c FW filter. |
| 3 | * |
| 4 | * This program is free software; you can redistribute 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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 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> |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 22 | #include <linux/if.h> /* IFNAMSIZ */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 23 | #include "utils.h" |
| 24 | #include "tc_util.h" |
| 25 | |
| 26 | static void explain(void) |
| 27 | { |
Jamal Hadi Salim | 863ecb0 | 2014-10-06 07:41:21 -0400 | [diff] [blame] | 28 | fprintf(stderr, "Usage: ... fw [ classid CLASSID ] [ action ACTION_SPEC ]\n"); |
| 29 | fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 30 | fprintf(stderr, " CLASSID := X:Y\n"); |
PJ Waskiewicz | e9acc24 | 2008-02-13 03:49:09 -0800 | [diff] [blame] | 31 | fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 32 | } |
| 33 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n) |
| 35 | { |
| 36 | struct tc_police tp; |
| 37 | struct tcmsg *t = NLMSG_DATA(n); |
| 38 | struct rtattr *tail; |
Patrick McHardy | c90308f | 2009-11-23 12:03:41 +0100 | [diff] [blame] | 39 | __u32 mask = 0; |
| 40 | int mask_set = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | |
| 42 | memset(&tp, 0, sizeof(tp)); |
| 43 | |
| 44 | if (handle) { |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 45 | char *slash; |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 46 | if ((slash = strchr(handle, '/')) != NULL) |
| 47 | *slash = '\0'; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 48 | if (get_u32(&t->tcm_handle, handle, 0)) { |
| 49 | fprintf(stderr, "Illegal \"handle\"\n"); |
| 50 | return -1; |
| 51 | } |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 52 | if (slash) { |
| 53 | if (get_u32(&mask, slash+1, 0)) { |
| 54 | fprintf(stderr, "Illegal \"handle\" mask\n"); |
| 55 | return -1; |
| 56 | } |
Patrick McHardy | c90308f | 2009-11-23 12:03:41 +0100 | [diff] [blame] | 57 | mask_set = 1; |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 58 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (argc == 0) |
| 62 | return 0; |
| 63 | |
Patrick McHardy | c90308f | 2009-11-23 12:03:41 +0100 | [diff] [blame] | 64 | tail = NLMSG_TAIL(n); |
| 65 | addattr_l(n, 4096, TCA_OPTIONS, NULL, 0); |
| 66 | |
| 67 | if (mask_set) |
| 68 | addattr32(n, MAX_MSG, TCA_FW_MASK, mask); |
| 69 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 70 | while (argc > 0) { |
| 71 | if (matches(*argv, "classid") == 0 || |
| 72 | matches(*argv, "flowid") == 0) { |
| 73 | unsigned handle; |
| 74 | NEXT_ARG(); |
| 75 | if (get_tc_classid(&handle, *argv)) { |
| 76 | fprintf(stderr, "Illegal \"classid\"\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4); |
| 80 | } else if (matches(*argv, "police") == 0) { |
| 81 | NEXT_ARG(); |
| 82 | if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) { |
| 83 | fprintf(stderr, "Illegal \"police\"\n"); |
| 84 | return -1; |
| 85 | } |
| 86 | continue; |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 87 | } else if (matches(*argv, "action") == 0) { |
| 88 | NEXT_ARG(); |
| 89 | if (parse_action(&argc, &argv, TCA_FW_ACT, n)) { |
| 90 | fprintf(stderr, "Illegal fw \"action\"\n"); |
| 91 | return -1; |
| 92 | } |
| 93 | continue; |
| 94 | } else if (strcmp(*argv, "indev") == 0) { |
| 95 | char d[IFNAMSIZ+1]; |
| 96 | memset(d, 0, sizeof (d)); |
| 97 | argc--; |
| 98 | argv++; |
| 99 | if (argc < 1) { |
| 100 | fprintf(stderr, "Illegal indev\n"); |
| 101 | return -1; |
| 102 | } |
| 103 | strncpy(d, *argv, sizeof (d) - 1); |
| 104 | addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 105 | } else if (strcmp(*argv, "help") == 0) { |
| 106 | explain(); |
| 107 | return -1; |
| 108 | } else { |
| 109 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 110 | explain(); |
| 111 | return -1; |
| 112 | } |
| 113 | argc--; argv++; |
| 114 | } |
5!tgraf | 034102f | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 115 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle) |
| 120 | { |
| 121 | struct rtattr *tb[TCA_FW_MAX+1]; |
| 122 | |
| 123 | if (opt == NULL) |
| 124 | return 0; |
| 125 | |
5!tgraf | 021ed13 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 126 | parse_rtattr_nested(tb, TCA_FW_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 128 | if (handle || tb[TCA_FW_MASK]) { |
| 129 | __u32 mark = 0, mask = 0; |
| 130 | if(handle) |
| 131 | mark = handle; |
| 132 | if(tb[TCA_FW_MASK] && |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 133 | (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF) |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 134 | fprintf(f, "handle 0x%x/0x%x ", mark, mask); |
| 135 | else |
| 136 | fprintf(f, "handle 0x%x ", handle); |
| 137 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 138 | |
| 139 | if (tb[TCA_FW_CLASSID]) { |
| 140 | SPRINT_BUF(b1); |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 141 | fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (tb[TCA_FW_POLICE]) |
| 145 | tc_print_police(f, tb[TCA_FW_POLICE]); |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 146 | if (tb[TCA_FW_INDEV]) { |
| 147 | struct rtattr *idev = tb[TCA_FW_INDEV]; |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 148 | fprintf(f, "input dev %s ",rta_getattr_str(idev)); |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 149 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 150 | |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 151 | if (tb[TCA_FW_ACT]) { |
| 152 | fprintf(f, "\n"); |
| 153 | tc_print_action(f, tb[TCA_FW_ACT]); |
| 154 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
osdl.net!shemminger | 6b7dff1 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 158 | struct filter_util fw_filter_util = { |
| 159 | .id = "fw", |
| 160 | .parse_fopt = fw_parse_opt, |
| 161 | .print_fopt = fw_print_opt, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 162 | }; |