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 (at) 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> 22 #include <linux/if.h> /* IFNAMSIZ */ 23 #include "utils.h" 24 #include "tc_util.h" 25 26 static void explain(void) 27 { 28 fprintf(stderr, "Usage: ... fw [ classid CLASSID ] [ police POLICE_SPEC ]\n"); 29 fprintf(stderr, " POLICE_SPEC := ... look at TBF\n"); 30 fprintf(stderr, " CLASSID := X:Y\n"); 31 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n"); 32 } 33 34 #define usage() return(-1) 35 36 static int fw_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n) 37 { 38 struct tc_police tp; 39 struct tcmsg *t = NLMSG_DATA(n); 40 struct rtattr *tail; 41 __u32 mask = 0; 42 int mask_set = 0; 43 44 memset(&tp, 0, sizeof(tp)); 45 46 if (handle) { 47 char *slash; 48 if ((slash = strchr(handle, '/')) != NULL) 49 *slash = '\0'; 50 if (get_u32(&t->tcm_handle, handle, 0)) { 51 fprintf(stderr, "Illegal \"handle\"\n"); 52 return -1; 53 } 54 if (slash) { 55 if (get_u32(&mask, slash+1, 0)) { 56 fprintf(stderr, "Illegal \"handle\" mask\n"); 57 return -1; 58 } 59 mask_set = 1; 60 } 61 } 62 63 if (argc == 0) 64 return 0; 65 66 tail = NLMSG_TAIL(n); 67 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0); 68 69 if (mask_set) 70 addattr32(n, MAX_MSG, TCA_FW_MASK, mask); 71 72 while (argc > 0) { 73 if (matches(*argv, "classid") == 0 || 74 matches(*argv, "flowid") == 0) { 75 unsigned handle; 76 NEXT_ARG(); 77 if (get_tc_classid(&handle, *argv)) { 78 fprintf(stderr, "Illegal \"classid\"\n"); 79 return -1; 80 } 81 addattr_l(n, 4096, TCA_FW_CLASSID, &handle, 4); 82 } else if (matches(*argv, "police") == 0) { 83 NEXT_ARG(); 84 if (parse_police(&argc, &argv, TCA_FW_POLICE, n)) { 85 fprintf(stderr, "Illegal \"police\"\n"); 86 return -1; 87 } 88 continue; 89 } else if (matches(*argv, "action") == 0) { 90 NEXT_ARG(); 91 if (parse_action(&argc, &argv, TCA_FW_ACT, n)) { 92 fprintf(stderr, "Illegal fw \"action\"\n"); 93 return -1; 94 } 95 continue; 96 } else if (strcmp(*argv, "indev") == 0) { 97 char d[IFNAMSIZ+1]; 98 memset(d, 0, sizeof (d)); 99 argc--; 100 argv++; 101 if (argc < 1) { 102 fprintf(stderr, "Illegal indev\n"); 103 return -1; 104 } 105 strncpy(d, *argv, sizeof (d) - 1); 106 addattr_l(n, MAX_MSG, TCA_FW_INDEV, d, strlen(d) + 1); 107 } else if (strcmp(*argv, "help") == 0) { 108 explain(); 109 return -1; 110 } else { 111 fprintf(stderr, "What is \"%s\"?\n", *argv); 112 explain(); 113 return -1; 114 } 115 argc--; argv++; 116 } 117 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 118 return 0; 119 } 120 121 static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle) 122 { 123 struct rtattr *tb[TCA_FW_MAX+1]; 124 125 if (opt == NULL) 126 return 0; 127 128 parse_rtattr_nested(tb, TCA_FW_MAX, opt); 129 130 if (handle || tb[TCA_FW_MASK]) { 131 __u32 mark = 0, mask = 0; 132 if(handle) 133 mark = handle; 134 if(tb[TCA_FW_MASK] && 135 (mask = *(__u32*)RTA_DATA(tb[TCA_FW_MASK])) != 0xFFFFFFFF) 136 fprintf(f, "handle 0x%x/0x%x ", mark, mask); 137 else 138 fprintf(f, "handle 0x%x ", handle); 139 } 140 141 if (tb[TCA_FW_CLASSID]) { 142 SPRINT_BUF(b1); 143 fprintf(f, "classid %s ", sprint_tc_classid(*(__u32*)RTA_DATA(tb[TCA_FW_CLASSID]), b1)); 144 } 145 146 if (tb[TCA_FW_POLICE]) 147 tc_print_police(f, tb[TCA_FW_POLICE]); 148 if (tb[TCA_FW_INDEV]) { 149 struct rtattr *idev = tb[TCA_FW_INDEV]; 150 fprintf(f, "input dev %s ",(char *)RTA_DATA(idev)); 151 } 152 153 if (tb[TCA_FW_ACT]) { 154 fprintf(f, "\n"); 155 tc_print_action(f, tb[TCA_FW_ACT]); 156 } 157 return 0; 158 } 159 160 struct filter_util fw_filter_util = { 161 .id = "fw", 162 .parse_fopt = fw_parse_opt, 163 .print_fopt = fw_print_opt, 164 }; 165