af_netlink.c 41 KB

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