af_netlink.c 31 KB

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