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 | { |
| 88 | struct tc_csum sel; |
| 89 | |
| 90 | int argc = *argc_p; |
| 91 | char **argv = *argv_p; |
| 92 | int ok = 0; |
| 93 | struct rtattr *tail; |
| 94 | |
| 95 | memset(&sel, 0, sizeof(sel)); |
| 96 | |
| 97 | while (argc > 0) { |
| 98 | if (matches(*argv, "csum") == 0) { |
| 99 | NEXT_ARG(); |
| 100 | if (parse_csum_args(&argc, &argv, &sel)) { |
| 101 | fprintf(stderr, "Illegal csum construct (%s)\n", |
| 102 | *argv); |
| 103 | explain(); |
| 104 | return -1; |
| 105 | } |
| 106 | ok++; |
| 107 | continue; |
| 108 | } else if (matches(*argv, "help") == 0) { |
| 109 | usage(); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 110 | } else { |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!ok) { |
| 116 | explain(); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | if (sel.update_flags == 0) { |
| 121 | fprintf(stderr, "Illegal csum construct, empty <UPDATE> list\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (argc) { |
| 126 | if (matches(*argv, "reclassify") == 0) { |
| 127 | sel.action = TC_ACT_RECLASSIFY; |
| 128 | argc--; |
| 129 | argv++; |
| 130 | } else if (matches(*argv, "pipe") == 0) { |
| 131 | sel.action = TC_ACT_PIPE; |
| 132 | argc--; |
| 133 | argv++; |
| 134 | } else if (matches(*argv, "drop") == 0 || |
| 135 | matches(*argv, "shot") == 0) { |
| 136 | sel.action = TC_ACT_SHOT; |
| 137 | argc--; |
| 138 | argv++; |
| 139 | } else if (matches(*argv, "continue") == 0) { |
| 140 | sel.action = TC_ACT_UNSPEC; |
| 141 | argc--; |
| 142 | argv++; |
Jamal Hadi Salim | 43726b7 | 2016-05-07 09:39:36 -0400 | [diff] [blame] | 143 | } else if (matches(*argv, "pass") == 0 || |
| 144 | matches(*argv, "ok") == 0) { |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 145 | sel.action = TC_ACT_OK; |
| 146 | argc--; |
| 147 | argv++; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (argc) { |
| 152 | if (matches(*argv, "index") == 0) { |
| 153 | NEXT_ARG(); |
| 154 | if (get_u32(&sel.index, *argv, 10)) { |
| 155 | fprintf(stderr, "Illegal \"index\" (%s) <csum>\n", |
| 156 | *argv); |
| 157 | return -1; |
| 158 | } |
| 159 | argc--; |
| 160 | argv++; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | tail = NLMSG_TAIL(n); |
| 165 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 166 | addattr_l(n, MAX_MSG, TCA_CSUM_PARMS, &sel, sizeof(sel)); |
| 167 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 168 | |
| 169 | *argc_p = argc; |
| 170 | *argv_p = argv; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 176 | print_csum(struct action_util *au, FILE *f, struct rtattr *arg) |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 177 | { |
| 178 | struct tc_csum *sel; |
| 179 | |
| 180 | struct rtattr *tb[TCA_CSUM_MAX + 1]; |
| 181 | |
| 182 | char *uflag_1 = ""; |
| 183 | char *uflag_2 = ""; |
| 184 | char *uflag_3 = ""; |
| 185 | char *uflag_4 = ""; |
| 186 | char *uflag_5 = ""; |
| 187 | char *uflag_6 = ""; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 188 | |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 189 | SPRINT_BUF(action_buf); |
| 190 | |
| 191 | int uflag_count = 0; |
| 192 | |
| 193 | if (arg == NULL) |
| 194 | return -1; |
| 195 | |
| 196 | parse_rtattr_nested(tb, TCA_CSUM_MAX, arg); |
| 197 | |
| 198 | if (tb[TCA_CSUM_PARMS] == NULL) { |
| 199 | fprintf(f, "[NULL csum parameters]"); |
| 200 | return -1; |
| 201 | } |
| 202 | sel = RTA_DATA(tb[TCA_CSUM_PARMS]); |
| 203 | |
| 204 | if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) { |
| 205 | uflag_1 = "iph"; |
| 206 | uflag_count++; |
| 207 | } |
| 208 | #define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \ |
| 209 | do { \ |
| 210 | if (sel->update_flags & flag_value) { \ |
| 211 | flag_buffer = uflag_count > 0 ? \ |
| 212 | ", " flag_string : flag_string; \ |
| 213 | uflag_count++; \ |
| 214 | } \ |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 215 | } while (0) |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 216 | CSUM_UFLAG_BUFFER(uflag_2, TCA_CSUM_UPDATE_FLAG_ICMP, "icmp"); |
| 217 | CSUM_UFLAG_BUFFER(uflag_3, TCA_CSUM_UPDATE_FLAG_IGMP, "igmp"); |
Anton Danilov | 90d98ed | 2012-03-15 14:24:59 -0700 | [diff] [blame] | 218 | CSUM_UFLAG_BUFFER(uflag_4, TCA_CSUM_UPDATE_FLAG_TCP, "tcp"); |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 219 | CSUM_UFLAG_BUFFER(uflag_5, TCA_CSUM_UPDATE_FLAG_UDP, "udp"); |
| 220 | CSUM_UFLAG_BUFFER(uflag_6, TCA_CSUM_UPDATE_FLAG_UDPLITE, "udplite"); |
| 221 | if (!uflag_count) { |
| 222 | uflag_1 = "?empty"; |
| 223 | } |
| 224 | |
| 225 | fprintf(f, "csum (%s%s%s%s%s%s) action %s\n", |
| 226 | uflag_1, uflag_2, uflag_3, |
| 227 | uflag_4, uflag_5, uflag_6, |
| 228 | action_n2a(sel->action, action_buf, sizeof(action_buf))); |
| 229 | fprintf(f, "\tindex %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt); |
| 230 | |
| 231 | if (show_stats) { |
| 232 | if (tb[TCA_CSUM_TM]) { |
| 233 | struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 234 | |
| 235 | print_tm(f, tm); |
Gregoire Baron | 3822cc9 | 2010-12-01 11:17:46 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | fprintf(f, "\n"); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | struct action_util csum_action_util = { |
| 244 | .id = "csum", |
| 245 | .parse_aopt = parse_csum, |
| 246 | .print_aopt = print_csum, |
| 247 | }; |