dn_rules.c 5.3 KB

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