diff options
author | 2018-06-29 11:45:50 +0000 | |
---|---|---|
committer | 2018-06-29 11:45:50 +0000 | |
commit | 5d2e648fc161b927eaa73281c44542710ce1ad0b (patch) | |
tree | 74e21365a90a6abc44b58c06d16a9b08621b54ed | |
parent | ACPI: Allow (with warning) GPE handler reassignment, instead of returning (diff) | |
download | wireguard-openbsd-5d2e648fc161b927eaa73281c44542710ce1ad0b.tar.xz wireguard-openbsd-5d2e648fc161b927eaa73281c44542710ce1ad0b.zip |
Prepare the ground for allowing temporary aspath object living on the stack.
To do this path_copy() gets a second argument (dst, src) and a new function
path_prep() is introduced to initialize an aspath object. The current
path_copy() usage is replaced with path_copy(path_get(), asp) which does
the same. Additionally some const where added to the *_copy functions to
make it more obvious which is the source and target. Also the pftable_ref()
and rtlabel_ref() functions return now the id instead of void.
OK sthen@
-rw-r--r-- | usr.sbin/bgpd/bgpd.h | 6 | ||||
-rw-r--r-- | usr.sbin/bgpd/name2id.c | 21 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde.h | 7 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_attr.c | 4 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_filter.c | 10 | ||||
-rw-r--r-- | usr.sbin/bgpd/rde_rib.c | 70 |
6 files changed, 62 insertions, 56 deletions
diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index 17108cbe0f4..01eee6b7498 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.319 2018/06/25 14:28:33 claudio Exp $ */ +/* $OpenBSD: bgpd.h,v 1.320 2018/06/29 11:45:50 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -1110,11 +1110,11 @@ void rib_ref(u_int16_t); u_int16_t rtlabel_name2id(const char *); const char *rtlabel_id2name(u_int16_t); void rtlabel_unref(u_int16_t); -void rtlabel_ref(u_int16_t); +u_int16_t rtlabel_ref(u_int16_t); u_int16_t pftable_name2id(const char *); const char *pftable_id2name(u_int16_t); void pftable_unref(u_int16_t); -void pftable_ref(u_int16_t); +u_int16_t pftable_ref(u_int16_t); /* parse.y */ int cmdline_symset(char *); diff --git a/usr.sbin/bgpd/name2id.c b/usr.sbin/bgpd/name2id.c index 2af133278f9..a8c77e04287 100644 --- a/usr.sbin/bgpd/name2id.c +++ b/usr.sbin/bgpd/name2id.c @@ -1,4 +1,4 @@ -/* $OpenBSD: name2id.c,v 1.9 2009/06/04 04:46:42 claudio Exp $ */ +/* $OpenBSD: name2id.c,v 1.10 2018/06/29 11:45:50 claudio Exp $ */ /* * Copyright (c) 2004, 2005 Henning Brauer <henning@openbsd.org> @@ -41,7 +41,7 @@ TAILQ_HEAD(n2id_labels, n2id_label); u_int16_t _name2id(struct n2id_labels *, const char *); const char *_id2name(struct n2id_labels *, u_int16_t); void _unref(struct n2id_labels *, u_int16_t); -void _ref(struct n2id_labels *, u_int16_t); +u_int16_t _ref(struct n2id_labels *, u_int16_t); struct n2id_labels rt_labels = TAILQ_HEAD_INITIALIZER(rt_labels); struct n2id_labels pftable_labels = TAILQ_HEAD_INITIALIZER(pftable_labels); @@ -64,10 +64,10 @@ rtlabel_unref(u_int16_t id) _unref(&rt_labels, id); } -void +u_int16_t rtlabel_ref(u_int16_t id) { - _ref(&rt_labels, id); + return (_ref(&rt_labels, id)); } u_int16_t @@ -88,10 +88,10 @@ pftable_unref(u_int16_t id) _unref(&pftable_labels, id); } -void +u_int16_t pftable_ref(u_int16_t id) { - _ref(&pftable_labels, id); + return (_ref(&pftable_labels, id)); } u_int16_t @@ -180,17 +180,20 @@ _unref(struct n2id_labels *head, u_int16_t id) } } -void +u_int16_t _ref(struct n2id_labels *head, u_int16_t id) { struct n2id_label *label; if (id == 0) - return; + return (0); TAILQ_FOREACH(label, head, entry) if (label->id == id) { ++label->ref; - break; + return (id); } + + /* id not found, treat like no id */ + return (0); } diff --git a/usr.sbin/bgpd/rde.h b/usr.sbin/bgpd/rde.h index ac8ad3553f7..4e79c174289 100644 --- a/usr.sbin/bgpd/rde.h +++ b/usr.sbin/bgpd/rde.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.h,v 1.174 2018/06/28 09:54:48 claudio Exp $ */ +/* $OpenBSD: rde.h,v 1.175 2018/06/29 11:45:50 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> and @@ -351,7 +351,7 @@ void attr_shutdown(void); int attr_optadd(struct rde_aspath *, u_int8_t, u_int8_t, void *, u_int16_t); struct attr *attr_optget(const struct rde_aspath *, u_int8_t); -void attr_copy(struct rde_aspath *, struct rde_aspath *); +void attr_copy(struct rde_aspath *, const struct rde_aspath *); int attr_compare(struct rde_aspath *, struct rde_aspath *); void attr_freeall(struct rde_aspath *); void attr_free(struct rde_aspath *, struct attr *); @@ -473,7 +473,8 @@ void path_remove(struct rde_aspath *); u_int32_t path_remove_stale(struct rde_aspath *, u_int8_t); void path_destroy(struct rde_aspath *); int path_empty(struct rde_aspath *); -struct rde_aspath *path_copy(struct rde_aspath *); +struct rde_aspath *path_copy(struct rde_aspath *, const struct rde_aspath *); +struct rde_aspath *path_prep(struct rde_aspath *); struct rde_aspath *path_get(void); void path_put(struct rde_aspath *); diff --git a/usr.sbin/bgpd/rde_attr.c b/usr.sbin/bgpd/rde_attr.c index f18e2d559a8..424aac1d8a4 100644 --- a/usr.sbin/bgpd/rde_attr.c +++ b/usr.sbin/bgpd/rde_attr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_attr.c,v 1.101 2018/04/02 19:25:33 claudio Exp $ */ +/* $OpenBSD: rde_attr.c,v 1.102 2018/06/29 11:45:50 claudio Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -209,7 +209,7 @@ attr_optget(const struct rde_aspath *asp, u_int8_t type) } void -attr_copy(struct rde_aspath *t, struct rde_aspath *s) +attr_copy(struct rde_aspath *t, const struct rde_aspath *s) { u_int8_t l; diff --git a/usr.sbin/bgpd/rde_filter.c b/usr.sbin/bgpd/rde_filter.c index c7675bda4ed..9ce19c08ae0 100644 --- a/usr.sbin/bgpd/rde_filter.c +++ b/usr.sbin/bgpd/rde_filter.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_filter.c,v 1.93 2018/06/28 09:54:48 claudio Exp $ */ +/* $OpenBSD: rde_filter.c,v 1.94 2018/06/29 11:45:50 claudio Exp $ */ /* * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org> @@ -305,8 +305,7 @@ rde_apply_set(struct filter_set_head *sh, struct rde_aspath *asp, /* FALLTHROUGH */ case ACTION_PFTABLE_ID: pftable_unref(asp->pftableid); - asp->pftableid = set->action.id; - pftable_ref(asp->pftableid); + asp->pftableid = pftable_ref(set->action.id); break; case ACTION_RTLABEL: /* convert the route label to an id for faster access */ @@ -315,8 +314,7 @@ rde_apply_set(struct filter_set_head *sh, struct rde_aspath *asp, /* FALLTHROUGH */ case ACTION_RTLABEL_ID: rtlabel_unref(asp->rtlabelid); - asp->rtlabelid = set->action.id; - rtlabel_ref(asp->rtlabelid); + asp->rtlabelid = rtlabel_ref(set->action.id); break; case ACTION_SET_ORIGIN: asp->origin = set->action.origin; @@ -1023,7 +1021,7 @@ rde_filter(struct filter_head *rules, struct rde_peer *peer, if (asp != NULL && new != NULL) { /* asp may get modified so create a copy */ if (*new == NULL) { - *new = path_copy(asp); + *new = path_copy(path_get(), asp); /* ... and use the copy from now on */ asp = *new; } diff --git a/usr.sbin/bgpd/rde_rib.c b/usr.sbin/bgpd/rde_rib.c index 6ba15e94998..94cb6287c34 100644 --- a/usr.sbin/bgpd/rde_rib.c +++ b/usr.sbin/bgpd/rde_rib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_rib.c,v 1.164 2018/06/28 08:55:56 claudio Exp $ */ +/* $OpenBSD: rde_rib.c,v 1.165 2018/06/29 11:45:50 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org> @@ -420,7 +420,7 @@ path_update(struct rib *rib, struct rde_peer *peer, struct rde_aspath *nasp, */ if ((asp = path_lookup(nasp, peer)) == NULL) { /* Path not available, create and link a new one. */ - asp = path_copy(nasp); + asp = path_copy(path_get(), nasp); path_link(asp, peer); } @@ -638,46 +638,36 @@ path_link(struct rde_aspath *asp, struct rde_peer *peer) } /* - * copy asp to a new UNLINKED one mainly for filtering + * Copy asp to a new UNLINKED aspath. + * On dst either path_get() or path_prep() had to be called before. */ struct rde_aspath * -path_copy(struct rde_aspath *asp) +path_copy(struct rde_aspath *dst, const struct rde_aspath *src) { - struct rde_aspath *nasp; - - nasp = path_get(); - nasp->aspath = asp->aspath; - if (nasp->aspath != NULL) { - nasp->aspath->refcnt++; + dst->aspath = src->aspath; + if (dst->aspath != NULL) { + dst->aspath->refcnt++; rdemem.aspath_refs++; } - nasp->nexthop = nexthop_ref(asp->nexthop); - nasp->med = asp->med; - nasp->lpref = asp->lpref; - nasp->weight = asp->weight; - nasp->origin = asp->origin; - nasp->rtlabelid = asp->rtlabelid; - rtlabel_ref(nasp->rtlabelid); - nasp->pftableid = asp->pftableid; - pftable_ref(nasp->pftableid); - - nasp->flags = asp->flags & ~(F_ATTR_LINKED | F_ATTR_UPDATE); - attr_copy(nasp, asp); - - return (nasp); + dst->nexthop = nexthop_ref(src->nexthop); + dst->med = src->med; + dst->lpref = src->lpref; + dst->weight = src->weight; + dst->origin = src->origin; + dst->rtlabelid = rtlabel_ref(src->rtlabelid); + dst->pftableid = pftable_ref(src->pftableid); + + dst->flags = src->flags & ~(F_ATTR_LINKED | F_ATTR_UPDATE); + attr_copy(dst, src); + + return (dst); } -/* alloc and initialize new entry. May not fail. */ +/* initialize or pepare an aspath for use */ struct rde_aspath * -path_get(void) +path_prep(struct rde_aspath *asp) { - struct rde_aspath *asp; - - asp = calloc(1, sizeof(*asp)); - if (asp == NULL) - fatal("path_alloc"); - rdemem.path_cnt++; - + memset(asp, 0, sizeof(*asp)); TAILQ_INIT(&asp->prefixes); TAILQ_INIT(&asp->updates); asp->origin = ORIGIN_INCOMPLETE; @@ -689,6 +679,20 @@ path_get(void) return (asp); } +/* alloc and initialize new entry. May not fail. */ +struct rde_aspath * +path_get(void) +{ + struct rde_aspath *asp; + + asp = malloc(sizeof(*asp)); + if (asp == NULL) + fatal("path_get"); + rdemem.path_cnt++; + + return (path_prep(asp)); +} + /* free an unlinked element */ void path_put(struct rde_aspath *asp) |