fib_rules.c 17 KB

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