On Fri, Jun 07, 2019 at 11:59:10AM +0200, Claudio Jeker wrote:
> On Tue, May 28, 2019 at 03:53:11PM +0200, Claudio Jeker wrote:
> > On Tue, May 14, 2019 at 12:37:25AM +0200, Claudio Jeker wrote:
> > > This diff changes the way communites are stored and modified in bgpd.
> > > The current implementation was showing that community_*_delete() consumed
> > > a lot of CPU time because of the way rde_attr was used.
> > > Communities are extensivly used by filters (e.g. arouteserver) and
> > > therefor they are stored in an own special datastructure that allows fast
> > > changes especially during filtering. The communities are now a special
> > > part of the filterstate and because of this set and delete of a community
> > > is much simpler.
> > > 
> > > This seems to make my test setup a fair bit snappier and quicker at
> > > processing UPDATEs.
> > 
> > While the old diff got us to warp speed this diff goes to warp 5 or 6.
> > It optimizes the filter functions community_match, community_delete and
> > community_set to be as quick as possible since those functions are in the
> > hot path. As a side-effect the filter specific community struct got removed
> > and everything uses the unified one (which is where I wanted to end up).
> > 
> > Please test (especially the ext. community cases since extended
> > communities are just painful)
> 
> I would like to get this into the tree in the next days since there are a
> few other big changes in the works and juggling that outside of the tree
> is getting complicated.

Here an updated diff that actually applies cleanly after the last few bgpd
commits.

-- 
:wq Claudio

Index: usr.sbin/bgpctl/bgpctl.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpctl/bgpctl.c,v
retrieving revision 1.238
diff -u -p -r1.238 bgpctl.c
--- usr.sbin/bgpctl/bgpctl.c    23 May 2019 14:12:06 -0000      1.238
+++ usr.sbin/bgpctl/bgpctl.c    28 May 2019 13:24:30 -0000
@@ -81,6 +81,7 @@ int            show_rib_detail_msg(struct imsg *,
 void            show_rib_brief(struct ctl_show_rib *, u_char *);
 void            show_rib_detail(struct ctl_show_rib *, u_char *, int, int);
 void            show_attr(void *, u_int16_t, int);
+void            show_communities(u_char *, size_t, int);
 void            show_community(u_char *, u_int16_t);
 void            show_large_community(u_char *, u_int16_t);
 void            show_ext_community(u_char *, u_int16_t);
@@ -282,7 +283,7 @@ main(int argc, char *argv[])
                }
                if (res->as.type != AS_UNDEF)
                        ribreq.as = res->as;
-               if (res->community.type != COMMUNITY_TYPE_NONE)
+               if (res->community.flags != 0)
                        ribreq.community = res->community;
                ribreq.neighbor = neighbor;
                strlcpy(ribreq.rib, res->rib, sizeof(ribreq.rib));
