fib_rules.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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/slab.h>
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <net/net_namespace.h>
  16. #include <net/sock.h>
  17. #include <net/fib_rules.h>
  18. #include <net/ip_tunnels.h>
  19. int fib_default_rule_add(struct fib_rules_ops *ops,
  20. u32 pref, u32 table, u32 flags)
  21. {
  22. struct fib_rule *r;
  23. r = kzalloc(ops->rule_size, GFP_KERNEL);
  24. if (r == NULL)
  25. return -ENOMEM;
  26. atomic_set(&r->refcnt, 1);
  27. r->action = FR_ACT_TO_TBL;
  28. r->pref = pref;
  29. r->table = table;
  30. r->flags = flags;
  31. r->fr_net = ops->fro_net;
  32. r->suppress_prefixlen = -1;
  33. r->suppress_ifgroup = -1;
  34. /* The lock is not required here, the list in unreacheable
  35. * at the moment this function is called */
  36. list_add_tail(&r->list, &ops->rules_list);
  37. return 0;
  38. }
  39. EXPORT_SYMBOL(fib_default_rule_add);
  40. static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
  41. {
  42. struct list_head *pos;
  43. struct fib_rule *rule;
  44. if (!list_empty(&ops->rules_list)) {
  45. pos = ops->rules_list.next;
  46. if (pos->next != &ops->rules_list) {
  47. rule = list_entry(pos->next, struct fib_rule, list);
  48. if (rule->pref)
  49. return rule->pref - 1;
  50. }
  51. }
  52. return 0;
  53. }
  54. static void notify_rule_change(int event, struct fib_rule *rule,
  55. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  56. u32 pid);
  57. static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
  58. {
  59. struct fib_rules_ops *ops;
  60. rcu_read_lock();
  61. list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  62. if (ops->family == family) {
  63. if (!try_module_get(ops->owner))
  64. ops = NULL;
  65. rcu_read_unlock();
  66. return ops;
  67. }
  68. }
  69. rcu_read_unlock();
  70. return NULL;
  71. }
  72. static void rules_ops_put(struct fib_rules_ops *ops)
  73. {
  74. if (ops)
  75. module_put(ops->owner);
  76. }
  77. static void flush_route_cache(struct fib_rules_ops *ops)
  78. {
  79. if (ops->flush_cache)
  80. ops->flush_cache(ops);
  81. }
  82. static int __fib_rules_register(struct fib_rules_ops *ops)
  83. {
  84. int err = -EEXIST;
  85. struct fib_rules_ops *o;
  86. struct net *net;
  87. net = ops->fro_net;
  88. if (ops->rule_size < sizeof(struct fib_rule))
  89. return -EINVAL;
  90. if (ops->match == NULL || ops->configure == NULL ||
  91. ops->compare == NULL || ops->fill == NULL ||
  92. ops->action == NULL)
  93. return -EINVAL;
  94. spin_lock(&net->rules_mod_lock);
  95. list_for_each_entry(o, &net->rules_ops, list)
  96. if (ops->family == o->family)
  97. goto errout;
  98. list_add_tail_rcu(&ops->list, &net->rules_ops);
  99. err = 0;
  100. errout:
  101. spin_unlock(&net->rules_mod_lock);
  102. return err;
  103. }
  104. struct fib_rules_ops *
  105. fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
  106. {
  107. struct fib_rules_ops *ops;
  108. int err;
  109. ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
  110. if (ops == NULL)
  111. return ERR_PTR(-ENOMEM);
  112. INIT_LIST_HEAD(&ops->rules_list);
  113. ops->fro_net = net;
  114. err = __fib_rules_register(ops);
  115. if (err) {
  116. kfree(ops);
  117. ops = ERR_PTR(err);
  118. }
  119. return ops;
  120. }
  121. EXPORT_SYMBOL_GPL(fib_rules_register);
  122. static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
  123. {
  124. struct fib_rule *rule, *tmp;
  125. list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
  126. list_del_rcu(&rule->list);
  127. if (ops->delete)
  128. ops->delete(rule);
  129. fib_rule_put(rule);
  130. }
  131. }
  132. void fib_rules_unregister(struct fib_rules_ops *ops)
  133. {
  134. struct net *net = ops->fro_net;
  135. spin_lock(&net->rules_mod_lock);
  136. list_del_rcu(&ops->list);
  137. spin_unlock(&net->rules_mod_lock);
  138. fib_rules_cleanup_ops(ops);
  139. kfree_rcu(ops, rcu);
  140. }
  141. EXPORT_SYMBOL_GPL(fib_rules_unregister);
  142. static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
  143. struct flowi *fl, int flags,
  144. struct fib_lookup_arg *arg)
  145. {
  146. int ret = 0;
  147. if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
  148. goto out;
  149. if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
  150. goto out;
  151. if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
  152. goto out;
  153. if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
  154. goto out;
  155. if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
  156. goto out;
  157. ret = ops->match(rule, fl, flags);
  158. out:
  159. return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
  160. }
  161. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  162. int flags, struct fib_lookup_arg *arg)
  163. {
  164. struct fib_rule *rule;
  165. int err;
  166. rcu_read_lock();
  167. list_for_each_entry_rcu(rule, &ops->rules_list, list) {
  168. jumped:
  169. if (!fib_rule_match(rule, ops, fl, flags, arg))
  170. continue;
  171. if (rule->action == FR_ACT_GOTO) {
  172. struct fib_rule *target;
  173. target = rcu_dereference(rule->ctarget);
  174. if (target == NULL) {
  175. continue;
  176. } else {
  177. rule = target;
  178. goto jumped;
  179. }
  180. } else if (rule->action == FR_ACT_NOP)
  181. continue;
  182. else
  183. err = ops->action(rule, fl, flags, arg);
  184. if (!err && ops->suppress && ops->suppress(rule, arg))
  185. continue;
  186. if (err != -EAGAIN) {
  187. if ((arg->flags & FIB_LOOKUP_NOREF) ||
  188. likely(atomic_inc_not_zero(&rule->refcnt))) {
  189. arg->rule = rule;
  190. goto out;
  191. }
  192. break;
  193. }
  194. }
  195. err = -ESRCH;
  196. out:
  197. rcu_read_unlock();
  198. return err;
  199. }
  200. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  201. static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
  202. struct fib_rules_ops *ops)
  203. {
  204. int err = -EINVAL;
  205. if (frh->src_len)
  206. if (tb[FRA_SRC] == NULL ||
  207. frh->src_len > (ops->addr_size * 8) ||
  208. nla_len(tb[FRA_SRC]) != ops->addr_size)
  209. goto errout;
  210. if (frh->dst_len)
  211. if (tb[FRA_DST] == NULL ||
  212. frh->dst_len > (ops->addr_size * 8) ||
  213. nla_len(tb[FRA_DST]) != ops->addr_size)
  214. goto errout;
  215. err = 0;
  216. errout:
  217. return err;
  218. }
  219. static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
  220. struct nlattr **tb, struct fib_rule *rule)
  221. {
  222. struct fib_rule *r;
  223. list_for_each_entry(r, &ops->rules_list, list) {
  224. if (r->action != rule->action)
  225. continue;
  226. if (r->table != rule->table)
  227. continue;
  228. if (r->pref != rule->pref)
  229. continue;
  230. if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
  231. continue;
  232. if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
  233. continue;
  234. if (r->mark != rule->mark)
  235. continue;
  236. if (r->mark_mask != rule->mark_mask)
  237. continue;
  238. if (r->tun_id != rule->tun_id)
  239. continue;
  240. if (r->fr_net != rule->fr_net)
  241. continue;
  242. if (r->l3mdev != rule->l3mdev)
  243. continue;
  244. if (!ops->compare(r, frh, tb))
  245. continue;
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
  251. {
  252. struct net *net = sock_net(skb->sk);
  253. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  254. struct fib_rules_ops *ops = NULL;
  255. struct fib_rule *rule, *r, *last = NULL;
  256. struct nlattr *tb[FRA_MAX+1];
  257. int err = -EINVAL, unresolved = 0;
  258. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  259. goto errout;
  260. ops = lookup_rules_ops(net, frh->family);
  261. if (ops == NULL) {
  262. err = -EAFNOSUPPORT;
  263. goto errout;
  264. }
  265. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  266. if (err < 0)
  267. goto errout;
  268. err = validate_rulemsg(frh, tb, ops);
  269. if (err < 0)
  270. goto errout;
  271. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  272. if (rule == NULL) {
  273. err = -ENOMEM;
  274. goto errout;
  275. }
  276. rule->fr_net = net;
  277. rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
  278. : fib_default_rule_pref(ops);
  279. if (tb[FRA_IIFNAME]) {
  280. struct net_device *dev;
  281. rule->iifindex = -1;
  282. nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
  283. dev = __dev_get_by_name(net, rule->iifname);
  284. if (dev)
  285. rule->iifindex = dev->ifindex;
  286. }
  287. if (tb[FRA_OIFNAME]) {
  288. struct net_device *dev;
  289. rule->oifindex = -1;
  290. nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
  291. dev = __dev_get_by_name(net, rule->oifname);
  292. if (dev)
  293. rule->oifindex = dev->ifindex;
  294. }
  295. if (tb[FRA_FWMARK]) {
  296. rule->mark = nla_get_u32(tb[FRA_FWMARK]);
  297. if (rule->mark)
  298. /* compatibility: if the mark value is non-zero all bits
  299. * are compared unless a mask is explicitly specified.
  300. */
  301. rule->mark_mask = 0xFFFFFFFF;
  302. }
  303. if (tb[FRA_FWMASK])
  304. rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
  305. if (tb[FRA_TUN_ID])
  306. rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
  307. if (tb[FRA_L3MDEV]) {
  308. #ifdef CONFIG_NET_L3_MASTER_DEV
  309. rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
  310. if (rule->l3mdev != 1)
  311. #endif
  312. goto errout_free;
  313. }
  314. rule->action = frh->action;
  315. rule->flags = frh->flags;
  316. rule->table = frh_get_table(frh, tb);
  317. if (tb[FRA_SUPPRESS_PREFIXLEN])
  318. rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
  319. else
  320. rule->suppress_prefixlen = -1;
  321. if (tb[FRA_SUPPRESS_IFGROUP])
  322. rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
  323. else
  324. rule->suppress_ifgroup = -1;
  325. err = -EINVAL;
  326. if (tb[FRA_GOTO]) {
  327. if (rule->action != FR_ACT_GOTO)
  328. goto errout_free;
  329. rule->target = nla_get_u32(tb[FRA_GOTO]);
  330. /* Backward jumps are prohibited to avoid endless loops */
  331. if (rule->target <= rule->pref)
  332. goto errout_free;
  333. list_for_each_entry(r, &ops->rules_list, list) {
  334. if (r->pref == rule->target) {
  335. RCU_INIT_POINTER(rule->ctarget, r);
  336. break;
  337. }
  338. }
  339. if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
  340. unresolved = 1;
  341. } else if (rule->action == FR_ACT_GOTO)
  342. goto errout_free;
  343. if (rule->l3mdev && rule->table)
  344. goto errout_free;
  345. if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
  346. rule_exists(ops, frh, tb, rule)) {
  347. err = -EEXIST;
  348. goto errout_free;
  349. }
  350. err = ops->configure(rule, skb, frh, tb);
  351. if (err < 0)
  352. goto errout_free;
  353. list_for_each_entry(r, &ops->rules_list, list) {
  354. if (r->pref > rule->pref)
  355. break;
  356. last = r;
  357. }
  358. fib_rule_get(rule);
  359. if (last)
  360. list_add_rcu(&rule->list, &last->list);
  361. else
  362. list_add_rcu(&rule->list, &ops->rules_list);
  363. if (ops->unresolved_rules) {
  364. /*
  365. * There are unresolved goto rules in the list, check if
  366. * any of them are pointing to this new rule.
  367. */
  368. list_for_each_entry(r, &ops->rules_list, list) {
  369. if (r->action == FR_ACT_GOTO &&
  370. r->target == rule->pref &&
  371. rtnl_dereference(r->ctarget) == NULL) {
  372. rcu_assign_pointer(r->ctarget, rule);
  373. if (--ops->unresolved_rules == 0)
  374. break;
  375. }
  376. }
  377. }
  378. if (rule->action == FR_ACT_GOTO)
  379. ops->nr_goto_rules++;
  380. if (unresolved)
  381. ops->unresolved_rules++;
  382. if (rule->tun_id)
  383. ip_tunnel_need_metadata();
  384. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
  385. flush_route_cache(ops);
  386. rules_ops_put(ops);
  387. return 0;
  388. errout_free:
  389. kfree(rule);
  390. errout:
  391. rules_ops_put(ops);
  392. return err;
  393. }
  394. EXPORT_SYMBOL_GPL(fib_nl_newrule);
  395. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
  396. {
  397. struct net *net = sock_net(skb->sk);
  398. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  399. struct fib_rules_ops *ops = NULL;
  400. struct fib_rule *rule, *tmp;
  401. struct nlattr *tb[FRA_MAX+1];
  402. int err = -EINVAL;
  403. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  404. goto errout;
  405. ops = lookup_rules_ops(net, frh->family);
  406. if (ops == NULL) {
  407. err = -EAFNOSUPPORT;
  408. goto errout;
  409. }
  410. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  411. if (err < 0)
  412. goto errout;
  413. err = validate_rulemsg(frh, tb, ops);
  414. if (err < 0)
  415. goto errout;
  416. list_for_each_entry(rule, &ops->rules_list, list) {
  417. if (frh->action && (frh->action != rule->action))
  418. continue;
  419. if (frh_get_table(frh, tb) &&
  420. (frh_get_table(frh, tb) != rule->table))
  421. continue;
  422. if (tb[FRA_PRIORITY] &&
  423. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  424. continue;
  425. if (tb[FRA_IIFNAME] &&
  426. nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
  427. continue;
  428. if (tb[FRA_OIFNAME] &&
  429. nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
  430. continue;
  431. if (tb[FRA_FWMARK] &&
  432. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  433. continue;
  434. if (tb[FRA_FWMASK] &&
  435. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  436. continue;
  437. if (tb[FRA_TUN_ID] &&
  438. (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
  439. continue;
  440. if (tb[FRA_L3MDEV] &&
  441. (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
  442. continue;
  443. if (!ops->compare(rule, frh, tb))
  444. continue;
  445. if (rule->flags & FIB_RULE_PERMANENT) {
  446. err = -EPERM;
  447. goto errout;
  448. }
  449. if (ops->delete) {
  450. err = ops->delete(rule);
  451. if (err)
  452. goto errout;
  453. }
  454. if (rule->tun_id)
  455. ip_tunnel_unneed_metadata();
  456. list_del_rcu(&rule->list);
  457. if (rule->action == FR_ACT_GOTO) {
  458. ops->nr_goto_rules--;
  459. if (rtnl_dereference(rule->ctarget) == NULL)
  460. ops->unresolved_rules--;
  461. }
  462. /*
  463. * Check if this rule is a target to any of them. If so,
  464. * disable them. As this operation is eventually very
  465. * expensive, it is only performed if goto rules have
  466. * actually been added.
  467. */
  468. if (ops->nr_goto_rules > 0) {
  469. list_for_each_entry(tmp, &ops->rules_list, list) {
  470. if (rtnl_dereference(tmp->ctarget) == rule) {
  471. RCU_INIT_POINTER(tmp->ctarget, NULL);
  472. ops->unresolved_rules++;
  473. }
  474. }
  475. }
  476. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  477. NETLINK_CB(skb).portid);
  478. fib_rule_put(rule);
  479. flush_route_cache(ops);
  480. rules_ops_put(ops);
  481. return 0;
  482. }
  483. err = -ENOENT;
  484. errout:
  485. rules_ops_put(ops);
  486. return err;
  487. }
  488. EXPORT_SYMBOL_GPL(fib_nl_delrule);
  489. static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
  490. struct fib_rule *rule)
  491. {
  492. size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
  493. + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
  494. + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
  495. + nla_total_size(4) /* FRA_PRIORITY */
  496. + nla_total_size(4) /* FRA_TABLE */
  497. + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
  498. + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
  499. + nla_total_size(4) /* FRA_FWMARK */
  500. + nla_total_size(4) /* FRA_FWMASK */
  501. + nla_total_size_64bit(8); /* FRA_TUN_ID */
  502. if (ops->nlmsg_payload)
  503. payload += ops->nlmsg_payload(rule);
  504. return payload;
  505. }
  506. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  507. u32 pid, u32 seq, int type, int flags,
  508. struct fib_rules_ops *ops)
  509. {
  510. struct nlmsghdr *nlh;
  511. struct fib_rule_hdr *frh;
  512. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  513. if (nlh == NULL)
  514. return -EMSGSIZE;
  515. frh = nlmsg_data(nlh);
  516. frh->family = ops->family;
  517. frh->table = rule->table;
  518. if (nla_put_u32(skb, FRA_TABLE, rule->table))
  519. goto nla_put_failure;
  520. if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
  521. goto nla_put_failure;
  522. frh->res1 = 0;
  523. frh->res2 = 0;
  524. frh->action = rule->action;
  525. frh->flags = rule->flags;
  526. if (rule->action == FR_ACT_GOTO &&
  527. rcu_access_pointer(rule->ctarget) == NULL)
  528. frh->flags |= FIB_RULE_UNRESOLVED;
  529. if (rule->iifname[0]) {
  530. if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
  531. goto nla_put_failure;
  532. if (rule->iifindex == -1)
  533. frh->flags |= FIB_RULE_IIF_DETACHED;
  534. }
  535. if (rule->oifname[0]) {
  536. if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
  537. goto nla_put_failure;
  538. if (rule->oifindex == -1)
  539. frh->flags |= FIB_RULE_OIF_DETACHED;
  540. }
  541. if ((rule->pref &&
  542. nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
  543. (rule->mark &&
  544. nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
  545. ((rule->mark_mask || rule->mark) &&
  546. nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
  547. (rule->target &&
  548. nla_put_u32(skb, FRA_GOTO, rule->target)) ||
  549. (rule->tun_id &&
  550. nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
  551. (rule->l3mdev &&
  552. nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)))
  553. goto nla_put_failure;
  554. if (rule->suppress_ifgroup != -1) {
  555. if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
  556. goto nla_put_failure;
  557. }
  558. if (ops->fill(rule, skb, frh) < 0)
  559. goto nla_put_failure;
  560. nlmsg_end(skb, nlh);
  561. return 0;
  562. nla_put_failure:
  563. nlmsg_cancel(skb, nlh);
  564. return -EMSGSIZE;
  565. }
  566. static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
  567. struct fib_rules_ops *ops)
  568. {
  569. int idx = 0;
  570. struct fib_rule *rule;
  571. int err = 0;
  572. rcu_read_lock();
  573. list_for_each_entry_rcu(rule, &ops->rules_list, list) {
  574. if (idx < cb->args[1])
  575. goto skip;
  576. err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
  577. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  578. NLM_F_MULTI, ops);
  579. if (err)
  580. break;
  581. skip:
  582. idx++;
  583. }
  584. rcu_read_unlock();
  585. cb->args[1] = idx;
  586. rules_ops_put(ops);
  587. return err;
  588. }
  589. static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
  590. {
  591. struct net *net = sock_net(skb->sk);
  592. struct fib_rules_ops *ops;
  593. int idx = 0, family;
  594. family = rtnl_msg_family(cb->nlh);
  595. if (family != AF_UNSPEC) {
  596. /* Protocol specific dump request */
  597. ops = lookup_rules_ops(net, family);
  598. if (ops == NULL)
  599. return -EAFNOSUPPORT;
  600. dump_rules(skb, cb, ops);
  601. return skb->len;
  602. }
  603. rcu_read_lock();
  604. list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  605. if (idx < cb->args[0] || !try_module_get(ops->owner))
  606. goto skip;
  607. if (dump_rules(skb, cb, ops) < 0)
  608. break;
  609. cb->args[1] = 0;
  610. skip:
  611. idx++;
  612. }
  613. rcu_read_unlock();
  614. cb->args[0] = idx;
  615. return skb->len;
  616. }
  617. static void notify_rule_change(int event, struct fib_rule *rule,
  618. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  619. u32 pid)
  620. {
  621. struct net *net;
  622. struct sk_buff *skb;
  623. int err = -ENOBUFS;
  624. net = ops->fro_net;
  625. skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
  626. if (skb == NULL)
  627. goto errout;
  628. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  629. if (err < 0) {
  630. /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
  631. WARN_ON(err == -EMSGSIZE);
  632. kfree_skb(skb);
  633. goto errout;
  634. }
  635. rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
  636. return;
  637. errout:
  638. if (err < 0)
  639. rtnl_set_sk_err(net, ops->nlgroup, err);
  640. }
  641. static void attach_rules(struct list_head *rules, struct net_device *dev)
  642. {
  643. struct fib_rule *rule;
  644. list_for_each_entry(rule, rules, list) {
  645. if (rule->iifindex == -1 &&
  646. strcmp(dev->name, rule->iifname) == 0)
  647. rule->iifindex = dev->ifindex;
  648. if (rule->oifindex == -1 &&
  649. strcmp(dev->name, rule->oifname) == 0)
  650. rule->oifindex = dev->ifindex;
  651. }
  652. }
  653. static void detach_rules(struct list_head *rules, struct net_device *dev)
  654. {
  655. struct fib_rule *rule;
  656. list_for_each_entry(rule, rules, list) {
  657. if (rule->iifindex == dev->ifindex)
  658. rule->iifindex = -1;
  659. if (rule->oifindex == dev->ifindex)
  660. rule->oifindex = -1;
  661. }
  662. }
  663. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  664. void *ptr)
  665. {
  666. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  667. struct net *net = dev_net(dev);
  668. struct fib_rules_ops *ops;
  669. ASSERT_RTNL();
  670. switch (event) {
  671. case NETDEV_REGISTER:
  672. list_for_each_entry(ops, &net->rules_ops, list)
  673. attach_rules(&ops->rules_list, dev);
  674. break;
  675. case NETDEV_CHANGENAME:
  676. list_for_each_entry(ops, &net->rules_ops, list) {
  677. detach_rules(&ops->rules_list, dev);
  678. attach_rules(&ops->rules_list, dev);
  679. }
  680. break;
  681. case NETDEV_UNREGISTER:
  682. list_for_each_entry(ops, &net->rules_ops, list)
  683. detach_rules(&ops->rules_list, dev);
  684. break;
  685. }
  686. return NOTIFY_DONE;
  687. }
  688. static struct notifier_block fib_rules_notifier = {
  689. .notifier_call = fib_rules_event,
  690. };
  691. static int __net_init fib_rules_net_init(struct net *net)
  692. {
  693. INIT_LIST_HEAD(&net->rules_ops);
  694. spin_lock_init(&net->rules_mod_lock);
  695. return 0;
  696. }
  697. static struct pernet_operations fib_rules_net_ops = {
  698. .init = fib_rules_net_init,
  699. };
  700. static int __init fib_rules_init(void)
  701. {
  702. int err;
  703. rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
  704. rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
  705. rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
  706. err = register_pernet_subsys(&fib_rules_net_ops);
  707. if (err < 0)
  708. goto fail;
  709. err = register_netdevice_notifier(&fib_rules_notifier);
  710. if (err < 0)
  711. goto fail_unregister;
  712. return 0;
  713. fail_unregister:
  714. unregister_pernet_subsys(&fib_rules_net_ops);
  715. fail:
  716. rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
  717. rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
  718. rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
  719. return err;
  720. }
  721. subsys_initcall(fib_rules_init);