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> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/inet.h> |
| 31 | #include <linux/netdevice.h> |
| 32 | #include <linux/if_packet.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <net/ip.h> |
| 35 | #include <net/protocol.h> |
Patrick McHardy | 4738c1d | 2008-04-10 02:02:28 -0700 | [diff] [blame] | 36 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/skbuff.h> |
| 38 | #include <net/sock.h> |
Jiri Pirko | 10b89ee4 | 2015-05-12 14:56:09 +0200 | [diff] [blame] | 39 | #include <net/flow_dissector.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/errno.h> |
| 41 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
Dmitry Mishin | 40daafc | 2006-04-18 14:50:10 -0700 | [diff] [blame] | 43 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/filter.h> |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 45 | #include <linux/ratelimit.h> |
Will Drewry | 46b325c | 2012-04-12 16:47:52 -0500 | [diff] [blame] | 46 | #include <linux/seccomp.h> |
Eric Dumazet | f333503 | 2012-10-27 02:26:17 +0000 | [diff] [blame] | 47 | #include <linux/if_vlan.h> |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 48 | #include <linux/bpf.h> |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 49 | #include <net/sch_generic.h> |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 50 | #include <net/cls_cgroup.h> |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 51 | #include <net/dst_metadata.h> |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 52 | #include <net/dst.h> |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 53 | #include <net/sock_reuseport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /** |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 56 | * sk_filter - run a packet through a socket filter |
| 57 | * @sk: sock associated with &sk_buff |
| 58 | * @skb: buffer to filter |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 59 | * |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 60 | * Run the eBPF program and then cut skb->data to correct size returned by |
| 61 | * 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] | 62 | * 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] | 63 | * wrapper to BPF_PROG_RUN. It returns 0 if the packet should |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 64 | * be accepted or -EPERM if the packet should be tossed. |
| 65 | * |
| 66 | */ |
| 67 | int sk_filter(struct sock *sk, struct sk_buff *skb) |
| 68 | { |
| 69 | int err; |
| 70 | struct sk_filter *filter; |
| 71 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 72 | /* |
| 73 | * If the skb was allocated from pfmemalloc reserves, only |
| 74 | * allow SOCK_MEMALLOC sockets to use it as this socket is |
| 75 | * helping free memory |
| 76 | */ |
| 77 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) |
| 78 | return -ENOMEM; |
| 79 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 80 | err = security_sock_rcv_skb(sk, skb); |
| 81 | if (err) |
| 82 | return err; |
| 83 | |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 84 | rcu_read_lock(); |
| 85 | filter = rcu_dereference(sk->sk_filter); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 86 | if (filter) { |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 87 | unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb); |
Eric Dumazet | 0d7da9d | 2010-10-25 03:47:05 +0000 | [diff] [blame] | 88 | |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 89 | err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM; |
| 90 | } |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 91 | rcu_read_unlock(); |
Stephen Hemminger | 43db6d6 | 2008-04-10 01:43:09 -0700 | [diff] [blame] | 92 | |
| 93 | return err; |
| 94 | } |
| 95 | EXPORT_SYMBOL(sk_filter); |
| 96 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 97 | static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 98 | { |
Alexander Duyck | 56193d1 | 2014-09-05 19:20:26 -0400 | [diff] [blame] | 99 | return skb_get_poff((struct sk_buff *)(unsigned long) ctx); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 102 | static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 103 | { |
Daniel Borkmann | eb9672f | 2014-05-01 18:34:20 +0200 | [diff] [blame] | 104 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 105 | struct nlattr *nla; |
| 106 | |
| 107 | if (skb_is_nonlinear(skb)) |
| 108 | return 0; |
| 109 | |
Mathias Krause | 05ab8f2 | 2014-04-13 18:23:33 +0200 | [diff] [blame] | 110 | if (skb->len < sizeof(struct nlattr)) |
| 111 | return 0; |
| 112 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 113 | if (a > skb->len - sizeof(struct nlattr)) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 114 | return 0; |
| 115 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 116 | nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 117 | if (nla) |
| 118 | return (void *) nla - (void *) skb->data; |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 123 | static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 124 | { |
Daniel Borkmann | eb9672f | 2014-05-01 18:34:20 +0200 | [diff] [blame] | 125 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 126 | struct nlattr *nla; |
| 127 | |
| 128 | if (skb_is_nonlinear(skb)) |
| 129 | return 0; |
| 130 | |
Mathias Krause | 05ab8f2 | 2014-04-13 18:23:33 +0200 | [diff] [blame] | 131 | if (skb->len < sizeof(struct nlattr)) |
| 132 | return 0; |
| 133 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 134 | if (a > skb->len - sizeof(struct nlattr)) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 137 | nla = (struct nlattr *) &skb->data[a]; |
| 138 | if (nla->nla_len > skb->len - a) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 139 | return 0; |
| 140 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 141 | nla = nla_find_nested(nla, x); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 142 | if (nla) |
| 143 | return (void *) nla - (void *) skb->data; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 148 | static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 149 | { |
| 150 | return raw_smp_processor_id(); |
| 151 | } |
| 152 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 153 | static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, |
| 154 | struct bpf_insn *insn_buf) |
| 155 | { |
| 156 | struct bpf_insn *insn = insn_buf; |
| 157 | |
| 158 | switch (skb_field) { |
| 159 | case SKF_AD_MARK: |
| 160 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 161 | |
| 162 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 163 | offsetof(struct sk_buff, mark)); |
| 164 | break; |
| 165 | |
| 166 | case SKF_AD_PKTTYPE: |
| 167 | *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); |
| 168 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); |
| 169 | #ifdef __BIG_ENDIAN_BITFIELD |
| 170 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); |
| 171 | #endif |
| 172 | break; |
| 173 | |
| 174 | case SKF_AD_QUEUE: |
| 175 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); |
| 176 | |
| 177 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 178 | offsetof(struct sk_buff, queue_mapping)); |
| 179 | break; |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 180 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 181 | case SKF_AD_VLAN_TAG: |
| 182 | case SKF_AD_VLAN_TAG_PRESENT: |
| 183 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); |
| 184 | BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); |
| 185 | |
| 186 | /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ |
| 187 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 188 | offsetof(struct sk_buff, vlan_tci)); |
| 189 | if (skb_field == SKF_AD_VLAN_TAG) { |
| 190 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, |
| 191 | ~VLAN_TAG_PRESENT); |
| 192 | } else { |
| 193 | /* dst_reg >>= 12 */ |
| 194 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12); |
| 195 | /* dst_reg &= 1 */ |
| 196 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); |
| 197 | } |
| 198 | break; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return insn - insn_buf; |
| 202 | } |
| 203 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 204 | static bool convert_bpf_extensions(struct sock_filter *fp, |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 205 | struct bpf_insn **insnp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 206 | { |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 207 | struct bpf_insn *insn = *insnp; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 208 | u32 cnt; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 209 | |
| 210 | switch (fp->k) { |
| 211 | case SKF_AD_OFF + SKF_AD_PROTOCOL: |
Daniel Borkmann | 0b8c707 | 2015-03-19 19:38:27 +0100 | [diff] [blame] | 212 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); |
| 213 | |
| 214 | /* A = *(u16 *) (CTX + offsetof(protocol)) */ |
| 215 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 216 | offsetof(struct sk_buff, protocol)); |
| 217 | /* A = ntohs(A) [emitting a nop or swap16] */ |
| 218 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case SKF_AD_OFF + SKF_AD_PKTTYPE: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 222 | cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); |
| 223 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | case SKF_AD_OFF + SKF_AD_IFINDEX: |
| 227 | case SKF_AD_OFF + SKF_AD_HATYPE: |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 228 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 229 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2); |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 230 | BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 231 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 232 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), |
| 233 | BPF_REG_TMP, BPF_REG_CTX, |
| 234 | offsetof(struct sk_buff, dev)); |
| 235 | /* if (tmp != 0) goto pc + 1 */ |
| 236 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); |
| 237 | *insn++ = BPF_EXIT_INSN(); |
| 238 | if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) |
| 239 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, |
| 240 | offsetof(struct net_device, ifindex)); |
| 241 | else |
| 242 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, |
| 243 | offsetof(struct net_device, type)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case SKF_AD_OFF + SKF_AD_MARK: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 247 | cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); |
| 248 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 249 | break; |
| 250 | |
| 251 | case SKF_AD_OFF + SKF_AD_RXHASH: |
| 252 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 253 | |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 254 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, |
| 255 | offsetof(struct sk_buff, hash)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | case SKF_AD_OFF + SKF_AD_QUEUE: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 259 | cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); |
| 260 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 261 | break; |
| 262 | |
| 263 | case SKF_AD_OFF + SKF_AD_VLAN_TAG: |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 264 | cnt = convert_skb_access(SKF_AD_VLAN_TAG, |
| 265 | BPF_REG_A, BPF_REG_CTX, insn); |
| 266 | insn += cnt - 1; |
| 267 | break; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 268 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 269 | case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: |
| 270 | cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, |
| 271 | BPF_REG_A, BPF_REG_CTX, insn); |
| 272 | insn += cnt - 1; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 273 | break; |
| 274 | |
Michal Sekletar | 27cd545 | 2015-03-24 14:48:41 +0100 | [diff] [blame] | 275 | case SKF_AD_OFF + SKF_AD_VLAN_TPID: |
| 276 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); |
| 277 | |
| 278 | /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ |
| 279 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, |
| 280 | offsetof(struct sk_buff, vlan_proto)); |
| 281 | /* A = ntohs(A) [emitting a nop or swap16] */ |
| 282 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); |
| 283 | break; |
| 284 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 285 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
| 286 | case SKF_AD_OFF + SKF_AD_NLATTR: |
| 287 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: |
| 288 | case SKF_AD_OFF + SKF_AD_CPU: |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 289 | case SKF_AD_OFF + SKF_AD_RANDOM: |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 290 | /* arg1 = CTX */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 291 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 292 | /* arg2 = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 293 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 294 | /* arg3 = X */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 295 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 296 | /* Emit call(arg1=CTX, arg2=A, arg3=X) */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 297 | switch (fp->k) { |
| 298 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 299 | *insn = BPF_EMIT_CALL(__skb_get_pay_offset); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 300 | break; |
| 301 | case SKF_AD_OFF + SKF_AD_NLATTR: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 302 | *insn = BPF_EMIT_CALL(__skb_get_nlattr); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 303 | break; |
| 304 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 305 | *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 306 | break; |
| 307 | case SKF_AD_OFF + SKF_AD_CPU: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 308 | *insn = BPF_EMIT_CALL(__get_raw_cpu_id); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 309 | break; |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 310 | case SKF_AD_OFF + SKF_AD_RANDOM: |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 311 | *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); |
| 312 | bpf_user_rnd_init_once(); |
Chema Gonzalez | 4cd3675 | 2014-04-21 09:21:24 -0700 | [diff] [blame] | 313 | break; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 314 | } |
| 315 | break; |
| 316 | |
| 317 | case SKF_AD_OFF + SKF_AD_ALU_XOR_X: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 318 | /* A ^= X */ |
| 319 | *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 320 | break; |
| 321 | |
| 322 | default: |
| 323 | /* This is just a dummy call to avoid letting the compiler |
| 324 | * evict __bpf_call_base() as an optimization. Placed here |
| 325 | * where no-one bothers. |
| 326 | */ |
| 327 | BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | *insnp = insn; |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | /** |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 336 | * bpf_convert_filter - convert filter program |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 337 | * @prog: the user passed filter program |
| 338 | * @len: the length of the user passed filter program |
| 339 | * @new_prog: buffer where converted program will be stored |
| 340 | * @new_len: pointer to store length of converted program |
| 341 | * |
| 342 | * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style. |
| 343 | * Conversion workflow: |
| 344 | * |
| 345 | * 1) First pass for calculating the new program length: |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 346 | * bpf_convert_filter(old_prog, old_len, NULL, &new_len) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 347 | * |
| 348 | * 2) 2nd pass to remap in two passes: 1st pass finds new |
| 349 | * jump offsets, 2nd pass remapping: |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 350 | * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len); |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 351 | * bpf_convert_filter(old_prog, old_len, new_prog, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 352 | */ |
Nicolas Schichan | d9e12f4 | 2015-05-06 16:12:28 +0200 | [diff] [blame] | 353 | static int bpf_convert_filter(struct sock_filter *prog, int len, |
| 354 | struct bpf_insn *new_prog, int *new_len) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 355 | { |
| 356 | int new_flen = 0, pass = 0, target, i; |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 357 | struct bpf_insn *new_insn; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 358 | struct sock_filter *fp; |
| 359 | int *addrs = NULL; |
| 360 | u8 bpf_src; |
| 361 | |
| 362 | BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); |
Daniel Borkmann | 3074383 | 2014-05-01 18:34:19 +0200 | [diff] [blame] | 363 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 364 | |
Kees Cook | 6f9a093 | 2014-06-18 15:34:57 -0700 | [diff] [blame] | 365 | if (len <= 0 || len > BPF_MAXINSNS) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 366 | return -EINVAL; |
| 367 | |
| 368 | if (new_prog) { |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 369 | addrs = kcalloc(len, sizeof(*addrs), |
| 370 | GFP_KERNEL | __GFP_NOWARN); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 371 | if (!addrs) |
| 372 | return -ENOMEM; |
| 373 | } |
| 374 | |
| 375 | do_pass: |
| 376 | new_insn = new_prog; |
| 377 | fp = prog; |
| 378 | |
Daniel Borkmann | 8b614ae | 2015-12-17 23:51:54 +0100 | [diff] [blame] | 379 | /* Classic BPF related prologue emission. */ |
| 380 | if (new_insn) { |
| 381 | /* Classic BPF expects A and X to be reset first. These need |
| 382 | * to be guaranteed to be the first two instructions. |
| 383 | */ |
| 384 | *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); |
| 385 | *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); |
| 386 | |
| 387 | /* All programs must keep CTX in callee saved BPF_REG_CTX. |
| 388 | * In eBPF case it's done by the compiler, here we need to |
| 389 | * do this ourself. Initial CTX is present in BPF_REG_ARG1. |
| 390 | */ |
| 391 | *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); |
| 392 | } else { |
| 393 | new_insn += 3; |
| 394 | } |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 395 | |
| 396 | for (i = 0; i < len; fp++, i++) { |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 397 | struct bpf_insn tmp_insns[6] = { }; |
| 398 | struct bpf_insn *insn = tmp_insns; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 399 | |
| 400 | if (addrs) |
| 401 | addrs[i] = new_insn - new_prog; |
| 402 | |
| 403 | switch (fp->code) { |
| 404 | /* All arithmetic insns and skb loads map as-is. */ |
| 405 | case BPF_ALU | BPF_ADD | BPF_X: |
| 406 | case BPF_ALU | BPF_ADD | BPF_K: |
| 407 | case BPF_ALU | BPF_SUB | BPF_X: |
| 408 | case BPF_ALU | BPF_SUB | BPF_K: |
| 409 | case BPF_ALU | BPF_AND | BPF_X: |
| 410 | case BPF_ALU | BPF_AND | BPF_K: |
| 411 | case BPF_ALU | BPF_OR | BPF_X: |
| 412 | case BPF_ALU | BPF_OR | BPF_K: |
| 413 | case BPF_ALU | BPF_LSH | BPF_X: |
| 414 | case BPF_ALU | BPF_LSH | BPF_K: |
| 415 | case BPF_ALU | BPF_RSH | BPF_X: |
| 416 | case BPF_ALU | BPF_RSH | BPF_K: |
| 417 | case BPF_ALU | BPF_XOR | BPF_X: |
| 418 | case BPF_ALU | BPF_XOR | BPF_K: |
| 419 | case BPF_ALU | BPF_MUL | BPF_X: |
| 420 | case BPF_ALU | BPF_MUL | BPF_K: |
| 421 | case BPF_ALU | BPF_DIV | BPF_X: |
| 422 | case BPF_ALU | BPF_DIV | BPF_K: |
| 423 | case BPF_ALU | BPF_MOD | BPF_X: |
| 424 | case BPF_ALU | BPF_MOD | BPF_K: |
| 425 | case BPF_ALU | BPF_NEG: |
| 426 | case BPF_LD | BPF_ABS | BPF_W: |
| 427 | case BPF_LD | BPF_ABS | BPF_H: |
| 428 | case BPF_LD | BPF_ABS | BPF_B: |
| 429 | case BPF_LD | BPF_IND | BPF_W: |
| 430 | case BPF_LD | BPF_IND | BPF_H: |
| 431 | case BPF_LD | BPF_IND | BPF_B: |
| 432 | /* Check for overloaded BPF extension and |
| 433 | * directly convert it if found, otherwise |
| 434 | * just move on with mapping. |
| 435 | */ |
| 436 | if (BPF_CLASS(fp->code) == BPF_LD && |
| 437 | BPF_MODE(fp->code) == BPF_ABS && |
| 438 | convert_bpf_extensions(fp, &insn)) |
| 439 | break; |
| 440 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 441 | *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] | 442 | break; |
| 443 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 444 | /* Jump transformation cannot use BPF block macros |
| 445 | * everywhere as offset calculation and target updates |
| 446 | * require a bit more work than the rest, i.e. jump |
| 447 | * opcodes map as-is, but offsets need adjustment. |
| 448 | */ |
| 449 | |
| 450 | #define BPF_EMIT_JMP \ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 451 | do { \ |
| 452 | if (target >= len || target < 0) \ |
| 453 | goto err; \ |
| 454 | insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ |
| 455 | /* Adjust pc relative offset for 2nd or 3rd insn. */ \ |
| 456 | insn->off -= insn - tmp_insns; \ |
| 457 | } while (0) |
| 458 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 459 | case BPF_JMP | BPF_JA: |
| 460 | target = i + fp->k + 1; |
| 461 | insn->code = fp->code; |
| 462 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 466 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 467 | case BPF_JMP | BPF_JSET | BPF_K: |
| 468 | case BPF_JMP | BPF_JSET | BPF_X: |
| 469 | case BPF_JMP | BPF_JGT | BPF_K: |
| 470 | case BPF_JMP | BPF_JGT | BPF_X: |
| 471 | case BPF_JMP | BPF_JGE | BPF_K: |
| 472 | case BPF_JMP | BPF_JGE | BPF_X: |
| 473 | if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { |
| 474 | /* BPF immediates are signed, zero extend |
| 475 | * immediate into tmp register and use it |
| 476 | * in compare insn. |
| 477 | */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 478 | *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 479 | |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 480 | insn->dst_reg = BPF_REG_A; |
| 481 | insn->src_reg = BPF_REG_TMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 482 | bpf_src = BPF_X; |
| 483 | } else { |
Alexei Starovoitov | e430f34 | 2014-06-06 14:46:06 -0700 | [diff] [blame] | 484 | insn->dst_reg = BPF_REG_A; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 485 | insn->imm = fp->k; |
| 486 | bpf_src = BPF_SRC(fp->code); |
Tycho Andersen | 19539ce | 2015-09-10 18:25:07 -0600 | [diff] [blame] | 487 | insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /* Common case where 'jump_false' is next insn. */ |
| 491 | if (fp->jf == 0) { |
| 492 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; |
| 493 | target = i + fp->jt + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 494 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | |
| 498 | /* Convert JEQ into JNE when 'jump_true' is next insn. */ |
| 499 | if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) { |
| 500 | insn->code = BPF_JMP | BPF_JNE | bpf_src; |
| 501 | target = i + fp->jf + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 502 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | |
| 506 | /* Other jumps are mapped into two insns: Jxx and JA. */ |
| 507 | target = i + fp->jt + 1; |
| 508 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 509 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 510 | insn++; |
| 511 | |
| 512 | insn->code = BPF_JMP | BPF_JA; |
| 513 | target = i + fp->jf + 1; |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 514 | BPF_EMIT_JMP; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 515 | break; |
| 516 | |
| 517 | /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ |
| 518 | case BPF_LDX | BPF_MSH | BPF_B: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 519 | /* tmp = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 520 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A); |
David S. Miller | 1268e25 | 2014-05-13 13:13:33 -0400 | [diff] [blame] | 521 | /* A = BPF_R0 = *(u8 *) (skb->data + K) */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 522 | *insn++ = BPF_LD_ABS(BPF_B, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 523 | /* A &= 0xf */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 524 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 525 | /* A <<= 2 */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 526 | *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 527 | /* X = A */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 528 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 529 | /* A = tmp */ |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 530 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 531 | break; |
| 532 | |
Daniel Borkmann | 6205b9c | 2016-02-19 23:05:27 +0100 | [diff] [blame] | 533 | /* RET_K is remaped into 2 insns. RET_A case doesn't need an |
| 534 | * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. |
| 535 | */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 536 | case BPF_RET | BPF_A: |
| 537 | case BPF_RET | BPF_K: |
Daniel Borkmann | 6205b9c | 2016-02-19 23:05:27 +0100 | [diff] [blame] | 538 | if (BPF_RVAL(fp->code) == BPF_K) |
| 539 | *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, |
| 540 | 0, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 541 | *insn = BPF_EXIT_INSN(); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 542 | break; |
| 543 | |
| 544 | /* Store to stack. */ |
| 545 | case BPF_ST: |
| 546 | case BPF_STX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 547 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == |
| 548 | BPF_ST ? BPF_REG_A : BPF_REG_X, |
| 549 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | /* Load from stack. */ |
| 553 | case BPF_LD | BPF_MEM: |
| 554 | case BPF_LDX | BPF_MEM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 555 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 556 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, |
| 557 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | /* A = K or X = K */ |
| 561 | case BPF_LD | BPF_IMM: |
| 562 | case BPF_LDX | BPF_IMM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 563 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? |
| 564 | BPF_REG_A : BPF_REG_X, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 565 | break; |
| 566 | |
| 567 | /* X = A */ |
| 568 | case BPF_MISC | BPF_TAX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 569 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | /* A = X */ |
| 573 | case BPF_MISC | BPF_TXA: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 574 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 575 | break; |
| 576 | |
| 577 | /* A = skb->len or X = skb->len */ |
| 578 | case BPF_LD | BPF_W | BPF_LEN: |
| 579 | case BPF_LDX | BPF_W | BPF_LEN: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 580 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 581 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, |
| 582 | offsetof(struct sk_buff, len)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 583 | break; |
| 584 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 585 | /* Access seccomp_data fields. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 586 | case BPF_LDX | BPF_ABS | BPF_W: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 587 | /* A = *(u32 *) (ctx + K) */ |
| 588 | *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] | 589 | break; |
| 590 | |
Stephen Hemminger | ca9f1fd | 2015-02-14 13:47:54 -0500 | [diff] [blame] | 591 | /* Unknown instruction. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 592 | default: |
| 593 | goto err; |
| 594 | } |
| 595 | |
| 596 | insn++; |
| 597 | if (new_prog) |
| 598 | memcpy(new_insn, tmp_insns, |
| 599 | sizeof(*insn) * (insn - tmp_insns)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 600 | new_insn += insn - tmp_insns; |
| 601 | } |
| 602 | |
| 603 | if (!new_prog) { |
| 604 | /* Only calculating new length. */ |
| 605 | *new_len = new_insn - new_prog; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | pass++; |
| 610 | if (new_flen != new_insn - new_prog) { |
| 611 | new_flen = new_insn - new_prog; |
| 612 | if (pass > 2) |
| 613 | goto err; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 614 | goto do_pass; |
| 615 | } |
| 616 | |
| 617 | kfree(addrs); |
| 618 | BUG_ON(*new_len != new_flen); |
| 619 | return 0; |
| 620 | err: |
| 621 | kfree(addrs); |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | |
| 625 | /* Security: |
| 626 | * |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 627 | * 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] | 628 | * __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] | 629 | * 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] | 630 | * a malicious user doesn't try to abuse us. |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 631 | */ |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 632 | 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] | 633 | { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 634 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 635 | int pc, ret = 0; |
| 636 | |
| 637 | BUILD_BUG_ON(BPF_MEMWORDS > 16); |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 638 | |
Tobias Klauser | 99e72a0 | 2014-06-24 15:33:22 +0200 | [diff] [blame] | 639 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 640 | if (!masks) |
| 641 | return -ENOMEM; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 642 | |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 643 | memset(masks, 0xff, flen * sizeof(*masks)); |
| 644 | |
| 645 | for (pc = 0; pc < flen; pc++) { |
| 646 | memvalid &= masks[pc]; |
| 647 | |
| 648 | switch (filter[pc].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 649 | case BPF_ST: |
| 650 | case BPF_STX: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 651 | memvalid |= (1 << filter[pc].k); |
| 652 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 653 | case BPF_LD | BPF_MEM: |
| 654 | case BPF_LDX | BPF_MEM: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 655 | if (!(memvalid & (1 << filter[pc].k))) { |
| 656 | ret = -EINVAL; |
| 657 | goto error; |
| 658 | } |
| 659 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 660 | case BPF_JMP | BPF_JA: |
| 661 | /* A jump must set masks on target */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 662 | masks[pc + 1 + filter[pc].k] &= memvalid; |
| 663 | memvalid = ~0; |
| 664 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 665 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 666 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 667 | case BPF_JMP | BPF_JGE | BPF_K: |
| 668 | case BPF_JMP | BPF_JGE | BPF_X: |
| 669 | case BPF_JMP | BPF_JGT | BPF_K: |
| 670 | case BPF_JMP | BPF_JGT | BPF_X: |
| 671 | case BPF_JMP | BPF_JSET | BPF_K: |
| 672 | case BPF_JMP | BPF_JSET | BPF_X: |
| 673 | /* A jump must set masks on targets */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 674 | masks[pc + 1 + filter[pc].jt] &= memvalid; |
| 675 | masks[pc + 1 + filter[pc].jf] &= memvalid; |
| 676 | memvalid = ~0; |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | error: |
| 681 | kfree(masks); |
| 682 | return ret; |
| 683 | } |
| 684 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 685 | static bool chk_code_allowed(u16 code_to_probe) |
| 686 | { |
| 687 | static const bool codes[] = { |
| 688 | /* 32 bit ALU operations */ |
| 689 | [BPF_ALU | BPF_ADD | BPF_K] = true, |
| 690 | [BPF_ALU | BPF_ADD | BPF_X] = true, |
| 691 | [BPF_ALU | BPF_SUB | BPF_K] = true, |
| 692 | [BPF_ALU | BPF_SUB | BPF_X] = true, |
| 693 | [BPF_ALU | BPF_MUL | BPF_K] = true, |
| 694 | [BPF_ALU | BPF_MUL | BPF_X] = true, |
| 695 | [BPF_ALU | BPF_DIV | BPF_K] = true, |
| 696 | [BPF_ALU | BPF_DIV | BPF_X] = true, |
| 697 | [BPF_ALU | BPF_MOD | BPF_K] = true, |
| 698 | [BPF_ALU | BPF_MOD | BPF_X] = true, |
| 699 | [BPF_ALU | BPF_AND | BPF_K] = true, |
| 700 | [BPF_ALU | BPF_AND | BPF_X] = true, |
| 701 | [BPF_ALU | BPF_OR | BPF_K] = true, |
| 702 | [BPF_ALU | BPF_OR | BPF_X] = true, |
| 703 | [BPF_ALU | BPF_XOR | BPF_K] = true, |
| 704 | [BPF_ALU | BPF_XOR | BPF_X] = true, |
| 705 | [BPF_ALU | BPF_LSH | BPF_K] = true, |
| 706 | [BPF_ALU | BPF_LSH | BPF_X] = true, |
| 707 | [BPF_ALU | BPF_RSH | BPF_K] = true, |
| 708 | [BPF_ALU | BPF_RSH | BPF_X] = true, |
| 709 | [BPF_ALU | BPF_NEG] = true, |
| 710 | /* Load instructions */ |
| 711 | [BPF_LD | BPF_W | BPF_ABS] = true, |
| 712 | [BPF_LD | BPF_H | BPF_ABS] = true, |
| 713 | [BPF_LD | BPF_B | BPF_ABS] = true, |
| 714 | [BPF_LD | BPF_W | BPF_LEN] = true, |
| 715 | [BPF_LD | BPF_W | BPF_IND] = true, |
| 716 | [BPF_LD | BPF_H | BPF_IND] = true, |
| 717 | [BPF_LD | BPF_B | BPF_IND] = true, |
| 718 | [BPF_LD | BPF_IMM] = true, |
| 719 | [BPF_LD | BPF_MEM] = true, |
| 720 | [BPF_LDX | BPF_W | BPF_LEN] = true, |
| 721 | [BPF_LDX | BPF_B | BPF_MSH] = true, |
| 722 | [BPF_LDX | BPF_IMM] = true, |
| 723 | [BPF_LDX | BPF_MEM] = true, |
| 724 | /* Store instructions */ |
| 725 | [BPF_ST] = true, |
| 726 | [BPF_STX] = true, |
| 727 | /* Misc instructions */ |
| 728 | [BPF_MISC | BPF_TAX] = true, |
| 729 | [BPF_MISC | BPF_TXA] = true, |
| 730 | /* Return instructions */ |
| 731 | [BPF_RET | BPF_K] = true, |
| 732 | [BPF_RET | BPF_A] = true, |
| 733 | /* Jump instructions */ |
| 734 | [BPF_JMP | BPF_JA] = true, |
| 735 | [BPF_JMP | BPF_JEQ | BPF_K] = true, |
| 736 | [BPF_JMP | BPF_JEQ | BPF_X] = true, |
| 737 | [BPF_JMP | BPF_JGE | BPF_K] = true, |
| 738 | [BPF_JMP | BPF_JGE | BPF_X] = true, |
| 739 | [BPF_JMP | BPF_JGT | BPF_K] = true, |
| 740 | [BPF_JMP | BPF_JGT | BPF_X] = true, |
| 741 | [BPF_JMP | BPF_JSET | BPF_K] = true, |
| 742 | [BPF_JMP | BPF_JSET | BPF_X] = true, |
| 743 | }; |
| 744 | |
| 745 | if (code_to_probe >= ARRAY_SIZE(codes)) |
| 746 | return false; |
| 747 | |
| 748 | return codes[code_to_probe]; |
| 749 | } |
| 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | /** |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 752 | * bpf_check_classic - verify socket filter code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | * @filter: filter to verify |
| 754 | * @flen: length of filter |
| 755 | * |
| 756 | * Check the user's filter code. If we let some ugly |
| 757 | * filter code slip through kaboom! The filter must contain |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 758 | * no references or jumps that are out of range, no illegal |
| 759 | * instructions, and must end with a RET instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * |
Kris Katterjohn | 7b11f69 | 2006-01-13 14:33:06 -0800 | [diff] [blame] | 761 | * All jumps are forward as they are not signed. |
| 762 | * |
| 763 | * Returns 0 if the rule set is legal or -EINVAL if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | */ |
Nicolas Schichan | d9e12f4 | 2015-05-06 16:12:28 +0200 | [diff] [blame] | 765 | static int bpf_check_classic(const struct sock_filter *filter, |
| 766 | unsigned int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 768 | bool anc_found; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 769 | int pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
David S. Miller | 1b93ae64 | 2005-12-27 13:57:59 -0800 | [diff] [blame] | 771 | if (flen == 0 || flen > BPF_MAXINSNS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | return -EINVAL; |
| 773 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 774 | /* Check the filter code now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | for (pc = 0; pc < flen; pc++) { |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 776 | const struct sock_filter *ftest = &filter[pc]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 778 | /* May we actually operate on this code? */ |
| 779 | if (!chk_code_allowed(ftest->code)) |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 780 | return -EINVAL; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 781 | |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 782 | /* Some instructions need special checks */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 783 | switch (ftest->code) { |
| 784 | case BPF_ALU | BPF_DIV | BPF_K: |
| 785 | case BPF_ALU | BPF_MOD | BPF_K: |
| 786 | /* Check for division by zero */ |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame] | 787 | if (ftest->k == 0) |
| 788 | return -EINVAL; |
| 789 | break; |
Rabin Vincent | 229394e | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 790 | case BPF_ALU | BPF_LSH | BPF_K: |
| 791 | case BPF_ALU | BPF_RSH | BPF_K: |
| 792 | if (ftest->k >= 32) |
| 793 | return -EINVAL; |
| 794 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 795 | case BPF_LD | BPF_MEM: |
| 796 | case BPF_LDX | BPF_MEM: |
| 797 | case BPF_ST: |
| 798 | case BPF_STX: |
| 799 | /* Check for invalid memory addresses */ |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 800 | if (ftest->k >= BPF_MEMWORDS) |
| 801 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 802 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 803 | case BPF_JMP | BPF_JA: |
| 804 | /* Note, the large ftest->k might cause loops. |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 805 | * Compare this with conditional jumps below, |
| 806 | * where offsets are limited. --ANK (981016) |
| 807 | */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 808 | if (ftest->k >= (unsigned int)(flen - pc - 1)) |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 809 | return -EINVAL; |
| 810 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 811 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 812 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 813 | case BPF_JMP | BPF_JGE | BPF_K: |
| 814 | case BPF_JMP | BPF_JGE | BPF_X: |
| 815 | case BPF_JMP | BPF_JGT | BPF_K: |
| 816 | case BPF_JMP | BPF_JGT | BPF_X: |
| 817 | case BPF_JMP | BPF_JSET | BPF_K: |
| 818 | case BPF_JMP | BPF_JSET | BPF_X: |
| 819 | /* Both conditionals must be safe */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 820 | if (pc + ftest->jt + 1 >= flen || |
| 821 | pc + ftest->jf + 1 >= flen) |
| 822 | return -EINVAL; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 823 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 824 | case BPF_LD | BPF_W | BPF_ABS: |
| 825 | case BPF_LD | BPF_H | BPF_ABS: |
| 826 | case BPF_LD | BPF_B | BPF_ABS: |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 827 | anc_found = false; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 828 | if (bpf_anc_helper(ftest) & BPF_ANC) |
| 829 | anc_found = true; |
| 830 | /* Ancillary operation unknown or unsupported */ |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 831 | if (anc_found == false && ftest->k >= SKF_AD_OFF) |
| 832 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 836 | /* Last instruction must be a RET code */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 837 | switch (filter[flen - 1].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 838 | case BPF_RET | BPF_K: |
| 839 | case BPF_RET | BPF_A: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 840 | return check_load_and_stores(filter, flen); |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 841 | } |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 842 | |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 843 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 846 | static int bpf_prog_store_orig_filter(struct bpf_prog *fp, |
| 847 | const struct sock_fprog *fprog) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 848 | { |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 849 | unsigned int fsize = bpf_classic_proglen(fprog); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 850 | struct sock_fprog_kern *fkprog; |
| 851 | |
| 852 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); |
| 853 | if (!fp->orig_prog) |
| 854 | return -ENOMEM; |
| 855 | |
| 856 | fkprog = fp->orig_prog; |
| 857 | fkprog->len = fprog->len; |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 858 | |
| 859 | fkprog->filter = kmemdup(fp->insns, fsize, |
| 860 | GFP_KERNEL | __GFP_NOWARN); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 861 | if (!fkprog->filter) { |
| 862 | kfree(fp->orig_prog); |
| 863 | return -ENOMEM; |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 869 | static void bpf_release_orig_filter(struct bpf_prog *fp) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 870 | { |
| 871 | struct sock_fprog_kern *fprog = fp->orig_prog; |
| 872 | |
| 873 | if (fprog) { |
| 874 | kfree(fprog->filter); |
| 875 | kfree(fprog); |
| 876 | } |
| 877 | } |
| 878 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 879 | static void __bpf_prog_release(struct bpf_prog *prog) |
| 880 | { |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 881 | if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 882 | bpf_prog_put(prog); |
| 883 | } else { |
| 884 | bpf_release_orig_filter(prog); |
| 885 | bpf_prog_free(prog); |
| 886 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 889 | static void __sk_filter_release(struct sk_filter *fp) |
| 890 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 891 | __bpf_prog_release(fp->prog); |
| 892 | kfree(fp); |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 893 | } |
| 894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | /** |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 896 | * sk_filter_release_rcu - Release a socket filter by rcu_head |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 897 | * @rcu: rcu_head that contains the sk_filter to free |
| 898 | */ |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 899 | static void sk_filter_release_rcu(struct rcu_head *rcu) |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 900 | { |
| 901 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); |
| 902 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 903 | __sk_filter_release(fp); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 904 | } |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 905 | |
| 906 | /** |
| 907 | * sk_filter_release - release a socket filter |
| 908 | * @fp: filter to remove |
| 909 | * |
| 910 | * Remove a filter from a socket and release its resources. |
| 911 | */ |
| 912 | static void sk_filter_release(struct sk_filter *fp) |
| 913 | { |
| 914 | if (atomic_dec_and_test(&fp->refcnt)) |
| 915 | call_rcu(&fp->rcu, sk_filter_release_rcu); |
| 916 | } |
| 917 | |
| 918 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) |
| 919 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 920 | u32 filter_size = bpf_prog_size(fp->prog->len); |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 921 | |
| 922 | atomic_sub(filter_size, &sk->sk_omem_alloc); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 923 | sk_filter_release(fp); |
| 924 | } |
| 925 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 926 | /* try to charge the socket memory if there is space available |
| 927 | * return true on success |
| 928 | */ |
| 929 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 930 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 931 | u32 filter_size = bpf_prog_size(fp->prog->len); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 932 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 933 | /* same check as in sock_kmalloc() */ |
| 934 | if (filter_size <= sysctl_optmem_max && |
| 935 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { |
| 936 | atomic_inc(&fp->refcnt); |
| 937 | atomic_add(filter_size, &sk->sk_omem_alloc); |
| 938 | return true; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 939 | } |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 940 | return false; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 943 | static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 944 | { |
| 945 | struct sock_filter *old_prog; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 946 | struct bpf_prog *old_fp; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 947 | int err, new_len, old_len = fp->len; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 948 | |
| 949 | /* We are free to overwrite insns et al right here as it |
| 950 | * won't be used at this point in time anymore internally |
| 951 | * after the migration to the internal BPF instruction |
| 952 | * representation. |
| 953 | */ |
| 954 | BUILD_BUG_ON(sizeof(struct sock_filter) != |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 955 | sizeof(struct bpf_insn)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 956 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 957 | /* Conversion cannot happen on overlapping memory areas, |
| 958 | * so we need to keep the user BPF around until the 2nd |
| 959 | * pass. At this time, the user BPF is stored in fp->insns. |
| 960 | */ |
| 961 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 962 | GFP_KERNEL | __GFP_NOWARN); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 963 | if (!old_prog) { |
| 964 | err = -ENOMEM; |
| 965 | goto out_err; |
| 966 | } |
| 967 | |
| 968 | /* 1st pass: calculate the new program length. */ |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 969 | err = bpf_convert_filter(old_prog, old_len, NULL, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 970 | if (err) |
| 971 | goto out_err_free; |
| 972 | |
| 973 | /* Expand fp for appending the new filter representation. */ |
| 974 | old_fp = fp; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 975 | fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 976 | if (!fp) { |
| 977 | /* The old_fp is still around in case we couldn't |
| 978 | * allocate new memory, so uncharge on that one. |
| 979 | */ |
| 980 | fp = old_fp; |
| 981 | err = -ENOMEM; |
| 982 | goto out_err_free; |
| 983 | } |
| 984 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 985 | fp->len = new_len; |
| 986 | |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 987 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 988 | err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 989 | if (err) |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 990 | /* 2nd bpf_convert_filter() can fail only if it fails |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 991 | * to allocate memory, remapping must succeed. Note, |
| 992 | * that at this time old_fp has already been released |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 993 | * by krealloc(). |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 994 | */ |
| 995 | goto out_err_free; |
| 996 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 997 | bpf_prog_select_runtime(fp); |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 998 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 999 | kfree(old_prog); |
| 1000 | return fp; |
| 1001 | |
| 1002 | out_err_free: |
| 1003 | kfree(old_prog); |
| 1004 | out_err: |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1005 | __bpf_prog_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1006 | return ERR_PTR(err); |
| 1007 | } |
| 1008 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1009 | static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, |
| 1010 | bpf_aux_classic_check_t trans) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1011 | { |
| 1012 | int err; |
| 1013 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1014 | fp->bpf_func = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1015 | fp->jited = 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1016 | |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 1017 | err = bpf_check_classic(fp->insns, fp->len); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 1018 | if (err) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1019 | __bpf_prog_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1020 | return ERR_PTR(err); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 1021 | } |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1022 | |
Nicolas Schichan | 4ae92bc | 2015-05-06 16:12:27 +0200 | [diff] [blame] | 1023 | /* There might be additional checks and transformations |
| 1024 | * needed on classic filters, f.e. in case of seccomp. |
| 1025 | */ |
| 1026 | if (trans) { |
| 1027 | err = trans(fp->insns, fp->len); |
| 1028 | if (err) { |
| 1029 | __bpf_prog_release(fp); |
| 1030 | return ERR_PTR(err); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1034 | /* Probe if we can JIT compile the filter and if so, do |
| 1035 | * the compilation of the filter. |
| 1036 | */ |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1037 | bpf_jit_compile(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1038 | |
| 1039 | /* JIT compiler couldn't process this filter, so do the |
| 1040 | * internal BPF translation for the optimized interpreter. |
| 1041 | */ |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 1042 | if (!fp->jited) |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1043 | fp = bpf_migrate_filter(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1044 | |
| 1045 | return fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /** |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1049 | * bpf_prog_create - create an unattached filter |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 1050 | * @pfp: the unattached filter that is created |
Tobias Klauser | 677a9fd | 2014-06-24 15:33:21 +0200 | [diff] [blame] | 1051 | * @fprog: the filter program |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1052 | * |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 1053 | * Create a filter independent of any socket. We first run some |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1054 | * sanity checks on it to make sure it does not explode on us later. |
| 1055 | * If an error occurs or there is insufficient memory for the filter |
| 1056 | * a negative errno code is returned. On success the return is zero. |
| 1057 | */ |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1058 | 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] | 1059 | { |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 1060 | unsigned int fsize = bpf_classic_proglen(fprog); |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1061 | struct bpf_prog *fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* Make sure new filter is there and in the right amounts. */ |
| 1064 | if (fprog->filter == NULL) |
| 1065 | return -EINVAL; |
| 1066 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1067 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1068 | if (!fp) |
| 1069 | return -ENOMEM; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1070 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1071 | memcpy(fp->insns, fprog->filter, fsize); |
| 1072 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1073 | fp->len = fprog->len; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1074 | /* Since unattached filters are not copied back to user |
| 1075 | * space through sk_get_filter(), we do not need to hold |
| 1076 | * a copy here, and can spare us the work. |
| 1077 | */ |
| 1078 | fp->orig_prog = NULL; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1079 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1080 | /* bpf_prepare_filter() already takes care of freeing |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1081 | * memory in case something goes wrong. |
| 1082 | */ |
Nicolas Schichan | 4ae92bc | 2015-05-06 16:12:27 +0200 | [diff] [blame] | 1083 | fp = bpf_prepare_filter(fp, NULL); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1084 | if (IS_ERR(fp)) |
| 1085 | return PTR_ERR(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1086 | |
| 1087 | *pfp = fp; |
| 1088 | return 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1089 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1090 | EXPORT_SYMBOL_GPL(bpf_prog_create); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1091 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1092 | /** |
| 1093 | * bpf_prog_create_from_user - create an unattached filter from user buffer |
| 1094 | * @pfp: the unattached filter that is created |
| 1095 | * @fprog: the filter program |
| 1096 | * @trans: post-classic verifier transformation handler |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1097 | * @save_orig: save classic BPF program |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1098 | * |
| 1099 | * This function effectively does the same as bpf_prog_create(), only |
| 1100 | * that it builds up its insns buffer from user space provided buffer. |
| 1101 | * It also allows for passing a bpf_aux_classic_check_t handler. |
| 1102 | */ |
| 1103 | 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] | 1104 | bpf_aux_classic_check_t trans, bool save_orig) |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1105 | { |
| 1106 | unsigned int fsize = bpf_classic_proglen(fprog); |
| 1107 | struct bpf_prog *fp; |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1108 | int err; |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1109 | |
| 1110 | /* Make sure new filter is there and in the right amounts. */ |
| 1111 | if (fprog->filter == NULL) |
| 1112 | return -EINVAL; |
| 1113 | |
| 1114 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
| 1115 | if (!fp) |
| 1116 | return -ENOMEM; |
| 1117 | |
| 1118 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { |
| 1119 | __bpf_prog_free(fp); |
| 1120 | return -EFAULT; |
| 1121 | } |
| 1122 | |
| 1123 | fp->len = fprog->len; |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1124 | fp->orig_prog = NULL; |
| 1125 | |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1126 | if (save_orig) { |
| 1127 | err = bpf_prog_store_orig_filter(fp, fprog); |
| 1128 | if (err) { |
| 1129 | __bpf_prog_free(fp); |
| 1130 | return -ENOMEM; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1134 | /* bpf_prepare_filter() already takes care of freeing |
| 1135 | * memory in case something goes wrong. |
| 1136 | */ |
| 1137 | fp = bpf_prepare_filter(fp, trans); |
| 1138 | if (IS_ERR(fp)) |
| 1139 | return PTR_ERR(fp); |
| 1140 | |
| 1141 | *pfp = fp; |
| 1142 | return 0; |
| 1143 | } |
David S. Miller | 2ea273d | 2015-08-17 14:37:06 -0700 | [diff] [blame] | 1144 | EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1145 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1146 | void bpf_prog_destroy(struct bpf_prog *fp) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1147 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1148 | __bpf_prog_release(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1149 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1150 | EXPORT_SYMBOL_GPL(bpf_prog_destroy); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1151 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1152 | static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) |
| 1153 | { |
| 1154 | struct sk_filter *fp, *old_fp; |
| 1155 | |
| 1156 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); |
| 1157 | if (!fp) |
| 1158 | return -ENOMEM; |
| 1159 | |
| 1160 | fp->prog = prog; |
| 1161 | atomic_set(&fp->refcnt, 0); |
| 1162 | |
| 1163 | if (!sk_filter_charge(sk, fp)) { |
| 1164 | kfree(fp); |
| 1165 | return -ENOMEM; |
| 1166 | } |
| 1167 | |
| 1168 | old_fp = rcu_dereference_protected(sk->sk_filter, |
| 1169 | sock_owned_by_user(sk)); |
| 1170 | rcu_assign_pointer(sk->sk_filter, fp); |
| 1171 | |
| 1172 | if (old_fp) |
| 1173 | sk_filter_uncharge(sk, old_fp); |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1178 | static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk) |
| 1179 | { |
| 1180 | struct bpf_prog *old_prog; |
| 1181 | int err; |
| 1182 | |
| 1183 | if (bpf_prog_size(prog->len) > sysctl_optmem_max) |
| 1184 | return -ENOMEM; |
| 1185 | |
Craig Gallek | fa46349 | 2016-02-10 11:50:39 -0500 | [diff] [blame] | 1186 | if (sk_unhashed(sk) && sk->sk_reuseport) { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1187 | err = reuseport_alloc(sk); |
| 1188 | if (err) |
| 1189 | return err; |
| 1190 | } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) { |
| 1191 | /* The socket wasn't bound with SO_REUSEPORT */ |
| 1192 | return -EINVAL; |
| 1193 | } |
| 1194 | |
| 1195 | old_prog = reuseport_attach_prog(sk, prog); |
| 1196 | if (old_prog) |
| 1197 | bpf_prog_destroy(old_prog); |
| 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | static |
| 1203 | struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) |
| 1204 | { |
| 1205 | unsigned int fsize = bpf_classic_proglen(fprog); |
| 1206 | unsigned int bpf_fsize = bpf_prog_size(fprog->len); |
| 1207 | struct bpf_prog *prog; |
| 1208 | int err; |
| 1209 | |
| 1210 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1211 | return ERR_PTR(-EPERM); |
| 1212 | |
| 1213 | /* Make sure new filter is there and in the right amounts. */ |
| 1214 | if (fprog->filter == NULL) |
| 1215 | return ERR_PTR(-EINVAL); |
| 1216 | |
| 1217 | prog = bpf_prog_alloc(bpf_fsize, 0); |
| 1218 | if (!prog) |
| 1219 | return ERR_PTR(-ENOMEM); |
| 1220 | |
| 1221 | if (copy_from_user(prog->insns, fprog->filter, fsize)) { |
| 1222 | __bpf_prog_free(prog); |
| 1223 | return ERR_PTR(-EFAULT); |
| 1224 | } |
| 1225 | |
| 1226 | prog->len = fprog->len; |
| 1227 | |
| 1228 | err = bpf_prog_store_orig_filter(prog, fprog); |
| 1229 | if (err) { |
| 1230 | __bpf_prog_free(prog); |
| 1231 | return ERR_PTR(-ENOMEM); |
| 1232 | } |
| 1233 | |
| 1234 | /* bpf_prepare_filter() already takes care of freeing |
| 1235 | * memory in case something goes wrong. |
| 1236 | */ |
| 1237 | return bpf_prepare_filter(prog, NULL); |
| 1238 | } |
| 1239 | |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 1240 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | * sk_attach_filter - attach a socket filter |
| 1242 | * @fprog: the filter program |
| 1243 | * @sk: the socket to use |
| 1244 | * |
| 1245 | * Attach the user's filter code. We first run some sanity checks on |
| 1246 | * it to make sure it does not explode on us later. If an error |
| 1247 | * occurs or there is insufficient memory for the filter a negative |
| 1248 | * errno code is returned. On success the return is zero. |
| 1249 | */ |
| 1250 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
| 1251 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1252 | struct bpf_prog *prog = __get_filter(fprog, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | int err; |
| 1254 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1255 | if (IS_ERR(prog)) |
| 1256 | return PTR_ERR(prog); |
| 1257 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1258 | err = __sk_attach_prog(prog, sk); |
| 1259 | if (err < 0) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1260 | __bpf_prog_release(prog); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1261 | return err; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1262 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1263 | |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1264 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 1266 | EXPORT_SYMBOL_GPL(sk_attach_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1268 | int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1269 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1270 | struct bpf_prog *prog = __get_filter(fprog, sk); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1271 | int err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1272 | |
Alexei Starovoitov | 198bf1b | 2014-12-10 20:14:55 -0800 | [diff] [blame] | 1273 | if (IS_ERR(prog)) |
| 1274 | return PTR_ERR(prog); |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1275 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1276 | err = __reuseport_attach_prog(prog, sk); |
| 1277 | if (err < 0) { |
| 1278 | __bpf_prog_release(prog); |
| 1279 | return err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) |
| 1286 | { |
| 1287 | struct bpf_prog *prog; |
| 1288 | |
| 1289 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1290 | return ERR_PTR(-EPERM); |
| 1291 | |
| 1292 | prog = bpf_prog_get(ufd); |
| 1293 | if (IS_ERR(prog)) |
| 1294 | return prog; |
| 1295 | |
| 1296 | if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) { |
| 1297 | bpf_prog_put(prog); |
| 1298 | return ERR_PTR(-EINVAL); |
| 1299 | } |
| 1300 | |
| 1301 | return prog; |
| 1302 | } |
| 1303 | |
| 1304 | int sk_attach_bpf(u32 ufd, struct sock *sk) |
| 1305 | { |
| 1306 | struct bpf_prog *prog = __get_bpf(ufd, sk); |
| 1307 | int err; |
| 1308 | |
| 1309 | if (IS_ERR(prog)) |
| 1310 | return PTR_ERR(prog); |
| 1311 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1312 | err = __sk_attach_prog(prog, sk); |
| 1313 | if (err < 0) { |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1314 | bpf_prog_put(prog); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1315 | return err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1316 | } |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1317 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1318 | return 0; |
| 1319 | } |
| 1320 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1321 | int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) |
| 1322 | { |
| 1323 | struct bpf_prog *prog = __get_bpf(ufd, sk); |
| 1324 | int err; |
| 1325 | |
| 1326 | if (IS_ERR(prog)) |
| 1327 | return PTR_ERR(prog); |
| 1328 | |
| 1329 | err = __reuseport_attach_prog(prog, sk); |
| 1330 | if (err < 0) { |
| 1331 | bpf_prog_put(prog); |
| 1332 | return err; |
| 1333 | } |
| 1334 | |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1338 | struct bpf_scratchpad { |
| 1339 | union { |
| 1340 | __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; |
| 1341 | u8 buff[MAX_BPF_STACK]; |
| 1342 | }; |
| 1343 | }; |
| 1344 | |
| 1345 | static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1346 | |
| 1347 | static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1348 | { |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1349 | struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1350 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1351 | int offset = (int) r2; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1352 | void *from = (void *) (long) r3; |
| 1353 | unsigned int len = (unsigned int) r4; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1354 | void *ptr; |
| 1355 | |
Daniel Borkmann | 8afd54c | 2016-03-04 15:15:03 +0100 | [diff] [blame^] | 1356 | if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1357 | return -EINVAL; |
| 1358 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1359 | /* bpf verifier guarantees that: |
| 1360 | * 'from' pointer points to bpf program stack |
| 1361 | * 'len' bytes of it were initialized |
| 1362 | * 'len' > 0 |
| 1363 | * 'skb' is a valid pointer to 'struct sk_buff' |
| 1364 | * |
| 1365 | * so check for invalid 'offset' and too large 'len' |
| 1366 | */ |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1367 | if (unlikely((u32) offset > 0xffff || len > sizeof(sp->buff))) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1368 | return -EFAULT; |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 1369 | if (unlikely(skb_try_make_writable(skb, offset + len))) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1370 | return -EFAULT; |
| 1371 | |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1372 | ptr = skb_header_pointer(skb, offset, len, sp->buff); |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1373 | if (unlikely(!ptr)) |
| 1374 | return -EFAULT; |
| 1375 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1376 | if (flags & BPF_F_RECOMPUTE_CSUM) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1377 | skb_postpull_rcsum(skb, ptr, len); |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1378 | |
| 1379 | memcpy(ptr, from, len); |
| 1380 | |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1381 | if (ptr == sp->buff) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1382 | /* skb_store_bits cannot return -EFAULT here */ |
| 1383 | skb_store_bits(skb, offset, ptr, len); |
| 1384 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1385 | if (flags & BPF_F_RECOMPUTE_CSUM) |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1386 | skb_postpush_rcsum(skb, ptr, len); |
Daniel Borkmann | 8afd54c | 2016-03-04 15:15:03 +0100 | [diff] [blame^] | 1387 | if (flags & BPF_F_INVALIDATE_HASH) |
| 1388 | skb_clear_hash(skb); |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1389 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | const struct bpf_func_proto bpf_skb_store_bytes_proto = { |
| 1394 | .func = bpf_skb_store_bytes, |
| 1395 | .gpl_only = false, |
| 1396 | .ret_type = RET_INTEGER, |
| 1397 | .arg1_type = ARG_PTR_TO_CTX, |
| 1398 | .arg2_type = ARG_ANYTHING, |
| 1399 | .arg3_type = ARG_PTR_TO_STACK, |
| 1400 | .arg4_type = ARG_CONST_STACK_SIZE, |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1401 | .arg5_type = ARG_ANYTHING, |
| 1402 | }; |
| 1403 | |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1404 | static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1405 | { |
| 1406 | const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1; |
| 1407 | int offset = (int) r2; |
| 1408 | void *to = (void *)(unsigned long) r3; |
| 1409 | unsigned int len = (unsigned int) r4; |
| 1410 | void *ptr; |
| 1411 | |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1412 | if (unlikely((u32) offset > 0xffff || len > MAX_BPF_STACK)) |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1413 | return -EFAULT; |
| 1414 | |
| 1415 | ptr = skb_header_pointer(skb, offset, len, to); |
| 1416 | if (unlikely(!ptr)) |
| 1417 | return -EFAULT; |
| 1418 | if (ptr != to) |
| 1419 | memcpy(to, ptr, len); |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | const struct bpf_func_proto bpf_skb_load_bytes_proto = { |
| 1425 | .func = bpf_skb_load_bytes, |
| 1426 | .gpl_only = false, |
| 1427 | .ret_type = RET_INTEGER, |
| 1428 | .arg1_type = ARG_PTR_TO_CTX, |
| 1429 | .arg2_type = ARG_ANYTHING, |
| 1430 | .arg3_type = ARG_PTR_TO_STACK, |
| 1431 | .arg4_type = ARG_CONST_STACK_SIZE, |
| 1432 | }; |
| 1433 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1434 | static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1435 | { |
| 1436 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1437 | int offset = (int) r2; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1438 | __sum16 sum, *ptr; |
| 1439 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1440 | if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) |
| 1441 | return -EINVAL; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1442 | if (unlikely((u32) offset > 0xffff)) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1443 | return -EFAULT; |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 1444 | if (unlikely(skb_try_make_writable(skb, offset + sizeof(sum)))) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1445 | return -EFAULT; |
| 1446 | |
| 1447 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); |
| 1448 | if (unlikely(!ptr)) |
| 1449 | return -EFAULT; |
| 1450 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1451 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
Daniel Borkmann | 8050c0f | 2016-03-04 15:15:02 +0100 | [diff] [blame] | 1452 | case 0: |
| 1453 | if (unlikely(from != 0)) |
| 1454 | return -EINVAL; |
| 1455 | |
| 1456 | csum_replace_by_diff(ptr, to); |
| 1457 | break; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1458 | case 2: |
| 1459 | csum_replace2(ptr, from, to); |
| 1460 | break; |
| 1461 | case 4: |
| 1462 | csum_replace4(ptr, from, to); |
| 1463 | break; |
| 1464 | default: |
| 1465 | return -EINVAL; |
| 1466 | } |
| 1467 | |
| 1468 | if (ptr == &sum) |
| 1469 | /* skb_store_bits guaranteed to not return -EFAULT here */ |
| 1470 | skb_store_bits(skb, offset, ptr, sizeof(sum)); |
| 1471 | |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | const struct bpf_func_proto bpf_l3_csum_replace_proto = { |
| 1476 | .func = bpf_l3_csum_replace, |
| 1477 | .gpl_only = false, |
| 1478 | .ret_type = RET_INTEGER, |
| 1479 | .arg1_type = ARG_PTR_TO_CTX, |
| 1480 | .arg2_type = ARG_ANYTHING, |
| 1481 | .arg3_type = ARG_ANYTHING, |
| 1482 | .arg4_type = ARG_ANYTHING, |
| 1483 | .arg5_type = ARG_ANYTHING, |
| 1484 | }; |
| 1485 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1486 | static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1487 | { |
| 1488 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1489 | bool is_pseudo = flags & BPF_F_PSEUDO_HDR; |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1490 | bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1491 | int offset = (int) r2; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1492 | __sum16 sum, *ptr; |
| 1493 | |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1494 | if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR | |
| 1495 | BPF_F_HDR_FIELD_MASK))) |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1496 | return -EINVAL; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1497 | if (unlikely((u32) offset > 0xffff)) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1498 | return -EFAULT; |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 1499 | if (unlikely(skb_try_make_writable(skb, offset + sizeof(sum)))) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1500 | return -EFAULT; |
| 1501 | |
| 1502 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); |
| 1503 | if (unlikely(!ptr)) |
| 1504 | return -EFAULT; |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1505 | if (is_mmzero && !*ptr) |
| 1506 | return 0; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1507 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1508 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1509 | case 0: |
| 1510 | if (unlikely(from != 0)) |
| 1511 | return -EINVAL; |
| 1512 | |
| 1513 | inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); |
| 1514 | break; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1515 | case 2: |
| 1516 | inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); |
| 1517 | break; |
| 1518 | case 4: |
| 1519 | inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); |
| 1520 | break; |
| 1521 | default: |
| 1522 | return -EINVAL; |
| 1523 | } |
| 1524 | |
Daniel Borkmann | 2f72959 | 2016-02-19 23:05:26 +0100 | [diff] [blame] | 1525 | if (is_mmzero && !*ptr) |
| 1526 | *ptr = CSUM_MANGLED_0; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1527 | if (ptr == &sum) |
| 1528 | /* skb_store_bits guaranteed to not return -EFAULT here */ |
| 1529 | skb_store_bits(skb, offset, ptr, sizeof(sum)); |
| 1530 | |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | const struct bpf_func_proto bpf_l4_csum_replace_proto = { |
| 1535 | .func = bpf_l4_csum_replace, |
| 1536 | .gpl_only = false, |
| 1537 | .ret_type = RET_INTEGER, |
| 1538 | .arg1_type = ARG_PTR_TO_CTX, |
| 1539 | .arg2_type = ARG_ANYTHING, |
| 1540 | .arg3_type = ARG_ANYTHING, |
| 1541 | .arg4_type = ARG_ANYTHING, |
| 1542 | .arg5_type = ARG_ANYTHING, |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1543 | }; |
| 1544 | |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1545 | static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed) |
| 1546 | { |
Daniel Borkmann | 21cafc1 | 2016-02-19 23:05:24 +0100 | [diff] [blame] | 1547 | struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1548 | u64 diff_size = from_size + to_size; |
| 1549 | __be32 *from = (__be32 *) (long) r1; |
| 1550 | __be32 *to = (__be32 *) (long) r3; |
| 1551 | int i, j = 0; |
| 1552 | |
| 1553 | /* This is quite flexible, some examples: |
| 1554 | * |
| 1555 | * from_size == 0, to_size > 0, seed := csum --> pushing data |
| 1556 | * from_size > 0, to_size == 0, seed := csum --> pulling data |
| 1557 | * from_size > 0, to_size > 0, seed := 0 --> diffing data |
| 1558 | * |
| 1559 | * Even for diffing, from_size and to_size don't need to be equal. |
| 1560 | */ |
| 1561 | if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || |
| 1562 | diff_size > sizeof(sp->diff))) |
| 1563 | return -EINVAL; |
| 1564 | |
| 1565 | for (i = 0; i < from_size / sizeof(__be32); i++, j++) |
| 1566 | sp->diff[j] = ~from[i]; |
| 1567 | for (i = 0; i < to_size / sizeof(__be32); i++, j++) |
| 1568 | sp->diff[j] = to[i]; |
| 1569 | |
| 1570 | return csum_partial(sp->diff, diff_size, seed); |
| 1571 | } |
| 1572 | |
| 1573 | const struct bpf_func_proto bpf_csum_diff_proto = { |
| 1574 | .func = bpf_csum_diff, |
| 1575 | .gpl_only = false, |
| 1576 | .ret_type = RET_INTEGER, |
| 1577 | .arg1_type = ARG_PTR_TO_STACK, |
| 1578 | .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO, |
| 1579 | .arg3_type = ARG_PTR_TO_STACK, |
| 1580 | .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO, |
| 1581 | .arg5_type = ARG_ANYTHING, |
| 1582 | }; |
| 1583 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1584 | static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5) |
| 1585 | { |
| 1586 | struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2; |
| 1587 | struct net_device *dev; |
| 1588 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1589 | if (unlikely(flags & ~(BPF_F_INGRESS))) |
| 1590 | return -EINVAL; |
| 1591 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1592 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); |
| 1593 | if (unlikely(!dev)) |
| 1594 | return -EINVAL; |
| 1595 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1596 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1597 | if (unlikely(!skb2)) |
| 1598 | return -ENOMEM; |
| 1599 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1600 | if (flags & BPF_F_INGRESS) { |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1601 | if (skb_at_tc_ingress(skb2)) |
| 1602 | skb_postpush_rcsum(skb2, skb_mac_header(skb2), |
| 1603 | skb2->mac_len); |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1604 | return dev_forward_skb(dev, skb2); |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1605 | } |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1606 | |
| 1607 | skb2->dev = dev; |
| 1608 | return dev_queue_xmit(skb2); |
| 1609 | } |
| 1610 | |
| 1611 | const struct bpf_func_proto bpf_clone_redirect_proto = { |
| 1612 | .func = bpf_clone_redirect, |
| 1613 | .gpl_only = false, |
| 1614 | .ret_type = RET_INTEGER, |
| 1615 | .arg1_type = ARG_PTR_TO_CTX, |
| 1616 | .arg2_type = ARG_ANYTHING, |
| 1617 | .arg3_type = ARG_ANYTHING, |
| 1618 | }; |
| 1619 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1620 | struct redirect_info { |
| 1621 | u32 ifindex; |
| 1622 | u32 flags; |
| 1623 | }; |
| 1624 | |
| 1625 | static DEFINE_PER_CPU(struct redirect_info, redirect_info); |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1626 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1627 | static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5) |
| 1628 | { |
| 1629 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); |
| 1630 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1631 | if (unlikely(flags & ~(BPF_F_INGRESS))) |
| 1632 | return TC_ACT_SHOT; |
| 1633 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1634 | ri->ifindex = ifindex; |
| 1635 | ri->flags = flags; |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1636 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1637 | return TC_ACT_REDIRECT; |
| 1638 | } |
| 1639 | |
| 1640 | int skb_do_redirect(struct sk_buff *skb) |
| 1641 | { |
| 1642 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); |
| 1643 | struct net_device *dev; |
| 1644 | |
| 1645 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex); |
| 1646 | ri->ifindex = 0; |
| 1647 | if (unlikely(!dev)) { |
| 1648 | kfree_skb(skb); |
| 1649 | return -EINVAL; |
| 1650 | } |
| 1651 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1652 | if (ri->flags & BPF_F_INGRESS) { |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1653 | if (skb_at_tc_ingress(skb)) |
| 1654 | skb_postpush_rcsum(skb, skb_mac_header(skb), |
| 1655 | skb->mac_len); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1656 | return dev_forward_skb(dev, skb); |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1657 | } |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1658 | |
| 1659 | skb->dev = dev; |
| 1660 | return dev_queue_xmit(skb); |
| 1661 | } |
| 1662 | |
| 1663 | const struct bpf_func_proto bpf_redirect_proto = { |
| 1664 | .func = bpf_redirect, |
| 1665 | .gpl_only = false, |
| 1666 | .ret_type = RET_INTEGER, |
| 1667 | .arg1_type = ARG_ANYTHING, |
| 1668 | .arg2_type = ARG_ANYTHING, |
| 1669 | }; |
| 1670 | |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 1671 | static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1672 | { |
| 1673 | return task_get_classid((struct sk_buff *) (unsigned long) r1); |
| 1674 | } |
| 1675 | |
| 1676 | static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { |
| 1677 | .func = bpf_get_cgroup_classid, |
| 1678 | .gpl_only = false, |
| 1679 | .ret_type = RET_INTEGER, |
| 1680 | .arg1_type = ARG_PTR_TO_CTX, |
| 1681 | }; |
| 1682 | |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 1683 | static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1684 | { |
| 1685 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 1686 | const struct dst_entry *dst; |
| 1687 | |
| 1688 | dst = skb_dst((struct sk_buff *) (unsigned long) r1); |
| 1689 | if (dst) |
| 1690 | return dst->tclassid; |
| 1691 | #endif |
| 1692 | return 0; |
| 1693 | } |
| 1694 | |
| 1695 | static const struct bpf_func_proto bpf_get_route_realm_proto = { |
| 1696 | .func = bpf_get_route_realm, |
| 1697 | .gpl_only = false, |
| 1698 | .ret_type = RET_INTEGER, |
| 1699 | .arg1_type = ARG_PTR_TO_CTX, |
| 1700 | }; |
| 1701 | |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1702 | static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5) |
| 1703 | { |
| 1704 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1705 | __be16 vlan_proto = (__force __be16) r2; |
| 1706 | |
| 1707 | if (unlikely(vlan_proto != htons(ETH_P_8021Q) && |
| 1708 | vlan_proto != htons(ETH_P_8021AD))) |
| 1709 | vlan_proto = htons(ETH_P_8021Q); |
| 1710 | |
| 1711 | return skb_vlan_push(skb, vlan_proto, vlan_tci); |
| 1712 | } |
| 1713 | |
| 1714 | const struct bpf_func_proto bpf_skb_vlan_push_proto = { |
| 1715 | .func = bpf_skb_vlan_push, |
| 1716 | .gpl_only = false, |
| 1717 | .ret_type = RET_INTEGER, |
| 1718 | .arg1_type = ARG_PTR_TO_CTX, |
| 1719 | .arg2_type = ARG_ANYTHING, |
| 1720 | .arg3_type = ARG_ANYTHING, |
| 1721 | }; |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 1722 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto); |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1723 | |
| 1724 | static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1725 | { |
| 1726 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1727 | |
| 1728 | return skb_vlan_pop(skb); |
| 1729 | } |
| 1730 | |
| 1731 | const struct bpf_func_proto bpf_skb_vlan_pop_proto = { |
| 1732 | .func = bpf_skb_vlan_pop, |
| 1733 | .gpl_only = false, |
| 1734 | .ret_type = RET_INTEGER, |
| 1735 | .arg1_type = ARG_PTR_TO_CTX, |
| 1736 | }; |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 1737 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto); |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1738 | |
| 1739 | bool bpf_helper_changes_skb_data(void *func) |
| 1740 | { |
| 1741 | if (func == bpf_skb_vlan_push) |
| 1742 | return true; |
| 1743 | if (func == bpf_skb_vlan_pop) |
| 1744 | return true; |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 1745 | if (func == bpf_skb_store_bytes) |
| 1746 | return true; |
| 1747 | if (func == bpf_l3_csum_replace) |
| 1748 | return true; |
| 1749 | if (func == bpf_l4_csum_replace) |
| 1750 | return true; |
| 1751 | |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1752 | return false; |
| 1753 | } |
| 1754 | |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1755 | static unsigned short bpf_tunnel_key_af(u64 flags) |
| 1756 | { |
| 1757 | return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; |
| 1758 | } |
| 1759 | |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1760 | static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5) |
| 1761 | { |
| 1762 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1763 | struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1764 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 1765 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1766 | |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1767 | if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1768 | return -EINVAL; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1769 | if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) |
| 1770 | return -EPROTO; |
| 1771 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
| 1772 | switch (size) { |
| 1773 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
| 1774 | /* Fixup deprecated structure layouts here, so we have |
| 1775 | * a common path later on. |
| 1776 | */ |
| 1777 | if (ip_tunnel_info_af(info) != AF_INET) |
| 1778 | return -EINVAL; |
| 1779 | to = (struct bpf_tunnel_key *)compat; |
| 1780 | break; |
| 1781 | default: |
| 1782 | return -EINVAL; |
| 1783 | } |
| 1784 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1785 | |
| 1786 | to->tunnel_id = be64_to_cpu(info->key.tun_id); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1787 | to->tunnel_tos = info->key.tos; |
| 1788 | to->tunnel_ttl = info->key.ttl; |
| 1789 | |
| 1790 | if (flags & BPF_F_TUNINFO_IPV6) |
| 1791 | memcpy(to->remote_ipv6, &info->key.u.ipv6.src, |
| 1792 | sizeof(to->remote_ipv6)); |
| 1793 | else |
| 1794 | to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); |
| 1795 | |
| 1796 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) |
| 1797 | memcpy((void *)(long) r2, to, size); |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1798 | |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { |
| 1803 | .func = bpf_skb_get_tunnel_key, |
| 1804 | .gpl_only = false, |
| 1805 | .ret_type = RET_INTEGER, |
| 1806 | .arg1_type = ARG_PTR_TO_CTX, |
| 1807 | .arg2_type = ARG_PTR_TO_STACK, |
| 1808 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 1809 | .arg4_type = ARG_ANYTHING, |
| 1810 | }; |
| 1811 | |
| 1812 | static struct metadata_dst __percpu *md_dst; |
| 1813 | |
| 1814 | static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5) |
| 1815 | { |
| 1816 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1817 | struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2; |
| 1818 | struct metadata_dst *md = this_cpu_ptr(md_dst); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1819 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1820 | struct ip_tunnel_info *info; |
| 1821 | |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame] | 1822 | if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX))) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1823 | return -EINVAL; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1824 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
| 1825 | switch (size) { |
| 1826 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
| 1827 | /* Fixup deprecated structure layouts here, so we have |
| 1828 | * a common path later on. |
| 1829 | */ |
| 1830 | memcpy(compat, from, size); |
| 1831 | memset(compat + size, 0, sizeof(compat) - size); |
| 1832 | from = (struct bpf_tunnel_key *)compat; |
| 1833 | break; |
| 1834 | default: |
| 1835 | return -EINVAL; |
| 1836 | } |
| 1837 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1838 | |
| 1839 | skb_dst_drop(skb); |
| 1840 | dst_hold((struct dst_entry *) md); |
| 1841 | skb_dst_set(skb, (struct dst_entry *) md); |
| 1842 | |
| 1843 | info = &md->u.tun_info; |
| 1844 | info->mode = IP_TUNNEL_INFO_TX; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1845 | |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame] | 1846 | info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1847 | info->key.tun_id = cpu_to_be64(from->tunnel_id); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1848 | info->key.tos = from->tunnel_tos; |
| 1849 | info->key.ttl = from->tunnel_ttl; |
| 1850 | |
| 1851 | if (flags & BPF_F_TUNINFO_IPV6) { |
| 1852 | info->mode |= IP_TUNNEL_INFO_IPV6; |
| 1853 | memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, |
| 1854 | sizeof(from->remote_ipv6)); |
| 1855 | } else { |
| 1856 | info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame] | 1857 | if (flags & BPF_F_ZERO_CSUM_TX) |
| 1858 | info->key.tun_flags &= ~TUNNEL_CSUM; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1859 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1860 | |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { |
| 1865 | .func = bpf_skb_set_tunnel_key, |
| 1866 | .gpl_only = false, |
| 1867 | .ret_type = RET_INTEGER, |
| 1868 | .arg1_type = ARG_PTR_TO_CTX, |
| 1869 | .arg2_type = ARG_PTR_TO_STACK, |
| 1870 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 1871 | .arg4_type = ARG_ANYTHING, |
| 1872 | }; |
| 1873 | |
| 1874 | static const struct bpf_func_proto *bpf_get_skb_set_tunnel_key_proto(void) |
| 1875 | { |
| 1876 | if (!md_dst) { |
| 1877 | /* race is not possible, since it's called from |
| 1878 | * verifier that is holding verifier mutex |
| 1879 | */ |
| 1880 | md_dst = metadata_dst_alloc_percpu(0, GFP_KERNEL); |
| 1881 | if (!md_dst) |
| 1882 | return NULL; |
| 1883 | } |
| 1884 | return &bpf_skb_set_tunnel_key_proto; |
| 1885 | } |
| 1886 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 1887 | static const struct bpf_func_proto * |
| 1888 | sk_filter_func_proto(enum bpf_func_id func_id) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1889 | { |
| 1890 | switch (func_id) { |
| 1891 | case BPF_FUNC_map_lookup_elem: |
| 1892 | return &bpf_map_lookup_elem_proto; |
| 1893 | case BPF_FUNC_map_update_elem: |
| 1894 | return &bpf_map_update_elem_proto; |
| 1895 | case BPF_FUNC_map_delete_elem: |
| 1896 | return &bpf_map_delete_elem_proto; |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1897 | case BPF_FUNC_get_prandom_u32: |
| 1898 | return &bpf_get_prandom_u32_proto; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 1899 | case BPF_FUNC_get_smp_processor_id: |
| 1900 | return &bpf_get_smp_processor_id_proto; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1901 | case BPF_FUNC_tail_call: |
| 1902 | return &bpf_tail_call_proto; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 1903 | case BPF_FUNC_ktime_get_ns: |
| 1904 | return &bpf_ktime_get_ns_proto; |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 1905 | case BPF_FUNC_trace_printk: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1906 | if (capable(CAP_SYS_ADMIN)) |
| 1907 | return bpf_get_trace_printk_proto(); |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1908 | default: |
| 1909 | return NULL; |
| 1910 | } |
| 1911 | } |
| 1912 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1913 | static const struct bpf_func_proto * |
| 1914 | tc_cls_act_func_proto(enum bpf_func_id func_id) |
| 1915 | { |
| 1916 | switch (func_id) { |
| 1917 | case BPF_FUNC_skb_store_bytes: |
| 1918 | return &bpf_skb_store_bytes_proto; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1919 | case BPF_FUNC_skb_load_bytes: |
| 1920 | return &bpf_skb_load_bytes_proto; |
Daniel Borkmann | 7d67234 | 2016-02-19 23:05:23 +0100 | [diff] [blame] | 1921 | case BPF_FUNC_csum_diff: |
| 1922 | return &bpf_csum_diff_proto; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1923 | case BPF_FUNC_l3_csum_replace: |
| 1924 | return &bpf_l3_csum_replace_proto; |
| 1925 | case BPF_FUNC_l4_csum_replace: |
| 1926 | return &bpf_l4_csum_replace_proto; |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1927 | case BPF_FUNC_clone_redirect: |
| 1928 | return &bpf_clone_redirect_proto; |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 1929 | case BPF_FUNC_get_cgroup_classid: |
| 1930 | return &bpf_get_cgroup_classid_proto; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1931 | case BPF_FUNC_skb_vlan_push: |
| 1932 | return &bpf_skb_vlan_push_proto; |
| 1933 | case BPF_FUNC_skb_vlan_pop: |
| 1934 | return &bpf_skb_vlan_pop_proto; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1935 | case BPF_FUNC_skb_get_tunnel_key: |
| 1936 | return &bpf_skb_get_tunnel_key_proto; |
| 1937 | case BPF_FUNC_skb_set_tunnel_key: |
| 1938 | return bpf_get_skb_set_tunnel_key_proto(); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1939 | case BPF_FUNC_redirect: |
| 1940 | return &bpf_redirect_proto; |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 1941 | case BPF_FUNC_get_route_realm: |
| 1942 | return &bpf_get_route_realm_proto; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1943 | default: |
| 1944 | return sk_filter_func_proto(func_id); |
| 1945 | } |
| 1946 | } |
| 1947 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1948 | 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] | 1949 | { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1950 | /* check bounds */ |
| 1951 | if (off < 0 || off >= sizeof(struct __sk_buff)) |
| 1952 | return false; |
| 1953 | |
| 1954 | /* disallow misaligned access */ |
| 1955 | if (off % size != 0) |
| 1956 | return false; |
| 1957 | |
| 1958 | /* all __sk_buff fields are __u32 */ |
| 1959 | if (size != 4) |
| 1960 | return false; |
| 1961 | |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1965 | static bool sk_filter_is_valid_access(int off, int size, |
| 1966 | enum bpf_access_type type) |
| 1967 | { |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 1968 | if (off == offsetof(struct __sk_buff, tc_classid)) |
| 1969 | return false; |
| 1970 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1971 | if (type == BPF_WRITE) { |
| 1972 | switch (off) { |
| 1973 | case offsetof(struct __sk_buff, cb[0]) ... |
| 1974 | offsetof(struct __sk_buff, cb[4]): |
| 1975 | break; |
| 1976 | default: |
| 1977 | return false; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | return __is_valid_access(off, size, type); |
| 1982 | } |
| 1983 | |
| 1984 | static bool tc_cls_act_is_valid_access(int off, int size, |
| 1985 | enum bpf_access_type type) |
| 1986 | { |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 1987 | if (off == offsetof(struct __sk_buff, tc_classid)) |
| 1988 | return type == BPF_WRITE ? true : false; |
| 1989 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1990 | if (type == BPF_WRITE) { |
| 1991 | switch (off) { |
| 1992 | case offsetof(struct __sk_buff, mark): |
| 1993 | case offsetof(struct __sk_buff, tc_index): |
Daniel Borkmann | 754f1e6 | 2015-09-30 01:41:52 +0200 | [diff] [blame] | 1994 | case offsetof(struct __sk_buff, priority): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1995 | case offsetof(struct __sk_buff, cb[0]) ... |
| 1996 | offsetof(struct __sk_buff, cb[4]): |
| 1997 | break; |
| 1998 | default: |
| 1999 | return false; |
| 2000 | } |
| 2001 | } |
| 2002 | return __is_valid_access(off, size, type); |
| 2003 | } |
| 2004 | |
| 2005 | static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg, |
| 2006 | int src_reg, int ctx_off, |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2007 | struct bpf_insn *insn_buf, |
| 2008 | struct bpf_prog *prog) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2009 | { |
| 2010 | struct bpf_insn *insn = insn_buf; |
| 2011 | |
| 2012 | switch (ctx_off) { |
| 2013 | case offsetof(struct __sk_buff, len): |
| 2014 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); |
| 2015 | |
| 2016 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2017 | offsetof(struct sk_buff, len)); |
| 2018 | break; |
| 2019 | |
Daniel Borkmann | 0b8c707 | 2015-03-19 19:38:27 +0100 | [diff] [blame] | 2020 | case offsetof(struct __sk_buff, protocol): |
| 2021 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); |
| 2022 | |
| 2023 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2024 | offsetof(struct sk_buff, protocol)); |
| 2025 | break; |
| 2026 | |
Michal Sekletar | 27cd545 | 2015-03-24 14:48:41 +0100 | [diff] [blame] | 2027 | case offsetof(struct __sk_buff, vlan_proto): |
| 2028 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); |
| 2029 | |
| 2030 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2031 | offsetof(struct sk_buff, vlan_proto)); |
| 2032 | break; |
| 2033 | |
Daniel Borkmann | bcad571 | 2015-04-03 20:52:24 +0200 | [diff] [blame] | 2034 | case offsetof(struct __sk_buff, priority): |
| 2035 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4); |
| 2036 | |
Daniel Borkmann | 754f1e6 | 2015-09-30 01:41:52 +0200 | [diff] [blame] | 2037 | if (type == BPF_WRITE) |
| 2038 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, |
| 2039 | offsetof(struct sk_buff, priority)); |
| 2040 | else |
| 2041 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2042 | offsetof(struct sk_buff, priority)); |
Daniel Borkmann | bcad571 | 2015-04-03 20:52:24 +0200 | [diff] [blame] | 2043 | break; |
| 2044 | |
Alexei Starovoitov | 37e82c2 | 2015-05-27 15:30:39 -0700 | [diff] [blame] | 2045 | case offsetof(struct __sk_buff, ingress_ifindex): |
| 2046 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4); |
| 2047 | |
| 2048 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2049 | offsetof(struct sk_buff, skb_iif)); |
| 2050 | break; |
| 2051 | |
| 2052 | case offsetof(struct __sk_buff, ifindex): |
| 2053 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 2054 | |
| 2055 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), |
| 2056 | dst_reg, src_reg, |
| 2057 | offsetof(struct sk_buff, dev)); |
| 2058 | *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1); |
| 2059 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg, |
| 2060 | offsetof(struct net_device, ifindex)); |
| 2061 | break; |
| 2062 | |
Daniel Borkmann | ba7591d | 2015-08-01 00:46:29 +0200 | [diff] [blame] | 2063 | case offsetof(struct __sk_buff, hash): |
| 2064 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 2065 | |
| 2066 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2067 | offsetof(struct sk_buff, hash)); |
| 2068 | break; |
| 2069 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2070 | case offsetof(struct __sk_buff, mark): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2071 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 2072 | |
| 2073 | if (type == BPF_WRITE) |
| 2074 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, |
| 2075 | offsetof(struct sk_buff, mark)); |
| 2076 | else |
| 2077 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2078 | offsetof(struct sk_buff, mark)); |
| 2079 | break; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2080 | |
| 2081 | case offsetof(struct __sk_buff, pkt_type): |
| 2082 | return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn); |
| 2083 | |
| 2084 | case offsetof(struct __sk_buff, queue_mapping): |
| 2085 | return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn); |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 2086 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 2087 | case offsetof(struct __sk_buff, vlan_present): |
| 2088 | return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, |
| 2089 | dst_reg, src_reg, insn); |
| 2090 | |
| 2091 | case offsetof(struct __sk_buff, vlan_tci): |
| 2092 | return convert_skb_access(SKF_AD_VLAN_TAG, |
| 2093 | dst_reg, src_reg, insn); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2094 | |
| 2095 | case offsetof(struct __sk_buff, cb[0]) ... |
| 2096 | offsetof(struct __sk_buff, cb[4]): |
| 2097 | BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20); |
| 2098 | |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2099 | prog->cb_access = 1; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2100 | ctx_off -= offsetof(struct __sk_buff, cb[0]); |
| 2101 | ctx_off += offsetof(struct sk_buff, cb); |
| 2102 | ctx_off += offsetof(struct qdisc_skb_cb, data); |
| 2103 | if (type == BPF_WRITE) |
| 2104 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off); |
| 2105 | else |
| 2106 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off); |
| 2107 | break; |
| 2108 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 2109 | case offsetof(struct __sk_buff, tc_classid): |
| 2110 | ctx_off -= offsetof(struct __sk_buff, tc_classid); |
| 2111 | ctx_off += offsetof(struct sk_buff, cb); |
| 2112 | ctx_off += offsetof(struct qdisc_skb_cb, tc_classid); |
| 2113 | WARN_ON(type != BPF_WRITE); |
| 2114 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off); |
| 2115 | break; |
| 2116 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2117 | case offsetof(struct __sk_buff, tc_index): |
| 2118 | #ifdef CONFIG_NET_SCHED |
| 2119 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2); |
| 2120 | |
| 2121 | if (type == BPF_WRITE) |
| 2122 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, |
| 2123 | offsetof(struct sk_buff, tc_index)); |
| 2124 | else |
| 2125 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2126 | offsetof(struct sk_buff, tc_index)); |
| 2127 | break; |
| 2128 | #else |
| 2129 | if (type == BPF_WRITE) |
| 2130 | *insn++ = BPF_MOV64_REG(dst_reg, dst_reg); |
| 2131 | else |
| 2132 | *insn++ = BPF_MOV64_IMM(dst_reg, 0); |
| 2133 | break; |
| 2134 | #endif |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | return insn - insn_buf; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2140 | static const struct bpf_verifier_ops sk_filter_ops = { |
| 2141 | .get_func_proto = sk_filter_func_proto, |
| 2142 | .is_valid_access = sk_filter_is_valid_access, |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2143 | .convert_ctx_access = bpf_net_convert_ctx_access, |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2144 | }; |
| 2145 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2146 | static const struct bpf_verifier_ops tc_cls_act_ops = { |
| 2147 | .get_func_proto = tc_cls_act_func_proto, |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2148 | .is_valid_access = tc_cls_act_is_valid_access, |
| 2149 | .convert_ctx_access = bpf_net_convert_ctx_access, |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2150 | }; |
| 2151 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2152 | static struct bpf_prog_type_list sk_filter_type __read_mostly = { |
| 2153 | .ops = &sk_filter_ops, |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2154 | .type = BPF_PROG_TYPE_SOCKET_FILTER, |
| 2155 | }; |
| 2156 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2157 | static struct bpf_prog_type_list sched_cls_type __read_mostly = { |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2158 | .ops = &tc_cls_act_ops, |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2159 | .type = BPF_PROG_TYPE_SCHED_CLS, |
| 2160 | }; |
| 2161 | |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2162 | static struct bpf_prog_type_list sched_act_type __read_mostly = { |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2163 | .ops = &tc_cls_act_ops, |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2164 | .type = BPF_PROG_TYPE_SCHED_ACT, |
| 2165 | }; |
| 2166 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2167 | static int __init register_sk_filter_ops(void) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2168 | { |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2169 | bpf_register_prog_type(&sk_filter_type); |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2170 | bpf_register_prog_type(&sched_cls_type); |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2171 | bpf_register_prog_type(&sched_act_type); |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2172 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2173 | return 0; |
| 2174 | } |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2175 | late_initcall(register_sk_filter_ops); |
| 2176 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2177 | int sk_detach_filter(struct sock *sk) |
| 2178 | { |
| 2179 | int ret = -ENOENT; |
| 2180 | struct sk_filter *filter; |
| 2181 | |
Vincent Bernat | d59577b | 2013-01-16 22:55:49 +0100 | [diff] [blame] | 2182 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 2183 | return -EPERM; |
| 2184 | |
Eric Dumazet | f91ff5b | 2010-09-27 06:07:30 +0000 | [diff] [blame] | 2185 | filter = rcu_dereference_protected(sk->sk_filter, |
| 2186 | sock_owned_by_user(sk)); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2187 | if (filter) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 2188 | RCU_INIT_POINTER(sk->sk_filter, NULL); |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 2189 | sk_filter_uncharge(sk, filter); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2190 | ret = 0; |
| 2191 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2192 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2193 | return ret; |
| 2194 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 2195 | EXPORT_SYMBOL_GPL(sk_detach_filter); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2196 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2197 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, |
| 2198 | unsigned int len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2199 | { |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2200 | struct sock_fprog_kern *fprog; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2201 | struct sk_filter *filter; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2202 | int ret = 0; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2203 | |
| 2204 | lock_sock(sk); |
| 2205 | filter = rcu_dereference_protected(sk->sk_filter, |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2206 | sock_owned_by_user(sk)); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2207 | if (!filter) |
| 2208 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2209 | |
| 2210 | /* We're copying the filter that has been originally attached, |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 2211 | * so no conversion/decode needed anymore. eBPF programs that |
| 2212 | * have no original program cannot be dumped through this. |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2213 | */ |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 2214 | ret = -EACCES; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 2215 | fprog = filter->prog->orig_prog; |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 2216 | if (!fprog) |
| 2217 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2218 | |
| 2219 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2220 | if (!len) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2221 | /* User space only enquires number of filter blocks. */ |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2222 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2223 | |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2224 | ret = -EINVAL; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2225 | if (len < fprog->len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2226 | goto out; |
| 2227 | |
| 2228 | ret = -EFAULT; |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 2229 | if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2230 | goto out; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2231 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2232 | /* Instead of bytes, the API requests to return the number |
| 2233 | * of filter blocks. |
| 2234 | */ |
| 2235 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2236 | out: |
| 2237 | release_sock(sk); |
| 2238 | return ret; |
| 2239 | } |