dn_neigh.c 15 KB

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