af_netlink.c (1b8d7ae42d02e483ad94035cca851e4f7fbecb40) af_netlink.c (b4b510290b056b86611757ce1175a230f1080f53)
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

--- 197 unchanged lines hidden (view full) ---

206
207static __inline__ void
208netlink_unlock_table(void)
209{
210 if (atomic_dec_and_test(&nl_table_users))
211 wake_up(&nl_table_wait);
212}
213
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

--- 197 unchanged lines hidden (view full) ---

206
207static __inline__ void
208netlink_unlock_table(void)
209{
210 if (atomic_dec_and_test(&nl_table_users))
211 wake_up(&nl_table_wait);
212}
213
214static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
214static __inline__ struct sock *netlink_lookup(struct net *net, int protocol, u32 pid)
215{
216 struct nl_pid_hash *hash = &nl_table[protocol].hash;
217 struct hlist_head *head;
218 struct sock *sk;
219 struct hlist_node *node;
220
221 read_lock(&nl_table_lock);
222 head = nl_pid_hashfn(hash, pid);
223 sk_for_each(sk, node, head) {
215{
216 struct nl_pid_hash *hash = &nl_table[protocol].hash;
217 struct hlist_head *head;
218 struct sock *sk;
219 struct hlist_node *node;
220
221 read_lock(&nl_table_lock);
222 head = nl_pid_hashfn(hash, pid);
223 sk_for_each(sk, node, head) {
224 if (nlk_sk(sk)->pid == pid) {
224 if ((sk->sk_net == net) && (nlk_sk(sk)->pid == pid)) {
225 sock_hold(sk);
226 goto found;
227 }
228 }
229 sk = NULL;
230found:
231 read_unlock(&nl_table_lock);
232 return sk;

--- 90 unchanged lines hidden (view full) ---

323 mask |= nlk_sk(sk)->groups[i];
324 }
325 tbl->listeners[i] = mask;
326 }
327 /* this function is only called with the netlink table "grabbed", which
328 * makes sure updates are visible before bind or setsockopt return. */
329}
330
225 sock_hold(sk);
226 goto found;
227 }
228 }
229 sk = NULL;
230found:
231 read_unlock(&nl_table_lock);
232 return sk;

--- 90 unchanged lines hidden (view full) ---

