dn_route.c 46 KB

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