dn_rules.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
  95. [FRA_PRIORITY] = { .type = NLA_U32 },
  96. [FRA_SRC] = { .type = NLA_U16 },
  97. [FRA_DST] = { .type = NLA_U16 },
  98. [FRA_FWMARK] = { .type = NLA_U32 },
  99. [FRA_FWMASK] = { .type = NLA_U32 },
  100. [FRA_TABLE] = { .type = NLA_U32 },
  101. };
  102. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  103. {
  104. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  105. __le16 daddr = fl->fld_dst;
  106. __le16 saddr = fl->fld_src;
  107. if (((saddr ^ r->src) & r->srcmask) ||
  108. ((daddr ^ r->dst) & r->dstmask))
  109. return 0;
  110. return 1;
  111. }
  112. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  113. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  114. struct nlattr **tb)
  115. {
  116. int err = -EINVAL;
  117. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  118. if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
  119. goto errout;
  120. if (rule->table == RT_TABLE_UNSPEC) {
  121. if (rule->action == FR_ACT_TO_TBL) {
  122. struct dn_fib_table *table;
  123. table = dn_fib_empty_table();
  124. if (table == NULL) {
  125. err = -ENOBUFS;
  126. goto errout;
  127. }
  128. rule->table = table->n;
  129. }
  130. }
  131. if (tb[FRA_SRC])
  132. r->src = nla_get_u16(tb[FRA_SRC]);
  133. if (tb[FRA_DST])
  134. r->dst = nla_get_u16(tb[FRA_DST]);
  135. r->src_len = frh->src_len;
  136. r->srcmask = dnet_make_mask(r->src_len);
  137. r->dst_len = frh->dst_len;
  138. r->dstmask = dnet_make_mask(r->dst_len);
  139. err = 0;
  140. errout:
  141. return err;
  142. }
  143. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  144. struct nlattr **tb)
  145. {
  146. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  147. if (frh->src_len && (r->src_len != frh->src_len))
  148. return 0;
  149. if (frh->dst_len && (r->dst_len != frh->dst_len))
  150. return 0;
  151. if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
  152. return 0;
  153. if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
  154. return 0;
  155. return 1;
  156. }
  157. unsigned dnet_addr_type(__le16 addr)
  158. {
  159. struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
  160. struct dn_fib_res res;
  161. unsigned ret = RTN_UNICAST;
  162. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  163. res.r = NULL;
  164. if (tb) {
  165. if (!tb->lookup(tb, &fl, &res)) {
  166. ret = res.type;
  167. dn_fib_res_put(&res);
  168. }
  169. }
  170. return ret;
  171. }
  172. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  173. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  174. {
  175. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  176. frh->family = AF_DECnet;
  177. frh->dst_len = r->dst_len;
  178. frh->src_len = r->src_len;
  179. frh->tos = 0;
  180. if (r->dst_len)
  181. NLA_PUT_U16(skb, FRA_DST, r->dst);
  182. if (r->src_len)
  183. NLA_PUT_U16(skb, FRA_SRC, r->src);
  184. return 0;
  185. nla_put_failure:
  186. return -ENOBUFS;
  187. }
  188. static u32 dn_fib_rule_default_pref(void)
  189. {
  190. struct list_head *pos;
  191. struct fib_rule *rule;
  192. if (!list_empty(&dn_fib_rules)) {
  193. pos = dn_fib_rules.next;
  194. if (pos->next != &dn_fib_rules) {
  195. rule = list_entry(pos->next, struct fib_rule, list);
  196. if (rule->pref)
  197. return rule->pref - 1;
  198. }
  199. }
  200. return 0;
  201. }
  202. int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
  203. {
  204. return fib_rules_dump(skb, cb, AF_DECnet);
  205. }
  206. static struct fib_rules_ops dn_fib_rules_ops = {
  207. .family = AF_DECnet,
  208. .rule_size = sizeof(struct dn_fib_rule),
  209. .action = dn_fib_rule_action,
  210. .match = dn_fib_rule_match,
  211. .configure = dn_fib_rule_configure,
  212. .compare = dn_fib_rule_compare,
  213. .fill = dn_fib_rule_fill,
  214. .default_pref = dn_fib_rule_default_pref,
  215. .nlgroup = RTNLGRP_DECnet_RULE,
  216. .policy = dn_fib_rule_policy,
  217. .rules_list = &dn_fib_rules,
  218. .owner = THIS_MODULE,
  219. };
  220. void __init dn_fib_rules_init(void)
  221. {
  222. list_add_tail(&default_rule.common.list, &dn_fib_rules);
  223. fib_rules_register(&dn_fib_rules_ops);
  224. }
  225. void __exit dn_fib_rules_cleanup(void)
  226. {
  227. fib_rules_unregister(&dn_fib_rules_ops);
  228. }