ip6_flowlabel.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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;
  278. int olen;
  279. int addr_type;
  280. int err;
  281. err = -ENOMEM;
  282. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  283. if (fl == NULL)
  284. goto done;
  285. olen = optlen - CMSG_ALIGN(sizeof(*freq));
  286. if (olen > 0) {
  287. struct msghdr msg;
  288. struct flowi flowi;
  289. int junk;
  290. err = -ENOMEM;
  291. fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
  292. if (fl->opt == NULL)
  293. goto done;
  294. memset(fl->opt, 0, sizeof(*fl->opt));
  295. fl->opt->tot_len = sizeof(*fl->opt) + olen;
  296. err = -EFAULT;
  297. if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
  298. goto done;
  299. msg.msg_controllen = olen;
  300. msg.msg_control = (void*)(fl->opt+1);
  301. flowi.oif = 0;
  302. err = datagram_send_ctl(net, &msg, &flowi, fl->opt, &junk, &junk);
  303. if (err)
  304. goto done;
  305. err = -EINVAL;
  306. if (fl->opt->opt_flen)
  307. goto done;
  308. if (fl->opt->opt_nflen == 0) {
  309. kfree(fl->opt);
  310. fl->opt = NULL;
  311. }
  312. }
  313. fl->fl_net = hold_net(net);
  314. fl->expires = jiffies;
  315. err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
  316. if (err)
  317. goto done;
  318. fl->share = freq->flr_share;
  319. addr_type = ipv6_addr_type(&freq->flr_dst);
  320. if ((addr_type&IPV6_ADDR_MAPPED)
  321. || addr_type == IPV6_ADDR_ANY) {
  322. err = -EINVAL;
  323. goto done;
  324. }
  325. ipv6_addr_copy(&fl->dst, &freq->flr_dst);
  326. atomic_set(&fl->users, 1);
  327. switch (fl->share) {
  328. case IPV6_FL_S_EXCL:
  329. case IPV6_FL_S_ANY:
  330. break;
  331. case IPV6_FL_S_PROCESS:
  332. fl->owner = current->pid;
  333. break;
  334. case IPV6_FL_S_USER:
  335. fl->owner = current->euid;
  336. break;
  337. default:
  338. err = -EINVAL;
  339. goto done;
  340. }
  341. return fl;
  342. done:
  343. fl_free(fl);
  344. *err_p = err;
  345. return NULL;
  346. }
  347. static int mem_check(struct sock *sk)
  348. {
  349. struct ipv6_pinfo *np = inet6_sk(sk);
  350. struct ipv6_fl_socklist *sfl;
  351. int room = FL_MAX_SIZE - atomic_read(&fl_size);
  352. int count = 0;
  353. if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
  354. return 0;
  355. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
  356. count++;
  357. if (room <= 0 ||
  358. ((count >= FL_MAX_PER_SOCK ||
  359. (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
  360. && !capable(CAP_NET_ADMIN)))
  361. return -ENOBUFS;
  362. return 0;
  363. }
  364. static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
  365. {
  366. if (h1 == h2)
  367. return 0;
  368. if (h1 == NULL || h2 == NULL)
  369. return 1;
  370. if (h1->hdrlen != h2->hdrlen)
  371. return 1;
  372. return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
  373. }
  374. static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
  375. {
  376. if (o1 == o2)
  377. return 0;
  378. if (o1 == NULL || o2 == NULL)
  379. return 1;
  380. if (o1->opt_nflen != o2->opt_nflen)
  381. return 1;
  382. if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
  383. return 1;
  384. if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
  385. return 1;
  386. if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
  387. return 1;
  388. return 0;
  389. }
  390. static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
  391. struct ip6_flowlabel *fl)
  392. {
  393. write_lock_bh(&ip6_sk_fl_lock);
  394. sfl->fl = fl;
  395. sfl->next = np->ipv6_fl_list;
  396. np->ipv6_fl_list = sfl;
  397. write_unlock_bh(&ip6_sk_fl_lock);
  398. }
  399. int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
  400. {
  401. int err;
  402. struct net *net = sock_net(sk);
  403. struct ipv6_pinfo *np = inet6_sk(sk);
  404. struct in6_flowlabel_req freq;
  405. struct ipv6_fl_socklist *sfl1=NULL;
  406. struct ipv6_fl_socklist *sfl, **sflp;
  407. struct ip6_flowlabel *fl, *fl1 = NULL;
  408. if (optlen < sizeof(freq))
  409. return -EINVAL;
  410. if (copy_from_user(&freq, optval, sizeof(freq)))
  411. return -EFAULT;
  412. switch (freq.flr_action) {
  413. case IPV6_FL_A_PUT:
  414. write_lock_bh(&ip6_sk_fl_lock);
  415. for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
  416. if (sfl->fl->label == freq.flr_label) {
  417. if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
  418. np->flow_label &= ~IPV6_FLOWLABEL_MASK;
  419. *sflp = sfl->next;
  420. write_unlock_bh(&ip6_sk_fl_lock);
  421. fl_release(sfl->fl);
  422. kfree(sfl);
  423. return 0;
  424. }
  425. }
  426. write_unlock_bh(&ip6_sk_fl_lock);
  427. return -ESRCH;
  428. case IPV6_FL_A_RENEW:
  429. read_lock_bh(&ip6_sk_fl_lock);
  430. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
  431. if (sfl->fl->label == freq.flr_label) {
  432. err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
  433. read_unlock_bh(&ip6_sk_fl_lock);
  434. return err;
  435. }
  436. }
  437. read_unlock_bh(&ip6_sk_fl_lock);
  438. if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
  439. fl = fl_lookup(net, freq.flr_label);
  440. if (fl) {
  441. err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
  442. fl_release(fl);
  443. return err;
  444. }
  445. }
  446. return -ESRCH;
  447. case IPV6_FL_A_GET:
  448. if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
  449. return -EINVAL;
  450. fl = fl_create(net, &freq, optval, optlen, &err);
  451. if (fl == NULL)
  452. return err;
  453. sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
  454. if (freq.flr_label) {
  455. err = -EEXIST;
  456. read_lock_bh(&ip6_sk_fl_lock);
  457. for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
  458. if (sfl->fl->label == freq.flr_label) {
  459. if (freq.flr_flags&IPV6_FL_F_EXCL) {
  460. read_unlock_bh(&ip6_sk_fl_lock);
  461. goto done;
  462. }
  463. fl1 = sfl->fl;
  464. atomic_inc(&fl1->users);
  465. break;
  466. }
  467. }
  468. read_unlock_bh(&ip6_sk_fl_lock);
  469. if (fl1 == NULL)
  470. fl1 = fl_lookup(net, freq.flr_label);
  471. if (fl1) {
  472. recheck:
  473. err = -EEXIST;
  474. if (freq.flr_flags&IPV6_FL_F_EXCL)
  475. goto release;
  476. err = -EPERM;
  477. if (fl1->share == IPV6_FL_S_EXCL ||
  478. fl1->share != fl->share ||
  479. fl1->owner != fl->owner)
  480. goto release;
  481. err = -EINVAL;
  482. if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
  483. ipv6_opt_cmp(fl1->opt, fl->opt))
  484. goto release;
  485. err = -ENOMEM;
  486. if (sfl1 == NULL)
  487. goto release;
  488. if (fl->linger > fl1->linger)
  489. fl1->linger = fl->linger;
  490. if ((long)(fl->expires - fl1->expires) > 0)
  491. fl1->expires = fl->expires;
  492. fl_link(np, sfl1, fl1);
  493. fl_free(fl);
  494. return 0;
  495. release:
  496. fl_release(fl1);
  497. goto done;
  498. }
  499. }
  500. err = -ENOENT;
  501. if (!(freq.flr_flags&IPV6_FL_F_CREATE))
  502. goto done;
  503. err = -ENOMEM;
  504. if (sfl1 == NULL || (err = mem_check(sk)) != 0)
  505. goto done;
  506. fl1 = fl_intern(net, fl, freq.flr_label);
  507. if (fl1 != NULL)
  508. goto recheck;
  509. if (!freq.flr_label) {
  510. if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
  511. &fl->label, sizeof(fl->label))) {
  512. /* Intentionally ignore fault. */
  513. }
  514. }
  515. fl_link(np, sfl1, fl);
  516. return 0;
  517. default:
  518. return -EINVAL;
  519. }
  520. done:
  521. fl_free(fl);
  522. kfree(sfl1);
  523. return err;
  524. }
  525. #ifdef CONFIG_PROC_FS
  526. struct ip6fl_iter_state {
  527. struct seq_net_private p;
  528. int bucket;
  529. };
  530. #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
  531. static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
  532. {
  533. struct ip6_flowlabel *fl = NULL;
  534. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  535. struct net *net = seq_file_net(seq);
  536. for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
  537. fl = fl_ht[state->bucket];
  538. while (fl && fl->fl_net != net)
  539. fl = fl->next;
  540. if (fl)
  541. break;
  542. }
  543. return fl;
  544. }
  545. static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
  546. {
  547. struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
  548. struct net *net = seq_file_net(seq);
  549. fl = fl->next;
  550. try_again:
  551. while (fl && fl->fl_net != net)
  552. fl = fl->next;
  553. while (!fl) {
  554. if (++state->bucket <= FL_HASH_MASK) {
  555. fl = fl_ht[state->bucket];
  556. goto try_again;
  557. } else
  558. break;
  559. }
  560. return fl;
  561. }
  562. static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
  563. {
  564. struct ip6_flowlabel *fl = ip6fl_get_first(seq);
  565. if (fl)
  566. while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
  567. --pos;
  568. return pos ? NULL : fl;
  569. }
  570. static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
  571. __acquires(ip6_fl_lock)
  572. {
  573. read_lock_bh(&ip6_fl_lock);
  574. return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  575. }
  576. static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  577. {
  578. struct ip6_flowlabel *fl;
  579. if (v == SEQ_START_TOKEN)
  580. fl = ip6fl_get_first(seq);
  581. else
  582. fl = ip6fl_get_next(seq, v);
  583. ++*pos;
  584. return fl;
  585. }
  586. static void ip6fl_seq_stop(struct seq_file *seq, void *v)
  587. __releases(ip6_fl_lock)
  588. {
  589. read_unlock_bh(&ip6_fl_lock);
  590. }
  591. static int ip6fl_seq_show(struct seq_file *seq, void *v)
  592. {
  593. if (v == SEQ_START_TOKEN)
  594. seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
  595. "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
  596. else {
  597. struct ip6_flowlabel *fl = v;
  598. seq_printf(seq,
  599. "%05X %-1d %-6d %-6d %-6ld %-8ld " NIP6_SEQFMT " %-4d\n",
  600. (unsigned)ntohl(fl->label),
  601. fl->share,
  602. (unsigned)fl->owner,
  603. atomic_read(&fl->users),
  604. fl->linger/HZ,
  605. (long)(fl->expires - jiffies)/HZ,
  606. NIP6(fl->dst),
  607. fl->opt ? fl->opt->opt_nflen : 0);
  608. }
  609. return 0;
  610. }
  611. static const struct seq_operations ip6fl_seq_ops = {
  612. .start = ip6fl_seq_start,
  613. .next = ip6fl_seq_next,
  614. .stop = ip6fl_seq_stop,
  615. .show = ip6fl_seq_show,
  616. };
  617. static int ip6fl_seq_open(struct inode *inode, struct file *file)
  618. {
  619. return seq_open_net(inode, file, &ip6fl_seq_ops,
  620. sizeof(struct ip6fl_iter_state));
  621. }
  622. static const struct file_operations ip6fl_seq_fops = {
  623. .owner = THIS_MODULE,
  624. .open = ip6fl_seq_open,
  625. .read = seq_read,
  626. .llseek = seq_lseek,
  627. .release = seq_release_net,
  628. };
  629. static int ip6_flowlabel_proc_init(struct net *net)
  630. {
  631. if (!proc_net_fops_create(net, "ip6_flowlabel",
  632. S_IRUGO, &ip6fl_seq_fops))
  633. return -ENOMEM;
  634. return 0;
  635. }
  636. static void ip6_flowlabel_proc_fini(struct net *net)
  637. {
  638. proc_net_remove(net, "ip6_flowlabel");
  639. }
  640. #else
  641. static inline int ip6_flowlabel_proc_init(struct net *net)
  642. {
  643. return 0;
  644. }
  645. static inline void ip6_flowlabel_proc_fini(struct net *net)
  646. {
  647. return ;
  648. }
  649. #endif
  650. static inline void ip6_flowlabel_net_exit(struct net *net)
  651. {
  652. ip6_fl_purge(net);
  653. ip6_flowlabel_proc_fini(net);
  654. }
  655. static struct pernet_operations ip6_flowlabel_net_ops = {
  656. .init = ip6_flowlabel_proc_init,
  657. .exit = ip6_flowlabel_net_exit,
  658. };
  659. int ip6_flowlabel_init(void)
  660. {
  661. return register_pernet_subsys(&ip6_flowlabel_net_ops);
  662. }
  663. void ip6_flowlabel_cleanup(void)
  664. {
  665. del_timer(&ip6_fl_gc_timer);
  666. unregister_pernet_subsys(&ip6_flowlabel_net_ops);
  667. }