Split rde_filterstate_prep() into three functions.
authorclaudio <claudio@openbsd.org>
Thu, 12 Jan 2023 17:35:51 +0000 (17:35 +0000)
committerclaudio <claudio@openbsd.org>
Thu, 12 Jan 2023 17:35:51 +0000 (17:35 +0000)
- rde_filterstate_init(): initialize a filterstate to default values
- rde_filterstate_copy(): copy from a filterstate into a new state object
- rde_filterstate_prep(): set filtersate based on prefix passed as argument.

This makes the code a bit easier to read.
OK tb@

usr.sbin/bgpd/rde.c
usr.sbin/bgpd/rde.h
usr.sbin/bgpd/rde_filter.c
usr.sbin/bgpd/rde_update.c

index 5f0029e..c035a91 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rde.c,v 1.584 2023/01/11 17:10:25 claudio Exp $ */
+/*     $OpenBSD: rde.c,v 1.585 2023/01/12 17:35:51 claudio Exp $ */
 
 /*
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -462,8 +462,7 @@ rde_dispatch_imsg_session(struct imsgbuf *ibuf)
                        }
                        memcpy(&netconf_s, imsg.data, sizeof(netconf_s));
                        TAILQ_INIT(&netconf_s.attrset);
-                       rde_filterstate_prep(&netconf_state, NULL, NULL, NULL,
-                           0, 0);
+                       rde_filterstate_init(&netconf_state);
                        asp = &netconf_state.aspath;
                        asp->aspath = aspath_get(NULL, 0);
                        asp->origin = ORIGIN_IGP;
@@ -802,7 +801,7 @@ rde_dispatch_imsg_parent(struct imsgbuf *ibuf)
                case IMSG_NETWORK_DONE:
                        TAILQ_CONCAT(&netconf_p.attrset, &parent_set, entry);
 
-                       rde_filterstate_prep(&state, NULL, NULL, NULL, 0, 0);
+                       rde_filterstate_init(&state);
                        asp = &state.aspath;
                        asp->aspath = aspath_get(NULL, 0);
                        asp->origin = ORIGIN_IGP;
@@ -1235,7 +1234,7 @@ rde_update_dispatch(struct rde_peer *peer, struct imsg *imsg)
        }
 
        memset(&mpa, 0, sizeof(mpa));
-       rde_filterstate_prep(&state, NULL, NULL, NULL, 0, 0);
+       rde_filterstate_init(&state);
        if (attrpath_len != 0) { /* 0 = no NLRI information in this message */
                /* parse path attributes */
                while (len > 0) {
@@ -1696,8 +1695,7 @@ rde_update_update(struct rde_peer *peer, uint32_t path_id,
                struct rib *rib = rib_byid(i);
                if (rib == NULL)
                        continue;
-               rde_filterstate_prep(&state, &in->aspath, &in->communities,
-                   in->nexthop, in->nhflags, in->vstate);
+               rde_filterstate_copy(&state, in);
                /* input filter */
                action = rde_filter(rib->in_rules, peer, peer, prefix,
                    prefixlen, &state);
@@ -3774,9 +3772,7 @@ rde_softreconfig_in(struct rib_entry *re, void *bula)
                        if (rib->state != RECONF_RELOAD)
                                continue;
 
-                       rde_filterstate_prep(&state, asp, prefix_communities(p),
-                           prefix_nexthop(p), prefix_nhflags(p),
-                           prefix_roa_vstate(p));
+                       rde_filterstate_prep(&state, p);
                        action = rde_filter(rib->in_rules, peer, peer, &prefix,
                            pt->prefixlen, &state);
 
@@ -3915,9 +3911,7 @@ rde_roa_softreload(struct rib_entry *re, void *bula)
                        if (rib == NULL)
                                continue;
 
-                       rde_filterstate_prep(&state, asp, prefix_communities(p),
-                           prefix_nexthop(p), prefix_nhflags(p),
-                           prefix_roa_vstate(p));
+                       rde_filterstate_prep(&state, p);
                        action = rde_filter(rib->in_rules, peer, peer, &prefix,
                            pt->prefixlen, &state);
 
index cae7baa..85f7235 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rde.h,v 1.277 2023/01/11 17:10:26 claudio Exp $ */
+/*     $OpenBSD: rde.h,v 1.278 2023/01/12 17:35:51 claudio Exp $ */
 
 /*
  * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> and
@@ -533,8 +533,9 @@ void                 prefix_evaluate_nexthop(struct prefix *, enum nexthop_state,
 /* rde_filter.c */
 void   rde_apply_set(struct filter_set_head *, struct rde_peer *,
            struct rde_peer *, struct filterstate *, uint8_t);
-void   rde_filterstate_prep(struct filterstate *, struct rde_aspath *,
-           struct rde_community *, struct nexthop *, uint8_t, uint8_t);
+void   rde_filterstate_init(struct filterstate *);
+void   rde_filterstate_prep(struct filterstate *, struct prefix *);
+void   rde_filterstate_copy(struct filterstate *, struct filterstate *);
 void   rde_filterstate_clean(struct filterstate *);
 int    rde_filter_equal(struct filter_head *, struct filter_head *,
            struct rde_peer *);
index abf4fc3..18d3dad 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rde_filter.c,v 1.130 2023/01/11 17:10:26 claudio Exp $ */
+/*     $OpenBSD: rde_filter.c,v 1.131 2023/01/12 17:35:51 claudio Exp $ */
 
 /*
  * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org>
@@ -426,13 +426,19 @@ rde_filter_equal(struct filter_head *a, struct filter_head *b,
 }
 
 void
-rde_filterstate_prep(struct filterstate *state, struct rde_aspath *asp,
+rde_filterstate_init(struct filterstate *state)
+{
+       memset(state, 0, sizeof(*state));
+       path_prep(&state->aspath);
+}
+
+static void
+rde_filterstate_set(struct filterstate *state, struct rde_aspath *asp,
     struct rde_community *communities, struct nexthop *nh, uint8_t nhflags,
     uint8_t vstate)
 {
-       memset(state, 0, sizeof(*state));
+       rde_filterstate_init(state);
 
-       path_prep(&state->aspath);
        if (asp)
                path_copy(&state->aspath, asp);
        if (communities)
@@ -442,6 +448,20 @@ rde_filterstate_prep(struct filterstate *state, struct rde_aspath *asp,
        state->vstate = vstate;
 }
 
+void
+rde_filterstate_prep(struct filterstate *state, struct prefix *p)
+{
+       rde_filterstate_set(state, prefix_aspath(p), prefix_communities(p),
+           prefix_nexthop(p), prefix_nhflags(p), prefix_roa_vstate(p));
+}
+
+void
+rde_filterstate_copy(struct filterstate *state, struct filterstate *src)
+{
+       rde_filterstate_set(state, &src->aspath, &src->communities,
+           src->nexthop, src->nhflags, src->vstate);
+}
+
 void
 rde_filterstate_clean(struct filterstate *state)
 {
index db44894..5af1473 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rde_update.c,v 1.150 2023/01/11 17:10:26 claudio Exp $ */
+/*     $OpenBSD: rde_update.c,v 1.151 2023/01/12 17:35:51 claudio Exp $ */
 
 /*
  * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org>
@@ -176,9 +176,7 @@ up_generate_updates(struct filter_head *rules, struct rde_peer *peer,
                    !(peer->flags & PEERFLAG_EVALUATE_ALL))
                        break;
 
-               rde_filterstate_prep(&state, prefix_aspath(new),
-                   prefix_communities(new), prefix_nexthop(new),
-                   prefix_nhflags(new), prefix_roa_vstate(new));
+               rde_filterstate_prep(&state, new);
                if (rde_filter(rules, peer, prefix_peer(new), &addr,
                    prefixlen, &state) == ACTION_DENY) {
                        rde_filterstate_clean(&state);
@@ -318,9 +316,7 @@ up_generate_addpath(struct filter_head *rules, struct rde_peer *peer,
                if (!up_test_update(peer, new))
                        continue;
 
-               rde_filterstate_prep(&state, prefix_aspath(new),
-                   prefix_communities(new), prefix_nexthop(new),
-                   prefix_nhflags(new), prefix_roa_vstate(new));
+               rde_filterstate_prep(&state, new);
                if (rde_filter(rules, peer, prefix_peer(new), &addr,
                    prefixlen, &state) == ACTION_DENY) {
                        rde_filterstate_clean(&state);
@@ -428,9 +424,7 @@ up_generate_addpath_all(struct filter_head *rules, struct rde_peer *peer,
                if (!up_test_update(peer, new))
                        continue;
 
-               rde_filterstate_prep(&state, prefix_aspath(new),
-                   prefix_communities(new), prefix_nexthop(new),
-                   prefix_nhflags(new), prefix_roa_vstate(new));
+               rde_filterstate_prep(&state, new);
                if (rde_filter(rules, peer, prefix_peer(new), &addr,
                    prefixlen, &state) == ACTION_DENY) {
                        rde_filterstate_clean(&state);
@@ -488,7 +482,7 @@ up_generate_default(struct filter_head *rules, struct rde_peer *peer,
        if (peer->capa.mp[aid] == 0)
                return;
 
-       rde_filterstate_prep(&state, NULL, NULL, NULL, 0, ROA_NOTFOUND);
+       rde_filterstate_init(&state);
        asp = &state.aspath;
        asp->aspath = aspath_get(NULL, 0);
        asp->origin = ORIGIN_IGP;
@@ -1033,8 +1027,7 @@ up_dump_attrnlri(u_char *buf, int len, struct rde_peer *peer)
        if (p == NULL)
                goto done;
 
-       rde_filterstate_prep(&state, prefix_aspath(p), prefix_communities(p),
-           prefix_nexthop(p), prefix_nhflags(p), prefix_roa_vstate(p));
+       rde_filterstate_prep(&state, p);
 
        r = up_generate_attr(buf + 2, len - 2, peer, &state, AID_INET);
        rde_filterstate_clean(&state);
@@ -1172,8 +1165,7 @@ up_dump_mp_reach(u_char *buf, int len, struct rde_peer *peer, uint8_t aid)
 
        wpos = 4;       /* reserve space for length fields */
 
-       rde_filterstate_prep(&state, prefix_aspath(p), prefix_communities(p),
-           prefix_nexthop(p), prefix_nhflags(p), prefix_roa_vstate(p));
+       rde_filterstate_prep(&state, p);
 
        /* write regular path attributes */
        r = up_generate_attr(buf + wpos, len - wpos, peer, &state, aid);