af_netlink.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /*
  2. * NETLINK Kernel-user communication protocol.
  3. *
  4. * Authors: Alan Cox <alan@redhat.com>
  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. *
  17. */
  18. #include <linux/config.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/signal.h>
  23. #include <linux/sched.h>
  24. #include <linux/errno.h>
  25. #include <linux/string.h>
  26. #include <linux/stat.h>
  27. #include <linux/socket.h>
  28. #include <linux/un.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/termios.h>
  31. #include <linux/sockios.h>
  32. #include <linux/net.h>
  33. #include <linux/fs.h>
  34. #include <linux/slab.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/rtnetlink.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/seq_file.h>
  41. #include <linux/smp_lock.h>
  42. #include <linux/notifier.h>
  43. #include <linux/security.h>
  44. #include <linux/jhash.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/random.h>
  47. #include <linux/bitops.h>
  48. #include <linux/mm.h>
  49. #include <linux/types.h>
  50. #include <linux/audit.h>
  51. #include <net/sock.h>
  52. #include <net/scm.h>
  53. #define Nprintk(a...)
  54. struct netlink_sock {
  55. /* struct sock has to be the first member of netlink_sock */
  56. struct sock sk;
  57. u32 pid;
  58. unsigned int groups;
  59. u32 dst_pid;
  60. unsigned int dst_groups;
  61. unsigned long state;
  62. wait_queue_head_t wait;
  63. struct netlink_callback *cb;
  64. spinlock_t cb_lock;
  65. void (*data_ready)(struct sock *sk, int bytes);
  66. };
  67. static inline struct netlink_sock *nlk_sk(struct sock *sk)
  68. {
  69. return (struct netlink_sock *)sk;
  70. }
  71. struct nl_pid_hash {
  72. struct hlist_head *table;
  73. unsigned long rehash_time;
  74. unsigned int mask;
  75. unsigned int shift;
  76. unsigned int entries;
  77. unsigned int max_shift;
  78. u32 rnd;
  79. };
  80. struct netlink_table {
  81. struct nl_pid_hash hash;
  82. struct hlist_head mc_list;
  83. unsigned int nl_nonroot;
  84. };
  85. static struct netlink_table *nl_table;
  86. static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
  87. static int netlink_dump(struct sock *sk);
  88. static void netlink_destroy_callback(struct netlink_callback *cb);
  89. static DEFINE_RWLOCK(nl_table_lock);
  90. static atomic_t nl_table_users = ATOMIC_INIT(0);
  91. static struct notifier_block *netlink_chain;
  92. static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid)
  93. {
  94. return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask];
  95. }
  96. static void netlink_sock_destruct(struct sock *sk)
  97. {
  98. skb_queue_purge(&sk->sk_receive_queue);
  99. if (!sock_flag(sk, SOCK_DEAD)) {
  100. printk("Freeing alive netlink socket %p\n", sk);
  101. return;
  102. }
  103. BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
  104. BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
  105. BUG_TRAP(!nlk_sk(sk)->cb);
  106. }
  107. /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
  108. * Look, when several writers sleep and reader wakes them up, all but one
  109. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  110. * this, _but_ remember, it adds useless work on UP machines.
  111. */
  112. static void netlink_table_grab(void)
  113. {
  114. write_lock_bh(&nl_table_lock);
  115. if (atomic_read(&nl_table_users)) {
  116. DECLARE_WAITQUEUE(wait, current);
  117. add_wait_queue_exclusive(&nl_table_wait, &wait);
  118. for(;;) {
  119. set_current_state(TASK_UNINTERRUPTIBLE);
  120. if (atomic_read(&nl_table_users) == 0)
  121. break;
  122. write_unlock_bh(&nl_table_lock);
  123. schedule();
  124. write_lock_bh(&nl_table_lock);
  125. }
  126. __set_current_state(TASK_RUNNING);
  127. remove_wait_queue(&nl_table_wait, &wait);
  128. }
  129. }
  130. static __inline__ void netlink_table_ungrab(void)
  131. {
  132. write_unlock_bh(&nl_table_lock);
  133. wake_up(&nl_table_wait);
  134. }
  135. static __inline__ void
  136. netlink_lock_table(void)
  137. {
  138. /* read_lock() synchronizes us to netlink_table_grab */
  139. read_lock(&nl_table_lock);
  140. atomic_inc(&nl_table_users);
  141. read_unlock(&nl_table_lock);
  142. }
  143. static __inline__ void
  144. netlink_unlock_table(void)
  145. {
  146. if (atomic_dec_and_test(&nl_table_users))
  147. wake_up(&nl_table_wait);
  148. }
  149. static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
  150. {
  151. struct nl_pid_hash *hash = &nl_table[protocol].hash;
  152. struct hlist_head *head;
  153. struct sock *sk;
  154. struct hlist_node *node;
  155. read_lock(&nl_table_lock);
  156. head = nl_pid_hashfn(hash, pid);
  157. sk_for_each(sk, node, head) {
  158. if (nlk_sk(sk)->pid == pid) {
  159. sock_hold(sk);
  160. goto found;
  161. }
  162. }
  163. sk = NULL;
  164. found:
  165. read_unlock(&nl_table_lock);
  166. return sk;
  167. }
  168. static inline struct hlist_head *nl_pid_hash_alloc(size_t size)
  169. {
  170. if (size <= PAGE_SIZE)
  171. return kmalloc(size, GFP_ATOMIC);
  172. else
  173. return (struct hlist_head *)
  174. __get_free_pages(GFP_ATOMIC, get_order(size));
  175. }
  176. static inline void nl_pid_hash_free(struct hlist_head *table, size_t size)
  177. {
  178. if (size <= PAGE_SIZE)
  179. kfree(table);
  180. else
  181. free_pages((unsigned long)table, get_order(size));
  182. }
  183. static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow)
  184. {
  185. unsigned int omask, mask, shift;
  186. size_t osize, size;
  187. struct hlist_head *otable, *table;
  188. int i;
  189. omask = mask = hash->mask;
  190. osize = size = (mask + 1) * sizeof(*table);
  191. shift = hash->shift;
  192. if (grow) {
  193. if (++shift > hash->max_shift)
  194. return 0;
  195. mask = mask * 2 + 1;
  196. size *= 2;
  197. }
  198. table = nl_pid_hash_alloc(size);
  199. if (!table)
  200. return 0;
  201. memset(table, 0, size);
  202. otable = hash->table;
  203. hash->table = table;
  204. hash->mask = mask;
  205. hash->shift = shift;
  206. get_random_bytes(&hash->rnd, sizeof(hash->rnd));
  207. for (i = 0; i <= omask; i++) {
  208. struct sock *sk;
  209. struct hlist_node *node, *tmp;
  210. sk_for_each_safe(sk, node, tmp, &otable[i])
  211. __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid));
  212. }
  213. nl_pid_hash_free(otable, osize);
  214. hash->rehash_time = jiffies + 10 * 60 * HZ;
  215. return 1;
  216. }
  217. static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len)
  218. {
  219. int avg = hash->entries >> hash->shift;
  220. if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1))
  221. return 1;
  222. if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
  223. nl_pid_hash_rehash(hash, 0);
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. static struct proto_ops netlink_ops;
  229. static int netlink_insert(struct sock *sk, u32 pid)
  230. {
  231. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  232. struct hlist_head *head;
  233. int err = -EADDRINUSE;
  234. struct sock *osk;
  235. struct hlist_node *node;
  236. int len;
  237. netlink_table_grab();
  238. head = nl_pid_hashfn(hash, pid);
  239. len = 0;
  240. sk_for_each(osk, node, head) {
  241. if (nlk_sk(osk)->pid == pid)
  242. break;
  243. len++;
  244. }
  245. if (node)
  246. goto err;
  247. err = -EBUSY;
  248. if (nlk_sk(sk)->pid)
  249. goto err;
  250. err = -ENOMEM;
  251. if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
  252. goto err;
  253. if (len && nl_pid_hash_dilute(hash, len))
  254. head = nl_pid_hashfn(hash, pid);
  255. hash->entries++;
  256. nlk_sk(sk)->pid = pid;
  257. sk_add_node(sk, head);
  258. err = 0;
  259. err:
  260. netlink_table_ungrab();
  261. return err;
  262. }
  263. static void netlink_remove(struct sock *sk)
  264. {
  265. netlink_table_grab();
  266. if (sk_del_node_init(sk))
  267. nl_table[sk->sk_protocol].hash.entries--;
  268. if (nlk_sk(sk)->groups)
  269. __sk_del_bind_node(sk);
  270. netlink_table_ungrab();
  271. }
  272. static struct proto netlink_proto = {
  273. .name = "NETLINK",
  274. .owner = THIS_MODULE,
  275. .obj_size = sizeof(struct netlink_sock),
  276. };
  277. static int netlink_create(struct socket *sock, int protocol)
  278. {
  279. struct sock *sk;
  280. struct netlink_sock *nlk;
  281. sock->state = SS_UNCONNECTED;
  282. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  283. return -ESOCKTNOSUPPORT;
  284. if (protocol<0 || protocol >= MAX_LINKS)
  285. return -EPROTONOSUPPORT;
  286. sock->ops = &netlink_ops;
  287. sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1);
  288. if (!sk)
  289. return -ENOMEM;
  290. sock_init_data(sock, sk);
  291. nlk = nlk_sk(sk);
  292. spin_lock_init(&nlk->cb_lock);
  293. init_waitqueue_head(&nlk->wait);
  294. sk->sk_destruct = netlink_sock_destruct;
  295. sk->sk_protocol = protocol;
  296. return 0;
  297. }
  298. static int netlink_release(struct socket *sock)
  299. {
  300. struct sock *sk = sock->sk;
  301. struct netlink_sock *nlk;
  302. if (!sk)
  303. return 0;
  304. netlink_remove(sk);
  305. nlk = nlk_sk(sk);
  306. spin_lock(&nlk->cb_lock);
  307. if (nlk->cb) {
  308. nlk->cb->done(nlk->cb);
  309. netlink_destroy_callback(nlk->cb);
  310. nlk->cb = NULL;
  311. }
  312. spin_unlock(&nlk->cb_lock);
  313. /* OK. Socket is unlinked, and, therefore,
  314. no new packets will arrive */
  315. sock_orphan(sk);
  316. sock->sk = NULL;
  317. wake_up_interruptible_all(&nlk->wait);
  318. skb_queue_purge(&sk->sk_write_queue);
  319. if (nlk->pid && !nlk->groups) {
  320. struct netlink_notify n = {
  321. .protocol = sk->sk_protocol,
  322. .pid = nlk->pid,
  323. };
  324. notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n);
  325. }
  326. sock_put(sk);
  327. return 0;
  328. }
  329. static int netlink_autobind(struct socket *sock)
  330. {
  331. struct sock *sk = sock->sk;
  332. struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
  333. struct hlist_head *head;
  334. struct sock *osk;
  335. struct hlist_node *node;
  336. s32 pid = current->pid;
  337. int err;
  338. static s32 rover = -4097;
  339. retry:
  340. cond_resched();
  341. netlink_table_grab();
  342. head = nl_pid_hashfn(hash, pid);
  343. sk_for_each(osk, node, head) {
  344. if (nlk_sk(osk)->pid == pid) {
  345. /* Bind collision, search negative pid values. */
  346. pid = rover--;
  347. if (rover > -4097)
  348. rover = -4097;
  349. netlink_table_ungrab();
  350. goto retry;
  351. }
  352. }
  353. netlink_table_ungrab();
  354. err = netlink_insert(sk, pid);
  355. if (err == -EADDRINUSE)
  356. goto retry;
  357. /* If 2 threads race to autobind, that is fine. */
  358. if (err == -EBUSY)
  359. err = 0;
  360. return err;
  361. }
  362. static inline int netlink_capable(struct socket *sock, unsigned int flag)
  363. {
  364. return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) ||
  365. capable(CAP_NET_ADMIN);
  366. }
  367. static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  368. {
  369. struct sock *sk = sock->sk;
  370. struct netlink_sock *nlk = nlk_sk(sk);
  371. struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
  372. int err;
  373. if (nladdr->nl_family != AF_NETLINK)
  374. return -EINVAL;
  375. /* Only superuser is allowed to listen multicasts */
  376. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV))
  377. return -EPERM;
  378. if (nlk->pid) {
  379. if (nladdr->nl_pid != nlk->pid)
  380. return -EINVAL;
  381. } else {
  382. err = nladdr->nl_pid ?
  383. netlink_insert(sk, nladdr->nl_pid) :
  384. netlink_autobind(sock);
  385. if (err)
  386. return err;
  387. }
  388. if (!nladdr->nl_groups && !nlk->groups)
  389. return 0;
  390. netlink_table_grab();
  391. if (nlk->groups && !nladdr->nl_groups)
  392. __sk_del_bind_node(sk);
  393. else if (!nlk->groups && nladdr->nl_groups)
  394. sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
  395. nlk->groups = nladdr->nl_groups;
  396. netlink_table_ungrab();
  397. return 0;
  398. }
  399. static int netlink_connect(struct socket *sock, struct sockaddr *addr,
  400. int alen, int flags)
  401. {
  402. int err = 0;
  403. struct sock *sk = sock->sk;
  404. struct netlink_sock *nlk = nlk_sk(sk);
  405. struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
  406. if (addr->sa_family == AF_UNSPEC) {
  407. sk->sk_state = NETLINK_UNCONNECTED;
  408. nlk->dst_pid = 0;
  409. nlk->dst_groups = 0;
  410. return 0;
  411. }
  412. if (addr->sa_family != AF_NETLINK)
  413. return -EINVAL;
  414. /* Only superuser is allowed to send multicasts */
  415. if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  416. return -EPERM;
  417. if (!nlk->pid)
  418. err = netlink_autobind(sock);
  419. if (err == 0) {
  420. sk->sk_state = NETLINK_CONNECTED;
  421. nlk->dst_pid = nladdr->nl_pid;
  422. nlk->dst_groups = nladdr->nl_groups;
  423. }
  424. return err;
  425. }
  426. static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
  427. {
  428. struct sock *sk = sock->sk;
  429. struct netlink_sock *nlk = nlk_sk(sk);
  430. struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
  431. nladdr->nl_family = AF_NETLINK;
  432. nladdr->nl_pad = 0;
  433. *addr_len = sizeof(*nladdr);
  434. if (peer) {
  435. nladdr->nl_pid = nlk->dst_pid;
  436. nladdr->nl_groups = nlk->dst_groups;
  437. } else {
  438. nladdr->nl_pid = nlk->pid;
  439. nladdr->nl_groups = nlk->groups;
  440. }
  441. return 0;
  442. }
  443. static void netlink_overrun(struct sock *sk)
  444. {
  445. if (!test_and_set_bit(0, &nlk_sk(sk)->state)) {
  446. sk->sk_err = ENOBUFS;
  447. sk->sk_error_report(sk);
  448. }
  449. }
  450. static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
  451. {
  452. int protocol = ssk->sk_protocol;
  453. struct sock *sock;
  454. struct netlink_sock *nlk;
  455. sock = netlink_lookup(protocol, pid);
  456. if (!sock)
  457. return ERR_PTR(-ECONNREFUSED);
  458. /* Don't bother queuing skb if kernel socket has no input function */
  459. nlk = nlk_sk(sock);
  460. if ((nlk->pid == 0 && !nlk->data_ready) ||
  461. (sock->sk_state == NETLINK_CONNECTED &&
  462. nlk->dst_pid != nlk_sk(ssk)->pid)) {
  463. sock_put(sock);
  464. return ERR_PTR(-ECONNREFUSED);
  465. }
  466. return sock;
  467. }
  468. struct sock *netlink_getsockbyfilp(struct file *filp)
  469. {
  470. struct inode *inode = filp->f_dentry->d_inode;
  471. struct sock *sock;
  472. if (!S_ISSOCK(inode->i_mode))
  473. return ERR_PTR(-ENOTSOCK);
  474. sock = SOCKET_I(inode)->sk;
  475. if (sock->sk_family != AF_NETLINK)
  476. return ERR_PTR(-EINVAL);
  477. sock_hold(sock);
  478. return sock;
  479. }
  480. /*
  481. * Attach a skb to a netlink socket.
  482. * The caller must hold a reference to the destination socket. On error, the
  483. * reference is dropped. The skb is not send to the destination, just all
  484. * all error checks are performed and memory in the queue is reserved.
  485. * Return values:
  486. * < 0: error. skb freed, reference to sock dropped.
  487. * 0: continue
  488. * 1: repeat lookup - reference dropped while waiting for socket memory.
  489. */
  490. int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo)
  491. {
  492. struct netlink_sock *nlk;
  493. nlk = nlk_sk(sk);
  494. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  495. test_bit(0, &nlk->state)) {
  496. DECLARE_WAITQUEUE(wait, current);
  497. if (!timeo) {
  498. if (!nlk->pid)
  499. netlink_overrun(sk);
  500. sock_put(sk);
  501. kfree_skb(skb);
  502. return -EAGAIN;
  503. }
  504. __set_current_state(TASK_INTERRUPTIBLE);
  505. add_wait_queue(&nlk->wait, &wait);
  506. if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  507. test_bit(0, &nlk->state)) &&
  508. !sock_flag(sk, SOCK_DEAD))
  509. timeo = schedule_timeout(timeo);
  510. __set_current_state(TASK_RUNNING);
  511. remove_wait_queue(&nlk->wait, &wait);
  512. sock_put(sk);
  513. if (signal_pending(current)) {
  514. kfree_skb(skb);
  515. return sock_intr_errno(timeo);
  516. }
  517. return 1;
  518. }
  519. skb_set_owner_r(skb, sk);
  520. return 0;
  521. }
  522. int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol)
  523. {
  524. struct netlink_sock *nlk;
  525. int len = skb->len;
  526. nlk = nlk_sk(sk);
  527. skb_queue_tail(&sk->sk_receive_queue, skb);
  528. sk->sk_data_ready(sk, len);
  529. sock_put(sk);
  530. return len;
  531. }
  532. void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
  533. {
  534. kfree_skb(skb);
  535. sock_put(sk);
  536. }
  537. static inline struct sk_buff *netlink_trim(struct sk_buff *skb,
  538. unsigned int __nocast allocation)
  539. {
  540. int delta;
  541. skb_orphan(skb);
  542. delta = skb->end - skb->tail;
  543. if (delta * 2 < skb->truesize)
  544. return skb;
  545. if (skb_shared(skb)) {
  546. struct sk_buff *nskb = skb_clone(skb, allocation);
  547. if (!nskb)
  548. return skb;
  549. kfree_skb(skb);
  550. skb = nskb;
  551. }
  552. if (!pskb_expand_head(skb, 0, -delta, allocation))
  553. skb->truesize -= delta;
  554. return skb;
  555. }
  556. int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
  557. {
  558. struct sock *sk;
  559. int err;
  560. long timeo;
  561. skb = netlink_trim(skb, gfp_any());
  562. timeo = sock_sndtimeo(ssk, nonblock);
  563. retry:
  564. sk = netlink_getsockbypid(ssk, pid);
  565. if (IS_ERR(sk)) {
  566. kfree_skb(skb);
  567. return PTR_ERR(sk);
  568. }
  569. err = netlink_attachskb(sk, skb, nonblock, timeo);
  570. if (err == 1)
  571. goto retry;
  572. if (err)
  573. return err;
  574. return netlink_sendskb(sk, skb, ssk->sk_protocol);
  575. }
  576. static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  577. {
  578. struct netlink_sock *nlk = nlk_sk(sk);
  579. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  580. !test_bit(0, &nlk->state)) {
  581. skb_set_owner_r(skb, sk);
  582. skb_queue_tail(&sk->sk_receive_queue, skb);
  583. sk->sk_data_ready(sk, skb->len);
  584. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  585. }
  586. return -1;
  587. }
  588. struct netlink_broadcast_data {
  589. struct sock *exclude_sk;
  590. u32 pid;
  591. u32 group;
  592. int failure;
  593. int congested;
  594. int delivered;
  595. unsigned int allocation;
  596. struct sk_buff *skb, *skb2;
  597. };
  598. static inline int do_one_broadcast(struct sock *sk,
  599. struct netlink_broadcast_data *p)
  600. {
  601. struct netlink_sock *nlk = nlk_sk(sk);
  602. int val;
  603. if (p->exclude_sk == sk)
  604. goto out;
  605. if (nlk->pid == p->pid || !(nlk->groups & p->group))
  606. goto out;
  607. if (p->failure) {
  608. netlink_overrun(sk);
  609. goto out;
  610. }
  611. sock_hold(sk);
  612. if (p->skb2 == NULL) {
  613. if (skb_shared(p->skb)) {
  614. p->skb2 = skb_clone(p->skb, p->allocation);
  615. } else {
  616. p->skb2 = skb_get(p->skb);
  617. /*
  618. * skb ownership may have been set when
  619. * delivered to a previous socket.
  620. */
  621. skb_orphan(p->skb2);
  622. }
  623. }
  624. if (p->skb2 == NULL) {
  625. netlink_overrun(sk);
  626. /* Clone failed. Notify ALL listeners. */
  627. p->failure = 1;
  628. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  629. netlink_overrun(sk);
  630. } else {
  631. p->congested |= val;
  632. p->delivered = 1;
  633. p->skb2 = NULL;
  634. }
  635. sock_put(sk);
  636. out:
  637. return 0;
  638. }
  639. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  640. u32 group, int allocation)
  641. {
  642. struct netlink_broadcast_data info;
  643. struct hlist_node *node;
  644. struct sock *sk;
  645. skb = netlink_trim(skb, allocation);
  646. info.exclude_sk = ssk;
  647. info.pid = pid;
  648. info.group = group;
  649. info.failure = 0;
  650. info.congested = 0;
  651. info.delivered = 0;
  652. info.allocation = allocation;
  653. info.skb = skb;
  654. info.skb2 = NULL;
  655. /* While we sleep in clone, do not allow to change socket list */
  656. netlink_lock_table();
  657. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  658. do_one_broadcast(sk, &info);
  659. kfree_skb(skb);
  660. netlink_unlock_table();
  661. if (info.skb2)
  662. kfree_skb(info.skb2);
  663. if (info.delivered) {
  664. if (info.congested && (allocation & __GFP_WAIT))
  665. yield();
  666. return 0;
  667. }
  668. if (info.failure)
  669. return -ENOBUFS;
  670. return -ESRCH;
  671. }
  672. struct netlink_set_err_data {
  673. struct sock *exclude_sk;
  674. u32 pid;
  675. u32 group;
  676. int code;
  677. };
  678. static inline int do_one_set_err(struct sock *sk,
  679. struct netlink_set_err_data *p)
  680. {
  681. struct netlink_sock *nlk = nlk_sk(sk);
  682. if (sk == p->exclude_sk)
  683. goto out;
  684. if (nlk->pid == p->pid || !(nlk->groups & p->group))
  685. goto out;
  686. sk->sk_err = p->code;
  687. sk->sk_error_report(sk);
  688. out:
  689. return 0;
  690. }
  691. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  692. {
  693. struct netlink_set_err_data info;
  694. struct hlist_node *node;
  695. struct sock *sk;
  696. info.exclude_sk = ssk;
  697. info.pid = pid;
  698. info.group = group;
  699. info.code = code;
  700. read_lock(&nl_table_lock);
  701. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  702. do_one_set_err(sk, &info);
  703. read_unlock(&nl_table_lock);
  704. }
  705. static inline void netlink_rcv_wake(struct sock *sk)
  706. {
  707. struct netlink_sock *nlk = nlk_sk(sk);
  708. if (skb_queue_empty(&sk->sk_receive_queue))
  709. clear_bit(0, &nlk->state);
  710. if (!test_bit(0, &nlk->state))
  711. wake_up_interruptible(&nlk->wait);
  712. }
  713. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  714. struct msghdr *msg, size_t len)
  715. {
  716. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  717. struct sock *sk = sock->sk;
  718. struct netlink_sock *nlk = nlk_sk(sk);
  719. struct sockaddr_nl *addr=msg->msg_name;
  720. u32 dst_pid;
  721. u32 dst_groups;
  722. struct sk_buff *skb;
  723. int err;
  724. struct scm_cookie scm;
  725. if (msg->msg_flags&MSG_OOB)
  726. return -EOPNOTSUPP;
  727. if (NULL == siocb->scm)
  728. siocb->scm = &scm;
  729. err = scm_send(sock, msg, siocb->scm);
  730. if (err < 0)
  731. return err;
  732. if (msg->msg_namelen) {
  733. if (addr->nl_family != AF_NETLINK)
  734. return -EINVAL;
  735. dst_pid = addr->nl_pid;
  736. dst_groups = addr->nl_groups;
  737. if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  738. return -EPERM;
  739. } else {
  740. dst_pid = nlk->dst_pid;
  741. dst_groups = nlk->dst_groups;
  742. }
  743. if (!nlk->pid) {
  744. err = netlink_autobind(sock);
  745. if (err)
  746. goto out;
  747. }
  748. err = -EMSGSIZE;
  749. if (len > sk->sk_sndbuf - 32)
  750. goto out;
  751. err = -ENOBUFS;
  752. skb = alloc_skb(len, GFP_KERNEL);
  753. if (skb==NULL)
  754. goto out;
  755. NETLINK_CB(skb).pid = nlk->pid;
  756. NETLINK_CB(skb).groups = nlk->groups;
  757. NETLINK_CB(skb).dst_pid = dst_pid;
  758. NETLINK_CB(skb).dst_groups = dst_groups;
  759. NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
  760. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  761. /* What can I do? Netlink is asynchronous, so that
  762. we will have to save current capabilities to
  763. check them, when this message will be delivered
  764. to corresponding kernel module. --ANK (980802)
  765. */
  766. err = -EFAULT;
  767. if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
  768. kfree_skb(skb);
  769. goto out;
  770. }
  771. err = security_netlink_send(sk, skb);
  772. if (err) {
  773. kfree_skb(skb);
  774. goto out;
  775. }
  776. if (dst_groups) {
  777. atomic_inc(&skb->users);
  778. netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
  779. }
  780. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  781. out:
  782. return err;
  783. }
  784. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  785. struct msghdr *msg, size_t len,
  786. int flags)
  787. {
  788. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  789. struct scm_cookie scm;
  790. struct sock *sk = sock->sk;
  791. struct netlink_sock *nlk = nlk_sk(sk);
  792. int noblock = flags&MSG_DONTWAIT;
  793. size_t copied;
  794. struct sk_buff *skb;
  795. int err;
  796. if (flags&MSG_OOB)
  797. return -EOPNOTSUPP;
  798. copied = 0;
  799. skb = skb_recv_datagram(sk,flags,noblock,&err);
  800. if (skb==NULL)
  801. goto out;
  802. msg->msg_namelen = 0;
  803. copied = skb->len;
  804. if (len < copied) {
  805. msg->msg_flags |= MSG_TRUNC;
  806. copied = len;
  807. }
  808. skb->h.raw = skb->data;
  809. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  810. if (msg->msg_name) {
  811. struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
  812. addr->nl_family = AF_NETLINK;
  813. addr->nl_pad = 0;
  814. addr->nl_pid = NETLINK_CB(skb).pid;
  815. addr->nl_groups = NETLINK_CB(skb).dst_groups;
  816. msg->msg_namelen = sizeof(*addr);
  817. }
  818. if (NULL == siocb->scm) {
  819. memset(&scm, 0, sizeof(scm));
  820. siocb->scm = &scm;
  821. }
  822. siocb->scm->creds = *NETLINK_CREDS(skb);
  823. skb_free_datagram(sk, skb);
  824. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  825. netlink_dump(sk);
  826. scm_recv(sock, msg, siocb->scm, flags);
  827. out:
  828. netlink_rcv_wake(sk);
  829. return err ? : copied;
  830. }
  831. static void netlink_data_ready(struct sock *sk, int len)
  832. {
  833. struct netlink_sock *nlk = nlk_sk(sk);
  834. if (nlk->data_ready)
  835. nlk->data_ready(sk, len);
  836. netlink_rcv_wake(sk);
  837. }
  838. /*
  839. * We export these functions to other modules. They provide a
  840. * complete set of kernel non-blocking support for message
  841. * queueing.
  842. */
  843. struct sock *
  844. netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
  845. {
  846. struct socket *sock;
  847. struct sock *sk;
  848. if (!nl_table)
  849. return NULL;
  850. if (unit<0 || unit>=MAX_LINKS)
  851. return NULL;
  852. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  853. return NULL;
  854. if (netlink_create(sock, unit) < 0) {
  855. sock_release(sock);
  856. return NULL;
  857. }
  858. sk = sock->sk;
  859. sk->sk_data_ready = netlink_data_ready;
  860. if (input)
  861. nlk_sk(sk)->data_ready = input;
  862. if (netlink_insert(sk, 0)) {
  863. sock_release(sock);
  864. return NULL;
  865. }
  866. return sk;
  867. }
  868. void netlink_set_nonroot(int protocol, unsigned int flags)
  869. {
  870. if ((unsigned int)protocol < MAX_LINKS)
  871. nl_table[protocol].nl_nonroot = flags;
  872. }
  873. static void netlink_destroy_callback(struct netlink_callback *cb)
  874. {
  875. if (cb->skb)
  876. kfree_skb(cb->skb);
  877. kfree(cb);
  878. }
  879. /*
  880. * It looks a bit ugly.
  881. * It would be better to create kernel thread.
  882. */
  883. static int netlink_dump(struct sock *sk)
  884. {
  885. struct netlink_sock *nlk = nlk_sk(sk);
  886. struct netlink_callback *cb;
  887. struct sk_buff *skb;
  888. struct nlmsghdr *nlh;
  889. int len;
  890. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  891. if (!skb)
  892. return -ENOBUFS;
  893. spin_lock(&nlk->cb_lock);
  894. cb = nlk->cb;
  895. if (cb == NULL) {
  896. spin_unlock(&nlk->cb_lock);
  897. kfree_skb(skb);
  898. return -EINVAL;
  899. }
  900. len = cb->dump(skb, cb);
  901. if (len > 0) {
  902. spin_unlock(&nlk->cb_lock);
  903. skb_queue_tail(&sk->sk_receive_queue, skb);
  904. sk->sk_data_ready(sk, len);
  905. return 0;
  906. }
  907. nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  908. memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
  909. skb_queue_tail(&sk->sk_receive_queue, skb);
  910. sk->sk_data_ready(sk, skb->len);
  911. cb->done(cb);
  912. nlk->cb = NULL;
  913. spin_unlock(&nlk->cb_lock);
  914. netlink_destroy_callback(cb);
  915. return 0;
  916. nlmsg_failure:
  917. return -ENOBUFS;
  918. }
  919. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  920. struct nlmsghdr *nlh,
  921. int (*dump)(struct sk_buff *skb, struct netlink_callback*),
  922. int (*done)(struct netlink_callback*))
  923. {
  924. struct netlink_callback *cb;
  925. struct sock *sk;
  926. struct netlink_sock *nlk;
  927. cb = kmalloc(sizeof(*cb), GFP_KERNEL);
  928. if (cb == NULL)
  929. return -ENOBUFS;
  930. memset(cb, 0, sizeof(*cb));
  931. cb->dump = dump;
  932. cb->done = done;
  933. cb->nlh = nlh;
  934. atomic_inc(&skb->users);
  935. cb->skb = skb;
  936. sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
  937. if (sk == NULL) {
  938. netlink_destroy_callback(cb);
  939. return -ECONNREFUSED;
  940. }
  941. nlk = nlk_sk(sk);
  942. /* A dump is in progress... */
  943. spin_lock(&nlk->cb_lock);
  944. if (nlk->cb) {
  945. spin_unlock(&nlk->cb_lock);
  946. netlink_destroy_callback(cb);
  947. sock_put(sk);
  948. return -EBUSY;
  949. }
  950. nlk->cb = cb;
  951. spin_unlock(&nlk->cb_lock);
  952. netlink_dump(sk);
  953. sock_put(sk);
  954. return 0;
  955. }
  956. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  957. {
  958. struct sk_buff *skb;
  959. struct nlmsghdr *rep;
  960. struct nlmsgerr *errmsg;
  961. int size;
  962. if (err == 0)
  963. size = NLMSG_SPACE(sizeof(struct nlmsgerr));
  964. else
  965. size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
  966. skb = alloc_skb(size, GFP_KERNEL);
  967. if (!skb) {
  968. struct sock *sk;
  969. sk = netlink_lookup(in_skb->sk->sk_protocol,
  970. NETLINK_CB(in_skb).pid);
  971. if (sk) {
  972. sk->sk_err = ENOBUFS;
  973. sk->sk_error_report(sk);
  974. sock_put(sk);
  975. }
  976. return;
  977. }
  978. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  979. NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
  980. errmsg = NLMSG_DATA(rep);
  981. errmsg->error = err;
  982. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
  983. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  984. }
  985. #ifdef CONFIG_PROC_FS
  986. struct nl_seq_iter {
  987. int link;
  988. int hash_idx;
  989. };
  990. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  991. {
  992. struct nl_seq_iter *iter = seq->private;
  993. int i, j;
  994. struct sock *s;
  995. struct hlist_node *node;
  996. loff_t off = 0;
  997. for (i=0; i<MAX_LINKS; i++) {
  998. struct nl_pid_hash *hash = &nl_table[i].hash;
  999. for (j = 0; j <= hash->mask; j++) {
  1000. sk_for_each(s, node, &hash->table[j]) {
  1001. if (off == pos) {
  1002. iter->link = i;
  1003. iter->hash_idx = j;
  1004. return s;
  1005. }
  1006. ++off;
  1007. }
  1008. }
  1009. }
  1010. return NULL;
  1011. }
  1012. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1013. {
  1014. read_lock(&nl_table_lock);
  1015. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1016. }
  1017. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1018. {
  1019. struct sock *s;
  1020. struct nl_seq_iter *iter;
  1021. int i, j;
  1022. ++*pos;
  1023. if (v == SEQ_START_TOKEN)
  1024. return netlink_seq_socket_idx(seq, 0);
  1025. s = sk_next(v);
  1026. if (s)
  1027. return s;
  1028. iter = seq->private;
  1029. i = iter->link;
  1030. j = iter->hash_idx + 1;
  1031. do {
  1032. struct nl_pid_hash *hash = &nl_table[i].hash;
  1033. for (; j <= hash->mask; j++) {
  1034. s = sk_head(&hash->table[j]);
  1035. if (s) {
  1036. iter->link = i;
  1037. iter->hash_idx = j;
  1038. return s;
  1039. }
  1040. }
  1041. j = 0;
  1042. } while (++i < MAX_LINKS);
  1043. return NULL;
  1044. }
  1045. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1046. {
  1047. read_unlock(&nl_table_lock);
  1048. }
  1049. static int netlink_seq_show(struct seq_file *seq, void *v)
  1050. {
  1051. if (v == SEQ_START_TOKEN)
  1052. seq_puts(seq,
  1053. "sk Eth Pid Groups "
  1054. "Rmem Wmem Dump Locks\n");
  1055. else {
  1056. struct sock *s = v;
  1057. struct netlink_sock *nlk = nlk_sk(s);
  1058. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
  1059. s,
  1060. s->sk_protocol,
  1061. nlk->pid,
  1062. nlk->groups,
  1063. atomic_read(&s->sk_rmem_alloc),
  1064. atomic_read(&s->sk_wmem_alloc),
  1065. nlk->cb,
  1066. atomic_read(&s->sk_refcnt)
  1067. );
  1068. }
  1069. return 0;
  1070. }
  1071. static struct seq_operations netlink_seq_ops = {
  1072. .start = netlink_seq_start,
  1073. .next = netlink_seq_next,
  1074. .stop = netlink_seq_stop,
  1075. .show = netlink_seq_show,
  1076. };
  1077. static int netlink_seq_open(struct inode *inode, struct file *file)
  1078. {
  1079. struct seq_file *seq;
  1080. struct nl_seq_iter *iter;
  1081. int err;
  1082. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  1083. if (!iter)
  1084. return -ENOMEM;
  1085. err = seq_open(file, &netlink_seq_ops);
  1086. if (err) {
  1087. kfree(iter);
  1088. return err;
  1089. }
  1090. memset(iter, 0, sizeof(*iter));
  1091. seq = file->private_data;
  1092. seq->private = iter;
  1093. return 0;
  1094. }
  1095. static struct file_operations netlink_seq_fops = {
  1096. .owner = THIS_MODULE,
  1097. .open = netlink_seq_open,
  1098. .read = seq_read,
  1099. .llseek = seq_lseek,
  1100. .release = seq_release_private,
  1101. };
  1102. #endif
  1103. int netlink_register_notifier(struct notifier_block *nb)
  1104. {
  1105. return notifier_chain_register(&netlink_chain, nb);
  1106. }
  1107. int netlink_unregister_notifier(struct notifier_block *nb)
  1108. {
  1109. return notifier_chain_unregister(&netlink_chain, nb);
  1110. }
  1111. static struct proto_ops netlink_ops = {
  1112. .family = PF_NETLINK,
  1113. .owner = THIS_MODULE,
  1114. .release = netlink_release,
  1115. .bind = netlink_bind,
  1116. .connect = netlink_connect,
  1117. .socketpair = sock_no_socketpair,
  1118. .accept = sock_no_accept,
  1119. .getname = netlink_getname,
  1120. .poll = datagram_poll,
  1121. .ioctl = sock_no_ioctl,
  1122. .listen = sock_no_listen,
  1123. .shutdown = sock_no_shutdown,
  1124. .setsockopt = sock_no_setsockopt,
  1125. .getsockopt = sock_no_getsockopt,
  1126. .sendmsg = netlink_sendmsg,
  1127. .recvmsg = netlink_recvmsg,
  1128. .mmap = sock_no_mmap,
  1129. .sendpage = sock_no_sendpage,
  1130. };
  1131. static struct net_proto_family netlink_family_ops = {
  1132. .family = PF_NETLINK,
  1133. .create = netlink_create,
  1134. .owner = THIS_MODULE, /* for consistency 8) */
  1135. };
  1136. extern void netlink_skb_parms_too_large(void);
  1137. static int __init netlink_proto_init(void)
  1138. {
  1139. struct sk_buff *dummy_skb;
  1140. int i;
  1141. unsigned long max;
  1142. unsigned int order;
  1143. int err = proto_register(&netlink_proto, 0);
  1144. if (err != 0)
  1145. goto out;
  1146. if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
  1147. netlink_skb_parms_too_large();
  1148. nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
  1149. if (!nl_table) {
  1150. enomem:
  1151. printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
  1152. return -ENOMEM;
  1153. }
  1154. memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
  1155. if (num_physpages >= (128 * 1024))
  1156. max = num_physpages >> (21 - PAGE_SHIFT);
  1157. else
  1158. max = num_physpages >> (23 - PAGE_SHIFT);
  1159. order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
  1160. max = (1UL << order) / sizeof(struct hlist_head);
  1161. order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
  1162. for (i = 0; i < MAX_LINKS; i++) {
  1163. struct nl_pid_hash *hash = &nl_table[i].hash;
  1164. hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
  1165. if (!hash->table) {
  1166. while (i-- > 0)
  1167. nl_pid_hash_free(nl_table[i].hash.table,
  1168. 1 * sizeof(*hash->table));
  1169. kfree(nl_table);
  1170. goto enomem;
  1171. }
  1172. memset(hash->table, 0, 1 * sizeof(*hash->table));
  1173. hash->max_shift = order;
  1174. hash->shift = 0;
  1175. hash->mask = 0;
  1176. hash->rehash_time = jiffies;
  1177. }
  1178. sock_register(&netlink_family_ops);
  1179. #ifdef CONFIG_PROC_FS
  1180. proc_net_fops_create("netlink", 0, &netlink_seq_fops);
  1181. #endif
  1182. /* The netlink device handler may be needed early. */
  1183. rtnetlink_init();
  1184. out:
  1185. return err;
  1186. }
  1187. static void __exit netlink_proto_exit(void)
  1188. {
  1189. sock_unregister(PF_NETLINK);
  1190. proc_net_remove("netlink");
  1191. kfree(nl_table);
  1192. nl_table = NULL;
  1193. proto_unregister(&netlink_proto);
  1194. }
  1195. core_initcall(netlink_proto_init);
  1196. module_exit(netlink_proto_exit);
  1197. MODULE_LICENSE("GPL");
  1198. MODULE_ALIAS_NETPROTO(PF_NETLINK);
  1199. EXPORT_SYMBOL(netlink_ack);
  1200. EXPORT_SYMBOL(netlink_broadcast);
  1201. EXPORT_SYMBOL(netlink_dump_start);
  1202. EXPORT_SYMBOL(netlink_kernel_create);
  1203. EXPORT_SYMBOL(netlink_register_notifier);
  1204. EXPORT_SYMBOL(netlink_set_err);
  1205. EXPORT_SYMBOL(netlink_set_nonroot);
  1206. EXPORT_SYMBOL(netlink_unicast);
  1207. EXPORT_SYMBOL(netlink_unregister_notifier);