aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@plumgrid.com>2015-03-26 22:53:57 -0400
committerDavid S. Miller <davem@davemloft.net>2015-03-29 16:26:54 -0400
commit608cd71a9c7c9db76e78a792c5a4101e12fea43f (patch)
tree9824e0795f923a6370e53799f84e8ad13764cb23 /net
parent7836b16c0dd1d3039ccc35ceeff6d75401fbba8c (diff)
tc: bpf: generalize pedit action
existing TC action 'pedit' can munge any bits of the packet. Generalize it for use in bpf programs attached as cls_bpf and act_bpf via bpf_skb_store_bytes() helper function. Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Reviewed-by: Jiri Pirko <jiri@resnulli.us> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net')
-rw-r--r--net/core/filter.c71
1 files changed, 69 insertions, 2 deletions
diff --git a/net/core/filter.c b/net/core/filter.c
index 32f43c59908c..444a07e4f68d 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -1175,6 +1175,56 @@ int sk_attach_bpf(u32 ufd, struct sock *sk)
1175 return 0; 1175 return 0;
1176} 1176}
1177 1177
1178static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1179{
1180 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1181 unsigned int offset = (unsigned int) r2;
1182 void *from = (void *) (long) r3;
1183 unsigned int len = (unsigned int) r4;
1184 char buf[16];
1185 void *ptr;
1186
1187 /* bpf verifier guarantees that:
1188 * 'from' pointer points to bpf program stack
1189 * 'len' bytes of it were initialized
1190 * 'len' > 0
1191 * 'skb' is a valid pointer to 'struct sk_buff'
1192 *
1193 * so check for invalid 'offset' and too large 'len'
1194 */
1195 if (offset > 0xffff || len > sizeof(buf))
1196 return -EFAULT;
1197
1198 if (skb_cloned(skb) && !skb_clone_writable(skb, offset + len))
1199 return -EFAULT;
1200
1201 ptr = skb_header_pointer(skb, offset, len, buf);
1202 if (unlikely(!ptr))
1203 return -EFAULT;
1204
1205 skb_postpull_rcsum(skb, ptr, len);
1206
1207 memcpy(ptr, from, len);
1208
1209 if (ptr == buf)
1210 /* skb_store_bits cannot return -EFAULT here */
1211 skb_store_bits(skb, offset, ptr, len);
1212
1213 if (skb->ip_summed == CHECKSUM_COMPLETE)
1214 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1215 return 0;
1216}
1217
1218const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1219 .func = bpf_skb_store_bytes,
1220 .gpl_only = false,
1221 .ret_type = RET_INTEGER,
1222 .arg1_type = ARG_PTR_TO_CTX,
1223 .arg2_type = ARG_ANYTHING,
1224 .arg3_type = ARG_PTR_TO_STACK,
1225 .arg4_type = ARG_CONST_STACK_SIZE,
1226};
1227
1178static const struct bpf_func_proto * 1228static const struct bpf_func_proto *
1179sk_filter_func_proto(enum bpf_func_id func_id) 1229sk_filter_func_proto(enum bpf_func_id func_id)
1180{ 1230{
@@ -1194,6 +1244,17 @@ sk_filter_func_proto(enum bpf_func_id func_id)
1194 } 1244 }
1195} 1245}
1196 1246
1247static const struct bpf_func_proto *
1248tc_cls_act_func_proto(enum bpf_func_id func_id)
1249{
1250 switch (func_id) {
1251 case BPF_FUNC_skb_store_bytes:
1252 return &bpf_skb_store_bytes_proto;
1253 default:
1254 return sk_filter_func_proto(func_id);
1255 }
1256}
1257
1197static bool sk_filter_is_valid_access(int off, int size, 1258static bool sk_filter_is_valid_access(int off, int size,
1198 enum bpf_access_type type) 1259 enum bpf_access_type type)
1199{ 1260{
@@ -1270,18 +1331,24 @@ static const struct bpf_verifier_ops sk_filter_ops = {
1270 .convert_ctx_access = sk_filter_convert_ctx_access, 1331 .convert_ctx_access = sk_filter_convert_ctx_access,
1271}; 1332};
1272 1333
1334static const struct bpf_verifier_ops tc_cls_act_ops = {
1335 .get_func_proto = tc_cls_act_func_proto,
1336 .is_valid_access = sk_filter_is_valid_access,
1337 .convert_ctx_access = sk_filter_convert_ctx_access,
1338};
1339
1273static struct bpf_prog_type_list sk_filter_type __read_mostly = { 1340static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1274 .ops = &sk_filter_ops, 1341 .ops = &sk_filter_ops,
1275 .type = BPF_PROG_TYPE_SOCKET_FILTER, 1342 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1276}; 1343};
1277 1344
1278static struct bpf_prog_type_list sched_cls_type __read_mostly = { 1345static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1279 .ops = &sk_filter_ops, 1346 .ops = &tc_cls_act_ops,
1280 .type = BPF_PROG_TYPE_SCHED_CLS, 1347 .type = BPF_PROG_TYPE_SCHED_CLS,
1281}; 1348};
1282 1349
1283static struct bpf_prog_type_list sched_act_type __read_mostly = { 1350static struct bpf_prog_type_list sched_act_type __read_mostly = {
1284 .ops = &sk_filter_ops, 1351 .ops = &tc_cls_act_ops,
1285 .type = BPF_PROG_TYPE_SCHED_ACT, 1352 .type = BPF_PROG_TYPE_SCHED_ACT,
1286}; 1353};
1287 1354