af_netlink.c 41 KB

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