net_namespace.c 23 KB

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