fib_rules.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/net_namespace.h>
  14. #include <net/sock.h>
  15. #include <net/fib_rules.h>
  16. int fib_default_rule_add(struct fib_rules_ops *ops,
  17. u32 pref, u32 table, u32 flags)
  18. {
  19. struct fib_rule *r;
  20. r = kzalloc(ops->rule_size, GFP_KERNEL);
  21. if (r == NULL)
  22. return -ENOMEM;
  23. atomic_set(&r->refcnt, 1);
  24. r->action = FR_ACT_TO_TBL;
  25. r->pref = pref;
  26. r->table = table;
  27. r->flags = flags;
  28. /* The lock is not required here, the list in unreacheable
  29. * at the moment this function is called */
  30. list_add_tail(&r->list, &ops->rules_list);
  31. return 0;
  32. }
  33. EXPORT_SYMBOL(fib_default_rule_add);
  34. static void notify_rule_change(int event, struct fib_rule *rule,
  35. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  36. u32 pid);
  37. static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
  38. {
  39. struct fib_rules_ops *ops;
  40. rcu_read_lock();
  41. list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  42. if (ops->family == family) {
  43. if (!try_module_get(ops->owner))
  44. ops = NULL;
  45. rcu_read_unlock();
  46. return ops;
  47. }
  48. }
  49. rcu_read_unlock();
  50. return NULL;
  51. }
  52. static void rules_ops_put(struct fib_rules_ops *ops)
  53. {
  54. if (ops)
  55. module_put(ops->owner);
  56. }
  57. static void flush_route_cache(struct fib_rules_ops *ops)
  58. {
  59. if (ops->flush_cache)
  60. ops->flush_cache();
  61. }
  62. int fib_rules_register(struct fib_rules_ops *ops)
  63. {
  64. int err = -EEXIST;
  65. struct fib_rules_ops *o;
  66. struct net *net;
  67. net = ops->fro_net;
  68. if (ops->rule_size < sizeof(struct fib_rule))
  69. return -EINVAL;
  70. if (ops->match == NULL || ops->configure == NULL ||
  71. ops->compare == NULL || ops->fill == NULL ||
  72. ops->action == NULL)
  73. return -EINVAL;
  74. spin_lock(&net->rules_mod_lock);
  75. list_for_each_entry(o, &net->rules_ops, list)
  76. if (ops->family == o->family)
  77. goto errout;
  78. hold_net(net);
  79. list_add_tail_rcu(&ops->list, &net->rules_ops);
  80. err = 0;
  81. errout:
  82. spin_unlock(&net->rules_mod_lock);
  83. return err;
  84. }
  85. EXPORT_SYMBOL_GPL(fib_rules_register);
  86. void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
  87. {
  88. struct fib_rule *rule, *tmp;
  89. list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
  90. list_del_rcu(&rule->list);
  91. fib_rule_put(rule);
  92. }
  93. }
  94. EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
  95. void fib_rules_unregister(struct fib_rules_ops *ops)
  96. {
  97. struct net *net = ops->fro_net;
  98. spin_lock(&net->rules_mod_lock);
  99. list_del_rcu(&ops->list);
  100. fib_rules_cleanup_ops(ops);
  101. spin_unlock(&net->rules_mod_lock);
  102. synchronize_rcu();
  103. release_net(net);
  104. }
  105. EXPORT_SYMBOL_GPL(fib_rules_unregister);
  106. static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
  107. struct flowi *fl, int flags)
  108. {
  109. int ret = 0;
  110. if (rule->ifindex && (rule->ifindex != fl->iif))
  111. goto out;
  112. if ((rule->mark ^ fl->mark) & rule->mark_mask)
  113. goto out;
  114. ret = ops->match(rule, fl, flags);
  115. out:
  116. return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
  117. }
  118. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  119. int flags, struct fib_lookup_arg *arg)
  120. {
  121. struct fib_rule *rule;
  122. int err;
  123. rcu_read_lock();
  124. list_for_each_entry_rcu(rule, &ops->rules_list, list) {
  125. jumped:
  126. if (!fib_rule_match(rule, ops, fl, flags))
  127. continue;
  128. if (rule->action == FR_ACT_GOTO) {
  129. struct fib_rule *target;
  130. target = rcu_dereference(rule->ctarget);
  131. if (target == NULL) {
  132. continue;
  133. } else {
  134. rule = target;
  135. goto jumped;
  136. }
  137. } else if (rule->action == FR_ACT_NOP)
  138. continue;
  139. else
  140. err = ops->action(rule, fl, flags, arg);
  141. if (err != -EAGAIN) {
  142. fib_rule_get(rule);
  143. arg->rule = rule;
  144. goto out;
  145. }
  146. }
  147. err = -ESRCH;
  148. out:
  149. rcu_read_unlock();
  150. return err;
  151. }
  152. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  153. static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
  154. struct fib_rules_ops *ops)
  155. {
  156. int err = -EINVAL;
  157. if (frh->src_len)
  158. if (tb[FRA_SRC] == NULL ||
  159. frh->src_len > (ops->addr_size * 8) ||
  160. nla_len(tb[FRA_SRC]) != ops->addr_size)
  161. goto errout;
  162. if (frh->dst_len)
  163. if (tb[FRA_DST] == NULL ||
  164. frh->dst_len > (ops->addr_size * 8) ||
  165. nla_len(tb[FRA_DST]) != ops->addr_size)
  166. goto errout;
  167. err = 0;
  168. errout:
  169. return err;
  170. }
  171. static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  172. {
  173. struct net *net = skb->sk->sk_net;
  174. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  175. struct fib_rules_ops *ops = NULL;
  176. struct fib_rule *rule, *r, *last = NULL;
  177. struct nlattr *tb[FRA_MAX+1];
  178. int err = -EINVAL, unresolved = 0;
  179. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  180. goto errout;
  181. ops = lookup_rules_ops(net, frh->family);
  182. if (ops == NULL) {
  183. err = EAFNOSUPPORT;
  184. goto errout;
  185. }
  186. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  187. if (err < 0)
  188. goto errout;
  189. err = validate_rulemsg(frh, tb, ops);
  190. if (err < 0)
  191. goto errout;
  192. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  193. if (rule == NULL) {
  194. err = -ENOMEM;
  195. goto errout;
  196. }
  197. if (tb[FRA_PRIORITY])
  198. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  199. if (tb[FRA_IFNAME]) {
  200. struct net_device *dev;
  201. rule->ifindex = -1;
  202. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  203. dev = __dev_get_by_name(net, rule->ifname);
  204. if (dev)
  205. rule->ifindex = dev->ifindex;
  206. }
  207. if (tb[FRA_FWMARK]) {
  208. rule->mark = nla_get_u32(tb[FRA_FWMARK]);
  209. if (rule->mark)
  210. /* compatibility: if the mark value is non-zero all bits
  211. * are compared unless a mask is explicitly specified.
  212. */
  213. rule->mark_mask = 0xFFFFFFFF;
  214. }
  215. if (tb[FRA_FWMASK])
  216. rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
  217. rule->action = frh->action;
  218. rule->flags = frh->flags;
  219. rule->table = frh_get_table(frh, tb);
  220. if (!rule->pref && ops->default_pref)
  221. rule->pref = ops->default_pref(ops);
  222. err = -EINVAL;
  223. if (tb[FRA_GOTO]) {
  224. if (rule->action != FR_ACT_GOTO)
  225. goto errout_free;
  226. rule->target = nla_get_u32(tb[FRA_GOTO]);
  227. /* Backward jumps are prohibited to avoid endless loops */
  228. if (rule->target <= rule->pref)
  229. goto errout_free;
  230. list_for_each_entry(r, &ops->rules_list, list) {
  231. if (r->pref == rule->target) {
  232. rule->ctarget = r;
  233. break;
  234. }
  235. }
  236. if (rule->ctarget == NULL)
  237. unresolved = 1;
  238. } else if (rule->action == FR_ACT_GOTO)
  239. goto errout_free;
  240. err = ops->configure(rule, skb, nlh, frh, tb);
  241. if (err < 0)
  242. goto errout_free;
  243. list_for_each_entry(r, &ops->rules_list, list) {
  244. if (r->pref > rule->pref)
  245. break;
  246. last = r;
  247. }
  248. fib_rule_get(rule);
  249. if (ops->unresolved_rules) {
  250. /*
  251. * There are unresolved goto rules in the list, check if
  252. * any of them are pointing to this new rule.
  253. */
  254. list_for_each_entry(r, &ops->rules_list, list) {
  255. if (r->action == FR_ACT_GOTO &&
  256. r->target == rule->pref) {
  257. BUG_ON(r->ctarget != NULL);
  258. rcu_assign_pointer(r->ctarget, rule);
  259. if (--ops->unresolved_rules == 0)
  260. break;
  261. }
  262. }
  263. }
  264. if (rule->action == FR_ACT_GOTO)
  265. ops->nr_goto_rules++;
  266. if (unresolved)
  267. ops->unresolved_rules++;
  268. if (last)
  269. list_add_rcu(&rule->list, &last->list);
  270. else
  271. list_add_rcu(&rule->list, &ops->rules_list);
  272. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  273. flush_route_cache(ops);
  274. rules_ops_put(ops);
  275. return 0;
  276. errout_free:
  277. kfree(rule);
  278. errout:
  279. rules_ops_put(ops);
  280. return err;
  281. }
  282. static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  283. {
  284. struct net *net = skb->sk->sk_net;
  285. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  286. struct fib_rules_ops *ops = NULL;
  287. struct fib_rule *rule, *tmp;
  288. struct nlattr *tb[FRA_MAX+1];
  289. int err = -EINVAL;
  290. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  291. goto errout;
  292. ops = lookup_rules_ops(net, frh->family);
  293. if (ops == NULL) {
  294. err = EAFNOSUPPORT;
  295. goto errout;
  296. }
  297. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  298. if (err < 0)
  299. goto errout;
  300. err = validate_rulemsg(frh, tb, ops);
  301. if (err < 0)
  302. goto errout;
  303. list_for_each_entry(rule, &ops->rules_list, list) {
  304. if (frh->action && (frh->action != rule->action))
  305. continue;
  306. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  307. continue;
  308. if (tb[FRA_PRIORITY] &&
  309. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  310. continue;
  311. if (tb[FRA_IFNAME] &&
  312. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  313. continue;
  314. if (tb[FRA_FWMARK] &&
  315. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  316. continue;
  317. if (tb[FRA_FWMASK] &&
  318. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  319. continue;
  320. if (!ops->compare(rule, frh, tb))
  321. continue;
  322. if (rule->flags & FIB_RULE_PERMANENT) {
  323. err = -EPERM;
  324. goto errout;
  325. }
  326. list_del_rcu(&rule->list);
  327. if (rule->action == FR_ACT_GOTO)
  328. ops->nr_goto_rules--;
  329. /*
  330. * Check if this rule is a target to any of them. If so,
  331. * disable them. As this operation is eventually very
  332. * expensive, it is only performed if goto rules have
  333. * actually been added.
  334. */
  335. if (ops->nr_goto_rules > 0) {
  336. list_for_each_entry(tmp, &ops->rules_list, list) {
  337. if (tmp->ctarget == rule) {
  338. rcu_assign_pointer(tmp->ctarget, NULL);
  339. ops->unresolved_rules++;
  340. }
  341. }
  342. }
  343. synchronize_rcu();
  344. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  345. NETLINK_CB(skb).pid);
  346. fib_rule_put(rule);
  347. flush_route_cache(ops);
  348. rules_ops_put(ops);
  349. return 0;
  350. }
  351. err = -ENOENT;
  352. errout:
  353. rules_ops_put(ops);
  354. return err;
  355. }
  356. static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
  357. struct fib_rule *rule)
  358. {
  359. size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
  360. + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
  361. + nla_total_size(4) /* FRA_PRIORITY */
  362. + nla_total_size(4) /* FRA_TABLE */
  363. + nla_total_size(4) /* FRA_FWMARK */
  364. + nla_total_size(4); /* FRA_FWMASK */
  365. if (ops->nlmsg_payload)
  366. payload += ops->nlmsg_payload(rule);
  367. return payload;
  368. }
  369. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  370. u32 pid, u32 seq, int type, int flags,
  371. struct fib_rules_ops *ops)
  372. {
  373. struct nlmsghdr *nlh;
  374. struct fib_rule_hdr *frh;
  375. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  376. if (nlh == NULL)
  377. return -EMSGSIZE;
  378. frh = nlmsg_data(nlh);
  379. frh->table = rule->table;
  380. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  381. frh->res1 = 0;
  382. frh->res2 = 0;
  383. frh->action = rule->action;
  384. frh->flags = rule->flags;
  385. if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
  386. frh->flags |= FIB_RULE_UNRESOLVED;
  387. if (rule->ifname[0]) {
  388. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  389. if (rule->ifindex == -1)
  390. frh->flags |= FIB_RULE_DEV_DETACHED;
  391. }
  392. if (rule->pref)
  393. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  394. if (rule->mark)
  395. NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
  396. if (rule->mark_mask || rule->mark)
  397. NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
  398. if (rule->target)
  399. NLA_PUT_U32(skb, FRA_GOTO, rule->target);
  400. if (ops->fill(rule, skb, nlh, frh) < 0)
  401. goto nla_put_failure;
  402. return nlmsg_end(skb, nlh);
  403. nla_put_failure:
  404. nlmsg_cancel(skb, nlh);
  405. return -EMSGSIZE;
  406. }
  407. static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
  408. struct fib_rules_ops *ops)
  409. {
  410. int idx = 0;
  411. struct fib_rule *rule;
  412. list_for_each_entry(rule, &ops->rules_list, list) {
  413. if (idx < cb->args[1])
  414. goto skip;
  415. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  416. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  417. NLM_F_MULTI, ops) < 0)
  418. break;
  419. skip:
  420. idx++;
  421. }
  422. cb->args[1] = idx;
  423. rules_ops_put(ops);
  424. return skb->len;
  425. }
  426. static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
  427. {
  428. struct net *net = skb->sk->sk_net;
  429. struct fib_rules_ops *ops;
  430. int idx = 0, family;
  431. family = rtnl_msg_family(cb->nlh);
  432. if (family != AF_UNSPEC) {
  433. /* Protocol specific dump request */
  434. ops = lookup_rules_ops(net, family);
  435. if (ops == NULL)
  436. return -EAFNOSUPPORT;
  437. return dump_rules(skb, cb, ops);
  438. }
  439. rcu_read_lock();
  440. list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  441. if (idx < cb->args[0] || !try_module_get(ops->owner))
  442. goto skip;
  443. if (dump_rules(skb, cb, ops) < 0)
  444. break;
  445. cb->args[1] = 0;
  446. skip:
  447. idx++;
  448. }
  449. rcu_read_unlock();
  450. cb->args[0] = idx;
  451. return skb->len;
  452. }
  453. static void notify_rule_change(int event, struct fib_rule *rule,
  454. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  455. u32 pid)
  456. {
  457. struct net *net;
  458. struct sk_buff *skb;
  459. int err = -ENOBUFS;
  460. net = ops->fro_net;
  461. skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
  462. if (skb == NULL)
  463. goto errout;
  464. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  465. if (err < 0) {
  466. /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
  467. WARN_ON(err == -EMSGSIZE);
  468. kfree_skb(skb);
  469. goto errout;
  470. }
  471. err = rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
  472. errout:
  473. if (err < 0)
  474. rtnl_set_sk_err(net, ops->nlgroup, err);
  475. }
  476. static void attach_rules(struct list_head *rules, struct net_device *dev)
  477. {
  478. struct fib_rule *rule;
  479. list_for_each_entry(rule, rules, list) {
  480. if (rule->ifindex == -1 &&
  481. strcmp(dev->name, rule->ifname) == 0)
  482. rule->ifindex = dev->ifindex;
  483. }
  484. }
  485. static void detach_rules(struct list_head *rules, struct net_device *dev)
  486. {
  487. struct fib_rule *rule;
  488. list_for_each_entry(rule, rules, list)
  489. if (rule->ifindex == dev->ifindex)
  490. rule->ifindex = -1;
  491. }
  492. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  493. void *ptr)
  494. {
  495. struct net_device *dev = ptr;
  496. struct net *net = dev->nd_net;
  497. struct fib_rules_ops *ops;
  498. ASSERT_RTNL();
  499. rcu_read_lock();
  500. switch (event) {
  501. case NETDEV_REGISTER:
  502. list_for_each_entry(ops, &net->rules_ops, list)
  503. attach_rules(&ops->rules_list, dev);
  504. break;
  505. case NETDEV_UNREGISTER:
  506. list_for_each_entry(ops, &net->rules_ops, list)
  507. detach_rules(&ops->rules_list, dev);
  508. break;
  509. }
  510. rcu_read_unlock();
  511. return NOTIFY_DONE;
  512. }
  513. static struct notifier_block fib_rules_notifier = {
  514. .notifier_call = fib_rules_event,
  515. };
  516. static int fib_rules_net_init(struct net *net)
  517. {
  518. INIT_LIST_HEAD(&net->rules_ops);
  519. spin_lock_init(&net->rules_mod_lock);
  520. return 0;
  521. }
  522. static struct pernet_operations fib_rules_net_ops = {
  523. .init = fib_rules_net_init,
  524. };
  525. static int __init fib_rules_init(void)
  526. {
  527. int err;
  528. rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
  529. rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
  530. rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
  531. err = register_netdevice_notifier(&fib_rules_notifier);
  532. if (err < 0)
  533. goto fail;
  534. err = register_pernet_subsys(&fib_rules_net_ops);
  535. if (err < 0)
  536. goto fail_unregister;
  537. return 0;
  538. fail_unregister:
  539. unregister_netdevice_notifier(&fib_rules_notifier);
  540. fail:
  541. rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
  542. rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
  543. rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
  544. return err;
  545. }
  546. subsys_initcall(fib_rules_init);