ip6_flowlabel.c 17 KB

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