Felix Fietkau | b8d5c9a | 2015-02-15 11:57:19 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * m_connmark.c Connection tracking marking import |
| 3 | * |
| 4 | * Copyright (c) 2011 Felix Fietkau <nbd@openwrt.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 17 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
| 24 | #include "utils.h" |
| 25 | #include "tc_util.h" |
| 26 | #include <linux/tc_act/tc_connmark.h> |
| 27 | |
| 28 | static void |
| 29 | explain(void) |
| 30 | { |
| 31 | fprintf(stderr, "Usage: ... connmark [ZONE] [BRANCH] [index <INDEX>]\n"); |
| 32 | fprintf(stderr, "where :\n" |
| 33 | "\tZONE is the conntrack zone\n" |
| 34 | "\tBRANCH := reclassify|pipe|drop|continue|ok\n"); |
| 35 | } |
| 36 | |
| 37 | static void |
| 38 | usage(void) |
| 39 | { |
| 40 | explain(); |
| 41 | exit(-1); |
| 42 | } |
| 43 | |
| 44 | static int |
| 45 | parse_connmark(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, |
| 46 | struct nlmsghdr *n) |
| 47 | { |
| 48 | struct tc_connmark sel = {}; |
| 49 | char **argv = *argv_p; |
| 50 | int argc = *argc_p; |
| 51 | int ok = 0; |
| 52 | struct rtattr *tail; |
| 53 | |
| 54 | while (argc > 0) { |
| 55 | if (matches(*argv, "connmark") == 0) { |
| 56 | ok = 1; |
| 57 | argc--; |
| 58 | argv++; |
| 59 | } else if (matches(*argv, "help") == 0) { |
| 60 | usage(); |
| 61 | } else { |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | if (!ok) { |
| 68 | explain(); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | if (argc) { |
| 73 | if (matches(*argv, "zone") == 0) { |
| 74 | NEXT_ARG(); |
| 75 | if (get_u16(&sel.zone, *argv, 10)) { |
| 76 | fprintf(stderr, "simple: Illegal \"index\"\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | argc--; |
| 80 | argv++; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | sel.action = TC_ACT_PIPE; |
| 85 | if (argc) { |
| 86 | if (matches(*argv, "reclassify") == 0) { |
| 87 | sel.action = TC_ACT_RECLASSIFY; |
| 88 | argc--; |
| 89 | argv++; |
| 90 | } else if (matches(*argv, "pipe") == 0) { |
| 91 | sel.action = TC_ACT_PIPE; |
| 92 | argc--; |
| 93 | argv++; |
| 94 | } else if (matches(*argv, "drop") == 0 || |
| 95 | matches(*argv, "shot") == 0) { |
| 96 | sel.action = TC_ACT_SHOT; |
| 97 | argc--; |
| 98 | argv++; |
| 99 | } else if (matches(*argv, "continue") == 0) { |
| 100 | sel.action = TC_ACT_UNSPEC; |
| 101 | argc--; |
| 102 | argv++; |
| 103 | } else if (matches(*argv, "pass") == 0) { |
| 104 | sel.action = TC_ACT_OK; |
| 105 | argc--; |
| 106 | argv++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (argc) { |
| 111 | if (matches(*argv, "index") == 0) { |
| 112 | NEXT_ARG(); |
| 113 | if (get_u32(&sel.index, *argv, 10)) { |
| 114 | fprintf(stderr, "simple: Illegal \"index\"\n"); |
| 115 | return -1; |
| 116 | } |
| 117 | argc--; |
| 118 | argv++; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | tail = NLMSG_TAIL(n); |
| 123 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 124 | addattr_l(n, MAX_MSG, TCA_CONNMARK_PARMS, &sel, sizeof(sel)); |
| 125 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 126 | |
| 127 | *argc_p = argc; |
| 128 | *argv_p = argv; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int print_connmark(struct action_util *au, FILE *f, struct rtattr *arg) |
| 133 | { |
| 134 | struct rtattr *tb[TCA_CONNMARK_MAX + 1]; |
| 135 | struct tc_connmark *ci; |
| 136 | |
| 137 | if (arg == NULL) |
| 138 | return -1; |
| 139 | |
| 140 | parse_rtattr_nested(tb, TCA_CONNMARK_MAX, arg); |
| 141 | if (tb[TCA_CONNMARK_PARMS] == NULL) { |
| 142 | fprintf(f, "[NULL connmark parameters]"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | ci = RTA_DATA(tb[TCA_CONNMARK_PARMS]); |
| 147 | |
| 148 | fprintf(f, " connmark zone %d\n", ci->zone); |
| 149 | fprintf(f, "\t index %d ref %d bind %d", ci->index, |
| 150 | ci->refcnt, ci->bindcnt); |
| 151 | |
| 152 | if (show_stats) { |
| 153 | if (tb[TCA_CONNMARK_TM]) { |
| 154 | struct tcf_t *tm = RTA_DATA(tb[TCA_CONNMARK_TM]); |
| 155 | print_tm(f, tm); |
| 156 | } |
| 157 | } |
| 158 | fprintf(f, "\n"); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | struct action_util connmark_action_util = { |
| 164 | .id = "connmark", |
| 165 | .parse_aopt = parse_connmark, |
| 166 | .print_aopt = print_connmark, |
| 167 | }; |