aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Pargmann <mpa@pengutronix.de>2016-05-15 05:07:43 -0400
committerSimon Wunderlich <sw@simonwunderlich.de>2016-06-30 04:29:43 -0400
commit1f8dce4992d03fc15cfbaf67cd09f0d1648c4606 (patch)
tree0cb7041da75c8d99d99bb727391640530885fc7f
parentfcafa5e74b42a182a5bcc5c7f94ca026d4e5f06e (diff)
batman-adv: split tvlv into a separate file
The tvlv functionality in main.c is mostly unrelated to the rest of the content. It still takes up a large portion of this source file (~45%, 588 lines). Moving it to a separate file makes it better visible as a main component of the batman-adv implementation and hides it less in the other helper functions in main.c Signed-off-by: Markus Pargmann <mpa@pengutronix.de> [sven@narfation.org: fix conflicts with current version, fix includes, rewrote commit message] Signed-off-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: Marek Lindner <mareklindner@neomailbox.ch> Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
-rw-r--r--net/batman-adv/Makefile1
-rw-r--r--net/batman-adv/bat_iv_ogm.c1
-rw-r--r--net/batman-adv/bat_v_ogm.c1
-rw-r--r--net/batman-adv/distributed-arp-table.c1
-rw-r--r--net/batman-adv/gateway_common.c1
-rw-r--r--net/batman-adv/main.c591
-rw-r--r--net/batman-adv/main.h34
-rw-r--r--net/batman-adv/multicast.c1
-rw-r--r--net/batman-adv/network-coding.c1
-rw-r--r--net/batman-adv/routing.c1
-rw-r--r--net/batman-adv/translation-table.c1
-rw-r--r--net/batman-adv/tvlv.c630
-rw-r--r--net/batman-adv/tvlv.h61
13 files changed, 700 insertions, 625 deletions
diff --git a/net/batman-adv/Makefile b/net/batman-adv/Makefile
index 797cf2fc88c1..5c6ece0cfc17 100644
--- a/net/batman-adv/Makefile
+++ b/net/batman-adv/Makefile
@@ -40,3 +40,4 @@ batman-adv-y += send.o
40batman-adv-y += soft-interface.o 40batman-adv-y += soft-interface.o
41batman-adv-y += sysfs.o 41batman-adv-y += sysfs.o
42batman-adv-y += translation-table.o 42batman-adv-y += translation-table.o
43batman-adv-y += tvlv.o
diff --git a/net/batman-adv/bat_iv_ogm.c b/net/batman-adv/bat_iv_ogm.c
index 6dc89b0eb01a..948a5b45474d 100644
--- a/net/batman-adv/bat_iv_ogm.c
+++ b/net/batman-adv/bat_iv_ogm.c
@@ -58,6 +58,7 @@
58#include "routing.h" 58#include "routing.h"
59#include "send.h" 59#include "send.h"
60#include "translation-table.h" 60#include "translation-table.h"
61#include "tvlv.h"
61 62
62static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work); 63static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
63 64
diff --git a/net/batman-adv/bat_v_ogm.c b/net/batman-adv/bat_v_ogm.c
index 23ea9bfb9f67..ca5a679d112f 100644
--- a/net/batman-adv/bat_v_ogm.c
+++ b/net/batman-adv/bat_v_ogm.c
@@ -46,6 +46,7 @@
46#include "routing.h" 46#include "routing.h"
47#include "send.h" 47#include "send.h"
48#include "translation-table.h" 48#include "translation-table.h"
49#include "tvlv.h"
49 50
50/** 51/**
51 * batadv_v_ogm_orig_get - retrieve and possibly create an originator node 52 * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
diff --git a/net/batman-adv/distributed-arp-table.c b/net/batman-adv/distributed-arp-table.c
index 278800a99c69..584b82744699 100644
--- a/net/batman-adv/distributed-arp-table.c
+++ b/net/batman-adv/distributed-arp-table.c
@@ -48,6 +48,7 @@
48#include "originator.h" 48#include "originator.h"
49#include "send.h" 49#include "send.h"
50#include "translation-table.h" 50#include "translation-table.h"
51#include "tvlv.h"
51 52
52static void batadv_dat_purge(struct work_struct *work); 53static void batadv_dat_purge(struct work_struct *work);
53 54
diff --git a/net/batman-adv/gateway_common.c b/net/batman-adv/gateway_common.c
index 7754435611eb..6a6f2d4987e5 100644
--- a/net/batman-adv/gateway_common.c
+++ b/net/batman-adv/gateway_common.c
@@ -29,6 +29,7 @@
29 29
30#include "gateway_client.h" 30#include "gateway_client.h"
31#include "packet.h" 31#include "packet.h"
32#include "tvlv.h"
32 33
33/** 34/**
34 * batadv_parse_throughput - parse supplied string buffer to extract throughput 35 * batadv_parse_throughput - parse supplied string buffer to extract throughput
diff --git a/net/batman-adv/main.c b/net/batman-adv/main.c
index 627d14ececaf..225d63e0c711 100644
--- a/net/batman-adv/main.c
+++ b/net/batman-adv/main.c
@@ -31,16 +31,13 @@
31#include <linux/kernel.h> 31#include <linux/kernel.h>
32#include <linux/kref.h> 32#include <linux/kref.h>
33#include <linux/list.h> 33#include <linux/list.h>
34#include <linux/lockdep.h>
35#include <linux/module.h> 34#include <linux/module.h>
36#include <linux/moduleparam.h> 35#include <linux/moduleparam.h>
37#include <linux/netdevice.h> 36#include <linux/netdevice.h>
38#include <linux/pkt_sched.h>
39#include <linux/rculist.h> 37#include <linux/rculist.h>
40#include <linux/rcupdate.h> 38#include <linux/rcupdate.h>
41#include <linux/seq_file.h> 39#include <linux/seq_file.h>
42#include <linux/skbuff.h> 40#include <linux/skbuff.h>
43#include <linux/slab.h>
44#include <linux/spinlock.h> 41#include <linux/spinlock.h>
45#include <linux/stddef.h> 42#include <linux/stddef.h>
46#include <linux/string.h> 43#include <linux/string.h>
@@ -642,594 +639,6 @@ __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
642} 639}
643 640
644/** 641/**
645 * batadv_tvlv_handler_release - release tvlv handler from lists and queue for
646 * free after rcu grace period
647 * @ref: kref pointer of the tvlv
648 */
649static void batadv_tvlv_handler_release(struct kref *ref)
650{
651 struct batadv_tvlv_handler *tvlv_handler;
652
653 tvlv_handler = container_of(ref, struct batadv_tvlv_handler, refcount);
654 kfree_rcu(tvlv_handler, rcu);
655}
656
657/**
658 * batadv_tvlv_handler_put - decrement the tvlv container refcounter and
659 * possibly release it
660 * @tvlv_handler: the tvlv handler to free
661 */
662static void batadv_tvlv_handler_put(struct batadv_tvlv_handler *tvlv_handler)
663{
664 kref_put(&tvlv_handler->refcount, batadv_tvlv_handler_release);
665}
666
667/**
668 * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
669 * based on the provided type and version (both need to match)
670 * @bat_priv: the bat priv with all the soft interface information
671 * @type: tvlv handler type to look for
672 * @version: tvlv handler version to look for
673 *
674 * Return: tvlv handler if found or NULL otherwise.
675 */
676static struct batadv_tvlv_handler *
677batadv_tvlv_handler_get(struct batadv_priv *bat_priv, u8 type, u8 version)
678{
679 struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
680
681 rcu_read_lock();
682 hlist_for_each_entry_rcu(tvlv_handler_tmp,
683 &bat_priv->tvlv.handler_list, list) {
684 if (tvlv_handler_tmp->type != type)
685 continue;
686
687 if (tvlv_handler_tmp->version != version)
688 continue;
689
690 if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
691 continue;
692
693 tvlv_handler = tvlv_handler_tmp;
694 break;
695 }
696 rcu_read_unlock();
697
698 return tvlv_handler;
699}
700
701/**
702 * batadv_tvlv_container_release - release tvlv from lists and free
703 * @ref: kref pointer of the tvlv
704 */
705static void batadv_tvlv_container_release(struct kref *ref)
706{
707 struct batadv_tvlv_container *tvlv;
708
709 tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
710 kfree(tvlv);
711}
712
713/**
714 * batadv_tvlv_container_put - decrement the tvlv container refcounter and
715 * possibly release it
716 * @tvlv: the tvlv container to free
717 */
718static void batadv_tvlv_container_put(struct batadv_tvlv_container *tvlv)
719{
720 kref_put(&tvlv->refcount, batadv_tvlv_container_release);
721}
722
723/**
724 * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
725 * list based on the provided type and version (both need to match)
726 * @bat_priv: the bat priv with all the soft interface information
727 * @type: tvlv container type to look for
728 * @version: tvlv container version to look for
729 *
730 * Has to be called with the appropriate locks being acquired
731 * (tvlv.container_list_lock).
732 *
733 * Return: tvlv container if found or NULL otherwise.
734 */
735static struct batadv_tvlv_container *
736batadv_tvlv_container_get(struct batadv_priv *bat_priv, u8 type, u8 version)
737{
738 struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
739
740 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
741
742 hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
743 if (tvlv_tmp->tvlv_hdr.type != type)
744 continue;
745
746 if (tvlv_tmp->tvlv_hdr.version != version)
747 continue;
748
749 kref_get(&tvlv_tmp->refcount);
750 tvlv = tvlv_tmp;
751 break;
752 }
753
754 return tvlv;
755}
756
757/**
758 * batadv_tvlv_container_list_size - calculate the size of the tvlv container
759 * list entries
760 * @bat_priv: the bat priv with all the soft interface information
761 *
762 * Has to be called with the appropriate locks being acquired
763 * (tvlv.container_list_lock).
764 *
765 * Return: size of all currently registered tvlv containers in bytes.
766 */
767static u16 batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
768{
769 struct batadv_tvlv_container *tvlv;
770 u16 tvlv_len = 0;
771
772 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
773
774 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
775 tvlv_len += sizeof(struct batadv_tvlv_hdr);
776 tvlv_len += ntohs(tvlv->tvlv_hdr.len);
777 }
778
779 return tvlv_len;
780}
781
782/**
783 * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
784 * list
785 * @bat_priv: the bat priv with all the soft interface information
786 * @tvlv: the to be removed tvlv container
787 *
788 * Has to be called with the appropriate locks being acquired
789 * (tvlv.container_list_lock).
790 */
791static void batadv_tvlv_container_remove(struct batadv_priv *bat_priv,
792 struct batadv_tvlv_container *tvlv)
793{
794 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
795
796 if (!tvlv)
797 return;
798
799 hlist_del(&tvlv->list);
800
801 /* first call to decrement the counter, second call to free */
802 batadv_tvlv_container_put(tvlv);
803 batadv_tvlv_container_put(tvlv);
804}
805
806/**
807 * batadv_tvlv_container_unregister - unregister tvlv container based on the
808 * provided type and version (both need to match)
809 * @bat_priv: the bat priv with all the soft interface information
810 * @type: tvlv container type to unregister
811 * @version: tvlv container type to unregister
812 */
813void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
814 u8 type, u8 version)
815{
816 struct batadv_tvlv_container *tvlv;
817
818 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
819 tvlv = batadv_tvlv_container_get(bat_priv, type, version);
820 batadv_tvlv_container_remove(bat_priv, tvlv);
821 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
822}
823
824/**
825 * batadv_tvlv_container_register - register tvlv type, version and content
826 * to be propagated with each (primary interface) OGM
827 * @bat_priv: the bat priv with all the soft interface information
828 * @type: tvlv container type
829 * @version: tvlv container version
830 * @tvlv_value: tvlv container content
831 * @tvlv_value_len: tvlv container content length
832 *
833 * If a container of the same type and version was already registered the new
834 * content is going to replace the old one.
835 */
836void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
837 u8 type, u8 version,
838 void *tvlv_value, u16 tvlv_value_len)
839{
840 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
841
842 if (!tvlv_value)
843 tvlv_value_len = 0;
844
845 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
846 if (!tvlv_new)
847 return;
848
849 tvlv_new->tvlv_hdr.version = version;
850 tvlv_new->tvlv_hdr.type = type;
851 tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
852
853 memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
854 INIT_HLIST_NODE(&tvlv_new->list);
855 kref_init(&tvlv_new->refcount);
856
857 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
858 tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
859 batadv_tvlv_container_remove(bat_priv, tvlv_old);
860 hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
861 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
862}
863
864/**
865 * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accommodate
866 * requested packet size
867 * @packet_buff: packet buffer
868 * @packet_buff_len: packet buffer size
869 * @min_packet_len: requested packet minimum size
870 * @additional_packet_len: requested additional packet size on top of minimum
871 * size
872 *
873 * Return: true of the packet buffer could be changed to the requested size,
874 * false otherwise.
875 */
876static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
877 int *packet_buff_len,
878 int min_packet_len,
879 int additional_packet_len)
880{
881 unsigned char *new_buff;
882
883 new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
884
885 /* keep old buffer if kmalloc should fail */
886 if (!new_buff)
887 return false;
888
889 memcpy(new_buff, *packet_buff, min_packet_len);
890 kfree(*packet_buff);
891 *packet_buff = new_buff;
892 *packet_buff_len = min_packet_len + additional_packet_len;
893
894 return true;
895}
896
897/**
898 * batadv_tvlv_container_ogm_append - append tvlv container content to given
899 * OGM packet buffer
900 * @bat_priv: the bat priv with all the soft interface information
901 * @packet_buff: ogm packet buffer
902 * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
903 * content
904 * @packet_min_len: ogm header size to be preserved for the OGM itself
905 *
906 * The ogm packet might be enlarged or shrunk depending on the current size
907 * and the size of the to-be-appended tvlv containers.
908 *
909 * Return: size of all appended tvlv containers in bytes.
910 */
911u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
912 unsigned char **packet_buff,
913 int *packet_buff_len, int packet_min_len)
914{
915 struct batadv_tvlv_container *tvlv;
916 struct batadv_tvlv_hdr *tvlv_hdr;
917 u16 tvlv_value_len;
918 void *tvlv_value;
919 bool ret;
920
921 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
922 tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
923
924 ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
925 packet_min_len, tvlv_value_len);
926
927 if (!ret)
928 goto end;
929
930 if (!tvlv_value_len)
931 goto end;
932
933 tvlv_value = (*packet_buff) + packet_min_len;
934
935 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
936 tvlv_hdr = tvlv_value;
937 tvlv_hdr->type = tvlv->tvlv_hdr.type;
938 tvlv_hdr->version = tvlv->tvlv_hdr.version;
939 tvlv_hdr->len = tvlv->tvlv_hdr.len;
940 tvlv_value = tvlv_hdr + 1;
941 memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
942 tvlv_value = (u8 *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
943 }
944
945end:
946 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
947 return tvlv_value_len;
948}
949
950/**
951 * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
952 * appropriate handlers
953 * @bat_priv: the bat priv with all the soft interface information
954 * @tvlv_handler: tvlv callback function handling the tvlv content
955 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
956 * @orig_node: orig node emitting the ogm packet
957 * @src: source mac address of the unicast packet
958 * @dst: destination mac address of the unicast packet
959 * @tvlv_value: tvlv content
960 * @tvlv_value_len: tvlv content length
961 *
962 * Return: success if handler was not found or the return value of the handler
963 * callback.
964 */
965static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
966 struct batadv_tvlv_handler *tvlv_handler,
967 bool ogm_source,
968 struct batadv_orig_node *orig_node,
969 u8 *src, u8 *dst,
970 void *tvlv_value, u16 tvlv_value_len)
971{
972 if (!tvlv_handler)
973 return NET_RX_SUCCESS;
974
975 if (ogm_source) {
976 if (!tvlv_handler->ogm_handler)
977 return NET_RX_SUCCESS;
978
979 if (!orig_node)
980 return NET_RX_SUCCESS;
981
982 tvlv_handler->ogm_handler(bat_priv, orig_node,
983 BATADV_NO_FLAGS,
984 tvlv_value, tvlv_value_len);
985 tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
986 } else {
987 if (!src)
988 return NET_RX_SUCCESS;
989
990 if (!dst)
991 return NET_RX_SUCCESS;
992
993 if (!tvlv_handler->unicast_handler)
994 return NET_RX_SUCCESS;
995
996 return tvlv_handler->unicast_handler(bat_priv, src,
997 dst, tvlv_value,
998 tvlv_value_len);
999 }
1000
1001 return NET_RX_SUCCESS;
1002}
1003
1004/**
1005 * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
1006 * appropriate handlers
1007 * @bat_priv: the bat priv with all the soft interface information
1008 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
1009 * @orig_node: orig node emitting the ogm packet
1010 * @src: source mac address of the unicast packet
1011 * @dst: destination mac address of the unicast packet
1012 * @tvlv_value: tvlv content
1013 * @tvlv_value_len: tvlv content length
1014 *
1015 * Return: success when processing an OGM or the return value of all called
1016 * handler callbacks.
1017 */
1018int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
1019 bool ogm_source,
1020 struct batadv_orig_node *orig_node,
1021 u8 *src, u8 *dst,
1022 void *tvlv_value, u16 tvlv_value_len)
1023{
1024 struct batadv_tvlv_handler *tvlv_handler;
1025 struct batadv_tvlv_hdr *tvlv_hdr;
1026 u16 tvlv_value_cont_len;
1027 u8 cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
1028 int ret = NET_RX_SUCCESS;
1029
1030 while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
1031 tvlv_hdr = tvlv_value;
1032 tvlv_value_cont_len = ntohs(tvlv_hdr->len);
1033 tvlv_value = tvlv_hdr + 1;
1034 tvlv_value_len -= sizeof(*tvlv_hdr);
1035
1036 if (tvlv_value_cont_len > tvlv_value_len)
1037 break;
1038
1039 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
1040 tvlv_hdr->type,
1041 tvlv_hdr->version);
1042
1043 ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
1044 ogm_source, orig_node,
1045 src, dst, tvlv_value,
1046 tvlv_value_cont_len);
1047 if (tvlv_handler)
1048 batadv_tvlv_handler_put(tvlv_handler);
1049 tvlv_value = (u8 *)tvlv_value + tvlv_value_cont_len;
1050 tvlv_value_len -= tvlv_value_cont_len;
1051 }
1052
1053 if (!ogm_source)
1054 return ret;
1055
1056 rcu_read_lock();
1057 hlist_for_each_entry_rcu(tvlv_handler,
1058 &bat_priv->tvlv.handler_list, list) {
1059 if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
1060 !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
1061 tvlv_handler->ogm_handler(bat_priv, orig_node,
1062 cifnotfound, NULL, 0);
1063
1064 tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
1065 }
1066 rcu_read_unlock();
1067
1068 return NET_RX_SUCCESS;
1069}
1070
1071/**
1072 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
1073 * handlers
1074 * @bat_priv: the bat priv with all the soft interface information
1075 * @batadv_ogm_packet: ogm packet containing the tvlv containers
1076 * @orig_node: orig node emitting the ogm packet
1077 */
1078void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
1079 struct batadv_ogm_packet *batadv_ogm_packet,
1080 struct batadv_orig_node *orig_node)
1081{
1082 void *tvlv_value;
1083 u16 tvlv_value_len;
1084
1085 if (!batadv_ogm_packet)
1086 return;
1087
1088 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
1089 if (!tvlv_value_len)
1090 return;
1091
1092 tvlv_value = batadv_ogm_packet + 1;
1093
1094 batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
1095 tvlv_value, tvlv_value_len);
1096}
1097
1098/**
1099 * batadv_tvlv_handler_register - register tvlv handler based on the provided
1100 * type and version (both need to match) for ogm tvlv payload and/or unicast
1101 * payload
1102 * @bat_priv: the bat priv with all the soft interface information
1103 * @optr: ogm tvlv handler callback function. This function receives the orig
1104 * node, flags and the tvlv content as argument to process.
1105 * @uptr: unicast tvlv handler callback function. This function receives the
1106 * source & destination of the unicast packet as well as the tvlv content
1107 * to process.
1108 * @type: tvlv handler type to be registered
1109 * @version: tvlv handler version to be registered
1110 * @flags: flags to enable or disable TVLV API behavior
1111 */
1112void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
1113 void (*optr)(struct batadv_priv *bat_priv,
1114 struct batadv_orig_node *orig,
1115 u8 flags,
1116 void *tvlv_value,
1117 u16 tvlv_value_len),
1118 int (*uptr)(struct batadv_priv *bat_priv,
1119 u8 *src, u8 *dst,
1120 void *tvlv_value,
1121 u16 tvlv_value_len),
1122 u8 type, u8 version, u8 flags)
1123{
1124 struct batadv_tvlv_handler *tvlv_handler;
1125
1126 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
1127 if (tvlv_handler) {
1128 batadv_tvlv_handler_put(tvlv_handler);
1129 return;
1130 }
1131
1132 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
1133 if (!tvlv_handler)
1134 return;
1135
1136 tvlv_handler->ogm_handler = optr;
1137 tvlv_handler->unicast_handler = uptr;
1138 tvlv_handler->type = type;
1139 tvlv_handler->version = version;
1140 tvlv_handler->flags = flags;
1141 kref_init(&tvlv_handler->refcount);
1142 INIT_HLIST_NODE(&tvlv_handler->list);
1143
1144 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
1145 hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
1146 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
1147}
1148
1149/**
1150 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
1151 * provided type and version (both need to match)
1152 * @bat_priv: the bat priv with all the soft interface information
1153 * @type: tvlv handler type to be unregistered
1154 * @version: tvlv handler version to be unregistered
1155 */
1156void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
1157 u8 type, u8 version)
1158{
1159 struct batadv_tvlv_handler *tvlv_handler;
1160
1161 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
1162 if (!tvlv_handler)
1163 return;
1164
1165 batadv_tvlv_handler_put(tvlv_handler);
1166 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
1167 hlist_del_rcu(&tvlv_handler->list);
1168 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
1169 batadv_tvlv_handler_put(tvlv_handler);
1170}
1171
1172/**
1173 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
1174 * specified host
1175 * @bat_priv: the bat priv with all the soft interface information
1176 * @src: source mac address of the unicast packet
1177 * @dst: destination mac address of the unicast packet
1178 * @type: tvlv type
1179 * @version: tvlv version
1180 * @tvlv_value: tvlv content
1181 * @tvlv_value_len: tvlv content length
1182 */
1183void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
1184 u8 *dst, u8 type, u8 version,
1185 void *tvlv_value, u16 tvlv_value_len)
1186{
1187 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1188 struct batadv_tvlv_hdr *tvlv_hdr;
1189 struct batadv_orig_node *orig_node;
1190 struct sk_buff *skb;
1191 unsigned char *tvlv_buff;
1192 unsigned int tvlv_len;
1193 ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
1194
1195 orig_node = batadv_orig_hash_find(bat_priv, dst);
1196 if (!orig_node)
1197 return;
1198
1199 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
1200
1201 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
1202 if (!skb)
1203 goto out;
1204
1205 skb->priority = TC_PRIO_CONTROL;
1206 skb_reserve(skb, ETH_HLEN);
1207 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
1208 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
1209 unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
1210 unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
1211 unicast_tvlv_packet->ttl = BATADV_TTL;
1212 unicast_tvlv_packet->reserved = 0;
1213 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
1214 unicast_tvlv_packet->align = 0;
1215 ether_addr_copy(unicast_tvlv_packet->src, src);
1216 ether_addr_copy(unicast_tvlv_packet->dst, dst);
1217
1218 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
1219 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
1220 tvlv_hdr->version = version;
1221 tvlv_hdr->type = type;
1222 tvlv_hdr->len = htons(tvlv_value_len);
1223 tvlv_buff += sizeof(*tvlv_hdr);
1224 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
1225
1226 if (batadv_send_skb_to_orig(skb, orig_node, NULL) == NET_XMIT_DROP)
1227 kfree_skb(skb);
1228out:
1229 batadv_orig_node_put(orig_node);
1230}
1231
1232/**
1233 * batadv_get_vid - extract the VLAN identifier from skb if any 642 * batadv_get_vid - extract the VLAN identifier from skb if any
1234 * @skb: the buffer containing the packet 643 * @skb: the buffer containing the packet
1235 * @header_len: length of the batman header preceding the ethernet header 644 * @header_len: length of the batman header preceding the ethernet header
diff --git a/net/batman-adv/main.h b/net/batman-adv/main.h
index 38f9e5523190..3af6582aab8b 100644
--- a/net/batman-adv/main.h
+++ b/net/batman-adv/main.h
@@ -190,7 +190,6 @@ enum batadv_uev_type {
190 190
191#include "types.h" 191#include "types.h"
192 192
193struct batadv_ogm_packet;
194struct seq_file; 193struct seq_file;
195struct sk_buff; 194struct sk_buff;
196 195
@@ -372,39 +371,6 @@ static inline u64 batadv_sum_counter(struct batadv_priv *bat_priv, size_t idx)
372 */ 371 */
373#define BATADV_SKB_CB(__skb) ((struct batadv_skb_cb *)&((__skb)->cb[0])) 372#define BATADV_SKB_CB(__skb) ((struct batadv_skb_cb *)&((__skb)->cb[0]))
374 373
375void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
376 u8 type, u8 version,
377 void *tvlv_value, u16 tvlv_value_len);
378u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
379 unsigned char **packet_buff,
380 int *packet_buff_len, int packet_min_len);
381void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
382 struct batadv_ogm_packet *batadv_ogm_packet,
383 struct batadv_orig_node *orig_node);
384void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
385 u8 type, u8 version);
386
387void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
388 void (*optr)(struct batadv_priv *bat_priv,
389 struct batadv_orig_node *orig,
390 u8 flags,
391 void *tvlv_value,
392 u16 tvlv_value_len),
393 int (*uptr)(struct batadv_priv *bat_priv,
394 u8 *src, u8 *dst,
395 void *tvlv_value,
396 u16 tvlv_value_len),
397 u8 type, u8 version, u8 flags);
398void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
399 u8 type, u8 version);
400int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
401 bool ogm_source,
402 struct batadv_orig_node *orig_node,
403 u8 *src, u8 *dst,
404 void *tvlv_buff, u16 tvlv_buff_len);
405void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
406 u8 *dst, u8 type, u8 version,
407 void *tvlv_value, u16 tvlv_value_len);
408unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len); 374unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len);
409bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid); 375bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid);
410 376
diff --git a/net/batman-adv/multicast.c b/net/batman-adv/multicast.c
index d3222db60fd0..0e7d78f4f1b8 100644
--- a/net/batman-adv/multicast.c
+++ b/net/batman-adv/multicast.c
@@ -57,6 +57,7 @@
57#include "hash.h" 57#include "hash.h"
58#include "packet.h" 58#include "packet.h"
59#include "translation-table.h" 59#include "translation-table.h"
60#include "tvlv.h"
60 61
61/** 62/**
62 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists 63 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
diff --git a/net/batman-adv/network-coding.c b/net/batman-adv/network-coding.c
index 678f06865312..d0383dea6440 100644
--- a/net/batman-adv/network-coding.c
+++ b/net/batman-adv/network-coding.c
@@ -55,6 +55,7 @@
55#include "packet.h" 55#include "packet.h"
56#include "routing.h" 56#include "routing.h"
57#include "send.h" 57#include "send.h"
58#include "tvlv.h"
58 59
59static struct lock_class_key batadv_nc_coding_hash_lock_class_key; 60static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
60static struct lock_class_key batadv_nc_decoding_hash_lock_class_key; 61static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
diff --git a/net/batman-adv/routing.c b/net/batman-adv/routing.c
index 24fc75335b31..8cb459a57219 100644
--- a/net/batman-adv/routing.c
+++ b/net/batman-adv/routing.c
@@ -46,6 +46,7 @@
46#include "send.h" 46#include "send.h"
47#include "soft-interface.h" 47#include "soft-interface.h"
48#include "translation-table.h" 48#include "translation-table.h"
49#include "tvlv.h"
49 50
50static int batadv_route_unicast_packet(struct sk_buff *skb, 51static int batadv_route_unicast_packet(struct sk_buff *skb,
51 struct batadv_hard_iface *recv_if); 52 struct batadv_hard_iface *recv_if);
diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
index 87bb2030186d..6c8d624e4581 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -51,6 +51,7 @@
51#include "originator.h" 51#include "originator.h"
52#include "packet.h" 52#include "packet.h"
53#include "soft-interface.h" 53#include "soft-interface.h"
54#include "tvlv.h"
54 55
55/* hash class keys */ 56/* hash class keys */
56static struct lock_class_key batadv_tt_local_hash_lock_class_key; 57static struct lock_class_key batadv_tt_local_hash_lock_class_key;
diff --git a/net/batman-adv/tvlv.c b/net/batman-adv/tvlv.c
new file mode 100644
index 000000000000..2fd542e0d6a8
--- /dev/null
+++ b/net/batman-adv/tvlv.c
@@ -0,0 +1,630 @@
1/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "main.h"
19
20#include <linux/byteorder/generic.h>
21#include <linux/etherdevice.h>
22#include <linux/fs.h>
23#include <linux/if_ether.h>
24#include <linux/kernel.h>
25#include <linux/kref.h>
26#include <linux/list.h>
27#include <linux/lockdep.h>
28#include <linux/netdevice.h>
29#include <linux/pkt_sched.h>
30#include <linux/rculist.h>
31#include <linux/rcupdate.h>
32#include <linux/skbuff.h>
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/stddef.h>
36#include <linux/string.h>
37#include <linux/types.h>
38
39#include "originator.h"
40#include "packet.h"
41#include "send.h"
42#include "tvlv.h"
43
44/**
45 * batadv_tvlv_handler_release - release tvlv handler from lists and queue for
46 * free after rcu grace period
47 * @ref: kref pointer of the tvlv
48 */
49static void batadv_tvlv_handler_release(struct kref *ref)
50{
51 struct batadv_tvlv_handler *tvlv_handler;
52
53 tvlv_handler = container_of(ref, struct batadv_tvlv_handler, refcount);
54 kfree_rcu(tvlv_handler, rcu);
55}
56
57/**
58 * batadv_tvlv_handler_put - decrement the tvlv container refcounter and
59 * possibly release it
60 * @tvlv_handler: the tvlv handler to free
61 */
62static void batadv_tvlv_handler_put(struct batadv_tvlv_handler *tvlv_handler)
63{
64 kref_put(&tvlv_handler->refcount, batadv_tvlv_handler_release);
65}
66
67/**
68 * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
69 * based on the provided type and version (both need to match)
70 * @bat_priv: the bat priv with all the soft interface information
71 * @type: tvlv handler type to look for
72 * @version: tvlv handler version to look for
73 *
74 * Return: tvlv handler if found or NULL otherwise.
75 */
76static struct batadv_tvlv_handler *
77batadv_tvlv_handler_get(struct batadv_priv *bat_priv, u8 type, u8 version)
78{
79 struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
80
81 rcu_read_lock();
82 hlist_for_each_entry_rcu(tvlv_handler_tmp,
83 &bat_priv->tvlv.handler_list, list) {
84 if (tvlv_handler_tmp->type != type)
85 continue;
86
87 if (tvlv_handler_tmp->version != version)
88 continue;
89
90 if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
91 continue;
92
93 tvlv_handler = tvlv_handler_tmp;
94 break;
95 }
96 rcu_read_unlock();
97
98 return tvlv_handler;
99}
100
101/**
102 * batadv_tvlv_container_release - release tvlv from lists and free
103 * @ref: kref pointer of the tvlv
104 */
105static void batadv_tvlv_container_release(struct kref *ref)
106{
107 struct batadv_tvlv_container *tvlv;
108
109 tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
110 kfree(tvlv);
111}
112
113/**
114 * batadv_tvlv_container_put - decrement the tvlv container refcounter and
115 * possibly release it
116 * @tvlv: the tvlv container to free
117 */
118static void batadv_tvlv_container_put(struct batadv_tvlv_container *tvlv)
119{
120 kref_put(&tvlv->refcount, batadv_tvlv_container_release);
121}
122
123/**
124 * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
125 * list based on the provided type and version (both need to match)
126 * @bat_priv: the bat priv with all the soft interface information
127 * @type: tvlv container type to look for
128 * @version: tvlv container version to look for
129 *
130 * Has to be called with the appropriate locks being acquired
131 * (tvlv.container_list_lock).
132 *
133 * Return: tvlv container if found or NULL otherwise.
134 */
135static struct batadv_tvlv_container *
136batadv_tvlv_container_get(struct batadv_priv *bat_priv, u8 type, u8 version)
137{
138 struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
139
140 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
141
142 hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
143 if (tvlv_tmp->tvlv_hdr.type != type)
144 continue;
145
146 if (tvlv_tmp->tvlv_hdr.version != version)
147 continue;
148
149 kref_get(&tvlv_tmp->refcount);
150 tvlv = tvlv_tmp;
151 break;
152 }
153
154 return tvlv;
155}
156
157/**
158 * batadv_tvlv_container_list_size - calculate the size of the tvlv container
159 * list entries
160 * @bat_priv: the bat priv with all the soft interface information
161 *
162 * Has to be called with the appropriate locks being acquired
163 * (tvlv.container_list_lock).
164 *
165 * Return: size of all currently registered tvlv containers in bytes.
166 */
167static u16 batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
168{
169 struct batadv_tvlv_container *tvlv;
170 u16 tvlv_len = 0;
171
172 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
173
174 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
175 tvlv_len += sizeof(struct batadv_tvlv_hdr);
176 tvlv_len += ntohs(tvlv->tvlv_hdr.len);
177 }
178
179 return tvlv_len;
180}
181
182/**
183 * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
184 * list
185 * @bat_priv: the bat priv with all the soft interface information
186 * @tvlv: the to be removed tvlv container
187 *
188 * Has to be called with the appropriate locks being acquired
189 * (tvlv.container_list_lock).
190 */
191static void batadv_tvlv_container_remove(struct batadv_priv *bat_priv,
192 struct batadv_tvlv_container *tvlv)
193{
194 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
195
196 if (!tvlv)
197 return;
198
199 hlist_del(&tvlv->list);
200
201 /* first call to decrement the counter, second call to free */
202 batadv_tvlv_container_put(tvlv);
203 batadv_tvlv_container_put(tvlv);
204}
205
206/**
207 * batadv_tvlv_container_unregister - unregister tvlv container based on the
208 * provided type and version (both need to match)
209 * @bat_priv: the bat priv with all the soft interface information
210 * @type: tvlv container type to unregister
211 * @version: tvlv container type to unregister
212 */
213void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
214 u8 type, u8 version)
215{
216 struct batadv_tvlv_container *tvlv;
217
218 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
219 tvlv = batadv_tvlv_container_get(bat_priv, type, version);
220 batadv_tvlv_container_remove(bat_priv, tvlv);
221 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
222}
223
224/**
225 * batadv_tvlv_container_register - register tvlv type, version and content
226 * to be propagated with each (primary interface) OGM
227 * @bat_priv: the bat priv with all the soft interface information
228 * @type: tvlv container type
229 * @version: tvlv container version
230 * @tvlv_value: tvlv container content
231 * @tvlv_value_len: tvlv container content length
232 *
233 * If a container of the same type and version was already registered the new
234 * content is going to replace the old one.
235 */
236void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
237 u8 type, u8 version,
238 void *tvlv_value, u16 tvlv_value_len)
239{
240 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
241
242 if (!tvlv_value)
243 tvlv_value_len = 0;
244
245 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
246 if (!tvlv_new)
247 return;
248
249 tvlv_new->tvlv_hdr.version = version;
250 tvlv_new->tvlv_hdr.type = type;
251 tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
252
253 memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
254 INIT_HLIST_NODE(&tvlv_new->list);
255 kref_init(&tvlv_new->refcount);
256
257 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
258 tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
259 batadv_tvlv_container_remove(bat_priv, tvlv_old);
260 hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
261 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
262}
263
264/**
265 * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accommodate
266 * requested packet size
267 * @packet_buff: packet buffer
268 * @packet_buff_len: packet buffer size
269 * @min_packet_len: requested packet minimum size
270 * @additional_packet_len: requested additional packet size on top of minimum
271 * size
272 *
273 * Return: true of the packet buffer could be changed to the requested size,
274 * false otherwise.
275 */
276static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
277 int *packet_buff_len,
278 int min_packet_len,
279 int additional_packet_len)
280{
281 unsigned char *new_buff;
282
283 new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
284
285 /* keep old buffer if kmalloc should fail */
286 if (!new_buff)
287 return false;
288
289 memcpy(new_buff, *packet_buff, min_packet_len);
290 kfree(*packet_buff);
291 *packet_buff = new_buff;
292 *packet_buff_len = min_packet_len + additional_packet_len;
293
294 return true;
295}
296
297/**
298 * batadv_tvlv_container_ogm_append - append tvlv container content to given
299 * OGM packet buffer
300 * @bat_priv: the bat priv with all the soft interface information
301 * @packet_buff: ogm packet buffer
302 * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
303 * content
304 * @packet_min_len: ogm header size to be preserved for the OGM itself
305 *
306 * The ogm packet might be enlarged or shrunk depending on the current size
307 * and the size of the to-be-appended tvlv containers.
308 *
309 * Return: size of all appended tvlv containers in bytes.
310 */
311u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
312 unsigned char **packet_buff,
313 int *packet_buff_len, int packet_min_len)
314{
315 struct batadv_tvlv_container *tvlv;
316 struct batadv_tvlv_hdr *tvlv_hdr;
317 u16 tvlv_value_len;
318 void *tvlv_value;
319 bool ret;
320
321 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
322 tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
323
324 ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
325 packet_min_len, tvlv_value_len);
326
327 if (!ret)
328 goto end;
329
330 if (!tvlv_value_len)
331 goto end;
332
333 tvlv_value = (*packet_buff) + packet_min_len;
334
335 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
336 tvlv_hdr = tvlv_value;
337 tvlv_hdr->type = tvlv->tvlv_hdr.type;
338 tvlv_hdr->version = tvlv->tvlv_hdr.version;
339 tvlv_hdr->len = tvlv->tvlv_hdr.len;
340 tvlv_value = tvlv_hdr + 1;
341 memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
342 tvlv_value = (u8 *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
343 }
344
345end:
346 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
347 return tvlv_value_len;
348}
349
350/**
351 * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
352 * appropriate handlers
353 * @bat_priv: the bat priv with all the soft interface information
354 * @tvlv_handler: tvlv callback function handling the tvlv content
355 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
356 * @orig_node: orig node emitting the ogm packet
357 * @src: source mac address of the unicast packet
358 * @dst: destination mac address of the unicast packet
359 * @tvlv_value: tvlv content
360 * @tvlv_value_len: tvlv content length
361 *
362 * Return: success if handler was not found or the return value of the handler
363 * callback.
364 */
365static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
366 struct batadv_tvlv_handler *tvlv_handler,
367 bool ogm_source,
368 struct batadv_orig_node *orig_node,
369 u8 *src, u8 *dst,
370 void *tvlv_value, u16 tvlv_value_len)
371{
372 if (!tvlv_handler)
373 return NET_RX_SUCCESS;
374
375 if (ogm_source) {
376 if (!tvlv_handler->ogm_handler)
377 return NET_RX_SUCCESS;
378
379 if (!orig_node)
380 return NET_RX_SUCCESS;
381
382 tvlv_handler->ogm_handler(bat_priv, orig_node,
383 BATADV_NO_FLAGS,
384 tvlv_value, tvlv_value_len);
385 tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
386 } else {
387 if (!src)
388 return NET_RX_SUCCESS;
389
390 if (!dst)
391 return NET_RX_SUCCESS;
392
393 if (!tvlv_handler->unicast_handler)
394 return NET_RX_SUCCESS;
395
396 return tvlv_handler->unicast_handler(bat_priv, src,
397 dst, tvlv_value,
398 tvlv_value_len);
399 }
400
401 return NET_RX_SUCCESS;
402}
403
404/**
405 * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
406 * appropriate handlers
407 * @bat_priv: the bat priv with all the soft interface information
408 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
409 * @orig_node: orig node emitting the ogm packet
410 * @src: source mac address of the unicast packet
411 * @dst: destination mac address of the unicast packet
412 * @tvlv_value: tvlv content
413 * @tvlv_value_len: tvlv content length
414 *
415 * Return: success when processing an OGM or the return value of all called
416 * handler callbacks.
417 */
418int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
419 bool ogm_source,
420 struct batadv_orig_node *orig_node,
421 u8 *src, u8 *dst,
422 void *tvlv_value, u16 tvlv_value_len)
423{
424 struct batadv_tvlv_handler *tvlv_handler;
425 struct batadv_tvlv_hdr *tvlv_hdr;
426 u16 tvlv_value_cont_len;
427 u8 cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
428 int ret = NET_RX_SUCCESS;
429
430 while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
431 tvlv_hdr = tvlv_value;
432 tvlv_value_cont_len = ntohs(tvlv_hdr->len);
433 tvlv_value = tvlv_hdr + 1;
434 tvlv_value_len -= sizeof(*tvlv_hdr);
435
436 if (tvlv_value_cont_len > tvlv_value_len)
437 break;
438
439 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
440 tvlv_hdr->type,
441 tvlv_hdr->version);
442
443 ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
444 ogm_source, orig_node,
445 src, dst, tvlv_value,
446 tvlv_value_cont_len);
447 if (tvlv_handler)
448 batadv_tvlv_handler_put(tvlv_handler);
449 tvlv_value = (u8 *)tvlv_value + tvlv_value_cont_len;
450 tvlv_value_len -= tvlv_value_cont_len;
451 }
452
453 if (!ogm_source)
454 return ret;
455
456 rcu_read_lock();
457 hlist_for_each_entry_rcu(tvlv_handler,
458 &bat_priv->tvlv.handler_list, list) {
459 if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
460 !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
461 tvlv_handler->ogm_handler(bat_priv, orig_node,
462 cifnotfound, NULL, 0);
463
464 tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
465 }
466 rcu_read_unlock();
467
468 return NET_RX_SUCCESS;
469}
470
471/**
472 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
473 * handlers
474 * @bat_priv: the bat priv with all the soft interface information
475 * @batadv_ogm_packet: ogm packet containing the tvlv containers
476 * @orig_node: orig node emitting the ogm packet
477 */
478void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
479 struct batadv_ogm_packet *batadv_ogm_packet,
480 struct batadv_orig_node *orig_node)
481{
482 void *tvlv_value;
483 u16 tvlv_value_len;
484
485 if (!batadv_ogm_packet)
486 return;
487
488 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
489 if (!tvlv_value_len)
490 return;
491
492 tvlv_value = batadv_ogm_packet + 1;
493
494 batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
495 tvlv_value, tvlv_value_len);
496}
497
498/**
499 * batadv_tvlv_handler_register - register tvlv handler based on the provided
500 * type and version (both need to match) for ogm tvlv payload and/or unicast
501 * payload
502 * @bat_priv: the bat priv with all the soft interface information
503 * @optr: ogm tvlv handler callback function. This function receives the orig
504 * node, flags and the tvlv content as argument to process.
505 * @uptr: unicast tvlv handler callback function. This function receives the
506 * source & destination of the unicast packet as well as the tvlv content
507 * to process.
508 * @type: tvlv handler type to be registered
509 * @version: tvlv handler version to be registered
510 * @flags: flags to enable or disable TVLV API behavior
511 */
512void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
513 void (*optr)(struct batadv_priv *bat_priv,
514 struct batadv_orig_node *orig,
515 u8 flags,
516 void *tvlv_value,
517 u16 tvlv_value_len),
518 int (*uptr)(struct batadv_priv *bat_priv,
519 u8 *src, u8 *dst,
520 void *tvlv_value,
521 u16 tvlv_value_len),
522 u8 type, u8 version, u8 flags)
523{
524 struct batadv_tvlv_handler *tvlv_handler;
525
526 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
527 if (tvlv_handler) {
528 batadv_tvlv_handler_put(tvlv_handler);
529 return;
530 }
531
532 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
533 if (!tvlv_handler)
534 return;
535
536 tvlv_handler->ogm_handler = optr;
537 tvlv_handler->unicast_handler = uptr;
538 tvlv_handler->type = type;
539 tvlv_handler->version = version;
540 tvlv_handler->flags = flags;
541 kref_init(&tvlv_handler->refcount);
542 INIT_HLIST_NODE(&tvlv_handler->list);
543
544 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
545 hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
546 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
547}
548
549/**
550 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
551 * provided type and version (both need to match)
552 * @bat_priv: the bat priv with all the soft interface information
553 * @type: tvlv handler type to be unregistered
554 * @version: tvlv handler version to be unregistered
555 */
556void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
557 u8 type, u8 version)
558{
559 struct batadv_tvlv_handler *tvlv_handler;
560
561 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
562 if (!tvlv_handler)
563 return;
564
565 batadv_tvlv_handler_put(tvlv_handler);
566 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
567 hlist_del_rcu(&tvlv_handler->list);
568 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
569 batadv_tvlv_handler_put(tvlv_handler);
570}
571
572/**
573 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
574 * specified host
575 * @bat_priv: the bat priv with all the soft interface information
576 * @src: source mac address of the unicast packet
577 * @dst: destination mac address of the unicast packet
578 * @type: tvlv type
579 * @version: tvlv version
580 * @tvlv_value: tvlv content
581 * @tvlv_value_len: tvlv content length
582 */
583void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
584 u8 *dst, u8 type, u8 version,
585 void *tvlv_value, u16 tvlv_value_len)
586{
587 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
588 struct batadv_tvlv_hdr *tvlv_hdr;
589 struct batadv_orig_node *orig_node;
590 struct sk_buff *skb;
591 unsigned char *tvlv_buff;
592 unsigned int tvlv_len;
593 ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
594
595 orig_node = batadv_orig_hash_find(bat_priv, dst);
596 if (!orig_node)
597 return;
598
599 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
600
601 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
602 if (!skb)
603 goto out;
604
605 skb->priority = TC_PRIO_CONTROL;
606 skb_reserve(skb, ETH_HLEN);
607 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
608 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
609 unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
610 unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
611 unicast_tvlv_packet->ttl = BATADV_TTL;
612 unicast_tvlv_packet->reserved = 0;
613 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
614 unicast_tvlv_packet->align = 0;
615 ether_addr_copy(unicast_tvlv_packet->src, src);
616 ether_addr_copy(unicast_tvlv_packet->dst, dst);
617
618 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
619 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
620 tvlv_hdr->version = version;
621 tvlv_hdr->type = type;
622 tvlv_hdr->len = htons(tvlv_value_len);
623 tvlv_buff += sizeof(*tvlv_hdr);
624 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
625
626 if (batadv_send_skb_to_orig(skb, orig_node, NULL) == NET_XMIT_DROP)
627 kfree_skb(skb);
628out:
629 batadv_orig_node_put(orig_node);
630}
diff --git a/net/batman-adv/tvlv.h b/net/batman-adv/tvlv.h
new file mode 100644
index 000000000000..e4369b547b43
--- /dev/null
+++ b/net/batman-adv/tvlv.h
@@ -0,0 +1,61 @@
1/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef _NET_BATMAN_ADV_TVLV_H_
19#define _NET_BATMAN_ADV_TVLV_H_
20
21#include "main.h"
22
23#include <linux/types.h>
24
25struct batadv_ogm_packet;
26
27void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
28 u8 type, u8 version,
29 void *tvlv_value, u16 tvlv_value_len);
30u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
31 unsigned char **packet_buff,
32 int *packet_buff_len, int packet_min_len);
33void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
34 struct batadv_ogm_packet *batadv_ogm_packet,
35 struct batadv_orig_node *orig_node);
36void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
37 u8 type, u8 version);
38
39void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
40 void (*optr)(struct batadv_priv *bat_priv,
41 struct batadv_orig_node *orig,
42 u8 flags,
43 void *tvlv_value,
44 u16 tvlv_value_len),
45 int (*uptr)(struct batadv_priv *bat_priv,
46 u8 *src, u8 *dst,
47 void *tvlv_value,
48 u16 tvlv_value_len),
49 u8 type, u8 version, u8 flags);
50void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
51 u8 type, u8 version);
52int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
53 bool ogm_source,
54 struct batadv_orig_node *orig_node,
55 u8 *src, u8 *dst,
56 void *tvlv_buff, u16 tvlv_buff_len);
57void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
58 u8 *dst, u8 type, u8 version,
59 void *tvlv_value, u16 tvlv_value_len);
60
61#endif /* _NET_BATMAN_ADV_TVLV_H_ */