fib_rules.c 15 KB

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