dn_route.c 47 KB

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