fib_rules.c 16 KB

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