15 class ASTAction(Enum):
23 def __init__(self, action, left, right=None):
25 if action == ASTAction.FALSE or action == ASTAction.TRUE:
26 assert left is None and right is None
30 assert action == ASTAction.EXPR or action == ASTAction.NOT
34 def write(self, expr_param, expr_param2=None):
35 if self.action == ASTAction.OR:
36 return "(" + self.left.write(expr_param, expr_param2) + ") || (" + self.right.write(expr_param, expr_param2) + ")"
37 if self.action == ASTAction.AND:
38 return "(" + self.left.write(expr_param, expr_param2) + ") && (" + self.right.write(expr_param, expr_param2) + ")"
39 if self.action == ASTAction.NOT:
40 return "!(" + self.left.write(expr_param, expr_param2) + ")"
41 if self.action == ASTAction.FALSE:
43 if self.action == ASTAction.TRUE:
45 if self.action == ASTAction.EXPR:
46 return self.left.write(expr_param, expr_param2)
48 def parse_ast(expr, parse_expr):
51 comma_split = expr.split(",", 1)
52 or_split = expr.split("||", 1)
53 if len(comma_split) > 1 and not "||" in comma_split[0]:
54 return ASTNode(ASTAction.OR, parse_ast(comma_split[0], parse_expr), parse_ast(comma_split[1], parse_expr))
56 assert not "," in or_split[0]
57 return ASTNode(ASTAction.OR, parse_ast(or_split[0], parse_expr), parse_ast(or_split[1], parse_expr))
59 and_split = expr.split("&&", 1)
60 if len(and_split) > 1:
61 return ASTNode(ASTAction.AND, parse_ast(and_split[0], parse_expr), parse_ast(and_split[1], parse_expr))
63 if expr.strip() == "true":
64 return ASTNode(ASTAction.TRUE, None)
65 if expr.strip() == "false":
66 return ASTNode(ASTAction.FALSE, None)
68 if expr.startswith("!"):
69 return ASTNode(ASTAction.NOT, parse_ast(expr[1:], parse_expr))
71 return parse_expr(expr)
74 class NumbersAction(Enum):
81 def __init__(self, action, val):
85 def write(self, param, param2):
86 if param2 is not None:
87 return "(" + param + self.action.value + self.val + ") || (" + param2 + self.action.value + self.val + ")"
88 return param + self.action.value + self.val
90 def parse_numbers_expr(expr):
91 space_split = expr.split(" ")
92 if expr.startswith(">="):
93 assert len(space_split) == 2
94 return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GTOE, space_split[1]))
95 if expr.startswith(">"):
96 assert len(space_split) == 2
97 return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GT, space_split[1]))
98 if expr.startswith("<="):
99 assert len(space_split) == 2
100 return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LTOE, space_split[1]))
101 if expr.startswith("<"):
102 assert len(space_split) == 2
103 return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LT, space_split[1]))
105 rangesplit = expr.split("..")
106 assert len(rangesplit) == 2
107 #XXX: Are ranges really inclusive,inclusive?
108 left = ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GTOE, rangesplit[0]))
109 right = ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LTOE, rangesplit[1]))
110 return ASTNode(ASTAction.AND, left, right)
112 if expr.startswith("= "):
114 return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.EQ, expr))
116 class FragExpr(Enum):
122 def write(self, ipproto, _param2):
124 if self == FragExpr.IF:
125 return "(ip->frag_off & BE16(IP_MF|IP_OFFSET)) != 0"
126 elif self == FragExpr.FF:
127 return "((ip->frag_off & BE16(IP_MF)) != 0 && (ip->frag_off & BE16(IP_OFFSET)) == 0)"
128 elif self == FragExpr.DF:
129 return "(ip->frag_off & BE16(IP_DF)) != 0"
130 elif self == FragExpr.LF:
131 return "((ip->frag_off & BE16(IP_MF)) == 0 && (ip->frag_off & BE16(IP_OFFSET)) != 0)"
135 if self == FragExpr.IF:
136 return "frag6 != NULL"
137 elif self == FragExpr.FF:
138 return "(frag6 != NULL && (frag6->frag_off & BE16(IP6_MF)) != 0 && (frag6->frag_off & BE16(IP6_FRAGOFF)) == 0)"
139 elif self == FragExpr.DF:
140 assert False # No such thing in v6
141 elif self == FragExpr.LF:
142 return "(frag6 != NULL && (frag6->frag_off & BE16(IP6_MF)) == 0 && (frag6->frag_off & BE16(IP6_FRAGOFF)) != 0)"
146 def parse_frag_expr(expr):
147 if expr == "is_fragment":
148 return ASTNode(ASTAction.EXPR, FragExpr.IF)
149 elif expr == "first_fragment":
150 return ASTNode(ASTAction.EXPR, FragExpr.FF)
151 elif expr == "dont_fragment":
152 return ASTNode(ASTAction.EXPR, FragExpr.DF)
153 elif expr == "last_fragment":
154 return ASTNode(ASTAction.EXPR, FragExpr.LF)
159 def __init__(self, val):
165 def write(self, param, _param2):
166 return f"({param} & {self.mask}) == {self.match}"
168 def parse_bit_expr(expr):
169 return ASTNode(ASTAction.EXPR, BitExpr(expr))
172 def ip_to_rule(proto, inip, ty, offset):
174 assert offset is None
175 net = ipaddress.IPv4Network(inip.strip())
176 if net.prefixlen == 0:
178 return f"""if ((ip->{ty} & MASK4({net.prefixlen})) != BIGEND32({int(net.network_address)}ULL))
181 net = ipaddress.IPv6Network(inip.strip())
182 if net.prefixlen == 0:
184 u32s = [(int(net.network_address) >> (3*32)) & 0xffffffff,
185 (int(net.network_address) >> (2*32)) & 0xffffffff,
186 (int(net.network_address) >> (1*32)) & 0xffffffff,
187 (int(net.network_address) >> (0*32)) & 0xffffffff]
189 mask = f"MASK6({net.prefixlen})"
191 mask = f"MASK6_OFFS({offset}, {net.prefixlen})"
192 return f"""if ((ip6->{ty} & {mask}) != (BIGEND128({u32s[0]}ULL, {u32s[1]}ULL, {u32s[2]}ULL, {u32s[3]}ULL) & {mask}))
195 def fragment_to_rule(ipproto, rules):
196 ast = parse_ast(rules, parse_frag_expr)
197 return "if (!( " + ast.write(ipproto) + " )) break;"
199 def len_to_rule(rules):
200 ast = parse_ast(rules, parse_numbers_expr)
201 return "if (!( " + ast.write("(data_end - pktdata)") + " )) break;"
203 def proto_to_rule(ipproto, proto):
204 ast = parse_ast(proto, parse_numbers_expr)
207 return "if (!( " + ast.write("ip->protocol") + " )) break;"
209 return "if (!( " + ast.write("ip6->nexthdr") + " )) break;"
211 def icmp_type_to_rule(proto, ty):
212 ast = parse_ast(ty, parse_numbers_expr)
214 return "if (icmp == NULL) break;\nif (!( " + ast.write("icmp->type") + " )) break;"
216 return "if (icmpv6 == NULL) break;\nif (!( " + ast.write("icmpv6->icmp6_type") + " )) break;"
218 def icmp_code_to_rule(proto, code):
219 ast = parse_ast(code, parse_numbers_expr)
221 return "if (icmp == NULL) break;\nif (!( " + ast.write("icmp->code") + " )) break;"
223 return "if (icmpv6 == NULL) break;\nif (!( " + ast.write("icmpv6->icmp6_code") + " )) break;"
225 def dscp_to_rule(proto, rules):
226 ast = parse_ast(rules, parse_numbers_expr)
229 return "if (!( " + ast.write("((ip->tos & 0xfc) >> 2)") + " )) break;"
231 return "if (!( " + ast.write("((ip6->priority << 2) | ((ip6->flow_lbl[0] & 0xc0) >> 6))") + " )) break;"
233 def port_to_rule(ty, rules):
235 ast = parse_ast(rules, parse_numbers_expr)
236 return "if (!ports_valid) break;\nif (!( " + ast.write("sport", "dport") + " )) break;"
238 ast = parse_ast(rules, parse_numbers_expr)
239 return "if (!ports_valid) break;\nif (!( " + ast.write(ty) + " )) break;"
241 def tcp_flags_to_rule(rules):
242 ast = parse_ast(rules, parse_bit_expr)
244 return f"""if (tcp == NULL) break;
245 if (!( {ast.write("(ntohs(tcp->flags) & 0xfff)")} )) break;"""
247 def flow_label_to_rule(rules):
248 ast = parse_ast(rules, parse_bit_expr)
250 return f"""if (ip6 == NULL) break;
251 if (!( {ast.write("((((uint32_t)(ip6->flow_lbl[0] & 0xf)) << 2*8) | (((uint32_t)ip6->flow_lbl[1]) << 1*8) | (uint32_t)ip6->flow_lbl[0])")} )) break;"""
254 with open("rules.h", "w") as out:
255 parse = argparse.ArgumentParser()
256 parse.add_argument("--ihl", dest="ihl", required=True, choices=["drop-options","accept-options","parse-options"])
257 parse.add_argument("--v6frag", dest="v6frag", required=True, choices=["drop-frags","ignore","parse-frags","ignore-parse-if-rule"])
258 parse.add_argument("--8021q", dest="vlan", required=True, choices=["drop-vlan","accept-vlan","parse-vlan"])
259 parse.add_argument("--require-8021q", dest="vlan_tag")
260 args = parse.parse_args(sys.argv[1:])
262 if args.ihl == "drop-options":
263 out.write("#define PARSE_IHL XDP_DROP\n")
264 elif args.ihl == "accept-options":
265 out.write("#define PARSE_IHL XDP_PASS\n")
266 elif args.ihl == "parse-options":
267 out.write("#define PARSE_IHL PARSE\n")
269 if args.v6frag == "drop-frags":
270 out.write("#define PARSE_V6_FRAG XDP_DROP\n")
271 elif args.v6frag == "ignore":
273 elif args.v6frag == "parse-frags":
274 out.write("#define PARSE_V6_FRAG PARSE\n")
276 if args.vlan == "drop-vlan":
277 out.write("#define PARSE_8021Q XDP_DROP\n")
278 elif args.vlan == "accept-vlan":
279 out.write("#define PARSE_8021Q XDP_PASS\n")
280 elif args.vlan == "parse-vlan":
281 out.write("#define PARSE_8021Q PARSE\n")
283 if args.vlan_tag is not None:
284 if args.vlan != "parse-vlan":
286 out.write("#define REQ_8021Q " + args.vlan_tag + "\n")
293 v4persrcratelimitcnt = 0
294 v6persrcratelimitcnt = 0
297 for line in sys.stdin.readlines():
299 if lastrule is not None:
300 print("Skipped rule due to lack of understood community tag: " + lastrule)
303 if "BGP.ext_community: " in line:
304 assert lastrule is not None
306 t = lastrule.split("{")
307 if t[0].strip() == "flow4":
309 rules4 += "\tdo {\\\n"
310 elif t[0].strip() == "flow6":
312 rules6 += "\tdo {\\\n"
317 global rules4, rules6
319 rules6 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
321 rules4 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
323 rule = t[1].split("}")[0].strip()
324 for step in rule.split(";"):
325 if step.strip().startswith("src") or step.strip().startswith("dst"):
326 nets = step.strip()[3:].strip().split(" ")
328 assert nets[1] == "offset"
332 if step.strip().startswith("src"):
333 write_rule(ip_to_rule(proto, nets[0], "saddr", offset))
335 write_rule(ip_to_rule(proto, nets[0], "daddr", offset))
336 elif step.strip().startswith("proto") and proto == 4:
337 write_rule(proto_to_rule(4, step.strip()[6:]))
338 elif step.strip().startswith("next header") and proto == 6:
339 write_rule(proto_to_rule(6, step.strip()[12:]))
340 elif step.strip().startswith("icmp type"):
341 write_rule(icmp_type_to_rule(proto, step.strip()[10:]))
342 elif step.strip().startswith("icmp code"):
343 write_rule(icmp_code_to_rule(proto, step.strip()[10:]))
344 elif step.strip().startswith("sport") or step.strip().startswith("dport") or step.strip().startswith("port"):
345 write_rule(port_to_rule(step.strip().split(" ")[0], step.strip().split(" ", 1)[1]))
346 elif step.strip().startswith("length"):
347 write_rule(len_to_rule(step.strip()[7:]))
348 elif step.strip().startswith("dscp"):
349 write_rule(dscp_to_rule(proto, step.strip()[5:]))
350 elif step.strip().startswith("tcp flags"):
351 write_rule(tcp_flags_to_rule(step.strip()[10:]))
352 elif step.strip().startswith("label"):
353 write_rule(flow_label_to_rule(step.strip()[6:]))
354 elif step.strip().startswith("fragment"):
357 write_rule(fragment_to_rule(proto, step.strip()[9:]))
358 elif step.strip() == "":
363 # Now write the match handling!
367 for community in line.split("("):
368 if not community.startswith("generic, "):
370 blocks = community.split(",")
371 assert len(blocks) == 3
372 if len(blocks[1].strip()) != 10: # Should be 0x12345678
374 ty = blocks[1].strip()[:6]
375 high_byte = int(blocks[1].strip()[8:], 16)
376 low_bytes = int(blocks[2].strip(") \n"), 16)
377 if ty == "0x8006" or ty == "0x800c" or ty == "0x8306" or ty == "0x830c":
378 if first_action is not None:
379 # Two ratelimit actions, just drop the old one. RFC 8955 says we can.
381 exp = (low_bytes & (0xff << 23)) >> 23
383 first_action = "{stats_replace}\nreturn XDP_DROP;"
384 elif low_bytes & (1 << 31) != 0:
385 # Negative limit, just drop
386 first_action = "{stats_replace}\nreturn XDP_DROP;"
388 # NaN/INF. Just treat as INF and accept
390 elif exp <= 127: # < 1
391 first_action = "{stats_replace}\nreturn XDP_DROP;"
392 elif exp >= 127 + 63: # The count won't even fit in 64-bits, just accept
395 mantissa = low_bytes & ((1 << 23) - 1)
396 value = 1.0 + mantissa / (2**23)
397 value *= 2**(exp-127)
398 if ty == "0x8006" or ty == "0x8306":
399 accessor = "rate->rate.sent_bytes"
401 accessor = "rate->rate.sent_packets"
402 # Note that int64_t will overflow after 292 years of uptime
403 first_action = "int64_t time = bpf_ktime_get_ns();\n"
404 first_action += "uint64_t allowed_since_last = 0;\n"
405 if ty == "0x8006" or ty == "0x800c":
406 spin_lock = "bpf_spin_lock(&rate->lock);"
407 spin_unlock = "bpf_spin_unlock(&rate->lock);"
408 first_action += f"const uint32_t ratelimitidx = {ratelimitcnt};\n"
409 first_action += "struct ratelimit *rate = bpf_map_lookup_elem(&rate_map, &ratelimitidx);\n"
412 spin_lock = "/* No locking as we're per-CPU */"
413 spin_unlock = "/* No locking as we're per-CPU */"
417 first_action += f"const uint32_t srcip = ip->saddr & MASK4({high_byte});\n"
418 first_action += f"void *rate_map = &v4_src_rate_{v4persrcratelimitcnt};\n"
419 v4persrcratelimitcnt += 1
423 first_action += f"const uint128_t srcip = ip6->saddr & MASK6({high_byte});\n"
424 first_action += f"void *rate_map = &v6_src_rate_{v6persrcratelimitcnt};\n"
425 v6persrcratelimitcnt += 1
426 first_action += f"struct percpu_ratelimit *rate = bpf_map_lookup_elem(rate_map, &srcip);\n"
427 first_action += "if (rate) {\n"
428 first_action += f"\t{spin_lock}\n"
429 first_action += f"\tif (likely({accessor} > 0))" + " {\n"
430 first_action += "\t\tint64_t diff = time - rate->sent_time;\n"
431 # Unlikely or not, if the flow is slow, take a perf hit (though with the else if branch it doesn't matter)
432 first_action += "\t\tif (unlikely(diff > 1000000000))\n"
433 first_action += f"\t\t\t{accessor} = 0;\n"
434 first_action += "\t\telse if (likely(diff > 0))\n"
435 first_action += f"\t\t\tallowed_since_last = ((uint64_t)diff) * {math.floor(value)} / 1000000000;\n"
436 first_action += "\t}\n"
437 first_action += f"\tif ({accessor} - ((int64_t)allowed_since_last) <= 0)" + " {\n"
438 if ty == "0x8006" or ty == "0x8306":
439 first_action += f"\t\t{accessor} = data_end - pktdata;\n"
441 first_action += f"\t\t{accessor} = 1;\n"
442 first_action += "\t\trate->sent_time = time;\n"
443 first_action += f"\t\t{spin_unlock}\n"
444 first_action += "\t} else {\n"
445 first_action += f"\t\t{spin_unlock}\n"
446 first_action += "\t\t{stats_replace}\n"
447 first_action += "\t\treturn XDP_DROP;\n"
448 first_action += "\t}\n"
449 if ty == "0x8306" or ty == "0x830c":
450 first_action += "} else {\n"
451 first_action += "\tstruct percpu_ratelimit new_rate = { .sent_time = time, };\n"
452 first_action += "\trate = &new_rate;\n"
453 if ty == "0x8006" or ty == "0x8306":
454 first_action += f"\t\t{accessor} = data_end - pktdata;\n"
456 first_action += f"\t\t{accessor} = 1;\n"
457 first_action += "\tbpf_map_update_elem(rate_map, &srcip, rate, BPF_ANY);\n"
458 first_action += "}\n"
460 if low_bytes & 1 == 0:
461 last_action = "return XDP_PASS;"
462 if low_bytes & 2 == 2:
463 stats_action = f"const uint32_t ruleidx = STATIC_RULE_CNT + {rulecnt};\n"
464 stats_action += "INCREMENT_MATCH(ruleidx);"
466 assert False # We do not implement the redirect action
468 if low_bytes & ~0b111111 != 0:
469 assert False # Invalid DSCP value
471 write_rule("int32_t chk = ~BE16(ip->check) & 0xffff;")
472 write_rule("uint8_t orig_tos = ip->tos;")
473 write_rule("ip->tos = (ip->tos & 3) | " + str(low_bytes << 2) + ";")
474 write_rule("chk = (chk - orig_tos + ip->tos);")
475 write_rule("if (unlikely(chk > 0xffff)) { chk -= 65535; }")
476 write_rule("else if (unlikely(chk < 0)) { chk += 65535; }")
477 write_rule("ip->check = ~BE16(chk);")
479 write_rule("ip6->priority = " + str(low_bytes >> 2) + ";")
480 write_rule("ip6->flow_lbl[0] = (ip6->flow_lbl[0] & 0x3f) | " + str((low_bytes & 3) << 6) + ";")
481 if first_action is not None:
482 write_rule(first_action.replace("{stats_replace}", stats_action))
483 if stats_action is not None and (first_action is None or "{stats_replace}" not in first_action):
484 write_rule(stats_action)
485 if last_action is not None:
486 write_rule(last_action)
488 rules6 += "\t} while(0);\\\n"
490 rules4 += "\t} while(0);\\\n"
495 out.write(f"#define RULECNT {rulecnt}\n")
496 if ratelimitcnt != 0:
497 out.write(f"#define RATE_CNT {ratelimitcnt}\n")
498 if v4persrcratelimitcnt != 0:
499 out.write(f"#define V4_SRC_RATE_CNT {v4persrcratelimitcnt}\n")
500 if v6persrcratelimitcnt != 0:
501 out.write(f"#define V6_SRC_RATE_CNT {v6persrcratelimitcnt}\n")
503 out.write("#define NEED_V4_PARSE\n")
504 out.write("#define RULES4 {\\\n" + rules4 + "}\n")
506 out.write("#define NEED_V6_PARSE\n")
507 out.write("#define RULES6 {\\\n" + rules6 + "}\n")
508 if args.v6frag == "ignore-parse-if-rule":
510 out.write("#define PARSE_V6_FRAG PARSE\n")