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")
295 for line in sys.stdin.readlines():
297 if lastrule is not None:
298 print("Skipped rule due to lack of understood community tag: " + lastrule)
301 if "BGP.ext_community: " in line:
302 assert lastrule is not None
304 t = lastrule.split("{")
305 if t[0].strip() == "flow4":
307 rules4 += "\tdo {\\\n"
308 elif t[0].strip() == "flow6":
310 rules6 += "\tdo {\\\n"
315 global rules4, rules6
317 rules6 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
319 rules4 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
321 rule = t[1].split("}")[0].strip()
322 for step in rule.split(";"):
323 if step.strip().startswith("src") or step.strip().startswith("dst"):
324 nets = step.strip()[3:].strip().split(" ")
326 assert nets[1] == "offset"
330 if step.strip().startswith("src"):
331 write_rule(ip_to_rule(proto, nets[0], "saddr", offset))
333 write_rule(ip_to_rule(proto, nets[0], "daddr", offset))
334 elif step.strip().startswith("proto") and proto == 4:
335 write_rule(proto_to_rule(4, step.strip()[6:]))
336 elif step.strip().startswith("next header") and proto == 6:
337 write_rule(proto_to_rule(6, step.strip()[12:]))
338 elif step.strip().startswith("icmp type"):
339 write_rule(icmp_type_to_rule(proto, step.strip()[10:]))
340 elif step.strip().startswith("icmp code"):
341 write_rule(icmp_code_to_rule(proto, step.strip()[10:]))
342 elif step.strip().startswith("sport") or step.strip().startswith("dport") or step.strip().startswith("port"):
343 write_rule(port_to_rule(step.strip().split(" ")[0], step.strip().split(" ", 1)[1]))
344 elif step.strip().startswith("length"):
345 write_rule(len_to_rule(step.strip()[7:]))
346 elif step.strip().startswith("dscp"):
347 write_rule(dscp_to_rule(proto, step.strip()[5:]))
348 elif step.strip().startswith("tcp flags"):
349 write_rule(tcp_flags_to_rule(step.strip()[10:]))
350 elif step.strip().startswith("label"):
351 write_rule(flow_label_to_rule(step.strip()[6:]))
352 elif step.strip().startswith("fragment"):
355 write_rule(fragment_to_rule(proto, step.strip()[9:]))
356 elif step.strip() == "":
361 # Now write the match handling!
364 for community in line.split("("):
365 if not community.startswith("generic, "):
367 blocks = community.split(",")
368 assert len(blocks) == 3
369 if len(blocks[1].strip()) != 10: # Should be 0x12345678
371 ty = blocks[1].strip()[:6]
372 low_bytes = int(blocks[2].strip(") \n"), 16)
374 if first_action is not None:
375 assert False # Two ratelimit actions?
377 first_action = "return XDP_DROP;"
379 if low_bytes & (1 << 31) != 0:
380 assert False # Negative ratelimit?
381 exp = (low_bytes & (0xff << 23)) >> 23
383 assert False # NaN/INF?
385 first_action = "return XDP_DROP;"
386 if exp >= 127 + 63: # The count won't even fit in 64-bits, just accept
387 first_action = "return XDP_PASS;"
388 mantissa = low_bytes & ((1 << 23) - 1)
389 value = 1.0 + mantissa / (2**23)
390 value *= 2**(exp-127)
391 # Note that int64_t will overflow after 292 years of uptime
392 first_action = "int64_t time = bpf_ktime_get_ns();\n"
393 first_action += f"const uint32_t ratelimitidx = {ratelimitcnt};\n"
394 first_action += "size_t pktlen = data_end - pktdata;\n"
395 first_action += "uint64_t extra_bytes = 0;\n"
396 first_action += "struct ratelimit *rate = bpf_map_lookup_elem(&rate_map, &ratelimitidx);\n"
397 first_action += "if (rate) {\n"
398 first_action += "\tbpf_spin_lock(&rate->lock);\n"
399 first_action += "\tif (likely(rate->sent_bytes > 0)) {\n"
400 first_action += "\t\tint64_t diff = time - rate->sent_time;\n"
401 # Unlikely or not, if the flow is slow, take a perf hit (though with the else if branch it doesn't matter)
402 first_action += "\t\tif (unlikely(diff > 1000000000))\n"
403 first_action += "\t\t\trate->sent_bytes = 0;\n"
404 first_action += "\t\telse if (likely(diff > 0))\n"
405 first_action += f"\t\t\textra_bytes = ((uint64_t)diff) * {math.floor(value)} / 1000000000;\n"
406 first_action += "\t}\n"
407 first_action += "\tif (rate->sent_bytes - ((int64_t)extra_bytes) <= 0) {\n"
408 first_action += "\t\trate->sent_bytes = data_end - pktdata;\n"
409 first_action += "\t\trate->sent_time = time;\n"
410 first_action += "\t\tbpf_spin_unlock(&rate->lock);\n"
411 first_action += "\t} else {\n"
412 first_action += "\t\tbpf_spin_unlock(&rate->lock);\n"
413 first_action += "\t\treturn XDP_DROP;\n"
414 first_action += "\t}\n"
415 first_action += "}\n"
418 if low_bytes & 1 == 0:
419 last_action = "return XDP_PASS;"
420 if low_bytes & 2 == 2:
421 write_rule(f"const uint32_t ruleidx = STATIC_RULE_CNT + {rulecnt};")
422 write_rule("INCREMENT_MATCH(ruleidx);")
424 assert False # We do not implement the redirect action
426 if low_bytes & ~0b111111 != 0:
427 assert False # Invalid DSCP value
429 write_rule("int32_t chk = ~BE16(ip->check) & 0xffff;")
430 write_rule("uint8_t orig_tos = ip->tos;")
431 write_rule("ip->tos = (ip->tos & 3) | " + str(low_bytes << 2) + ";")
432 write_rule("chk = (chk - orig_tos + ip->tos);")
433 write_rule("if (unlikely(chk < 0)) { chk += 65534; }")
434 write_rule("ip->check = ~BE16(chk);")
436 write_rule("ip6->priority = " + str(low_bytes >> 2) + ";")
437 write_rule("ip6->flow_lbl[0] = (ip6->flow_lbl[0] & 0x3f) | " + str((low_bytes & 3) << 6) + ";")
438 if first_action is not None:
439 write_rule(first_action)
440 if last_action is not None:
441 write_rule(last_action)
443 rules6 += "\t} while(0);\\\n"
445 rules4 += "\t} while(0);\\\n"
450 out.write(f"#define RULECNT {rulecnt}\n")
451 if ratelimitcnt != 0:
452 out.write(f"#define RATE_CNT {ratelimitcnt}\n")
454 out.write("#define NEED_V4_PARSE\n")
455 out.write("#define RULES4 {\\\n" + rules4 + "}\n")
457 out.write("#define NEED_V6_PARSE\n")
458 out.write("#define RULES6 {\\\n" + rules6 + "}\n")
459 if args.v6frag == "ignore-parse-if-rule":
461 out.write("#define PARSE_V6_FRAG PARSE\n")