af_netlink.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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. * Patrick McHardy <kaber@trash.net>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
  14. * added netlink_proto_exit
  15. * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
  16. * use nlk_sk, as sk->protinfo is on a diet 8)
  17. * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
  18. * - inc module use count of module that owns
  19. * the kernel socket in case userspace opens
  20. * socket of same protocol
  21. * - remove all module support, since netlink is
  22. * mandatory if CONFIG_NET=y these days
  23. */
  24. #include <linux/module.h>
  25. #include <linux/capability.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/signal.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/stat.h>
  33. #include <linux/socket.h>
  34. #include <linux/un.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/termios.h>
  37. #include <linux/sockios.h>
  38. #include <linux/net.h>
  39. #include <linux/fs.h>
  40. #include <linux/slab.h>
  41. #include <asm/uaccess.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/rtnetlink.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/notifier.h>
  48. #include <linux/security.h>
  49. #include <linux/jhash.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/random.h>
  52. #include <linux/bitops.h>
  53. #include <linux/mm.h>
  54. #include <linux/types.h>
  55. #include <linux/audit.h>
  56. #include <linux/mutex.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/if_arp.h>
  59. #include <linux/rhashtable.h>
  60. #include <asm/cacheflush.h>
  61. #include <linux/hash.h>
  62. #include <linux/genetlink.h>
  63. #include <linux/nospec.h>
  64. #include <net/net_namespace.h>
  65. #include <net/sock.h>
  66. #include <net/scm.h>
  67. #include <net/netlink.h>
  68. #include "af_netlink.h"
  69. struct listeners {
  70. struct rcu_head rcu;
  71. unsigned long masks[0];
  72. };
  73. /* state bits */
  74. #define NETLINK_S_CONGESTED 0x0
  75. /* flags */
  76. #define NETLINK_F_KERNEL_SOCKET 0x1
  77. #define NETLINK_F_RECV_PKTINFO 0x2
  78. #define NETLINK_F_BROADCAST_SEND_ERROR 0x4
  79. #define NETLINK_F_RECV_NO_ENOBUFS 0x8
  80. #define NETLINK_F_LISTEN_ALL_NSID 0x10
  81. #define NETLINK_F_CAP_ACK 0x20
  82. static inline int netlink_is_kernel(struct sock *sk)
  83. {
  84. return nlk_sk(sk)->flags & NETLINK_F_KERNEL_SOCKET;
  85. }
  86. struct netlink_table *nl_table __read_mostly;
  87. EXPORT_SYMBOL_GPL(nl_table);
  88. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  89. static struct lock_class_key nlk_cb_mutex_keys[MAX_LINKS];
  90. static const char *const nlk_cb_mutex_key_strings[MAX_LINKS + 1] = {
  91. "nlk_cb_mutex-ROUTE",
  92. "nlk_cb_mutex-1",
  93. "nlk_cb_mutex-USERSOCK",
  94. "nlk_cb_mutex-FIREWALL",
  95. "nlk_cb_mutex-SOCK_DIAG",
  96. "nlk_cb_mutex-NFLOG",
  97. "nlk_cb_mutex-XFRM",
  98. "nlk_cb_mutex-SELINUX",
  99. "nlk_cb_mutex-ISCSI",
  100. "nlk_cb_mutex-AUDIT",
  101. "nlk_cb_mutex-FIB_LOOKUP",
  102. "nlk_cb_mutex-CONNECTOR",
  103. "nlk_cb_mutex-NETFILTER",
  104. "nlk_cb_mutex-IP6_FW",
  105. "nlk_cb_mutex-DNRTMSG",
  106. "nlk_cb_mutex-KOBJECT_UEVENT",
  107. "nlk_cb_mutex-GENERIC",
  108. "nlk_cb_mutex-17",
  109. "nlk_cb_mutex-SCSITRANSPORT",
  110. "nlk_cb_mutex-ECRYPTFS",
  111. "nlk_cb_mutex-RDMA",
  112. "nlk_cb_mutex-CRYPTO",
  113. "nlk_cb_mutex-SMC",
  114. "nlk_cb_mutex-23",
  115. "nlk_cb_mutex-24",
  116. "nlk_cb_mutex-25",
  117. "nlk_cb_mutex-26",
  118. "nlk_cb_mutex-27",
  119. "nlk_cb_mutex-28",
  120. "nlk_cb_mutex-29",
  121. "nlk_cb_mutex-30",
  122. "nlk_cb_mutex-31",
  123. "nlk_cb_mutex-MAX_LINKS"
  124. };
  125. static int netlink_dump(struct sock *sk);
  126. static void netlink_skb_destructor(struct sk_buff *skb);
  127. /* nl_table locking explained:
  128. * Lookup and traversal are protected with an RCU read-side lock. Insertion
  129. * and removal are protected with per bucket lock while using RCU list
  130. * modification primitives and may run in parallel to RCU protected lookups.
  131. * Destruction of the Netlink socket may only occur *after* nl_table_lock has
  132. * been acquired * either during or after the socket has been removed from
  133. * the list and after an RCU grace period.
  134. */
  135. DEFINE_RWLOCK(nl_table_lock);
  136. EXPORT_SYMBOL_GPL(nl_table_lock);
  137. static atomic_t nl_table_users = ATOMIC_INIT(0);
  138. #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
  139. static ATOMIC_NOTIFIER_HEAD(netlink_chain);
  140. static DEFINE_SPINLOCK(netlink_tap_lock);
  141. static struct list_head netlink_tap_all __read_mostly;
  142. static const struct rhashtable_params netlink_rhashtable_params;
  143. static inline u32 netlink_group_mask(u32 group)
  144. {
  145. return group ? 1 << (group - 1) : 0;
  146. }
  147. static struct sk_buff *netlink_to_full_skb(const struct sk_buff *skb,
  148. gfp_t gfp_mask)
  149. {
  150. unsigned int len = skb_end_offset(skb);
  151. struct sk_buff *new;
  152. new = alloc_skb(len, gfp_mask);
  153. if (new == NULL)
  154. return NULL;
  155. NETLINK_CB(new).portid = NETLINK_CB(skb).portid;
  156. NETLINK_CB(new).dst_group = NETLINK_CB(skb).dst_group;
  157. NETLINK_CB(new).creds = NETLINK_CB(skb).creds;
  158. memcpy(skb_put(new, len), skb->data, len);
  159. return new;
  160. }
  161. int netlink_add_tap(struct netlink_tap *nt)
  162. {
  163. if (unlikely(nt->dev->type != ARPHRD_NETLINK))
  164. return -EINVAL;
  165. spin_lock(&netlink_tap_lock);
  166. list_add_rcu(&nt->list, &netlink_tap_all);
  167. spin_unlock(&netlink_tap_lock);
  168. __module_get(nt->module);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL_GPL(netlink_add_tap);
  172. static int __netlink_remove_tap(struct netlink_tap *nt)
  173. {
  174. bool found = false;
  175. struct netlink_tap *tmp;
  176. spin_lock(&netlink_tap_lock);
  177. list_for_each_entry(tmp, &netlink_tap_all, list) {
  178. if (nt == tmp) {
  179. list_del_rcu(&nt->list);
  180. found = true;
  181. goto out;
  182. }
  183. }
  184. pr_warn("__netlink_remove_tap: %p not found\n", nt);
  185. out:
  186. spin_unlock(&netlink_tap_lock);
  187. if (found)
  188. module_put(nt->module);
  189. return found ? 0 : -ENODEV;
  190. }
  191. int netlink_remove_tap(struct netlink_tap *nt)
  192. {
  193. int ret;
  194. ret = __netlink_remove_tap(nt);
  195. synchronize_net();
  196. return ret;
  197. }
  198. EXPORT_SYMBOL_GPL(netlink_remove_tap);
  199. static bool netlink_filter_tap(const struct sk_buff *skb)
  200. {
  201. struct sock *sk = skb->sk;
  202. /* We take the more conservative approach and
  203. * whitelist socket protocols that may pass.
  204. */
  205. switch (sk->sk_protocol) {
  206. case NETLINK_ROUTE:
  207. case NETLINK_USERSOCK:
  208. case NETLINK_SOCK_DIAG:
  209. case NETLINK_NFLOG:
  210. case NETLINK_XFRM:
  211. case NETLINK_FIB_LOOKUP:
  212. case NETLINK_NETFILTER:
  213. case NETLINK_GENERIC:
  214. return true;
  215. }
  216. return false;
  217. }
  218. static int __netlink_deliver_tap_skb(struct sk_buff *skb,
  219. struct net_device *dev)
  220. {
  221. struct sk_buff *nskb;
  222. struct sock *sk = skb->sk;
  223. int ret = -ENOMEM;
  224. if (!net_eq(dev_net(dev), sock_net(sk)))
  225. return 0;
  226. dev_hold(dev);
  227. if (is_vmalloc_addr(skb->head))
  228. nskb = netlink_to_full_skb(skb, GFP_ATOMIC);
  229. else
  230. nskb = skb_clone(skb, GFP_ATOMIC);
  231. if (nskb) {
  232. nskb->dev = dev;
  233. nskb->protocol = htons((u16) sk->sk_protocol);
  234. nskb->pkt_type = netlink_is_kernel(sk) ?
  235. PACKET_KERNEL : PACKET_USER;
  236. skb_reset_network_header(nskb);
  237. ret = dev_queue_xmit(nskb);
  238. if (unlikely(ret > 0))
  239. ret = net_xmit_errno(ret);
  240. }
  241. dev_put(dev);
  242. return ret;
  243. }
  244. static void __netlink_deliver_tap(struct sk_buff *skb)
  245. {
  246. int ret;
  247. struct netlink_tap *tmp;
  248. if (!netlink_filter_tap(skb))
  249. return;
  250. list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
  251. ret = __netlink_deliver_tap_skb(skb, tmp->dev);
  252. if (unlikely(ret))
  253. break;
  254. }
  255. }
  256. static void netlink_deliver_tap(struct sk_buff *skb)
  257. {
  258. rcu_read_lock();
  259. if (unlikely(!list_empty(&netlink_tap_all)))
  260. __netlink_deliver_tap(skb);
  261. rcu_read_unlock();
  262. }
  263. static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
  264. struct sk_buff *skb)
  265. {
  266. if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
  267. netlink_deliver_tap(skb);
  268. }
  269. static void netlink_overrun(struct sock *sk)
  270. {
  271. struct netlink_sock *nlk = nlk_sk(sk);
  272. if (!(nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)) {
  273. if (!test_and_set_bit(NETLINK_S_CONGESTED,
  274. &nlk_sk(sk)->state)) {
  275. sk->sk_err = ENOBUFS;
  276. sk->sk_error_report(sk);
  277. }
  278. }
  279. atomic_inc(&sk->sk_drops);
  280. }
  281. static void netlink_rcv_wake(struct sock *sk)
  282. {
  283. struct netlink_sock *nlk = nlk_sk(sk);
  284. if (skb_queue_empty(&sk->sk_receive_queue))
  285. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  286. if (!test_bit(NETLINK_S_CONGESTED, &nlk->state))
  287. wake_up_interruptible(&nlk->wait);
  288. }
  289. static void netlink_skb_destructor(struct sk_buff *skb)
  290. {
  291. if (is_vmalloc_addr(skb->head)) {
  292. if (!skb->cloned ||
  293. !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
  294. vfree(skb->head);
  295. skb->head = NULL;
  296. }
  297. if (skb->sk != NULL)
  298. sock_rfree(skb);
  299. }
  300. static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  301. {
  302. WARN_ON(skb->sk != NULL);
  303. skb->sk = sk;
  304. skb->destructor = netlink_skb_destructor;
  305. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  306. sk_mem_charge(sk, skb->truesize);
  307. }
  308. static void netlink_sock_destruct(struct sock *sk)
  309. {
  310. struct netlink_sock *nlk = nlk_sk(sk);
  311. if (nlk->cb_running) {
  312. if (nlk->cb.done)
  313. nlk->cb.done(&nlk->cb);
  314. module_put(nlk->cb.module);
  315. kfree_skb(nlk->cb.skb);
  316. }
  317. skb_queue_purge(&sk->sk_receive_queue);
  318. if (!sock_flag(sk, SOCK_DEAD)) {
  319. printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
  320. return;
  321. }
  322. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  323. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  324. WARN_ON(nlk_sk(sk)->groups);
  325. }
  326. static void netlink_sock_destruct_work(struct work_struct *work)
  327. {
  328. struct netlink_sock *nlk = container_of(work, struct netlink_sock,
  329. work);
  330. sk_free(&nlk->sk);
  331. }
  332. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
  333. * SMP. Look, when several writers sleep and reader wakes them up, all but one
  334. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  335. * this, _but_ remember, it adds useless work on UP machines.
  336. */
  337. void netlink_table_grab(void)
  338. __acquires(nl_table_lock)
  339. {
  340. might_sleep();
  341. write_lock_irq(&nl_table_lock);
  342. if (atomic_read(&nl_table_users)) {
  343. DECLARE_WAITQUEUE(wait, current);
  344. add_wait_queue_exclusive(&nl_table_wait, &wait);
  345. for (;;) {
  346. set_current_state(TASK_UNINTERRUPTIBLE);
  347. if (atomic_read(&nl_table_users) == 0)
  348. break;
  349. write_unlock_irq(&nl_table_lock);
  350. schedule();
  351. write_lock_irq(&nl_table_lock);
  352. }
  353. __set_current_state(TASK_RUNNING);
  354. remove_wait_queue(&nl_table_wait, &wait);
  355. }
  356. }
  357. void netlink_table_ungrab(void)
  358. __releases(nl_table_lock)
  359. {
  360. write_unlock_irq(&nl_table_lock);
  361. wake_up(&nl_table_wait);
  362. }
  363. static inline void
  364. netlink_lock_table(void)
  365. {
  366. /* read_lock() synchronizes us to netlink_table_grab */
  367. read_lock(&nl_table_lock);
  368. atomic_inc(&nl_table_users);
  369. read_unlock(&nl_table_lock);
  370. }
  371. static inline void
  372. netlink_unlock_table(void)
  373. {
  374. if (atomic_dec_and_test(&nl_table_users))
  375. wake_up(&nl_table_wait);
  376. }
  377. struct netlink_compare_arg
  378. {
  379. possible_net_t pnet;
  380. u32 portid;
  381. };
  382. /* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
  383. #define netlink_compare_arg_len \
  384. (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
  385. static inline int netlink_compare(struct rhashtable_compare_arg *arg,
  386. const void *ptr)
  387. {
  388. const struct netlink_compare_arg *x = arg->key;
  389. const struct netlink_sock *nlk = ptr;
  390. return nlk->portid != x->portid ||
  391. !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
  392. }
  393. static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
  394. struct net *net, u32 portid)
  395. {
  396. memset(arg, 0, sizeof(*arg));
  397. write_pnet(&arg->pnet, net);
  398. arg->portid = portid;
  399. }
  400. static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
  401. struct net *net)
  402. {
  403. struct netlink_compare_arg arg;
  404. netlink_compare_arg_init(&arg, net, portid);
  405. return rhashtable_lookup_fast(&table->hash, &arg,
  406. netlink_rhashtable_params);
  407. }
  408. static int __netlink_insert(struct netlink_table *table, struct sock *sk)
  409. {
  410. struct netlink_compare_arg arg;
  411. netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
  412. return rhashtable_lookup_insert_key(&table->hash, &arg,
  413. &nlk_sk(sk)->node,
  414. netlink_rhashtable_params);
  415. }
  416. static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
  417. {
  418. struct netlink_table *table = &nl_table[protocol];
  419. struct sock *sk;
  420. rcu_read_lock();
  421. sk = __netlink_lookup(table, portid, net);
  422. if (sk)
  423. sock_hold(sk);
  424. rcu_read_unlock();
  425. return sk;
  426. }
  427. static const struct proto_ops netlink_ops;
  428. static void
  429. netlink_update_listeners(struct sock *sk)
  430. {
  431. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  432. unsigned long mask;
  433. unsigned int i;
  434. struct listeners *listeners;
  435. listeners = nl_deref_protected(tbl->listeners);
  436. if (!listeners)
  437. return;
  438. for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
  439. mask = 0;
  440. sk_for_each_bound(sk, &tbl->mc_list) {
  441. if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
  442. mask |= nlk_sk(sk)->groups[i];
  443. }
  444. listeners->masks[i] = mask;
  445. }
  446. /* this function is only called with the netlink table "grabbed", which
  447. * makes sure updates are visible before bind or setsockopt return. */
  448. }
  449. static int netlink_insert(struct sock *sk, u32 portid)
  450. {
  451. struct netlink_table *table = &nl_table[sk->sk_protocol];
  452. int err;
  453. lock_sock(sk);
  454. err = nlk_sk(sk)->portid == portid ? 0 : -EBUSY;
  455. if (nlk_sk(sk)->bound)
  456. goto err;
  457. err = -ENOMEM;
  458. if (BITS_PER_LONG > 32 &&
  459. unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
  460. goto err;
  461. nlk_sk(sk)->portid = portid;
  462. sock_hold(sk);
  463. err = __netlink_insert(table, sk);
  464. if (err) {
  465. /* In case the hashtable backend returns with -EBUSY
  466. * from here, it must not escape to the caller.
  467. */
  468. if (unlikely(err == -EBUSY))
  469. err = -EOVERFLOW;
  470. if (err == -EEXIST)
  471. err = -EADDRINUSE;
  472. sock_put(sk);
  473. goto err;
  474. }
  475. /* We need to ensure that the socket is hashed and visible. */
  476. smp_wmb();
  477. nlk_sk(sk)->bound = portid;
  478. err:
  479. release_sock(sk);
  480. return err;
  481. }
  482. static void netlink_remove(struct sock *sk)
  483. {
  484. struct netlink_table *table;
  485. table = &nl_table[sk->sk_protocol];
  486. if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
  487. netlink_rhashtable_params)) {
  488. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  489. __sock_put(sk);
  490. }
  491. netlink_table_grab();
  492. if (nlk_sk(sk)->subscriptions) {
  493. __sk_del_bind_node(sk);
  494. netlink_update_listeners(sk);
  495. }
  496. if (sk->sk_protocol == NETLINK_GENERIC)
  497. atomic_inc(&genl_sk_destructing_cnt);
  498. netlink_table_ungrab();
  499. }
  500. static struct proto netlink_proto = {
  501. .name = "NETLINK",
  502. .owner = THIS_MODULE,
  503. .obj_size = sizeof(struct netlink_sock),
  504. };
  505. static int __netlink_create(struct net *net, struct socket *sock,
  506. struct mutex *cb_mutex, int protocol,
  507. int kern)
  508. {
  509. struct sock *sk;
  510. struct netlink_sock *nlk;
  511. sock->ops = &netlink_ops;
  512. sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, kern);
  513. if (!sk)
  514. return -ENOMEM;
  515. sock_init_data(sock, sk);
  516. nlk = nlk_sk(sk);
  517. if (cb_mutex) {
  518. nlk->cb_mutex = cb_mutex;
  519. } else {
  520. nlk->cb_mutex = &nlk->cb_def_mutex;
  521. mutex_init(nlk->cb_mutex);
  522. lockdep_set_class_and_name(nlk->cb_mutex,
  523. nlk_cb_mutex_keys + protocol,
  524. nlk_cb_mutex_key_strings[protocol]);
  525. }
  526. init_waitqueue_head(&nlk->wait);
  527. sk->sk_destruct = netlink_sock_destruct;
  528. sk->sk_protocol = protocol;
  529. return 0;
  530. }
  531. static int netlink_create(struct net *net, struct socket *sock, int protocol,
  532. int kern)
  533. {
  534. struct module *module = NULL;
  535. struct mutex *cb_mutex;
  536. struct netlink_sock *nlk;
  537. int (*bind)(struct net *net, int group);
  538. void (*unbind)(struct net *net, int group);
  539. int err = 0;
  540. sock->state = SS_UNCONNECTED;
  541. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  542. return -ESOCKTNOSUPPORT;
  543. if (protocol < 0 || protocol >= MAX_LINKS)
  544. return -EPROTONOSUPPORT;
  545. protocol = array_index_nospec(protocol, MAX_LINKS);
  546. netlink_lock_table();
  547. #ifdef CONFIG_MODULES
  548. if (!nl_table[protocol].registered) {
  549. netlink_unlock_table();
  550. request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
  551. netlink_lock_table();
  552. }
  553. #endif
  554. if (nl_table[protocol].registered &&
  555. try_module_get(nl_table[protocol].module))
  556. module = nl_table[protocol].module;
  557. else
  558. err = -EPROTONOSUPPORT;
  559. cb_mutex = nl_table[protocol].cb_mutex;
  560. bind = nl_table[protocol].bind;
  561. unbind = nl_table[protocol].unbind;
  562. netlink_unlock_table();
  563. if (err < 0)
  564. goto out;
  565. err = __netlink_create(net, sock, cb_mutex, protocol, kern);
  566. if (err < 0)
  567. goto out_module;
  568. local_bh_disable();
  569. sock_prot_inuse_add(net, &netlink_proto, 1);
  570. local_bh_enable();
  571. nlk = nlk_sk(sock->sk);
  572. nlk->module = module;
  573. nlk->netlink_bind = bind;
  574. nlk->netlink_unbind = unbind;
  575. out:
  576. return err;
  577. out_module:
  578. module_put(module);
  579. goto out;
  580. }
  581. static void deferred_put_nlk_sk(struct rcu_head *head)
  582. {
  583. struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
  584. struct sock *sk = &nlk->sk;
  585. if (!atomic_dec_and_test(&sk->sk_refcnt))
  586. return;
  587. if (nlk->cb_running && nlk->cb.done) {
  588. INIT_WORK(&nlk->work, netlink_sock_destruct_work);
  589. schedule_work(&nlk->work);
  590. return;
  591. }
  592. sk_free(sk);
  593. }
  594. static int netlink_release(struct socket *sock)
  595. {
  596. struct sock *sk = sock->sk;
  597. struct netlink_sock *nlk;
  598. if (!sk)
  599. return 0;
  600. netlink_remove(sk);
  601. sock_orphan(sk);
  602. nlk = nlk_sk(sk);
  603. /*
  604. * OK. Socket is unlinked, any packets that arrive now
  605. * will be purged.
  606. */
  607. /* must not acquire netlink_table_lock in any way again before unbind
  608. * and notifying genetlink is done as otherwise it might deadlock
  609. */
  610. if (nlk->netlink_unbind) {
  611. int i;
  612. for (i = 0; i < nlk->ngroups; i++)
  613. if (test_bit(i, nlk->groups))
  614. nlk->netlink_unbind(sock_net(sk), i + 1);
  615. }
  616. if (sk->sk_protocol == NETLINK_GENERIC &&
  617. atomic_dec_return(&genl_sk_destructing_cnt) == 0)
  618. wake_up(&genl_sk_destructing_waitq);
  619. sock->sk = NULL;
  620. wake_up_interruptible_all(&nlk->wait);
  621. skb_queue_purge(&sk->sk_write_queue);
  622. if (nlk->portid && nlk->bound) {
  623. struct netlink_notify n = {
  624. .net = sock_net(sk),
  625. .protocol = sk->sk_protocol,
  626. .portid = nlk->portid,
  627. };
  628. atomic_notifier_call_chain(&netlink_chain,
  629. NETLINK_URELEASE, &n);
  630. }
  631. module_put(nlk->module);
  632. if (netlink_is_kernel(sk)) {
  633. netlink_table_grab();
  634. BUG_ON(nl_table[sk->sk_protocol].registered == 0);
  635. if (--nl_table[sk->sk_protocol].registered == 0) {
  636. struct listeners *old;
  637. old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
  638. RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
  639. kfree_rcu(old, rcu);
  640. nl_table[sk->sk_protocol].module = NULL;
  641. nl_table[sk->sk_protocol].bind = NULL;
  642. nl_table[sk->sk_protocol].unbind = NULL;
  643. nl_table[sk->sk_protocol].flags = 0;
  644. nl_table[sk->sk_protocol].registered = 0;
  645. }
  646. netlink_table_ungrab();
  647. }
  648. kfree(nlk->groups);
  649. nlk->groups = NULL;
  650. local_bh_disable();
  651. sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
  652. local_bh_enable();
  653. call_rcu(&nlk->rcu, deferred_put_nlk_sk);
  654. return 0;
  655. }
  656. static int netlink_autobind(struct socket *sock)
  657. {
  658. struct sock *sk = sock->sk;
  659. struct net *net = sock_net(sk);
  660. struct netlink_table *table = &nl_table[sk->sk_protocol];
  661. s32 portid = task_tgid_vnr(current);
  662. int err;
  663. s32 rover = -4096;
  664. bool ok;
  665. retry:
  666. cond_resched();
  667. rcu_read_lock();
  668. ok = !__netlink_lookup(table, portid, net);
  669. rcu_read_unlock();
  670. if (!ok) {
  671. /* Bind collision, search negative portid values. */
  672. if (rover == -4096)
  673. /* rover will be in range [S32_MIN, -4097] */
  674. rover = S32_MIN + prandom_u32_max(-4096 - S32_MIN);
  675. else if (rover >= -4096)
  676. rover = -4097;
  677. portid = rover--;
  678. goto retry;
  679. }
  680. err = netlink_insert(sk, portid);
  681. if (err == -EADDRINUSE)
  682. goto retry;
  683. /* If 2 threads race to autobind, that is fine. */
  684. if (err == -EBUSY)
  685. err = 0;
  686. return err;
  687. }
  688. /**
  689. * __netlink_ns_capable - General netlink message capability test
  690. * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
  691. * @user_ns: The user namespace of the capability to use
  692. * @cap: The capability to use
  693. *
  694. * Test to see if the opener of the socket we received the message
  695. * from had when the netlink socket was created and the sender of the
  696. * message has has the capability @cap in the user namespace @user_ns.
  697. */
  698. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  699. struct user_namespace *user_ns, int cap)
  700. {
  701. return ((nsp->flags & NETLINK_SKB_DST) ||
  702. file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
  703. ns_capable(user_ns, cap);
  704. }
  705. EXPORT_SYMBOL(__netlink_ns_capable);
  706. /**
  707. * netlink_ns_capable - General netlink message capability test
  708. * @skb: socket buffer holding a netlink command from userspace
  709. * @user_ns: The user namespace of the capability to use
  710. * @cap: The capability to use
  711. *
  712. * Test to see if the opener of the socket we received the message
  713. * from had when the netlink socket was created and the sender of the
  714. * message has has the capability @cap in the user namespace @user_ns.
  715. */
  716. bool netlink_ns_capable(const struct sk_buff *skb,
  717. struct user_namespace *user_ns, int cap)
  718. {
  719. return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
  720. }
  721. EXPORT_SYMBOL(netlink_ns_capable);
  722. /**
  723. * netlink_capable - Netlink global message capability test
  724. * @skb: socket buffer holding a netlink command from userspace
  725. * @cap: The capability to use
  726. *
  727. * Test to see if the opener of the socket we received the message
  728. * from had when the netlink socket was created and the sender of the
  729. * message has has the capability @cap in all user namespaces.
  730. */
  731. bool netlink_capable(const struct sk_buff *skb, int cap)
  732. {
  733. return netlink_ns_capable(skb, &init_user_ns, cap);
  734. }
  735. EXPORT_SYMBOL(netlink_capable);
  736. /**
  737. * netlink_net_capable - Netlink network namespace message capability test
  738. * @skb: socket buffer holding a netlink command from userspace
  739. * @cap: The capability to use
  740. *
  741. * Test to see if the opener of the socket we received the message
  742. * from had when the netlink socket was created and the sender of the
  743. * message has has the capability @cap over the network namespace of
  744. * the socket we received the message from.
  745. */
  746. bool netlink_net_capable(const struct sk_buff *skb, int cap)
  747. {
  748. return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
  749. }
  750. EXPORT_SYMBOL(netlink_net_capable);
  751. static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
  752. {
  753. return (nl_table[sock->sk->sk_protocol].flags & flag) ||
  754. ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
  755. }
  756. static void
  757. netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
  758. {
  759. struct netlink_sock *nlk = nlk_sk(sk);
  760. if (nlk->subscriptions && !subscriptions)
  761. __sk_del_bind_node(sk);
  762. else if (!nlk->subscriptions && subscriptions)
  763. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  764. nlk->subscriptions = subscriptions;
  765. }
  766. static int netlink_realloc_groups(struct sock *sk)
  767. {
  768. struct netlink_sock *nlk = nlk_sk(sk);
  769. unsigned int groups;
  770. unsigned long *new_groups;
  771. int err = 0;
  772. netlink_table_grab();
  773. groups = nl_table[sk->sk_protocol].groups;
  774. if (!nl_table[sk->sk_protocol].registered) {
  775. err = -ENOENT;
  776. goto out_unlock;
  777. }
  778. if (nlk->ngroups >= groups)
  779. goto out_unlock;
  780. new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
  781. if (new_groups == NULL) {
  782. err = -ENOMEM;
  783. goto out_unlock;
  784. }
  785. memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
  786. NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
  787. nlk->groups = new_groups;
  788. nlk->ngroups = groups;
  789. out_unlock:
  790. netlink_table_ungrab();
  791. return err;
  792. }
  793. static void netlink_undo_bind(int group, long unsigned int groups,
  794. struct sock *sk)
  795. {
  796. struct netlink_sock *nlk = nlk_sk(sk);
  797. int undo;
  798. if (!nlk->netlink_unbind)
  799. return;
  800. for (undo = 0; undo < group; undo++)
  801. if (test_bit(undo, &groups))
  802. nlk->netlink_unbind(sock_net(sk), undo + 1);
  803. }
  804. static int netlink_bind(struct socket *sock, struct sockaddr *addr,
  805. int addr_len)
  806. {
  807. struct sock *sk = sock->sk;
  808. struct net *net = sock_net(sk);
  809. struct netlink_sock *nlk = nlk_sk(sk);
  810. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  811. int err;
  812. long unsigned int groups = nladdr->nl_groups;
  813. bool bound;
  814. if (addr_len < sizeof(struct sockaddr_nl))
  815. return -EINVAL;
  816. if (nladdr->nl_family != AF_NETLINK)
  817. return -EINVAL;
  818. /* Only superuser is allowed to listen multicasts */
  819. if (groups) {
  820. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  821. return -EPERM;
  822. err = netlink_realloc_groups(sk);
  823. if (err)
  824. return err;
  825. }
  826. if (nlk->ngroups == 0)
  827. groups = 0;
  828. else if (nlk->ngroups < 8*sizeof(groups))
  829. groups &= (1UL << nlk->ngroups) - 1;
  830. bound = nlk->bound;
  831. if (bound) {
  832. /* Ensure nlk->portid is up-to-date. */
  833. smp_rmb();
  834. if (nladdr->nl_pid != nlk->portid)
  835. return -EINVAL;
  836. }
  837. if (nlk->netlink_bind && groups) {
  838. int group;
  839. for (group = 0; group < nlk->ngroups; group++) {
  840. if (!test_bit(group, &groups))
  841. continue;
  842. err = nlk->netlink_bind(net, group + 1);
  843. if (!err)
  844. continue;
  845. netlink_undo_bind(group, groups, sk);
  846. return err;
  847. }
  848. }
  849. /* No need for barriers here as we return to user-space without
  850. * using any of the bound attributes.
  851. */
  852. if (!bound) {
  853. err = nladdr->nl_pid ?
  854. netlink_insert(sk, nladdr->nl_pid) :
  855. netlink_autobind(sock);
  856. if (err) {
  857. netlink_undo_bind(nlk->ngroups, groups, sk);
  858. return err;
  859. }
  860. }
  861. if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
  862. return 0;
  863. netlink_table_grab();
  864. netlink_update_subscriptions(sk, nlk->subscriptions +
  865. hweight32(groups) -
  866. hweight32(nlk->groups[0]));
  867. nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
  868. netlink_update_listeners(sk);
  869. netlink_table_ungrab();
  870. return 0;
  871. }
  872. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  873. int alen, int flags)
  874. {
  875. int err = 0;
  876. struct sock *sk = sock->sk;
  877. struct netlink_sock *nlk = nlk_sk(sk);
  878. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  879. if (alen < sizeof(addr->sa_family))
  880. return -EINVAL;
  881. if (addr->sa_family == AF_UNSPEC) {
  882. sk->sk_state = NETLINK_UNCONNECTED;
  883. nlk->dst_portid = 0;
  884. nlk->dst_group = 0;
  885. return 0;
  886. }
  887. if (addr->sa_family != AF_NETLINK)
  888. return -EINVAL;
  889. if (alen < sizeof(struct sockaddr_nl))
  890. return -EINVAL;
  891. if ((nladdr->nl_groups || nladdr->nl_pid) &&
  892. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  893. return -EPERM;
  894. /* No need for barriers here as we return to user-space without
  895. * using any of the bound attributes.
  896. */
  897. if (!nlk->bound)
  898. err = netlink_autobind(sock);
  899. if (err == 0) {
  900. sk->sk_state = NETLINK_CONNECTED;
  901. nlk->dst_portid = nladdr->nl_pid;
  902. nlk->dst_group = ffs(nladdr->nl_groups);
  903. }
  904. return err;
  905. }
  906. static int netlink_getname(struct socket *sock, struct sockaddr *addr,
  907. int *addr_len, int peer)
  908. {
  909. struct sock *sk = sock->sk;
  910. struct netlink_sock *nlk = nlk_sk(sk);
  911. DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
  912. nladdr->nl_family = AF_NETLINK;
  913. nladdr->nl_pad = 0;
  914. *addr_len = sizeof(*nladdr);
  915. if (peer) {
  916. nladdr->nl_pid = nlk->dst_portid;
  917. nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
  918. } else {
  919. nladdr->nl_pid = nlk->portid;
  920. nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
  921. }
  922. return 0;
  923. }
  924. static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
  925. {
  926. struct sock *sock;
  927. struct netlink_sock *nlk;
  928. sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
  929. if (!sock)
  930. return ERR_PTR(-ECONNREFUSED);
  931. /* Don't bother queuing skb if kernel socket has no input function */
  932. nlk = nlk_sk(sock);
  933. if (sock->sk_state == NETLINK_CONNECTED &&
  934. nlk->dst_portid != nlk_sk(ssk)->portid) {
  935. sock_put(sock);
  936. return ERR_PTR(-ECONNREFUSED);
  937. }
  938. return sock;
  939. }
  940. struct sock *netlink_getsockbyfilp(struct file *filp)
  941. {
  942. struct inode *inode = file_inode(filp);
  943. struct sock *sock;
  944. if (!S_ISSOCK(inode->i_mode))
  945. return ERR_PTR(-ENOTSOCK);
  946. sock = SOCKET_I(inode)->sk;
  947. if (sock->sk_family != AF_NETLINK)
  948. return ERR_PTR(-EINVAL);
  949. sock_hold(sock);
  950. return sock;
  951. }
  952. static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
  953. int broadcast)
  954. {
  955. struct sk_buff *skb;
  956. void *data;
  957. if (size <= NLMSG_GOODSIZE || broadcast)
  958. return alloc_skb(size, GFP_KERNEL);
  959. size = SKB_DATA_ALIGN(size) +
  960. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  961. data = vmalloc(size);
  962. if (data == NULL)
  963. return NULL;
  964. skb = __build_skb(data, size);
  965. if (skb == NULL)
  966. vfree(data);
  967. else
  968. skb->destructor = netlink_skb_destructor;
  969. return skb;
  970. }
  971. /*
  972. * Attach a skb to a netlink socket.
  973. * The caller must hold a reference to the destination socket. On error, the
  974. * reference is dropped. The skb is not send to the destination, just all
  975. * all error checks are performed and memory in the queue is reserved.
  976. * Return values:
  977. * < 0: error. skb freed, reference to sock dropped.
  978. * 0: continue
  979. * 1: repeat lookup - reference dropped while waiting for socket memory.
  980. */
  981. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  982. long *timeo, struct sock *ssk)
  983. {
  984. struct netlink_sock *nlk;
  985. nlk = nlk_sk(sk);
  986. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  987. test_bit(NETLINK_S_CONGESTED, &nlk->state))) {
  988. DECLARE_WAITQUEUE(wait, current);
  989. if (!*timeo) {
  990. if (!ssk || netlink_is_kernel(ssk))
  991. netlink_overrun(sk);
  992. sock_put(sk);
  993. kfree_skb(skb);
  994. return -EAGAIN;
  995. }
  996. __set_current_state(TASK_INTERRUPTIBLE);
  997. add_wait_queue(&nlk->wait, &wait);
  998. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  999. test_bit(NETLINK_S_CONGESTED, &nlk->state)) &&
  1000. !sock_flag(sk, SOCK_DEAD))
  1001. *timeo = schedule_timeout(*timeo);
  1002. __set_current_state(TASK_RUNNING);
  1003. remove_wait_queue(&nlk->wait, &wait);
  1004. sock_put(sk);
  1005. if (signal_pending(current)) {
  1006. kfree_skb(skb);
  1007. return sock_intr_errno(*timeo);
  1008. }
  1009. return 1;
  1010. }
  1011. netlink_skb_set_owner_r(skb, sk);
  1012. return 0;
  1013. }
  1014. static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1015. {
  1016. int len = skb->len;
  1017. netlink_deliver_tap(skb);
  1018. skb_queue_tail(&sk->sk_receive_queue, skb);
  1019. sk->sk_data_ready(sk);
  1020. return len;
  1021. }
  1022. int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
  1023. {
  1024. int len = __netlink_sendskb(sk, skb);
  1025. sock_put(sk);
  1026. return len;
  1027. }
  1028. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  1029. {
  1030. kfree_skb(skb);
  1031. sock_put(sk);
  1032. }
  1033. static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
  1034. {
  1035. int delta;
  1036. WARN_ON(skb->sk != NULL);
  1037. delta = skb->end - skb->tail;
  1038. if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
  1039. return skb;
  1040. if (skb_shared(skb)) {
  1041. struct sk_buff *nskb = skb_clone(skb, allocation);
  1042. if (!nskb)
  1043. return skb;
  1044. consume_skb(skb);
  1045. skb = nskb;
  1046. }
  1047. if (!pskb_expand_head(skb, 0, -delta, allocation))
  1048. skb->truesize -= delta;
  1049. return skb;
  1050. }
  1051. static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
  1052. struct sock *ssk)
  1053. {
  1054. int ret;
  1055. struct netlink_sock *nlk = nlk_sk(sk);
  1056. ret = -ECONNREFUSED;
  1057. if (nlk->netlink_rcv != NULL) {
  1058. ret = skb->len;
  1059. netlink_skb_set_owner_r(skb, sk);
  1060. NETLINK_CB(skb).sk = ssk;
  1061. netlink_deliver_tap_kernel(sk, ssk, skb);
  1062. nlk->netlink_rcv(skb);
  1063. consume_skb(skb);
  1064. } else {
  1065. kfree_skb(skb);
  1066. }
  1067. sock_put(sk);
  1068. return ret;
  1069. }
  1070. int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
  1071. u32 portid, int nonblock)
  1072. {
  1073. struct sock *sk;
  1074. int err;
  1075. long timeo;
  1076. skb = netlink_trim(skb, gfp_any());
  1077. timeo = sock_sndtimeo(ssk, nonblock);
  1078. retry:
  1079. sk = netlink_getsockbyportid(ssk, portid);
  1080. if (IS_ERR(sk)) {
  1081. kfree_skb(skb);
  1082. return PTR_ERR(sk);
  1083. }
  1084. if (netlink_is_kernel(sk))
  1085. return netlink_unicast_kernel(sk, skb, ssk);
  1086. if (sk_filter(sk, skb)) {
  1087. err = skb->len;
  1088. kfree_skb(skb);
  1089. sock_put(sk);
  1090. return err;
  1091. }
  1092. err = netlink_attachskb(sk, skb, &timeo, ssk);
  1093. if (err == 1)
  1094. goto retry;
  1095. if (err)
  1096. return err;
  1097. return netlink_sendskb(sk, skb);
  1098. }
  1099. EXPORT_SYMBOL(netlink_unicast);
  1100. struct sk_buff *__netlink_alloc_skb(struct sock *ssk, unsigned int size,
  1101. unsigned int ldiff, u32 dst_portid,
  1102. gfp_t gfp_mask)
  1103. {
  1104. return alloc_skb(size, gfp_mask);
  1105. }
  1106. EXPORT_SYMBOL_GPL(__netlink_alloc_skb);
  1107. int netlink_has_listeners(struct sock *sk, unsigned int group)
  1108. {
  1109. int res = 0;
  1110. struct listeners *listeners;
  1111. BUG_ON(!netlink_is_kernel(sk));
  1112. rcu_read_lock();
  1113. listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
  1114. if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
  1115. res = test_bit(group - 1, listeners->masks);
  1116. rcu_read_unlock();
  1117. return res;
  1118. }
  1119. EXPORT_SYMBOL_GPL(netlink_has_listeners);
  1120. static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  1121. {
  1122. struct netlink_sock *nlk = nlk_sk(sk);
  1123. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  1124. !test_bit(NETLINK_S_CONGESTED, &nlk->state)) {
  1125. netlink_skb_set_owner_r(skb, sk);
  1126. __netlink_sendskb(sk, skb);
  1127. return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
  1128. }
  1129. return -1;
  1130. }
  1131. struct netlink_broadcast_data {
  1132. struct sock *exclude_sk;
  1133. struct net *net;
  1134. u32 portid;
  1135. u32 group;
  1136. int failure;
  1137. int delivery_failure;
  1138. int congested;
  1139. int delivered;
  1140. gfp_t allocation;
  1141. struct sk_buff *skb, *skb2;
  1142. int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
  1143. void *tx_data;
  1144. };
  1145. static void do_one_broadcast(struct sock *sk,
  1146. struct netlink_broadcast_data *p)
  1147. {
  1148. struct netlink_sock *nlk = nlk_sk(sk);
  1149. int val;
  1150. if (p->exclude_sk == sk)
  1151. return;
  1152. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1153. !test_bit(p->group - 1, nlk->groups))
  1154. return;
  1155. if (!net_eq(sock_net(sk), p->net)) {
  1156. if (!(nlk->flags & NETLINK_F_LISTEN_ALL_NSID))
  1157. return;
  1158. if (!peernet_has_id(sock_net(sk), p->net))
  1159. return;
  1160. if (!file_ns_capable(sk->sk_socket->file, p->net->user_ns,
  1161. CAP_NET_BROADCAST))
  1162. return;
  1163. }
  1164. if (p->failure) {
  1165. netlink_overrun(sk);
  1166. return;
  1167. }
  1168. sock_hold(sk);
  1169. if (p->skb2 == NULL) {
  1170. if (skb_shared(p->skb)) {
  1171. p->skb2 = skb_clone(p->skb, p->allocation);
  1172. } else {
  1173. p->skb2 = skb_get(p->skb);
  1174. /*
  1175. * skb ownership may have been set when
  1176. * delivered to a previous socket.
  1177. */
  1178. skb_orphan(p->skb2);
  1179. }
  1180. }
  1181. if (p->skb2 == NULL) {
  1182. netlink_overrun(sk);
  1183. /* Clone failed. Notify ALL listeners. */
  1184. p->failure = 1;
  1185. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1186. p->delivery_failure = 1;
  1187. goto out;
  1188. }
  1189. if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
  1190. kfree_skb(p->skb2);
  1191. p->skb2 = NULL;
  1192. goto out;
  1193. }
  1194. if (sk_filter(sk, p->skb2)) {
  1195. kfree_skb(p->skb2);
  1196. p->skb2 = NULL;
  1197. goto out;
  1198. }
  1199. NETLINK_CB(p->skb2).nsid = peernet2id(sock_net(sk), p->net);
  1200. NETLINK_CB(p->skb2).nsid_is_set = true;
  1201. val = netlink_broadcast_deliver(sk, p->skb2);
  1202. if (val < 0) {
  1203. netlink_overrun(sk);
  1204. if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
  1205. p->delivery_failure = 1;
  1206. } else {
  1207. p->congested |= val;
  1208. p->delivered = 1;
  1209. p->skb2 = NULL;
  1210. }
  1211. out:
  1212. sock_put(sk);
  1213. }
  1214. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1215. u32 group, gfp_t allocation,
  1216. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  1217. void *filter_data)
  1218. {
  1219. struct net *net = sock_net(ssk);
  1220. struct netlink_broadcast_data info;
  1221. struct sock *sk;
  1222. skb = netlink_trim(skb, allocation);
  1223. info.exclude_sk = ssk;
  1224. info.net = net;
  1225. info.portid = portid;
  1226. info.group = group;
  1227. info.failure = 0;
  1228. info.delivery_failure = 0;
  1229. info.congested = 0;
  1230. info.delivered = 0;
  1231. info.allocation = allocation;
  1232. info.skb = skb;
  1233. info.skb2 = NULL;
  1234. info.tx_filter = filter;
  1235. info.tx_data = filter_data;
  1236. /* While we sleep in clone, do not allow to change socket list */
  1237. netlink_lock_table();
  1238. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1239. do_one_broadcast(sk, &info);
  1240. consume_skb(skb);
  1241. netlink_unlock_table();
  1242. if (info.delivery_failure) {
  1243. kfree_skb(info.skb2);
  1244. return -ENOBUFS;
  1245. }
  1246. consume_skb(info.skb2);
  1247. if (info.delivered) {
  1248. if (info.congested && gfpflags_allow_blocking(allocation))
  1249. yield();
  1250. return 0;
  1251. }
  1252. return -ESRCH;
  1253. }
  1254. EXPORT_SYMBOL(netlink_broadcast_filtered);
  1255. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
  1256. u32 group, gfp_t allocation)
  1257. {
  1258. return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
  1259. NULL, NULL);
  1260. }
  1261. EXPORT_SYMBOL(netlink_broadcast);
  1262. struct netlink_set_err_data {
  1263. struct sock *exclude_sk;
  1264. u32 portid;
  1265. u32 group;
  1266. int code;
  1267. };
  1268. static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
  1269. {
  1270. struct netlink_sock *nlk = nlk_sk(sk);
  1271. int ret = 0;
  1272. if (sk == p->exclude_sk)
  1273. goto out;
  1274. if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
  1275. goto out;
  1276. if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
  1277. !test_bit(p->group - 1, nlk->groups))
  1278. goto out;
  1279. if (p->code == ENOBUFS && nlk->flags & NETLINK_F_RECV_NO_ENOBUFS) {
  1280. ret = 1;
  1281. goto out;
  1282. }
  1283. sk->sk_err = p->code;
  1284. sk->sk_error_report(sk);
  1285. out:
  1286. return ret;
  1287. }
  1288. /**
  1289. * netlink_set_err - report error to broadcast listeners
  1290. * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
  1291. * @portid: the PORTID of a process that we want to skip (if any)
  1292. * @group: the broadcast group that will notice the error
  1293. * @code: error code, must be negative (as usual in kernelspace)
  1294. *
  1295. * This function returns the number of broadcast listeners that have set the
  1296. * NETLINK_NO_ENOBUFS socket option.
  1297. */
  1298. int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
  1299. {
  1300. struct netlink_set_err_data info;
  1301. struct sock *sk;
  1302. int ret = 0;
  1303. info.exclude_sk = ssk;
  1304. info.portid = portid;
  1305. info.group = group;
  1306. /* sk->sk_err wants a positive error value */
  1307. info.code = -code;
  1308. read_lock(&nl_table_lock);
  1309. sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
  1310. ret += do_one_set_err(sk, &info);
  1311. read_unlock(&nl_table_lock);
  1312. return ret;
  1313. }
  1314. EXPORT_SYMBOL(netlink_set_err);
  1315. /* must be called with netlink table grabbed */
  1316. static void netlink_update_socket_mc(struct netlink_sock *nlk,
  1317. unsigned int group,
  1318. int is_new)
  1319. {
  1320. int old, new = !!is_new, subscriptions;
  1321. old = test_bit(group - 1, nlk->groups);
  1322. subscriptions = nlk->subscriptions - old + new;
  1323. if (new)
  1324. __set_bit(group - 1, nlk->groups);
  1325. else
  1326. __clear_bit(group - 1, nlk->groups);
  1327. netlink_update_subscriptions(&nlk->sk, subscriptions);
  1328. netlink_update_listeners(&nlk->sk);
  1329. }
  1330. static int netlink_setsockopt(struct socket *sock, int level, int optname,
  1331. char __user *optval, unsigned int optlen)
  1332. {
  1333. struct sock *sk = sock->sk;
  1334. struct netlink_sock *nlk = nlk_sk(sk);
  1335. unsigned int val = 0;
  1336. int err;
  1337. if (level != SOL_NETLINK)
  1338. return -ENOPROTOOPT;
  1339. if (optlen >= sizeof(int) &&
  1340. get_user(val, (unsigned int __user *)optval))
  1341. return -EFAULT;
  1342. switch (optname) {
  1343. case NETLINK_PKTINFO:
  1344. if (val)
  1345. nlk->flags |= NETLINK_F_RECV_PKTINFO;
  1346. else
  1347. nlk->flags &= ~NETLINK_F_RECV_PKTINFO;
  1348. err = 0;
  1349. break;
  1350. case NETLINK_ADD_MEMBERSHIP:
  1351. case NETLINK_DROP_MEMBERSHIP: {
  1352. if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
  1353. return -EPERM;
  1354. err = netlink_realloc_groups(sk);
  1355. if (err)
  1356. return err;
  1357. if (!val || val - 1 >= nlk->ngroups)
  1358. return -EINVAL;
  1359. if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
  1360. err = nlk->netlink_bind(sock_net(sk), val);
  1361. if (err)
  1362. return err;
  1363. }
  1364. netlink_table_grab();
  1365. netlink_update_socket_mc(nlk, val,
  1366. optname == NETLINK_ADD_MEMBERSHIP);
  1367. netlink_table_ungrab();
  1368. if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
  1369. nlk->netlink_unbind(sock_net(sk), val);
  1370. err = 0;
  1371. break;
  1372. }
  1373. case NETLINK_BROADCAST_ERROR:
  1374. if (val)
  1375. nlk->flags |= NETLINK_F_BROADCAST_SEND_ERROR;
  1376. else
  1377. nlk->flags &= ~NETLINK_F_BROADCAST_SEND_ERROR;
  1378. err = 0;
  1379. break;
  1380. case NETLINK_NO_ENOBUFS:
  1381. if (val) {
  1382. nlk->flags |= NETLINK_F_RECV_NO_ENOBUFS;
  1383. clear_bit(NETLINK_S_CONGESTED, &nlk->state);
  1384. wake_up_interruptible(&nlk->wait);
  1385. } else {
  1386. nlk->flags &= ~NETLINK_F_RECV_NO_ENOBUFS;
  1387. }
  1388. err = 0;
  1389. break;
  1390. case NETLINK_LISTEN_ALL_NSID:
  1391. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_BROADCAST))
  1392. return -EPERM;
  1393. if (val)
  1394. nlk->flags |= NETLINK_F_LISTEN_ALL_NSID;
  1395. else
  1396. nlk->flags &= ~NETLINK_F_LISTEN_ALL_NSID;
  1397. err = 0;
  1398. break;
  1399. case NETLINK_CAP_ACK:
  1400. if (val)
  1401. nlk->flags |= NETLINK_F_CAP_ACK;
  1402. else
  1403. nlk->flags &= ~NETLINK_F_CAP_ACK;
  1404. err = 0;
  1405. break;
  1406. default:
  1407. err = -ENOPROTOOPT;
  1408. }
  1409. return err;
  1410. }
  1411. static int netlink_getsockopt(struct socket *sock, int level, int optname,
  1412. char __user *optval, int __user *optlen)
  1413. {
  1414. struct sock *sk = sock->sk;
  1415. struct netlink_sock *nlk = nlk_sk(sk);
  1416. int len, val, err;
  1417. if (level != SOL_NETLINK)
  1418. return -ENOPROTOOPT;
  1419. if (get_user(len, optlen))
  1420. return -EFAULT;
  1421. if (len < 0)
  1422. return -EINVAL;
  1423. switch (optname) {
  1424. case NETLINK_PKTINFO:
  1425. if (len < sizeof(int))
  1426. return -EINVAL;
  1427. len = sizeof(int);
  1428. val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
  1429. if (put_user(len, optlen) ||
  1430. put_user(val, optval))
  1431. return -EFAULT;
  1432. err = 0;
  1433. break;
  1434. case NETLINK_BROADCAST_ERROR:
  1435. if (len < sizeof(int))
  1436. return -EINVAL;
  1437. len = sizeof(int);
  1438. val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
  1439. if (put_user(len, optlen) ||
  1440. put_user(val, optval))
  1441. return -EFAULT;
  1442. err = 0;
  1443. break;
  1444. case NETLINK_NO_ENOBUFS:
  1445. if (len < sizeof(int))
  1446. return -EINVAL;
  1447. len = sizeof(int);
  1448. val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
  1449. if (put_user(len, optlen) ||
  1450. put_user(val, optval))
  1451. return -EFAULT;
  1452. err = 0;
  1453. break;
  1454. case NETLINK_LIST_MEMBERSHIPS: {
  1455. int pos, idx, shift;
  1456. err = 0;
  1457. netlink_lock_table();
  1458. for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
  1459. if (len - pos < sizeof(u32))
  1460. break;
  1461. idx = pos / sizeof(unsigned long);
  1462. shift = (pos % sizeof(unsigned long)) * 8;
  1463. if (put_user((u32)(nlk->groups[idx] >> shift),
  1464. (u32 __user *)(optval + pos))) {
  1465. err = -EFAULT;
  1466. break;
  1467. }
  1468. }
  1469. if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
  1470. err = -EFAULT;
  1471. netlink_unlock_table();
  1472. break;
  1473. }
  1474. case NETLINK_CAP_ACK:
  1475. if (len < sizeof(int))
  1476. return -EINVAL;
  1477. len = sizeof(int);
  1478. val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
  1479. if (put_user(len, optlen) ||
  1480. put_user(val, optval))
  1481. return -EFAULT;
  1482. err = 0;
  1483. break;
  1484. default:
  1485. err = -ENOPROTOOPT;
  1486. }
  1487. return err;
  1488. }
  1489. static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  1490. {
  1491. struct nl_pktinfo info;
  1492. info.group = NETLINK_CB(skb).dst_group;
  1493. put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
  1494. }
  1495. static void netlink_cmsg_listen_all_nsid(struct sock *sk, struct msghdr *msg,
  1496. struct sk_buff *skb)
  1497. {
  1498. if (!NETLINK_CB(skb).nsid_is_set)
  1499. return;
  1500. put_cmsg(msg, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, sizeof(int),
  1501. &NETLINK_CB(skb).nsid);
  1502. }
  1503. static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1504. {
  1505. struct sock *sk = sock->sk;
  1506. struct netlink_sock *nlk = nlk_sk(sk);
  1507. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1508. u32 dst_portid;
  1509. u32 dst_group;
  1510. struct sk_buff *skb;
  1511. int err;
  1512. struct scm_cookie scm;
  1513. u32 netlink_skb_flags = 0;
  1514. if (msg->msg_flags&MSG_OOB)
  1515. return -EOPNOTSUPP;
  1516. err = scm_send(sock, msg, &scm, true);
  1517. if (err < 0)
  1518. return err;
  1519. if (msg->msg_namelen) {
  1520. err = -EINVAL;
  1521. if (msg->msg_namelen < sizeof(struct sockaddr_nl))
  1522. goto out;
  1523. if (addr->nl_family != AF_NETLINK)
  1524. goto out;
  1525. dst_portid = addr->nl_pid;
  1526. dst_group = ffs(addr->nl_groups);
  1527. err = -EPERM;
  1528. if ((dst_group || dst_portid) &&
  1529. !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
  1530. goto out;
  1531. netlink_skb_flags |= NETLINK_SKB_DST;
  1532. } else {
  1533. dst_portid = nlk->dst_portid;
  1534. dst_group = nlk->dst_group;
  1535. }
  1536. if (!nlk->bound) {
  1537. err = netlink_autobind(sock);
  1538. if (err)
  1539. goto out;
  1540. } else {
  1541. /* Ensure nlk is hashed and visible. */
  1542. smp_rmb();
  1543. }
  1544. err = -EMSGSIZE;
  1545. if (len > sk->sk_sndbuf - 32)
  1546. goto out;
  1547. err = -ENOBUFS;
  1548. skb = netlink_alloc_large_skb(len, dst_group);
  1549. if (skb == NULL)
  1550. goto out;
  1551. NETLINK_CB(skb).portid = nlk->portid;
  1552. NETLINK_CB(skb).dst_group = dst_group;
  1553. NETLINK_CB(skb).creds = scm.creds;
  1554. NETLINK_CB(skb).flags = netlink_skb_flags;
  1555. err = -EFAULT;
  1556. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1557. kfree_skb(skb);
  1558. goto out;
  1559. }
  1560. err = security_netlink_send(sk, skb);
  1561. if (err) {
  1562. kfree_skb(skb);
  1563. goto out;
  1564. }
  1565. if (dst_group) {
  1566. atomic_inc(&skb->users);
  1567. netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
  1568. }
  1569. err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
  1570. out:
  1571. scm_destroy(&scm);
  1572. return err;
  1573. }
  1574. static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1575. int flags)
  1576. {
  1577. struct scm_cookie scm;
  1578. struct sock *sk = sock->sk;
  1579. struct netlink_sock *nlk = nlk_sk(sk);
  1580. int noblock = flags&MSG_DONTWAIT;
  1581. size_t copied;
  1582. struct sk_buff *skb, *data_skb;
  1583. int err, ret;
  1584. if (flags&MSG_OOB)
  1585. return -EOPNOTSUPP;
  1586. copied = 0;
  1587. skb = skb_recv_datagram(sk, flags, noblock, &err);
  1588. if (skb == NULL)
  1589. goto out;
  1590. data_skb = skb;
  1591. #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
  1592. if (unlikely(skb_shinfo(skb)->frag_list)) {
  1593. /*
  1594. * If this skb has a frag_list, then here that means that we
  1595. * will have to use the frag_list skb's data for compat tasks
  1596. * and the regular skb's data for normal (non-compat) tasks.
  1597. *
  1598. * If we need to send the compat skb, assign it to the
  1599. * 'data_skb' variable so that it will be used below for data
  1600. * copying. We keep 'skb' for everything else, including
  1601. * freeing both later.
  1602. */
  1603. if (flags & MSG_CMSG_COMPAT)
  1604. data_skb = skb_shinfo(skb)->frag_list;
  1605. }
  1606. #endif
  1607. /* Record the max length of recvmsg() calls for future allocations */
  1608. nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
  1609. nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
  1610. SKB_WITH_OVERHEAD(32768));
  1611. copied = data_skb->len;
  1612. if (len < copied) {
  1613. msg->msg_flags |= MSG_TRUNC;
  1614. copied = len;
  1615. }
  1616. skb_reset_transport_header(data_skb);
  1617. err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
  1618. if (msg->msg_name) {
  1619. DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
  1620. addr->nl_family = AF_NETLINK;
  1621. addr->nl_pad = 0;
  1622. addr->nl_pid = NETLINK_CB(skb).portid;
  1623. addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
  1624. msg->msg_namelen = sizeof(*addr);
  1625. }
  1626. if (nlk->flags & NETLINK_F_RECV_PKTINFO)
  1627. netlink_cmsg_recv_pktinfo(msg, skb);
  1628. if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
  1629. netlink_cmsg_listen_all_nsid(sk, msg, skb);
  1630. memset(&scm, 0, sizeof(scm));
  1631. scm.creds = *NETLINK_CREDS(skb);
  1632. if (flags & MSG_TRUNC)
  1633. copied = data_skb->len;
  1634. skb_free_datagram(sk, skb);
  1635. if (nlk->cb_running &&
  1636. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
  1637. ret = netlink_dump(sk);
  1638. if (ret) {
  1639. sk->sk_err = -ret;
  1640. sk->sk_error_report(sk);
  1641. }
  1642. }
  1643. scm_recv(sock, msg, &scm, flags);
  1644. out:
  1645. netlink_rcv_wake(sk);
  1646. return err ? : copied;
  1647. }
  1648. static void netlink_data_ready(struct sock *sk)
  1649. {
  1650. BUG();
  1651. }
  1652. /*
  1653. * We export these functions to other modules. They provide a
  1654. * complete set of kernel non-blocking support for message
  1655. * queueing.
  1656. */
  1657. struct sock *
  1658. __netlink_kernel_create(struct net *net, int unit, struct module *module,
  1659. struct netlink_kernel_cfg *cfg)
  1660. {
  1661. struct socket *sock;
  1662. struct sock *sk;
  1663. struct netlink_sock *nlk;
  1664. struct listeners *listeners = NULL;
  1665. struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
  1666. unsigned int groups;
  1667. BUG_ON(!nl_table);
  1668. if (unit < 0 || unit >= MAX_LINKS)
  1669. return NULL;
  1670. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  1671. return NULL;
  1672. if (__netlink_create(net, sock, cb_mutex, unit, 1) < 0)
  1673. goto out_sock_release_nosk;
  1674. sk = sock->sk;
  1675. if (!cfg || cfg->groups < 32)
  1676. groups = 32;
  1677. else
  1678. groups = cfg->groups;
  1679. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  1680. if (!listeners)
  1681. goto out_sock_release;
  1682. sk->sk_data_ready = netlink_data_ready;
  1683. if (cfg && cfg->input)
  1684. nlk_sk(sk)->netlink_rcv = cfg->input;
  1685. if (netlink_insert(sk, 0))
  1686. goto out_sock_release;
  1687. nlk = nlk_sk(sk);
  1688. nlk->flags |= NETLINK_F_KERNEL_SOCKET;
  1689. netlink_table_grab();
  1690. if (!nl_table[unit].registered) {
  1691. nl_table[unit].groups = groups;
  1692. rcu_assign_pointer(nl_table[unit].listeners, listeners);
  1693. nl_table[unit].cb_mutex = cb_mutex;
  1694. nl_table[unit].module = module;
  1695. if (cfg) {
  1696. nl_table[unit].bind = cfg->bind;
  1697. nl_table[unit].unbind = cfg->unbind;
  1698. nl_table[unit].flags = cfg->flags;
  1699. if (cfg->compare)
  1700. nl_table[unit].compare = cfg->compare;
  1701. }
  1702. nl_table[unit].registered = 1;
  1703. } else {
  1704. kfree(listeners);
  1705. nl_table[unit].registered++;
  1706. }
  1707. netlink_table_ungrab();
  1708. return sk;
  1709. out_sock_release:
  1710. kfree(listeners);
  1711. netlink_kernel_release(sk);
  1712. return NULL;
  1713. out_sock_release_nosk:
  1714. sock_release(sock);
  1715. return NULL;
  1716. }
  1717. EXPORT_SYMBOL(__netlink_kernel_create);
  1718. void
  1719. netlink_kernel_release(struct sock *sk)
  1720. {
  1721. if (sk == NULL || sk->sk_socket == NULL)
  1722. return;
  1723. sock_release(sk->sk_socket);
  1724. }
  1725. EXPORT_SYMBOL(netlink_kernel_release);
  1726. int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1727. {
  1728. struct listeners *new, *old;
  1729. struct netlink_table *tbl = &nl_table[sk->sk_protocol];
  1730. if (groups < 32)
  1731. groups = 32;
  1732. if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
  1733. new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
  1734. if (!new)
  1735. return -ENOMEM;
  1736. old = nl_deref_protected(tbl->listeners);
  1737. memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
  1738. rcu_assign_pointer(tbl->listeners, new);
  1739. kfree_rcu(old, rcu);
  1740. }
  1741. tbl->groups = groups;
  1742. return 0;
  1743. }
  1744. /**
  1745. * netlink_change_ngroups - change number of multicast groups
  1746. *
  1747. * This changes the number of multicast groups that are available
  1748. * on a certain netlink family. Note that it is not possible to
  1749. * change the number of groups to below 32. Also note that it does
  1750. * not implicitly call netlink_clear_multicast_users() when the
  1751. * number of groups is reduced.
  1752. *
  1753. * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
  1754. * @groups: The new number of groups.
  1755. */
  1756. int netlink_change_ngroups(struct sock *sk, unsigned int groups)
  1757. {
  1758. int err;
  1759. netlink_table_grab();
  1760. err = __netlink_change_ngroups(sk, groups);
  1761. netlink_table_ungrab();
  1762. return err;
  1763. }
  1764. void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
  1765. {
  1766. struct sock *sk;
  1767. struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
  1768. sk_for_each_bound(sk, &tbl->mc_list)
  1769. netlink_update_socket_mc(nlk_sk(sk), group, 0);
  1770. }
  1771. struct nlmsghdr *
  1772. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
  1773. {
  1774. struct nlmsghdr *nlh;
  1775. int size = nlmsg_msg_size(len);
  1776. nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
  1777. nlh->nlmsg_type = type;
  1778. nlh->nlmsg_len = size;
  1779. nlh->nlmsg_flags = flags;
  1780. nlh->nlmsg_pid = portid;
  1781. nlh->nlmsg_seq = seq;
  1782. if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
  1783. memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
  1784. return nlh;
  1785. }
  1786. EXPORT_SYMBOL(__nlmsg_put);
  1787. /*
  1788. * It looks a bit ugly.
  1789. * It would be better to create kernel thread.
  1790. */
  1791. static int netlink_dump(struct sock *sk)
  1792. {
  1793. struct netlink_sock *nlk = nlk_sk(sk);
  1794. struct netlink_callback *cb;
  1795. struct sk_buff *skb = NULL;
  1796. struct nlmsghdr *nlh;
  1797. struct module *module;
  1798. int err = -ENOBUFS;
  1799. int alloc_min_size;
  1800. int alloc_size;
  1801. mutex_lock(nlk->cb_mutex);
  1802. if (!nlk->cb_running) {
  1803. err = -EINVAL;
  1804. goto errout_skb;
  1805. }
  1806. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1807. goto errout_skb;
  1808. /* NLMSG_GOODSIZE is small to avoid high order allocations being
  1809. * required, but it makes sense to _attempt_ a 16K bytes allocation
  1810. * to reduce number of system calls on dump operations, if user
  1811. * ever provided a big enough buffer.
  1812. */
  1813. cb = &nlk->cb;
  1814. alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
  1815. if (alloc_min_size < nlk->max_recvmsg_len) {
  1816. alloc_size = nlk->max_recvmsg_len;
  1817. skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
  1818. (GFP_KERNEL & ~__GFP_DIRECT_RECLAIM) |
  1819. __GFP_NOWARN | __GFP_NORETRY);
  1820. }
  1821. if (!skb) {
  1822. alloc_size = alloc_min_size;
  1823. skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
  1824. GFP_KERNEL);
  1825. }
  1826. if (!skb)
  1827. goto errout_skb;
  1828. /* Trim skb to allocated size. User is expected to provide buffer as
  1829. * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
  1830. * netlink_recvmsg())). dump will pack as many smaller messages as
  1831. * could fit within the allocated skb. skb is typically allocated
  1832. * with larger space than required (could be as much as near 2x the
  1833. * requested size with align to next power of 2 approach). Allowing
  1834. * dump to use the excess space makes it difficult for a user to have a
  1835. * reasonable static buffer based on the expected largest dump of a
  1836. * single netdev. The outcome is MSG_TRUNC error.
  1837. */
  1838. skb_reserve(skb, skb_tailroom(skb) - alloc_size);
  1839. netlink_skb_set_owner_r(skb, sk);
  1840. if (nlk->dump_done_errno > 0)
  1841. nlk->dump_done_errno = cb->dump(skb, cb);
  1842. if (nlk->dump_done_errno > 0 ||
  1843. skb_tailroom(skb) < nlmsg_total_size(sizeof(nlk->dump_done_errno))) {
  1844. mutex_unlock(nlk->cb_mutex);
  1845. if (sk_filter(sk, skb))
  1846. kfree_skb(skb);
  1847. else
  1848. __netlink_sendskb(sk, skb);
  1849. return 0;
  1850. }
  1851. nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE,
  1852. sizeof(nlk->dump_done_errno), NLM_F_MULTI);
  1853. if (WARN_ON(!nlh))
  1854. goto errout_skb;
  1855. nl_dump_check_consistent(cb, nlh);
  1856. memcpy(nlmsg_data(nlh), &nlk->dump_done_errno,
  1857. sizeof(nlk->dump_done_errno));
  1858. if (sk_filter(sk, skb))
  1859. kfree_skb(skb);
  1860. else
  1861. __netlink_sendskb(sk, skb);
  1862. if (cb->done)
  1863. cb->done(cb);
  1864. nlk->cb_running = false;
  1865. module = cb->module;
  1866. skb = cb->skb;
  1867. mutex_unlock(nlk->cb_mutex);
  1868. module_put(module);
  1869. consume_skb(skb);
  1870. return 0;
  1871. errout_skb:
  1872. mutex_unlock(nlk->cb_mutex);
  1873. kfree_skb(skb);
  1874. return err;
  1875. }
  1876. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  1877. const struct nlmsghdr *nlh,
  1878. struct netlink_dump_control *control)
  1879. {
  1880. struct netlink_callback *cb;
  1881. struct sock *sk;
  1882. struct netlink_sock *nlk;
  1883. int ret;
  1884. atomic_inc(&skb->users);
  1885. sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
  1886. if (sk == NULL) {
  1887. ret = -ECONNREFUSED;
  1888. goto error_free;
  1889. }
  1890. nlk = nlk_sk(sk);
  1891. mutex_lock(nlk->cb_mutex);
  1892. /* A dump is in progress... */
  1893. if (nlk->cb_running) {
  1894. ret = -EBUSY;
  1895. goto error_unlock;
  1896. }
  1897. /* add reference of module which cb->dump belongs to */
  1898. if (!try_module_get(control->module)) {
  1899. ret = -EPROTONOSUPPORT;
  1900. goto error_unlock;
  1901. }
  1902. cb = &nlk->cb;
  1903. memset(cb, 0, sizeof(*cb));
  1904. cb->start = control->start;
  1905. cb->dump = control->dump;
  1906. cb->done = control->done;
  1907. cb->nlh = nlh;
  1908. cb->data = control->data;
  1909. cb->module = control->module;
  1910. cb->min_dump_alloc = control->min_dump_alloc;
  1911. cb->skb = skb;
  1912. nlk->cb_running = true;
  1913. nlk->dump_done_errno = INT_MAX;
  1914. mutex_unlock(nlk->cb_mutex);
  1915. if (cb->start)
  1916. cb->start(cb);
  1917. ret = netlink_dump(sk);
  1918. sock_put(sk);
  1919. if (ret)
  1920. return ret;
  1921. /* We successfully started a dump, by returning -EINTR we
  1922. * signal not to send ACK even if it was requested.
  1923. */
  1924. return -EINTR;
  1925. error_unlock:
  1926. sock_put(sk);
  1927. mutex_unlock(nlk->cb_mutex);
  1928. error_free:
  1929. kfree_skb(skb);
  1930. return ret;
  1931. }
  1932. EXPORT_SYMBOL(__netlink_dump_start);
  1933. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  1934. {
  1935. struct sk_buff *skb;
  1936. struct nlmsghdr *rep;
  1937. struct nlmsgerr *errmsg;
  1938. size_t payload = sizeof(*errmsg);
  1939. struct netlink_sock *nlk = nlk_sk(NETLINK_CB(in_skb).sk);
  1940. /* Error messages get the original request appened, unless the user
  1941. * requests to cap the error message.
  1942. */
  1943. if (!(nlk->flags & NETLINK_F_CAP_ACK) && err)
  1944. payload += nlmsg_len(nlh);
  1945. skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
  1946. NETLINK_CB(in_skb).portid, GFP_KERNEL);
  1947. if (!skb) {
  1948. struct sock *sk;
  1949. sk = netlink_lookup(sock_net(in_skb->sk),
  1950. in_skb->sk->sk_protocol,
  1951. NETLINK_CB(in_skb).portid);
  1952. if (sk) {
  1953. sk->sk_err = ENOBUFS;
  1954. sk->sk_error_report(sk);
  1955. sock_put(sk);
  1956. }
  1957. return;
  1958. }
  1959. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  1960. NLMSG_ERROR, payload, 0);
  1961. errmsg = nlmsg_data(rep);
  1962. errmsg->error = err;
  1963. memcpy(&errmsg->msg, nlh, payload > sizeof(*errmsg) ? nlh->nlmsg_len : sizeof(*nlh));
  1964. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
  1965. }
  1966. EXPORT_SYMBOL(netlink_ack);
  1967. int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  1968. struct nlmsghdr *))
  1969. {
  1970. struct nlmsghdr *nlh;
  1971. int err;
  1972. while (skb->len >= nlmsg_total_size(0)) {
  1973. int msglen;
  1974. nlh = nlmsg_hdr(skb);
  1975. err = 0;
  1976. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  1977. return 0;
  1978. /* Only requests are handled by the kernel */
  1979. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1980. goto ack;
  1981. /* Skip control messages */
  1982. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  1983. goto ack;
  1984. err = cb(skb, nlh);
  1985. if (err == -EINTR)
  1986. goto skip;
  1987. ack:
  1988. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  1989. netlink_ack(skb, nlh, err);
  1990. skip:
  1991. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  1992. if (msglen > skb->len)
  1993. msglen = skb->len;
  1994. skb_pull(skb, msglen);
  1995. }
  1996. return 0;
  1997. }
  1998. EXPORT_SYMBOL(netlink_rcv_skb);
  1999. /**
  2000. * nlmsg_notify - send a notification netlink message
  2001. * @sk: netlink socket to use
  2002. * @skb: notification message
  2003. * @portid: destination netlink portid for reports or 0
  2004. * @group: destination multicast group or 0
  2005. * @report: 1 to report back, 0 to disable
  2006. * @flags: allocation flags
  2007. */
  2008. int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
  2009. unsigned int group, int report, gfp_t flags)
  2010. {
  2011. int err = 0;
  2012. if (group) {
  2013. int exclude_portid = 0;
  2014. if (report) {
  2015. atomic_inc(&skb->users);
  2016. exclude_portid = portid;
  2017. }
  2018. /* errors reported via destination sk->sk_err, but propagate
  2019. * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
  2020. err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
  2021. }
  2022. if (report) {
  2023. int err2;
  2024. err2 = nlmsg_unicast(sk, skb, portid);
  2025. if (!err || err == -ESRCH)
  2026. err = err2;
  2027. }
  2028. return err;
  2029. }
  2030. EXPORT_SYMBOL(nlmsg_notify);
  2031. #ifdef CONFIG_PROC_FS
  2032. struct nl_seq_iter {
  2033. struct seq_net_private p;
  2034. struct rhashtable_iter hti;
  2035. int link;
  2036. };
  2037. static int netlink_walk_start(struct nl_seq_iter *iter)
  2038. {
  2039. int err;
  2040. err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti);
  2041. if (err) {
  2042. iter->link = MAX_LINKS;
  2043. return err;
  2044. }
  2045. err = rhashtable_walk_start(&iter->hti);
  2046. return err == -EAGAIN ? 0 : err;
  2047. }
  2048. static void netlink_walk_stop(struct nl_seq_iter *iter)
  2049. {
  2050. rhashtable_walk_stop(&iter->hti);
  2051. rhashtable_walk_exit(&iter->hti);
  2052. }
  2053. static void *__netlink_seq_next(struct seq_file *seq)
  2054. {
  2055. struct nl_seq_iter *iter = seq->private;
  2056. struct netlink_sock *nlk;
  2057. do {
  2058. for (;;) {
  2059. int err;
  2060. nlk = rhashtable_walk_next(&iter->hti);
  2061. if (IS_ERR(nlk)) {
  2062. if (PTR_ERR(nlk) == -EAGAIN)
  2063. continue;
  2064. return nlk;
  2065. }
  2066. if (nlk)
  2067. break;
  2068. netlink_walk_stop(iter);
  2069. if (++iter->link >= MAX_LINKS)
  2070. return NULL;
  2071. err = netlink_walk_start(iter);
  2072. if (err)
  2073. return ERR_PTR(err);
  2074. }
  2075. } while (sock_net(&nlk->sk) != seq_file_net(seq));
  2076. return nlk;
  2077. }
  2078. static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
  2079. {
  2080. struct nl_seq_iter *iter = seq->private;
  2081. void *obj = SEQ_START_TOKEN;
  2082. loff_t pos;
  2083. int err;
  2084. iter->link = 0;
  2085. err = netlink_walk_start(iter);
  2086. if (err)
  2087. return ERR_PTR(err);
  2088. for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
  2089. obj = __netlink_seq_next(seq);
  2090. return obj;
  2091. }
  2092. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2093. {
  2094. ++*pos;
  2095. return __netlink_seq_next(seq);
  2096. }
  2097. static void netlink_seq_stop(struct seq_file *seq, void *v)
  2098. {
  2099. struct nl_seq_iter *iter = seq->private;
  2100. if (iter->link >= MAX_LINKS)
  2101. return;
  2102. netlink_walk_stop(iter);
  2103. }
  2104. static int netlink_seq_show(struct seq_file *seq, void *v)
  2105. {
  2106. if (v == SEQ_START_TOKEN) {
  2107. seq_puts(seq,
  2108. "sk Eth Pid Groups "
  2109. "Rmem Wmem Dump Locks Drops Inode\n");
  2110. } else {
  2111. struct sock *s = v;
  2112. struct netlink_sock *nlk = nlk_sk(s);
  2113. seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
  2114. s,
  2115. s->sk_protocol,
  2116. nlk->portid,
  2117. nlk->groups ? (u32)nlk->groups[0] : 0,
  2118. sk_rmem_alloc_get(s),
  2119. sk_wmem_alloc_get(s),
  2120. nlk->cb_running,
  2121. atomic_read(&s->sk_refcnt),
  2122. atomic_read(&s->sk_drops),
  2123. sock_i_ino(s)
  2124. );
  2125. }
  2126. return 0;
  2127. }
  2128. static const struct seq_operations netlink_seq_ops = {
  2129. .start = netlink_seq_start,
  2130. .next = netlink_seq_next,
  2131. .stop = netlink_seq_stop,
  2132. .show = netlink_seq_show,
  2133. };
  2134. static int netlink_seq_open(struct inode *inode, struct file *file)
  2135. {
  2136. return seq_open_net(inode, file, &netlink_seq_ops,
  2137. sizeof(struct nl_seq_iter));
  2138. }
  2139. static const struct file_operations netlink_seq_fops = {
  2140. .owner = THIS_MODULE,
  2141. .open = netlink_seq_open,
  2142. .read = seq_read,
  2143. .llseek = seq_lseek,
  2144. .release = seq_release_net,
  2145. };
  2146. #endif
  2147. int netlink_register_notifier(struct notifier_block *nb)
  2148. {
  2149. return atomic_notifier_chain_register(&netlink_chain, nb);
  2150. }
  2151. EXPORT_SYMBOL(netlink_register_notifier);
  2152. int netlink_unregister_notifier(struct notifier_block *nb)
  2153. {
  2154. return atomic_notifier_chain_unregister(&netlink_chain, nb);
  2155. }
  2156. EXPORT_SYMBOL(netlink_unregister_notifier);
  2157. static const struct proto_ops netlink_ops = {
  2158. .family = PF_NETLINK,
  2159. .owner = THIS_MODULE,
  2160. .release = netlink_release,
  2161. .bind = netlink_bind,
  2162. .connect = netlink_connect,
  2163. .socketpair = sock_no_socketpair,
  2164. .accept = sock_no_accept,
  2165. .getname = netlink_getname,
  2166. .poll = datagram_poll,
  2167. .ioctl = sock_no_ioctl,
  2168. .listen = sock_no_listen,
  2169. .shutdown = sock_no_shutdown,
  2170. .setsockopt = netlink_setsockopt,
  2171. .getsockopt = netlink_getsockopt,
  2172. .sendmsg = netlink_sendmsg,
  2173. .recvmsg = netlink_recvmsg,
  2174. .mmap = sock_no_mmap,
  2175. .sendpage = sock_no_sendpage,
  2176. };
  2177. static const struct net_proto_family netlink_family_ops = {
  2178. .family = PF_NETLINK,
  2179. .create = netlink_create,
  2180. .owner = THIS_MODULE, /* for consistency 8) */
  2181. };
  2182. static int __net_init netlink_net_init(struct net *net)
  2183. {
  2184. #ifdef CONFIG_PROC_FS
  2185. if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
  2186. return -ENOMEM;
  2187. #endif
  2188. return 0;
  2189. }
  2190. static void __net_exit netlink_net_exit(struct net *net)
  2191. {
  2192. #ifdef CONFIG_PROC_FS
  2193. remove_proc_entry("netlink", net->proc_net);
  2194. #endif
  2195. }
  2196. static void __init netlink_add_usersock_entry(void)
  2197. {
  2198. struct listeners *listeners;
  2199. int groups = 32;
  2200. listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
  2201. if (!listeners)
  2202. panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
  2203. netlink_table_grab();
  2204. nl_table[NETLINK_USERSOCK].groups = groups;
  2205. rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
  2206. nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
  2207. nl_table[NETLINK_USERSOCK].registered = 1;
  2208. nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
  2209. netlink_table_ungrab();
  2210. }
  2211. static struct pernet_operations __net_initdata netlink_net_ops = {
  2212. .init = netlink_net_init,
  2213. .exit = netlink_net_exit,
  2214. };
  2215. static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
  2216. {
  2217. const struct netlink_sock *nlk = data;
  2218. struct netlink_compare_arg arg;
  2219. netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
  2220. return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
  2221. }
  2222. static const struct rhashtable_params netlink_rhashtable_params = {
  2223. .head_offset = offsetof(struct netlink_sock, node),
  2224. .key_len = netlink_compare_arg_len,
  2225. .obj_hashfn = netlink_hash,
  2226. .obj_cmpfn = netlink_compare,
  2227. .automatic_shrinking = true,
  2228. };
  2229. static int __init netlink_proto_init(void)
  2230. {
  2231. int i;
  2232. int err = proto_register(&netlink_proto, 0);
  2233. if (err != 0)
  2234. goto out;
  2235. BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
  2236. nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
  2237. if (!nl_table)
  2238. goto panic;
  2239. for (i = 0; i < MAX_LINKS; i++) {
  2240. if (rhashtable_init(&nl_table[i].hash,
  2241. &netlink_rhashtable_params) < 0) {
  2242. while (--i > 0)
  2243. rhashtable_destroy(&nl_table[i].hash);
  2244. kfree(nl_table);
  2245. goto panic;
  2246. }
  2247. }
  2248. INIT_LIST_HEAD(&netlink_tap_all);
  2249. netlink_add_usersock_entry();
  2250. sock_register(&netlink_family_ops);
  2251. register_pernet_subsys(&netlink_net_ops);
  2252. /* The netlink device handler may be needed early. */
  2253. rtnetlink_init();
  2254. out:
  2255. return err;
  2256. panic:
  2257. panic("netlink_init: Cannot allocate nl_table\n");
  2258. }
  2259. core_initcall(netlink_proto_init);