net_namespace.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. bool alloc;
  181. int id;
  182. spin_lock_bh(&net->nsid_lock);
  183. alloc = atomic_read(&peer->count) == 0 ? false : true;
  184. id = __peernet2id_alloc(net, peer, &alloc);
  185. spin_unlock_bh(&net->nsid_lock);
  186. if (alloc && id >= 0)
  187. rtnl_net_notifyid(net, RTM_NEWNSID, id);
  188. return id;
  189. }
  190. /* This function returns, if assigned, the id of a peer netns. */
  191. int peernet2id(struct net *net, struct net *peer)
  192. {
  193. int id;
  194. spin_lock_bh(&net->nsid_lock);
  195. id = __peernet2id(net, peer);
  196. spin_unlock_bh(&net->nsid_lock);
  197. return id;
  198. }
  199. EXPORT_SYMBOL(peernet2id);
  200. /* This function returns true is the peer netns has an id assigned into the
  201. * current netns.
  202. */
  203. bool peernet_has_id(struct net *net, struct net *peer)
  204. {
  205. return peernet2id(net, peer) >= 0;
  206. }
  207. struct net *get_net_ns_by_id(struct net *net, int id)
  208. {
  209. struct net *peer;
  210. if (id < 0)
  211. return NULL;
  212. rcu_read_lock();
  213. spin_lock_bh(&net->nsid_lock);
  214. peer = idr_find(&net->netns_ids, id);
  215. if (peer)
  216. get_net(peer);
  217. spin_unlock_bh(&net->nsid_lock);
  218. rcu_read_unlock();
  219. return peer;
  220. }
  221. /*
  222. * setup_net runs the initializers for the network namespace object.
  223. */
  224. static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
  225. {
  226. /* Must be called with net_mutex held */
  227. const struct pernet_operations *ops, *saved_ops;
  228. int error = 0;
  229. LIST_HEAD(net_exit_list);
  230. atomic_set(&net->count, 1);
  231. atomic_set(&net->passive, 1);
  232. net->dev_base_seq = 1;
  233. net->user_ns = user_ns;
  234. idr_init(&net->netns_ids);
  235. spin_lock_init(&net->nsid_lock);
  236. list_for_each_entry(ops, &pernet_list, list) {
  237. error = ops_init(ops, net);
  238. if (error < 0)
  239. goto out_undo;
  240. }
  241. out:
  242. return error;
  243. out_undo:
  244. /* Walk through the list backwards calling the exit functions
  245. * for the pernet modules whose init functions did not fail.
  246. */
  247. list_add(&net->exit_list, &net_exit_list);
  248. saved_ops = ops;
  249. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  250. ops_exit_list(ops, &net_exit_list);
  251. ops = saved_ops;
  252. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  253. ops_free_list(ops, &net_exit_list);
  254. rcu_barrier();
  255. goto out;
  256. }
  257. #ifdef CONFIG_NET_NS
  258. static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
  259. {
  260. return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
  261. }
  262. static void dec_net_namespaces(struct ucounts *ucounts)
  263. {
  264. dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
  265. }
  266. static struct kmem_cache *net_cachep;
  267. static struct workqueue_struct *netns_wq;
  268. static struct net *net_alloc(void)
  269. {
  270. struct net *net = NULL;
  271. struct net_generic *ng;
  272. ng = net_alloc_generic();
  273. if (!ng)
  274. goto out;
  275. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  276. if (!net)
  277. goto out_free;
  278. rcu_assign_pointer(net->gen, ng);
  279. out:
  280. return net;
  281. out_free:
  282. kfree(ng);
  283. goto out;
  284. }
  285. static void net_free(struct net *net)
  286. {
  287. kfree(rcu_access_pointer(net->gen));
  288. kmem_cache_free(net_cachep, net);
  289. }
  290. void net_drop_ns(void *p)
  291. {
  292. struct net *ns = p;
  293. if (ns && atomic_dec_and_test(&ns->passive))
  294. net_free(ns);
  295. }
  296. struct net *copy_net_ns(unsigned long flags,
  297. struct user_namespace *user_ns, struct net *old_net)
  298. {
  299. struct ucounts *ucounts;
  300. struct net *net;
  301. int rv;
  302. if (!(flags & CLONE_NEWNET))
  303. return get_net(old_net);
  304. ucounts = inc_net_namespaces(user_ns);
  305. if (!ucounts)
  306. return ERR_PTR(-ENOSPC);
  307. net = net_alloc();
  308. if (!net) {
  309. dec_net_namespaces(ucounts);
  310. return ERR_PTR(-ENOMEM);
  311. }
  312. get_user_ns(user_ns);
  313. mutex_lock(&net_mutex);
  314. net->ucounts = ucounts;
  315. rv = setup_net(net, user_ns);
  316. if (rv == 0) {
  317. rtnl_lock();
  318. list_add_tail_rcu(&net->list, &net_namespace_list);
  319. rtnl_unlock();
  320. }
  321. mutex_unlock(&net_mutex);
  322. if (rv < 0) {
  323. dec_net_namespaces(ucounts);
  324. put_user_ns(user_ns);
  325. net_drop_ns(net);
  326. return ERR_PTR(rv);
  327. }
  328. return net;
  329. }
  330. static DEFINE_SPINLOCK(cleanup_list_lock);
  331. static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
  332. static void cleanup_net(struct work_struct *work)
  333. {
  334. const struct pernet_operations *ops;
  335. struct net *net, *tmp;
  336. struct list_head net_kill_list;
  337. LIST_HEAD(net_exit_list);
  338. /* Atomically snapshot the list of namespaces to cleanup */
  339. spin_lock_irq(&cleanup_list_lock);
  340. list_replace_init(&cleanup_list, &net_kill_list);
  341. spin_unlock_irq(&cleanup_list_lock);
  342. mutex_lock(&net_mutex);
  343. /* Don't let anyone else find us. */
  344. rtnl_lock();
  345. list_for_each_entry(net, &net_kill_list, cleanup_list) {
  346. list_del_rcu(&net->list);
  347. list_add_tail(&net->exit_list, &net_exit_list);
  348. for_each_net(tmp) {
  349. int id;
  350. spin_lock_bh(&tmp->nsid_lock);
  351. id = __peernet2id(tmp, net);
  352. if (id >= 0)
  353. idr_remove(&tmp->netns_ids, id);
  354. spin_unlock_bh(&tmp->nsid_lock);
  355. if (id >= 0)
  356. rtnl_net_notifyid(tmp, RTM_DELNSID, id);
  357. }
  358. spin_lock_bh(&net->nsid_lock);
  359. idr_destroy(&net->netns_ids);
  360. spin_unlock_bh(&net->nsid_lock);
  361. }
  362. rtnl_unlock();
  363. /*
  364. * Another CPU might be rcu-iterating the list, wait for it.
  365. * This needs to be before calling the exit() notifiers, so
  366. * the rcu_barrier() below isn't sufficient alone.
  367. */
  368. synchronize_rcu();
  369. /* Run all of the network namespace exit methods */
  370. list_for_each_entry_reverse(ops, &pernet_list, list)
  371. ops_exit_list(ops, &net_exit_list);
  372. /* Free the net generic variables */
  373. list_for_each_entry_reverse(ops, &pernet_list, list)
  374. ops_free_list(ops, &net_exit_list);
  375. mutex_unlock(&net_mutex);
  376. /* Ensure there are no outstanding rcu callbacks using this
  377. * network namespace.
  378. */
  379. rcu_barrier();
  380. /* Finally it is safe to free my network namespace structure */
  381. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  382. list_del_init(&net->exit_list);
  383. dec_net_namespaces(net->ucounts);
  384. put_user_ns(net->user_ns);
  385. net_drop_ns(net);
  386. }
  387. }
  388. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  389. void __put_net(struct net *net)
  390. {
  391. /* Cleanup the network namespace in process context */
  392. unsigned long flags;
  393. spin_lock_irqsave(&cleanup_list_lock, flags);
  394. list_add(&net->cleanup_list, &cleanup_list);
  395. spin_unlock_irqrestore(&cleanup_list_lock, flags);
  396. queue_work(netns_wq, &net_cleanup_work);
  397. }
  398. EXPORT_SYMBOL_GPL(__put_net);
  399. struct net *get_net_ns_by_fd(int fd)
  400. {
  401. struct file *file;
  402. struct ns_common *ns;
  403. struct net *net;
  404. file = proc_ns_fget(fd);
  405. if (IS_ERR(file))
  406. return ERR_CAST(file);
  407. ns = get_proc_ns(file_inode(file));
  408. if (ns->ops == &netns_operations)
  409. net = get_net(container_of(ns, struct net, ns));
  410. else
  411. net = ERR_PTR(-EINVAL);
  412. fput(file);
  413. return net;
  414. }
  415. #else
  416. struct net *get_net_ns_by_fd(int fd)
  417. {
  418. return ERR_PTR(-EINVAL);
  419. }
  420. #endif
  421. EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
  422. struct net *get_net_ns_by_pid(pid_t pid)
  423. {
  424. struct task_struct *tsk;
  425. struct net *net;
  426. /* Lookup the network namespace */
  427. net = ERR_PTR(-ESRCH);
  428. rcu_read_lock();
  429. tsk = find_task_by_vpid(pid);
  430. if (tsk) {
  431. struct nsproxy *nsproxy;
  432. task_lock(tsk);
  433. nsproxy = tsk->nsproxy;
  434. if (nsproxy)
  435. net = get_net(nsproxy->net_ns);
  436. task_unlock(tsk);
  437. }
  438. rcu_read_unlock();
  439. return net;
  440. }
  441. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  442. static __net_init int net_ns_net_init(struct net *net)
  443. {
  444. #ifdef CONFIG_NET_NS
  445. net->ns.ops = &netns_operations;
  446. #endif
  447. return ns_alloc_inum(&net->ns);
  448. }
  449. static __net_exit void net_ns_net_exit(struct net *net)
  450. {
  451. ns_free_inum(&net->ns);
  452. }
  453. static struct pernet_operations __net_initdata net_ns_ops = {
  454. .init = net_ns_net_init,
  455. .exit = net_ns_net_exit,
  456. };
  457. static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
  458. [NETNSA_NONE] = { .type = NLA_UNSPEC },
  459. [NETNSA_NSID] = { .type = NLA_S32 },
  460. [NETNSA_PID] = { .type = NLA_U32 },
  461. [NETNSA_FD] = { .type = NLA_U32 },
  462. };
  463. static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh)
  464. {
  465. struct net *net = sock_net(skb->sk);
  466. struct nlattr *tb[NETNSA_MAX + 1];
  467. struct net *peer;
  468. int nsid, err;
  469. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  470. rtnl_net_policy);
  471. if (err < 0)
  472. return err;
  473. if (!tb[NETNSA_NSID])
  474. return -EINVAL;
  475. nsid = nla_get_s32(tb[NETNSA_NSID]);
  476. if (tb[NETNSA_PID])
  477. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  478. else if (tb[NETNSA_FD])
  479. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  480. else
  481. return -EINVAL;
  482. if (IS_ERR(peer))
  483. return PTR_ERR(peer);
  484. spin_lock_bh(&net->nsid_lock);
  485. if (__peernet2id(net, peer) >= 0) {
  486. spin_unlock_bh(&net->nsid_lock);
  487. err = -EEXIST;
  488. goto out;
  489. }
  490. err = alloc_netid(net, peer, nsid);
  491. spin_unlock_bh(&net->nsid_lock);
  492. if (err >= 0) {
  493. rtnl_net_notifyid(net, RTM_NEWNSID, err);
  494. err = 0;
  495. }
  496. out:
  497. put_net(peer);
  498. return err;
  499. }
  500. static int rtnl_net_get_size(void)
  501. {
  502. return NLMSG_ALIGN(sizeof(struct rtgenmsg))
  503. + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
  504. ;
  505. }
  506. static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  507. int cmd, struct net *net, int nsid)
  508. {
  509. struct nlmsghdr *nlh;
  510. struct rtgenmsg *rth;
  511. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
  512. if (!nlh)
  513. return -EMSGSIZE;
  514. rth = nlmsg_data(nlh);
  515. rth->rtgen_family = AF_UNSPEC;
  516. if (nla_put_s32(skb, NETNSA_NSID, nsid))
  517. goto nla_put_failure;
  518. nlmsg_end(skb, nlh);
  519. return 0;
  520. nla_put_failure:
  521. nlmsg_cancel(skb, nlh);
  522. return -EMSGSIZE;
  523. }
  524. static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)
  525. {
  526. struct net *net = sock_net(skb->sk);
  527. struct nlattr *tb[NETNSA_MAX + 1];
  528. struct sk_buff *msg;
  529. struct net *peer;
  530. int err, id;
  531. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  532. rtnl_net_policy);
  533. if (err < 0)
  534. return err;
  535. if (tb[NETNSA_PID])
  536. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  537. else if (tb[NETNSA_FD])
  538. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  539. else
  540. return -EINVAL;
  541. if (IS_ERR(peer))
  542. return PTR_ERR(peer);
  543. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  544. if (!msg) {
  545. err = -ENOMEM;
  546. goto out;
  547. }
  548. id = peernet2id(net, peer);
  549. err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  550. RTM_NEWNSID, net, id);
  551. if (err < 0)
  552. goto err_out;
  553. err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
  554. goto out;
  555. err_out:
  556. nlmsg_free(msg);
  557. out:
  558. put_net(peer);
  559. return err;
  560. }
  561. struct rtnl_net_dump_cb {
  562. struct net *net;
  563. struct sk_buff *skb;
  564. struct netlink_callback *cb;
  565. int idx;
  566. int s_idx;
  567. };
  568. static int rtnl_net_dumpid_one(int id, void *peer, void *data)
  569. {
  570. struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
  571. int ret;
  572. if (net_cb->idx < net_cb->s_idx)
  573. goto cont;
  574. ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
  575. net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  576. RTM_NEWNSID, net_cb->net, id);
  577. if (ret < 0)
  578. return ret;
  579. cont:
  580. net_cb->idx++;
  581. return 0;
  582. }
  583. static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
  584. {
  585. struct net *net = sock_net(skb->sk);
  586. struct rtnl_net_dump_cb net_cb = {
  587. .net = net,
  588. .skb = skb,
  589. .cb = cb,
  590. .idx = 0,
  591. .s_idx = cb->args[0],
  592. };
  593. spin_lock_bh(&net->nsid_lock);
  594. idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
  595. spin_unlock_bh(&net->nsid_lock);
  596. cb->args[0] = net_cb.idx;
  597. return skb->len;
  598. }
  599. static void rtnl_net_notifyid(struct net *net, int cmd, int id)
  600. {
  601. struct sk_buff *msg;
  602. int err = -ENOMEM;
  603. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  604. if (!msg)
  605. goto out;
  606. err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
  607. if (err < 0)
  608. goto err_out;
  609. rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
  610. return;
  611. err_out:
  612. nlmsg_free(msg);
  613. out:
  614. rtnl_set_sk_err(net, RTNLGRP_NSID, err);
  615. }
  616. static int __init net_ns_init(void)
  617. {
  618. struct net_generic *ng;
  619. #ifdef CONFIG_NET_NS
  620. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  621. SMP_CACHE_BYTES,
  622. SLAB_PANIC, NULL);
  623. /* Create workqueue for cleanup */
  624. netns_wq = create_singlethread_workqueue("netns");
  625. if (!netns_wq)
  626. panic("Could not create netns workq");
  627. #endif
  628. ng = net_alloc_generic();
  629. if (!ng)
  630. panic("Could not allocate generic netns");
  631. rcu_assign_pointer(init_net.gen, ng);
  632. mutex_lock(&net_mutex);
  633. if (setup_net(&init_net, &init_user_ns))
  634. panic("Could not setup the initial network namespace");
  635. init_net_initialized = true;
  636. rtnl_lock();
  637. list_add_tail_rcu(&init_net.list, &net_namespace_list);
  638. rtnl_unlock();
  639. mutex_unlock(&net_mutex);
  640. register_pernet_subsys(&net_ns_ops);
  641. rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL, NULL);
  642. rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
  643. NULL);
  644. return 0;
  645. }
  646. pure_initcall(net_ns_init);
  647. #ifdef CONFIG_NET_NS
  648. static int __register_pernet_operations(struct list_head *list,
  649. struct pernet_operations *ops)
  650. {
  651. struct net *net;
  652. int error;
  653. LIST_HEAD(net_exit_list);
  654. list_add_tail(&ops->list, list);
  655. if (ops->init || (ops->id && ops->size)) {
  656. for_each_net(net) {
  657. error = ops_init(ops, net);
  658. if (error)
  659. goto out_undo;
  660. list_add_tail(&net->exit_list, &net_exit_list);
  661. }
  662. }
  663. return 0;
  664. out_undo:
  665. /* If I have an error cleanup all namespaces I initialized */
  666. list_del(&ops->list);
  667. ops_exit_list(ops, &net_exit_list);
  668. ops_free_list(ops, &net_exit_list);
  669. return error;
  670. }
  671. static void __unregister_pernet_operations(struct pernet_operations *ops)
  672. {
  673. struct net *net;
  674. LIST_HEAD(net_exit_list);
  675. list_del(&ops->list);
  676. for_each_net(net)
  677. list_add_tail(&net->exit_list, &net_exit_list);
  678. ops_exit_list(ops, &net_exit_list);
  679. ops_free_list(ops, &net_exit_list);
  680. }
  681. #else
  682. static int __register_pernet_operations(struct list_head *list,
  683. struct pernet_operations *ops)
  684. {
  685. if (!init_net_initialized) {
  686. list_add_tail(&ops->list, list);
  687. return 0;
  688. }
  689. return ops_init(ops, &init_net);
  690. }
  691. static void __unregister_pernet_operations(struct pernet_operations *ops)
  692. {
  693. if (!init_net_initialized) {
  694. list_del(&ops->list);
  695. } else {
  696. LIST_HEAD(net_exit_list);
  697. list_add(&init_net.exit_list, &net_exit_list);
  698. ops_exit_list(ops, &net_exit_list);
  699. ops_free_list(ops, &net_exit_list);
  700. }
  701. }
  702. #endif /* CONFIG_NET_NS */
  703. static DEFINE_IDA(net_generic_ids);
  704. static int register_pernet_operations(struct list_head *list,
  705. struct pernet_operations *ops)
  706. {
  707. int error;
  708. if (ops->id) {
  709. again:
  710. error = ida_get_new_above(&net_generic_ids, 1, ops->id);
  711. if (error < 0) {
  712. if (error == -EAGAIN) {
  713. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  714. goto again;
  715. }
  716. return error;
  717. }
  718. max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id);
  719. }
  720. error = __register_pernet_operations(list, ops);
  721. if (error) {
  722. rcu_barrier();
  723. if (ops->id)
  724. ida_remove(&net_generic_ids, *ops->id);
  725. }
  726. return error;
  727. }
  728. static void unregister_pernet_operations(struct pernet_operations *ops)
  729. {
  730. __unregister_pernet_operations(ops);
  731. rcu_barrier();
  732. if (ops->id)
  733. ida_remove(&net_generic_ids, *ops->id);
  734. }
  735. /**
  736. * register_pernet_subsys - register a network namespace subsystem
  737. * @ops: pernet operations structure for the subsystem
  738. *
  739. * Register a subsystem which has init and exit functions
  740. * that are called when network namespaces are created and
  741. * destroyed respectively.
  742. *
  743. * When registered all network namespace init functions are
  744. * called for every existing network namespace. Allowing kernel
  745. * modules to have a race free view of the set of network namespaces.
  746. *
  747. * When a new network namespace is created all of the init
  748. * methods are called in the order in which they were registered.
  749. *
  750. * When a network namespace is destroyed all of the exit methods
  751. * are called in the reverse of the order with which they were
  752. * registered.
  753. */
  754. int register_pernet_subsys(struct pernet_operations *ops)
  755. {
  756. int error;
  757. mutex_lock(&net_mutex);
  758. error = register_pernet_operations(first_device, ops);
  759. mutex_unlock(&net_mutex);
  760. return error;
  761. }
  762. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  763. /**
  764. * unregister_pernet_subsys - unregister a network namespace subsystem
  765. * @ops: pernet operations structure to manipulate
  766. *
  767. * Remove the pernet operations structure from the list to be
  768. * used when network namespaces are created or destroyed. In
  769. * addition run the exit method for all existing network
  770. * namespaces.
  771. */
  772. void unregister_pernet_subsys(struct pernet_operations *ops)
  773. {
  774. mutex_lock(&net_mutex);
  775. unregister_pernet_operations(ops);
  776. mutex_unlock(&net_mutex);
  777. }
  778. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  779. /**
  780. * register_pernet_device - register a network namespace device
  781. * @ops: pernet operations structure for the subsystem
  782. *
  783. * Register a device which has init and exit functions
  784. * that are called when network namespaces are created and
  785. * destroyed respectively.
  786. *
  787. * When registered all network namespace init functions are
  788. * called for every existing network namespace. Allowing kernel
  789. * modules to have a race free view of the set of network namespaces.
  790. *
  791. * When a new network namespace is created all of the init
  792. * methods are called in the order in which they were registered.
  793. *
  794. * When a network namespace is destroyed all of the exit methods
  795. * are called in the reverse of the order with which they were
  796. * registered.
  797. */
  798. int register_pernet_device(struct pernet_operations *ops)
  799. {
  800. int error;
  801. mutex_lock(&net_mutex);
  802. error = register_pernet_operations(&pernet_list, ops);
  803. if (!error && (first_device == &pernet_list))
  804. first_device = &ops->list;
  805. mutex_unlock(&net_mutex);
  806. return error;
  807. }
  808. EXPORT_SYMBOL_GPL(register_pernet_device);
  809. /**
  810. * unregister_pernet_device - unregister a network namespace netdevice
  811. * @ops: pernet operations structure to manipulate
  812. *
  813. * Remove the pernet operations structure from the list to be
  814. * used when network namespaces are created or destroyed. In
  815. * addition run the exit method for all existing network
  816. * namespaces.
  817. */
  818. void unregister_pernet_device(struct pernet_operations *ops)
  819. {
  820. mutex_lock(&net_mutex);
  821. if (&ops->list == first_device)
  822. first_device = first_device->next;
  823. unregister_pernet_operations(ops);
  824. mutex_unlock(&net_mutex);
  825. }
  826. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  827. #ifdef CONFIG_NET_NS
  828. static struct ns_common *netns_get(struct task_struct *task)
  829. {
  830. struct net *net = NULL;
  831. struct nsproxy *nsproxy;
  832. task_lock(task);
  833. nsproxy = task->nsproxy;
  834. if (nsproxy)
  835. net = get_net(nsproxy->net_ns);
  836. task_unlock(task);
  837. return net ? &net->ns : NULL;
  838. }
  839. static inline struct net *to_net_ns(struct ns_common *ns)
  840. {
  841. return container_of(ns, struct net, ns);
  842. }
  843. static void netns_put(struct ns_common *ns)
  844. {
  845. put_net(to_net_ns(ns));
  846. }
  847. static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  848. {
  849. struct net *net = to_net_ns(ns);
  850. if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
  851. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  852. return -EPERM;
  853. put_net(nsproxy->net_ns);
  854. nsproxy->net_ns = get_net(net);
  855. return 0;
  856. }
  857. static struct user_namespace *netns_owner(struct ns_common *ns)
  858. {
  859. return to_net_ns(ns)->user_ns;
  860. }
  861. const struct proc_ns_operations netns_operations = {
  862. .name = "net",
  863. .type = CLONE_NEWNET,
  864. .get = netns_get,
  865. .put = netns_put,
  866. .install = netns_install,
  867. .owner = netns_owner,
  868. };
  869. #endif