af_netlink.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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, int allocation)
  538. {
  539. int delta;
  540. skb_orphan(skb);
  541. delta = skb->end - skb->tail;
  542. if (delta * 2 < skb->truesize)
  543. return skb;
  544. if (skb_shared(skb)) {
  545. struct sk_buff *nskb = skb_clone(skb, allocation);
  546. if (!nskb)
  547. return skb;
  548. kfree_skb(skb);
  549. skb = nskb;
  550. }
  551. if (!pskb_expand_head(skb, 0, -delta, allocation))
  552. skb->truesize -= delta;
  553. return skb;
  554. }
  555. int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
  556. {
  557. struct sock *sk;
  558. int err;
  559. long timeo;
  560. skb = netlink_trim(skb, gfp_any());
  561. timeo = sock_sndtimeo(ssk, nonblock);
  562. retry:
  563. sk = netlink_getsockbypid(ssk, pid);
  564. if (IS_ERR(sk)) {
  565. kfree_skb(skb);
  566. return PTR_ERR(sk);
  567. }
  568. err = netlink_attachskb(sk, skb, nonblock, timeo);
  569. if (err == 1)
  570. goto retry;
  571. if (err)
  572. return err;
  573. return netlink_sendskb(sk, skb, ssk->sk_protocol);
  574. }
  575. static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
  576. {
  577. struct netlink_sock *nlk = nlk_sk(sk);
  578. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
  579. !test_bit(0, &nlk->state)) {
  580. skb_set_owner_r(skb, sk);
  581. skb_queue_tail(&sk->sk_receive_queue, skb);
  582. sk->sk_data_ready(sk, skb->len);
  583. return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
  584. }
  585. return -1;
  586. }
  587. struct netlink_broadcast_data {
  588. struct sock *exclude_sk;
  589. u32 pid;
  590. u32 group;
  591. int failure;
  592. int congested;
  593. int delivered;
  594. int allocation;
  595. struct sk_buff *skb, *skb2;
  596. };
  597. static inline int do_one_broadcast(struct sock *sk,
  598. struct netlink_broadcast_data *p)
  599. {
  600. struct netlink_sock *nlk = nlk_sk(sk);
  601. int val;
  602. if (p->exclude_sk == sk)
  603. goto out;
  604. if (nlk->pid == p->pid || !(nlk->groups & p->group))
  605. goto out;
  606. if (p->failure) {
  607. netlink_overrun(sk);
  608. goto out;
  609. }
  610. sock_hold(sk);
  611. if (p->skb2 == NULL) {
  612. if (skb_shared(p->skb)) {
  613. p->skb2 = skb_clone(p->skb, p->allocation);
  614. } else {
  615. p->skb2 = skb_get(p->skb);
  616. /*
  617. * skb ownership may have been set when
  618. * delivered to a previous socket.
  619. */
  620. skb_orphan(p->skb2);
  621. }
  622. }
  623. if (p->skb2 == NULL) {
  624. netlink_overrun(sk);
  625. /* Clone failed. Notify ALL listeners. */
  626. p->failure = 1;
  627. } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
  628. netlink_overrun(sk);
  629. } else {
  630. p->congested |= val;
  631. p->delivered = 1;
  632. p->skb2 = NULL;
  633. }
  634. sock_put(sk);
  635. out:
  636. return 0;
  637. }
  638. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
  639. u32 group, int allocation)
  640. {
  641. struct netlink_broadcast_data info;
  642. struct hlist_node *node;
  643. struct sock *sk;
  644. skb = netlink_trim(skb, allocation);
  645. info.exclude_sk = ssk;
  646. info.pid = pid;
  647. info.group = group;
  648. info.failure = 0;
  649. info.congested = 0;
  650. info.delivered = 0;
  651. info.allocation = allocation;
  652. info.skb = skb;
  653. info.skb2 = NULL;
  654. /* While we sleep in clone, do not allow to change socket list */
  655. netlink_lock_table();
  656. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  657. do_one_broadcast(sk, &info);
  658. kfree_skb(skb);
  659. netlink_unlock_table();
  660. if (info.skb2)
  661. kfree_skb(info.skb2);
  662. if (info.delivered) {
  663. if (info.congested && (allocation & __GFP_WAIT))
  664. yield();
  665. return 0;
  666. }
  667. if (info.failure)
  668. return -ENOBUFS;
  669. return -ESRCH;
  670. }
  671. struct netlink_set_err_data {
  672. struct sock *exclude_sk;
  673. u32 pid;
  674. u32 group;
  675. int code;
  676. };
  677. static inline int do_one_set_err(struct sock *sk,
  678. struct netlink_set_err_data *p)
  679. {
  680. struct netlink_sock *nlk = nlk_sk(sk);
  681. if (sk == p->exclude_sk)
  682. goto out;
  683. if (nlk->pid == p->pid || !(nlk->groups & p->group))
  684. goto out;
  685. sk->sk_err = p->code;
  686. sk->sk_error_report(sk);
  687. out:
  688. return 0;
  689. }
  690. void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
  691. {
  692. struct netlink_set_err_data info;
  693. struct hlist_node *node;
  694. struct sock *sk;
  695. info.exclude_sk = ssk;
  696. info.pid = pid;
  697. info.group = group;
  698. info.code = code;
  699. read_lock(&nl_table_lock);
  700. sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list)
  701. do_one_set_err(sk, &info);
  702. read_unlock(&nl_table_lock);
  703. }
  704. static inline void netlink_rcv_wake(struct sock *sk)
  705. {
  706. struct netlink_sock *nlk = nlk_sk(sk);
  707. if (!skb_queue_len(&sk->sk_receive_queue))
  708. clear_bit(0, &nlk->state);
  709. if (!test_bit(0, &nlk->state))
  710. wake_up_interruptible(&nlk->wait);
  711. }
  712. static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
  713. struct msghdr *msg, size_t len)
  714. {
  715. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  716. struct sock *sk = sock->sk;
  717. struct netlink_sock *nlk = nlk_sk(sk);
  718. struct sockaddr_nl *addr=msg->msg_name;
  719. u32 dst_pid;
  720. u32 dst_groups;
  721. struct sk_buff *skb;
  722. int err;
  723. struct scm_cookie scm;
  724. if (msg->msg_flags&MSG_OOB)
  725. return -EOPNOTSUPP;
  726. if (NULL == siocb->scm)
  727. siocb->scm = &scm;
  728. err = scm_send(sock, msg, siocb->scm);
  729. if (err < 0)
  730. return err;
  731. if (msg->msg_namelen) {
  732. if (addr->nl_family != AF_NETLINK)
  733. return -EINVAL;
  734. dst_pid = addr->nl_pid;
  735. dst_groups = addr->nl_groups;
  736. if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND))
  737. return -EPERM;
  738. } else {
  739. dst_pid = nlk->dst_pid;
  740. dst_groups = nlk->dst_groups;
  741. }
  742. if (!nlk->pid) {
  743. err = netlink_autobind(sock);
  744. if (err)
  745. goto out;
  746. }
  747. err = -EMSGSIZE;
  748. if (len > sk->sk_sndbuf - 32)
  749. goto out;
  750. err = -ENOBUFS;
  751. skb = alloc_skb(len, GFP_KERNEL);
  752. if (skb==NULL)
  753. goto out;
  754. NETLINK_CB(skb).pid = nlk->pid;
  755. NETLINK_CB(skb).groups = nlk->groups;
  756. NETLINK_CB(skb).dst_pid = dst_pid;
  757. NETLINK_CB(skb).dst_groups = dst_groups;
  758. NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context);
  759. memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred));
  760. /* What can I do? Netlink is asynchronous, so that
  761. we will have to save current capabilities to
  762. check them, when this message will be delivered
  763. to corresponding kernel module. --ANK (980802)
  764. */
  765. err = -EFAULT;
  766. if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
  767. kfree_skb(skb);
  768. goto out;
  769. }
  770. err = security_netlink_send(sk, skb);
  771. if (err) {
  772. kfree_skb(skb);
  773. goto out;
  774. }
  775. if (dst_groups) {
  776. atomic_inc(&skb->users);
  777. netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
  778. }
  779. err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
  780. out:
  781. return err;
  782. }
  783. static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
  784. struct msghdr *msg, size_t len,
  785. int flags)
  786. {
  787. struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
  788. struct scm_cookie scm;
  789. struct sock *sk = sock->sk;
  790. struct netlink_sock *nlk = nlk_sk(sk);
  791. int noblock = flags&MSG_DONTWAIT;
  792. size_t copied;
  793. struct sk_buff *skb;
  794. int err;
  795. if (flags&MSG_OOB)
  796. return -EOPNOTSUPP;
  797. copied = 0;
  798. skb = skb_recv_datagram(sk,flags,noblock,&err);
  799. if (skb==NULL)
  800. goto out;
  801. msg->msg_namelen = 0;
  802. copied = skb->len;
  803. if (len < copied) {
  804. msg->msg_flags |= MSG_TRUNC;
  805. copied = len;
  806. }
  807. skb->h.raw = skb->data;
  808. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  809. if (msg->msg_name) {
  810. struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
  811. addr->nl_family = AF_NETLINK;
  812. addr->nl_pad = 0;
  813. addr->nl_pid = NETLINK_CB(skb).pid;
  814. addr->nl_groups = NETLINK_CB(skb).dst_groups;
  815. msg->msg_namelen = sizeof(*addr);
  816. }
  817. if (NULL == siocb->scm) {
  818. memset(&scm, 0, sizeof(scm));
  819. siocb->scm = &scm;
  820. }
  821. siocb->scm->creds = *NETLINK_CREDS(skb);
  822. skb_free_datagram(sk, skb);
  823. if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2)
  824. netlink_dump(sk);
  825. scm_recv(sock, msg, siocb->scm, flags);
  826. out:
  827. netlink_rcv_wake(sk);
  828. return err ? : copied;
  829. }
  830. static void netlink_data_ready(struct sock *sk, int len)
  831. {
  832. struct netlink_sock *nlk = nlk_sk(sk);
  833. if (nlk->data_ready)
  834. nlk->data_ready(sk, len);
  835. netlink_rcv_wake(sk);
  836. }
  837. /*
  838. * We export these functions to other modules. They provide a
  839. * complete set of kernel non-blocking support for message
  840. * queueing.
  841. */
  842. struct sock *
  843. netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
  844. {
  845. struct socket *sock;
  846. struct sock *sk;
  847. if (!nl_table)
  848. return NULL;
  849. if (unit<0 || unit>=MAX_LINKS)
  850. return NULL;
  851. if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
  852. return NULL;
  853. if (netlink_create(sock, unit) < 0) {
  854. sock_release(sock);
  855. return NULL;
  856. }
  857. sk = sock->sk;
  858. sk->sk_data_ready = netlink_data_ready;
  859. if (input)
  860. nlk_sk(sk)->data_ready = input;
  861. if (netlink_insert(sk, 0)) {
  862. sock_release(sock);
  863. return NULL;
  864. }
  865. return sk;
  866. }
  867. void netlink_set_nonroot(int protocol, unsigned int flags)
  868. {
  869. if ((unsigned int)protocol < MAX_LINKS)
  870. nl_table[protocol].nl_nonroot = flags;
  871. }
  872. static void netlink_destroy_callback(struct netlink_callback *cb)
  873. {
  874. if (cb->skb)
  875. kfree_skb(cb->skb);
  876. kfree(cb);
  877. }
  878. /*
  879. * It looks a bit ugly.
  880. * It would be better to create kernel thread.
  881. */
  882. static int netlink_dump(struct sock *sk)
  883. {
  884. struct netlink_sock *nlk = nlk_sk(sk);
  885. struct netlink_callback *cb;
  886. struct sk_buff *skb;
  887. struct nlmsghdr *nlh;
  888. int len;
  889. skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
  890. if (!skb)
  891. return -ENOBUFS;
  892. spin_lock(&nlk->cb_lock);
  893. cb = nlk->cb;
  894. if (cb == NULL) {
  895. spin_unlock(&nlk->cb_lock);
  896. kfree_skb(skb);
  897. return -EINVAL;
  898. }
  899. len = cb->dump(skb, cb);
  900. if (len > 0) {
  901. spin_unlock(&nlk->cb_lock);
  902. skb_queue_tail(&sk->sk_receive_queue, skb);
  903. sk->sk_data_ready(sk, len);
  904. return 0;
  905. }
  906. nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
  907. memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
  908. skb_queue_tail(&sk->sk_receive_queue, skb);
  909. sk->sk_data_ready(sk, skb->len);
  910. cb->done(cb);
  911. nlk->cb = NULL;
  912. spin_unlock(&nlk->cb_lock);
  913. netlink_destroy_callback(cb);
  914. return 0;
  915. nlmsg_failure:
  916. return -ENOBUFS;
  917. }
  918. int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  919. struct nlmsghdr *nlh,
  920. int (*dump)(struct sk_buff *skb, struct netlink_callback*),
  921. int (*done)(struct netlink_callback*))
  922. {
  923. struct netlink_callback *cb;
  924. struct sock *sk;
  925. struct netlink_sock *nlk;
  926. cb = kmalloc(sizeof(*cb), GFP_KERNEL);
  927. if (cb == NULL)
  928. return -ENOBUFS;
  929. memset(cb, 0, sizeof(*cb));
  930. cb->dump = dump;
  931. cb->done = done;
  932. cb->nlh = nlh;
  933. atomic_inc(&skb->users);
  934. cb->skb = skb;
  935. sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
  936. if (sk == NULL) {
  937. netlink_destroy_callback(cb);
  938. return -ECONNREFUSED;
  939. }
  940. nlk = nlk_sk(sk);
  941. /* A dump is in progress... */
  942. spin_lock(&nlk->cb_lock);
  943. if (nlk->cb) {
  944. spin_unlock(&nlk->cb_lock);
  945. netlink_destroy_callback(cb);
  946. sock_put(sk);
  947. return -EBUSY;
  948. }
  949. nlk->cb = cb;
  950. spin_unlock(&nlk->cb_lock);
  951. netlink_dump(sk);
  952. sock_put(sk);
  953. return 0;
  954. }
  955. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
  956. {
  957. struct sk_buff *skb;
  958. struct nlmsghdr *rep;
  959. struct nlmsgerr *errmsg;
  960. int size;
  961. if (err == 0)
  962. size = NLMSG_SPACE(sizeof(struct nlmsgerr));
  963. else
  964. size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
  965. skb = alloc_skb(size, GFP_KERNEL);
  966. if (!skb) {
  967. struct sock *sk;
  968. sk = netlink_lookup(in_skb->sk->sk_protocol,
  969. NETLINK_CB(in_skb).pid);
  970. if (sk) {
  971. sk->sk_err = ENOBUFS;
  972. sk->sk_error_report(sk);
  973. sock_put(sk);
  974. }
  975. return;
  976. }
  977. rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  978. NLMSG_ERROR, sizeof(struct nlmsgerr), 0);
  979. errmsg = NLMSG_DATA(rep);
  980. errmsg->error = err;
  981. memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
  982. netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  983. }
  984. #ifdef CONFIG_PROC_FS
  985. struct nl_seq_iter {
  986. int link;
  987. int hash_idx;
  988. };
  989. static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
  990. {
  991. struct nl_seq_iter *iter = seq->private;
  992. int i, j;
  993. struct sock *s;
  994. struct hlist_node *node;
  995. loff_t off = 0;
  996. for (i=0; i<MAX_LINKS; i++) {
  997. struct nl_pid_hash *hash = &nl_table[i].hash;
  998. for (j = 0; j <= hash->mask; j++) {
  999. sk_for_each(s, node, &hash->table[j]) {
  1000. if (off == pos) {
  1001. iter->link = i;
  1002. iter->hash_idx = j;
  1003. return s;
  1004. }
  1005. ++off;
  1006. }
  1007. }
  1008. }
  1009. return NULL;
  1010. }
  1011. static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
  1012. {
  1013. read_lock(&nl_table_lock);
  1014. return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1015. }
  1016. static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1017. {
  1018. struct sock *s;
  1019. struct nl_seq_iter *iter;
  1020. int i, j;
  1021. ++*pos;
  1022. if (v == SEQ_START_TOKEN)
  1023. return netlink_seq_socket_idx(seq, 0);
  1024. s = sk_next(v);
  1025. if (s)
  1026. return s;
  1027. iter = seq->private;
  1028. i = iter->link;
  1029. j = iter->hash_idx + 1;
  1030. do {
  1031. struct nl_pid_hash *hash = &nl_table[i].hash;
  1032. for (; j <= hash->mask; j++) {
  1033. s = sk_head(&hash->table[j]);
  1034. if (s) {
  1035. iter->link = i;
  1036. iter->hash_idx = j;
  1037. return s;
  1038. }
  1039. }
  1040. j = 0;
  1041. } while (++i < MAX_LINKS);
  1042. return NULL;
  1043. }
  1044. static void netlink_seq_stop(struct seq_file *seq, void *v)
  1045. {
  1046. read_unlock(&nl_table_lock);
  1047. }
  1048. static int netlink_seq_show(struct seq_file *seq, void *v)
  1049. {
  1050. if (v == SEQ_START_TOKEN)
  1051. seq_puts(seq,
  1052. "sk Eth Pid Groups "
  1053. "Rmem Wmem Dump Locks\n");
  1054. else {
  1055. struct sock *s = v;
  1056. struct netlink_sock *nlk = nlk_sk(s);
  1057. seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
  1058. s,
  1059. s->sk_protocol,
  1060. nlk->pid,
  1061. nlk->groups,
  1062. atomic_read(&s->sk_rmem_alloc),
  1063. atomic_read(&s->sk_wmem_alloc),
  1064. nlk->cb,
  1065. atomic_read(&s->sk_refcnt)
  1066. );
  1067. }
  1068. return 0;
  1069. }
  1070. static struct seq_operations netlink_seq_ops = {
  1071. .start = netlink_seq_start,
  1072. .next = netlink_seq_next,
  1073. .stop = netlink_seq_stop,
  1074. .show = netlink_seq_show,
  1075. };
  1076. static int netlink_seq_open(struct inode *inode, struct file *file)
  1077. {
  1078. struct seq_file *seq;
  1079. struct nl_seq_iter *iter;
  1080. int err;
  1081. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  1082. if (!iter)
  1083. return -ENOMEM;
  1084. err = seq_open(file, &netlink_seq_ops);
  1085. if (err) {
  1086. kfree(iter);
  1087. return err;
  1088. }
  1089. memset(iter, 0, sizeof(*iter));
  1090. seq = file->private_data;
  1091. seq->private = iter;
  1092. return 0;
  1093. }
  1094. static struct file_operations netlink_seq_fops = {
  1095. .owner = THIS_MODULE,
  1096. .open = netlink_seq_open,
  1097. .read = seq_read,
  1098. .llseek = seq_lseek,
  1099. .release = seq_release_private,
  1100. };
  1101. #endif
  1102. int netlink_register_notifier(struct notifier_block *nb)
  1103. {
  1104. return notifier_chain_register(&netlink_chain, nb);
  1105. }
  1106. int netlink_unregister_notifier(struct notifier_block *nb)
  1107. {
  1108. return notifier_chain_unregister(&netlink_chain, nb);
  1109. }
  1110. static struct proto_ops netlink_ops = {
  1111. .family = PF_NETLINK,
  1112. .owner = THIS_MODULE,
  1113. .release = netlink_release,
  1114. .bind = netlink_bind,
  1115. .connect = netlink_connect,
  1116. .socketpair = sock_no_socketpair,
  1117. .accept = sock_no_accept,
  1118. .getname = netlink_getname,
  1119. .poll = datagram_poll,
  1120. .ioctl = sock_no_ioctl,
  1121. .listen = sock_no_listen,
  1122. .shutdown = sock_no_shutdown,
  1123. .setsockopt = sock_no_setsockopt,
  1124. .getsockopt = sock_no_getsockopt,
  1125. .sendmsg = netlink_sendmsg,
  1126. .recvmsg = netlink_recvmsg,
  1127. .mmap = sock_no_mmap,
  1128. .sendpage = sock_no_sendpage,
  1129. };
  1130. static struct net_proto_family netlink_family_ops = {
  1131. .family = PF_NETLINK,
  1132. .create = netlink_create,
  1133. .owner = THIS_MODULE, /* for consistency 8) */
  1134. };
  1135. extern void netlink_skb_parms_too_large(void);
  1136. static int __init netlink_proto_init(void)
  1137. {
  1138. struct sk_buff *dummy_skb;
  1139. int i;
  1140. unsigned long max;
  1141. unsigned int order;
  1142. int err = proto_register(&netlink_proto, 0);
  1143. if (err != 0)
  1144. goto out;
  1145. if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb))
  1146. netlink_skb_parms_too_large();
  1147. nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL);
  1148. if (!nl_table) {
  1149. enomem:
  1150. printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n");
  1151. return -ENOMEM;
  1152. }
  1153. memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS);
  1154. if (num_physpages >= (128 * 1024))
  1155. max = num_physpages >> (21 - PAGE_SHIFT);
  1156. else
  1157. max = num_physpages >> (23 - PAGE_SHIFT);
  1158. order = get_bitmask_order(max) - 1 + PAGE_SHIFT;
  1159. max = (1UL << order) / sizeof(struct hlist_head);
  1160. order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1;
  1161. for (i = 0; i < MAX_LINKS; i++) {
  1162. struct nl_pid_hash *hash = &nl_table[i].hash;
  1163. hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table));
  1164. if (!hash->table) {
  1165. while (i-- > 0)
  1166. nl_pid_hash_free(nl_table[i].hash.table,
  1167. 1 * sizeof(*hash->table));
  1168. kfree(nl_table);
  1169. goto enomem;
  1170. }
  1171. memset(hash->table, 0, 1 * sizeof(*hash->table));
  1172. hash->max_shift = order;
  1173. hash->shift = 0;
  1174. hash->mask = 0;
  1175. hash->rehash_time = jiffies;
  1176. }
  1177. sock_register(&netlink_family_ops);
  1178. #ifdef CONFIG_PROC_FS
  1179. proc_net_fops_create("netlink", 0, &netlink_seq_fops);
  1180. #endif
  1181. /* The netlink device handler may be needed early. */
  1182. rtnetlink_init();
  1183. out:
  1184. return err;
  1185. }
  1186. static void __exit netlink_proto_exit(void)
  1187. {
  1188. sock_unregister(PF_NETLINK);
  1189. proc_net_remove("netlink");
  1190. kfree(nl_table);
  1191. nl_table = NULL;
  1192. proto_unregister(&netlink_proto);
  1193. }
  1194. core_initcall(netlink_proto_init);
  1195. module_exit(netlink_proto_exit);
  1196. MODULE_LICENSE("GPL");
  1197. MODULE_ALIAS_NETPROTO(PF_NETLINK);
  1198. EXPORT_SYMBOL(netlink_ack);
  1199. EXPORT_SYMBOL(netlink_broadcast);
  1200. EXPORT_SYMBOL(netlink_dump_start);
  1201. EXPORT_SYMBOL(netlink_kernel_create);
  1202. EXPORT_SYMBOL(netlink_register_notifier);
  1203. EXPORT_SYMBOL(netlink_set_err);
  1204. EXPORT_SYMBOL(netlink_set_nonroot);
  1205. EXPORT_SYMBOL(netlink_unicast);
  1206. EXPORT_SYMBOL(netlink_unregister_notifier);