dn_route.c 47 KB

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