af_netlink.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. /*
  2. * NETLINK Kernel-user communication protocol.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  13. * added netlink_proto_exit
  14. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  15. * use nlk_sk, as sk->protinfo is on a diet 8)
  16. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  17. * - inc module use count of module that owns
  18. * the kernel socket in case userspace opens
  19. * socket of same protocol
  20. * - remove all module support, since netlink is
  21. * mandatory if CONFIG_NET=y these days
  22. */
  23. #include <linux/module.h>
  24. #include <linux/capability.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/signal.h>
  28. #include <linux/sched.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. #include <linux/stat.h>
  32. #include <linux/socket.h>
  33. #include <linux/un.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/termios.h>
  36. #include <linux/sockios.h>
  37. #include <linux/net.h>
  38. #include <linux/fs.h>
  39. #include <linux/slab.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/rtnetlink.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/notifier.h>
  47. #include <linux/security.h>
  48. #include <linux/jhash.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/random.h>
  51. #include <linux/bitops.h>
  52. #include <linux/mm.h>
  53. #include <linux/types.h>
  54. #include <linux/audit.h>
  55. #include <linux/mutex.h>
  56. #include <net/net_namespace.h>
  57. #include <net/sock.h>
  58. #include <net/scm.h>
  59. #include <net/netlink.h>
  60. #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
  61. #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
  62. struct netlink_sock {
  63. /* struct sock has to be the first member of netlink_sock */
  64. struct sock sk;
  65. u32 pid;
  66. u32 dst_pid;
  67. u32 dst_group;
  68. u32 flags;
  69. u32 subscriptions;
  70. u32 ngroups;
  71. unsigned long *groups;
  72. unsigned long state;
  73. wait_queue_head_t wait;
  74. struct netlink_callback *cb;
  75. struct mutex *cb_mutex;
  76. struct mutex cb_def_mutex;
  77. void (*netlink_rcv)(struct sk_buff *skb);
  78. struct module *module;
  79. };
  80. struct listeners_rcu_head {
  81. struct rcu_head rcu_head;
  82. void *ptr;
  83. };
  84. #define NETLINK_KERNEL_SOCKET 0x1
  85. #define NETLINK_RECV_PKTINFO 0x2
  86. #define NETLINK_BROADCAST_SEND_ERROR 0x4
  87. #define NETLINK_RECV_NO_ENOBUFS 0x8
  88. static inline struct netlink_sock *nlk_sk(struct sock *sk)
  89. {
  90. return container_of(sk, struct netlink_sock, sk);
  91. }
  92. static inline int netlink_is_kernel(struct sock *sk)
  93. {
  94. return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
  95. }
  96. struct nl_pid_hash {
  97. struct hlist_head *table;
  98. unsigned long rehash_time;
  99. unsigned int mask;
  100. unsigned int shift;
  101. unsigned int entries;
  102. unsigned int max_shift;
  103. u32 rnd;
  104. };
  105. struct netlink_table {
  106. struct nl_pid_hash hash;
  107. struct hlist_head mc_list;
  108. unsigned long *listeners;
  109. unsigned int nl_nonroot;
  110. unsigned int groups;
  111. struct mutex *cb_mutex;
  112. struct module *module;
  113. int registered;
  114. };
  115. static struct netlink_table *nl_table;
  116. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  117. static int netlink_dump(struct sock *sk);
  118. static void netlink_destroy_callback(struct netlink_callback *cb);
  119. static DEFINE_RWLOCK(nl_table_lock);
  120. static atomic_t nl_table_users = ATOMIC_INIT(0);
  121. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  122. static u32 netlink_group_mask(u32 group)
  123. {
  124. return group ? 1 << (group - 1) : 0;
  125. }
  126. static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
  127. {
  128. return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
  129. }
  130. static void netlink_sock_destruct(struct sock *sk)
  131. {
  132. struct netlink_sock *nlk = nlk_sk(sk);
  133. if (nlk->cb) {
  134. if (nlk->cb->done)
  135. nlk->cb->done(nlk->cb);
  136. netlink_destroy_callback(nlk->cb);
  137. }
  138. skb_queue_purge(&sk->sk_receive_queue);
  139. if (!sock_flag(sk, SOCK_DEAD)) {
  140. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  141. return;
  142. }
  143. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  144. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  145. WARN_ON(nlk_sk(sk)->groups);
  146. }
  147. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  148. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  149. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  150. * this, _but_ remember, it adds useless work on UP machines.
  151. */
  152. static void netlink_table_grab(void)
  153. __acquires(nl_table_lock)
  154. {
  155. write_lock_irq(&nl_table_lock);
  156. if (atomic_read(&nl_table_users)) {
  157. DECLARE_WAITQUEUE(wait, current);
  158. add_wait_queue_exclusive(&nl_table_wait, &wait);
  159. for (;;) {
  160. set_current_state(TASK_UNINTERRUPTIBLE);
  161. if (atomic_read(&nl_table_users) == 0)
  162. break;
  163. write_unlock_irq(&nl_table_lock);
  164. schedule();
  165. write_lock_irq(&nl_table_lock);
  166. }
  167. __set_current_state(TASK_RUNNING);
  168. remove_wait_queue(&nl_table_wait, &wait);
  169. }
  170. }
  171. static void netlink_table_ungrab(void)
  172. __releases(nl_table_lock)
  173. {
  174. write_unlock_irq(&nl_table_lock);
  175. wake_up(&nl_table_wait);
  176. }
  177. static inline void
  178. netlink_lock_table(void)
  179. {
  180. /* read_lock() synchronizes us to netlink_table_grab */
  181. read_lock(&nl_table_lock);
  182. atomic_inc(&nl_table_users);
  183. read_unlock(&nl_table_lock);
  184. }
  185. static inline void
  186. netlink_unlock_table(void)
  187. {
  188. if (atomic_dec_and_test(&nl_table_users))
  189. wake_up(&nl_table_wait);
  190. }
  191. static inline struct sock *netlink_lookup(struct net *net, int protocol,
  192. u32 pid)
  193. {
  194. struct nl_pid_hash *hash = &nl_table[protocol].hash;
  195. struct hlist_head *head;
  196. struct sock *sk;
  197. struct hlist_node *node;
  198. read_lock(&nl_table_lock);
  199. head = nl_pid_hashfn(hash, pid);
  200. sk_for_each(sk, node, head) {
  201. if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
  202. sock_hold(sk);
  203. goto found;
  204. }
  205. }
  206. sk = NULL;
  207. found:
  208. read_unlock(&nl_table_lock);
  209. return sk;
  210. }
  211. static inline struct hlist_head *nl_pid_hash_zalloc(size_t size)
  212. {
  213. if (size <= PAGE_SIZE)
  214. return kzalloc(size, GFP_ATOMIC);
  215. else
  216. return (struct hlist_head *)
  217. __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
  218. get_order(size));
  219. }
  220. static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
  221. {
  222. if (size <= PAGE_SIZE)
  223. kfree(table);
  224. else
  225. free_pages((unsigned long)table, get_order(size));
  226. }
  227. static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
  228. {
  229. unsigned int omask, mask, shift;
  230. size_t osize, size;
  231. struct hlist_head *otable, *table;
  232. int i;
  233. omask = mask = hash->mask;
  234. osize = size = (mask + 1) * sizeof(*table);
  235. shift = hash->shift;
  236. if (grow) {
  237. if (++shift > hash->max_shift)
  238. return 0;
  239. mask = mask * 2 + 1;
  240. size *= 2;
  241. }
  242. table = nl_pid_hash_zalloc(size);
  243. if (!table)
  244. return 0;
  245. otable = hash->table;
  246. hash->table = table;
  247. hash->mask = mask;
  248. hash->shift = shift;
  249. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  250. for (i = 0; i <= omask; i++) {
  251. struct sock *sk;
  252. struct hlist_node *node, *tmp;
  253. sk_for_each_safe(sk, node, tmp, &otable[i])
  254. __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
  255. }
  256. nl_pid_hash_free(otable, osize);
  257. hash->rehash_time = jiffies + 10 * 60 * HZ;
  258. return 1;
  259. }
  260. static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
  261. {
  262. int avg = hash->entries >> hash->shift;
  263. if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
  264. return 1;
  265. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  266. nl_pid_hash_rehash(hash, 0);
  267. return 1;
  268. }
  269. return 0;
  270. }
  271. static const struct proto_ops netlink_ops;
  272. static void
  273. netlink_update_listeners(struct sock *sk)
  274. {
  275. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  276. struct hlist_node *node;
  277. unsigned long mask;
  278. unsigned int i;
  279. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  280. mask = 0;
  281. sk_for_each_bound(sk, node, &tbl->mc_list) {
  282. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  283. mask |= nlk_sk(sk)->groups[i];
  284. }
  285. tbl->listeners[i] = mask;
  286. }
  287. /* this function is only called with the netlink table "grabbed", which
  288. * makes sure updates are visible before bind or setsockopt return. */
  289. }
  290. static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
  291. {
  292. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  293. struct hlist_head *head;
  294. int err = -EADDRINUSE;
  295. struct sock *osk;
  296. struct hlist_node *node;
  297. int len;
  298. netlink_table_grab();
  299. head = nl_pid_hashfn(hash, pid);
  300. len = 0;
  301. sk_for_each(osk, node, head) {
  302. if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
  303. break;
  304. len++;
  305. }
  306. if (node)
  307. goto err;
  308. err = -EBUSY;
  309. if (nlk_sk(sk)->pid)
  310. goto err;
  311. err = -ENOMEM;
  312. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  313. goto err;
  314. if (len && nl_pid_hash_dilute(hash, len))
  315. head = nl_pid_hashfn(hash, pid);
  316. hash->entries++;
  317. nlk_sk(sk)->pid = pid;
  318. sk_add_node(sk, head);
  319. err = 0;
  320. err:
  321. netlink_table_ungrab();
  322. return err;
  323. }
  324. static void netlink_remove(struct sock *sk)
  325. {
  326. netlink_table_grab();
  327. if (sk_del_node_init(sk))
  328. nl_table[sk->sk_protocol].hash.entries--;
  329. if (nlk_sk(sk)->subscriptions)
  330. __sk_del_bind_node(sk);
  331. netlink_table_ungrab();
  332. }
  333. static struct proto netlink_proto = {
  334. .name = "NETLINK",
  335. .owner = THIS_MODULE,
  336. .obj_size = sizeof(struct netlink_sock),
  337. };
  338. static int __netlink_create(struct net *net, struct socket *sock,
  339. struct mutex *cb_mutex, int protocol)
  340. {
  341. struct sock *sk;
  342. struct netlink_sock *nlk;
  343. sock->ops = &netlink_ops;
  344. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  345. if (!sk)
  346. return -ENOMEM;
  347. sock_init_data(sock, sk);
  348. nlk = nlk_sk(sk);
  349. if (cb_mutex)
  350. nlk->cb_mutex = cb_mutex;
  351. else {
  352. nlk->cb_mutex = &nlk->cb_def_mutex;
  353. mutex_init(nlk->cb_mutex);
  354. }
  355. init_waitqueue_head(&nlk->wait);
  356. sk->sk_destruct = netlink_sock_destruct;
  357. sk->sk_protocol = protocol;
  358. return 0;
  359. }
  360. static int netlink_create(struct net *net, struct socket *sock, int protocol)
  361. {
  362. struct module *module = NULL;
  363. struct mutex *cb_mutex;
  364. struct netlink_sock *nlk;
  365. int err = 0;
  366. sock->state = SS_UNCONNECTED;
  367. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  368. return -ESOCKTNOSUPPORT;
  369. if (protocol < 0 || protocol >= MAX_LINKS)
  370. return -EPROTONOSUPPORT;
  371. netlink_lock_table();
  372. #ifdef CONFIG_MODULES
  373. if (!nl_table[protocol].registered) {
  374. netlink_unlock_table();
  375. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  376. netlink_lock_table();
  377. }
  378. #endif
  379. if (nl_table[protocol].registered &&
  380. try_module_get(nl_table[protocol].module))
  381. module = nl_table[protocol].module;
  382. cb_mutex = nl_table[protocol].cb_mutex;
  383. netlink_unlock_table();
  384. err = __netlink_create(net, sock, cb_mutex, protocol);
  385. if (err < 0)
  386. goto out_module;
  387. local_bh_disable();
  388. sock_prot_inuse_add(net, &netlink_proto, 1);
  389. local_bh_enable();
  390. nlk = nlk_sk(sock->sk);
  391. nlk->module = module;
  392. out:
  393. return err;
  394. out_module:
  395. module_put(module);
  396. goto out;
  397. }
  398. static int netlink_release(struct socket *sock)
  399. {
  400. struct sock *sk = sock->sk;
  401. struct netlink_sock *nlk;
  402. if (!sk)
  403. return 0;
  404. netlink_remove(sk);
  405. sock_orphan(sk);
  406. nlk = nlk_sk(sk);
  407. /*
  408. * OK. Socket is unlinked, any packets that arrive now
  409. * will be purged.
  410. */
  411. sock->sk = NULL;
  412. wake_up_interruptible_all(&nlk->wait);
  413. skb_queue_purge(&sk->sk_write_queue);
  414. if (nlk->pid && !nlk->subscriptions) {
  415. struct netlink_notify n = {
  416. .net = sock_net(sk),
  417. .protocol = sk->sk_protocol,
  418. .pid = nlk->pid,
  419. };
  420. atomic_notifier_call_chain(&netlink_chain,
  421. NETLINK_URELEASE, &n);
  422. }
  423. module_put(nlk->module);
  424. netlink_table_grab();
  425. if (netlink_is_kernel(sk)) {
  426. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  427. if (--nl_table[sk->sk_protocol].registered == 0) {
  428. kfree(nl_table[sk->sk_protocol].listeners);
  429. nl_table[sk->sk_protocol].module = NULL;
  430. nl_table[sk->sk_protocol].registered = 0;
  431. }
  432. } else if (nlk->subscriptions)
  433. netlink_update_listeners(sk);
  434. netlink_table_ungrab();
  435. kfree(nlk->groups);
  436. nlk->groups = NULL;
  437. local_bh_disable();
  438. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  439. local_bh_enable();
  440. sock_put(sk);
  441. return 0;
  442. }
  443. static int netlink_autobind(struct socket *sock)
  444. {
  445. struct sock *sk = sock->sk;
  446. struct net *net = sock_net(sk);
  447. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  448. struct hlist_head *head;
  449. struct sock *osk;
  450. struct hlist_node *node;
  451. s32 pid = current->tgid;
  452. int err;
  453. static s32 rover = -4097;
  454. retry:
  455. cond_resched();
  456. netlink_table_grab();
  457. head = nl_pid_hashfn(hash, pid);
  458. sk_for_each(osk, node, head) {
  459. if (!net_eq(sock_net(osk), net))
  460. continue;
  461. if (nlk_sk(osk)->pid == pid) {
  462. /* Bind collision, search negative pid values. */
  463. pid = rover--;
  464. if (rover > -4097)
  465. rover = -4097;
  466. netlink_table_ungrab();
  467. goto retry;
  468. }
  469. }
  470. netlink_table_ungrab();
  471. err = netlink_insert(sk, net, pid);
  472. if (err == -EADDRINUSE)
  473. goto retry;
  474. /* If 2 threads race to autobind, that is fine. */
  475. if (err == -EBUSY)
  476. err = 0;
  477. return err;
  478. }
  479. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  480. {
  481. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  482. capable(CAP_NET_ADMIN);
  483. }
  484. static void
  485. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  486. {
  487. struct netlink_sock *nlk = nlk_sk(sk);
  488. if (nlk->subscriptions && !subscriptions)
  489. __sk_del_bind_node(sk);
  490. else if (!nlk->subscriptions && subscriptions)
  491. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  492. nlk->subscriptions = subscriptions;
  493. }
  494. static int netlink_realloc_groups(struct sock *sk)
  495. {
  496. struct netlink_sock *nlk = nlk_sk(sk);
  497. unsigned int groups;
  498. unsigned long *new_groups;
  499. int err = 0;
  500. netlink_table_grab();
  501. groups = nl_table[sk->sk_protocol].groups;
  502. if (!nl_table[sk->sk_protocol].registered) {
  503. err = -ENOENT;
  504. goto out_unlock;
  505. }
  506. if (nlk->ngroups >= groups)
  507. goto out_unlock;
  508. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  509. if (new_groups == NULL) {
  510. err = -ENOMEM;
  511. goto out_unlock;
  512. }
  513. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  514. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  515. nlk->groups = new_groups;
  516. nlk->ngroups = groups;
  517. out_unlock:
  518. netlink_table_ungrab();
  519. return err;
  520. }
  521. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  522. int addr_len)
  523. {
  524. struct sock *sk = sock->sk;
  525. struct net *net = sock_net(sk);
  526. struct netlink_sock *nlk = nlk_sk(sk);
  527. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  528. int err;
  529. if (nladdr->nl_family != AF_NETLINK)
  530. return -EINVAL;
  531. /* Only superuser is allowed to listen multicasts */
  532. if (nladdr->nl_groups) {
  533. if (!netlink_capable(sock, NL_NONROOT_RECV))
  534. return -EPERM;
  535. err = netlink_realloc_groups(sk);
  536. if (err)
  537. return err;
  538. }
  539. if (nlk->pid) {
  540. if (nladdr->nl_pid != nlk->pid)
  541. return -EINVAL;
  542. } else {
  543. err = nladdr->nl_pid ?
  544. netlink_insert(sk, net, nladdr->nl_pid) :
  545. netlink_autobind(sock);
  546. if (err)
  547. return err;
  548. }
  549. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  550. return 0;
  551. netlink_table_grab();
  552. netlink_update_subscriptions(sk, nlk->subscriptions +
  553. hweight32(nladdr->nl_groups) -
  554. hweight32(nlk->groups[0]));
  555. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  556. netlink_update_listeners(sk);
  557. netlink_table_ungrab();
  558. return 0;
  559. }
  560. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  561. int alen, int flags)
  562. {
  563. int err = 0;
  564. struct sock *sk = sock->sk;
  565. struct netlink_sock *nlk = nlk_sk(sk);
  566. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  567. if (addr->sa_family == AF_UNSPEC) {
  568. sk->sk_state = NETLINK_UNCONNECTED;
  569. nlk->dst_pid = 0;
  570. nlk->dst_group = 0;
  571. return 0;
  572. }
  573. if (addr->sa_family != AF_NETLINK)
  574. return -EINVAL;
  575. /* Only superuser is allowed to send multicasts */
  576. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  577. return -EPERM;
  578. if (!nlk->pid)
  579. err = netlink_autobind(sock);
  580. if (err == 0) {
  581. sk->sk_state = NETLINK_CONNECTED;
  582. nlk->dst_pid = nladdr->nl_pid;
  583. nlk->dst_group = ffs(nladdr->nl_groups);
  584. }
  585. return err;
  586. }
  587. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  588. int *addr_len, int peer)
  589. {
  590. struct sock *sk = sock->sk;
  591. struct netlink_sock *nlk = nlk_sk(sk);
  592. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  593. nladdr->nl_family = AF_NETLINK;
  594. nladdr->nl_pad = 0;
  595. *addr_len = sizeof(*nladdr);
  596. if (peer) {
  597. nladdr->nl_pid = nlk->dst_pid;
  598. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  599. } else {
  600. nladdr->nl_pid = nlk->pid;
  601. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  602. }
  603. return 0;
  604. }
  605. static void netlink_overrun(struct sock *sk)
  606. {
  607. struct netlink_sock *nlk = nlk_sk(sk);
  608. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  609. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  610. sk->sk_err = ENOBUFS;
  611. sk->sk_error_report(sk);
  612. }
  613. }
  614. atomic_inc(&sk->sk_drops);
  615. }
  616. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  617. {
  618. struct sock *sock;
  619. struct netlink_sock *nlk;
  620. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
  621. if (!sock)
  622. return ERR_PTR(-ECONNREFUSED);
  623. /* Don't bother queuing skb if kernel socket has no input function */
  624. nlk = nlk_sk(sock);
  625. if (sock->sk_state == NETLINK_CONNECTED &&
  626. nlk->dst_pid != nlk_sk(ssk)->pid) {
  627. sock_put(sock);
  628. return ERR_PTR(-ECONNREFUSED);
  629. }
  630. return sock;
  631. }
  632. struct sock *netlink_getsockbyfilp(struct file *filp)
  633. {
  634. struct inode *inode = filp->f_path.dentry->d_inode;
  635. struct sock *sock;
  636. if (!S_ISSOCK(inode->i_mode))
  637. return ERR_PTR(-ENOTSOCK);
  638. sock = SOCKET_I(inode)->sk;
  639. if (sock->sk_family != AF_NETLINK)
  640. return ERR_PTR(-EINVAL);
  641. sock_hold(sock);
  642. return sock;
  643. }
  644. /*
  645. * Attach a skb to a netlink socket.
  646. * The caller must hold a reference to the destination socket. On error, the
  647. * reference is dropped. The skb is not send to the destination, just all
  648. * all error checks are performed and memory in the queue is reserved.
  649. * Return values:
  650. * < 0: error. skb freed, reference to sock dropped.
  651. * 0: continue
  652. * 1: repeat lookup - reference dropped while waiting for socket memory.
  653. */
  654. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  655. long *timeo, struct sock *ssk)
  656. {
  657. struct netlink_sock *nlk;
  658. nlk = nlk_sk(sk);
  659. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  660. test_bit(0, &nlk->state)) {
  661. DECLARE_WAITQUEUE(wait, current);
  662. if (!*timeo) {
  663. if (!ssk || netlink_is_kernel(ssk))
  664. netlink_overrun(sk);
  665. sock_put(sk);
  666. kfree_skb(skb);
  667. return -EAGAIN;
  668. }
  669. __set_current_state(TASK_INTERRUPTIBLE);
  670. add_wait_queue(&nlk->wait, &wait);
  671. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  672. test_bit(0, &nlk->state)) &&
  673. !sock_flag(sk, SOCK_DEAD))
  674. *timeo = schedule_timeout(*timeo);
  675. __set_current_state(TASK_RUNNING);
  676. remove_wait_queue(&nlk->wait, &wait);
  677. sock_put(sk);
  678. if (signal_pending(current)) {
  679. kfree_skb(skb);
  680. return sock_intr_errno(*timeo);
  681. }
  682. return 1;
  683. }
  684. skb_set_owner_r(skb, sk);
  685. return 0;
  686. }
  687. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  688. {
  689. int len = skb->len;
  690. skb_queue_tail(&sk->sk_receive_queue, skb);
  691. sk->sk_data_ready(sk, len);
  692. sock_put(sk);
  693. return len;
  694. }
  695. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  696. {
  697. kfree_skb(skb);
  698. sock_put(sk);
  699. }
  700. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  701. gfp_t allocation)
  702. {
  703. int delta;
  704. skb_orphan(skb);
  705. delta = skb->end - skb->tail;
  706. if (delta * 2 < skb->truesize)
  707. return skb;
  708. if (skb_shared(skb)) {
  709. struct sk_buff *nskb = skb_clone(skb, allocation);
  710. if (!nskb)
  711. return skb;
  712. kfree_skb(skb);
  713. skb = nskb;
  714. }
  715. if (!pskb_expand_head(skb, 0, -delta, allocation))
  716. skb->truesize -= delta;
  717. return skb;
  718. }
  719. static inline void netlink_rcv_wake(struct sock *sk)
  720. {
  721. struct netlink_sock *nlk = nlk_sk(sk);
  722. if (skb_queue_empty(&sk->sk_receive_queue))
  723. clear_bit(0, &nlk->state);
  724. if (!test_bit(0, &nlk->state))
  725. wake_up_interruptible(&nlk->wait);
  726. }
  727. static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
  728. {
  729. int ret;
  730. struct netlink_sock *nlk = nlk_sk(sk);
  731. ret = -ECONNREFUSED;
  732. if (nlk->netlink_rcv != NULL) {
  733. ret = skb->len;
  734. skb_set_owner_r(skb, sk);
  735. nlk->netlink_rcv(skb);
  736. }
  737. kfree_skb(skb);
  738. sock_put(sk);
  739. return ret;
  740. }
  741. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  742. u32 pid, int nonblock)
  743. {
  744. struct sock *sk;
  745. int err;
  746. long timeo;
  747. skb = netlink_trim(skb, gfp_any());
  748. timeo = sock_sndtimeo(ssk, nonblock);
  749. retry:
  750. sk = netlink_getsockbypid(ssk, pid);
  751. if (IS_ERR(sk)) {
  752. kfree_skb(skb);
  753. return PTR_ERR(sk);
  754. }
  755. if (netlink_is_kernel(sk))
  756. return netlink_unicast_kernel(sk, skb);
  757. if (sk_filter(sk, skb)) {
  758. err = skb->len;
  759. kfree_skb(skb);
  760. sock_put(sk);
  761. return err;
  762. }
  763. err = netlink_attachskb(sk, skb, &timeo, ssk);
  764. if (err == 1)
  765. goto retry;
  766. if (err)
  767. return err;
  768. return netlink_sendskb(sk, skb);
  769. }
  770. EXPORT_SYMBOL(netlink_unicast);
  771. int netlink_has_listeners(struct sock *sk, unsigned int group)
  772. {
  773. int res = 0;
  774. unsigned long *listeners;
  775. BUG_ON(!netlink_is_kernel(sk));
  776. rcu_read_lock();
  777. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  778. if (group - 1 < nl_table[sk->sk_protocol].groups)
  779. res = test_bit(group - 1, listeners);
  780. rcu_read_unlock();
  781. return res;
  782. }
  783. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  784. static inline int netlink_broadcast_deliver(struct sock *sk,
  785. struct sk_buff *skb)
  786. {
  787. struct netlink_sock *nlk = nlk_sk(sk);
  788. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  789. !test_bit(0, &nlk->state)) {
  790. skb_set_owner_r(skb, sk);
  791. skb_queue_tail(&sk->sk_receive_queue, skb);
  792. sk->sk_data_ready(sk, skb->len);
  793. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  794. }
  795. return -1;
  796. }
  797. struct netlink_broadcast_data {
  798. struct sock *exclude_sk;
  799. struct net *net;
  800. u32 pid;
  801. u32 group;
  802. int failure;
  803. int delivery_failure;
  804. int congested;
  805. int delivered;
  806. gfp_t allocation;
  807. struct sk_buff *skb, *skb2;
  808. };
  809. static inline int do_one_broadcast(struct sock *sk,
  810. struct netlink_broadcast_data *p)
  811. {
  812. struct netlink_sock *nlk = nlk_sk(sk);
  813. int val;
  814. if (p->exclude_sk == sk)
  815. goto out;
  816. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  817. !test_bit(p->group - 1, nlk->groups))
  818. goto out;
  819. if (!net_eq(sock_net(sk), p->net))
  820. goto out;
  821. if (p->failure) {
  822. netlink_overrun(sk);
  823. goto out;
  824. }
  825. sock_hold(sk);
  826. if (p->skb2 == NULL) {
  827. if (skb_shared(p->skb)) {
  828. p->skb2 = skb_clone(p->skb, p->allocation);
  829. } else {
  830. p->skb2 = skb_get(p->skb);
  831. /*
  832. * skb ownership may have been set when
  833. * delivered to a previous socket.
  834. */
  835. skb_orphan(p->skb2);
  836. }
  837. }
  838. if (p->skb2 == NULL) {
  839. netlink_overrun(sk);
  840. /* Clone failed. Notify ALL listeners. */
  841. p->failure = 1;
  842. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  843. p->delivery_failure = 1;
  844. } else if (sk_filter(sk, p->skb2)) {
  845. kfree_skb(p->skb2);
  846. p->skb2 = NULL;
  847. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  848. netlink_overrun(sk);
  849. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  850. p->delivery_failure = 1;
  851. } else {
  852. p->congested |= val;
  853. p->delivered = 1;
  854. p->skb2 = NULL;
  855. }
  856. sock_put(sk);
  857. out:
  858. return 0;
  859. }
  860. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  861. u32 group, gfp_t allocation)
  862. {
  863. struct net *net = sock_net(ssk);
  864. struct netlink_broadcast_data info;
  865. struct hlist_node *node;
  866. struct sock *sk;
  867. skb = netlink_trim(skb, allocation);
  868. info.exclude_sk = ssk;
  869. info.net = net;
  870. info.pid = pid;
  871. info.group = group;
  872. info.failure = 0;
  873. info.delivery_failure = 0;
  874. info.congested = 0;
  875. info.delivered = 0;
  876. info.allocation = allocation;
  877. info.skb = skb;
  878. info.skb2 = NULL;
  879. /* While we sleep in clone, do not allow to change socket list */
  880. netlink_lock_table();
  881. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  882. do_one_broadcast(sk, &info);
  883. kfree_skb(skb);
  884. netlink_unlock_table();
  885. kfree_skb(info.skb2);
  886. if (info.delivery_failure)
  887. return -ENOBUFS;
  888. if (info.delivered) {
  889. if (info.congested && (allocation & __GFP_WAIT))
  890. yield();
  891. return 0;
  892. }
  893. return -ESRCH;
  894. }
  895. EXPORT_SYMBOL(netlink_broadcast);
  896. struct netlink_set_err_data {
  897. struct sock *exclude_sk;
  898. u32 pid;
  899. u32 group;
  900. int code;
  901. };
  902. static inline int do_one_set_err(struct sock *sk,
  903. struct netlink_set_err_data *p)
  904. {
  905. struct netlink_sock *nlk = nlk_sk(sk);
  906. if (sk == p->exclude_sk)
  907. goto out;
  908. if (sock_net(sk) != sock_net(p->exclude_sk))
  909. goto out;
  910. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  911. !test_bit(p->group - 1, nlk->groups))
  912. goto out;
  913. sk->sk_err = p->code;
  914. sk->sk_error_report(sk);
  915. out:
  916. return 0;
  917. }
  918. /**
  919. * netlink_set_err - report error to broadcast listeners
  920. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  921. * @pid: the PID of a process that we want to skip (if any)
  922. * @groups: the broadcast group that will notice the error
  923. * @code: error code, must be negative (as usual in kernelspace)
  924. */
  925. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  926. {
  927. struct netlink_set_err_data info;
  928. struct hlist_node *node;
  929. struct sock *sk;
  930. info.exclude_sk = ssk;
  931. info.pid = pid;
  932. info.group = group;
  933. /* sk->sk_err wants a positive error value */
  934. info.code = -code;
  935. read_lock(&nl_table_lock);
  936. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  937. do_one_set_err(sk, &info);
  938. read_unlock(&nl_table_lock);
  939. }
  940. EXPORT_SYMBOL(netlink_set_err);
  941. /* must be called with netlink table grabbed */
  942. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  943. unsigned int group,
  944. int is_new)
  945. {
  946. int old, new = !!is_new, subscriptions;
  947. old = test_bit(group - 1, nlk->groups);
  948. subscriptions = nlk->subscriptions - old + new;
  949. if (new)
  950. __set_bit(group - 1, nlk->groups);
  951. else
  952. __clear_bit(group - 1, nlk->groups);
  953. netlink_update_subscriptions(&nlk->sk, subscriptions);
  954. netlink_update_listeners(&nlk->sk);
  955. }
  956. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  957. char __user *optval, int optlen)
  958. {
  959. struct sock *sk = sock->sk;
  960. struct netlink_sock *nlk = nlk_sk(sk);
  961. unsigned int val = 0;
  962. int err;
  963. if (level != SOL_NETLINK)
  964. return -ENOPROTOOPT;
  965. if (optlen >= sizeof(int) &&
  966. get_user(val, (unsigned int __user *)optval))
  967. return -EFAULT;
  968. switch (optname) {
  969. case NETLINK_PKTINFO:
  970. if (val)
  971. nlk->flags |= NETLINK_RECV_PKTINFO;
  972. else
  973. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  974. err = 0;
  975. break;
  976. case NETLINK_ADD_MEMBERSHIP:
  977. case NETLINK_DROP_MEMBERSHIP: {
  978. if (!netlink_capable(sock, NL_NONROOT_RECV))
  979. return -EPERM;
  980. err = netlink_realloc_groups(sk);
  981. if (err)
  982. return err;
  983. if (!val || val - 1 >= nlk->ngroups)
  984. return -EINVAL;
  985. netlink_table_grab();
  986. netlink_update_socket_mc(nlk, val,
  987. optname == NETLINK_ADD_MEMBERSHIP);
  988. netlink_table_ungrab();
  989. err = 0;
  990. break;
  991. }
  992. case NETLINK_BROADCAST_ERROR:
  993. if (val)
  994. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  995. else
  996. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  997. err = 0;
  998. break;
  999. case NETLINK_NO_ENOBUFS:
  1000. if (val) {
  1001. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1002. clear_bit(0, &nlk->state);
  1003. wake_up_interruptible(&nlk->wait);
  1004. } else
  1005. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1006. err = 0;
  1007. break;
  1008. default:
  1009. err = -ENOPROTOOPT;
  1010. }
  1011. return err;
  1012. }
  1013. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1014. char __user *optval, int __user *optlen)
  1015. {
  1016. struct sock *sk = sock->sk;
  1017. struct netlink_sock *nlk = nlk_sk(sk);
  1018. int len, val, err;
  1019. if (level != SOL_NETLINK)
  1020. return -ENOPROTOOPT;
  1021. if (get_user(len, optlen))
  1022. return -EFAULT;
  1023. if (len < 0)
  1024. return -EINVAL;
  1025. switch (optname) {
  1026. case NETLINK_PKTINFO:
  1027. if (len < sizeof(int))
  1028. return -EINVAL;
  1029. len = sizeof(int);
  1030. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1031. if (put_user(len, optlen) ||
  1032. put_user(val, optval))
  1033. return -EFAULT;
  1034. err = 0;
  1035. break;
  1036. case NETLINK_BROADCAST_ERROR:
  1037. if (len < sizeof(int))
  1038. return -EINVAL;
  1039. len = sizeof(int);
  1040. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1041. if (put_user(len, optlen) ||
  1042. put_user(val, optval))
  1043. return -EFAULT;
  1044. err = 0;
  1045. break;
  1046. case NETLINK_NO_ENOBUFS:
  1047. if (len < sizeof(int))
  1048. return -EINVAL;
  1049. len = sizeof(int);
  1050. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1051. if (put_user(len, optlen) ||
  1052. put_user(val, optval))
  1053. return -EFAULT;
  1054. err = 0;
  1055. break;
  1056. default:
  1057. err = -ENOPROTOOPT;
  1058. }
  1059. return err;
  1060. }
  1061. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1062. {
  1063. struct nl_pktinfo info;
  1064. info.group = NETLINK_CB(skb).dst_group;
  1065. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1066. }
  1067. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1068. struct msghdr *msg, size_t len)
  1069. {
  1070. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1071. struct sock *sk = sock->sk;
  1072. struct netlink_sock *nlk = nlk_sk(sk);
  1073. struct sockaddr_nl *addr = msg->msg_name;
  1074. u32 dst_pid;
  1075. u32 dst_group;
  1076. struct sk_buff *skb;
  1077. int err;
  1078. struct scm_cookie scm;
  1079. if (msg->msg_flags&MSG_OOB)
  1080. return -EOPNOTSUPP;
  1081. if (NULL == siocb->scm)
  1082. siocb->scm = &scm;
  1083. err = scm_send(sock, msg, siocb->scm);
  1084. if (err < 0)
  1085. return err;
  1086. if (msg->msg_namelen) {
  1087. if (addr->nl_family != AF_NETLINK)
  1088. return -EINVAL;
  1089. dst_pid = addr->nl_pid;
  1090. dst_group = ffs(addr->nl_groups);
  1091. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  1092. return -EPERM;
  1093. } else {
  1094. dst_pid = nlk->dst_pid;
  1095. dst_group = nlk->dst_group;
  1096. }
  1097. if (!nlk->pid) {
  1098. err = netlink_autobind(sock);
  1099. if (err)
  1100. goto out;
  1101. }
  1102. err = -EMSGSIZE;
  1103. if (len > sk->sk_sndbuf - 32)
  1104. goto out;
  1105. err = -ENOBUFS;
  1106. skb = alloc_skb(len, GFP_KERNEL);
  1107. if (skb == NULL)
  1108. goto out;
  1109. NETLINK_CB(skb).pid = nlk->pid;
  1110. NETLINK_CB(skb).dst_group = dst_group;
  1111. NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
  1112. NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
  1113. security_task_getsecid(current, &(NETLINK_CB(skb).sid));
  1114. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1115. /* What can I do? Netlink is asynchronous, so that
  1116. we will have to save current capabilities to
  1117. check them, when this message will be delivered
  1118. to corresponding kernel module. --ANK (980802)
  1119. */
  1120. err = -EFAULT;
  1121. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1122. kfree_skb(skb);
  1123. goto out;
  1124. }
  1125. err = security_netlink_send(sk, skb);
  1126. if (err) {
  1127. kfree_skb(skb);
  1128. goto out;
  1129. }
  1130. if (dst_group) {
  1131. atomic_inc(&skb->users);
  1132. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  1133. }
  1134. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  1135. out:
  1136. return err;
  1137. }
  1138. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1139. struct msghdr *msg, size_t len,
  1140. int flags)
  1141. {
  1142. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1143. struct scm_cookie scm;
  1144. struct sock *sk = sock->sk;
  1145. struct netlink_sock *nlk = nlk_sk(sk);
  1146. int noblock = flags&MSG_DONTWAIT;
  1147. size_t copied;
  1148. struct sk_buff *skb, *frag __maybe_unused = NULL;
  1149. int err;
  1150. if (flags&MSG_OOB)
  1151. return -EOPNOTSUPP;
  1152. copied = 0;
  1153. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1154. if (skb == NULL)
  1155. goto out;
  1156. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1157. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1158. bool need_compat = !!(flags & MSG_CMSG_COMPAT);
  1159. /*
  1160. * If this skb has a frag_list, then here that means that
  1161. * we will have to use the frag_list skb for compat tasks
  1162. * and the regular skb for non-compat tasks.
  1163. *
  1164. * The skb might (and likely will) be cloned, so we can't
  1165. * just reset frag_list and go on with things -- we need to
  1166. * keep that. For the compat case that's easy -- simply get
  1167. * a reference to the compat skb and free the regular one
  1168. * including the frag. For the non-compat case, we need to
  1169. * avoid sending the frag to the user -- so assign NULL but
  1170. * restore it below before freeing the skb.
  1171. */
  1172. if (need_compat) {
  1173. struct sk_buff *compskb = skb_shinfo(skb)->frag_list;
  1174. skb_get(compskb);
  1175. kfree_skb(skb);
  1176. skb = compskb;
  1177. } else {
  1178. frag = skb_shinfo(skb)->frag_list;
  1179. skb_shinfo(skb)->frag_list = NULL;
  1180. }
  1181. }
  1182. #endif
  1183. msg->msg_namelen = 0;
  1184. copied = skb->len;
  1185. if (len < copied) {
  1186. msg->msg_flags |= MSG_TRUNC;
  1187. copied = len;
  1188. }
  1189. skb_reset_transport_header(skb);
  1190. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1191. if (msg->msg_name) {
  1192. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1193. addr->nl_family = AF_NETLINK;
  1194. addr->nl_pad = 0;
  1195. addr->nl_pid = NETLINK_CB(skb).pid;
  1196. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1197. msg->msg_namelen = sizeof(*addr);
  1198. }
  1199. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1200. netlink_cmsg_recv_pktinfo(msg, skb);
  1201. if (NULL == siocb->scm) {
  1202. memset(&scm, 0, sizeof(scm));
  1203. siocb->scm = &scm;
  1204. }
  1205. siocb->scm->creds = *NETLINK_CREDS(skb);
  1206. if (flags & MSG_TRUNC)
  1207. copied = skb->len;
  1208. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1209. skb_shinfo(skb)->frag_list = frag;
  1210. #endif
  1211. skb_free_datagram(sk, skb);
  1212. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  1213. netlink_dump(sk);
  1214. scm_recv(sock, msg, siocb->scm, flags);
  1215. out:
  1216. netlink_rcv_wake(sk);
  1217. return err ? : copied;
  1218. }
  1219. static void netlink_data_ready(struct sock *sk, int len)
  1220. {
  1221. BUG();
  1222. }
  1223. /*
  1224. * We export these functions to other modules. They provide a
  1225. * complete set of kernel non-blocking support for message
  1226. * queueing.
  1227. */
  1228. struct sock *
  1229. netlink_kernel_create(struct net *net, int unit, unsigned int groups,
  1230. void (*input)(struct sk_buff *skb),
  1231. struct mutex *cb_mutex, struct module *module)
  1232. {
  1233. struct socket *sock;
  1234. struct sock *sk;
  1235. struct netlink_sock *nlk;
  1236. unsigned long *listeners = NULL;
  1237. BUG_ON(!nl_table);
  1238. if (unit < 0 || unit >= MAX_LINKS)
  1239. return NULL;
  1240. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1241. return NULL;
  1242. /*
  1243. * We have to just have a reference on the net from sk, but don't
  1244. * get_net it. Besides, we cannot get and then put the net here.
  1245. * So we create one inside init_net and the move it to net.
  1246. */
  1247. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1248. goto out_sock_release_nosk;
  1249. sk = sock->sk;
  1250. sk_change_net(sk, net);
  1251. if (groups < 32)
  1252. groups = 32;
  1253. listeners = kzalloc(NLGRPSZ(groups) + sizeof(struct listeners_rcu_head),
  1254. GFP_KERNEL);
  1255. if (!listeners)
  1256. goto out_sock_release;
  1257. sk->sk_data_ready = netlink_data_ready;
  1258. if (input)
  1259. nlk_sk(sk)->netlink_rcv = input;
  1260. if (netlink_insert(sk, net, 0))
  1261. goto out_sock_release;
  1262. nlk = nlk_sk(sk);
  1263. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1264. netlink_table_grab();
  1265. if (!nl_table[unit].registered) {
  1266. nl_table[unit].groups = groups;
  1267. nl_table[unit].listeners = listeners;
  1268. nl_table[unit].cb_mutex = cb_mutex;
  1269. nl_table[unit].module = module;
  1270. nl_table[unit].registered = 1;
  1271. } else {
  1272. kfree(listeners);
  1273. nl_table[unit].registered++;
  1274. }
  1275. netlink_table_ungrab();
  1276. return sk;
  1277. out_sock_release:
  1278. kfree(listeners);
  1279. netlink_kernel_release(sk);
  1280. return NULL;
  1281. out_sock_release_nosk:
  1282. sock_release(sock);
  1283. return NULL;
  1284. }
  1285. EXPORT_SYMBOL(netlink_kernel_create);
  1286. void
  1287. netlink_kernel_release(struct sock *sk)
  1288. {
  1289. sk_release_kernel(sk);
  1290. }
  1291. EXPORT_SYMBOL(netlink_kernel_release);
  1292. static void netlink_free_old_listeners(struct rcu_head *rcu_head)
  1293. {
  1294. struct listeners_rcu_head *lrh;
  1295. lrh = container_of(rcu_head, struct listeners_rcu_head, rcu_head);
  1296. kfree(lrh->ptr);
  1297. }
  1298. /**
  1299. * netlink_change_ngroups - change number of multicast groups
  1300. *
  1301. * This changes the number of multicast groups that are available
  1302. * on a certain netlink family. Note that it is not possible to
  1303. * change the number of groups to below 32. Also note that it does
  1304. * not implicitly call netlink_clear_multicast_users() when the
  1305. * number of groups is reduced.
  1306. *
  1307. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1308. * @groups: The new number of groups.
  1309. */
  1310. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1311. {
  1312. unsigned long *listeners, *old = NULL;
  1313. struct listeners_rcu_head *old_rcu_head;
  1314. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1315. int err = 0;
  1316. if (groups < 32)
  1317. groups = 32;
  1318. netlink_table_grab();
  1319. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1320. listeners = kzalloc(NLGRPSZ(groups) +
  1321. sizeof(struct listeners_rcu_head),
  1322. GFP_ATOMIC);
  1323. if (!listeners) {
  1324. err = -ENOMEM;
  1325. goto out_ungrab;
  1326. }
  1327. old = tbl->listeners;
  1328. memcpy(listeners, old, NLGRPSZ(tbl->groups));
  1329. rcu_assign_pointer(tbl->listeners, listeners);
  1330. /*
  1331. * Free the old memory after an RCU grace period so we
  1332. * don't leak it. We use call_rcu() here in order to be
  1333. * able to call this function from atomic contexts. The
  1334. * allocation of this memory will have reserved enough
  1335. * space for struct listeners_rcu_head at the end.
  1336. */
  1337. old_rcu_head = (void *)(tbl->listeners +
  1338. NLGRPLONGS(tbl->groups));
  1339. old_rcu_head->ptr = old;
  1340. call_rcu(&old_rcu_head->rcu_head, netlink_free_old_listeners);
  1341. }
  1342. tbl->groups = groups;
  1343. out_ungrab:
  1344. netlink_table_ungrab();
  1345. return err;
  1346. }
  1347. /**
  1348. * netlink_clear_multicast_users - kick off multicast listeners
  1349. *
  1350. * This function removes all listeners from the given group.
  1351. * @ksk: The kernel netlink socket, as returned by
  1352. * netlink_kernel_create().
  1353. * @group: The multicast group to clear.
  1354. */
  1355. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1356. {
  1357. struct sock *sk;
  1358. struct hlist_node *node;
  1359. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1360. netlink_table_grab();
  1361. sk_for_each_bound(sk, node, &tbl->mc_list)
  1362. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1363. netlink_table_ungrab();
  1364. }
  1365. void netlink_set_nonroot(int protocol, unsigned int flags)
  1366. {
  1367. if ((unsigned int)protocol < MAX_LINKS)
  1368. nl_table[protocol].nl_nonroot = flags;
  1369. }
  1370. EXPORT_SYMBOL(netlink_set_nonroot);
  1371. static void netlink_destroy_callback(struct netlink_callback *cb)
  1372. {
  1373. kfree_skb(cb->skb);
  1374. kfree(cb);
  1375. }
  1376. /*
  1377. * It looks a bit ugly.
  1378. * It would be better to create kernel thread.
  1379. */
  1380. static int netlink_dump(struct sock *sk)
  1381. {
  1382. struct netlink_sock *nlk = nlk_sk(sk);
  1383. struct netlink_callback *cb;
  1384. struct sk_buff *skb;
  1385. struct nlmsghdr *nlh;
  1386. int len, err = -ENOBUFS;
  1387. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1388. if (!skb)
  1389. goto errout;
  1390. mutex_lock(nlk->cb_mutex);
  1391. cb = nlk->cb;
  1392. if (cb == NULL) {
  1393. err = -EINVAL;
  1394. goto errout_skb;
  1395. }
  1396. len = cb->dump(skb, cb);
  1397. if (len > 0) {
  1398. mutex_unlock(nlk->cb_mutex);
  1399. if (sk_filter(sk, skb))
  1400. kfree_skb(skb);
  1401. else {
  1402. skb_queue_tail(&sk->sk_receive_queue, skb);
  1403. sk->sk_data_ready(sk, skb->len);
  1404. }
  1405. return 0;
  1406. }
  1407. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1408. if (!nlh)
  1409. goto errout_skb;
  1410. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1411. if (sk_filter(sk, skb))
  1412. kfree_skb(skb);
  1413. else {
  1414. skb_queue_tail(&sk->sk_receive_queue, skb);
  1415. sk->sk_data_ready(sk, skb->len);
  1416. }
  1417. if (cb->done)
  1418. cb->done(cb);
  1419. nlk->cb = NULL;
  1420. mutex_unlock(nlk->cb_mutex);
  1421. netlink_destroy_callback(cb);
  1422. return 0;
  1423. errout_skb:
  1424. mutex_unlock(nlk->cb_mutex);
  1425. kfree_skb(skb);
  1426. errout:
  1427. return err;
  1428. }
  1429. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1430. struct nlmsghdr *nlh,
  1431. int (*dump)(struct sk_buff *skb,
  1432. struct netlink_callback *),
  1433. int (*done)(struct netlink_callback *))
  1434. {
  1435. struct netlink_callback *cb;
  1436. struct sock *sk;
  1437. struct netlink_sock *nlk;
  1438. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1439. if (cb == NULL)
  1440. return -ENOBUFS;
  1441. cb->dump = dump;
  1442. cb->done = done;
  1443. cb->nlh = nlh;
  1444. atomic_inc(&skb->users);
  1445. cb->skb = skb;
  1446. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
  1447. if (sk == NULL) {
  1448. netlink_destroy_callback(cb);
  1449. return -ECONNREFUSED;
  1450. }
  1451. nlk = nlk_sk(sk);
  1452. /* A dump is in progress... */
  1453. mutex_lock(nlk->cb_mutex);
  1454. if (nlk->cb) {
  1455. mutex_unlock(nlk->cb_mutex);
  1456. netlink_destroy_callback(cb);
  1457. sock_put(sk);
  1458. return -EBUSY;
  1459. }
  1460. nlk->cb = cb;
  1461. mutex_unlock(nlk->cb_mutex);
  1462. netlink_dump(sk);
  1463. sock_put(sk);
  1464. /* We successfully started a dump, by returning -EINTR we
  1465. * signal not to send ACK even if it was requested.
  1466. */
  1467. return -EINTR;
  1468. }
  1469. EXPORT_SYMBOL(netlink_dump_start);
  1470. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1471. {
  1472. struct sk_buff *skb;
  1473. struct nlmsghdr *rep;
  1474. struct nlmsgerr *errmsg;
  1475. size_t payload = sizeof(*errmsg);
  1476. /* error messages get the original request appened */
  1477. if (err)
  1478. payload += nlmsg_len(nlh);
  1479. skb = nlmsg_new(payload, GFP_KERNEL);
  1480. if (!skb) {
  1481. struct sock *sk;
  1482. sk = netlink_lookup(sock_net(in_skb->sk),
  1483. in_skb->sk->sk_protocol,
  1484. NETLINK_CB(in_skb).pid);
  1485. if (sk) {
  1486. sk->sk_err = ENOBUFS;
  1487. sk->sk_error_report(sk);
  1488. sock_put(sk);
  1489. }
  1490. return;
  1491. }
  1492. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1493. NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
  1494. errmsg = nlmsg_data(rep);
  1495. errmsg->error = err;
  1496. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1497. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1498. }
  1499. EXPORT_SYMBOL(netlink_ack);
  1500. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1501. struct nlmsghdr *))
  1502. {
  1503. struct nlmsghdr *nlh;
  1504. int err;
  1505. while (skb->len >= nlmsg_total_size(0)) {
  1506. int msglen;
  1507. nlh = nlmsg_hdr(skb);
  1508. err = 0;
  1509. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1510. return 0;
  1511. /* Only requests are handled by the kernel */
  1512. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1513. goto ack;
  1514. /* Skip control messages */
  1515. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1516. goto ack;
  1517. err = cb(skb, nlh);
  1518. if (err == -EINTR)
  1519. goto skip;
  1520. ack:
  1521. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1522. netlink_ack(skb, nlh, err);
  1523. skip:
  1524. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1525. if (msglen > skb->len)
  1526. msglen = skb->len;
  1527. skb_pull(skb, msglen);
  1528. }
  1529. return 0;
  1530. }
  1531. EXPORT_SYMBOL(netlink_rcv_skb);
  1532. /**
  1533. * nlmsg_notify - send a notification netlink message
  1534. * @sk: netlink socket to use
  1535. * @skb: notification message
  1536. * @pid: destination netlink pid for reports or 0
  1537. * @group: destination multicast group or 0
  1538. * @report: 1 to report back, 0 to disable
  1539. * @flags: allocation flags
  1540. */
  1541. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
  1542. unsigned int group, int report, gfp_t flags)
  1543. {
  1544. int err = 0;
  1545. if (group) {
  1546. int exclude_pid = 0;
  1547. if (report) {
  1548. atomic_inc(&skb->users);
  1549. exclude_pid = pid;
  1550. }
  1551. /* errors reported via destination sk->sk_err, but propagate
  1552. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1553. err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
  1554. }
  1555. if (report) {
  1556. int err2;
  1557. err2 = nlmsg_unicast(sk, skb, pid);
  1558. if (!err || err == -ESRCH)
  1559. err = err2;
  1560. }
  1561. return err;
  1562. }
  1563. EXPORT_SYMBOL(nlmsg_notify);
  1564. #ifdef CONFIG_PROC_FS
  1565. struct nl_seq_iter {
  1566. struct seq_net_private p;
  1567. int link;
  1568. int hash_idx;
  1569. };
  1570. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1571. {
  1572. struct nl_seq_iter *iter = seq->private;
  1573. int i, j;
  1574. struct sock *s;
  1575. struct hlist_node *node;
  1576. loff_t off = 0;
  1577. for (i = 0; i < MAX_LINKS; i++) {
  1578. struct nl_pid_hash *hash = &nl_table[i].hash;
  1579. for (j = 0; j <= hash->mask; j++) {
  1580. sk_for_each(s, node, &hash->table[j]) {
  1581. if (sock_net(s) != seq_file_net(seq))
  1582. continue;
  1583. if (off == pos) {
  1584. iter->link = i;
  1585. iter->hash_idx = j;
  1586. return s;
  1587. }
  1588. ++off;
  1589. }
  1590. }
  1591. }
  1592. return NULL;
  1593. }
  1594. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1595. __acquires(nl_table_lock)
  1596. {
  1597. read_lock(&nl_table_lock);
  1598. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1599. }
  1600. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1601. {
  1602. struct sock *s;
  1603. struct nl_seq_iter *iter;
  1604. int i, j;
  1605. ++*pos;
  1606. if (v == SEQ_START_TOKEN)
  1607. return netlink_seq_socket_idx(seq, 0);
  1608. iter = seq->private;
  1609. s = v;
  1610. do {
  1611. s = sk_next(s);
  1612. } while (s && sock_net(s) != seq_file_net(seq));
  1613. if (s)
  1614. return s;
  1615. i = iter->link;
  1616. j = iter->hash_idx + 1;
  1617. do {
  1618. struct nl_pid_hash *hash = &nl_table[i].hash;
  1619. for (; j <= hash->mask; j++) {
  1620. s = sk_head(&hash->table[j]);
  1621. while (s && sock_net(s) != seq_file_net(seq))
  1622. s = sk_next(s);
  1623. if (s) {
  1624. iter->link = i;
  1625. iter->hash_idx = j;
  1626. return s;
  1627. }
  1628. }
  1629. j = 0;
  1630. } while (++i < MAX_LINKS);
  1631. return NULL;
  1632. }
  1633. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1634. __releases(nl_table_lock)
  1635. {
  1636. read_unlock(&nl_table_lock);
  1637. }
  1638. static int netlink_seq_show(struct seq_file *seq, void *v)
  1639. {
  1640. if (v == SEQ_START_TOKEN)
  1641. seq_puts(seq,
  1642. "sk Eth Pid Groups "
  1643. "Rmem Wmem Dump Locks Drops\n");
  1644. else {
  1645. struct sock *s = v;
  1646. struct netlink_sock *nlk = nlk_sk(s);
  1647. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %-8d %-8d\n",
  1648. s,
  1649. s->sk_protocol,
  1650. nlk->pid,
  1651. nlk->groups ? (u32)nlk->groups[0] : 0,
  1652. sk_rmem_alloc_get(s),
  1653. sk_wmem_alloc_get(s),
  1654. nlk->cb,
  1655. atomic_read(&s->sk_refcnt),
  1656. atomic_read(&s->sk_drops)
  1657. );
  1658. }
  1659. return 0;
  1660. }
  1661. static const struct seq_operations netlink_seq_ops = {
  1662. .start = netlink_seq_start,
  1663. .next = netlink_seq_next,
  1664. .stop = netlink_seq_stop,
  1665. .show = netlink_seq_show,
  1666. };
  1667. static int netlink_seq_open(struct inode *inode, struct file *file)
  1668. {
  1669. return seq_open_net(inode, file, &netlink_seq_ops,
  1670. sizeof(struct nl_seq_iter));
  1671. }
  1672. static const struct file_operations netlink_seq_fops = {
  1673. .owner = THIS_MODULE,
  1674. .open = netlink_seq_open,
  1675. .read = seq_read,
  1676. .llseek = seq_lseek,
  1677. .release = seq_release_net,
  1678. };
  1679. #endif
  1680. int netlink_register_notifier(struct notifier_block *nb)
  1681. {
  1682. return atomic_notifier_chain_register(&netlink_chain, nb);
  1683. }
  1684. EXPORT_SYMBOL(netlink_register_notifier);
  1685. int netlink_unregister_notifier(struct notifier_block *nb)
  1686. {
  1687. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1688. }
  1689. EXPORT_SYMBOL(netlink_unregister_notifier);
  1690. static const struct proto_ops netlink_ops = {
  1691. .family = PF_NETLINK,
  1692. .owner = THIS_MODULE,
  1693. .release = netlink_release,
  1694. .bind = netlink_bind,
  1695. .connect = netlink_connect,
  1696. .socketpair = sock_no_socketpair,
  1697. .accept = sock_no_accept,
  1698. .getname = netlink_getname,
  1699. .poll = datagram_poll,
  1700. .ioctl = sock_no_ioctl,
  1701. .listen = sock_no_listen,
  1702. .shutdown = sock_no_shutdown,
  1703. .setsockopt = netlink_setsockopt,
  1704. .getsockopt = netlink_getsockopt,
  1705. .sendmsg = netlink_sendmsg,
  1706. .recvmsg = netlink_recvmsg,
  1707. .mmap = sock_no_mmap,
  1708. .sendpage = sock_no_sendpage,
  1709. };
  1710. static struct net_proto_family netlink_family_ops = {
  1711. .family = PF_NETLINK,
  1712. .create = netlink_create,
  1713. .owner = THIS_MODULE, /* for consistency 8) */
  1714. };
  1715. static int __net_init netlink_net_init(struct net *net)
  1716. {
  1717. #ifdef CONFIG_PROC_FS
  1718. if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
  1719. return -ENOMEM;
  1720. #endif
  1721. return 0;
  1722. }
  1723. static void __net_exit netlink_net_exit(struct net *net)
  1724. {
  1725. #ifdef CONFIG_PROC_FS
  1726. proc_net_remove(net, "netlink");
  1727. #endif
  1728. }
  1729. static struct pernet_operations __net_initdata netlink_net_ops = {
  1730. .init = netlink_net_init,
  1731. .exit = netlink_net_exit,
  1732. };
  1733. static int __init netlink_proto_init(void)
  1734. {
  1735. struct sk_buff *dummy_skb;
  1736. int i;
  1737. unsigned long limit;
  1738. unsigned int order;
  1739. int err = proto_register(&netlink_proto, 0);
  1740. if (err != 0)
  1741. goto out;
  1742. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
  1743. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1744. if (!nl_table)
  1745. goto panic;
  1746. if (num_physpages >= (128 * 1024))
  1747. limit = num_physpages >> (21 - PAGE_SHIFT);
  1748. else
  1749. limit = num_physpages >> (23 - PAGE_SHIFT);
  1750. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1751. limit = (1UL << order) / sizeof(struct hlist_head);
  1752. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1753. for (i = 0; i < MAX_LINKS; i++) {
  1754. struct nl_pid_hash *hash = &nl_table[i].hash;
  1755. hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
  1756. if (!hash->table) {
  1757. while (i-- > 0)
  1758. nl_pid_hash_free(nl_table[i].hash.table,
  1759. 1 * sizeof(*hash->table));
  1760. kfree(nl_table);
  1761. goto panic;
  1762. }
  1763. hash->max_shift = order;
  1764. hash->shift = 0;
  1765. hash->mask = 0;
  1766. hash->rehash_time = jiffies;
  1767. }
  1768. sock_register(&netlink_family_ops);
  1769. register_pernet_subsys(&netlink_net_ops);
  1770. /* The netlink device handler may be needed early. */
  1771. rtnetlink_init();
  1772. out:
  1773. return err;
  1774. panic:
  1775. panic("netlink_init: Cannot allocate nl_table\n");
  1776. }
  1777. core_initcall(netlink_proto_init);