dn_route.c 45 KB

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