ip6_flowlabel.c 17 KB

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