fib_rules.c 9.1 KB

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