af_netlink.c 44 KB

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