af_netlink.c 31 KB

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