fib_rules.c 18 KB

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