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 | |
| 533 | /* RET_K, RET_A are remaped into 2 insns. */ |
| 534 | case BPF_RET | BPF_A: |
| 535 | case BPF_RET | BPF_K: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 536 | *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ? |
| 537 | BPF_K : BPF_X, BPF_REG_0, |
| 538 | BPF_REG_A, fp->k); |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 539 | *insn = BPF_EXIT_INSN(); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 540 | break; |
| 541 | |
| 542 | /* Store to stack. */ |
| 543 | case BPF_ST: |
| 544 | case BPF_STX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 545 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == |
| 546 | BPF_ST ? BPF_REG_A : BPF_REG_X, |
| 547 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 548 | break; |
| 549 | |
| 550 | /* Load from stack. */ |
| 551 | case BPF_LD | BPF_MEM: |
| 552 | case BPF_LDX | BPF_MEM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 553 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 554 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, |
| 555 | -(BPF_MEMWORDS - fp->k) * 4); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 556 | break; |
| 557 | |
| 558 | /* A = K or X = K */ |
| 559 | case BPF_LD | BPF_IMM: |
| 560 | case BPF_LDX | BPF_IMM: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 561 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? |
| 562 | BPF_REG_A : BPF_REG_X, fp->k); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 563 | break; |
| 564 | |
| 565 | /* X = A */ |
| 566 | case BPF_MISC | BPF_TAX: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 567 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 568 | break; |
| 569 | |
| 570 | /* A = X */ |
| 571 | case BPF_MISC | BPF_TXA: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 572 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 573 | break; |
| 574 | |
| 575 | /* A = skb->len or X = skb->len */ |
| 576 | case BPF_LD | BPF_W | BPF_LEN: |
| 577 | case BPF_LDX | BPF_W | BPF_LEN: |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 578 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
| 579 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, |
| 580 | offsetof(struct sk_buff, len)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 581 | break; |
| 582 | |
Daniel Borkmann | f8f6d67 | 2014-05-29 10:22:51 +0200 | [diff] [blame] | 583 | /* Access seccomp_data fields. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 584 | case BPF_LDX | BPF_ABS | BPF_W: |
Alexei Starovoitov | 9739eef | 2014-05-08 14:10:51 -0700 | [diff] [blame] | 585 | /* A = *(u32 *) (ctx + K) */ |
| 586 | *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] | 587 | break; |
| 588 | |
Stephen Hemminger | ca9f1fd | 2015-02-14 13:47:54 -0500 | [diff] [blame] | 589 | /* Unknown instruction. */ |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 590 | default: |
| 591 | goto err; |
| 592 | } |
| 593 | |
| 594 | insn++; |
| 595 | if (new_prog) |
| 596 | memcpy(new_insn, tmp_insns, |
| 597 | sizeof(*insn) * (insn - tmp_insns)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 598 | new_insn += insn - tmp_insns; |
| 599 | } |
| 600 | |
| 601 | if (!new_prog) { |
| 602 | /* Only calculating new length. */ |
| 603 | *new_len = new_insn - new_prog; |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | pass++; |
| 608 | if (new_flen != new_insn - new_prog) { |
| 609 | new_flen = new_insn - new_prog; |
| 610 | if (pass > 2) |
| 611 | goto err; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 612 | goto do_pass; |
| 613 | } |
| 614 | |
| 615 | kfree(addrs); |
| 616 | BUG_ON(*new_len != new_flen); |
| 617 | return 0; |
| 618 | err: |
| 619 | kfree(addrs); |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | |
| 623 | /* Security: |
| 624 | * |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 625 | * 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] | 626 | * __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] | 627 | * 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] | 628 | * a malicious user doesn't try to abuse us. |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 629 | */ |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 630 | 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] | 631 | { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 632 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 633 | int pc, ret = 0; |
| 634 | |
| 635 | BUILD_BUG_ON(BPF_MEMWORDS > 16); |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 636 | |
Tobias Klauser | 99e72a0 | 2014-06-24 15:33:22 +0200 | [diff] [blame] | 637 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 638 | if (!masks) |
| 639 | return -ENOMEM; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 640 | |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 641 | memset(masks, 0xff, flen * sizeof(*masks)); |
| 642 | |
| 643 | for (pc = 0; pc < flen; pc++) { |
| 644 | memvalid &= masks[pc]; |
| 645 | |
| 646 | switch (filter[pc].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 647 | case BPF_ST: |
| 648 | case BPF_STX: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 649 | memvalid |= (1 << filter[pc].k); |
| 650 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 651 | case BPF_LD | BPF_MEM: |
| 652 | case BPF_LDX | BPF_MEM: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 653 | if (!(memvalid & (1 << filter[pc].k))) { |
| 654 | ret = -EINVAL; |
| 655 | goto error; |
| 656 | } |
| 657 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 658 | case BPF_JMP | BPF_JA: |
| 659 | /* A jump must set masks on target */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 660 | masks[pc + 1 + filter[pc].k] &= memvalid; |
| 661 | memvalid = ~0; |
| 662 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 663 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 664 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 665 | case BPF_JMP | BPF_JGE | BPF_K: |
| 666 | case BPF_JMP | BPF_JGE | BPF_X: |
| 667 | case BPF_JMP | BPF_JGT | BPF_K: |
| 668 | case BPF_JMP | BPF_JGT | BPF_X: |
| 669 | case BPF_JMP | BPF_JSET | BPF_K: |
| 670 | case BPF_JMP | BPF_JSET | BPF_X: |
| 671 | /* A jump must set masks on targets */ |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 672 | masks[pc + 1 + filter[pc].jt] &= memvalid; |
| 673 | masks[pc + 1 + filter[pc].jf] &= memvalid; |
| 674 | memvalid = ~0; |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | error: |
| 679 | kfree(masks); |
| 680 | return ret; |
| 681 | } |
| 682 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 683 | static bool chk_code_allowed(u16 code_to_probe) |
| 684 | { |
| 685 | static const bool codes[] = { |
| 686 | /* 32 bit ALU operations */ |
| 687 | [BPF_ALU | BPF_ADD | BPF_K] = true, |
| 688 | [BPF_ALU | BPF_ADD | BPF_X] = true, |
| 689 | [BPF_ALU | BPF_SUB | BPF_K] = true, |
| 690 | [BPF_ALU | BPF_SUB | BPF_X] = true, |
| 691 | [BPF_ALU | BPF_MUL | BPF_K] = true, |
| 692 | [BPF_ALU | BPF_MUL | BPF_X] = true, |
| 693 | [BPF_ALU | BPF_DIV | BPF_K] = true, |
| 694 | [BPF_ALU | BPF_DIV | BPF_X] = true, |
| 695 | [BPF_ALU | BPF_MOD | BPF_K] = true, |
| 696 | [BPF_ALU | BPF_MOD | BPF_X] = true, |
| 697 | [BPF_ALU | BPF_AND | BPF_K] = true, |
| 698 | [BPF_ALU | BPF_AND | BPF_X] = true, |
| 699 | [BPF_ALU | BPF_OR | BPF_K] = true, |
| 700 | [BPF_ALU | BPF_OR | BPF_X] = true, |
| 701 | [BPF_ALU | BPF_XOR | BPF_K] = true, |
| 702 | [BPF_ALU | BPF_XOR | BPF_X] = true, |
| 703 | [BPF_ALU | BPF_LSH | BPF_K] = true, |
| 704 | [BPF_ALU | BPF_LSH | BPF_X] = true, |
| 705 | [BPF_ALU | BPF_RSH | BPF_K] = true, |
| 706 | [BPF_ALU | BPF_RSH | BPF_X] = true, |
| 707 | [BPF_ALU | BPF_NEG] = true, |
| 708 | /* Load instructions */ |
| 709 | [BPF_LD | BPF_W | BPF_ABS] = true, |
| 710 | [BPF_LD | BPF_H | BPF_ABS] = true, |
| 711 | [BPF_LD | BPF_B | BPF_ABS] = true, |
| 712 | [BPF_LD | BPF_W | BPF_LEN] = true, |
| 713 | [BPF_LD | BPF_W | BPF_IND] = true, |
| 714 | [BPF_LD | BPF_H | BPF_IND] = true, |
| 715 | [BPF_LD | BPF_B | BPF_IND] = true, |
| 716 | [BPF_LD | BPF_IMM] = true, |
| 717 | [BPF_LD | BPF_MEM] = true, |
| 718 | [BPF_LDX | BPF_W | BPF_LEN] = true, |
| 719 | [BPF_LDX | BPF_B | BPF_MSH] = true, |
| 720 | [BPF_LDX | BPF_IMM] = true, |
| 721 | [BPF_LDX | BPF_MEM] = true, |
| 722 | /* Store instructions */ |
| 723 | [BPF_ST] = true, |
| 724 | [BPF_STX] = true, |
| 725 | /* Misc instructions */ |
| 726 | [BPF_MISC | BPF_TAX] = true, |
| 727 | [BPF_MISC | BPF_TXA] = true, |
| 728 | /* Return instructions */ |
| 729 | [BPF_RET | BPF_K] = true, |
| 730 | [BPF_RET | BPF_A] = true, |
| 731 | /* Jump instructions */ |
| 732 | [BPF_JMP | BPF_JA] = true, |
| 733 | [BPF_JMP | BPF_JEQ | BPF_K] = true, |
| 734 | [BPF_JMP | BPF_JEQ | BPF_X] = true, |
| 735 | [BPF_JMP | BPF_JGE | BPF_K] = true, |
| 736 | [BPF_JMP | BPF_JGE | BPF_X] = true, |
| 737 | [BPF_JMP | BPF_JGT | BPF_K] = true, |
| 738 | [BPF_JMP | BPF_JGT | BPF_X] = true, |
| 739 | [BPF_JMP | BPF_JSET | BPF_K] = true, |
| 740 | [BPF_JMP | BPF_JSET | BPF_X] = true, |
| 741 | }; |
| 742 | |
| 743 | if (code_to_probe >= ARRAY_SIZE(codes)) |
| 744 | return false; |
| 745 | |
| 746 | return codes[code_to_probe]; |
| 747 | } |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | /** |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 750 | * bpf_check_classic - verify socket filter code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | * @filter: filter to verify |
| 752 | * @flen: length of filter |
| 753 | * |
| 754 | * Check the user's filter code. If we let some ugly |
| 755 | * filter code slip through kaboom! The filter must contain |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 756 | * no references or jumps that are out of range, no illegal |
| 757 | * instructions, and must end with a RET instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | * |
Kris Katterjohn | 7b11f69 | 2006-01-13 14:33:06 -0800 | [diff] [blame] | 759 | * All jumps are forward as they are not signed. |
| 760 | * |
| 761 | * Returns 0 if the rule set is legal or -EINVAL if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | */ |
Nicolas Schichan | d9e12f4 | 2015-05-06 16:12:28 +0200 | [diff] [blame] | 763 | static int bpf_check_classic(const struct sock_filter *filter, |
| 764 | unsigned int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 766 | bool anc_found; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 767 | int pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
David S. Miller | 1b93ae64 | 2005-12-27 13:57:59 -0800 | [diff] [blame] | 769 | if (flen == 0 || flen > BPF_MAXINSNS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | return -EINVAL; |
| 771 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 772 | /* Check the filter code now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | for (pc = 0; pc < flen; pc++) { |
Eric Dumazet | ec31a05 | 2014-07-12 15:49:16 +0200 | [diff] [blame] | 774 | const struct sock_filter *ftest = &filter[pc]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 776 | /* May we actually operate on this code? */ |
| 777 | if (!chk_code_allowed(ftest->code)) |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 778 | return -EINVAL; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 779 | |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 780 | /* Some instructions need special checks */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 781 | switch (ftest->code) { |
| 782 | case BPF_ALU | BPF_DIV | BPF_K: |
| 783 | case BPF_ALU | BPF_MOD | BPF_K: |
| 784 | /* Check for division by zero */ |
Eric Dumazet | b6069a9 | 2012-09-07 22:03:35 +0000 | [diff] [blame] | 785 | if (ftest->k == 0) |
| 786 | return -EINVAL; |
| 787 | break; |
Rabin Vincent | 229394e | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 788 | case BPF_ALU | BPF_LSH | BPF_K: |
| 789 | case BPF_ALU | BPF_RSH | BPF_K: |
| 790 | if (ftest->k >= 32) |
| 791 | return -EINVAL; |
| 792 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 793 | case BPF_LD | BPF_MEM: |
| 794 | case BPF_LDX | BPF_MEM: |
| 795 | case BPF_ST: |
| 796 | case BPF_STX: |
| 797 | /* Check for invalid memory addresses */ |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 798 | if (ftest->k >= BPF_MEMWORDS) |
| 799 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 800 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 801 | case BPF_JMP | BPF_JA: |
| 802 | /* Note, the large ftest->k might cause loops. |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 803 | * Compare this with conditional jumps below, |
| 804 | * where offsets are limited. --ANK (981016) |
| 805 | */ |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 806 | if (ftest->k >= (unsigned int)(flen - pc - 1)) |
Kris Katterjohn | 9369986 | 2006-01-04 13:58:36 -0800 | [diff] [blame] | 807 | return -EINVAL; |
| 808 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 809 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 810 | case BPF_JMP | BPF_JEQ | BPF_X: |
| 811 | case BPF_JMP | BPF_JGE | BPF_K: |
| 812 | case BPF_JMP | BPF_JGE | BPF_X: |
| 813 | case BPF_JMP | BPF_JGT | BPF_K: |
| 814 | case BPF_JMP | BPF_JGT | BPF_X: |
| 815 | case BPF_JMP | BPF_JSET | BPF_K: |
| 816 | case BPF_JMP | BPF_JSET | BPF_X: |
| 817 | /* Both conditionals must be safe */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 818 | if (pc + ftest->jt + 1 >= flen || |
| 819 | pc + ftest->jf + 1 >= flen) |
| 820 | return -EINVAL; |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 821 | break; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 822 | case BPF_LD | BPF_W | BPF_ABS: |
| 823 | case BPF_LD | BPF_H | BPF_ABS: |
| 824 | case BPF_LD | BPF_B | BPF_ABS: |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 825 | anc_found = false; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 826 | if (bpf_anc_helper(ftest) & BPF_ANC) |
| 827 | anc_found = true; |
| 828 | /* Ancillary operation unknown or unsupported */ |
Daniel Borkmann | aa1113d | 2012-12-28 10:50:17 +0000 | [diff] [blame] | 829 | if (anc_found == false && ftest->k >= SKF_AD_OFF) |
| 830 | return -EINVAL; |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 834 | /* Last instruction must be a RET code */ |
Hagen Paul Pfeifer | 01f2f3f | 2010-06-19 17:05:36 +0000 | [diff] [blame] | 835 | switch (filter[flen - 1].code) { |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 836 | case BPF_RET | BPF_K: |
| 837 | case BPF_RET | BPF_A: |
Eric Dumazet | 2d5311e | 2010-12-01 20:46:24 +0000 | [diff] [blame] | 838 | return check_load_and_stores(filter, flen); |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 839 | } |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 840 | |
Tetsuo Handa | cba328f | 2010-11-16 15:19:51 +0000 | [diff] [blame] | 841 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 844 | static int bpf_prog_store_orig_filter(struct bpf_prog *fp, |
| 845 | const struct sock_fprog *fprog) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 846 | { |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 847 | unsigned int fsize = bpf_classic_proglen(fprog); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 848 | struct sock_fprog_kern *fkprog; |
| 849 | |
| 850 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); |
| 851 | if (!fp->orig_prog) |
| 852 | return -ENOMEM; |
| 853 | |
| 854 | fkprog = fp->orig_prog; |
| 855 | fkprog->len = fprog->len; |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 856 | |
| 857 | fkprog->filter = kmemdup(fp->insns, fsize, |
| 858 | GFP_KERNEL | __GFP_NOWARN); |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 859 | if (!fkprog->filter) { |
| 860 | kfree(fp->orig_prog); |
| 861 | return -ENOMEM; |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 867 | static void bpf_release_orig_filter(struct bpf_prog *fp) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 868 | { |
| 869 | struct sock_fprog_kern *fprog = fp->orig_prog; |
| 870 | |
| 871 | if (fprog) { |
| 872 | kfree(fprog->filter); |
| 873 | kfree(fprog); |
| 874 | } |
| 875 | } |
| 876 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 877 | static void __bpf_prog_release(struct bpf_prog *prog) |
| 878 | { |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 879 | if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 880 | bpf_prog_put(prog); |
| 881 | } else { |
| 882 | bpf_release_orig_filter(prog); |
| 883 | bpf_prog_free(prog); |
| 884 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 887 | static void __sk_filter_release(struct sk_filter *fp) |
| 888 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 889 | __bpf_prog_release(fp->prog); |
| 890 | kfree(fp); |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 891 | } |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | /** |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 894 | * sk_filter_release_rcu - Release a socket filter by rcu_head |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 895 | * @rcu: rcu_head that contains the sk_filter to free |
| 896 | */ |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 897 | static void sk_filter_release_rcu(struct rcu_head *rcu) |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 898 | { |
| 899 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); |
| 900 | |
Pablo Neira | 34c5bd6 | 2014-07-29 17:36:28 +0200 | [diff] [blame] | 901 | __sk_filter_release(fp); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 902 | } |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 903 | |
| 904 | /** |
| 905 | * sk_filter_release - release a socket filter |
| 906 | * @fp: filter to remove |
| 907 | * |
| 908 | * Remove a filter from a socket and release its resources. |
| 909 | */ |
| 910 | static void sk_filter_release(struct sk_filter *fp) |
| 911 | { |
| 912 | if (atomic_dec_and_test(&fp->refcnt)) |
| 913 | call_rcu(&fp->rcu, sk_filter_release_rcu); |
| 914 | } |
| 915 | |
| 916 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) |
| 917 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 918 | u32 filter_size = bpf_prog_size(fp->prog->len); |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 919 | |
| 920 | atomic_sub(filter_size, &sk->sk_omem_alloc); |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 921 | sk_filter_release(fp); |
| 922 | } |
| 923 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 924 | /* try to charge the socket memory if there is space available |
| 925 | * return true on success |
| 926 | */ |
| 927 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) |
Daniel Borkmann | fbc907f | 2014-03-28 18:58:20 +0100 | [diff] [blame] | 928 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 929 | u32 filter_size = bpf_prog_size(fp->prog->len); |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 930 | |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 931 | /* same check as in sock_kmalloc() */ |
| 932 | if (filter_size <= sysctl_optmem_max && |
| 933 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { |
| 934 | atomic_inc(&fp->refcnt); |
| 935 | atomic_add(filter_size, &sk->sk_omem_alloc); |
| 936 | return true; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 937 | } |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 938 | return false; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 941 | static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 942 | { |
| 943 | struct sock_filter *old_prog; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 944 | struct bpf_prog *old_fp; |
Daniel Borkmann | 3480593 | 2014-05-29 10:22:50 +0200 | [diff] [blame] | 945 | int err, new_len, old_len = fp->len; |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 946 | |
| 947 | /* We are free to overwrite insns et al right here as it |
| 948 | * won't be used at this point in time anymore internally |
| 949 | * after the migration to the internal BPF instruction |
| 950 | * representation. |
| 951 | */ |
| 952 | BUILD_BUG_ON(sizeof(struct sock_filter) != |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 953 | sizeof(struct bpf_insn)); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 954 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 955 | /* Conversion cannot happen on overlapping memory areas, |
| 956 | * so we need to keep the user BPF around until the 2nd |
| 957 | * pass. At this time, the user BPF is stored in fp->insns. |
| 958 | */ |
| 959 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), |
Daniel Borkmann | 658da93 | 2015-05-06 16:12:29 +0200 | [diff] [blame] | 960 | GFP_KERNEL | __GFP_NOWARN); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 961 | if (!old_prog) { |
| 962 | err = -ENOMEM; |
| 963 | goto out_err; |
| 964 | } |
| 965 | |
| 966 | /* 1st pass: calculate the new program length. */ |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 967 | err = bpf_convert_filter(old_prog, old_len, NULL, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 968 | if (err) |
| 969 | goto out_err_free; |
| 970 | |
| 971 | /* Expand fp for appending the new filter representation. */ |
| 972 | old_fp = fp; |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 973 | fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 974 | if (!fp) { |
| 975 | /* The old_fp is still around in case we couldn't |
| 976 | * allocate new memory, so uncharge on that one. |
| 977 | */ |
| 978 | fp = old_fp; |
| 979 | err = -ENOMEM; |
| 980 | goto out_err_free; |
| 981 | } |
| 982 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 983 | fp->len = new_len; |
| 984 | |
Alexei Starovoitov | 2695fb5 | 2014-07-24 16:38:21 -0700 | [diff] [blame] | 985 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 986 | err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 987 | if (err) |
Alexei Starovoitov | 8fb575c | 2014-07-30 20:34:15 -0700 | [diff] [blame] | 988 | /* 2nd bpf_convert_filter() can fail only if it fails |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 989 | * to allocate memory, remapping must succeed. Note, |
| 990 | * that at this time old_fp has already been released |
Alexei Starovoitov | 278571b | 2014-07-30 20:34:12 -0700 | [diff] [blame] | 991 | * by krealloc(). |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 992 | */ |
| 993 | goto out_err_free; |
| 994 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 995 | bpf_prog_select_runtime(fp); |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 996 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 997 | kfree(old_prog); |
| 998 | return fp; |
| 999 | |
| 1000 | out_err_free: |
| 1001 | kfree(old_prog); |
| 1002 | out_err: |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1003 | __bpf_prog_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1004 | return ERR_PTR(err); |
| 1005 | } |
| 1006 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1007 | static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, |
| 1008 | bpf_aux_classic_check_t trans) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1009 | { |
| 1010 | int err; |
| 1011 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1012 | fp->bpf_func = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1013 | fp->jited = 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1014 | |
Alexei Starovoitov | 4df95ff | 2014-07-30 20:34:14 -0700 | [diff] [blame] | 1015 | err = bpf_check_classic(fp->insns, fp->len); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 1016 | if (err) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1017 | __bpf_prog_release(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1018 | return ERR_PTR(err); |
Leon Yu | 418c96a | 2014-06-01 05:37:25 +0000 | [diff] [blame] | 1019 | } |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1020 | |
Nicolas Schichan | 4ae92bc | 2015-05-06 16:12:27 +0200 | [diff] [blame] | 1021 | /* There might be additional checks and transformations |
| 1022 | * needed on classic filters, f.e. in case of seccomp. |
| 1023 | */ |
| 1024 | if (trans) { |
| 1025 | err = trans(fp->insns, fp->len); |
| 1026 | if (err) { |
| 1027 | __bpf_prog_release(fp); |
| 1028 | return ERR_PTR(err); |
| 1029 | } |
| 1030 | } |
| 1031 | |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1032 | /* Probe if we can JIT compile the filter and if so, do |
| 1033 | * the compilation of the filter. |
| 1034 | */ |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1035 | bpf_jit_compile(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1036 | |
| 1037 | /* JIT compiler couldn't process this filter, so do the |
| 1038 | * internal BPF translation for the optimized interpreter. |
| 1039 | */ |
Alexei Starovoitov | 5fe821a | 2014-05-19 14:56:14 -0700 | [diff] [blame] | 1040 | if (!fp->jited) |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1041 | fp = bpf_migrate_filter(fp); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1042 | |
| 1043 | return fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /** |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1047 | * bpf_prog_create - create an unattached filter |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 1048 | * @pfp: the unattached filter that is created |
Tobias Klauser | 677a9fd | 2014-06-24 15:33:21 +0200 | [diff] [blame] | 1049 | * @fprog: the filter program |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1050 | * |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 1051 | * Create a filter independent of any socket. We first run some |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1052 | * sanity checks on it to make sure it does not explode on us later. |
| 1053 | * If an error occurs or there is insufficient memory for the filter |
| 1054 | * a negative errno code is returned. On success the return is zero. |
| 1055 | */ |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1056 | 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] | 1057 | { |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 1058 | unsigned int fsize = bpf_classic_proglen(fprog); |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1059 | struct bpf_prog *fp; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1060 | |
| 1061 | /* Make sure new filter is there and in the right amounts. */ |
| 1062 | if (fprog->filter == NULL) |
| 1063 | return -EINVAL; |
| 1064 | |
Daniel Borkmann | 60a3b22 | 2014-09-02 22:53:44 +0200 | [diff] [blame] | 1065 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1066 | if (!fp) |
| 1067 | return -ENOMEM; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1068 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1069 | memcpy(fp->insns, fprog->filter, fsize); |
| 1070 | |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1071 | fp->len = fprog->len; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1072 | /* Since unattached filters are not copied back to user |
| 1073 | * space through sk_get_filter(), we do not need to hold |
| 1074 | * a copy here, and can spare us the work. |
| 1075 | */ |
| 1076 | fp->orig_prog = NULL; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1077 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1078 | /* bpf_prepare_filter() already takes care of freeing |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1079 | * memory in case something goes wrong. |
| 1080 | */ |
Nicolas Schichan | 4ae92bc | 2015-05-06 16:12:27 +0200 | [diff] [blame] | 1081 | fp = bpf_prepare_filter(fp, NULL); |
Alexei Starovoitov | bd4cf0e | 2014-03-28 18:58:25 +0100 | [diff] [blame] | 1082 | if (IS_ERR(fp)) |
| 1083 | return PTR_ERR(fp); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1084 | |
| 1085 | *pfp = fp; |
| 1086 | return 0; |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1087 | } |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1088 | EXPORT_SYMBOL_GPL(bpf_prog_create); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1089 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1090 | /** |
| 1091 | * bpf_prog_create_from_user - create an unattached filter from user buffer |
| 1092 | * @pfp: the unattached filter that is created |
| 1093 | * @fprog: the filter program |
| 1094 | * @trans: post-classic verifier transformation handler |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1095 | * @save_orig: save classic BPF program |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1096 | * |
| 1097 | * This function effectively does the same as bpf_prog_create(), only |
| 1098 | * that it builds up its insns buffer from user space provided buffer. |
| 1099 | * It also allows for passing a bpf_aux_classic_check_t handler. |
| 1100 | */ |
| 1101 | 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] | 1102 | bpf_aux_classic_check_t trans, bool save_orig) |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1103 | { |
| 1104 | unsigned int fsize = bpf_classic_proglen(fprog); |
| 1105 | struct bpf_prog *fp; |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1106 | int err; |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* Make sure new filter is there and in the right amounts. */ |
| 1109 | if (fprog->filter == NULL) |
| 1110 | return -EINVAL; |
| 1111 | |
| 1112 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
| 1113 | if (!fp) |
| 1114 | return -ENOMEM; |
| 1115 | |
| 1116 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { |
| 1117 | __bpf_prog_free(fp); |
| 1118 | return -EFAULT; |
| 1119 | } |
| 1120 | |
| 1121 | fp->len = fprog->len; |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1122 | fp->orig_prog = NULL; |
| 1123 | |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1124 | if (save_orig) { |
| 1125 | err = bpf_prog_store_orig_filter(fp, fprog); |
| 1126 | if (err) { |
| 1127 | __bpf_prog_free(fp); |
| 1128 | return -ENOMEM; |
| 1129 | } |
| 1130 | } |
| 1131 | |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1132 | /* bpf_prepare_filter() already takes care of freeing |
| 1133 | * memory in case something goes wrong. |
| 1134 | */ |
| 1135 | fp = bpf_prepare_filter(fp, trans); |
| 1136 | if (IS_ERR(fp)) |
| 1137 | return PTR_ERR(fp); |
| 1138 | |
| 1139 | *pfp = fp; |
| 1140 | return 0; |
| 1141 | } |
David S. Miller | 2ea273d | 2015-08-17 14:37:06 -0700 | [diff] [blame] | 1142 | EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); |
Daniel Borkmann | ac67eb2 | 2015-05-06 16:12:30 +0200 | [diff] [blame] | 1143 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1144 | void bpf_prog_destroy(struct bpf_prog *fp) |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1145 | { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1146 | __bpf_prog_release(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 | EXPORT_SYMBOL_GPL(bpf_prog_destroy); |
Jiri Pirko | 302d663 | 2012-03-31 11:01:19 +0000 | [diff] [blame] | 1149 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1150 | static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) |
| 1151 | { |
| 1152 | struct sk_filter *fp, *old_fp; |
| 1153 | |
| 1154 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); |
| 1155 | if (!fp) |
| 1156 | return -ENOMEM; |
| 1157 | |
| 1158 | fp->prog = prog; |
| 1159 | atomic_set(&fp->refcnt, 0); |
| 1160 | |
| 1161 | if (!sk_filter_charge(sk, fp)) { |
| 1162 | kfree(fp); |
| 1163 | return -ENOMEM; |
| 1164 | } |
| 1165 | |
| 1166 | old_fp = rcu_dereference_protected(sk->sk_filter, |
| 1167 | sock_owned_by_user(sk)); |
| 1168 | rcu_assign_pointer(sk->sk_filter, fp); |
| 1169 | |
| 1170 | if (old_fp) |
| 1171 | sk_filter_uncharge(sk, old_fp); |
| 1172 | |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1176 | static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk) |
| 1177 | { |
| 1178 | struct bpf_prog *old_prog; |
| 1179 | int err; |
| 1180 | |
| 1181 | if (bpf_prog_size(prog->len) > sysctl_optmem_max) |
| 1182 | return -ENOMEM; |
| 1183 | |
| 1184 | if (sk_unhashed(sk)) { |
| 1185 | err = reuseport_alloc(sk); |
| 1186 | if (err) |
| 1187 | return err; |
| 1188 | } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) { |
| 1189 | /* The socket wasn't bound with SO_REUSEPORT */ |
| 1190 | return -EINVAL; |
| 1191 | } |
| 1192 | |
| 1193 | old_prog = reuseport_attach_prog(sk, prog); |
| 1194 | if (old_prog) |
| 1195 | bpf_prog_destroy(old_prog); |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | static |
| 1201 | struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) |
| 1202 | { |
| 1203 | unsigned int fsize = bpf_classic_proglen(fprog); |
| 1204 | unsigned int bpf_fsize = bpf_prog_size(fprog->len); |
| 1205 | struct bpf_prog *prog; |
| 1206 | int err; |
| 1207 | |
| 1208 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1209 | return ERR_PTR(-EPERM); |
| 1210 | |
| 1211 | /* Make sure new filter is there and in the right amounts. */ |
| 1212 | if (fprog->filter == NULL) |
| 1213 | return ERR_PTR(-EINVAL); |
| 1214 | |
| 1215 | prog = bpf_prog_alloc(bpf_fsize, 0); |
| 1216 | if (!prog) |
| 1217 | return ERR_PTR(-ENOMEM); |
| 1218 | |
| 1219 | if (copy_from_user(prog->insns, fprog->filter, fsize)) { |
| 1220 | __bpf_prog_free(prog); |
| 1221 | return ERR_PTR(-EFAULT); |
| 1222 | } |
| 1223 | |
| 1224 | prog->len = fprog->len; |
| 1225 | |
| 1226 | err = bpf_prog_store_orig_filter(prog, fprog); |
| 1227 | if (err) { |
| 1228 | __bpf_prog_free(prog); |
| 1229 | return ERR_PTR(-ENOMEM); |
| 1230 | } |
| 1231 | |
| 1232 | /* bpf_prepare_filter() already takes care of freeing |
| 1233 | * memory in case something goes wrong. |
| 1234 | */ |
| 1235 | return bpf_prepare_filter(prog, NULL); |
| 1236 | } |
| 1237 | |
Pavel Emelyanov | 47e958e | 2007-10-17 21:22:42 -0700 | [diff] [blame] | 1238 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | * sk_attach_filter - attach a socket filter |
| 1240 | * @fprog: the filter program |
| 1241 | * @sk: the socket to use |
| 1242 | * |
| 1243 | * Attach the user's filter code. We first run some sanity checks on |
| 1244 | * it to make sure it does not explode on us later. If an error |
| 1245 | * occurs or there is insufficient memory for the filter a negative |
| 1246 | * errno code is returned. On success the return is zero. |
| 1247 | */ |
| 1248 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
| 1249 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1250 | struct bpf_prog *prog = __get_filter(fprog, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | int err; |
| 1252 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1253 | if (IS_ERR(prog)) |
| 1254 | return PTR_ERR(prog); |
| 1255 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1256 | err = __sk_attach_prog(prog, sk); |
| 1257 | if (err < 0) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1258 | __bpf_prog_release(prog); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1259 | return err; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 1260 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 1261 | |
Pavel Emelyanov | d3904b7 | 2007-10-17 21:22:17 -0700 | [diff] [blame] | 1262 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 1264 | EXPORT_SYMBOL_GPL(sk_attach_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1266 | int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1267 | { |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1268 | struct bpf_prog *prog = __get_filter(fprog, sk); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1269 | int err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1270 | |
Alexei Starovoitov | 198bf1b | 2014-12-10 20:14:55 -0800 | [diff] [blame] | 1271 | if (IS_ERR(prog)) |
| 1272 | return PTR_ERR(prog); |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1273 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1274 | err = __reuseport_attach_prog(prog, sk); |
| 1275 | if (err < 0) { |
| 1276 | __bpf_prog_release(prog); |
| 1277 | return err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) |
| 1284 | { |
| 1285 | struct bpf_prog *prog; |
| 1286 | |
| 1287 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 1288 | return ERR_PTR(-EPERM); |
| 1289 | |
| 1290 | prog = bpf_prog_get(ufd); |
| 1291 | if (IS_ERR(prog)) |
| 1292 | return prog; |
| 1293 | |
| 1294 | if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) { |
| 1295 | bpf_prog_put(prog); |
| 1296 | return ERR_PTR(-EINVAL); |
| 1297 | } |
| 1298 | |
| 1299 | return prog; |
| 1300 | } |
| 1301 | |
| 1302 | int sk_attach_bpf(u32 ufd, struct sock *sk) |
| 1303 | { |
| 1304 | struct bpf_prog *prog = __get_bpf(ufd, sk); |
| 1305 | int err; |
| 1306 | |
| 1307 | if (IS_ERR(prog)) |
| 1308 | return PTR_ERR(prog); |
| 1309 | |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1310 | err = __sk_attach_prog(prog, sk); |
| 1311 | if (err < 0) { |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1312 | bpf_prog_put(prog); |
Daniel Borkmann | 49b31e5 | 2015-03-02 12:25:51 +0100 | [diff] [blame] | 1313 | return err; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1314 | } |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1315 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 1319 | int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) |
| 1320 | { |
| 1321 | struct bpf_prog *prog = __get_bpf(ufd, sk); |
| 1322 | int err; |
| 1323 | |
| 1324 | if (IS_ERR(prog)) |
| 1325 | return PTR_ERR(prog); |
| 1326 | |
| 1327 | err = __reuseport_attach_prog(prog, sk); |
| 1328 | if (err < 0) { |
| 1329 | bpf_prog_put(prog); |
| 1330 | return err; |
| 1331 | } |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1336 | #define BPF_LDST_LEN 16U |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1337 | |
| 1338 | 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] | 1339 | { |
| 1340 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1341 | int offset = (int) r2; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1342 | void *from = (void *) (long) r3; |
| 1343 | unsigned int len = (unsigned int) r4; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1344 | char buf[BPF_LDST_LEN]; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1345 | void *ptr; |
| 1346 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1347 | if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM))) |
| 1348 | return -EINVAL; |
| 1349 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1350 | /* bpf verifier guarantees that: |
| 1351 | * 'from' pointer points to bpf program stack |
| 1352 | * 'len' bytes of it were initialized |
| 1353 | * 'len' > 0 |
| 1354 | * 'skb' is a valid pointer to 'struct sk_buff' |
| 1355 | * |
| 1356 | * so check for invalid 'offset' and too large 'len' |
| 1357 | */ |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1358 | if (unlikely((u32) offset > 0xffff || len > sizeof(buf))) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1359 | return -EFAULT; |
| 1360 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1361 | if (unlikely(skb_cloned(skb) && |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 1362 | !skb_clone_writable(skb, offset + len))) |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1363 | return -EFAULT; |
| 1364 | |
| 1365 | ptr = skb_header_pointer(skb, offset, len, buf); |
| 1366 | if (unlikely(!ptr)) |
| 1367 | return -EFAULT; |
| 1368 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1369 | if (flags & BPF_F_RECOMPUTE_CSUM) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1370 | skb_postpull_rcsum(skb, ptr, len); |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1371 | |
| 1372 | memcpy(ptr, from, len); |
| 1373 | |
| 1374 | if (ptr == buf) |
| 1375 | /* skb_store_bits cannot return -EFAULT here */ |
| 1376 | skb_store_bits(skb, offset, ptr, len); |
| 1377 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1378 | if (flags & BPF_F_RECOMPUTE_CSUM) |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1379 | skb_postpush_rcsum(skb, ptr, len); |
| 1380 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | const struct bpf_func_proto bpf_skb_store_bytes_proto = { |
| 1385 | .func = bpf_skb_store_bytes, |
| 1386 | .gpl_only = false, |
| 1387 | .ret_type = RET_INTEGER, |
| 1388 | .arg1_type = ARG_PTR_TO_CTX, |
| 1389 | .arg2_type = ARG_ANYTHING, |
| 1390 | .arg3_type = ARG_PTR_TO_STACK, |
| 1391 | .arg4_type = ARG_CONST_STACK_SIZE, |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1392 | .arg5_type = ARG_ANYTHING, |
| 1393 | }; |
| 1394 | |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1395 | static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1396 | { |
| 1397 | const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1; |
| 1398 | int offset = (int) r2; |
| 1399 | void *to = (void *)(unsigned long) r3; |
| 1400 | unsigned int len = (unsigned int) r4; |
| 1401 | void *ptr; |
| 1402 | |
| 1403 | if (unlikely((u32) offset > 0xffff || len > BPF_LDST_LEN)) |
| 1404 | return -EFAULT; |
| 1405 | |
| 1406 | ptr = skb_header_pointer(skb, offset, len, to); |
| 1407 | if (unlikely(!ptr)) |
| 1408 | return -EFAULT; |
| 1409 | if (ptr != to) |
| 1410 | memcpy(to, ptr, len); |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | const struct bpf_func_proto bpf_skb_load_bytes_proto = { |
| 1416 | .func = bpf_skb_load_bytes, |
| 1417 | .gpl_only = false, |
| 1418 | .ret_type = RET_INTEGER, |
| 1419 | .arg1_type = ARG_PTR_TO_CTX, |
| 1420 | .arg2_type = ARG_ANYTHING, |
| 1421 | .arg3_type = ARG_PTR_TO_STACK, |
| 1422 | .arg4_type = ARG_CONST_STACK_SIZE, |
| 1423 | }; |
| 1424 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1425 | 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] | 1426 | { |
| 1427 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1428 | int offset = (int) r2; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1429 | __sum16 sum, *ptr; |
| 1430 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1431 | if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) |
| 1432 | return -EINVAL; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1433 | if (unlikely((u32) offset > 0xffff)) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1434 | return -EFAULT; |
| 1435 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1436 | if (unlikely(skb_cloned(skb) && |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 1437 | !skb_clone_writable(skb, offset + sizeof(sum)))) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1438 | return -EFAULT; |
| 1439 | |
| 1440 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); |
| 1441 | if (unlikely(!ptr)) |
| 1442 | return -EFAULT; |
| 1443 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1444 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1445 | case 2: |
| 1446 | csum_replace2(ptr, from, to); |
| 1447 | break; |
| 1448 | case 4: |
| 1449 | csum_replace4(ptr, from, to); |
| 1450 | break; |
| 1451 | default: |
| 1452 | return -EINVAL; |
| 1453 | } |
| 1454 | |
| 1455 | if (ptr == &sum) |
| 1456 | /* skb_store_bits guaranteed to not return -EFAULT here */ |
| 1457 | skb_store_bits(skb, offset, ptr, sizeof(sum)); |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | const struct bpf_func_proto bpf_l3_csum_replace_proto = { |
| 1463 | .func = bpf_l3_csum_replace, |
| 1464 | .gpl_only = false, |
| 1465 | .ret_type = RET_INTEGER, |
| 1466 | .arg1_type = ARG_PTR_TO_CTX, |
| 1467 | .arg2_type = ARG_ANYTHING, |
| 1468 | .arg3_type = ARG_ANYTHING, |
| 1469 | .arg4_type = ARG_ANYTHING, |
| 1470 | .arg5_type = ARG_ANYTHING, |
| 1471 | }; |
| 1472 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1473 | 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] | 1474 | { |
| 1475 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1476 | bool is_pseudo = flags & BPF_F_PSEUDO_HDR; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1477 | int offset = (int) r2; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1478 | __sum16 sum, *ptr; |
| 1479 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1480 | if (unlikely(flags & ~(BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK))) |
| 1481 | return -EINVAL; |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1482 | if (unlikely((u32) offset > 0xffff)) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1483 | return -EFAULT; |
| 1484 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 1485 | if (unlikely(skb_cloned(skb) && |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 1486 | !skb_clone_writable(skb, offset + sizeof(sum)))) |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1487 | return -EFAULT; |
| 1488 | |
| 1489 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); |
| 1490 | if (unlikely(!ptr)) |
| 1491 | return -EFAULT; |
| 1492 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1493 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1494 | case 2: |
| 1495 | inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); |
| 1496 | break; |
| 1497 | case 4: |
| 1498 | inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); |
| 1499 | break; |
| 1500 | default: |
| 1501 | return -EINVAL; |
| 1502 | } |
| 1503 | |
| 1504 | if (ptr == &sum) |
| 1505 | /* skb_store_bits guaranteed to not return -EFAULT here */ |
| 1506 | skb_store_bits(skb, offset, ptr, sizeof(sum)); |
| 1507 | |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | const struct bpf_func_proto bpf_l4_csum_replace_proto = { |
| 1512 | .func = bpf_l4_csum_replace, |
| 1513 | .gpl_only = false, |
| 1514 | .ret_type = RET_INTEGER, |
| 1515 | .arg1_type = ARG_PTR_TO_CTX, |
| 1516 | .arg2_type = ARG_ANYTHING, |
| 1517 | .arg3_type = ARG_ANYTHING, |
| 1518 | .arg4_type = ARG_ANYTHING, |
| 1519 | .arg5_type = ARG_ANYTHING, |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1520 | }; |
| 1521 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1522 | static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5) |
| 1523 | { |
| 1524 | struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2; |
| 1525 | struct net_device *dev; |
| 1526 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1527 | if (unlikely(flags & ~(BPF_F_INGRESS))) |
| 1528 | return -EINVAL; |
| 1529 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1530 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); |
| 1531 | if (unlikely(!dev)) |
| 1532 | return -EINVAL; |
| 1533 | |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1534 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1535 | if (unlikely(!skb2)) |
| 1536 | return -ENOMEM; |
| 1537 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1538 | if (flags & BPF_F_INGRESS) { |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1539 | if (skb_at_tc_ingress(skb2)) |
| 1540 | skb_postpush_rcsum(skb2, skb_mac_header(skb2), |
| 1541 | skb2->mac_len); |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1542 | return dev_forward_skb(dev, skb2); |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1543 | } |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1544 | |
| 1545 | skb2->dev = dev; |
Alexei Starovoitov | 6bf0577 | 2015-10-06 20:46:07 -0700 | [diff] [blame] | 1546 | skb_sender_cpu_clear(skb2); |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1547 | return dev_queue_xmit(skb2); |
| 1548 | } |
| 1549 | |
| 1550 | const struct bpf_func_proto bpf_clone_redirect_proto = { |
| 1551 | .func = bpf_clone_redirect, |
| 1552 | .gpl_only = false, |
| 1553 | .ret_type = RET_INTEGER, |
| 1554 | .arg1_type = ARG_PTR_TO_CTX, |
| 1555 | .arg2_type = ARG_ANYTHING, |
| 1556 | .arg3_type = ARG_ANYTHING, |
| 1557 | }; |
| 1558 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1559 | struct redirect_info { |
| 1560 | u32 ifindex; |
| 1561 | u32 flags; |
| 1562 | }; |
| 1563 | |
| 1564 | static DEFINE_PER_CPU(struct redirect_info, redirect_info); |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1565 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1566 | static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5) |
| 1567 | { |
| 1568 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); |
| 1569 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1570 | if (unlikely(flags & ~(BPF_F_INGRESS))) |
| 1571 | return TC_ACT_SHOT; |
| 1572 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1573 | ri->ifindex = ifindex; |
| 1574 | ri->flags = flags; |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1575 | |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1576 | return TC_ACT_REDIRECT; |
| 1577 | } |
| 1578 | |
| 1579 | int skb_do_redirect(struct sk_buff *skb) |
| 1580 | { |
| 1581 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); |
| 1582 | struct net_device *dev; |
| 1583 | |
| 1584 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex); |
| 1585 | ri->ifindex = 0; |
| 1586 | if (unlikely(!dev)) { |
| 1587 | kfree_skb(skb); |
| 1588 | return -EINVAL; |
| 1589 | } |
| 1590 | |
Daniel Borkmann | 781c53b | 2016-01-11 01:16:38 +0100 | [diff] [blame] | 1591 | if (ri->flags & BPF_F_INGRESS) { |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1592 | if (skb_at_tc_ingress(skb)) |
| 1593 | skb_postpush_rcsum(skb, skb_mac_header(skb), |
| 1594 | skb->mac_len); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1595 | return dev_forward_skb(dev, skb); |
Daniel Borkmann | f8ffad69 | 2016-01-07 15:50:23 +0100 | [diff] [blame] | 1596 | } |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1597 | |
| 1598 | skb->dev = dev; |
Daniel Borkmann | cfc81b5 | 2015-10-07 10:16:09 +0200 | [diff] [blame] | 1599 | skb_sender_cpu_clear(skb); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1600 | return dev_queue_xmit(skb); |
| 1601 | } |
| 1602 | |
| 1603 | const struct bpf_func_proto bpf_redirect_proto = { |
| 1604 | .func = bpf_redirect, |
| 1605 | .gpl_only = false, |
| 1606 | .ret_type = RET_INTEGER, |
| 1607 | .arg1_type = ARG_ANYTHING, |
| 1608 | .arg2_type = ARG_ANYTHING, |
| 1609 | }; |
| 1610 | |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 1611 | static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1612 | { |
| 1613 | return task_get_classid((struct sk_buff *) (unsigned long) r1); |
| 1614 | } |
| 1615 | |
| 1616 | static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { |
| 1617 | .func = bpf_get_cgroup_classid, |
| 1618 | .gpl_only = false, |
| 1619 | .ret_type = RET_INTEGER, |
| 1620 | .arg1_type = ARG_PTR_TO_CTX, |
| 1621 | }; |
| 1622 | |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 1623 | static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1624 | { |
| 1625 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 1626 | const struct dst_entry *dst; |
| 1627 | |
| 1628 | dst = skb_dst((struct sk_buff *) (unsigned long) r1); |
| 1629 | if (dst) |
| 1630 | return dst->tclassid; |
| 1631 | #endif |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static const struct bpf_func_proto bpf_get_route_realm_proto = { |
| 1636 | .func = bpf_get_route_realm, |
| 1637 | .gpl_only = false, |
| 1638 | .ret_type = RET_INTEGER, |
| 1639 | .arg1_type = ARG_PTR_TO_CTX, |
| 1640 | }; |
| 1641 | |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1642 | static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5) |
| 1643 | { |
| 1644 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1645 | __be16 vlan_proto = (__force __be16) r2; |
| 1646 | |
| 1647 | if (unlikely(vlan_proto != htons(ETH_P_8021Q) && |
| 1648 | vlan_proto != htons(ETH_P_8021AD))) |
| 1649 | vlan_proto = htons(ETH_P_8021Q); |
| 1650 | |
| 1651 | return skb_vlan_push(skb, vlan_proto, vlan_tci); |
| 1652 | } |
| 1653 | |
| 1654 | const struct bpf_func_proto bpf_skb_vlan_push_proto = { |
| 1655 | .func = bpf_skb_vlan_push, |
| 1656 | .gpl_only = false, |
| 1657 | .ret_type = RET_INTEGER, |
| 1658 | .arg1_type = ARG_PTR_TO_CTX, |
| 1659 | .arg2_type = ARG_ANYTHING, |
| 1660 | .arg3_type = ARG_ANYTHING, |
| 1661 | }; |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 1662 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto); |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1663 | |
| 1664 | static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1665 | { |
| 1666 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1667 | |
| 1668 | return skb_vlan_pop(skb); |
| 1669 | } |
| 1670 | |
| 1671 | const struct bpf_func_proto bpf_skb_vlan_pop_proto = { |
| 1672 | .func = bpf_skb_vlan_pop, |
| 1673 | .gpl_only = false, |
| 1674 | .ret_type = RET_INTEGER, |
| 1675 | .arg1_type = ARG_PTR_TO_CTX, |
| 1676 | }; |
Alexei Starovoitov | 4d9c5c5 | 2015-07-20 20:34:19 -0700 | [diff] [blame] | 1677 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto); |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1678 | |
| 1679 | bool bpf_helper_changes_skb_data(void *func) |
| 1680 | { |
| 1681 | if (func == bpf_skb_vlan_push) |
| 1682 | return true; |
| 1683 | if (func == bpf_skb_vlan_pop) |
| 1684 | return true; |
| 1685 | return false; |
| 1686 | } |
| 1687 | |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1688 | static unsigned short bpf_tunnel_key_af(u64 flags) |
| 1689 | { |
| 1690 | return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; |
| 1691 | } |
| 1692 | |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1693 | static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5) |
| 1694 | { |
| 1695 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1696 | struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1697 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); |
| 1698 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1699 | |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1700 | if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1701 | return -EINVAL; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1702 | if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) |
| 1703 | return -EPROTO; |
| 1704 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
| 1705 | switch (size) { |
| 1706 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
| 1707 | /* Fixup deprecated structure layouts here, so we have |
| 1708 | * a common path later on. |
| 1709 | */ |
| 1710 | if (ip_tunnel_info_af(info) != AF_INET) |
| 1711 | return -EINVAL; |
| 1712 | to = (struct bpf_tunnel_key *)compat; |
| 1713 | break; |
| 1714 | default: |
| 1715 | return -EINVAL; |
| 1716 | } |
| 1717 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1718 | |
| 1719 | to->tunnel_id = be64_to_cpu(info->key.tun_id); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1720 | to->tunnel_tos = info->key.tos; |
| 1721 | to->tunnel_ttl = info->key.ttl; |
| 1722 | |
| 1723 | if (flags & BPF_F_TUNINFO_IPV6) |
| 1724 | memcpy(to->remote_ipv6, &info->key.u.ipv6.src, |
| 1725 | sizeof(to->remote_ipv6)); |
| 1726 | else |
| 1727 | to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); |
| 1728 | |
| 1729 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) |
| 1730 | memcpy((void *)(long) r2, to, size); |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1731 | |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
| 1735 | const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { |
| 1736 | .func = bpf_skb_get_tunnel_key, |
| 1737 | .gpl_only = false, |
| 1738 | .ret_type = RET_INTEGER, |
| 1739 | .arg1_type = ARG_PTR_TO_CTX, |
| 1740 | .arg2_type = ARG_PTR_TO_STACK, |
| 1741 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 1742 | .arg4_type = ARG_ANYTHING, |
| 1743 | }; |
| 1744 | |
| 1745 | static struct metadata_dst __percpu *md_dst; |
| 1746 | |
| 1747 | static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5) |
| 1748 | { |
| 1749 | struct sk_buff *skb = (struct sk_buff *) (long) r1; |
| 1750 | struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2; |
| 1751 | struct metadata_dst *md = this_cpu_ptr(md_dst); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1752 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1753 | struct ip_tunnel_info *info; |
| 1754 | |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame^] | 1755 | if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX))) |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1756 | return -EINVAL; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1757 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
| 1758 | switch (size) { |
| 1759 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
| 1760 | /* Fixup deprecated structure layouts here, so we have |
| 1761 | * a common path later on. |
| 1762 | */ |
| 1763 | memcpy(compat, from, size); |
| 1764 | memset(compat + size, 0, sizeof(compat) - size); |
| 1765 | from = (struct bpf_tunnel_key *)compat; |
| 1766 | break; |
| 1767 | default: |
| 1768 | return -EINVAL; |
| 1769 | } |
| 1770 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1771 | |
| 1772 | skb_dst_drop(skb); |
| 1773 | dst_hold((struct dst_entry *) md); |
| 1774 | skb_dst_set(skb, (struct dst_entry *) md); |
| 1775 | |
| 1776 | info = &md->u.tun_info; |
| 1777 | info->mode = IP_TUNNEL_INFO_TX; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1778 | |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame^] | 1779 | info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1780 | info->key.tun_id = cpu_to_be64(from->tunnel_id); |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1781 | info->key.tos = from->tunnel_tos; |
| 1782 | info->key.ttl = from->tunnel_ttl; |
| 1783 | |
| 1784 | if (flags & BPF_F_TUNINFO_IPV6) { |
| 1785 | info->mode |= IP_TUNNEL_INFO_IPV6; |
| 1786 | memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, |
| 1787 | sizeof(from->remote_ipv6)); |
| 1788 | } else { |
| 1789 | info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); |
Daniel Borkmann | 2da897e | 2016-02-23 02:05:26 +0100 | [diff] [blame^] | 1790 | if (flags & BPF_F_ZERO_CSUM_TX) |
| 1791 | info->key.tun_flags &= ~TUNNEL_CSUM; |
Daniel Borkmann | c6c3345 | 2016-01-11 01:16:39 +0100 | [diff] [blame] | 1792 | } |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1793 | |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { |
| 1798 | .func = bpf_skb_set_tunnel_key, |
| 1799 | .gpl_only = false, |
| 1800 | .ret_type = RET_INTEGER, |
| 1801 | .arg1_type = ARG_PTR_TO_CTX, |
| 1802 | .arg2_type = ARG_PTR_TO_STACK, |
| 1803 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 1804 | .arg4_type = ARG_ANYTHING, |
| 1805 | }; |
| 1806 | |
| 1807 | static const struct bpf_func_proto *bpf_get_skb_set_tunnel_key_proto(void) |
| 1808 | { |
| 1809 | if (!md_dst) { |
| 1810 | /* race is not possible, since it's called from |
| 1811 | * verifier that is holding verifier mutex |
| 1812 | */ |
| 1813 | md_dst = metadata_dst_alloc_percpu(0, GFP_KERNEL); |
| 1814 | if (!md_dst) |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | return &bpf_skb_set_tunnel_key_proto; |
| 1818 | } |
| 1819 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 1820 | static const struct bpf_func_proto * |
| 1821 | sk_filter_func_proto(enum bpf_func_id func_id) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1822 | { |
| 1823 | switch (func_id) { |
| 1824 | case BPF_FUNC_map_lookup_elem: |
| 1825 | return &bpf_map_lookup_elem_proto; |
| 1826 | case BPF_FUNC_map_update_elem: |
| 1827 | return &bpf_map_update_elem_proto; |
| 1828 | case BPF_FUNC_map_delete_elem: |
| 1829 | return &bpf_map_delete_elem_proto; |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 1830 | case BPF_FUNC_get_prandom_u32: |
| 1831 | return &bpf_get_prandom_u32_proto; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 1832 | case BPF_FUNC_get_smp_processor_id: |
| 1833 | return &bpf_get_smp_processor_id_proto; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1834 | case BPF_FUNC_tail_call: |
| 1835 | return &bpf_tail_call_proto; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 1836 | case BPF_FUNC_ktime_get_ns: |
| 1837 | return &bpf_ktime_get_ns_proto; |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 1838 | case BPF_FUNC_trace_printk: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1839 | if (capable(CAP_SYS_ADMIN)) |
| 1840 | return bpf_get_trace_printk_proto(); |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 1841 | default: |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | } |
| 1845 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1846 | static const struct bpf_func_proto * |
| 1847 | tc_cls_act_func_proto(enum bpf_func_id func_id) |
| 1848 | { |
| 1849 | switch (func_id) { |
| 1850 | case BPF_FUNC_skb_store_bytes: |
| 1851 | return &bpf_skb_store_bytes_proto; |
Daniel Borkmann | 05c74e5 | 2015-12-17 23:51:53 +0100 | [diff] [blame] | 1852 | case BPF_FUNC_skb_load_bytes: |
| 1853 | return &bpf_skb_load_bytes_proto; |
Alexei Starovoitov | 91bc4822 | 2015-04-01 17:12:13 -0700 | [diff] [blame] | 1854 | case BPF_FUNC_l3_csum_replace: |
| 1855 | return &bpf_l3_csum_replace_proto; |
| 1856 | case BPF_FUNC_l4_csum_replace: |
| 1857 | return &bpf_l4_csum_replace_proto; |
Alexei Starovoitov | 3896d65 | 2015-06-02 16:03:14 -0700 | [diff] [blame] | 1858 | case BPF_FUNC_clone_redirect: |
| 1859 | return &bpf_clone_redirect_proto; |
Daniel Borkmann | 8d20aab | 2015-07-15 14:21:42 +0200 | [diff] [blame] | 1860 | case BPF_FUNC_get_cgroup_classid: |
| 1861 | return &bpf_get_cgroup_classid_proto; |
Alexei Starovoitov | 4e10df9 | 2015-07-20 20:34:18 -0700 | [diff] [blame] | 1862 | case BPF_FUNC_skb_vlan_push: |
| 1863 | return &bpf_skb_vlan_push_proto; |
| 1864 | case BPF_FUNC_skb_vlan_pop: |
| 1865 | return &bpf_skb_vlan_pop_proto; |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 1866 | case BPF_FUNC_skb_get_tunnel_key: |
| 1867 | return &bpf_skb_get_tunnel_key_proto; |
| 1868 | case BPF_FUNC_skb_set_tunnel_key: |
| 1869 | return bpf_get_skb_set_tunnel_key_proto(); |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 1870 | case BPF_FUNC_redirect: |
| 1871 | return &bpf_redirect_proto; |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 1872 | case BPF_FUNC_get_route_realm: |
| 1873 | return &bpf_get_route_realm_proto; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1874 | default: |
| 1875 | return sk_filter_func_proto(func_id); |
| 1876 | } |
| 1877 | } |
| 1878 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1879 | 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] | 1880 | { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1881 | /* check bounds */ |
| 1882 | if (off < 0 || off >= sizeof(struct __sk_buff)) |
| 1883 | return false; |
| 1884 | |
| 1885 | /* disallow misaligned access */ |
| 1886 | if (off % size != 0) |
| 1887 | return false; |
| 1888 | |
| 1889 | /* all __sk_buff fields are __u32 */ |
| 1890 | if (size != 4) |
| 1891 | return false; |
| 1892 | |
| 1893 | return true; |
| 1894 | } |
| 1895 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1896 | static bool sk_filter_is_valid_access(int off, int size, |
| 1897 | enum bpf_access_type type) |
| 1898 | { |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 1899 | if (off == offsetof(struct __sk_buff, tc_classid)) |
| 1900 | return false; |
| 1901 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1902 | if (type == BPF_WRITE) { |
| 1903 | switch (off) { |
| 1904 | case offsetof(struct __sk_buff, cb[0]) ... |
| 1905 | offsetof(struct __sk_buff, cb[4]): |
| 1906 | break; |
| 1907 | default: |
| 1908 | return false; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | return __is_valid_access(off, size, type); |
| 1913 | } |
| 1914 | |
| 1915 | static bool tc_cls_act_is_valid_access(int off, int size, |
| 1916 | enum bpf_access_type type) |
| 1917 | { |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 1918 | if (off == offsetof(struct __sk_buff, tc_classid)) |
| 1919 | return type == BPF_WRITE ? true : false; |
| 1920 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1921 | if (type == BPF_WRITE) { |
| 1922 | switch (off) { |
| 1923 | case offsetof(struct __sk_buff, mark): |
| 1924 | case offsetof(struct __sk_buff, tc_index): |
Daniel Borkmann | 754f1e6 | 2015-09-30 01:41:52 +0200 | [diff] [blame] | 1925 | case offsetof(struct __sk_buff, priority): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 1926 | case offsetof(struct __sk_buff, cb[0]) ... |
| 1927 | offsetof(struct __sk_buff, cb[4]): |
| 1928 | break; |
| 1929 | default: |
| 1930 | return false; |
| 1931 | } |
| 1932 | } |
| 1933 | return __is_valid_access(off, size, type); |
| 1934 | } |
| 1935 | |
| 1936 | static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg, |
| 1937 | int src_reg, int ctx_off, |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 1938 | struct bpf_insn *insn_buf, |
| 1939 | struct bpf_prog *prog) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1940 | { |
| 1941 | struct bpf_insn *insn = insn_buf; |
| 1942 | |
| 1943 | switch (ctx_off) { |
| 1944 | case offsetof(struct __sk_buff, len): |
| 1945 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); |
| 1946 | |
| 1947 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 1948 | offsetof(struct sk_buff, len)); |
| 1949 | break; |
| 1950 | |
Daniel Borkmann | 0b8c707 | 2015-03-19 19:38:27 +0100 | [diff] [blame] | 1951 | case offsetof(struct __sk_buff, protocol): |
| 1952 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); |
| 1953 | |
| 1954 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 1955 | offsetof(struct sk_buff, protocol)); |
| 1956 | break; |
| 1957 | |
Michal Sekletar | 27cd545 | 2015-03-24 14:48:41 +0100 | [diff] [blame] | 1958 | case offsetof(struct __sk_buff, vlan_proto): |
| 1959 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); |
| 1960 | |
| 1961 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 1962 | offsetof(struct sk_buff, vlan_proto)); |
| 1963 | break; |
| 1964 | |
Daniel Borkmann | bcad571 | 2015-04-03 20:52:24 +0200 | [diff] [blame] | 1965 | case offsetof(struct __sk_buff, priority): |
| 1966 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4); |
| 1967 | |
Daniel Borkmann | 754f1e6 | 2015-09-30 01:41:52 +0200 | [diff] [blame] | 1968 | if (type == BPF_WRITE) |
| 1969 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, |
| 1970 | offsetof(struct sk_buff, priority)); |
| 1971 | else |
| 1972 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 1973 | offsetof(struct sk_buff, priority)); |
Daniel Borkmann | bcad571 | 2015-04-03 20:52:24 +0200 | [diff] [blame] | 1974 | break; |
| 1975 | |
Alexei Starovoitov | 37e82c2 | 2015-05-27 15:30:39 -0700 | [diff] [blame] | 1976 | case offsetof(struct __sk_buff, ingress_ifindex): |
| 1977 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4); |
| 1978 | |
| 1979 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 1980 | offsetof(struct sk_buff, skb_iif)); |
| 1981 | break; |
| 1982 | |
| 1983 | case offsetof(struct __sk_buff, ifindex): |
| 1984 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); |
| 1985 | |
| 1986 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), |
| 1987 | dst_reg, src_reg, |
| 1988 | offsetof(struct sk_buff, dev)); |
| 1989 | *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1); |
| 1990 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg, |
| 1991 | offsetof(struct net_device, ifindex)); |
| 1992 | break; |
| 1993 | |
Daniel Borkmann | ba7591d | 2015-08-01 00:46:29 +0200 | [diff] [blame] | 1994 | case offsetof(struct __sk_buff, hash): |
| 1995 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); |
| 1996 | |
| 1997 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 1998 | offsetof(struct sk_buff, hash)); |
| 1999 | break; |
| 2000 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2001 | case offsetof(struct __sk_buff, mark): |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2002 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); |
| 2003 | |
| 2004 | if (type == BPF_WRITE) |
| 2005 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, |
| 2006 | offsetof(struct sk_buff, mark)); |
| 2007 | else |
| 2008 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, |
| 2009 | offsetof(struct sk_buff, mark)); |
| 2010 | break; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2011 | |
| 2012 | case offsetof(struct __sk_buff, pkt_type): |
| 2013 | return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn); |
| 2014 | |
| 2015 | case offsetof(struct __sk_buff, queue_mapping): |
| 2016 | return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn); |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 2017 | |
Alexei Starovoitov | c249739 | 2015-03-16 18:06:02 -0700 | [diff] [blame] | 2018 | case offsetof(struct __sk_buff, vlan_present): |
| 2019 | return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, |
| 2020 | dst_reg, src_reg, insn); |
| 2021 | |
| 2022 | case offsetof(struct __sk_buff, vlan_tci): |
| 2023 | return convert_skb_access(SKF_AD_VLAN_TAG, |
| 2024 | dst_reg, src_reg, insn); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2025 | |
| 2026 | case offsetof(struct __sk_buff, cb[0]) ... |
| 2027 | offsetof(struct __sk_buff, cb[4]): |
| 2028 | BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20); |
| 2029 | |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2030 | prog->cb_access = 1; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2031 | ctx_off -= offsetof(struct __sk_buff, cb[0]); |
| 2032 | ctx_off += offsetof(struct sk_buff, cb); |
| 2033 | ctx_off += offsetof(struct qdisc_skb_cb, data); |
| 2034 | if (type == BPF_WRITE) |
| 2035 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off); |
| 2036 | else |
| 2037 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off); |
| 2038 | break; |
| 2039 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 2040 | case offsetof(struct __sk_buff, tc_classid): |
| 2041 | ctx_off -= offsetof(struct __sk_buff, tc_classid); |
| 2042 | ctx_off += offsetof(struct sk_buff, cb); |
| 2043 | ctx_off += offsetof(struct qdisc_skb_cb, tc_classid); |
| 2044 | WARN_ON(type != BPF_WRITE); |
| 2045 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off); |
| 2046 | break; |
| 2047 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2048 | case offsetof(struct __sk_buff, tc_index): |
| 2049 | #ifdef CONFIG_NET_SCHED |
| 2050 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2); |
| 2051 | |
| 2052 | if (type == BPF_WRITE) |
| 2053 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, |
| 2054 | offsetof(struct sk_buff, tc_index)); |
| 2055 | else |
| 2056 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, |
| 2057 | offsetof(struct sk_buff, tc_index)); |
| 2058 | break; |
| 2059 | #else |
| 2060 | if (type == BPF_WRITE) |
| 2061 | *insn++ = BPF_MOV64_REG(dst_reg, dst_reg); |
| 2062 | else |
| 2063 | *insn++ = BPF_MOV64_IMM(dst_reg, 0); |
| 2064 | break; |
| 2065 | #endif |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | return insn - insn_buf; |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2071 | static const struct bpf_verifier_ops sk_filter_ops = { |
| 2072 | .get_func_proto = sk_filter_func_proto, |
| 2073 | .is_valid_access = sk_filter_is_valid_access, |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2074 | .convert_ctx_access = bpf_net_convert_ctx_access, |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2075 | }; |
| 2076 | |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2077 | static const struct bpf_verifier_ops tc_cls_act_ops = { |
| 2078 | .get_func_proto = tc_cls_act_func_proto, |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2079 | .is_valid_access = tc_cls_act_is_valid_access, |
| 2080 | .convert_ctx_access = bpf_net_convert_ctx_access, |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2081 | }; |
| 2082 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2083 | static struct bpf_prog_type_list sk_filter_type __read_mostly = { |
| 2084 | .ops = &sk_filter_ops, |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2085 | .type = BPF_PROG_TYPE_SOCKET_FILTER, |
| 2086 | }; |
| 2087 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2088 | static struct bpf_prog_type_list sched_cls_type __read_mostly = { |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2089 | .ops = &tc_cls_act_ops, |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2090 | .type = BPF_PROG_TYPE_SCHED_CLS, |
| 2091 | }; |
| 2092 | |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2093 | static struct bpf_prog_type_list sched_act_type __read_mostly = { |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2094 | .ops = &tc_cls_act_ops, |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2095 | .type = BPF_PROG_TYPE_SCHED_ACT, |
| 2096 | }; |
| 2097 | |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2098 | static int __init register_sk_filter_ops(void) |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2099 | { |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2100 | bpf_register_prog_type(&sk_filter_type); |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2101 | bpf_register_prog_type(&sched_cls_type); |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2102 | bpf_register_prog_type(&sched_act_type); |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2103 | |
Alexei Starovoitov | 89aa075 | 2014-12-01 15:06:35 -0800 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
Daniel Borkmann | d4052c4 | 2015-03-01 12:31:45 +0100 | [diff] [blame] | 2106 | late_initcall(register_sk_filter_ops); |
| 2107 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2108 | int sk_detach_filter(struct sock *sk) |
| 2109 | { |
| 2110 | int ret = -ENOENT; |
| 2111 | struct sk_filter *filter; |
| 2112 | |
Vincent Bernat | d59577b | 2013-01-16 22:55:49 +0100 | [diff] [blame] | 2113 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
| 2114 | return -EPERM; |
| 2115 | |
Eric Dumazet | f91ff5b | 2010-09-27 06:07:30 +0000 | [diff] [blame] | 2116 | filter = rcu_dereference_protected(sk->sk_filter, |
| 2117 | sock_owned_by_user(sk)); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2118 | if (filter) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 2119 | RCU_INIT_POINTER(sk->sk_filter, NULL); |
Eric Dumazet | 46bcf14 | 2010-12-06 09:29:43 -0800 | [diff] [blame] | 2120 | sk_filter_uncharge(sk, filter); |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2121 | ret = 0; |
| 2122 | } |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2123 | |
Pavel Emelyanov | 55b3332 | 2007-10-17 21:21:26 -0700 | [diff] [blame] | 2124 | return ret; |
| 2125 | } |
Michael S. Tsirkin | 5ff3f07 | 2010-02-14 01:01:00 +0000 | [diff] [blame] | 2126 | EXPORT_SYMBOL_GPL(sk_detach_filter); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2127 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2128 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, |
| 2129 | unsigned int len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2130 | { |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2131 | struct sock_fprog_kern *fprog; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2132 | struct sk_filter *filter; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2133 | int ret = 0; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2134 | |
| 2135 | lock_sock(sk); |
| 2136 | filter = rcu_dereference_protected(sk->sk_filter, |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2137 | sock_owned_by_user(sk)); |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2138 | if (!filter) |
| 2139 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2140 | |
| 2141 | /* We're copying the filter that has been originally attached, |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 2142 | * so no conversion/decode needed anymore. eBPF programs that |
| 2143 | * have no original program cannot be dumped through this. |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2144 | */ |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 2145 | ret = -EACCES; |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 2146 | fprog = filter->prog->orig_prog; |
Daniel Borkmann | 93d08b6 | 2015-10-02 12:06:03 +0200 | [diff] [blame] | 2147 | if (!fprog) |
| 2148 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2149 | |
| 2150 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2151 | if (!len) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2152 | /* User space only enquires number of filter blocks. */ |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2153 | goto out; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2154 | |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2155 | ret = -EINVAL; |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2156 | if (len < fprog->len) |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2157 | goto out; |
| 2158 | |
| 2159 | ret = -EFAULT; |
Alexei Starovoitov | 009937e | 2014-07-30 20:34:13 -0700 | [diff] [blame] | 2160 | if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2161 | goto out; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2162 | |
Daniel Borkmann | a3ea269 | 2014-03-28 18:58:19 +0100 | [diff] [blame] | 2163 | /* Instead of bytes, the API requests to return the number |
| 2164 | * of filter blocks. |
| 2165 | */ |
| 2166 | ret = fprog->len; |
Pavel Emelyanov | a8fc927 | 2012-11-01 02:01:48 +0000 | [diff] [blame] | 2167 | out: |
| 2168 | release_sock(sk); |
| 2169 | return ret; |
| 2170 | } |