dn_rules.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. static struct fib_rules_ops dn_fib_rules_ops;
  34. struct dn_fib_rule
  35. {
  36. struct fib_rule common;
  37. unsigned char dst_len;
  38. unsigned char src_len;
  39. __le16 src;
  40. __le16 srcmask;
  41. __le16 dst;
  42. __le16 dstmask;
  43. __le16 srcmap;
  44. u8 flags;
  45. };
  46. static struct dn_fib_rule default_rule = {
  47. .common = {
  48. .refcnt = ATOMIC_INIT(2),
  49. .pref = 0x7fff,
  50. .table = RT_TABLE_MAIN,
  51. .action = FR_ACT_TO_TBL,
  52. },
  53. };
  54. static LIST_HEAD(dn_fib_rules);
  55. int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
  56. {
  57. struct fib_lookup_arg arg = {
  58. .result = res,
  59. };
  60. int err;
  61. err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
  62. res->r = arg.rule;
  63. return err;
  64. }
  65. static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  66. int flags, struct fib_lookup_arg *arg)
  67. {
  68. int err = -EAGAIN;
  69. struct dn_fib_table *tbl;
  70. switch(rule->action) {
  71. case FR_ACT_TO_TBL:
  72. break;
  73. case FR_ACT_UNREACHABLE:
  74. err = -ENETUNREACH;
  75. goto errout;
  76. case FR_ACT_PROHIBIT:
  77. err = -EACCES;
  78. goto errout;
  79. case FR_ACT_BLACKHOLE:
  80. default:
  81. err = -EINVAL;
  82. goto errout;
  83. }
  84. tbl = dn_fib_get_table(rule->table, 0);
  85. if (tbl == NULL)
  86. goto errout;
  87. err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
  88. if (err > 0)
  89. err = -EAGAIN;
  90. errout:
  91. return err;
  92. }
  93. static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
  94. FRA_GENERIC_POLICY,
  95. [FRA_SRC] = { .type = NLA_U16 },
  96. [FRA_DST] = { .type = NLA_U16 },
  97. };
  98. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  99. {
  100. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  101. __le16 daddr = fl->fld_dst;
  102. __le16 saddr = fl->fld_src;
  103. if (((saddr ^ r->src) & r->srcmask) ||
  104. ((daddr ^ r->dst) & r->dstmask))
  105. return 0;
  106. return 1;
  107. }
  108. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  109. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  110. struct nlattr **tb)
  111. {
  112. int err = -EINVAL;
  113. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  114. if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
  115. goto errout;
  116. if (rule->table == RT_TABLE_UNSPEC) {
  117. if (rule->action == FR_ACT_TO_TBL) {
  118. struct dn_fib_table *table;
  119. table = dn_fib_empty_table();
  120. if (table == NULL) {
  121. err = -ENOBUFS;
  122. goto errout;
  123. }
  124. rule->table = table->n;
  125. }
  126. }
  127. if (tb[FRA_SRC])
  128. r->src = nla_get_u16(tb[FRA_SRC]);
  129. if (tb[FRA_DST])
  130. r->dst = nla_get_u16(tb[FRA_DST]);
  131. r->src_len = frh->src_len;
  132. r->srcmask = dnet_make_mask(r->src_len);
  133. r->dst_len = frh->dst_len;
  134. r->dstmask = dnet_make_mask(r->dst_len);
  135. err = 0;
  136. errout:
  137. return err;
  138. }
  139. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  140. struct nlattr **tb)
  141. {
  142. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  143. if (frh->src_len && (r->src_len != frh->src_len))
  144. return 0;
  145. if (frh->dst_len && (r->dst_len != frh->dst_len))
  146. return 0;
  147. if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
  148. return 0;
  149. if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
  150. return 0;
  151. return 1;
  152. }
  153. unsigned dnet_addr_type(__le16 addr)
  154. {
  155. struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
  156. struct dn_fib_res res;
  157. unsigned ret = RTN_UNICAST;
  158. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  159. res.r = NULL;
  160. if (tb) {
  161. if (!tb->lookup(tb, &fl, &res)) {
  162. ret = res.type;
  163. dn_fib_res_put(&res);
  164. }
  165. }
  166. return ret;
  167. }
  168. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  169. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  170. {
  171. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  172. frh->family = AF_DECnet;
  173. frh->dst_len = r->dst_len;
  174. frh->src_len = r->src_len;
  175. frh->tos = 0;
  176. if (r->dst_len)
  177. NLA_PUT_U16(skb, FRA_DST, r->dst);
  178. if (r->src_len)
  179. NLA_PUT_U16(skb, FRA_SRC, r->src);
  180. return 0;
  181. nla_put_failure:
  182. return -ENOBUFS;
  183. }
  184. static u32 dn_fib_rule_default_pref(void)
  185. {
  186. struct list_head *pos;
  187. struct fib_rule *rule;
  188. if (!list_empty(&dn_fib_rules)) {
  189. pos = dn_fib_rules.next;
  190. if (pos->next != &dn_fib_rules) {
  191. rule = list_entry(pos->next, struct fib_rule, list);
  192. if (rule->pref)
  193. return rule->pref - 1;
  194. }
  195. }
  196. return 0;
  197. }
  198. int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
  199. {
  200. return fib_rules_dump(skb, cb, AF_DECnet);
  201. }
  202. static struct fib_rules_ops dn_fib_rules_ops = {
  203. .family = AF_DECnet,
  204. .rule_size = sizeof(struct dn_fib_rule),
  205. .action = dn_fib_rule_action,
  206. .match = dn_fib_rule_match,
  207. .configure = dn_fib_rule_configure,
  208. .compare = dn_fib_rule_compare,
  209. .fill = dn_fib_rule_fill,
  210. .default_pref = dn_fib_rule_default_pref,
  211. .nlgroup = RTNLGRP_DECnet_RULE,
  212. .policy = dn_fib_rule_policy,
  213. .rules_list = &dn_fib_rules,
  214. .owner = THIS_MODULE,
  215. };
  216. void __init dn_fib_rules_init(void)
  217. {
  218. list_add_tail(&default_rule.common.list, &dn_fib_rules);
  219. fib_rules_register(&dn_fib_rules_ops);
  220. }
  221. void __exit dn_fib_rules_cleanup(void)
  222. {
  223. fib_rules_unregister(&dn_fib_rules_ops);
  224. }