dn_route.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * DECnet An implementation of the DECnet protocol suite for the LINUX
  3. * operating system. DECnet is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * DECnet Routing Functions (Endnode and Router)
  7. *
  8. * Authors: Steve Whitehouse <SteveW@ACM.org>
  9. * Eduardo Marcelo Serrat <emserrat@geocities.com>
  10. *
  11. * Changes:
  12. * Steve Whitehouse : Fixes to allow "intra-ethernet" and
  13. * "return-to-sender" bits on outgoing
  14. * packets.
  15. * Steve Whitehouse : Timeouts for cached routes.
  16. * Steve Whitehouse : Use dst cache for input routes too.
  17. * Steve Whitehouse : Fixed error values in dn_send_skb.
  18. * Steve Whitehouse : Rework routing functions to better fit
  19. * DECnet routing design
  20. * Alexey Kuznetsov : New SMP locking
  21. * Steve Whitehouse : More SMP locking changes & dn_cache_dump()
  22. * Steve Whitehouse : Prerouting NF hook, now really is prerouting.
  23. * Fixed possible skb leak in rtnetlink funcs.
  24. * Steve Whitehouse : Dave Miller's dynamic hash table sizing and
  25. * Alexey Kuznetsov's finer grained locking
  26. * from ipv4/route.c.
  27. * Steve Whitehouse : Routing is now starting to look like a
  28. * sensible set of code now, mainly due to
  29. * my copying the IPv4 routing code. The
  30. * hooks here are modified and will continue
  31. * to evolve for a while.
  32. * Steve Whitehouse : Real SMP at last :-) Also new netfilter
  33. * stuff. Look out raw sockets your days
  34. * are numbered!
  35. * Steve Whitehouse : Added return-to-sender functions. Added
  36. * backlog congestion level return codes.
  37. * Steve Whitehouse : Fixed bug where routes were set up with
  38. * no ref count on net devices.
  39. * Steve Whitehouse : RCU for the route cache
  40. * Steve Whitehouse : Preparations for the flow cache
  41. * Steve Whitehouse : Prepare for nonlinear skbs
  42. */
  43. /******************************************************************************
  44. (c) 1995-1998 E.M. Serrat emserrat@geocities.com
  45. This program is free software; you can redistribute it and/or modify
  46. it under the terms of the GNU General Public License as published by
  47. the Free Software Foundation; either version 2 of the License, or
  48. any later version.
  49. This program is distributed in the hope that it will be useful,
  50. but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. GNU General Public License for more details.
  53. *******************************************************************************/
  54. #include <linux/errno.h>
  55. #include <linux/types.h>
  56. #include <linux/socket.h>
  57. #include <linux/in.h>
  58. #include <linux/kernel.h>
  59. #include <linux/sockios.h>
  60. #include <linux/net.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/inet.h>
  63. #include <linux/route.h>
  64. #include <linux/in_route.h>
  65. #include <linux/slab.h>
  66. #include <net/sock.h>
  67. #include <linux/mm.h>
  68. #include <linux/proc_fs.h>
  69. #include <linux/seq_file.h>
  70. #include <linux/init.h>
  71. #include <linux/rtnetlink.h>
  72. #include <linux/string.h>
  73. #include <linux/netfilter_decnet.h>
  74. #include <linux/rcupdate.h>
  75. #include <linux/times.h>
  76. #include <asm/errno.h>
  77. #include <net/net_namespace.h>
  78. #include <net/netlink.h>
  79. #include <net/neighbour.h>
  80. #include <net/dst.h>
  81. #include <net/flow.h>
  82. #include <net/fib_rules.h>
  83. #include <net/dn.h>
  84. #include <net/dn_dev.h>
  85. #include <net/dn_nsp.h>
  86. #include <net/dn_route.h>
  87. #include <net/dn_neigh.h>
  88. #include <net/dn_fib.h>
  89. struct dn_rt_hash_bucket
  90. {
  91. struct dn_route __rcu *chain;
  92. spinlock_t lock;
  93. };
  94. extern struct neigh_table dn_neigh_table;
  95. static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
  96. static const int dn_rt_min_delay = 2 * HZ;
  97. static const int dn_rt_max_delay = 10 * HZ;
  98. static const int dn_rt_mtu_expires = 10 * 60 * HZ;
  99. static unsigned long dn_rt_deadline;
  100. static int dn_dst_gc(struct dst_ops *ops);
  101. static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
  102. static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
  103. static unsigned int dn_dst_default_mtu(const struct dst_entry *dst);
  104. static void dn_dst_destroy(struct dst_entry *);
  105. static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
  106. static void dn_dst_link_failure(struct sk_buff *);
  107. static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
  108. static int dn_route_input(struct sk_buff *);
  109. static void dn_run_flush(unsigned long dummy);
  110. static struct dn_rt_hash_bucket *dn_rt_hash_table;
  111. static unsigned dn_rt_hash_mask;
  112. static struct timer_list dn_route_timer;
  113. static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
  114. int decnet_dst_gc_interval = 2;
  115. static struct dst_ops dn_dst_ops = {
  116. .family = PF_DECnet,
  117. .protocol = cpu_to_be16(ETH_P_DNA_RT),
  118. .gc_thresh = 128,
  119. .gc = dn_dst_gc,
  120. .check = dn_dst_check,
  121. .default_advmss = dn_dst_default_advmss,
  122. .default_mtu = dn_dst_default_mtu,
  123. .cow_metrics = dst_cow_metrics_generic,
  124. .destroy = dn_dst_destroy,
  125. .negative_advice = dn_dst_negative_advice,
  126. .link_failure = dn_dst_link_failure,
  127. .update_pmtu = dn_dst_update_pmtu,
  128. };
  129. static void dn_dst_destroy(struct dst_entry *dst)
  130. {
  131. dst_destroy_metrics_generic(dst);
  132. }
  133. static __inline__ unsigned dn_hash(__le16 src, __le16 dst)
  134. {
  135. __u16 tmp = (__u16 __force)(src ^ dst);
  136. tmp ^= (tmp >> 3);
  137. tmp ^= (tmp >> 5);
  138. tmp ^= (tmp >> 10);
  139. return dn_rt_hash_mask & (unsigned)tmp;
  140. }
  141. static inline void dnrt_free(struct dn_route *rt)
  142. {
  143. call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
  144. }
  145. static inline void dnrt_drop(struct dn_route *rt)
  146. {
  147. dst_release(&rt->dst);
  148. call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
  149. }
  150. static void dn_dst_check_expire(unsigned long dummy)
  151. {
  152. int i;
  153. struct dn_route *rt;
  154. struct dn_route __rcu **rtp;
  155. unsigned long now = jiffies;
  156. unsigned long expire = 120 * HZ;
  157. for (i = 0; i <= dn_rt_hash_mask; i++) {
  158. rtp = &dn_rt_hash_table[i].chain;
  159. spin_lock(&dn_rt_hash_table[i].lock);
  160. while ((rt = rcu_dereference_protected(*rtp,
  161. lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
  162. if (atomic_read(&rt->dst.__refcnt) ||
  163. (now - rt->dst.lastuse) < expire) {
  164. rtp = &rt->dst.dn_next;
  165. continue;
  166. }
  167. *rtp = rt->dst.dn_next;
  168. rt->dst.dn_next = NULL;
  169. dnrt_free(rt);
  170. }
  171. spin_unlock(&dn_rt_hash_table[i].lock);
  172. if ((jiffies - now) > 0)
  173. break;
  174. }
  175. mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
  176. }
  177. static int dn_dst_gc(struct dst_ops *ops)
  178. {
  179. struct dn_route *rt;
  180. struct dn_route __rcu **rtp;
  181. int i;
  182. unsigned long now = jiffies;
  183. unsigned long expire = 10 * HZ;
  184. for (i = 0; i <= dn_rt_hash_mask; i++) {
  185. spin_lock_bh(&dn_rt_hash_table[i].lock);
  186. rtp = &dn_rt_hash_table[i].chain;
  187. while ((rt = rcu_dereference_protected(*rtp,
  188. lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
  189. if (atomic_read(&rt->dst.__refcnt) ||
  190. (now - rt->dst.lastuse) < expire) {
  191. rtp = &rt->dst.dn_next;
  192. continue;
  193. }
  194. *rtp = rt->dst.dn_next;
  195. rt->dst.dn_next = NULL;
  196. dnrt_drop(rt);
  197. break;
  198. }
  199. spin_unlock_bh(&dn_rt_hash_table[i].lock);
  200. }
  201. return 0;
  202. }
  203. /*
  204. * The decnet standards don't impose a particular minimum mtu, what they
  205. * do insist on is that the routing layer accepts a datagram of at least
  206. * 230 bytes long. Here we have to subtract the routing header length from
  207. * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
  208. * assume the worst and use a long header size.
  209. *
  210. * We update both the mtu and the advertised mss (i.e. the segment size we
  211. * advertise to the other end).
  212. */
  213. static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
  214. {
  215. struct neighbour *n = dst_get_neighbour(dst);
  216. u32 min_mtu = 230;
  217. struct dn_dev *dn;
  218. dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
  219. if (dn && dn->use_long == 0)
  220. min_mtu -= 6;
  221. else
  222. min_mtu -= 21;
  223. if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
  224. if (!(dst_metric_locked(dst, RTAX_MTU))) {
  225. dst_metric_set(dst, RTAX_MTU, mtu);
  226. dst_set_expires(dst, dn_rt_mtu_expires);
  227. }
  228. if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
  229. u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
  230. u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
  231. if (!existing_mss || existing_mss > mss)
  232. dst_metric_set(dst, RTAX_ADVMSS, mss);
  233. }
  234. }
  235. }
  236. /*
  237. * When a route has been marked obsolete. (e.g. routing cache flush)
  238. */
  239. static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
  240. {
  241. return NULL;
  242. }
  243. static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
  244. {
  245. dst_release(dst);
  246. return NULL;
  247. }
  248. static void dn_dst_link_failure(struct sk_buff *skb)
  249. {
  250. }
  251. static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
  252. {
  253. return ((fl1->daddr ^ fl2->daddr) |
  254. (fl1->saddr ^ fl2->saddr) |
  255. (fl1->flowidn_mark ^ fl2->flowidn_mark) |
  256. (fl1->flowidn_scope ^ fl2->flowidn_scope) |
  257. (fl1->flowidn_oif ^ fl2->flowidn_oif) |
  258. (fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
  259. }
  260. static int dn_insert_route(struct dn_route *rt, unsigned hash, struct dn_route **rp)
  261. {
  262. struct dn_route *rth;
  263. struct dn_route __rcu **rthp;
  264. unsigned long now = jiffies;
  265. rthp = &dn_rt_hash_table[hash].chain;
  266. spin_lock_bh(&dn_rt_hash_table[hash].lock);
  267. while ((rth = rcu_dereference_protected(*rthp,
  268. lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
  269. if (compare_keys(&rth->fld, &rt->fld)) {
  270. /* Put it first */
  271. *rthp = rth->dst.dn_next;
  272. rcu_assign_pointer(rth->dst.dn_next,
  273. dn_rt_hash_table[hash].chain);
  274. rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
  275. dst_use(&rth->dst, now);
  276. spin_unlock_bh(&dn_rt_hash_table[hash].lock);
  277. dnrt_drop(rt);
  278. *rp = rth;
  279. return 0;
  280. }
  281. rthp = &rth->dst.dn_next;
  282. }
  283. rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
  284. rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
  285. dst_use(&rt->dst, now);
  286. spin_unlock_bh(&dn_rt_hash_table[hash].lock);
  287. *rp = rt;
  288. return 0;
  289. }
  290. static void dn_run_flush(unsigned long dummy)
  291. {
  292. int i;
  293. struct dn_route *rt, *next;
  294. for (i = 0; i < dn_rt_hash_mask; i++) {
  295. spin_lock_bh(&dn_rt_hash_table[i].lock);
  296. if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
  297. goto nothing_to_declare;
  298. for(; rt; rt = next) {
  299. next = rcu_dereference_raw(rt->dst.dn_next);
  300. RCU_INIT_POINTER(rt->dst.dn_next, NULL);
  301. dst_free((struct dst_entry *)rt);
  302. }
  303. nothing_to_declare:
  304. spin_unlock_bh(&dn_rt_hash_table[i].lock);
  305. }
  306. }
  307. static DEFINE_SPINLOCK(dn_rt_flush_lock);
  308. void dn_rt_cache_flush(int delay)
  309. {
  310. unsigned long now = jiffies;
  311. int user_mode = !in_interrupt();
  312. if (delay < 0)
  313. delay = dn_rt_min_delay;
  314. spin_lock_bh(&dn_rt_flush_lock);
  315. if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
  316. long tmo = (long)(dn_rt_deadline - now);
  317. if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
  318. tmo = 0;
  319. if (delay > tmo)
  320. delay = tmo;
  321. }
  322. if (delay <= 0) {
  323. spin_unlock_bh(&dn_rt_flush_lock);
  324. dn_run_flush(0);
  325. return;
  326. }
  327. if (dn_rt_deadline == 0)
  328. dn_rt_deadline = now + dn_rt_max_delay;
  329. dn_rt_flush_timer.expires = now + delay;
  330. add_timer(&dn_rt_flush_timer);
  331. spin_unlock_bh(&dn_rt_flush_lock);
  332. }
  333. /**
  334. * dn_return_short - Return a short packet to its sender
  335. * @skb: The packet to return
  336. *
  337. */
  338. static int dn_return_short(struct sk_buff *skb)
  339. {
  340. struct dn_skb_cb *cb;
  341. unsigned char *ptr;
  342. __le16 *src;
  343. __le16 *dst;
  344. /* Add back headers */
  345. skb_push(skb, skb->data - skb_network_header(skb));
  346. if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
  347. return NET_RX_DROP;
  348. cb = DN_SKB_CB(skb);
  349. /* Skip packet length and point to flags */
  350. ptr = skb->data + 2;
  351. *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
  352. dst = (__le16 *)ptr;
  353. ptr += 2;
  354. src = (__le16 *)ptr;
  355. ptr += 2;
  356. *ptr = 0; /* Zero hop count */
  357. swap(*src, *dst);
  358. skb->pkt_type = PACKET_OUTGOING;
  359. dn_rt_finish_output(skb, NULL, NULL);
  360. return NET_RX_SUCCESS;
  361. }
  362. /**
  363. * dn_return_long - Return a long packet to its sender
  364. * @skb: The long format packet to return
  365. *
  366. */
  367. static int dn_return_long(struct sk_buff *skb)
  368. {
  369. struct dn_skb_cb *cb;
  370. unsigned char *ptr;
  371. unsigned char *src_addr, *dst_addr;
  372. unsigned char tmp[ETH_ALEN];
  373. /* Add back all headers */
  374. skb_push(skb, skb->data - skb_network_header(skb));
  375. if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
  376. return NET_RX_DROP;
  377. cb = DN_SKB_CB(skb);
  378. /* Ignore packet length and point to flags */
  379. ptr = skb->data + 2;
  380. /* Skip padding */
  381. if (*ptr & DN_RT_F_PF) {
  382. char padlen = (*ptr & ~DN_RT_F_PF);
  383. ptr += padlen;
  384. }
  385. *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
  386. ptr += 2;
  387. dst_addr = ptr;
  388. ptr += 8;
  389. src_addr = ptr;
  390. ptr += 6;
  391. *ptr = 0; /* Zero hop count */
  392. /* Swap source and destination */
  393. memcpy(tmp, src_addr, ETH_ALEN);
  394. memcpy(src_addr, dst_addr, ETH_ALEN);
  395. memcpy(dst_addr, tmp, ETH_ALEN);
  396. skb->pkt_type = PACKET_OUTGOING;
  397. dn_rt_finish_output(skb, dst_addr, src_addr);
  398. return NET_RX_SUCCESS;
  399. }
  400. /**
  401. * dn_route_rx_packet - Try and find a route for an incoming packet
  402. * @skb: The packet to find a route for
  403. *
  404. * Returns: result of input function if route is found, error code otherwise
  405. */
  406. static int dn_route_rx_packet(struct sk_buff *skb)
  407. {
  408. struct dn_skb_cb *cb;
  409. int err;
  410. if ((err = dn_route_input(skb)) == 0)
  411. return dst_input(skb);
  412. cb = DN_SKB_CB(skb);
  413. if (decnet_debug_level & 4) {
  414. char *devname = skb->dev ? skb->dev->name : "???";
  415. printk(KERN_DEBUG
  416. "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
  417. (int)cb->rt_flags, devname, skb->len,
  418. le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
  419. err, skb->pkt_type);
  420. }
  421. if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
  422. switch(cb->rt_flags & DN_RT_PKT_MSK) {
  423. case DN_RT_PKT_SHORT:
  424. return dn_return_short(skb);
  425. case DN_RT_PKT_LONG:
  426. return dn_return_long(skb);
  427. }
  428. }
  429. kfree_skb(skb);
  430. return NET_RX_DROP;
  431. }
  432. static int dn_route_rx_long(struct sk_buff *skb)
  433. {
  434. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  435. unsigned char *ptr = skb->data;
  436. if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
  437. goto drop_it;
  438. skb_pull(skb, 20);
  439. skb_reset_transport_header(skb);
  440. /* Destination info */
  441. ptr += 2;
  442. cb->dst = dn_eth2dn(ptr);
  443. if (memcmp(ptr, dn_hiord_addr, 4) != 0)
  444. goto drop_it;
  445. ptr += 6;
  446. /* Source info */
  447. ptr += 2;
  448. cb->src = dn_eth2dn(ptr);
  449. if (memcmp(ptr, dn_hiord_addr, 4) != 0)
  450. goto drop_it;
  451. ptr += 6;
  452. /* Other junk */
  453. ptr++;
  454. cb->hops = *ptr++; /* Visit Count */
  455. return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
  456. dn_route_rx_packet);
  457. drop_it:
  458. kfree_skb(skb);
  459. return NET_RX_DROP;
  460. }
  461. static int dn_route_rx_short(struct sk_buff *skb)
  462. {
  463. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  464. unsigned char *ptr = skb->data;
  465. if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
  466. goto drop_it;
  467. skb_pull(skb, 5);
  468. skb_reset_transport_header(skb);
  469. cb->dst = *(__le16 *)ptr;
  470. ptr += 2;
  471. cb->src = *(__le16 *)ptr;
  472. ptr += 2;
  473. cb->hops = *ptr & 0x3f;
  474. return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
  475. dn_route_rx_packet);
  476. drop_it:
  477. kfree_skb(skb);
  478. return NET_RX_DROP;
  479. }
  480. static int dn_route_discard(struct sk_buff *skb)
  481. {
  482. /*
  483. * I know we drop the packet here, but thats considered success in
  484. * this case
  485. */
  486. kfree_skb(skb);
  487. return NET_RX_SUCCESS;
  488. }
  489. static int dn_route_ptp_hello(struct sk_buff *skb)
  490. {
  491. dn_dev_hello(skb);
  492. dn_neigh_pointopoint_hello(skb);
  493. return NET_RX_SUCCESS;
  494. }
  495. int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  496. {
  497. struct dn_skb_cb *cb;
  498. unsigned char flags = 0;
  499. __u16 len = le16_to_cpu(*(__le16 *)skb->data);
  500. struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
  501. unsigned char padlen = 0;
  502. if (!net_eq(dev_net(dev), &init_net))
  503. goto dump_it;
  504. if (dn == NULL)
  505. goto dump_it;
  506. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  507. goto out;
  508. if (!pskb_may_pull(skb, 3))
  509. goto dump_it;
  510. skb_pull(skb, 2);
  511. if (len > skb->len)
  512. goto dump_it;
  513. skb_trim(skb, len);
  514. flags = *skb->data;
  515. cb = DN_SKB_CB(skb);
  516. cb->stamp = jiffies;
  517. cb->iif = dev->ifindex;
  518. /*
  519. * If we have padding, remove it.
  520. */
  521. if (flags & DN_RT_F_PF) {
  522. padlen = flags & ~DN_RT_F_PF;
  523. if (!pskb_may_pull(skb, padlen + 1))
  524. goto dump_it;
  525. skb_pull(skb, padlen);
  526. flags = *skb->data;
  527. }
  528. skb_reset_network_header(skb);
  529. /*
  530. * Weed out future version DECnet
  531. */
  532. if (flags & DN_RT_F_VER)
  533. goto dump_it;
  534. cb->rt_flags = flags;
  535. if (decnet_debug_level & 1)
  536. printk(KERN_DEBUG
  537. "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
  538. (int)flags, (dev) ? dev->name : "???", len, skb->len,
  539. padlen);
  540. if (flags & DN_RT_PKT_CNTL) {
  541. if (unlikely(skb_linearize(skb)))
  542. goto dump_it;
  543. switch(flags & DN_RT_CNTL_MSK) {
  544. case DN_RT_PKT_INIT:
  545. dn_dev_init_pkt(skb);
  546. break;
  547. case DN_RT_PKT_VERI:
  548. dn_dev_veri_pkt(skb);
  549. break;
  550. }
  551. if (dn->parms.state != DN_DEV_S_RU)
  552. goto dump_it;
  553. switch(flags & DN_RT_CNTL_MSK) {
  554. case DN_RT_PKT_HELO:
  555. return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
  556. skb, skb->dev, NULL,
  557. dn_route_ptp_hello);
  558. case DN_RT_PKT_L1RT:
  559. case DN_RT_PKT_L2RT:
  560. return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
  561. skb, skb->dev, NULL,
  562. dn_route_discard);
  563. case DN_RT_PKT_ERTH:
  564. return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
  565. skb, skb->dev, NULL,
  566. dn_neigh_router_hello);
  567. case DN_RT_PKT_EEDH:
  568. return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
  569. skb, skb->dev, NULL,
  570. dn_neigh_endnode_hello);
  571. }
  572. } else {
  573. if (dn->parms.state != DN_DEV_S_RU)
  574. goto dump_it;
  575. skb_pull(skb, 1); /* Pull flags */
  576. switch(flags & DN_RT_PKT_MSK) {
  577. case DN_RT_PKT_LONG:
  578. return dn_route_rx_long(skb);
  579. case DN_RT_PKT_SHORT:
  580. return dn_route_rx_short(skb);
  581. }
  582. }
  583. dump_it:
  584. kfree_skb(skb);
  585. out:
  586. return NET_RX_DROP;
  587. }
  588. static int dn_output(struct sk_buff *skb)
  589. {
  590. struct dst_entry *dst = skb_dst(skb);
  591. struct dn_route *rt = (struct dn_route *)dst;
  592. struct net_device *dev = dst->dev;
  593. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  594. struct neighbour *neigh;
  595. int err = -EINVAL;
  596. if ((neigh = dst_get_neighbour(dst)) == NULL)
  597. goto error;
  598. skb->dev = dev;
  599. cb->src = rt->rt_saddr;
  600. cb->dst = rt->rt_daddr;
  601. /*
  602. * Always set the Intra-Ethernet bit on all outgoing packets
  603. * originated on this node. Only valid flag from upper layers
  604. * is return-to-sender-requested. Set hop count to 0 too.
  605. */
  606. cb->rt_flags &= ~DN_RT_F_RQR;
  607. cb->rt_flags |= DN_RT_F_IE;
  608. cb->hops = 0;
  609. return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
  610. neigh->output);
  611. error:
  612. if (net_ratelimit())
  613. printk(KERN_DEBUG "dn_output: This should not happen\n");
  614. kfree_skb(skb);
  615. return err;
  616. }
  617. static int dn_forward(struct sk_buff *skb)
  618. {
  619. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  620. struct dst_entry *dst = skb_dst(skb);
  621. struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
  622. struct dn_route *rt;
  623. struct neighbour *neigh = dst_get_neighbour(dst);
  624. int header_len;
  625. #ifdef CONFIG_NETFILTER
  626. struct net_device *dev = skb->dev;
  627. #endif
  628. if (skb->pkt_type != PACKET_HOST)
  629. goto drop;
  630. /* Ensure that we have enough space for headers */
  631. rt = (struct dn_route *)skb_dst(skb);
  632. header_len = dn_db->use_long ? 21 : 6;
  633. if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
  634. goto drop;
  635. /*
  636. * Hop count exceeded.
  637. */
  638. if (++cb->hops > 30)
  639. goto drop;
  640. skb->dev = rt->dst.dev;
  641. /*
  642. * If packet goes out same interface it came in on, then set
  643. * the Intra-Ethernet bit. This has no effect for short
  644. * packets, so we don't need to test for them here.
  645. */
  646. cb->rt_flags &= ~DN_RT_F_IE;
  647. if (rt->rt_flags & RTCF_DOREDIRECT)
  648. cb->rt_flags |= DN_RT_F_IE;
  649. return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
  650. neigh->output);
  651. drop:
  652. kfree_skb(skb);
  653. return NET_RX_DROP;
  654. }
  655. /*
  656. * Used to catch bugs. This should never normally get
  657. * called.
  658. */
  659. static int dn_rt_bug(struct sk_buff *skb)
  660. {
  661. if (net_ratelimit()) {
  662. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  663. printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
  664. le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
  665. }
  666. kfree_skb(skb);
  667. return NET_RX_DROP;
  668. }
  669. static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
  670. {
  671. return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
  672. }
  673. static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
  674. {
  675. return dst->dev->mtu;
  676. }
  677. static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
  678. {
  679. struct dn_fib_info *fi = res->fi;
  680. struct net_device *dev = rt->dst.dev;
  681. unsigned int mss_metric;
  682. struct neighbour *n;
  683. if (fi) {
  684. if (DN_FIB_RES_GW(*res) &&
  685. DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  686. rt->rt_gateway = DN_FIB_RES_GW(*res);
  687. dst_init_metrics(&rt->dst, fi->fib_metrics, true);
  688. }
  689. rt->rt_type = res->type;
  690. if (dev != NULL && dst_get_neighbour(&rt->dst) == NULL) {
  691. n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
  692. if (IS_ERR(n))
  693. return PTR_ERR(n);
  694. dst_set_neighbour(&rt->dst, n);
  695. }
  696. if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
  697. dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
  698. mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
  699. if (mss_metric) {
  700. unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
  701. if (mss_metric > mss)
  702. dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
  703. }
  704. return 0;
  705. }
  706. static inline int dn_match_addr(__le16 addr1, __le16 addr2)
  707. {
  708. __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
  709. int match = 16;
  710. while(tmp) {
  711. tmp >>= 1;
  712. match--;
  713. }
  714. return match;
  715. }
  716. static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
  717. {
  718. __le16 saddr = 0;
  719. struct dn_dev *dn_db;
  720. struct dn_ifaddr *ifa;
  721. int best_match = 0;
  722. int ret;
  723. rcu_read_lock();
  724. dn_db = rcu_dereference(dev->dn_ptr);
  725. for (ifa = rcu_dereference(dn_db->ifa_list);
  726. ifa != NULL;
  727. ifa = rcu_dereference(ifa->ifa_next)) {
  728. if (ifa->ifa_scope > scope)
  729. continue;
  730. if (!daddr) {
  731. saddr = ifa->ifa_local;
  732. break;
  733. }
  734. ret = dn_match_addr(daddr, ifa->ifa_local);
  735. if (ret > best_match)
  736. saddr = ifa->ifa_local;
  737. if (best_match == 0)
  738. saddr = ifa->ifa_local;
  739. }
  740. rcu_read_unlock();
  741. return saddr;
  742. }
  743. static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
  744. {
  745. return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
  746. }
  747. static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
  748. {
  749. __le16 mask = dnet_make_mask(res->prefixlen);
  750. return (daddr&~mask)|res->fi->fib_nh->nh_gw;
  751. }
  752. static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
  753. {
  754. struct flowidn fld = {
  755. .daddr = oldflp->daddr,
  756. .saddr = oldflp->saddr,
  757. .flowidn_scope = RT_SCOPE_UNIVERSE,
  758. .flowidn_mark = oldflp->flowidn_mark,
  759. .flowidn_iif = init_net.loopback_dev->ifindex,
  760. .flowidn_oif = oldflp->flowidn_oif,
  761. };
  762. struct dn_route *rt = NULL;
  763. struct net_device *dev_out = NULL, *dev;
  764. struct neighbour *neigh = NULL;
  765. unsigned hash;
  766. unsigned flags = 0;
  767. struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
  768. int err;
  769. int free_res = 0;
  770. __le16 gateway = 0;
  771. if (decnet_debug_level & 16)
  772. printk(KERN_DEBUG
  773. "dn_route_output_slow: dst=%04x src=%04x mark=%d"
  774. " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
  775. le16_to_cpu(oldflp->saddr),
  776. oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
  777. oldflp->flowidn_oif);
  778. /* If we have an output interface, verify its a DECnet device */
  779. if (oldflp->flowidn_oif) {
  780. dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
  781. err = -ENODEV;
  782. if (dev_out && dev_out->dn_ptr == NULL) {
  783. dev_put(dev_out);
  784. dev_out = NULL;
  785. }
  786. if (dev_out == NULL)
  787. goto out;
  788. }
  789. /* If we have a source address, verify that its a local address */
  790. if (oldflp->saddr) {
  791. err = -EADDRNOTAVAIL;
  792. if (dev_out) {
  793. if (dn_dev_islocal(dev_out, oldflp->saddr))
  794. goto source_ok;
  795. dev_put(dev_out);
  796. goto out;
  797. }
  798. rcu_read_lock();
  799. for_each_netdev_rcu(&init_net, dev) {
  800. if (!dev->dn_ptr)
  801. continue;
  802. if (!dn_dev_islocal(dev, oldflp->saddr))
  803. continue;
  804. if ((dev->flags & IFF_LOOPBACK) &&
  805. oldflp->daddr &&
  806. !dn_dev_islocal(dev, oldflp->daddr))
  807. continue;
  808. dev_out = dev;
  809. break;
  810. }
  811. rcu_read_unlock();
  812. if (dev_out == NULL)
  813. goto out;
  814. dev_hold(dev_out);
  815. source_ok:
  816. ;
  817. }
  818. /* No destination? Assume its local */
  819. if (!fld.daddr) {
  820. fld.daddr = fld.saddr;
  821. err = -EADDRNOTAVAIL;
  822. if (dev_out)
  823. dev_put(dev_out);
  824. dev_out = init_net.loopback_dev;
  825. dev_hold(dev_out);
  826. if (!fld.daddr) {
  827. fld.daddr =
  828. fld.saddr = dnet_select_source(dev_out, 0,
  829. RT_SCOPE_HOST);
  830. if (!fld.daddr)
  831. goto out;
  832. }
  833. fld.flowidn_oif = init_net.loopback_dev->ifindex;
  834. res.type = RTN_LOCAL;
  835. goto make_route;
  836. }
  837. if (decnet_debug_level & 16)
  838. printk(KERN_DEBUG
  839. "dn_route_output_slow: initial checks complete."
  840. " dst=%o4x src=%04x oif=%d try_hard=%d\n",
  841. le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
  842. fld.flowidn_oif, try_hard);
  843. /*
  844. * N.B. If the kernel is compiled without router support then
  845. * dn_fib_lookup() will evaluate to non-zero so this if () block
  846. * will always be executed.
  847. */
  848. err = -ESRCH;
  849. if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
  850. struct dn_dev *dn_db;
  851. if (err != -ESRCH)
  852. goto out;
  853. /*
  854. * Here the fallback is basically the standard algorithm for
  855. * routing in endnodes which is described in the DECnet routing
  856. * docs
  857. *
  858. * If we are not trying hard, look in neighbour cache.
  859. * The result is tested to ensure that if a specific output
  860. * device/source address was requested, then we honour that
  861. * here
  862. */
  863. if (!try_hard) {
  864. neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
  865. if (neigh) {
  866. if ((oldflp->flowidn_oif &&
  867. (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
  868. (oldflp->saddr &&
  869. (!dn_dev_islocal(neigh->dev,
  870. oldflp->saddr)))) {
  871. neigh_release(neigh);
  872. neigh = NULL;
  873. } else {
  874. if (dev_out)
  875. dev_put(dev_out);
  876. if (dn_dev_islocal(neigh->dev, fld.daddr)) {
  877. dev_out = init_net.loopback_dev;
  878. res.type = RTN_LOCAL;
  879. } else {
  880. dev_out = neigh->dev;
  881. }
  882. dev_hold(dev_out);
  883. goto select_source;
  884. }
  885. }
  886. }
  887. /* Not there? Perhaps its a local address */
  888. if (dev_out == NULL)
  889. dev_out = dn_dev_get_default();
  890. err = -ENODEV;
  891. if (dev_out == NULL)
  892. goto out;
  893. dn_db = rcu_dereference_raw(dev_out->dn_ptr);
  894. /* Possible improvement - check all devices for local addr */
  895. if (dn_dev_islocal(dev_out, fld.daddr)) {
  896. dev_put(dev_out);
  897. dev_out = init_net.loopback_dev;
  898. dev_hold(dev_out);
  899. res.type = RTN_LOCAL;
  900. goto select_source;
  901. }
  902. /* Not local either.... try sending it to the default router */
  903. neigh = neigh_clone(dn_db->router);
  904. BUG_ON(neigh && neigh->dev != dev_out);
  905. /* Ok then, we assume its directly connected and move on */
  906. select_source:
  907. if (neigh)
  908. gateway = ((struct dn_neigh *)neigh)->addr;
  909. if (gateway == 0)
  910. gateway = fld.daddr;
  911. if (fld.saddr == 0) {
  912. fld.saddr = dnet_select_source(dev_out, gateway,
  913. res.type == RTN_LOCAL ?
  914. RT_SCOPE_HOST :
  915. RT_SCOPE_LINK);
  916. if (fld.saddr == 0 && res.type != RTN_LOCAL)
  917. goto e_addr;
  918. }
  919. fld.flowidn_oif = dev_out->ifindex;
  920. goto make_route;
  921. }
  922. free_res = 1;
  923. if (res.type == RTN_NAT)
  924. goto e_inval;
  925. if (res.type == RTN_LOCAL) {
  926. if (!fld.saddr)
  927. fld.saddr = fld.daddr;
  928. if (dev_out)
  929. dev_put(dev_out);
  930. dev_out = init_net.loopback_dev;
  931. dev_hold(dev_out);
  932. fld.flowidn_oif = dev_out->ifindex;
  933. if (res.fi)
  934. dn_fib_info_put(res.fi);
  935. res.fi = NULL;
  936. goto make_route;
  937. }
  938. if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
  939. dn_fib_select_multipath(&fld, &res);
  940. /*
  941. * We could add some logic to deal with default routes here and
  942. * get rid of some of the special casing above.
  943. */
  944. if (!fld.saddr)
  945. fld.saddr = DN_FIB_RES_PREFSRC(res);
  946. if (dev_out)
  947. dev_put(dev_out);
  948. dev_out = DN_FIB_RES_DEV(res);
  949. dev_hold(dev_out);
  950. fld.flowidn_oif = dev_out->ifindex;
  951. gateway = DN_FIB_RES_GW(res);
  952. make_route:
  953. if (dev_out->flags & IFF_LOOPBACK)
  954. flags |= RTCF_LOCAL;
  955. rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
  956. if (rt == NULL)
  957. goto e_nobufs;
  958. memset(&rt->fld, 0, sizeof(rt->fld));
  959. rt->fld.saddr = oldflp->saddr;
  960. rt->fld.daddr = oldflp->daddr;
  961. rt->fld.flowidn_oif = oldflp->flowidn_oif;
  962. rt->fld.flowidn_iif = 0;
  963. rt->fld.flowidn_mark = oldflp->flowidn_mark;
  964. rt->rt_saddr = fld.saddr;
  965. rt->rt_daddr = fld.daddr;
  966. rt->rt_gateway = gateway ? gateway : fld.daddr;
  967. rt->rt_local_src = fld.saddr;
  968. rt->rt_dst_map = fld.daddr;
  969. rt->rt_src_map = fld.saddr;
  970. dst_set_neighbour(&rt->dst, neigh);
  971. neigh = NULL;
  972. rt->dst.lastuse = jiffies;
  973. rt->dst.output = dn_output;
  974. rt->dst.input = dn_rt_bug;
  975. rt->rt_flags = flags;
  976. if (flags & RTCF_LOCAL)
  977. rt->dst.input = dn_nsp_rx;
  978. err = dn_rt_set_next_hop(rt, &res);
  979. if (err)
  980. goto e_neighbour;
  981. hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
  982. dn_insert_route(rt, hash, (struct dn_route **)pprt);
  983. done:
  984. if (neigh)
  985. neigh_release(neigh);
  986. if (free_res)
  987. dn_fib_res_put(&res);
  988. if (dev_out)
  989. dev_put(dev_out);
  990. out:
  991. return err;
  992. e_addr:
  993. err = -EADDRNOTAVAIL;
  994. goto done;
  995. e_inval:
  996. err = -EINVAL;
  997. goto done;
  998. e_nobufs:
  999. err = -ENOBUFS;
  1000. goto done;
  1001. e_neighbour:
  1002. dst_free(&rt->dst);
  1003. goto e_nobufs;
  1004. }
  1005. /*
  1006. * N.B. The flags may be moved into the flowi at some future stage.
  1007. */
  1008. static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
  1009. {
  1010. unsigned hash = dn_hash(flp->saddr, flp->daddr);
  1011. struct dn_route *rt = NULL;
  1012. if (!(flags & MSG_TRYHARD)) {
  1013. rcu_read_lock_bh();
  1014. for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
  1015. rt = rcu_dereference_bh(rt->dst.dn_next)) {
  1016. if ((flp->daddr == rt->fld.daddr) &&
  1017. (flp->saddr == rt->fld.saddr) &&
  1018. (flp->flowidn_mark == rt->fld.flowidn_mark) &&
  1019. dn_is_output_route(rt) &&
  1020. (rt->fld.flowidn_oif == flp->flowidn_oif)) {
  1021. dst_use(&rt->dst, jiffies);
  1022. rcu_read_unlock_bh();
  1023. *pprt = &rt->dst;
  1024. return 0;
  1025. }
  1026. }
  1027. rcu_read_unlock_bh();
  1028. }
  1029. return dn_route_output_slow(pprt, flp, flags);
  1030. }
  1031. static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
  1032. {
  1033. int err;
  1034. err = __dn_route_output_key(pprt, flp, flags);
  1035. if (err == 0 && flp->flowidn_proto) {
  1036. *pprt = xfrm_lookup(&init_net, *pprt,
  1037. flowidn_to_flowi(flp), NULL, 0);
  1038. if (IS_ERR(*pprt)) {
  1039. err = PTR_ERR(*pprt);
  1040. *pprt = NULL;
  1041. }
  1042. }
  1043. return err;
  1044. }
  1045. int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
  1046. {
  1047. int err;
  1048. err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
  1049. if (err == 0 && fl->flowidn_proto) {
  1050. if (!(flags & MSG_DONTWAIT))
  1051. fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
  1052. *pprt = xfrm_lookup(&init_net, *pprt,
  1053. flowidn_to_flowi(fl), sk, 0);
  1054. if (IS_ERR(*pprt)) {
  1055. err = PTR_ERR(*pprt);
  1056. *pprt = NULL;
  1057. }
  1058. }
  1059. return err;
  1060. }
  1061. static int dn_route_input_slow(struct sk_buff *skb)
  1062. {
  1063. struct dn_route *rt = NULL;
  1064. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1065. struct net_device *in_dev = skb->dev;
  1066. struct net_device *out_dev = NULL;
  1067. struct dn_dev *dn_db;
  1068. struct neighbour *neigh = NULL;
  1069. unsigned hash;
  1070. int flags = 0;
  1071. __le16 gateway = 0;
  1072. __le16 local_src = 0;
  1073. struct flowidn fld = {
  1074. .daddr = cb->dst,
  1075. .saddr = cb->src,
  1076. .flowidn_scope = RT_SCOPE_UNIVERSE,
  1077. .flowidn_mark = skb->mark,
  1078. .flowidn_iif = skb->dev->ifindex,
  1079. };
  1080. struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
  1081. int err = -EINVAL;
  1082. int free_res = 0;
  1083. dev_hold(in_dev);
  1084. if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
  1085. goto out;
  1086. /* Zero source addresses are not allowed */
  1087. if (fld.saddr == 0)
  1088. goto out;
  1089. /*
  1090. * In this case we've just received a packet from a source
  1091. * outside ourselves pretending to come from us. We don't
  1092. * allow it any further to prevent routing loops, spoofing and
  1093. * other nasties. Loopback packets already have the dst attached
  1094. * so this only affects packets which have originated elsewhere.
  1095. */
  1096. err = -ENOTUNIQ;
  1097. if (dn_dev_islocal(in_dev, cb->src))
  1098. goto out;
  1099. err = dn_fib_lookup(&fld, &res);
  1100. if (err) {
  1101. if (err != -ESRCH)
  1102. goto out;
  1103. /*
  1104. * Is the destination us ?
  1105. */
  1106. if (!dn_dev_islocal(in_dev, cb->dst))
  1107. goto e_inval;
  1108. res.type = RTN_LOCAL;
  1109. } else {
  1110. __le16 src_map = fld.saddr;
  1111. free_res = 1;
  1112. out_dev = DN_FIB_RES_DEV(res);
  1113. if (out_dev == NULL) {
  1114. if (net_ratelimit())
  1115. printk(KERN_CRIT "Bug in dn_route_input_slow() "
  1116. "No output device\n");
  1117. goto e_inval;
  1118. }
  1119. dev_hold(out_dev);
  1120. if (res.r)
  1121. src_map = fld.saddr; /* no NAT support for now */
  1122. gateway = DN_FIB_RES_GW(res);
  1123. if (res.type == RTN_NAT) {
  1124. fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
  1125. dn_fib_res_put(&res);
  1126. free_res = 0;
  1127. if (dn_fib_lookup(&fld, &res))
  1128. goto e_inval;
  1129. free_res = 1;
  1130. if (res.type != RTN_UNICAST)
  1131. goto e_inval;
  1132. flags |= RTCF_DNAT;
  1133. gateway = fld.daddr;
  1134. }
  1135. fld.saddr = src_map;
  1136. }
  1137. switch(res.type) {
  1138. case RTN_UNICAST:
  1139. /*
  1140. * Forwarding check here, we only check for forwarding
  1141. * being turned off, if you want to only forward intra
  1142. * area, its up to you to set the routing tables up
  1143. * correctly.
  1144. */
  1145. if (dn_db->parms.forwarding == 0)
  1146. goto e_inval;
  1147. if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
  1148. dn_fib_select_multipath(&fld, &res);
  1149. /*
  1150. * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
  1151. * flag as a hint to set the intra-ethernet bit when
  1152. * forwarding. If we've got NAT in operation, we don't do
  1153. * this optimisation.
  1154. */
  1155. if (out_dev == in_dev && !(flags & RTCF_NAT))
  1156. flags |= RTCF_DOREDIRECT;
  1157. local_src = DN_FIB_RES_PREFSRC(res);
  1158. case RTN_BLACKHOLE:
  1159. case RTN_UNREACHABLE:
  1160. break;
  1161. case RTN_LOCAL:
  1162. flags |= RTCF_LOCAL;
  1163. fld.saddr = cb->dst;
  1164. fld.daddr = cb->src;
  1165. /* Routing tables gave us a gateway */
  1166. if (gateway)
  1167. goto make_route;
  1168. /* Packet was intra-ethernet, so we know its on-link */
  1169. if (cb->rt_flags & DN_RT_F_IE) {
  1170. gateway = cb->src;
  1171. flags |= RTCF_DIRECTSRC;
  1172. goto make_route;
  1173. }
  1174. /* Use the default router if there is one */
  1175. neigh = neigh_clone(dn_db->router);
  1176. if (neigh) {
  1177. gateway = ((struct dn_neigh *)neigh)->addr;
  1178. goto make_route;
  1179. }
  1180. /* Close eyes and pray */
  1181. gateway = cb->src;
  1182. flags |= RTCF_DIRECTSRC;
  1183. goto make_route;
  1184. default:
  1185. goto e_inval;
  1186. }
  1187. make_route:
  1188. rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
  1189. if (rt == NULL)
  1190. goto e_nobufs;
  1191. memset(&rt->fld, 0, sizeof(rt->fld));
  1192. rt->rt_saddr = fld.saddr;
  1193. rt->rt_daddr = fld.daddr;
  1194. rt->rt_gateway = fld.daddr;
  1195. if (gateway)
  1196. rt->rt_gateway = gateway;
  1197. rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
  1198. rt->rt_dst_map = fld.daddr;
  1199. rt->rt_src_map = fld.saddr;
  1200. rt->fld.saddr = cb->src;
  1201. rt->fld.daddr = cb->dst;
  1202. rt->fld.flowidn_oif = 0;
  1203. rt->fld.flowidn_iif = in_dev->ifindex;
  1204. rt->fld.flowidn_mark = fld.flowidn_mark;
  1205. dst_set_neighbour(&rt->dst, neigh);
  1206. rt->dst.lastuse = jiffies;
  1207. rt->dst.output = dn_rt_bug;
  1208. switch(res.type) {
  1209. case RTN_UNICAST:
  1210. rt->dst.input = dn_forward;
  1211. break;
  1212. case RTN_LOCAL:
  1213. rt->dst.output = dn_output;
  1214. rt->dst.input = dn_nsp_rx;
  1215. rt->dst.dev = in_dev;
  1216. flags |= RTCF_LOCAL;
  1217. break;
  1218. default:
  1219. case RTN_UNREACHABLE:
  1220. case RTN_BLACKHOLE:
  1221. rt->dst.input = dst_discard;
  1222. }
  1223. rt->rt_flags = flags;
  1224. err = dn_rt_set_next_hop(rt, &res);
  1225. if (err)
  1226. goto e_neighbour;
  1227. hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
  1228. dn_insert_route(rt, hash, &rt);
  1229. skb_dst_set(skb, &rt->dst);
  1230. done:
  1231. if (neigh)
  1232. neigh_release(neigh);
  1233. if (free_res)
  1234. dn_fib_res_put(&res);
  1235. dev_put(in_dev);
  1236. if (out_dev)
  1237. dev_put(out_dev);
  1238. out:
  1239. return err;
  1240. e_inval:
  1241. err = -EINVAL;
  1242. goto done;
  1243. e_nobufs:
  1244. err = -ENOBUFS;
  1245. goto done;
  1246. e_neighbour:
  1247. dst_free(&rt->dst);
  1248. goto done;
  1249. }
  1250. static int dn_route_input(struct sk_buff *skb)
  1251. {
  1252. struct dn_route *rt;
  1253. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1254. unsigned hash = dn_hash(cb->src, cb->dst);
  1255. if (skb_dst(skb))
  1256. return 0;
  1257. rcu_read_lock();
  1258. for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
  1259. rt = rcu_dereference(rt->dst.dn_next)) {
  1260. if ((rt->fld.saddr == cb->src) &&
  1261. (rt->fld.daddr == cb->dst) &&
  1262. (rt->fld.flowidn_oif == 0) &&
  1263. (rt->fld.flowidn_mark == skb->mark) &&
  1264. (rt->fld.flowidn_iif == cb->iif)) {
  1265. dst_use(&rt->dst, jiffies);
  1266. rcu_read_unlock();
  1267. skb_dst_set(skb, (struct dst_entry *)rt);
  1268. return 0;
  1269. }
  1270. }
  1271. rcu_read_unlock();
  1272. return dn_route_input_slow(skb);
  1273. }
  1274. static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
  1275. int event, int nowait, unsigned int flags)
  1276. {
  1277. struct dn_route *rt = (struct dn_route *)skb_dst(skb);
  1278. struct rtmsg *r;
  1279. struct nlmsghdr *nlh;
  1280. unsigned char *b = skb_tail_pointer(skb);
  1281. long expires;
  1282. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
  1283. r = NLMSG_DATA(nlh);
  1284. r->rtm_family = AF_DECnet;
  1285. r->rtm_dst_len = 16;
  1286. r->rtm_src_len = 0;
  1287. r->rtm_tos = 0;
  1288. r->rtm_table = RT_TABLE_MAIN;
  1289. RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
  1290. r->rtm_type = rt->rt_type;
  1291. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  1292. r->rtm_scope = RT_SCOPE_UNIVERSE;
  1293. r->rtm_protocol = RTPROT_UNSPEC;
  1294. if (rt->rt_flags & RTCF_NOTIFY)
  1295. r->rtm_flags |= RTM_F_NOTIFY;
  1296. RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
  1297. if (rt->fld.saddr) {
  1298. r->rtm_src_len = 16;
  1299. RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
  1300. }
  1301. if (rt->dst.dev)
  1302. RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
  1303. /*
  1304. * Note to self - change this if input routes reverse direction when
  1305. * they deal only with inputs and not with replies like they do
  1306. * currently.
  1307. */
  1308. RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
  1309. if (rt->rt_daddr != rt->rt_gateway)
  1310. RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
  1311. if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
  1312. goto rtattr_failure;
  1313. expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
  1314. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
  1315. rt->dst.error) < 0)
  1316. goto rtattr_failure;
  1317. if (dn_is_input_route(rt))
  1318. RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
  1319. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1320. return skb->len;
  1321. nlmsg_failure:
  1322. rtattr_failure:
  1323. nlmsg_trim(skb, b);
  1324. return -1;
  1325. }
  1326. /*
  1327. * This is called by both endnodes and routers now.
  1328. */
  1329. static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
  1330. {
  1331. struct net *net = sock_net(in_skb->sk);
  1332. struct rtattr **rta = arg;
  1333. struct rtmsg *rtm = NLMSG_DATA(nlh);
  1334. struct dn_route *rt = NULL;
  1335. struct dn_skb_cb *cb;
  1336. int err;
  1337. struct sk_buff *skb;
  1338. struct flowidn fld;
  1339. if (!net_eq(net, &init_net))
  1340. return -EINVAL;
  1341. memset(&fld, 0, sizeof(fld));
  1342. fld.flowidn_proto = DNPROTO_NSP;
  1343. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1344. if (skb == NULL)
  1345. return -ENOBUFS;
  1346. skb_reset_mac_header(skb);
  1347. cb = DN_SKB_CB(skb);
  1348. if (rta[RTA_SRC-1])
  1349. memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
  1350. if (rta[RTA_DST-1])
  1351. memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
  1352. if (rta[RTA_IIF-1])
  1353. memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
  1354. if (fld.flowidn_iif) {
  1355. struct net_device *dev;
  1356. if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
  1357. kfree_skb(skb);
  1358. return -ENODEV;
  1359. }
  1360. if (!dev->dn_ptr) {
  1361. dev_put(dev);
  1362. kfree_skb(skb);
  1363. return -ENODEV;
  1364. }
  1365. skb->protocol = htons(ETH_P_DNA_RT);
  1366. skb->dev = dev;
  1367. cb->src = fld.saddr;
  1368. cb->dst = fld.daddr;
  1369. local_bh_disable();
  1370. err = dn_route_input(skb);
  1371. local_bh_enable();
  1372. memset(cb, 0, sizeof(struct dn_skb_cb));
  1373. rt = (struct dn_route *)skb_dst(skb);
  1374. if (!err && -rt->dst.error)
  1375. err = rt->dst.error;
  1376. } else {
  1377. int oif = 0;
  1378. if (rta[RTA_OIF - 1])
  1379. memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
  1380. fld.flowidn_oif = oif;
  1381. err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
  1382. }
  1383. if (skb->dev)
  1384. dev_put(skb->dev);
  1385. skb->dev = NULL;
  1386. if (err)
  1387. goto out_free;
  1388. skb_dst_set(skb, &rt->dst);
  1389. if (rtm->rtm_flags & RTM_F_NOTIFY)
  1390. rt->rt_flags |= RTCF_NOTIFY;
  1391. err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
  1392. if (err == 0)
  1393. goto out_free;
  1394. if (err < 0) {
  1395. err = -EMSGSIZE;
  1396. goto out_free;
  1397. }
  1398. return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
  1399. out_free:
  1400. kfree_skb(skb);
  1401. return err;
  1402. }
  1403. /*
  1404. * For routers, this is called from dn_fib_dump, but for endnodes its
  1405. * called directly from the rtnetlink dispatch table.
  1406. */
  1407. int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1408. {
  1409. struct net *net = sock_net(skb->sk);
  1410. struct dn_route *rt;
  1411. int h, s_h;
  1412. int idx, s_idx;
  1413. if (!net_eq(net, &init_net))
  1414. return 0;
  1415. if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
  1416. return -EINVAL;
  1417. if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
  1418. return 0;
  1419. s_h = cb->args[0];
  1420. s_idx = idx = cb->args[1];
  1421. for(h = 0; h <= dn_rt_hash_mask; h++) {
  1422. if (h < s_h)
  1423. continue;
  1424. if (h > s_h)
  1425. s_idx = 0;
  1426. rcu_read_lock_bh();
  1427. for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
  1428. rt;
  1429. rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
  1430. if (idx < s_idx)
  1431. continue;
  1432. skb_dst_set(skb, dst_clone(&rt->dst));
  1433. if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
  1434. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1435. 1, NLM_F_MULTI) <= 0) {
  1436. skb_dst_drop(skb);
  1437. rcu_read_unlock_bh();
  1438. goto done;
  1439. }
  1440. skb_dst_drop(skb);
  1441. }
  1442. rcu_read_unlock_bh();
  1443. }
  1444. done:
  1445. cb->args[0] = h;
  1446. cb->args[1] = idx;
  1447. return skb->len;
  1448. }
  1449. #ifdef CONFIG_PROC_FS
  1450. struct dn_rt_cache_iter_state {
  1451. int bucket;
  1452. };
  1453. static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
  1454. {
  1455. struct dn_route *rt = NULL;
  1456. struct dn_rt_cache_iter_state *s = seq->private;
  1457. for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
  1458. rcu_read_lock_bh();
  1459. rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
  1460. if (rt)
  1461. break;
  1462. rcu_read_unlock_bh();
  1463. }
  1464. return rt;
  1465. }
  1466. static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
  1467. {
  1468. struct dn_rt_cache_iter_state *s = seq->private;
  1469. rt = rcu_dereference_bh(rt->dst.dn_next);
  1470. while (!rt) {
  1471. rcu_read_unlock_bh();
  1472. if (--s->bucket < 0)
  1473. break;
  1474. rcu_read_lock_bh();
  1475. rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
  1476. }
  1477. return rt;
  1478. }
  1479. static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  1480. {
  1481. struct dn_route *rt = dn_rt_cache_get_first(seq);
  1482. if (rt) {
  1483. while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
  1484. --*pos;
  1485. }
  1486. return *pos ? NULL : rt;
  1487. }
  1488. static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1489. {
  1490. struct dn_route *rt = dn_rt_cache_get_next(seq, v);
  1491. ++*pos;
  1492. return rt;
  1493. }
  1494. static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
  1495. {
  1496. if (v)
  1497. rcu_read_unlock_bh();
  1498. }
  1499. static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
  1500. {
  1501. struct dn_route *rt = v;
  1502. char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
  1503. seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
  1504. rt->dst.dev ? rt->dst.dev->name : "*",
  1505. dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
  1506. dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
  1507. atomic_read(&rt->dst.__refcnt),
  1508. rt->dst.__use,
  1509. (int) dst_metric(&rt->dst, RTAX_RTT));
  1510. return 0;
  1511. }
  1512. static const struct seq_operations dn_rt_cache_seq_ops = {
  1513. .start = dn_rt_cache_seq_start,
  1514. .next = dn_rt_cache_seq_next,
  1515. .stop = dn_rt_cache_seq_stop,
  1516. .show = dn_rt_cache_seq_show,
  1517. };
  1518. static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
  1519. {
  1520. return seq_open_private(file, &dn_rt_cache_seq_ops,
  1521. sizeof(struct dn_rt_cache_iter_state));
  1522. }
  1523. static const struct file_operations dn_rt_cache_seq_fops = {
  1524. .owner = THIS_MODULE,
  1525. .open = dn_rt_cache_seq_open,
  1526. .read = seq_read,
  1527. .llseek = seq_lseek,
  1528. .release = seq_release_private,
  1529. };
  1530. #endif /* CONFIG_PROC_FS */
  1531. void __init dn_route_init(void)
  1532. {
  1533. int i, goal, order;
  1534. dn_dst_ops.kmem_cachep =
  1535. kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
  1536. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1537. dst_entries_init(&dn_dst_ops);
  1538. setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
  1539. dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
  1540. add_timer(&dn_route_timer);
  1541. goal = totalram_pages >> (26 - PAGE_SHIFT);
  1542. for(order = 0; (1UL << order) < goal; order++)
  1543. /* NOTHING */;
  1544. /*
  1545. * Only want 1024 entries max, since the table is very, very unlikely
  1546. * to be larger than that.
  1547. */
  1548. while(order && ((((1UL << order) * PAGE_SIZE) /
  1549. sizeof(struct dn_rt_hash_bucket)) >= 2048))
  1550. order--;
  1551. do {
  1552. dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
  1553. sizeof(struct dn_rt_hash_bucket);
  1554. while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
  1555. dn_rt_hash_mask--;
  1556. dn_rt_hash_table = (struct dn_rt_hash_bucket *)
  1557. __get_free_pages(GFP_ATOMIC, order);
  1558. } while (dn_rt_hash_table == NULL && --order > 0);
  1559. if (!dn_rt_hash_table)
  1560. panic("Failed to allocate DECnet route cache hash table\n");
  1561. printk(KERN_INFO
  1562. "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
  1563. dn_rt_hash_mask,
  1564. (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
  1565. dn_rt_hash_mask--;
  1566. for(i = 0; i <= dn_rt_hash_mask; i++) {
  1567. spin_lock_init(&dn_rt_hash_table[i].lock);
  1568. dn_rt_hash_table[i].chain = NULL;
  1569. }
  1570. dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
  1571. proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
  1572. #ifdef CONFIG_DECNET_ROUTER
  1573. rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
  1574. #else
  1575. rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
  1576. dn_cache_dump);
  1577. #endif
  1578. }
  1579. void __exit dn_route_cleanup(void)
  1580. {
  1581. del_timer(&dn_route_timer);
  1582. dn_run_flush(0);
  1583. proc_net_remove(&init_net, "decnet_cache");
  1584. dst_entries_destroy(&dn_dst_ops);
  1585. }