dn_neigh.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 Neighbour Functions (Adjacency Database and
  7. * On-Ethernet Cache)
  8. *
  9. * Author: Steve Whitehouse <SteveW@ACM.org>
  10. *
  11. *
  12. * Changes:
  13. * Steve Whitehouse : Fixed router listing routine
  14. * Steve Whitehouse : Added error_report functions
  15. * Steve Whitehouse : Added default router detection
  16. * Steve Whitehouse : Hop counts in outgoing messages
  17. * Steve Whitehouse : Fixed src/dst in outgoing messages so
  18. * forwarding now stands a good chance of
  19. * working.
  20. * Steve Whitehouse : Fixed neighbour states (for now anyway).
  21. * Steve Whitehouse : Made error_report functions dummies. This
  22. * is not the right place to return skbs.
  23. * Steve Whitehouse : Convert to seq_file
  24. *
  25. */
  26. #include <linux/net.h>
  27. #include <linux/module.h>
  28. #include <linux/socket.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/init.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/string.h>
  34. #include <linux/netfilter_decnet.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/jhash.h>
  39. #include <asm/atomic.h>
  40. #include <net/neighbour.h>
  41. #include <net/dst.h>
  42. #include <net/flow.h>
  43. #include <net/dn.h>
  44. #include <net/dn_dev.h>
  45. #include <net/dn_neigh.h>
  46. #include <net/dn_route.h>
  47. static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev);
  48. static int dn_neigh_construct(struct neighbour *);
  49. static void dn_long_error_report(struct neighbour *, struct sk_buff *);
  50. static void dn_short_error_report(struct neighbour *, struct sk_buff *);
  51. static int dn_long_output(struct sk_buff *);
  52. static int dn_short_output(struct sk_buff *);
  53. static int dn_phase3_output(struct sk_buff *);
  54. /*
  55. * For talking to broadcast devices: Ethernet & PPP
  56. */
  57. static struct neigh_ops dn_long_ops = {
  58. .family = AF_DECnet,
  59. .error_report = dn_long_error_report,
  60. .output = dn_long_output,
  61. .connected_output = dn_long_output,
  62. .hh_output = dev_queue_xmit,
  63. .queue_xmit = dev_queue_xmit,
  64. };
  65. /*
  66. * For talking to pointopoint and multidrop devices: DDCMP and X.25
  67. */
  68. static struct neigh_ops dn_short_ops = {
  69. .family = AF_DECnet,
  70. .error_report = dn_short_error_report,
  71. .output = dn_short_output,
  72. .connected_output = dn_short_output,
  73. .hh_output = dev_queue_xmit,
  74. .queue_xmit = dev_queue_xmit,
  75. };
  76. /*
  77. * For talking to DECnet phase III nodes
  78. */
  79. static struct neigh_ops dn_phase3_ops = {
  80. .family = AF_DECnet,
  81. .error_report = dn_short_error_report, /* Can use short version here */
  82. .output = dn_phase3_output,
  83. .connected_output = dn_phase3_output,
  84. .hh_output = dev_queue_xmit,
  85. .queue_xmit = dev_queue_xmit
  86. };
  87. struct neigh_table dn_neigh_table = {
  88. .family = PF_DECnet,
  89. .entry_size = sizeof(struct dn_neigh),
  90. .key_len = sizeof(__le16),
  91. .hash = dn_neigh_hash,
  92. .constructor = dn_neigh_construct,
  93. .id = "dn_neigh_cache",
  94. .parms ={
  95. .tbl = &dn_neigh_table,
  96. .base_reachable_time = 30 * HZ,
  97. .retrans_time = 1 * HZ,
  98. .gc_staletime = 60 * HZ,
  99. .reachable_time = 30 * HZ,
  100. .delay_probe_time = 5 * HZ,
  101. .queue_len = 3,
  102. .ucast_probes = 0,
  103. .app_probes = 0,
  104. .mcast_probes = 0,
  105. .anycast_delay = 0,
  106. .proxy_delay = 0,
  107. .proxy_qlen = 0,
  108. .locktime = 1 * HZ,
  109. },
  110. .gc_interval = 30 * HZ,
  111. .gc_thresh1 = 128,
  112. .gc_thresh2 = 512,
  113. .gc_thresh3 = 1024,
  114. };
  115. static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev)
  116. {
  117. return jhash_2words(*(__u16 *)pkey, 0, dn_neigh_table.hash_rnd);
  118. }
  119. static int dn_neigh_construct(struct neighbour *neigh)
  120. {
  121. struct net_device *dev = neigh->dev;
  122. struct dn_neigh *dn = (struct dn_neigh *)neigh;
  123. struct dn_dev *dn_db;
  124. struct neigh_parms *parms;
  125. rcu_read_lock();
  126. dn_db = rcu_dereference(dev->dn_ptr);
  127. if (dn_db == NULL) {
  128. rcu_read_unlock();
  129. return -EINVAL;
  130. }
  131. parms = dn_db->neigh_parms;
  132. if (!parms) {
  133. rcu_read_unlock();
  134. return -EINVAL;
  135. }
  136. __neigh_parms_put(neigh->parms);
  137. neigh->parms = neigh_parms_clone(parms);
  138. if (dn_db->use_long)
  139. neigh->ops = &dn_long_ops;
  140. else
  141. neigh->ops = &dn_short_ops;
  142. rcu_read_unlock();
  143. if (dn->flags & DN_NDFLAG_P3)
  144. neigh->ops = &dn_phase3_ops;
  145. neigh->nud_state = NUD_NOARP;
  146. neigh->output = neigh->ops->connected_output;
  147. if ((dev->type == ARPHRD_IPGRE) || (dev->flags & IFF_POINTOPOINT))
  148. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  149. else if ((dev->type == ARPHRD_ETHER) || (dev->type == ARPHRD_LOOPBACK))
  150. dn_dn2eth(neigh->ha, dn->addr);
  151. else {
  152. if (net_ratelimit())
  153. printk(KERN_DEBUG "Trying to create neigh for hw %d\n", dev->type);
  154. return -EINVAL;
  155. }
  156. /*
  157. * Make an estimate of the remote block size by assuming that its
  158. * two less then the device mtu, which it true for ethernet (and
  159. * other things which support long format headers) since there is
  160. * an extra length field (of 16 bits) which isn't part of the
  161. * ethernet headers and which the DECnet specs won't admit is part
  162. * of the DECnet routing headers either.
  163. *
  164. * If we over estimate here its no big deal, the NSP negotiations
  165. * will prevent us from sending packets which are too large for the
  166. * remote node to handle. In any case this figure is normally updated
  167. * by a hello message in most cases.
  168. */
  169. dn->blksize = dev->mtu - 2;
  170. return 0;
  171. }
  172. static void dn_long_error_report(struct neighbour *neigh, struct sk_buff *skb)
  173. {
  174. printk(KERN_DEBUG "dn_long_error_report: called\n");
  175. kfree_skb(skb);
  176. }
  177. static void dn_short_error_report(struct neighbour *neigh, struct sk_buff *skb)
  178. {
  179. printk(KERN_DEBUG "dn_short_error_report: called\n");
  180. kfree_skb(skb);
  181. }
  182. static int dn_neigh_output_packet(struct sk_buff *skb)
  183. {
  184. struct dst_entry *dst = skb->dst;
  185. struct dn_route *rt = (struct dn_route *)dst;
  186. struct neighbour *neigh = dst->neighbour;
  187. struct net_device *dev = neigh->dev;
  188. char mac_addr[ETH_ALEN];
  189. dn_dn2eth(mac_addr, rt->rt_local_src);
  190. if (!dev->hard_header || dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, mac_addr, skb->len) >= 0)
  191. return neigh->ops->queue_xmit(skb);
  192. if (net_ratelimit())
  193. printk(KERN_DEBUG "dn_neigh_output_packet: oops, can't send packet\n");
  194. kfree_skb(skb);
  195. return -EINVAL;
  196. }
  197. static int dn_long_output(struct sk_buff *skb)
  198. {
  199. struct dst_entry *dst = skb->dst;
  200. struct neighbour *neigh = dst->neighbour;
  201. struct net_device *dev = neigh->dev;
  202. int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3;
  203. unsigned char *data;
  204. struct dn_long_packet *lp;
  205. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  206. if (skb_headroom(skb) < headroom) {
  207. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  208. if (skb2 == NULL) {
  209. if (net_ratelimit())
  210. printk(KERN_CRIT "dn_long_output: no memory\n");
  211. kfree_skb(skb);
  212. return -ENOBUFS;
  213. }
  214. kfree_skb(skb);
  215. skb = skb2;
  216. if (net_ratelimit())
  217. printk(KERN_INFO "dn_long_output: Increasing headroom\n");
  218. }
  219. data = skb_push(skb, sizeof(struct dn_long_packet) + 3);
  220. lp = (struct dn_long_packet *)(data+3);
  221. *((__le16 *)data) = dn_htons(skb->len - 2);
  222. *(data + 2) = 1 | DN_RT_F_PF; /* Padding */
  223. lp->msgflg = DN_RT_PKT_LONG|(cb->rt_flags&(DN_RT_F_IE|DN_RT_F_RQR|DN_RT_F_RTS));
  224. lp->d_area = lp->d_subarea = 0;
  225. dn_dn2eth(lp->d_id, cb->dst);
  226. lp->s_area = lp->s_subarea = 0;
  227. dn_dn2eth(lp->s_id, cb->src);
  228. lp->nl2 = 0;
  229. lp->visit_ct = cb->hops & 0x3f;
  230. lp->s_class = 0;
  231. lp->pt = 0;
  232. skb->nh.raw = skb->data;
  233. return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
  234. }
  235. static int dn_short_output(struct sk_buff *skb)
  236. {
  237. struct dst_entry *dst = skb->dst;
  238. struct neighbour *neigh = dst->neighbour;
  239. struct net_device *dev = neigh->dev;
  240. int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
  241. struct dn_short_packet *sp;
  242. unsigned char *data;
  243. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  244. if (skb_headroom(skb) < headroom) {
  245. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  246. if (skb2 == NULL) {
  247. if (net_ratelimit())
  248. printk(KERN_CRIT "dn_short_output: no memory\n");
  249. kfree_skb(skb);
  250. return -ENOBUFS;
  251. }
  252. kfree_skb(skb);
  253. skb = skb2;
  254. if (net_ratelimit())
  255. printk(KERN_INFO "dn_short_output: Increasing headroom\n");
  256. }
  257. data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
  258. *((__le16 *)data) = dn_htons(skb->len - 2);
  259. sp = (struct dn_short_packet *)(data+2);
  260. sp->msgflg = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
  261. sp->dstnode = cb->dst;
  262. sp->srcnode = cb->src;
  263. sp->forward = cb->hops & 0x3f;
  264. skb->nh.raw = skb->data;
  265. return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
  266. }
  267. /*
  268. * Phase 3 output is the same is short output, execpt that
  269. * it clears the area bits before transmission.
  270. */
  271. static int dn_phase3_output(struct sk_buff *skb)
  272. {
  273. struct dst_entry *dst = skb->dst;
  274. struct neighbour *neigh = dst->neighbour;
  275. struct net_device *dev = neigh->dev;
  276. int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
  277. struct dn_short_packet *sp;
  278. unsigned char *data;
  279. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  280. if (skb_headroom(skb) < headroom) {
  281. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  282. if (skb2 == NULL) {
  283. if (net_ratelimit())
  284. printk(KERN_CRIT "dn_phase3_output: no memory\n");
  285. kfree_skb(skb);
  286. return -ENOBUFS;
  287. }
  288. kfree_skb(skb);
  289. skb = skb2;
  290. if (net_ratelimit())
  291. printk(KERN_INFO "dn_phase3_output: Increasing headroom\n");
  292. }
  293. data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
  294. *((__le16 *)data) = dn_htons(skb->len - 2);
  295. sp = (struct dn_short_packet *)(data + 2);
  296. sp->msgflg = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
  297. sp->dstnode = cb->dst & dn_htons(0x03ff);
  298. sp->srcnode = cb->src & dn_htons(0x03ff);
  299. sp->forward = cb->hops & 0x3f;
  300. skb->nh.raw = skb->data;
  301. return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
  302. }
  303. /*
  304. * Unfortunately, the neighbour code uses the device in its hash
  305. * function, so we don't get any advantage from it. This function
  306. * basically does a neigh_lookup(), but without comparing the device
  307. * field. This is required for the On-Ethernet cache
  308. */
  309. /*
  310. * Pointopoint link receives a hello message
  311. */
  312. void dn_neigh_pointopoint_hello(struct sk_buff *skb)
  313. {
  314. kfree_skb(skb);
  315. }
  316. /*
  317. * Ethernet router hello message received
  318. */
  319. int dn_neigh_router_hello(struct sk_buff *skb)
  320. {
  321. struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data;
  322. struct neighbour *neigh;
  323. struct dn_neigh *dn;
  324. struct dn_dev *dn_db;
  325. __le16 src;
  326. src = dn_eth2dn(msg->id);
  327. neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
  328. dn = (struct dn_neigh *)neigh;
  329. if (neigh) {
  330. write_lock(&neigh->lock);
  331. neigh->used = jiffies;
  332. dn_db = (struct dn_dev *)neigh->dev->dn_ptr;
  333. if (!(neigh->nud_state & NUD_PERMANENT)) {
  334. neigh->updated = jiffies;
  335. if (neigh->dev->type == ARPHRD_ETHER)
  336. memcpy(neigh->ha, &eth_hdr(skb)->h_source, ETH_ALEN);
  337. dn->blksize = dn_ntohs(msg->blksize);
  338. dn->priority = msg->priority;
  339. dn->flags &= ~DN_NDFLAG_P3;
  340. switch(msg->iinfo & DN_RT_INFO_TYPE) {
  341. case DN_RT_INFO_L1RT:
  342. dn->flags &=~DN_NDFLAG_R2;
  343. dn->flags |= DN_NDFLAG_R1;
  344. break;
  345. case DN_RT_INFO_L2RT:
  346. dn->flags |= DN_NDFLAG_R2;
  347. }
  348. }
  349. /* Only use routers in our area */
  350. if ((dn_ntohs(src)>>10) == (dn_ntohs((decnet_address))>>10)) {
  351. if (!dn_db->router) {
  352. dn_db->router = neigh_clone(neigh);
  353. } else {
  354. if (msg->priority > ((struct dn_neigh *)dn_db->router)->priority)
  355. neigh_release(xchg(&dn_db->router, neigh_clone(neigh)));
  356. }
  357. }
  358. write_unlock(&neigh->lock);
  359. neigh_release(neigh);
  360. }
  361. kfree_skb(skb);
  362. return 0;
  363. }
  364. /*
  365. * Endnode hello message received
  366. */
  367. int dn_neigh_endnode_hello(struct sk_buff *skb)
  368. {
  369. struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data;
  370. struct neighbour *neigh;
  371. struct dn_neigh *dn;
  372. __le16 src;
  373. src = dn_eth2dn(msg->id);
  374. neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
  375. dn = (struct dn_neigh *)neigh;
  376. if (neigh) {
  377. write_lock(&neigh->lock);
  378. neigh->used = jiffies;
  379. if (!(neigh->nud_state & NUD_PERMANENT)) {
  380. neigh->updated = jiffies;
  381. if (neigh->dev->type == ARPHRD_ETHER)
  382. memcpy(neigh->ha, &eth_hdr(skb)->h_source, ETH_ALEN);
  383. dn->flags &= ~(DN_NDFLAG_R1 | DN_NDFLAG_R2);
  384. dn->blksize = dn_ntohs(msg->blksize);
  385. dn->priority = 0;
  386. }
  387. write_unlock(&neigh->lock);
  388. neigh_release(neigh);
  389. }
  390. kfree_skb(skb);
  391. return 0;
  392. }
  393. static char *dn_find_slot(char *base, int max, int priority)
  394. {
  395. int i;
  396. unsigned char *min = NULL;
  397. base += 6; /* skip first id */
  398. for(i = 0; i < max; i++) {
  399. if (!min || (*base < *min))
  400. min = base;
  401. base += 7; /* find next priority */
  402. }
  403. if (!min)
  404. return NULL;
  405. return (*min < priority) ? (min - 6) : NULL;
  406. }
  407. struct elist_cb_state {
  408. struct net_device *dev;
  409. unsigned char *ptr;
  410. unsigned char *rs;
  411. int t, n;
  412. };
  413. static void neigh_elist_cb(struct neighbour *neigh, void *_info)
  414. {
  415. struct elist_cb_state *s = _info;
  416. struct dn_neigh *dn;
  417. if (neigh->dev != s->dev)
  418. return;
  419. dn = (struct dn_neigh *) neigh;
  420. if (!(dn->flags & (DN_NDFLAG_R1|DN_NDFLAG_R2)))
  421. return;
  422. if (s->t == s->n)
  423. s->rs = dn_find_slot(s->ptr, s->n, dn->priority);
  424. else
  425. s->t++;
  426. if (s->rs == NULL)
  427. return;
  428. dn_dn2eth(s->rs, dn->addr);
  429. s->rs += 6;
  430. *(s->rs) = neigh->nud_state & NUD_CONNECTED ? 0x80 : 0x0;
  431. *(s->rs) |= dn->priority;
  432. s->rs++;
  433. }
  434. int dn_neigh_elist(struct net_device *dev, unsigned char *ptr, int n)
  435. {
  436. struct elist_cb_state state;
  437. state.dev = dev;
  438. state.t = 0;
  439. state.n = n;
  440. state.ptr = ptr;
  441. state.rs = ptr;
  442. neigh_for_each(&dn_neigh_table, neigh_elist_cb, &state);
  443. return state.t;
  444. }
  445. #ifdef CONFIG_PROC_FS
  446. static inline void dn_neigh_format_entry(struct seq_file *seq,
  447. struct neighbour *n)
  448. {
  449. struct dn_neigh *dn = (struct dn_neigh *) n;
  450. char buf[DN_ASCBUF_LEN];
  451. read_lock(&n->lock);
  452. seq_printf(seq, "%-7s %s%s%s %02x %02d %07ld %-8s\n",
  453. dn_addr2asc(dn_ntohs(dn->addr), buf),
  454. (dn->flags&DN_NDFLAG_R1) ? "1" : "-",
  455. (dn->flags&DN_NDFLAG_R2) ? "2" : "-",
  456. (dn->flags&DN_NDFLAG_P3) ? "3" : "-",
  457. dn->n.nud_state,
  458. atomic_read(&dn->n.refcnt),
  459. dn->blksize,
  460. (dn->n.dev) ? dn->n.dev->name : "?");
  461. read_unlock(&n->lock);
  462. }
  463. static int dn_neigh_seq_show(struct seq_file *seq, void *v)
  464. {
  465. if (v == SEQ_START_TOKEN) {
  466. seq_puts(seq, "Addr Flags State Use Blksize Dev\n");
  467. } else {
  468. dn_neigh_format_entry(seq, v);
  469. }
  470. return 0;
  471. }
  472. static void *dn_neigh_seq_start(struct seq_file *seq, loff_t *pos)
  473. {
  474. return neigh_seq_start(seq, pos, &dn_neigh_table,
  475. NEIGH_SEQ_NEIGH_ONLY);
  476. }
  477. static struct seq_operations dn_neigh_seq_ops = {
  478. .start = dn_neigh_seq_start,
  479. .next = neigh_seq_next,
  480. .stop = neigh_seq_stop,
  481. .show = dn_neigh_seq_show,
  482. };
  483. static int dn_neigh_seq_open(struct inode *inode, struct file *file)
  484. {
  485. struct seq_file *seq;
  486. int rc = -ENOMEM;
  487. struct neigh_seq_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  488. if (!s)
  489. goto out;
  490. rc = seq_open(file, &dn_neigh_seq_ops);
  491. if (rc)
  492. goto out_kfree;
  493. seq = file->private_data;
  494. seq->private = s;
  495. memset(s, 0, sizeof(*s));
  496. out:
  497. return rc;
  498. out_kfree:
  499. kfree(s);
  500. goto out;
  501. }
  502. static struct file_operations dn_neigh_seq_fops = {
  503. .owner = THIS_MODULE,
  504. .open = dn_neigh_seq_open,
  505. .read = seq_read,
  506. .llseek = seq_lseek,
  507. .release = seq_release_private,
  508. };
  509. #endif
  510. void __init dn_neigh_init(void)
  511. {
  512. neigh_table_init(&dn_neigh_table);
  513. proc_net_fops_create("decnet_neigh", S_IRUGO, &dn_neigh_seq_fops);
  514. }
  515. void __exit dn_neigh_cleanup(void)
  516. {
  517. proc_net_remove("decnet_neigh");
  518. neigh_table_clear(&dn_neigh_table);
  519. }