af_netlink.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  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. 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. 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. sock_put(sk);
  701. return len;
  702. }
  703. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  704. {
  705. kfree_skb(skb);
  706. sock_put(sk);
  707. }
  708. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  709. gfp_t allocation)
  710. {
  711. int delta;
  712. skb_orphan(skb);
  713. delta = skb->end - skb->tail;
  714. if (delta * 2 < skb->truesize)
  715. return skb;
  716. if (skb_shared(skb)) {
  717. struct sk_buff *nskb = skb_clone(skb, allocation);
  718. if (!nskb)
  719. return skb;
  720. kfree_skb(skb);
  721. skb = nskb;
  722. }
  723. if (!pskb_expand_head(skb, 0, -delta, allocation))
  724. skb->truesize -= delta;
  725. return skb;
  726. }
  727. static inline void netlink_rcv_wake(struct sock *sk)
  728. {
  729. struct netlink_sock *nlk = nlk_sk(sk);
  730. if (skb_queue_empty(&sk->sk_receive_queue))
  731. clear_bit(0, &nlk->state);
  732. if (!test_bit(0, &nlk->state))
  733. wake_up_interruptible(&nlk->wait);
  734. }
  735. static inline int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb)
  736. {
  737. int ret;
  738. struct netlink_sock *nlk = nlk_sk(sk);
  739. ret = -ECONNREFUSED;
  740. if (nlk->netlink_rcv != NULL) {
  741. ret = skb->len;
  742. skb_set_owner_r(skb, sk);
  743. nlk->netlink_rcv(skb);
  744. }
  745. kfree_skb(skb);
  746. sock_put(sk);
  747. return ret;
  748. }
  749. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  750. u32 pid, int nonblock)
  751. {
  752. struct sock *sk;
  753. int err;
  754. long timeo;
  755. skb = netlink_trim(skb, gfp_any());
  756. timeo = sock_sndtimeo(ssk, nonblock);
  757. retry:
  758. sk = netlink_getsockbypid(ssk, pid);
  759. if (IS_ERR(sk)) {
  760. kfree_skb(skb);
  761. return PTR_ERR(sk);
  762. }
  763. if (netlink_is_kernel(sk))
  764. return netlink_unicast_kernel(sk, skb);
  765. if (sk_filter(sk, skb)) {
  766. err = skb->len;
  767. kfree_skb(skb);
  768. sock_put(sk);
  769. return err;
  770. }
  771. err = netlink_attachskb(sk, skb, &timeo, ssk);
  772. if (err == 1)
  773. goto retry;
  774. if (err)
  775. return err;
  776. return netlink_sendskb(sk, skb);
  777. }
  778. EXPORT_SYMBOL(netlink_unicast);
  779. int netlink_has_listeners(struct sock *sk, unsigned int group)
  780. {
  781. int res = 0;
  782. unsigned long *listeners;
  783. BUG_ON(!netlink_is_kernel(sk));
  784. rcu_read_lock();
  785. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  786. if (group - 1 < nl_table[sk->sk_protocol].groups)
  787. res = test_bit(group - 1, listeners);
  788. rcu_read_unlock();
  789. return res;
  790. }
  791. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  792. static inline int netlink_broadcast_deliver(struct sock *sk,
  793. struct sk_buff *skb)
  794. {
  795. struct netlink_sock *nlk = nlk_sk(sk);
  796. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  797. !test_bit(0, &nlk->state)) {
  798. skb_set_owner_r(skb, sk);
  799. skb_queue_tail(&sk->sk_receive_queue, skb);
  800. sk->sk_data_ready(sk, skb->len);
  801. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  802. }
  803. return -1;
  804. }
  805. struct netlink_broadcast_data {
  806. struct sock *exclude_sk;
  807. struct net *net;
  808. u32 pid;
  809. u32 group;
  810. int failure;
  811. int delivery_failure;
  812. int congested;
  813. int delivered;
  814. gfp_t allocation;
  815. struct sk_buff *skb, *skb2;
  816. };
  817. static inline int do_one_broadcast(struct sock *sk,
  818. struct netlink_broadcast_data *p)
  819. {
  820. struct netlink_sock *nlk = nlk_sk(sk);
  821. int val;
  822. if (p->exclude_sk == sk)
  823. goto out;
  824. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  825. !test_bit(p->group - 1, nlk->groups))
  826. goto out;
  827. if (!net_eq(sock_net(sk), p->net))
  828. goto out;
  829. if (p->failure) {
  830. netlink_overrun(sk);
  831. goto out;
  832. }
  833. sock_hold(sk);
  834. if (p->skb2 == NULL) {
  835. if (skb_shared(p->skb)) {
  836. p->skb2 = skb_clone(p->skb, p->allocation);
  837. } else {
  838. p->skb2 = skb_get(p->skb);
  839. /*
  840. * skb ownership may have been set when
  841. * delivered to a previous socket.
  842. */
  843. skb_orphan(p->skb2);
  844. }
  845. }
  846. if (p->skb2 == NULL) {
  847. netlink_overrun(sk);
  848. /* Clone failed. Notify ALL listeners. */
  849. p->failure = 1;
  850. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  851. p->delivery_failure = 1;
  852. } else if (sk_filter(sk, p->skb2)) {
  853. kfree_skb(p->skb2);
  854. p->skb2 = NULL;
  855. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  856. netlink_overrun(sk);
  857. if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
  858. p->delivery_failure = 1;
  859. } else {
  860. p->congested |= val;
  861. p->delivered = 1;
  862. p->skb2 = NULL;
  863. }
  864. sock_put(sk);
  865. out:
  866. return 0;
  867. }
  868. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  869. u32 group, gfp_t allocation)
  870. {
  871. struct net *net = sock_net(ssk);
  872. struct netlink_broadcast_data info;
  873. struct hlist_node *node;
  874. struct sock *sk;
  875. skb = netlink_trim(skb, allocation);
  876. info.exclude_sk = ssk;
  877. info.net = net;
  878. info.pid = pid;
  879. info.group = group;
  880. info.failure = 0;
  881. info.delivery_failure = 0;
  882. info.congested = 0;
  883. info.delivered = 0;
  884. info.allocation = allocation;
  885. info.skb = skb;
  886. info.skb2 = NULL;
  887. /* While we sleep in clone, do not allow to change socket list */
  888. netlink_lock_table();
  889. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  890. do_one_broadcast(sk, &info);
  891. kfree_skb(skb);
  892. netlink_unlock_table();
  893. kfree_skb(info.skb2);
  894. if (info.delivery_failure)
  895. return -ENOBUFS;
  896. if (info.delivered) {
  897. if (info.congested && (allocation & __GFP_WAIT))
  898. yield();
  899. return 0;
  900. }
  901. return -ESRCH;
  902. }
  903. EXPORT_SYMBOL(netlink_broadcast);
  904. struct netlink_set_err_data {
  905. struct sock *exclude_sk;
  906. u32 pid;
  907. u32 group;
  908. int code;
  909. };
  910. static inline int do_one_set_err(struct sock *sk,
  911. struct netlink_set_err_data *p)
  912. {
  913. struct netlink_sock *nlk = nlk_sk(sk);
  914. int ret = 0;
  915. if (sk == p->exclude_sk)
  916. goto out;
  917. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  918. goto out;
  919. if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
  920. !test_bit(p->group - 1, nlk->groups))
  921. goto out;
  922. if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
  923. ret = 1;
  924. goto out;
  925. }
  926. sk->sk_err = p->code;
  927. sk->sk_error_report(sk);
  928. out:
  929. return ret;
  930. }
  931. /**
  932. * netlink_set_err - report error to broadcast listeners
  933. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  934. * @pid: the PID of a process that we want to skip (if any)
  935. * @groups: the broadcast group that will notice the error
  936. * @code: error code, must be negative (as usual in kernelspace)
  937. *
  938. * This function returns the number of broadcast listeners that have set the
  939. * NETLINK_RECV_NO_ENOBUFS socket option.
  940. */
  941. int netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  942. {
  943. struct netlink_set_err_data info;
  944. struct hlist_node *node;
  945. struct sock *sk;
  946. int ret = 0;
  947. info.exclude_sk = ssk;
  948. info.pid = pid;
  949. info.group = group;
  950. /* sk->sk_err wants a positive error value */
  951. info.code = -code;
  952. read_lock(&nl_table_lock);
  953. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  954. ret += do_one_set_err(sk, &info);
  955. read_unlock(&nl_table_lock);
  956. return ret;
  957. }
  958. EXPORT_SYMBOL(netlink_set_err);
  959. /* must be called with netlink table grabbed */
  960. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  961. unsigned int group,
  962. int is_new)
  963. {
  964. int old, new = !!is_new, subscriptions;
  965. old = test_bit(group - 1, nlk->groups);
  966. subscriptions = nlk->subscriptions - old + new;
  967. if (new)
  968. __set_bit(group - 1, nlk->groups);
  969. else
  970. __clear_bit(group - 1, nlk->groups);
  971. netlink_update_subscriptions(&nlk->sk, subscriptions);
  972. netlink_update_listeners(&nlk->sk);
  973. }
  974. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  975. char __user *optval, unsigned int optlen)
  976. {
  977. struct sock *sk = sock->sk;
  978. struct netlink_sock *nlk = nlk_sk(sk);
  979. unsigned int val = 0;
  980. int err;
  981. if (level != SOL_NETLINK)
  982. return -ENOPROTOOPT;
  983. if (optlen >= sizeof(int) &&
  984. get_user(val, (unsigned int __user *)optval))
  985. return -EFAULT;
  986. switch (optname) {
  987. case NETLINK_PKTINFO:
  988. if (val)
  989. nlk->flags |= NETLINK_RECV_PKTINFO;
  990. else
  991. nlk->flags &= ~NETLINK_RECV_PKTINFO;
  992. err = 0;
  993. break;
  994. case NETLINK_ADD_MEMBERSHIP:
  995. case NETLINK_DROP_MEMBERSHIP: {
  996. if (!netlink_capable(sock, NL_NONROOT_RECV))
  997. return -EPERM;
  998. err = netlink_realloc_groups(sk);
  999. if (err)
  1000. return err;
  1001. if (!val || val - 1 >= nlk->ngroups)
  1002. return -EINVAL;
  1003. netlink_table_grab();
  1004. netlink_update_socket_mc(nlk, val,
  1005. optname == NETLINK_ADD_MEMBERSHIP);
  1006. netlink_table_ungrab();
  1007. err = 0;
  1008. break;
  1009. }
  1010. case NETLINK_BROADCAST_ERROR:
  1011. if (val)
  1012. nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
  1013. else
  1014. nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
  1015. err = 0;
  1016. break;
  1017. case NETLINK_NO_ENOBUFS:
  1018. if (val) {
  1019. nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
  1020. clear_bit(0, &nlk->state);
  1021. wake_up_interruptible(&nlk->wait);
  1022. } else
  1023. nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
  1024. err = 0;
  1025. break;
  1026. default:
  1027. err = -ENOPROTOOPT;
  1028. }
  1029. return err;
  1030. }
  1031. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1032. char __user *optval, int __user *optlen)
  1033. {
  1034. struct sock *sk = sock->sk;
  1035. struct netlink_sock *nlk = nlk_sk(sk);
  1036. int len, val, err;
  1037. if (level != SOL_NETLINK)
  1038. return -ENOPROTOOPT;
  1039. if (get_user(len, optlen))
  1040. return -EFAULT;
  1041. if (len < 0)
  1042. return -EINVAL;
  1043. switch (optname) {
  1044. case NETLINK_PKTINFO:
  1045. if (len < sizeof(int))
  1046. return -EINVAL;
  1047. len = sizeof(int);
  1048. val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
  1049. if (put_user(len, optlen) ||
  1050. put_user(val, optval))
  1051. return -EFAULT;
  1052. err = 0;
  1053. break;
  1054. case NETLINK_BROADCAST_ERROR:
  1055. if (len < sizeof(int))
  1056. return -EINVAL;
  1057. len = sizeof(int);
  1058. val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
  1059. if (put_user(len, optlen) ||
  1060. put_user(val, optval))
  1061. return -EFAULT;
  1062. err = 0;
  1063. break;
  1064. case NETLINK_NO_ENOBUFS:
  1065. if (len < sizeof(int))
  1066. return -EINVAL;
  1067. len = sizeof(int);
  1068. val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
  1069. if (put_user(len, optlen) ||
  1070. put_user(val, optval))
  1071. return -EFAULT;
  1072. err = 0;
  1073. break;
  1074. default:
  1075. err = -ENOPROTOOPT;
  1076. }
  1077. return err;
  1078. }
  1079. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1080. {
  1081. struct nl_pktinfo info;
  1082. info.group = NETLINK_CB(skb).dst_group;
  1083. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1084. }
  1085. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  1086. struct msghdr *msg, size_t len)
  1087. {
  1088. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1089. struct sock *sk = sock->sk;
  1090. struct netlink_sock *nlk = nlk_sk(sk);
  1091. struct sockaddr_nl *addr = msg->msg_name;
  1092. u32 dst_pid;
  1093. u32 dst_group;
  1094. struct sk_buff *skb;
  1095. int err;
  1096. struct scm_cookie scm;
  1097. if (msg->msg_flags&MSG_OOB)
  1098. return -EOPNOTSUPP;
  1099. if (NULL == siocb->scm)
  1100. siocb->scm = &scm;
  1101. err = scm_send(sock, msg, siocb->scm);
  1102. if (err < 0)
  1103. return err;
  1104. if (msg->msg_namelen) {
  1105. if (addr->nl_family != AF_NETLINK)
  1106. return -EINVAL;
  1107. dst_pid = addr->nl_pid;
  1108. dst_group = ffs(addr->nl_groups);
  1109. if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND))
  1110. return -EPERM;
  1111. } else {
  1112. dst_pid = nlk->dst_pid;
  1113. dst_group = nlk->dst_group;
  1114. }
  1115. if (!nlk->pid) {
  1116. err = netlink_autobind(sock);
  1117. if (err)
  1118. goto out;
  1119. }
  1120. err = -EMSGSIZE;
  1121. if (len > sk->sk_sndbuf - 32)
  1122. goto out;
  1123. err = -ENOBUFS;
  1124. skb = alloc_skb(len, GFP_KERNEL);
  1125. if (skb == NULL)
  1126. goto out;
  1127. NETLINK_CB(skb).pid = nlk->pid;
  1128. NETLINK_CB(skb).dst_group = dst_group;
  1129. NETLINK_CB(skb).loginuid = audit_get_loginuid(current);
  1130. NETLINK_CB(skb).sessionid = audit_get_sessionid(current);
  1131. security_task_getsecid(current, &(NETLINK_CB(skb).sid));
  1132. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  1133. /* What can I do? Netlink is asynchronous, so that
  1134. we will have to save current capabilities to
  1135. check them, when this message will be delivered
  1136. to corresponding kernel module. --ANK (980802)
  1137. */
  1138. err = -EFAULT;
  1139. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1140. kfree_skb(skb);
  1141. goto out;
  1142. }
  1143. err = security_netlink_send(sk, skb);
  1144. if (err) {
  1145. kfree_skb(skb);
  1146. goto out;
  1147. }
  1148. if (dst_group) {
  1149. atomic_inc(&skb->users);
  1150. netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL);
  1151. }
  1152. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  1153. out:
  1154. return err;
  1155. }
  1156. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  1157. struct msghdr *msg, size_t len,
  1158. int flags)
  1159. {
  1160. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  1161. struct scm_cookie scm;
  1162. struct sock *sk = sock->sk;
  1163. struct netlink_sock *nlk = nlk_sk(sk);
  1164. int noblock = flags&MSG_DONTWAIT;
  1165. size_t copied;
  1166. struct sk_buff *skb, *frag __maybe_unused = NULL;
  1167. int err;
  1168. if (flags&MSG_OOB)
  1169. return -EOPNOTSUPP;
  1170. copied = 0;
  1171. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1172. if (skb == NULL)
  1173. goto out;
  1174. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1175. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1176. bool need_compat = !!(flags & MSG_CMSG_COMPAT);
  1177. /*
  1178. * If this skb has a frag_list, then here that means that
  1179. * we will have to use the frag_list skb for compat tasks
  1180. * and the regular skb for non-compat tasks.
  1181. *
  1182. * The skb might (and likely will) be cloned, so we can't
  1183. * just reset frag_list and go on with things -- we need to
  1184. * keep that. For the compat case that's easy -- simply get
  1185. * a reference to the compat skb and free the regular one
  1186. * including the frag. For the non-compat case, we need to
  1187. * avoid sending the frag to the user -- so assign NULL but
  1188. * restore it below before freeing the skb.
  1189. */
  1190. if (need_compat) {
  1191. struct sk_buff *compskb = skb_shinfo(skb)->frag_list;
  1192. skb_get(compskb);
  1193. kfree_skb(skb);
  1194. skb = compskb;
  1195. } else {
  1196. frag = skb_shinfo(skb)->frag_list;
  1197. skb_shinfo(skb)->frag_list = NULL;
  1198. }
  1199. }
  1200. #endif
  1201. msg->msg_namelen = 0;
  1202. copied = skb->len;
  1203. if (len < copied) {
  1204. msg->msg_flags |= MSG_TRUNC;
  1205. copied = len;
  1206. }
  1207. skb_reset_transport_header(skb);
  1208. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1209. if (msg->msg_name) {
  1210. struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
  1211. addr->nl_family = AF_NETLINK;
  1212. addr->nl_pad = 0;
  1213. addr->nl_pid = NETLINK_CB(skb).pid;
  1214. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1215. msg->msg_namelen = sizeof(*addr);
  1216. }
  1217. if (nlk->flags & NETLINK_RECV_PKTINFO)
  1218. netlink_cmsg_recv_pktinfo(msg, skb);
  1219. if (NULL == siocb->scm) {
  1220. memset(&scm, 0, sizeof(scm));
  1221. siocb->scm = &scm;
  1222. }
  1223. siocb->scm->creds = *NETLINK_CREDS(skb);
  1224. if (flags & MSG_TRUNC)
  1225. copied = skb->len;
  1226. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1227. skb_shinfo(skb)->frag_list = frag;
  1228. #endif
  1229. skb_free_datagram(sk, skb);
  1230. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  1231. netlink_dump(sk);
  1232. scm_recv(sock, msg, siocb->scm, flags);
  1233. out:
  1234. netlink_rcv_wake(sk);
  1235. return err ? : copied;
  1236. }
  1237. static void netlink_data_ready(struct sock *sk, int len)
  1238. {
  1239. BUG();
  1240. }
  1241. /*
  1242. * We export these functions to other modules. They provide a
  1243. * complete set of kernel non-blocking support for message
  1244. * queueing.
  1245. */
  1246. struct sock *
  1247. netlink_kernel_create(struct net *net, int unit, unsigned int groups,
  1248. void (*input)(struct sk_buff *skb),
  1249. struct mutex *cb_mutex, struct module *module)
  1250. {
  1251. struct socket *sock;
  1252. struct sock *sk;
  1253. struct netlink_sock *nlk;
  1254. unsigned long *listeners = NULL;
  1255. BUG_ON(!nl_table);
  1256. if (unit < 0 || unit >= MAX_LINKS)
  1257. return NULL;
  1258. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1259. return NULL;
  1260. /*
  1261. * We have to just have a reference on the net from sk, but don't
  1262. * get_net it. Besides, we cannot get and then put the net here.
  1263. * So we create one inside init_net and the move it to net.
  1264. */
  1265. if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
  1266. goto out_sock_release_nosk;
  1267. sk = sock->sk;
  1268. sk_change_net(sk, net);
  1269. if (groups < 32)
  1270. groups = 32;
  1271. listeners = kzalloc(NLGRPSZ(groups) + sizeof(struct listeners_rcu_head),
  1272. GFP_KERNEL);
  1273. if (!listeners)
  1274. goto out_sock_release;
  1275. sk->sk_data_ready = netlink_data_ready;
  1276. if (input)
  1277. nlk_sk(sk)->netlink_rcv = input;
  1278. if (netlink_insert(sk, net, 0))
  1279. goto out_sock_release;
  1280. nlk = nlk_sk(sk);
  1281. nlk->flags |= NETLINK_KERNEL_SOCKET;
  1282. netlink_table_grab();
  1283. if (!nl_table[unit].registered) {
  1284. nl_table[unit].groups = groups;
  1285. nl_table[unit].listeners = listeners;
  1286. nl_table[unit].cb_mutex = cb_mutex;
  1287. nl_table[unit].module = module;
  1288. nl_table[unit].registered = 1;
  1289. } else {
  1290. kfree(listeners);
  1291. nl_table[unit].registered++;
  1292. }
  1293. netlink_table_ungrab();
  1294. return sk;
  1295. out_sock_release:
  1296. kfree(listeners);
  1297. netlink_kernel_release(sk);
  1298. return NULL;
  1299. out_sock_release_nosk:
  1300. sock_release(sock);
  1301. return NULL;
  1302. }
  1303. EXPORT_SYMBOL(netlink_kernel_create);
  1304. void
  1305. netlink_kernel_release(struct sock *sk)
  1306. {
  1307. sk_release_kernel(sk);
  1308. }
  1309. EXPORT_SYMBOL(netlink_kernel_release);
  1310. static void netlink_free_old_listeners(struct rcu_head *rcu_head)
  1311. {
  1312. struct listeners_rcu_head *lrh;
  1313. lrh = container_of(rcu_head, struct listeners_rcu_head, rcu_head);
  1314. kfree(lrh->ptr);
  1315. }
  1316. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1317. {
  1318. unsigned long *listeners, *old = NULL;
  1319. struct listeners_rcu_head *old_rcu_head;
  1320. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1321. if (groups < 32)
  1322. groups = 32;
  1323. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1324. listeners = kzalloc(NLGRPSZ(groups) +
  1325. sizeof(struct listeners_rcu_head),
  1326. GFP_ATOMIC);
  1327. if (!listeners)
  1328. return -ENOMEM;
  1329. old = tbl->listeners;
  1330. memcpy(listeners, old, NLGRPSZ(tbl->groups));
  1331. rcu_assign_pointer(tbl->listeners, listeners);
  1332. /*
  1333. * Free the old memory after an RCU grace period so we
  1334. * don't leak it. We use call_rcu() here in order to be
  1335. * able to call this function from atomic contexts. The
  1336. * allocation of this memory will have reserved enough
  1337. * space for struct listeners_rcu_head at the end.
  1338. */
  1339. old_rcu_head = (void *)(tbl->listeners +
  1340. NLGRPLONGS(tbl->groups));
  1341. old_rcu_head->ptr = old;
  1342. call_rcu(&old_rcu_head->rcu_head, netlink_free_old_listeners);
  1343. }
  1344. tbl->groups = groups;
  1345. return 0;
  1346. }
  1347. /**
  1348. * netlink_change_ngroups - change number of multicast groups
  1349. *
  1350. * This changes the number of multicast groups that are available
  1351. * on a certain netlink family. Note that it is not possible to
  1352. * change the number of groups to below 32. Also note that it does
  1353. * not implicitly call netlink_clear_multicast_users() when the
  1354. * number of groups is reduced.
  1355. *
  1356. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1357. * @groups: The new number of groups.
  1358. */
  1359. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1360. {
  1361. int err;
  1362. netlink_table_grab();
  1363. err = __netlink_change_ngroups(sk, groups);
  1364. netlink_table_ungrab();
  1365. return err;
  1366. }
  1367. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1368. {
  1369. struct sock *sk;
  1370. struct hlist_node *node;
  1371. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1372. sk_for_each_bound(sk, node, &tbl->mc_list)
  1373. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1374. }
  1375. /**
  1376. * netlink_clear_multicast_users - kick off multicast listeners
  1377. *
  1378. * This function removes all listeners from the given group.
  1379. * @ksk: The kernel netlink socket, as returned by
  1380. * netlink_kernel_create().
  1381. * @group: The multicast group to clear.
  1382. */
  1383. void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1384. {
  1385. netlink_table_grab();
  1386. __netlink_clear_multicast_users(ksk, group);
  1387. netlink_table_ungrab();
  1388. }
  1389. void netlink_set_nonroot(int protocol, unsigned int flags)
  1390. {
  1391. if ((unsigned int)protocol < MAX_LINKS)
  1392. nl_table[protocol].nl_nonroot = flags;
  1393. }
  1394. EXPORT_SYMBOL(netlink_set_nonroot);
  1395. static void netlink_destroy_callback(struct netlink_callback *cb)
  1396. {
  1397. kfree_skb(cb->skb);
  1398. kfree(cb);
  1399. }
  1400. /*
  1401. * It looks a bit ugly.
  1402. * It would be better to create kernel thread.
  1403. */
  1404. static int netlink_dump(struct sock *sk)
  1405. {
  1406. struct netlink_sock *nlk = nlk_sk(sk);
  1407. struct netlink_callback *cb;
  1408. struct sk_buff *skb;
  1409. struct nlmsghdr *nlh;
  1410. int len, err = -ENOBUFS;
  1411. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  1412. if (!skb)
  1413. goto errout;
  1414. mutex_lock(nlk->cb_mutex);
  1415. cb = nlk->cb;
  1416. if (cb == NULL) {
  1417. err = -EINVAL;
  1418. goto errout_skb;
  1419. }
  1420. len = cb->dump(skb, cb);
  1421. if (len > 0) {
  1422. mutex_unlock(nlk->cb_mutex);
  1423. if (sk_filter(sk, skb))
  1424. kfree_skb(skb);
  1425. else {
  1426. skb_queue_tail(&sk->sk_receive_queue, skb);
  1427. sk->sk_data_ready(sk, skb->len);
  1428. }
  1429. return 0;
  1430. }
  1431. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  1432. if (!nlh)
  1433. goto errout_skb;
  1434. memcpy(nlmsg_data(nlh), &len, sizeof(len));
  1435. if (sk_filter(sk, skb))
  1436. kfree_skb(skb);
  1437. else {
  1438. skb_queue_tail(&sk->sk_receive_queue, skb);
  1439. sk->sk_data_ready(sk, skb->len);
  1440. }
  1441. if (cb->done)
  1442. cb->done(cb);
  1443. nlk->cb = NULL;
  1444. mutex_unlock(nlk->cb_mutex);
  1445. netlink_destroy_callback(cb);
  1446. return 0;
  1447. errout_skb:
  1448. mutex_unlock(nlk->cb_mutex);
  1449. kfree_skb(skb);
  1450. errout:
  1451. return err;
  1452. }
  1453. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1454. const struct nlmsghdr *nlh,
  1455. int (*dump)(struct sk_buff *skb,
  1456. struct netlink_callback *),
  1457. int (*done)(struct netlink_callback *))
  1458. {
  1459. struct netlink_callback *cb;
  1460. struct sock *sk;
  1461. struct netlink_sock *nlk;
  1462. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  1463. if (cb == NULL)
  1464. return -ENOBUFS;
  1465. cb->dump = dump;
  1466. cb->done = done;
  1467. cb->nlh = nlh;
  1468. atomic_inc(&skb->users);
  1469. cb->skb = skb;
  1470. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).pid);
  1471. if (sk == NULL) {
  1472. netlink_destroy_callback(cb);
  1473. return -ECONNREFUSED;
  1474. }
  1475. nlk = nlk_sk(sk);
  1476. /* A dump is in progress... */
  1477. mutex_lock(nlk->cb_mutex);
  1478. if (nlk->cb) {
  1479. mutex_unlock(nlk->cb_mutex);
  1480. netlink_destroy_callback(cb);
  1481. sock_put(sk);
  1482. return -EBUSY;
  1483. }
  1484. nlk->cb = cb;
  1485. mutex_unlock(nlk->cb_mutex);
  1486. netlink_dump(sk);
  1487. sock_put(sk);
  1488. /* We successfully started a dump, by returning -EINTR we
  1489. * signal not to send ACK even if it was requested.
  1490. */
  1491. return -EINTR;
  1492. }
  1493. EXPORT_SYMBOL(netlink_dump_start);
  1494. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1495. {
  1496. struct sk_buff *skb;
  1497. struct nlmsghdr *rep;
  1498. struct nlmsgerr *errmsg;
  1499. size_t payload = sizeof(*errmsg);
  1500. /* error messages get the original request appened */
  1501. if (err)
  1502. payload += nlmsg_len(nlh);
  1503. skb = nlmsg_new(payload, GFP_KERNEL);
  1504. if (!skb) {
  1505. struct sock *sk;
  1506. sk = netlink_lookup(sock_net(in_skb->sk),
  1507. in_skb->sk->sk_protocol,
  1508. NETLINK_CB(in_skb).pid);
  1509. if (sk) {
  1510. sk->sk_err = ENOBUFS;
  1511. sk->sk_error_report(sk);
  1512. sock_put(sk);
  1513. }
  1514. return;
  1515. }
  1516. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  1517. NLMSG_ERROR, payload, 0);
  1518. errmsg = nlmsg_data(rep);
  1519. errmsg->error = err;
  1520. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
  1521. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  1522. }
  1523. EXPORT_SYMBOL(netlink_ack);
  1524. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1525. struct nlmsghdr *))
  1526. {
  1527. struct nlmsghdr *nlh;
  1528. int err;
  1529. while (skb->len >= nlmsg_total_size(0)) {
  1530. int msglen;
  1531. nlh = nlmsg_hdr(skb);
  1532. err = 0;
  1533. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1534. return 0;
  1535. /* Only requests are handled by the kernel */
  1536. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1537. goto ack;
  1538. /* Skip control messages */
  1539. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1540. goto ack;
  1541. err = cb(skb, nlh);
  1542. if (err == -EINTR)
  1543. goto skip;
  1544. ack:
  1545. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1546. netlink_ack(skb, nlh, err);
  1547. skip:
  1548. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1549. if (msglen > skb->len)
  1550. msglen = skb->len;
  1551. skb_pull(skb, msglen);
  1552. }
  1553. return 0;
  1554. }
  1555. EXPORT_SYMBOL(netlink_rcv_skb);
  1556. /**
  1557. * nlmsg_notify - send a notification netlink message
  1558. * @sk: netlink socket to use
  1559. * @skb: notification message
  1560. * @pid: destination netlink pid for reports or 0
  1561. * @group: destination multicast group or 0
  1562. * @report: 1 to report back, 0 to disable
  1563. * @flags: allocation flags
  1564. */
  1565. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid,
  1566. unsigned int group, int report, gfp_t flags)
  1567. {
  1568. int err = 0;
  1569. if (group) {
  1570. int exclude_pid = 0;
  1571. if (report) {
  1572. atomic_inc(&skb->users);
  1573. exclude_pid = pid;
  1574. }
  1575. /* errors reported via destination sk->sk_err, but propagate
  1576. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  1577. err = nlmsg_multicast(sk, skb, exclude_pid, group, flags);
  1578. }
  1579. if (report) {
  1580. int err2;
  1581. err2 = nlmsg_unicast(sk, skb, pid);
  1582. if (!err || err == -ESRCH)
  1583. err = err2;
  1584. }
  1585. return err;
  1586. }
  1587. EXPORT_SYMBOL(nlmsg_notify);
  1588. #ifdef CONFIG_PROC_FS
  1589. struct nl_seq_iter {
  1590. struct seq_net_private p;
  1591. int link;
  1592. int hash_idx;
  1593. };
  1594. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  1595. {
  1596. struct nl_seq_iter *iter = seq->private;
  1597. int i, j;
  1598. struct sock *s;
  1599. struct hlist_node *node;
  1600. loff_t off = 0;
  1601. for (i = 0; i < MAX_LINKS; i++) {
  1602. struct nl_pid_hash *hash = &nl_table[i].hash;
  1603. for (j = 0; j <= hash->mask; j++) {
  1604. sk_for_each(s, node, &hash->table[j]) {
  1605. if (sock_net(s) != seq_file_net(seq))
  1606. continue;
  1607. if (off == pos) {
  1608. iter->link = i;
  1609. iter->hash_idx = j;
  1610. return s;
  1611. }
  1612. ++off;
  1613. }
  1614. }
  1615. }
  1616. return NULL;
  1617. }
  1618. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1619. __acquires(nl_table_lock)
  1620. {
  1621. read_lock(&nl_table_lock);
  1622. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1623. }
  1624. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1625. {
  1626. struct sock *s;
  1627. struct nl_seq_iter *iter;
  1628. int i, j;
  1629. ++*pos;
  1630. if (v == SEQ_START_TOKEN)
  1631. return netlink_seq_socket_idx(seq, 0);
  1632. iter = seq->private;
  1633. s = v;
  1634. do {
  1635. s = sk_next(s);
  1636. } while (s && sock_net(s) != seq_file_net(seq));
  1637. if (s)
  1638. return s;
  1639. i = iter->link;
  1640. j = iter->hash_idx + 1;
  1641. do {
  1642. struct nl_pid_hash *hash = &nl_table[i].hash;
  1643. for (; j <= hash->mask; j++) {
  1644. s = sk_head(&hash->table[j]);
  1645. while (s && sock_net(s) != seq_file_net(seq))
  1646. s = sk_next(s);
  1647. if (s) {
  1648. iter->link = i;
  1649. iter->hash_idx = j;
  1650. return s;
  1651. }
  1652. }
  1653. j = 0;
  1654. } while (++i < MAX_LINKS);
  1655. return NULL;
  1656. }
  1657. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1658. __releases(nl_table_lock)
  1659. {
  1660. read_unlock(&nl_table_lock);
  1661. }
  1662. static int netlink_seq_show(struct seq_file *seq, void *v)
  1663. {
  1664. if (v == SEQ_START_TOKEN)
  1665. seq_puts(seq,
  1666. "sk Eth Pid Groups "
  1667. "Rmem Wmem Dump Locks Drops Inode\n");
  1668. else {
  1669. struct sock *s = v;
  1670. struct netlink_sock *nlk = nlk_sk(s);
  1671. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %-8d %-8d %-8lu\n",
  1672. s,
  1673. s->sk_protocol,
  1674. nlk->pid,
  1675. nlk->groups ? (u32)nlk->groups[0] : 0,
  1676. sk_rmem_alloc_get(s),
  1677. sk_wmem_alloc_get(s),
  1678. nlk->cb,
  1679. atomic_read(&s->sk_refcnt),
  1680. atomic_read(&s->sk_drops),
  1681. sock_i_ino(s)
  1682. );
  1683. }
  1684. return 0;
  1685. }
  1686. static const struct seq_operations netlink_seq_ops = {
  1687. .start = netlink_seq_start,
  1688. .next = netlink_seq_next,
  1689. .stop = netlink_seq_stop,
  1690. .show = netlink_seq_show,
  1691. };
  1692. static int netlink_seq_open(struct inode *inode, struct file *file)
  1693. {
  1694. return seq_open_net(inode, file, &netlink_seq_ops,
  1695. sizeof(struct nl_seq_iter));
  1696. }
  1697. static const struct file_operations netlink_seq_fops = {
  1698. .owner = THIS_MODULE,
  1699. .open = netlink_seq_open,
  1700. .read = seq_read,
  1701. .llseek = seq_lseek,
  1702. .release = seq_release_net,
  1703. };
  1704. #endif
  1705. int netlink_register_notifier(struct notifier_block *nb)
  1706. {
  1707. return atomic_notifier_chain_register(&netlink_chain, nb);
  1708. }
  1709. EXPORT_SYMBOL(netlink_register_notifier);
  1710. int netlink_unregister_notifier(struct notifier_block *nb)
  1711. {
  1712. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  1713. }
  1714. EXPORT_SYMBOL(netlink_unregister_notifier);
  1715. static const struct proto_ops netlink_ops = {
  1716. .family = PF_NETLINK,
  1717. .owner = THIS_MODULE,
  1718. .release = netlink_release,
  1719. .bind = netlink_bind,
  1720. .connect = netlink_connect,
  1721. .socketpair = sock_no_socketpair,
  1722. .accept = sock_no_accept,
  1723. .getname = netlink_getname,
  1724. .poll = datagram_poll,
  1725. .ioctl = sock_no_ioctl,
  1726. .listen = sock_no_listen,
  1727. .shutdown = sock_no_shutdown,
  1728. .setsockopt = netlink_setsockopt,
  1729. .getsockopt = netlink_getsockopt,
  1730. .sendmsg = netlink_sendmsg,
  1731. .recvmsg = netlink_recvmsg,
  1732. .mmap = sock_no_mmap,
  1733. .sendpage = sock_no_sendpage,
  1734. };
  1735. static const struct net_proto_family netlink_family_ops = {
  1736. .family = PF_NETLINK,
  1737. .create = netlink_create,
  1738. .owner = THIS_MODULE, /* for consistency 8) */
  1739. };
  1740. static int __net_init netlink_net_init(struct net *net)
  1741. {
  1742. #ifdef CONFIG_PROC_FS
  1743. if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
  1744. return -ENOMEM;
  1745. #endif
  1746. return 0;
  1747. }
  1748. static void __net_exit netlink_net_exit(struct net *net)
  1749. {
  1750. #ifdef CONFIG_PROC_FS
  1751. proc_net_remove(net, "netlink");
  1752. #endif
  1753. }
  1754. static struct pernet_operations __net_initdata netlink_net_ops = {
  1755. .init = netlink_net_init,
  1756. .exit = netlink_net_exit,
  1757. };
  1758. static int __init netlink_proto_init(void)
  1759. {
  1760. struct sk_buff *dummy_skb;
  1761. int i;
  1762. unsigned long limit;
  1763. unsigned int order;
  1764. int err = proto_register(&netlink_proto, 0);
  1765. if (err != 0)
  1766. goto out;
  1767. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb));
  1768. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  1769. if (!nl_table)
  1770. goto panic;
  1771. if (totalram_pages >= (128 * 1024))
  1772. limit = totalram_pages >> (21 - PAGE_SHIFT);
  1773. else
  1774. limit = totalram_pages >> (23 - PAGE_SHIFT);
  1775. order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
  1776. limit = (1UL << order) / sizeof(struct hlist_head);
  1777. order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
  1778. for (i = 0; i < MAX_LINKS; i++) {
  1779. struct nl_pid_hash *hash = &nl_table[i].hash;
  1780. hash->table = nl_pid_hash_zalloc(1 * sizeof(*hash->table));
  1781. if (!hash->table) {
  1782. while (i-- > 0)
  1783. nl_pid_hash_free(nl_table[i].hash.table,
  1784. 1 * sizeof(*hash->table));
  1785. kfree(nl_table);
  1786. goto panic;
  1787. }
  1788. hash->max_shift = order;
  1789. hash->shift = 0;
  1790. hash->mask = 0;
  1791. hash->rehash_time = jiffies;
  1792. }
  1793. sock_register(&netlink_family_ops);
  1794. register_pernet_subsys(&netlink_net_ops);
  1795. /* The netlink device handler may be needed early. */
  1796. rtnetlink_init();
  1797. out:
  1798. return err;
  1799. panic:
  1800. panic("netlink_init: Cannot allocate nl_table\n");
  1801. }
  1802. core_initcall(netlink_proto_init);