ip6_flowlabel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * ip6_flowlabel.c IPv6 flowlabel manager.
  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
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. */
  11. #include <linux/capability.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/socket.h>
  15. #include <linux/net.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/in6.h>
  19. #include <linux/route.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #include <linux/pid_namespace.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/ipv6.h>
  28. #include <net/ndisc.h>
  29. #include <net/protocol.h>
  30. #include <net/ip6_route.h>
  31. #include <net/addrconf.h>
  32. #include <net/rawv6.h>
  33. #include <net/icmp.h>
  34. #include <net/transp_v6.h>
  35. #include <asm/uaccess.h>
  36. #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
  37. in old IPv6 RFC. Well, it was reasonable value.
  38. */
  39. #define FL_MAX_LINGER 60 /* Maximal linger timeout */
  40. /* FL hash table */
  41. #define FL_MAX_PER_SOCK 32
  42. #define FL_MAX_SIZE 4096
  43. #define FL_HASH_MASK 255
  44. #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
  45. static atomic_t fl_size = ATOMIC_INIT(0);
  46. static struct ip6_flowlabel __rcu *fl_ht[FL_HASH_MASK+1];
  47. static void ip6_fl_gc(unsigned long dummy);
  48. static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0);
  49. /* FL hash table lock: it protects only of GC */
  50. static DEFINE_SPINLOCK(ip6_fl_lock);
  51. /* Big socket sock */
  52. static DEFINE_SPINLOCK(ip6_sk_fl_lock);
  53. #define for_each_fl_rcu(hash, fl) \
  54. for (fl = rcu_dereference_bh(fl_ht[(hash)]); \
  55. fl != NULL; \
  56. fl = rcu_dereference_bh(fl->next))
  57. #define for_each_fl_continue_rcu(fl) \
  58. for (fl = rcu_dereference_bh(fl->next); \
  59. fl != NULL; \
  60. fl = rcu_dereference_bh(fl->next))
  61. #define for_each_sk_fl_rcu(np, sfl) \
  62. for (sfl = rcu_dereference_bh(np->ipv6_fl_list); \
  63. sfl != NULL; \
  64. sfl = rcu_dereference_bh(sfl->next))
  65. static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
  66. {
  67. struct ip6_flowlabel *fl;
  68. for_each_fl_rcu(FL_HASH(label), fl) {
  69. if (fl->label == label && net_eq(fl->fl_net, net))
  70. return fl;
  71. }
  72. return NULL;
  73. }
  74. static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
  75. {
  76. struct ip6_flowlabel *fl;
  77. rcu_read_lock_bh();
  78. fl = __fl_lookup(net, label);
  79. if (fl && !atomic_inc_not_zero(&fl->users))
  80. fl = NULL;
  81. rcu_read_unlock_bh();
  82. return fl;
  83. }
  84. static void fl_free(struct ip6_flowlabel *fl)
  85. {
  86. if (fl) {
  87. if (fl->share == IPV6_FL_S_PROCESS)
  88. put_pid(fl->owner.pid);
  89. release_net(fl->fl_net);
  90. kfree(fl->opt);
  91. kfree_rcu(fl, rcu);
  92. }
  93. }
  94. static void fl_release(struct ip6_flowlabel *fl)
  95. {
  96. spin_lock_bh(&ip6_fl_lock);
  97. fl->lastuse = jiffies;
  98. if (atomic_dec_and_test(&fl->users)) {
  99. unsigned long ttd = fl->lastuse + fl->linger;
  100. if (time_after(ttd, fl->expires))
  101. fl->expires = ttd;
  102. ttd = fl->expires;
  103. if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
  104. struct ipv6_txoptions *opt = fl->opt;
  105. fl->opt = NULL;
  106. kfree(opt);
  107. }
  108. if (!timer_pending(&ip6_fl_gc_timer) ||
  109. time_after(ip6_fl_gc_timer.expires, ttd))
  110. mod_timer(&ip6_fl_gc_timer, ttd);
  111. }
  112. spin_unlock_bh(&ip6_fl_lock);
  113. }
  114. static void ip6_fl_gc(unsigned long dummy)
  115. {
  116. int i;
  117. unsigned long now = jiffies;
  118. unsigned long sched = 0;
  119. spin_lock(&ip6_fl_lock);
  120. for (i=0; i<=FL_HASH_MASK; i++) {
  121. struct ip6_flowlabel *fl, **flp;
  122. flp = &fl_ht[i];
  123. while ((fl = rcu_dereference_protected(*flp,
  124. lockdep_is_held(&ip6_fl_lock))) != NULL) {
  125. if (atomic_read(&fl->users) == 0) {
  126. unsigned long ttd = fl->lastuse + fl->linger;
  127. if (time_after(ttd, fl->expires))
  128. fl->expires = ttd;
  129. ttd = fl->expires;
  130. if (time_after_eq(now, ttd)) {
  131. *flp = fl->next;
  132. fl_free(fl);
  133. atomic_dec(&fl_size);
  134. continue;
  135. }
  136. if (!sched || time_before(ttd, sched))
  137. sched = ttd;
  138. }
  139. flp = &fl->next;
  140. }
  141. }
  142. if (!sched && atomic_read(&fl_size))
  143. sched = now + FL_MAX_LINGER;
  144. if (sched) {
  145. mod_timer(&ip6_fl_gc_timer, sched);
  146. }
  147. spin_unlock(&ip6_fl_lock);
  148. }
  149. static void __net_exit ip6_fl_purge(struct net *net)
  150. {
  151. int i;
  152. spin_lock(&ip6_fl_lock);
  153. for (i = 0; i <= FL_HASH_MASK; i++) {
  154. struct ip6_flowlabel *fl, **flp;
  155. flp = &fl_ht[i];
  156. while ((fl = rcu_dereference_protected(*flp,
  157. lockdep_is_held(&ip6_fl_lock))) != NULL) {
  158. if (net_eq(fl->fl_net, net) &&
  159. atomic_read(&fl->users) == 0) {
  160. *flp = fl->next;
  161. fl_free(fl);
  162. atomic_dec(&fl_size);
  163. continue;
  164. }
  165. flp = &fl->next;
  166. }
  167. }
  168. spin_unlock(&ip6_fl_lock);
  169. }
  170. static struct ip6_flowlabel *fl_intern(struct net *net,
  171. struct ip6_flowlabel *fl, __be32 label)
  172. {
  173. struct ip6_flowlabel *lfl;
  174. fl->label = label & IPV6_FLOWLABEL_MASK;
  175. spin_lock_bh(&ip6_fl_lock);
  176. if (label == 0) {
  177. for (;;) {
  178. fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
  179. if (fl->label) {
  180. lfl = __fl_lookup(net, fl->label);
  181. if (lfl == NULL)
  182. break;
  183. }
  184. }
  185. } else {
  186. /*
  187. * we dropper the ip6_fl_lock, so this entry could reappear
  188. * and we need to recheck with it.
  189. *
  190. * OTOH no need to search the active socket first, like it is
  191. * done in ipv6_flowlabel_opt - sock is locked, so new entry
  192. * with the same label can only appear on another sock
  193. */
  194. lfl = __fl_lookup(net, fl->label);
  195. if (lfl != NULL) {
  196. atomic_inc(&lfl->users);
  197. spin_unlock_bh(&ip6_fl_lock);
  198. return lfl;
  199. }
  200. }
  201. fl->lastuse = jiffies;
  202. fl->next = fl_ht[FL_HASH(fl->label)];
  203. rcu_assign_pointer(fl_ht[FL_HASH(fl->label)], fl);
  204. atomic_inc(&fl_size);
  205. spin_unlock_bh(&ip6_fl_lock);
  206. return NULL;
  207. }
  208. /* Socket flowlabel lists */
  209. struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label)
  210. {
  211. struct ipv6_fl_socklist *sfl;
  212. struct ipv6_pinfo *np = inet6_sk(sk);
  213. label &= IPV6_FLOWLABEL_MASK;
  214. rcu_read_lock_bh();
  215. for_each_sk_fl_rcu(np, sfl) {
  216. struct ip6_flowlabel *fl = sfl->fl;
  217. if (fl->label == label) {
  218. fl->lastuse = jiffies;
  219. atomic_inc(&fl->users);
  220. rcu_read_unlock_bh();
  221. return fl;
  222. }
  223. }
  224. rcu_read_unlock_bh();
  225. return NULL;
  226. }
  227. EXPORT_SYMBOL_GPL(fl6_sock_lookup);
  228. void fl6_free_socklist(struct sock *sk)
  229. {
  230. struct ipv6_pinfo *np = inet6_sk(sk);
  231. struct ipv6_fl_socklist *sfl;
  232. if (!rcu_access_pointer(np->ipv6_fl_list))
  233. return;
  234. spin_lock_bh(&ip6_sk_fl_lock);
  235. while ((sfl = rcu_dereference_protected(np->ipv6_fl_list,
  236. lockdep_is_held(&ip6_sk_fl_lock))) != NULL) {
  237. np->ipv6_fl_list = sfl->next;
  238. spin_unlock_bh(&ip6_sk_fl_lock);
  239. fl_release(sfl->fl);
  240. kfree_rcu(sfl, rcu);
  241. spin_lock_bh(&ip6_sk_fl_lock);
  242. }
  243. spin_unlock_bh(&ip6_sk_fl_lock);
  244. }
  245. /* Service routines */
  246. /*
  247. It is the only difficult place. flowlabel enforces equal headers
  248. before and including routing header, however user may supply options
  249. following rthdr.
  250. */
  251. struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
  252. struct ip6_flowlabel * fl,
  253. struct ipv6_txoptions * fopt)
  254. {
  255. struct ipv6_txoptions * fl_opt = fl->opt;
  256. if (fopt == NULL || fopt->opt_flen == 0)
  257. return fl_opt;
  258. if (fl_opt != NULL) {
  259. opt_space->hopopt = fl_opt->hopopt;
  260. opt_space->dst0opt = fl_opt->dst0opt;
  261. opt_space->srcrt = fl_opt->srcrt;
  262. opt_space->opt_nflen = fl_opt->opt_nflen;
  263. } else {
  264. if (fopt->opt_nflen == 0)
  265. return fopt;
  266. opt_space->hopopt = NULL;
  267. opt_space->dst0opt = NULL;
  268. opt_space->srcrt = NULL;
  269. opt_space->opt_nflen = 0;
  270. }
  271. opt_space->dst1opt = fopt->dst1opt;
  272. opt_space->opt_flen = fopt->opt_flen;
  273. return opt_space;
  274. }
  275. EXPORT_SYMBOL_GPL(fl6_merge_options);
  276. static unsigned long check_linger(unsigned long ttl)
  277. {
  278. if (ttl < FL_MIN_LINGER)
  279. return FL_MIN_LINGER*HZ;
  280. if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
  281. return 0;
  282. return ttl*HZ;
  283. }
  284. static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
  285. {
  286. linger = check_linger(linger);
  287. if (!linger)
  288. return -EPERM;
  289. expires = check_linger(expires);
  290. if (!expires)
  291. return -EPERM;
  292. fl->lastuse = jiffies;
  293. if (time_before(fl->linger, linger))
  294. fl->linger = linger;
  295. if (time_before(expires, fl->linger))
  296. expires = fl->linger;
  297. if (time_before(fl->expires, fl->lastuse + expires))
  298. fl->expires = fl->lastuse + expires;
  299. return 0;
  300. }
  301. static struct ip6_flowlabel *
  302. fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
  303. char __user *optval, int optlen, int *err_p)
  304. {
  305. struct ip6_flowlabel *fl = NULL;
  306. int olen;
  307. int addr_type;
  308. int err;
  309. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  310. err = -EINVAL;
  311. if (olen > 64 * 1024)
  312. goto done;
  313. err = -ENOMEM;
  314. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  315. if (fl == NULL)
  316. goto done;
  317. if (olen > 0) {
  318. struct msghdr msg;
  319. struct flowi6 flowi6;
  320. int junk;
  321. err = -ENOMEM;
  322. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  323. if (fl->opt == NULL)
  324. goto done;
  325. memset(fl->opt, 0, sizeof(*fl->opt));
  326. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  327. err = -EFAULT;
  328. if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
  329. goto done;
  330. msg.msg_controllen = olen;
  331. msg.msg_control = (void*)(fl->opt+1);
  332. memset(&flowi6, 0, sizeof(flowi6));
  333. err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, fl->opt,
  334. &junk, &junk, &junk);
  335. if (err)
  336. goto done;
  337. err = -EINVAL;
  338. if (fl->opt->opt_flen)
  339. goto done;
  340. if (fl->opt->opt_nflen == 0) {
  341. kfree(fl->opt);
  342. fl->opt = NULL;
  343. }
  344. }
  345. fl->fl_net = hold_net(net);
  346. fl->expires = jiffies;
  347. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  348. if (err)
  349. goto done;
  350. fl->share = freq->flr_share;
  351. addr_type = ipv6_addr_type(&freq->flr_dst);
  352. if ((addr_type & IPV6_ADDR_MAPPED) ||
  353. addr_type == IPV6_ADDR_ANY) {
  354. err = -EINVAL;
  355. goto done;
  356. }
  357. fl->dst = freq->flr_dst;
  358. atomic_set(&fl->users, 1);
  359. switch (fl->share) {
  360. case IPV6_FL_S_EXCL:
  361. case IPV6_FL_S_ANY:
  362. break;
  363. case IPV6_FL_S_PROCESS:
  364. fl->owner.pid = get_task_pid(current, PIDTYPE_PID);
  365. break;
  366. case IPV6_FL_S_USER:
  367. fl->owner.uid = current_euid();
  368. break;
  369. default:
  370. err = -EINVAL;
  371. goto done;
  372. }
  373. return fl;
  374. done:
  375. fl_free(fl);
  376. *err_p = err;
  377. return NULL;
  378. }
  379. static int mem_check(struct sock *sk)
  380. {
  381. struct ipv6_pinfo *np = inet6_sk(sk);
  382. struct ipv6_fl_socklist *sfl;
  383. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  384. int count = 0;
  385. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  386. return 0;
  387. for_each_sk_fl_rcu(np, sfl)
  388. count++;
  389. if (room <= 0 ||
  390. ((count >= FL_MAX_PER_SOCK ||
  391. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
  392. !capable(CAP_NET_ADMIN)))
  393. return -ENOBUFS;
  394. return 0;
  395. }
  396. static bool ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
  397. {
  398. if (h1 == h2)
  399. return false;
  400. if (h1 == NULL || h2 == NULL)
  401. return true;
  402. if (h1->hdrlen != h2->hdrlen)
  403. return true;
  404. return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
  405. }
  406. static bool ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
  407. {
  408. if (o1 == o2)
  409. return false;
  410. if (o1 == NULL || o2 == NULL)
  411. return true;
  412. if (o1->opt_nflen != o2->opt_nflen)
  413. return true;
  414. if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
  415. return true;
  416. if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
  417. return true;
  418. if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
  419. return true;
  420. return false;
  421. }
  422. static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
  423. struct ip6_flowlabel *fl)
  424. {
  425. spin_lock_bh(&ip6_sk_fl_lock);
  426. sfl->fl = fl;
  427. sfl->next = np->ipv6_fl_list;
  428. rcu_assign_pointer(np->ipv6_fl_list, sfl);
  429. spin_unlock_bh(&ip6_sk_fl_lock);
  430. }
  431. int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
  432. {
  433. int uninitialized_var(err);
  434. struct net *net = sock_net(sk);
  435. struct ipv6_pinfo *np = inet6_sk(sk);
  436. struct in6_flowlabel_req freq;
  437. struct ipv6_fl_socklist *sfl1=NULL;
  438. struct ipv6_fl_socklist *sfl, **sflp;
  439. struct ip6_flowlabel *fl, *fl1 = NULL;
  440. if (optlen < sizeof(freq))
  441. return -EINVAL;
  442. if (copy_from_user(&freq, optval, sizeof(freq)))
  443. return -EFAULT;
  444. switch (freq.flr_action) {
  445. case IPV6_FL_A_PUT:
  446. spin_lock_bh(&ip6_sk_fl_lock);
  447. for (sflp = &np->ipv6_fl_list;
  448. (sfl = rcu_dereference(*sflp))!=NULL;
  449. sflp = &sfl->next) {
  450. if (sfl->fl->label == freq.flr_label) {
  451. if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
  452. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  453. *sflp = rcu_dereference(sfl->next);
  454. spin_unlock_bh(&ip6_sk_fl_lock);
  455. fl_release(sfl->fl);
  456. kfree_rcu(sfl, rcu);
  457. return 0;
  458. }
  459. }
  460. spin_unlock_bh(&ip6_sk_fl_lock);
  461. return -ESRCH;
  462. case IPV6_FL_A_RENEW:
  463. rcu_read_lock_bh();
  464. for_each_sk_fl_rcu(np, sfl) {
  465. if (sfl->fl->label == freq.flr_label) {
  466. err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
  467. rcu_read_unlock_bh();
  468. return err;
  469. }
  470. }
  471. rcu_read_unlock_bh();
  472. if (freq.flr_share == IPV6_FL_S_NONE &&
  473. ns_capable(net->user_ns, CAP_NET_ADMIN)) {
  474. fl = fl_lookup(net, freq.flr_label);
  475. if (fl) {
  476. err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
  477. fl_release(fl);
  478. return err;
  479. }
  480. }
  481. return -ESRCH;
  482. case IPV6_FL_A_GET:
  483. if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
  484. return -EINVAL;
  485. fl = fl_create(net, sk, &freq, optval, optlen, &err);
  486. if (fl == NULL)
  487. return err;
  488. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  489. if (freq.flr_label) {
  490. err = -EEXIST;
  491. rcu_read_lock_bh();
  492. for_each_sk_fl_rcu(np, sfl) {
  493. if (sfl->fl->label == freq.flr_label) {
  494. if (freq.flr_flags&IPV6_FL_F_EXCL) {
  495. rcu_read_unlock_bh();
  496. goto done;
  497. }
  498. fl1 = sfl->fl;
  499. atomic_inc(&fl1->users);
  500. break;
  501. }
  502. }
  503. rcu_read_unlock_bh();
  504. if (fl1 == NULL)
  505. fl1 = fl_lookup(net, freq.flr_label);
  506. if (fl1) {
  507. recheck:
  508. err = -EEXIST;
  509. if (freq.flr_flags&IPV6_FL_F_EXCL)
  510. goto release;
  511. err = -EPERM;
  512. if (fl1->share == IPV6_FL_S_EXCL ||
  513. fl1->share != fl->share ||
  514. ((fl1->share == IPV6_FL_S_PROCESS) &&
  515. (fl1->owner.pid == fl->owner.pid)) ||
  516. ((fl1->share == IPV6_FL_S_USER) &&
  517. uid_eq(fl1->owner.uid, fl->owner.uid)))
  518. goto release;
  519. err = -EINVAL;
  520. if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
  521. ipv6_opt_cmp(fl1->opt, fl->opt))
  522. goto release;
  523. err = -ENOMEM;
  524. if (sfl1 == NULL)
  525. goto release;
  526. if (fl->linger > fl1->linger)
  527. fl1->linger = fl->linger;
  528. if ((long)(fl->expires - fl1->expires) > 0)
  529. fl1->expires = fl->expires;
  530. fl_link(np, sfl1, fl1);
  531. fl_free(fl);
  532. return 0;
  533. release:
  534. fl_release(fl1);
  535. goto done;
  536. }
  537. }
  538. err = -ENOENT;
  539. if (!(freq.flr_flags&IPV6_FL_F_CREATE))
  540. goto done;
  541. err = -ENOMEM;
  542. if (sfl1 == NULL || (err = mem_check(sk)) != 0)
  543. goto done;
  544. fl1 = fl_intern(net, fl, freq.flr_label);
  545. if (fl1 != NULL)
  546. goto recheck;
  547. if (!freq.flr_label) {
  548. if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
  549. &fl->label, sizeof(fl->label))) {
  550. /* Intentionally ignore fault. */
  551. }
  552. }
  553. fl_link(np, sfl1, fl);
  554. return 0;
  555. default:
  556. return -EINVAL;
  557. }
  558. done:
  559. fl_free(fl);
  560. kfree(sfl1);
  561. return err;
  562. }
  563. #ifdef CONFIG_PROC_FS
  564. struct ip6fl_iter_state {
  565. struct seq_net_private p;
  566. struct pid_namespace *pid_ns;
  567. int bucket;
  568. };
  569. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  570. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  571. {
  572. struct ip6_flowlabel *fl = NULL;
  573. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  574. struct net *net = seq_file_net(seq);
  575. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  576. for_each_fl_rcu(state->bucket, fl) {
  577. if (net_eq(fl->fl_net, net))
  578. goto out;
  579. }
  580. }
  581. fl = NULL;
  582. out:
  583. return fl;
  584. }
  585. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  586. {
  587. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  588. struct net *net = seq_file_net(seq);
  589. for_each_fl_continue_rcu(fl) {
  590. if (net_eq(fl->fl_net, net))
  591. goto out;
  592. }
  593. try_again:
  594. if (++state->bucket <= FL_HASH_MASK) {
  595. for_each_fl_rcu(state->bucket, fl) {
  596. if (net_eq(fl->fl_net, net))
  597. goto out;
  598. }
  599. goto try_again;
  600. }
  601. fl = NULL;
  602. out:
  603. return fl;
  604. }
  605. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  606. {
  607. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  608. if (fl)
  609. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  610. --pos;
  611. return pos ? NULL : fl;
  612. }
  613. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  614. __acquires(RCU)
  615. {
  616. rcu_read_lock_bh();
  617. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  618. }
  619. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  620. {
  621. struct ip6_flowlabel *fl;
  622. if (v == SEQ_START_TOKEN)
  623. fl = ip6fl_get_first(seq);
  624. else
  625. fl = ip6fl_get_next(seq, v);
  626. ++*pos;
  627. return fl;
  628. }
  629. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  630. __releases(RCU)
  631. {
  632. rcu_read_unlock_bh();
  633. }
  634. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  635. {
  636. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  637. if (v == SEQ_START_TOKEN)
  638. seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
  639. "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
  640. else {
  641. struct ip6_flowlabel *fl = v;
  642. seq_printf(seq,
  643. "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
  644. (unsigned int)ntohl(fl->label),
  645. fl->share,
  646. ((fl->share == IPV6_FL_S_PROCESS) ?
  647. pid_nr_ns(fl->owner.pid, state->pid_ns) :
  648. ((fl->share == IPV6_FL_S_USER) ?
  649. from_kuid_munged(seq_user_ns(seq), fl->owner.uid) :
  650. 0)),
  651. atomic_read(&fl->users),
  652. fl->linger/HZ,
  653. (long)(fl->expires - jiffies)/HZ,
  654. &fl->dst,
  655. fl->opt ? fl->opt->opt_nflen : 0);
  656. }
  657. return 0;
  658. }
  659. static const struct seq_operations ip6fl_seq_ops = {
  660. .start = ip6fl_seq_start,
  661. .next = ip6fl_seq_next,
  662. .stop = ip6fl_seq_stop,
  663. .show = ip6fl_seq_show,
  664. };
  665. static int ip6fl_seq_open(struct inode *inode, struct file *file)
  666. {
  667. struct seq_file *seq;
  668. struct ip6fl_iter_state *state;
  669. int err;
  670. err = seq_open_net(inode, file, &ip6fl_seq_ops,
  671. sizeof(struct ip6fl_iter_state));
  672. if (!err) {
  673. seq = file->private_data;
  674. state = ip6fl_seq_private(seq);
  675. rcu_read_lock();
  676. state->pid_ns = get_pid_ns(task_active_pid_ns(current));
  677. rcu_read_unlock();
  678. }
  679. return err;
  680. }
  681. static int ip6fl_seq_release(struct inode *inode, struct file *file)
  682. {
  683. struct seq_file *seq = file->private_data;
  684. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  685. put_pid_ns(state->pid_ns);
  686. return seq_release_net(inode, file);
  687. }
  688. static const struct file_operations ip6fl_seq_fops = {
  689. .owner = THIS_MODULE,
  690. .open = ip6fl_seq_open,
  691. .read = seq_read,
  692. .llseek = seq_lseek,
  693. .release = ip6fl_seq_release,
  694. };
  695. static int __net_init ip6_flowlabel_proc_init(struct net *net)
  696. {
  697. if (!proc_create("ip6_flowlabel", S_IRUGO, net->proc_net,
  698. &ip6fl_seq_fops))
  699. return -ENOMEM;
  700. return 0;
  701. }
  702. static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
  703. {
  704. remove_proc_entry("ip6_flowlabel", net->proc_net);
  705. }
  706. #else
  707. static inline int ip6_flowlabel_proc_init(struct net *net)
  708. {
  709. return 0;
  710. }
  711. static inline void ip6_flowlabel_proc_fini(struct net *net)
  712. {
  713. }
  714. #endif
  715. static void __net_exit ip6_flowlabel_net_exit(struct net *net)
  716. {
  717. ip6_fl_purge(net);
  718. ip6_flowlabel_proc_fini(net);
  719. }
  720. static struct pernet_operations ip6_flowlabel_net_ops = {
  721. .init = ip6_flowlabel_proc_init,
  722. .exit = ip6_flowlabel_net_exit,
  723. };
  724. int ip6_flowlabel_init(void)
  725. {
  726. return register_pernet_subsys(&ip6_flowlabel_net_ops);
  727. }
  728. void ip6_flowlabel_cleanup(void)
  729. {
  730. del_timer(&ip6_fl_gc_timer);
  731. unregister_pernet_subsys(&ip6_flowlabel_net_ops);
  732. }