af_netlink.c 40 KB

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