af_netlink.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  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_rcu_head {
  81. struct rcu_head rcu_head;
  82. void *ptr;
  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. unsigned long *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[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. {
  363. struct module *module = NULL;
  364. struct mutex *cb_mutex;
  365. struct netlink_sock *nlk;
  366. int err = 0;
  367. sock->state = SS_UNCONNECTED;
  368. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  369. return -ESOCKTNOSUPPORT;
  370. if (protocol < 0 || protocol >= MAX_LINKS)
  371. return -EPROTONOSUPPORT;
  372. netlink_lock_table();
  373. #ifdef CONFIG_MODULES
  374. if (!nl_table[protocol].registered) {
  375. netlink_unlock_table();
  376. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  377. netlink_lock_table();
  378. }
  379. #endif
  380. if (nl_table[protocol].registered &&
  381. try_module_get(nl_table[protocol].module))
  382. module = nl_table[protocol].module;
  383. cb_mutex = nl_table[protocol].cb_mutex;
  384. netlink_unlock_table();
  385. err = __netlink_create(net, sock, cb_mutex, protocol);
  386. if (err < 0)
  387. goto out_module;
  388. local_bh_disable();
  389. sock_prot_inuse_add(net, &netlink_proto, 1);
  390. local_bh_enable();
  391. nlk = nlk_sk(sock->sk);
  392. nlk->module = module;
  393. out:
  394. return err;
  395. out_module:
  396. module_put(module);
  397. goto out;
  398. }
  399. static int netlink_release(struct socket *sock)
  400. {
  401. struct sock *sk = sock->sk;
  402. struct netlink_sock *nlk;
  403. if (!sk)
  404. return 0;
  405. netlink_remove(sk);
  406. sock_orphan(sk);
  407. nlk = nlk_sk(sk);
  408. /*
  409. * OK. Socket is unlinked, any packets that arrive now
  410. * will be purged.
  411. */
  412. sock->sk = NULL;
  413. wake_up_interruptible_all(&nlk->wait);
  414. skb_queue_purge(&sk->sk_write_queue);
  415. if (nlk->pid && !nlk->subscriptions) {
  416. struct netlink_notify n = {
  417. .net = sock_net(sk),
  418. .protocol = sk->sk_protocol,
  419. .pid = nlk->pid,
  420. };
  421. atomic_notifier_call_chain(&netlink_chain,
  422. NETLINK_URELEASE, &n);
  423. }
  424. module_put(nlk->module);
  425. netlink_table_grab();
  426. if (netlink_is_kernel(sk)) {
  427. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  428. if (--nl_table[sk->sk_protocol].registered == 0) {
  429. kfree(nl_table[sk->sk_protocol].listeners);
  430. nl_table[sk->sk_protocol].module = NULL;
  431. nl_table[sk->sk_protocol].registered = 0;
  432. }
  433. } else if (nlk->subscriptions)
  434. netlink_update_listeners(sk);
  435. netlink_table_ungrab();
  436. kfree(nlk->groups);
  437. nlk->groups = NULL;
  438. local_bh_disable();
  439. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  440. local_bh_enable();
  441. sock_put(sk);
  442. return 0;
  443. }
  444. static int netlink_autobind(struct socket *sock)
  445. {
  446. struct sock *sk = sock->sk;
  447. struct net *net = sock_net(sk);
  448. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  449. struct hlist_head *head;
  450. struct sock *osk;
  451. struct hlist_node *node;
  452. s32 pid = current->tgid;
  453. int err;
  454. static s32 rover = -4097;
  455. retry:
  456. cond_resched();
  457. netlink_table_grab();
  458. head = nl_pid_hashfn(hash, pid);
  459. sk_for_each(osk, node, head) {
  460. if (!net_eq(sock_net(osk), net))
  461. continue;
  462. if (nlk_sk(osk)->pid == pid) {
  463. /* Bind collision, search negative pid values. */
  464. pid = rover--;
  465. if (rover > -4097)
  466. rover = -4097;
  467. netlink_table_ungrab();
  468. goto retry;
  469. }
  470. }
  471. netlink_table_ungrab();
  472. err = netlink_insert(sk, net, pid);
  473. if (err == -EADDRINUSE)
  474. goto retry;
  475. /* If 2 threads race to autobind, that is fine. */
  476. if (err == -EBUSY)
  477. err = 0;
  478. return err;
  479. }
  480. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  481. {
  482. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  483. capable(CAP_NET_ADMIN);
  484. }
  485. static void
  486. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  487. {
  488. struct netlink_sock *nlk = nlk_sk(sk);
  489. if (nlk->subscriptions && !subscriptions)
  490. __sk_del_bind_node(sk);
  491. else if (!nlk->subscriptions && subscriptions)
  492. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  493. nlk->subscriptions = subscriptions;
  494. }
  495. static int netlink_realloc_groups(struct sock *sk)
  496. {
  497. struct netlink_sock *nlk = nlk_sk(sk);
  498. unsigned int groups;
  499. unsigned long *new_groups;
  500. int err = 0;
  501. netlink_table_grab();
  502. groups = nl_table[sk->sk_protocol].groups;
  503. if (!nl_table[sk->sk_protocol].registered) {
  504. err = -ENOENT;
  505. goto out_unlock;
  506. }
  507. if (nlk->ngroups >= groups)
  508. goto out_unlock;
  509. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  510. if (new_groups == NULL) {
  511. err = -ENOMEM;
  512. goto out_unlock;
  513. }
  514. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  515. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  516. nlk->groups = new_groups;
  517. nlk->ngroups = groups;
  518. out_unlock:
  519. netlink_table_ungrab();
  520. return err;
  521. }
  522. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  523. int addr_len)
  524. {
  525. struct sock *sk = sock->sk;
  526. struct net *net = sock_net(sk);
  527. struct netlink_sock *nlk = nlk_sk(sk);
  528. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  529. int err;
  530. if (nladdr->nl_family != AF_NETLINK)
  531. return -EINVAL;
  532. /* Only superuser is allowed to listen multicasts */
  533. if (nladdr->nl_groups) {
  534. if (!netlink_capable(sock, NL_NONROOT_RECV))
  535. return -EPERM;
  536. err = netlink_realloc_groups(sk);
  537. if (err)
  538. return err;
  539. }
  540. if (nlk->pid) {
  541. if (nladdr->nl_pid != nlk->pid)
  542. return -EINVAL;
  543. } else {
  544. err = nladdr->nl_pid ?
  545. netlink_insert(sk, net, nladdr->nl_pid) :
  546. netlink_autobind(sock);
  547. if (err)
  548. return err;
  549. }
  550. if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  551. return 0;
  552. netlink_table_grab();
  553. netlink_update_subscriptions(sk, nlk->subscriptions +
  554. hweight32(nladdr->nl_groups) -
  555. hweight32(nlk->groups[0]));
  556. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
  557. netlink_update_listeners(sk);
  558. netlink_table_ungrab();
  559. return 0;
  560. }
  561. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  562. int alen, int flags)
  563. {
  564. int err = 0;
  565. struct sock *sk = sock->sk;
  566. struct netlink_sock *nlk = nlk_sk(sk);
  567. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  568. if (addr->sa_family == AF_UNSPEC) {
  569. sk->sk_state = NETLINK_UNCONNECTED;
  570. nlk->dst_pid = 0;
  571. nlk->dst_group = 0;
  572. return 0;
  573. }
  574. if (addr->sa_family != AF_NETLINK)
  575. return -EINVAL;
  576. /* Only superuser is allowed to send multicasts */
  577. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  578. return -EPERM;
  579. if (!nlk->pid)
  580. err = netlink_autobind(sock);
  581. if (err == 0) {
  582. sk->sk_state = NETLINK_CONNECTED;
  583. nlk->dst_pid = nladdr->nl_pid;
  584. nlk->dst_group = ffs(nladdr->nl_groups);
  585. }
  586. return err;
  587. }
  588. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  589. int *addr_len, int peer)
  590. {
  591. struct sock *sk = sock->sk;
  592. struct netlink_sock *nlk = nlk_sk(sk);
  593. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  594. nladdr->nl_family = AF_NETLINK;
  595. nladdr->nl_pad = 0;
  596. *addr_len = sizeof(*nladdr);
  597. if (peer) {
  598. nladdr->nl_pid = nlk->dst_pid;
  599. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  600. } else {
  601. nladdr->nl_pid = nlk->pid;
  602. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  603. }
  604. return 0;
  605. }
  606. static void netlink_overrun(struct sock *sk)
  607. {
  608. struct netlink_sock *nlk = nlk_sk(sk);
  609. if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
  610. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  611. sk->sk_err = ENOBUFS;
  612. sk->sk_error_report(sk);
  613. }
  614. }
  615. atomic_inc(&sk->sk_drops);
  616. }
  617. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  618. {
  619. struct sock *sock;
  620. struct netlink_sock *nlk;
  621. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, pid);
  622. if (!sock)
  623. return ERR_PTR(-ECONNREFUSED);
  624. /* Don't bother queuing skb if kernel socket has no input function */
  625. nlk = nlk_sk(sock);
  626. if (sock->sk_state == NETLINK_CONNECTED &&
  627. nlk->dst_pid != nlk_sk(ssk)->pid) {
  628. sock_put(sock);
  629. return ERR_PTR(-ECONNREFUSED);
  630. }
  631. return sock;
  632. }
  633. struct sock *netlink_getsockbyfilp(struct file *filp)
  634. {
  635. struct inode *inode = filp->f_path.dentry->d_inode;
  636. struct sock *sock;
  637. if (!S_ISSOCK(inode->i_mode))
  638. return ERR_PTR(-ENOTSOCK);
  639. sock = SOCKET_I(inode)->sk;
  640. if (sock->sk_family != AF_NETLINK)
  641. return ERR_PTR(-EINVAL);
  642. sock_hold(sock);
  643. return sock;
  644. }
  645. /*
  646. * Attach a skb to a netlink socket.
  647. * The caller must hold a reference to the destination socket. On error, the
  648. * reference is dropped. The skb is not send to the destination, just all
  649. * all error checks are performed and memory in the queue is reserved.
  650. * Return values:
  651. * < 0: error. skb freed, reference to sock dropped.
  652. * 0: continue
  653. * 1: repeat lookup - reference dropped while waiting for socket memory.
  654. */
  655. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  656. long *timeo, struct sock *ssk)
  657. {
  658. struct netlink_sock *nlk;
  659. nlk = nlk_sk(sk);
  660. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  661. test_bit(0, &nlk->state)) {
  662. DECLARE_WAITQUEUE(wait, current);
  663. if (!*timeo) {
  664. if (!ssk || netlink_is_kernel(ssk))
  665. netlink_overrun(sk);
  666. sock_put(sk);
  667. kfree_skb(skb);
  668. return -EAGAIN;
  669. }
  670. __set_current_state(TASK_INTERRUPTIBLE);
  671. add_wait_queue(&nlk->wait, &wait);
  672. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  673. test_bit(0, &nlk->state)) &&
  674. !sock_flag(sk, SOCK_DEAD))
  675. *timeo = schedule_timeout(*timeo);
  676. __set_current_state(TASK_RUNNING);
  677. remove_wait_queue(&nlk->wait, &wait);
  678. sock_put(sk);
  679. if (signal_pending(current)) {
  680. kfree_skb(skb);
  681. return sock_intr_errno(*timeo);
  682. }
  683. return 1;
  684. }
  685. skb_set_owner_r(skb, sk);
  686. return 0;
  687. }
  688. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  689. {
  690. int len = skb->len;
  691. skb_queue_tail(&sk->sk_receive_queue, skb);
  692. sk->sk_data_ready(sk, len);
  693. sock_put(sk);
  694. return len;
  695. }
  696. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  697. {
  698. kfree_skb(skb);
  699. sock_put(sk);
  700. }
  701. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  702. gfp_t allocation)
  703. {
  704. int delta;
  705. skb_orphan(skb);
  706. delta = skb->end - skb->tail;
  707. if (delta * 2 < skb->truesize)
  708. return skb;
  709. if (skb_shared(skb)) {
  710. struct sk_buff *nskb = skb_clone(skb, allocation);
  711. if (!nskb)
  712. return skb;
  713. kfree_skb(skb);
  714. skb = nskb;
  715. }
  716. if (!pskb_expand_head(skb, 0, -delta, allocation))
  717. skb->truesize -= delta;
  718. return skb;
  719. }
  720. static inline void netlink_rcv_wake(struct sock *sk)
  721. {
  722. struct netlink_sock *nlk = nlk_sk(sk);
  723. if (skb_queue_empty(&sk->sk_receive_queue))
  724. clear_bit(0, &nlk->state);
  725. if (!test_bit(0, &nlk->state))
  726. wake_up_interruptible(&nlk->wait);
  727. }
  728. static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
  729. {
  730. int ret;
  731. struct netlink_sock *nlk = nlk_sk(sk);
  732. ret = -ECONNREFUSED;
  733. if (nlk->netlink_rcv != NULL) {
  734. ret = skb->len;
  735. skb_set_owner_r(skb, sk);
  736. nlk->netlink_rcv(skb);
  737. }
  738. kfree_skb(skb);
  739. sock_put(sk);
  740. return ret;
  741. }
  742. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  743. u32 pid, int nonblock)
  744. {
  745. struct sock *sk;
  746. int err;
  747. long timeo;
  748. skb = netlink_trim(skb, gfp_any());
  749. timeo = sock_sndtimeo(ssk, nonblock);
  750. retry:
  751. sk = netlink_getsockbypid(ssk, pid);
  752. if (IS_ERR(sk)) {
  753. kfree_skb(skb);
  754. return PTR_ERR(sk);
  755. }
  756. if (netlink_is_kernel(sk))
  757. return netlink_unicast_kernel(sk, skb);
  758. if (sk_filter(sk, skb)) {
  759. err = skb->len;
  760. kfree_skb(skb);
  761. sock_put(sk);
  762. return err;
  763. }
  764. err = netlink_attachskb(sk, skb, &timeo, ssk);
  765. if (err == 1)
  766. goto retry;
  767. if (err)
  768. return err;
  769. return netlink_sendskb(sk, skb);
  770. }
  771. EXPORT_SYMBOL(netlink_unicast);
  772. int netlink_has_listeners(struct sock *sk, unsigned int group)
  773. {
  774. int res = 0;
  775. unsigned long *listeners;
  776. BUG_ON(!netlink_is_kernel(sk));
  777. rcu_read_lock();
  778. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  779. if (group - 1 < nl_table[sk->sk_protocol].groups)
  780. res = test_bit(group - 1, listeners);
  781. rcu_read_unlock();
  782. return res;
  783. }
  784. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  785. static inline int netlink_broadcast_deliver(struct sock *sk,
  786. struct sk_buff *skb)
  787. {
  788. struct netlink_sock *nlk = nlk_sk(sk);
  789. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  790. !test_bit(0, &nlk->state)) {
  791. skb_set_owner_r(skb, sk);
  792. skb_queue_tail(&sk->sk_receive_queue, skb);
  793. sk->sk_data_ready(sk, skb->len);
  794. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  795. }
  796. return -1;
  797. }
  798. struct netlink_broadcast_data {
  799. struct sock *exclude_sk;
  800. struct net *net;
  801. u32 pid;
  802. u32 group;
  803. int failure;
  804. int delivery_failure;
  805. int congested;
  806. int delivered;
  807. gfp_t allocation;
  808. struct sk_buff *skb, *skb2;
  809. };
  810. static inline int do_one_broadcast(struct sock *sk,
  811. struct netlink_broadcast_data *p)
  812. {
  813. struct netlink_sock *nlk = nlk_sk(sk);
  814. int val;
  815. if (p->exclude_sk == sk)
  816. goto out;
  817. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  818. !test_bit(p->group - 1, nlk->groups))
  819. goto out;
  820. if (!net_eq(sock_net(sk), p->net))
  821. goto out;
  822. if (p->failure) {
  823. netlink_overrun(sk);
  824. goto out;
  825. }
  826. sock_hold(sk);
  827. if (p->skb2 == NULL) {
  828. if (skb_shared(p->skb)) {
  829. p->skb2 = skb_clone(p->skb, p->allocation);
  830. } else {
  831. p->skb2 = skb_get(p->skb);
  832. /*
  833. * skb ownership may have been set when
  834. * delivered to a previous socket.
  835. */
  836. skb_orphan(p->skb2);
  837. }
  838. }
  839. if (p->skb2 == NULL) {
  840. netlink_overrun(sk);
  841. /* Clone failed. Notify ALL listeners. */
  842. p->failure = 1;
  843. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  844. p->delivery_failure = 1;
  845. } else if (sk_filter(sk, p->skb2)) {
  846. kfree_skb(p->skb2);
  847. p->skb2 = NULL;
  848. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  849. netlink_overrun(sk);
  850. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  851. p->delivery_failure = 1;
  852. } else {
  853. p->congested |= val;
  854. p->delivered = 1;
  855. p->skb2 = NULL;
  856. }
  857. sock_put(sk);
  858. out:
  859. return 0;
  860. }
  861. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  862. u32 group, gfp_t allocation)
  863. {
  864. struct net *net = sock_net(ssk);
  865. struct netlink_broadcast_data info;
  866. struct hlist_node *node;
  867. struct sock *sk;
  868. skb = netlink_trim(skb, allocation);
  869. info.exclude_sk = ssk;
  870. info.net = net;
  871. info.pid = pid;
  872. info.group = group;
  873. info.failure = 0;
  874. info.delivery_failure = 0;
  875. info.congested = 0;
  876. info.delivered = 0;
  877. info.allocation = allocation;
  878. info.skb = skb;
  879. info.skb2 = NULL;
  880. /* While we sleep in clone, do not allow to change socket list */
  881. netlink_lock_table();
  882. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  883. do_one_broadcast(sk, &info);
  884. kfree_skb(skb);
  885. netlink_unlock_table();
  886. kfree_skb(info.skb2);
  887. if (info.delivery_failure)
  888. return -ENOBUFS;
  889. if (info.delivered) {
  890. if (info.congested && (allocation & __GFP_WAIT))
  891. yield();
  892. return 0;
  893. }
  894. return -ESRCH;
  895. }
  896. EXPORT_SYMBOL(netlink_broadcast);
  897. struct netlink_set_err_data {
  898. struct sock *exclude_sk;
  899. u32 pid;
  900. u32 group;
  901. int code;
  902. };
  903. static inline int do_one_set_err(struct sock *sk,
  904. struct netlink_set_err_data *p)
  905. {
  906. struct netlink_sock *nlk = nlk_sk(sk);
  907. if (sk == p->exclude_sk)
  908. goto out;
  909. if (sock_net(sk) != sock_net(p->exclude_sk))
  910. goto out;
  911. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  912. !test_bit(p->group - 1, nlk->groups))
  913. goto out;
  914. sk->sk_err = p->code;
  915. sk->sk_error_report(sk);
  916. out:
  917. return 0;
  918. }
  919. /**
  920. * netlink_set_err - report error to broadcast listeners
  921. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  922. * @pid: the PID of a process that we want to skip (if any)
  923. * @groups: the broadcast group that will notice the error
  924. * @code: error code, must be negative (as usual in kernelspace)
  925. */
  926. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  927. {
  928. struct netlink_set_err_data info;
  929. struct hlist_node *node;
  930. struct sock *sk;
  931. info.exclude_sk = ssk;
  932. info.pid = pid;
  933. info.group = group;
  934. /* sk->sk_err wants a positive error value */
  935. info.code = -code;
  936. read_lock(&nl_table_lock);
  937. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  938. do_one_set_err(sk, &info);
  939. read_unlock(&nl_table_lock);
  940. }
  941. EXPORT_SYMBOL(netlink_set_err);
  942. /* must be called with netlink table grabbed */
  943. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  944. unsigned int group,
  945. int is_new)
  946. {
  947. int old, new = !!is_new, subscriptions;
  948. old = test_bit(group - 1, nlk->groups);
  949. subscriptions = nlk->subscriptions - old + new;
  950. if (new)
  951. __set_bit(group - 1, nlk->groups);
  952. else
  953. __clear_bit(group - 1, nlk->groups);
  954. netlink_update_subscriptions(&nlk->sk, subscriptions);
  955. netlink_update_listeners(&nlk->sk);
  956. }
  957. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  958. char __user *optval, unsigned int optlen)
  959. {
  960. struct sock *sk = sock->sk;
  961. struct netlink_sock *nlk = nlk_sk(sk);
  962. unsigned int val = 0;
  963. int err;
  964. if (level != SOL_NETLINK)
  965. return -ENOPROTOOPT;
  966. if (optlen >= sizeof(int) &&
  967. get_user(val, (unsigned int __user *)optval))
  968. return -EFAULT;
  969. switch (optname) {
  970. case NETLINK_PKTINFO:
  971. if (val)
  972. nlk->flags |= NETLINK_RECV_PKTINFO;
  973. else
  974. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  975. err = 0;
  976. break;
  977. case NETLINK_ADD_MEMBERSHIP:
  978. case NETLINK_DROP_MEMBERSHIP: {
  979. if (!netlink_capable(sock, NL_NONROOT_RECV))
  980. return -EPERM;
  981. err = netlink_realloc_groups(sk);
  982. if (err)
  983. return err;
  984. if (!val || val - 1 >= nlk->ngroups)
  985. return -EINVAL;
  986. netlink_table_grab();
  987. netlink_update_socket_mc(nlk, val,
  988. optname == NETLINK_ADD_MEMBERSHIP);
  989. netlink_table_ungrab();
  990. err = 0;
  991. break;
  992. }
  993. case NETLINK_BROADCAST_ERROR:
  994. if (val)
  995. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  996. else
  997. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  998. err = 0;
  999. break;
  1000. case NETLINK_NO_ENOBUFS:
  1001. if (val) {
  1002. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1003. clear_bit(0, &nlk->state);
  1004. wake_up_interruptible(&nlk->wait);
  1005. } else
  1006. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1007. err = 0;
  1008. break;
  1009. default:
  1010. err = -ENOPROTOOPT;
  1011. }
  1012. return err;
  1013. }
  1014. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1015. char __user *optval, int __user *optlen)
  1016. {
  1017. struct sock *sk = sock->sk;
  1018. struct netlink_sock *nlk = nlk_sk(sk);
  1019. int len, val, err;
  1020. if (level != SOL_NETLINK)
  1021. return -ENOPROTOOPT;
  1022. if (get_user(len, optlen))
  1023. return -EFAULT;
  1024. if (len < 0)
  1025. return -EINVAL;
  1026. switch (optname) {
  1027. case NETLINK_PKTINFO:
  1028. if (len < sizeof(int))
  1029. return -EINVAL;
  1030. len = sizeof(int);
  1031. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1032. if (put_user(len, optlen) ||
  1033. put_user(val, optval))
  1034. return -EFAULT;
  1035. err = 0;
  1036. break;
  1037. case NETLINK_BROADCAST_ERROR:
  1038. if (len < sizeof(int))
  1039. return -EINVAL;
  1040. len = sizeof(int);
  1041. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1042. if (put_user(len, optlen) ||
  1043. put_user(val, optval))
  1044. return -EFAULT;
  1045. err = 0;
  1046. break;
  1047. case NETLINK_NO_ENOBUFS:
  1048. if (len < sizeof(int))
  1049. return -EINVAL;
  1050. len = sizeof(int);
  1051. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1052. if (put_user(len, optlen) ||
  1053. put_user(val, optval))
  1054. return -EFAULT;
  1055. err = 0;
  1056. break;
  1057. default:
  1058. err = -ENOPROTOOPT;
  1059. }
  1060. return err;
  1061. }
  1062. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1063. {
  1064. struct nl_pktinfo info;
  1065. info.group = NETLINK_CB(skb).dst_group;
  1066. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1067. }
  1068. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1069. struct msghdr *msg, size_t len)
  1070. {
  1071. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1072. struct sock *sk = sock->sk;
  1073. struct netlink_sock *nlk = nlk_sk(sk);
  1074. struct sockaddr_nl *addr = msg->msg_name;
  1075. u32 dst_pid;
  1076. u32 dst_group;
  1077. struct sk_buff *skb;
  1078. int err;
  1079. struct scm_cookie scm;
  1080. if (msg->msg_flags&MSG_OOB)
  1081. return -EOPNOTSUPP;
  1082. if (NULL == siocb->scm)
  1083. siocb->scm = &scm;
  1084. err = scm_send(sock, msg, siocb->scm);
  1085. if (err < 0)
  1086. return err;
  1087. if (msg->msg_namelen) {
  1088. if (addr->nl_family != AF_NETLINK)
  1089. return -EINVAL;
  1090. dst_pid = addr->nl_pid;
  1091. dst_group = ffs(addr->nl_groups);
  1092. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  1093. return -EPERM;
  1094. } else {
  1095. dst_pid = nlk->dst_pid;
  1096. dst_group = nlk->dst_group;
  1097. }
  1098. if (!nlk->pid) {
  1099. err = netlink_autobind(sock);
  1100. if (err)
  1101. goto out;
  1102. }
  1103. err = -EMSGSIZE;
  1104. if (len > sk->sk_sndbuf - 32)
  1105. goto out;
  1106. err = -ENOBUFS;
  1107. skb = alloc_skb(len, GFP_KERNEL);
  1108. if (skb == NULL)
  1109. goto out;
  1110. NETLINK_CB(skb).pid = nlk->pid;
  1111. NETLINK_CB(skb).dst_group = dst_group;
  1112. NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
  1113. NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
  1114. security_task_getsecid(current, &(NETLINK_CB(skb).sid));
  1115. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1116. /* What can I do? Netlink is asynchronous, so that
  1117. we will have to save current capabilities to
  1118. check them, when this message will be delivered
  1119. to corresponding kernel module. --ANK (980802)
  1120. */
  1121. err = -EFAULT;
  1122. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1123. kfree_skb(skb);
  1124. goto out;
  1125. }
  1126. err = security_netlink_send(sk, skb);
  1127. if (err) {
  1128. kfree_skb(skb);
  1129. goto out;
  1130. }
  1131. if (dst_group) {
  1132. atomic_inc(&skb->users);
  1133. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  1134. }
  1135. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  1136. out:
  1137. return err;
  1138. }
  1139. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1140. struct msghdr *msg, size_t len,
  1141. int flags)
  1142. {
  1143. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1144. struct scm_cookie scm;
  1145. struct sock *sk = sock->sk;
  1146. struct netlink_sock *nlk = nlk_sk(sk);
  1147. int noblock = flags&MSG_DONTWAIT;
  1148. size_t copied;
  1149. struct sk_buff *skb, *frag __maybe_unused = NULL;
  1150. int err;
  1151. if (flags&MSG_OOB)
  1152. return -EOPNOTSUPP;
  1153. copied = 0;
  1154. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1155. if (skb == NULL)
  1156. goto out;
  1157. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1158. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1159. bool need_compat = !!(flags & MSG_CMSG_COMPAT);
  1160. /*
  1161. * If this skb has a frag_list, then here that means that
  1162. * we will have to use the frag_list skb for compat tasks
  1163. * and the regular skb for non-compat tasks.
  1164. *
  1165. * The skb might (and likely will) be cloned, so we can't
  1166. * just reset frag_list and go on with things -- we need to
  1167. * keep that. For the compat case that's easy -- simply get
  1168. * a reference to the compat skb and free the regular one
  1169. * including the frag. For the non-compat case, we need to
  1170. * avoid sending the frag to the user -- so assign NULL but
  1171. * restore it below before freeing the skb.
  1172. */
  1173. if (need_compat) {
  1174. struct sk_buff *compskb = skb_shinfo(skb)->frag_list;
  1175. skb_get(compskb);
  1176. kfree_skb(skb);
  1177. skb = compskb;
  1178. } else {
  1179. frag = skb_shinfo(skb)->frag_list;
  1180. skb_shinfo(skb)->frag_list = NULL;
  1181. }
  1182. }
  1183. #endif
  1184. msg->msg_namelen = 0;
  1185. copied = skb->len;
  1186. if (len < copied) {
  1187. msg->msg_flags |= MSG_TRUNC;
  1188. copied = len;
  1189. }
  1190. skb_reset_transport_header(skb);
  1191. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1192. if (msg->msg_name) {
  1193. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1194. addr->nl_family = AF_NETLINK;
  1195. addr->nl_pad = 0;
  1196. addr->nl_pid = NETLINK_CB(skb).pid;
  1197. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1198. msg->msg_namelen = sizeof(*addr);
  1199. }
  1200. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1201. netlink_cmsg_recv_pktinfo(msg, skb);
  1202. if (NULL == siocb->scm) {
  1203. memset(&scm, 0, sizeof(scm));
  1204. siocb->scm = &scm;
  1205. }
  1206. siocb->scm->creds = *NETLINK_CREDS(skb);
  1207. if (flags & MSG_TRUNC)
  1208. copied = skb->len;
  1209. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1210. skb_shinfo(skb)->frag_list = frag;
  1211. #endif
  1212. skb_free_datagram(sk, skb);
  1213. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  1214. netlink_dump(sk);
  1215. scm_recv(sock, msg, siocb->scm, flags);
  1216. out:
  1217. netlink_rcv_wake(sk);
  1218. return err ? : copied;
  1219. }
  1220. static void netlink_data_ready(struct sock *sk, int len)
  1221. {
  1222. BUG();
  1223. }
  1224. /*
  1225. * We export these functions to other modules. They provide a
  1226. * complete set of kernel non-blocking support for message
  1227. * queueing.
  1228. */
  1229. struct sock *
  1230. netlink_kernel_create(struct net *net, int unit, unsigned int groups,
  1231. void (*input)(struct sk_buff *skb),
  1232. struct mutex *cb_mutex, struct module *module)
  1233. {
  1234. struct socket *sock;
  1235. struct sock *sk;
  1236. struct netlink_sock *nlk;
  1237. unsigned long *listeners = NULL;
  1238. BUG_ON(!nl_table);
  1239. if (unit < 0 || unit >= MAX_LINKS)
  1240. return NULL;
  1241. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1242. return NULL;
  1243. /*
  1244. * We have to just have a reference on the net from sk, but don't
  1245. * get_net it. Besides, we cannot get and then put the net here.
  1246. * So we create one inside init_net and the move it to net.
  1247. */
  1248. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1249. goto out_sock_release_nosk;
  1250. sk = sock->sk;
  1251. sk_change_net(sk, net);
  1252. if (groups < 32)
  1253. groups = 32;
  1254. listeners = kzalloc(NLGRPSZ(groups) + sizeof(struct listeners_rcu_head),
  1255. GFP_KERNEL);
  1256. if (!listeners)
  1257. goto out_sock_release;
  1258. sk->sk_data_ready = netlink_data_ready;
  1259. if (input)
  1260. nlk_sk(sk)->netlink_rcv = input;
  1261. if (netlink_insert(sk, net, 0))
  1262. goto out_sock_release;
  1263. nlk = nlk_sk(sk);
  1264. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1265. netlink_table_grab();
  1266. if (!nl_table[unit].registered) {
  1267. nl_table[unit].groups = groups;
  1268. nl_table[unit].listeners = listeners;
  1269. nl_table[unit].cb_mutex = cb_mutex;
  1270. nl_table[unit].module = module;
  1271. nl_table[unit].registered = 1;
  1272. } else {
  1273. kfree(listeners);
  1274. nl_table[unit].registered++;
  1275. }
  1276. netlink_table_ungrab();
  1277. return sk;
  1278. out_sock_release:
  1279. kfree(listeners);
  1280. netlink_kernel_release(sk);
  1281. return NULL;
  1282. out_sock_release_nosk:
  1283. sock_release(sock);
  1284. return NULL;
  1285. }
  1286. EXPORT_SYMBOL(netlink_kernel_create);
  1287. void
  1288. netlink_kernel_release(struct sock *sk)
  1289. {
  1290. sk_release_kernel(sk);
  1291. }
  1292. EXPORT_SYMBOL(netlink_kernel_release);
  1293. static void netlink_free_old_listeners(struct rcu_head *rcu_head)
  1294. {
  1295. struct listeners_rcu_head *lrh;
  1296. lrh = container_of(rcu_head, struct listeners_rcu_head, rcu_head);
  1297. kfree(lrh->ptr);
  1298. }
  1299. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1300. {
  1301. unsigned long *listeners, *old = NULL;
  1302. struct listeners_rcu_head *old_rcu_head;
  1303. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1304. if (groups < 32)
  1305. groups = 32;
  1306. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1307. listeners = kzalloc(NLGRPSZ(groups) +
  1308. sizeof(struct listeners_rcu_head),
  1309. GFP_ATOMIC);
  1310. if (!listeners)
  1311. return -ENOMEM;
  1312. old = tbl->listeners;
  1313. memcpy(listeners, old, NLGRPSZ(tbl->groups));
  1314. rcu_assign_pointer(tbl->listeners, listeners);
  1315. /*
  1316. * Free the old memory after an RCU grace period so we
  1317. * don't leak it. We use call_rcu() here in order to be
  1318. * able to call this function from atomic contexts. The
  1319. * allocation of this memory will have reserved enough
  1320. * space for struct listeners_rcu_head at the end.
  1321. */
  1322. old_rcu_head = (void *)(tbl->listeners +
  1323. NLGRPLONGS(tbl->groups));
  1324. old_rcu_head->ptr = old;
  1325. call_rcu(&old_rcu_head->rcu_head, netlink_free_old_listeners);
  1326. }
  1327. tbl->groups = groups;
  1328. return 0;
  1329. }
  1330. /**
  1331. * netlink_change_ngroups - change number of multicast groups
  1332. *
  1333. * This changes the number of multicast groups that are available
  1334. * on a certain netlink family. Note that it is not possible to
  1335. * change the number of groups to below 32. Also note that it does
  1336. * not implicitly call netlink_clear_multicast_users() when the
  1337. * number of groups is reduced.
  1338. *
  1339. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1340. * @groups: The new number of groups.
  1341. */
  1342. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1343. {
  1344. int err;
  1345. netlink_table_grab();
  1346. err = __netlink_change_ngroups(sk, groups);
  1347. netlink_table_ungrab();
  1348. return err;
  1349. }
  1350. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1351. {
  1352. struct sock *sk;
  1353. struct hlist_node *node;
  1354. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1355. sk_for_each_bound(sk, node, &tbl->mc_list)
  1356. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1357. }
  1358. /**
  1359. * netlink_clear_multicast_users - kick off multicast listeners
  1360. *
  1361. * This function removes all listeners from the given group.
  1362. * @ksk: The kernel netlink socket, as returned by
  1363. * netlink_kernel_create().
  1364. * @group: The multicast group to clear.
  1365. */
  1366. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1367. {
  1368. netlink_table_grab();
  1369. __netlink_clear_multicast_users(ksk, group);
  1370. netlink_table_ungrab();
  1371. }
  1372. void netlink_set_nonroot(int protocol, unsigned int flags)
  1373. {
  1374. if ((unsigned int)protocol < MAX_LINKS)
  1375. nl_table[protocol].nl_nonroot = flags;
  1376. }
  1377. EXPORT_SYMBOL(netlink_set_nonroot);
  1378. static void netlink_destroy_callback(struct netlink_callback *cb)
  1379. {
  1380. kfree_skb(cb->skb);
  1381. kfree(cb);
  1382. }
  1383. /*
  1384. * It looks a bit ugly.
  1385. * It would be better to create kernel thread.
  1386. */
  1387. static int netlink_dump(struct sock *sk)
  1388. {
  1389. struct netlink_sock *nlk = nlk_sk(sk);
  1390. struct netlink_callback *cb;
  1391. struct sk_buff *skb;
  1392. struct nlmsghdr *nlh;
  1393. int len, err = -ENOBUFS;
  1394. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1395. if (!skb)
  1396. goto errout;
  1397. mutex_lock(nlk->cb_mutex);
  1398. cb = nlk->cb;
  1399. if (cb == NULL) {
  1400. err = -EINVAL;
  1401. goto errout_skb;
  1402. }
  1403. len = cb->dump(skb, cb);
  1404. if (len > 0) {
  1405. mutex_unlock(nlk->cb_mutex);
  1406. if (sk_filter(sk, skb))
  1407. kfree_skb(skb);
  1408. else {
  1409. skb_queue_tail(&sk->sk_receive_queue, skb);
  1410. sk->sk_data_ready(sk, skb->len);
  1411. }
  1412. return 0;
  1413. }
  1414. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1415. if (!nlh)
  1416. goto errout_skb;
  1417. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1418. if (sk_filter(sk, skb))
  1419. kfree_skb(skb);
  1420. else {
  1421. skb_queue_tail(&sk->sk_receive_queue, skb);
  1422. sk->sk_data_ready(sk, skb->len);
  1423. }
  1424. if (cb->done)
  1425. cb->done(cb);
  1426. nlk->cb = NULL;
  1427. mutex_unlock(nlk->cb_mutex);
  1428. netlink_destroy_callback(cb);
  1429. return 0;
  1430. errout_skb:
  1431. mutex_unlock(nlk->cb_mutex);
  1432. kfree_skb(skb);
  1433. errout:
  1434. return err;
  1435. }
  1436. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1437. const struct nlmsghdr *nlh,
  1438. int (*dump)(struct sk_buff *skb,
  1439. struct netlink_callback *),
  1440. int (*done)(struct netlink_callback *))
  1441. {
  1442. struct netlink_callback *cb;
  1443. struct sock *sk;
  1444. struct netlink_sock *nlk;
  1445. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1446. if (cb == NULL)
  1447. return -ENOBUFS;
  1448. cb->dump = dump;
  1449. cb->done = done;
  1450. cb->nlh = nlh;
  1451. atomic_inc(&skb->users);
  1452. cb->skb = skb;
  1453. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
  1454. if (sk == NULL) {
  1455. netlink_destroy_callback(cb);
  1456. return -ECONNREFUSED;
  1457. }
  1458. nlk = nlk_sk(sk);
  1459. /* A dump is in progress... */
  1460. mutex_lock(nlk->cb_mutex);
  1461. if (nlk->cb) {
  1462. mutex_unlock(nlk->cb_mutex);
  1463. netlink_destroy_callback(cb);
  1464. sock_put(sk);
  1465. return -EBUSY;
  1466. }
  1467. nlk->cb = cb;
  1468. mutex_unlock(nlk->cb_mutex);
  1469. netlink_dump(sk);
  1470. sock_put(sk);
  1471. /* We successfully started a dump, by returning -EINTR we
  1472. * signal not to send ACK even if it was requested.
  1473. */
  1474. return -EINTR;
  1475. }
  1476. EXPORT_SYMBOL(netlink_dump_start);
  1477. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1478. {
  1479. struct sk_buff *skb;
  1480. struct nlmsghdr *rep;
  1481. struct nlmsgerr *errmsg;
  1482. size_t payload = sizeof(*errmsg);
  1483. /* error messages get the original request appened */
  1484. if (err)
  1485. payload += nlmsg_len(nlh);
  1486. skb = nlmsg_new(payload, GFP_KERNEL);
  1487. if (!skb) {
  1488. struct sock *sk;
  1489. sk = netlink_lookup(sock_net(in_skb->sk),
  1490. in_skb->sk->sk_protocol,
  1491. NETLINK_CB(in_skb).pid);
  1492. if (sk) {
  1493. sk->sk_err = ENOBUFS;
  1494. sk->sk_error_report(sk);
  1495. sock_put(sk);
  1496. }
  1497. return;
  1498. }
  1499. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1500. NLMSG_ERROR, payload, 0);
  1501. errmsg = nlmsg_data(rep);
  1502. errmsg->error = err;
  1503. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1504. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1505. }
  1506. EXPORT_SYMBOL(netlink_ack);
  1507. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1508. struct nlmsghdr *))
  1509. {
  1510. struct nlmsghdr *nlh;
  1511. int err;
  1512. while (skb->len >= nlmsg_total_size(0)) {
  1513. int msglen;
  1514. nlh = nlmsg_hdr(skb);
  1515. err = 0;
  1516. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1517. return 0;
  1518. /* Only requests are handled by the kernel */
  1519. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1520. goto ack;
  1521. /* Skip control messages */
  1522. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1523. goto ack;
  1524. err = cb(skb, nlh);
  1525. if (err == -EINTR)
  1526. goto skip;
  1527. ack:
  1528. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1529. netlink_ack(skb, nlh, err);
  1530. skip:
  1531. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1532. if (msglen > skb->len)
  1533. msglen = skb->len;
  1534. skb_pull(skb, msglen);
  1535. }
  1536. return 0;
  1537. }
  1538. EXPORT_SYMBOL(netlink_rcv_skb);
  1539. /**
  1540. * nlmsg_notify - send a notification netlink message
  1541. * @sk: netlink socket to use
  1542. * @skb: notification message
  1543. * @pid: destination netlink pid for reports or 0
  1544. * @group: destination multicast group or 0
  1545. * @report: 1 to report back, 0 to disable
  1546. * @flags: allocation flags
  1547. */
  1548. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
  1549. unsigned int group, int report, gfp_t flags)
  1550. {
  1551. int err = 0;
  1552. if (group) {
  1553. int exclude_pid = 0;
  1554. if (report) {
  1555. atomic_inc(&skb->users);
  1556. exclude_pid = pid;
  1557. }
  1558. /* errors reported via destination sk->sk_err, but propagate
  1559. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1560. err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
  1561. }
  1562. if (report) {
  1563. int err2;
  1564. err2 = nlmsg_unicast(sk, skb, pid);
  1565. if (!err || err == -ESRCH)
  1566. err = err2;
  1567. }
  1568. return err;
  1569. }
  1570. EXPORT_SYMBOL(nlmsg_notify);
  1571. #ifdef CONFIG_PROC_FS
  1572. struct nl_seq_iter {
  1573. struct seq_net_private p;
  1574. int link;
  1575. int hash_idx;
  1576. };
  1577. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1578. {
  1579. struct nl_seq_iter *iter = seq->private;
  1580. int i, j;
  1581. struct sock *s;
  1582. struct hlist_node *node;
  1583. loff_t off = 0;
  1584. for (i = 0; i < MAX_LINKS; i++) {
  1585. struct nl_pid_hash *hash = &nl_table[i].hash;
  1586. for (j = 0; j <= hash->mask; j++) {
  1587. sk_for_each(s, node, &hash->table[j]) {
  1588. if (sock_net(s) != seq_file_net(seq))
  1589. continue;
  1590. if (off == pos) {
  1591. iter->link = i;
  1592. iter->hash_idx = j;
  1593. return s;
  1594. }
  1595. ++off;
  1596. }
  1597. }
  1598. }
  1599. return NULL;
  1600. }
  1601. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1602. __acquires(nl_table_lock)
  1603. {
  1604. read_lock(&nl_table_lock);
  1605. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1606. }
  1607. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1608. {
  1609. struct sock *s;
  1610. struct nl_seq_iter *iter;
  1611. int i, j;
  1612. ++*pos;
  1613. if (v == SEQ_START_TOKEN)
  1614. return netlink_seq_socket_idx(seq, 0);
  1615. iter = seq->private;
  1616. s = v;
  1617. do {
  1618. s = sk_next(s);
  1619. } while (s && sock_net(s) != seq_file_net(seq));
  1620. if (s)
  1621. return s;
  1622. i = iter->link;
  1623. j = iter->hash_idx + 1;
  1624. do {
  1625. struct nl_pid_hash *hash = &nl_table[i].hash;
  1626. for (; j <= hash->mask; j++) {
  1627. s = sk_head(&hash->table[j]);
  1628. while (s && sock_net(s) != seq_file_net(seq))
  1629. s = sk_next(s);
  1630. if (s) {
  1631. iter->link = i;
  1632. iter->hash_idx = j;
  1633. return s;
  1634. }
  1635. }
  1636. j = 0;
  1637. } while (++i < MAX_LINKS);
  1638. return NULL;
  1639. }
  1640. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1641. __releases(nl_table_lock)
  1642. {
  1643. read_unlock(&nl_table_lock);
  1644. }
  1645. static int netlink_seq_show(struct seq_file *seq, void *v)
  1646. {
  1647. if (v == SEQ_START_TOKEN)
  1648. seq_puts(seq,
  1649. "sk Eth Pid Groups "
  1650. "Rmem Wmem Dump Locks Drops\n");
  1651. else {
  1652. struct sock *s = v;
  1653. struct netlink_sock *nlk = nlk_sk(s);
  1654. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %-8d %-8d\n",
  1655. s,
  1656. s->sk_protocol,
  1657. nlk->pid,
  1658. nlk->groups ? (u32)nlk->groups[0] : 0,
  1659. sk_rmem_alloc_get(s),
  1660. sk_wmem_alloc_get(s),
  1661. nlk->cb,
  1662. atomic_read(&s->sk_refcnt),
  1663. atomic_read(&s->sk_drops)
  1664. );
  1665. }
  1666. return 0;
  1667. }
  1668. static const struct seq_operations netlink_seq_ops = {
  1669. .start = netlink_seq_start,
  1670. .next = netlink_seq_next,
  1671. .stop = netlink_seq_stop,
  1672. .show = netlink_seq_show,
  1673. };
  1674. static int netlink_seq_open(struct inode *inode, struct file *file)
  1675. {
  1676. return seq_open_net(inode, file, &netlink_seq_ops,
  1677. sizeof(struct nl_seq_iter));
  1678. }
  1679. static const struct file_operations netlink_seq_fops = {
  1680. .owner = THIS_MODULE,
  1681. .open = netlink_seq_open,
  1682. .read = seq_read,
  1683. .llseek = seq_lseek,
  1684. .release = seq_release_net,
  1685. };
  1686. #endif
  1687. int netlink_register_notifier(struct notifier_block *nb)
  1688. {
  1689. return atomic_notifier_chain_register(&netlink_chain, nb);
  1690. }
  1691. EXPORT_SYMBOL(netlink_register_notifier);
  1692. int netlink_unregister_notifier(struct notifier_block *nb)
  1693. {
  1694. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1695. }
  1696. EXPORT_SYMBOL(netlink_unregister_notifier);
  1697. static const struct proto_ops netlink_ops = {
  1698. .family = PF_NETLINK,
  1699. .owner = THIS_MODULE,
  1700. .release = netlink_release,
  1701. .bind = netlink_bind,
  1702. .connect = netlink_connect,
  1703. .socketpair = sock_no_socketpair,
  1704. .accept = sock_no_accept,
  1705. .getname = netlink_getname,
  1706. .poll = datagram_poll,
  1707. .ioctl = sock_no_ioctl,
  1708. .listen = sock_no_listen,
  1709. .shutdown = sock_no_shutdown,
  1710. .setsockopt = netlink_setsockopt,
  1711. .getsockopt = netlink_getsockopt,
  1712. .sendmsg = netlink_sendmsg,
  1713. .recvmsg = netlink_recvmsg,
  1714. .mmap = sock_no_mmap,
  1715. .sendpage = sock_no_sendpage,
  1716. };
  1717. static struct net_proto_family netlink_family_ops = {
  1718. .family = PF_NETLINK,
  1719. .create = netlink_create,
  1720. .owner = THIS_MODULE, /* for consistency 8) */
  1721. };
  1722. static int __net_init netlink_net_init(struct net *net)
  1723. {
  1724. #ifdef CONFIG_PROC_FS
  1725. if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
  1726. return -ENOMEM;
  1727. #endif
  1728. return 0;
  1729. }
  1730. static void __net_exit netlink_net_exit(struct net *net)
  1731. {
  1732. #ifdef CONFIG_PROC_FS
  1733. proc_net_remove(net, "netlink");
  1734. #endif
  1735. }
  1736. static struct pernet_operations __net_initdata netlink_net_ops = {
  1737. .init = netlink_net_init,
  1738. .exit = netlink_net_exit,
  1739. };
  1740. static int __init netlink_proto_init(void)
  1741. {
  1742. struct sk_buff *dummy_skb;
  1743. int i;
  1744. unsigned long limit;
  1745. unsigned int order;
  1746. int err = proto_register(&netlink_proto, 0);
  1747. if (err != 0)
  1748. goto out;
  1749. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
  1750. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1751. if (!nl_table)
  1752. goto panic;
  1753. if (totalram_pages >= (128 * 1024))
  1754. limit = totalram_pages >> (21 - PAGE_SHIFT);
  1755. else
  1756. limit = totalram_pages >> (23 - PAGE_SHIFT);
  1757. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1758. limit = (1UL << order) / sizeof(struct hlist_head);
  1759. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1760. for (i = 0; i < MAX_LINKS; i++) {
  1761. struct nl_pid_hash *hash = &nl_table[i].hash;
  1762. hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
  1763. if (!hash->table) {
  1764. while (i-- > 0)
  1765. nl_pid_hash_free(nl_table[i].hash.table,
  1766. 1 * sizeof(*hash->table));
  1767. kfree(nl_table);
  1768. goto panic;
  1769. }
  1770. hash->max_shift = order;
  1771. hash->shift = 0;
  1772. hash->mask = 0;
  1773. hash->rehash_time = jiffies;
  1774. }
  1775. sock_register(&netlink_family_ops);
  1776. register_pernet_subsys(&netlink_net_ops);
  1777. /* The netlink device handler may be needed early. */
  1778. rtnetlink_init();
  1779. out:
  1780. return err;
  1781. panic:
  1782. panic("netlink_init: Cannot allocate nl_table\n");
  1783. }
  1784. core_initcall(netlink_proto_init);