fib_rules.c 19 KB

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