ip6_flowlabel.c 17 KB

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