ip6_flowlabel.c 19 KB

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