net_namespace.c 27 KB

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