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 |
Phil Sutter | 5c32fa1 | 2015-09-18 20:17:02 +0200 | [diff] [blame] | 16 | * this program; if not, see <http://www.gnu.org/licenses>. |
Felix Fietkau | b8d5c9a | 2015-02-15 11:57:19 -0500 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string.h> |
| 23 | #include "utils.h" |
| 24 | #include "tc_util.h" |
| 25 | #include <linux/tc_act/tc_connmark.h> |
| 26 | |
| 27 | static void |
| 28 | explain(void) |
| 29 | { |
Phil Sutter | 1672f42 | 2016-03-22 15:48:33 +0100 | [diff] [blame] | 30 | fprintf(stderr, "Usage: ... connmark [zone ZONE] [CONTROL] [index <INDEX>]\n"); |
Felix Fietkau | b8d5c9a | 2015-02-15 11:57:19 -0500 | [diff] [blame] | 31 | fprintf(stderr, "where :\n" |
| 32 | "\tZONE is the conntrack zone\n" |
Phil Sutter | 1672f42 | 2016-03-22 15:48:33 +0100 | [diff] [blame] | 33 | "\tCONTROL := reclassify|pipe|drop|continue|ok\n"); |
Felix Fietkau | b8d5c9a | 2015-02-15 11:57:19 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static void |
| 37 | usage(void) |
| 38 | { |
| 39 | explain(); |
| 40 | exit(-1); |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | parse_connmark(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, |
| 45 | struct nlmsghdr *n) |
| 46 | { |
| 47 | struct tc_connmark sel = {}; |
| 48 | char **argv = *argv_p; |
| 49 | int argc = *argc_p; |
| 50 | int ok = 0; |
| 51 | struct rtattr *tail; |
| 52 | |
| 53 | while (argc > 0) { |
| 54 | if (matches(*argv, "connmark") == 0) { |
| 55 | ok = 1; |
| 56 | argc--; |
| 57 | argv++; |
| 58 | } else if (matches(*argv, "help") == 0) { |
| 59 | usage(); |
| 60 | } else { |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | if (!ok) { |
| 67 | explain(); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (argc) { |
| 72 | if (matches(*argv, "zone") == 0) { |
| 73 | NEXT_ARG(); |
| 74 | if (get_u16(&sel.zone, *argv, 10)) { |
| 75 | fprintf(stderr, "simple: Illegal \"index\"\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | argc--; |
| 79 | argv++; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | sel.action = TC_ACT_PIPE; |
Phil Sutter | 69f5aff | 2016-07-23 13:28:09 +0200 | [diff] [blame] | 84 | if (argc && !action_a2n(*argv, &sel.action, false)) |
| 85 | NEXT_ARG_FWD(); |
Felix Fietkau | b8d5c9a | 2015-02-15 11:57:19 -0500 | [diff] [blame] | 86 | |
| 87 | if (argc) { |
| 88 | if (matches(*argv, "index") == 0) { |
| 89 | NEXT_ARG(); |
| 90 | if (get_u32(&sel.index, *argv, 10)) { |
| 91 | fprintf(stderr, "simple: Illegal \"index\"\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | argc--; |
| 95 | argv++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | tail = NLMSG_TAIL(n); |
| 100 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 101 | addattr_l(n, MAX_MSG, TCA_CONNMARK_PARMS, &sel, sizeof(sel)); |
| 102 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 103 | |
| 104 | *argc_p = argc; |
| 105 | *argv_p = argv; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int print_connmark(struct action_util *au, FILE *f, struct rtattr *arg) |
| 110 | { |
| 111 | struct rtattr *tb[TCA_CONNMARK_MAX + 1]; |
| 112 | struct tc_connmark *ci; |
| 113 | |
| 114 | if (arg == NULL) |
| 115 | return -1; |
| 116 | |
| 117 | parse_rtattr_nested(tb, TCA_CONNMARK_MAX, arg); |
| 118 | if (tb[TCA_CONNMARK_PARMS] == NULL) { |
| 119 | fprintf(f, "[NULL connmark parameters]"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | ci = RTA_DATA(tb[TCA_CONNMARK_PARMS]); |
| 124 | |
| 125 | fprintf(f, " connmark zone %d\n", ci->zone); |
| 126 | fprintf(f, "\t index %d ref %d bind %d", ci->index, |
| 127 | ci->refcnt, ci->bindcnt); |
| 128 | |
| 129 | if (show_stats) { |
| 130 | if (tb[TCA_CONNMARK_TM]) { |
| 131 | struct tcf_t *tm = RTA_DATA(tb[TCA_CONNMARK_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 132 | |
Felix Fietkau | b8d5c9a | 2015-02-15 11:57:19 -0500 | [diff] [blame] | 133 | print_tm(f, tm); |
| 134 | } |
| 135 | } |
| 136 | fprintf(f, "\n"); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | struct action_util connmark_action_util = { |
| 142 | .id = "connmark", |
| 143 | .parse_aopt = parse_connmark, |
| 144 | .print_aopt = print_connmark, |
| 145 | }; |