net_namespace.c 24 KB

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