Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * f_bpf.c BPF-based Classifier |
| 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: Daniel Borkmann <dborkman@redhat.com> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> |
| 20 | #include <string.h> |
| 21 | #include <stdbool.h> |
| 22 | #include <errno.h> |
Natanael Copa | dd9cc0e | 2014-05-27 07:40:10 +0000 | [diff] [blame] | 23 | #include <limits.h> |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 24 | #include <linux/filter.h> |
| 25 | #include <linux/if.h> |
| 26 | |
| 27 | #include "utils.h" |
| 28 | #include "tc_util.h" |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 29 | #include "tc_bpf.h" |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 30 | |
| 31 | static void explain(void) |
| 32 | { |
| 33 | fprintf(stderr, "Usage: ... bpf ...\n"); |
| 34 | fprintf(stderr, "\n"); |
| 35 | fprintf(stderr, " [inline]: run bytecode BPF_BYTECODE\n"); |
| 36 | fprintf(stderr, " [from file]: run bytecode-file FILE\n"); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 37 | fprintf(stderr, " [from file]: run object-file FILE\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 38 | fprintf(stderr, "\n"); |
Jamal Hadi Salim | 863ecb0 | 2014-10-06 07:41:21 -0400 | [diff] [blame] | 39 | fprintf(stderr, " [ action ACTION_SPEC ]\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 40 | fprintf(stderr, " [ classid CLASSID ]\n"); |
| 41 | fprintf(stderr, "\n"); |
| 42 | fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n"); |
| 43 | fprintf(stderr, " c,t,f,k and s are decimals; s denotes number of 4-tuples\n"); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 44 | fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n"); |
| 45 | fprintf(stderr, "or an ELF file containing eBPF map definitions and bytecode.\n"); |
Jamal Hadi Salim | 863ecb0 | 2014-10-06 07:41:21 -0400 | [diff] [blame] | 46 | fprintf(stderr, "\nACTION_SPEC := ... look at individual actions\n"); |
| 47 | fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 50 | static int bpf_parse_opt(struct filter_util *qu, char *handle, |
| 51 | int argc, char **argv, struct nlmsghdr *n) |
| 52 | { |
| 53 | struct tcmsg *t = NLMSG_DATA(n); |
| 54 | struct rtattr *tail; |
| 55 | long h = 0; |
| 56 | |
| 57 | if (argc == 0) |
| 58 | return 0; |
| 59 | |
| 60 | if (handle) { |
| 61 | h = strtol(handle, NULL, 0); |
| 62 | if (h == LONG_MIN || h == LONG_MAX) { |
| 63 | fprintf(stderr, "Illegal handle \"%s\", must be " |
| 64 | "numeric.\n", handle); |
| 65 | return -1; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | t->tcm_handle = h; |
| 70 | |
| 71 | tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len)); |
| 72 | addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
| 73 | |
| 74 | while (argc > 0) { |
| 75 | if (matches(*argv, "run") == 0) { |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 76 | bool from_file = true, ebpf; |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 77 | struct sock_filter bpf_ops[BPF_MAXINSNS]; |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 78 | int ret; |
| 79 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 80 | NEXT_ARG(); |
| 81 | if (strcmp(*argv, "bytecode-file") == 0) { |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 82 | ebpf = false; |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 83 | } else if (strcmp(*argv, "bytecode") == 0) { |
| 84 | from_file = false; |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 85 | ebpf = false; |
| 86 | } else if (strcmp(*argv, "object-file") == 0) { |
| 87 | ebpf = true; |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 88 | } else { |
| 89 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 90 | explain(); |
| 91 | return -1; |
| 92 | } |
| 93 | NEXT_ARG(); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 94 | ret = ebpf ? bpf_open_object(*argv, BPF_PROG_TYPE_SCHED_CLS) : |
| 95 | bpf_parse_ops(argc, argv, bpf_ops, from_file); |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 96 | if (ret < 0) { |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 97 | fprintf(stderr, "%s\n", ebpf ? |
| 98 | "Could not load object" : |
| 99 | "Illegal \"bytecode\""); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 100 | return -1; |
| 101 | } |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 102 | if (ebpf) { |
| 103 | addattr32(n, MAX_MSG, TCA_BPF_FD, ret); |
| 104 | addattrstrz(n, MAX_MSG, TCA_BPF_NAME, *argv); |
| 105 | } else { |
| 106 | addattr16(n, MAX_MSG, TCA_BPF_OPS_LEN, ret); |
| 107 | addattr_l(n, MAX_MSG, TCA_BPF_OPS, &bpf_ops, |
| 108 | ret * sizeof(struct sock_filter)); |
| 109 | } |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 110 | } else if (matches(*argv, "classid") == 0 || |
| 111 | strcmp(*argv, "flowid") == 0) { |
| 112 | unsigned handle; |
| 113 | NEXT_ARG(); |
| 114 | if (get_tc_classid(&handle, *argv)) { |
| 115 | fprintf(stderr, "Illegal \"classid\"\n"); |
| 116 | return -1; |
| 117 | } |
| 118 | addattr_l(n, MAX_MSG, TCA_BPF_CLASSID, &handle, 4); |
| 119 | } else if (matches(*argv, "action") == 0) { |
| 120 | NEXT_ARG(); |
| 121 | if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) { |
| 122 | fprintf(stderr, "Illegal \"action\"\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | continue; |
| 126 | } else if (matches(*argv, "police") == 0) { |
| 127 | NEXT_ARG(); |
| 128 | if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) { |
| 129 | fprintf(stderr, "Illegal \"police\"\n"); |
| 130 | return -1; |
| 131 | } |
| 132 | continue; |
| 133 | } else if (strcmp(*argv, "help") == 0) { |
| 134 | explain(); |
| 135 | return -1; |
| 136 | } else { |
| 137 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 138 | explain(); |
| 139 | return -1; |
| 140 | } |
| 141 | argc--; argv++; |
| 142 | } |
| 143 | |
| 144 | tail->rta_len = (((void*)n)+n->nlmsg_len) - (void*)tail; |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int bpf_print_opt(struct filter_util *qu, FILE *f, |
| 149 | struct rtattr *opt, __u32 handle) |
| 150 | { |
| 151 | struct rtattr *tb[TCA_BPF_MAX + 1]; |
| 152 | |
| 153 | if (opt == NULL) |
| 154 | return 0; |
| 155 | |
| 156 | parse_rtattr_nested(tb, TCA_BPF_MAX, opt); |
| 157 | |
| 158 | if (handle) |
| 159 | fprintf(f, "handle 0x%x ", handle); |
| 160 | |
| 161 | if (tb[TCA_BPF_CLASSID]) { |
| 162 | SPRINT_BUF(b1); |
| 163 | fprintf(f, "flowid %s ", |
| 164 | sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1)); |
| 165 | } |
| 166 | |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 167 | if (tb[TCA_BPF_NAME]) |
| 168 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME])); |
| 169 | else if (tb[TCA_BPF_FD]) |
| 170 | fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_BPF_FD])); |
| 171 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 172 | if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN]) |
| 173 | bpf_print_ops(f, tb[TCA_BPF_OPS], |
| 174 | rta_getattr_u16(tb[TCA_BPF_OPS_LEN])); |
| 175 | |
| 176 | if (tb[TCA_BPF_POLICE]) { |
| 177 | fprintf(f, "\n"); |
| 178 | tc_print_police(f, tb[TCA_BPF_POLICE]); |
| 179 | } |
| 180 | |
| 181 | if (tb[TCA_BPF_ACT]) { |
| 182 | tc_print_action(f, tb[TCA_BPF_ACT]); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | struct filter_util bpf_filter_util = { |
| 189 | .id = "bpf", |
| 190 | .parse_fopt = bpf_parse_opt, |
| 191 | .print_fopt = bpf_print_opt, |
| 192 | }; |