net_namespace.c 25 KB

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