Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * m_csum.c checksum updating action |
| 3 | * |
| 4 | * This program is free software; you can distribute 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: Gregoire Baron <baronchon@n7mm.org> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include <linux/tc_act/tc_csum.h> |
| 18 | |
| 19 | #include "utils.h" |
| 20 | #include "tc_util.h" |
| 21 | |
| 22 | static void |
| 23 | explain(void) |
| 24 | { |
| 25 | fprintf(stderr, "Usage: ... csum <UPDATE>\n" |
| 26 | "Where: UPDATE := <TARGET> [<UPDATE>]\n" |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 27 | " TARGET := { ip4h | icmp | igmp | tcp | udp | udplite | <SWEETS> }\n" |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 28 | " SWEETS := { and | or | \'+\' }\n"); |
| 29 | } |
| 30 | |
| 31 | static void |
| 32 | usage(void) |
| 33 | { |
| 34 | explain(); |
| 35 | exit(-1); |
| 36 | } |
| 37 | |
| 38 | static int |
| 39 | parse_csum_args(int *argc_p, char ***argv_p, struct tc_csum *sel) |
| 40 | { |
| 41 | int argc = *argc_p; |
| 42 | char **argv = *argv_p; |
| 43 | |
| 44 | if (argc <= 0) |
| 45 | return -1; |
| 46 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 47 | while (argc > 0) { |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 48 | if ((matches(*argv, "iph") == 0) || |
| 49 | (matches(*argv, "ip4h") == 0) || |
| 50 | (matches(*argv, "ipv4h") == 0)) |
| 51 | sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IPV4HDR; |
| 52 | |
| 53 | else if (matches(*argv, "icmp") == 0) |
| 54 | sel->update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP; |
| 55 | |
| 56 | else if (matches(*argv, "igmp") == 0) |
| 57 | sel->update_flags |= TCA_CSUM_UPDATE_FLAG_IGMP; |
| 58 | |
| 59 | else if (matches(*argv, "tcp") == 0) |
| 60 | sel->update_flags |= TCA_CSUM_UPDATE_FLAG_TCP; |
| 61 | |
| 62 | else if (matches(*argv, "udp") == 0) |
| 63 | sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDP; |
| 64 | |
| 65 | else if (matches(*argv, "udplite") == 0) |
| 66 | sel->update_flags |= TCA_CSUM_UPDATE_FLAG_UDPLITE; |
| 67 | |
| 68 | else if ((matches(*argv, "and") == 0) || |
| 69 | (matches(*argv, "or") == 0) || |
| 70 | (matches(*argv, "+") == 0)) |
| 71 | ; /* just ignore: ... csum iph and tcp or udp */ |
| 72 | else |
| 73 | break; |
| 74 | argc--; |
| 75 | argv++; |
| 76 | } |
| 77 | |
| 78 | *argc_p = argc; |
| 79 | *argv_p = argv; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int |
| 85 | parse_csum(struct action_util *a, int *argc_p, |
| 86 | char ***argv_p, int tca_id, struct nlmsghdr *n) |
| 87 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 88 | struct tc_csum sel = {}; |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 89 | |
| 90 | int argc = *argc_p; |
| 91 | char **argv = *argv_p; |
| 92 | int ok = 0; |
| 93 | struct rtattr *tail; |
| 94 | |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 95 | while (argc > 0) { |
| 96 | if (matches(*argv, "csum") == 0) { |
| 97 | NEXT_ARG(); |
| 98 | if (parse_csum_args(&argc, &argv, &sel)) { |
| 99 | fprintf(stderr, "Illegal csum construct (%s)\n", |
| 100 | *argv); |
| 101 | explain(); |
| 102 | return -1; |
| 103 | } |
| 104 | ok++; |
| 105 | continue; |
| 106 | } else if (matches(*argv, "help") == 0) { |
| 107 | usage(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 108 | } else { |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (!ok) { |
| 114 | explain(); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | if (sel.update_flags == 0) { |
| 119 | fprintf(stderr, "Illegal csum construct, empty <UPDATE> list\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
Phil Sutter | 69f5aff | 2016-07-23 13:28:09 +0200 | [diff] [blame] | 123 | if (argc && !action_a2n(*argv, &sel.action, false)) |
| 124 | NEXT_ARG_FWD(); |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 125 | |
| 126 | if (argc) { |
| 127 | if (matches(*argv, "index") == 0) { |
| 128 | NEXT_ARG(); |
| 129 | if (get_u32(&sel.index, *argv, 10)) { |
| 130 | fprintf(stderr, "Illegal \"index\" (%s) <csum>\n", |
| 131 | *argv); |
| 132 | return -1; |
| 133 | } |
| 134 | argc--; |
| 135 | argv++; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | tail = NLMSG_TAIL(n); |
| 140 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 141 | addattr_l(n, MAX_MSG, TCA_CSUM_PARMS, &sel, sizeof(sel)); |
| 142 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 143 | |
| 144 | *argc_p = argc; |
| 145 | *argv_p = argv; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 151 | print_csum(struct action_util *au, FILE *f, struct rtattr *arg) |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 152 | { |
| 153 | struct tc_csum *sel; |
| 154 | |
| 155 | struct rtattr *tb[TCA_CSUM_MAX + 1]; |
| 156 | |
| 157 | char *uflag_1 = ""; |
| 158 | char *uflag_2 = ""; |
| 159 | char *uflag_3 = ""; |
| 160 | char *uflag_4 = ""; |
| 161 | char *uflag_5 = ""; |
| 162 | char *uflag_6 = ""; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 163 | |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 164 | int uflag_count = 0; |
| 165 | |
| 166 | if (arg == NULL) |
| 167 | return -1; |
| 168 | |
| 169 | parse_rtattr_nested(tb, TCA_CSUM_MAX, arg); |
| 170 | |
| 171 | if (tb[TCA_CSUM_PARMS] == NULL) { |
| 172 | fprintf(f, "[NULL csum parameters]"); |
| 173 | return -1; |
| 174 | } |
| 175 | sel = RTA_DATA(tb[TCA_CSUM_PARMS]); |
| 176 | |
| 177 | if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) { |
| 178 | uflag_1 = "iph"; |
| 179 | uflag_count++; |
| 180 | } |
| 181 | #define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \ |
| 182 | do { \ |
| 183 | if (sel->update_flags & flag_value) { \ |
| 184 | flag_buffer = uflag_count > 0 ? \ |
| 185 | ", " flag_string : flag_string; \ |
| 186 | uflag_count++; \ |
| 187 | } \ |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 188 | } while (0) |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 189 | CSUM_UFLAG_BUFFER(uflag_2, TCA_CSUM_UPDATE_FLAG_ICMP, "icmp"); |
| 190 | CSUM_UFLAG_BUFFER(uflag_3, TCA_CSUM_UPDATE_FLAG_IGMP, "igmp"); |
Anton Danilov | 90d98ed | 2012-03-15 14:24:59 -0700 | [diff] [blame] | 191 | CSUM_UFLAG_BUFFER(uflag_4, TCA_CSUM_UPDATE_FLAG_TCP, "tcp"); |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 192 | CSUM_UFLAG_BUFFER(uflag_5, TCA_CSUM_UPDATE_FLAG_UDP, "udp"); |
| 193 | CSUM_UFLAG_BUFFER(uflag_6, TCA_CSUM_UPDATE_FLAG_UDPLITE, "udplite"); |
| 194 | if (!uflag_count) { |
| 195 | uflag_1 = "?empty"; |
| 196 | } |
| 197 | |
| 198 | fprintf(f, "csum (%s%s%s%s%s%s) action %s\n", |
| 199 | uflag_1, uflag_2, uflag_3, |
| 200 | uflag_4, uflag_5, uflag_6, |
Phil Sutter | 7093200 | 2016-07-23 13:28:10 +0200 | [diff] [blame] | 201 | action_n2a(sel->action)); |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 202 | fprintf(f, "\tindex %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt); |
| 203 | |
| 204 | if (show_stats) { |
| 205 | if (tb[TCA_CSUM_TM]) { |
| 206 | struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 207 | |
| 208 | print_tm(f, tm); |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | fprintf(f, "\n"); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | struct action_util csum_action_util = { |
| 217 | .id = "csum", |
| 218 | .parse_aopt = parse_csum, |
| 219 | .print_aopt = print_csum, |
| 220 | }; |