af_netlink.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. /*
  2. * NETLINK Kernel-user communication protocol.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  13. * added netlink_proto_exit
  14. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  15. * use nlk_sk, as sk->protinfo is on a diet 8)
  16. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  17. * - inc module use count of module that owns
  18. * the kernel socket in case userspace opens
  19. * socket of same protocol
  20. * - remove all module support, since netlink is
  21. * mandatory if CONFIG_NET=y these days
  22. */
  23. #include <linux/module.h>
  24. #include <linux/capability.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/signal.h>
  28. #include <linux/sched.h>
  29. #include <linux/errno.h>
  30. #include <linux/string.h>
  31. #include <linux/stat.h>
  32. #include <linux/socket.h>
  33. #include <linux/un.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/termios.h>
  36. #include <linux/sockios.h>
  37. #include <linux/net.h>
  38. #include <linux/fs.h>
  39. #include <linux/slab.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/rtnetlink.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/notifier.h>
  47. #include <linux/security.h>
  48. #include <linux/jhash.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/random.h>
  51. #include <linux/bitops.h>
  52. #include <linux/mm.h>
  53. #include <linux/types.h>
  54. #include <linux/audit.h>
  55. #include <linux/mutex.h>
  56. #include <net/net_namespace.h>
  57. #include <net/sock.h>
  58. #include <net/scm.h>
  59. #include <net/netlink.h>
  60. #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
  61. #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
  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 (*netlink_rcv)(struct sk_buff *skb);
  78. struct module *module;
  79. };
  80. struct listeners {
  81. struct rcu_head rcu;
  82. unsigned long masks[0];
  83. };
  84. #define NETLINK_KERNEL_SOCKET 0x1
  85. #define NETLINK_RECV_PKTINFO 0x2
  86. #define NETLINK_BROADCAST_SEND_ERROR 0x4
  87. #define NETLINK_RECV_NO_ENOBUFS 0x8
  88. static inline struct netlink_sock *nlk_sk(struct sock *sk)
  89. {
  90. return container_of(sk, struct netlink_sock, sk);
  91. }
  92. static inline int netlink_is_kernel(struct sock *sk)
  93. {
  94. return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
  95. }
  96. struct nl_pid_hash {
  97. struct hlist_head *table;
  98. unsigned long rehash_time;
  99. unsigned int mask;
  100. unsigned int shift;
  101. unsigned int entries;
  102. unsigned int max_shift;
  103. u32 rnd;
  104. };
  105. struct netlink_table {
  106. struct nl_pid_hash hash;
  107. struct hlist_head mc_list;
  108. struct listeners __rcu *listeners;
  109. unsigned int nl_nonroot;
  110. unsigned int groups;
  111. struct mutex *cb_mutex;
  112. struct module *module;
  113. int registered;
  114. };
  115. static struct netlink_table *nl_table;
  116. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  117. static int netlink_dump(struct sock *sk);
  118. static void netlink_destroy_callback(struct netlink_callback *cb);
  119. static DEFINE_RWLOCK(nl_table_lock);
  120. static atomic_t nl_table_users = ATOMIC_INIT(0);
  121. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  122. static u32 netlink_group_mask(u32 group)
  123. {
  124. return group ? 1 << (group - 1) : 0;
  125. }
  126. static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
  127. {
  128. return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
  129. }
  130. static void netlink_sock_destruct(struct sock *sk)
  131. {
  132. struct netlink_sock *nlk = nlk_sk(sk);
  133. if (nlk->cb) {
  134. if (nlk->cb->done)
  135. nlk->cb->done(nlk->cb);
  136. netlink_destroy_callback(nlk->cb);
  137. }
  138. skb_queue_purge(&sk->sk_receive_queue);
  139. if (!sock_flag(sk, SOCK_DEAD)) {
  140. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  141. return;
  142. }
  143. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  144. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  145. WARN_ON(nlk_sk(sk)->groups);
  146. }
  147. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  148. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  149. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  150. * this, _but_ remember, it adds useless work on UP machines.
  151. */
  152. void netlink_table_grab(void)
  153. __acquires(nl_table_lock)
  154. {
  155. might_sleep();
  156. write_lock_irq(&nl_table_lock);
  157. if (atomic_read(&nl_table_users)) {
  158. DECLARE_WAITQUEUE(wait, current);
  159. add_wait_queue_exclusive(&nl_table_wait, &wait);
  160. for (;;) {
  161. set_current_state(TASK_UNINTERRUPTIBLE);
  162. if (atomic_read(&nl_table_users) == 0)
  163. break;
  164. write_unlock_irq(&nl_table_lock);
  165. schedule();
  166. write_lock_irq(&nl_table_lock);
  167. }
  168. __set_current_state(TASK_RUNNING);
  169. remove_wait_queue(&nl_table_wait, &wait);
  170. }
  171. }
  172. void netlink_table_ungrab(void)
  173. __releases(nl_table_lock)
  174. {
  175. write_unlock_irq(&nl_table_lock);
  176. wake_up(&nl_table_wait);
  177. }
  178. static inline void
  179. netlink_lock_table(void)
  180. {
  181. /* read_lock() synchronizes us to netlink_table_grab */
  182. read_lock(&nl_table_lock);
  183. atomic_inc(&nl_table_users);
  184. read_unlock(&nl_table_lock);
  185. }
  186. static inline void
  187. netlink_unlock_table(void)
  188. {
  189. if (atomic_dec_and_test(&nl_table_users))
  190. wake_up(&nl_table_wait);
  191. }
  192. static inline struct sock *netlink_lookup(struct net *net, int protocol,
  193. u32 pid)
  194. {
  195. struct nl_pid_hash *hash = &nl_table[protocol].hash;
  196. struct hlist_head *head;
  197. struct sock *sk;
  198. struct hlist_node *node;
  199. read_lock(&nl_table_lock);
  200. head = nl_pid_hashfn(hash, pid);
  201. sk_for_each(sk, node, head) {
  202. if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->pid == pid)) {
  203. sock_hold(sk);
  204. goto found;
  205. }
  206. }
  207. sk = NULL;
  208. found:
  209. read_unlock(&nl_table_lock);
  210. return sk;
  211. }
  212. static inline struct hlist_head *nl_pid_hash_zalloc(size_t size)
  213. {
  214. if (size <= PAGE_SIZE)
  215. return kzalloc(size, GFP_ATOMIC);
  216. else
  217. return (struct hlist_head *)
  218. __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
  219. get_order(size));
  220. }
  221. static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
  222. {
  223. if (size <= PAGE_SIZE)
  224. kfree(table);
  225. else
  226. free_pages((unsigned long)table, get_order(size));
  227. }
  228. static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
  229. {
  230. unsigned int omask, mask, shift;
  231. size_t osize, size;
  232. struct hlist_head *otable, *table;
  233. int i;
  234. omask = mask = hash->mask;
  235. osize = size = (mask + 1) * sizeof(*table);
  236. shift = hash->shift;
  237. if (grow) {
  238. if (++shift > hash->max_shift)
  239. return 0;
  240. mask = mask * 2 + 1;
  241. size *= 2;
  242. }
  243. table = nl_pid_hash_zalloc(size);
  244. if (!table)
  245. return 0;
  246. otable = hash->table;
  247. hash->table = table;
  248. hash->mask = mask;
  249. hash->shift = shift;
  250. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  251. for (i = 0; i <= omask; i++) {
  252. struct sock *sk;
  253. struct hlist_node *node, *tmp;
  254. sk_for_each_safe(sk, node, tmp, &otable[i])
  255. __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
  256. }
  257. nl_pid_hash_free(otable, osize);
  258. hash->rehash_time = jiffies + 10 * 60 * HZ;
  259. return 1;
  260. }
  261. static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
  262. {
  263. int avg = hash->entries >> hash->shift;
  264. if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
  265. return 1;
  266. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  267. nl_pid_hash_rehash(hash, 0);
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. static const struct proto_ops netlink_ops;
  273. static void
  274. netlink_update_listeners(struct sock *sk)
  275. {
  276. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  277. struct hlist_node *node;
  278. unsigned long mask;
  279. unsigned int i;
  280. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  281. mask = 0;
  282. sk_for_each_bound(sk, node, &tbl->mc_list) {
  283. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  284. mask |= nlk_sk(sk)->groups[i];
  285. }
  286. tbl->listeners->masks[i] = mask;
  287. }
  288. /* this function is only called with the netlink table "grabbed", which
  289. * makes sure updates are visible before bind or setsockopt return. */
  290. }
  291. static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
  292. {
  293. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  294. struct hlist_head *head;
  295. int err = -EADDRINUSE;
  296. struct sock *osk;
  297. struct hlist_node *node;
  298. int len;
  299. netlink_table_grab();
  300. head = nl_pid_hashfn(hash, pid);
  301. len = 0;
  302. sk_for_each(osk, node, head) {
  303. if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->pid == pid))
  304. break;
  305. len++;
  306. }
  307. if (node)
  308. goto err;
  309. err = -EBUSY;
  310. if (nlk_sk(sk)->pid)
  311. goto err;
  312. err = -ENOMEM;
  313. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  314. goto err;
  315. if (len && nl_pid_hash_dilute(hash, len))
  316. head = nl_pid_hashfn(hash, pid);
  317. hash->entries++;
  318. nlk_sk(sk)->pid = pid;
  319. sk_add_node(sk, head);
  320. err = 0;
  321. err:
  322. netlink_table_ungrab();
  323. return err;
  324. }
  325. static void netlink_remove(struct sock *sk)
  326. {
  327. netlink_table_grab();
  328. if (sk_del_node_init(sk))
  329. nl_table[sk->sk_protocol].hash.entries--;
  330. if (nlk_sk(sk)->subscriptions)
  331. __sk_del_bind_node(sk);
  332. netlink_table_ungrab();
  333. }
  334. static struct proto netlink_proto = {
  335. .name = "NETLINK",
  336. .owner = THIS_MODULE,
  337. .obj_size = sizeof(struct netlink_sock),
  338. };
  339. static int __netlink_create(struct net *net, struct socket *sock,
  340. struct mutex *cb_mutex, int protocol)
  341. {
  342. struct sock *sk;
  343. struct netlink_sock *nlk;
  344. sock->ops = &netlink_ops;
  345. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
  346. if (!sk)
  347. return -ENOMEM;
  348. sock_init_data(sock, sk);
  349. nlk = nlk_sk(sk);
  350. if (cb_mutex)
  351. nlk->cb_mutex = cb_mutex;
  352. else {
  353. nlk->cb_mutex = &nlk->cb_def_mutex;
  354. mutex_init(nlk->cb_mutex);
  355. }
  356. init_waitqueue_head(&nlk->wait);
  357. sk->sk_destruct = netlink_sock_destruct;
  358. sk->sk_protocol = protocol;
  359. return 0;
  360. }
  361. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  362. int kern)
  363. {
  364. struct module *module = NULL;
  365. struct mutex *cb_mutex;
  366. struct netlink_sock *nlk;
  367. int err = 0;
  368. sock->state = SS_UNCONNECTED;
  369. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  370. return -ESOCKTNOSUPPORT;
  371. if (protocol < 0 || protocol >= MAX_LINKS)
  372. return -EPROTONOSUPPORT;
  373. netlink_lock_table();
  374. #ifdef CONFIG_MODULES
  375. if (!nl_table[protocol].registered) {
  376. netlink_unlock_table();
  377. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  378. netlink_lock_table();
  379. }
  380. #endif
  381. if (nl_table[protocol].registered &&
  382. try_module_get(nl_table[protocol].module))
  383. module = nl_table[protocol].module;
  384. else
  385. err = -EPROTONOSUPPORT;
  386. cb_mutex = nl_table[protocol].cb_mutex;
  387. netlink_unlock_table();
  388. if (err < 0)
  389. goto out;
  390. err = __netlink_create(net, sock, cb_mutex, protocol);
  391. if (err < 0)
  392. goto out_module;
  393. local_bh_disable();
  394. sock_prot_inuse_add(net, &netlink_proto, 1);
  395. local_bh_enable();
  396. nlk = nlk_sk(sock->sk);
  397. nlk->module = module;
  398. out:
  399. return err;
  400. out_module:
  401. module_put(module);
  402. goto out;
  403. }
  404. static int netlink_release(struct socket *sock)
  405. {
  406. struct sock *sk = sock->sk;
  407. struct netlink_sock *nlk;
  408. if (!sk)
  409. return 0;
  410. netlink_remove(sk);
  411. sock_orphan(sk);
  412. nlk = nlk_sk(sk);
  413. /*
  414. * OK. Socket is unlinked, any packets that arrive now
  415. * will be purged.
  416. */
  417. sock->sk = NULL;
  418. wake_up_interruptible_all(&nlk->wait);
  419. skb_queue_purge(&sk->sk_write_queue);
  420. if (nlk->pid) {
  421. struct netlink_notify n = {
  422. .net = sock_net(sk),
  423. .protocol = sk->sk_protocol,
  424. .pid = nlk->pid,
  425. };
  426. atomic_notifier_call_chain(&netlink_chain,
  427. NETLINK_URELEASE, &n);
  428. }
  429. module_put(nlk->module);
  430. netlink_table_grab();
  431. if (netlink_is_kernel(sk)) {
  432. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  433. if (--nl_table[sk->sk_protocol].registered == 0) {
  434. kfree(nl_table[sk->sk_protocol].listeners);
  435. nl_table[sk->sk_protocol].module = NULL;
  436. nl_table[sk->sk_protocol].registered = 0;
  437. }
  438. } else if (nlk->subscriptions)
  439. netlink_update_listeners(sk);
  440. netlink_table_ungrab();
  441. kfree(nlk->groups);
  442. nlk->groups = NULL;
  443. local_bh_disable();
  444. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  445. local_bh_enable();
  446. sock_put(sk);
  447. return 0;
  448. }
  449. static int netlink_autobind(struct socket *sock)
  450. {
  451. struct sock *sk = sock->sk;
  452. struct net *net = sock_net(sk);
  453. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  454. struct hlist_head *head;
  455. struct sock *osk;
  456. struct hlist_node *node;
  457. s32 pid = task_tgid_vnr(current);
  458. int err;
  459. static s32 rover = -4097;
  460. retry:
  461. cond_resched();
  462. netlink_table_grab();
  463. head = nl_pid_hashfn(hash, pid);
  464. sk_for_each(osk, node, head) {
  465. if (!net_eq(sock_net(osk), net))
  466. continue;
  467. if (nlk_sk(osk)->pid == pid) {
  468. /* Bind collision, search negative pid values. */
  469. pid = rover--;
  470. if (rover > -4097)
  471. rover = -4097;
  472. netlink_table_ungrab();
  473. goto retry;
  474. }
  475. }
  476. netlink_table_ungrab();
  477. err = netlink_insert(sk, net, pid);
  478. if (err == -EADDRINUSE)
  479. goto retry;
  480. /* If 2 threads race to autobind, that is fine. */
  481. if (err == -EBUSY)
  482. err = 0;
  483. return err;
  484. }
  485. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  486. {
  487. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  488. capable(CAP_NET_ADMIN);
  489. }
  490. static void
  491. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  492. {
  493. struct netlink_sock *nlk = nlk_sk(sk);
  494. if (nlk->subscriptions && !subscriptions)
  495. __sk_del_bind_node(sk);
  496. else if (!nlk->subscriptions && subscriptions)
  497. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  498. nlk->subscriptions = subscriptions;
  499. }
  500. static int netlink_realloc_groups(struct sock *sk)
  501. {
  502. struct netlink_sock *nlk = nlk_sk(sk);
  503. unsigned int groups;
  504. unsigned long *new_groups;
  505. int err = 0;
  506. netlink_table_grab();
  507. groups = nl_table[sk->sk_protocol].groups;
  508. if (!nl_table[sk->sk_protocol].registered) {
  509. err = -ENOENT;
  510. goto out_unlock;
  511. }
  512. if (nlk->ngroups >= groups)
  513. goto out_unlock;
  514. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  515. if (new_groups == NULL) {
  516. err = -ENOMEM;
  517. goto out_unlock;
  518. }
  519. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  520. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  521. nlk->groups = new_groups;
  522. nlk->ngroups = groups;
  523. out_unlock:
  524. netlink_table_ungrab();
  525. return err;
  526. }
  527. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  528. int addr_len)
  529. {
  530. struct sock *sk = sock->sk;
  531. struct net *net = sock_net(sk);
  532. struct netlink_sock *nlk = nlk_sk(sk);
  533. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  534. int err;
  535. if (nladdr->nl_family != AF_NETLINK)
  536. return -EINVAL;
  537. /* Only superuser is allowed to listen multicasts */
  538. if (nladdr->nl_groups) {
  539. if (!netlink_capable(sock, NL_NONROOT_RECV))
  540. return -EPERM;
  541. err = netlink_realloc_groups(sk);
  542. if (err)
  543. return err;
  544. }
  545. if (nlk->pid) {
  546. if (nladdr->nl_pid != nlk->pid)
  547. return -EINVAL;
  548. } else {
  549. err = nladdr->nl_pid ?
  550. netlink_insert(sk, net, nladdr->nl_pid) :
  551. netlink_autobind(sock);
  552. if (err)
  553. return err;
  554. }
  555. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  556. return 0;
  557. netlink_table_grab();
  558. netlink_update_subscriptions(sk, nlk->subscriptions +
  559. hweight32(nladdr->nl_groups) -
  560. hweight32(nlk->groups[0]));
  561. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  562. netlink_update_listeners(sk);
  563. netlink_table_ungrab();
  564. return 0;
  565. }
  566. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  567. int alen, int flags)
  568. {
  569. int err = 0;
  570. struct sock *sk = sock->sk;
  571. struct netlink_sock *nlk = nlk_sk(sk);
  572. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  573. if (alen < sizeof(addr->sa_family))
  574. return -EINVAL;
  575. if (addr->sa_family == AF_UNSPEC) {
  576. sk->sk_state = NETLINK_UNCONNECTED;
  577. nlk->dst_pid = 0;
  578. nlk->dst_group = 0;
  579. return 0;
  580. }
  581. if (addr->sa_family != AF_NETLINK)
  582. return -EINVAL;
  583. /* Only superuser is allowed to send multicasts */
  584. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  585. return -EPERM;
  586. if (!nlk->pid)
  587. err = netlink_autobind(sock);
  588. if (err == 0) {
  589. sk->sk_state = NETLINK_CONNECTED;
  590. nlk->dst_pid = nladdr->nl_pid;
  591. nlk->dst_group = ffs(nladdr->nl_groups);
  592. }
  593. return err;
  594. }
  595. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  596. int *addr_len, int peer)
  597. {
  598. struct sock *sk = sock->sk;
  599. struct netlink_sock *nlk = nlk_sk(sk);
  600. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  601. nladdr->nl_family = AF_NETLINK;
  602. nladdr->nl_pad = 0;
  603. *addr_len = sizeof(*nladdr);
  604. if (peer) {
  605. nladdr->nl_pid = nlk->dst_pid;
  606. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  607. } else {
  608. nladdr->nl_pid = nlk->pid;
  609. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  610. }
  611. return 0;
  612. }
  613. static void netlink_overrun(struct sock *sk)
  614. {
  615. struct netlink_sock *nlk = nlk_sk(sk);
  616. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  617. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  618. sk->sk_err = ENOBUFS;
  619. sk->sk_error_report(sk);
  620. }
  621. }
  622. atomic_inc(&sk->sk_drops);
  623. }
  624. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  625. {
  626. struct sock *sock;
  627. struct netlink_sock *nlk;
  628. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
  629. if (!sock)
  630. return ERR_PTR(-ECONNREFUSED);
  631. /* Don't bother queuing skb if kernel socket has no input function */
  632. nlk = nlk_sk(sock);
  633. if (sock->sk_state == NETLINK_CONNECTED &&
  634. nlk->dst_pid != nlk_sk(ssk)->pid) {
  635. sock_put(sock);
  636. return ERR_PTR(-ECONNREFUSED);
  637. }
  638. return sock;
  639. }
  640. struct sock *netlink_getsockbyfilp(struct file *filp)
  641. {
  642. struct inode *inode = filp->f_path.dentry->d_inode;
  643. struct sock *sock;
  644. if (!S_ISSOCK(inode->i_mode))
  645. return ERR_PTR(-ENOTSOCK);
  646. sock = SOCKET_I(inode)->sk;
  647. if (sock->sk_family != AF_NETLINK)
  648. return ERR_PTR(-EINVAL);
  649. sock_hold(sock);
  650. return sock;
  651. }
  652. /*
  653. * Attach a skb to a netlink socket.
  654. * The caller must hold a reference to the destination socket. On error, the
  655. * reference is dropped. The skb is not send to the destination, just all
  656. * all error checks are performed and memory in the queue is reserved.
  657. * Return values:
  658. * < 0: error. skb freed, reference to sock dropped.
  659. * 0: continue
  660. * 1: repeat lookup - reference dropped while waiting for socket memory.
  661. */
  662. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  663. long *timeo, struct sock *ssk)
  664. {
  665. struct netlink_sock *nlk;
  666. nlk = nlk_sk(sk);
  667. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  668. test_bit(0, &nlk->state)) {
  669. DECLARE_WAITQUEUE(wait, current);
  670. if (!*timeo) {
  671. if (!ssk || netlink_is_kernel(ssk))
  672. netlink_overrun(sk);
  673. sock_put(sk);
  674. kfree_skb(skb);
  675. return -EAGAIN;
  676. }
  677. __set_current_state(TASK_INTERRUPTIBLE);
  678. add_wait_queue(&nlk->wait, &wait);
  679. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  680. test_bit(0, &nlk->state)) &&
  681. !sock_flag(sk, SOCK_DEAD))
  682. *timeo = schedule_timeout(*timeo);
  683. __set_current_state(TASK_RUNNING);
  684. remove_wait_queue(&nlk->wait, &wait);
  685. sock_put(sk);
  686. if (signal_pending(current)) {
  687. kfree_skb(skb);
  688. return sock_intr_errno(*timeo);
  689. }
  690. return 1;
  691. }
  692. skb_set_owner_r(skb, sk);
  693. return 0;
  694. }
  695. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  696. {
  697. int len = skb->len;
  698. skb_queue_tail(&sk->sk_receive_queue, skb);
  699. sk->sk_data_ready(sk, len);
  700. return len;
  701. }
  702. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  703. {
  704. int len = __netlink_sendskb(sk, skb);
  705. sock_put(sk);
  706. return len;
  707. }
  708. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  709. {
  710. kfree_skb(skb);
  711. sock_put(sk);
  712. }
  713. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  714. gfp_t allocation)
  715. {
  716. int delta;
  717. skb_orphan(skb);
  718. delta = skb->end - skb->tail;
  719. if (delta * 2 < skb->truesize)
  720. return skb;
  721. if (skb_shared(skb)) {
  722. struct sk_buff *nskb = skb_clone(skb, allocation);
  723. if (!nskb)
  724. return skb;
  725. kfree_skb(skb);
  726. skb = nskb;
  727. }
  728. if (!pskb_expand_head(skb, 0, -delta, allocation))
  729. skb->truesize -= delta;
  730. return skb;
  731. }
  732. static inline void netlink_rcv_wake(struct sock *sk)
  733. {
  734. struct netlink_sock *nlk = nlk_sk(sk);
  735. if (skb_queue_empty(&sk->sk_receive_queue))
  736. clear_bit(0, &nlk->state);
  737. if (!test_bit(0, &nlk->state))
  738. wake_up_interruptible(&nlk->wait);
  739. }
  740. static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
  741. {
  742. int ret;
  743. struct netlink_sock *nlk = nlk_sk(sk);
  744. ret = -ECONNREFUSED;
  745. if (nlk->netlink_rcv != NULL) {
  746. ret = skb->len;
  747. skb_set_owner_r(skb, sk);
  748. nlk->netlink_rcv(skb);
  749. }
  750. kfree_skb(skb);
  751. sock_put(sk);
  752. return ret;
  753. }
  754. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  755. u32 pid, int nonblock)
  756. {
  757. struct sock *sk;
  758. int err;
  759. long timeo;
  760. skb = netlink_trim(skb, gfp_any());
  761. timeo = sock_sndtimeo(ssk, nonblock);
  762. retry:
  763. sk = netlink_getsockbypid(ssk, pid);
  764. if (IS_ERR(sk)) {
  765. kfree_skb(skb);
  766. return PTR_ERR(sk);
  767. }
  768. if (netlink_is_kernel(sk))
  769. return netlink_unicast_kernel(sk, skb);
  770. if (sk_filter(sk, skb)) {
  771. err = skb->len;
  772. kfree_skb(skb);
  773. sock_put(sk);
  774. return err;
  775. }
  776. err = netlink_attachskb(sk, skb, &timeo, ssk);
  777. if (err == 1)
  778. goto retry;
  779. if (err)
  780. return err;
  781. return netlink_sendskb(sk, skb);
  782. }
  783. EXPORT_SYMBOL(netlink_unicast);
  784. int netlink_has_listeners(struct sock *sk, unsigned int group)
  785. {
  786. int res = 0;
  787. struct listeners *listeners;
  788. BUG_ON(!netlink_is_kernel(sk));
  789. rcu_read_lock();
  790. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  791. if (group - 1 < nl_table[sk->sk_protocol].groups)
  792. res = test_bit(group - 1, listeners->masks);
  793. rcu_read_unlock();
  794. return res;
  795. }
  796. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  797. static inline int netlink_broadcast_deliver(struct sock *sk,
  798. struct sk_buff *skb)
  799. {
  800. struct netlink_sock *nlk = nlk_sk(sk);
  801. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  802. !test_bit(0, &nlk->state)) {
  803. skb_set_owner_r(skb, sk);
  804. __netlink_sendskb(sk, skb);
  805. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  806. }
  807. return -1;
  808. }
  809. struct netlink_broadcast_data {
  810. struct sock *exclude_sk;
  811. struct net *net;
  812. u32 pid;
  813. u32 group;
  814. int failure;
  815. int delivery_failure;
  816. int congested;
  817. int delivered;
  818. gfp_t allocation;
  819. struct sk_buff *skb, *skb2;
  820. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  821. void *tx_data;
  822. };
  823. static inline int do_one_broadcast(struct sock *sk,
  824. struct netlink_broadcast_data *p)
  825. {
  826. struct netlink_sock *nlk = nlk_sk(sk);
  827. int val;
  828. if (p->exclude_sk == sk)
  829. goto out;
  830. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  831. !test_bit(p->group - 1, nlk->groups))
  832. goto out;
  833. if (!net_eq(sock_net(sk), p->net))
  834. goto out;
  835. if (p->failure) {
  836. netlink_overrun(sk);
  837. goto out;
  838. }
  839. sock_hold(sk);
  840. if (p->skb2 == NULL) {
  841. if (skb_shared(p->skb)) {
  842. p->skb2 = skb_clone(p->skb, p->allocation);
  843. } else {
  844. p->skb2 = skb_get(p->skb);
  845. /*
  846. * skb ownership may have been set when
  847. * delivered to a previous socket.
  848. */
  849. skb_orphan(p->skb2);
  850. }
  851. }
  852. if (p->skb2 == NULL) {
  853. netlink_overrun(sk);
  854. /* Clone failed. Notify ALL listeners. */
  855. p->failure = 1;
  856. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  857. p->delivery_failure = 1;
  858. } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  859. kfree_skb(p->skb2);
  860. p->skb2 = NULL;
  861. } else if (sk_filter(sk, p->skb2)) {
  862. kfree_skb(p->skb2);
  863. p->skb2 = NULL;
  864. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  865. netlink_overrun(sk);
  866. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  867. p->delivery_failure = 1;
  868. } else {
  869. p->congested |= val;
  870. p->delivered = 1;
  871. p->skb2 = NULL;
  872. }
  873. sock_put(sk);
  874. out:
  875. return 0;
  876. }
  877. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 pid,
  878. u32 group, gfp_t allocation,
  879. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  880. void *filter_data)
  881. {
  882. struct net *net = sock_net(ssk);
  883. struct netlink_broadcast_data info;
  884. struct hlist_node *node;
  885. struct sock *sk;
  886. skb = netlink_trim(skb, allocation);
  887. info.exclude_sk = ssk;
  888. info.net = net;
  889. info.pid = pid;
  890. info.group = group;
  891. info.failure = 0;
  892. info.delivery_failure = 0;
  893. info.congested = 0;
  894. info.delivered = 0;
  895. info.allocation = allocation;
  896. info.skb = skb;
  897. info.skb2 = NULL;
  898. info.tx_filter = filter;
  899. info.tx_data = filter_data;
  900. /* While we sleep in clone, do not allow to change socket list */
  901. netlink_lock_table();
  902. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  903. do_one_broadcast(sk, &info);
  904. consume_skb(skb);
  905. netlink_unlock_table();
  906. if (info.delivery_failure) {
  907. kfree_skb(info.skb2);
  908. return -ENOBUFS;
  909. } else
  910. consume_skb(info.skb2);
  911. if (info.delivered) {
  912. if (info.congested && (allocation & __GFP_WAIT))
  913. yield();
  914. return 0;
  915. }
  916. return -ESRCH;
  917. }
  918. EXPORT_SYMBOL(netlink_broadcast_filtered);
  919. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  920. u32 group, gfp_t allocation)
  921. {
  922. return netlink_broadcast_filtered(ssk, skb, pid, group, allocation,
  923. NULL, NULL);
  924. }
  925. EXPORT_SYMBOL(netlink_broadcast);
  926. struct netlink_set_err_data {
  927. struct sock *exclude_sk;
  928. u32 pid;
  929. u32 group;
  930. int code;
  931. };
  932. static inline int do_one_set_err(struct sock *sk,
  933. struct netlink_set_err_data *p)
  934. {
  935. struct netlink_sock *nlk = nlk_sk(sk);
  936. int ret = 0;
  937. if (sk == p->exclude_sk)
  938. goto out;
  939. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  940. goto out;
  941. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  942. !test_bit(p->group - 1, nlk->groups))
  943. goto out;
  944. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  945. ret = 1;
  946. goto out;
  947. }
  948. sk->sk_err = p->code;
  949. sk->sk_error_report(sk);
  950. out:
  951. return ret;
  952. }
  953. /**
  954. * netlink_set_err - report error to broadcast listeners
  955. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  956. * @pid: the PID of a process that we want to skip (if any)
  957. * @groups: the broadcast group that will notice the error
  958. * @code: error code, must be negative (as usual in kernelspace)
  959. *
  960. * This function returns the number of broadcast listeners that have set the
  961. * NETLINK_RECV_NO_ENOBUFS socket option.
  962. */
  963. int netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  964. {
  965. struct netlink_set_err_data info;
  966. struct hlist_node *node;
  967. struct sock *sk;
  968. int ret = 0;
  969. info.exclude_sk = ssk;
  970. info.pid = pid;
  971. info.group = group;
  972. /* sk->sk_err wants a positive error value */
  973. info.code = -code;
  974. read_lock(&nl_table_lock);
  975. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  976. ret += do_one_set_err(sk, &info);
  977. read_unlock(&nl_table_lock);
  978. return ret;
  979. }
  980. EXPORT_SYMBOL(netlink_set_err);
  981. /* must be called with netlink table grabbed */
  982. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  983. unsigned int group,
  984. int is_new)
  985. {
  986. int old, new = !!is_new, subscriptions;
  987. old = test_bit(group - 1, nlk->groups);
  988. subscriptions = nlk->subscriptions - old + new;
  989. if (new)
  990. __set_bit(group - 1, nlk->groups);
  991. else
  992. __clear_bit(group - 1, nlk->groups);
  993. netlink_update_subscriptions(&nlk->sk, subscriptions);
  994. netlink_update_listeners(&nlk->sk);
  995. }
  996. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  997. char __user *optval, unsigned int optlen)
  998. {
  999. struct sock *sk = sock->sk;
  1000. struct netlink_sock *nlk = nlk_sk(sk);
  1001. unsigned int val = 0;
  1002. int err;
  1003. if (level != SOL_NETLINK)
  1004. return -ENOPROTOOPT;
  1005. if (optlen >= sizeof(int) &&
  1006. get_user(val, (unsigned int __user *)optval))
  1007. return -EFAULT;
  1008. switch (optname) {
  1009. case NETLINK_PKTINFO:
  1010. if (val)
  1011. nlk->flags |= NETLINK_RECV_PKTINFO;
  1012. else
  1013. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  1014. err = 0;
  1015. break;
  1016. case NETLINK_ADD_MEMBERSHIP:
  1017. case NETLINK_DROP_MEMBERSHIP: {
  1018. if (!netlink_capable(sock, NL_NONROOT_RECV))
  1019. return -EPERM;
  1020. err = netlink_realloc_groups(sk);
  1021. if (err)
  1022. return err;
  1023. if (!val || val - 1 >= nlk->ngroups)
  1024. return -EINVAL;
  1025. netlink_table_grab();
  1026. netlink_update_socket_mc(nlk, val,
  1027. optname == NETLINK_ADD_MEMBERSHIP);
  1028. netlink_table_ungrab();
  1029. err = 0;
  1030. break;
  1031. }
  1032. case NETLINK_BROADCAST_ERROR:
  1033. if (val)
  1034. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1035. else
  1036. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1037. err = 0;
  1038. break;
  1039. case NETLINK_NO_ENOBUFS:
  1040. if (val) {
  1041. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1042. clear_bit(0, &nlk->state);
  1043. wake_up_interruptible(&nlk->wait);
  1044. } else
  1045. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1046. err = 0;
  1047. break;
  1048. default:
  1049. err = -ENOPROTOOPT;
  1050. }
  1051. return err;
  1052. }
  1053. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1054. char __user *optval, int __user *optlen)
  1055. {
  1056. struct sock *sk = sock->sk;
  1057. struct netlink_sock *nlk = nlk_sk(sk);
  1058. int len, val, err;
  1059. if (level != SOL_NETLINK)
  1060. return -ENOPROTOOPT;
  1061. if (get_user(len, optlen))
  1062. return -EFAULT;
  1063. if (len < 0)
  1064. return -EINVAL;
  1065. switch (optname) {
  1066. case NETLINK_PKTINFO:
  1067. if (len < sizeof(int))
  1068. return -EINVAL;
  1069. len = sizeof(int);
  1070. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1071. if (put_user(len, optlen) ||
  1072. put_user(val, optval))
  1073. return -EFAULT;
  1074. err = 0;
  1075. break;
  1076. case NETLINK_BROADCAST_ERROR:
  1077. if (len < sizeof(int))
  1078. return -EINVAL;
  1079. len = sizeof(int);
  1080. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1081. if (put_user(len, optlen) ||
  1082. put_user(val, optval))
  1083. return -EFAULT;
  1084. err = 0;
  1085. break;
  1086. case NETLINK_NO_ENOBUFS:
  1087. if (len < sizeof(int))
  1088. return -EINVAL;
  1089. len = sizeof(int);
  1090. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1091. if (put_user(len, optlen) ||
  1092. put_user(val, optval))
  1093. return -EFAULT;
  1094. err = 0;
  1095. break;
  1096. default:
  1097. err = -ENOPROTOOPT;
  1098. }
  1099. return err;
  1100. }
  1101. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1102. {
  1103. struct nl_pktinfo info;
  1104. info.group = NETLINK_CB(skb).dst_group;
  1105. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1106. }
  1107. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1108. struct msghdr *msg, size_t len)
  1109. {
  1110. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1111. struct sock *sk = sock->sk;
  1112. struct netlink_sock *nlk = nlk_sk(sk);
  1113. struct sockaddr_nl *addr = msg->msg_name;
  1114. u32 dst_pid;
  1115. u32 dst_group;
  1116. struct sk_buff *skb;
  1117. int err;
  1118. struct scm_cookie scm;
  1119. if (msg->msg_flags&MSG_OOB)
  1120. return -EOPNOTSUPP;
  1121. if (NULL == siocb->scm) {
  1122. siocb->scm = &scm;
  1123. memset(&scm, 0, sizeof(scm));
  1124. }
  1125. err = scm_send(sock, msg, siocb->scm);
  1126. if (err < 0)
  1127. return err;
  1128. if (msg->msg_namelen) {
  1129. err = -EINVAL;
  1130. if (addr->nl_family != AF_NETLINK)
  1131. goto out;
  1132. dst_pid = addr->nl_pid;
  1133. dst_group = ffs(addr->nl_groups);
  1134. err = -EPERM;
  1135. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  1136. goto out;
  1137. } else {
  1138. dst_pid = nlk->dst_pid;
  1139. dst_group = nlk->dst_group;
  1140. }
  1141. if (!nlk->pid) {
  1142. err = netlink_autobind(sock);
  1143. if (err)
  1144. goto out;
  1145. }
  1146. err = -EMSGSIZE;
  1147. if (len > sk->sk_sndbuf - 32)
  1148. goto out;
  1149. err = -ENOBUFS;
  1150. skb = alloc_skb(len, GFP_KERNEL);
  1151. if (skb == NULL)
  1152. goto out;
  1153. NETLINK_CB(skb).pid = nlk->pid;
  1154. NETLINK_CB(skb).dst_group = dst_group;
  1155. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1156. err = -EFAULT;
  1157. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1158. kfree_skb(skb);
  1159. goto out;
  1160. }
  1161. err = security_netlink_send(sk, skb);
  1162. if (err) {
  1163. kfree_skb(skb);
  1164. goto out;
  1165. }
  1166. if (dst_group) {
  1167. atomic_inc(&skb->users);
  1168. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  1169. }
  1170. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  1171. out:
  1172. scm_destroy(siocb->scm);
  1173. return err;
  1174. }
  1175. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1176. struct msghdr *msg, size_t len,
  1177. int flags)
  1178. {
  1179. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1180. struct scm_cookie scm;
  1181. struct sock *sk = sock->sk;
  1182. struct netlink_sock *nlk = nlk_sk(sk);
  1183. int noblock = flags&MSG_DONTWAIT;
  1184. size_t copied;
  1185. struct sk_buff *skb, *data_skb;
  1186. int err, ret;
  1187. if (flags&MSG_OOB)
  1188. return -EOPNOTSUPP;
  1189. copied = 0;
  1190. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1191. if (skb == NULL)
  1192. goto out;
  1193. data_skb = skb;
  1194. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1195. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1196. /*
  1197. * If this skb has a frag_list, then here that means that we
  1198. * will have to use the frag_list skb's data for compat tasks
  1199. * and the regular skb's data for normal (non-compat) tasks.
  1200. *
  1201. * If we need to send the compat skb, assign it to the
  1202. * 'data_skb' variable so that it will be used below for data
  1203. * copying. We keep 'skb' for everything else, including
  1204. * freeing both later.
  1205. */
  1206. if (flags & MSG_CMSG_COMPAT)
  1207. data_skb = skb_shinfo(skb)->frag_list;
  1208. }
  1209. #endif
  1210. msg->msg_namelen = 0;
  1211. copied = data_skb->len;
  1212. if (len < copied) {
  1213. msg->msg_flags |= MSG_TRUNC;
  1214. copied = len;
  1215. }
  1216. skb_reset_transport_header(data_skb);
  1217. err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
  1218. if (msg->msg_name) {
  1219. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1220. addr->nl_family = AF_NETLINK;
  1221. addr->nl_pad = 0;
  1222. addr->nl_pid = NETLINK_CB(skb).pid;
  1223. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1224. msg->msg_namelen = sizeof(*addr);
  1225. }
  1226. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1227. netlink_cmsg_recv_pktinfo(msg, skb);
  1228. if (NULL == siocb->scm) {
  1229. memset(&scm, 0, sizeof(scm));
  1230. siocb->scm = &scm;
  1231. }
  1232. siocb->scm->creds = *NETLINK_CREDS(skb);
  1233. if (flags & MSG_TRUNC)
  1234. copied = data_skb->len;
  1235. skb_free_datagram(sk, skb);
  1236. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  1237. ret = netlink_dump(sk);
  1238. if (ret) {
  1239. sk->sk_err = ret;
  1240. sk->sk_error_report(sk);
  1241. }
  1242. }
  1243. scm_recv(sock, msg, siocb->scm, flags);
  1244. out:
  1245. netlink_rcv_wake(sk);
  1246. return err ? : copied;
  1247. }
  1248. static void netlink_data_ready(struct sock *sk, int len)
  1249. {
  1250. BUG();
  1251. }
  1252. /*
  1253. * We export these functions to other modules. They provide a
  1254. * complete set of kernel non-blocking support for message
  1255. * queueing.
  1256. */
  1257. struct sock *
  1258. netlink_kernel_create(struct net *net, int unit, unsigned int groups,
  1259. void (*input)(struct sk_buff *skb),
  1260. struct mutex *cb_mutex, struct module *module)
  1261. {
  1262. struct socket *sock;
  1263. struct sock *sk;
  1264. struct netlink_sock *nlk;
  1265. struct listeners *listeners = NULL;
  1266. BUG_ON(!nl_table);
  1267. if (unit < 0 || unit >= MAX_LINKS)
  1268. return NULL;
  1269. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1270. return NULL;
  1271. /*
  1272. * We have to just have a reference on the net from sk, but don't
  1273. * get_net it. Besides, we cannot get and then put the net here.
  1274. * So we create one inside init_net and the move it to net.
  1275. */
  1276. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1277. goto out_sock_release_nosk;
  1278. sk = sock->sk;
  1279. sk_change_net(sk, net);
  1280. if (groups < 32)
  1281. groups = 32;
  1282. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1283. if (!listeners)
  1284. goto out_sock_release;
  1285. sk->sk_data_ready = netlink_data_ready;
  1286. if (input)
  1287. nlk_sk(sk)->netlink_rcv = input;
  1288. if (netlink_insert(sk, net, 0))
  1289. goto out_sock_release;
  1290. nlk = nlk_sk(sk);
  1291. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1292. netlink_table_grab();
  1293. if (!nl_table[unit].registered) {
  1294. nl_table[unit].groups = groups;
  1295. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  1296. nl_table[unit].cb_mutex = cb_mutex;
  1297. nl_table[unit].module = module;
  1298. nl_table[unit].registered = 1;
  1299. } else {
  1300. kfree(listeners);
  1301. nl_table[unit].registered++;
  1302. }
  1303. netlink_table_ungrab();
  1304. return sk;
  1305. out_sock_release:
  1306. kfree(listeners);
  1307. netlink_kernel_release(sk);
  1308. return NULL;
  1309. out_sock_release_nosk:
  1310. sock_release(sock);
  1311. return NULL;
  1312. }
  1313. EXPORT_SYMBOL(netlink_kernel_create);
  1314. void
  1315. netlink_kernel_release(struct sock *sk)
  1316. {
  1317. sk_release_kernel(sk);
  1318. }
  1319. EXPORT_SYMBOL(netlink_kernel_release);
  1320. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1321. {
  1322. struct listeners *new, *old;
  1323. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1324. if (groups < 32)
  1325. groups = 32;
  1326. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1327. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  1328. if (!new)
  1329. return -ENOMEM;
  1330. old = rcu_dereference_raw(tbl->listeners);
  1331. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  1332. rcu_assign_pointer(tbl->listeners, new);
  1333. kfree_rcu(old, rcu);
  1334. }
  1335. tbl->groups = groups;
  1336. return 0;
  1337. }
  1338. /**
  1339. * netlink_change_ngroups - change number of multicast groups
  1340. *
  1341. * This changes the number of multicast groups that are available
  1342. * on a certain netlink family. Note that it is not possible to
  1343. * change the number of groups to below 32. Also note that it does
  1344. * not implicitly call netlink_clear_multicast_users() when the
  1345. * number of groups is reduced.
  1346. *
  1347. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1348. * @groups: The new number of groups.
  1349. */
  1350. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1351. {
  1352. int err;
  1353. netlink_table_grab();
  1354. err = __netlink_change_ngroups(sk, groups);
  1355. netlink_table_ungrab();
  1356. return err;
  1357. }
  1358. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1359. {
  1360. struct sock *sk;
  1361. struct hlist_node *node;
  1362. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1363. sk_for_each_bound(sk, node, &tbl->mc_list)
  1364. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1365. }
  1366. /**
  1367. * netlink_clear_multicast_users - kick off multicast listeners
  1368. *
  1369. * This function removes all listeners from the given group.
  1370. * @ksk: The kernel netlink socket, as returned by
  1371. * netlink_kernel_create().
  1372. * @group: The multicast group to clear.
  1373. */
  1374. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1375. {
  1376. netlink_table_grab();
  1377. __netlink_clear_multicast_users(ksk, group);
  1378. netlink_table_ungrab();
  1379. }
  1380. void netlink_set_nonroot(int protocol, unsigned int flags)
  1381. {
  1382. if ((unsigned int)protocol < MAX_LINKS)
  1383. nl_table[protocol].nl_nonroot = flags;
  1384. }
  1385. EXPORT_SYMBOL(netlink_set_nonroot);
  1386. static void netlink_destroy_callback(struct netlink_callback *cb)
  1387. {
  1388. kfree_skb(cb->skb);
  1389. kfree(cb);
  1390. }
  1391. /*
  1392. * It looks a bit ugly.
  1393. * It would be better to create kernel thread.
  1394. */
  1395. static int netlink_dump(struct sock *sk)
  1396. {
  1397. struct netlink_sock *nlk = nlk_sk(sk);
  1398. struct netlink_callback *cb;
  1399. struct sk_buff *skb;
  1400. struct nlmsghdr *nlh;
  1401. int len, err = -ENOBUFS;
  1402. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1403. if (!skb)
  1404. goto errout;
  1405. mutex_lock(nlk->cb_mutex);
  1406. cb = nlk->cb;
  1407. if (cb == NULL) {
  1408. err = -EINVAL;
  1409. goto errout_skb;
  1410. }
  1411. len = cb->dump(skb, cb);
  1412. if (len > 0) {
  1413. mutex_unlock(nlk->cb_mutex);
  1414. if (sk_filter(sk, skb))
  1415. kfree_skb(skb);
  1416. else
  1417. __netlink_sendskb(sk, skb);
  1418. return 0;
  1419. }
  1420. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1421. if (!nlh)
  1422. goto errout_skb;
  1423. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1424. if (sk_filter(sk, skb))
  1425. kfree_skb(skb);
  1426. else
  1427. __netlink_sendskb(sk, skb);
  1428. if (cb->done)
  1429. cb->done(cb);
  1430. nlk->cb = NULL;
  1431. mutex_unlock(nlk->cb_mutex);
  1432. netlink_destroy_callback(cb);
  1433. return 0;
  1434. errout_skb:
  1435. mutex_unlock(nlk->cb_mutex);
  1436. kfree_skb(skb);
  1437. errout:
  1438. return err;
  1439. }
  1440. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1441. const struct nlmsghdr *nlh,
  1442. int (*dump)(struct sk_buff *skb,
  1443. struct netlink_callback *),
  1444. int (*done)(struct netlink_callback *))
  1445. {
  1446. struct netlink_callback *cb;
  1447. struct sock *sk;
  1448. struct netlink_sock *nlk;
  1449. int ret;
  1450. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1451. if (cb == NULL)
  1452. return -ENOBUFS;
  1453. cb->dump = dump;
  1454. cb->done = done;
  1455. cb->nlh = nlh;
  1456. atomic_inc(&skb->users);
  1457. cb->skb = skb;
  1458. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
  1459. if (sk == NULL) {
  1460. netlink_destroy_callback(cb);
  1461. return -ECONNREFUSED;
  1462. }
  1463. nlk = nlk_sk(sk);
  1464. /* A dump is in progress... */
  1465. mutex_lock(nlk->cb_mutex);
  1466. if (nlk->cb) {
  1467. mutex_unlock(nlk->cb_mutex);
  1468. netlink_destroy_callback(cb);
  1469. sock_put(sk);
  1470. return -EBUSY;
  1471. }
  1472. nlk->cb = cb;
  1473. mutex_unlock(nlk->cb_mutex);
  1474. ret = netlink_dump(sk);
  1475. sock_put(sk);
  1476. if (ret)
  1477. return ret;
  1478. /* We successfully started a dump, by returning -EINTR we
  1479. * signal not to send ACK even if it was requested.
  1480. */
  1481. return -EINTR;
  1482. }
  1483. EXPORT_SYMBOL(netlink_dump_start);
  1484. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1485. {
  1486. struct sk_buff *skb;
  1487. struct nlmsghdr *rep;
  1488. struct nlmsgerr *errmsg;
  1489. size_t payload = sizeof(*errmsg);
  1490. /* error messages get the original request appened */
  1491. if (err)
  1492. payload += nlmsg_len(nlh);
  1493. skb = nlmsg_new(payload, GFP_KERNEL);
  1494. if (!skb) {
  1495. struct sock *sk;
  1496. sk = netlink_lookup(sock_net(in_skb->sk),
  1497. in_skb->sk->sk_protocol,
  1498. NETLINK_CB(in_skb).pid);
  1499. if (sk) {
  1500. sk->sk_err = ENOBUFS;
  1501. sk->sk_error_report(sk);
  1502. sock_put(sk);
  1503. }
  1504. return;
  1505. }
  1506. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1507. NLMSG_ERROR, payload, 0);
  1508. errmsg = nlmsg_data(rep);
  1509. errmsg->error = err;
  1510. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1511. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1512. }
  1513. EXPORT_SYMBOL(netlink_ack);
  1514. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1515. struct nlmsghdr *))
  1516. {
  1517. struct nlmsghdr *nlh;
  1518. int err;
  1519. while (skb->len >= nlmsg_total_size(0)) {
  1520. int msglen;
  1521. nlh = nlmsg_hdr(skb);
  1522. err = 0;
  1523. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1524. return 0;
  1525. /* Only requests are handled by the kernel */
  1526. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1527. goto ack;
  1528. /* Skip control messages */
  1529. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1530. goto ack;
  1531. err = cb(skb, nlh);
  1532. if (err == -EINTR)
  1533. goto skip;
  1534. ack:
  1535. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1536. netlink_ack(skb, nlh, err);
  1537. skip:
  1538. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1539. if (msglen > skb->len)
  1540. msglen = skb->len;
  1541. skb_pull(skb, msglen);
  1542. }
  1543. return 0;
  1544. }
  1545. EXPORT_SYMBOL(netlink_rcv_skb);
  1546. /**
  1547. * nlmsg_notify - send a notification netlink message
  1548. * @sk: netlink socket to use
  1549. * @skb: notification message
  1550. * @pid: destination netlink pid for reports or 0
  1551. * @group: destination multicast group or 0
  1552. * @report: 1 to report back, 0 to disable
  1553. * @flags: allocation flags
  1554. */
  1555. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
  1556. unsigned int group, int report, gfp_t flags)
  1557. {
  1558. int err = 0;
  1559. if (group) {
  1560. int exclude_pid = 0;
  1561. if (report) {
  1562. atomic_inc(&skb->users);
  1563. exclude_pid = pid;
  1564. }
  1565. /* errors reported via destination sk->sk_err, but propagate
  1566. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1567. err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
  1568. }
  1569. if (report) {
  1570. int err2;
  1571. err2 = nlmsg_unicast(sk, skb, pid);
  1572. if (!err || err == -ESRCH)
  1573. err = err2;
  1574. }
  1575. return err;
  1576. }
  1577. EXPORT_SYMBOL(nlmsg_notify);
  1578. #ifdef CONFIG_PROC_FS
  1579. struct nl_seq_iter {
  1580. struct seq_net_private p;
  1581. int link;
  1582. int hash_idx;
  1583. };
  1584. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1585. {
  1586. struct nl_seq_iter *iter = seq->private;
  1587. int i, j;
  1588. struct sock *s;
  1589. struct hlist_node *node;
  1590. loff_t off = 0;
  1591. for (i = 0; i < MAX_LINKS; i++) {
  1592. struct nl_pid_hash *hash = &nl_table[i].hash;
  1593. for (j = 0; j <= hash->mask; j++) {
  1594. sk_for_each(s, node, &hash->table[j]) {
  1595. if (sock_net(s) != seq_file_net(seq))
  1596. continue;
  1597. if (off == pos) {
  1598. iter->link = i;
  1599. iter->hash_idx = j;
  1600. return s;
  1601. }
  1602. ++off;
  1603. }
  1604. }
  1605. }
  1606. return NULL;
  1607. }
  1608. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1609. __acquires(nl_table_lock)
  1610. {
  1611. read_lock(&nl_table_lock);
  1612. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1613. }
  1614. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1615. {
  1616. struct sock *s;
  1617. struct nl_seq_iter *iter;
  1618. int i, j;
  1619. ++*pos;
  1620. if (v == SEQ_START_TOKEN)
  1621. return netlink_seq_socket_idx(seq, 0);
  1622. iter = seq->private;
  1623. s = v;
  1624. do {
  1625. s = sk_next(s);
  1626. } while (s && sock_net(s) != seq_file_net(seq));
  1627. if (s)
  1628. return s;
  1629. i = iter->link;
  1630. j = iter->hash_idx + 1;
  1631. do {
  1632. struct nl_pid_hash *hash = &nl_table[i].hash;
  1633. for (; j <= hash->mask; j++) {
  1634. s = sk_head(&hash->table[j]);
  1635. while (s && sock_net(s) != seq_file_net(seq))
  1636. s = sk_next(s);
  1637. if (s) {
  1638. iter->link = i;
  1639. iter->hash_idx = j;
  1640. return s;
  1641. }
  1642. }
  1643. j = 0;
  1644. } while (++i < MAX_LINKS);
  1645. return NULL;
  1646. }
  1647. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1648. __releases(nl_table_lock)
  1649. {
  1650. read_unlock(&nl_table_lock);
  1651. }
  1652. static int netlink_seq_show(struct seq_file *seq, void *v)
  1653. {
  1654. if (v == SEQ_START_TOKEN)
  1655. seq_puts(seq,
  1656. "sk Eth Pid Groups "
  1657. "Rmem Wmem Dump Locks Drops Inode\n");
  1658. else {
  1659. struct sock *s = v;
  1660. struct netlink_sock *nlk = nlk_sk(s);
  1661. seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
  1662. s,
  1663. s->sk_protocol,
  1664. nlk->pid,
  1665. nlk->groups ? (u32)nlk->groups[0] : 0,
  1666. sk_rmem_alloc_get(s),
  1667. sk_wmem_alloc_get(s),
  1668. nlk->cb,
  1669. atomic_read(&s->sk_refcnt),
  1670. atomic_read(&s->sk_drops),
  1671. sock_i_ino(s)
  1672. );
  1673. }
  1674. return 0;
  1675. }
  1676. static const struct seq_operations netlink_seq_ops = {
  1677. .start = netlink_seq_start,
  1678. .next = netlink_seq_next,
  1679. .stop = netlink_seq_stop,
  1680. .show = netlink_seq_show,
  1681. };
  1682. static int netlink_seq_open(struct inode *inode, struct file *file)
  1683. {
  1684. return seq_open_net(inode, file, &netlink_seq_ops,
  1685. sizeof(struct nl_seq_iter));
  1686. }
  1687. static const struct file_operations netlink_seq_fops = {
  1688. .owner = THIS_MODULE,
  1689. .open = netlink_seq_open,
  1690. .read = seq_read,
  1691. .llseek = seq_lseek,
  1692. .release = seq_release_net,
  1693. };
  1694. #endif
  1695. int netlink_register_notifier(struct notifier_block *nb)
  1696. {
  1697. return atomic_notifier_chain_register(&netlink_chain, nb);
  1698. }
  1699. EXPORT_SYMBOL(netlink_register_notifier);
  1700. int netlink_unregister_notifier(struct notifier_block *nb)
  1701. {
  1702. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1703. }
  1704. EXPORT_SYMBOL(netlink_unregister_notifier);
  1705. static const struct proto_ops netlink_ops = {
  1706. .family = PF_NETLINK,
  1707. .owner = THIS_MODULE,
  1708. .release = netlink_release,
  1709. .bind = netlink_bind,
  1710. .connect = netlink_connect,
  1711. .socketpair = sock_no_socketpair,
  1712. .accept = sock_no_accept,
  1713. .getname = netlink_getname,
  1714. .poll = datagram_poll,
  1715. .ioctl = sock_no_ioctl,
  1716. .listen = sock_no_listen,
  1717. .shutdown = sock_no_shutdown,
  1718. .setsockopt = netlink_setsockopt,
  1719. .getsockopt = netlink_getsockopt,
  1720. .sendmsg = netlink_sendmsg,
  1721. .recvmsg = netlink_recvmsg,
  1722. .mmap = sock_no_mmap,
  1723. .sendpage = sock_no_sendpage,
  1724. };
  1725. static const struct net_proto_family netlink_family_ops = {
  1726. .family = PF_NETLINK,
  1727. .create = netlink_create,
  1728. .owner = THIS_MODULE, /* for consistency 8) */
  1729. };
  1730. static int __net_init netlink_net_init(struct net *net)
  1731. {
  1732. #ifdef CONFIG_PROC_FS
  1733. if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
  1734. return -ENOMEM;
  1735. #endif
  1736. return 0;
  1737. }
  1738. static void __net_exit netlink_net_exit(struct net *net)
  1739. {
  1740. #ifdef CONFIG_PROC_FS
  1741. proc_net_remove(net, "netlink");
  1742. #endif
  1743. }
  1744. static void __init netlink_add_usersock_entry(void)
  1745. {
  1746. struct listeners *listeners;
  1747. int groups = 32;
  1748. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1749. if (!listeners)
  1750. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  1751. netlink_table_grab();
  1752. nl_table[NETLINK_USERSOCK].groups = groups;
  1753. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  1754. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  1755. nl_table[NETLINK_USERSOCK].registered = 1;
  1756. netlink_table_ungrab();
  1757. }
  1758. static struct pernet_operations __net_initdata netlink_net_ops = {
  1759. .init = netlink_net_init,
  1760. .exit = netlink_net_exit,
  1761. };
  1762. static int __init netlink_proto_init(void)
  1763. {
  1764. struct sk_buff *dummy_skb;
  1765. int i;
  1766. unsigned long limit;
  1767. unsigned int order;
  1768. int err = proto_register(&netlink_proto, 0);
  1769. if (err != 0)
  1770. goto out;
  1771. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
  1772. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1773. if (!nl_table)
  1774. goto panic;
  1775. if (totalram_pages >= (128 * 1024))
  1776. limit = totalram_pages >> (21 - PAGE_SHIFT);
  1777. else
  1778. limit = totalram_pages >> (23 - PAGE_SHIFT);
  1779. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1780. limit = (1UL << order) / sizeof(struct hlist_head);
  1781. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1782. for (i = 0; i < MAX_LINKS; i++) {
  1783. struct nl_pid_hash *hash = &nl_table[i].hash;
  1784. hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
  1785. if (!hash->table) {
  1786. while (i-- > 0)
  1787. nl_pid_hash_free(nl_table[i].hash.table,
  1788. 1 * sizeof(*hash->table));
  1789. kfree(nl_table);
  1790. goto panic;
  1791. }
  1792. hash->max_shift = order;
  1793. hash->shift = 0;
  1794. hash->mask = 0;
  1795. hash->rehash_time = jiffies;
  1796. }
  1797. netlink_add_usersock_entry();
  1798. sock_register(&netlink_family_ops);
  1799. register_pernet_subsys(&netlink_net_ops);
  1800. /* The netlink device handler may be needed early. */
  1801. rtnetlink_init();
  1802. out:
  1803. return err;
  1804. panic:
  1805. panic("netlink_init: Cannot allocate nl_table\n");
  1806. }
  1807. core_initcall(netlink_proto_init);