Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux Socket Filter - Kernel level socket filtering |
| 3 | * |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 4 | * Based on the design of the Berkeley Packet Filter. The new |
| 5 | * internal format has been designed by PLUMgrid: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 7 | * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com |
| 8 | * |
| 9 | * Authors: |
| 10 | * |
| 11 | * Jay Schulist <jschlst@samba.org> |
| 12 | * Alexei Starovoitov <ast@plumgrid.com> |
| 13 | * Daniel Borkmann <dborkman@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * Andi Kleen - Fix a few bad bugs and races. |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 21 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/fcntl.h> |
| 28 | #include <linux/socket.h> |
Chenbo Feng | 0931366 | 2017-03-22 17:27:34 -0700 | [diff] [blame] | 29 | #include <linux/sock_diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/in.h> |
| 31 | #include <linux/inet.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/if_packet.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <net/ip.h> |
| 36 | #include <net/protocol.h> |
Patrick McHardy | 4738c1d | 2008-04-10 02:02:28 -0700 | [diff] [blame] | 37 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/skbuff.h> |
| 39 | #include <net/sock.h> |
Jiri Pirko | 10b89ee4 | 2015-05-12 14:56:09 +0200 | [diff] [blame] | 40 | #include <net/flow_dissector.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/errno.h> |
| 42 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/uaccess.h> |
Dmitry Mishin | 40daafc | 2006-04-18 14:50:10 -0700 | [diff] [blame] | 44 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/filter.h> |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 46 | #include <linux/ratelimit.h> |
Will Drewry | 46b325c | 2012-04-12 16:47:52 -0500 | [diff] [blame] | 47 | #include <linux/seccomp.h> |
Eric Dumazet | f333503 | 2012-10-27 02:26:17 +0000 | [diff] [blame] | 48 | #include <linux/if_vlan.h> |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 49 | #include <linux/bpf.h> |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 50 | #include <net/sch_generic.h> |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 51 | #include <net/cls_cgroup.h> |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 52 | #include <net/dst_metadata.h> |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 53 | #include <net/dst.h> |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 54 | #include <net/sock_reuseport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /** |
Willem de Bruijn | f4979fc | 2016-07-12 18:18:56 -0400 | [diff] [blame] | 57 | * sk_filter_trim_cap - run a packet through a socket filter |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 58 | * @sk: sock associated with &sk_buff |
| 59 | * @skb: buffer to filter |
Willem de Bruijn | f4979fc | 2016-07-12 18:18:56 -0400 | [diff] [blame] | 60 | * @cap: limit on how short the eBPF program may trim the packet |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 61 | * |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 62 | * Run the eBPF program and then cut skb->data to correct size returned by |
| 63 | * the program. If pkt_len is 0 we toss packet. If skb->len is smaller |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 64 | * than pkt_len we keep whole skb->data. This is the socket level |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 65 | * wrapper to BPF_PROG_RUN. It returns 0 if the packet should |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 66 | * be accepted or -EPERM if the packet should be tossed. |
| 67 | * |
| 68 | */ |
Willem de Bruijn | f4979fc | 2016-07-12 18:18:56 -0400 | [diff] [blame] | 69 | int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap) |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 70 | { |
| 71 | int err; |
| 72 | struct sk_filter *filter; |
| 73 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 74 | /* |
| 75 | * If the skb was allocated from pfmemalloc reserves, only |
| 76 | * allow SOCK_MEMALLOC sockets to use it as this socket is |
| 77 | * helping free memory |
| 78 | */ |
| 79 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) |
| 80 | return -ENOMEM; |
| 81 | |
Daniel Mack | 719a297 | 2016-11-23 16:52:28 +0100 | [diff] [blame] | 82 | err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb); |
| 83 | if (err) |
| 84 | return err; |
| 85 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 86 | err = security_sock_rcv_skb(sk, skb); |
| 87 | if (err) |
| 88 | return err; |
| 89 | |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 90 | rcu_read_lock(); |
| 91 | filter = rcu_dereference(sk->sk_filter); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 92 | if (filter) { |
Willem de Bruijn | 2c1289c | 2017-04-11 14:08:08 -0400 | [diff] [blame] | 93 | struct sock *save_sk = skb->sk; |
| 94 | unsigned int pkt_len; |
| 95 | |
| 96 | skb->sk = sk; |
| 97 | pkt_len = bpf_prog_run_save_cb(filter->prog, skb); |
Willem de Bruijn | 2c1289c | 2017-04-11 14:08:08 -0400 | [diff] [blame] | 98 | skb->sk = save_sk; |
Eric Dumazet | 0e9efe2 | 2017-04-26 09:09:23 -0700 | [diff] [blame] | 99 | err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM; |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 100 | } |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 101 | rcu_read_unlock(); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 102 | |
| 103 | return err; |
| 104 | } |
Willem de Bruijn | f4979fc | 2016-07-12 18:18:56 -0400 | [diff] [blame] | 105 | EXPORT_SYMBOL(sk_filter_trim_cap); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 106 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 107 | BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 108 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 109 | return skb_get_poff(skb); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 112 | BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 113 | { |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 114 | struct nlattr *nla; |
| 115 | |
| 116 | if (skb_is_nonlinear(skb)) |
| 117 | return 0; |
| 118 | |
Mathias Krause | 05ab8f2 | 2014-04-13 18:23:33 +0200 | [diff] [blame] | 119 | if (skb->len < sizeof(struct nlattr)) |
| 120 | return 0; |
| 121 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 122 | if (a > skb->len - sizeof(struct nlattr)) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 123 | return 0; |
| 124 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 125 | nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 126 | if (nla) |
| 127 | return (void *) nla - (void *) skb->data; |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 132 | BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 133 | { |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 134 | struct nlattr *nla; |
| 135 | |
| 136 | if (skb_is_nonlinear(skb)) |
| 137 | return 0; |
| 138 | |
Mathias Krause | 05ab8f2 | 2014-04-13 18:23:33 +0200 | [diff] [blame] | 139 | if (skb->len < sizeof(struct nlattr)) |
| 140 | return 0; |
| 141 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 142 | if (a > skb->len - sizeof(struct nlattr)) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 143 | return 0; |
| 144 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 145 | nla = (struct nlattr *) &skb->data[a]; |
| 146 | if (nla->nla_len > skb->len - a) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 147 | return 0; |
| 148 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 149 | nla = nla_find_nested(nla, x); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 150 | if (nla) |
| 151 | return (void *) nla - (void *) skb->data; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 156 | BPF_CALL_0(__get_raw_cpu_id) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 157 | { |
| 158 | return raw_smp_processor_id(); |
| 159 | } |
| 160 | |
Daniel Borkmann | 80b48c4 | 2016-06-28 12:18:26 +0200 | [diff] [blame] | 161 | static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = { |
| 162 | .func = __get_raw_cpu_id, |
| 163 | .gpl_only = false, |
| 164 | .ret_type = RET_INTEGER, |
| 165 | }; |
| 166 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 167 | static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, |
| 168 | struct bpf_insn *insn_buf) |
| 169 | { |
| 170 | struct bpf_insn *insn = insn_buf; |
| 171 | |
| 172 | switch (skb_field) { |
| 173 | case SKF_AD_MARK: |
| 174 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 175 | |
| 176 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 177 | offsetof(struct sk_buff, mark)); |
| 178 | break; |
| 179 | |
| 180 | case SKF_AD_PKTTYPE: |
| 181 | *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); |
| 182 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); |
| 183 | #ifdef __BIG_ENDIAN_BITFIELD |
| 184 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); |
| 185 | #endif |
| 186 | break; |
| 187 | |
| 188 | case SKF_AD_QUEUE: |
| 189 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); |
| 190 | |
| 191 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 192 | offsetof(struct sk_buff, queue_mapping)); |
| 193 | break; |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 194 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 195 | case SKF_AD_VLAN_TAG: |
| 196 | case SKF_AD_VLAN_TAG_PRESENT: |
| 197 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); |
| 198 | BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); |
| 199 | |
| 200 | /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ |
| 201 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 202 | offsetof(struct sk_buff, vlan_tci)); |
| 203 | if (skb_field == SKF_AD_VLAN_TAG) { |
| 204 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, |
| 205 | ~VLAN_TAG_PRESENT); |
| 206 | } else { |
| 207 | /* dst_reg >>= 12 */ |
| 208 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12); |
| 209 | /* dst_reg &= 1 */ |
| 210 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); |
| 211 | } |
| 212 | break; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return insn - insn_buf; |
| 216 | } |
| 217 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 218 | static bool convert_bpf_extensions(struct sock_filter *fp, |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 219 | struct bpf_insn **insnp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 220 | { |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 221 | struct bpf_insn *insn = *insnp; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 222 | u32 cnt; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 223 | |
| 224 | switch (fp->k) { |
| 225 | case SKF_AD_OFF + SKF_AD_PROTOCOL: |
Daniel Borkmann | 0b8c707 | 2015-03-19 19:38:27 +0100 | [diff] [blame] | 226 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); |
| 227 | |
| 228 | /* A = *(u16 *) (CTX + offsetof(protocol)) */ |
| 229 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 230 | offsetof(struct sk_buff, protocol)); |
| 231 | /* A = ntohs(A) [emitting a nop or swap16] */ |
| 232 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 233 | break; |
| 234 | |
| 235 | case SKF_AD_OFF + SKF_AD_PKTTYPE: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 236 | cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); |
| 237 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 238 | break; |
| 239 | |
| 240 | case SKF_AD_OFF + SKF_AD_IFINDEX: |
| 241 | case SKF_AD_OFF + SKF_AD_HATYPE: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 242 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 243 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2); |
| 244 | |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 245 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 246 | BPF_REG_TMP, BPF_REG_CTX, |
| 247 | offsetof(struct sk_buff, dev)); |
| 248 | /* if (tmp != 0) goto pc + 1 */ |
| 249 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); |
| 250 | *insn++ = BPF_EXIT_INSN(); |
| 251 | if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) |
| 252 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, |
| 253 | offsetof(struct net_device, ifindex)); |
| 254 | else |
| 255 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, |
| 256 | offsetof(struct net_device, type)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case SKF_AD_OFF + SKF_AD_MARK: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 260 | cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); |
| 261 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | case SKF_AD_OFF + SKF_AD_RXHASH: |
| 265 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 266 | |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 267 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, |
| 268 | offsetof(struct sk_buff, hash)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case SKF_AD_OFF + SKF_AD_QUEUE: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 272 | cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); |
| 273 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 274 | break; |
| 275 | |
| 276 | case SKF_AD_OFF + SKF_AD_VLAN_TAG: |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 277 | cnt = convert_skb_access(SKF_AD_VLAN_TAG, |
| 278 | BPF_REG_A, BPF_REG_CTX, insn); |
| 279 | insn += cnt - 1; |
| 280 | break; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 281 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 282 | case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: |
| 283 | cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, |
| 284 | BPF_REG_A, BPF_REG_CTX, insn); |
| 285 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 286 | break; |
| 287 | |
Michal Sekletar | 27cd545 | 2015-03-24 14:48:41 +0100 | [diff] [blame] | 288 | case SKF_AD_OFF + SKF_AD_VLAN_TPID: |
| 289 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); |
| 290 | |
| 291 | /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ |
| 292 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 293 | offsetof(struct sk_buff, vlan_proto)); |
| 294 | /* A = ntohs(A) [emitting a nop or swap16] */ |
| 295 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); |
| 296 | break; |
| 297 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 298 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
| 299 | case SKF_AD_OFF + SKF_AD_NLATTR: |
| 300 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: |
| 301 | case SKF_AD_OFF + SKF_AD_CPU: |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 302 | case SKF_AD_OFF + SKF_AD_RANDOM: |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 303 | /* arg1 = CTX */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 304 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 305 | /* arg2 = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 306 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 307 | /* arg3 = X */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 308 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 309 | /* Emit call(arg1=CTX, arg2=A, arg3=X) */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 310 | switch (fp->k) { |
| 311 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 312 | *insn = BPF_EMIT_CALL(__skb_get_pay_offset); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 313 | break; |
| 314 | case SKF_AD_OFF + SKF_AD_NLATTR: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 315 | *insn = BPF_EMIT_CALL(__skb_get_nlattr); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 316 | break; |
| 317 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 318 | *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 319 | break; |
| 320 | case SKF_AD_OFF + SKF_AD_CPU: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 321 | *insn = BPF_EMIT_CALL(__get_raw_cpu_id); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 322 | break; |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 323 | case SKF_AD_OFF + SKF_AD_RANDOM: |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 324 | *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); |
| 325 | bpf_user_rnd_init_once(); |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 326 | break; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 327 | } |
| 328 | break; |
| 329 | |
| 330 | case SKF_AD_OFF + SKF_AD_ALU_XOR_X: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 331 | /* A ^= X */ |
| 332 | *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 333 | break; |
| 334 | |
| 335 | default: |
| 336 | /* This is just a dummy call to avoid letting the compiler |
| 337 | * evict __bpf_call_base() as an optimization. Placed here |
| 338 | * where no-one bothers. |
| 339 | */ |
| 340 | BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | *insnp = insn; |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | /** |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 349 | * bpf_convert_filter - convert filter program |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 350 | * @prog: the user passed filter program |
| 351 | * @len: the length of the user passed filter program |
| 352 | * @new_prog: buffer where converted program will be stored |
| 353 | * @new_len: pointer to store length of converted program |
| 354 | * |
| 355 | * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style. |
| 356 | * Conversion workflow: |
| 357 | * |
| 358 | * 1) First pass for calculating the new program length: |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 359 | * bpf_convert_filter(old_prog, old_len, NULL, &new_len) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 360 | * |
| 361 | * 2) 2nd pass to remap in two passes: 1st pass finds new |
| 362 | * jump offsets, 2nd pass remapping: |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 363 | * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len); |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 364 | * bpf_convert_filter(old_prog, old_len, new_prog, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 365 | */ |
Nicolas Schichan | d9e12f4 | 2015-05-06 16:12:28 +0200 | [diff] [blame] | 366 | static int bpf_convert_filter(struct sock_filter *prog, int len, |
| 367 | struct bpf_insn *new_prog, int *new_len) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 368 | { |
| 369 | int new_flen = 0, pass = 0, target, i; |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 370 | struct bpf_insn *new_insn; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 371 | struct sock_filter *fp; |
| 372 | int *addrs = NULL; |
| 373 | u8 bpf_src; |
| 374 | |
| 375 | BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 376 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 377 | |
Kees Cook | 6f9a093 | 2014-06-18 15:34:57 -0700 | [diff] [blame] | 378 | if (len <= 0 || len > BPF_MAXINSNS) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 379 | return -EINVAL; |
| 380 | |
| 381 | if (new_prog) { |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 382 | addrs = kcalloc(len, sizeof(*addrs), |
| 383 | GFP_KERNEL | __GFP_NOWARN); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 384 | if (!addrs) |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | |
| 388 | do_pass: |
| 389 | new_insn = new_prog; |
| 390 | fp = prog; |
| 391 | |
Daniel Borkmann | 8b614ae | 2015-12-17 23:51:54 +0100 | [diff] [blame] | 392 | /* Classic BPF related prologue emission. */ |
| 393 | if (new_insn) { |
| 394 | /* Classic BPF expects A and X to be reset first. These need |
| 395 | * to be guaranteed to be the first two instructions. |
| 396 | */ |
| 397 | *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); |
| 398 | *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); |
| 399 | |
| 400 | /* All programs must keep CTX in callee saved BPF_REG_CTX. |
| 401 | * In eBPF case it's done by the compiler, here we need to |
| 402 | * do this ourself. Initial CTX is present in BPF_REG_ARG1. |
| 403 | */ |
| 404 | *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); |
| 405 | } else { |
| 406 | new_insn += 3; |
| 407 | } |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 408 | |
| 409 | for (i = 0; i < len; fp++, i++) { |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 410 | struct bpf_insn tmp_insns[6] = { }; |
| 411 | struct bpf_insn *insn = tmp_insns; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 412 | |
| 413 | if (addrs) |
| 414 | addrs[i] = new_insn - new_prog; |
| 415 | |
| 416 | switch (fp->code) { |
| 417 | /* All arithmetic insns and skb loads map as-is. */ |
| 418 | case BPF_ALU | BPF_ADD | BPF_X: |
| 419 | case BPF_ALU | BPF_ADD | BPF_K: |
| 420 | case BPF_ALU | BPF_SUB | BPF_X: |
| 421 | case BPF_ALU | BPF_SUB | BPF_K: |
| 422 | case BPF_ALU | BPF_AND | BPF_X: |
| 423 | case BPF_ALU | BPF_AND | BPF_K: |
| 424 | case BPF_ALU | BPF_OR | BPF_X: |
| 425 | case BPF_ALU | BPF_OR | BPF_K: |
| 426 | case BPF_ALU | BPF_LSH | BPF_X: |
| 427 | case BPF_ALU | BPF_LSH | BPF_K: |
| 428 | case BPF_ALU | BPF_RSH | BPF_X: |
| 429 | case BPF_ALU | BPF_RSH | BPF_K: |
| 430 | case BPF_ALU | BPF_XOR | BPF_X: |
| 431 | case BPF_ALU | BPF_XOR | BPF_K: |
| 432 | case BPF_ALU | BPF_MUL | BPF_X: |
| 433 | case BPF_ALU | BPF_MUL | BPF_K: |
| 434 | case BPF_ALU | BPF_DIV | BPF_X: |
| 435 | case BPF_ALU | BPF_DIV | BPF_K: |
| 436 | case BPF_ALU | BPF_MOD | BPF_X: |
| 437 | case BPF_ALU | BPF_MOD | BPF_K: |
| 438 | case BPF_ALU | BPF_NEG: |
| 439 | case BPF_LD | BPF_ABS | BPF_W: |
| 440 | case BPF_LD | BPF_ABS | BPF_H: |
| 441 | case BPF_LD | BPF_ABS | BPF_B: |
| 442 | case BPF_LD | BPF_IND | BPF_W: |
| 443 | case BPF_LD | BPF_IND | BPF_H: |
| 444 | case BPF_LD | BPF_IND | BPF_B: |
| 445 | /* Check for overloaded BPF extension and |
| 446 | * directly convert it if found, otherwise |
| 447 | * just move on with mapping. |
| 448 | */ |
| 449 | if (BPF_CLASS(fp->code) == BPF_LD && |
| 450 | BPF_MODE(fp->code) == BPF_ABS && |
| 451 | convert_bpf_extensions(fp, &insn)) |
| 452 | break; |
| 453 | |
Alexei Starovoitov | 265d765 | 2018-01-29 02:49:00 +0100 | [diff] [blame] | 454 | if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) || |
| 455 | fp->code == (BPF_ALU | BPF_MOD | BPF_X)) |
| 456 | *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X); |
| 457 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 458 | *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 459 | break; |
| 460 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 461 | /* Jump transformation cannot use BPF block macros |
| 462 | * everywhere as offset calculation and target updates |
| 463 | * require a bit more work than the rest, i.e. jump |
| 464 | * opcodes map as-is, but offsets need adjustment. |
| 465 | */ |
| 466 | |
| 467 | #define BPF_EMIT_JMP \ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 468 | do { \ |
| 469 | if (target >= len || target < 0) \ |
| 470 | goto err; \ |
| 471 | insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ |
| 472 | /* Adjust pc relative offset for 2nd or 3rd insn. */ \ |
| 473 | insn->off -= insn - tmp_insns; \ |
| 474 | } while (0) |
| 475 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 476 | case BPF_JMP | BPF_JA: |
| 477 | target = i + fp->k + 1; |
| 478 | insn->code = fp->code; |
| 479 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 480 | break; |
| 481 | |
| 482 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 483 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 484 | case BPF_JMP | BPF_JSET | BPF_K: |
| 485 | case BPF_JMP | BPF_JSET | BPF_X: |
| 486 | case BPF_JMP | BPF_JGT | BPF_K: |
| 487 | case BPF_JMP | BPF_JGT | BPF_X: |
| 488 | case BPF_JMP | BPF_JGE | BPF_K: |
| 489 | case BPF_JMP | BPF_JGE | BPF_X: |
| 490 | if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { |
| 491 | /* BPF immediates are signed, zero extend |
| 492 | * immediate into tmp register and use it |
| 493 | * in compare insn. |
| 494 | */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 495 | *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 496 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 497 | insn->dst_reg = BPF_REG_A; |
| 498 | insn->src_reg = BPF_REG_TMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 499 | bpf_src = BPF_X; |
| 500 | } else { |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 501 | insn->dst_reg = BPF_REG_A; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 502 | insn->imm = fp->k; |
| 503 | bpf_src = BPF_SRC(fp->code); |
Tycho Andersen | 19539ce | 2015-09-10 18:25:07 -0600 | [diff] [blame] | 504 | insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* Common case where 'jump_false' is next insn. */ |
| 508 | if (fp->jf == 0) { |
| 509 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; |
| 510 | target = i + fp->jt + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 511 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | |
| 515 | /* Convert JEQ into JNE when 'jump_true' is next insn. */ |
| 516 | if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) { |
| 517 | insn->code = BPF_JMP | BPF_JNE | bpf_src; |
| 518 | target = i + fp->jf + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 519 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 520 | break; |
| 521 | } |
| 522 | |
| 523 | /* Other jumps are mapped into two insns: Jxx and JA. */ |
| 524 | target = i + fp->jt + 1; |
| 525 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 526 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 527 | insn++; |
| 528 | |
| 529 | insn->code = BPF_JMP | BPF_JA; |
| 530 | target = i + fp->jf + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 531 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ |
| 535 | case BPF_LDX | BPF_MSH | BPF_B: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 536 | /* tmp = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 537 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A); |
David S. Miller | 1268e25 | 2014-05-13 13:13:33 -0400 | [diff] [blame] | 538 | /* A = BPF_R0 = *(u8 *) (skb->data + K) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 539 | *insn++ = BPF_LD_ABS(BPF_B, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 540 | /* A &= 0xf */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 541 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 542 | /* A <<= 2 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 543 | *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 544 | /* X = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 545 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 546 | /* A = tmp */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 547 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 548 | break; |
| 549 | |
Daniel Borkmann | 6205b9c | 2016-02-19 23:05:27 +0100 | [diff] [blame] | 550 | /* RET_K is remaped into 2 insns. RET_A case doesn't need an |
| 551 | * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. |
| 552 | */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 553 | case BPF_RET | BPF_A: |
| 554 | case BPF_RET | BPF_K: |
Daniel Borkmann | 6205b9c | 2016-02-19 23:05:27 +0100 | [diff] [blame] | 555 | if (BPF_RVAL(fp->code) == BPF_K) |
| 556 | *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, |
| 557 | 0, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 558 | *insn = BPF_EXIT_INSN(); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 559 | break; |
| 560 | |
| 561 | /* Store to stack. */ |
| 562 | case BPF_ST: |
| 563 | case BPF_STX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 564 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == |
| 565 | BPF_ST ? BPF_REG_A : BPF_REG_X, |
| 566 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | /* Load from stack. */ |
| 570 | case BPF_LD | BPF_MEM: |
| 571 | case BPF_LDX | BPF_MEM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 572 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 573 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, |
| 574 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 575 | break; |
| 576 | |
| 577 | /* A = K or X = K */ |
| 578 | case BPF_LD | BPF_IMM: |
| 579 | case BPF_LDX | BPF_IMM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 580 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? |
| 581 | BPF_REG_A : BPF_REG_X, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 582 | break; |
| 583 | |
| 584 | /* X = A */ |
| 585 | case BPF_MISC | BPF_TAX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 586 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 587 | break; |
| 588 | |
| 589 | /* A = X */ |
| 590 | case BPF_MISC | BPF_TXA: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 591 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 592 | break; |
| 593 | |
| 594 | /* A = skb->len or X = skb->len */ |
| 595 | case BPF_LD | BPF_W | BPF_LEN: |
| 596 | case BPF_LDX | BPF_W | BPF_LEN: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 597 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 598 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, |
| 599 | offsetof(struct sk_buff, len)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 600 | break; |
| 601 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 602 | /* Access seccomp_data fields. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 603 | case BPF_LDX | BPF_ABS | BPF_W: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 604 | /* A = *(u32 *) (ctx + K) */ |
| 605 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 606 | break; |
| 607 | |
Stephen Hemminger | ca9f1fd | 2015-02-14 13:47:54 -0500 | [diff] [blame] | 608 | /* Unknown instruction. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 609 | default: |
| 610 | goto err; |
| 611 | } |
| 612 | |
| 613 | insn++; |
| 614 | if (new_prog) |
| 615 | memcpy(new_insn, tmp_insns, |
| 616 | sizeof(*insn) * (insn - tmp_insns)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 617 | new_insn += insn - tmp_insns; |
| 618 | } |
| 619 | |
| 620 | if (!new_prog) { |
| 621 | /* Only calculating new length. */ |
| 622 | *new_len = new_insn - new_prog; |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | pass++; |
| 627 | if (new_flen != new_insn - new_prog) { |
| 628 | new_flen = new_insn - new_prog; |
| 629 | if (pass > 2) |
| 630 | goto err; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 631 | goto do_pass; |
| 632 | } |
| 633 | |
| 634 | kfree(addrs); |
| 635 | BUG_ON(*new_len != new_flen); |
| 636 | return 0; |
| 637 | err: |
| 638 | kfree(addrs); |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | |
| 642 | /* Security: |
| 643 | * |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 644 | * As we dont want to clear mem[] array for each packet going through |
Li RongQing | 8ea6e34 | 2014-10-10 13:56:51 +0800 | [diff] [blame] | 645 | * __bpf_prog_run(), we check that filter loaded by user never try to read |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 646 | * a cell if not previously written, and we check all branches to be sure |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 647 | * a malicious user doesn't try to abuse us. |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 648 | */ |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 649 | static int check_load_and_stores(const struct sock_filter *filter, int flen) |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 650 | { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 651 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 652 | int pc, ret = 0; |
| 653 | |
| 654 | BUILD_BUG_ON(BPF_MEMWORDS > 16); |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 655 | |
Tobias Klauser | 99e72a0 | 2014-06-24 15:33:22 +0200 | [diff] [blame] | 656 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 657 | if (!masks) |
| 658 | return -ENOMEM; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 659 | |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 660 | memset(masks, 0xff, flen * sizeof(*masks)); |
| 661 | |
| 662 | for (pc = 0; pc < flen; pc++) { |
| 663 | memvalid &= masks[pc]; |
| 664 | |
| 665 | switch (filter[pc].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 666 | case BPF_ST: |
| 667 | case BPF_STX: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 668 | memvalid |= (1 << filter[pc].k); |
| 669 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 670 | case BPF_LD | BPF_MEM: |
| 671 | case BPF_LDX | BPF_MEM: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 672 | if (!(memvalid & (1 << filter[pc].k))) { |
| 673 | ret = -EINVAL; |
| 674 | goto error; |
| 675 | } |
| 676 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 677 | case BPF_JMP | BPF_JA: |
| 678 | /* A jump must set masks on target */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 679 | masks[pc + 1 + filter[pc].k] &= memvalid; |
| 680 | memvalid = ~0; |
| 681 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 682 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 683 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 684 | case BPF_JMP | BPF_JGE | BPF_K: |
| 685 | case BPF_JMP | BPF_JGE | BPF_X: |
| 686 | case BPF_JMP | BPF_JGT | BPF_K: |
| 687 | case BPF_JMP | BPF_JGT | BPF_X: |
| 688 | case BPF_JMP | BPF_JSET | BPF_K: |
| 689 | case BPF_JMP | BPF_JSET | BPF_X: |
| 690 | /* A jump must set masks on targets */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 691 | masks[pc + 1 + filter[pc].jt] &= memvalid; |
| 692 | masks[pc + 1 + filter[pc].jf] &= memvalid; |
| 693 | memvalid = ~0; |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | error: |
| 698 | kfree(masks); |
| 699 | return ret; |
| 700 | } |
| 701 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 702 | static bool chk_code_allowed(u16 code_to_probe) |
| 703 | { |
| 704 | static const bool codes[] = { |
| 705 | /* 32 bit ALU operations */ |
| 706 | [BPF_ALU | BPF_ADD | BPF_K] = true, |
| 707 | [BPF_ALU | BPF_ADD | BPF_X] = true, |
| 708 | [BPF_ALU | BPF_SUB | BPF_K] = true, |
| 709 | [BPF_ALU | BPF_SUB | BPF_X] = true, |
| 710 | [BPF_ALU | BPF_MUL | BPF_K] = true, |
| 711 | [BPF_ALU | BPF_MUL | BPF_X] = true, |
| 712 | [BPF_ALU | BPF_DIV | BPF_K] = true, |
| 713 | [BPF_ALU | BPF_DIV | BPF_X] = true, |
| 714 | [BPF_ALU | BPF_MOD | BPF_K] = true, |
| 715 | [BPF_ALU | BPF_MOD | BPF_X] = true, |
| 716 | [BPF_ALU | BPF_AND | BPF_K] = true, |
| 717 | [BPF_ALU | BPF_AND | BPF_X] = true, |
| 718 | [BPF_ALU | BPF_OR | BPF_K] = true, |
| 719 | [BPF_ALU | BPF_OR | BPF_X] = true, |
| 720 | [BPF_ALU | BPF_XOR | BPF_K] = true, |
| 721 | [BPF_ALU | BPF_XOR | BPF_X] = true, |
| 722 | [BPF_ALU | BPF_LSH | BPF_K] = true, |
| 723 | [BPF_ALU | BPF_LSH | BPF_X] = true, |
| 724 | [BPF_ALU | BPF_RSH | BPF_K] = true, |
| 725 | [BPF_ALU | BPF_RSH | BPF_X] = true, |
| 726 | [BPF_ALU | BPF_NEG] = true, |
| 727 | /* Load instructions */ |
| 728 | [BPF_LD | BPF_W | BPF_ABS] = true, |
| 729 | [BPF_LD | BPF_H | BPF_ABS] = true, |
| 730 | [BPF_LD | BPF_B | BPF_ABS] = true, |
| 731 | [BPF_LD | BPF_W | BPF_LEN] = true, |
| 732 | [BPF_LD | BPF_W | BPF_IND] = true, |
| 733 | [BPF_LD | BPF_H | BPF_IND] = true, |
| 734 | [BPF_LD | BPF_B | BPF_IND] = true, |
| 735 | [BPF_LD | BPF_IMM] = true, |
| 736 | [BPF_LD | BPF_MEM] = true, |
| 737 | [BPF_LDX | BPF_W | BPF_LEN] = true, |
| 738 | [BPF_LDX | BPF_B | BPF_MSH] = true, |
| 739 | [BPF_LDX | BPF_IMM] = true, |
| 740 | [BPF_LDX | BPF_MEM] = true, |
| 741 | /* Store instructions */ |
| 742 | [BPF_ST] = true, |
| 743 | [BPF_STX] = true, |
| 744 | /* Misc instructions */ |
| 745 | [BPF_MISC | BPF_TAX] = true, |
| 746 | [BPF_MISC | BPF_TXA] = true, |
| 747 | /* Return instructions */ |
| 748 | [BPF_RET | BPF_K] = true, |
| 749 | [BPF_RET | BPF_A] = true, |
| 750 | /* Jump instructions */ |
| 751 | [BPF_JMP | BPF_JA] = true, |
| 752 | [BPF_JMP | BPF_JEQ | BPF_K] = true, |
| 753 | [BPF_JMP | BPF_JEQ | BPF_X] = true, |
| 754 | [BPF_JMP | BPF_JGE | BPF_K] = true, |
| 755 | [BPF_JMP | BPF_JGE | BPF_X] = true, |
| 756 | [BPF_JMP | BPF_JGT | BPF_K] = true, |
| 757 | [BPF_JMP | BPF_JGT | BPF_X] = true, |
| 758 | [BPF_JMP | BPF_JSET | BPF_K] = true, |
| 759 | [BPF_JMP | BPF_JSET | BPF_X] = true, |
| 760 | }; |
| 761 | |
| 762 | if (code_to_probe >= ARRAY_SIZE(codes)) |
| 763 | return false; |
| 764 | |
| 765 | return codes[code_to_probe]; |
| 766 | } |
| 767 | |
Daniel Borkmann | f7bd9e3 | 2016-06-10 21:19:07 +0200 | [diff] [blame] | 768 | static bool bpf_check_basics_ok(const struct sock_filter *filter, |
| 769 | unsigned int flen) |
| 770 | { |
| 771 | if (filter == NULL) |
| 772 | return false; |
| 773 | if (flen == 0 || flen > BPF_MAXINSNS) |
| 774 | return false; |
| 775 | |
| 776 | return true; |
| 777 | } |
| 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /** |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 780 | * bpf_check_classic - verify socket filter code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * @filter: filter to verify |
| 782 | * @flen: length of filter |
| 783 | * |
| 784 | * Check the user's filter code. If we let some ugly |
| 785 | * filter code slip through kaboom! The filter must contain |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 786 | * no references or jumps that are out of range, no illegal |
| 787 | * instructions, and must end with a RET instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | * |
Kris Katterjohn | 7b11f69 | 2006-01-13 14:33:06 -0800 | [diff] [blame] | 789 | * All jumps are forward as they are not signed. |
| 790 | * |
| 791 | * Returns 0 if the rule set is legal or -EINVAL if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | */ |
Nicolas Schichan | d9e12f4 | 2015-05-06 16:12:28 +0200 | [diff] [blame] | 793 | static int bpf_check_classic(const struct sock_filter *filter, |
| 794 | unsigned int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 796 | bool anc_found; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 797 | int pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 799 | /* Check the filter code now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | for (pc = 0; pc < flen; pc++) { |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 801 | const struct sock_filter *ftest = &filter[pc]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 803 | /* May we actually operate on this code? */ |
| 804 | if (!chk_code_allowed(ftest->code)) |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 805 | return -EINVAL; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 806 | |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 807 | /* Some instructions need special checks */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 808 | switch (ftest->code) { |
| 809 | case BPF_ALU | BPF_DIV | BPF_K: |
| 810 | case BPF_ALU | BPF_MOD | BPF_K: |
| 811 | /* Check for division by zero */ |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame] | 812 | if (ftest->k == 0) |
| 813 | return -EINVAL; |
| 814 | break; |
Rabin Vincent | 229394e | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 815 | case BPF_ALU | BPF_LSH | BPF_K: |
| 816 | case BPF_ALU | BPF_RSH | BPF_K: |
| 817 | if (ftest->k >= 32) |
| 818 | return -EINVAL; |
| 819 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 820 | case BPF_LD | BPF_MEM: |
| 821 | case BPF_LDX | BPF_MEM: |
| 822 | case BPF_ST: |
| 823 | case BPF_STX: |
| 824 | /* Check for invalid memory addresses */ |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 825 | if (ftest->k >= BPF_MEMWORDS) |
| 826 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 827 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 828 | case BPF_JMP | BPF_JA: |
| 829 | /* Note, the large ftest->k might cause loops. |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 830 | * Compare this with conditional jumps below, |
| 831 | * where offsets are limited. --ANK (981016) |
| 832 | */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 833 | if (ftest->k >= (unsigned int)(flen - pc - 1)) |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 834 | return -EINVAL; |
| 835 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 836 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 837 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 838 | case BPF_JMP | BPF_JGE | BPF_K: |
| 839 | case BPF_JMP | BPF_JGE | BPF_X: |
| 840 | case BPF_JMP | BPF_JGT | BPF_K: |
| 841 | case BPF_JMP | BPF_JGT | BPF_X: |
| 842 | case BPF_JMP | BPF_JSET | BPF_K: |
| 843 | case BPF_JMP | BPF_JSET | BPF_X: |
| 844 | /* Both conditionals must be safe */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 845 | if (pc + ftest->jt + 1 >= flen || |
| 846 | pc + ftest->jf + 1 >= flen) |
| 847 | return -EINVAL; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 848 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 849 | case BPF_LD | BPF_W | BPF_ABS: |
| 850 | case BPF_LD | BPF_H | BPF_ABS: |
| 851 | case BPF_LD | BPF_B | BPF_ABS: |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 852 | anc_found = false; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 853 | if (bpf_anc_helper(ftest) & BPF_ANC) |
| 854 | anc_found = true; |
| 855 | /* Ancillary operation unknown or unsupported */ |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 856 | if (anc_found == false && ftest->k >= SKF_AD_OFF) |
| 857 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 861 | /* Last instruction must be a RET code */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 862 | switch (filter[flen - 1].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 863 | case BPF_RET | BPF_K: |
| 864 | case BPF_RET | BPF_A: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 865 | return check_load_and_stores(filter, flen); |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 866 | } |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 867 | |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 868 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 871 | static int bpf_prog_store_orig_filter(struct bpf_prog *fp, |
| 872 | const struct sock_fprog *fprog) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 873 | { |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 874 | unsigned int fsize = bpf_classic_proglen(fprog); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 875 | struct sock_fprog_kern *fkprog; |
| 876 | |
| 877 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); |
| 878 | if (!fp->orig_prog) |
| 879 | return -ENOMEM; |
| 880 | |
| 881 | fkprog = fp->orig_prog; |
| 882 | fkprog->len = fprog->len; |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 883 | |
| 884 | fkprog->filter = kmemdup(fp->insns, fsize, |
| 885 | GFP_KERNEL | __GFP_NOWARN); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 886 | if (!fkprog->filter) { |
| 887 | kfree(fp->orig_prog); |
| 888 | return -ENOMEM; |
| 889 | } |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 894 | static void bpf_release_orig_filter(struct bpf_prog *fp) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 895 | { |
| 896 | struct sock_fprog_kern *fprog = fp->orig_prog; |
| 897 | |
| 898 | if (fprog) { |
| 899 | kfree(fprog->filter); |
| 900 | kfree(fprog); |
| 901 | } |
| 902 | } |
| 903 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 904 | static void __bpf_prog_release(struct bpf_prog *prog) |
| 905 | { |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 906 | if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 907 | bpf_prog_put(prog); |
| 908 | } else { |
| 909 | bpf_release_orig_filter(prog); |
| 910 | bpf_prog_free(prog); |
| 911 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 914 | static void __sk_filter_release(struct sk_filter *fp) |
| 915 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 916 | __bpf_prog_release(fp->prog); |
| 917 | kfree(fp); |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | /** |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 921 | * sk_filter_release_rcu - Release a socket filter by rcu_head |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 922 | * @rcu: rcu_head that contains the sk_filter to free |
| 923 | */ |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 924 | static void sk_filter_release_rcu(struct rcu_head *rcu) |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 925 | { |
| 926 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); |
| 927 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 928 | __sk_filter_release(fp); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 929 | } |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 930 | |
| 931 | /** |
| 932 | * sk_filter_release - release a socket filter |
| 933 | * @fp: filter to remove |
| 934 | * |
| 935 | * Remove a filter from a socket and release its resources. |
| 936 | */ |
| 937 | static void sk_filter_release(struct sk_filter *fp) |
| 938 | { |
| 939 | if (atomic_dec_and_test(&fp->refcnt)) |
| 940 | call_rcu(&fp->rcu, sk_filter_release_rcu); |
| 941 | } |
| 942 | |
| 943 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) |
| 944 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 945 | u32 filter_size = bpf_prog_size(fp->prog->len); |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 946 | |
| 947 | atomic_sub(filter_size, &sk->sk_omem_alloc); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 948 | sk_filter_release(fp); |
| 949 | } |
| 950 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 951 | /* try to charge the socket memory if there is space available |
| 952 | * return true on success |
| 953 | */ |
Greg Kroah-Hartman | 0044962 | 2017-10-12 21:21:39 +0200 | [diff] [blame] | 954 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 955 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 956 | u32 filter_size = bpf_prog_size(fp->prog->len); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 957 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 958 | /* same check as in sock_kmalloc() */ |
| 959 | if (filter_size <= sysctl_optmem_max && |
| 960 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { |
Greg Kroah-Hartman | 0044962 | 2017-10-12 21:21:39 +0200 | [diff] [blame] | 961 | atomic_inc(&fp->refcnt); |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 962 | atomic_add(filter_size, &sk->sk_omem_alloc); |
| 963 | return true; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 964 | } |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 965 | return false; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 968 | static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 969 | { |
| 970 | struct sock_filter *old_prog; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 971 | struct bpf_prog *old_fp; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 972 | int err, new_len, old_len = fp->len; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 973 | |
| 974 | /* We are free to overwrite insns et al right here as it |
| 975 | * won't be used at this point in time anymore internally |
| 976 | * after the migration to the internal BPF instruction |
| 977 | * representation. |
| 978 | */ |
| 979 | BUILD_BUG_ON(sizeof(struct sock_filter) != |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 980 | sizeof(struct bpf_insn)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 981 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 982 | /* Conversion cannot happen on overlapping memory areas, |
| 983 | * so we need to keep the user BPF around until the 2nd |
| 984 | * pass. At this time, the user BPF is stored in fp->insns. |
| 985 | */ |
| 986 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 987 | GFP_KERNEL | __GFP_NOWARN); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 988 | if (!old_prog) { |
| 989 | err = -ENOMEM; |
| 990 | goto out_err; |
| 991 | } |
| 992 | |
| 993 | /* 1st pass: calculate the new program length. */ |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 994 | err = bpf_convert_filter(old_prog, old_len, NULL, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 995 | if (err) |
| 996 | goto out_err_free; |
| 997 | |
| 998 | /* Expand fp for appending the new filter representation. */ |
| 999 | old_fp = fp; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1000 | fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1001 | if (!fp) { |
| 1002 | /* The old_fp is still around in case we couldn't |
| 1003 | * allocate new memory, so uncharge on that one. |
| 1004 | */ |
| 1005 | fp = old_fp; |
| 1006 | err = -ENOMEM; |
| 1007 | goto out_err_free; |
| 1008 | } |
| 1009 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1010 | fp->len = new_len; |
| 1011 | |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 1012 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 1013 | err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1014 | if (err) |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 1015 | /* 2nd bpf_convert_filter() can fail only if it fails |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1016 | * to allocate memory, remapping must succeed. Note, |
| 1017 | * that at this time old_fp has already been released |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 1018 | * by krealloc(). |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1019 | */ |
| 1020 | goto out_err_free; |
| 1021 | |
Daniel Borkmann | d1c55ab | 2016-05-13 19:08:31 +0200 | [diff] [blame] | 1022 | fp = bpf_prog_select_runtime(fp, &err); |
Alexei Starovoitov | a3d6dd6 | 2018-01-29 02:48:56 +0100 | [diff] [blame] | 1023 | if (err) |
| 1024 | goto out_err_free; |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 1025 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1026 | kfree(old_prog); |
| 1027 | return fp; |
| 1028 | |
| 1029 | out_err_free: |
| 1030 | kfree(old_prog); |
| 1031 | out_err: |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1032 | __bpf_prog_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1033 | return ERR_PTR(err); |
| 1034 | } |
| 1035 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1036 | static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, |
| 1037 | bpf_aux_classic_check_t trans) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1038 | { |
| 1039 | int err; |
| 1040 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1041 | fp->bpf_func = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1042 | fp->jited = 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1043 | |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 1044 | err = bpf_check_classic(fp->insns, fp->len); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 1045 | if (err) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1046 | __bpf_prog_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1047 | return ERR_PTR(err); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 1048 | } |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1049 | |
Nicolas Schichan | 4ae92bc | 2015-05-06 16:12:27 +0200 | [diff] [blame] | 1050 | /* There might be additional checks and transformations |
| 1051 | * needed on classic filters, f.e. in case of seccomp. |
| 1052 | */ |
| 1053 | if (trans) { |
| 1054 | err = trans(fp->insns, fp->len); |
| 1055 | if (err) { |
| 1056 | __bpf_prog_release(fp); |
| 1057 | return ERR_PTR(err); |
| 1058 | } |
| 1059 | } |
| 1060 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1061 | /* Probe if we can JIT compile the filter and if so, do |
| 1062 | * the compilation of the filter. |
| 1063 | */ |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1064 | bpf_jit_compile(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1065 | |
| 1066 | /* JIT compiler couldn't process this filter, so do the |
| 1067 | * internal BPF translation for the optimized interpreter. |
| 1068 | */ |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 1069 | if (!fp->jited) |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1070 | fp = bpf_migrate_filter(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1071 | |
| 1072 | return fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /** |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1076 | * bpf_prog_create - create an unattached filter |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 1077 | * @pfp: the unattached filter that is created |
Tobias Klauser | 677a9fd | 2014-06-24 15:33:21 +0200 | [diff] [blame] | 1078 | * @fprog: the filter program |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1079 | * |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 1080 | * Create a filter independent of any socket. We first run some |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1081 | * sanity checks on it to make sure it does not explode on us later. |
| 1082 | * If an error occurs or there is insufficient memory for the filter |
| 1083 | * a negative errno code is returned. On success the return is zero. |
| 1084 | */ |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1085 | int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1086 | { |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 1087 | unsigned int fsize = bpf_classic_proglen(fprog); |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1088 | struct bpf_prog *fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* Make sure new filter is there and in the right amounts. */ |
Daniel Borkmann | f7bd9e3 | 2016-06-10 21:19:07 +0200 | [diff] [blame] | 1091 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1092 | return -EINVAL; |
| 1093 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1094 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1095 | if (!fp) |
| 1096 | return -ENOMEM; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1097 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1098 | memcpy(fp->insns, fprog->filter, fsize); |
| 1099 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1100 | fp->len = fprog->len; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1101 | /* Since unattached filters are not copied back to user |
| 1102 | * space through sk_get_filter(), we do not need to hold |
| 1103 | * a copy here, and can spare us the work. |
| 1104 | */ |
| 1105 | fp->orig_prog = NULL; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1106 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1107 | /* bpf_prepare_filter() already takes care of freeing |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1108 | * memory in case something goes wrong. |
| 1109 | */ |
Nicolas Schichan | 4ae92bc | 2015-05-06 16:12:27 +0200 | [diff] [blame] | 1110 | fp = bpf_prepare_filter(fp, NULL); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1111 | if (IS_ERR(fp)) |
| 1112 | return PTR_ERR(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1113 | |
| 1114 | *pfp = fp; |
| 1115 | return 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1116 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1117 | EXPORT_SYMBOL_GPL(bpf_prog_create); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1118 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1119 | /** |
| 1120 | * bpf_prog_create_from_user - create an unattached filter from user buffer |
| 1121 | * @pfp: the unattached filter that is created |
| 1122 | * @fprog: the filter program |
| 1123 | * @trans: post-classic verifier transformation handler |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1124 | * @save_orig: save classic BPF program |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1125 | * |
| 1126 | * This function effectively does the same as bpf_prog_create(), only |
| 1127 | * that it builds up its insns buffer from user space provided buffer. |
| 1128 | * It also allows for passing a bpf_aux_classic_check_t handler. |
| 1129 | */ |
| 1130 | int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1131 | bpf_aux_classic_check_t trans, bool save_orig) |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1132 | { |
| 1133 | unsigned int fsize = bpf_classic_proglen(fprog); |
| 1134 | struct bpf_prog *fp; |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1135 | int err; |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1136 | |
| 1137 | /* Make sure new filter is there and in the right amounts. */ |
Daniel Borkmann | f7bd9e3 | 2016-06-10 21:19:07 +0200 | [diff] [blame] | 1138 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1139 | return -EINVAL; |
| 1140 | |
| 1141 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
| 1142 | if (!fp) |
| 1143 | return -ENOMEM; |
| 1144 | |
| 1145 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { |
| 1146 | __bpf_prog_free(fp); |
| 1147 | return -EFAULT; |
| 1148 | } |
| 1149 | |
| 1150 | fp->len = fprog->len; |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1151 | fp->orig_prog = NULL; |
| 1152 | |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1153 | if (save_orig) { |
| 1154 | err = bpf_prog_store_orig_filter(fp, fprog); |
| 1155 | if (err) { |
| 1156 | __bpf_prog_free(fp); |
| 1157 | return -ENOMEM; |
| 1158 | } |
| 1159 | } |
| 1160 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1161 | /* bpf_prepare_filter() already takes care of freeing |
| 1162 | * memory in case something goes wrong. |
| 1163 | */ |
| 1164 | fp = bpf_prepare_filter(fp, trans); |
| 1165 | if (IS_ERR(fp)) |
| 1166 | return PTR_ERR(fp); |
| 1167 | |
| 1168 | *pfp = fp; |
| 1169 | return 0; |
| 1170 | } |
David S. Miller | 2ea273d | 2015-08-17 14:37:06 -0700 | [diff] [blame] | 1171 | EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1172 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1173 | void bpf_prog_destroy(struct bpf_prog *fp) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1174 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1175 | __bpf_prog_release(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1176 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1177 | EXPORT_SYMBOL_GPL(bpf_prog_destroy); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1178 | |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1179 | static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1180 | { |
| 1181 | struct sk_filter *fp, *old_fp; |
| 1182 | |
| 1183 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); |
| 1184 | if (!fp) |
| 1185 | return -ENOMEM; |
| 1186 | |
| 1187 | fp->prog = prog; |
| 1188 | atomic_set(&fp->refcnt, 0); |
| 1189 | |
| 1190 | if (!sk_filter_charge(sk, fp)) { |
| 1191 | kfree(fp); |
| 1192 | return -ENOMEM; |
| 1193 | } |
| 1194 | |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1195 | old_fp = rcu_dereference_protected(sk->sk_filter, |
| 1196 | lockdep_sock_is_held(sk)); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1197 | rcu_assign_pointer(sk->sk_filter, fp); |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1198 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1199 | if (old_fp) |
| 1200 | sk_filter_uncharge(sk, old_fp); |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1205 | static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk) |
| 1206 | { |
| 1207 | struct bpf_prog *old_prog; |
| 1208 | int err; |
| 1209 | |
| 1210 | if (bpf_prog_size(prog->len) > sysctl_optmem_max) |
| 1211 | return -ENOMEM; |
| 1212 | |
Craig Gallek | fa46349 | 2016-02-10 11:50:39 -0500 | [diff] [blame] | 1213 | if (sk_unhashed(sk) && sk->sk_reuseport) { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1214 | err = reuseport_alloc(sk); |
| 1215 | if (err) |
| 1216 | return err; |
| 1217 | } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) { |
| 1218 | /* The socket wasn't bound with SO_REUSEPORT */ |
| 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
| 1222 | old_prog = reuseport_attach_prog(sk, prog); |
| 1223 | if (old_prog) |
| 1224 | bpf_prog_destroy(old_prog); |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | static |
| 1230 | struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) |
| 1231 | { |
| 1232 | unsigned int fsize = bpf_classic_proglen(fprog); |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1233 | struct bpf_prog *prog; |
| 1234 | int err; |
| 1235 | |
| 1236 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1237 | return ERR_PTR(-EPERM); |
| 1238 | |
| 1239 | /* Make sure new filter is there and in the right amounts. */ |
Daniel Borkmann | f7bd9e3 | 2016-06-10 21:19:07 +0200 | [diff] [blame] | 1240 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1241 | return ERR_PTR(-EINVAL); |
| 1242 | |
Daniel Borkmann | f7bd9e3 | 2016-06-10 21:19:07 +0200 | [diff] [blame] | 1243 | prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1244 | if (!prog) |
| 1245 | return ERR_PTR(-ENOMEM); |
| 1246 | |
| 1247 | if (copy_from_user(prog->insns, fprog->filter, fsize)) { |
| 1248 | __bpf_prog_free(prog); |
| 1249 | return ERR_PTR(-EFAULT); |
| 1250 | } |
| 1251 | |
| 1252 | prog->len = fprog->len; |
| 1253 | |
| 1254 | err = bpf_prog_store_orig_filter(prog, fprog); |
| 1255 | if (err) { |
| 1256 | __bpf_prog_free(prog); |
| 1257 | return ERR_PTR(-ENOMEM); |
| 1258 | } |
| 1259 | |
| 1260 | /* bpf_prepare_filter() already takes care of freeing |
| 1261 | * memory in case something goes wrong. |
| 1262 | */ |
| 1263 | return bpf_prepare_filter(prog, NULL); |
| 1264 | } |
| 1265 | |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 1266 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | * sk_attach_filter - attach a socket filter |
| 1268 | * @fprog: the filter program |
| 1269 | * @sk: the socket to use |
| 1270 | * |
| 1271 | * Attach the user's filter code. We first run some sanity checks on |
| 1272 | * it to make sure it does not explode on us later. If an error |
| 1273 | * occurs or there is insufficient memory for the filter a negative |
| 1274 | * errno code is returned. On success the return is zero. |
| 1275 | */ |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1276 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1278 | struct bpf_prog *prog = __get_filter(fprog, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | int err; |
| 1280 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1281 | if (IS_ERR(prog)) |
| 1282 | return PTR_ERR(prog); |
| 1283 | |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1284 | err = __sk_attach_prog(prog, sk); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1285 | if (err < 0) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1286 | __bpf_prog_release(prog); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1287 | return err; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1288 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1289 | |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1290 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1292 | EXPORT_SYMBOL_GPL(sk_attach_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1294 | int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1295 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1296 | struct bpf_prog *prog = __get_filter(fprog, sk); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1297 | int err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1298 | |
Alexei Starovoitov | 198bf1b | 2014-12-10 20:14:55 -0800 | [diff] [blame] | 1299 | if (IS_ERR(prog)) |
| 1300 | return PTR_ERR(prog); |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1301 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1302 | err = __reuseport_attach_prog(prog, sk); |
| 1303 | if (err < 0) { |
| 1304 | __bpf_prog_release(prog); |
| 1305 | return err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) |
| 1312 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1313 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1314 | return ERR_PTR(-EPERM); |
| 1315 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1316 | return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | int sk_attach_bpf(u32 ufd, struct sock *sk) |
| 1320 | { |
| 1321 | struct bpf_prog *prog = __get_bpf(ufd, sk); |
| 1322 | int err; |
| 1323 | |
| 1324 | if (IS_ERR(prog)) |
| 1325 | return PTR_ERR(prog); |
| 1326 | |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 1327 | err = __sk_attach_prog(prog, sk); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1328 | if (err < 0) { |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1329 | bpf_prog_put(prog); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1330 | return err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1331 | } |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1332 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1333 | return 0; |
| 1334 | } |
| 1335 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1336 | int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) |
| 1337 | { |
| 1338 | struct bpf_prog *prog = __get_bpf(ufd, sk); |
| 1339 | int err; |
| 1340 | |
| 1341 | if (IS_ERR(prog)) |
| 1342 | return PTR_ERR(prog); |
| 1343 | |
| 1344 | err = __reuseport_attach_prog(prog, sk); |
| 1345 | if (err < 0) { |
| 1346 | bpf_prog_put(prog); |
| 1347 | return err; |
| 1348 | } |
| 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1353 | struct bpf_scratchpad { |
| 1354 | union { |
| 1355 | __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; |
| 1356 | u8 buff[MAX_BPF_STACK]; |
| 1357 | }; |
| 1358 | }; |
| 1359 | |
| 1360 | static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1361 | |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 1362 | static inline int __bpf_try_make_writable(struct sk_buff *skb, |
| 1363 | unsigned int write_len) |
| 1364 | { |
| 1365 | return skb_ensure_writable(skb, write_len); |
| 1366 | } |
| 1367 | |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1368 | static inline int bpf_try_make_writable(struct sk_buff *skb, |
| 1369 | unsigned int write_len) |
| 1370 | { |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 1371 | int err = __bpf_try_make_writable(skb, write_len); |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1372 | |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1373 | bpf_compute_data_end(skb); |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1374 | return err; |
| 1375 | } |
| 1376 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1377 | static int bpf_try_make_head_writable(struct sk_buff *skb) |
| 1378 | { |
| 1379 | return bpf_try_make_writable(skb, skb_headlen(skb)); |
| 1380 | } |
| 1381 | |
Daniel Borkmann | a2bfe6b | 2016-08-05 00:11:11 +0200 | [diff] [blame] | 1382 | static inline void bpf_push_mac_rcsum(struct sk_buff *skb) |
| 1383 | { |
| 1384 | if (skb_at_tc_ingress(skb)) |
| 1385 | skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len); |
| 1386 | } |
| 1387 | |
Daniel Borkmann | 8065694 | 2016-08-05 00:11:13 +0200 | [diff] [blame] | 1388 | static inline void bpf_pull_mac_rcsum(struct sk_buff *skb) |
| 1389 | { |
| 1390 | if (skb_at_tc_ingress(skb)) |
| 1391 | skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len); |
| 1392 | } |
| 1393 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1394 | BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset, |
| 1395 | const void *, from, u32, len, u64, flags) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1396 | { |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1397 | void *ptr; |
| 1398 | |
Daniel Borkmann | 8afd54c | 2016-03-04 15:15:03 +0100 | [diff] [blame] | 1399 | if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1400 | return -EINVAL; |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1401 | if (unlikely(offset > 0xffff)) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1402 | return -EFAULT; |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1403 | if (unlikely(bpf_try_make_writable(skb, offset + len))) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1404 | return -EFAULT; |
| 1405 | |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1406 | ptr = skb->data + offset; |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1407 | if (flags & BPF_F_RECOMPUTE_CSUM) |
Daniel Borkmann | 479ffcc | 2016-08-05 00:11:12 +0200 | [diff] [blame] | 1408 | __skb_postpull_rcsum(skb, ptr, len, offset); |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1409 | |
| 1410 | memcpy(ptr, from, len); |
| 1411 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1412 | if (flags & BPF_F_RECOMPUTE_CSUM) |
Daniel Borkmann | 479ffcc | 2016-08-05 00:11:12 +0200 | [diff] [blame] | 1413 | __skb_postpush_rcsum(skb, ptr, len, offset); |
Daniel Borkmann | 8afd54c | 2016-03-04 15:15:03 +0100 | [diff] [blame] | 1414 | if (flags & BPF_F_INVALIDATE_HASH) |
| 1415 | skb_clear_hash(skb); |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1416 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1417 | return 0; |
| 1418 | } |
| 1419 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1420 | static const struct bpf_func_proto bpf_skb_store_bytes_proto = { |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1421 | .func = bpf_skb_store_bytes, |
| 1422 | .gpl_only = false, |
| 1423 | .ret_type = RET_INTEGER, |
| 1424 | .arg1_type = ARG_PTR_TO_CTX, |
| 1425 | .arg2_type = ARG_ANYTHING, |
| 1426 | .arg3_type = ARG_PTR_TO_STACK, |
| 1427 | .arg4_type = ARG_CONST_STACK_SIZE, |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1428 | .arg5_type = ARG_ANYTHING, |
| 1429 | }; |
| 1430 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1431 | BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset, |
| 1432 | void *, to, u32, len) |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1433 | { |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1434 | void *ptr; |
| 1435 | |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1436 | if (unlikely(offset > 0xffff)) |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 1437 | goto err_clear; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1438 | |
| 1439 | ptr = skb_header_pointer(skb, offset, len, to); |
| 1440 | if (unlikely(!ptr)) |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 1441 | goto err_clear; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1442 | if (ptr != to) |
| 1443 | memcpy(to, ptr, len); |
| 1444 | |
| 1445 | return 0; |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 1446 | err_clear: |
| 1447 | memset(to, 0, len); |
| 1448 | return -EFAULT; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1451 | static const struct bpf_func_proto bpf_skb_load_bytes_proto = { |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1452 | .func = bpf_skb_load_bytes, |
| 1453 | .gpl_only = false, |
| 1454 | .ret_type = RET_INTEGER, |
| 1455 | .arg1_type = ARG_PTR_TO_CTX, |
| 1456 | .arg2_type = ARG_ANYTHING, |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 1457 | .arg3_type = ARG_PTR_TO_RAW_STACK, |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1458 | .arg4_type = ARG_CONST_STACK_SIZE, |
| 1459 | }; |
| 1460 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1461 | BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len) |
| 1462 | { |
| 1463 | /* Idea is the following: should the needed direct read/write |
| 1464 | * test fail during runtime, we can pull in more data and redo |
| 1465 | * again, since implicitly, we invalidate previous checks here. |
| 1466 | * |
| 1467 | * Or, since we know how much we need to make read/writeable, |
| 1468 | * this can be done once at the program beginning for direct |
| 1469 | * access case. By this we overcome limitations of only current |
| 1470 | * headroom being accessible. |
| 1471 | */ |
| 1472 | return bpf_try_make_writable(skb, len ? : skb_headlen(skb)); |
| 1473 | } |
| 1474 | |
| 1475 | static const struct bpf_func_proto bpf_skb_pull_data_proto = { |
| 1476 | .func = bpf_skb_pull_data, |
| 1477 | .gpl_only = false, |
| 1478 | .ret_type = RET_INTEGER, |
| 1479 | .arg1_type = ARG_PTR_TO_CTX, |
| 1480 | .arg2_type = ARG_ANYTHING, |
| 1481 | }; |
| 1482 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1483 | BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset, |
| 1484 | u64, from, u64, to, u64, flags) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1485 | { |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1486 | __sum16 *ptr; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1487 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1488 | if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) |
| 1489 | return -EINVAL; |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1490 | if (unlikely(offset > 0xffff || offset & 1)) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1491 | return -EFAULT; |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1492 | if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1493 | return -EFAULT; |
| 1494 | |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1495 | ptr = (__sum16 *)(skb->data + offset); |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1496 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
Daniel Borkmann | 8050c0f | 2016-03-04 15:15:02 +0100 | [diff] [blame] | 1497 | case 0: |
| 1498 | if (unlikely(from != 0)) |
| 1499 | return -EINVAL; |
| 1500 | |
| 1501 | csum_replace_by_diff(ptr, to); |
| 1502 | break; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1503 | case 2: |
| 1504 | csum_replace2(ptr, from, to); |
| 1505 | break; |
| 1506 | case 4: |
| 1507 | csum_replace4(ptr, from, to); |
| 1508 | break; |
| 1509 | default: |
| 1510 | return -EINVAL; |
| 1511 | } |
| 1512 | |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1513 | return 0; |
| 1514 | } |
| 1515 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1516 | static const struct bpf_func_proto bpf_l3_csum_replace_proto = { |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1517 | .func = bpf_l3_csum_replace, |
| 1518 | .gpl_only = false, |
| 1519 | .ret_type = RET_INTEGER, |
| 1520 | .arg1_type = ARG_PTR_TO_CTX, |
| 1521 | .arg2_type = ARG_ANYTHING, |
| 1522 | .arg3_type = ARG_ANYTHING, |
| 1523 | .arg4_type = ARG_ANYTHING, |
| 1524 | .arg5_type = ARG_ANYTHING, |
| 1525 | }; |
| 1526 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1527 | BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset, |
| 1528 | u64, from, u64, to, u64, flags) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1529 | { |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1530 | bool is_pseudo = flags & BPF_F_PSEUDO_HDR; |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1531 | bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1532 | __sum16 *ptr; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1533 | |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1534 | if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR | |
| 1535 | BPF_F_HDR_FIELD_MASK))) |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1536 | return -EINVAL; |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1537 | if (unlikely(offset > 0xffff || offset & 1)) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1538 | return -EFAULT; |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1539 | if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1540 | return -EFAULT; |
| 1541 | |
Daniel Borkmann | 0ed661d | 2016-08-11 21:38:37 +0200 | [diff] [blame] | 1542 | ptr = (__sum16 *)(skb->data + offset); |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1543 | if (is_mmzero && !*ptr) |
| 1544 | return 0; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1545 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1546 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1547 | case 0: |
| 1548 | if (unlikely(from != 0)) |
| 1549 | return -EINVAL; |
| 1550 | |
| 1551 | inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); |
| 1552 | break; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1553 | case 2: |
| 1554 | inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); |
| 1555 | break; |
| 1556 | case 4: |
| 1557 | inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); |
| 1558 | break; |
| 1559 | default: |
| 1560 | return -EINVAL; |
| 1561 | } |
| 1562 | |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1563 | if (is_mmzero && !*ptr) |
| 1564 | *ptr = CSUM_MANGLED_0; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1565 | return 0; |
| 1566 | } |
| 1567 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1568 | static const struct bpf_func_proto bpf_l4_csum_replace_proto = { |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1569 | .func = bpf_l4_csum_replace, |
| 1570 | .gpl_only = false, |
| 1571 | .ret_type = RET_INTEGER, |
| 1572 | .arg1_type = ARG_PTR_TO_CTX, |
| 1573 | .arg2_type = ARG_ANYTHING, |
| 1574 | .arg3_type = ARG_ANYTHING, |
| 1575 | .arg4_type = ARG_ANYTHING, |
| 1576 | .arg5_type = ARG_ANYTHING, |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1577 | }; |
| 1578 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1579 | BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size, |
| 1580 | __be32 *, to, u32, to_size, __wsum, seed) |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1581 | { |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1582 | struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1583 | u32 diff_size = from_size + to_size; |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1584 | int i, j = 0; |
| 1585 | |
| 1586 | /* This is quite flexible, some examples: |
| 1587 | * |
| 1588 | * from_size == 0, to_size > 0, seed := csum --> pushing data |
| 1589 | * from_size > 0, to_size == 0, seed := csum --> pulling data |
| 1590 | * from_size > 0, to_size > 0, seed := 0 --> diffing data |
| 1591 | * |
| 1592 | * Even for diffing, from_size and to_size don't need to be equal. |
| 1593 | */ |
| 1594 | if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || |
| 1595 | diff_size > sizeof(sp->diff))) |
| 1596 | return -EINVAL; |
| 1597 | |
| 1598 | for (i = 0; i < from_size / sizeof(__be32); i++, j++) |
| 1599 | sp->diff[j] = ~from[i]; |
| 1600 | for (i = 0; i < to_size / sizeof(__be32); i++, j++) |
| 1601 | sp->diff[j] = to[i]; |
| 1602 | |
| 1603 | return csum_partial(sp->diff, diff_size, seed); |
| 1604 | } |
| 1605 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1606 | static const struct bpf_func_proto bpf_csum_diff_proto = { |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1607 | .func = bpf_csum_diff, |
| 1608 | .gpl_only = false, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1609 | .pkt_access = true, |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1610 | .ret_type = RET_INTEGER, |
| 1611 | .arg1_type = ARG_PTR_TO_STACK, |
| 1612 | .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO, |
| 1613 | .arg3_type = ARG_PTR_TO_STACK, |
| 1614 | .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO, |
| 1615 | .arg5_type = ARG_ANYTHING, |
| 1616 | }; |
| 1617 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1618 | BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum) |
| 1619 | { |
| 1620 | /* The interface is to be used in combination with bpf_csum_diff() |
| 1621 | * for direct packet writes. csum rotation for alignment as well |
| 1622 | * as emulating csum_sub() can be done from the eBPF program. |
| 1623 | */ |
| 1624 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 1625 | return (skb->csum = csum_add(skb->csum, csum)); |
| 1626 | |
| 1627 | return -ENOTSUPP; |
| 1628 | } |
| 1629 | |
| 1630 | static const struct bpf_func_proto bpf_csum_update_proto = { |
| 1631 | .func = bpf_csum_update, |
| 1632 | .gpl_only = false, |
| 1633 | .ret_type = RET_INTEGER, |
| 1634 | .arg1_type = ARG_PTR_TO_CTX, |
| 1635 | .arg2_type = ARG_ANYTHING, |
| 1636 | }; |
| 1637 | |
Daniel Borkmann | a70b506 | 2016-06-10 21:19:06 +0200 | [diff] [blame] | 1638 | static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb) |
| 1639 | { |
Daniel Borkmann | a70b506 | 2016-06-10 21:19:06 +0200 | [diff] [blame] | 1640 | return dev_forward_skb(dev, skb); |
| 1641 | } |
| 1642 | |
Martin KaFai Lau | 4e3264d | 2016-11-09 15:36:33 -0800 | [diff] [blame] | 1643 | static inline int __bpf_rx_skb_no_mac(struct net_device *dev, |
| 1644 | struct sk_buff *skb) |
| 1645 | { |
| 1646 | int ret = ____dev_forward_skb(dev, skb); |
| 1647 | |
| 1648 | if (likely(!ret)) { |
| 1649 | skb->dev = dev; |
| 1650 | ret = netif_rx(skb); |
| 1651 | } |
| 1652 | |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
Daniel Borkmann | a70b506 | 2016-06-10 21:19:06 +0200 | [diff] [blame] | 1656 | static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) |
| 1657 | { |
| 1658 | int ret; |
| 1659 | |
| 1660 | if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) { |
| 1661 | net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); |
| 1662 | kfree_skb(skb); |
| 1663 | return -ENETDOWN; |
| 1664 | } |
| 1665 | |
| 1666 | skb->dev = dev; |
| 1667 | |
| 1668 | __this_cpu_inc(xmit_recursion); |
| 1669 | ret = dev_queue_xmit(skb); |
| 1670 | __this_cpu_dec(xmit_recursion); |
| 1671 | |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
Martin KaFai Lau | 4e3264d | 2016-11-09 15:36:33 -0800 | [diff] [blame] | 1675 | static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev, |
| 1676 | u32 flags) |
| 1677 | { |
| 1678 | /* skb->mac_len is not set on normal egress */ |
| 1679 | unsigned int mlen = skb->network_header - skb->mac_header; |
| 1680 | |
| 1681 | __skb_pull(skb, mlen); |
| 1682 | |
| 1683 | /* At ingress, the mac header has already been pulled once. |
| 1684 | * At egress, skb_pospull_rcsum has to be done in case that |
| 1685 | * the skb is originated from ingress (i.e. a forwarded skb) |
| 1686 | * to ensure that rcsum starts at net header. |
| 1687 | */ |
| 1688 | if (!skb_at_tc_ingress(skb)) |
| 1689 | skb_postpull_rcsum(skb, skb_mac_header(skb), mlen); |
| 1690 | skb_pop_mac_header(skb); |
| 1691 | skb_reset_mac_len(skb); |
| 1692 | return flags & BPF_F_INGRESS ? |
| 1693 | __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb); |
| 1694 | } |
| 1695 | |
| 1696 | static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, |
| 1697 | u32 flags) |
| 1698 | { |
| 1699 | bpf_push_mac_rcsum(skb); |
| 1700 | return flags & BPF_F_INGRESS ? |
| 1701 | __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); |
| 1702 | } |
| 1703 | |
| 1704 | static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev, |
| 1705 | u32 flags) |
| 1706 | { |
| 1707 | switch (dev->type) { |
| 1708 | case ARPHRD_TUNNEL: |
| 1709 | case ARPHRD_TUNNEL6: |
| 1710 | case ARPHRD_SIT: |
| 1711 | case ARPHRD_IPGRE: |
| 1712 | case ARPHRD_VOID: |
| 1713 | case ARPHRD_NONE: |
| 1714 | return __bpf_redirect_no_mac(skb, dev, flags); |
| 1715 | default: |
| 1716 | return __bpf_redirect_common(skb, dev, flags); |
| 1717 | } |
| 1718 | } |
| 1719 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1720 | BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags) |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1721 | { |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1722 | struct net_device *dev; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1723 | struct sk_buff *clone; |
| 1724 | int ret; |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1725 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1726 | if (unlikely(flags & ~(BPF_F_INGRESS))) |
| 1727 | return -EINVAL; |
| 1728 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1729 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); |
| 1730 | if (unlikely(!dev)) |
| 1731 | return -EINVAL; |
| 1732 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1733 | clone = skb_clone(skb, GFP_ATOMIC); |
| 1734 | if (unlikely(!clone)) |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1735 | return -ENOMEM; |
| 1736 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1737 | /* For direct write, we need to keep the invariant that the skbs |
| 1738 | * we're dealing with need to be uncloned. Should uncloning fail |
| 1739 | * here, we need to free the just generated clone to unclone once |
| 1740 | * again. |
| 1741 | */ |
| 1742 | ret = bpf_try_make_head_writable(skb); |
| 1743 | if (unlikely(ret)) { |
| 1744 | kfree_skb(clone); |
| 1745 | return -ENOMEM; |
| 1746 | } |
| 1747 | |
Martin KaFai Lau | 4e3264d | 2016-11-09 15:36:33 -0800 | [diff] [blame] | 1748 | return __bpf_redirect(clone, dev, flags); |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1751 | static const struct bpf_func_proto bpf_clone_redirect_proto = { |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1752 | .func = bpf_clone_redirect, |
| 1753 | .gpl_only = false, |
| 1754 | .ret_type = RET_INTEGER, |
| 1755 | .arg1_type = ARG_PTR_TO_CTX, |
| 1756 | .arg2_type = ARG_ANYTHING, |
| 1757 | .arg3_type = ARG_ANYTHING, |
| 1758 | }; |
| 1759 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1760 | struct redirect_info { |
| 1761 | u32 ifindex; |
| 1762 | u32 flags; |
| 1763 | }; |
| 1764 | |
| 1765 | static DEFINE_PER_CPU(struct redirect_info, redirect_info); |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1766 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1767 | BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1768 | { |
| 1769 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); |
| 1770 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1771 | if (unlikely(flags & ~(BPF_F_INGRESS))) |
| 1772 | return TC_ACT_SHOT; |
| 1773 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1774 | ri->ifindex = ifindex; |
| 1775 | ri->flags = flags; |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1776 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1777 | return TC_ACT_REDIRECT; |
| 1778 | } |
| 1779 | |
| 1780 | int skb_do_redirect(struct sk_buff *skb) |
| 1781 | { |
| 1782 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); |
| 1783 | struct net_device *dev; |
| 1784 | |
| 1785 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex); |
| 1786 | ri->ifindex = 0; |
| 1787 | if (unlikely(!dev)) { |
| 1788 | kfree_skb(skb); |
| 1789 | return -EINVAL; |
| 1790 | } |
| 1791 | |
Martin KaFai Lau | 4e3264d | 2016-11-09 15:36:33 -0800 | [diff] [blame] | 1792 | return __bpf_redirect(skb, dev, ri->flags); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 1795 | static const struct bpf_func_proto bpf_redirect_proto = { |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1796 | .func = bpf_redirect, |
| 1797 | .gpl_only = false, |
| 1798 | .ret_type = RET_INTEGER, |
| 1799 | .arg1_type = ARG_ANYTHING, |
| 1800 | .arg2_type = ARG_ANYTHING, |
| 1801 | }; |
| 1802 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1803 | BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb) |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 1804 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1805 | return task_get_classid(skb); |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { |
| 1809 | .func = bpf_get_cgroup_classid, |
| 1810 | .gpl_only = false, |
| 1811 | .ret_type = RET_INTEGER, |
| 1812 | .arg1_type = ARG_PTR_TO_CTX, |
| 1813 | }; |
| 1814 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1815 | BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb) |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 1816 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1817 | return dst_tclassid(skb); |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | static const struct bpf_func_proto bpf_get_route_realm_proto = { |
| 1821 | .func = bpf_get_route_realm, |
| 1822 | .gpl_only = false, |
| 1823 | .ret_type = RET_INTEGER, |
| 1824 | .arg1_type = ARG_PTR_TO_CTX, |
| 1825 | }; |
| 1826 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1827 | BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb) |
Daniel Borkmann | 13c5c24 | 2016-07-03 01:28:47 +0200 | [diff] [blame] | 1828 | { |
| 1829 | /* If skb_clear_hash() was called due to mangling, we can |
| 1830 | * trigger SW recalculation here. Later access to hash |
| 1831 | * can then use the inline skb->hash via context directly |
| 1832 | * instead of calling this helper again. |
| 1833 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1834 | return skb_get_hash(skb); |
Daniel Borkmann | 13c5c24 | 2016-07-03 01:28:47 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | static const struct bpf_func_proto bpf_get_hash_recalc_proto = { |
| 1838 | .func = bpf_get_hash_recalc, |
| 1839 | .gpl_only = false, |
| 1840 | .ret_type = RET_INTEGER, |
| 1841 | .arg1_type = ARG_PTR_TO_CTX, |
| 1842 | }; |
| 1843 | |
Daniel Borkmann | 7a4b28c | 2016-09-23 01:28:37 +0200 | [diff] [blame] | 1844 | BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb) |
| 1845 | { |
| 1846 | /* After all direct packet write, this can be used once for |
| 1847 | * triggering a lazy recalc on next skb_get_hash() invocation. |
| 1848 | */ |
| 1849 | skb_clear_hash(skb); |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | static const struct bpf_func_proto bpf_set_hash_invalid_proto = { |
| 1854 | .func = bpf_set_hash_invalid, |
| 1855 | .gpl_only = false, |
| 1856 | .ret_type = RET_INTEGER, |
| 1857 | .arg1_type = ARG_PTR_TO_CTX, |
| 1858 | }; |
| 1859 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1860 | BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto, |
| 1861 | u16, vlan_tci) |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1862 | { |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1863 | int ret; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1864 | |
| 1865 | if (unlikely(vlan_proto != htons(ETH_P_8021Q) && |
| 1866 | vlan_proto != htons(ETH_P_8021AD))) |
| 1867 | vlan_proto = htons(ETH_P_8021Q); |
| 1868 | |
Daniel Borkmann | 8065694 | 2016-08-05 00:11:13 +0200 | [diff] [blame] | 1869 | bpf_push_mac_rcsum(skb); |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1870 | ret = skb_vlan_push(skb, vlan_proto, vlan_tci); |
Daniel Borkmann | 8065694 | 2016-08-05 00:11:13 +0200 | [diff] [blame] | 1871 | bpf_pull_mac_rcsum(skb); |
| 1872 | |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1873 | bpf_compute_data_end(skb); |
| 1874 | return ret; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | const struct bpf_func_proto bpf_skb_vlan_push_proto = { |
| 1878 | .func = bpf_skb_vlan_push, |
| 1879 | .gpl_only = false, |
| 1880 | .ret_type = RET_INTEGER, |
| 1881 | .arg1_type = ARG_PTR_TO_CTX, |
| 1882 | .arg2_type = ARG_ANYTHING, |
| 1883 | .arg3_type = ARG_ANYTHING, |
| 1884 | }; |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 1885 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto); |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1886 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 1887 | BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb) |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1888 | { |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1889 | int ret; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1890 | |
Daniel Borkmann | 8065694 | 2016-08-05 00:11:13 +0200 | [diff] [blame] | 1891 | bpf_push_mac_rcsum(skb); |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1892 | ret = skb_vlan_pop(skb); |
Daniel Borkmann | 8065694 | 2016-08-05 00:11:13 +0200 | [diff] [blame] | 1893 | bpf_pull_mac_rcsum(skb); |
| 1894 | |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 1895 | bpf_compute_data_end(skb); |
| 1896 | return ret; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | const struct bpf_func_proto bpf_skb_vlan_pop_proto = { |
| 1900 | .func = bpf_skb_vlan_pop, |
| 1901 | .gpl_only = false, |
| 1902 | .ret_type = RET_INTEGER, |
| 1903 | .arg1_type = ARG_PTR_TO_CTX, |
| 1904 | }; |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 1905 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto); |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1906 | |
Daniel Borkmann | 6578171 | 2016-06-28 12:18:27 +0200 | [diff] [blame] | 1907 | static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len) |
| 1908 | { |
| 1909 | /* Caller already did skb_cow() with len as headroom, |
| 1910 | * so no need to do it here. |
| 1911 | */ |
| 1912 | skb_push(skb, len); |
| 1913 | memmove(skb->data, skb->data + len, off); |
| 1914 | memset(skb->data + off, 0, len); |
| 1915 | |
| 1916 | /* No skb_postpush_rcsum(skb, skb->data + off, len) |
| 1917 | * needed here as it does not change the skb->csum |
| 1918 | * result for checksum complete when summing over |
| 1919 | * zeroed blocks. |
| 1920 | */ |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len) |
| 1925 | { |
| 1926 | /* skb_ensure_writable() is not needed here, as we're |
| 1927 | * already working on an uncloned skb. |
| 1928 | */ |
| 1929 | if (unlikely(!pskb_may_pull(skb, off + len))) |
| 1930 | return -ENOMEM; |
| 1931 | |
| 1932 | skb_postpull_rcsum(skb, skb->data + off, len); |
| 1933 | memmove(skb->data + len, skb->data, off); |
| 1934 | __skb_pull(skb, len); |
| 1935 | |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len) |
| 1940 | { |
| 1941 | bool trans_same = skb->transport_header == skb->network_header; |
| 1942 | int ret; |
| 1943 | |
| 1944 | /* There's no need for __skb_push()/__skb_pull() pair to |
| 1945 | * get to the start of the mac header as we're guaranteed |
| 1946 | * to always start from here under eBPF. |
| 1947 | */ |
| 1948 | ret = bpf_skb_generic_push(skb, off, len); |
| 1949 | if (likely(!ret)) { |
| 1950 | skb->mac_header -= len; |
| 1951 | skb->network_header -= len; |
| 1952 | if (trans_same) |
| 1953 | skb->transport_header = skb->network_header; |
| 1954 | } |
| 1955 | |
| 1956 | return ret; |
| 1957 | } |
| 1958 | |
| 1959 | static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len) |
| 1960 | { |
| 1961 | bool trans_same = skb->transport_header == skb->network_header; |
| 1962 | int ret; |
| 1963 | |
| 1964 | /* Same here, __skb_push()/__skb_pull() pair not needed. */ |
| 1965 | ret = bpf_skb_generic_pop(skb, off, len); |
| 1966 | if (likely(!ret)) { |
| 1967 | skb->mac_header += len; |
| 1968 | skb->network_header += len; |
| 1969 | if (trans_same) |
| 1970 | skb->transport_header = skb->network_header; |
| 1971 | } |
| 1972 | |
| 1973 | return ret; |
| 1974 | } |
| 1975 | |
| 1976 | static int bpf_skb_proto_4_to_6(struct sk_buff *skb) |
| 1977 | { |
| 1978 | const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); |
| 1979 | u32 off = skb->network_header - skb->mac_header; |
| 1980 | int ret; |
| 1981 | |
| 1982 | ret = skb_cow(skb, len_diff); |
| 1983 | if (unlikely(ret < 0)) |
| 1984 | return ret; |
| 1985 | |
| 1986 | ret = bpf_skb_net_hdr_push(skb, off, len_diff); |
| 1987 | if (unlikely(ret < 0)) |
| 1988 | return ret; |
| 1989 | |
| 1990 | if (skb_is_gso(skb)) { |
| 1991 | /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to |
| 1992 | * be changed into SKB_GSO_TCPV6. |
| 1993 | */ |
| 1994 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { |
| 1995 | skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4; |
| 1996 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6; |
| 1997 | } |
| 1998 | |
| 1999 | /* Due to IPv6 header, MSS needs to be downgraded. */ |
| 2000 | skb_shinfo(skb)->gso_size -= len_diff; |
| 2001 | /* Header must be checked, and gso_segs recomputed. */ |
| 2002 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 2003 | skb_shinfo(skb)->gso_segs = 0; |
| 2004 | } |
| 2005 | |
| 2006 | skb->protocol = htons(ETH_P_IPV6); |
| 2007 | skb_clear_hash(skb); |
| 2008 | |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | static int bpf_skb_proto_6_to_4(struct sk_buff *skb) |
| 2013 | { |
| 2014 | const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); |
| 2015 | u32 off = skb->network_header - skb->mac_header; |
| 2016 | int ret; |
| 2017 | |
| 2018 | ret = skb_unclone(skb, GFP_ATOMIC); |
| 2019 | if (unlikely(ret < 0)) |
| 2020 | return ret; |
| 2021 | |
| 2022 | ret = bpf_skb_net_hdr_pop(skb, off, len_diff); |
| 2023 | if (unlikely(ret < 0)) |
| 2024 | return ret; |
| 2025 | |
| 2026 | if (skb_is_gso(skb)) { |
| 2027 | /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to |
| 2028 | * be changed into SKB_GSO_TCPV4. |
| 2029 | */ |
| 2030 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) { |
| 2031 | skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6; |
| 2032 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4; |
| 2033 | } |
| 2034 | |
| 2035 | /* Due to IPv4 header, MSS can be upgraded. */ |
| 2036 | skb_shinfo(skb)->gso_size += len_diff; |
| 2037 | /* Header must be checked, and gso_segs recomputed. */ |
| 2038 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 2039 | skb_shinfo(skb)->gso_segs = 0; |
| 2040 | } |
| 2041 | |
| 2042 | skb->protocol = htons(ETH_P_IP); |
| 2043 | skb_clear_hash(skb); |
| 2044 | |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
| 2048 | static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) |
| 2049 | { |
| 2050 | __be16 from_proto = skb->protocol; |
| 2051 | |
| 2052 | if (from_proto == htons(ETH_P_IP) && |
| 2053 | to_proto == htons(ETH_P_IPV6)) |
| 2054 | return bpf_skb_proto_4_to_6(skb); |
| 2055 | |
| 2056 | if (from_proto == htons(ETH_P_IPV6) && |
| 2057 | to_proto == htons(ETH_P_IP)) |
| 2058 | return bpf_skb_proto_6_to_4(skb); |
| 2059 | |
| 2060 | return -ENOTSUPP; |
| 2061 | } |
| 2062 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2063 | BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto, |
| 2064 | u64, flags) |
Daniel Borkmann | 6578171 | 2016-06-28 12:18:27 +0200 | [diff] [blame] | 2065 | { |
Daniel Borkmann | 6578171 | 2016-06-28 12:18:27 +0200 | [diff] [blame] | 2066 | int ret; |
| 2067 | |
| 2068 | if (unlikely(flags)) |
| 2069 | return -EINVAL; |
| 2070 | |
| 2071 | /* General idea is that this helper does the basic groundwork |
| 2072 | * needed for changing the protocol, and eBPF program fills the |
| 2073 | * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace() |
| 2074 | * and other helpers, rather than passing a raw buffer here. |
| 2075 | * |
| 2076 | * The rationale is to keep this minimal and without a need to |
| 2077 | * deal with raw packet data. F.e. even if we would pass buffers |
| 2078 | * here, the program still needs to call the bpf_lX_csum_replace() |
| 2079 | * helpers anyway. Plus, this way we keep also separation of |
| 2080 | * concerns, since f.e. bpf_skb_store_bytes() should only take |
| 2081 | * care of stores. |
| 2082 | * |
| 2083 | * Currently, additional options and extension header space are |
| 2084 | * not supported, but flags register is reserved so we can adapt |
| 2085 | * that. For offloads, we mark packet as dodgy, so that headers |
| 2086 | * need to be verified first. |
| 2087 | */ |
| 2088 | ret = bpf_skb_proto_xlat(skb, proto); |
| 2089 | bpf_compute_data_end(skb); |
| 2090 | return ret; |
| 2091 | } |
| 2092 | |
| 2093 | static const struct bpf_func_proto bpf_skb_change_proto_proto = { |
| 2094 | .func = bpf_skb_change_proto, |
| 2095 | .gpl_only = false, |
| 2096 | .ret_type = RET_INTEGER, |
| 2097 | .arg1_type = ARG_PTR_TO_CTX, |
| 2098 | .arg2_type = ARG_ANYTHING, |
| 2099 | .arg3_type = ARG_ANYTHING, |
| 2100 | }; |
| 2101 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2102 | BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type) |
Daniel Borkmann | d2485c4 | 2016-06-28 12:18:28 +0200 | [diff] [blame] | 2103 | { |
Daniel Borkmann | d2485c4 | 2016-06-28 12:18:28 +0200 | [diff] [blame] | 2104 | /* We only allow a restricted subset to be changed for now. */ |
Daniel Borkmann | 45c7fff | 2016-08-18 01:00:38 +0200 | [diff] [blame] | 2105 | if (unlikely(!skb_pkt_type_ok(skb->pkt_type) || |
| 2106 | !skb_pkt_type_ok(pkt_type))) |
Daniel Borkmann | d2485c4 | 2016-06-28 12:18:28 +0200 | [diff] [blame] | 2107 | return -EINVAL; |
| 2108 | |
| 2109 | skb->pkt_type = pkt_type; |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
| 2113 | static const struct bpf_func_proto bpf_skb_change_type_proto = { |
| 2114 | .func = bpf_skb_change_type, |
| 2115 | .gpl_only = false, |
| 2116 | .ret_type = RET_INTEGER, |
| 2117 | .arg1_type = ARG_PTR_TO_CTX, |
| 2118 | .arg2_type = ARG_ANYTHING, |
| 2119 | }; |
| 2120 | |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 2121 | static u32 __bpf_skb_min_len(const struct sk_buff *skb) |
| 2122 | { |
| 2123 | u32 min_len = skb_network_offset(skb); |
| 2124 | |
| 2125 | if (skb_transport_header_was_set(skb)) |
| 2126 | min_len = skb_transport_offset(skb); |
| 2127 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 2128 | min_len = skb_checksum_start_offset(skb) + |
| 2129 | skb->csum_offset + sizeof(__sum16); |
| 2130 | return min_len; |
| 2131 | } |
| 2132 | |
| 2133 | static u32 __bpf_skb_max_len(const struct sk_buff *skb) |
| 2134 | { |
Daniel Borkmann | 6088b58 | 2016-09-09 02:45:28 +0200 | [diff] [blame] | 2135 | return skb->dev->mtu + skb->dev->hard_header_len; |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len) |
| 2139 | { |
| 2140 | unsigned int old_len = skb->len; |
| 2141 | int ret; |
| 2142 | |
| 2143 | ret = __skb_grow_rcsum(skb, new_len); |
| 2144 | if (!ret) |
| 2145 | memset(skb->data + old_len, 0, new_len - old_len); |
| 2146 | return ret; |
| 2147 | } |
| 2148 | |
| 2149 | static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len) |
| 2150 | { |
| 2151 | return __skb_trim_rcsum(skb, new_len); |
| 2152 | } |
| 2153 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2154 | BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len, |
| 2155 | u64, flags) |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 2156 | { |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 2157 | u32 max_len = __bpf_skb_max_len(skb); |
| 2158 | u32 min_len = __bpf_skb_min_len(skb); |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 2159 | int ret; |
| 2160 | |
| 2161 | if (unlikely(flags || new_len > max_len || new_len < min_len)) |
| 2162 | return -EINVAL; |
| 2163 | if (skb->encapsulation) |
| 2164 | return -ENOTSUPP; |
| 2165 | |
| 2166 | /* The basic idea of this helper is that it's performing the |
| 2167 | * needed work to either grow or trim an skb, and eBPF program |
| 2168 | * rewrites the rest via helpers like bpf_skb_store_bytes(), |
| 2169 | * bpf_lX_csum_replace() and others rather than passing a raw |
| 2170 | * buffer here. This one is a slow path helper and intended |
| 2171 | * for replies with control messages. |
| 2172 | * |
| 2173 | * Like in bpf_skb_change_proto(), we want to keep this rather |
| 2174 | * minimal and without protocol specifics so that we are able |
| 2175 | * to separate concerns as in bpf_skb_store_bytes() should only |
| 2176 | * be the one responsible for writing buffers. |
| 2177 | * |
| 2178 | * It's really expected to be a slow path operation here for |
| 2179 | * control message replies, so we're implicitly linearizing, |
| 2180 | * uncloning and drop offloads from the skb by this. |
| 2181 | */ |
| 2182 | ret = __bpf_try_make_writable(skb, skb->len); |
| 2183 | if (!ret) { |
| 2184 | if (new_len > skb->len) |
| 2185 | ret = bpf_skb_grow_rcsum(skb, new_len); |
| 2186 | else if (new_len < skb->len) |
| 2187 | ret = bpf_skb_trim_rcsum(skb, new_len); |
| 2188 | if (!ret && skb_is_gso(skb)) |
| 2189 | skb_gso_reset(skb); |
| 2190 | } |
| 2191 | |
| 2192 | bpf_compute_data_end(skb); |
| 2193 | return ret; |
| 2194 | } |
| 2195 | |
| 2196 | static const struct bpf_func_proto bpf_skb_change_tail_proto = { |
| 2197 | .func = bpf_skb_change_tail, |
| 2198 | .gpl_only = false, |
| 2199 | .ret_type = RET_INTEGER, |
| 2200 | .arg1_type = ARG_PTR_TO_CTX, |
| 2201 | .arg2_type = ARG_ANYTHING, |
| 2202 | .arg3_type = ARG_ANYTHING, |
| 2203 | }; |
| 2204 | |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 2205 | bool bpf_helper_changes_skb_data(void *func) |
| 2206 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2207 | if (func == bpf_skb_vlan_push || |
| 2208 | func == bpf_skb_vlan_pop || |
| 2209 | func == bpf_skb_store_bytes || |
| 2210 | func == bpf_skb_change_proto || |
| 2211 | func == bpf_skb_change_tail || |
| 2212 | func == bpf_skb_pull_data || |
Daniel Borkmann | c1133c6 | 2017-05-25 01:05:07 +0200 | [diff] [blame] | 2213 | func == bpf_clone_redirect || |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2214 | func == bpf_l3_csum_replace || |
| 2215 | func == bpf_l4_csum_replace) |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 2216 | return true; |
| 2217 | |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 2218 | return false; |
| 2219 | } |
| 2220 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2221 | static unsigned long bpf_skb_copy(void *dst_buff, const void *skb, |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 2222 | unsigned long off, unsigned long len) |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2223 | { |
Daniel Borkmann | aa7145c | 2016-07-22 01:19:42 +0200 | [diff] [blame] | 2224 | void *ptr = skb_header_pointer(skb, off, len, dst_buff); |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2225 | |
| 2226 | if (unlikely(!ptr)) |
| 2227 | return len; |
| 2228 | if (ptr != dst_buff) |
| 2229 | memcpy(dst_buff, ptr, len); |
| 2230 | |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2234 | BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map, |
| 2235 | u64, flags, void *, meta, u64, meta_size) |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2236 | { |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2237 | u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32; |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2238 | |
| 2239 | if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) |
| 2240 | return -EINVAL; |
| 2241 | if (unlikely(skb_size > skb->len)) |
| 2242 | return -EFAULT; |
| 2243 | |
| 2244 | return bpf_event_output(map, flags, meta, meta_size, skb, skb_size, |
| 2245 | bpf_skb_copy); |
| 2246 | } |
| 2247 | |
| 2248 | static const struct bpf_func_proto bpf_skb_event_output_proto = { |
| 2249 | .func = bpf_skb_event_output, |
| 2250 | .gpl_only = true, |
| 2251 | .ret_type = RET_INTEGER, |
| 2252 | .arg1_type = ARG_PTR_TO_CTX, |
| 2253 | .arg2_type = ARG_CONST_MAP_PTR, |
| 2254 | .arg3_type = ARG_ANYTHING, |
| 2255 | .arg4_type = ARG_PTR_TO_STACK, |
| 2256 | .arg5_type = ARG_CONST_STACK_SIZE, |
| 2257 | }; |
| 2258 | |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2259 | static unsigned short bpf_tunnel_key_af(u64 flags) |
| 2260 | { |
| 2261 | return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; |
| 2262 | } |
| 2263 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2264 | BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to, |
| 2265 | u32, size, u64, flags) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2266 | { |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2267 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 2268 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2269 | void *to_orig = to; |
| 2270 | int err; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2271 | |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2272 | if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) { |
| 2273 | err = -EINVAL; |
| 2274 | goto err_clear; |
| 2275 | } |
| 2276 | if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) { |
| 2277 | err = -EPROTO; |
| 2278 | goto err_clear; |
| 2279 | } |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2280 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2281 | err = -EINVAL; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2282 | switch (size) { |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2283 | case offsetof(struct bpf_tunnel_key, tunnel_label): |
Daniel Borkmann | c0e760c | 2016-03-30 00:02:00 +0200 | [diff] [blame] | 2284 | case offsetof(struct bpf_tunnel_key, tunnel_ext): |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2285 | goto set_compat; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2286 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
| 2287 | /* Fixup deprecated structure layouts here, so we have |
| 2288 | * a common path later on. |
| 2289 | */ |
| 2290 | if (ip_tunnel_info_af(info) != AF_INET) |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2291 | goto err_clear; |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2292 | set_compat: |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2293 | to = (struct bpf_tunnel_key *)compat; |
| 2294 | break; |
| 2295 | default: |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2296 | goto err_clear; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2297 | } |
| 2298 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2299 | |
| 2300 | to->tunnel_id = be64_to_cpu(info->key.tun_id); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2301 | to->tunnel_tos = info->key.tos; |
| 2302 | to->tunnel_ttl = info->key.ttl; |
| 2303 | |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2304 | if (flags & BPF_F_TUNINFO_IPV6) { |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2305 | memcpy(to->remote_ipv6, &info->key.u.ipv6.src, |
| 2306 | sizeof(to->remote_ipv6)); |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2307 | to->tunnel_label = be32_to_cpu(info->key.label); |
| 2308 | } else { |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2309 | to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2310 | } |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2311 | |
| 2312 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2313 | memcpy(to_orig, to, size); |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2314 | |
| 2315 | return 0; |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2316 | err_clear: |
| 2317 | memset(to_orig, 0, size); |
| 2318 | return err; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 2321 | static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2322 | .func = bpf_skb_get_tunnel_key, |
| 2323 | .gpl_only = false, |
| 2324 | .ret_type = RET_INTEGER, |
| 2325 | .arg1_type = ARG_PTR_TO_CTX, |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2326 | .arg2_type = ARG_PTR_TO_RAW_STACK, |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2327 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 2328 | .arg4_type = ARG_ANYTHING, |
| 2329 | }; |
| 2330 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2331 | BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size) |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2332 | { |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2333 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2334 | int err; |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2335 | |
| 2336 | if (unlikely(!info || |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2337 | !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) { |
| 2338 | err = -ENOENT; |
| 2339 | goto err_clear; |
| 2340 | } |
| 2341 | if (unlikely(size < info->options_len)) { |
| 2342 | err = -ENOMEM; |
| 2343 | goto err_clear; |
| 2344 | } |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2345 | |
| 2346 | ip_tunnel_info_opts_get(to, info); |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2347 | if (size > info->options_len) |
| 2348 | memset(to + info->options_len, 0, size - info->options_len); |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2349 | |
| 2350 | return info->options_len; |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2351 | err_clear: |
| 2352 | memset(to, 0, size); |
| 2353 | return err; |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = { |
| 2357 | .func = bpf_skb_get_tunnel_opt, |
| 2358 | .gpl_only = false, |
| 2359 | .ret_type = RET_INTEGER, |
| 2360 | .arg1_type = ARG_PTR_TO_CTX, |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 2361 | .arg2_type = ARG_PTR_TO_RAW_STACK, |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2362 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 2363 | }; |
| 2364 | |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2365 | static struct metadata_dst __percpu *md_dst; |
| 2366 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2367 | BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, |
| 2368 | const struct bpf_tunnel_key *, from, u32, size, u64, flags) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2369 | { |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2370 | struct metadata_dst *md = this_cpu_ptr(md_dst); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2371 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2372 | struct ip_tunnel_info *info; |
| 2373 | |
Daniel Borkmann | 2208087 | 2016-03-04 15:15:05 +0100 | [diff] [blame] | 2374 | if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX | |
| 2375 | BPF_F_DONT_FRAGMENT))) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2376 | return -EINVAL; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2377 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
| 2378 | switch (size) { |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2379 | case offsetof(struct bpf_tunnel_key, tunnel_label): |
Daniel Borkmann | c0e760c | 2016-03-30 00:02:00 +0200 | [diff] [blame] | 2380 | case offsetof(struct bpf_tunnel_key, tunnel_ext): |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2381 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
| 2382 | /* Fixup deprecated structure layouts here, so we have |
| 2383 | * a common path later on. |
| 2384 | */ |
| 2385 | memcpy(compat, from, size); |
| 2386 | memset(compat + size, 0, sizeof(compat) - size); |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2387 | from = (const struct bpf_tunnel_key *) compat; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2388 | break; |
| 2389 | default: |
| 2390 | return -EINVAL; |
| 2391 | } |
| 2392 | } |
Daniel Borkmann | c0e760c | 2016-03-30 00:02:00 +0200 | [diff] [blame] | 2393 | if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) || |
| 2394 | from->tunnel_ext)) |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2395 | return -EINVAL; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2396 | |
| 2397 | skb_dst_drop(skb); |
| 2398 | dst_hold((struct dst_entry *) md); |
| 2399 | skb_dst_set(skb, (struct dst_entry *) md); |
| 2400 | |
| 2401 | info = &md->u.tun_info; |
| 2402 | info->mode = IP_TUNNEL_INFO_TX; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2403 | |
Daniel Borkmann | db3c613 | 2016-03-04 15:15:07 +0100 | [diff] [blame] | 2404 | info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE; |
Daniel Borkmann | 2208087 | 2016-03-04 15:15:05 +0100 | [diff] [blame] | 2405 | if (flags & BPF_F_DONT_FRAGMENT) |
| 2406 | info->key.tun_flags |= TUNNEL_DONT_FRAGMENT; |
| 2407 | |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2408 | info->key.tun_id = cpu_to_be64(from->tunnel_id); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2409 | info->key.tos = from->tunnel_tos; |
| 2410 | info->key.ttl = from->tunnel_ttl; |
| 2411 | |
| 2412 | if (flags & BPF_F_TUNINFO_IPV6) { |
| 2413 | info->mode |= IP_TUNNEL_INFO_IPV6; |
| 2414 | memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, |
| 2415 | sizeof(from->remote_ipv6)); |
Daniel Borkmann | 4018ab1 | 2016-03-09 03:00:05 +0100 | [diff] [blame] | 2416 | info->key.label = cpu_to_be32(from->tunnel_label) & |
| 2417 | IPV6_FLOWLABEL_MASK; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2418 | } else { |
| 2419 | info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame] | 2420 | if (flags & BPF_F_ZERO_CSUM_TX) |
| 2421 | info->key.tun_flags &= ~TUNNEL_CSUM; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 2422 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2423 | |
| 2424 | return 0; |
| 2425 | } |
| 2426 | |
Daniel Borkmann | 577c50a | 2016-03-04 15:15:04 +0100 | [diff] [blame] | 2427 | static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2428 | .func = bpf_skb_set_tunnel_key, |
| 2429 | .gpl_only = false, |
| 2430 | .ret_type = RET_INTEGER, |
| 2431 | .arg1_type = ARG_PTR_TO_CTX, |
| 2432 | .arg2_type = ARG_PTR_TO_STACK, |
| 2433 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 2434 | .arg4_type = ARG_ANYTHING, |
| 2435 | }; |
| 2436 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2437 | BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, |
| 2438 | const u8 *, from, u32, size) |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2439 | { |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2440 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 2441 | const struct metadata_dst *md = this_cpu_ptr(md_dst); |
| 2442 | |
| 2443 | if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1)))) |
| 2444 | return -EINVAL; |
Daniel Borkmann | fca5fdf | 2016-03-16 01:42:51 +0100 | [diff] [blame] | 2445 | if (unlikely(size > IP_TUNNEL_OPTS_MAX)) |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2446 | return -ENOMEM; |
| 2447 | |
| 2448 | ip_tunnel_info_opts_set(info, from, size); |
| 2449 | |
| 2450 | return 0; |
| 2451 | } |
| 2452 | |
| 2453 | static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { |
| 2454 | .func = bpf_skb_set_tunnel_opt, |
| 2455 | .gpl_only = false, |
| 2456 | .ret_type = RET_INTEGER, |
| 2457 | .arg1_type = ARG_PTR_TO_CTX, |
| 2458 | .arg2_type = ARG_PTR_TO_STACK, |
| 2459 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 2460 | }; |
| 2461 | |
| 2462 | static const struct bpf_func_proto * |
| 2463 | bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2464 | { |
| 2465 | if (!md_dst) { |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2466 | /* Race is not possible, since it's called from verifier |
| 2467 | * that is holding verifier mutex. |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2468 | */ |
Daniel Borkmann | fca5fdf | 2016-03-16 01:42:51 +0100 | [diff] [blame] | 2469 | md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX, |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2470 | GFP_KERNEL); |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2471 | if (!md_dst) |
| 2472 | return NULL; |
| 2473 | } |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2474 | |
| 2475 | switch (which) { |
| 2476 | case BPF_FUNC_skb_set_tunnel_key: |
| 2477 | return &bpf_skb_set_tunnel_key_proto; |
| 2478 | case BPF_FUNC_skb_set_tunnel_opt: |
| 2479 | return &bpf_skb_set_tunnel_opt_proto; |
| 2480 | default: |
| 2481 | return NULL; |
| 2482 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2485 | BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map, |
| 2486 | u32, idx) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2487 | { |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2488 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 2489 | struct cgroup *cgrp; |
| 2490 | struct sock *sk; |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2491 | |
Daniel Borkmann | 2d48c5f | 2016-09-23 01:28:35 +0200 | [diff] [blame] | 2492 | sk = skb_to_full_sk(skb); |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2493 | if (!sk || !sk_fullsock(sk)) |
| 2494 | return -ENOENT; |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2495 | if (unlikely(idx >= array->map.max_entries)) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2496 | return -E2BIG; |
| 2497 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2498 | cgrp = READ_ONCE(array->ptrs[idx]); |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2499 | if (unlikely(!cgrp)) |
| 2500 | return -EAGAIN; |
| 2501 | |
Daniel Borkmann | 54fd9c2 | 2016-08-18 01:00:41 +0200 | [diff] [blame] | 2502 | return sk_under_cgroup_hierarchy(sk, cgrp); |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2505 | static const struct bpf_func_proto bpf_skb_under_cgroup_proto = { |
| 2506 | .func = bpf_skb_under_cgroup, |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2507 | .gpl_only = false, |
| 2508 | .ret_type = RET_INTEGER, |
| 2509 | .arg1_type = ARG_PTR_TO_CTX, |
| 2510 | .arg2_type = ARG_CONST_MAP_PTR, |
| 2511 | .arg3_type = ARG_ANYTHING, |
| 2512 | }; |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2513 | |
Daniel Borkmann | 4de1696 | 2016-08-18 01:00:40 +0200 | [diff] [blame] | 2514 | static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff, |
| 2515 | unsigned long off, unsigned long len) |
| 2516 | { |
| 2517 | memcpy(dst_buff, src_buff + off, len); |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 2521 | BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map, |
| 2522 | u64, flags, void *, meta, u64, meta_size) |
Daniel Borkmann | 4de1696 | 2016-08-18 01:00:40 +0200 | [diff] [blame] | 2523 | { |
Daniel Borkmann | 4de1696 | 2016-08-18 01:00:40 +0200 | [diff] [blame] | 2524 | u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32; |
Daniel Borkmann | 4de1696 | 2016-08-18 01:00:40 +0200 | [diff] [blame] | 2525 | |
| 2526 | if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) |
| 2527 | return -EINVAL; |
| 2528 | if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data))) |
| 2529 | return -EFAULT; |
| 2530 | |
| 2531 | return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size, |
| 2532 | bpf_xdp_copy); |
| 2533 | } |
| 2534 | |
| 2535 | static const struct bpf_func_proto bpf_xdp_event_output_proto = { |
| 2536 | .func = bpf_xdp_event_output, |
| 2537 | .gpl_only = true, |
| 2538 | .ret_type = RET_INTEGER, |
| 2539 | .arg1_type = ARG_PTR_TO_CTX, |
| 2540 | .arg2_type = ARG_CONST_MAP_PTR, |
| 2541 | .arg3_type = ARG_ANYTHING, |
| 2542 | .arg4_type = ARG_PTR_TO_STACK, |
| 2543 | .arg5_type = ARG_CONST_STACK_SIZE, |
| 2544 | }; |
| 2545 | |
Chenbo Feng | 0931366 | 2017-03-22 17:27:34 -0700 | [diff] [blame] | 2546 | BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) |
| 2547 | { |
| 2548 | return skb->sk ? sock_gen_cookie(skb->sk) : 0; |
| 2549 | } |
| 2550 | |
| 2551 | static const struct bpf_func_proto bpf_get_socket_cookie_proto = { |
| 2552 | .func = bpf_get_socket_cookie, |
| 2553 | .gpl_only = false, |
| 2554 | .ret_type = RET_INTEGER, |
| 2555 | .arg1_type = ARG_PTR_TO_CTX, |
| 2556 | }; |
| 2557 | |
Chenbo Feng | a29c81e | 2017-03-22 17:27:35 -0700 | [diff] [blame] | 2558 | BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb) |
| 2559 | { |
| 2560 | struct sock *sk = sk_to_full_sk(skb->sk); |
| 2561 | kuid_t kuid; |
| 2562 | |
| 2563 | if (!sk || !sk_fullsock(sk)) |
| 2564 | return overflowuid; |
| 2565 | kuid = sock_net_uid(sock_net(sk), sk); |
| 2566 | return from_kuid_munged(sock_net(sk)->user_ns, kuid); |
| 2567 | } |
| 2568 | |
| 2569 | static const struct bpf_func_proto bpf_get_socket_uid_proto = { |
| 2570 | .func = bpf_get_socket_uid, |
| 2571 | .gpl_only = false, |
| 2572 | .ret_type = RET_INTEGER, |
| 2573 | .arg1_type = ARG_PTR_TO_CTX, |
| 2574 | }; |
| 2575 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2576 | static const struct bpf_func_proto * |
| 2577 | sk_filter_func_proto(enum bpf_func_id func_id) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2578 | { |
| 2579 | switch (func_id) { |
| 2580 | case BPF_FUNC_map_lookup_elem: |
| 2581 | return &bpf_map_lookup_elem_proto; |
| 2582 | case BPF_FUNC_map_update_elem: |
| 2583 | return &bpf_map_update_elem_proto; |
| 2584 | case BPF_FUNC_map_delete_elem: |
| 2585 | return &bpf_map_delete_elem_proto; |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 2586 | case BPF_FUNC_get_prandom_u32: |
| 2587 | return &bpf_get_prandom_u32_proto; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 2588 | case BPF_FUNC_get_smp_processor_id: |
Daniel Borkmann | 80b48c4 | 2016-06-28 12:18:26 +0200 | [diff] [blame] | 2589 | return &bpf_get_raw_smp_processor_id_proto; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2590 | case BPF_FUNC_tail_call: |
| 2591 | return &bpf_tail_call_proto; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 2592 | case BPF_FUNC_ktime_get_ns: |
| 2593 | return &bpf_ktime_get_ns_proto; |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 2594 | case BPF_FUNC_trace_printk: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2595 | if (capable(CAP_SYS_ADMIN)) |
| 2596 | return bpf_get_trace_printk_proto(); |
Chenbo Feng | 0931366 | 2017-03-22 17:27:34 -0700 | [diff] [blame] | 2597 | case BPF_FUNC_get_socket_cookie: |
| 2598 | return &bpf_get_socket_cookie_proto; |
Chenbo Feng | a29c81e | 2017-03-22 17:27:35 -0700 | [diff] [blame] | 2599 | case BPF_FUNC_get_socket_uid: |
| 2600 | return &bpf_get_socket_uid_proto; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2601 | default: |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2606 | static const struct bpf_func_proto * |
| 2607 | tc_cls_act_func_proto(enum bpf_func_id func_id) |
| 2608 | { |
| 2609 | switch (func_id) { |
| 2610 | case BPF_FUNC_skb_store_bytes: |
| 2611 | return &bpf_skb_store_bytes_proto; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 2612 | case BPF_FUNC_skb_load_bytes: |
| 2613 | return &bpf_skb_load_bytes_proto; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2614 | case BPF_FUNC_skb_pull_data: |
| 2615 | return &bpf_skb_pull_data_proto; |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 2616 | case BPF_FUNC_csum_diff: |
| 2617 | return &bpf_csum_diff_proto; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2618 | case BPF_FUNC_csum_update: |
| 2619 | return &bpf_csum_update_proto; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 2620 | case BPF_FUNC_l3_csum_replace: |
| 2621 | return &bpf_l3_csum_replace_proto; |
| 2622 | case BPF_FUNC_l4_csum_replace: |
| 2623 | return &bpf_l4_csum_replace_proto; |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 2624 | case BPF_FUNC_clone_redirect: |
| 2625 | return &bpf_clone_redirect_proto; |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 2626 | case BPF_FUNC_get_cgroup_classid: |
| 2627 | return &bpf_get_cgroup_classid_proto; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 2628 | case BPF_FUNC_skb_vlan_push: |
| 2629 | return &bpf_skb_vlan_push_proto; |
| 2630 | case BPF_FUNC_skb_vlan_pop: |
| 2631 | return &bpf_skb_vlan_pop_proto; |
Daniel Borkmann | 6578171 | 2016-06-28 12:18:27 +0200 | [diff] [blame] | 2632 | case BPF_FUNC_skb_change_proto: |
| 2633 | return &bpf_skb_change_proto_proto; |
Daniel Borkmann | d2485c4 | 2016-06-28 12:18:28 +0200 | [diff] [blame] | 2634 | case BPF_FUNC_skb_change_type: |
| 2635 | return &bpf_skb_change_type_proto; |
Daniel Borkmann | 5293efe | 2016-08-18 01:00:39 +0200 | [diff] [blame] | 2636 | case BPF_FUNC_skb_change_tail: |
| 2637 | return &bpf_skb_change_tail_proto; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 2638 | case BPF_FUNC_skb_get_tunnel_key: |
| 2639 | return &bpf_skb_get_tunnel_key_proto; |
| 2640 | case BPF_FUNC_skb_set_tunnel_key: |
Daniel Borkmann | 14ca075 | 2016-03-04 15:15:06 +0100 | [diff] [blame] | 2641 | return bpf_get_skb_set_tunnel_proto(func_id); |
| 2642 | case BPF_FUNC_skb_get_tunnel_opt: |
| 2643 | return &bpf_skb_get_tunnel_opt_proto; |
| 2644 | case BPF_FUNC_skb_set_tunnel_opt: |
| 2645 | return bpf_get_skb_set_tunnel_proto(func_id); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 2646 | case BPF_FUNC_redirect: |
| 2647 | return &bpf_redirect_proto; |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 2648 | case BPF_FUNC_get_route_realm: |
| 2649 | return &bpf_get_route_realm_proto; |
Daniel Borkmann | 13c5c24 | 2016-07-03 01:28:47 +0200 | [diff] [blame] | 2650 | case BPF_FUNC_get_hash_recalc: |
| 2651 | return &bpf_get_hash_recalc_proto; |
Daniel Borkmann | 7a4b28c | 2016-09-23 01:28:37 +0200 | [diff] [blame] | 2652 | case BPF_FUNC_set_hash_invalid: |
| 2653 | return &bpf_set_hash_invalid_proto; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 2654 | case BPF_FUNC_perf_event_output: |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 2655 | return &bpf_skb_event_output_proto; |
Daniel Borkmann | 80b48c4 | 2016-06-28 12:18:26 +0200 | [diff] [blame] | 2656 | case BPF_FUNC_get_smp_processor_id: |
| 2657 | return &bpf_get_smp_processor_id_proto; |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2658 | case BPF_FUNC_skb_under_cgroup: |
| 2659 | return &bpf_skb_under_cgroup_proto; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2660 | default: |
| 2661 | return sk_filter_func_proto(func_id); |
| 2662 | } |
| 2663 | } |
| 2664 | |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 2665 | static const struct bpf_func_proto * |
| 2666 | xdp_func_proto(enum bpf_func_id func_id) |
| 2667 | { |
Daniel Borkmann | 4de1696 | 2016-08-18 01:00:40 +0200 | [diff] [blame] | 2668 | switch (func_id) { |
| 2669 | case BPF_FUNC_perf_event_output: |
| 2670 | return &bpf_xdp_event_output_proto; |
Daniel Borkmann | 669dc4d | 2016-09-23 01:28:36 +0200 | [diff] [blame] | 2671 | case BPF_FUNC_get_smp_processor_id: |
| 2672 | return &bpf_get_smp_processor_id_proto; |
Daniel Borkmann | 4de1696 | 2016-08-18 01:00:40 +0200 | [diff] [blame] | 2673 | default: |
| 2674 | return sk_filter_func_proto(func_id); |
| 2675 | } |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
Daniel Mack | 760b55c | 2016-11-23 16:52:25 +0100 | [diff] [blame] | 2678 | static const struct bpf_func_proto * |
| 2679 | cg_skb_func_proto(enum bpf_func_id func_id) |
| 2680 | { |
| 2681 | switch (func_id) { |
| 2682 | case BPF_FUNC_skb_load_bytes: |
| 2683 | return &bpf_skb_load_bytes_proto; |
| 2684 | default: |
| 2685 | return sk_filter_func_proto(func_id); |
| 2686 | } |
| 2687 | } |
| 2688 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2689 | static bool __is_valid_access(int off, int size, enum bpf_access_type type) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2690 | { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2691 | if (off < 0 || off >= sizeof(struct __sk_buff)) |
| 2692 | return false; |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 2693 | /* The verifier guarantees that size > 0. */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2694 | if (off % size != 0) |
| 2695 | return false; |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 2696 | if (size != sizeof(__u32)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2697 | return false; |
| 2698 | |
| 2699 | return true; |
| 2700 | } |
| 2701 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2702 | static bool sk_filter_is_valid_access(int off, int size, |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2703 | enum bpf_access_type type, |
| 2704 | enum bpf_reg_type *reg_type) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2705 | { |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 2706 | switch (off) { |
| 2707 | case offsetof(struct __sk_buff, tc_classid): |
| 2708 | case offsetof(struct __sk_buff, data): |
| 2709 | case offsetof(struct __sk_buff, data_end): |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 2710 | return false; |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 2711 | } |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 2712 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2713 | if (type == BPF_WRITE) { |
| 2714 | switch (off) { |
| 2715 | case offsetof(struct __sk_buff, cb[0]) ... |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 2716 | offsetof(struct __sk_buff, cb[4]): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2717 | break; |
| 2718 | default: |
| 2719 | return false; |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | return __is_valid_access(off, size, type); |
| 2724 | } |
| 2725 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2726 | static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write, |
| 2727 | const struct bpf_prog *prog) |
| 2728 | { |
| 2729 | struct bpf_insn *insn = insn_buf; |
| 2730 | |
| 2731 | if (!direct_write) |
| 2732 | return 0; |
| 2733 | |
| 2734 | /* if (!skb->cloned) |
| 2735 | * goto start; |
| 2736 | * |
| 2737 | * (Fast-path, otherwise approximation that we might be |
| 2738 | * a clone, do the rest in helper.) |
| 2739 | */ |
| 2740 | *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET()); |
| 2741 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK); |
| 2742 | *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7); |
| 2743 | |
| 2744 | /* ret = bpf_skb_pull_data(skb, 0); */ |
| 2745 | *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); |
| 2746 | *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2); |
| 2747 | *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, |
| 2748 | BPF_FUNC_skb_pull_data); |
| 2749 | /* if (!ret) |
| 2750 | * goto restore; |
| 2751 | * return TC_ACT_SHOT; |
| 2752 | */ |
| 2753 | *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2); |
| 2754 | *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT); |
| 2755 | *insn++ = BPF_EXIT_INSN(); |
| 2756 | |
| 2757 | /* restore: */ |
| 2758 | *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); |
| 2759 | /* start: */ |
| 2760 | *insn++ = prog->insnsi[0]; |
| 2761 | |
| 2762 | return insn - insn_buf; |
| 2763 | } |
| 2764 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2765 | static bool tc_cls_act_is_valid_access(int off, int size, |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2766 | enum bpf_access_type type, |
| 2767 | enum bpf_reg_type *reg_type) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2768 | { |
| 2769 | if (type == BPF_WRITE) { |
| 2770 | switch (off) { |
| 2771 | case offsetof(struct __sk_buff, mark): |
| 2772 | case offsetof(struct __sk_buff, tc_index): |
Daniel Borkmann | 754f1e6 | 2015-09-30 01:41:52 +0200 | [diff] [blame] | 2773 | case offsetof(struct __sk_buff, priority): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2774 | case offsetof(struct __sk_buff, cb[0]) ... |
Daniel Borkmann | 09c37a2 | 2016-03-16 01:42:49 +0100 | [diff] [blame] | 2775 | offsetof(struct __sk_buff, cb[4]): |
| 2776 | case offsetof(struct __sk_buff, tc_classid): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2777 | break; |
| 2778 | default: |
| 2779 | return false; |
| 2780 | } |
| 2781 | } |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2782 | |
| 2783 | switch (off) { |
| 2784 | case offsetof(struct __sk_buff, data): |
| 2785 | *reg_type = PTR_TO_PACKET; |
| 2786 | break; |
| 2787 | case offsetof(struct __sk_buff, data_end): |
| 2788 | *reg_type = PTR_TO_PACKET_END; |
| 2789 | break; |
| 2790 | } |
| 2791 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2792 | return __is_valid_access(off, size, type); |
| 2793 | } |
| 2794 | |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 2795 | static bool __is_valid_xdp_access(int off, int size, |
| 2796 | enum bpf_access_type type) |
| 2797 | { |
| 2798 | if (off < 0 || off >= sizeof(struct xdp_md)) |
| 2799 | return false; |
| 2800 | if (off % size != 0) |
| 2801 | return false; |
Daniel Borkmann | 6088b58 | 2016-09-09 02:45:28 +0200 | [diff] [blame] | 2802 | if (size != sizeof(__u32)) |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 2803 | return false; |
| 2804 | |
| 2805 | return true; |
| 2806 | } |
| 2807 | |
| 2808 | static bool xdp_is_valid_access(int off, int size, |
| 2809 | enum bpf_access_type type, |
| 2810 | enum bpf_reg_type *reg_type) |
| 2811 | { |
| 2812 | if (type == BPF_WRITE) |
| 2813 | return false; |
| 2814 | |
| 2815 | switch (off) { |
| 2816 | case offsetof(struct xdp_md, data): |
| 2817 | *reg_type = PTR_TO_PACKET; |
| 2818 | break; |
| 2819 | case offsetof(struct xdp_md, data_end): |
| 2820 | *reg_type = PTR_TO_PACKET_END; |
| 2821 | break; |
| 2822 | } |
| 2823 | |
| 2824 | return __is_valid_xdp_access(off, size, type); |
| 2825 | } |
| 2826 | |
| 2827 | void bpf_warn_invalid_xdp_action(u32 act) |
| 2828 | { |
| 2829 | WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act); |
| 2830 | } |
| 2831 | EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action); |
| 2832 | |
Daniel Borkmann | 374fb54 | 2016-09-09 02:45:30 +0200 | [diff] [blame] | 2833 | static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg, |
| 2834 | int src_reg, int ctx_off, |
| 2835 | struct bpf_insn *insn_buf, |
| 2836 | struct bpf_prog *prog) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2837 | { |
| 2838 | struct bpf_insn *insn = insn_buf; |
| 2839 | |
| 2840 | switch (ctx_off) { |
| 2841 | case offsetof(struct __sk_buff, len): |
| 2842 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); |
| 2843 | |
| 2844 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2845 | offsetof(struct sk_buff, len)); |
| 2846 | break; |
| 2847 | |
Daniel Borkmann | 0b8c707 | 2015-03-19 19:38:27 +0100 | [diff] [blame] | 2848 | case offsetof(struct __sk_buff, protocol): |
| 2849 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); |
| 2850 | |
| 2851 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2852 | offsetof(struct sk_buff, protocol)); |
| 2853 | break; |
| 2854 | |
Michal Sekletar | 27cd545 | 2015-03-24 14:48:41 +0100 | [diff] [blame] | 2855 | case offsetof(struct __sk_buff, vlan_proto): |
| 2856 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); |
| 2857 | |
| 2858 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2859 | offsetof(struct sk_buff, vlan_proto)); |
| 2860 | break; |
| 2861 | |
Daniel Borkmann | bcad571 | 2015-04-03 20:52:24 +0200 | [diff] [blame] | 2862 | case offsetof(struct __sk_buff, priority): |
| 2863 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4); |
| 2864 | |
Daniel Borkmann | 754f1e6 | 2015-09-30 01:41:52 +0200 | [diff] [blame] | 2865 | if (type == BPF_WRITE) |
| 2866 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, |
| 2867 | offsetof(struct sk_buff, priority)); |
| 2868 | else |
| 2869 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2870 | offsetof(struct sk_buff, priority)); |
Daniel Borkmann | bcad571 | 2015-04-03 20:52:24 +0200 | [diff] [blame] | 2871 | break; |
| 2872 | |
Alexei Starovoitov | 37e82c2 | 2015-05-27 15:30:39 -0700 | [diff] [blame] | 2873 | case offsetof(struct __sk_buff, ingress_ifindex): |
| 2874 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4); |
| 2875 | |
| 2876 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2877 | offsetof(struct sk_buff, skb_iif)); |
| 2878 | break; |
| 2879 | |
| 2880 | case offsetof(struct __sk_buff, ifindex): |
| 2881 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 2882 | |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 2883 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), |
Alexei Starovoitov | 37e82c2 | 2015-05-27 15:30:39 -0700 | [diff] [blame] | 2884 | dst_reg, src_reg, |
| 2885 | offsetof(struct sk_buff, dev)); |
| 2886 | *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1); |
| 2887 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg, |
| 2888 | offsetof(struct net_device, ifindex)); |
| 2889 | break; |
| 2890 | |
Daniel Borkmann | ba7591d | 2015-08-01 00:46:29 +0200 | [diff] [blame] | 2891 | case offsetof(struct __sk_buff, hash): |
| 2892 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 2893 | |
| 2894 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2895 | offsetof(struct sk_buff, hash)); |
| 2896 | break; |
| 2897 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2898 | case offsetof(struct __sk_buff, mark): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2899 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 2900 | |
| 2901 | if (type == BPF_WRITE) |
| 2902 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, |
| 2903 | offsetof(struct sk_buff, mark)); |
| 2904 | else |
| 2905 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2906 | offsetof(struct sk_buff, mark)); |
| 2907 | break; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2908 | |
| 2909 | case offsetof(struct __sk_buff, pkt_type): |
| 2910 | return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn); |
| 2911 | |
| 2912 | case offsetof(struct __sk_buff, queue_mapping): |
| 2913 | return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn); |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 2914 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 2915 | case offsetof(struct __sk_buff, vlan_present): |
| 2916 | return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, |
| 2917 | dst_reg, src_reg, insn); |
| 2918 | |
| 2919 | case offsetof(struct __sk_buff, vlan_tci): |
| 2920 | return convert_skb_access(SKF_AD_VLAN_TAG, |
| 2921 | dst_reg, src_reg, insn); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2922 | |
| 2923 | case offsetof(struct __sk_buff, cb[0]) ... |
Daniel Borkmann | 6088b58 | 2016-09-09 02:45:28 +0200 | [diff] [blame] | 2924 | offsetof(struct __sk_buff, cb[4]): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2925 | BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20); |
| 2926 | |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2927 | prog->cb_access = 1; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2928 | ctx_off -= offsetof(struct __sk_buff, cb[0]); |
| 2929 | ctx_off += offsetof(struct sk_buff, cb); |
| 2930 | ctx_off += offsetof(struct qdisc_skb_cb, data); |
| 2931 | if (type == BPF_WRITE) |
| 2932 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off); |
| 2933 | else |
| 2934 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off); |
| 2935 | break; |
| 2936 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 2937 | case offsetof(struct __sk_buff, tc_classid): |
| 2938 | ctx_off -= offsetof(struct __sk_buff, tc_classid); |
| 2939 | ctx_off += offsetof(struct sk_buff, cb); |
| 2940 | ctx_off += offsetof(struct qdisc_skb_cb, tc_classid); |
Daniel Borkmann | 09c37a2 | 2016-03-16 01:42:49 +0100 | [diff] [blame] | 2941 | if (type == BPF_WRITE) |
| 2942 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off); |
| 2943 | else |
| 2944 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off); |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 2945 | break; |
| 2946 | |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 2947 | case offsetof(struct __sk_buff, data): |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 2948 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 2949 | dst_reg, src_reg, |
| 2950 | offsetof(struct sk_buff, data)); |
| 2951 | break; |
| 2952 | |
| 2953 | case offsetof(struct __sk_buff, data_end): |
| 2954 | ctx_off -= offsetof(struct __sk_buff, data_end); |
| 2955 | ctx_off += offsetof(struct sk_buff, cb); |
| 2956 | ctx_off += offsetof(struct bpf_skb_data_end, data_end); |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 2957 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg, |
| 2958 | ctx_off); |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 2959 | break; |
| 2960 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2961 | case offsetof(struct __sk_buff, tc_index): |
| 2962 | #ifdef CONFIG_NET_SCHED |
| 2963 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2); |
| 2964 | |
| 2965 | if (type == BPF_WRITE) |
| 2966 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, |
| 2967 | offsetof(struct sk_buff, tc_index)); |
| 2968 | else |
| 2969 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2970 | offsetof(struct sk_buff, tc_index)); |
| 2971 | break; |
| 2972 | #else |
| 2973 | if (type == BPF_WRITE) |
| 2974 | *insn++ = BPF_MOV64_REG(dst_reg, dst_reg); |
| 2975 | else |
| 2976 | *insn++ = BPF_MOV64_IMM(dst_reg, 0); |
| 2977 | break; |
| 2978 | #endif |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | return insn - insn_buf; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2982 | } |
| 2983 | |
Daniel Borkmann | 374fb54 | 2016-09-09 02:45:30 +0200 | [diff] [blame] | 2984 | static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg, |
| 2985 | int src_reg, int ctx_off, |
| 2986 | struct bpf_insn *insn_buf, |
| 2987 | struct bpf_prog *prog) |
| 2988 | { |
| 2989 | struct bpf_insn *insn = insn_buf; |
| 2990 | |
| 2991 | switch (ctx_off) { |
| 2992 | case offsetof(struct __sk_buff, ifindex): |
| 2993 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 2994 | |
| 2995 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), |
| 2996 | dst_reg, src_reg, |
| 2997 | offsetof(struct sk_buff, dev)); |
| 2998 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg, |
| 2999 | offsetof(struct net_device, ifindex)); |
| 3000 | break; |
| 3001 | default: |
| 3002 | return sk_filter_convert_ctx_access(type, dst_reg, src_reg, |
| 3003 | ctx_off, insn_buf, prog); |
| 3004 | } |
| 3005 | |
| 3006 | return insn - insn_buf; |
| 3007 | } |
| 3008 | |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 3009 | static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg, |
| 3010 | int src_reg, int ctx_off, |
| 3011 | struct bpf_insn *insn_buf, |
| 3012 | struct bpf_prog *prog) |
| 3013 | { |
| 3014 | struct bpf_insn *insn = insn_buf; |
| 3015 | |
| 3016 | switch (ctx_off) { |
| 3017 | case offsetof(struct xdp_md, data): |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 3018 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data), |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 3019 | dst_reg, src_reg, |
| 3020 | offsetof(struct xdp_buff, data)); |
| 3021 | break; |
| 3022 | case offsetof(struct xdp_md, data_end): |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 3023 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end), |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 3024 | dst_reg, src_reg, |
| 3025 | offsetof(struct xdp_buff, data_end)); |
| 3026 | break; |
| 3027 | } |
| 3028 | |
| 3029 | return insn - insn_buf; |
| 3030 | } |
| 3031 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 3032 | static const struct bpf_verifier_ops sk_filter_ops = { |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 3033 | .get_func_proto = sk_filter_func_proto, |
| 3034 | .is_valid_access = sk_filter_is_valid_access, |
Daniel Borkmann | 374fb54 | 2016-09-09 02:45:30 +0200 | [diff] [blame] | 3035 | .convert_ctx_access = sk_filter_convert_ctx_access, |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 3036 | }; |
| 3037 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 3038 | static const struct bpf_verifier_ops tc_cls_act_ops = { |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 3039 | .get_func_proto = tc_cls_act_func_proto, |
| 3040 | .is_valid_access = tc_cls_act_is_valid_access, |
Daniel Borkmann | 374fb54 | 2016-09-09 02:45:30 +0200 | [diff] [blame] | 3041 | .convert_ctx_access = tc_cls_act_convert_ctx_access, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3042 | .gen_prologue = tc_cls_act_prologue, |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 3043 | }; |
| 3044 | |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 3045 | static const struct bpf_verifier_ops xdp_ops = { |
| 3046 | .get_func_proto = xdp_func_proto, |
| 3047 | .is_valid_access = xdp_is_valid_access, |
| 3048 | .convert_ctx_access = xdp_convert_ctx_access, |
| 3049 | }; |
| 3050 | |
Daniel Mack | 760b55c | 2016-11-23 16:52:25 +0100 | [diff] [blame] | 3051 | static const struct bpf_verifier_ops cg_skb_ops = { |
| 3052 | .get_func_proto = cg_skb_func_proto, |
| 3053 | .is_valid_access = sk_filter_is_valid_access, |
| 3054 | .convert_ctx_access = sk_filter_convert_ctx_access, |
| 3055 | }; |
| 3056 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 3057 | static struct bpf_prog_type_list sk_filter_type __read_mostly = { |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 3058 | .ops = &sk_filter_ops, |
| 3059 | .type = BPF_PROG_TYPE_SOCKET_FILTER, |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 3060 | }; |
| 3061 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3062 | static struct bpf_prog_type_list sched_cls_type __read_mostly = { |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 3063 | .ops = &tc_cls_act_ops, |
| 3064 | .type = BPF_PROG_TYPE_SCHED_CLS, |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3065 | }; |
| 3066 | |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3067 | static struct bpf_prog_type_list sched_act_type __read_mostly = { |
Daniel Borkmann | 4936e35 | 2016-05-13 19:08:26 +0200 | [diff] [blame] | 3068 | .ops = &tc_cls_act_ops, |
| 3069 | .type = BPF_PROG_TYPE_SCHED_ACT, |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3070 | }; |
| 3071 | |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 3072 | static struct bpf_prog_type_list xdp_type __read_mostly = { |
| 3073 | .ops = &xdp_ops, |
| 3074 | .type = BPF_PROG_TYPE_XDP, |
| 3075 | }; |
| 3076 | |
Daniel Mack | 760b55c | 2016-11-23 16:52:25 +0100 | [diff] [blame] | 3077 | static struct bpf_prog_type_list cg_skb_type __read_mostly = { |
| 3078 | .ops = &cg_skb_ops, |
| 3079 | .type = BPF_PROG_TYPE_CGROUP_SKB, |
| 3080 | }; |
| 3081 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 3082 | static int __init register_sk_filter_ops(void) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 3083 | { |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 3084 | bpf_register_prog_type(&sk_filter_type); |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3085 | bpf_register_prog_type(&sched_cls_type); |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3086 | bpf_register_prog_type(&sched_act_type); |
Brenden Blanco | 6a773a1 | 2016-07-19 12:16:47 -0700 | [diff] [blame] | 3087 | bpf_register_prog_type(&xdp_type); |
Daniel Mack | 760b55c | 2016-11-23 16:52:25 +0100 | [diff] [blame] | 3088 | bpf_register_prog_type(&cg_skb_type); |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3089 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 3090 | return 0; |
| 3091 | } |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 3092 | late_initcall(register_sk_filter_ops); |
| 3093 | |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 3094 | int sk_detach_filter(struct sock *sk) |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 3095 | { |
| 3096 | int ret = -ENOENT; |
| 3097 | struct sk_filter *filter; |
| 3098 | |
Vincent Bernat | d59577b | 2013-01-16 22:55:49 +0100 | [diff] [blame] | 3099 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 3100 | return -EPERM; |
| 3101 | |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 3102 | filter = rcu_dereference_protected(sk->sk_filter, |
| 3103 | lockdep_sock_is_held(sk)); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 3104 | if (filter) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 3105 | RCU_INIT_POINTER(sk->sk_filter, NULL); |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 3106 | sk_filter_uncharge(sk, filter); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 3107 | ret = 0; |
| 3108 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3109 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 3110 | return ret; |
| 3111 | } |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 3112 | EXPORT_SYMBOL_GPL(sk_detach_filter); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3113 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3114 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, |
| 3115 | unsigned int len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3116 | { |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3117 | struct sock_fprog_kern *fprog; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3118 | struct sk_filter *filter; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3119 | int ret = 0; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3120 | |
| 3121 | lock_sock(sk); |
| 3122 | filter = rcu_dereference_protected(sk->sk_filter, |
Hannes Frederic Sowa | 8ced425 | 2016-04-05 17:10:16 +0200 | [diff] [blame] | 3123 | lockdep_sock_is_held(sk)); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3124 | if (!filter) |
| 3125 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3126 | |
| 3127 | /* We're copying the filter that has been originally attached, |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 3128 | * so no conversion/decode needed anymore. eBPF programs that |
| 3129 | * have no original program cannot be dumped through this. |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3130 | */ |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 3131 | ret = -EACCES; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 3132 | fprog = filter->prog->orig_prog; |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 3133 | if (!fprog) |
| 3134 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3135 | |
| 3136 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3137 | if (!len) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3138 | /* User space only enquires number of filter blocks. */ |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3139 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3140 | |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3141 | ret = -EINVAL; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3142 | if (len < fprog->len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3143 | goto out; |
| 3144 | |
| 3145 | ret = -EFAULT; |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 3146 | if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3147 | goto out; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3148 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 3149 | /* Instead of bytes, the API requests to return the number |
| 3150 | * of filter blocks. |
| 3151 | */ |
| 3152 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 3153 | out: |
| 3154 | release_sock(sk); |
| 3155 | return ret; |
| 3156 | } |