net_namespace.c 24 KB

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