fib_rules.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
  88. struct flowi *fl, int flags)
  89. {
  90. int ret = 0;
  91. if (rule->ifindex && (rule->ifindex != fl->iif))
  92. goto out;
  93. if ((rule->mark ^ fl->mark) & rule->mark_mask)
  94. goto out;
  95. ret = ops->match(rule, fl, flags);
  96. out:
  97. return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
  98. }
  99. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  100. int flags, struct fib_lookup_arg *arg)
  101. {
  102. struct fib_rule *rule;
  103. int err;
  104. rcu_read_lock();
  105. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  106. jumped:
  107. if (!fib_rule_match(rule, ops, fl, flags))
  108. continue;
  109. if (rule->action == FR_ACT_GOTO) {
  110. struct fib_rule *target;
  111. target = rcu_dereference(rule->ctarget);
  112. if (target == NULL) {
  113. continue;
  114. } else {
  115. rule = target;
  116. goto jumped;
  117. }
  118. } else
  119. err = ops->action(rule, fl, flags, arg);
  120. if (err != -EAGAIN) {
  121. fib_rule_get(rule);
  122. arg->rule = rule;
  123. goto out;
  124. }
  125. }
  126. err = -ESRCH;
  127. out:
  128. rcu_read_unlock();
  129. return err;
  130. }
  131. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  132. static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
  133. struct fib_rules_ops *ops)
  134. {
  135. int err = -EINVAL;
  136. if (frh->src_len)
  137. if (tb[FRA_SRC] == NULL ||
  138. frh->src_len > (ops->addr_size * 8) ||
  139. nla_len(tb[FRA_SRC]) != ops->addr_size)
  140. goto errout;
  141. if (frh->dst_len)
  142. if (tb[FRA_DST] == NULL ||
  143. frh->dst_len > (ops->addr_size * 8) ||
  144. nla_len(tb[FRA_DST]) != ops->addr_size)
  145. goto errout;
  146. err = 0;
  147. errout:
  148. return err;
  149. }
  150. static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  151. {
  152. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  153. struct fib_rules_ops *ops = NULL;
  154. struct fib_rule *rule, *r, *last = NULL;
  155. struct nlattr *tb[FRA_MAX+1];
  156. int err = -EINVAL, unresolved = 0;
  157. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  158. goto errout;
  159. ops = lookup_rules_ops(frh->family);
  160. if (ops == NULL) {
  161. err = EAFNOSUPPORT;
  162. goto errout;
  163. }
  164. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  165. if (err < 0)
  166. goto errout;
  167. err = validate_rulemsg(frh, tb, ops);
  168. if (err < 0)
  169. goto errout;
  170. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  171. if (rule == NULL) {
  172. err = -ENOMEM;
  173. goto errout;
  174. }
  175. if (tb[FRA_PRIORITY])
  176. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  177. if (tb[FRA_IFNAME]) {
  178. struct net_device *dev;
  179. rule->ifindex = -1;
  180. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  181. dev = __dev_get_by_name(rule->ifname);
  182. if (dev)
  183. rule->ifindex = dev->ifindex;
  184. }
  185. if (tb[FRA_FWMARK]) {
  186. rule->mark = nla_get_u32(tb[FRA_FWMARK]);
  187. if (rule->mark)
  188. /* compatibility: if the mark value is non-zero all bits
  189. * are compared unless a mask is explicitly specified.
  190. */
  191. rule->mark_mask = 0xFFFFFFFF;
  192. }
  193. if (tb[FRA_FWMASK])
  194. rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
  195. rule->action = frh->action;
  196. rule->flags = frh->flags;
  197. rule->table = frh_get_table(frh, tb);
  198. if (!rule->pref && ops->default_pref)
  199. rule->pref = ops->default_pref();
  200. err = -EINVAL;
  201. if (tb[FRA_GOTO]) {
  202. if (rule->action != FR_ACT_GOTO)
  203. goto errout_free;
  204. rule->target = nla_get_u32(tb[FRA_GOTO]);
  205. /* Backward jumps are prohibited to avoid endless loops */
  206. if (rule->target <= rule->pref)
  207. goto errout_free;
  208. list_for_each_entry(r, ops->rules_list, list) {
  209. if (r->pref == rule->target) {
  210. rule->ctarget = r;
  211. break;
  212. }
  213. }
  214. if (rule->ctarget == NULL)
  215. unresolved = 1;
  216. } else if (rule->action == FR_ACT_GOTO)
  217. goto errout_free;
  218. err = ops->configure(rule, skb, nlh, frh, tb);
  219. if (err < 0)
  220. goto errout_free;
  221. list_for_each_entry(r, ops->rules_list, list) {
  222. if (r->pref > rule->pref)
  223. break;
  224. last = r;
  225. }
  226. fib_rule_get(rule);
  227. if (ops->unresolved_rules) {
  228. /*
  229. * There are unresolved goto rules in the list, check if
  230. * any of them are pointing to this new rule.
  231. */
  232. list_for_each_entry(r, ops->rules_list, list) {
  233. if (r->action == FR_ACT_GOTO &&
  234. r->target == rule->pref) {
  235. BUG_ON(r->ctarget != NULL);
  236. rcu_assign_pointer(r->ctarget, rule);
  237. if (--ops->unresolved_rules == 0)
  238. break;
  239. }
  240. }
  241. }
  242. if (rule->action == FR_ACT_GOTO)
  243. ops->nr_goto_rules++;
  244. if (unresolved)
  245. ops->unresolved_rules++;
  246. if (last)
  247. list_add_rcu(&rule->list, &last->list);
  248. else
  249. list_add_rcu(&rule->list, ops->rules_list);
  250. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  251. rules_ops_put(ops);
  252. return 0;
  253. errout_free:
  254. kfree(rule);
  255. errout:
  256. rules_ops_put(ops);
  257. return err;
  258. }
  259. static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  260. {
  261. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  262. struct fib_rules_ops *ops = NULL;
  263. struct fib_rule *rule, *tmp;
  264. struct nlattr *tb[FRA_MAX+1];
  265. int err = -EINVAL;
  266. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  267. goto errout;
  268. ops = lookup_rules_ops(frh->family);
  269. if (ops == NULL) {
  270. err = EAFNOSUPPORT;
  271. goto errout;
  272. }
  273. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  274. if (err < 0)
  275. goto errout;
  276. err = validate_rulemsg(frh, tb, ops);
  277. if (err < 0)
  278. goto errout;
  279. list_for_each_entry(rule, ops->rules_list, list) {
  280. if (frh->action && (frh->action != rule->action))
  281. continue;
  282. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  283. continue;
  284. if (tb[FRA_PRIORITY] &&
  285. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  286. continue;
  287. if (tb[FRA_IFNAME] &&
  288. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  289. continue;
  290. if (tb[FRA_FWMARK] &&
  291. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  292. continue;
  293. if (tb[FRA_FWMASK] &&
  294. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  295. continue;
  296. if (!ops->compare(rule, frh, tb))
  297. continue;
  298. if (rule->flags & FIB_RULE_PERMANENT) {
  299. err = -EPERM;
  300. goto errout;
  301. }
  302. list_del_rcu(&rule->list);
  303. if (rule->action == FR_ACT_GOTO)
  304. ops->nr_goto_rules--;
  305. /*
  306. * Check if this rule is a target to any of them. If so,
  307. * disable them. As this operation is eventually very
  308. * expensive, it is only performed if goto rules have
  309. * actually been added.
  310. */
  311. if (ops->nr_goto_rules > 0) {
  312. list_for_each_entry(tmp, ops->rules_list, list) {
  313. if (tmp->ctarget == rule) {
  314. rcu_assign_pointer(tmp->ctarget, NULL);
  315. ops->unresolved_rules++;
  316. }
  317. }
  318. }
  319. synchronize_rcu();
  320. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  321. NETLINK_CB(skb).pid);
  322. fib_rule_put(rule);
  323. rules_ops_put(ops);
  324. return 0;
  325. }
  326. err = -ENOENT;
  327. errout:
  328. rules_ops_put(ops);
  329. return err;
  330. }
  331. static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
  332. struct fib_rule *rule)
  333. {
  334. size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
  335. + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
  336. + nla_total_size(4) /* FRA_PRIORITY */
  337. + nla_total_size(4) /* FRA_TABLE */
  338. + nla_total_size(4) /* FRA_FWMARK */
  339. + nla_total_size(4); /* FRA_FWMASK */
  340. if (ops->nlmsg_payload)
  341. payload += ops->nlmsg_payload(rule);
  342. return payload;
  343. }
  344. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  345. u32 pid, u32 seq, int type, int flags,
  346. struct fib_rules_ops *ops)
  347. {
  348. struct nlmsghdr *nlh;
  349. struct fib_rule_hdr *frh;
  350. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  351. if (nlh == NULL)
  352. return -EMSGSIZE;
  353. frh = nlmsg_data(nlh);
  354. frh->table = rule->table;
  355. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  356. frh->res1 = 0;
  357. frh->res2 = 0;
  358. frh->action = rule->action;
  359. frh->flags = rule->flags;
  360. if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
  361. frh->flags |= FIB_RULE_UNRESOLVED;
  362. if (rule->ifname[0])
  363. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  364. if (rule->pref)
  365. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  366. if (rule->mark)
  367. NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
  368. if (rule->mark_mask || rule->mark)
  369. NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
  370. if (rule->target)
  371. NLA_PUT_U32(skb, FRA_GOTO, rule->target);
  372. if (ops->fill(rule, skb, nlh, frh) < 0)
  373. goto nla_put_failure;
  374. return nlmsg_end(skb, nlh);
  375. nla_put_failure:
  376. nlmsg_cancel(skb, nlh);
  377. return -EMSGSIZE;
  378. }
  379. static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
  380. struct fib_rules_ops *ops)
  381. {
  382. int idx = 0;
  383. struct fib_rule *rule;
  384. rcu_read_lock();
  385. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  386. if (idx < cb->args[1])
  387. goto skip;
  388. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  389. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  390. NLM_F_MULTI, ops) < 0)
  391. break;
  392. skip:
  393. idx++;
  394. }
  395. rcu_read_unlock();
  396. cb->args[1] = idx;
  397. rules_ops_put(ops);
  398. return skb->len;
  399. }
  400. static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
  401. {
  402. struct fib_rules_ops *ops;
  403. int idx = 0, family;
  404. family = rtnl_msg_family(cb->nlh);
  405. if (family != AF_UNSPEC) {
  406. /* Protocol specific dump request */
  407. ops = lookup_rules_ops(family);
  408. if (ops == NULL)
  409. return -EAFNOSUPPORT;
  410. return dump_rules(skb, cb, ops);
  411. }
  412. rcu_read_lock();
  413. list_for_each_entry_rcu(ops, &rules_ops, list) {
  414. if (idx < cb->args[0] || !try_module_get(ops->owner))
  415. goto skip;
  416. if (dump_rules(skb, cb, ops) < 0)
  417. break;
  418. cb->args[1] = 0;
  419. skip:
  420. idx++;
  421. }
  422. rcu_read_unlock();
  423. cb->args[0] = idx;
  424. return skb->len;
  425. }
  426. static void notify_rule_change(int event, struct fib_rule *rule,
  427. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  428. u32 pid)
  429. {
  430. struct sk_buff *skb;
  431. int err = -ENOBUFS;
  432. skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
  433. if (skb == NULL)
  434. goto errout;
  435. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  436. if (err < 0) {
  437. /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
  438. WARN_ON(err == -EMSGSIZE);
  439. kfree_skb(skb);
  440. goto errout;
  441. }
  442. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  443. errout:
  444. if (err < 0)
  445. rtnl_set_sk_err(ops->nlgroup, err);
  446. }
  447. static void attach_rules(struct list_head *rules, struct net_device *dev)
  448. {
  449. struct fib_rule *rule;
  450. list_for_each_entry(rule, rules, list) {
  451. if (rule->ifindex == -1 &&
  452. strcmp(dev->name, rule->ifname) == 0)
  453. rule->ifindex = dev->ifindex;
  454. }
  455. }
  456. static void detach_rules(struct list_head *rules, struct net_device *dev)
  457. {
  458. struct fib_rule *rule;
  459. list_for_each_entry(rule, rules, list)
  460. if (rule->ifindex == dev->ifindex)
  461. rule->ifindex = -1;
  462. }
  463. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  464. void *ptr)
  465. {
  466. struct net_device *dev = ptr;
  467. struct fib_rules_ops *ops;
  468. ASSERT_RTNL();
  469. rcu_read_lock();
  470. switch (event) {
  471. case NETDEV_REGISTER:
  472. list_for_each_entry(ops, &rules_ops, list)
  473. attach_rules(ops->rules_list, dev);
  474. break;
  475. case NETDEV_UNREGISTER:
  476. list_for_each_entry(ops, &rules_ops, list)
  477. detach_rules(ops->rules_list, dev);
  478. break;
  479. }
  480. rcu_read_unlock();
  481. return NOTIFY_DONE;
  482. }
  483. static struct notifier_block fib_rules_notifier = {
  484. .notifier_call = fib_rules_event,
  485. };
  486. static int __init fib_rules_init(void)
  487. {
  488. rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
  489. rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
  490. rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
  491. return register_netdevice_notifier(&fib_rules_notifier);
  492. }
  493. subsys_initcall(fib_rules_init);