fib_rules.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: policy rules.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. * Thomas Graf <tgraf@suug.ch>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Fixes:
  17. * Rani Assaf : local_rule cannot be deleted
  18. * Marc Boucher : routing by fwmark
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/netlink.h>
  24. #include <linux/inetdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/rcupdate.h>
  28. #include <net/ip.h>
  29. #include <net/route.h>
  30. #include <net/tcp.h>
  31. #include <net/ip_fib.h>
  32. #include <net/fib_rules.h>
  33. struct fib4_rule
  34. {
  35. struct fib_rule common;
  36. u8 dst_len;
  37. u8 src_len;
  38. u8 tos;
  39. __be32 src;
  40. __be32 srcmask;
  41. __be32 dst;
  42. __be32 dstmask;
  43. #ifdef CONFIG_NET_CLS_ROUTE
  44. u32 tclassid;
  45. #endif
  46. };
  47. #ifdef CONFIG_NET_CLS_ROUTE
  48. u32 fib_rules_tclass(struct fib_result *res)
  49. {
  50. return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
  51. }
  52. #endif
  53. int fib_lookup(struct net *net, struct flowi *flp, struct fib_result *res)
  54. {
  55. struct fib_lookup_arg arg = {
  56. .result = res,
  57. };
  58. int err;
  59. err = fib_rules_lookup(net->ipv4.rules_ops, flp, 0, &arg);
  60. res->r = arg.rule;
  61. return err;
  62. }
  63. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  64. int flags, struct fib_lookup_arg *arg)
  65. {
  66. int err = -EAGAIN;
  67. struct fib_table *tbl;
  68. switch (rule->action) {
  69. case FR_ACT_TO_TBL:
  70. break;
  71. case FR_ACT_UNREACHABLE:
  72. err = -ENETUNREACH;
  73. goto errout;
  74. case FR_ACT_PROHIBIT:
  75. err = -EACCES;
  76. goto errout;
  77. case FR_ACT_BLACKHOLE:
  78. default:
  79. err = -EINVAL;
  80. goto errout;
  81. }
  82. if ((tbl = fib_get_table(rule->fr_net, rule->table)) == NULL)
  83. goto errout;
  84. err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
  85. if (err > 0)
  86. err = -EAGAIN;
  87. errout:
  88. return err;
  89. }
  90. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  91. {
  92. struct fib4_rule *r = (struct fib4_rule *) rule;
  93. __be32 daddr = fl->fl4_dst;
  94. __be32 saddr = fl->fl4_src;
  95. if (((saddr ^ r->src) & r->srcmask) ||
  96. ((daddr ^ r->dst) & r->dstmask))
  97. return 0;
  98. if (r->tos && (r->tos != fl->fl4_tos))
  99. return 0;
  100. return 1;
  101. }
  102. static struct fib_table *fib_empty_table(struct net *net)
  103. {
  104. u32 id;
  105. for (id = 1; id <= RT_TABLE_MAX; id++)
  106. if (fib_get_table(net, id) == NULL)
  107. return fib_new_table(net, id);
  108. return NULL;
  109. }
  110. static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
  111. FRA_GENERIC_POLICY,
  112. [FRA_FLOW] = { .type = NLA_U32 },
  113. };
  114. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  115. struct fib_rule_hdr *frh,
  116. struct nlattr **tb)
  117. {
  118. struct net *net = sock_net(skb->sk);
  119. int err = -EINVAL;
  120. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  121. if (frh->tos & ~IPTOS_TOS_MASK)
  122. goto errout;
  123. if (rule->table == RT_TABLE_UNSPEC) {
  124. if (rule->action == FR_ACT_TO_TBL) {
  125. struct fib_table *table;
  126. table = fib_empty_table(net);
  127. if (table == NULL) {
  128. err = -ENOBUFS;
  129. goto errout;
  130. }
  131. rule->table = table->tb_id;
  132. }
  133. }
  134. if (frh->src_len)
  135. rule4->src = nla_get_be32(tb[FRA_SRC]);
  136. if (frh->dst_len)
  137. rule4->dst = nla_get_be32(tb[FRA_DST]);
  138. #ifdef CONFIG_NET_CLS_ROUTE
  139. if (tb[FRA_FLOW])
  140. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  141. #endif
  142. rule4->src_len = frh->src_len;
  143. rule4->srcmask = inet_make_mask(rule4->src_len);
  144. rule4->dst_len = frh->dst_len;
  145. rule4->dstmask = inet_make_mask(rule4->dst_len);
  146. rule4->tos = frh->tos;
  147. err = 0;
  148. errout:
  149. return err;
  150. }
  151. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  152. struct nlattr **tb)
  153. {
  154. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  155. if (frh->src_len && (rule4->src_len != frh->src_len))
  156. return 0;
  157. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  158. return 0;
  159. if (frh->tos && (rule4->tos != frh->tos))
  160. return 0;
  161. #ifdef CONFIG_NET_CLS_ROUTE
  162. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  163. return 0;
  164. #endif
  165. if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  166. return 0;
  167. if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  168. return 0;
  169. return 1;
  170. }
  171. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  172. struct fib_rule_hdr *frh)
  173. {
  174. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  175. frh->family = AF_INET;
  176. frh->dst_len = rule4->dst_len;
  177. frh->src_len = rule4->src_len;
  178. frh->tos = rule4->tos;
  179. if (rule4->dst_len)
  180. NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
  181. if (rule4->src_len)
  182. NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
  183. #ifdef CONFIG_NET_CLS_ROUTE
  184. if (rule4->tclassid)
  185. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  186. #endif
  187. return 0;
  188. nla_put_failure:
  189. return -ENOBUFS;
  190. }
  191. static u32 fib4_rule_default_pref(struct fib_rules_ops *ops)
  192. {
  193. struct list_head *pos;
  194. struct fib_rule *rule;
  195. if (!list_empty(&ops->rules_list)) {
  196. pos = ops->rules_list.next;
  197. if (pos->next != &ops->rules_list) {
  198. rule = list_entry(pos->next, struct fib_rule, list);
  199. if (rule->pref)
  200. return rule->pref - 1;
  201. }
  202. }
  203. return 0;
  204. }
  205. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  206. {
  207. return nla_total_size(4) /* dst */
  208. + nla_total_size(4) /* src */
  209. + nla_total_size(4); /* flow */
  210. }
  211. static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
  212. {
  213. rt_cache_flush(ops->fro_net, -1);
  214. }
  215. static struct fib_rules_ops fib4_rules_ops_template = {
  216. .family = AF_INET,
  217. .rule_size = sizeof(struct fib4_rule),
  218. .addr_size = sizeof(u32),
  219. .action = fib4_rule_action,
  220. .match = fib4_rule_match,
  221. .configure = fib4_rule_configure,
  222. .compare = fib4_rule_compare,
  223. .fill = fib4_rule_fill,
  224. .default_pref = fib4_rule_default_pref,
  225. .nlmsg_payload = fib4_rule_nlmsg_payload,
  226. .flush_cache = fib4_rule_flush_cache,
  227. .nlgroup = RTNLGRP_IPV4_RULE,
  228. .policy = fib4_rule_policy,
  229. .owner = THIS_MODULE,
  230. };
  231. static int fib_default_rules_init(struct fib_rules_ops *ops)
  232. {
  233. int err;
  234. err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, FIB_RULE_PERMANENT);
  235. if (err < 0)
  236. return err;
  237. err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
  238. if (err < 0)
  239. return err;
  240. err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
  241. if (err < 0)
  242. return err;
  243. return 0;
  244. }
  245. int __net_init fib4_rules_init(struct net *net)
  246. {
  247. int err;
  248. struct fib_rules_ops *ops;
  249. ops = kmemdup(&fib4_rules_ops_template, sizeof(*ops), GFP_KERNEL);
  250. if (ops == NULL)
  251. return -ENOMEM;
  252. INIT_LIST_HEAD(&ops->rules_list);
  253. ops->fro_net = net;
  254. fib_rules_register(ops);
  255. err = fib_default_rules_init(ops);
  256. if (err < 0)
  257. goto fail;
  258. net->ipv4.rules_ops = ops;
  259. return 0;
  260. fail:
  261. /* also cleans all rules already added */
  262. fib_rules_unregister(ops);
  263. kfree(ops);
  264. return err;
  265. }
  266. void __net_exit fib4_rules_exit(struct net *net)
  267. {
  268. fib_rules_unregister(net->ipv4.rules_ops);
  269. kfree(net->ipv4.rules_ops);
  270. }