af_netlink.c 43 KB

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