dn_route.c 47 KB

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