12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435 |
- /*
- * net/tipc/node.c: TIPC node management routines
- *
- * Copyright (c) 2000-2006, 2012-2016, Ericsson AB
- * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the names of the copyright holders nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * Alternatively, this software may be distributed under the terms of the
- * GNU General Public License ("GPL") version 2 as published by the Free
- * Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- #include "core.h"
- #include "link.h"
- #include "node.h"
- #include "name_distr.h"
- #include "socket.h"
- #include "bcast.h"
- #include "monitor.h"
- #include "discover.h"
- #include "netlink.h"
- #define INVALID_NODE_SIG 0x10000
- #define NODE_CLEANUP_AFTER 300000
- /* Flags used to take different actions according to flag type
- * TIPC_NOTIFY_NODE_DOWN: notify node is down
- * TIPC_NOTIFY_NODE_UP: notify node is up
- * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
- */
- enum {
- TIPC_NOTIFY_NODE_DOWN = (1 << 3),
- TIPC_NOTIFY_NODE_UP = (1 << 4),
- TIPC_NOTIFY_LINK_UP = (1 << 6),
- TIPC_NOTIFY_LINK_DOWN = (1 << 7)
- };
- struct tipc_link_entry {
- struct tipc_link *link;
- spinlock_t lock; /* per link */
- u32 mtu;
- struct sk_buff_head inputq;
- struct tipc_media_addr maddr;
- };
- struct tipc_bclink_entry {
- struct tipc_link *link;
- struct sk_buff_head inputq1;
- struct sk_buff_head arrvq;
- struct sk_buff_head inputq2;
- struct sk_buff_head namedq;
- };
- /**
- * struct tipc_node - TIPC node structure
- * @addr: network address of node
- * @ref: reference counter to node object
- * @lock: rwlock governing access to structure
- * @net: the applicable net namespace
- * @hash: links to adjacent nodes in unsorted hash chain
- * @inputq: pointer to input queue containing messages for msg event
- * @namedq: pointer to name table input queue with name table messages
- * @active_links: bearer ids of active links, used as index into links[] array
- * @links: array containing references to all links to node
- * @action_flags: bit mask of different types of node actions
- * @state: connectivity state vs peer node
- * @sync_point: sequence number where synch/failover is finished
- * @list: links to adjacent nodes in sorted list of cluster's nodes
- * @working_links: number of working links to node (both active and standby)
- * @link_cnt: number of links to node
- * @capabilities: bitmap, indicating peer node's functional capabilities
- * @signature: node instance identifier
- * @link_id: local and remote bearer ids of changing link, if any
- * @publ_list: list of publications
- * @rcu: rcu struct for tipc_node
- * @delete_at: indicates the time for deleting a down node
- */
- struct tipc_node {
- u32 addr;
- struct kref kref;
- rwlock_t lock;
- struct net *net;
- struct hlist_node hash;
- int active_links[2];
- struct tipc_link_entry links[MAX_BEARERS];
- struct tipc_bclink_entry bc_entry;
- int action_flags;
- struct list_head list;
- int state;
- bool failover_sent;
- u16 sync_point;
- int link_cnt;
- u16 working_links;
- u16 capabilities;
- u32 signature;
- u32 link_id;
- u8 peer_id[16];
- struct list_head publ_list;
- struct list_head conn_sks;
- unsigned long keepalive_intv;
- struct timer_list timer;
- struct rcu_head rcu;
- unsigned long delete_at;
- };
- /* Node FSM states and events:
- */
- enum {
- SELF_DOWN_PEER_DOWN = 0xdd,
- SELF_UP_PEER_UP = 0xaa,
- SELF_DOWN_PEER_LEAVING = 0xd1,
- SELF_UP_PEER_COMING = 0xac,
- SELF_COMING_PEER_UP = 0xca,
- SELF_LEAVING_PEER_DOWN = 0x1d,
- NODE_FAILINGOVER = 0xf0,
- NODE_SYNCHING = 0xcc
- };
- enum {
- SELF_ESTABL_CONTACT_EVT = 0xece,
- SELF_LOST_CONTACT_EVT = 0x1ce,
- PEER_ESTABL_CONTACT_EVT = 0x9ece,
- PEER_LOST_CONTACT_EVT = 0x91ce,
- NODE_FAILOVER_BEGIN_EVT = 0xfbe,
- NODE_FAILOVER_END_EVT = 0xfee,
- NODE_SYNCH_BEGIN_EVT = 0xcbe,
- NODE_SYNCH_END_EVT = 0xcee
- };
- static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
- struct sk_buff_head *xmitq,
- struct tipc_media_addr **maddr);
- static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
- bool delete);
- static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
- static void tipc_node_delete(struct tipc_node *node);
- static void tipc_node_timeout(struct timer_list *t);
- static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
- static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
- static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
- static void tipc_node_put(struct tipc_node *node);
- static bool node_is_up(struct tipc_node *n);
- static void tipc_node_delete_from_list(struct tipc_node *node);
- struct tipc_sock_conn {
- u32 port;
- u32 peer_port;
- u32 peer_node;
- struct list_head list;
- };
- static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
- {
- int bearer_id = n->active_links[sel & 1];
- if (unlikely(bearer_id == INVALID_BEARER_ID))
- return NULL;
- return n->links[bearer_id].link;
- }
- int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
- {
- struct tipc_node *n;
- int bearer_id;
- unsigned int mtu = MAX_MSG_SIZE;
- n = tipc_node_find(net, addr);
- if (unlikely(!n))
- return mtu;
- bearer_id = n->active_links[sel & 1];
- if (likely(bearer_id != INVALID_BEARER_ID))
- mtu = n->links[bearer_id].mtu;
- tipc_node_put(n);
- return mtu;
- }
- bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
- {
- u8 *own_id = tipc_own_id(net);
- struct tipc_node *n;
- if (!own_id)
- return true;
- if (addr == tipc_own_addr(net)) {
- memcpy(id, own_id, TIPC_NODEID_LEN);
- return true;
- }
- n = tipc_node_find(net, addr);
- if (!n)
- return false;
- memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
- tipc_node_put(n);
- return true;
- }
- u16 tipc_node_get_capabilities(struct net *net, u32 addr)
- {
- struct tipc_node *n;
- u16 caps;
- n = tipc_node_find(net, addr);
- if (unlikely(!n))
- return TIPC_NODE_CAPABILITIES;
- caps = n->capabilities;
- tipc_node_put(n);
- return caps;
- }
- static void tipc_node_kref_release(struct kref *kref)
- {
- struct tipc_node *n = container_of(kref, struct tipc_node, kref);
- kfree(n->bc_entry.link);
- kfree_rcu(n, rcu);
- }
- static void tipc_node_put(struct tipc_node *node)
- {
- kref_put(&node->kref, tipc_node_kref_release);
- }
- static void tipc_node_get(struct tipc_node *node)
- {
- kref_get(&node->kref);
- }
- /*
- * tipc_node_find - locate specified node object, if it exists
- */
- static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
- {
- struct tipc_net *tn = tipc_net(net);
- struct tipc_node *node;
- unsigned int thash = tipc_hashfn(addr);
- rcu_read_lock();
- hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
- if (node->addr != addr)
- continue;
- if (!kref_get_unless_zero(&node->kref))
- node = NULL;
- break;
- }
- rcu_read_unlock();
- return node;
- }
- /* tipc_node_find_by_id - locate specified node object by its 128-bit id
- * Note: this function is called only when a discovery request failed
- * to find the node by its 32-bit id, and is not time critical
- */
- static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
- {
- struct tipc_net *tn = tipc_net(net);
- struct tipc_node *n;
- bool found = false;
- rcu_read_lock();
- list_for_each_entry_rcu(n, &tn->node_list, list) {
- read_lock_bh(&n->lock);
- if (!memcmp(id, n->peer_id, 16) &&
- kref_get_unless_zero(&n->kref))
- found = true;
- read_unlock_bh(&n->lock);
- if (found)
- break;
- }
- rcu_read_unlock();
- return found ? n : NULL;
- }
- static void tipc_node_read_lock(struct tipc_node *n)
- {
- read_lock_bh(&n->lock);
- }
- static void tipc_node_read_unlock(struct tipc_node *n)
- {
- read_unlock_bh(&n->lock);
- }
- static void tipc_node_write_lock(struct tipc_node *n)
- {
- write_lock_bh(&n->lock);
- }
- static void tipc_node_write_unlock_fast(struct tipc_node *n)
- {
- write_unlock_bh(&n->lock);
- }
- static void tipc_node_write_unlock(struct tipc_node *n)
- {
- struct net *net = n->net;
- u32 addr = 0;
- u32 flags = n->action_flags;
- u32 link_id = 0;
- u32 bearer_id;
- struct list_head *publ_list;
- if (likely(!flags)) {
- write_unlock_bh(&n->lock);
- return;
- }
- addr = n->addr;
- link_id = n->link_id;
- bearer_id = link_id & 0xffff;
- publ_list = &n->publ_list;
- n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
- TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
- write_unlock_bh(&n->lock);
- if (flags & TIPC_NOTIFY_NODE_DOWN)
- tipc_publ_notify(net, publ_list, addr);
- if (flags & TIPC_NOTIFY_NODE_UP)
- tipc_named_node_up(net, addr);
- if (flags & TIPC_NOTIFY_LINK_UP) {
- tipc_mon_peer_up(net, addr, bearer_id);
- tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
- TIPC_NODE_SCOPE, link_id, link_id);
- }
- if (flags & TIPC_NOTIFY_LINK_DOWN) {
- tipc_mon_peer_down(net, addr, bearer_id);
- tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
- addr, link_id);
- }
- }
- static struct tipc_node *tipc_node_create(struct net *net, u32 addr,
- u8 *peer_id, u16 capabilities)
- {
- struct tipc_net *tn = net_generic(net, tipc_net_id);
- struct tipc_node *n, *temp_node;
- struct tipc_link *l;
- int bearer_id;
- int i;
- spin_lock_bh(&tn->node_list_lock);
- n = tipc_node_find(net, addr);
- if (n) {
- if (n->capabilities == capabilities)
- goto exit;
- /* Same node may come back with new capabilities */
- write_lock_bh(&n->lock);
- n->capabilities = capabilities;
- for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
- l = n->links[bearer_id].link;
- if (l)
- tipc_link_update_caps(l, capabilities);
- }
- write_unlock_bh(&n->lock);
- goto exit;
- }
- n = kzalloc(sizeof(*n), GFP_ATOMIC);
- if (!n) {
- pr_warn("Node creation failed, no memory\n");
- goto exit;
- }
- n->addr = addr;
- memcpy(&n->peer_id, peer_id, 16);
- n->net = net;
- n->capabilities = capabilities;
- kref_init(&n->kref);
- rwlock_init(&n->lock);
- INIT_HLIST_NODE(&n->hash);
- INIT_LIST_HEAD(&n->list);
- INIT_LIST_HEAD(&n->publ_list);
- INIT_LIST_HEAD(&n->conn_sks);
- skb_queue_head_init(&n->bc_entry.namedq);
- skb_queue_head_init(&n->bc_entry.inputq1);
- __skb_queue_head_init(&n->bc_entry.arrvq);
- skb_queue_head_init(&n->bc_entry.inputq2);
- for (i = 0; i < MAX_BEARERS; i++)
- spin_lock_init(&n->links[i].lock);
- n->state = SELF_DOWN_PEER_LEAVING;
- n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
- n->signature = INVALID_NODE_SIG;
- n->active_links[0] = INVALID_BEARER_ID;
- n->active_links[1] = INVALID_BEARER_ID;
- if (!tipc_link_bc_create(net, tipc_own_addr(net),
- addr, U16_MAX,
- tipc_link_window(tipc_bc_sndlink(net)),
- n->capabilities,
- &n->bc_entry.inputq1,
- &n->bc_entry.namedq,
- tipc_bc_sndlink(net),
- &n->bc_entry.link)) {
- pr_warn("Broadcast rcv link creation failed, no memory\n");
- kfree(n);
- n = NULL;
- goto exit;
- }
- tipc_node_get(n);
- timer_setup(&n->timer, tipc_node_timeout, 0);
- n->keepalive_intv = U32_MAX;
- hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
- list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
- if (n->addr < temp_node->addr)
- break;
- }
- list_add_tail_rcu(&n->list, &temp_node->list);
- exit:
- spin_unlock_bh(&tn->node_list_lock);
- return n;
- }
- static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
- {
- unsigned long tol = tipc_link_tolerance(l);
- unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
- /* Link with lowest tolerance determines timer interval */
- if (intv < n->keepalive_intv)
- n->keepalive_intv = intv;
- /* Ensure link's abort limit corresponds to current tolerance */
- tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
- }
- static void tipc_node_delete_from_list(struct tipc_node *node)
- {
- list_del_rcu(&node->list);
- hlist_del_rcu(&node->hash);
- tipc_node_put(node);
- }
- static void tipc_node_delete(struct tipc_node *node)
- {
- tipc_node_delete_from_list(node);
- del_timer_sync(&node->timer);
- tipc_node_put(node);
- }
- void tipc_node_stop(struct net *net)
- {
- struct tipc_net *tn = tipc_net(net);
- struct tipc_node *node, *t_node;
- spin_lock_bh(&tn->node_list_lock);
- list_for_each_entry_safe(node, t_node, &tn->node_list, list)
- tipc_node_delete(node);
- spin_unlock_bh(&tn->node_list_lock);
- }
- void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
- {
- struct tipc_node *n;
- if (in_own_node(net, addr))
- return;
- n = tipc_node_find(net, addr);
- if (!n) {
- pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
- return;
- }
- tipc_node_write_lock(n);
- list_add_tail(subscr, &n->publ_list);
- tipc_node_write_unlock_fast(n);
- tipc_node_put(n);
- }
- void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
- {
- struct tipc_node *n;
- if (in_own_node(net, addr))
- return;
- n = tipc_node_find(net, addr);
- if (!n) {
- pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
- return;
- }
- tipc_node_write_lock(n);
- list_del_init(subscr);
- tipc_node_write_unlock_fast(n);
- tipc_node_put(n);
- }
- int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
- {
- struct tipc_node *node;
- struct tipc_sock_conn *conn;
- int err = 0;
- if (in_own_node(net, dnode))
- return 0;
- node = tipc_node_find(net, dnode);
- if (!node) {
- pr_warn("Connecting sock to node 0x%x failed\n", dnode);
- return -EHOSTUNREACH;
- }
- conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
- if (!conn) {
- err = -EHOSTUNREACH;
- goto exit;
- }
- conn->peer_node = dnode;
- conn->port = port;
- conn->peer_port = peer_port;
- tipc_node_write_lock(node);
- list_add_tail(&conn->list, &node->conn_sks);
- tipc_node_write_unlock(node);
- exit:
- tipc_node_put(node);
- return err;
- }
- void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
- {
- struct tipc_node *node;
- struct tipc_sock_conn *conn, *safe;
- if (in_own_node(net, dnode))
- return;
- node = tipc_node_find(net, dnode);
- if (!node)
- return;
- tipc_node_write_lock(node);
- list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
- if (port != conn->port)
- continue;
- list_del(&conn->list);
- kfree(conn);
- }
- tipc_node_write_unlock(node);
- tipc_node_put(node);
- }
- static void tipc_node_clear_links(struct tipc_node *node)
- {
- int i;
- for (i = 0; i < MAX_BEARERS; i++) {
- struct tipc_link_entry *le = &node->links[i];
- if (le->link) {
- kfree(le->link);
- le->link = NULL;
- node->link_cnt--;
- }
- }
- }
- /* tipc_node_cleanup - delete nodes that does not
- * have active links for NODE_CLEANUP_AFTER time
- */
- static bool tipc_node_cleanup(struct tipc_node *peer)
- {
- struct tipc_net *tn = tipc_net(peer->net);
- bool deleted = false;
- /* If lock held by tipc_node_stop() the node will be deleted anyway */
- if (!spin_trylock_bh(&tn->node_list_lock))
- return false;
- tipc_node_write_lock(peer);
- if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
- tipc_node_clear_links(peer);
- tipc_node_delete_from_list(peer);
- deleted = true;
- }
- tipc_node_write_unlock(peer);
- spin_unlock_bh(&tn->node_list_lock);
- return deleted;
- }
- /* tipc_node_timeout - handle expiration of node timer
- */
- static void tipc_node_timeout(struct timer_list *t)
- {
- struct tipc_node *n = from_timer(n, t, timer);
- struct tipc_link_entry *le;
- struct sk_buff_head xmitq;
- int remains = n->link_cnt;
- int bearer_id;
- int rc = 0;
- if (!node_is_up(n) && tipc_node_cleanup(n)) {
- /*Removing the reference of Timer*/
- tipc_node_put(n);
- return;
- }
- __skb_queue_head_init(&xmitq);
- /* Initial node interval to value larger (10 seconds), then it will be
- * recalculated with link lowest tolerance
- */
- tipc_node_read_lock(n);
- n->keepalive_intv = 10000;
- tipc_node_read_unlock(n);
- for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
- tipc_node_read_lock(n);
- le = &n->links[bearer_id];
- if (le->link) {
- spin_lock_bh(&le->lock);
- /* Link tolerance may change asynchronously: */
- tipc_node_calculate_timer(n, le->link);
- rc = tipc_link_timeout(le->link, &xmitq);
- spin_unlock_bh(&le->lock);
- remains--;
- }
- tipc_node_read_unlock(n);
- tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
- if (rc & TIPC_LINK_DOWN_EVT)
- tipc_node_link_down(n, bearer_id, false);
- }
- mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
- }
- /**
- * __tipc_node_link_up - handle addition of link
- * Node lock must be held by caller
- * Link becomes active (alone or shared) or standby, depending on its priority.
- */
- static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
- struct sk_buff_head *xmitq)
- {
- int *slot0 = &n->active_links[0];
- int *slot1 = &n->active_links[1];
- struct tipc_link *ol = node_active_link(n, 0);
- struct tipc_link *nl = n->links[bearer_id].link;
- if (!nl || tipc_link_is_up(nl))
- return;
- tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
- if (!tipc_link_is_up(nl))
- return;
- n->working_links++;
- n->action_flags |= TIPC_NOTIFY_LINK_UP;
- n->link_id = tipc_link_id(nl);
- /* Leave room for tunnel header when returning 'mtu' to users: */
- n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
- tipc_bearer_add_dest(n->net, bearer_id, n->addr);
- tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
- pr_debug("Established link <%s> on network plane %c\n",
- tipc_link_name(nl), tipc_link_plane(nl));
- /* Ensure that a STATE message goes first */
- tipc_link_build_state_msg(nl, xmitq);
- /* First link? => give it both slots */
- if (!ol) {
- *slot0 = bearer_id;
- *slot1 = bearer_id;
- tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
- n->failover_sent = false;
- n->action_flags |= TIPC_NOTIFY_NODE_UP;
- tipc_link_set_active(nl, true);
- tipc_bcast_add_peer(n->net, nl, xmitq);
- return;
- }
- /* Second link => redistribute slots */
- if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
- pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
- *slot0 = bearer_id;
- *slot1 = bearer_id;
- tipc_link_set_active(nl, true);
- tipc_link_set_active(ol, false);
- } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
- tipc_link_set_active(nl, true);
- *slot1 = bearer_id;
- } else {
- pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
- }
- /* Prepare synchronization with first link */
- tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
- }
- /**
- * tipc_node_link_up - handle addition of link
- *
- * Link becomes active (alone or shared) or standby, depending on its priority.
- */
- static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
- struct sk_buff_head *xmitq)
- {
- struct tipc_media_addr *maddr;
- tipc_node_write_lock(n);
- __tipc_node_link_up(n, bearer_id, xmitq);
- maddr = &n->links[bearer_id].maddr;
- tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
- tipc_node_write_unlock(n);
- }
- /**
- * __tipc_node_link_down - handle loss of link
- */
- static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
- struct sk_buff_head *xmitq,
- struct tipc_media_addr **maddr)
- {
- struct tipc_link_entry *le = &n->links[*bearer_id];
- int *slot0 = &n->active_links[0];
- int *slot1 = &n->active_links[1];
- int i, highest = 0, prio;
- struct tipc_link *l, *_l, *tnl;
- l = n->links[*bearer_id].link;
- if (!l || tipc_link_is_reset(l))
- return;
- n->working_links--;
- n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
- n->link_id = tipc_link_id(l);
- tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
- pr_debug("Lost link <%s> on network plane %c\n",
- tipc_link_name(l), tipc_link_plane(l));
- /* Select new active link if any available */
- *slot0 = INVALID_BEARER_ID;
- *slot1 = INVALID_BEARER_ID;
- for (i = 0; i < MAX_BEARERS; i++) {
- _l = n->links[i].link;
- if (!_l || !tipc_link_is_up(_l))
- continue;
- if (_l == l)
- continue;
- prio = tipc_link_prio(_l);
- if (prio < highest)
- continue;
- if (prio > highest) {
- highest = prio;
- *slot0 = i;
- *slot1 = i;
- continue;
- }
- *slot1 = i;
- }
- if (!node_is_up(n)) {
- if (tipc_link_peer_is_down(l))
- tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
- tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
- tipc_link_fsm_evt(l, LINK_RESET_EVT);
- tipc_link_reset(l);
- tipc_link_build_reset_msg(l, xmitq);
- *maddr = &n->links[*bearer_id].maddr;
- node_lost_contact(n, &le->inputq);
- tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
- return;
- }
- tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
- /* There is still a working link => initiate failover */
- *bearer_id = n->active_links[0];
- tnl = n->links[*bearer_id].link;
- tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
- tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
- n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
- tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
- tipc_link_reset(l);
- tipc_link_fsm_evt(l, LINK_RESET_EVT);
- tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
- tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
- *maddr = &n->links[*bearer_id].maddr;
- }
- static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
- {
- struct tipc_link_entry *le = &n->links[bearer_id];
- struct tipc_media_addr *maddr = NULL;
- struct tipc_link *l = le->link;
- int old_bearer_id = bearer_id;
- struct sk_buff_head xmitq;
- if (!l)
- return;
- __skb_queue_head_init(&xmitq);
- tipc_node_write_lock(n);
- if (!tipc_link_is_establishing(l)) {
- __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
- if (delete) {
- kfree(l);
- le->link = NULL;
- n->link_cnt--;
- }
- } else {
- /* Defuse pending tipc_node_link_up() */
- tipc_link_fsm_evt(l, LINK_RESET_EVT);
- }
- tipc_node_write_unlock(n);
- if (delete)
- tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
- if (!skb_queue_empty(&xmitq))
- tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
- tipc_sk_rcv(n->net, &le->inputq);
- }
- static bool node_is_up(struct tipc_node *n)
- {
- return n->active_links[0] != INVALID_BEARER_ID;
- }
- bool tipc_node_is_up(struct net *net, u32 addr)
- {
- struct tipc_node *n;
- bool retval = false;
- if (in_own_node(net, addr))
- return true;
- n = tipc_node_find(net, addr);
- if (!n)
- return false;
- retval = node_is_up(n);
- tipc_node_put(n);
- return retval;
- }
- static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
- {
- struct tipc_node *n;
- addr ^= tipc_net(net)->random;
- while ((n = tipc_node_find(net, addr))) {
- tipc_node_put(n);
- addr++;
- }
- return addr;
- }
- /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
- * Returns suggested address if any, otherwise 0
- */
- u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
- {
- struct tipc_net *tn = tipc_net(net);
- struct tipc_node *n;
- /* Suggest new address if some other peer is using this one */
- n = tipc_node_find(net, addr);
- if (n) {
- if (!memcmp(n->peer_id, id, NODE_ID_LEN))
- addr = 0;
- tipc_node_put(n);
- if (!addr)
- return 0;
- return tipc_node_suggest_addr(net, addr);
- }
- /* Suggest previously used address if peer is known */
- n = tipc_node_find_by_id(net, id);
- if (n) {
- addr = n->addr;
- tipc_node_put(n);
- return addr;
- }
- /* Even this node may be in conflict */
- if (tn->trial_addr == addr)
- return tipc_node_suggest_addr(net, addr);
- return 0;
- }
- void tipc_node_check_dest(struct net *net, u32 addr,
- u8 *peer_id, struct tipc_bearer *b,
- u16 capabilities, u32 signature,
- struct tipc_media_addr *maddr,
- bool *respond, bool *dupl_addr)
- {
- struct tipc_node *n;
- struct tipc_link *l;
- struct tipc_link_entry *le;
- bool addr_match = false;
- bool sign_match = false;
- bool link_up = false;
- bool accept_addr = false;
- bool reset = true;
- char *if_name;
- unsigned long intv;
- u16 session;
- *dupl_addr = false;
- *respond = false;
- n = tipc_node_create(net, addr, peer_id, capabilities);
- if (!n)
- return;
- tipc_node_write_lock(n);
- le = &n->links[b->identity];
- /* Prepare to validate requesting node's signature and media address */
- l = le->link;
- link_up = l && tipc_link_is_up(l);
- addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
- sign_match = (signature == n->signature);
- /* These three flags give us eight permutations: */
- if (sign_match && addr_match && link_up) {
- /* All is fine. Do nothing. */
- reset = false;
- } else if (sign_match && addr_match && !link_up) {
- /* Respond. The link will come up in due time */
- *respond = true;
- } else if (sign_match && !addr_match && link_up) {
- /* Peer has changed i/f address without rebooting.
- * If so, the link will reset soon, and the next
- * discovery will be accepted. So we can ignore it.
- * It may also be an cloned or malicious peer having
- * chosen the same node address and signature as an
- * existing one.
- * Ignore requests until the link goes down, if ever.
- */
- *dupl_addr = true;
- } else if (sign_match && !addr_match && !link_up) {
- /* Peer link has changed i/f address without rebooting.
- * It may also be a cloned or malicious peer; we can't
- * distinguish between the two.
- * The signature is correct, so we must accept.
- */
- accept_addr = true;
- *respond = true;
- } else if (!sign_match && addr_match && link_up) {
- /* Peer node rebooted. Two possibilities:
- * - Delayed re-discovery; this link endpoint has already
- * reset and re-established contact with the peer, before
- * receiving a discovery message from that node.
- * (The peer happened to receive one from this node first).
- * - The peer came back so fast that our side has not
- * discovered it yet. Probing from this side will soon
- * reset the link, since there can be no working link
- * endpoint at the peer end, and the link will re-establish.
- * Accept the signature, since it comes from a known peer.
- */
- n->signature = signature;
- } else if (!sign_match && addr_match && !link_up) {
- /* The peer node has rebooted.
- * Accept signature, since it is a known peer.
- */
- n->signature = signature;
- *respond = true;
- } else if (!sign_match && !addr_match && link_up) {
- /* Peer rebooted with new address, or a new/duplicate peer.
- * Ignore until the link goes down, if ever.
- */
- *dupl_addr = true;
- } else if (!sign_match && !addr_match && !link_up) {
- /* Peer rebooted with new address, or it is a new peer.
- * Accept signature and address.
- */
- n->signature = signature;
- accept_addr = true;
- *respond = true;
- }
- if (!accept_addr)
- goto exit;
- /* Now create new link if not already existing */
- if (!l) {
- if (n->link_cnt == 2)
- goto exit;
- if_name = strchr(b->name, ':') + 1;
- get_random_bytes(&session, sizeof(u16));
- if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
- b->net_plane, b->mtu, b->priority,
- b->window, session,
- tipc_own_addr(net), addr, peer_id,
- n->capabilities,
- tipc_bc_sndlink(n->net), n->bc_entry.link,
- &le->inputq,
- &n->bc_entry.namedq, &l)) {
- *respond = false;
- goto exit;
- }
- tipc_link_reset(l);
- tipc_link_fsm_evt(l, LINK_RESET_EVT);
- if (n->state == NODE_FAILINGOVER)
- tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
- le->link = l;
- n->link_cnt++;
- tipc_node_calculate_timer(n, l);
- if (n->link_cnt == 1) {
- intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
- if (!mod_timer(&n->timer, intv))
- tipc_node_get(n);
- }
- }
- memcpy(&le->maddr, maddr, sizeof(*maddr));
- exit:
- tipc_node_write_unlock(n);
- if (reset && l && !tipc_link_is_reset(l))
- tipc_node_link_down(n, b->identity, false);
- tipc_node_put(n);
- }
- void tipc_node_delete_links(struct net *net, int bearer_id)
- {
- struct tipc_net *tn = net_generic(net, tipc_net_id);
- struct tipc_node *n;
- rcu_read_lock();
- list_for_each_entry_rcu(n, &tn->node_list, list) {
- tipc_node_link_down(n, bearer_id, true);
- }
- rcu_read_unlock();
- }
- static void tipc_node_reset_links(struct tipc_node *n)
- {
- int i;
- pr_warn("Resetting all links to %x\n", n->addr);
- for (i = 0; i < MAX_BEARERS; i++) {
- tipc_node_link_down(n, i, false);
- }
- }
- /* tipc_node_fsm_evt - node finite state machine
- * Determines when contact is allowed with peer node
- */
- static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
- {
- int state = n->state;
- switch (state) {
- case SELF_DOWN_PEER_DOWN:
- switch (evt) {
- case SELF_ESTABL_CONTACT_EVT:
- state = SELF_UP_PEER_COMING;
- break;
- case PEER_ESTABL_CONTACT_EVT:
- state = SELF_COMING_PEER_UP;
- break;
- case SELF_LOST_CONTACT_EVT:
- case PEER_LOST_CONTACT_EVT:
- break;
- case NODE_SYNCH_END_EVT:
- case NODE_SYNCH_BEGIN_EVT:
- case NODE_FAILOVER_BEGIN_EVT:
- case NODE_FAILOVER_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- case SELF_UP_PEER_UP:
- switch (evt) {
- case SELF_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_LEAVING;
- break;
- case PEER_LOST_CONTACT_EVT:
- state = SELF_LEAVING_PEER_DOWN;
- break;
- case NODE_SYNCH_BEGIN_EVT:
- state = NODE_SYNCHING;
- break;
- case NODE_FAILOVER_BEGIN_EVT:
- state = NODE_FAILINGOVER;
- break;
- case SELF_ESTABL_CONTACT_EVT:
- case PEER_ESTABL_CONTACT_EVT:
- case NODE_SYNCH_END_EVT:
- case NODE_FAILOVER_END_EVT:
- break;
- default:
- goto illegal_evt;
- }
- break;
- case SELF_DOWN_PEER_LEAVING:
- switch (evt) {
- case PEER_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_DOWN;
- break;
- case SELF_ESTABL_CONTACT_EVT:
- case PEER_ESTABL_CONTACT_EVT:
- case SELF_LOST_CONTACT_EVT:
- break;
- case NODE_SYNCH_END_EVT:
- case NODE_SYNCH_BEGIN_EVT:
- case NODE_FAILOVER_BEGIN_EVT:
- case NODE_FAILOVER_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- case SELF_UP_PEER_COMING:
- switch (evt) {
- case PEER_ESTABL_CONTACT_EVT:
- state = SELF_UP_PEER_UP;
- break;
- case SELF_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_DOWN;
- break;
- case SELF_ESTABL_CONTACT_EVT:
- case PEER_LOST_CONTACT_EVT:
- case NODE_SYNCH_END_EVT:
- case NODE_FAILOVER_BEGIN_EVT:
- break;
- case NODE_SYNCH_BEGIN_EVT:
- case NODE_FAILOVER_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- case SELF_COMING_PEER_UP:
- switch (evt) {
- case SELF_ESTABL_CONTACT_EVT:
- state = SELF_UP_PEER_UP;
- break;
- case PEER_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_DOWN;
- break;
- case SELF_LOST_CONTACT_EVT:
- case PEER_ESTABL_CONTACT_EVT:
- break;
- case NODE_SYNCH_END_EVT:
- case NODE_SYNCH_BEGIN_EVT:
- case NODE_FAILOVER_BEGIN_EVT:
- case NODE_FAILOVER_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- case SELF_LEAVING_PEER_DOWN:
- switch (evt) {
- case SELF_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_DOWN;
- break;
- case SELF_ESTABL_CONTACT_EVT:
- case PEER_ESTABL_CONTACT_EVT:
- case PEER_LOST_CONTACT_EVT:
- break;
- case NODE_SYNCH_END_EVT:
- case NODE_SYNCH_BEGIN_EVT:
- case NODE_FAILOVER_BEGIN_EVT:
- case NODE_FAILOVER_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- case NODE_FAILINGOVER:
- switch (evt) {
- case SELF_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_LEAVING;
- break;
- case PEER_LOST_CONTACT_EVT:
- state = SELF_LEAVING_PEER_DOWN;
- break;
- case NODE_FAILOVER_END_EVT:
- state = SELF_UP_PEER_UP;
- break;
- case NODE_FAILOVER_BEGIN_EVT:
- case SELF_ESTABL_CONTACT_EVT:
- case PEER_ESTABL_CONTACT_EVT:
- break;
- case NODE_SYNCH_BEGIN_EVT:
- case NODE_SYNCH_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- case NODE_SYNCHING:
- switch (evt) {
- case SELF_LOST_CONTACT_EVT:
- state = SELF_DOWN_PEER_LEAVING;
- break;
- case PEER_LOST_CONTACT_EVT:
- state = SELF_LEAVING_PEER_DOWN;
- break;
- case NODE_SYNCH_END_EVT:
- state = SELF_UP_PEER_UP;
- break;
- case NODE_FAILOVER_BEGIN_EVT:
- state = NODE_FAILINGOVER;
- break;
- case NODE_SYNCH_BEGIN_EVT:
- case SELF_ESTABL_CONTACT_EVT:
- case PEER_ESTABL_CONTACT_EVT:
- break;
- case NODE_FAILOVER_END_EVT:
- default:
- goto illegal_evt;
- }
- break;
- default:
- pr_err("Unknown node fsm state %x\n", state);
- break;
- }
- n->state = state;
- return;
- illegal_evt:
- pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
- }
- static void node_lost_contact(struct tipc_node *n,
- struct sk_buff_head *inputq)
- {
- struct tipc_sock_conn *conn, *safe;
- struct tipc_link *l;
- struct list_head *conns = &n->conn_sks;
- struct sk_buff *skb;
- uint i;
- pr_debug("Lost contact with %x\n", n->addr);
- n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
- /* Clean up broadcast state */
- tipc_bcast_remove_peer(n->net, n->bc_entry.link);
- /* Abort any ongoing link failover */
- for (i = 0; i < MAX_BEARERS; i++) {
- l = n->links[i].link;
- if (l)
- tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
- }
- /* Notify publications from this node */
- n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
- /* Notify sockets connected to node */
- list_for_each_entry_safe(conn, safe, conns, list) {
- skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
- SHORT_H_SIZE, 0, tipc_own_addr(n->net),
- conn->peer_node, conn->port,
- conn->peer_port, TIPC_ERR_NO_NODE);
- if (likely(skb))
- skb_queue_tail(inputq, skb);
- list_del(&conn->list);
- kfree(conn);
- }
- }
- /**
- * tipc_node_get_linkname - get the name of a link
- *
- * @bearer_id: id of the bearer
- * @node: peer node address
- * @linkname: link name output buffer
- *
- * Returns 0 on success
- */
- int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
- char *linkname, size_t len)
- {
- struct tipc_link *link;
- int err = -EINVAL;
- struct tipc_node *node = tipc_node_find(net, addr);
- if (!node)
- return err;
- if (bearer_id >= MAX_BEARERS)
- goto exit;
- tipc_node_read_lock(node);
- link = node->links[bearer_id].link;
- if (link) {
- strncpy(linkname, tipc_link_name(link), len);
- err = 0;
- }
- tipc_node_read_unlock(node);
- exit:
- tipc_node_put(node);
- return err;
- }
- /* Caller should hold node lock for the passed node */
- static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
- {
- void *hdr;
- struct nlattr *attrs;
- hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
- NLM_F_MULTI, TIPC_NL_NODE_GET);
- if (!hdr)
- return -EMSGSIZE;
- attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
- if (!attrs)
- goto msg_full;
- if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
- goto attr_msg_full;
- if (node_is_up(node))
- if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
- goto attr_msg_full;
- nla_nest_end(msg->skb, attrs);
- genlmsg_end(msg->skb, hdr);
- return 0;
- attr_msg_full:
- nla_nest_cancel(msg->skb, attrs);
- msg_full:
- genlmsg_cancel(msg->skb, hdr);
- return -EMSGSIZE;
- }
- /**
- * tipc_node_xmit() is the general link level function for message sending
- * @net: the applicable net namespace
- * @list: chain of buffers containing message
- * @dnode: address of destination node
- * @selector: a number used for deterministic link selection
- * Consumes the buffer chain.
- * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
- */
- int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
- u32 dnode, int selector)
- {
- struct tipc_link_entry *le = NULL;
- struct tipc_node *n;
- struct sk_buff_head xmitq;
- int bearer_id;
- int rc;
- if (in_own_node(net, dnode)) {
- tipc_sk_rcv(net, list);
- return 0;
- }
- n = tipc_node_find(net, dnode);
- if (unlikely(!n)) {
- skb_queue_purge(list);
- return -EHOSTUNREACH;
- }
- tipc_node_read_lock(n);
- bearer_id = n->active_links[selector & 1];
- if (unlikely(bearer_id == INVALID_BEARER_ID)) {
- tipc_node_read_unlock(n);
- tipc_node_put(n);
- skb_queue_purge(list);
- return -EHOSTUNREACH;
- }
- __skb_queue_head_init(&xmitq);
- le = &n->links[bearer_id];
- spin_lock_bh(&le->lock);
- rc = tipc_link_xmit(le->link, list, &xmitq);
- spin_unlock_bh(&le->lock);
- tipc_node_read_unlock(n);
- if (unlikely(rc == -ENOBUFS))
- tipc_node_link_down(n, bearer_id, false);
- else
- tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
- tipc_node_put(n);
- return rc;
- }
- /* tipc_node_xmit_skb(): send single buffer to destination
- * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
- * messages, which will not be rejected
- * The only exception is datagram messages rerouted after secondary
- * lookup, which are rare and safe to dispose of anyway.
- */
- int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
- u32 selector)
- {
- struct sk_buff_head head;
- skb_queue_head_init(&head);
- __skb_queue_tail(&head, skb);
- tipc_node_xmit(net, &head, dnode, selector);
- return 0;
- }
- /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
- * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
- */
- int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
- {
- struct sk_buff *skb;
- u32 selector, dnode;
- while ((skb = __skb_dequeue(xmitq))) {
- selector = msg_origport(buf_msg(skb));
- dnode = msg_destnode(buf_msg(skb));
- tipc_node_xmit_skb(net, skb, dnode, selector);
- }
- return 0;
- }
- void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
- {
- struct sk_buff *txskb;
- struct tipc_node *n;
- u32 dst;
- rcu_read_lock();
- list_for_each_entry_rcu(n, tipc_nodes(net), list) {
- dst = n->addr;
- if (in_own_node(net, dst))
- continue;
- if (!node_is_up(n))
- continue;
- txskb = pskb_copy(skb, GFP_ATOMIC);
- if (!txskb)
- break;
- msg_set_destnode(buf_msg(txskb), dst);
- tipc_node_xmit_skb(net, txskb, dst, 0);
- }
- rcu_read_unlock();
- kfree_skb(skb);
- }
- static void tipc_node_mcast_rcv(struct tipc_node *n)
- {
- struct tipc_bclink_entry *be = &n->bc_entry;
- /* 'arrvq' is under inputq2's lock protection */
- spin_lock_bh(&be->inputq2.lock);
- spin_lock_bh(&be->inputq1.lock);
- skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
- spin_unlock_bh(&be->inputq1.lock);
- spin_unlock_bh(&be->inputq2.lock);
- tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
- }
- static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
- int bearer_id, struct sk_buff_head *xmitq)
- {
- struct tipc_link *ucl;
- int rc;
- rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
- if (rc & TIPC_LINK_DOWN_EVT) {
- tipc_node_reset_links(n);
- return;
- }
- if (!(rc & TIPC_LINK_SND_STATE))
- return;
- /* If probe message, a STATE response will be sent anyway */
- if (msg_probe(hdr))
- return;
- /* Produce a STATE message carrying broadcast NACK */
- tipc_node_read_lock(n);
- ucl = n->links[bearer_id].link;
- if (ucl)
- tipc_link_build_state_msg(ucl, xmitq);
- tipc_node_read_unlock(n);
- }
- /**
- * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
- * @net: the applicable net namespace
- * @skb: TIPC packet
- * @bearer_id: id of bearer message arrived on
- *
- * Invoked with no locks held.
- */
- static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
- {
- int rc;
- struct sk_buff_head xmitq;
- struct tipc_bclink_entry *be;
- struct tipc_link_entry *le;
- struct tipc_msg *hdr = buf_msg(skb);
- int usr = msg_user(hdr);
- u32 dnode = msg_destnode(hdr);
- struct tipc_node *n;
- __skb_queue_head_init(&xmitq);
- /* If NACK for other node, let rcv link for that node peek into it */
- if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
- n = tipc_node_find(net, dnode);
- else
- n = tipc_node_find(net, msg_prevnode(hdr));
- if (!n) {
- kfree_skb(skb);
- return;
- }
- be = &n->bc_entry;
- le = &n->links[bearer_id];
- rc = tipc_bcast_rcv(net, be->link, skb);
- /* Broadcast ACKs are sent on a unicast link */
- if (rc & TIPC_LINK_SND_STATE) {
- tipc_node_read_lock(n);
- tipc_link_build_state_msg(le->link, &xmitq);
- tipc_node_read_unlock(n);
- }
- if (!skb_queue_empty(&xmitq))
- tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
- if (!skb_queue_empty(&be->inputq1))
- tipc_node_mcast_rcv(n);
- /* If reassembly or retransmission failure => reset all links to peer */
- if (rc & TIPC_LINK_DOWN_EVT)
- tipc_node_reset_links(n);
- tipc_node_put(n);
- }
- /**
- * tipc_node_check_state - check and if necessary update node state
- * @skb: TIPC packet
- * @bearer_id: identity of bearer delivering the packet
- * Returns true if state and msg are ok, otherwise false
- */
- static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
- int bearer_id, struct sk_buff_head *xmitq)
- {
- struct tipc_msg *hdr = buf_msg(skb);
- int usr = msg_user(hdr);
- int mtyp = msg_type(hdr);
- u16 oseqno = msg_seqno(hdr);
- u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
- u16 exp_pkts = msg_msgcnt(hdr);
- u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
- int state = n->state;
- struct tipc_link *l, *tnl, *pl = NULL;
- struct tipc_media_addr *maddr;
- int pb_id;
- l = n->links[bearer_id].link;
- if (!l)
- return false;
- rcv_nxt = tipc_link_rcv_nxt(l);
- if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
- return true;
- /* Find parallel link, if any */
- for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
- if ((pb_id != bearer_id) && n->links[pb_id].link) {
- pl = n->links[pb_id].link;
- break;
- }
- }
- if (!tipc_link_validate_msg(l, hdr))
- return false;
- /* Check and update node accesibility if applicable */
- if (state == SELF_UP_PEER_COMING) {
- if (!tipc_link_is_up(l))
- return true;
- if (!msg_peer_link_is_up(hdr))
- return true;
- tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
- }
- if (state == SELF_DOWN_PEER_LEAVING) {
- if (msg_peer_node_is_up(hdr))
- return false;
- tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
- return true;
- }
- if (state == SELF_LEAVING_PEER_DOWN)
- return false;
- /* Ignore duplicate packets */
- if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
- return true;
- /* Initiate or update failover mode if applicable */
- if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
- syncpt = oseqno + exp_pkts - 1;
- if (pl && tipc_link_is_up(pl)) {
- __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
- tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
- tipc_link_inputq(l));
- }
- /* If parallel link was already down, and this happened before
- * the tunnel link came up, FAILOVER was never sent. Ensure that
- * FAILOVER is sent to get peer out of NODE_FAILINGOVER state.
- */
- if (n->state != NODE_FAILINGOVER && !n->failover_sent) {
- tipc_link_create_dummy_tnl_msg(l, xmitq);
- n->failover_sent = true;
- }
- /* If pkts arrive out of order, use lowest calculated syncpt */
- if (less(syncpt, n->sync_point))
- n->sync_point = syncpt;
- }
- /* Open parallel link when tunnel link reaches synch point */
- if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
- if (!more(rcv_nxt, n->sync_point))
- return true;
- tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
- if (pl)
- tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
- return true;
- }
- /* No synching needed if only one link */
- if (!pl || !tipc_link_is_up(pl))
- return true;
- /* Initiate synch mode if applicable */
- if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
- syncpt = iseqno + exp_pkts - 1;
- if (!tipc_link_is_up(l))
- __tipc_node_link_up(n, bearer_id, xmitq);
- if (n->state == SELF_UP_PEER_UP) {
- n->sync_point = syncpt;
- tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
- tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
- }
- }
- /* Open tunnel link when parallel link reaches synch point */
- if (n->state == NODE_SYNCHING) {
- if (tipc_link_is_synching(l)) {
- tnl = l;
- } else {
- tnl = pl;
- pl = l;
- }
- inputq_len = skb_queue_len(tipc_link_inputq(pl));
- dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
- if (more(dlv_nxt, n->sync_point)) {
- tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
- tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
- return true;
- }
- if (l == pl)
- return true;
- if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
- return true;
- if (usr == LINK_PROTOCOL)
- return true;
- return false;
- }
- return true;
- }
- /**
- * tipc_rcv - process TIPC packets/messages arriving from off-node
- * @net: the applicable net namespace
- * @skb: TIPC packet
- * @bearer: pointer to bearer message arrived on
- *
- * Invoked with no locks held. Bearer pointer must point to a valid bearer
- * structure (i.e. cannot be NULL), but bearer can be inactive.
- */
- void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
- {
- struct sk_buff_head xmitq;
- struct tipc_node *n;
- struct tipc_msg *hdr;
- int bearer_id = b->identity;
- struct tipc_link_entry *le;
- u32 self = tipc_own_addr(net);
- int usr, rc = 0;
- u16 bc_ack;
- __skb_queue_head_init(&xmitq);
- /* Ensure message is well-formed before touching the header */
- if (unlikely(!tipc_msg_validate(&skb)))
- goto discard;
- hdr = buf_msg(skb);
- usr = msg_user(hdr);
- bc_ack = msg_bcast_ack(hdr);
- /* Handle arrival of discovery or broadcast packet */
- if (unlikely(msg_non_seq(hdr))) {
- if (unlikely(usr == LINK_CONFIG))
- return tipc_disc_rcv(net, skb, b);
- else
- return tipc_node_bc_rcv(net, skb, bearer_id);
- }
- /* Discard unicast link messages destined for another node */
- if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
- goto discard;
- /* Locate neighboring node that sent packet */
- n = tipc_node_find(net, msg_prevnode(hdr));
- if (unlikely(!n))
- goto discard;
- le = &n->links[bearer_id];
- /* Ensure broadcast reception is in synch with peer's send state */
- if (unlikely(usr == LINK_PROTOCOL))
- tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
- else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
- tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
- /* Receive packet directly if conditions permit */
- tipc_node_read_lock(n);
- if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
- spin_lock_bh(&le->lock);
- if (le->link) {
- rc = tipc_link_rcv(le->link, skb, &xmitq);
- skb = NULL;
- }
- spin_unlock_bh(&le->lock);
- }
- tipc_node_read_unlock(n);
- /* Check/update node state before receiving */
- if (unlikely(skb)) {
- if (unlikely(skb_linearize(skb)))
- goto discard;
- tipc_node_write_lock(n);
- if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
- if (le->link) {
- rc = tipc_link_rcv(le->link, skb, &xmitq);
- skb = NULL;
- }
- }
- tipc_node_write_unlock(n);
- }
- if (unlikely(rc & TIPC_LINK_UP_EVT))
- tipc_node_link_up(n, bearer_id, &xmitq);
- if (unlikely(rc & TIPC_LINK_DOWN_EVT))
- tipc_node_link_down(n, bearer_id, false);
- if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
- tipc_named_rcv(net, &n->bc_entry.namedq);
- if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
- tipc_node_mcast_rcv(n);
- if (!skb_queue_empty(&le->inputq))
- tipc_sk_rcv(net, &le->inputq);
- if (!skb_queue_empty(&xmitq))
- tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
- tipc_node_put(n);
- discard:
- kfree_skb(skb);
- }
- void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
- int prop)
- {
- struct tipc_net *tn = tipc_net(net);
- int bearer_id = b->identity;
- struct sk_buff_head xmitq;
- struct tipc_link_entry *e;
- struct tipc_node *n;
- __skb_queue_head_init(&xmitq);
- rcu_read_lock();
- list_for_each_entry_rcu(n, &tn->node_list, list) {
- tipc_node_write_lock(n);
- e = &n->links[bearer_id];
- if (e->link) {
- if (prop == TIPC_NLA_PROP_TOL)
- tipc_link_set_tolerance(e->link, b->tolerance,
- &xmitq);
- else if (prop == TIPC_NLA_PROP_MTU)
- tipc_link_set_mtu(e->link, b->mtu);
- }
- tipc_node_write_unlock(n);
- tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr);
- }
- rcu_read_unlock();
- }
- int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
- {
- struct net *net = sock_net(skb->sk);
- struct tipc_net *tn = net_generic(net, tipc_net_id);
- struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
- struct tipc_node *peer;
- u32 addr;
- int err;
- /* We identify the peer by its net */
- if (!info->attrs[TIPC_NLA_NET])
- return -EINVAL;
- err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
- info->attrs[TIPC_NLA_NET], tipc_nl_net_policy,
- info->extack);
- if (err)
- return err;
- if (!attrs[TIPC_NLA_NET_ADDR])
- return -EINVAL;
- addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
- if (in_own_node(net, addr))
- return -ENOTSUPP;
- spin_lock_bh(&tn->node_list_lock);
- peer = tipc_node_find(net, addr);
- if (!peer) {
- spin_unlock_bh(&tn->node_list_lock);
- return -ENXIO;
- }
- tipc_node_write_lock(peer);
- if (peer->state != SELF_DOWN_PEER_DOWN &&
- peer->state != SELF_DOWN_PEER_LEAVING) {
- tipc_node_write_unlock(peer);
- err = -EBUSY;
- goto err_out;
- }
- tipc_node_clear_links(peer);
- tipc_node_write_unlock(peer);
- tipc_node_delete(peer);
- err = 0;
- err_out:
- tipc_node_put(peer);
- spin_unlock_bh(&tn->node_list_lock);
- return err;
- }
- int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
- {
- int err;
- struct net *net = sock_net(skb->sk);
- struct tipc_net *tn = net_generic(net, tipc_net_id);
- int done = cb->args[0];
- int last_addr = cb->args[1];
- struct tipc_node *node;
- struct tipc_nl_msg msg;
- if (done)
- return 0;
- msg.skb = skb;
- msg.portid = NETLINK_CB(cb->skb).portid;
- msg.seq = cb->nlh->nlmsg_seq;
- rcu_read_lock();
- if (last_addr) {
- node = tipc_node_find(net, last_addr);
- if (!node) {
- rcu_read_unlock();
- /* We never set seq or call nl_dump_check_consistent()
- * this means that setting prev_seq here will cause the
- * consistence check to fail in the netlink callback
- * handler. Resulting in the NLMSG_DONE message having
- * the NLM_F_DUMP_INTR flag set if the node state
- * changed while we released the lock.
- */
- cb->prev_seq = 1;
- return -EPIPE;
- }
- tipc_node_put(node);
- }
- list_for_each_entry_rcu(node, &tn->node_list, list) {
- if (last_addr) {
- if (node->addr == last_addr)
- last_addr = 0;
- else
- continue;
- }
- tipc_node_read_lock(node);
- err = __tipc_nl_add_node(&msg, node);
- if (err) {
- last_addr = node->addr;
- tipc_node_read_unlock(node);
- goto out;
- }
- tipc_node_read_unlock(node);
- }
- done = 1;
- out:
- cb->args[0] = done;
- cb->args[1] = last_addr;
- rcu_read_unlock();
- return skb->len;
- }
- /* tipc_node_find_by_name - locate owner node of link by link's name
- * @net: the applicable net namespace
- * @name: pointer to link name string
- * @bearer_id: pointer to index in 'node->links' array where the link was found.
- *
- * Returns pointer to node owning the link, or 0 if no matching link is found.
- */
- static struct tipc_node *tipc_node_find_by_name(struct net *net,
- const char *link_name,
- unsigned int *bearer_id)
- {
- struct tipc_net *tn = net_generic(net, tipc_net_id);
- struct tipc_link *l;
- struct tipc_node *n;
- struct tipc_node *found_node = NULL;
- int i;
- *bearer_id = 0;
- rcu_read_lock();
- list_for_each_entry_rcu(n, &tn->node_list, list) {
- tipc_node_read_lock(n);
- for (i = 0; i < MAX_BEARERS; i++) {
- l = n->links[i].link;
- if (l && !strcmp(tipc_link_name(l), link_name)) {
- *bearer_id = i;
- found_node = n;
- break;
- }
- }
- tipc_node_read_unlock(n);
- if (found_node)
- break;
- }
- rcu_read_unlock();
- return found_node;
- }
- int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
- {
- int err;
- int res = 0;
- int bearer_id;
- char *name;
- struct tipc_link *link;
- struct tipc_node *node;
- struct sk_buff_head xmitq;
- struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
- struct net *net = sock_net(skb->sk);
- __skb_queue_head_init(&xmitq);
- if (!info->attrs[TIPC_NLA_LINK])
- return -EINVAL;
- err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
- info->attrs[TIPC_NLA_LINK],
- tipc_nl_link_policy, info->extack);
- if (err)
- return err;
- if (!attrs[TIPC_NLA_LINK_NAME])
- return -EINVAL;
- name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
- if (strcmp(name, tipc_bclink_name) == 0)
- return tipc_nl_bc_link_set(net, attrs);
- node = tipc_node_find_by_name(net, name, &bearer_id);
- if (!node)
- return -EINVAL;
- tipc_node_read_lock(node);
- link = node->links[bearer_id].link;
- if (!link) {
- res = -EINVAL;
- goto out;
- }
- if (attrs[TIPC_NLA_LINK_PROP]) {
- struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
- err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
- props);
- if (err) {
- res = err;
- goto out;
- }
- if (props[TIPC_NLA_PROP_TOL]) {
- u32 tol;
- tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
- tipc_link_set_tolerance(link, tol, &xmitq);
- }
- if (props[TIPC_NLA_PROP_PRIO]) {
- u32 prio;
- prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
- tipc_link_set_prio(link, prio, &xmitq);
- }
- if (props[TIPC_NLA_PROP_WIN]) {
- u32 win;
- win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
- tipc_link_set_queue_limits(link, win);
- }
- }
- out:
- tipc_node_read_unlock(node);
- tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
- return res;
- }
- int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
- {
- struct net *net = genl_info_net(info);
- struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
- struct tipc_nl_msg msg;
- char *name;
- int err;
- msg.portid = info->snd_portid;
- msg.seq = info->snd_seq;
- if (!info->attrs[TIPC_NLA_LINK])
- return -EINVAL;
- err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
- info->attrs[TIPC_NLA_LINK],
- tipc_nl_link_policy, info->extack);
- if (err)
- return err;
- if (!attrs[TIPC_NLA_LINK_NAME])
- return -EINVAL;
- name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
- msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
- if (!msg.skb)
- return -ENOMEM;
- if (strcmp(name, tipc_bclink_name) == 0) {
- err = tipc_nl_add_bc_link(net, &msg);
- if (err)
- goto err_free;
- } else {
- int bearer_id;
- struct tipc_node *node;
- struct tipc_link *link;
- node = tipc_node_find_by_name(net, name, &bearer_id);
- if (!node) {
- err = -EINVAL;
- goto err_free;
- }
- tipc_node_read_lock(node);
- link = node->links[bearer_id].link;
- if (!link) {
- tipc_node_read_unlock(node);
- err = -EINVAL;
- goto err_free;
- }
- err = __tipc_nl_add_link(net, &msg, link, 0);
- tipc_node_read_unlock(node);
- if (err)
- goto err_free;
- }
- return genlmsg_reply(msg.skb, info);
- err_free:
- nlmsg_free(msg.skb);
- return err;
- }
- int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
- {
- int err;
- char *link_name;
- unsigned int bearer_id;
- struct tipc_link *link;
- struct tipc_node *node;
- struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
- struct net *net = sock_net(skb->sk);
- struct tipc_link_entry *le;
- if (!info->attrs[TIPC_NLA_LINK])
- return -EINVAL;
- err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
- info->attrs[TIPC_NLA_LINK],
- tipc_nl_link_policy, info->extack);
- if (err)
- return err;
- if (!attrs[TIPC_NLA_LINK_NAME])
- return -EINVAL;
- link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
- if (strcmp(link_name, tipc_bclink_name) == 0) {
- err = tipc_bclink_reset_stats(net);
- if (err)
- return err;
- return 0;
- }
- node = tipc_node_find_by_name(net, link_name, &bearer_id);
- if (!node)
- return -EINVAL;
- le = &node->links[bearer_id];
- tipc_node_read_lock(node);
- spin_lock_bh(&le->lock);
- link = node->links[bearer_id].link;
- if (!link) {
- spin_unlock_bh(&le->lock);
- tipc_node_read_unlock(node);
- return -EINVAL;
- }
- tipc_link_reset_stats(link);
- spin_unlock_bh(&le->lock);
- tipc_node_read_unlock(node);
- return 0;
- }
- /* Caller should hold node lock */
- static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
- struct tipc_node *node, u32 *prev_link)
- {
- u32 i;
- int err;
- for (i = *prev_link; i < MAX_BEARERS; i++) {
- *prev_link = i;
- if (!node->links[i].link)
- continue;
- err = __tipc_nl_add_link(net, msg,
- node->links[i].link, NLM_F_MULTI);
- if (err)
- return err;
- }
- *prev_link = 0;
- return 0;
- }
- int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
- {
- struct net *net = sock_net(skb->sk);
- struct tipc_net *tn = net_generic(net, tipc_net_id);
- struct tipc_node *node;
- struct tipc_nl_msg msg;
- u32 prev_node = cb->args[0];
- u32 prev_link = cb->args[1];
- int done = cb->args[2];
- int err;
- if (done)
- return 0;
- msg.skb = skb;
- msg.portid = NETLINK_CB(cb->skb).portid;
- msg.seq = cb->nlh->nlmsg_seq;
- rcu_read_lock();
- if (prev_node) {
- node = tipc_node_find(net, prev_node);
- if (!node) {
- /* We never set seq or call nl_dump_check_consistent()
- * this means that setting prev_seq here will cause the
- * consistence check to fail in the netlink callback
- * handler. Resulting in the last NLMSG_DONE message
- * having the NLM_F_DUMP_INTR flag set.
- */
- cb->prev_seq = 1;
- goto out;
- }
- tipc_node_put(node);
- list_for_each_entry_continue_rcu(node, &tn->node_list,
- list) {
- tipc_node_read_lock(node);
- err = __tipc_nl_add_node_links(net, &msg, node,
- &prev_link);
- tipc_node_read_unlock(node);
- if (err)
- goto out;
- prev_node = node->addr;
- }
- } else {
- err = tipc_nl_add_bc_link(net, &msg);
- if (err)
- goto out;
- list_for_each_entry_rcu(node, &tn->node_list, list) {
- tipc_node_read_lock(node);
- err = __tipc_nl_add_node_links(net, &msg, node,
- &prev_link);
- tipc_node_read_unlock(node);
- if (err)
- goto out;
- prev_node = node->addr;
- }
- }
- done = 1;
- out:
- rcu_read_unlock();
- cb->args[0] = prev_node;
- cb->args[1] = prev_link;
- cb->args[2] = done;
- return skb->len;
- }
- int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
- {
- struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
- struct net *net = sock_net(skb->sk);
- int err;
- if (!info->attrs[TIPC_NLA_MON])
- return -EINVAL;
- err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
- info->attrs[TIPC_NLA_MON],
- tipc_nl_monitor_policy, info->extack);
- if (err)
- return err;
- if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
- u32 val;
- val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
- err = tipc_nl_monitor_set_threshold(net, val);
- if (err)
- return err;
- }
- return 0;
- }
- static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
- {
- struct nlattr *attrs;
- void *hdr;
- u32 val;
- hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
- 0, TIPC_NL_MON_GET);
- if (!hdr)
- return -EMSGSIZE;
- attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
- if (!attrs)
- goto msg_full;
- val = tipc_nl_monitor_get_threshold(net);
- if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
- goto attr_msg_full;
- nla_nest_end(msg->skb, attrs);
- genlmsg_end(msg->skb, hdr);
- return 0;
- attr_msg_full:
- nla_nest_cancel(msg->skb, attrs);
- msg_full:
- genlmsg_cancel(msg->skb, hdr);
- return -EMSGSIZE;
- }
- int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
- {
- struct net *net = sock_net(skb->sk);
- struct tipc_nl_msg msg;
- int err;
- msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
- if (!msg.skb)
- return -ENOMEM;
- msg.portid = info->snd_portid;
- msg.seq = info->snd_seq;
- err = __tipc_nl_add_monitor_prop(net, &msg);
- if (err) {
- nlmsg_free(msg.skb);
- return err;
- }
- return genlmsg_reply(msg.skb, info);
- }
- int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
- {
- struct net *net = sock_net(skb->sk);
- u32 prev_bearer = cb->args[0];
- struct tipc_nl_msg msg;
- int bearer_id;
- int err;
- if (prev_bearer == MAX_BEARERS)
- return 0;
- msg.skb = skb;
- msg.portid = NETLINK_CB(cb->skb).portid;
- msg.seq = cb->nlh->nlmsg_seq;
- rtnl_lock();
- for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
- err = __tipc_nl_add_monitor(net, &msg, bearer_id);
- if (err)
- break;
- }
- rtnl_unlock();
- cb->args[0] = bearer_id;
- return skb->len;
- }
- int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
- struct netlink_callback *cb)
- {
- struct net *net = sock_net(skb->sk);
- u32 prev_node = cb->args[1];
- u32 bearer_id = cb->args[2];
- int done = cb->args[0];
- struct tipc_nl_msg msg;
- int err;
- if (!prev_node) {
- struct nlattr **attrs;
- struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
- err = tipc_nlmsg_parse(cb->nlh, &attrs);
- if (err)
- return err;
- if (!attrs[TIPC_NLA_MON])
- return -EINVAL;
- err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
- attrs[TIPC_NLA_MON],
- tipc_nl_monitor_policy, NULL);
- if (err)
- return err;
- if (!mon[TIPC_NLA_MON_REF])
- return -EINVAL;
- bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
- if (bearer_id >= MAX_BEARERS)
- return -EINVAL;
- }
- if (done)
- return 0;
- msg.skb = skb;
- msg.portid = NETLINK_CB(cb->skb).portid;
- msg.seq = cb->nlh->nlmsg_seq;
- rtnl_lock();
- err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
- if (!err)
- done = 1;
- rtnl_unlock();
- cb->args[0] = done;
- cb->args[1] = prev_node;
- cb->args[2] = bearer_id;
- return skb->len;
- }
|