fib_rules.c 16 KB

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