@@ -1252,6 +1253,12 @@ show_rib_detail_msg(struct imsg *imsg, i
                asdata += sizeof(struct ctl_show_rib);
                show_rib_detail(&rib, asdata, nodescr, flag0);
                break;
+       case IMSG_CTL_SHOW_RIB_COMMUNITIES:
+               ilen = imsg->hdr.len - IMSG_HEADER_SIZE;
+               if (ilen % sizeof(struct community))
+                       errx(1, "bad IMSG_CTL_SHOW_RIB_COMMUNITIES received");
+               show_communities(imsg->data, ilen, flag0);
+               break;
        case IMSG_CTL_SHOW_RIB_ATTR:
                ilen = imsg->hdr.len - IMSG_HEADER_SIZE;
                if (ilen < 3)
@@ -1632,6 +1639,154 @@ show_attr(void *b, u_int16_t len, int fl
        printf("%c", EOL0(flag0));
 }
 
+static void
+print_community(u_int16_t a, u_int16_t v)
+{
+       if (a == COMMUNITY_WELLKNOWN)
+               switch (v) {
+               case COMMUNITY_GRACEFUL_SHUTDOWN:
+                       printf("GRACEFUL_SHUTDOWN");
+                       break;
+               case COMMUNITY_NO_EXPORT:
+                       printf("NO_EXPORT");
+                       break;
+               case COMMUNITY_NO_ADVERTISE:
+                       printf("NO_ADVERTISE");
+                       break;
+               case COMMUNITY_NO_EXPSUBCONFED:
+                       printf("NO_EXPORT_SUBCONFED");
+                       break;
+               case COMMUNITY_NO_PEER:
+                       printf("NO_PEER");
+                       break;
+               case COMMUNITY_BLACKHOLE:
+                       printf("BLACKHOLE");
+                       break;
+               default:
+                       printf("%hu:%hu", a, v);
+                       break;
+               }
+       else
+               printf("%hu:%hu", a, v);
+}
+
+static void
+print_ext_community(u_int8_t *data)
+{
+       u_int64_t       ext;
+       struct in_addr  ip;
+       u_int32_t       as4, u32;
+       u_int16_t       as2, u16;
+       u_int8_t        type, subtype;
+
+       type = data[0];
+       subtype = data[1];
+
+       printf("%s ", log_ext_subtype(type, subtype));
+
+       switch (type) {
+       case EXT_COMMUNITY_TRANS_TWO_AS:
+               memcpy(&as2, data + 2, sizeof(as2));
+               memcpy(&u32, data + 4, sizeof(u32));
+               printf("%s:%u", log_as(ntohs(as2)), ntohl(u32));
+               break;
+       case EXT_COMMUNITY_TRANS_IPV4:
+               memcpy(&ip, data + 2, sizeof(ip));
+               memcpy(&u16, data + 6, sizeof(u16));
+               printf("%s:%hu", inet_ntoa(ip), ntohs(u16));
+               break;
+       case EXT_COMMUNITY_TRANS_FOUR_AS:
+               memcpy(&as4, data + 2, sizeof(as4));
+               memcpy(&u16, data + 6, sizeof(u16));
+               printf("%s:%hu", log_as(ntohl(as4)), ntohs(u16));
+               break;
+       case EXT_COMMUNITY_TRANS_OPAQUE:
+       case EXT_COMMUNITY_TRANS_EVPN:
+               memcpy(&ext, data, sizeof(ext));
+               ext = be64toh(ext) & 0xffffffffffffLL;
+               printf("0x%llx", (unsigned long long)ext);
+               break;
+       case EXT_COMMUNITY_NON_TRANS_OPAQUE:
+               memcpy(&ext, data, sizeof(ext));
+               ext = be64toh(ext) & 0xffffffffffffLL;
+               switch (ext) {
+               case EXT_COMMUNITY_OVS_VALID:
+                       printf("valid ");
+                       break;
+               case EXT_COMMUNITY_OVS_NOTFOUND:
+                       printf("not-found ");
+                       break;
+               case EXT_COMMUNITY_OVS_INVALID:
+                       printf("invalid ");
+                       break;
+               default:
+                       printf("0x%llx ", (unsigned long long)ext);
+                       break;
+               }
+               break;
+       default:
+               memcpy(&ext, data, sizeof(ext));
+               printf("0x%llx", (unsigned long long)be64toh(ext));
+       }
+}
+
+void
+show_communities(u_char *data, size_t len, int flag0)
+{
+       struct community c;
+       size_t  i;
+       u_int64_t ext;
+       u_int8_t type = 0, nt;
+
+       if (len % sizeof(c))
+               return;
+
+       for (i = 0; i < len; i += sizeof(c)) {
+               memcpy(&c, data + i, sizeof(c));
+
+               nt = c.flags;
+               if (type != nt) {
+                       if (type != 0)
+                               printf("%c", EOL0(flag0));
+                       printf("    %s:", print_attr(nt,
+                           ATTR_OPTIONAL | ATTR_TRANSITIVE));
+                       type = nt;
+               }
+               printf(" ");
+
+               switch (nt) {
+               case COMMUNITY_TYPE_BASIC:
+                       print_community(c.data1, c.data2);
+                       break;
+               case COMMUNITY_TYPE_LARGE:
+                       printf("%u:%u:%u", c.data1, c.data2, c.data3);
+                       break;
+               case COMMUNITY_TYPE_EXT:
+                       ext = (u_int64_t)c.data3 << 48;
+                       switch (c.data3 >> 8) {
+                       case EXT_COMMUNITY_TRANS_TWO_AS:
+                       case EXT_COMMUNITY_TRANS_OPAQUE:
+                       case EXT_COMMUNITY_TRANS_EVPN:
+                       case EXT_COMMUNITY_NON_TRANS_OPAQUE:
+                               ext |= ((u_int64_t)c.data1 & 0xffff) << 32;
+                               ext |= (u_int64_t)c.data2;
+                               break;
+                       case EXT_COMMUNITY_TRANS_FOUR_AS:
+                       case EXT_COMMUNITY_TRANS_IPV4:
+                               ext |= (u_int64_t)c.data1 << 16;
+                               ext |= (u_int64_t)c.data2 & 0xffff;
+                               break;
+                       }
+                       ext = htobe64(ext);
+                       
+                       print_ext_community((void *)&ext);
+                       break;
+               }
+       }
+
+       printf("%c", EOL0(flag0));
+}
+
 void
 show_community(u_char *data, u_int16_t len)
 {
@@ -1646,33 +1801,7 @@ show_community(u_char *data, u_int16_t l
                memcpy(&v, data + i + 2, sizeof(v));
                a = ntohs(a);
                v = ntohs(v);
-               if (a == COMMUNITY_WELLKNOWN)
-                       switch (v) {
-                       case COMMUNITY_GRACEFUL_SHUTDOWN:
-                               printf("GRACEFUL_SHUTDOWN");
-                               break;
-                       case COMMUNITY_NO_EXPORT:
-                               printf("NO_EXPORT");
-                               break;
-                       case COMMUNITY_NO_ADVERTISE:
-                               printf("NO_ADVERTISE");
-                               break;
-                       case COMMUNITY_NO_EXPSUBCONFED:
-                               printf("NO_EXPORT_SUBCONFED");
-                               break;
-                       case COMMUNITY_NO_PEER:
-                               printf("NO_PEER");
-                               break;
-                       case COMMUNITY_BLACKHOLE:
-                               printf("BLACKHOLE");
-                               break;
-                       default:
-                               printf("%hu:%hu", a, v);
-                               break;
-                       }
-               else
-                       printf("%hu:%hu", a, v);
-
+               print_community(a, v);
                if (i + 4 < len)
                        printf(" ");
        }
@@ -1704,67 +1833,16 @@ show_large_community(u_char *data, u_int
 void
 show_ext_community(u_char *data, u_int16_t len)
 {
-       u_int64_t       ext;
-       struct in_addr  ip;
-       u_int32_t       as4, u32;
-       u_int16_t       i, as2, u16;
-       u_int8_t        type, subtype;
+       u_int16_t       i;
 
        if (len & 0x7)
                return;
 
        for (i = 0; i < len; i += 8) {
-               type = data[i];
-               subtype = data[i + 1];
+               print_ext_community(data + i);
 
-               printf("%s ", log_ext_subtype(type, subtype));
-
-               switch (type) {
-               case EXT_COMMUNITY_TRANS_TWO_AS:
-                       memcpy(&as2, data + i + 2, sizeof(as2));
-                       memcpy(&u32, data + i + 4, sizeof(u32));
-                       printf("%s:%u", log_as(ntohs(as2)), ntohl(u32));
-                       break;
-               case EXT_COMMUNITY_TRANS_IPV4:
-                       memcpy(&ip, data + i + 2, sizeof(ip));
-                       memcpy(&u16, data + i + 6, sizeof(u16));
-                       printf("%s:%hu", inet_ntoa(ip), ntohs(u16));
-                       break;
-               case EXT_COMMUNITY_TRANS_FOUR_AS:
-                       memcpy(&as4, data + i + 2, sizeof(as4));
-                       memcpy(&u16, data + i + 6, sizeof(u16));
-                       printf("%s:%hu", log_as(ntohl(as4)), ntohs(u16));
-                       break;
-               case EXT_COMMUNITY_TRANS_OPAQUE:
-               case EXT_COMMUNITY_TRANS_EVPN:
-                       memcpy(&ext, data + i, sizeof(ext));
-                       ext = be64toh(ext) & 0xffffffffffffLL;
-                       printf("0x%llx", (unsigned long long)ext);
-                       break;
-               case EXT_COMMUNITY_NON_TRANS_OPAQUE:
-                       memcpy(&ext, data + i, sizeof(ext));
-                       ext = be64toh(ext) & 0xffffffffffffLL;
-                       switch (ext) {
-                       case EXT_COMMUNITY_OVS_VALID:
-                               printf("valid ");
-                               break;
-                       case EXT_COMMUNITY_OVS_NOTFOUND:
-                               printf("not-found ");
-                               break;
-                       case EXT_COMMUNITY_OVS_INVALID:
-                               printf("invalid ");
-                               break;
-                       default:
-                               printf("0x%llx ", (unsigned long long)ext);
-                               break;
-                       }
-                       break;
-               default:
-                       memcpy(&ext, data + i, sizeof(ext));
-                       printf("0x%llx", (unsigned long long)be64toh(ext));
-               }
                if (i + 8 < len)
-                       printf(", ");
+                       printf(" ");
        }
 }
 
@@ -1817,6 +1895,12 @@ show_rib_memory_msg(struct imsg *imsg)
                    "%s of memory\n\t   and holding %lld references\n",
                    stats.aspath_cnt, fmt_mem(stats.aspath_size),
                    stats.aspath_refs);
+               printf("%10lld entries for %lld BGP communities "
+                   "using %s of memory\n", stats.comm_cnt, stats.comm_nmemb,
+                   fmt_mem(stats.comm_cnt * sizeof(struct rde_community) +
+                   stats.comm_size * sizeof(struct community)));
+               printf("\t   and holding %lld references\n",
+                   stats.comm_refs);
                printf("%10lld BGP attributes entries using %s of memory\n",
                    stats.attr_cnt, fmt_mem(stats.attr_cnt *
                    sizeof(struct attr)));
Index: usr.sbin/bgpctl/parser.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpctl/parser.c,v
retrieving revision 1.94
diff -u -p -r1.94 parser.c
--- usr.sbin/bgpctl/parser.c    23 May 2019 14:12:06 -0000      1.94
+++ usr.sbin/bgpctl/parser.c    28 May 2019 13:06:13 -0000
@@ -506,8 +506,8 @@ void                         show_valid_args(const struct 
tok
 int    parse_addr(const char *, struct bgpd_addr *);
 int    parse_asnum(const char *, size_t, u_int32_t *);
 int    parse_number(const char *, struct parse_result *, enum token_type);
-void   parsecommunity(struct filter_community *c, int type, char *s);
-int    parseextcommunity(struct filter_community *c, const char *t, char *s);
+void   parsecommunity(struct community *c, int type, char *s);
+void   parseextcommunity(struct community *c, const char *t, char *s);
 int    parse_nexthop(const char *, struct parse_result *);
 int    bgpctl_getopt(int *, char **[], int);
 
@@ -733,7 +733,7 @@ match_token(int *argc, char **argv[], co
                case RD:
                        if (word != NULL && wordlen > 0) {
                                char *p = strdup(word);
-                               struct filter_community ext;
+                               struct community ext;
                                u_int64_t rd;
 
                                if (p == NULL)
@@ -741,21 +741,22 @@ match_token(int *argc, char **argv[], co
                                parseextcommunity(&ext, "rt", p);
                                free(p);
 
-                               switch (ext.c.e.type) {
+                               switch (ext.data3 >> 8) {
                                case EXT_COMMUNITY_TRANS_TWO_AS:
                                        rd = (0ULL << 48);
-                                       rd |= (u_int64_t)ext.c.e.data1 << 32;
-                                       rd |= ext.c.e.data2 & 0xffffffff;
-                                       break;
+                                       rd |= ((u_int64_t)ext.data1 & 0xffff)
+                                           << 32;
+                                       rd |= (u_int64_t)ext.data2;
+                               break;
                                case EXT_COMMUNITY_TRANS_IPV4:
                                        rd = (1ULL << 48);
-                                       rd |= (u_int64_t)ext.c.e.data1 << 16;
-                                       rd |= ext.c.e.data2 & 0xffff;
+                                       rd |= (u_int64_t)ext.data1 << 16;
+                                       rd |= (u_int64_t)ext.data2 & 0xffff;
                                        break;
                                case EXT_COMMUNITY_TRANS_FOUR_AS:
                                        rd = (2ULL << 48);
-                                       rd |= (u_int64_t)ext.c.e.data1 << 16;
-                                       rd |= ext.c.e.data2 & 0xffff;
+                                       rd |= (u_int64_t)ext.data1 << 16;
+                                       rd |= (u_int64_t)ext.data2 & 0xffff;
                                        break;
                                default:
                                        errx(1, "bad encoding of rd");
@@ -1098,7 +1099,7 @@ parse_number(const char *word, struct pa
 }
 
 static void
-getcommunity(char *s, int large, u_int32_t *val, u_int8_t *flag)
+getcommunity(char *s, int large, u_int32_t *val, u_int32_t *flag)
 {
        long long        max = USHRT_MAX;
        const char      *errstr;
@@ -1123,21 +1124,22 @@ getcommunity(char *s, int large, u_int32
 }
 
 static void
-setcommunity(struct filter_community *c, u_int32_t as, u_int32_t data,
-    u_int8_t asflag, u_int8_t dataflag)
+setcommunity(struct community *c, u_int32_t as, u_int32_t data,
+    u_int32_t asflag, u_int32_t dataflag)
 {
-       memset(c, 0, sizeof(*c));
-       c->type = COMMUNITY_TYPE_BASIC;
-       c->dflag1 = asflag;
-       c->dflag2 = dataflag;
-       c->c.b.data1 = as;
-       c->c.b.data2 = data;
+       c->flags = COMMUNITY_TYPE_BASIC;
+       c->flags |= asflag << 8;
+       c->flags |= dataflag << 16;
+       c->data1 = as;
+       c->data2 = data;
+       c->data3 = 0;
 }
 
 static void
-parselargecommunity(struct filter_community *c, char *s)
+parselargecommunity(struct community *c, char *s)
 {
        char *p, *q;
+       u_int32_t dflag1, dflag2, dflag3;
 
        if ((p = strchr(s, ':')) == NULL)
                errx(1, "Bad community syntax");
@@ -1147,19 +1149,21 @@ parselargecommunity(struct filter_commun
                errx(1, "Bad community syntax");
        *q++ = 0;
 
-       getcommunity(s, 1, &c->c.l.data1, &c->dflag1);
-       getcommunity(p, 1, &c->c.l.data2, &c->dflag2);
-       getcommunity(q, 1, &c->c.l.data3, &c->dflag3);
-
-       c->type = COMMUNITY_TYPE_LARGE;
+       getcommunity(s, 1, &c->data1, &dflag1);
+       getcommunity(p, 1, &c->data2, &dflag2);
+       getcommunity(q, 1, &c->data3, &dflag3);
+
+       c->flags = COMMUNITY_TYPE_LARGE;
+       c->flags |= dflag1 << 8;;
+       c->flags |= dflag2 << 16;;
+       c->flags |= dflag3 << 24;;
 }
 
 void
-parsecommunity(struct filter_community *c, int type, char *s)
+parsecommunity(struct community *c, int type, char *s)
 {
        char *p;
-       u_int32_t as, data;
-       u_int8_t asflag, dataflag;
+       u_int32_t as, data, asflag, dataflag;
 
        if (type == COMMUNITY_TYPE_LARGE) {
                parselargecommunity(c, s);
@@ -1208,7 +1212,6 @@ parsesubtype(const char *name, int *type
        const struct ext_comm_pairs *cp;
        int found = 0;
 
-printf("%s: looking for %s\n", __func__, name);
        for (cp = iana_ext_comms; cp->subname != NULL; cp++) {
                if (strcmp(name, cp->subname) == 0) {
                        if (found == 0) {
@@ -1224,7 +1227,7 @@ printf("%s: looking for %s\n", __func__,
 }
 
 static int
-parseextvalue(int type, char *s, u_int32_t *v)
+parseextvalue(int type, char *s, u_int32_t *v, u_int32_t *flag)
 {
        const char      *errstr;
        char            *p;
@@ -1233,6 +1236,14 @@ parseextvalue(int type, char *s, u_int32
 
        if (type != -1) {
                /* nothing */
+       } else if (strcmp(s, "neighbor-as") == 0) {
+               *flag = COMMUNITY_NEIGHBOR_AS;
+               *v = 0;
+               return EXT_COMMUNITY_TRANS_FOUR_AS;
+       } else if (strcmp(s, "local-as") == 0) {
+               *flag = COMMUNITY_LOCAL_AS;
+               *v = 0;
+               return EXT_COMMUNITY_TRANS_FOUR_AS;
        } else if ((p = strchr(s, '.')) == NULL) {
                /* AS_PLAIN number (4 or 2 byte) */
                strtonum(s, 0, USHRT_MAX, &errstr);
@@ -1284,16 +1295,20 @@ parseextvalue(int type, char *s, u_int32
        return (type);
 }
 
-int
-parseextcommunity(struct filter_community *c, const char *t, char *s)
+void
+parseextcommunity(struct community *c, const char *t, char *s)
 {
        const struct ext_comm_pairs *cp;
-       const char      *errstr;
-       u_int64_t        ullval;
-       u_int32_t        uval;
        char            *p, *ep;
+       u_int64_t        ullval;
+       u_int32_t        uval, uval2, dflag1 = 0, dflag2 = 0;
        int              type = 0, subtype = 0;
 
+       if (strcmp(t, "*") == 0 && strcmp(s, "*") == 0) {
+               c->flags = COMMUNITY_TYPE_EXT;
+               c->flags |= COMMUNITY_ANY << 24;
+               return;
+       }
        if (parsesubtype(t, &type, &subtype) == 0)
                errx(1, "Bad ext-community unknown type");
 
@@ -1302,55 +1317,80 @@ parseextcommunity(struct filter_communit
        case EXT_COMMUNITY_TRANS_FOUR_AS:
        case EXT_COMMUNITY_TRANS_IPV4:
        case -1:
+               if (strcmp(s, "*") == 0) {
+                       dflag1 = COMMUNITY_ANY;
+                       break;
+               }
                if ((p = strchr(s, ':')) == NULL)
                        errx(1, "Bad ext-community %s", s);
                *p++ = '\0';
-               type = parseextvalue(type, s, &uval);
+               type = parseextvalue(type, s, &uval, &dflag1);
+
                switch (type) {
                case EXT_COMMUNITY_TRANS_TWO_AS:
-                       ullval = strtonum(p, 0, UINT_MAX, &errstr);
+                       getcommunity(p, 1, &uval2, &dflag2);
                        break;
                case EXT_COMMUNITY_TRANS_IPV4:
                case EXT_COMMUNITY_TRANS_FOUR_AS:
-                       ullval = strtonum(p, 0, USHRT_MAX, &errstr);
+                       getcommunity(p, 0, &uval2, &dflag2);
                        break;
                default:
                        errx(1, "parseextcommunity: unexpected result");
                }
-               if (errstr)
-                       errx(1, "Bad ext-community %s is %s", p, errstr);
-               c->c.e.data1 = uval;
-               c->c.e.data2 = ullval;
+
+               c->data1 = uval;
+               c->data2 = uval2;
                break;
        case EXT_COMMUNITY_TRANS_OPAQUE:
        case EXT_COMMUNITY_TRANS_EVPN:
+               if (strcmp(s, "*") == 0) {
+                       dflag1 = COMMUNITY_ANY;
+                       break;
+               }
                errno = 0;
                ullval = strtoull(s, &ep, 0);
                if (s[0] == '\0' || *ep != '\0')
                        errx(1, "Bad ext-community bad value");
                if (errno == ERANGE && ullval > EXT_COMMUNITY_OPAQUE_MAX)
                        errx(1, "Bad ext-community value too big");
-               c->c.e.data2 = ullval;
+               c->data1 = ullval >> 32;
+               c->data2 = ullval;
                break;
        case EXT_COMMUNITY_NON_TRANS_OPAQUE:
-               if (strcmp(s, "valid") == 0)
-                       c->c.e.data2 = EXT_COMMUNITY_OVS_VALID;
-               else if (strcmp(s, "invalid") == 0)
-                       c->c.e.data2 = EXT_COMMUNITY_OVS_INVALID;
-               else if (strcmp(s, "not-found") == 0)
-                       c->c.e.data2 = EXT_COMMUNITY_OVS_NOTFOUND;
-               else
-                       errx(1, "Bad ext-community %s", s);
-               break;
+               if (subtype == EXT_COMMUNITY_SUBTYPE_OVS) {
+                       if (strcmp(s, "valid") == 0) {
+                               c->data2 = EXT_COMMUNITY_OVS_VALID;
+                               break;
+                       } else if (strcmp(s, "invalid") == 0) {
+                               c->data2 = EXT_COMMUNITY_OVS_INVALID;
+                               break;
+                       } else if (strcmp(s, "not-found") == 0) {
+                               c->data2 = EXT_COMMUNITY_OVS_NOTFOUND;
+                               break;
+                       } else if (strcmp(s, "*") == 0) {
+                               dflag1 = COMMUNITY_ANY;
+                               break;
+                       }
+               }
+               errx(1, "Bad ext-community %s", s);
        }
-       c->c.e.type = type;
-       c->c.e.subtype = subtype;
 
+       c->data3 = type << 8 | subtype;
+
+       /* special handling of ext-community rt * since type is not known */
+       if (dflag1 == COMMUNITY_ANY && type == -1) {
+               c->flags = COMMUNITY_TYPE_EXT;
+               c->flags |= dflag1 << 8;
+               return;
+       }
+               
        /* verify type/subtype combo */
        for (cp = iana_ext_comms; cp->subname != NULL; cp++) {
                if (cp->type == type && cp->subtype == subtype) {
-                       c->type = COMMUNITY_TYPE_EXT;
-                       return (0);
+                       c->flags = COMMUNITY_TYPE_EXT;
+                       c->flags |= dflag1 << 8;
+                       c->flags |= dflag2 << 16;
+                       return;
                }
        }
 
Index: usr.sbin/bgpctl/parser.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpctl/parser.h,v
retrieving revision 1.35
diff -u -p -r1.35 parser.h
--- usr.sbin/bgpctl/parser.h    11 Feb 2019 15:47:55 -0000      1.35
+++ usr.sbin/bgpctl/parser.h    28 May 2019 12:44:21 -0000
@@ -62,7 +62,7 @@ struct parse_result {
        struct bgpd_addr         peeraddr;
        struct filter_as         as;
        struct filter_set_head   set;
-       struct filter_community  community;
+       struct community         community;
        char                     peerdesc[PEER_DESCR_LEN];
        char                     rib[PEER_DESCR_LEN];
        char                     shutcomm[SHUT_COMM_LEN];
Index: usr.sbin/bgpd/Makefile
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/Makefile,v
retrieving revision 1.34
diff -u -p -r1.34 Makefile
--- usr.sbin/bgpd/Makefile      7 Sep 2018 10:49:22 -0000       1.34
+++ usr.sbin/bgpd/Makefile      12 May 2019 15:25:13 -0000
@@ -2,8 +2,8 @@
 
 PROG=  bgpd
 SRCS=  bgpd.c session.c log.c logmsg.c parse.y config.c \
-       rde.c rde_rib.c rde_decide.c rde_prefix.c mrt.c kroute.c \
-       control.c pfkey.c rde_update.c rde_attr.c printconf.c \
+       rde.c rde_rib.c rde_decide.c rde_prefix.c mrt.c kroute.c control.c \
+       pfkey.c rde_update.c rde_attr.c rde_community.c printconf.c \
        rde_filter.c rde_sets.c rde_trie.c pftable.c name2id.c \
        util.c carp.c timer.c
 CFLAGS+= -Wall -I${.CURDIR}
Index: usr.sbin/bgpd/bgpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/bgpd.h,v
retrieving revision 1.384
diff -u -p -r1.384 bgpd.h
--- usr.sbin/bgpd/bgpd.h        27 May 2019 09:14:32 -0000      1.384
+++ usr.sbin/bgpd/bgpd.h        28 May 2019 13:29:25 -0000
@@ -413,7 +413,6 @@ enum network_type {
 struct network_config {
        struct bgpd_addr         prefix;
        struct filter_set_head   attrset;
-       struct rde_aspath       *asp;
        char                     psname[SET_NAME_LEN];
        u_int64_t                rd;
        u_int16_t                rtlabel;
@@ -447,6 +446,7 @@ enum imsg_type {
        IMSG_CTL_SHOW_INTERFACE,
        IMSG_CTL_SHOW_RIB,
        IMSG_CTL_SHOW_RIB_PREFIX,
+       IMSG_CTL_SHOW_RIB_COMMUNITIES,
        IMSG_CTL_SHOW_RIB_ATTR,
        IMSG_CTL_SHOW_NETWORK,
        IMSG_CTL_SHOW_RIB_MEM,
@@ -771,28 +771,12 @@ struct filter_ovs {
        u_int8_t                 is_set;
 };
 
-struct filter_community {
-       u_int8_t        type;
-       u_int8_t        dflag1; /* one of set, any, local-as, neighbor-as */
-       u_int8_t        dflag2;
-       u_int8_t        dflag3;
-       union {
-               struct basic {
-                       u_int32_t       data1;
-                       u_int32_t       data2;
-               } b;
-               struct large {
-                       u_int32_t       data1;
-                       u_int32_t       data2;
-                       u_int32_t       data3;
-               } l;
-               struct ext {
-                       u_int32_t       data1;
-                       u_int64_t       data2;
-                       short           type;
-                       u_int8_t        subtype;        /* if extended type */
-               } e;
-       }               c;
+struct community {
+       u_int32_t       flags;
+#define        COMMUNITY_TYPE_MASK     0xff
+       u_int32_t       data1;
+       u_int32_t       data2;
+       u_int32_t       data3;
 };
 
 struct ctl_show_rib_request {
@@ -800,7 +784,7 @@ struct ctl_show_rib_request {
        struct ctl_neighbor     neighbor;
        struct bgpd_addr        prefix;
        struct filter_as        as;
-       struct filter_community community;
+       struct community        community;
        u_int32_t               flags;
        u_int8_t                validation_state;
        pid_t                   pid;
@@ -848,11 +832,11 @@ struct filter_peers {
        u_int8_t        ibgp;
 };
 
-/* special community type */
+/* special community type, keep in sync with the attribute type */
 #define        COMMUNITY_TYPE_NONE             0
-#define        COMMUNITY_TYPE_BASIC            1
-#define        COMMUNITY_TYPE_EXT              2
-#define        COMMUNITY_TYPE_LARGE            3
+#define        COMMUNITY_TYPE_BASIC            8
+#define        COMMUNITY_TYPE_EXT              16
+#define        COMMUNITY_TYPE_LARGE            32
 
 #define        COMMUNITY_ANY                   1
 #define        COMMUNITY_NEIGHBOR_AS           2
@@ -869,7 +853,7 @@ struct filter_peers {
 
 /* extended community definitions */
 #define EXT_COMMUNITY_IANA             0x80
-#define EXT_COMMUNITY_TRANSITIVE       0x40
+#define EXT_COMMUNITY_NON_TRANSITIVE   0x40
 #define EXT_COMMUNITY_VALUE            0x3f
 /* extended types transitive */
 #define EXT_COMMUNITY_TRANS_TWO_AS     0x00    /* 2 octet AS specific */
@@ -955,7 +939,7 @@ struct filter_match {
        struct filter_nexthop           nexthop;
        struct filter_as                as;
        struct filter_aslen             aslen;
-       struct filter_community         community[MAX_COMM_MATCH];
+       struct community                community[MAX_COMM_MATCH];
        struct filter_prefixset         prefixset;
        struct filter_originset         originset;
        struct filter_ovs               ovs;
@@ -1012,7 +996,7 @@ struct filter_set {
                int32_t                          relative;
                struct bgpd_addr                 nexthop;
                struct nexthop                  *nh;
-               struct filter_community          community;
+               struct community                 community;
                char                             pftable[PFTABLE_LEN];
                char                             rtlabel[RTLABEL_LEN];
                u_int8_t                         origin;
@@ -1090,6 +1074,10 @@ struct rde_memstats {
        long long       aspath_cnt;
        long long       aspath_size;
        long long       aspath_refs;
+       long long       comm_cnt;
+       long long       comm_nmemb;
+       long long       comm_size;
+       long long       comm_refs;
        long long       attr_cnt;
        long long       attr_refs;
        long long       attr_data;
Index: usr.sbin/bgpd/parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/parse.y,v
retrieving revision 1.389
diff -u -p -r1.389 parse.y
--- usr.sbin/bgpd/parse.y       31 May 2019 08:22:25 -0000      1.389
+++ usr.sbin/bgpd/parse.y       31 May 2019 08:59:41 -0000
@@ -153,8 +153,8 @@ int          merge_filterset(struct filter_set_
 void            optimize_filters(struct filter_head *);
 struct filter_rule     *get_rule(enum action_types);
 
-int             parsecommunity(struct filter_community *, int, char *);
-int             parseextcommunity(struct filter_community *, char *,
+int             parsecommunity(struct community *, int, char *);
+int             parseextcommunity(struct community *, char *,
                    char *);
 static int      new_as_set(char *);
 static void     add_as_set(u_int32_t);
@@ -1103,8 +1103,7 @@ l3vpnopts_l       : /* empty */
                ;
 
 l3vpnopts      : RD STRING {
-                       struct filter_community ext;
-                       u_int64_t               rd;
+                       struct community        ext;
 
                        memset(&ext, 0, sizeof(ext));
                        if (parseextcommunity(&ext, "rt", $2) == -1) {
@@ -1116,26 +1115,10 @@ l3vpnopts       : RD STRING {
                         * RD is almost encode like an ext-community,
                         * but only almost so convert here.
                         */
-                       if (community_ext_conv(&ext, NULL, &rd, NULL)) {
+                       if (community_to_rd(&ext, &curvpn->rd) == -1) {
                                yyerror("bad encoding of rd");
                                YYERROR;
                        }
-                       rd = be64toh(rd) & 0xffffffffffffULL;
-                       switch (ext.c.e.type) {
-                       case EXT_COMMUNITY_TRANS_TWO_AS:
-                               rd |= (0ULL << 48);
-                               break;
-                       case EXT_COMMUNITY_TRANS_IPV4:
-                               rd |= (1ULL << 48);
-                               break;
-                       case EXT_COMMUNITY_TRANS_FOUR_AS:
-                               rd |= (2ULL << 48);
-                               break;
-                       default:
-                               yyerror("bad encoding of rd");
-                               YYERROR;
-                       }
-                       curvpn->rd = htobe64(rd);
                }
                | EXPORTTRGT STRING STRING      {
                        struct filter_set       *set;
@@ -2113,8 +2096,7 @@ filter_elm        : filter_prefix_h       {
                | community STRING      {
                        int i;
                        for (i = 0; i < MAX_COMM_MATCH; i++) {
-                               if (fmopts.m.community[i].type ==
-                                   COMMUNITY_TYPE_NONE)
+                               if (fmopts.m.community[i].flags == 0)
                                        break;
                        }
                        if (i >= MAX_COMM_MATCH) {
@@ -2132,8 +2114,7 @@ filter_elm        : filter_prefix_h       {
                | EXTCOMMUNITY STRING STRING {
                        int i;
                        for (i = 0; i < MAX_COMM_MATCH; i++) {
-                               if (fmopts.m.community[i].type ==
-                                   COMMUNITY_TYPE_NONE)
+                               if (fmopts.m.community[i].flags == 0)
                                        break;
                        }
                        if (i >= MAX_COMM_MATCH) {
@@ -2155,8 +2136,7 @@ filter_elm        : filter_prefix_h       {
                | EXTCOMMUNITY OVS STRING {
                        int i;
                        for (i = 0; i < MAX_COMM_MATCH; i++) {
-                               if (fmopts.m.community[i].type ==
-                                   COMMUNITY_TYPE_NONE)
+                               if (fmopts.m.community[i].flags == 0)
                                        break;
                        }
                        if (i >= MAX_COMM_MATCH) {
@@ -2616,6 +2596,8 @@ filter_set_opt    : LOCALPREF NUMBER              {
                        free($2);
                }
                | community delete STRING       {
+                       u_int8_t f1, f2, f3;
+
                        if (($$ = calloc(1, sizeof(struct filter_set))) == NULL)
                                fatal(NULL);
                        if ($2)
@@ -2631,10 +2613,11 @@ filter_set_opt  : LOCALPREF NUMBER              {
                        }
                        free($3);
                        /* Don't allow setting of any match */
-                       if (!$2 &&
-                           ($$->action.community.dflag1 == COMMUNITY_ANY ||
-                           $$->action.community.dflag2 == COMMUNITY_ANY ||
-                           $$->action.community.dflag3 == COMMUNITY_ANY)) {
+                       f1 = $$->action.community.flags >> 8;
+                       f2 = $$->action.community.flags >> 16;
+                       f3 = $$->action.community.flags >> 24;
+                       if (!$2 && (f1 == COMMUNITY_ANY ||
+                           f2 == COMMUNITY_ANY || f3 == COMMUNITY_ANY)) {
                                yyerror("'*' is not allowed in set community");
                                free($$);
                                YYERROR;
@@ -3450,7 +3433,7 @@ symget(const char *nam)
 }
 
 static int
-getcommunity(char *s, int large, u_int32_t *val, u_int8_t *flag)
+getcommunity(char *s, int large, u_int32_t *val, u_int32_t *flag)
 {
        long long        max = USHRT_MAX;
        const char      *errstr;
@@ -3478,21 +3461,22 @@ getcommunity(char *s, int large, u_int32
 }
 
 static void
-setcommunity(struct filter_community *c, u_int32_t as, u_int32_t data,
-    u_int8_t asflag, u_int8_t dataflag)
+setcommunity(struct community *c, u_int32_t as, u_int32_t data,
+    u_int32_t asflag, u_int32_t dataflag)
 {
-       memset(c, 0, sizeof(*c));
-       c->type = COMMUNITY_TYPE_BASIC;
-       c->dflag1 = asflag;
-       c->dflag2 = dataflag;
-       c->c.b.data1 = as;
-       c->c.b.data2 = data;
+       c->flags = COMMUNITY_TYPE_BASIC;
+       c->flags |= asflag << 8;
+       c->flags |= dataflag << 16;
+       c->data1 = as;
+       c->data2 = data;
+       c->data3 = 0;
 }
 
 static int
-parselargecommunity(struct filter_community *c, char *s)
+parselargecommunity(struct community *c, char *s)
 {
        char *p, *q;
+       u_int32_t dflag1, dflag2, dflag3;
 
        if ((p = strchr(s, ':')) == NULL) {
                yyerror("Bad community syntax");
@@ -3506,20 +3490,22 @@ parselargecommunity(struct filter_commun
        }
        *q++ = 0;
 
-       if (getcommunity(s, 1, &c->c.l.data1, &c->dflag1) == -1 ||
-           getcommunity(p, 1, &c->c.l.data2, &c->dflag2) == -1 ||
-           getcommunity(q, 1, &c->c.l.data3, &c->dflag3) == -1)
+       if (getcommunity(s, 1, &c->data1, &dflag1) == -1 ||
+           getcommunity(p, 1, &c->data2, &dflag2) == -1 ||
+           getcommunity(q, 1, &c->data3, &dflag3) == -1)
                return (-1);
-       c->type = COMMUNITY_TYPE_LARGE;
+       c->flags = COMMUNITY_TYPE_LARGE;
+       c->flags |= dflag1 << 8;;
+       c->flags |= dflag2 << 16;;
+       c->flags |= dflag3 << 24;;
        return (0);
 }
 
 int
-parsecommunity(struct filter_community *c, int type, char *s)
+parsecommunity(struct community *c, int type, char *s)
 {
        char *p;
-       u_int32_t as, data;
-       u_int8_t asflag, dataflag;
+       u_int32_t as, data, asflag, dataflag;
 
        if (type == COMMUNITY_TYPE_LARGE)
                return parselargecommunity(c, s);
@@ -3585,7 +3571,7 @@ parsesubtype(char *name, int *type, int 
 }
 
 static int
-parseextvalue(int type, char *s, u_int32_t *v, u_int8_t *flag)
+parseextvalue(int type, char *s, u_int32_t *v, u_int32_t *flag)
 {
        const char      *errstr;
        char            *p;
@@ -3664,17 +3650,17 @@ parseextvalue(int type, char *s, u_int32
 }
 
 int
-parseextcommunity(struct filter_community *c, char *t, char *s)
+parseextcommunity(struct community *c, char *t, char *s)
 {
        const struct ext_comm_pairs *cp;
-       u_int64_t        ullval;
-       u_int32_t        uval, uval2;
        char            *p, *ep;
+       u_int64_t        ullval;
+       u_int32_t        uval, uval2, dflag1 = 0, dflag2 = 0;
        int              type = 0, subtype = 0;
 
        if (strcmp(t, "*") == 0 && strcmp(s, "*") == 0) {
-               c->type = COMMUNITY_TYPE_EXT;
-               c->dflag3 = COMMUNITY_ANY;
+               c->flags = COMMUNITY_TYPE_EXT;
+               c->flags |= COMMUNITY_ANY << 24;
                return (0);
        }
        if (parsesubtype(t, &type, &subtype) == 0) {
@@ -3688,7 +3674,7 @@ parseextcommunity(struct filter_communit
        case EXT_COMMUNITY_TRANS_IPV4:
        case -1:
                if (strcmp(s, "*") == 0) {
-                       c->dflag1 = COMMUNITY_ANY;
+                       dflag1 = COMMUNITY_ANY;
                        break;
                }
                if ((p = strchr(s, ':')) == NULL) {
@@ -3696,28 +3682,30 @@ parseextcommunity(struct filter_communit
                        return (-1);
                }
                *p++ = '\0';
-               if ((type = parseextvalue(type, s, &uval, &c->dflag1)) == -1)
+               if ((type = parseextvalue(type, s, &uval, &dflag1)) == -1)
                        return (-1);
+
                switch (type) {
                case EXT_COMMUNITY_TRANS_TWO_AS:
-                       if (getcommunity(p, 1, &uval2, &c->dflag2) == -1)
+                       if (getcommunity(p, 1, &uval2, &dflag2) == -1)
                                return (-1);
                        break;
                case EXT_COMMUNITY_TRANS_IPV4:
                case EXT_COMMUNITY_TRANS_FOUR_AS:
-                       if (getcommunity(p, 0, &uval2, &c->dflag2) == -1)
+                       if (getcommunity(p, 0, &uval2, &dflag2) == -1)
                                return (-1);
                        break;
                default:
                        fatalx("parseextcommunity: unexpected result");
                }
-               c->c.e.data1 = uval;
-               c->c.e.data2 = uval2;
+
+               c->data1 = uval;
+               c->data2 = uval2;
                break;
        case EXT_COMMUNITY_TRANS_OPAQUE:
        case EXT_COMMUNITY_TRANS_EVPN:
                if (strcmp(s, "*") == 0) {
-                       c->dflag1 = COMMUNITY_ANY;
+                       dflag1 = COMMUNITY_ANY;
                        break;
                }
                errno = 0;
@@ -3730,40 +3718,44 @@ parseextcommunity(struct filter_communit
                        yyerror("Bad ext-community value too big");
                        return (-1);
                }
-               c->c.e.data2 = ullval;
+               c->data1 = ullval >> 32;
+               c->data2 = ullval;
                break;
        case EXT_COMMUNITY_NON_TRANS_OPAQUE:
                if (subtype == EXT_COMMUNITY_SUBTYPE_OVS) {
                        if (strcmp(s, "valid") == 0) {
-                               c->c.e.data2 = EXT_COMMUNITY_OVS_VALID;
+                               c->data2 = EXT_COMMUNITY_OVS_VALID;
                                break;
                        } else if (strcmp(s, "invalid") == 0) {
-                               c->c.e.data2 = EXT_COMMUNITY_OVS_INVALID;
+                               c->data2 = EXT_COMMUNITY_OVS_INVALID;
                                break;
                        } else if (strcmp(s, "not-found") == 0) {
-                               c->c.e.data2 = EXT_COMMUNITY_OVS_NOTFOUND;
+                               c->data2 = EXT_COMMUNITY_OVS_NOTFOUND;
                                break;
                        } else if (strcmp(s, "*") == 0) {
-                               c->dflag1 = COMMUNITY_ANY;
+                               dflag1 = COMMUNITY_ANY;
                                break;
                        }
                }
                yyerror("Bad ext-community %s", s);
                return (-1);
        }
-       c->c.e.type = type;
-       c->c.e.subtype = subtype;
+
+       c->data3 = type << 8 | subtype;
 
        /* special handling of ext-community rt * since type is not known */
-       if (c->dflag1 == COMMUNITY_ANY && c->c.e.type == -1) {
-               c->type = COMMUNITY_TYPE_EXT;
+       if (dflag1 == COMMUNITY_ANY && type == -1) {
+               c->flags = COMMUNITY_TYPE_EXT;
+               c->flags |= dflag1 << 8;
                return (0);
        }
                
        /* verify type/subtype combo */
        for (cp = iana_ext_comms; cp->subname != NULL; cp++) {
                if (cp->type == type && cp->subtype == subtype) {
-                       c->type = COMMUNITY_TYPE_EXT;
+                       c->flags = COMMUNITY_TYPE_EXT;
+                       c->flags |= dflag1 << 8;
+                       c->flags |= dflag2 << 16;
                        return (0);
                }
        }
Index: usr.sbin/bgpd/printconf.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/printconf.c,v
retrieving revision 1.135
diff -u -p -r1.135 printconf.c
--- usr.sbin/bgpd/printconf.c   27 May 2019 09:14:32 -0000      1.135
+++ usr.sbin/bgpd/printconf.c   28 May 2019 13:29:25 -0000
@@ -30,8 +30,8 @@
 #include "log.h"
 
 void            print_prefix(struct filter_prefix *p);
-const char     *community_type(struct filter_community *c);
-void            print_community(struct filter_community *c);
+const char     *community_type(struct community *c);
+void            print_community(struct community *c);
 void            print_origin(u_int8_t);
 void            print_set(struct filter_set_head *);
 void            print_mainconf(struct bgpd_config *);
@@ -105,9 +105,9 @@ print_prefix(struct filter_prefix *p)
 }
 
 const char *
-community_type(struct filter_community *c)
+community_type(struct community *c)
 {
-       switch (c->type) {
+       switch ((u_int8_t)c->flags) {
        case COMMUNITY_TYPE_BASIC:
                return "community";
        case COMMUNITY_TYPE_LARGE:
@@ -120,13 +120,15 @@ community_type(struct filter_community *
 }
 
 void
-print_community(struct filter_community *c)
+print_community(struct community *c)
 {
        struct in_addr addr;
+       short type;
+       u_int8_t subtype;
 
-       switch (c->type) {
+       switch ((u_int8_t)c->flags) {
        case COMMUNITY_TYPE_BASIC:
-               switch (c->dflag1) {
+               switch ((c->flags >> 8) & 0xff) {
                case COMMUNITY_ANY:
                        printf("*:");
                        break;
@@ -137,10 +139,10 @@ print_community(struct filter_community 
                        printf("local-as:");
                        break;
                default:
-                       printf("%u:", c->c.b.data1);
+                       printf("%u:", c->data1);
                        break;
                }
-               switch (c->dflag2) {
+               switch ((c->flags >> 16) & 0xff) {
                case COMMUNITY_ANY:
                        printf("* ");
                        break;
@@ -151,12 +153,12 @@ print_community(struct filter_community 
                        printf("local-as ");
                        break;
                default:
-                       printf("%u ", c->c.b.data2);
+                       printf("%u ", c->data2);
                        break;
                }
                break;
        case COMMUNITY_TYPE_LARGE:
-               switch (c->dflag1) {
+               switch ((c->flags >> 8) & 0xff) {
                case COMMUNITY_ANY:
                        printf("*:");
                        break;
@@ -167,10 +169,10 @@ print_community(struct filter_community 
                        printf("local-as:");
                        break;
                default:
-                       printf("%u:", c->c.l.data1);
+                       printf("%u:", c->data1);
                        break;
                }
-               switch (c->dflag2) {
+               switch ((c->flags >> 16) & 0xff) {
                case COMMUNITY_ANY:
                        printf("*:");
                        break;
@@ -181,10 +183,10 @@ print_community(struct filter_community 
                        printf("local-as:");
                        break;
                default:
-                       printf("%u:", c->c.l.data2);
+                       printf("%u:", c->data2);
                        break;
                }
-               switch (c->dflag3) {
+               switch ((c->flags >> 24) & 0xff) {
                case COMMUNITY_ANY:
                        printf("* ");
                        break;
@@ -195,58 +197,56 @@ print_community(struct filter_community 
                        printf("local-as ");
                        break;
                default:
-                       printf("%u ", c->c.l.data3);
+                       printf("%u ", c->data3);
                        break;
                }
                break;
        case COMMUNITY_TYPE_EXT:
-               if (c->dflag3 == COMMUNITY_ANY) {
+               if ((c->flags >> 24 & 0xff) == COMMUNITY_ANY) {
                        printf("* * ");
                        break;
                }
-               printf("%s ", log_ext_subtype(c->c.e.type, c->c.e.subtype));
-               if (c->dflag1 == COMMUNITY_ANY) {
+               type = (int32_t)c->data3 >> 8;
+               subtype = c->data3;
+               printf("%s ", log_ext_subtype(type, subtype));
+               if ((c->flags >> 8 & 0xff) == COMMUNITY_ANY) {
                        printf("* ");
                        break;
                }
 
-               switch (c->c.e.type) {
+               switch (type) {
                case EXT_COMMUNITY_TRANS_TWO_AS:
                case EXT_COMMUNITY_TRANS_FOUR_AS:
-                       if (c->dflag1 == COMMUNITY_NEIGHBOR_AS)
+                       if ((c->flags >> 8 & 0xff) == COMMUNITY_NEIGHBOR_AS)
                                printf("neighbor-as:");
-                       else if (c->dflag1 == COMMUNITY_LOCAL_AS)
+                       else if ((c->flags >> 8 & 0xff) == COMMUNITY_LOCAL_AS)
                                printf("local-as:");
                        else
-                               printf("%s:", log_as(c->c.e.data1));
+                               printf("%s:", log_as(c->data1));
                        break;
                case EXT_COMMUNITY_TRANS_IPV4:
-                       addr.s_addr = htonl(c->c.e.data1);
+                       addr.s_addr = htonl(c->data1);
                        printf("%s:", inet_ntoa(addr));
                        break;
                }
 
-               switch (c->c.e.type) {
+               switch (type) {
                case EXT_COMMUNITY_TRANS_TWO_AS:
                case EXT_COMMUNITY_TRANS_FOUR_AS:
                case EXT_COMMUNITY_TRANS_IPV4:
-                       if (c->dflag2 == COMMUNITY_ANY)
+                       if ((c->flags >> 16 & 0xff) == COMMUNITY_ANY)
                                printf("* ");
-                       else if (c->dflag2 == COMMUNITY_NEIGHBOR_AS)
+                       else if ((c->flags >> 16 & 0xff) ==
+                           COMMUNITY_NEIGHBOR_AS)
                                printf("neighbor-as ");
-                       else if (c->dflag2 == COMMUNITY_LOCAL_AS)
+                       else if ((c->flags >> 16 & 0xff) == COMMUNITY_LOCAL_AS)
                                printf("local-as ");
                        else
-                               printf("%llu ",
-                                   (unsigned long long)c->c.e.data2);
-                       break;
-               case EXT_COMMUNITY_TRANS_OPAQUE:
-               case EXT_COMMUNITY_TRANS_EVPN:
-                       printf("0x%llx ", (unsigned long long)c->c.e.data2);
+                               printf("%u ", c->data2);
                        break;
                case EXT_COMMUNITY_NON_TRANS_OPAQUE:
-                       if (c->c.e.subtype == EXT_COMMUNITY_SUBTYPE_OVS) {
-                               switch (c->c.e.data2) {
+                       if (subtype == EXT_COMMUNITY_SUBTYPE_OVS) {
+                               switch (c->data2) {
                                case EXT_COMMUNITY_OVS_VALID:
                                        printf("valid ");
                                        break;
@@ -259,10 +259,12 @@ print_community(struct filter_community 
                                }
                                break;
                        }
-                       printf("0x%llx ", (unsigned long long)c->c.e.data2);
+                       printf("0x%x%08x ", c->data1 & 0xffff, c->data2);
                        break;
+               case EXT_COMMUNITY_TRANS_OPAQUE:
+               case EXT_COMMUNITY_TRANS_EVPN:
                default:
-                       printf("0x%llx ", (unsigned long long)c->c.e.data2);
+                       printf("0x%x%08x ", c->data1 & 0xffff, c->data2);
                        break;
                }
        }
@@ -865,8 +867,8 @@ print_rule(struct bgpd_config *conf, str
        }
 
        for (i = 0; i < MAX_COMM_MATCH; i++) {
-               struct filter_community *c = &r->match.community[i];
-               if (c->type != COMMUNITY_TYPE_NONE) {
+               struct community *c = &r->match.community[i];
+               if (c->flags != 0) {
                        printf("%s ", community_type(c));
                        print_community(c);
                }
Index: usr.sbin/bgpd/rde.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.c,v
retrieving revision 1.468
diff -u -p -r1.468 rde.c
--- usr.sbin/bgpd/rde.c 31 Mar 2019 17:02:47 -0000      1.468
+++ usr.sbin/bgpd/rde.c 27 May 2019 19:12:26 -0000
@@ -57,7 +57,7 @@ void           rde_update_withdraw(struct rde_pe
                     u_int8_t);
 int             rde_attr_parse(u_char *, u_int16_t, struct rde_peer *,
                     struct filterstate *, struct mpattr *);
-int             rde_attr_add(struct rde_aspath *, u_char *, u_int16_t);
+int             rde_attr_add(struct filterstate *, u_char *, u_int16_t);
 u_int8_t        rde_attr_missing(struct rde_aspath *, int, u_int16_t);
 int             rde_get_mp_nexthop(u_char *, u_int16_t, u_int8_t,
                     struct filterstate *);
@@ -75,7 +75,7 @@ void           rde_dump_ctx_throttle(pid_t, int)
 void            rde_dump_ctx_terminate(pid_t);
 void            rde_dump_mrt_new(struct mrt *, pid_t, int);
 
-int             rde_l3vpn_import(struct rde_aspath *, struct l3vpn *);
+int             rde_l3vpn_import(struct rde_community *, struct l3vpn *);
 void            rde_reload_done(void);
 static void     rde_softreconfig_in_done(void *, u_int8_t);
 static void     rde_softreconfig_out_done(void *, u_int8_t);
@@ -104,7 +104,7 @@ void                 peer_dump(u_int32_t, u_int8_t);
 static void     peer_recv_eor(struct rde_peer *, u_int8_t);
 static void     peer_send_eor(struct rde_peer *, u_int8_t);
 
-void            network_add(struct network_config *, int);
+void            network_add(struct network_config *, struct filterstate *);
 void            network_delete(struct network_config *);
 static void     network_dump_upcall(struct rib_entry *, void *);
 static void     network_flush_upcall(struct rib_entry *, void *);
@@ -158,10 +158,10 @@ rde_sighdlr(int sig)
        }
 }
 
-u_int32_t      peerhashsize = 64;
+u_int32_t      peerhashsize = 1024;
 u_int32_t      pathhashsize = 128 * 1024;
 u_int32_t      attrhashsize = 16 * 1024;
-u_int32_t      nexthophashsize = 64;
+u_int32_t      nexthophashsize = 1024;
 
 void
 rde_main(int debug, int verbose)
@@ -213,6 +213,7 @@ rde_main(int debug, int verbose)
        pt_init();
        path_init(pathhashsize);
        aspath_init(pathhashsize);
+       communities_init(attrhashsize);
        attr_init(attrhashsize);
        nexthop_init(nexthophashsize);
        peer_init(peerhashsize);
@@ -352,6 +353,7 @@ rde_main(int debug, int verbose)
 }
 
 struct network_config   netconf_s, netconf_p;
+struct filterstate      netconf_state;
 struct filter_set_head *session_set, *parent_set;
 
 void
@@ -458,6 +460,14 @@ rde_dispatch_imsg_session(struct imsgbuf
                        memcpy(&netconf_s, imsg.data, sizeof(netconf_s));
                        TAILQ_INIT(&netconf_s.attrset);
                        session_set = &netconf_s.attrset;
+                       rde_filterstate_prep(&netconf_state, NULL, NULL, NULL,
+                           0);
+                       asp = &netconf_state.aspath;
+                       asp->aspath = aspath_get(NULL, 0);
+                       asp->origin = ORIGIN_IGP;
+                       asp->flags = F_ATTR_ORIGIN | F_ATTR_ASPATH |
+                           F_ATTR_LOCALPREF | F_PREFIX_ANNOUNCED |
+                           F_ANN_DYNAMIC;
                        break;
                case IMSG_NETWORK_ASPATH:
                        if (imsg.hdr.len - IMSG_HEADER_SIZE <
@@ -475,28 +485,28 @@ rde_dispatch_imsg_session(struct imsgbuf
                                bzero(&netconf_s, sizeof(netconf_s));
                                break;
                        }
-                       asp = path_get();
+                       asp = &netconf_state.aspath;
                        asp->lpref = csr.local_pref;
                        asp->med = csr.med;
                        asp->weight = csr.weight;
                        asp->flags = csr.flags;
                        asp->origin = csr.origin;
                        asp->flags |= F_PREFIX_ANNOUNCED | F_ANN_DYNAMIC;
+                       aspath_put(asp->aspath);
                        asp->aspath = aspath_get(asdata, csr.aspath_len);
-                       netconf_s.asp = asp;
                        break;
                case IMSG_NETWORK_ATTR:
                        if (imsg.hdr.len <= IMSG_HEADER_SIZE) {
                                log_warnx("rde_dispatch: wrong imsg len");
                                break;
                        }
-                       /* parse path attributes */
+                       /* parse optional path attributes */
                        len = imsg.hdr.len - IMSG_HEADER_SIZE;
-                       asp = netconf_s.asp;
-                       if (rde_attr_add(asp, imsg.data, len) == -1) {
+                       if (rde_attr_add(&netconf_state, imsg.data,
+                           len) == -1) {
                                log_warnx("rde_dispatch: bad network "
                                    "attribute");
-                               path_put(asp);
+                               rde_filterstate_clean(&netconf_state);
                                bzero(&netconf_s, sizeof(netconf_s));
                                break;
                        }
@@ -511,12 +521,12 @@ rde_dispatch_imsg_session(struct imsgbuf
                        case AID_INET:
                                if (netconf_s.prefixlen > 32)
                                        goto badnet;
-                               network_add(&netconf_s, 0);
+                               network_add(&netconf_s, &netconf_state);
                                break;
                        case AID_INET6:
                                if (netconf_s.prefixlen > 128)
                                        goto badnet;
-                               network_add(&netconf_s, 0);
+                               network_add(&netconf_s, &netconf_state);
                                break;
                        case 0:
                                /* something failed beforehands */
@@ -526,6 +536,7 @@ badnet:
                                log_warnx("request to insert invalid network");
                                break;
                        }
+                       rde_filterstate_clean(&netconf_state);
                        break;
                case IMSG_NETWORK_REMOVE:
                        if (imsg.hdr.len - IMSG_HEADER_SIZE !=
@@ -630,6 +641,9 @@ badnetdel:
                        aspath_hash_stats(&rdehash);
                        imsg_compose(ibuf_se_ctl, IMSG_CTL_SHOW_RIB_HASH, 0,
                            imsg.hdr.pid, -1, &rdehash, sizeof(rdehash));
+                       communities_hash_stats(&rdehash);
+                       imsg_compose(ibuf_se_ctl, IMSG_CTL_SHOW_RIB_HASH, 0,
+                           imsg.hdr.pid, -1, &rdehash, sizeof(rdehash));
                        attr_hash_stats(&rdehash);
                        imsg_compose(ibuf_se_ctl, IMSG_CTL_SHOW_RIB_HASH, 0,
                            imsg.hdr.pid, -1, &rdehash, sizeof(rdehash));
@@ -679,12 +693,14 @@ rde_dispatch_imsg_parent(struct imsgbuf 
        struct imsg              imsg;
        struct mrt               xmrt;
        struct rde_rib           rn;
+       struct filterstate       state;
        struct imsgbuf          *i;
        struct filter_head      *nr;
        struct filter_rule      *r;
        struct filter_set       *s;
        struct rib              *rib;
        struct rde_prefixset    *ps;
+       struct rde_aspath       *asp;
        struct prefixset_item    psi;
        char                    *name;
        size_t                   nmemb;
@@ -738,7 +754,16 @@ rde_dispatch_imsg_parent(struct imsgbuf 
                        break;
                case IMSG_NETWORK_DONE:
                        parent_set = NULL;
-                       network_add(&netconf_p, 1);
+
+                       rde_filterstate_prep(&state, NULL, NULL, NULL, 0);
+                       asp = &state.aspath;
+                       asp->aspath = aspath_get(NULL, 0);
+                       asp->origin = ORIGIN_IGP;
+                       asp->flags = F_ATTR_ORIGIN | F_ATTR_ASPATH |
+                           F_ATTR_LOCALPREF | F_PREFIX_ANNOUNCED;
+
+                       network_add(&netconf_p, &state);
+                       rde_filterstate_clean(&state);
                        break;
                case IMSG_NETWORK_REMOVE:
                        if (imsg.hdr.len - IMSG_HEADER_SIZE !=
@@ -1085,7 +1110,7 @@ rde_update_dispatch(struct imsg *imsg)
            imsg->hdr.len - IMSG_HEADER_SIZE - 4 - withdrawn_len - attrpath_len;
        bzero(&mpa, sizeof(mpa));
 
-       rde_filterstate_prep(&state, NULL, NULL, 0);
+       rde_filterstate_prep(&state, NULL, NULL, NULL, 0);
        if (attrpath_len != 0) { /* 0 = no NLRI information in this message */
                /* parse path attributes */
                while (len > 0) {
@@ -1445,8 +1470,8 @@ rde_update_update(struct rde_peer *peer,
        for (i = RIB_LOC_START; i < rib_size; i++) {
                if (!rib_valid(i))
                        continue;
-               rde_filterstate_prep(&state, &in->aspath, in->nexthop,
-                   in->nhflags);
+               rde_filterstate_prep(&state, &in->aspath, &in->communities,
+                   in->nexthop, in->nhflags);
                /* input filter */
                action = rde_filter(ribs[i].in_rules, peer, p, &state);
 
@@ -1696,7 +1721,8 @@ bad_flags:
                if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
                    ATTR_PARTIAL))
                        goto bad_flags;
-               if (attr_len == 0 || attr_len % 4 != 0) {
+               if (community_add(&state->communities, flags, p,
+                   attr_len) == -1) {
                        /*
                         * mark update as bad and withdraw all routes as per
                         * RFC 7606
@@ -1706,12 +1732,14 @@ bad_flags:
                            "path invalidated and prefix withdrawn");
                        break;
                }
-               goto optattr;
+               plen += attr_len;
+               break;
        case ATTR_LARGE_COMMUNITIES:
                if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
                    ATTR_PARTIAL))
                        goto bad_flags;
-               if (attr_len == 0 || attr_len % 12 != 0) {
+               if (community_large_add(&state->communities, flags, p,
+                   attr_len) == -1) {
                        /*
                         * mark update as bad and withdraw all routes as per
                         * RFC 7606
@@ -1721,12 +1749,14 @@ bad_flags:
                            "path invalidated and prefix withdrawn");
                        break;
                }
-               goto optattr;
+               plen += attr_len;
+               break;
        case ATTR_EXT_COMMUNITIES:
                if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
                    ATTR_PARTIAL))
                        goto bad_flags;
-               if (attr_len == 0 || attr_len % 8 != 0) {
+               if (community_ext_add(&state->communities, flags, p,
+                   attr_len) == -1) {
                        /*
                         * mark update as bad and withdraw all routes as per
                         * RFC 7606
@@ -1736,7 +1766,8 @@ bad_flags:
                            "path invalidated and prefix withdrawn");
                        break;
                }
-               goto optattr;
+               plen += attr_len;
+               break;
        case ATTR_ORIGINATOR_ID:
                if (attr_len != 4)
                        goto bad_len;
@@ -1843,7 +1874,7 @@ bad_list:
 }
 
 int
-rde_attr_add(struct rde_aspath *a, u_char *p, u_int16_t len)
+rde_attr_add(struct filterstate *state, u_char *p, u_int16_t len)
 {
        u_int16_t        attr_len;
        u_int16_t        plen = 0;
@@ -1851,8 +1882,6 @@ rde_attr_add(struct rde_aspath *a, u_cha
        u_int8_t         type;
        u_int8_t         tmp8;
 
-       if (a == NULL)          /* no aspath, nothing to do */
-               return (0);
        if (len < 3)
                return (-1);
 
@@ -1872,7 +1901,18 @@ rde_attr_add(struct rde_aspath *a, u_cha
        if (len - plen < attr_len)
                return (-1);
 
-       if (attr_optadd(a, flags, type, p, attr_len) == -1)
+       switch (type) {
+       case ATTR_COMMUNITIES:
+               return community_add(&state->communities, flags, p, attr_len);
+       case ATTR_LARGE_COMMUNITIES:
+               return community_large_add(&state->communities, flags, p,
+                   attr_len);
+       case ATTR_EXT_COMMUNITIES:
+               return community_ext_add(&state->communities, flags, p,
+                   attr_len);
+       }
+
+       if (attr_optadd(&state->aspath, flags, type, p, attr_len) == -1)
                return (-1);
        return (0);
 }
@@ -2226,7 +2266,21 @@ rde_dump_rib_as(struct prefix *p, struct
                return;
        imsg_close(ibuf_se_ctl, wbuf);
 
-       if (flags & F_CTL_DETAIL)
+       if (flags & F_CTL_DETAIL) {
+               struct rde_community *comm = prefix_communities(p);
+               size_t len = comm->nentries * sizeof(struct community);
+               if (comm->nentries > 0) {
+                       if ((wbuf = imsg_create(ibuf_se_ctl,
+                           IMSG_CTL_SHOW_RIB_COMMUNITIES, 0, pid,
+                           len)) == NULL)
+                               return;
+                       if ((bp = ibuf_reserve(wbuf, len)) == NULL) {
+                               ibuf_free(wbuf);
+                               return;
+                       }
+                       memcpy(bp, comm->communities, len);
+                       imsg_close(ibuf_se_ctl, wbuf);
+               }
                for (l = 0; l < asp->others_len; l++) {
                        if ((a = asp->others[l]) == NULL)
                                break;
@@ -2245,6 +2299,7 @@ rde_dump_rib_as(struct prefix *p, struct
                        }
                        imsg_close(ibuf_se_ctl, wbuf);
                }
+       }
 }
 
 static int
@@ -2283,21 +2338,10 @@ rde_dump_filter(struct prefix *p, struct
        if (req->as.type != AS_UNDEF &&
            !aspath_match(asp->aspath, &req->as, 0))
                return;
-       switch (req->community.type) {
-       case COMMUNITY_TYPE_NONE:
-               break;
-       case COMMUNITY_TYPE_BASIC:
-               if (!community_match(asp, &req->community, NULL))
-                       return;
-               break;
-       case COMMUNITY_TYPE_LARGE:
-               if (!community_large_match(asp, &req->community, NULL))
-                       return;
-               break;
-       case COMMUNITY_TYPE_EXT:
-               if (!community_ext_match(asp, &req->community, 0))
+       if (req->community.flags != 0) {
+               if (!community_match(prefix_communities(p), &req->community,
+                   NULL))
                        return;
-               break;
        }
        if (!ovs_match(p, req->flags))
                return;
@@ -2526,12 +2570,12 @@ rde_dump_mrt_new(struct mrt *mrt, pid_t 
  * kroute specific functions
  */
 int
-rde_l3vpn_import(struct rde_aspath *asp, struct l3vpn *rd)
+rde_l3vpn_import(struct rde_community *comm, struct l3vpn *rd)
 {
        struct filter_set       *s;
 
        TAILQ_FOREACH(s, &rd->import, entry) {
-               if (community_ext_match(asp, &s->action.community, 0))
+               if (community_match(comm, &s->action.community, 0))
                        return (1);
        }
        return (0);
@@ -2585,7 +2629,7 @@ rde_send_kroute(struct rib *rib, struct 
                        break;
 
                SIMPLEQ_FOREACH(vpn, l3vpns_l, entry) {
-                       if (!rde_l3vpn_import(asp, vpn))
+                       if (!rde_l3vpn_import(prefix_communities(p), vpn))
                                continue;
                        /* must send exit_nexthop so that correct MPLS tunnel
                         * is chosen
@@ -3178,8 +3222,8 @@ rde_softreconfig_in(struct rib_entry *re
                        if (rib->state != RECONF_RELOAD && !force_eval)
                                continue;
 
-                       rde_filterstate_prep(&state, asp, prefix_nexthop(p),
-                           prefix_nhflags(p));
+                       rde_filterstate_prep(&state, asp, prefix_communities(p),
+                           prefix_nexthop(p), prefix_nhflags(p));
                        action = rde_filter(rib->in_rules, peer, p, &state);
 
                        if (action == ACTION_ALLOW) {
@@ -3681,11 +3725,9 @@ peer_send_eor(struct rde_peer *peer, u_i
  * network announcement stuff
  */
 void
-network_add(struct network_config *nc, int flagstatic)
+network_add(struct network_config *nc, struct filterstate *state)
 {
-       struct filterstate       state;
        struct l3vpn            *vpn;
-       struct rde_aspath       *asp;
        struct filter_set_head  *vpnset = NULL;
        in_addr_t                prefix4;
        struct in6_addr          prefix6;
@@ -3747,40 +3789,25 @@ network_add(struct network_config *nc, i
                }
        }
 
-       if (nc->type == NETWORK_MRTCLONE) {
-               asp = nc->asp;
-       } else {
-               asp = path_get();
-               asp->aspath = aspath_get(NULL, 0);
-               asp->origin = ORIGIN_IGP;
-               asp->flags = F_ATTR_ORIGIN | F_ATTR_ASPATH |
-                   F_ATTR_LOCALPREF | F_PREFIX_ANNOUNCED;
-               /* the nexthop is unset unless a default set overrides it */
-       }
-       if (!flagstatic)
-               asp->flags |= F_ANN_DYNAMIC;
-       rde_filterstate_prep(&state, asp, NULL, 0); /* nexthop is not set */
-       rde_apply_set(&nc->attrset, &state, nc->prefix.aid, peerself, peerself);
+       rde_apply_set(&nc->attrset, state, nc->prefix.aid, peerself, peerself);
        if (vpnset)
-               rde_apply_set(vpnset, &state, nc->prefix.aid, peerself,
+               rde_apply_set(vpnset, state, nc->prefix.aid, peerself,
                    peerself);
 
        vstate = rde_roa_validity(&conf->rde_roa, &nc->prefix,
-           nc->prefixlen, aspath_origin(asp->aspath));
-       if (path_update(&ribs[RIB_ADJ_IN].rib, peerself, &state, &nc->prefix,
+           nc->prefixlen, aspath_origin(state->aspath.aspath));
+       if (path_update(&ribs[RIB_ADJ_IN].rib, peerself, state, &nc->prefix,
            nc->prefixlen, vstate) == 1)
                peerself->prefix_cnt++;
        for (i = RIB_LOC_START; i < rib_size; i++) {
                if (!rib_valid(i))
                        continue;
                rde_update_log("announce", i, peerself,
-                   state.nexthop ? &state.nexthop->exit_nexthop : NULL,
+                   state->nexthop ? &state->nexthop->exit_nexthop : NULL,
                    &nc->prefix, nc->prefixlen);
-               path_update(&ribs[i].rib, peerself, &state, &nc->prefix,
+               path_update(&ribs[i].rib, peerself, state, &nc->prefix,
                    nc->prefixlen, vstate);
        }
-       rde_filterstate_clean(&state);
-       path_put(asp);
        filterset_free(&nc->attrset);
 }
 
Index: usr.sbin/bgpd/rde.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.h,v
retrieving revision 1.212
diff -u -p -r1.212 rde.h
--- usr.sbin/bgpd/rde.h 31 May 2019 09:46:31 -0000      1.212
+++ usr.sbin/bgpd/rde.h 1 Jun 2019 09:12:41 -0000
@@ -174,6 +174,19 @@ struct mpattr {
        u_int16_t        unreach_len;
 };
 
+struct rde_community {
+       LIST_ENTRY(rde_community)       entry;
+       size_t                          size;
+       size_t                          nentries;
+       int                             flags;
+       int                             refcnt;
+       struct community                *communities;
+};
+
+#define        PARTIAL_COMMUNITIES             0x01
+#define        PARTIAL_LARGE_COMMUNITIES       0x02
+#define        PARTIAL_EXT_COMMUNITIES         0x04
+
 #define        F_ATTR_ORIGIN           0x00001
 #define        F_ATTR_ASPATH           0x00002
 #define        F_ATTR_NEXTHOP          0x00004
@@ -293,6 +306,7 @@ struct prefix {
        RB_ENTRY(prefix)                 entry;
        struct rib_entry                *re;
        struct rde_aspath               *aspath;
+       struct rde_community            *communities;
        struct rde_peer                 *peer;
        struct nexthop                  *nexthop;       /* may be NULL */
        time_t                           lastchange;
@@ -311,6 +325,7 @@ struct prefix {
 
 struct filterstate {
        struct rde_aspath        aspath;
+       struct rde_community     communities;
        struct nexthop          *nexthop;
        u_int8_t                 nhflags;
 };
@@ -375,34 +390,60 @@ u_char            *aspath_override(struct aspath *
                    u_int16_t *);
 int             aspath_lenmatch(struct aspath *, enum aslen_spec, u_int);
 
-int     community_match(struct rde_aspath *, struct filter_community *,
-           struct rde_peer *);
-int     community_set(struct rde_aspath *, struct filter_community *,
-           struct rde_peer *);
-void    community_delete(struct rde_aspath *, struct filter_community *,
+int     community_match(struct rde_community *, struct community *,
            struct rde_peer *);
-int     community_large_match(struct rde_aspath *, struct filter_community *,
+int     community_set(struct rde_community *, struct community *,
            struct rde_peer *);
-int     community_large_set(struct rde_aspath *, struct filter_community *,
+void    community_delete(struct rde_community *, struct community *,
            struct rde_peer *);
-void    community_large_delete(struct rde_aspath *, struct filter_community *,
-           struct rde_peer *);
-int     community_ext_match(struct rde_aspath *,
-           struct filter_community *, struct rde_peer *);
-int     community_ext_set(struct rde_aspath *,
-           struct filter_community *, struct rde_peer *);
-void    community_ext_delete(struct rde_aspath *,
-           struct filter_community *, struct rde_peer *);
-int     community_ext_conv(struct filter_community *, struct rde_peer *,
-           u_int64_t *, u_int64_t *);
-u_char *community_ext_delete_non_trans(u_char *, u_int16_t, u_int16_t *);
+
+int     community_add(struct rde_community *, int, void *, size_t);
+int     community_large_add(struct rde_community *, int, void *, size_t);
+int     community_ext_add(struct rde_community *, int, void *, size_t);
+
+int     community_write(struct rde_community *, void *, u_int16_t);
+int     community_large_write(struct rde_community *, void *, u_int16_t);
+int     community_ext_write(struct rde_community *, int, void *, u_int16_t);
+
+void                    communities_init(u_int32_t);
+void                    communities_shutdown(void);
+void                    communities_hash_stats(struct rde_hashstats *);
+struct rde_community   *communities_lookup(struct rde_community *);
+struct rde_community   *communities_link(struct rde_community *);
+void                    communities_unlink(struct rde_community *);
+
+int     communities_equal(struct rde_community *, struct rde_community *);
+void    communities_copy(struct rde_community *, struct rde_community *);
+void    communities_clean(struct rde_community *);
+
+static inline struct rde_community *
+communities_get(struct rde_community *comm)
+{
+       if (comm->refcnt == 0)
+               fatalx("%s: not-referenced community", __func__);
+       comm->refcnt++;
+       rdemem.comm_refs++;
+       return comm;
+}
+
+static inline void
+communities_put(struct rde_community *comm)
+{
+       if (comm == NULL)
+               return;
+       rdemem.comm_refs--;
+       if (--comm->refcnt == 1)
+               communities_unlink(comm);
+}
+
+int     community_to_rd(struct community *, u_int64_t *);
 
 /* rde_decide.c */
 void            prefix_evaluate(struct prefix *, struct rib_entry *);
 
 /* rde_filter.c */
 void            rde_filterstate_prep(struct filterstate *, struct rde_aspath *,
-                    struct nexthop *, u_int8_t);
+                    struct rde_community *, struct nexthop *, u_int8_t);
 void            rde_filterstate_clean(struct filterstate *);
 enum filter_actions rde_filter(struct filter_head *, struct rde_peer *,
                     struct prefix *, struct filterstate *);
@@ -524,6 +565,12 @@ static inline struct rde_aspath *
 prefix_aspath(struct prefix *p)
 {
        return (p->aspath);
+}
+
+static inline struct rde_community *
+prefix_communities(struct prefix *p)
+{
+       return (p->communities);
 }
 
 static inline struct nexthop *
Index: usr.sbin/bgpd/rde_attr.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_attr.c,v
retrieving revision 1.121
diff -u -p -r1.121 rde_attr.c
--- usr.sbin/bgpd/rde_attr.c    26 Feb 2019 12:46:08 -0000      1.121
+++ usr.sbin/bgpd/rde_attr.c    12 May 2019 15:25:13 -0000
@@ -18,15 +18,11 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/types.h>
 #include <sys/queue.h>
 
-#include <netinet/in.h>
-
 #include <endian.h>
 #include <limits.h>
 #include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <siphash.h>
 
@@ -61,6 +57,9 @@ attr_write(void *p, u_int16_t p_len, u_i
        } else
                *b++ = (u_char)data_len;
 
+       if (data == NULL)
+               return (tot_len - data_len);
+
        if (data_len != 0)
                memcpy(b, data, data_len);
 
@@ -129,11 +128,11 @@ attr_init(u_int32_t hashsize)
 void
 attr_shutdown(void)
 {
-       u_int32_t       i;
+       u_int64_t       i;
 
        for (i = 0; i <= attrtable.hashmask; i++)
                if (!LIST_EMPTY(&attrtable.hashtbl[i]))
-                       log_warnx("attr_shutdown: free non-free table");
+                       log_warnx("%s: free non-free table", __func__);
 
        free(attrtable.hashtbl);
 }
@@ -142,7 +141,7 @@ void
 attr_hash_stats(struct rde_hashstats *hs)
 {
        struct attr             *a;
-       u_int32_t               i;
+       u_int64_t               i;
        int64_t                 n;
 
        memset(hs, 0, sizeof(*hs));
@@ -1147,764 +1146,4 @@ aspath_lenmatch(struct aspath *a, enum a
                        fatalx("%s: would overflow", __func__);
        }
        return (0);
-}
-
-/*
- * Functions handling communities and extended communities.
- */
-
-static int
-community_extract(struct filter_community *fc, struct rde_peer *peer,
-     int field, u_int32_t *value)
-{
-       u_int32_t data;
-       u_int8_t flag;
-       switch (field) {
-       case 1:
-               flag = fc->dflag1;
-               switch (fc->type) {
-               case COMMUNITY_TYPE_BASIC:
-                       data = fc->c.b.data1;
-                       break;
-               case COMMUNITY_TYPE_LARGE:
-                       data = fc->c.l.data1;
-                       break;
-               case COMMUNITY_TYPE_EXT:
-                       data = fc->c.e.data1;
-                       break;
-               default:
-                       fatalx("%s: unknown type %d", __func__, fc->type);
-               }
-               break;
-       case 2:
-               flag = fc->dflag2;
-               switch (fc->type) {
-               case COMMUNITY_TYPE_BASIC:
-                       data = fc->c.b.data2;
-                       break;
-               case COMMUNITY_TYPE_LARGE:
-                       data = fc->c.l.data2;
-                       break;
-               case COMMUNITY_TYPE_EXT:
-                       data = fc->c.e.data2;
-                       break;
-               default:
-                       fatalx("%s: unknown type %d", __func__, fc->type);
-               }
-               break;
-       case 3:
-               if (fc->type != COMMUNITY_TYPE_LARGE)
-                       fatalx("%s: bad field %d", __func__, field);
-               flag = fc->dflag3;
-               data = fc->c.l.data3;
-               break;
-       default:
-               fatalx("%s: unknown field %d", __func__, field);
-       }
-
-       switch (flag) {
-       case COMMUNITY_NEIGHBOR_AS:
-               if (peer == NULL)
-                       return -1;
-               *value = peer->conf.remote_as;
-               break;
-       case COMMUNITY_LOCAL_AS:
-               if (peer == NULL)
-                       return -1;
-               *value = peer->conf.local_as;
-               break;
-       default:
-               *value = data;
-               break;
-       }
-       if (fc->type == COMMUNITY_TYPE_BASIC && *value > USHRT_MAX)
-               return -1;
-       return 0;
-}
-
-static int
-community_ext_matchone(struct filter_community *c, struct rde_peer *peer,
-    u_int64_t community)
-{
-       u_int32_t       val;
-       u_int64_t       com, mask;
-
-       community = be64toh(community);
-
-       if (c->dflag3 == COMMUNITY_ANY)
-               /* handle 'ext-community *', etc */
-               return (1);
-
-       /* special handling of ext-community rt * since type is not known */
-       if (c->dflag1 == COMMUNITY_ANY && c->c.e.type == -1) {
-               u_int8_t type = community >> 56;
-               if (type == EXT_COMMUNITY_TRANS_TWO_AS ||
-                   type == EXT_COMMUNITY_TRANS_FOUR_AS ||
-                   type == EXT_COMMUNITY_TRANS_IPV4)
-                       goto subtype;
-       }
-
-       com = (u_int64_t)c->c.e.type << 56;
-       mask = 0xffULL << 56;
-       if ((com & mask) != (community & mask))
-               return (0);
-
-       switch (c->c.e.type & EXT_COMMUNITY_VALUE) {
-       case EXT_COMMUNITY_TRANS_TWO_AS:
-       case EXT_COMMUNITY_TRANS_FOUR_AS:
-       case EXT_COMMUNITY_TRANS_IPV4:
-       case EXT_COMMUNITY_TRANS_OPAQUE:
-       case EXT_COMMUNITY_NON_TRANS_OPAQUE:
-subtype:
-               com = (u_int64_t)c->c.e.subtype << 48;
-               mask = 0xffULL << 48;
-               if ((com & mask) != (community & mask))
-                       return (0);
-               break;
-       default:
-               com = c->c.e.data2 & 0xffffffffffffffULL;
-               mask = 0xffffffffffffffULL;
-               if ((com & mask) == (community & mask))
-                       return (1);
-               return (0);
-       }
-
-       if (c->dflag1 == COMMUNITY_ANY)
-               /* handle 'ext-community rt *', etc */
-               return (1);
-
-       switch (c->c.e.type & EXT_COMMUNITY_VALUE) {
-       case EXT_COMMUNITY_TRANS_TWO_AS:
-               if (community_extract(c, peer, 1, &val) == -1)
-                       return (0);
-               com = (u_int64_t)val << 32;
-               mask = 0xffffULL << 32;
-               if ((com & mask) != (community & mask))
-                       return (0);
-
-               if (community_extract(c, peer, 2, &val) == -1)
-                       return (0);
-               com = val;
-               mask = 0xffffffffULL;
-               break;
-       case EXT_COMMUNITY_TRANS_IPV4:
-       case EXT_COMMUNITY_TRANS_FOUR_AS:
-               if (community_extract(c, peer, 1, &val) == -1)
-                       return (0);
-               com = (u_int64_t)val << 16;
-               mask = 0xffffffffULL << 16;
-               if ((com & mask) != (community & mask))
-                       return (0);
-
-               if (community_extract(c, peer, 2, &val) == -1)
-                       return (0);
-               com = val;
-               mask = 0xffff;
-               break;
-       case EXT_COMMUNITY_TRANS_OPAQUE:
-       case EXT_COMMUNITY_NON_TRANS_OPAQUE:
-               com = c->c.e.data2;
-               mask = EXT_COMMUNITY_OPAQUE_MAX;
-               break;
-       }
-
-       if (c->dflag2 == COMMUNITY_ANY ||
-           (com & mask) == (community & mask))
-               return (1);
-       return (0);
-}
-
-int
-community_match(struct rde_aspath *asp, struct filter_community *fc,
-    struct rde_peer *peer)
-{
-       struct attr     *a;
-       u_int8_t        *p;
-       u_int32_t        as, type, eas, etype;
-       u_int16_t        len;
-
-       a = attr_optget(asp, ATTR_COMMUNITIES);
-       if (a == NULL)
-               /* no communities, no match */
-               return (0);
-
-       if (community_extract(fc, peer, 1, &as) == -1 ||
-           community_extract(fc, peer, 2, &type) == -1)
-               /* can't match community */
-               return (0);
-
-       p = a->data;
-       for (len = a->len / 4; len > 0; len--) {
-               eas = *p++;
-               eas <<= 8;
-               eas |= *p++;
-               etype = *p++;
-               etype <<= 8;
-               etype |= *p++;
-               if ((fc->dflag1 == COMMUNITY_ANY || as == eas) &&
-                   (fc->dflag2 == COMMUNITY_ANY || type == etype))
-                       return (1);
-       }
-       return (0);
-}
-
-int
-community_set(struct rde_aspath *asp, struct filter_community *fc,
-    struct rde_peer *peer)
-{
-       struct attr     *attr;
-       u_int8_t        *p = NULL;
-       unsigned int     i, ncommunities = 0;
-       u_int32_t        as, type;
-       u_int8_t         f = ATTR_OPTIONAL|ATTR_TRANSITIVE;
-
-       if (fc->dflag1 == COMMUNITY_ANY || fc->dflag2 == COMMUNITY_ANY ||
-           community_extract(fc, peer, 1, &as) == -1 ||
-           community_extract(fc, peer, 2, &type) == -1)
-               /* bad community */
-               return (0);
-
-       attr = attr_optget(asp, ATTR_COMMUNITIES);
-       if (attr != NULL) {
-               p = attr->data;
-               ncommunities = attr->len / 4;
-       }
-
-       /* first check if the community is not already set */
-       for (i = 0; i < ncommunities; i++) {
-               if (as >> 8 == p[0] && (as & 0xff) == p[1] &&
-                   type >> 8 == p[2] && (type & 0xff) == p[3])
-                       /* already present, nothing todo */
-                       return (1);
-               p += 4;
-       }
-
-       if (ncommunities++ >= USHRT_MAX / 4)
-               /* overflow */
-               return (0);
-
-       if ((p = reallocarray(NULL, ncommunities, 4)) == NULL)
-               fatal("community_set");
-
-       p[0] = as >> 8;
-       p[1] = as & 0xff;
-       p[2] = type >> 8;
-       p[3] = type & 0xff;
-
-       if (attr != NULL) {
-               memcpy(p + 4, attr->data, attr->len);
-               f = attr->flags;
-               attr_free(asp, attr);
-       }
-
-       attr_optadd(asp, f, ATTR_COMMUNITIES, p, ncommunities * 4);
-
-       free(p);
-       return (1);
-}
-
-void
-community_delete(struct rde_aspath *asp, struct filter_community *fc,
-    struct rde_peer *peer)
-{
-       struct attr     *attr;
-       u_int8_t        *p, *n;
-       u_int16_t        l, len = 0;
-       u_int32_t        as, type, eas, etype;
-       u_int8_t         f;
-
-       attr = attr_optget(asp, ATTR_COMMUNITIES);
-       if (attr == NULL)
-               /* no attr nothing to do */
-               return;
-
-       if (community_extract(fc, peer, 1, &as) == -1 ||
-           community_extract(fc, peer, 2, &type) == -1)
-               /* bad community, nothing to do */
-               return;
-
-       p = attr->data;
-       for (l = 0; l < attr->len; l += 4) {
-               eas = *p++;
-               eas <<= 8;
-               eas |= *p++;
-               etype = *p++;
-               etype <<= 8;
-               etype |= *p++;
-
-               if ((fc->dflag1 == COMMUNITY_ANY || as == eas) &&
-                   (fc->dflag2 == COMMUNITY_ANY || type == etype))
-                       /* match */
-                       continue;
-               len += 4;
-       }
-
-       if (len == 0) {
-               attr_free(asp, attr);
-               return;
-       }
-
-       if ((n = malloc(len)) == NULL)
-               fatal("community_delete");
-
-       p = attr->data;
-       for (l = 0; l < len && p < attr->data + attr->len; ) {
-               eas = *p++;
-               eas <<= 8;
-               eas |= *p++;
-               etype = *p++;
-               etype <<= 8;
-               etype |= *p++;
-
-               if ((fc->dflag1 == COMMUNITY_ANY || as == eas) &&
-                   (fc->dflag2 == COMMUNITY_ANY || type == etype))
-                       /* match */
-                       continue;
-               n[l++] = eas >> 8;
-               n[l++] = eas & 0xff;
-               n[l++] = etype >> 8;
-               n[l++] = etype & 0xff;
-       }
-
-       f = attr->flags;
-
-       attr_free(asp, attr);
-       attr_optadd(asp, f, ATTR_COMMUNITIES, n, len);
-       free(n);
-}
-
-int
-community_ext_match(struct rde_aspath *asp, struct filter_community *c,
-    struct rde_peer *peer)
-{
-       struct attr     *attr;
-       u_int8_t        *p;
-       u_int64_t        ec;
-       u_int16_t        len;
-
-       attr = attr_optget(asp, ATTR_EXT_COMMUNITIES);
-       if (attr == NULL)
-               /* no communities, no match */
-               return (0);
-
-       p = attr->data;
-       for (len = attr->len / sizeof(ec); len > 0; len--) {
-               memcpy(&ec, p, sizeof(ec));
-               if (community_ext_matchone(c, peer, ec))
-                       return (1);
-               p += sizeof(ec);
-       }
-
-       return (0);
-}
-
-int
-community_ext_set(struct rde_aspath *asp, struct filter_community *c,
-    struct rde_peer *peer)
-{
-       struct attr     *attr;
-       u_int8_t        *p = NULL;
-       u_int64_t        community;
-       unsigned int     i, ncommunities = 0;
-       u_int8_t         f = ATTR_OPTIONAL|ATTR_TRANSITIVE;
-
-       if (community_ext_conv(c, peer, &community, NULL))
-               return (0);
-
-       attr = attr_optget(asp, ATTR_EXT_COMMUNITIES);
-       if (attr != NULL) {
-               p = attr->data;
-               ncommunities = attr->len / sizeof(community);
-       }
-
-       /* first check if the community is not already set */
-       for (i = 0; i < ncommunities; i++) {
-               if (memcmp(&community, p, sizeof(community)) == 0)
-                       /* already present, nothing todo */
-                       return (1);
-               p += sizeof(community);
-       }
-
-       if (ncommunities++ >= USHRT_MAX / sizeof(community))
-               /* overflow */
-               return (0);
-
-       if ((p = reallocarray(NULL, ncommunities, sizeof(community))) == NULL)
-               fatal("community_ext_set");
-
-       memcpy(p, &community, sizeof(community));
-       if (attr != NULL) {
-               memcpy(p + sizeof(community), attr->data, attr->len);
-               f = attr->flags;
-               attr_free(asp, attr);
-       }
-
-       attr_optadd(asp, f, ATTR_EXT_COMMUNITIES, p,
-           ncommunities * sizeof(community));
-
-       free(p);
-       return (1);
-}
-
-void
-community_ext_delete(struct rde_aspath *asp, struct filter_community *c,
-    struct rde_peer *peer)
-{
-       struct attr     *attr;
-       u_int8_t        *p, *n;
-       u_int64_t        community, mask, test;
-       u_int16_t        l, len = 0;
-       u_int8_t         f;
-       int              check_type = 0;
-
-       if (community_ext_conv(c, peer, &community, &mask))
-               return;
-       if (mask != 0 && be64toh(mask) >> 56 == 0)
-               check_type = 1;
-
-       attr = attr_optget(asp, ATTR_EXT_COMMUNITIES);
-       if (attr == NULL)
-               /* no attr nothing to do */
-               return;
-
-       p = attr->data;
-       for (l = 0; l < attr->len; l += sizeof(community)) {
-               memcpy(&test, p + l, sizeof(community));
-               /* special handling of ext-community rt *, type is not known */
-               if (check_type) {
-                       u_int8_t type = be64toh(test) >> 56;
-                       if (type != EXT_COMMUNITY_TRANS_TWO_AS &&
-                           type != EXT_COMMUNITY_TRANS_FOUR_AS &&
-                           type != EXT_COMMUNITY_TRANS_IPV4) {
-                               /* no match */
-                               len += sizeof(community);
-                               continue;
-                       }
-               }
-               if ((test & mask) == (community & mask))
-                       /* match */
-                       continue;
-               len += sizeof(community);
-       }
-
-       if (len == 0) {
-               attr_free(asp, attr);
-               return;
-       }
-
-       if ((n = malloc(len)) == NULL)
-               fatal("community_delete");
-
-       p = attr->data;
-       for (l = 0; l < len && p < attr->data + attr->len;
-           p += sizeof(community)) {
-               memcpy(&test, p, sizeof(community));
-               /* special handling of ext-community rt *, type is not known */
-               if (check_type) {
-                       u_int8_t type = be64toh(test) >> 56;
-                       if (type != EXT_COMMUNITY_TRANS_TWO_AS &&
-                           type != EXT_COMMUNITY_TRANS_FOUR_AS &&
-                           type != EXT_COMMUNITY_TRANS_IPV4) {
-                               /* no match */
-                               memcpy(n + l, p, sizeof(community));
-                               l += sizeof(community);
-                               continue;
-                       }
-               }
-               if ((test & mask) == (community & mask)) {
-                       /* match */
-                       continue;
-               }
-               memcpy(n + l, p, sizeof(community));
-               l += sizeof(community);
-       }
-
-       f = attr->flags;
-
-       attr_free(asp, attr);
-       attr_optadd(asp, f, ATTR_EXT_COMMUNITIES, n, len);
-       free(n);
-}
-
-int
-community_ext_conv(struct filter_community *c, struct rde_peer *peer,
-    u_int64_t *community, u_int64_t *mask)
-{
-       u_int64_t       com = 0, m = 0;
-       u_int32_t       val;
-
-       if (c->dflag3 == COMMUNITY_ANY) {
-               m = ~m;
-               goto done;
-       }
-
-       /* special handling of ext-community rt * since type is not known */
-       if (c->dflag1 == COMMUNITY_ANY && c->c.e.type == -1) {
-               m |= 0xffULL << 56;
-               goto subtype;
-       }
-       com = (u_int64_t)c->c.e.type << 56;
-       switch (c->c.e.type & EXT_COMMUNITY_VALUE) {
-       case EXT_COMMUNITY_TRANS_TWO_AS:
-       case EXT_COMMUNITY_TRANS_FOUR_AS:
-       case EXT_COMMUNITY_TRANS_IPV4:
-       case EXT_COMMUNITY_TRANS_OPAQUE:
-subtype:
-               com |= (u_int64_t)c->c.e.subtype << 48;
-               if (c->dflag1 == COMMUNITY_ANY) {
-                       m |= 0xffffffffffffULL;
-                       goto done;
-               }
-               break;
-       }
-
-       switch (c->c.e.type & EXT_COMMUNITY_VALUE) {
-       case EXT_COMMUNITY_TRANS_TWO_AS:
-               if (community_extract(c, peer, 1, &val) == -1)
-                       return (-1);
-               com |= (u_int64_t)val << 32;
-
-               if (c->dflag2 == COMMUNITY_ANY) {
-                       m |= 0xffffffffULL;
-                       goto done;
-               }
-               if (community_extract(c, peer, 2, &val) == -1)
-                       return (-1);
-               com |= (u_int64_t)val & 0xffffffffULL;
-               break;
-       case EXT_COMMUNITY_TRANS_IPV4:
-       case EXT_COMMUNITY_TRANS_FOUR_AS:
-               if (community_extract(c, peer, 1, &val) == -1)
-                       return (-1);
-               com |= (u_int64_t)val << 16;
-
-               if (c->dflag2 == COMMUNITY_ANY) {
-                       m |= 0xffffULL;
-                       goto done;
-               }
-               if (community_extract(c, peer, 2, &val) == -1)
-                       return (-1);
-               com |= (u_int64_t)val & 0xffffULL;
-               break;
-       case EXT_COMMUNITY_TRANS_OPAQUE:
-               com |= c->c.e.data2 & EXT_COMMUNITY_OPAQUE_MAX;
-               break;
-       default:
-               com |= c->c.e.data2 & 0xffffffffffffffULL;
-               break;
-       }
-
-done:
-       *community = htobe64(com);
-       if (mask)
-               *mask = htobe64(~m);
-       else if (m != 0)
-               return (-1);
-
-       return (0);
-}
-
-u_char *
-community_ext_delete_non_trans(u_char *data, u_int16_t len, u_int16_t *newlen)
-{
-       u_int8_t        *ext = data, *newdata;
-       u_int16_t       l, nlen = 0;
-
-       for (l = 0; l < len; l += sizeof(u_int64_t)) {
-               if (!(ext[l] & EXT_COMMUNITY_TRANSITIVE))
-                       nlen += sizeof(u_int64_t);
-       }
-
-       if (nlen == 0) {
-               *newlen = 0;
-               return NULL;
-       }
-
-       newdata = malloc(nlen);
-       if (newdata == NULL)
-               fatal("%s", __func__);
-
-       for (l = 0, nlen = 0; l < len; l += sizeof(u_int64_t)) {
-               if (!(ext[l] & EXT_COMMUNITY_TRANSITIVE)) {
-                       memcpy(newdata + nlen, ext + l, sizeof(u_int64_t));
-                       nlen += sizeof(u_int64_t);
-               }
-       }
-
-       *newlen = nlen;
-       return newdata;
-}
-
-struct wire_largecommunity {
-       uint32_t        as;
-       uint32_t        ld1;
-       uint32_t        ld2;
-};
-
-int
-community_large_match(struct rde_aspath *asp, struct filter_community *fc,
-    struct rde_peer *peer)
-{
-       struct wire_largecommunity      *wlc;
-       struct attr     *a;
-       u_int8_t        *p;
-       u_int16_t        len;
-       u_int32_t        as, ld1, ld2;
-
-       a = attr_optget(asp, ATTR_LARGE_COMMUNITIES);
-       if (a == NULL)
-               /* no communities, no match */
-               return (0);
-
-       if (community_extract(fc, peer, 1, &as) == -1 ||
-           community_extract(fc, peer, 2, &ld1) == -1 ||
-           community_extract(fc, peer, 3, &ld2) == -1)
-               /* can't match community */
-               return (0);
-       
-       as = htonl(as);
-       ld1 = htonl(ld1);
-       ld2 = htonl(ld2);
-
-       p = a->data;
-       for (len = a->len / 12; len > 0; len--) {
-               wlc = (struct wire_largecommunity *)p;
-               p += 12;
-
-               if ((fc->dflag1 == COMMUNITY_ANY || as == wlc->as) &&
-                   (fc->dflag2 == COMMUNITY_ANY || ld1 == wlc->ld1) &&
-                   (fc->dflag3 == COMMUNITY_ANY || ld2 == wlc->ld2))
-                       return (1);
-       }
-       return (0);
-}
-
-int
-community_large_set(struct rde_aspath *asp, struct filter_community *fc,
-    struct rde_peer *peer)
-{
-       struct wire_largecommunity      *wlc;
-       struct attr     *attr;
-       u_int8_t        *p = NULL;
-       unsigned int     i, ncommunities = 0;
-       u_int32_t        as, ld1, ld2;
-       u_int8_t         f = ATTR_OPTIONAL|ATTR_TRANSITIVE;
-
-       if (fc->dflag1 == COMMUNITY_ANY || fc->dflag2 == COMMUNITY_ANY ||
-           fc->dflag3 == COMMUNITY_ANY ||
-           community_extract(fc, peer, 1, &as) == -1 ||
-           community_extract(fc, peer, 2, &ld1) == -1 ||
-           community_extract(fc, peer, 3, &ld2) == -1)
-               /* can't match community */
-               return (0);
-
-       as = htonl(as);
-       ld1 = htonl(ld1);
-       ld2 = htonl(ld2);
-
-       attr = attr_optget(asp, ATTR_LARGE_COMMUNITIES);
-       if (attr != NULL) {
-               p = attr->data;
-               ncommunities = attr->len / 12;
-       }
-
-       /* first check if the community is not already set */
-       for (i = 0; i < ncommunities; i++) {
-               wlc = (struct wire_largecommunity *)p;
-               if (wlc->as == as && wlc->ld1 == ld1 && wlc->ld2 == ld2)
-                       /* already present, nothing todo */
-                       return (1);
-               p += 12;
-       }
-
-       if (ncommunities++ >= USHRT_MAX / 12)
-               /* overflow */
-               return (0);
-
-       if ((p = reallocarray(NULL, ncommunities, 12)) == NULL)
-               fatal("community_set");
-
-       wlc = (struct wire_largecommunity *)p;
-       wlc->as = as;
-       wlc->ld1 = ld1;
-       wlc->ld2 = ld2;
-
-       if (attr != NULL) {
-               memcpy(p + 12, attr->data, attr->len);
-               f = attr->flags;
-               attr_free(asp, attr);
-       }
-
-       attr_optadd(asp, f, ATTR_LARGE_COMMUNITIES, p, ncommunities * 12);
-
-       free(p);
-       return (1);
-}
-
-void
-community_large_delete(struct rde_aspath *asp, struct filter_community *fc,
-    struct rde_peer *peer)
-{
-       struct wire_largecommunity      *wlc;
-       struct attr     *attr;
-       u_int8_t        *p, *n;
-       u_int16_t        l = 0, len = 0;
-       u_int32_t        as, ld1, ld2;
-       u_int8_t         f;
-
-       attr = attr_optget(asp, ATTR_LARGE_COMMUNITIES);
-       if (attr == NULL)
-               /* no attr nothing to do */
-               return;
-
-       if (community_extract(fc, peer, 1, &as) == -1 ||
-           community_extract(fc, peer, 2, &ld1) == -1 ||
-           community_extract(fc, peer, 3, &ld2) == -1)
-               /* can't match community */
-               return;
-
-       as = htonl(as);
-       ld1 = htonl(ld1);
-       ld2 = htonl(ld2);
-
-       p = attr->data;
-       for (len = 0; l < attr->len; l += 12) {
-               wlc = (struct wire_largecommunity *)p;
-               p += 12;
-
-               if ((fc->dflag1 == COMMUNITY_ANY || as == wlc->as) &&
-                   (fc->dflag2 == COMMUNITY_ANY || ld1 == wlc->ld1) &&
-                   (fc->dflag3 == COMMUNITY_ANY || ld2 == wlc->ld2))
-                       /* match */
-                       continue;
-               len += 12;
-       }
-
-       if (len == 0) {
-               attr_free(asp, attr);
-               return;
-       }
-
-       if ((n = malloc(len)) == NULL)
-               fatal("community_delete");
-
-       p = attr->data;
-       for (l = 0; l < len && p < attr->data + attr->len; ) {
-               wlc = (struct wire_largecommunity *)p;
-               p += 12;
-
-               if ((fc->dflag1 == COMMUNITY_ANY || as == wlc->as) &&
-                   (fc->dflag2 == COMMUNITY_ANY || ld1 == wlc->ld1) &&
-                   (fc->dflag3 == COMMUNITY_ANY || ld2 == wlc->ld2))
-                       /* match */
-                       continue;
-               memcpy(n + l, wlc, sizeof(*wlc));
-               l += 12;
-       }
-
-       f = attr->flags;
-
-       attr_free(asp, attr);
-       attr_optadd(asp, f, ATTR_LARGE_COMMUNITIES, n, len);
-       free(n);
 }
Index: usr.sbin/bgpd/rde_community.c
===================================================================
RCS file: usr.sbin/bgpd/rde_community.c
diff -N usr.sbin/bgpd/rde_community.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ usr.sbin/bgpd/rde_community.c       27 May 2019 21:55:25 -0000
@@ -0,0 +1,827 @@
+/*     $OpenBSD$ */
+
+/*
+ * Copyright (c) 2019 Claudio Jeker <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include <sys/queue.h>
+
+#include <endian.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <siphash.h>
+
+#include "bgpd.h"
+#include "rde.h"
+#include "log.h"
+
+static int
+apply_flag(u_int32_t in, u_int8_t flag, struct rde_peer *peer, u_int32_t *out,
+    u_int32_t *mask)
+{
+       switch (flag) {
+       case COMMUNITY_ANY:
+               if (mask == NULL)
+                       return -1;
+               *out = 0;
+               *mask = 0;
+               return 0;
+       case COMMUNITY_NEIGHBOR_AS:
+               if (peer == NULL)
+                       return -1;
+               *out = peer->conf.remote_as;
+               break;
+       case COMMUNITY_LOCAL_AS:
+               if (peer == NULL)
+                       return -1;
+               *out = peer->conf.local_as;
+               break;
+       default:
+               *out = in;
+               break;
+       }
+       if (mask)
+               *mask = UINT32_MAX;
+       return 0;
+}
+
+static int
+fc2c(struct community *fc, struct rde_peer *peer, struct community *c,
+    struct community *m)
+{
+       short type;
+       u_int8_t subtype;
+
+       memset(c, 0, sizeof(*c));
+       if (m)
+               memset(m, 0xff, sizeof(*m));
+
+       c->flags = (u_int8_t)fc->flags;
+
+       switch ((u_int8_t)c->flags) {
+       case COMMUNITY_TYPE_BASIC:
+               if (apply_flag(fc->data1, fc->flags >> 8, peer,
+                   &c->data1, m ? &m->data1 : NULL))
+                       return -1;
+               if (apply_flag(fc->data2, fc->flags >> 16, peer,
+                   &c->data2, m ? &m->data2 : NULL))
+                       return -1;
+
+               /* check that values fit */
+               if (c->data1 > USHRT_MAX || c->data2 > USHRT_MAX)
+                       return -1;
+               return 0;
+       case COMMUNITY_TYPE_LARGE:
+               if (apply_flag(fc->data1, fc->flags >> 8, peer,
+                   &c->data1, m ? &m->data1 : NULL))
+                       return -1;
+               if (apply_flag(fc->data2, fc->flags >> 16, peer,
+                   &c->data2, m ? &m->data2 : NULL))
+                       return -1;
+               if (apply_flag(fc->data3, fc->flags >> 24, peer,
+                   &c->data3, m ? &m->data3 : NULL))
+                       return -1;
+               return 0;
+       case COMMUNITY_TYPE_EXT:
+               type = (int32_t)fc->data3 >> 8;
+               subtype = fc->data3 & 0xff;
+
+               c->data3 = type << 8 | subtype;
+               switch (type) {
+               case -1:
+                       /* special case for 'ext-community rt *' */
+                       if ((fc->flags >> 8 & 0xff) != COMMUNITY_ANY ||
+                           m == NULL)
+                               return -1;
+                       c->data3 = subtype;
+                       m->data1 = 0;
+                       m->data2 = 0;
+                       m->data3 = 0xff;
+                       return 0;
+               case EXT_COMMUNITY_TRANS_TWO_AS:
+                       if ((fc->flags >> 8 & 0xff) == COMMUNITY_ANY)
+                               break;
+
+                       if (apply_flag(fc->data1, fc->flags >> 8, peer,
+                           &c->data1, m ? &m->data1 : NULL))
+                               return -1;
+                       if (apply_flag(fc->data2, fc->flags >> 16, peer,
+                           &c->data2, m ? &m->data2 : NULL))
+                               return -1;
+                       /* check that values fit */
+                       if (c->data1 > USHRT_MAX)
+                               return -1;
+                       return 0;
+               case EXT_COMMUNITY_TRANS_FOUR_AS:
+               case EXT_COMMUNITY_TRANS_IPV4:
+                       if ((fc->flags >> 8 & 0xff) == COMMUNITY_ANY)
+                               break;
+
+                       if (apply_flag(fc->data1, fc->flags >> 8, peer,
+                           &c->data1, m ? &m->data1 : NULL))
+                               return -1;
+                       if (apply_flag(fc->data2, fc->flags >> 16, peer,
+                           &c->data2, m ? &m->data2 : NULL))
+                               return -1;
+                       /* check that values fit */
+                       if (c->data2 > USHRT_MAX)
+                               return -1;
+                       return 0;
+               case EXT_COMMUNITY_TRANS_OPAQUE:
+               case EXT_COMMUNITY_TRANS_EVPN:
+               case EXT_COMMUNITY_NON_TRANS_OPAQUE:
+                       if ((fc->flags >> 8 & 0xff) == COMMUNITY_ANY)
+                               break;
+
+                       c->data1 = fc->data1;
+                       c->data2 = fc->data2;
+                       return 0;;
+               }
+
+               if (m) {
+                       m->data1 = 0;
+                       m->data2 = 0;
+               }
+               return 0;
+       default:
+               fatalx("%s: unknown type %d", __func__, (u_int8_t)c->flags);
+       }
+}
+
+static int
+fast_match(const void *va, const void *vb)
+{
+       const struct community *a = va;
+       const struct community *b = vb;
+
+       if ((u_int8_t)a->flags != (u_int8_t)b->flags)
+               return (u_int8_t)a->flags > (u_int8_t)b->flags ? 1 : -1;
+
+       if (a->data1 != b->data1)
+               return a->data1 > b->data1 ? 1 : -1;
+       if (a->data2 != b->data2)
+               return a->data2 > b->data2 ? 1 : -1;
+       if (a->data3 != b->data3)
+               return a->data3 > b->data3 ? 1 : -1;
+       return 0;
+}
+
+static int
+mask_match(struct community *a, struct community *b, struct community *m)
+{
+       if ((u_int8_t)a->flags != (u_int8_t)b->flags)
+               return (u_int8_t)a->flags > (u_int8_t)b->flags ? 1 : -1;
+
+       if ((a->data1 & m->data1) != (b->data1 & m->data1)) {
+               if ((a->data1 & m->data1) > (b->data1 & m->data1))
+                       return 1;
+               return -1;
+       }
+       if ((a->data2 & m->data2) != (b->data2 & m->data2)) {
+               if ((a->data2 & m->data2) > (b->data2 & m->data2))
+                       return 1;
+               return -1;
+       }
+       if ((a->data3 & m->data3) != (b->data3 & m->data3)) {
+               if ((a->data3 & m->data3) > (b->data3 & m->data3))
+                       return 1;
+               return -1;
+       }
+       return 0;
+}
+
+/*
+ * Insert a community keeping the list sorted. Don't add if already present.
+ */
+static void
+insert_community(struct rde_community *comm, struct community *c)
+{
+       size_t l;
+       int r;
+
+       if (comm->nentries + 1 > comm->size) {
+               struct community *new;
+               size_t newsize = comm->size + 8;
+
+               if ((new = reallocarray(comm->communities, newsize,
+                   sizeof(struct community))) == NULL)
+                       fatal(__func__);
+               memset(new + comm->size, 0, 8 * sizeof(struct community));
+               comm->communities = new;
+               comm->size = newsize;
+       }
+
+       /* XXX can be made faster by binary search */
+       for (l = 0; l < comm->nentries; l++) {
+               r = fast_match(comm->communities + l, c);
+               if (r == 0) {
+                       /* already present, nothing to do */
+                       return;
+               } else if (r > 0) {
+                       /* shift reminder by one slot */
+                       memmove(comm->communities + l + 1,
+                           comm->communities + l,
+                           (comm->nentries - l) * sizeof(*c));
+                       break;
+               }
+       }
+
+       /* insert community at slot l */
+       comm->communities[l] = *c;
+       comm->nentries++;
+}
+
+static int
+non_transitive_community(struct community *c)
+{
+       if ((u_int8_t)c->flags == COMMUNITY_TYPE_EXT &&
+           !((ntohl(c->data1) >> 24) & EXT_COMMUNITY_NON_TRANSITIVE))
+               return 1;
+       return 0;
+}
+
+/*
+ * Check if a community is present. This function will expand local-as and
+ * neighbor-as and also mask of bits to support partial matches.
+ */
+int
+community_match(struct rde_community *comm, struct community *fc,
+struct rde_peer *peer)
+{
+       struct community test, mask;
+       size_t l;
+
+       if (fc->flags >> 8 == 0) {
+               /* fast path */
+               return (bsearch(fc, comm->communities, comm->nentries,
+                   sizeof(*fc), fast_match) != NULL);
+       } else {
+               /* slow path */
+
+               if (fc2c(fc, peer, &test, &mask) == -1)
+                       return 0;
+
+               for (l = 0; l < comm->nentries; l++) {
+                       if (mask_match(&comm->communities[l], &test, &mask) == 
0)
+                               return 1;
+               }
+               return 0;
+       }
+}
+
+/*
+ * Insert a community, expanding local-as and neighbor-as if needed.
+ */
+int
+community_set(struct rde_community *comm, struct community *fc,
+struct rde_peer *peer)
+{
+       struct community set;
+
+       if (fc->flags >> 8 == 0) {
+               /* fast path */
+               insert_community(comm, fc);
+       } else {
+               if (fc2c(fc, peer, &set, NULL) == -1)
+                       return 0;
+               insert_community(comm, &set);
+       }
+       return 1;
+}
+
+/*
+ * Remove a community if present, This function will expand local-as and
+ * neighbor-as and also mask of bits to support partial matches.
+ */
+void
+community_delete(struct rde_community *comm, struct community *fc,
+struct rde_peer *peer)
+{
+       struct community test, mask;
+       struct community *match;
+       size_t l = 0;
+
+       if (fc->flags >> 8 == 0) {
+               /* fast path */
+               match = bsearch(fc, comm->communities, comm->nentries,
+                   sizeof(*fc), fast_match);
+               if (match == NULL)
+                       return;
+               /* move everything after match down by 1 */
+               memmove(match, match + 1,
+                   (char *)(comm->communities + comm->nentries) -
+                   (char *)(match + 1));
+               comm->nentries--;
+               return;
+       } else {
+               if (fc2c(fc, peer, &test, &mask) == -1)
+                       return;
+
+               while (l < comm->nentries) {
+                       if (mask_match(&comm->communities[l], &test, &mask) == 
0) {
+                               memmove(comm->communities + l,
+                                   comm->communities + l + 1,
+                                   (comm->nentries - l - 1) * sizeof(test));
+                               comm->nentries--;
+                               continue;
+                       }
+                       l++;
+               }
+       }
+}
+
+/*
+ * Internalize communities from the wireformat.
+ * Store the partial flag in struct rde_community so it is not lost.
+ * - community_add for ATTR_COMMUNITUES
+ * - community_large_add for ATTR_LARGE_COMMUNITIES
+ * - community_ext_add for ATTR_EXT_COMMUNITIES
+ */
+int
+community_add(struct rde_community *comm, int flags, void *buf, size_t len)
+{
+       struct community set = { .flags = COMMUNITY_TYPE_BASIC };
+       u_int8_t *b = buf;
+       u_int16_t c;
+       size_t l;
+
+       if (len == 0 || len % 4 != 0)
+               return -1;
+
+       if (flags & ATTR_PARTIAL)
+               comm->flags |= PARTIAL_COMMUNITIES;
+
+       for (l = 0; l < len; l += 4, b += 4) {
+               memcpy(&c, b, sizeof(c));
+               set.data1 = ntohs(c);
+               memcpy(&c, b + 2, sizeof(c));
+               set.data2 = ntohs(c);
+               insert_community(comm, &set);
+       }
+
+       return 0;
+}
+
+int
+community_large_add(struct rde_community *comm, int flags, void *buf,
+    size_t len)
+{
+       struct community set = { .flags = COMMUNITY_TYPE_LARGE };
+       u_int8_t *b = buf;
+       size_t l;
+
+       if (len == 0 || len % 12 != 0)
+               return -1;
+
+       if (flags & ATTR_PARTIAL)
+               comm->flags |= PARTIAL_LARGE_COMMUNITIES;
+
+       for (l = 0; l < len; l += 12, b += 12) {
+               memcpy(&set.data1, b, sizeof(set.data1));
+               memcpy(&set.data2, b + 4, sizeof(set.data2));
+               memcpy(&set.data3, b + 8, sizeof(set.data3));
+               set.data1 = ntohl(set.data1);
+               set.data2 = ntohl(set.data2);
+               set.data3 = ntohl(set.data3);
+               insert_community(comm, &set);
+       }
+
+       return 0;
+}
+
+int
+community_ext_add(struct rde_community *comm, int flags, void *buf, size_t len)
+{
+       struct community set = { .flags = COMMUNITY_TYPE_EXT };
+       u_int8_t *b = buf, type;
+       u_int64_t c;
+       size_t l;
+
+       if (len == 0 || len % 8 != 0)
+               return -1;
+
+       if (flags & ATTR_PARTIAL)
+               comm->flags |= PARTIAL_EXT_COMMUNITIES;
+
+       for (l = 0; l < len; l += 8, b += 8) {
+               memcpy(&c, b, 8);
+
+               c = be64toh(c);
+               type = c >> 56;
+               switch (type) {
+               case EXT_COMMUNITY_TRANS_TWO_AS:
+               case EXT_COMMUNITY_TRANS_OPAQUE:
+               case EXT_COMMUNITY_TRANS_EVPN:
+               case EXT_COMMUNITY_NON_TRANS_OPAQUE:
+                       set.data1 = c >> 32 & 0xffff;
+                       set.data2 = c;
+                       break;
+               case EXT_COMMUNITY_TRANS_FOUR_AS:
+               case EXT_COMMUNITY_TRANS_IPV4:
+                       set.data1 = c >> 16;
+                       set.data2 = c & 0xffff;
+                       break;
+               }
+               set.data3 = c >> 48;
+
+               insert_community(comm, &set);
+       }
+
+       return 0;
+}
+
+/*
+ * Convert communities back to the wireformat.
+ * This function needs to make sure that the attribute buffer is overflowed
+ * while writing out the communities.
+ * - community_write for ATTR_COMMUNITUES
+ * - community_large_write for ATTR_LARGE_COMMUNITIES
+ * - community_ext_write for ATTR_EXT_COMMUNITIES
+ *   When writing ATTR_EXT_COMMUNITIES non-transitive communities need to
+ *   be skipped if it is sent to an ebgp peer.
+ */
+int
+community_write(struct rde_community *comm, void *buf, u_int16_t len)
+{
+       u_int8_t *b = buf;
+       u_int16_t c;
+       size_t l, n = 0;
+       int r, flags = ATTR_OPTIONAL | ATTR_TRANSITIVE;
+
+       if (comm->flags & PARTIAL_COMMUNITIES)
+               flags |= ATTR_PARTIAL;
+
+       /* first count how many communities will be written */
+       for (l = 0; l < comm->nentries; l++)
+               if ((u_int8_t)comm->communities[l].flags ==
+                   COMMUNITY_TYPE_BASIC)
+                       n++;
+
+       if (n == 0)
+               return 0;
+
+       /* write attribute header */
+       r = attr_write(b, len, flags, ATTR_COMMUNITIES, NULL, n * 4);
+       if (r == -1)
+               return -1;
+       b += r; 
+
+       /* write out the communities */
+       for (l = 0; l < comm->nentries; l++)
+               if ((u_int8_t)comm->communities[l].flags ==
+                   COMMUNITY_TYPE_BASIC) {
+                       c = htons(comm->communities[l].data1);
+                       memcpy(b, &c, sizeof(c));
+                       b += sizeof(c);
+                       r += sizeof(c);
+
+                       c = htons(comm->communities[l].data2);
+                       memcpy(b, &c, sizeof(c));
+                       b += sizeof(c);
+                       r += sizeof(c);
+               }
+
+       return r;
+}
+
+int
+community_large_write(struct rde_community *comm, void *buf, u_int16_t len)
+{
+       u_int8_t *b = buf;
+       u_int32_t c;
+       size_t l, n = 0;
+       int r, flags = ATTR_OPTIONAL | ATTR_TRANSITIVE;
+
+       if (comm->flags & PARTIAL_LARGE_COMMUNITIES)
+               flags |= ATTR_PARTIAL;
+
+       /* first count how many communities will be written */
+       for (l = 0; l < comm->nentries; l++)
+               if ((u_int8_t)comm->communities[l].flags ==
+                   COMMUNITY_TYPE_LARGE)
+                       n++;
+
+       if (n == 0)
+               return 0;
+
+       /* write attribute header */
+       r = attr_write(b, len, flags, ATTR_LARGE_COMMUNITIES, NULL, n * 12);
+       if (r == -1)
+               return -1;
+       b += r; 
+
+       /* write out the communities */
+       for (l = 0; l < comm->nentries; l++)
+               if ((u_int8_t)comm->communities[l].flags ==
+                   COMMUNITY_TYPE_LARGE) {
+                       c = htonl(comm->communities[l].data1);
+                       memcpy(b, &c, sizeof(c));
+                       b += sizeof(c);
+                       r += sizeof(c);
+
+                       c = htonl(comm->communities[l].data2);
+                       memcpy(b, &c, sizeof(c));
+                       b += sizeof(c);
+                       r += sizeof(c);
+
+                       c = htonl(comm->communities[l].data3);
+                       memcpy(b, &c, sizeof(c));
+                       b += sizeof(c);
+                       r += sizeof(c);
+               }
+
+       return r;
+}
+
+int
+community_ext_write(struct rde_community *comm, int ebgp, void *buf,
+    u_int16_t len)
+{
+       struct community *cp;
+       u_int8_t *b = buf;
+       u_int64_t ext;
+       size_t l, n = 0;
+       int r, flags = ATTR_OPTIONAL | ATTR_TRANSITIVE;
+
+       if (comm->flags & PARTIAL_EXT_COMMUNITIES)
+               flags |= ATTR_PARTIAL;
+
+       /* first count how many communities will be written */
+       for (l = 0; l < comm->nentries; l++)
+               if ((u_int8_t)comm->communities[l].flags ==
+                   COMMUNITY_TYPE_EXT && !(ebgp &&
+                   non_transitive_community(&comm->communities[l])))
+                       n++;
+
+       if (n == 0)
+               return 0;
+
+       /* write attribute header */
+       r = attr_write(b, len, flags, ATTR_EXT_COMMUNITIES, NULL, n * 8);
+       if (r == -1)
+               return -1;
+       b += r; 
+
+       /* write out the communities */
+       for (l = 0; l < comm->nentries; l++) {
+               cp = comm->communities + l;
+               if ((u_int8_t)cp->flags == COMMUNITY_TYPE_EXT && !(ebgp &&
+                   non_transitive_community(cp))) {
+                       ext = (u_int64_t)cp->data3 << 48;
+                       switch (cp->data3 >> 8) {
+                       case EXT_COMMUNITY_TRANS_TWO_AS:
+                       case EXT_COMMUNITY_TRANS_OPAQUE:
+                       case EXT_COMMUNITY_TRANS_EVPN:
+                       case EXT_COMMUNITY_NON_TRANS_OPAQUE:
+                               ext |= ((u_int64_t)cp->data1 & 0xffff) << 32;
+                               ext |= (u_int64_t)cp->data2;
+                               break;
+                       case EXT_COMMUNITY_TRANS_FOUR_AS:
+                       case EXT_COMMUNITY_TRANS_IPV4:
+                               ext |= (u_int64_t)cp->data1 << 16;
+                               ext |= (u_int64_t)cp->data2 & 0xffff;
+                               break;
+                       }
+                       ext = htobe64(ext);
+                       memcpy(b, &ext, sizeof(ext));
+                       b += sizeof(ext);
+                       r += sizeof(ext);
+               }
+       }
+
+       return r;
+}
+
+/*
+ * Global RIB cache for communities
+ */
+LIST_HEAD(commhead, rde_community);
+
+static struct comm_table {
+       struct commhead         *hashtbl;
+       u_int64_t                hashmask;
+} commtable;
+
+static SIPHASH_KEY commtablekey;
+
+static inline struct commhead *
+communities_hash(struct rde_community *comm)
+{
+       SIPHASH_CTX     ctx;
+       u_int64_t       hash;
+
+        SipHash24_Init(&ctx, &commtablekey);
+       SipHash24_Update(&ctx, &comm->nentries, sizeof(comm->nentries));
+       SipHash24_Update(&ctx, &comm->flags, sizeof(comm->flags));
+       if (comm->nentries > 0)
+               SipHash24_Update(&ctx, comm->communities,
+                   comm->nentries * sizeof(*comm->communities));
+       hash = SipHash24_End(&ctx);
+
+       return &commtable.hashtbl[hash & commtable.hashmask];
+}
+
+void
+communities_init(u_int32_t hashsize)
+{
+       u_int32_t       hs, i;
+
+       arc4random_buf(&commtablekey, sizeof(commtablekey));
+       for (hs = 1; hs < hashsize; hs <<= 1)
+               ;
+       commtable.hashtbl = calloc(hs, sizeof(*commtable.hashtbl));
+       if (commtable.hashtbl == NULL)
+               fatal(__func__);
+
+       for (i = 0; i < hs; i++)
+               LIST_INIT(&commtable.hashtbl[i]);
+       commtable.hashmask = hs - 1;
+}
+
+void
+communities_shutdown(void)
+{
+       u_int64_t       i;
+
+       for (i = 0; i <= commtable.hashmask; i++)
+               if (!LIST_EMPTY(&commtable.hashtbl[i]))
+                       log_warnx("%s: free non-free table", __func__);
+
+       free(commtable.hashtbl);
+}
+
+void
+communities_hash_stats(struct rde_hashstats *hs)
+{
+       struct rde_community *c;
+       u_int64_t i;
+       int64_t n;
+
+       memset(hs, 0, sizeof(*hs));
+       strlcpy(hs->name, "comm hash", sizeof(hs->name));
+       hs->min = LLONG_MAX;
+       hs->num = commtable.hashmask + 1;
+
+       for (i = 0; i <= commtable.hashmask; i++) {
+               n = 0;
+               LIST_FOREACH(c, &commtable.hashtbl[i], entry)
+                       n++;
+               if (n < hs->min)
+                       hs->min = n;
+               if (n > hs->max)
+                       hs->max = n;
+               hs->sum += n;
+               hs->sumq += n * n;
+       }
+}
+
+struct rde_community *
+communities_lookup(struct rde_community *comm)
+{
+       struct rde_community *c;
+       struct commhead *head;
+
+       head = communities_hash(comm);
+       LIST_FOREACH(c, head, entry) {
+               if (communities_equal(comm, c))
+                       return c;
+       }
+       return NULL;
+}
+
+struct rde_community *
+communities_link(struct rde_community *comm)
+{
+       struct rde_community *n;
+       struct commhead *head;
+
+       if ((n = malloc(sizeof(*n))) == NULL)
+               fatal(__func__);
+
+       communities_copy(n, comm);
+
+       head = communities_hash(n);
+       LIST_INSERT_HEAD(head, n, entry);
+       n->refcnt = 1;  /* initial reference by the cache */
+
+       rdemem.comm_size += n->size;
+       rdemem.comm_nmemb += n->nentries;
+       rdemem.comm_cnt++;
+
+       return n;
+}
+
+void
+communities_unlink(struct rde_community *comm)
+{
+       if (comm->refcnt != 1)
+               fatalx("%s: unlinking still referenced communities", __func__);
+
+       LIST_REMOVE(comm, entry);
+
+       rdemem.comm_size -= comm->size;
+       rdemem.comm_nmemb -= comm->nentries;
+       rdemem.comm_cnt--;
+
+       free(comm->communities);
+       free(comm);
+}
+
+/*
+ * Return true/1 if the two communities collections are identical,
+ * otherwise returns zero.
+ */
+int
+communities_equal(struct rde_community *a, struct rde_community *b)
+{
+       if (a->nentries != b->nentries)
+               return 0;
+       if (a->flags != b->flags)
+               return 0;
+
+       return (memcmp(a->communities, b->communities,
+           a->nentries * sizeof(struct community)) == 0);
+}
+
+/*
+ * Copy communities to a new unreferenced struct. Needs to call
+ * communities_clean() when done. to can be statically allocated,
+ * it will be cleaned first.
+ */
+void
+communities_copy(struct rde_community *to, struct rde_community *from)
+{
+       memset(to, 0, sizeof(*to));
+
+       /* ingore from->size and allocate the perfect amount */
+       to->size = from->size;
+       to->nentries = from->nentries;
+       to->flags = from->flags;
+
+       if ((to->communities = reallocarray(NULL, to->size,
+           sizeof(struct community))) == NULL)
+               fatal(__func__);
+       
+       memcpy(to->communities, from->communities,
+           to->nentries * sizeof(struct community));
+       memset(to->communities + to->nentries, 0, sizeof(struct community) *
+           (to->size - to->nentries));
+}
+
+/*
+ * Clean up the communities by freeing any dynamically allocated memory.
+ */
+void
+communities_clean(struct rde_community *comm)
+{
+       if (comm->refcnt != 0)
+               fatalx("%s: cleaning still referenced communities", __func__);
+
+       free(comm->communities);
+       memset(comm, 0, sizeof(*comm));
+}
+
+int
+community_to_rd(struct community *fc, u_int64_t *community)
+{
+       struct community c;
+       u_int64_t rd;
+
+       if (fc2c(fc, NULL, &c, NULL) == -1)
+               return -1;
+
+       switch (c.data3 >> 8) {
+       case EXT_COMMUNITY_TRANS_TWO_AS:
+               rd = (0ULL << 48);
+               rd |= ((u_int64_t)c.data1 & 0xffff) << 32;
+               rd |= (u_int64_t)c.data2;
+               break;
+       case EXT_COMMUNITY_TRANS_IPV4:
+               rd = (1ULL << 48);
+               rd |= (u_int64_t)c.data1 << 16;
+               rd |= (u_int64_t)c.data2 & 0xffff;
+               break;
+       case EXT_COMMUNITY_TRANS_FOUR_AS:
+               rd = (2ULL << 48);
+               rd |= (u_int64_t)c.data1 << 16;
+               rd |= (u_int64_t)c.data2 & 0xffff;
+               break;
+       default:
+               return -1;
+       }
+
+       *community = htobe64(rd);
+       return 0;
+}
Index: usr.sbin/bgpd/rde_filter.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_filter.c,v
retrieving revision 1.118
diff -u -p -r1.118 rde_filter.c
--- usr.sbin/bgpd/rde_filter.c  9 May 2019 22:27:33 -0000       1.118
+++ usr.sbin/bgpd/rde_filter.c  27 May 2019 20:48:50 -0000
@@ -146,35 +146,12 @@ rde_apply_set(struct filter_set_head *sh
                            &state->nexthop, &state->nhflags);
                        break;
                case ACTION_SET_COMMUNITY:
-                       switch (set->action.community.type) {
-                       case COMMUNITY_TYPE_BASIC:
-                               community_set(&state->aspath,
-                                   &set->action.community, peer);
-                               break;
-                       case COMMUNITY_TYPE_LARGE:
-                               community_large_set(&state->aspath,
-                                   &set->action.community, peer);
-                               break;
-                       case COMMUNITY_TYPE_EXT:
-                               community_ext_set(&state->aspath,
-                                   &set->action.community, peer);
-                               break;
-                       }
+                       community_set(&state->communities,
+                           &set->action.community, peer);
                        break;
                case ACTION_DEL_COMMUNITY:
-                       switch (set->action.community.type) {
-                       case COMMUNITY_TYPE_BASIC:
-                               community_delete(&state->aspath,
-                                   &set->action.community, peer);
-                               break;
-                       case COMMUNITY_TYPE_LARGE:
-                               community_large_delete(&state->aspath,
-                                   &set->action.community, peer);
-                               break;
-                       case COMMUNITY_TYPE_EXT:
-                               community_ext_delete(&state->aspath,
-                                   &set->action.community, peer);
-                       }
+                       community_delete(&state->communities,
+                           &set->action.community, peer);
                        break;
                case ACTION_PFTABLE:
                        /* convert pftable name to an id */
@@ -229,26 +206,12 @@ rde_filter_match(struct filter_rule *f, 
                    f->match.aslen.aslen) == 0)
                        return (0);
 
-       for (i = 0; asp != NULL && i < MAX_COMM_MATCH; i++) {
-               switch (f->match.community[i].type) {
-               case COMMUNITY_TYPE_NONE:
-                       i = MAX_COMM_MATCH;
+       for (i = 0; i < MAX_COMM_MATCH; i++) {
+               if (f->match.community[i].flags == 0)
                        break;
-               case COMMUNITY_TYPE_BASIC:
-                       if (community_match(asp, &f->match.community[i],
-                           peer) == 0)
-                               return (0);
-                       break;
-               case COMMUNITY_TYPE_LARGE:
-                       if (community_large_match(asp, &f->match.community[i],
-                           peer) == 0)
-                               return (0);
-                       break;
-               case COMMUNITY_TYPE_EXT:
-                       if (community_ext_match(asp, &f->match.community[i],
-                           peer) == 0)
-                               return (0);
-               }
+               if (community_match(&state->communities,
+                   &f->match.community[i], peer) == 0)
+                       return (0);
        }
 
        if (f->match.nexthop.flags != 0) {
@@ -463,13 +426,15 @@ rde_filter_equal(struct filter_head *a, 
 
 void
 rde_filterstate_prep(struct filterstate *state, struct rde_aspath *asp,
-    struct nexthop *nh, u_int8_t nhflags)
+    struct rde_community *communities, struct nexthop *nh, u_int8_t nhflags)
 {
        memset(state, 0, sizeof(*state));
 
        path_prep(&state->aspath);
        if (asp)
                path_copy(&state->aspath, asp);
+       if (communities)
+               communities_copy(&state->communities, communities);
        state->nexthop = nexthop_ref(nh);
        state->nhflags = nhflags;
 }
@@ -478,6 +443,7 @@ void
 rde_filterstate_clean(struct filterstate *state)
 {
        path_clean(&state->aspath);
+       communities_clean(&state->communities);
        nexthop_put(state->nexthop);
        state->nexthop = NULL;
 }
Index: usr.sbin/bgpd/rde_rib.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_rib.c,v
retrieving revision 1.190
diff -u -p -r1.190 rde_rib.c
--- usr.sbin/bgpd/rde_rib.c     7 Mar 2019 07:42:36 -0000       1.190
+++ usr.sbin/bgpd/rde_rib.c     27 May 2019 14:48:10 -0000
@@ -64,9 +64,11 @@ LIST_HEAD(, rib_context) rib_dumps = LIS
 
 static int      prefix_add(struct bgpd_addr *, int, struct rib *,
                    struct rde_peer *, struct rde_aspath *,
-                   struct filterstate *, u_int8_t);
+                   struct rde_community *, struct nexthop *,
+                   u_int8_t, u_int8_t);
 static int      prefix_move(struct prefix *, struct rde_peer *,
-                   struct rde_aspath *, struct filterstate *, u_int8_t);
+                   struct rde_aspath *, struct rde_community *,
+                   struct nexthop *, u_int8_t, u_int8_t);
 
 static inline struct rib_entry *
 re_lock(struct rib_entry *re)
@@ -598,6 +600,7 @@ path_update(struct rib *rib, struct rde_
     struct bgpd_addr *prefix, int prefixlen, u_int8_t vstate)
 {
        struct rde_aspath       *asp, *nasp = &state->aspath;
+       struct rde_community    *comm, *ncomm = &state->communities;
        struct prefix           *p;
 
        if (nasp->pftableid) {
@@ -609,9 +612,10 @@ path_update(struct rib *rib, struct rde_
         * First try to find a prefix in the specified RIB.
         */
        if ((p = prefix_get(rib, peer, prefix, prefixlen)) != NULL) {
-               if (path_compare(nasp, prefix_aspath(p)) == 0 &&
-                   prefix_nexthop(p) == state->nexthop &&
-                   prefix_nhflags(p) == state->nhflags) {
+               if (prefix_nexthop(p) == state->nexthop &&
+                   prefix_nhflags(p) == state->nhflags &&
+                   communities_equal(ncomm, prefix_communities(p)) &&
+                   path_compare(nasp, prefix_aspath(p)) == 0) {
                        /* no change, update last change */
                        p->lastchange = time(NULL);
                        p->validation_state = vstate;
@@ -639,12 +643,18 @@ path_update(struct rib *rib, struct rde_
                path_link(asp);
        }
 
+       if ((comm = communities_lookup(ncomm)) == NULL) {
+               /* Communities not available, create and link a new one. */
+               comm = communities_link(ncomm);
+       }
+
        /* If the prefix was found move it else add it to the aspath. */
        if (p != NULL)
-               return (prefix_move(p, peer, asp, state, vstate));
+               return (prefix_move(p, peer, asp, comm, state->nexthop,
+                   state->nhflags, vstate));
        else
-               return (prefix_add(prefix, prefixlen, rib, peer, asp,
-                   state, vstate));
+               return (prefix_add(prefix, prefixlen, rib, peer, asp, comm,
+                   state->nexthop, state->nhflags, vstate));
 }
 
 int
@@ -855,7 +865,8 @@ path_put(struct rde_aspath *asp)
 
 static void             prefix_link(struct prefix *, struct rib_entry *,
                             struct rde_peer *, struct rde_aspath *,
-                            struct filterstate *, u_int8_t);
+                            struct rde_community *, struct nexthop *,
+                            u_int8_t, u_int8_t);
 static void             prefix_unlink(struct prefix *);
 static struct prefix   *prefix_alloc(void);
 static void             prefix_free(struct prefix *);
@@ -868,6 +879,8 @@ prefix_cmp(struct prefix *a, struct pref
                return a->eor - b->eor;
        if (a->aspath != b->aspath)
                return (a->aspath > b->aspath ? 1 : -1);
+       if (a->communities != b->communities)
+               return (a->communities > b->communities ? 1 : -1);
        if (a->nexthop != b->nexthop)
                return (a->nexthop > b->nexthop ? 1 : -1);
        if (a->nhflags != b->nhflags)
@@ -897,8 +910,8 @@ prefix_get(struct rib *rib, struct rde_p
  */
 static int
 prefix_add(struct bgpd_addr *prefix, int prefixlen, struct rib *rib,
-    struct rde_peer *peer, struct rde_aspath *asp, struct filterstate *state,
-    u_int8_t vstate)
+    struct rde_peer *peer, struct rde_aspath *asp, struct rde_community *comm,
+    struct nexthop *nexthop, u_int8_t nhflags, u_int8_t vstate)
 {
        struct prefix           *p;
        struct rib_entry        *re;
@@ -910,14 +923,16 @@ prefix_add(struct bgpd_addr *prefix, int
        p = prefix_bypeer(re, peer);
        if (p == NULL) {
                p = prefix_alloc();
-               prefix_link(p, re, peer, asp, state, vstate);
+               prefix_link(p, re, peer, asp, comm, nexthop, nhflags, vstate);
                return (1);
        } else {
                if (prefix_aspath(p) != asp ||
-                   prefix_nexthop(p) != state->nexthop ||
-                   prefix_nhflags(p) != state->nhflags) {
+                   prefix_communities(p) != comm ||
+                   prefix_nexthop(p) != nexthop ||
+                   prefix_nhflags(p) != nhflags) {
                        /* prefix metadata changed therefor move */
-                       return (prefix_move(p, peer, asp, state, vstate));
+                       return (prefix_move(p, peer, asp, comm, nexthop,
+                           nhflags, vstate));
                }
                p->lastchange = time(NULL);
                p->validation_state = vstate;
@@ -930,7 +945,8 @@ prefix_add(struct bgpd_addr *prefix, int
  */
 static int
 prefix_move(struct prefix *p, struct rde_peer *peer,
-    struct rde_aspath *asp, struct filterstate *state, u_int8_t vstate)
+    struct rde_aspath *asp, struct rde_community *comm,
+    struct nexthop *nexthop, u_int8_t nhflags, u_int8_t vstate)
 {
        struct prefix           *np;
        struct rde_aspath       *oasp;
@@ -940,16 +956,18 @@ prefix_move(struct prefix *p, struct rde
 
        np = prefix_alloc();
        np->aspath = asp;
-       np->nexthop = nexthop_ref(state->nexthop);
+       np->communities = comm;
+       np->nexthop = nexthop_ref(nexthop);
        nexthop_link(np);
        np->peer = peer;
        np->re = p->re;
        np->lastchange = time(NULL);
-       np->nhflags = state->nhflags;
+       np->nhflags = nhflags;
        np->validation_state = vstate;
 
-       /* add reference to new as path */
+       /* add reference to new AS path and communities */
        path_ref(asp);
+       communities_get(comm);
 
        /*
         * no need to update the peer prefix count because we are only moving
@@ -975,6 +993,8 @@ prefix_move(struct prefix *p, struct rde
 
        /* destroy all references to other objects and free the old prefix */
        p->aspath = NULL;
+       communities_put(p->communities);
+       p->communities = NULL;
        p->peer = NULL;
        p->re = NULL;
        nexthop_unlink(p);
@@ -1076,6 +1096,10 @@ prefix_withdraw(struct rib *rib, struct 
                        path_unlink(asp);
        }
 
+       /* ... communities ... */
+       communities_put(p->communities);
+       p->communities = NULL;
+
        /* ... and nexthop but keep the re link */
        nexthop_unlink(p);
        nexthop_put(p->nexthop);
@@ -1278,17 +1302,19 @@ prefix_destroy(struct prefix *p)
  */
 static void
 prefix_link(struct prefix *p, struct rib_entry *re, struct rde_peer *peer,
-    struct rde_aspath *asp, struct filterstate *state, u_int8_t vstate)
+    struct rde_aspath *asp, struct rde_community *comm,
+    struct nexthop *nexthop, u_int8_t nhflags, u_int8_t vstate)
 {
        path_ref(asp);
 
        p->aspath = asp;
        p->peer = peer;
-       p->nexthop = nexthop_ref(state->nexthop);
+       p->communities = communities_get(comm);
+       p->nexthop = nexthop_ref(nexthop);
        nexthop_link(p);
        p->re = re;
        p->lastchange = time(NULL);
-       p->nhflags = state->nhflags;
+       p->nhflags = nhflags;
        p->validation_state = vstate;
 
        /* make route decision */
@@ -1317,8 +1343,10 @@ prefix_unlink(struct prefix *p)
                rib_remove(re);
 
        /* destroy all references to other objects */
+       communities_put(p->communities);
        nexthop_unlink(p);
        nexthop_put(p->nexthop);
+       p->communities = NULL;
        p->nexthop = NULL;
        p->aspath = NULL;
        p->peer = NULL;
Index: usr.sbin/bgpd/rde_update.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde_update.c,v
retrieving revision 1.113
diff -u -p -r1.113 rde_update.c
--- usr.sbin/bgpd/rde_update.c  7 Jun 2019 09:45:48 -0000       1.113
+++ usr.sbin/bgpd/rde_update.c  8 Jun 2019 07:31:53 -0000
@@ -28,20 +28,20 @@
 #include "rde.h"
 #include "log.h"
 
-static struct filter_community comm_no_advertise = {
-       .type = COMMUNITY_TYPE_BASIC,
-       .c.b.data1 = COMMUNITY_WELLKNOWN,
-       .c.b.data2 = COMMUNITY_NO_ADVERTISE
+static struct community        comm_no_advertise = {
+       .flags = COMMUNITY_TYPE_BASIC,
+       .data1 = COMMUNITY_WELLKNOWN,
+       .data2 = COMMUNITY_NO_ADVERTISE
 };
-static struct filter_community comm_no_export = {
-       .type = COMMUNITY_TYPE_BASIC,
-       .c.b.data1 = COMMUNITY_WELLKNOWN,
-       .c.b.data2 = COMMUNITY_NO_EXPORT
+static struct community        comm_no_export = {
+       .flags = COMMUNITY_TYPE_BASIC,
+       .data1 = COMMUNITY_WELLKNOWN,
+       .data2 = COMMUNITY_NO_EXPORT
 };
-static struct filter_community comm_no_expsubconfed = {
-       .type = COMMUNITY_TYPE_BASIC,
-       .c.b.data1 = COMMUNITY_WELLKNOWN,
-       .c.b.data2 = COMMUNITY_NO_EXPSUBCONFED
+static struct community        comm_no_expsubconfed = {
+       .flags = COMMUNITY_TYPE_BASIC,
+       .data1 = COMMUNITY_WELLKNOWN,
+       .data2 = COMMUNITY_NO_EXPSUBCONFED
 };
 
 static int
@@ -49,6 +49,7 @@ up_test_update(struct rde_peer *peer, st
 {
        struct bgpd_addr         addr;
        struct rde_aspath       *asp;
+       struct rde_community    *comm;
        struct rde_peer         *prefp;
        struct attr             *attr;
 
@@ -58,6 +59,7 @@ up_test_update(struct rde_peer *peer, st
 
        prefp = prefix_peer(p);
        asp = prefix_aspath(p);
+       comm = prefix_communities(p);
 
        if (peer == prefp)
                /* Do not send routes back to sender */
@@ -99,12 +101,12 @@ up_test_update(struct rde_peer *peer, st
        }
 
        /* well known communities */
-       if (community_match(asp, &comm_no_advertise, NULL))
+       if (community_match(comm, &comm_no_advertise, NULL))
                return (0);
        if (peer->conf.ebgp) {
-               if (community_match(asp, &comm_no_export, NULL))
+               if (community_match(comm, &comm_no_export, NULL))
                        return (0);
-               if (community_match(asp, &comm_no_expsubconfed, NULL))
+               if (community_match(comm, &comm_no_expsubconfed, NULL))
                        return (0);
        }
 
@@ -155,7 +157,8 @@ withdraw:
                }
 
                rde_filterstate_prep(&state, prefix_aspath(new),
-                   prefix_nexthop(new), prefix_nhflags(new));
+                   prefix_communities(new), prefix_nexthop(new),
+                   prefix_nhflags(new));
                if (rde_filter(rules, peer, new, &state) == ACTION_DENY) {
                        rde_filterstate_clean(&state);
                        goto withdraw;
@@ -192,7 +195,7 @@ up_generate_default(struct filter_head *
        if (peer->capa.mp[aid] == 0)
                return;
 
-       rde_filterstate_prep(&state, NULL, NULL, 0);
+       rde_filterstate_prep(&state, NULL, NULL, NULL, 0);
        asp = &state.aspath;
        asp->aspath = aspath_get(NULL, 0);
        asp->origin = ORIGIN_IGP;
@@ -323,6 +326,7 @@ up_generate_attr(u_char *buf, int len, s
     struct filterstate *state, u_int8_t aid)
 {
        struct rde_aspath *asp = &state->aspath;
+       struct rde_community *comm = &state->communities;
        struct attr     *oa = NULL, *newaggr = NULL;
        u_char          *pdata;
        u_int32_t        tmp32;
@@ -413,10 +417,21 @@ up_generate_attr(u_char *buf, int len, s
                        }
                        break;
                /*
-                * multiprotocol attributes are handled elsewhere
+                * Communities are stored in struct rde_community
                 */
-               case ATTR_MP_REACH_NLRI:
-               case ATTR_MP_UNREACH_NLRI:
+               case ATTR_COMMUNITIES:
+                       if ((r = community_write(comm, buf + wlen, len)) == -1)
+                               return (-1);
+                       break;
+               case ATTR_EXT_COMMUNITIES:
+                       if ((r = community_ext_write(comm, peer->conf.ebgp,
+                           buf + wlen, len)) == -1)
+                               return (-1);
+                       break;
+               case ATTR_LARGE_COMMUNITIES:
+                       if ((r = community_large_write(comm, buf + wlen,
+                           len)) == -1)
+                               return (-1);
                        break;
                /*
                 * NEW to OLD conversion when sending stuff to a 2byte AS peer
@@ -452,6 +467,10 @@ up_generate_attr(u_char *buf, int len, s
                                        return (-1);
                        }
                        break;
+               case ATTR_MP_REACH_NLRI:
+               case ATTR_MP_UNREACH_NLRI:
+                       /* specially handled later one */
+                       break;
                /*
                 * dump all other path attributes. Following rules apply:
                 *  1. well-known attrs: ATTR_ATOMIC_AGGREGATE and
@@ -501,10 +520,8 @@ up_generate_attr(u_char *buf, int len, s
                                break;
                        }
                        /* FALLTHROUGH */
-               case ATTR_COMMUNITIES:
                case ATTR_ORIGINATOR_ID:
                case ATTR_CLUSTER_LIST:
-               case ATTR_LARGE_COMMUNITIES:
                        if (oa == NULL || oa->type != type)
                                break;
                        if ((!(oa->flags & ATTR_TRANSITIVE)) &&
@@ -516,33 +533,6 @@ up_generate_attr(u_char *buf, int len, s
                            oa->flags, oa->type, oa->data, oa->len)) == -1)
                                return (-1);
                        break;
-               case ATTR_EXT_COMMUNITIES:
-                       if (oa == NULL || oa->type != type)
-                               break;
-                       /* handle (non-)transitive extended communities */
-                       if (peer->conf.ebgp) {
-                               u_char  *ndata;
-                               u_int16_t nlen;
-
-                               ndata = community_ext_delete_non_trans(oa->data,
-                                   oa->len, &nlen);
-
-                               if (nlen > 0) {
-                                       if ((r = attr_write(buf + wlen, len,
-                                           oa->flags, oa->type, ndata,
-                                           nlen)) == -1) {
-                                               free(ndata);
-                                               return (-1);
-                                       }
-                                       free(ndata);
-                               }
-                               /* else everything got removed */
-                       } else {
-                               if ((r = attr_write(buf + wlen, len, oa->flags,
-                                   oa->type, oa->data, oa->len)) == -1)
-                                       return (-1);
-                       }
-                       break;
                default:
                        if (oa == NULL && type >= ATTR_FIRST_UNKNOWN)
                                /* there is no attribute left to dump */
@@ -550,7 +540,6 @@ up_generate_attr(u_char *buf, int len, s
 
                        if (oa == NULL || oa->type != type)
                                break;
-
                        /* unknown attribute */
                        if (!(oa->flags & ATTR_TRANSITIVE)) {
                                /*
@@ -565,7 +554,6 @@ up_generate_attr(u_char *buf, int len, s
                            oa->flags | ATTR_PARTIAL, oa->type,
                            oa->data, oa->len)) == -1)
                                return (-1);
-                       break;
                }
                wlen += r;
                len -= r;
@@ -721,8 +709,8 @@ up_dump_attrnlri(u_char *buf, int len, s
        if (p == NULL)
                goto done;
 
-       rde_filterstate_prep(&state, prefix_aspath(p), prefix_nexthop(p),
-           prefix_nhflags(p));
+       rde_filterstate_prep(&state, prefix_aspath(p), prefix_communities(p),
+           prefix_nexthop(p), prefix_nhflags(p));
 
        r = up_generate_attr(buf + 2, len - 2, peer, &state, AID_INET);
        rde_filterstate_clean(&state);
@@ -860,8 +848,8 @@ up_dump_mp_reach(u_char *buf, int len, s
 
        wpos = 4;       /* reserve space for length fields */
 
-       rde_filterstate_prep(&state, prefix_aspath(p), prefix_nexthop(p),
-           prefix_nhflags(p));
+       rde_filterstate_prep(&state, prefix_aspath(p), prefix_communities(p),
+           prefix_nexthop(p), prefix_nhflags(p));
 
        /* write regular path attributes */
        r = up_generate_attr(buf + wpos, len - wpos, peer, &state, aid);
Index: usr.sbin/bgpd/session.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/session.c,v
retrieving revision 1.382
diff -u -p -r1.382 session.c
--- usr.sbin/bgpd/session.c     27 May 2019 09:14:33 -0000      1.382
+++ usr.sbin/bgpd/session.c     28 May 2019 13:29:25 -0000
@@ -2743,6 +2743,7 @@ session_dispatch_imsg(struct imsgbuf *ib
                        break;
                case IMSG_CTL_SHOW_RIB:
                case IMSG_CTL_SHOW_RIB_PREFIX:
+               case IMSG_CTL_SHOW_RIB_COMMUNITIES:
                case IMSG_CTL_SHOW_RIB_ATTR:
                case IMSG_CTL_SHOW_RIB_MEM:
                case IMSG_CTL_SHOW_RIB_HASH:

Reply via email to