net_namespace.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <linux/workqueue.h>
  4. #include <linux/rtnetlink.h>
  5. #include <linux/cache.h>
  6. #include <linux/slab.h>
  7. #include <linux/list.h>
  8. #include <linux/delay.h>
  9. #include <linux/sched.h>
  10. #include <linux/idr.h>
  11. #include <linux/rculist.h>
  12. #include <linux/nsproxy.h>
  13. #include <linux/fs.h>
  14. #include <linux/proc_ns.h>
  15. #include <linux/file.h>
  16. #include <linux/export.h>
  17. #include <linux/user_namespace.h>
  18. #include <linux/net_namespace.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/uidgid.h>
  21. #include <linux/cookie.h>
  22. #include <linux/proc_fs.h>
  23. #include <net/sock.h>
  24. #include <net/netlink.h>
  25. #include <net/net_namespace.h>
  26. #include <net/netns/generic.h>
  27. /*
  28. * Our network namespace constructor/destructor lists
  29. */
  30. static LIST_HEAD(pernet_list);
  31. static struct list_head *first_device = &pernet_list;
  32. LIST_HEAD(net_namespace_list);
  33. EXPORT_SYMBOL_GPL(net_namespace_list);
  34. /* Protects net_namespace_list. Nests iside rtnl_lock() */
  35. DECLARE_RWSEM(net_rwsem);
  36. EXPORT_SYMBOL_GPL(net_rwsem);
  37. #ifdef CONFIG_KEYS
  38. static struct key_tag init_net_key_domain = { .usage = REFCOUNT_INIT(1) };
  39. #endif
  40. struct net init_net;
  41. EXPORT_SYMBOL(init_net);
  42. static bool init_net_initialized;
  43. /*
  44. * pernet_ops_rwsem: protects: pernet_list, net_generic_ids,
  45. * init_net_initialized and first_device pointer.
  46. * This is internal net namespace object. Please, don't use it
  47. * outside.
  48. */
  49. DECLARE_RWSEM(pernet_ops_rwsem);
  50. EXPORT_SYMBOL_GPL(pernet_ops_rwsem);
  51. #define MIN_PERNET_OPS_ID \
  52. ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
  53. #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
  54. static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
  55. DEFINE_COOKIE(net_cookie);
  56. static struct net_generic *net_alloc_generic(void)
  57. {
  58. unsigned int gen_ptrs = READ_ONCE(max_gen_ptrs);
  59. unsigned int generic_size;
  60. struct net_generic *ng;
  61. generic_size = offsetof(struct net_generic, ptr[gen_ptrs]);
  62. ng = kzalloc(generic_size, GFP_KERNEL);
  63. if (ng)
  64. ng->s.len = gen_ptrs;
  65. return ng;
  66. }
  67. static int net_assign_generic(struct net *net, unsigned int id, void *data)
  68. {
  69. struct net_generic *ng, *old_ng;
  70. BUG_ON(id < MIN_PERNET_OPS_ID);
  71. old_ng = rcu_dereference_protected(net->gen,
  72. lockdep_is_held(&pernet_ops_rwsem));
  73. if (old_ng->s.len > id) {
  74. old_ng->ptr[id] = data;
  75. return 0;
  76. }
  77. ng = net_alloc_generic();
  78. if (!ng)
  79. return -ENOMEM;
  80. /*
  81. * Some synchronisation notes:
  82. *
  83. * The net_generic explores the net->gen array inside rcu
  84. * read section. Besides once set the net->gen->ptr[x]
  85. * pointer never changes (see rules in netns/generic.h).
  86. *
  87. * That said, we simply duplicate this array and schedule
  88. * the old copy for kfree after a grace period.
  89. */
  90. memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
  91. (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
  92. ng->ptr[id] = data;
  93. rcu_assign_pointer(net->gen, ng);
  94. kfree_rcu(old_ng, s.rcu);
  95. return 0;
  96. }
  97. static int ops_init(const struct pernet_operations *ops, struct net *net)
  98. {
  99. struct net_generic *ng;
  100. int err = -ENOMEM;
  101. void *data = NULL;
  102. if (ops->id) {
  103. data = kzalloc(ops->size, GFP_KERNEL);
  104. if (!data)
  105. goto out;
  106. err = net_assign_generic(net, *ops->id, data);
  107. if (err)
  108. goto cleanup;
  109. }
  110. err = 0;
  111. if (ops->init)
  112. err = ops->init(net);
  113. if (!err)
  114. return 0;
  115. if (ops->id) {
  116. ng = rcu_dereference_protected(net->gen,
  117. lockdep_is_held(&pernet_ops_rwsem));
  118. ng->ptr[*ops->id] = NULL;
  119. }
  120. cleanup:
  121. kfree(data);
  122. out:
  123. return err;
  124. }
  125. static void ops_pre_exit_list(const struct pernet_operations *ops,
  126. struct list_head *net_exit_list)
  127. {
  128. struct net *net;
  129. if (ops->pre_exit) {
  130. list_for_each_entry(net, net_exit_list, exit_list)
  131. ops->pre_exit(net);
  132. }
  133. }
  134. static void ops_exit_list(const struct pernet_operations *ops,
  135. struct list_head *net_exit_list)
  136. {
  137. struct net *net;
  138. if (ops->exit) {
  139. list_for_each_entry(net, net_exit_list, exit_list) {
  140. ops->exit(net);
  141. cond_resched();
  142. }
  143. }
  144. if (ops->exit_batch)
  145. ops->exit_batch(net_exit_list);
  146. }
  147. static void ops_free_list(const struct pernet_operations *ops,
  148. struct list_head *net_exit_list)
  149. {
  150. struct net *net;
  151. if (ops->id) {
  152. list_for_each_entry(net, net_exit_list, exit_list)
  153. kfree(net_generic(net, *ops->id));
  154. }
  155. }
  156. /* should be called with nsid_lock held */
  157. static int alloc_netid(struct net *net, struct net *peer, int reqid)
  158. {
  159. int min = 0, max = 0;
  160. if (reqid >= 0) {
  161. min = reqid;
  162. max = reqid + 1;
  163. }
  164. return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
  165. }
  166. /* This function is used by idr_for_each(). If net is equal to peer, the
  167. * function returns the id so that idr_for_each() stops. Because we cannot
  168. * returns the id 0 (idr_for_each() will not stop), we return the magic value
  169. * NET_ID_ZERO (-1) for it.
  170. */
  171. #define NET_ID_ZERO -1
  172. static int net_eq_idr(int id, void *net, void *peer)
  173. {
  174. if (net_eq(net, peer))
  175. return id ? : NET_ID_ZERO;
  176. return 0;
  177. }
  178. /* Must be called from RCU-critical section or with nsid_lock held */
  179. static int __peernet2id(const struct net *net, struct net *peer)
  180. {
  181. int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
  182. /* Magic value for id 0. */
  183. if (id == NET_ID_ZERO)
  184. return 0;
  185. if (id > 0)
  186. return id;
  187. return NETNSA_NSID_NOT_ASSIGNED;
  188. }
  189. static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
  190. struct nlmsghdr *nlh, gfp_t gfp);
  191. /* This function returns the id of a peer netns. If no id is assigned, one will
  192. * be allocated and returned.
  193. */
  194. int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp)
  195. {
  196. int id;
  197. if (refcount_read(&net->ns.count) == 0)
  198. return NETNSA_NSID_NOT_ASSIGNED;
  199. spin_lock_bh(&net->nsid_lock);
  200. id = __peernet2id(net, peer);
  201. if (id >= 0) {
  202. spin_unlock_bh(&net->nsid_lock);
  203. return id;
  204. }
  205. /* When peer is obtained from RCU lists, we may race with
  206. * its cleanup. Check whether it's alive, and this guarantees
  207. * we never hash a peer back to net->netns_ids, after it has
  208. * just been idr_remove()'d from there in cleanup_net().
  209. */
  210. if (!maybe_get_net(peer)) {
  211. spin_unlock_bh(&net->nsid_lock);
  212. return NETNSA_NSID_NOT_ASSIGNED;
  213. }
  214. id = alloc_netid(net, peer, -1);
  215. spin_unlock_bh(&net->nsid_lock);
  216. put_net(peer);
  217. if (id < 0)
  218. return NETNSA_NSID_NOT_ASSIGNED;
  219. rtnl_net_notifyid(net, RTM_NEWNSID, id, 0, NULL, gfp);
  220. return id;
  221. }
  222. EXPORT_SYMBOL_GPL(peernet2id_alloc);
  223. /* This function returns, if assigned, the id of a peer netns. */
  224. int peernet2id(const struct net *net, struct net *peer)
  225. {
  226. int id;
  227. rcu_read_lock();
  228. id = __peernet2id(net, peer);
  229. rcu_read_unlock();
  230. return id;
  231. }
  232. EXPORT_SYMBOL(peernet2id);
  233. /* This function returns true is the peer netns has an id assigned into the
  234. * current netns.
  235. */
  236. bool peernet_has_id(const struct net *net, struct net *peer)
  237. {
  238. return peernet2id(net, peer) >= 0;
  239. }
  240. struct net *get_net_ns_by_id(const struct net *net, int id)
  241. {
  242. struct net *peer;
  243. if (id < 0)
  244. return NULL;
  245. rcu_read_lock();
  246. peer = idr_find(&net->netns_ids, id);
  247. if (peer)
  248. peer = maybe_get_net(peer);
  249. rcu_read_unlock();
  250. return peer;
  251. }
  252. EXPORT_SYMBOL_GPL(get_net_ns_by_id);
  253. static __net_init void preinit_net_sysctl(struct net *net)
  254. {
  255. net->core.sysctl_somaxconn = SOMAXCONN;
  256. /* Limits per socket sk_omem_alloc usage.
  257. * TCP zerocopy regular usage needs 128 KB.
  258. */
  259. net->core.sysctl_optmem_max = 128 * 1024;
  260. net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
  261. }
  262. /* init code that must occur even if setup_net() is not called. */
  263. static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns)
  264. {
  265. refcount_set(&net->passive, 1);
  266. refcount_set(&net->ns.count, 1);
  267. ref_tracker_dir_init(&net->refcnt_tracker, 128, "net refcnt");
  268. ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net notrefcnt");
  269. get_random_bytes(&net->hash_mix, sizeof(u32));
  270. net->dev_base_seq = 1;
  271. net->user_ns = user_ns;
  272. idr_init(&net->netns_ids);
  273. spin_lock_init(&net->nsid_lock);
  274. mutex_init(&net->ipv4.ra_mutex);
  275. preinit_net_sysctl(net);
  276. }
  277. /*
  278. * setup_net runs the initializers for the network namespace object.
  279. */
  280. static __net_init int setup_net(struct net *net)
  281. {
  282. /* Must be called with pernet_ops_rwsem held */
  283. const struct pernet_operations *ops, *saved_ops;
  284. LIST_HEAD(net_exit_list);
  285. LIST_HEAD(dev_kill_list);
  286. int error = 0;
  287. preempt_disable();
  288. net->net_cookie = gen_cookie_next(&net_cookie);
  289. preempt_enable();
  290. list_for_each_entry(ops, &pernet_list, list) {
  291. error = ops_init(ops, net);
  292. if (error < 0)
  293. goto out_undo;
  294. }
  295. down_write(&net_rwsem);
  296. list_add_tail_rcu(&net->list, &net_namespace_list);
  297. up_write(&net_rwsem);
  298. out:
  299. return error;
  300. out_undo:
  301. /* Walk through the list backwards calling the exit functions
  302. * for the pernet modules whose init functions did not fail.
  303. */
  304. list_add(&net->exit_list, &net_exit_list);
  305. saved_ops = ops;
  306. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  307. ops_pre_exit_list(ops, &net_exit_list);
  308. synchronize_rcu();
  309. ops = saved_ops;
  310. rtnl_lock();
  311. list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
  312. if (ops->exit_batch_rtnl)
  313. ops->exit_batch_rtnl(&net_exit_list, &dev_kill_list);
  314. }
  315. unregister_netdevice_many(&dev_kill_list);
  316. rtnl_unlock();
  317. ops = saved_ops;
  318. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  319. ops_exit_list(ops, &net_exit_list);
  320. ops = saved_ops;
  321. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  322. ops_free_list(ops, &net_exit_list);
  323. rcu_barrier();
  324. goto out;
  325. }
  326. #ifdef CONFIG_NET_NS
  327. static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
  328. {
  329. return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
  330. }
  331. static void dec_net_namespaces(struct ucounts *ucounts)
  332. {
  333. dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
  334. }
  335. static struct kmem_cache *net_cachep __ro_after_init;
  336. static struct workqueue_struct *netns_wq;
  337. static struct net *net_alloc(void)
  338. {
  339. struct net *net = NULL;
  340. struct net_generic *ng;
  341. ng = net_alloc_generic();
  342. if (!ng)
  343. goto out;
  344. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  345. if (!net)
  346. goto out_free;
  347. #ifdef CONFIG_KEYS
  348. net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL);
  349. if (!net->key_domain)
  350. goto out_free_2;
  351. refcount_set(&net->key_domain->usage, 1);
  352. #endif
  353. rcu_assign_pointer(net->gen, ng);
  354. out:
  355. return net;
  356. #ifdef CONFIG_KEYS
  357. out_free_2:
  358. kmem_cache_free(net_cachep, net);
  359. net = NULL;
  360. #endif
  361. out_free:
  362. kfree(ng);
  363. goto out;
  364. }
  365. static LLIST_HEAD(defer_free_list);
  366. static void net_complete_free(void)
  367. {
  368. struct llist_node *kill_list;
  369. struct net *net, *next;
  370. /* Get the list of namespaces to free from last round. */
  371. kill_list = llist_del_all(&defer_free_list);
  372. llist_for_each_entry_safe(net, next, kill_list, defer_free_list)
  373. kmem_cache_free(net_cachep, net);
  374. }
  375. void net_passive_dec(struct net *net)
  376. {
  377. if (refcount_dec_and_test(&net->passive)) {
  378. kfree(rcu_access_pointer(net->gen));
  379. /* There should not be any trackers left there. */
  380. ref_tracker_dir_exit(&net->notrefcnt_tracker);
  381. /* Wait for an extra rcu_barrier() before final free. */
  382. llist_add(&net->defer_free_list, &defer_free_list);
  383. }
  384. }
  385. void net_drop_ns(void *p)
  386. {
  387. struct net *net = (struct net *)p;
  388. if (net)
  389. net_passive_dec(net);
  390. }
  391. struct net *copy_net_ns(unsigned long flags,
  392. struct user_namespace *user_ns, struct net *old_net)
  393. {
  394. struct ucounts *ucounts;
  395. struct net *net;
  396. int rv;
  397. if (!(flags & CLONE_NEWNET))
  398. return get_net(old_net);
  399. ucounts = inc_net_namespaces(user_ns);
  400. if (!ucounts)
  401. return ERR_PTR(-ENOSPC);
  402. net = net_alloc();
  403. if (!net) {
  404. rv = -ENOMEM;
  405. goto dec_ucounts;
  406. }
  407. preinit_net(net, user_ns);
  408. net->ucounts = ucounts;
  409. get_user_ns(user_ns);
  410. rv = down_read_killable(&pernet_ops_rwsem);
  411. if (rv < 0)
  412. goto put_userns;
  413. rv = setup_net(net);
  414. up_read(&pernet_ops_rwsem);
  415. if (rv < 0) {
  416. put_userns:
  417. #ifdef CONFIG_KEYS
  418. key_remove_domain(net->key_domain);
  419. #endif
  420. put_user_ns(user_ns);
  421. net_passive_dec(net);
  422. dec_ucounts:
  423. dec_net_namespaces(ucounts);
  424. return ERR_PTR(rv);
  425. }
  426. return net;
  427. }
  428. /**
  429. * net_ns_get_ownership - get sysfs ownership data for @net
  430. * @net: network namespace in question (can be NULL)
  431. * @uid: kernel user ID for sysfs objects
  432. * @gid: kernel group ID for sysfs objects
  433. *
  434. * Returns the uid/gid pair of root in the user namespace associated with the
  435. * given network namespace.
  436. */
  437. void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
  438. {
  439. if (net) {
  440. kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
  441. kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
  442. if (uid_valid(ns_root_uid))
  443. *uid = ns_root_uid;
  444. if (gid_valid(ns_root_gid))
  445. *gid = ns_root_gid;
  446. } else {
  447. *uid = GLOBAL_ROOT_UID;
  448. *gid = GLOBAL_ROOT_GID;
  449. }
  450. }
  451. EXPORT_SYMBOL_GPL(net_ns_get_ownership);
  452. static void unhash_nsid(struct net *net, struct net *last)
  453. {
  454. struct net *tmp;
  455. /* This function is only called from cleanup_net() work,
  456. * and this work is the only process, that may delete
  457. * a net from net_namespace_list. So, when the below
  458. * is executing, the list may only grow. Thus, we do not
  459. * use for_each_net_rcu() or net_rwsem.
  460. */
  461. for_each_net(tmp) {
  462. int id;
  463. spin_lock_bh(&tmp->nsid_lock);
  464. id = __peernet2id(tmp, net);
  465. if (id >= 0)
  466. idr_remove(&tmp->netns_ids, id);
  467. spin_unlock_bh(&tmp->nsid_lock);
  468. if (id >= 0)
  469. rtnl_net_notifyid(tmp, RTM_DELNSID, id, 0, NULL,
  470. GFP_KERNEL);
  471. if (tmp == last)
  472. break;
  473. }
  474. spin_lock_bh(&net->nsid_lock);
  475. idr_destroy(&net->netns_ids);
  476. spin_unlock_bh(&net->nsid_lock);
  477. }
  478. static LLIST_HEAD(cleanup_list);
  479. static void cleanup_net(struct work_struct *work)
  480. {
  481. const struct pernet_operations *ops;
  482. struct net *net, *tmp, *last;
  483. struct llist_node *net_kill_list;
  484. LIST_HEAD(net_exit_list);
  485. LIST_HEAD(dev_kill_list);
  486. /* Atomically snapshot the list of namespaces to cleanup */
  487. net_kill_list = llist_del_all(&cleanup_list);
  488. down_read(&pernet_ops_rwsem);
  489. /* Don't let anyone else find us. */
  490. down_write(&net_rwsem);
  491. llist_for_each_entry(net, net_kill_list, cleanup_list)
  492. list_del_rcu(&net->list);
  493. /* Cache last net. After we unlock rtnl, no one new net
  494. * added to net_namespace_list can assign nsid pointer
  495. * to a net from net_kill_list (see peernet2id_alloc()).
  496. * So, we skip them in unhash_nsid().
  497. *
  498. * Note, that unhash_nsid() does not delete nsid links
  499. * between net_kill_list's nets, as they've already
  500. * deleted from net_namespace_list. But, this would be
  501. * useless anyway, as netns_ids are destroyed there.
  502. */
  503. last = list_last_entry(&net_namespace_list, struct net, list);
  504. up_write(&net_rwsem);
  505. llist_for_each_entry(net, net_kill_list, cleanup_list) {
  506. unhash_nsid(net, last);
  507. list_add_tail(&net->exit_list, &net_exit_list);
  508. }
  509. /* Run all of the network namespace pre_exit methods */
  510. list_for_each_entry_reverse(ops, &pernet_list, list)
  511. ops_pre_exit_list(ops, &net_exit_list);
  512. /*
  513. * Another CPU might be rcu-iterating the list, wait for it.
  514. * This needs to be before calling the exit() notifiers, so
  515. * the rcu_barrier() below isn't sufficient alone.
  516. * Also the pre_exit() and exit() methods need this barrier.
  517. */
  518. synchronize_rcu_expedited();
  519. rtnl_lock();
  520. list_for_each_entry_reverse(ops, &pernet_list, list) {
  521. if (ops->exit_batch_rtnl)
  522. ops->exit_batch_rtnl(&net_exit_list, &dev_kill_list);
  523. }
  524. unregister_netdevice_many(&dev_kill_list);
  525. rtnl_unlock();
  526. /* Run all of the network namespace exit methods */
  527. list_for_each_entry_reverse(ops, &pernet_list, list)
  528. ops_exit_list(ops, &net_exit_list);
  529. /* Free the net generic variables */
  530. list_for_each_entry_reverse(ops, &pernet_list, list)
  531. ops_free_list(ops, &net_exit_list);
  532. up_read(&pernet_ops_rwsem);
  533. /* Ensure there are no outstanding rcu callbacks using this
  534. * network namespace.
  535. */
  536. rcu_barrier();
  537. net_complete_free();
  538. /* Finally it is safe to free my network namespace structure */
  539. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  540. list_del_init(&net->exit_list);
  541. dec_net_namespaces(net->ucounts);
  542. #ifdef CONFIG_KEYS
  543. key_remove_domain(net->key_domain);
  544. #endif
  545. put_user_ns(net->user_ns);
  546. net_passive_dec(net);
  547. }
  548. }
  549. /**
  550. * net_ns_barrier - wait until concurrent net_cleanup_work is done
  551. *
  552. * cleanup_net runs from work queue and will first remove namespaces
  553. * from the global list, then run net exit functions.
  554. *
  555. * Call this in module exit path to make sure that all netns
  556. * ->exit ops have been invoked before the function is removed.
  557. */
  558. void net_ns_barrier(void)
  559. {
  560. down_write(&pernet_ops_rwsem);
  561. up_write(&pernet_ops_rwsem);
  562. }
  563. EXPORT_SYMBOL(net_ns_barrier);
  564. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  565. void __put_net(struct net *net)
  566. {
  567. ref_tracker_dir_exit(&net->refcnt_tracker);
  568. /* Cleanup the network namespace in process context */
  569. if (llist_add(&net->cleanup_list, &cleanup_list))
  570. queue_work(netns_wq, &net_cleanup_work);
  571. }
  572. EXPORT_SYMBOL_GPL(__put_net);
  573. /**
  574. * get_net_ns - increment the refcount of the network namespace
  575. * @ns: common namespace (net)
  576. *
  577. * Returns the net's common namespace or ERR_PTR() if ref is zero.
  578. */
  579. struct ns_common *get_net_ns(struct ns_common *ns)
  580. {
  581. struct net *net;
  582. net = maybe_get_net(container_of(ns, struct net, ns));
  583. if (net)
  584. return &net->ns;
  585. return ERR_PTR(-EINVAL);
  586. }
  587. EXPORT_SYMBOL_GPL(get_net_ns);
  588. struct net *get_net_ns_by_fd(int fd)
  589. {
  590. struct fd f = fdget(fd);
  591. struct net *net = ERR_PTR(-EINVAL);
  592. if (!fd_file(f))
  593. return ERR_PTR(-EBADF);
  594. if (proc_ns_file(fd_file(f))) {
  595. struct ns_common *ns = get_proc_ns(file_inode(fd_file(f)));
  596. if (ns->ops == &netns_operations)
  597. net = get_net(container_of(ns, struct net, ns));
  598. }
  599. fdput(f);
  600. return net;
  601. }
  602. EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
  603. #endif
  604. struct net *get_net_ns_by_pid(pid_t pid)
  605. {
  606. struct task_struct *tsk;
  607. struct net *net;
  608. /* Lookup the network namespace */
  609. net = ERR_PTR(-ESRCH);
  610. rcu_read_lock();
  611. tsk = find_task_by_vpid(pid);
  612. if (tsk) {
  613. struct nsproxy *nsproxy;
  614. task_lock(tsk);
  615. nsproxy = tsk->nsproxy;
  616. if (nsproxy)
  617. net = get_net(nsproxy->net_ns);
  618. task_unlock(tsk);
  619. }
  620. rcu_read_unlock();
  621. return net;
  622. }
  623. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  624. static __net_init int net_ns_net_init(struct net *net)
  625. {
  626. #ifdef CONFIG_NET_NS
  627. net->ns.ops = &netns_operations;
  628. #endif
  629. return ns_alloc_inum(&net->ns);
  630. }
  631. static __net_exit void net_ns_net_exit(struct net *net)
  632. {
  633. ns_free_inum(&net->ns);
  634. }
  635. static struct pernet_operations __net_initdata net_ns_ops = {
  636. .init = net_ns_net_init,
  637. .exit = net_ns_net_exit,
  638. };
  639. static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
  640. [NETNSA_NONE] = { .type = NLA_UNSPEC },
  641. [NETNSA_NSID] = { .type = NLA_S32 },
  642. [NETNSA_PID] = { .type = NLA_U32 },
  643. [NETNSA_FD] = { .type = NLA_U32 },
  644. [NETNSA_TARGET_NSID] = { .type = NLA_S32 },
  645. };
  646. static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
  647. struct netlink_ext_ack *extack)
  648. {
  649. struct net *net = sock_net(skb->sk);
  650. struct nlattr *tb[NETNSA_MAX + 1];
  651. struct nlattr *nla;
  652. struct net *peer;
  653. int nsid, err;
  654. err = nlmsg_parse_deprecated(nlh, sizeof(struct rtgenmsg), tb,
  655. NETNSA_MAX, rtnl_net_policy, extack);
  656. if (err < 0)
  657. return err;
  658. if (!tb[NETNSA_NSID]) {
  659. NL_SET_ERR_MSG(extack, "nsid is missing");
  660. return -EINVAL;
  661. }
  662. nsid = nla_get_s32(tb[NETNSA_NSID]);
  663. if (tb[NETNSA_PID]) {
  664. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  665. nla = tb[NETNSA_PID];
  666. } else if (tb[NETNSA_FD]) {
  667. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  668. nla = tb[NETNSA_FD];
  669. } else {
  670. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  671. return -EINVAL;
  672. }
  673. if (IS_ERR(peer)) {
  674. NL_SET_BAD_ATTR(extack, nla);
  675. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  676. return PTR_ERR(peer);
  677. }
  678. spin_lock_bh(&net->nsid_lock);
  679. if (__peernet2id(net, peer) >= 0) {
  680. spin_unlock_bh(&net->nsid_lock);
  681. err = -EEXIST;
  682. NL_SET_BAD_ATTR(extack, nla);
  683. NL_SET_ERR_MSG(extack,
  684. "Peer netns already has a nsid assigned");
  685. goto out;
  686. }
  687. err = alloc_netid(net, peer, nsid);
  688. spin_unlock_bh(&net->nsid_lock);
  689. if (err >= 0) {
  690. rtnl_net_notifyid(net, RTM_NEWNSID, err, NETLINK_CB(skb).portid,
  691. nlh, GFP_KERNEL);
  692. err = 0;
  693. } else if (err == -ENOSPC && nsid >= 0) {
  694. err = -EEXIST;
  695. NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
  696. NL_SET_ERR_MSG(extack, "The specified nsid is already used");
  697. }
  698. out:
  699. put_net(peer);
  700. return err;
  701. }
  702. static int rtnl_net_get_size(void)
  703. {
  704. return NLMSG_ALIGN(sizeof(struct rtgenmsg))
  705. + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
  706. + nla_total_size(sizeof(s32)) /* NETNSA_CURRENT_NSID */
  707. ;
  708. }
  709. struct net_fill_args {
  710. u32 portid;
  711. u32 seq;
  712. int flags;
  713. int cmd;
  714. int nsid;
  715. bool add_ref;
  716. int ref_nsid;
  717. };
  718. static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
  719. {
  720. struct nlmsghdr *nlh;
  721. struct rtgenmsg *rth;
  722. nlh = nlmsg_put(skb, args->portid, args->seq, args->cmd, sizeof(*rth),
  723. args->flags);
  724. if (!nlh)
  725. return -EMSGSIZE;
  726. rth = nlmsg_data(nlh);
  727. rth->rtgen_family = AF_UNSPEC;
  728. if (nla_put_s32(skb, NETNSA_NSID, args->nsid))
  729. goto nla_put_failure;
  730. if (args->add_ref &&
  731. nla_put_s32(skb, NETNSA_CURRENT_NSID, args->ref_nsid))
  732. goto nla_put_failure;
  733. nlmsg_end(skb, nlh);
  734. return 0;
  735. nla_put_failure:
  736. nlmsg_cancel(skb, nlh);
  737. return -EMSGSIZE;
  738. }
  739. static int rtnl_net_valid_getid_req(struct sk_buff *skb,
  740. const struct nlmsghdr *nlh,
  741. struct nlattr **tb,
  742. struct netlink_ext_ack *extack)
  743. {
  744. int i, err;
  745. if (!netlink_strict_get_check(skb))
  746. return nlmsg_parse_deprecated(nlh, sizeof(struct rtgenmsg),
  747. tb, NETNSA_MAX, rtnl_net_policy,
  748. extack);
  749. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct rtgenmsg), tb,
  750. NETNSA_MAX, rtnl_net_policy,
  751. extack);
  752. if (err)
  753. return err;
  754. for (i = 0; i <= NETNSA_MAX; i++) {
  755. if (!tb[i])
  756. continue;
  757. switch (i) {
  758. case NETNSA_PID:
  759. case NETNSA_FD:
  760. case NETNSA_NSID:
  761. case NETNSA_TARGET_NSID:
  762. break;
  763. default:
  764. NL_SET_ERR_MSG(extack, "Unsupported attribute in peer netns getid request");
  765. return -EINVAL;
  766. }
  767. }
  768. return 0;
  769. }
  770. static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
  771. struct netlink_ext_ack *extack)
  772. {
  773. struct net *net = sock_net(skb->sk);
  774. struct nlattr *tb[NETNSA_MAX + 1];
  775. struct net_fill_args fillargs = {
  776. .portid = NETLINK_CB(skb).portid,
  777. .seq = nlh->nlmsg_seq,
  778. .cmd = RTM_NEWNSID,
  779. };
  780. struct net *peer, *target = net;
  781. struct nlattr *nla;
  782. struct sk_buff *msg;
  783. int err;
  784. err = rtnl_net_valid_getid_req(skb, nlh, tb, extack);
  785. if (err < 0)
  786. return err;
  787. if (tb[NETNSA_PID]) {
  788. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  789. nla = tb[NETNSA_PID];
  790. } else if (tb[NETNSA_FD]) {
  791. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  792. nla = tb[NETNSA_FD];
  793. } else if (tb[NETNSA_NSID]) {
  794. peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID]));
  795. if (!peer)
  796. peer = ERR_PTR(-ENOENT);
  797. nla = tb[NETNSA_NSID];
  798. } else {
  799. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  800. return -EINVAL;
  801. }
  802. if (IS_ERR(peer)) {
  803. NL_SET_BAD_ATTR(extack, nla);
  804. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  805. return PTR_ERR(peer);
  806. }
  807. if (tb[NETNSA_TARGET_NSID]) {
  808. int id = nla_get_s32(tb[NETNSA_TARGET_NSID]);
  809. target = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, id);
  810. if (IS_ERR(target)) {
  811. NL_SET_BAD_ATTR(extack, tb[NETNSA_TARGET_NSID]);
  812. NL_SET_ERR_MSG(extack,
  813. "Target netns reference is invalid");
  814. err = PTR_ERR(target);
  815. goto out;
  816. }
  817. fillargs.add_ref = true;
  818. fillargs.ref_nsid = peernet2id(net, peer);
  819. }
  820. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  821. if (!msg) {
  822. err = -ENOMEM;
  823. goto out;
  824. }
  825. fillargs.nsid = peernet2id(target, peer);
  826. err = rtnl_net_fill(msg, &fillargs);
  827. if (err < 0)
  828. goto err_out;
  829. err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
  830. goto out;
  831. err_out:
  832. nlmsg_free(msg);
  833. out:
  834. if (fillargs.add_ref)
  835. put_net(target);
  836. put_net(peer);
  837. return err;
  838. }
  839. struct rtnl_net_dump_cb {
  840. struct net *tgt_net;
  841. struct net *ref_net;
  842. struct sk_buff *skb;
  843. struct net_fill_args fillargs;
  844. int idx;
  845. int s_idx;
  846. };
  847. /* Runs in RCU-critical section. */
  848. static int rtnl_net_dumpid_one(int id, void *peer, void *data)
  849. {
  850. struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
  851. int ret;
  852. if (net_cb->idx < net_cb->s_idx)
  853. goto cont;
  854. net_cb->fillargs.nsid = id;
  855. if (net_cb->fillargs.add_ref)
  856. net_cb->fillargs.ref_nsid = __peernet2id(net_cb->ref_net, peer);
  857. ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs);
  858. if (ret < 0)
  859. return ret;
  860. cont:
  861. net_cb->idx++;
  862. return 0;
  863. }
  864. static int rtnl_valid_dump_net_req(const struct nlmsghdr *nlh, struct sock *sk,
  865. struct rtnl_net_dump_cb *net_cb,
  866. struct netlink_callback *cb)
  867. {
  868. struct netlink_ext_ack *extack = cb->extack;
  869. struct nlattr *tb[NETNSA_MAX + 1];
  870. int err, i;
  871. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct rtgenmsg), tb,
  872. NETNSA_MAX, rtnl_net_policy,
  873. extack);
  874. if (err < 0)
  875. return err;
  876. for (i = 0; i <= NETNSA_MAX; i++) {
  877. if (!tb[i])
  878. continue;
  879. if (i == NETNSA_TARGET_NSID) {
  880. struct net *net;
  881. net = rtnl_get_net_ns_capable(sk, nla_get_s32(tb[i]));
  882. if (IS_ERR(net)) {
  883. NL_SET_BAD_ATTR(extack, tb[i]);
  884. NL_SET_ERR_MSG(extack,
  885. "Invalid target network namespace id");
  886. return PTR_ERR(net);
  887. }
  888. net_cb->fillargs.add_ref = true;
  889. net_cb->ref_net = net_cb->tgt_net;
  890. net_cb->tgt_net = net;
  891. } else {
  892. NL_SET_BAD_ATTR(extack, tb[i]);
  893. NL_SET_ERR_MSG(extack,
  894. "Unsupported attribute in dump request");
  895. return -EINVAL;
  896. }
  897. }
  898. return 0;
  899. }
  900. static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
  901. {
  902. struct rtnl_net_dump_cb net_cb = {
  903. .tgt_net = sock_net(skb->sk),
  904. .skb = skb,
  905. .fillargs = {
  906. .portid = NETLINK_CB(cb->skb).portid,
  907. .seq = cb->nlh->nlmsg_seq,
  908. .flags = NLM_F_MULTI,
  909. .cmd = RTM_NEWNSID,
  910. },
  911. .idx = 0,
  912. .s_idx = cb->args[0],
  913. };
  914. int err = 0;
  915. if (cb->strict_check) {
  916. err = rtnl_valid_dump_net_req(cb->nlh, skb->sk, &net_cb, cb);
  917. if (err < 0)
  918. goto end;
  919. }
  920. rcu_read_lock();
  921. idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb);
  922. rcu_read_unlock();
  923. cb->args[0] = net_cb.idx;
  924. end:
  925. if (net_cb.fillargs.add_ref)
  926. put_net(net_cb.tgt_net);
  927. return err;
  928. }
  929. static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
  930. struct nlmsghdr *nlh, gfp_t gfp)
  931. {
  932. struct net_fill_args fillargs = {
  933. .portid = portid,
  934. .seq = nlh ? nlh->nlmsg_seq : 0,
  935. .cmd = cmd,
  936. .nsid = id,
  937. };
  938. struct sk_buff *msg;
  939. int err = -ENOMEM;
  940. msg = nlmsg_new(rtnl_net_get_size(), gfp);
  941. if (!msg)
  942. goto out;
  943. err = rtnl_net_fill(msg, &fillargs);
  944. if (err < 0)
  945. goto err_out;
  946. rtnl_notify(msg, net, portid, RTNLGRP_NSID, nlh, gfp);
  947. return;
  948. err_out:
  949. nlmsg_free(msg);
  950. out:
  951. rtnl_set_sk_err(net, RTNLGRP_NSID, err);
  952. }
  953. #ifdef CONFIG_NET_NS
  954. static void __init netns_ipv4_struct_check(void)
  955. {
  956. /* TX readonly hotpath cache lines */
  957. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  958. sysctl_tcp_early_retrans);
  959. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  960. sysctl_tcp_tso_win_divisor);
  961. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  962. sysctl_tcp_tso_rtt_log);
  963. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  964. sysctl_tcp_autocorking);
  965. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  966. sysctl_tcp_min_snd_mss);
  967. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  968. sysctl_tcp_notsent_lowat);
  969. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  970. sysctl_tcp_limit_output_bytes);
  971. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  972. sysctl_tcp_min_rtt_wlen);
  973. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  974. sysctl_tcp_wmem);
  975. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_tx,
  976. sysctl_ip_fwd_use_pmtu);
  977. CACHELINE_ASSERT_GROUP_SIZE(struct netns_ipv4, netns_ipv4_read_tx, 33);
  978. /* TXRX readonly hotpath cache lines */
  979. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_txrx,
  980. sysctl_tcp_moderate_rcvbuf);
  981. CACHELINE_ASSERT_GROUP_SIZE(struct netns_ipv4, netns_ipv4_read_txrx, 1);
  982. /* RX readonly hotpath cache line */
  983. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
  984. sysctl_ip_early_demux);
  985. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
  986. sysctl_tcp_early_demux);
  987. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
  988. sysctl_tcp_reordering);
  989. CACHELINE_ASSERT_GROUP_MEMBER(struct netns_ipv4, netns_ipv4_read_rx,
  990. sysctl_tcp_rmem);
  991. CACHELINE_ASSERT_GROUP_SIZE(struct netns_ipv4, netns_ipv4_read_rx, 18);
  992. }
  993. #endif
  994. void __init net_ns_init(void)
  995. {
  996. struct net_generic *ng;
  997. #ifdef CONFIG_NET_NS
  998. netns_ipv4_struct_check();
  999. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  1000. SMP_CACHE_BYTES,
  1001. SLAB_PANIC|SLAB_ACCOUNT, NULL);
  1002. /* Create workqueue for cleanup */
  1003. netns_wq = create_singlethread_workqueue("netns");
  1004. if (!netns_wq)
  1005. panic("Could not create netns workq");
  1006. #endif
  1007. ng = net_alloc_generic();
  1008. if (!ng)
  1009. panic("Could not allocate generic netns");
  1010. rcu_assign_pointer(init_net.gen, ng);
  1011. #ifdef CONFIG_KEYS
  1012. init_net.key_domain = &init_net_key_domain;
  1013. #endif
  1014. preinit_net(&init_net, &init_user_ns);
  1015. down_write(&pernet_ops_rwsem);
  1016. if (setup_net(&init_net))
  1017. panic("Could not setup the initial network namespace");
  1018. init_net_initialized = true;
  1019. up_write(&pernet_ops_rwsem);
  1020. if (register_pernet_subsys(&net_ns_ops))
  1021. panic("Could not register network namespace subsystems");
  1022. rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
  1023. RTNL_FLAG_DOIT_UNLOCKED);
  1024. rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
  1025. RTNL_FLAG_DOIT_UNLOCKED |
  1026. RTNL_FLAG_DUMP_UNLOCKED);
  1027. }
  1028. static void free_exit_list(struct pernet_operations *ops, struct list_head *net_exit_list)
  1029. {
  1030. ops_pre_exit_list(ops, net_exit_list);
  1031. synchronize_rcu();
  1032. if (ops->exit_batch_rtnl) {
  1033. LIST_HEAD(dev_kill_list);
  1034. rtnl_lock();
  1035. ops->exit_batch_rtnl(net_exit_list, &dev_kill_list);
  1036. unregister_netdevice_many(&dev_kill_list);
  1037. rtnl_unlock();
  1038. }
  1039. ops_exit_list(ops, net_exit_list);
  1040. ops_free_list(ops, net_exit_list);
  1041. }
  1042. #ifdef CONFIG_NET_NS
  1043. static int __register_pernet_operations(struct list_head *list,
  1044. struct pernet_operations *ops)
  1045. {
  1046. struct net *net;
  1047. int error;
  1048. LIST_HEAD(net_exit_list);
  1049. list_add_tail(&ops->list, list);
  1050. if (ops->init || ops->id) {
  1051. /* We held write locked pernet_ops_rwsem, and parallel
  1052. * setup_net() and cleanup_net() are not possible.
  1053. */
  1054. for_each_net(net) {
  1055. error = ops_init(ops, net);
  1056. if (error)
  1057. goto out_undo;
  1058. list_add_tail(&net->exit_list, &net_exit_list);
  1059. }
  1060. }
  1061. return 0;
  1062. out_undo:
  1063. /* If I have an error cleanup all namespaces I initialized */
  1064. list_del(&ops->list);
  1065. free_exit_list(ops, &net_exit_list);
  1066. return error;
  1067. }
  1068. static void __unregister_pernet_operations(struct pernet_operations *ops)
  1069. {
  1070. struct net *net;
  1071. LIST_HEAD(net_exit_list);
  1072. list_del(&ops->list);
  1073. /* See comment in __register_pernet_operations() */
  1074. for_each_net(net)
  1075. list_add_tail(&net->exit_list, &net_exit_list);
  1076. free_exit_list(ops, &net_exit_list);
  1077. }
  1078. #else
  1079. static int __register_pernet_operations(struct list_head *list,
  1080. struct pernet_operations *ops)
  1081. {
  1082. if (!init_net_initialized) {
  1083. list_add_tail(&ops->list, list);
  1084. return 0;
  1085. }
  1086. return ops_init(ops, &init_net);
  1087. }
  1088. static void __unregister_pernet_operations(struct pernet_operations *ops)
  1089. {
  1090. if (!init_net_initialized) {
  1091. list_del(&ops->list);
  1092. } else {
  1093. LIST_HEAD(net_exit_list);
  1094. list_add(&init_net.exit_list, &net_exit_list);
  1095. free_exit_list(ops, &net_exit_list);
  1096. }
  1097. }
  1098. #endif /* CONFIG_NET_NS */
  1099. static DEFINE_IDA(net_generic_ids);
  1100. static int register_pernet_operations(struct list_head *list,
  1101. struct pernet_operations *ops)
  1102. {
  1103. int error;
  1104. if (WARN_ON(!!ops->id ^ !!ops->size))
  1105. return -EINVAL;
  1106. if (ops->id) {
  1107. error = ida_alloc_min(&net_generic_ids, MIN_PERNET_OPS_ID,
  1108. GFP_KERNEL);
  1109. if (error < 0)
  1110. return error;
  1111. *ops->id = error;
  1112. /* This does not require READ_ONCE as writers already hold
  1113. * pernet_ops_rwsem. But WRITE_ONCE is needed to protect
  1114. * net_alloc_generic.
  1115. */
  1116. WRITE_ONCE(max_gen_ptrs, max(max_gen_ptrs, *ops->id + 1));
  1117. }
  1118. error = __register_pernet_operations(list, ops);
  1119. if (error) {
  1120. rcu_barrier();
  1121. if (ops->id)
  1122. ida_free(&net_generic_ids, *ops->id);
  1123. }
  1124. return error;
  1125. }
  1126. static void unregister_pernet_operations(struct pernet_operations *ops)
  1127. {
  1128. __unregister_pernet_operations(ops);
  1129. rcu_barrier();
  1130. if (ops->id)
  1131. ida_free(&net_generic_ids, *ops->id);
  1132. }
  1133. /**
  1134. * register_pernet_subsys - register a network namespace subsystem
  1135. * @ops: pernet operations structure for the subsystem
  1136. *
  1137. * Register a subsystem which has init and exit functions
  1138. * that are called when network namespaces are created and
  1139. * destroyed respectively.
  1140. *
  1141. * When registered all network namespace init functions are
  1142. * called for every existing network namespace. Allowing kernel
  1143. * modules to have a race free view of the set of network namespaces.
  1144. *
  1145. * When a new network namespace is created all of the init
  1146. * methods are called in the order in which they were registered.
  1147. *
  1148. * When a network namespace is destroyed all of the exit methods
  1149. * are called in the reverse of the order with which they were
  1150. * registered.
  1151. */
  1152. int register_pernet_subsys(struct pernet_operations *ops)
  1153. {
  1154. int error;
  1155. down_write(&pernet_ops_rwsem);
  1156. error = register_pernet_operations(first_device, ops);
  1157. up_write(&pernet_ops_rwsem);
  1158. return error;
  1159. }
  1160. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  1161. /**
  1162. * unregister_pernet_subsys - unregister a network namespace subsystem
  1163. * @ops: pernet operations structure to manipulate
  1164. *
  1165. * Remove the pernet operations structure from the list to be
  1166. * used when network namespaces are created or destroyed. In
  1167. * addition run the exit method for all existing network
  1168. * namespaces.
  1169. */
  1170. void unregister_pernet_subsys(struct pernet_operations *ops)
  1171. {
  1172. down_write(&pernet_ops_rwsem);
  1173. unregister_pernet_operations(ops);
  1174. up_write(&pernet_ops_rwsem);
  1175. }
  1176. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  1177. /**
  1178. * register_pernet_device - register a network namespace device
  1179. * @ops: pernet operations structure for the subsystem
  1180. *
  1181. * Register a device which has init and exit functions
  1182. * that are called when network namespaces are created and
  1183. * destroyed respectively.
  1184. *
  1185. * When registered all network namespace init functions are
  1186. * called for every existing network namespace. Allowing kernel
  1187. * modules to have a race free view of the set of network namespaces.
  1188. *
  1189. * When a new network namespace is created all of the init
  1190. * methods are called in the order in which they were registered.
  1191. *
  1192. * When a network namespace is destroyed all of the exit methods
  1193. * are called in the reverse of the order with which they were
  1194. * registered.
  1195. */
  1196. int register_pernet_device(struct pernet_operations *ops)
  1197. {
  1198. int error;
  1199. down_write(&pernet_ops_rwsem);
  1200. error = register_pernet_operations(&pernet_list, ops);
  1201. if (!error && (first_device == &pernet_list))
  1202. first_device = &ops->list;
  1203. up_write(&pernet_ops_rwsem);
  1204. return error;
  1205. }
  1206. EXPORT_SYMBOL_GPL(register_pernet_device);
  1207. /**
  1208. * unregister_pernet_device - unregister a network namespace netdevice
  1209. * @ops: pernet operations structure to manipulate
  1210. *
  1211. * Remove the pernet operations structure from the list to be
  1212. * used when network namespaces are created or destroyed. In
  1213. * addition run the exit method for all existing network
  1214. * namespaces.
  1215. */
  1216. void unregister_pernet_device(struct pernet_operations *ops)
  1217. {
  1218. down_write(&pernet_ops_rwsem);
  1219. if (&ops->list == first_device)
  1220. first_device = first_device->next;
  1221. unregister_pernet_operations(ops);
  1222. up_write(&pernet_ops_rwsem);
  1223. }
  1224. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  1225. #ifdef CONFIG_NET_NS
  1226. static struct ns_common *netns_get(struct task_struct *task)
  1227. {
  1228. struct net *net = NULL;
  1229. struct nsproxy *nsproxy;
  1230. task_lock(task);
  1231. nsproxy = task->nsproxy;
  1232. if (nsproxy)
  1233. net = get_net(nsproxy->net_ns);
  1234. task_unlock(task);
  1235. return net ? &net->ns : NULL;
  1236. }
  1237. static inline struct net *to_net_ns(struct ns_common *ns)
  1238. {
  1239. return container_of(ns, struct net, ns);
  1240. }
  1241. static void netns_put(struct ns_common *ns)
  1242. {
  1243. put_net(to_net_ns(ns));
  1244. }
  1245. static int netns_install(struct nsset *nsset, struct ns_common *ns)
  1246. {
  1247. struct nsproxy *nsproxy = nsset->nsproxy;
  1248. struct net *net = to_net_ns(ns);
  1249. if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
  1250. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  1251. return -EPERM;
  1252. put_net(nsproxy->net_ns);
  1253. nsproxy->net_ns = get_net(net);
  1254. return 0;
  1255. }
  1256. static struct user_namespace *netns_owner(struct ns_common *ns)
  1257. {
  1258. return to_net_ns(ns)->user_ns;
  1259. }
  1260. const struct proc_ns_operations netns_operations = {
  1261. .name = "net",
  1262. .type = CLONE_NEWNET,
  1263. .get = netns_get,
  1264. .put = netns_put,
  1265. .install = netns_install,
  1266. .owner = netns_owner,
  1267. };
  1268. #endif