323 mask |= nlk_sk(sk)->groups[i];
324 }
325 tbl->listeners[i] = mask;
326 }
327 /* this function is only called with the netlink table "grabbed", which
328 * makes sure updates are visible before bind or setsockopt return. */
329}
330
331static int netlink_insert(struct sock *sk, u32 pid)
331static int netlink_insert(struct sock *sk, struct net *net, u32 pid)
332{
333 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
334 struct hlist_head *head;
335 int err = -EADDRINUSE;
336 struct sock *osk;
337 struct hlist_node *node;
338 int len;
339
340 netlink_table_grab();
341 head = nl_pid_hashfn(hash, pid);
342 len = 0;
343 sk_for_each(osk, node, head) {
332{
333 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
334 struct hlist_head *head;
335 int err = -EADDRINUSE;
336 struct sock *osk;
337 struct hlist_node *node;
338 int len;
339
340 netlink_table_grab();
341 head = nl_pid_hashfn(hash, pid);
342 len = 0;
343 sk_for_each(osk, node, head) {
344 if (nlk_sk(osk)->pid == pid)
344 if ((osk->sk_net == net) && (nlk_sk(osk)->pid == pid))
345 break;
346 len++;
347 }
348 if (node)
349 goto err;
350
351 err = -EBUSY;
352 if (nlk_sk(sk)->pid)

--- 61 unchanged lines hidden (view full) ---

414
415static int netlink_create(struct net *net, struct socket *sock, int protocol)
416{
417 struct module *module = NULL;
418 struct mutex *cb_mutex;
419 struct netlink_sock *nlk;
420 int err = 0;
421
345 break;
346 len++;
347 }
348 if (node)
349 goto err;
350
351 err = -EBUSY;
352 if (nlk_sk(sk)->pid)

--- 61 unchanged lines hidden (view full) ---

414
415static int netlink_create(struct net *net, struct socket *sock, int protocol)
416{
417 struct module *module = NULL;
418 struct mutex *cb_mutex;
419 struct netlink_sock *nlk;
420 int err = 0;
421
422 if (net != &init_net)
423 return -EAFNOSUPPORT;
424
425 sock->state = SS_UNCONNECTED;
426
427 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
428 return -ESOCKTNOSUPPORT;
429
430 if (protocol<0 || protocol >= MAX_LINKS)
431 return -EPROTONOSUPPORT;
432

--- 43 unchanged lines hidden (view full) ---

476
477 sock->sk = NULL;
478 wake_up_interruptible_all(&nlk->wait);
479
480 skb_queue_purge(&sk->sk_write_queue);
481
482 if (nlk->pid && !nlk->subscriptions) {
483 struct netlink_notify n = {
422 sock->state = SS_UNCONNECTED;
423
424 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
425 return -ESOCKTNOSUPPORT;
426
427 if (protocol<0 || protocol >= MAX_LINKS)
428 return -EPROTONOSUPPORT;
429

--- 43 unchanged lines hidden (view full) ---

473
474 sock->sk = NULL;
475 wake_up_interruptible_all(&nlk->wait);
476
477 skb_queue_purge(&sk->sk_write_queue);
478
479 if (nlk->pid && !nlk->subscriptions) {
480 struct netlink_notify n = {
481 .net = sk->sk_net,
484 .protocol = sk->sk_protocol,
485 .pid = nlk->pid,
486 };
487 atomic_notifier_call_chain(&netlink_chain,
488 NETLINK_URELEASE, &n);
489 }
490
491 module_put(nlk->module);

--- 12 unchanged lines hidden (view full) ---

504
505 sock_put(sk);
506 return 0;
507}
508
509static int netlink_autobind(struct socket *sock)
510{
511 struct sock *sk = sock->sk;
482 .protocol = sk->sk_protocol,
483 .pid = nlk->pid,
484 };
485 atomic_notifier_call_chain(&netlink_chain,
486 NETLINK_URELEASE, &n);
487 }
488
489 module_put(nlk->module);

--- 12 unchanged lines hidden (view full) ---

502
503 sock_put(sk);
504 return 0;
505}
506
507static int netlink_autobind(struct socket *sock)
508{
509 struct sock *sk = sock->sk;
510 struct net *net = sk->sk_net;
512 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
513 struct hlist_head *head;
514 struct sock *osk;
515 struct hlist_node *node;
516 s32 pid = current->tgid;
517 int err;
518 static s32 rover = -4097;
519
520retry:
521 cond_resched();
522 netlink_table_grab();
523 head = nl_pid_hashfn(hash, pid);
524 sk_for_each(osk, node, head) {
511 struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash;
512 struct hlist_head *head;
513 struct sock *osk;
514 struct hlist_node *node;
515 s32 pid = current->tgid;
516 int err;
517 static s32 rover = -4097;
518
519retry:
520 cond_resched();
521 netlink_table_grab();
522 head = nl_pid_hashfn(hash, pid);
523 sk_for_each(osk, node, head) {
524 if ((osk->sk_net != net))
525 continue;
525 if (nlk_sk(osk)->pid == pid) {
526 /* Bind collision, search negative pid values. */
527 pid = rover--;
528 if (rover > -4097)
529 rover = -4097;
530 netlink_table_ungrab();
531 goto retry;
532 }
533 }
534 netlink_table_ungrab();
535
526 if (nlk_sk(osk)->pid == pid) {
527 /* Bind collision, search negative pid values. */
528 pid = rover--;
529 if (rover > -4097)
530 rover = -4097;
531 netlink_table_ungrab();
532 goto retry;
533 }
534 }
535 netlink_table_ungrab();
536
536 err = netlink_insert(sk, pid);
537 err = netlink_insert(sk, net, pid);
537 if (err == -EADDRINUSE)
538 goto retry;
539
540 /* If 2 threads race to autobind, that is fine. */
541 if (err == -EBUSY)
542 err = 0;
543
544 return err;

--- 48 unchanged lines hidden (view full) ---

593 out_unlock:
594 netlink_table_ungrab();
595 return err;
596}
597
598static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
599{
600 struct sock *sk = sock->sk;
538 if (err == -EADDRINUSE)
539 goto retry;
540
541 /* If 2 threads race to autobind, that is fine. */
542 if (err == -EBUSY)
543 err = 0;
544
545 return err;

--- 48 unchanged lines hidden (view full) ---

594 out_unlock:
595 netlink_table_ungrab();
596 return err;
597}
598
599static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
600{
601 struct sock *sk = sock->sk;
602 struct net *net = sk->sk_net;
601 struct netlink_sock *nlk = nlk_sk(sk);
602 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
603 int err;
604
605 if (nladdr->nl_family != AF_NETLINK)
606 return -EINVAL;
607
608 /* Only superuser is allowed to listen multicasts */

--- 5 unchanged lines hidden (view full) ---

614 return err;
615 }
616
617 if (nlk->pid) {
618 if (nladdr->nl_pid != nlk->pid)
619 return -EINVAL;
620 } else {
621 err = nladdr->nl_pid ?
603 struct netlink_sock *nlk = nlk_sk(sk);
604 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
605 int err;
606
607 if (nladdr->nl_family != AF_NETLINK)
608 return -EINVAL;
609
610 /* Only superuser is allowed to listen multicasts */

--- 5 unchanged lines hidden (view full) ---

616 return err;
617 }
618
619 if (nlk->pid) {
620 if (nladdr->nl_pid != nlk->pid)
621 return -EINVAL;
622 } else {
623 err = nladdr->nl_pid ?
622 netlink_insert(sk, nladdr->nl_pid) :
624 netlink_insert(sk, net, nladdr->nl_pid) :
623 netlink_autobind(sock);
624 if (err)
625 return err;
626 }
627
628 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
629 return 0;
630

--- 67 unchanged lines hidden (view full) ---

698 sk->sk_err = ENOBUFS;
699 sk->sk_error_report(sk);
700 }
701}
702
703static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
704{
705 int protocol = ssk->sk_protocol;
625 netlink_autobind(sock);
626 if (err)
627 return err;
628 }
629
630 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
631 return 0;
632

--- 67 unchanged lines hidden (view full) ---

700 sk->sk_err = ENOBUFS;
701 sk->sk_error_report(sk);
702 }
703}
704
705static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid)
706{
707 int protocol = ssk->sk_protocol;
708 struct net *net;
706 struct sock *sock;
707 struct netlink_sock *nlk;
708
709 struct sock *sock;
710 struct netlink_sock *nlk;
711
709 sock = netlink_lookup(protocol, pid);
712 net = ssk->sk_net;
713 sock = netlink_lookup(net, protocol, pid);
710 if (!sock)
711 return ERR_PTR(-ECONNREFUSED);
712
713 /* Don't bother queuing skb if kernel socket has no input function */
714 nlk = nlk_sk(sock);
715 if ((nlk->pid == 0 && !nlk->data_ready) ||
716 (sock->sk_state == NETLINK_CONNECTED &&
717 nlk->dst_pid != nlk_sk(ssk)->pid)) {

--- 164 unchanged lines hidden (view full) ---

882 sk->sk_data_ready(sk, skb->len);
883 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
884 }
885 return -1;
886}
887
888struct netlink_broadcast_data {
889 struct sock *exclude_sk;
714 if (!sock)
715 return ERR_PTR(-ECONNREFUSED);
716
717 /* Don't bother queuing skb if kernel socket has no input function */
718 nlk = nlk_sk(sock);
719 if ((nlk->pid == 0 && !nlk->data_ready) ||
720 (sock->sk_state == NETLINK_CONNECTED &&
721 nlk->dst_pid != nlk_sk(ssk)->pid)) {

--- 164 unchanged lines hidden (view full) ---

886 sk->sk_data_ready(sk, skb->len);
887 return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf;
888 }
889 return -1;
890}
891
892struct netlink_broadcast_data {
893 struct sock *exclude_sk;
894 struct net *net;
890 u32 pid;
891 u32 group;
892 int failure;
893 int congested;
894 int delivered;
895 gfp_t allocation;
896 struct sk_buff *skb, *skb2;
897};

