dn_rules.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 Forwarding Information Base (Rules)
  7. *
  8. * Author: Steve Whitehouse <SteveW@ACM.org>
  9. * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
  10. *
  11. *
  12. * Changes:
  13. * Steve Whitehouse <steve@chygwyn.com>
  14. * Updated for Thomas Graf's generic rules
  15. *
  16. */
  17. #include <linux/net.h>
  18. #include <linux/init.h>
  19. #include <linux/netlink.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/list.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/export.h>
  26. #include <net/neighbour.h>
  27. #include <net/dst.h>
  28. #include <net/flow.h>
  29. #include <net/fib_rules.h>
  30. #include <net/dn.h>
  31. #include <net/dn_fib.h>
  32. #include <net/dn_neigh.h>
  33. #include <net/dn_dev.h>
  34. #include <net/dn_route.h>
  35. static struct fib_rules_ops *dn_fib_rules_ops;
  36. struct dn_fib_rule
  37. {
  38. struct fib_rule common;
  39. unsigned char dst_len;
  40. unsigned char src_len;
  41. __le16 src;
  42. __le16 srcmask;
  43. __le16 dst;
  44. __le16 dstmask;
  45. __le16 srcmap;
  46. u8 flags;
  47. };
  48. int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
  49. {
  50. struct fib_lookup_arg arg = {
  51. .result = res,
  52. };
  53. int err;
  54. err = fib_rules_lookup(dn_fib_rules_ops,
  55. flowidn_to_flowi(flp), 0, &arg);
  56. res->r = arg.rule;
  57. return err;
  58. }
  59. static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  60. int flags, struct fib_lookup_arg *arg)
  61. {
  62. struct flowidn *fld = &flp->u.dn;
  63. int err = -EAGAIN;
  64. struct dn_fib_table *tbl;
  65. switch(rule->action) {
  66. case FR_ACT_TO_TBL:
  67. break;
  68. case FR_ACT_UNREACHABLE:
  69. err = -ENETUNREACH;
  70. goto errout;
  71. case FR_ACT_PROHIBIT:
  72. err = -EACCES;
  73. goto errout;
  74. case FR_ACT_BLACKHOLE:
  75. default:
  76. err = -EINVAL;
  77. goto errout;
  78. }
  79. tbl = dn_fib_get_table(rule->table, 0);
  80. if (tbl == NULL)
  81. goto errout;
  82. err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
  83. if (err > 0)
  84. err = -EAGAIN;
  85. errout:
  86. return err;
  87. }
  88. static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
  89. FRA_GENERIC_POLICY,
  90. };
  91. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  92. {
  93. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  94. struct flowidn *fld = &fl->u.dn;
  95. __le16 daddr = fld->daddr;
  96. __le16 saddr = fld->saddr;
  97. if (((saddr ^ r->src) & r->srcmask) ||
  98. ((daddr ^ r->dst) & r->dstmask))
  99. return 0;
  100. return 1;
  101. }
  102. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  103. struct fib_rule_hdr *frh,
  104. struct nlattr **tb)
  105. {
  106. int err = -EINVAL;
  107. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  108. if (frh->tos)
  109. goto errout;
  110. if (rule->table == RT_TABLE_UNSPEC) {
  111. if (rule->action == FR_ACT_TO_TBL) {
  112. struct dn_fib_table *table;
  113. table = dn_fib_empty_table();
  114. if (table == NULL) {
  115. err = -ENOBUFS;
  116. goto errout;
  117. }
  118. rule->table = table->n;
  119. }
  120. }
  121. if (frh->src_len)
  122. r->src = nla_get_le16(tb[FRA_SRC]);
  123. if (frh->dst_len)
  124. r->dst = nla_get_le16(tb[FRA_DST]);
  125. r->src_len = frh->src_len;
  126. r->srcmask = dnet_make_mask(r->src_len);
  127. r->dst_len = frh->dst_len;
  128. r->dstmask = dnet_make_mask(r->dst_len);
  129. err = 0;
  130. errout:
  131. return err;
  132. }
  133. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  134. struct nlattr **tb)
  135. {
  136. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  137. if (frh->src_len && (r->src_len != frh->src_len))
  138. return 0;
  139. if (frh->dst_len && (r->dst_len != frh->dst_len))
  140. return 0;
  141. if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
  142. return 0;
  143. if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
  144. return 0;
  145. return 1;
  146. }
  147. unsigned int dnet_addr_type(__le16 addr)
  148. {
  149. struct flowidn fld = { .daddr = addr };
  150. struct dn_fib_res res;
  151. unsigned int ret = RTN_UNICAST;
  152. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  153. res.r = NULL;
  154. if (tb) {
  155. if (!tb->lookup(tb, &fld, &res)) {
  156. ret = res.type;
  157. dn_fib_res_put(&res);
  158. }
  159. }
  160. return ret;
  161. }
  162. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  163. struct fib_rule_hdr *frh)
  164. {
  165. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  166. frh->dst_len = r->dst_len;
  167. frh->src_len = r->src_len;
  168. frh->tos = 0;
  169. if ((r->dst_len &&
  170. nla_put_le16(skb, FRA_DST, r->dst)) ||
  171. (r->src_len &&
  172. nla_put_le16(skb, FRA_SRC, r->src)))
  173. goto nla_put_failure;
  174. return 0;
  175. nla_put_failure:
  176. return -ENOBUFS;
  177. }
  178. static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
  179. {
  180. dn_rt_cache_flush(-1);
  181. }
  182. static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
  183. .family = AF_DECnet,
  184. .rule_size = sizeof(struct dn_fib_rule),
  185. .addr_size = sizeof(u16),
  186. .action = dn_fib_rule_action,
  187. .match = dn_fib_rule_match,
  188. .configure = dn_fib_rule_configure,
  189. .compare = dn_fib_rule_compare,
  190. .fill = dn_fib_rule_fill,
  191. .default_pref = fib_default_rule_pref,
  192. .flush_cache = dn_fib_rule_flush_cache,
  193. .nlgroup = RTNLGRP_DECnet_RULE,
  194. .policy = dn_fib_rule_policy,
  195. .owner = THIS_MODULE,
  196. .fro_net = &init_net,
  197. };
  198. void __init dn_fib_rules_init(void)
  199. {
  200. dn_fib_rules_ops =
  201. fib_rules_register(&dn_fib_rules_ops_template, &init_net);
  202. BUG_ON(IS_ERR(dn_fib_rules_ops));
  203. BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
  204. RT_TABLE_MAIN, 0));
  205. }
  206. void __exit dn_fib_rules_cleanup(void)
  207. {
  208. rtnl_lock();
  209. fib_rules_unregister(dn_fib_rules_ops);
  210. rtnl_unlock();
  211. rcu_barrier();
  212. }