af_netlink.c 44 KB

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