net_namespace.c 23 KB

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