+static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
+ const union if_addr *addr, uint8_t mask, unsigned int table,
+ struct interface *in_iface, const char *action, bool src)
+{
+ struct iprule rule = {
+ .flags = IPRULE_PRIORITY,
+ .priority = priority
+ };
+
+ if (addr) {
+ if (src) {
+ rule.flags |= IPRULE_SRC;
+ rule.src_addr = *addr;
+ rule.src_mask = mask;
+ } else {
+ rule.flags |= IPRULE_DEST;
+ rule.dest_addr = *addr;
+ rule.dest_mask = mask;
+ }
+ }
+
+ if (table) {
+ rule.flags |= IPRULE_LOOKUP;
+ rule.lookup = table;
+
+ if (!rule.lookup)
+ return 0;
+ } else if (action) {
+ rule.flags |= IPRULE_ACTION;
+ system_resolve_iprule_action(action, &rule.action);
+ }
+
+ if (in_iface && in_iface->l3_dev.dev) {
+ rule.flags |= IPRULE_IN;
+ strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
+ }
+
+ rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
+
+ return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
+}
+
+static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
+{
+ struct iprule rule = {
+ .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
+ .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
+ .lookup = (v6) ? iface->ip6table : iface->ip4table,
+ .in_dev = "lo"
+ };
+
+ if (!rule.lookup)
+ return 0;
+
+ rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
+
+ return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
+}
+