No need to duplicate the same block over and over again.
Just check it when we increment the counter and return failure if we hit
the limit.

OK?
-- 
:wq Claudio

Index: rde.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/rde.c,v
retrieving revision 1.384
diff -u -p -r1.384 rde.c
--- rde.c       5 Jul 2018 10:25:26 -0000       1.384
+++ rde.c       9 Jul 2018 12:47:16 -0000
@@ -51,7 +51,7 @@ void           rde_sighdlr(int);
 void            rde_dispatch_imsg_session(struct imsgbuf *);
 void            rde_dispatch_imsg_parent(struct imsgbuf *);
 int             rde_update_dispatch(struct imsg *);
-void            rde_update_update(struct rde_peer *, struct rde_aspath *,
+int             rde_update_update(struct rde_peer *, struct rde_aspath *,
                     struct bgpd_addr *, u_int8_t);
 void            rde_update_withdraw(struct rde_peer *, struct bgpd_addr *,
                     u_int8_t);
@@ -1208,18 +1208,8 @@ rde_update_dispatch(struct imsg *imsg)
                        goto done;
                }
 
-               rde_update_update(peer, asp, &prefix, prefixlen);
-
-               /* max prefix checker */
-               if (peer->conf.max_prefix &&
-                   peer->prefix_cnt > peer->conf.max_prefix) {
-                       log_peer_warnx(&peer->conf, "prefix limit reached"
-                           " (>%u/%u)", peer->prefix_cnt,
-                           peer->conf.max_prefix);
-                       rde_update_err(peer, ERR_CEASE, ERR_CEASE_MAX_PREFIX,
-                           NULL, 0);
+               if (rde_update_update(peer, asp, &prefix, prefixlen) == -1)
                        goto done;
-               }
 
        }
 
@@ -1289,21 +1279,9 @@ rde_update_dispatch(struct imsg *imsg)
                                mpp += pos;
                                mplen -= pos;
 
-                               rde_update_update(peer, asp, &prefix,
-                                   prefixlen);
-
-                               /* max prefix checker */
-                               if (peer->conf.max_prefix &&
-                                   peer->prefix_cnt > peer->conf.max_prefix) {
-                                       log_peer_warnx(&peer->conf,
-                                           "prefix limit reached"
-                                           " (>%u/%u)", peer->prefix_cnt,
-                                           peer->conf.max_prefix);
-                                       rde_update_err(peer, ERR_CEASE,
-                                           ERR_CEASE_MAX_PREFIX, NULL, 0);
+                               if (rde_update_update(peer, asp, &prefix,
+                                   prefixlen) == -1)
                                        goto done;
-                               }
-
                        }
                        break;
                case AID_VPN_IPv4:
@@ -1327,21 +1305,9 @@ rde_update_dispatch(struct imsg *imsg)
                                mpp += pos;
                                mplen -= pos;
 
-                               rde_update_update(peer, asp, &prefix,
-                                   prefixlen);
-
-                               /* max prefix checker */
-                               if (peer->conf.max_prefix &&
-                                   peer->prefix_cnt > peer->conf.max_prefix) {
-                                       log_peer_warnx(&peer->conf,
-                                           "prefix limit reached"
-                                           " (>%u/%u)", peer->prefix_cnt,
-                                           peer->conf.max_prefix);
-                                       rde_update_err(peer, ERR_CEASE,
-                                           ERR_CEASE_MAX_PREFIX, NULL, 0);
+                               if (rde_update_update(peer, asp, &prefix,
+                                   prefixlen) == -1)
                                        goto done;
-                               }
-
                        }
                        break;
                default:
@@ -1359,7 +1325,7 @@ done:
        return (error);
 }
 
-void
+int
 rde_update_update(struct rde_peer *peer, struct rde_aspath *asp,
     struct bgpd_addr *prefix, u_int8_t prefixlen)
 {
@@ -1373,6 +1339,14 @@ rde_update_update(struct rde_peer *peer,
        if (path_update(&ribs[RIB_ADJ_IN].rib, peer, asp, prefix, prefixlen, 0))
                peer->prefix_cnt++;
 
+       /* max prefix checker */
+       if (peer->conf.max_prefix && peer->prefix_cnt > peer->conf.max_prefix) {
+               log_peer_warnx(&peer->conf, "prefix limit reached (>%u/%u)",
+                   peer->prefix_cnt, peer->conf.max_prefix);
+               rde_update_err(peer, ERR_CEASE, ERR_CEASE_MAX_PREFIX, NULL, 0);
+               return (-1);
+       }
+
        p = prefix_get(&ribs[RIB_ADJ_IN].rib, peer, prefix, prefixlen, 0);
        if (p == NULL)
                fatalx("rde_update_update: no prefix in Adj-RIB-In");
@@ -1401,6 +1375,7 @@ rde_update_update(struct rde_peer *peer,
                if (fasp != asp)
                        path_put(fasp);
        }
+       return (0);
 }
 
 void

Reply via email to