fib_rules.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 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(init_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(&init_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. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  91. {
  92. if (res->r && res->r->action == FR_ACT_TO_TBL &&
  93. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  94. struct fib_table *tb;
  95. if ((tb = fib_get_table(&init_net, res->r->table)) != NULL)
  96. tb->tb_select_default(tb, flp, res);
  97. }
  98. }
  99. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  100. {
  101. struct fib4_rule *r = (struct fib4_rule *) rule;
  102. __be32 daddr = fl->fl4_dst;
  103. __be32 saddr = fl->fl4_src;
  104. if (((saddr ^ r->src) & r->srcmask) ||
  105. ((daddr ^ r->dst) & r->dstmask))
  106. return 0;
  107. if (r->tos && (r->tos != fl->fl4_tos))
  108. return 0;
  109. return 1;
  110. }
  111. static struct fib_table *fib_empty_table(struct net *net)
  112. {
  113. u32 id;
  114. for (id = 1; id <= RT_TABLE_MAX; id++)
  115. if (fib_get_table(net, id) == NULL)
  116. return fib_new_table(net, id);
  117. return NULL;
  118. }
  119. static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
  120. FRA_GENERIC_POLICY,
  121. [FRA_FLOW] = { .type = NLA_U32 },
  122. };
  123. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  124. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  125. struct nlattr **tb)
  126. {
  127. struct net *net = skb->sk->sk_net;
  128. int err = -EINVAL;
  129. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  130. if (frh->tos & ~IPTOS_TOS_MASK)
  131. goto errout;
  132. if (rule->table == RT_TABLE_UNSPEC) {
  133. if (rule->action == FR_ACT_TO_TBL) {
  134. struct fib_table *table;
  135. table = fib_empty_table(net);
  136. if (table == NULL) {
  137. err = -ENOBUFS;
  138. goto errout;
  139. }
  140. rule->table = table->tb_id;
  141. }
  142. }
  143. if (frh->src_len)
  144. rule4->src = nla_get_be32(tb[FRA_SRC]);
  145. if (frh->dst_len)
  146. rule4->dst = nla_get_be32(tb[FRA_DST]);
  147. #ifdef CONFIG_NET_CLS_ROUTE
  148. if (tb[FRA_FLOW])
  149. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  150. #endif
  151. rule4->src_len = frh->src_len;
  152. rule4->srcmask = inet_make_mask(rule4->src_len);
  153. rule4->dst_len = frh->dst_len;
  154. rule4->dstmask = inet_make_mask(rule4->dst_len);
  155. rule4->tos = frh->tos;
  156. err = 0;
  157. errout:
  158. return err;
  159. }
  160. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  161. struct nlattr **tb)
  162. {
  163. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  164. if (frh->src_len && (rule4->src_len != frh->src_len))
  165. return 0;
  166. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  167. return 0;
  168. if (frh->tos && (rule4->tos != frh->tos))
  169. return 0;
  170. #ifdef CONFIG_NET_CLS_ROUTE
  171. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  172. return 0;
  173. #endif
  174. if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  175. return 0;
  176. if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  177. return 0;
  178. return 1;
  179. }
  180. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  181. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  182. {
  183. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  184. frh->family = AF_INET;
  185. frh->dst_len = rule4->dst_len;
  186. frh->src_len = rule4->src_len;
  187. frh->tos = rule4->tos;
  188. if (rule4->dst_len)
  189. NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
  190. if (rule4->src_len)
  191. NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
  192. #ifdef CONFIG_NET_CLS_ROUTE
  193. if (rule4->tclassid)
  194. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  195. #endif
  196. return 0;
  197. nla_put_failure:
  198. return -ENOBUFS;
  199. }
  200. static u32 fib4_rule_default_pref(struct fib_rules_ops *ops)
  201. {
  202. struct list_head *pos;
  203. struct fib_rule *rule;
  204. if (!list_empty(&ops->rules_list)) {
  205. pos = ops->rules_list.next;
  206. if (pos->next != &ops->rules_list) {
  207. rule = list_entry(pos->next, struct fib_rule, list);
  208. if (rule->pref)
  209. return rule->pref - 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  215. {
  216. return nla_total_size(4) /* dst */
  217. + nla_total_size(4) /* src */
  218. + nla_total_size(4); /* flow */
  219. }
  220. static void fib4_rule_flush_cache(void)
  221. {
  222. rt_cache_flush(-1);
  223. }
  224. static struct fib_rules_ops fib4_rules_ops_template = {
  225. .family = AF_INET,
  226. .rule_size = sizeof(struct fib4_rule),
  227. .addr_size = sizeof(u32),
  228. .action = fib4_rule_action,
  229. .match = fib4_rule_match,
  230. .configure = fib4_rule_configure,
  231. .compare = fib4_rule_compare,
  232. .fill = fib4_rule_fill,
  233. .default_pref = fib4_rule_default_pref,
  234. .nlmsg_payload = fib4_rule_nlmsg_payload,
  235. .flush_cache = fib4_rule_flush_cache,
  236. .nlgroup = RTNLGRP_IPV4_RULE,
  237. .policy = fib4_rule_policy,
  238. .owner = THIS_MODULE,
  239. };
  240. static int fib_default_rules_init(struct fib_rules_ops *ops)
  241. {
  242. int err;
  243. err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, FIB_RULE_PERMANENT);
  244. if (err < 0)
  245. return err;
  246. err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
  247. if (err < 0)
  248. return err;
  249. err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
  250. if (err < 0)
  251. return err;
  252. return 0;
  253. }
  254. int __net_init fib4_rules_init(struct net *net)
  255. {
  256. int err;
  257. struct fib_rules_ops *ops;
  258. ops = kmemdup(&fib4_rules_ops_template, sizeof(*ops), GFP_KERNEL);
  259. if (ops == NULL)
  260. return -ENOMEM;
  261. INIT_LIST_HEAD(&ops->rules_list);
  262. ops->fro_net = net;
  263. fib_rules_register(ops);
  264. err = fib_default_rules_init(ops);
  265. if (err < 0)
  266. goto fail;
  267. net->ipv4.rules_ops = ops;
  268. return 0;
  269. fail:
  270. /* also cleans all rules already added */
  271. fib_rules_unregister(ops);
  272. kfree(ops);
  273. return err;
  274. }
  275. void __net_exit fib4_rules_exit(struct net *net)
  276. {
  277. fib_rules_unregister(net->ipv4.rules_ops);
  278. kfree(net->ipv4.rules_ops);
  279. }