dn_rules.c 5.4 KB

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