fib_rules.c 14 KB

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