af_netlink.c 44 KB

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