2 * Connection oriented routing
3 * Copyright (C) 2007-2009 Michael Blizek
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * Splited packet data format:
25 * announce proto version [4]
26 * is 0, may be increased if format changes
28 * starts with 0, increments every time the data field changes
30 * total data size of all merged packets
32 * used to determine the order when merging the split packet
35 * commulative checksum [8] (not yet)
36 * chunk 1 contains the checksum of the data in chunk 1
37 * chunk 2 contains the checksum of the data in chunk 1+2
40 * Data format of the announce packet "data" field:
41 * min_announce_proto_version [4]
42 * max_announce_proto_version [4]
43 * min_cor_proto_version [4]
44 * max_cor_proto_version [4]
45 * versions which are understood
49 * commanddata [commandlength]
54 #define NEIGHCMD_ADDADDR 1
59 * addrtype [addrtypelen]
66 * "I hear you" data format:
71 DEFINE_MUTEX(neighbor_operation_lock
);
73 char *addrtype
= "id";
79 struct kmem_cache
*nb_slab
;
81 LIST_HEAD(announce_out_list
);
83 struct notifier_block netdev_notify
;
86 #define ADDRTYPE_UNKNOWN 0
89 static int get_addrtype(__u32 addrtypelen
, char *addrtype
)
91 if (addrtypelen
== 2 &&
92 (addrtype
[0] == 'i' || addrtype
[0] == 'I') &&
93 (addrtype
[1] == 'd' || addrtype
[1] == 'D'))
96 return ADDRTYPE_UNKNOWN
;
99 static void neighbor_free(struct ref_counter
*ref
)
101 struct neighbor
*nb
= container_of(ref
, struct neighbor
, refs
);
102 BUG_ON(nb
->nb_list
.next
!= 0 || nb
->nb_list
.prev
!= 0);
109 kmem_cache_free(nb_slab
, nb
);
112 static struct ref_counter_def neighbor_ref
= {
113 .free
= neighbor_free
116 static struct neighbor
*alloc_neighbor(gfp_t allocflags
)
118 struct neighbor
*nb
= kmem_cache_alloc(nb_slab
, allocflags
);
123 memset(nb
, 0, sizeof(struct neighbor
));
125 ref_counter_init(&(nb
->refs
), &neighbor_ref
);
126 mutex_init(&(nb
->cmsg_lock
));
127 /*struct control_msg_out *first_cm;
128 struct control_msg_out *last_cm;
129 unsigned long timedue;*/
131 INIT_LIST_HEAD(&(nb
->control_msgs_out
));
132 atomic_set(&(nb
->ooo_packets
), 0);
133 mutex_init(&(nb
->conn_list_lock
));
134 INIT_LIST_HEAD(&(nb
->rcv_conn_list
));
135 INIT_LIST_HEAD(&(nb
->snd_conn_list
));
136 spin_lock_init(&(nb
->retrans_lock
));
137 spin_lock_init(&(nb
->retrans_lock
));
138 skb_queue_head_init(&(nb
->retrans_list
));
143 static void add_neighbor(struct neighbor
*nb
)
145 struct list_head
*currlh
= nb_list
.next
;
147 BUG_ON((nb
->addr
== 0) != (nb
->addrlen
== 0));
149 while (currlh
!= &nb_list
) {
150 struct neighbor
*curr
= container_of(currlh
, struct neighbor
,
153 if (curr
->addrlen
== nb
->addrlen
&& memcmp(curr
->addr
, nb
->addr
,
155 goto already_present
;
157 currlh
= currlh
->next
;
161 list_add_tail(&(nb
->nb_list
), &nb_list
);
162 schedule_controlmsg_timerfunc(nb
);
163 setup_timer(&(nb
->retrans_timer
), retransmit_timerfunc
,
168 kmem_cache_free(nb_slab
, nb
);
172 struct neighbor
*find_neigh(__u16 addrtypelen
, __u8
*addrtype
,
173 __u16 addrlen
, __u8
*addr
)
175 struct list_head
*currlh
;
176 struct neighbor
*ret
= 0;
178 if (get_addrtype(addrtypelen
, addrtype
) != ADDRTYPE_ID
)
181 mutex_lock(&(neighbor_operation_lock
));
183 currlh
= nb_list
.next
;
185 while (currlh
!= &nb_list
) {
186 struct neighbor
*curr
= container_of(currlh
, struct neighbor
,
189 if (curr
->addrlen
== addrlen
&& memcmp(curr
->addr
, addr
,
196 currlh
= currlh
->next
;
200 mutex_unlock(&(neighbor_operation_lock
));
205 __u32
generate_neigh_list(char *buf
, __u32 buflen
, __u32 limit
, __u32 offset
)
207 struct list_head
*currlh
;
209 char *p_totalneighs
= buf
;
210 char *p_response_rows
= buf
+ 4;
215 __u32 buf_offset
= 8;
220 mutex_lock(&(neighbor_operation_lock
));
222 currlh
= nb_list
.next
;
224 while (currlh
!= &nb_list
) {
225 struct neighbor
*curr
= container_of(currlh
, struct neighbor
,
231 if (unlikely(buflen
- buf_offset
- 6 - 2 - curr
->addrlen
< 0))
234 put_u16(buf
+ buf_offset
, 1, 1);/* numaddr */
236 put_u16(buf
+ buf_offset
, 2, 1);/* addrtypelen */
238 put_u16(buf
+ buf_offset
, curr
->addrlen
, 1);/* addren */
240 buf
[buf_offset
] = 'i'; /* addrtype */
242 buf
[buf_offset
] = 'd';
244 memcpy(buf
+ buf_offset
, curr
->addr
, curr
->addrlen
); /* addr */
245 buf_offset
+= curr
->addrlen
;
247 BUG_ON(buf_offset
> buflen
);
253 currlh
= currlh
->next
;
256 mutex_unlock(&(neighbor_operation_lock
));
258 put_u32(p_totalneighs
, total
, 1);
259 put_u32(p_response_rows
, cnt
, 1);
264 static __u32
pull_u32(struct sk_buff
*skb
, int convbo
)
266 char *ptr
= cor_pull_skb(skb
, 4);
272 ((char *)&ret
)[0] = ptr
[0];
273 ((char *)&ret
)[1] = ptr
[1];
274 ((char *)&ret
)[2] = ptr
[2];
275 ((char *)&ret
)[3] = ptr
[3];
278 return be32_to_cpu(ret
);
282 static int apply_announce_addaddr(struct neighbor
*nb
, __u32 cmd
, __u32 len
,
290 BUG_ON((nb
->addr
== 0) != (nb
->addrlen
== 0));
298 addrtypelen
= be16_to_cpu(*((__u16
*) cmddata
));
305 addrlen
= be16_to_cpu(*((__u16
*) cmddata
));
310 cmddata
+= addrtypelen
;
320 if (get_addrtype(addrtypelen
, addrtype
) != ADDRTYPE_ID
)
323 nb
->addr
= kmalloc(addrlen
, GFP_KERNEL
);
327 memcpy(nb
->addr
, addr
, addrlen
);
328 nb
->addrlen
= addrlen
;
333 static void apply_announce_cmd(struct neighbor
*nb
, __u32 cmd
, __u32 len
,
336 if (cmd
== NEIGHCMD_ADDADDR
) {
337 apply_announce_addaddr(nb
, cmd
, len
, cmddata
);
339 /* ignore unknown cmds */
343 static void apply_announce_cmds(char *msg
, __u32 len
, struct net_device
*dev
,
346 struct neighbor
*nb
= alloc_neighbor(GFP_KERNEL
);
355 cmd
= be32_to_cpu(*((__u32
*) msg
));
358 cmdlen
= be32_to_cpu(*((__u32
*) msg
));
362 BUG_ON(cmdlen
> len
);
364 apply_announce_cmd(nb
, cmd
, cmdlen
, msg
);
378 static int check_announce_cmds(char *msg
, __u32 len
)
384 cmd
= be32_to_cpu(*((__u32
*) msg
));
387 cmdlen
= be32_to_cpu(*((__u32
*) msg
));
391 /* malformated packet */
405 static void parse_announce(char *msg
, __u32 len
, struct net_device
*dev
,
408 __u32 min_announce_version
;
409 __u32 max_announce_version
;
410 __u32 min_cor_version
;
411 __u32 max_cor_version
;
416 min_announce_version
= be32_to_cpu(*((__u32
*) msg
));
419 max_announce_version
= be32_to_cpu(*((__u32
*) msg
));
422 min_cor_version
= be32_to_cpu(*((__u32
*) msg
));
425 max_cor_version
= be32_to_cpu(*((__u32
*) msg
));
429 if (min_announce_version
!= 0)
431 if (min_cor_version
!= 0)
433 if (check_announce_cmds(msg
, len
)) {
436 apply_announce_cmds(msg
, len
, dev
, source_hw
);
440 /* lh has to be first */
442 struct sk_buff_head skbs
; /* sorted by offset */
443 struct net_device
*dev
;
444 char source_hw
[MAX_ADDR_LEN
];
445 __u32 announce_proto_version
;
446 __u32 packet_version
;
449 __u64 last_received_packet
;
452 LIST_HEAD(announce_list
);
454 struct kmem_cache
*announce_in_slab
;
456 static void merge_announce(struct announce_in
*ann
)
458 char *msg
= kmalloc(ann
->total_size
, GFP_KERNEL
);
462 /* try again when next packet arrives */
466 while (copy
!= ann
->total_size
) {
470 if (skb_queue_empty(&(ann
->skbs
))) {
471 printk(KERN_ERR
"net/cor/neighbor.c: sk_head ran "
472 "empty while merging packets\n");
476 skb
= skb_dequeue(&(ann
->skbs
));
480 if (currcpy
+ copy
> ann
->total_size
)
483 #warning todo overlapping skbs
484 memcpy(msg
+ copy
, skb
->data
, currcpy
);
489 parse_announce(msg
, ann
->total_size
, ann
->dev
, ann
->source_hw
);
496 list_del(&(ann
->lh
));
497 kmem_cache_free(announce_in_slab
, ann
);
500 static int _rcv_announce(struct sk_buff
*skb
, struct announce_in
*ann
)
502 struct skb_procstate
*ps
= skb_pstate(skb
);
504 __u32 offset
= ps
->funcstate
.announce
.offset
;
505 __u32 len
= skb
->len
;
507 __u32 curroffset
= 0;
508 __u32 prevoffset
= 0;
511 struct sk_buff
*curr
= ann
->skbs
.next
;
513 if (len
+ offset
> ann
->total_size
) {
520 * Try to find the right place to insert in the sorted list. This
521 * means to process the list until we find a skb which has a greater
522 * offset, so we can insert before it to keep the sort order. However,
523 * this is complicated by the fact that the new skb must not be inserted
524 * between 2 skbs if there is no data missing in between. So the loop
525 * runs has to keep running until there is either a gap to insert or
526 * we see that this data has already been received.
528 while ((void *) curr
!= (void *) &(ann
->skbs
)) {
529 struct skb_procstate
*currps
= skb_pstate(skb
);
531 curroffset
= currps
->funcstate
.announce
.offset
;
533 if (curroffset
> offset
&& (prevoffset
+ prevlen
) < curroffset
)
536 prevoffset
= curroffset
;
540 if ((offset
+len
) <= (prevoffset
+prevlen
)) {
541 /* we already have this data */
548 * Calculate how much data was really received, by substracting
549 * the bytes we already have.
551 if (unlikely(prevoffset
+ prevlen
> offset
)) {
552 len
-= (prevoffset
+ prevlen
) - offset
;
553 offset
= prevoffset
+ prevlen
;
556 if (unlikely((void *) curr
!= (void *) &(ann
->skbs
) &&
557 (offset
+ len
) > curroffset
))
558 len
= curroffset
- offset
;
560 ann
->received_size
+= len
;
561 BUG_ON(ann
->received_size
> ann
->total_size
);
562 __skb_queue_before(&(ann
->skbs
), curr
, skb
);
563 ann
->last_received_packet
= get_jiffies_64();
565 if (ann
->received_size
== ann
->total_size
)
567 else if (ann
->skbs
.qlen
>= 16)
573 void rcv_announce(struct sk_buff
*skb
)
575 struct skb_procstate
*ps
= skb_pstate(skb
);
576 struct announce_in
*curr
= 0;
577 struct announce_in
*leastactive
= 0;
580 __u32 announce_proto_version
= pull_u32(skb
, 1);
581 __u32 packet_version
= pull_u32(skb
, 1);
582 __u32 total_size
= pull_u32(skb
, 1);
584 char source_hw
[MAX_ADDR_LEN
];
585 memset(source_hw
, 0, MAX_ADDR_LEN
);
586 if (skb
->dev
->header_ops
!= 0 &&
587 skb
->dev
->header_ops
->parse
!= 0)
588 skb
->dev
->header_ops
->parse(skb
, source_hw
);
590 ps
->funcstate
.announce
.offset
= pull_u32(skb
, 1);
592 if (total_size
> 8192)
595 mutex_lock(&(neighbor_operation_lock
));
597 if (announce_proto_version
!= 0)
600 curr
= (struct announce_in
*) announce_list
.next
;
602 while (((struct list_head
*) curr
) != &(announce_list
)) {
604 if (curr
->dev
== skb
->dev
&&
605 memcmp(curr
->source_hw
, source_hw
, MAX_ADDR_LEN
) == 0 &&
606 curr
->announce_proto_version
== announce_proto_version
&&
607 curr
->packet_version
== packet_version
&&
608 curr
->total_size
== total_size
)
611 if (leastactive
== 0 || curr
->last_received_packet
<
612 leastactive
->last_received_packet
)
615 curr
= (struct announce_in
*) curr
->lh
.next
;
618 if (list_size
>= 128) {
619 BUG_ON(leastactive
== 0);
622 curr
->last_received_packet
= get_jiffies_64();
624 while (!skb_queue_empty(&(curr
->skbs
))) {
625 struct sk_buff
*skb2
= skb_dequeue(&(curr
->skbs
));
631 curr
= kmem_cache_alloc(announce_in_slab
,
636 skb_queue_head_init(&(curr
->skbs
));
637 list_add_tail((struct list_head
*) curr
, &announce_list
);
640 curr
->packet_version
= packet_version
;
641 curr
->total_size
= total_size
;
642 curr
->received_size
= 0;
643 curr
->announce_proto_version
= announce_proto_version
;
644 curr
->dev
= skb
->dev
;
646 memcpy(curr
->source_hw
, source_hw
, MAX_ADDR_LEN
);
649 if (_rcv_announce(skb
, curr
)) {
650 list_del((struct list_head
*) curr
);
652 kmem_cache_free(announce_in_slab
, curr
);
660 mutex_unlock(&(neighbor_operation_lock
));
664 struct ref_counter refs
;
666 __u32 packet_version
;
668 __u32 announce_msg_len
;
671 struct announce
*last_announce
;
673 struct announce_data
{
674 struct delayed_work announce_work
;
676 struct net_device
*dev
;
678 struct announce
*ann
;
682 __u32 curr_announce_msg_offset
;
683 __u64 scheduled_announce_timer
;
686 static void _splitsend_announce(struct announce_data
*ann
)
689 __u32 packet_size
= 256;
690 __u32 remainingdata
= ann
->ann
->announce_msg_len
-
691 ann
->curr_announce_msg_offset
;
692 __u32 headroom
= LL_ALLOCATED_SPACE(ann
->dev
);
693 __u32 overhead
= 17 + headroom
;
697 if (remainingdata
< packet_size
)
698 packet_size
= remainingdata
;
700 skb
= alloc_skb(packet_size
+ overhead
, GFP_KERNEL
);
701 if (unlikely(0 == skb
))
704 skb
->protocol
= htons(ETH_P_COR
);
706 skb_reserve(skb
, overhead
);
708 if(unlikely(dev_hard_header(skb
, ann
->dev
, ETH_P_COR
,
709 ann
->dev
->broadcast
, ann
->dev
->dev_addr
, skb
->len
) < 0))
712 skb_reset_network_header(skb
);
714 header
= skb_put(skb
, 17);
715 if (unlikely(header
== 0))
718 header
[0] = PACKET_TYPE_ANNOUNCE
;
720 put_u32(header
+ 1, 0, 1); /* announce proto version */
721 put_u32(header
+ 5, ann
->ann
->packet_version
, 1); /* packet version */
722 put_u32(header
+ 9, ann
->ann
->announce_msg_len
, 1); /* total size */
723 put_u32(header
+ 13, ann
->curr_announce_msg_offset
, 1); /* offset */
725 ptr
= skb_put(skb
, packet_size
);
726 if (unlikely(ptr
== 0))
729 memcpy(ptr
, ann
->ann
->announce_msg
+ ann
->curr_announce_msg_offset
, packet_size
);
732 ann
->curr_announce_msg_offset
+= packet_size
;
734 if (ann
->curr_announce_msg_offset
== ann
->ann
->announce_msg_len
)
735 ann
->curr_announce_msg_offset
= 0;
744 static void splitsend_announce(struct work_struct
*work
)
746 struct announce_data
*ann
= container_of(to_delayed_work(work
),
747 struct announce_data
, announce_work
);
750 mutex_lock(&(neighbor_operation_lock
));
757 if (ann
->ann
== 0 && last_announce
== 0)
760 if (ann
->curr_announce_msg_offset
== 0 && ann
->ann
!= last_announce
) {
762 ref_counter_decr(&(ann
->ann
->refs
));
763 ann
->ann
= last_announce
;
764 ref_counter_incr(&(ann
->ann
->refs
));
767 _splitsend_announce(ann
);
769 mutex_unlock(&(neighbor_operation_lock
));
772 int target_delay_ms
= 500;
773 int target_delay_jiffies
= msecs_to_jiffies(target_delay_ms
);
774 __u64 jiffies
= get_jiffies_64();
777 ann
->scheduled_announce_timer
+= target_delay_jiffies
;
779 delay
= ann
->scheduled_announce_timer
- jiffies
;
783 INIT_DELAYED_WORK(&(ann
->announce_work
), splitsend_announce
);
784 schedule_delayed_work(&(ann
->announce_work
), delay
);
788 static void announce_free(struct ref_counter
*ref
)
790 struct announce
*ann
= container_of(ref
, struct announce
, refs
);
791 kfree(&(ann
->announce_msg
));
795 static struct ref_counter_def announce_ref
= {
796 .free
= announce_free
800 void blacklist_neighbor(struct neighbor
*nb
)
805 static struct announce_data
*get_announce_by_netdev(struct net_device
*dev
)
807 struct list_head
*lh
= announce_out_list
.next
;
809 while (lh
!= &announce_out_list
) {
810 struct announce_data
*curr
= (struct announce_data
*)(
812 offsetof(struct announce_data
, lh
));
814 if (curr
->dev
== dev
)
821 static void announce_sent_adddev(struct net_device
*dev
)
823 struct announce_data
*ann
;
825 ann
= kmalloc(sizeof(struct announce_data
), GFP_KERNEL
);
828 printk(KERN_ERR
"cor cannot allocate memory for sending "
833 memset(ann
, 0, sizeof(struct announce_data
));
838 mutex_lock(&(neighbor_operation_lock
));
839 list_add_tail(&(ann
->lh
), &announce_out_list
);
840 mutex_unlock(&(neighbor_operation_lock
));
842 ann
->scheduled_announce_timer
= get_jiffies_64();
843 INIT_DELAYED_WORK(&(ann
->announce_work
), splitsend_announce
);
844 schedule_delayed_work(&(ann
->announce_work
), 1);
847 static void announce_sent_rmdev(struct net_device
*dev
)
849 struct announce_data
*ann
;
851 mutex_lock(&(neighbor_operation_lock
));
853 ann
= get_announce_by_netdev(dev
);
862 mutex_unlock(&(neighbor_operation_lock
));
865 int netdev_notify_func(struct notifier_block
*not, unsigned long event
,
868 struct net_device
*dev
= (struct net_device
*) ptr
;
872 announce_sent_adddev(dev
);
875 announce_sent_rmdev(dev
);
879 case NETDEV_REGISTER
:
880 case NETDEV_UNREGISTER
:
881 case NETDEV_CHANGEMTU
:
882 case NETDEV_CHANGEADDR
:
883 case NETDEV_GOING_DOWN
:
884 case NETDEV_CHANGENAME
:
885 case NETDEV_FEAT_CHANGE
:
886 case NETDEV_BONDING_FAILOVER
:
895 static int set_announce(char *msg
, __u32 len
)
897 struct announce
*ann
= kmalloc(sizeof(struct announce
), GFP_KERNEL
);
904 memset(ann
, 0, sizeof(struct announce
));
906 ann
->announce_msg
= msg
;
907 ann
->announce_msg_len
= len
;
909 ref_counter_init(&(ann
->refs
), &announce_ref
);
911 mutex_lock(&(neighbor_operation_lock
));
913 if (last_announce
!= 0) {
914 ann
->packet_version
= last_announce
->packet_version
+ 1;
915 ref_counter_decr(&(last_announce
->refs
));
920 mutex_unlock(&(neighbor_operation_lock
));
925 static int generate_announce(void)
927 __u32 addrtypelen
= strlen(addrtype
);
930 __u32 cmd_hdr_len
= 8;
931 __u32 cmd_len
= 2 + 2 + addrtypelen
+ addrlen
;
933 __u32 len
= hdr_len
+ cmd_hdr_len
+ cmd_len
;
936 char *msg
= kmalloc(len
, GFP_KERNEL
);
940 put_u32(msg
+ offset
, 0, 1); /* min_announce_proto_version */
942 put_u32(msg
+ offset
, 0, 1); /* max_announce_proto_version */
944 put_u32(msg
+ offset
, 0, 1); /* min_cor_proto_version */
946 put_u32(msg
+ offset
, 0, 1); /* max_cor_proto_version */
950 put_u32(msg
+ offset
, NEIGHCMD_ADDADDR
, 1); /* command */
952 put_u32(msg
+ offset
, cmd_len
, 1); /* command length */
955 /* addrtypelen, addrlen */
956 put_u16(msg
+ offset
, addrtypelen
, 1);
958 put_u16(msg
+ offset
, addrlen
, 1);
962 memcpy(msg
+ offset
, addrtype
, addrtypelen
);
963 offset
+= addrtypelen
;
964 memcpy(msg
+ offset
, addr
, addrlen
);
967 BUG_ON(offset
!= len
);
969 return set_announce(msg
, len
);
972 int __init
cor_neighbor_init(void)
976 addr
= kmalloc(addrlen
, GFP_KERNEL
);
980 get_random_bytes(addr
, addrlen
);
982 nb_slab
= kmem_cache_create("cor_neighbor", sizeof(struct neighbor
), 8,
984 announce_in_slab
= kmem_cache_create("cor_announce_in",
985 sizeof(struct announce_in
), 8, 0, 0);
987 if (generate_announce())
990 memset(&netdev_notify
, 0, sizeof(netdev_notify
));
991 netdev_notify
.notifier_call
= netdev_notify_func
;
992 register_netdevice_notifier(&netdev_notify
);
1003 MODULE_LICENSE("GPL");