--- 6 unchanged lines hidden (view full) ---

904
905 if (p->exclude_sk == sk)
906 goto out;
907
908 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
909 !test_bit(p->group - 1, nlk->groups))
910 goto out;
911
895 u32 pid;
896 u32 group;
897 int failure;
898 int congested;
899 int delivered;
900 gfp_t allocation;
901 struct sk_buff *skb, *skb2;
902};

--- 6 unchanged lines hidden (view full) ---

909
910 if (p->exclude_sk == sk)
911 goto out;
912
913 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
914 !test_bit(p->group - 1, nlk->groups))
915 goto out;
916
917 if ((sk->sk_net != p->net))
918 goto out;
919
912 if (p->failure) {
913 netlink_overrun(sk);
914 goto out;
915 }
916
917 sock_hold(sk);
918 if (p->skb2 == NULL) {
919 if (skb_shared(p->skb)) {

--- 22 unchanged lines hidden (view full) ---

942
943out:
944 return 0;
945}
946
947int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
948 u32 group, gfp_t allocation)
949{
920 if (p->failure) {
921 netlink_overrun(sk);
922 goto out;
923 }
924
925 sock_hold(sk);
926 if (p->skb2 == NULL) {
927 if (skb_shared(p->skb)) {

--- 22 unchanged lines hidden (view full) ---

950
951out:
952 return 0;
953}
954
955int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
956 u32 group, gfp_t allocation)
957{
958 struct net *net = ssk->sk_net;
950 struct netlink_broadcast_data info;
951 struct hlist_node *node;
952 struct sock *sk;
953
954 skb = netlink_trim(skb, allocation);
955
956 info.exclude_sk = ssk;
959 struct netlink_broadcast_data info;
960 struct hlist_node *node;
961 struct sock *sk;
962
963 skb = netlink_trim(skb, allocation);
964
965 info.exclude_sk = ssk;
966 info.net = net;
957 info.pid = pid;
958 info.group = group;
959 info.failure = 0;
960 info.congested = 0;
961 info.delivered = 0;
962 info.allocation = allocation;
963 info.skb = skb;
964 info.skb2 = NULL;

--- 32 unchanged lines hidden (view full) ---

997static inline int do_one_set_err(struct sock *sk,
998 struct netlink_set_err_data *p)
999{
1000 struct netlink_sock *nlk = nlk_sk(sk);
1001
1002 if (sk == p->exclude_sk)
1003 goto out;
1004
967 info.pid = pid;
968 info.group = group;
969 info.failure = 0;
970 info.congested = 0;
971 info.delivered = 0;
972 info.allocation = allocation;
973 info.skb = skb;
974 info.skb2 = NULL;

--- 32 unchanged lines hidden (view full) ---

1007static inline int do_one_set_err(struct sock *sk,
1008 struct netlink_set_err_data *p)
1009{
1010 struct netlink_sock *nlk = nlk_sk(sk);
1011
1012 if (sk == p->exclude_sk)
1013 goto out;
1014
1015 if (sk->sk_net != p->exclude_sk->sk_net)
1016 goto out;
1017
1005 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1006 !test_bit(p->group - 1, nlk->groups))
1007 goto out;
1008
1009 sk->sk_err = p->code;
1010 sk->sk_error_report(sk);
1011out:
1012 return 0;

--- 286 unchanged lines hidden (view full) ---

1299
1300/*
1301 * We export these functions to other modules. They provide a
1302 * complete set of kernel non-blocking support for message
1303 * queueing.
1304 */
1305
1306struct sock *
1018 if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups ||
1019 !test_bit(p->group - 1, nlk->groups))
1020 goto out;
1021
1022 sk->sk_err = p->code;
1023 sk->sk_error_report(sk);
1024out:
1025 return 0;

--- 286 unchanged lines hidden (view full) ---

1312
1313/*
1314 * We export these functions to other modules. They provide a
1315 * complete set of kernel non-blocking support for message
1316 * queueing.
1317 */
1318
1319struct sock *
1307netlink_kernel_create(int unit, unsigned int groups,
1320netlink_kernel_create(struct net *net, int unit, unsigned int groups,
1308 void (*input)(struct sock *sk, int len),
1309 struct mutex *cb_mutex, struct module *module)
1310{
1311 struct socket *sock;
1312 struct sock *sk;
1313 struct netlink_sock *nlk;
1314 unsigned long *listeners = NULL;
1315
1316 BUG_ON(!nl_table);
1317
1318 if (unit<0 || unit>=MAX_LINKS)
1319 return NULL;
1320
1321 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1322 return NULL;
1323
1321 void (*input)(struct sock *sk, int len),
1322 struct mutex *cb_mutex, struct module *module)
1323{
1324 struct socket *sock;
1325 struct sock *sk;
1326 struct netlink_sock *nlk;
1327 unsigned long *listeners = NULL;
1328
1329 BUG_ON(!nl_table);
1330
1331 if (unit<0 || unit>=MAX_LINKS)
1332 return NULL;
1333
1334 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1335 return NULL;
1336
1324 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1337 if (__netlink_create(net, sock, cb_mutex, unit) < 0)
1325 goto out_sock_release;
1326
1327 if (groups < 32)
1328 groups = 32;
1329
1330 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
1331 if (!listeners)
1332 goto out_sock_release;
1333
1334 sk = sock->sk;
1335 sk->sk_data_ready = netlink_data_ready;
1336 if (input)
1337 nlk_sk(sk)->data_ready = input;
1338
1338 goto out_sock_release;
1339
1340 if (groups < 32)
1341 groups = 32;
1342
1343 listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL);
1344 if (!listeners)
1345 goto out_sock_release;
1346
1347 sk = sock->sk;
1348 sk->sk_data_ready = netlink_data_ready;
1349 if (input)
1350 nlk_sk(sk)->data_ready = input;
1351
1339 if (netlink_insert(sk, 0))
1352 if (netlink_insert(sk, net, 0))
1340 goto out_sock_release;
1341
1342 nlk = nlk_sk(sk);
1343 nlk->flags |= NETLINK_KERNEL_SOCKET;
1344
1345 netlink_table_grab();
1353 goto out_sock_release;
1354
1355 nlk = nlk_sk(sk);
1356 nlk->flags |= NETLINK_KERNEL_SOCKET;
1357
1358 netlink_table_grab();
1346 nl_table[unit].groups = groups;
1347 nl_table[unit].listeners = listeners;
1348 nl_table[unit].cb_mutex = cb_mutex;
1349 nl_table[unit].module = module;
1350 nl_table[unit].registered = 1;
1359 if (!nl_table[unit].registered) {
1360 nl_table[unit].groups = groups;
1361 nl_table[unit].listeners = listeners;
1362 nl_table[unit].cb_mutex = cb_mutex;
1363 nl_table[unit].module = module;
1364 nl_table[unit].registered = 1;
1365 }
1351 netlink_table_ungrab();
1352
1353 return sk;
1354
1355out_sock_release:
1356 kfree(listeners);
1357 sock_release(sock);
1358 return NULL;

--- 149 unchanged lines hidden (view full) ---

1508 return -ENOBUFS;
1509
1510 cb->dump = dump;
1511 cb->done = done;
1512 cb->nlh = nlh;
1513 atomic_inc(&skb->users);
1514 cb->skb = skb;
1515
1366 netlink_table_ungrab();
1367
1368 return sk;
1369
1370out_sock_release:
1371 kfree(listeners);
1372 sock_release(sock);
1373 return NULL;

--- 149 unchanged lines hidden (view full) ---

1523 return -ENOBUFS;
1524
1525 cb->dump = dump;
1526 cb->done = done;
1527 cb->nlh = nlh;
1528 atomic_inc(&skb->users);
1529 cb->skb = skb;
1530
1516 sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid);
1531 sk = netlink_lookup(ssk->sk_net, ssk->sk_protocol, NETLINK_CB(skb).pid);
1517 if (sk == NULL) {
1518 netlink_destroy_callback(cb);
1519 return -ECONNREFUSED;
1520 }
1521 nlk = nlk_sk(sk);
1522 /* A dump is in progress... */
1523 mutex_lock(nlk->cb_mutex);
1524 if (nlk->cb) {

--- 25 unchanged lines hidden (view full) ---

1550 /* error messages get the original request appened */
1551 if (err)
1552 payload += nlmsg_len(nlh);
1553
1554 skb = nlmsg_new(payload, GFP_KERNEL);
1555 if (!skb) {
1556 struct sock *sk;
1557
1532 if (sk == NULL) {
1533 netlink_destroy_callback(cb);
1534 return -ECONNREFUSED;
1535 }
1536 nlk = nlk_sk(sk);
1537 /* A dump is in progress... */
1538 mutex_lock(nlk->cb_mutex);
1539 if (nlk->cb) {

--- 25 unchanged lines hidden (view full) ---

1565 /* error messages get the original request appened */
1566 if (err)
1567 payload += nlmsg_len(nlh);
1568
1569 skb = nlmsg_new(payload, GFP_KERNEL);
1570 if (!skb) {
1571 struct sock *sk;
1572
1558 sk = netlink_lookup(in_skb->sk->sk_protocol,
1573 sk = netlink_lookup(in_skb->sk->sk_net,
1574 in_skb->sk->sk_protocol,
1559 NETLINK_CB(in_skb).pid);
1560 if (sk) {
1561 sk->sk_err = ENOBUFS;
1562 sk->sk_error_report(sk);
1563 sock_put(sk);
1564 }
1565 return;
1566 }

--- 134 unchanged lines hidden (view full) ---

1701 if (report)
1702 err = nlmsg_unicast(sk, skb, pid);
1703
1704 return err;
1705}
1706
1707#ifdef CONFIG_PROC_FS
1708struct nl_seq_iter {
1575 NETLINK_CB(in_skb).pid);
1576 if (sk) {
1577 sk->sk_err = ENOBUFS;
1578 sk->sk_error_report(sk);
1579 sock_put(sk);
1580 }
1581 return;
1582 }

--- 134 unchanged lines hidden (view full) ---

1717 if (report)
1718 err = nlmsg_unicast(sk, skb, pid);
1719
1720 return err;
1721}
1722
1723#ifdef CONFIG_PROC_FS
1724struct nl_seq_iter {
1725 struct net *net;
1709 int link;
1710 int hash_idx;
1711};
1712
1713static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1714{
1715 struct nl_seq_iter *iter = seq->private;
1716 int i, j;
1717 struct sock *s;
1718 struct hlist_node *node;
1719 loff_t off = 0;
1720
1721 for (i=0; i<MAX_LINKS; i++) {
1722 struct nl_pid_hash *hash = &nl_table[i].hash;
1723
1724 for (j = 0; j <= hash->mask; j++) {
1725 sk_for_each(s, node, &hash->table[j]) {
1726 int link;
1727 int hash_idx;
1728};
1729
1730static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
1731{
1732 struct nl_seq_iter *iter = seq->private;
1733 int i, j;
1734 struct sock *s;
1735 struct hlist_node *node;
1736 loff_t off = 0;
1737
1738 for (i=0; i<MAX_LINKS; i++) {
1739 struct nl_pid_hash *hash = &nl_table[i].hash;
1740
1741 for (j = 0; j <= hash->mask; j++) {
1742 sk_for_each(s, node, &hash->table[j]) {
1743 if (iter->net != s->sk_net)
1744 continue;
1726 if (off == pos) {
1727 iter->link = i;
1728 iter->hash_idx = j;
1729 return s;
1730 }
1731 ++off;
1732 }
1733 }

--- 13 unchanged lines hidden (view full) ---

1747 struct nl_seq_iter *iter;
1748 int i, j;
1749
1750 ++*pos;
1751
1752 if (v == SEQ_START_TOKEN)
1753 return netlink_seq_socket_idx(seq, 0);
1754
1745 if (off == pos) {
1746 iter->link = i;
1747 iter->hash_idx = j;
1748 return s;
1749 }
1750 ++off;
1751 }
1752 }

--- 13 unchanged lines hidden (view full) ---

1766 struct nl_seq_iter *iter;
1767 int i, j;
1768
1769 ++*pos;
1770
1771 if (v == SEQ_START_TOKEN)
1772 return netlink_seq_socket_idx(seq, 0);
1773
1755 s = sk_next(v);
1774 iter = seq->private;
1775 s = v;
1776 do {
1777 s = sk_next(s);
1778 } while (s && (iter->net != s->sk_net));
1756 if (s)
1757 return s;
1758
1779 if (s)
1780 return s;
1781
1759 iter = seq->private;
1760 i = iter->link;
1761 j = iter->hash_idx + 1;
1762
1763 do {
1764 struct nl_pid_hash *hash = &nl_table[i].hash;
1765
1766 for (; j <= hash->mask; j++) {
1767 s = sk_head(&hash->table[j]);
1782 i = iter->link;
1783 j = iter->hash_idx + 1;
1784
1785 do {
1786 struct nl_pid_hash *hash = &nl_table[i].hash;
1787
1788 for (; j <= hash->mask; j++) {
1789 s = sk_head(&hash->table[j]);
1790 while (s && (iter->net != s->sk_net))
1791 s = sk_next(s);
1768 if (s) {
1769 iter->link = i;
1770 iter->hash_idx = j;
1771 return s;
1772 }
1773 }
1774
1775 j = 0;

--- 54 unchanged lines hidden (view full) ---

1830 err = seq_open(file, &netlink_seq_ops);
1831 if (err) {
1832 kfree(iter);
1833 return err;
1834 }
1835
1836 seq = file->private_data;
1837 seq->private = iter;
1792 if (s) {
1793 iter->link = i;
1794 iter->hash_idx = j;
1795 return s;
1796 }
1797 }
1798
1799 j = 0;

--- 54 unchanged lines hidden (view full) ---

1854 err = seq_open(file, &netlink_seq_ops);
1855 if (err) {
1856 kfree(iter);
1857 return err;
1858 }
1859
1860 seq = file->private_data;
1861 seq->private = iter;
1862 iter->net = get_net(PROC_NET(inode));
1838 return 0;
1839}
1840
1863 return 0;
1864}
1865
1866static int netlink_seq_release(struct inode *inode, struct file *file)
1867{
1868 struct seq_file *seq = file->private_data;
1869 struct nl_seq_iter *iter = seq->private;
1870 put_net(iter->net);
1871 return seq_release_private(inode, file);
1872}
1873
1841static const struct file_operations netlink_seq_fops = {
1842 .owner = THIS_MODULE,
1843 .open = netlink_seq_open,
1844 .read = seq_read,
1845 .llseek = seq_lseek,
1874static const struct file_operations netlink_seq_fops = {
1875 .owner = THIS_MODULE,
1876 .open = netlink_seq_open,
1877 .read = seq_read,
1878 .llseek = seq_lseek,
1846 .release = seq_release_private,
1879 .release = netlink_seq_release,
1847};
1848
1849#endif
1850
1851int netlink_register_notifier(struct notifier_block *nb)
1852{
1853 return atomic_notifier_chain_register(&netlink_chain, nb);
1854}

--- 25 unchanged lines hidden (view full) ---

1880};
1881
1882static struct net_proto_family netlink_family_ops = {
1883 .family = PF_NETLINK,
1884 .create = netlink_create,
1885 .owner = THIS_MODULE, /* for consistency 8) */
1886};
1887
1880};
1881
1882#endif
1883
1884int netlink_register_notifier(struct notifier_block *nb)
1885{
1886 return atomic_notifier_chain_register(&netlink_chain, nb);
1887}

--- 25 unchanged lines hidden (view full) ---

1913};
1914
1915static struct net_proto_family netlink_family_ops = {
1916 .family = PF_NETLINK,
1917 .create = netlink_create,
1918 .owner = THIS_MODULE, /* for consistency 8) */
1919};
1920
1921static int netlink_net_init(struct net *net)
1922{
1923#ifdef CONFIG_PROC_FS
1924 if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops))
1925 return -ENOMEM;
1926#endif
1927 return 0;
1928}
1929
1930static void netlink_net_exit(struct net *net)
1931{
1932#ifdef CONFIG_PROC_FS
1933 proc_net_remove(net, "netlink");
1934#endif
1935}
1936
1937static struct pernet_operations netlink_net_ops = {
1938 .init = netlink_net_init,
1939 .exit = netlink_net_exit,
1940};
1941
1888static int __init netlink_proto_init(void)
1889{
1890 struct sk_buff *dummy_skb;
1891 int i;
1892 unsigned long max;
1893 unsigned int order;
1894 int err = proto_register(&netlink_proto, 0);
1895

--- 29 unchanged lines hidden (view full) ---

1925 memset(hash->table, 0, 1 * sizeof(*hash->table));
1926 hash->max_shift = order;
1927 hash->shift = 0;
1928 hash->mask = 0;
1929 hash->rehash_time = jiffies;
1930 }
1931
1932 sock_register(&netlink_family_ops);
1942static int __init netlink_proto_init(void)
1943{
1944 struct sk_buff *dummy_skb;
1945 int i;
1946 unsigned long max;
1947 unsigned int order;
1948 int err = proto_register(&netlink_proto, 0);
1949

--- 29 unchanged lines hidden (view full) ---

1979 memset(hash->table, 0, 1 * sizeof(*hash->table));
1980 hash->max_shift = order;
1981 hash->shift = 0;
1982 hash->mask = 0;
1983 hash->rehash_time = jiffies;
1984 }
1985
1986 sock_register(&netlink_family_ops);
1933#ifdef CONFIG_PROC_FS
1934 proc_net_fops_create(&init_net, "netlink", 0, &netlink_seq_fops);
1935#endif
1987 register_pernet_subsys(&netlink_net_ops);
1936 /* The netlink device handler may be needed early. */
1937 rtnetlink_init();
1938out:
1939 return err;
1940panic:
1941 panic("netlink_init: Cannot allocate nl_table\n");
1942}
1943

--- 12 unchanged lines hidden ---
1988 /* The netlink device handler may be needed early. */
1989 rtnetlink_init();
1990out:
1991 return err;
1992panic:
1993 panic("netlink_init: Cannot allocate nl_table\n");
1994}
1995

--- 12 unchanged lines hidden ---