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 | { |
Roman Mashak | 4b5451c | 2016-11-10 11:02:57 -0500 | [diff] [blame] | 28 | fprintf(stderr, |
| 29 | "Usage: ... fw [ classid CLASSID ] [ indev DEV ] [ action ACTION_SPEC ]\n"); |
| 30 | fprintf(stderr, |
| 31 | " CLASSID := Push matching packets to the class identified by CLASSID with format X:Y\n"); |
| 32 | fprintf(stderr, |
| 33 | " CLASSID is parsed as hexadecimal input.\n"); |
| 34 | fprintf(stderr, |
| 35 | " DEV := specify device for incoming device classification.\n"); |
| 36 | fprintf(stderr, |
| 37 | " ACTION_SPEC := Apply an action on matching packets.\n"); |
| 38 | fprintf(stderr, |
| 39 | " NOTE: handle is represented as HANDLE[/FWMASK].\n"); |
| 40 | fprintf(stderr, " FWMASK is 0xffffffff by default.\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | } |
| 42 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 43 | static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n) |
| 44 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | struct tcmsg *t = NLMSG_DATA(n); |
| 46 | struct rtattr *tail; |
Patrick McHardy | c90308f | 2009-11-23 12:03:41 +0100 | [diff] [blame] | 47 | __u32 mask = 0; |
| 48 | int mask_set = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 49 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 50 | if (handle) { |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 51 | char *slash; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 52 | |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 53 | if ((slash = strchr(handle, '/')) != NULL) |
| 54 | *slash = '\0'; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | if (get_u32(&t->tcm_handle, handle, 0)) { |
| 56 | fprintf(stderr, "Illegal \"handle\"\n"); |
| 57 | return -1; |
| 58 | } |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 59 | if (slash) { |
| 60 | if (get_u32(&mask, slash+1, 0)) { |
| 61 | fprintf(stderr, "Illegal \"handle\" mask\n"); |
| 62 | return -1; |
| 63 | } |
Patrick McHardy | c90308f | 2009-11-23 12:03:41 +0100 | [diff] [blame] | 64 | mask_set = 1; |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 65 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | if (argc == 0) |
| 69 | return 0; |
| 70 | |
Patrick McHardy | c90308f | 2009-11-23 12:03:41 +0100 | [diff] [blame] | 71 | tail = NLMSG_TAIL(n); |
| 72 | addattr_l(n, 4096, TCA_OPTIONS, NULL, 0); |
| 73 | |
| 74 | if (mask_set) |
| 75 | addattr32(n, MAX_MSG, TCA_FW_MASK, mask); |
| 76 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | while (argc > 0) { |
| 78 | if (matches(*argv, "classid") == 0 || |
| 79 | matches(*argv, "flowid") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 80 | unsigned int handle; |
| 81 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 82 | NEXT_ARG(); |
| 83 | if (get_tc_classid(&handle, *argv)) { |
| 84 | fprintf(stderr, "Illegal \"classid\"\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4); |
| 88 | } else if (matches(*argv, "police") == 0) { |
| 89 | NEXT_ARG(); |
| 90 | if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) { |
| 91 | fprintf(stderr, "Illegal \"police\"\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | continue; |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 95 | } else if (matches(*argv, "action") == 0) { |
| 96 | NEXT_ARG(); |
| 97 | if (parse_action(&argc, &argv, TCA_FW_ACT, n)) { |
| 98 | fprintf(stderr, "Illegal fw \"action\"\n"); |
| 99 | return -1; |
| 100 | } |
| 101 | continue; |
| 102 | } else if (strcmp(*argv, "indev") == 0) { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 103 | char d[IFNAMSIZ+1] = {}; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 104 | |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 105 | argc--; |
| 106 | argv++; |
| 107 | if (argc < 1) { |
| 108 | fprintf(stderr, "Illegal indev\n"); |
| 109 | return -1; |
| 110 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 111 | strncpy(d, *argv, sizeof(d) - 1); |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 112 | 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] | 113 | } else if (strcmp(*argv, "help") == 0) { |
| 114 | explain(); |
| 115 | return -1; |
| 116 | } else { |
| 117 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 118 | explain(); |
| 119 | return -1; |
| 120 | } |
| 121 | argc--; argv++; |
| 122 | } |
5!tgraf | 034102f | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 123 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle) |
| 128 | { |
| 129 | struct rtattr *tb[TCA_FW_MAX+1]; |
| 130 | |
| 131 | if (opt == NULL) |
| 132 | return 0; |
| 133 | |
5!tgraf | 021ed13 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 134 | parse_rtattr_nested(tb, TCA_FW_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 135 | |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 136 | if (handle || tb[TCA_FW_MASK]) { |
| 137 | __u32 mark = 0, mask = 0; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 138 | |
| 139 | if (handle) |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 140 | mark = handle; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 141 | if (tb[TCA_FW_MASK] && |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 142 | (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF) |
François Delawarde | e22b42a | 2007-11-26 18:13:24 +0100 | [diff] [blame] | 143 | fprintf(f, "handle 0x%x/0x%x ", mark, mask); |
| 144 | else |
| 145 | fprintf(f, "handle 0x%x ", handle); |
| 146 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | |
| 148 | if (tb[TCA_FW_CLASSID]) { |
| 149 | SPRINT_BUF(b1); |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 150 | 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] | 151 | } |
| 152 | |
| 153 | if (tb[TCA_FW_POLICE]) |
| 154 | tc_print_police(f, tb[TCA_FW_POLICE]); |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 155 | if (tb[TCA_FW_INDEV]) { |
| 156 | struct rtattr *idev = tb[TCA_FW_INDEV]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 157 | |
| 158 | fprintf(f, "input dev %s ", rta_getattr_str(idev)); |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 159 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 160 | |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 161 | if (tb[TCA_FW_ACT]) { |
| 162 | fprintf(f, "\n"); |
Jamal Hadi Salim | 9e71352 | 2017-08-02 07:46:26 -0400 | [diff] [blame] | 163 | tc_print_action(f, tb[TCA_FW_ACT], 0); |
osdl.net!shemminger | fa3a993 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 164 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
osdl.net!shemminger | 6b7dff1 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 168 | struct filter_util fw_filter_util = { |
| 169 | .id = "fw", |
| 170 | .parse_fopt = fw_parse_opt, |
| 171 | .print_fopt = fw_print_opt, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 172 | }; |