12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232 |
- #include <netlink-private/netlink.h>
- #include <netlink/netlink.h>
- #include <netlink/cache.h>
- #include <netlink/object.h>
- #include <netlink/hashtable.h>
- #include <netlink/utils.h>
- int nl_cache_nitems(struct nl_cache *cache)
- {
- return cache->c_nitems;
- }
- int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
- {
- struct nl_object *obj;
- int nitems = 0;
- if (cache->c_ops == NULL)
- BUG();
- nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
- if (filter && !nl_object_match_filter(obj, filter))
- continue;
- nitems++;
- }
- return nitems;
- }
- int nl_cache_is_empty(struct nl_cache *cache)
- {
- return nl_list_empty(&cache->c_items);
- }
- struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
- {
- return cache->c_ops;
- }
- struct nl_object *nl_cache_get_first(struct nl_cache *cache)
- {
- if (nl_list_empty(&cache->c_items))
- return NULL;
- return nl_list_entry(cache->c_items.next,
- struct nl_object, ce_list);
- }
- struct nl_object *nl_cache_get_last(struct nl_cache *cache)
- {
- if (nl_list_empty(&cache->c_items))
- return NULL;
- return nl_list_entry(cache->c_items.prev,
- struct nl_object, ce_list);
- }
- struct nl_object *nl_cache_get_next(struct nl_object *obj)
- {
- if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
- return NULL;
- else
- return nl_list_entry(obj->ce_list.next,
- struct nl_object, ce_list);
- }
- struct nl_object *nl_cache_get_prev(struct nl_object *obj)
- {
- if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
- return NULL;
- else
- return nl_list_entry(obj->ce_list.prev,
- struct nl_object, ce_list);
- }
- struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
- {
- struct nl_cache *cache;
- cache = calloc(1, sizeof(*cache));
- if (!cache)
- return NULL;
- nl_init_list_head(&cache->c_items);
- cache->c_ops = ops;
- cache->c_flags |= ops->co_flags;
- cache->c_refcnt = 1;
-
- if (ops->co_obj_ops->oo_keygen) {
- int hashtable_size;
- if (ops->co_hash_size)
- hashtable_size = ops->co_hash_size;
- else
- hashtable_size = NL_MAX_HASH_ENTRIES;
- cache->hashtable = nl_hash_table_alloc(hashtable_size);
- }
- NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
- return cache;
- }
- int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
- struct nl_cache **result)
- {
- struct nl_cache *cache;
- int err;
-
- if (!(cache = nl_cache_alloc(ops)))
- return -NLE_NOMEM;
- if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
- nl_cache_free(cache);
- return err;
- }
- *result = cache;
- return 0;
- }
- int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
- {
- struct nl_cache_ops *ops;
- struct nl_cache *cache;
- ops = nl_cache_ops_lookup_safe(kind);
- if (!ops)
- return -NLE_NOCACHE;
- cache = nl_cache_alloc(ops);
- nl_cache_ops_put(ops);
- if (!cache)
- return -NLE_NOMEM;
- *result = cache;
- return 0;
- }
- struct nl_cache *nl_cache_subset(struct nl_cache *orig,
- struct nl_object *filter)
- {
- struct nl_cache *cache;
- struct nl_object *obj;
- if (!filter)
- BUG();
- cache = nl_cache_alloc(orig->c_ops);
- if (!cache)
- return NULL;
- NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n",
- orig, nl_cache_name(orig), filter, cache);
- nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
- if (!nl_object_match_filter(obj, filter))
- continue;
- nl_cache_add(cache, obj);
- }
- return cache;
- }
- struct nl_cache *nl_cache_clone(struct nl_cache *cache)
- {
- struct nl_cache_ops *ops = nl_cache_get_ops(cache);
- struct nl_cache *clone;
- struct nl_object *obj;
- clone = nl_cache_alloc(ops);
- if (!clone)
- return NULL;
- NL_DBG(2, "Cloning %p into %p\n", cache, clone);
- nl_list_for_each_entry(obj, &cache->c_items, ce_list)
- nl_cache_add(clone, obj);
- return clone;
- }
- void nl_cache_clear(struct nl_cache *cache)
- {
- struct nl_object *obj, *tmp;
- NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
- nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
- nl_cache_remove(obj);
- }
- static void __nl_cache_free(struct nl_cache *cache)
- {
- nl_cache_clear(cache);
- if (cache->hashtable)
- nl_hash_table_free(cache->hashtable);
- NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
- free(cache);
- }
- void nl_cache_get(struct nl_cache *cache)
- {
- cache->c_refcnt++;
- NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n",
- cache, nl_cache_name(cache), cache->c_refcnt);
- }
- void nl_cache_free(struct nl_cache *cache)
- {
- if (!cache)
- return;
- cache->c_refcnt--;
- NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n",
- cache, nl_cache_name(cache), cache->c_refcnt);
- if (cache->c_refcnt <= 0)
- __nl_cache_free(cache);
- }
- void nl_cache_put(struct nl_cache *cache)
- {
- return nl_cache_free(cache);
- }
- static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
- {
- int ret;
- obj->ce_cache = cache;
- if (cache->hashtable) {
- ret = nl_hash_table_add(cache->hashtable, obj);
- if (ret < 0) {
- obj->ce_cache = NULL;
- return ret;
- }
- }
- nl_list_add_tail(&obj->ce_list, &cache->c_items);
- cache->c_nitems++;
- NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n",
- obj, cache, nl_cache_name(cache), cache->c_nitems);
- return 0;
- }
- int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
- {
- struct nl_object *new;
- int ret = 0;
- if (cache->c_ops->co_obj_ops != obj->ce_ops)
- return -NLE_OBJ_MISMATCH;
- if (!nl_list_empty(&obj->ce_list)) {
- NL_DBG(3, "Object %p already in cache, cloning new object\n", obj);
- new = nl_object_clone(obj);
- if (!new)
- return -NLE_NOMEM;
- } else {
- nl_object_get(obj);
- new = obj;
- }
- ret = __cache_add(cache, new);
- if (ret < 0)
- nl_object_put(new);
- return ret;
- }
- int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
- {
- if (cache->c_ops->co_obj_ops != obj->ce_ops)
- return -NLE_OBJ_MISMATCH;
- NL_DBG(3, "Moving object %p from cache %p to cache %p\n",
- obj, obj->ce_cache, cache);
-
-
- nl_object_get(obj);
- if (!nl_list_empty(&obj->ce_list))
- nl_cache_remove(obj);
- return __cache_add(cache, obj);
- }
- void nl_cache_remove(struct nl_object *obj)
- {
- int ret;
- struct nl_cache *cache = obj->ce_cache;
- if (cache == NULL)
- return;
- if (cache->hashtable) {
- ret = nl_hash_table_del(cache->hashtable, obj);
- if (ret < 0)
- NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n",
- obj, cache, nl_cache_name(cache));
- }
- nl_list_del(&obj->ce_list);
- obj->ce_cache = NULL;
- nl_object_put(obj);
- cache->c_nitems--;
- NL_DBG(2, "Deleted object %p from cache %p <%s>.\n",
- obj, cache, nl_cache_name(cache));
- }
- void nl_cache_set_arg1(struct nl_cache *cache, int arg)
- {
- cache->c_iarg1 = arg;
- }
- void nl_cache_set_arg2(struct nl_cache *cache, int arg)
- {
- cache->c_iarg2 = arg;
- }
- void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
- {
- cache->c_flags |= flags;
- }
- static int nl_cache_request_full_dump(struct nl_sock *sk,
- struct nl_cache *cache)
- {
- if (sk->s_proto != cache->c_ops->co_protocol)
- return -NLE_PROTO_MISMATCH;
- if (cache->c_ops->co_request_update == NULL)
- return -NLE_OPNOTSUPP;
- NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n",
- cache, nl_cache_name(cache));
- return cache->c_ops->co_request_update(cache, sk);
- }
- struct update_xdata {
- struct nl_cache_ops *ops;
- struct nl_parser_param *params;
- };
- static int update_msg_parser(struct nl_msg *msg, void *arg)
- {
- struct update_xdata *x = arg;
- int ret = 0;
- ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
- if (ret == -NLE_EXIST)
- return NL_SKIP;
- else
- return ret;
- }
- static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
- struct nl_parser_param *param)
- {
- int err;
- struct nl_cb *cb;
- struct update_xdata x = {
- .ops = cache->c_ops,
- .params = param,
- };
- NL_DBG(2, "Picking up answer for cache %p <%s>\n",
- cache, nl_cache_name(cache));
- cb = nl_cb_clone(sk->s_cb);
- if (cb == NULL)
- return -NLE_NOMEM;
- nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
- err = nl_recvmsgs(sk, cb);
- if (err < 0)
- NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned %d: %s\n",
- cache, nl_cache_name(cache), err, nl_geterror(err));
- nl_cb_put(cb);
- return err;
- }
- static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
- {
- struct nl_cache *cache = (struct nl_cache *)p->pp_arg;
- struct nl_object *old;
- old = nl_cache_search(cache, c);
- if (old) {
- if (nl_object_update(old, c) == 0) {
- nl_object_put(old);
- return 0;
- }
- nl_cache_remove(old);
- nl_object_put(old);
- }
- return nl_cache_add(cache, c);
- }
- int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
- {
- struct nl_parser_param p = {
- .pp_cb = pickup_cb,
- .pp_arg = cache,
- };
- if (sk->s_proto != cache->c_ops->co_protocol)
- return -NLE_PROTO_MISMATCH;
- return __cache_pickup(sk, cache, &p);
- }
- static int cache_include(struct nl_cache *cache, struct nl_object *obj,
- struct nl_msgtype *type, change_func_t cb, void *data)
- {
- struct nl_object *old;
- switch (type->mt_act) {
- case NL_ACT_NEW:
- case NL_ACT_DEL:
- old = nl_cache_search(cache, obj);
- if (old) {
-
- if (nl_object_update(old, obj) == 0) {
- if (cb)
- cb(cache, old, NL_ACT_CHANGE, data);
- nl_object_put(old);
- return 0;
- }
- nl_cache_remove(old);
- if (type->mt_act == NL_ACT_DEL) {
- if (cb)
- cb(cache, old, NL_ACT_DEL, data);
- nl_object_put(old);
- }
- }
- if (type->mt_act == NL_ACT_NEW) {
- nl_cache_move(cache, obj);
- if (old == NULL && cb)
- cb(cache, obj, NL_ACT_NEW, data);
- else if (old) {
- if (nl_object_diff(old, obj) && cb)
- cb(cache, obj, NL_ACT_CHANGE, data);
- nl_object_put(old);
- }
- }
- break;
- default:
- NL_DBG(2, "Unknown action associated to object %p\n", obj);
- return 0;
- }
- return 0;
- }
- int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
- change_func_t change_cb, void *data)
- {
- struct nl_cache_ops *ops = cache->c_ops;
- int i;
- if (ops->co_obj_ops != obj->ce_ops)
- return -NLE_OBJ_MISMATCH;
- for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
- if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
- return cache_include(cache, obj, &ops->co_msgtypes[i],
- change_cb, data);
- NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n",
- obj, cache, nl_cache_name(cache));
- return -NLE_MSGTYPE_NOSUPPORT;
- }
- static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
- {
- struct nl_cache_assoc *ca = p->pp_arg;
- return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
- }
- int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
- change_func_t change_cb, void *data)
- {
- struct nl_object *obj, *next;
- struct nl_af_group *grp;
- struct nl_cache_assoc ca = {
- .ca_cache = cache,
- .ca_change = change_cb,
- .ca_change_data = data,
- };
- struct nl_parser_param p = {
- .pp_cb = resync_cb,
- .pp_arg = &ca,
- };
- int err;
- if (sk->s_proto != cache->c_ops->co_protocol)
- return -NLE_PROTO_MISMATCH;
- NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
-
- nl_cache_mark_all(cache);
- grp = cache->c_ops->co_groups;
- do {
- if (grp && grp->ag_group &&
- (cache->c_flags & NL_CACHE_AF_ITER))
- nl_cache_set_arg1(cache, grp->ag_family);
- restart:
- err = nl_cache_request_full_dump(sk, cache);
- if (err < 0)
- goto errout;
- err = __cache_pickup(sk, cache, &p);
- if (err == -NLE_DUMP_INTR)
- goto restart;
- else if (err < 0)
- goto errout;
- if (grp)
- grp++;
- } while (grp && grp->ag_group &&
- (cache->c_flags & NL_CACHE_AF_ITER));
- nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
- if (nl_object_is_marked(obj)) {
- nl_object_get(obj);
- nl_cache_remove(obj);
- if (change_cb)
- change_cb(cache, obj, NL_ACT_DEL, data);
- nl_object_put(obj);
- }
- }
- NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
- err = 0;
- errout:
- return err;
- }
- int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
- struct nlmsghdr *nlh, struct nl_parser_param *params)
- {
- int i, err;
- if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
- return -NLE_MSG_TOOSHORT;
- for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
- if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
- err = ops->co_msg_parser(ops, who, nlh, params);
- if (err != -NLE_OPNOTSUPP)
- goto errout;
- }
- }
- err = -NLE_MSGTYPE_NOSUPPORT;
- errout:
- return err;
- }
- int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
- {
- struct nl_parser_param p = {
- .pp_cb = pickup_cb,
- .pp_arg = cache,
- };
- return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
- }
- int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
- {
- struct nl_af_group *grp;
- int err;
- if (sk->s_proto != cache->c_ops->co_protocol)
- return -NLE_PROTO_MISMATCH;
- nl_cache_clear(cache);
- grp = cache->c_ops->co_groups;
- do {
- if (grp && grp->ag_group &&
- (cache->c_flags & NL_CACHE_AF_ITER))
- nl_cache_set_arg1(cache, grp->ag_family);
- restart:
- err = nl_cache_request_full_dump(sk, cache);
- if (err < 0)
- return err;
- NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
- cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
- err = nl_cache_pickup(sk, cache);
- if (err == -NLE_DUMP_INTR) {
- NL_DBG(2, "Dump interrupted, restarting!\n");
- goto restart;
- } else if (err < 0)
- break;
- if (grp)
- grp++;
- } while (grp && grp->ag_group &&
- (cache->c_flags & NL_CACHE_AF_ITER));
- return err;
- }
- static struct nl_object *__cache_fast_lookup(struct nl_cache *cache,
- struct nl_object *needle)
- {
- struct nl_object *obj;
- obj = nl_hash_table_lookup(cache->hashtable, needle);
- if (obj) {
- nl_object_get(obj);
- return obj;
- }
- return NULL;
- }
- struct nl_object *nl_cache_search(struct nl_cache *cache,
- struct nl_object *needle)
- {
- struct nl_object *obj;
- if (cache->hashtable)
- return __cache_fast_lookup(cache, needle);
- nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
- if (nl_object_identical(obj, needle)) {
- nl_object_get(obj);
- return obj;
- }
- }
- return NULL;
- }
- struct nl_object *nl_cache_find(struct nl_cache *cache,
- struct nl_object *filter)
- {
- struct nl_object *obj;
- if (cache->c_ops == NULL)
- BUG();
- if ((nl_object_get_id_attrs(filter) == filter->ce_mask)
- && cache->hashtable)
- return __cache_fast_lookup(cache, filter);
- nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
- if (nl_object_match_filter(obj, filter)) {
- nl_object_get(obj);
- return obj;
- }
- }
- return NULL;
- }
- void nl_cache_mark_all(struct nl_cache *cache)
- {
- struct nl_object *obj;
- NL_DBG(2, "Marking all objects in cache %p <%s>\n",
- cache, nl_cache_name(cache));
- nl_list_for_each_entry(obj, &cache->c_items, ce_list)
- nl_object_mark(obj);
- }
- void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
- {
- nl_cache_dump_filter(cache, params, NULL);
- }
- void nl_cache_dump_filter(struct nl_cache *cache,
- struct nl_dump_params *params,
- struct nl_object *filter)
- {
- int type = params ? params->dp_type : NL_DUMP_DETAILS;
- struct nl_object_ops *ops;
- struct nl_object *obj;
- NL_DBG(2, "Dumping cache %p <%s> with filter %p\n",
- cache, nl_cache_name(cache), filter);
- if (type > NL_DUMP_MAX || type < 0)
- BUG();
- if (cache->c_ops == NULL)
- BUG();
- ops = cache->c_ops->co_obj_ops;
- if (!ops->oo_dump[type])
- return;
- if (params && params->dp_buf)
- memset(params->dp_buf, 0, params->dp_buflen);
- nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
- if (filter && !nl_object_match_filter(obj, filter))
- continue;
- NL_DBG(4, "Dumping object %p...\n", obj);
- dump_from_ops(obj, params);
- }
- }
- void nl_cache_foreach(struct nl_cache *cache,
- void (*cb)(struct nl_object *, void *), void *arg)
- {
- nl_cache_foreach_filter(cache, NULL, cb, arg);
- }
- void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
- void (*cb)(struct nl_object *, void *), void *arg)
- {
- struct nl_object *obj, *tmp;
- if (cache->c_ops == NULL)
- BUG();
- nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
- if (filter) {
- int diff = nl_object_match_filter(obj, filter);
- NL_DBG(3, "%p<->%p object difference: %x\n",
- obj, filter, diff);
- if (!diff)
- continue;
- }
-
- nl_object_get(obj);
- cb(obj, arg);
- nl_object_put(obj);
- }
- }
|