af_netlink.c 40 KB

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