fib_rules.c 18 KB

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