fib_rules.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * net/core/fib_rules.c Generic Routing Rules
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation, version 2.
  7. *
  8. * Authors: Thomas Graf <tgraf@suug.ch>
  9. */
  10. #include <linux/config.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <net/fib_rules.h>
  15. static LIST_HEAD(rules_ops);
  16. static DEFINE_SPINLOCK(rules_mod_lock);
  17. static void notify_rule_change(int event, struct fib_rule *rule,
  18. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  19. u32 pid);
  20. static struct fib_rules_ops *lookup_rules_ops(int family)
  21. {
  22. struct fib_rules_ops *ops;
  23. rcu_read_lock();
  24. list_for_each_entry_rcu(ops, &rules_ops, list) {
  25. if (ops->family == family) {
  26. if (!try_module_get(ops->owner))
  27. ops = NULL;
  28. rcu_read_unlock();
  29. return ops;
  30. }
  31. }
  32. rcu_read_unlock();
  33. return NULL;
  34. }
  35. static void rules_ops_put(struct fib_rules_ops *ops)
  36. {
  37. if (ops)
  38. module_put(ops->owner);
  39. }
  40. int fib_rules_register(struct fib_rules_ops *ops)
  41. {
  42. int err = -EEXIST;
  43. struct fib_rules_ops *o;
  44. if (ops->rule_size < sizeof(struct fib_rule))
  45. return -EINVAL;
  46. if (ops->match == NULL || ops->configure == NULL ||
  47. ops->compare == NULL || ops->fill == NULL ||
  48. ops->action == NULL)
  49. return -EINVAL;
  50. spin_lock(&rules_mod_lock);
  51. list_for_each_entry(o, &rules_ops, list)
  52. if (ops->family == o->family)
  53. goto errout;
  54. list_add_tail_rcu(&ops->list, &rules_ops);
  55. err = 0;
  56. errout:
  57. spin_unlock(&rules_mod_lock);
  58. return err;
  59. }
  60. EXPORT_SYMBOL_GPL(fib_rules_register);
  61. static void cleanup_ops(struct fib_rules_ops *ops)
  62. {
  63. struct fib_rule *rule, *tmp;
  64. list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
  65. list_del_rcu(&rule->list);
  66. fib_rule_put(rule);
  67. }
  68. }
  69. int fib_rules_unregister(struct fib_rules_ops *ops)
  70. {
  71. int err = 0;
  72. struct fib_rules_ops *o;
  73. spin_lock(&rules_mod_lock);
  74. list_for_each_entry(o, &rules_ops, list) {
  75. if (o == ops) {
  76. list_del_rcu(&o->list);
  77. cleanup_ops(ops);
  78. goto out;
  79. }
  80. }
  81. err = -ENOENT;
  82. out:
  83. spin_unlock(&rules_mod_lock);
  84. synchronize_rcu();
  85. return err;
  86. }
  87. EXPORT_SYMBOL_GPL(fib_rules_unregister);
  88. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  89. int flags, struct fib_lookup_arg *arg)
  90. {
  91. struct fib_rule *rule;
  92. int err;
  93. rcu_read_lock();
  94. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  95. if (rule->ifindex && (rule->ifindex != fl->iif))
  96. continue;
  97. if (!ops->match(rule, fl, flags))
  98. continue;
  99. err = ops->action(rule, fl, flags, arg);
  100. if (err != -EAGAIN) {
  101. fib_rule_get(rule);
  102. arg->rule = rule;
  103. goto out;
  104. }
  105. }
  106. err = -ENETUNREACH;
  107. out:
  108. rcu_read_unlock();
  109. return err;
  110. }
  111. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  112. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  113. {
  114. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  115. struct fib_rules_ops *ops = NULL;
  116. struct fib_rule *rule, *r, *last = NULL;
  117. struct nlattr *tb[FRA_MAX+1];
  118. int err = -EINVAL;
  119. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  120. goto errout;
  121. ops = lookup_rules_ops(frh->family);
  122. if (ops == NULL) {
  123. err = EAFNOSUPPORT;
  124. goto errout;
  125. }
  126. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  127. if (err < 0)
  128. goto errout;
  129. if (tb[FRA_IFNAME] && nla_len(tb[FRA_IFNAME]) > IFNAMSIZ)
  130. goto errout;
  131. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  132. if (rule == NULL) {
  133. err = -ENOMEM;
  134. goto errout;
  135. }
  136. if (tb[FRA_PRIORITY])
  137. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  138. if (tb[FRA_IFNAME]) {
  139. struct net_device *dev;
  140. rule->ifindex = -1;
  141. if (nla_strlcpy(rule->ifname, tb[FRA_IFNAME],
  142. IFNAMSIZ) >= IFNAMSIZ)
  143. goto errout_free;
  144. dev = __dev_get_by_name(rule->ifname);
  145. if (dev)
  146. rule->ifindex = dev->ifindex;
  147. }
  148. rule->action = frh->action;
  149. rule->flags = frh->flags;
  150. rule->table = frh_get_table(frh, tb);
  151. if (!rule->pref && ops->default_pref)
  152. rule->pref = ops->default_pref();
  153. err = ops->configure(rule, skb, nlh, frh, tb);
  154. if (err < 0)
  155. goto errout_free;
  156. list_for_each_entry(r, ops->rules_list, list) {
  157. if (r->pref > rule->pref)
  158. break;
  159. last = r;
  160. }
  161. fib_rule_get(rule);
  162. if (last)
  163. list_add_rcu(&rule->list, &last->list);
  164. else
  165. list_add_rcu(&rule->list, ops->rules_list);
  166. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  167. rules_ops_put(ops);
  168. return 0;
  169. errout_free:
  170. kfree(rule);
  171. errout:
  172. rules_ops_put(ops);
  173. return err;
  174. }
  175. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  176. {
  177. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  178. struct fib_rules_ops *ops = NULL;
  179. struct fib_rule *rule;
  180. struct nlattr *tb[FRA_MAX+1];
  181. int err = -EINVAL;
  182. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  183. goto errout;
  184. ops = lookup_rules_ops(frh->family);
  185. if (ops == NULL) {
  186. err = EAFNOSUPPORT;
  187. goto errout;
  188. }
  189. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  190. if (err < 0)
  191. goto errout;
  192. list_for_each_entry(rule, ops->rules_list, list) {
  193. if (frh->action && (frh->action != rule->action))
  194. continue;
  195. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  196. continue;
  197. if (tb[FRA_PRIORITY] &&
  198. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  199. continue;
  200. if (tb[FRA_IFNAME] &&
  201. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  202. continue;
  203. if (!ops->compare(rule, frh, tb))
  204. continue;
  205. if (rule->flags & FIB_RULE_PERMANENT) {
  206. err = -EPERM;
  207. goto errout;
  208. }
  209. list_del_rcu(&rule->list);
  210. synchronize_rcu();
  211. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  212. NETLINK_CB(skb).pid);
  213. fib_rule_put(rule);
  214. rules_ops_put(ops);
  215. return 0;
  216. }
  217. err = -ENOENT;
  218. errout:
  219. rules_ops_put(ops);
  220. return err;
  221. }
  222. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  223. u32 pid, u32 seq, int type, int flags,
  224. struct fib_rules_ops *ops)
  225. {
  226. struct nlmsghdr *nlh;
  227. struct fib_rule_hdr *frh;
  228. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  229. if (nlh == NULL)
  230. return -1;
  231. frh = nlmsg_data(nlh);
  232. frh->table = rule->table;
  233. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  234. frh->res1 = 0;
  235. frh->res2 = 0;
  236. frh->action = rule->action;
  237. frh->flags = rule->flags;
  238. if (rule->ifname[0])
  239. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  240. if (rule->pref)
  241. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  242. if (ops->fill(rule, skb, nlh, frh) < 0)
  243. goto nla_put_failure;
  244. return nlmsg_end(skb, nlh);
  245. nla_put_failure:
  246. return nlmsg_cancel(skb, nlh);
  247. }
  248. int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
  249. {
  250. int idx = 0;
  251. struct fib_rule *rule;
  252. struct fib_rules_ops *ops;
  253. ops = lookup_rules_ops(family);
  254. if (ops == NULL)
  255. return -EAFNOSUPPORT;
  256. rcu_read_lock();
  257. list_for_each_entry(rule, ops->rules_list, list) {
  258. if (idx < cb->args[0])
  259. goto skip;
  260. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  261. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  262. NLM_F_MULTI, ops) < 0)
  263. break;
  264. skip:
  265. idx++;
  266. }
  267. rcu_read_unlock();
  268. cb->args[0] = idx;
  269. rules_ops_put(ops);
  270. return skb->len;
  271. }
  272. EXPORT_SYMBOL_GPL(fib_rules_dump);
  273. static void notify_rule_change(int event, struct fib_rule *rule,
  274. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  275. u32 pid)
  276. {
  277. struct sk_buff *skb;
  278. int err = -ENOBUFS;
  279. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  280. if (skb == NULL)
  281. goto errout;
  282. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  283. if (err < 0) {
  284. kfree_skb(skb);
  285. goto errout;
  286. }
  287. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  288. errout:
  289. if (err < 0)
  290. rtnl_set_sk_err(ops->nlgroup, err);
  291. }
  292. static void attach_rules(struct list_head *rules, struct net_device *dev)
  293. {
  294. struct fib_rule *rule;
  295. list_for_each_entry(rule, rules, list) {
  296. if (rule->ifindex == -1 &&
  297. strcmp(dev->name, rule->ifname) == 0)
  298. rule->ifindex = dev->ifindex;
  299. }
  300. }
  301. static void detach_rules(struct list_head *rules, struct net_device *dev)
  302. {
  303. struct fib_rule *rule;
  304. list_for_each_entry(rule, rules, list)
  305. if (rule->ifindex == dev->ifindex)
  306. rule->ifindex = -1;
  307. }
  308. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  309. void *ptr)
  310. {
  311. struct net_device *dev = ptr;
  312. struct fib_rules_ops *ops;
  313. ASSERT_RTNL();
  314. rcu_read_lock();
  315. switch (event) {
  316. case NETDEV_REGISTER:
  317. list_for_each_entry(ops, &rules_ops, list)
  318. attach_rules(ops->rules_list, dev);
  319. break;
  320. case NETDEV_UNREGISTER:
  321. list_for_each_entry(ops, &rules_ops, list)
  322. detach_rules(ops->rules_list, dev);
  323. break;
  324. }
  325. rcu_read_unlock();
  326. return NOTIFY_DONE;
  327. }
  328. static struct notifier_block fib_rules_notifier = {
  329. .notifier_call = fib_rules_event,
  330. };
  331. static int __init fib_rules_init(void)
  332. {
  333. return register_netdevice_notifier(&fib_rules_notifier);
  334. }
  335. subsys_initcall(fib_rules_init);