net_namespace.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/workqueue.h>
  3. #include <linux/rtnetlink.h>
  4. #include <linux/cache.h>
  5. #include <linux/slab.h>
  6. #include <linux/list.h>
  7. #include <linux/delay.h>
  8. #include <linux/sched.h>
  9. #include <linux/idr.h>
  10. #include <linux/rculist.h>
  11. #include <linux/nsproxy.h>
  12. #include <linux/fs.h>
  13. #include <linux/proc_ns.h>
  14. #include <linux/file.h>
  15. #include <linux/export.h>
  16. #include <linux/user_namespace.h>
  17. #include <linux/net_namespace.h>
  18. #include <linux/sched/task.h>
  19. #include <net/sock.h>
  20. #include <net/netlink.h>
  21. #include <net/net_namespace.h>
  22. #include <net/netns/generic.h>
  23. /*
  24. * Our network namespace constructor/destructor lists
  25. */
  26. static LIST_HEAD(pernet_list);
  27. static struct list_head *first_device = &pernet_list;
  28. LIST_HEAD(net_namespace_list);
  29. EXPORT_SYMBOL_GPL(net_namespace_list);
  30. /* Protects net_namespace_list. Nests iside rtnl_lock() */
  31. DECLARE_RWSEM(net_rwsem);
  32. EXPORT_SYMBOL_GPL(net_rwsem);
  33. struct net init_net = {
  34. .count = REFCOUNT_INIT(1),
  35. .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
  36. };
  37. EXPORT_SYMBOL(init_net);
  38. static bool init_net_initialized;
  39. /*
  40. * pernet_ops_rwsem: protects: pernet_list, net_generic_ids,
  41. * init_net_initialized and first_device pointer.
  42. * This is internal net namespace object. Please, don't use it
  43. * outside.
  44. */
  45. DECLARE_RWSEM(pernet_ops_rwsem);
  46. #define MIN_PERNET_OPS_ID \
  47. ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
  48. #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
  49. static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
  50. static struct net_generic *net_alloc_generic(void)
  51. {
  52. struct net_generic *ng;
  53. unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
  54. ng = kzalloc(generic_size, GFP_KERNEL);
  55. if (ng)
  56. ng->s.len = max_gen_ptrs;
  57. return ng;
  58. }
  59. static int net_assign_generic(struct net *net, unsigned int id, void *data)
  60. {
  61. struct net_generic *ng, *old_ng;
  62. BUG_ON(id < MIN_PERNET_OPS_ID);
  63. old_ng = rcu_dereference_protected(net->gen,
  64. lockdep_is_held(&pernet_ops_rwsem));
  65. if (old_ng->s.len > id) {
  66. old_ng->ptr[id] = data;
  67. return 0;
  68. }
  69. ng = net_alloc_generic();
  70. if (ng == NULL)
  71. return -ENOMEM;
  72. /*
  73. * Some synchronisation notes:
  74. *
  75. * The net_generic explores the net->gen array inside rcu
  76. * read section. Besides once set the net->gen->ptr[x]
  77. * pointer never changes (see rules in netns/generic.h).
  78. *
  79. * That said, we simply duplicate this array and schedule
  80. * the old copy for kfree after a grace period.
  81. */
  82. memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
  83. (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
  84. ng->ptr[id] = data;
  85. rcu_assign_pointer(net->gen, ng);
  86. kfree_rcu(old_ng, s.rcu);
  87. return 0;
  88. }
  89. static int ops_init(const struct pernet_operations *ops, struct net *net)
  90. {
  91. int err = -ENOMEM;
  92. void *data = NULL;
  93. if (ops->id && ops->size) {
  94. data = kzalloc(ops->size, GFP_KERNEL);
  95. if (!data)
  96. goto out;
  97. err = net_assign_generic(net, *ops->id, data);
  98. if (err)
  99. goto cleanup;
  100. }
  101. err = 0;
  102. if (ops->init)
  103. err = ops->init(net);
  104. if (!err)
  105. return 0;
  106. cleanup:
  107. kfree(data);
  108. out:
  109. return err;
  110. }
  111. static void ops_free(const struct pernet_operations *ops, struct net *net)
  112. {
  113. if (ops->id && ops->size) {
  114. kfree(net_generic(net, *ops->id));
  115. }
  116. }
  117. static void ops_exit_list(const struct pernet_operations *ops,
  118. struct list_head *net_exit_list)
  119. {
  120. struct net *net;
  121. if (ops->exit) {
  122. list_for_each_entry(net, net_exit_list, exit_list)
  123. ops->exit(net);
  124. }
  125. if (ops->exit_batch)
  126. ops->exit_batch(net_exit_list);
  127. }
  128. static void ops_free_list(const struct pernet_operations *ops,
  129. struct list_head *net_exit_list)
  130. {
  131. struct net *net;
  132. if (ops->size && ops->id) {
  133. list_for_each_entry(net, net_exit_list, exit_list)
  134. ops_free(ops, net);
  135. }
  136. }
  137. /* should be called with nsid_lock held */
  138. static int alloc_netid(struct net *net, struct net *peer, int reqid)
  139. {
  140. int min = 0, max = 0;
  141. if (reqid >= 0) {
  142. min = reqid;
  143. max = reqid + 1;
  144. }
  145. return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
  146. }
  147. /* This function is used by idr_for_each(). If net is equal to peer, the
  148. * function returns the id so that idr_for_each() stops. Because we cannot
  149. * returns the id 0 (idr_for_each() will not stop), we return the magic value
  150. * NET_ID_ZERO (-1) for it.
  151. */
  152. #define NET_ID_ZERO -1
  153. static int net_eq_idr(int id, void *net, void *peer)
  154. {
  155. if (net_eq(net, peer))
  156. return id ? : NET_ID_ZERO;
  157. return 0;
  158. }
  159. /* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
  160. * is set to true, thus the caller knows that the new id must be notified via
  161. * rtnl.
  162. */
  163. static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
  164. {
  165. int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
  166. bool alloc_it = *alloc;
  167. *alloc = false;
  168. /* Magic value for id 0. */
  169. if (id == NET_ID_ZERO)
  170. return 0;
  171. if (id > 0)
  172. return id;
  173. if (alloc_it) {
  174. id = alloc_netid(net, peer, -1);
  175. *alloc = true;
  176. return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
  177. }
  178. return NETNSA_NSID_NOT_ASSIGNED;
  179. }
  180. /* should be called with nsid_lock held */
  181. static int __peernet2id(struct net *net, struct net *peer)
  182. {
  183. bool no = false;
  184. return __peernet2id_alloc(net, peer, &no);
  185. }
  186. static void rtnl_net_notifyid(struct net *net, int cmd, int id);
  187. /* This function returns the id of a peer netns. If no id is assigned, one will
  188. * be allocated and returned.
  189. */
  190. int peernet2id_alloc(struct net *net, struct net *peer)
  191. {
  192. bool alloc = false, alive = false;
  193. int id;
  194. if (refcount_read(&net->count) == 0)
  195. return NETNSA_NSID_NOT_ASSIGNED;
  196. spin_lock_bh(&net->nsid_lock);
  197. /*
  198. * When peer is obtained from RCU lists, we may race with
  199. * its cleanup. Check whether it's alive, and this guarantees
  200. * we never hash a peer back to net->netns_ids, after it has
  201. * just been idr_remove()'d from there in cleanup_net().
  202. */
  203. if (maybe_get_net(peer))
  204. alive = alloc = true;
  205. id = __peernet2id_alloc(net, peer, &alloc);
  206. spin_unlock_bh(&net->nsid_lock);
  207. if (alloc && id >= 0)
  208. rtnl_net_notifyid(net, RTM_NEWNSID, id);
  209. if (alive)
  210. put_net(peer);
  211. return id;
  212. }
  213. EXPORT_SYMBOL_GPL(peernet2id_alloc);
  214. /* This function returns, if assigned, the id of a peer netns. */
  215. int peernet2id(struct net *net, struct net *peer)
  216. {
  217. int id;
  218. spin_lock_bh(&net->nsid_lock);
  219. id = __peernet2id(net, peer);
  220. spin_unlock_bh(&net->nsid_lock);
  221. return id;
  222. }
  223. EXPORT_SYMBOL(peernet2id);
  224. /* This function returns true is the peer netns has an id assigned into the
  225. * current netns.
  226. */
  227. bool peernet_has_id(struct net *net, struct net *peer)
  228. {
  229. return peernet2id(net, peer) >= 0;
  230. }
  231. struct net *get_net_ns_by_id(struct net *net, int id)
  232. {
  233. struct net *peer;
  234. if (id < 0)
  235. return NULL;
  236. rcu_read_lock();
  237. peer = idr_find(&net->netns_ids, id);
  238. if (peer)
  239. peer = maybe_get_net(peer);
  240. rcu_read_unlock();
  241. return peer;
  242. }
  243. /*
  244. * setup_net runs the initializers for the network namespace object.
  245. */
  246. static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
  247. {
  248. /* Must be called with pernet_ops_rwsem held */
  249. const struct pernet_operations *ops, *saved_ops;
  250. int error = 0;
  251. LIST_HEAD(net_exit_list);
  252. refcount_set(&net->count, 1);
  253. refcount_set(&net->passive, 1);
  254. net->dev_base_seq = 1;
  255. net->user_ns = user_ns;
  256. idr_init(&net->netns_ids);
  257. spin_lock_init(&net->nsid_lock);
  258. mutex_init(&net->ipv4.ra_mutex);
  259. list_for_each_entry(ops, &pernet_list, list) {
  260. error = ops_init(ops, net);
  261. if (error < 0)
  262. goto out_undo;
  263. }
  264. down_write(&net_rwsem);
  265. list_add_tail_rcu(&net->list, &net_namespace_list);
  266. up_write(&net_rwsem);
  267. out:
  268. return error;
  269. out_undo:
  270. /* Walk through the list backwards calling the exit functions
  271. * for the pernet modules whose init functions did not fail.
  272. */
  273. list_add(&net->exit_list, &net_exit_list);
  274. saved_ops = ops;
  275. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  276. ops_exit_list(ops, &net_exit_list);
  277. ops = saved_ops;
  278. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  279. ops_free_list(ops, &net_exit_list);
  280. rcu_barrier();
  281. goto out;
  282. }
  283. static int __net_init net_defaults_init_net(struct net *net)
  284. {
  285. net->core.sysctl_somaxconn = SOMAXCONN;
  286. return 0;
  287. }
  288. static struct pernet_operations net_defaults_ops = {
  289. .init = net_defaults_init_net,
  290. };
  291. static __init int net_defaults_init(void)
  292. {
  293. if (register_pernet_subsys(&net_defaults_ops))
  294. panic("Cannot initialize net default settings");
  295. return 0;
  296. }
  297. core_initcall(net_defaults_init);
  298. #ifdef CONFIG_NET_NS
  299. static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
  300. {
  301. return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
  302. }
  303. static void dec_net_namespaces(struct ucounts *ucounts)
  304. {
  305. dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
  306. }
  307. static struct kmem_cache *net_cachep __ro_after_init;
  308. static struct workqueue_struct *netns_wq;
  309. static struct net *net_alloc(void)
  310. {
  311. struct net *net = NULL;
  312. struct net_generic *ng;
  313. ng = net_alloc_generic();
  314. if (!ng)
  315. goto out;
  316. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  317. if (!net)
  318. goto out_free;
  319. rcu_assign_pointer(net->gen, ng);
  320. out:
  321. return net;
  322. out_free:
  323. kfree(ng);
  324. goto out;
  325. }
  326. static void net_free(struct net *net)
  327. {
  328. kfree(rcu_access_pointer(net->gen));
  329. kmem_cache_free(net_cachep, net);
  330. }
  331. void net_drop_ns(void *p)
  332. {
  333. struct net *ns = p;
  334. if (ns && refcount_dec_and_test(&ns->passive))
  335. net_free(ns);
  336. }
  337. struct net *copy_net_ns(unsigned long flags,
  338. struct user_namespace *user_ns, struct net *old_net)
  339. {
  340. struct ucounts *ucounts;
  341. struct net *net;
  342. int rv;
  343. if (!(flags & CLONE_NEWNET))
  344. return get_net(old_net);
  345. ucounts = inc_net_namespaces(user_ns);
  346. if (!ucounts)
  347. return ERR_PTR(-ENOSPC);
  348. net = net_alloc();
  349. if (!net) {
  350. rv = -ENOMEM;
  351. goto dec_ucounts;
  352. }
  353. refcount_set(&net->passive, 1);
  354. net->ucounts = ucounts;
  355. get_user_ns(user_ns);
  356. rv = down_read_killable(&pernet_ops_rwsem);
  357. if (rv < 0)
  358. goto put_userns;
  359. rv = setup_net(net, user_ns);
  360. up_read(&pernet_ops_rwsem);
  361. if (rv < 0) {
  362. put_userns:
  363. put_user_ns(user_ns);
  364. net_drop_ns(net);
  365. dec_ucounts:
  366. dec_net_namespaces(ucounts);
  367. return ERR_PTR(rv);
  368. }
  369. return net;
  370. }
  371. static void unhash_nsid(struct net *net, struct net *last)
  372. {
  373. struct net *tmp;
  374. /* This function is only called from cleanup_net() work,
  375. * and this work is the only process, that may delete
  376. * a net from net_namespace_list. So, when the below
  377. * is executing, the list may only grow. Thus, we do not
  378. * use for_each_net_rcu() or net_rwsem.
  379. */
  380. for_each_net(tmp) {
  381. int id;
  382. spin_lock_bh(&tmp->nsid_lock);
  383. id = __peernet2id(tmp, net);
  384. if (id >= 0)
  385. idr_remove(&tmp->netns_ids, id);
  386. spin_unlock_bh(&tmp->nsid_lock);
  387. if (id >= 0)
  388. rtnl_net_notifyid(tmp, RTM_DELNSID, id);
  389. if (tmp == last)
  390. break;
  391. }
  392. spin_lock_bh(&net->nsid_lock);
  393. idr_destroy(&net->netns_ids);
  394. spin_unlock_bh(&net->nsid_lock);
  395. }
  396. static LLIST_HEAD(cleanup_list);
  397. static void cleanup_net(struct work_struct *work)
  398. {
  399. const struct pernet_operations *ops;
  400. struct net *net, *tmp, *last;
  401. struct llist_node *net_kill_list;
  402. LIST_HEAD(net_exit_list);
  403. /* Atomically snapshot the list of namespaces to cleanup */
  404. net_kill_list = llist_del_all(&cleanup_list);
  405. down_read(&pernet_ops_rwsem);
  406. /* Don't let anyone else find us. */
  407. down_write(&net_rwsem);
  408. llist_for_each_entry(net, net_kill_list, cleanup_list)
  409. list_del_rcu(&net->list);
  410. /* Cache last net. After we unlock rtnl, no one new net
  411. * added to net_namespace_list can assign nsid pointer
  412. * to a net from net_kill_list (see peernet2id_alloc()).
  413. * So, we skip them in unhash_nsid().
  414. *
  415. * Note, that unhash_nsid() does not delete nsid links
  416. * between net_kill_list's nets, as they've already
  417. * deleted from net_namespace_list. But, this would be
  418. * useless anyway, as netns_ids are destroyed there.
  419. */
  420. last = list_last_entry(&net_namespace_list, struct net, list);
  421. up_write(&net_rwsem);
  422. llist_for_each_entry(net, net_kill_list, cleanup_list) {
  423. unhash_nsid(net, last);
  424. list_add_tail(&net->exit_list, &net_exit_list);
  425. }
  426. /*
  427. * Another CPU might be rcu-iterating the list, wait for it.
  428. * This needs to be before calling the exit() notifiers, so
  429. * the rcu_barrier() below isn't sufficient alone.
  430. */
  431. synchronize_rcu();
  432. /* Run all of the network namespace exit methods */
  433. list_for_each_entry_reverse(ops, &pernet_list, list)
  434. ops_exit_list(ops, &net_exit_list);
  435. /* Free the net generic variables */
  436. list_for_each_entry_reverse(ops, &pernet_list, list)
  437. ops_free_list(ops, &net_exit_list);
  438. up_read(&pernet_ops_rwsem);
  439. /* Ensure there are no outstanding rcu callbacks using this
  440. * network namespace.
  441. */
  442. rcu_barrier();
  443. /* Finally it is safe to free my network namespace structure */
  444. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  445. list_del_init(&net->exit_list);
  446. dec_net_namespaces(net->ucounts);
  447. put_user_ns(net->user_ns);
  448. net_drop_ns(net);
  449. }
  450. }
  451. /**
  452. * net_ns_barrier - wait until concurrent net_cleanup_work is done
  453. *
  454. * cleanup_net runs from work queue and will first remove namespaces
  455. * from the global list, then run net exit functions.
  456. *
  457. * Call this in module exit path to make sure that all netns
  458. * ->exit ops have been invoked before the function is removed.
  459. */
  460. void net_ns_barrier(void)
  461. {
  462. down_write(&pernet_ops_rwsem);
  463. up_write(&pernet_ops_rwsem);
  464. }
  465. EXPORT_SYMBOL(net_ns_barrier);
  466. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  467. void __put_net(struct net *net)
  468. {
  469. /* Cleanup the network namespace in process context */
  470. if (llist_add(&net->cleanup_list, &cleanup_list))
  471. queue_work(netns_wq, &net_cleanup_work);
  472. }
  473. EXPORT_SYMBOL_GPL(__put_net);
  474. struct net *get_net_ns_by_fd(int fd)
  475. {
  476. struct file *file;
  477. struct ns_common *ns;
  478. struct net *net;
  479. file = proc_ns_fget(fd);
  480. if (IS_ERR(file))
  481. return ERR_CAST(file);
  482. ns = get_proc_ns(file_inode(file));
  483. if (ns->ops == &netns_operations)
  484. net = get_net(container_of(ns, struct net, ns));
  485. else
  486. net = ERR_PTR(-EINVAL);
  487. fput(file);
  488. return net;
  489. }
  490. #else
  491. struct net *get_net_ns_by_fd(int fd)
  492. {
  493. return ERR_PTR(-EINVAL);
  494. }
  495. #endif
  496. EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
  497. struct net *get_net_ns_by_pid(pid_t pid)
  498. {
  499. struct task_struct *tsk;
  500. struct net *net;
  501. /* Lookup the network namespace */
  502. net = ERR_PTR(-ESRCH);
  503. rcu_read_lock();
  504. tsk = find_task_by_vpid(pid);
  505. if (tsk) {
  506. struct nsproxy *nsproxy;
  507. task_lock(tsk);
  508. nsproxy = tsk->nsproxy;
  509. if (nsproxy)
  510. net = get_net(nsproxy->net_ns);
  511. task_unlock(tsk);
  512. }
  513. rcu_read_unlock();
  514. return net;
  515. }
  516. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  517. static __net_init int net_ns_net_init(struct net *net)
  518. {
  519. #ifdef CONFIG_NET_NS
  520. net->ns.ops = &netns_operations;
  521. #endif
  522. return ns_alloc_inum(&net->ns);
  523. }
  524. static __net_exit void net_ns_net_exit(struct net *net)
  525. {
  526. ns_free_inum(&net->ns);
  527. }
  528. static struct pernet_operations __net_initdata net_ns_ops = {
  529. .init = net_ns_net_init,
  530. .exit = net_ns_net_exit,
  531. };
  532. static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
  533. [NETNSA_NONE] = { .type = NLA_UNSPEC },
  534. [NETNSA_NSID] = { .type = NLA_S32 },
  535. [NETNSA_PID] = { .type = NLA_U32 },
  536. [NETNSA_FD] = { .type = NLA_U32 },
  537. };
  538. static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
  539. struct netlink_ext_ack *extack)
  540. {
  541. struct net *net = sock_net(skb->sk);
  542. struct nlattr *tb[NETNSA_MAX + 1];
  543. struct nlattr *nla;
  544. struct net *peer;
  545. int nsid, err;
  546. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  547. rtnl_net_policy, extack);
  548. if (err < 0)
  549. return err;
  550. if (!tb[NETNSA_NSID]) {
  551. NL_SET_ERR_MSG(extack, "nsid is missing");
  552. return -EINVAL;
  553. }
  554. nsid = nla_get_s32(tb[NETNSA_NSID]);
  555. if (tb[NETNSA_PID]) {
  556. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  557. nla = tb[NETNSA_PID];
  558. } else if (tb[NETNSA_FD]) {
  559. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  560. nla = tb[NETNSA_FD];
  561. } else {
  562. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  563. return -EINVAL;
  564. }
  565. if (IS_ERR(peer)) {
  566. NL_SET_BAD_ATTR(extack, nla);
  567. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  568. return PTR_ERR(peer);
  569. }
  570. spin_lock_bh(&net->nsid_lock);
  571. if (__peernet2id(net, peer) >= 0) {
  572. spin_unlock_bh(&net->nsid_lock);
  573. err = -EEXIST;
  574. NL_SET_BAD_ATTR(extack, nla);
  575. NL_SET_ERR_MSG(extack,
  576. "Peer netns already has a nsid assigned");
  577. goto out;
  578. }
  579. err = alloc_netid(net, peer, nsid);
  580. spin_unlock_bh(&net->nsid_lock);
  581. if (err >= 0) {
  582. rtnl_net_notifyid(net, RTM_NEWNSID, err);
  583. err = 0;
  584. } else if (err == -ENOSPC && nsid >= 0) {
  585. err = -EEXIST;
  586. NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
  587. NL_SET_ERR_MSG(extack, "The specified nsid is already used");
  588. }
  589. out:
  590. put_net(peer);
  591. return err;
  592. }
  593. static int rtnl_net_get_size(void)
  594. {
  595. return NLMSG_ALIGN(sizeof(struct rtgenmsg))
  596. + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
  597. ;
  598. }
  599. static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  600. int cmd, struct net *net, int nsid)
  601. {
  602. struct nlmsghdr *nlh;
  603. struct rtgenmsg *rth;
  604. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
  605. if (!nlh)
  606. return -EMSGSIZE;
  607. rth = nlmsg_data(nlh);
  608. rth->rtgen_family = AF_UNSPEC;
  609. if (nla_put_s32(skb, NETNSA_NSID, nsid))
  610. goto nla_put_failure;
  611. nlmsg_end(skb, nlh);
  612. return 0;
  613. nla_put_failure:
  614. nlmsg_cancel(skb, nlh);
  615. return -EMSGSIZE;
  616. }
  617. static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
  618. struct netlink_ext_ack *extack)
  619. {
  620. struct net *net = sock_net(skb->sk);
  621. struct nlattr *tb[NETNSA_MAX + 1];
  622. struct nlattr *nla;
  623. struct sk_buff *msg;
  624. struct net *peer;
  625. int err, id;
  626. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  627. rtnl_net_policy, extack);
  628. if (err < 0)
  629. return err;
  630. if (tb[NETNSA_PID]) {
  631. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  632. nla = tb[NETNSA_PID];
  633. } else if (tb[NETNSA_FD]) {
  634. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  635. nla = tb[NETNSA_FD];
  636. } else {
  637. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  638. return -EINVAL;
  639. }
  640. if (IS_ERR(peer)) {
  641. NL_SET_BAD_ATTR(extack, nla);
  642. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  643. return PTR_ERR(peer);
  644. }
  645. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  646. if (!msg) {
  647. err = -ENOMEM;
  648. goto out;
  649. }
  650. id = peernet2id(net, peer);
  651. err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  652. RTM_NEWNSID, net, id);
  653. if (err < 0)
  654. goto err_out;
  655. err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
  656. goto out;
  657. err_out:
  658. nlmsg_free(msg);
  659. out:
  660. put_net(peer);
  661. return err;
  662. }
  663. struct rtnl_net_dump_cb {
  664. struct net *net;
  665. struct sk_buff *skb;
  666. struct netlink_callback *cb;
  667. int idx;
  668. int s_idx;
  669. };
  670. static int rtnl_net_dumpid_one(int id, void *peer, void *data)
  671. {
  672. struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
  673. int ret;
  674. if (net_cb->idx < net_cb->s_idx)
  675. goto cont;
  676. ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
  677. net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  678. RTM_NEWNSID, net_cb->net, id);
  679. if (ret < 0)
  680. return ret;
  681. cont:
  682. net_cb->idx++;
  683. return 0;
  684. }
  685. static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
  686. {
  687. struct net *net = sock_net(skb->sk);
  688. struct rtnl_net_dump_cb net_cb = {
  689. .net = net,
  690. .skb = skb,
  691. .cb = cb,
  692. .idx = 0,
  693. .s_idx = cb->args[0],
  694. };
  695. spin_lock_bh(&net->nsid_lock);
  696. idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
  697. spin_unlock_bh(&net->nsid_lock);
  698. cb->args[0] = net_cb.idx;
  699. return skb->len;
  700. }
  701. static void rtnl_net_notifyid(struct net *net, int cmd, int id)
  702. {
  703. struct sk_buff *msg;
  704. int err = -ENOMEM;
  705. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  706. if (!msg)
  707. goto out;
  708. err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
  709. if (err < 0)
  710. goto err_out;
  711. rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
  712. return;
  713. err_out:
  714. nlmsg_free(msg);
  715. out:
  716. rtnl_set_sk_err(net, RTNLGRP_NSID, err);
  717. }
  718. static int __init net_ns_init(void)
  719. {
  720. struct net_generic *ng;
  721. #ifdef CONFIG_NET_NS
  722. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  723. SMP_CACHE_BYTES,
  724. SLAB_PANIC|SLAB_ACCOUNT, NULL);
  725. /* Create workqueue for cleanup */
  726. netns_wq = create_singlethread_workqueue("netns");
  727. if (!netns_wq)
  728. panic("Could not create netns workq");
  729. #endif
  730. ng = net_alloc_generic();
  731. if (!ng)
  732. panic("Could not allocate generic netns");
  733. rcu_assign_pointer(init_net.gen, ng);
  734. down_write(&pernet_ops_rwsem);
  735. if (setup_net(&init_net, &init_user_ns))
  736. panic("Could not setup the initial network namespace");
  737. init_net_initialized = true;
  738. up_write(&pernet_ops_rwsem);
  739. register_pernet_subsys(&net_ns_ops);
  740. rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
  741. RTNL_FLAG_DOIT_UNLOCKED);
  742. rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
  743. RTNL_FLAG_DOIT_UNLOCKED);
  744. return 0;
  745. }
  746. pure_initcall(net_ns_init);
  747. #ifdef CONFIG_NET_NS
  748. static int __register_pernet_operations(struct list_head *list,
  749. struct pernet_operations *ops)
  750. {
  751. struct net *net;
  752. int error;
  753. LIST_HEAD(net_exit_list);
  754. list_add_tail(&ops->list, list);
  755. if (ops->init || (ops->id && ops->size)) {
  756. /* We held write locked pernet_ops_rwsem, and parallel
  757. * setup_net() and cleanup_net() are not possible.
  758. */
  759. for_each_net(net) {
  760. error = ops_init(ops, net);
  761. if (error)
  762. goto out_undo;
  763. list_add_tail(&net->exit_list, &net_exit_list);
  764. }
  765. }
  766. return 0;
  767. out_undo:
  768. /* If I have an error cleanup all namespaces I initialized */
  769. list_del(&ops->list);
  770. ops_exit_list(ops, &net_exit_list);
  771. ops_free_list(ops, &net_exit_list);
  772. return error;
  773. }
  774. static void __unregister_pernet_operations(struct pernet_operations *ops)
  775. {
  776. struct net *net;
  777. LIST_HEAD(net_exit_list);
  778. list_del(&ops->list);
  779. /* See comment in __register_pernet_operations() */
  780. for_each_net(net)
  781. list_add_tail(&net->exit_list, &net_exit_list);
  782. ops_exit_list(ops, &net_exit_list);
  783. ops_free_list(ops, &net_exit_list);
  784. }
  785. #else
  786. static int __register_pernet_operations(struct list_head *list,
  787. struct pernet_operations *ops)
  788. {
  789. if (!init_net_initialized) {
  790. list_add_tail(&ops->list, list);
  791. return 0;
  792. }
  793. return ops_init(ops, &init_net);
  794. }
  795. static void __unregister_pernet_operations(struct pernet_operations *ops)
  796. {
  797. if (!init_net_initialized) {
  798. list_del(&ops->list);
  799. } else {
  800. LIST_HEAD(net_exit_list);
  801. list_add(&init_net.exit_list, &net_exit_list);
  802. ops_exit_list(ops, &net_exit_list);
  803. ops_free_list(ops, &net_exit_list);
  804. }
  805. }
  806. #endif /* CONFIG_NET_NS */
  807. static DEFINE_IDA(net_generic_ids);
  808. static int register_pernet_operations(struct list_head *list,
  809. struct pernet_operations *ops)
  810. {
  811. int error;
  812. if (ops->id) {
  813. again:
  814. error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
  815. if (error < 0) {
  816. if (error == -EAGAIN) {
  817. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  818. goto again;
  819. }
  820. return error;
  821. }
  822. max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
  823. }
  824. error = __register_pernet_operations(list, ops);
  825. if (error) {
  826. rcu_barrier();
  827. if (ops->id)
  828. ida_remove(&net_generic_ids, *ops->id);
  829. }
  830. return error;
  831. }
  832. static void unregister_pernet_operations(struct pernet_operations *ops)
  833. {
  834. __unregister_pernet_operations(ops);
  835. rcu_barrier();
  836. if (ops->id)
  837. ida_remove(&net_generic_ids, *ops->id);
  838. }
  839. /**
  840. * register_pernet_subsys - register a network namespace subsystem
  841. * @ops: pernet operations structure for the subsystem
  842. *
  843. * Register a subsystem which has init and exit functions
  844. * that are called when network namespaces are created and
  845. * destroyed respectively.
  846. *
  847. * When registered all network namespace init functions are
  848. * called for every existing network namespace. Allowing kernel
  849. * modules to have a race free view of the set of network namespaces.
  850. *
  851. * When a new network namespace is created all of the init
  852. * methods are called in the order in which they were registered.
  853. *
  854. * When a network namespace is destroyed all of the exit methods
  855. * are called in the reverse of the order with which they were
  856. * registered.
  857. */
  858. int register_pernet_subsys(struct pernet_operations *ops)
  859. {
  860. int error;
  861. down_write(&pernet_ops_rwsem);
  862. error = register_pernet_operations(first_device, ops);
  863. up_write(&pernet_ops_rwsem);
  864. return error;
  865. }
  866. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  867. /**
  868. * unregister_pernet_subsys - unregister a network namespace subsystem
  869. * @ops: pernet operations structure to manipulate
  870. *
  871. * Remove the pernet operations structure from the list to be
  872. * used when network namespaces are created or destroyed. In
  873. * addition run the exit method for all existing network
  874. * namespaces.
  875. */
  876. void unregister_pernet_subsys(struct pernet_operations *ops)
  877. {
  878. down_write(&pernet_ops_rwsem);
  879. unregister_pernet_operations(ops);
  880. up_write(&pernet_ops_rwsem);
  881. }
  882. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  883. /**
  884. * register_pernet_device - register a network namespace device
  885. * @ops: pernet operations structure for the subsystem
  886. *
  887. * Register a device which has init and exit functions
  888. * that are called when network namespaces are created and
  889. * destroyed respectively.
  890. *
  891. * When registered all network namespace init functions are
  892. * called for every existing network namespace. Allowing kernel
  893. * modules to have a race free view of the set of network namespaces.
  894. *
  895. * When a new network namespace is created all of the init
  896. * methods are called in the order in which they were registered.
  897. *
  898. * When a network namespace is destroyed all of the exit methods
  899. * are called in the reverse of the order with which they were
  900. * registered.
  901. */
  902. int register_pernet_device(struct pernet_operations *ops)
  903. {
  904. int error;
  905. down_write(&pernet_ops_rwsem);
  906. error = register_pernet_operations(&pernet_list, ops);
  907. if (!error && (first_device == &pernet_list))
  908. first_device = &ops->list;
  909. up_write(&pernet_ops_rwsem);
  910. return error;
  911. }
  912. EXPORT_SYMBOL_GPL(register_pernet_device);
  913. /**
  914. * unregister_pernet_device - unregister a network namespace netdevice
  915. * @ops: pernet operations structure to manipulate
  916. *
  917. * Remove the pernet operations structure from the list to be
  918. * used when network namespaces are created or destroyed. In
  919. * addition run the exit method for all existing network
  920. * namespaces.
  921. */
  922. void unregister_pernet_device(struct pernet_operations *ops)
  923. {
  924. down_write(&pernet_ops_rwsem);
  925. if (&ops->list == first_device)
  926. first_device = first_device->next;
  927. unregister_pernet_operations(ops);
  928. up_write(&pernet_ops_rwsem);
  929. }
  930. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  931. #ifdef CONFIG_NET_NS
  932. static struct ns_common *netns_get(struct task_struct *task)
  933. {
  934. struct net *net = NULL;
  935. struct nsproxy *nsproxy;
  936. task_lock(task);
  937. nsproxy = task->nsproxy;
  938. if (nsproxy)
  939. net = get_net(nsproxy->net_ns);
  940. task_unlock(task);
  941. return net ? &net->ns : NULL;
  942. }
  943. static inline struct net *to_net_ns(struct ns_common *ns)
  944. {
  945. return container_of(ns, struct net, ns);
  946. }
  947. static void netns_put(struct ns_common *ns)
  948. {
  949. put_net(to_net_ns(ns));
  950. }
  951. static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  952. {
  953. struct net *net = to_net_ns(ns);
  954. if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
  955. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  956. return -EPERM;
  957. put_net(nsproxy->net_ns);
  958. nsproxy->net_ns = get_net(net);
  959. return 0;
  960. }
  961. static struct user_namespace *netns_owner(struct ns_common *ns)
  962. {
  963. return to_net_ns(ns)->user_ns;
  964. }
  965. const struct proc_ns_operations netns_operations = {
  966. .name = "net",
  967. .type = CLONE_NEWNET,
  968. .get = netns_get,
  969. .put = netns_put,
  970. .install = netns_install,
  971. .owner = netns_owner,
  972. };
  973. #endif