fib_rules.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  130. if (rule == NULL) {
  131. err = -ENOMEM;
  132. goto errout;
  133. }
  134. if (tb[FRA_PRIORITY])
  135. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  136. if (tb[FRA_IFNAME]) {
  137. struct net_device *dev;
  138. rule->ifindex = -1;
  139. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  140. dev = __dev_get_by_name(rule->ifname);
  141. if (dev)
  142. rule->ifindex = dev->ifindex;
  143. }
  144. rule->action = frh->action;
  145. rule->flags = frh->flags;
  146. rule->table = frh_get_table(frh, tb);
  147. if (!rule->pref && ops->default_pref)
  148. rule->pref = ops->default_pref();
  149. err = ops->configure(rule, skb, nlh, frh, tb);
  150. if (err < 0)
  151. goto errout_free;
  152. list_for_each_entry(r, ops->rules_list, list) {
  153. if (r->pref > rule->pref)
  154. break;
  155. last = r;
  156. }
  157. fib_rule_get(rule);
  158. if (last)
  159. list_add_rcu(&rule->list, &last->list);
  160. else
  161. list_add_rcu(&rule->list, ops->rules_list);
  162. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  163. rules_ops_put(ops);
  164. return 0;
  165. errout_free:
  166. kfree(rule);
  167. errout:
  168. rules_ops_put(ops);
  169. return err;
  170. }
  171. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  172. {
  173. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  174. struct fib_rules_ops *ops = NULL;
  175. struct fib_rule *rule;
  176. struct nlattr *tb[FRA_MAX+1];
  177. int err = -EINVAL;
  178. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  179. goto errout;
  180. ops = lookup_rules_ops(frh->family);
  181. if (ops == NULL) {
  182. err = EAFNOSUPPORT;
  183. goto errout;
  184. }
  185. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  186. if (err < 0)
  187. goto errout;
  188. list_for_each_entry(rule, ops->rules_list, list) {
  189. if (frh->action && (frh->action != rule->action))
  190. continue;
  191. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  192. continue;
  193. if (tb[FRA_PRIORITY] &&
  194. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  195. continue;
  196. if (tb[FRA_IFNAME] &&
  197. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  198. continue;
  199. if (!ops->compare(rule, frh, tb))
  200. continue;
  201. if (rule->flags & FIB_RULE_PERMANENT) {
  202. err = -EPERM;
  203. goto errout;
  204. }
  205. list_del_rcu(&rule->list);
  206. synchronize_rcu();
  207. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  208. NETLINK_CB(skb).pid);
  209. fib_rule_put(rule);
  210. rules_ops_put(ops);
  211. return 0;
  212. }
  213. err = -ENOENT;
  214. errout:
  215. rules_ops_put(ops);
  216. return err;
  217. }
  218. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  219. u32 pid, u32 seq, int type, int flags,
  220. struct fib_rules_ops *ops)
  221. {
  222. struct nlmsghdr *nlh;
  223. struct fib_rule_hdr *frh;
  224. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  225. if (nlh == NULL)
  226. return -1;
  227. frh = nlmsg_data(nlh);
  228. frh->table = rule->table;
  229. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  230. frh->res1 = 0;
  231. frh->res2 = 0;
  232. frh->action = rule->action;
  233. frh->flags = rule->flags;
  234. if (rule->ifname[0])
  235. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  236. if (rule->pref)
  237. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  238. if (ops->fill(rule, skb, nlh, frh) < 0)
  239. goto nla_put_failure;
  240. return nlmsg_end(skb, nlh);
  241. nla_put_failure:
  242. return nlmsg_cancel(skb, nlh);
  243. }
  244. int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
  245. {
  246. int idx = 0;
  247. struct fib_rule *rule;
  248. struct fib_rules_ops *ops;
  249. ops = lookup_rules_ops(family);
  250. if (ops == NULL)
  251. return -EAFNOSUPPORT;
  252. rcu_read_lock();
  253. list_for_each_entry(rule, ops->rules_list, list) {
  254. if (idx < cb->args[0])
  255. goto skip;
  256. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  257. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  258. NLM_F_MULTI, ops) < 0)
  259. break;
  260. skip:
  261. idx++;
  262. }
  263. rcu_read_unlock();
  264. cb->args[0] = idx;
  265. rules_ops_put(ops);
  266. return skb->len;
  267. }
  268. EXPORT_SYMBOL_GPL(fib_rules_dump);
  269. static void notify_rule_change(int event, struct fib_rule *rule,
  270. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  271. u32 pid)
  272. {
  273. struct sk_buff *skb;
  274. int err = -ENOBUFS;
  275. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  276. if (skb == NULL)
  277. goto errout;
  278. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  279. if (err < 0) {
  280. kfree_skb(skb);
  281. goto errout;
  282. }
  283. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  284. errout:
  285. if (err < 0)
  286. rtnl_set_sk_err(ops->nlgroup, err);
  287. }
  288. static void attach_rules(struct list_head *rules, struct net_device *dev)
  289. {
  290. struct fib_rule *rule;
  291. list_for_each_entry(rule, rules, list) {
  292. if (rule->ifindex == -1 &&
  293. strcmp(dev->name, rule->ifname) == 0)
  294. rule->ifindex = dev->ifindex;
  295. }
  296. }
  297. static void detach_rules(struct list_head *rules, struct net_device *dev)
  298. {
  299. struct fib_rule *rule;
  300. list_for_each_entry(rule, rules, list)
  301. if (rule->ifindex == dev->ifindex)
  302. rule->ifindex = -1;
  303. }
  304. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  305. void *ptr)
  306. {
  307. struct net_device *dev = ptr;
  308. struct fib_rules_ops *ops;
  309. ASSERT_RTNL();
  310. rcu_read_lock();
  311. switch (event) {
  312. case NETDEV_REGISTER:
  313. list_for_each_entry(ops, &rules_ops, list)
  314. attach_rules(ops->rules_list, dev);
  315. break;
  316. case NETDEV_UNREGISTER:
  317. list_for_each_entry(ops, &rules_ops, list)
  318. detach_rules(ops->rules_list, dev);
  319. break;
  320. }
  321. rcu_read_unlock();
  322. return NOTIFY_DONE;
  323. }
  324. static struct notifier_block fib_rules_notifier = {
  325. .notifier_call = fib_rules_event,
  326. };
  327. static int __init fib_rules_init(void)
  328. {
  329. return register_netdevice_notifier(&fib_rules_notifier);
  330. }
  331. subsys_initcall(fib_rules_init);