fib_rules.c 15 KB

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