free_control_msg, reset/free conn
[cor_2_6_31.git] / net / cor / kpacket_parse.c
blob44a5dba013617a287f65f62b8af2a5af0f745863
1 /*
2 * Connection oriented routing
3 * Copyright (C) 2007-2008 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
18 * 02110-1301, USA.
21 #include <asm/byteorder.h>
23 #include "cor.h"
25 /* not used, avoid compiler warning
26 * static __u64 pull_u64(struct sk_buff *skb, int convbo)
28 char *ptr = cor_pull_skb(skb, 8);
30 __u64 ret = 0;
32 BUG_ON(0 == ptr);
34 ((char *)&ret)[0] = ptr[0];
35 ((char *)&ret)[1] = ptr[1];
36 ((char *)&ret)[2] = ptr[2];
37 ((char *)&ret)[3] = ptr[3];
38 ((char *)&ret)[4] = ptr[4];
39 ((char *)&ret)[5] = ptr[5];
40 ((char *)&ret)[6] = ptr[6];
41 ((char *)&ret)[7] = ptr[7];
43 if (convbo)
44 return be64_to_cpu(ret);
45 return ret;
46 } */
48 static __u32 pull_u32(struct sk_buff *skb, int convbo)
50 char *ptr = cor_pull_skb(skb, 4);
52 __u32 ret = 0;
54 BUG_ON(0 == ptr);
56 ((char *)&ret)[0] = ptr[0];
57 ((char *)&ret)[1] = ptr[1];
58 ((char *)&ret)[2] = ptr[2];
59 ((char *)&ret)[3] = ptr[3];
61 if (convbo)
62 return be32_to_cpu(ret);
63 return ret;
66 /* not used, avoid compiler warning
67 static __u16 pull_u16(struct sk_buff *skb, int convbo)
69 char *ptr = cor_pull_skb(skb, 2);
71 __u16 ret = 0;
73 BUG_ON(0 == ptr);
75 ((char *)&ret)[0] = ptr[0];
76 ((char *)&ret)[1] = ptr[1];
78 if (convbo)
79 return be16_to_cpu(ret);
80 return ret;
81 } */
83 static __u8 pull_u8(struct sk_buff *skb)
85 char *ptr = cor_pull_skb(skb, 1);
86 BUG_ON(0 == ptr);
87 return *ptr;
90 #warning todo
91 static void parse_speed(struct neighbor *nb, struct sk_buff *skb,
92 __u8 code, struct conn *rconn)
96 static void parse_conn_success(struct neighbor *nb, struct sk_buff *skb,
97 __u8 code, struct conn *rconn)
99 struct conn *sconn = rconn->reversedir;
101 BUG_ON(sconn == 0);
103 if (unlikely(unlikely(TARGET_OUT != sconn->targettype) ||
104 unlikely(nb != sconn->target.out.nb) ||
105 unlikely(rconn->target.out.conn_id != 0))) {
106 int conn_id = pull_u32(skb, 1);
107 struct control_msg_out *cm = alloc_control_msg();
108 if (likely(cm != 0))
109 send_reset_conn(cm, nb, conn_id);
110 return;
113 rconn->target.out.conn_id = pull_u32(skb, 1);
115 drain_ooo_queue(sconn);
118 static void parse_conn_failed(struct neighbor *nb, struct sk_buff *skb,
119 __u8 code, struct conn *rconn)
121 rconn->isreset = 1;
122 reset_conn(rconn);
125 static void parse_reset(struct neighbor *nb, struct sk_buff *skb,
126 __u8 code, struct conn *rconn)
128 rconn->isreset = 1;
129 reset_conn(rconn);
132 static void conn_cmd(struct neighbor *nb, struct sk_buff *skb, __u8 code,
133 void (*parsefunc)(struct neighbor *nb, struct sk_buff *skb,
134 __u8 code, struct conn *rconn))
136 __u32 conn_id = pull_u32(skb, 1);
137 struct conn *rconn = get_conn(conn_id);
139 if (unlikely(rconn == 0))
140 goto reset;
142 BUG_ON(SOURCE_IN != rconn->sourcetype);
143 BUG_ON(0 == rconn->reversedir);
145 if (unlikely(rconn->source.in.nb != nb))
146 goto reset;
148 parsefunc(nb, skb, code, rconn);
150 if (0) {
151 reset:
152 #warning todo reset backwards
153 return;
158 static void parse_conndata(struct neighbor *nb, struct sk_buff *skb)
160 __u32 conn_id = pull_u32(skb, 1);
161 __u32 seqno = pull_u32(skb, 1);
162 __u32 datalength = pull_u32(skb, 1);
163 char *data = cor_pull_skb(skb, datalength);
165 BUG_ON(data == 0);
167 conn_rcv_buildskb(data, datalength, conn_id, seqno);
170 static void parse_connect(struct neighbor *nb, struct sk_buff *skb)
172 struct conn *rconn;
173 __u32 conn_id = pull_u32(skb, 1);
174 struct control_msg_out *cm = alloc_control_msg();
176 if (unlikely(cm == 0))
177 return;
179 rconn = alloc_conn(GFP_KERNEL);
181 if (unlikely(rconn == 0))
182 goto err;
184 if (conn_init_out(rconn->reversedir, nb)) {
185 goto err;
187 rconn->target.out.conn_id = conn_id;
189 send_connect_success(cm, rconn);
190 err:
191 send_connect_failed_temp(cm, nb, conn_id);
192 return;
195 static void kernel_packet2(struct neighbor *nb, struct sk_buff *skb,
196 __u32 seqno)
198 struct control_msg_out *cm = alloc_control_msg();
199 int ack = 0;
201 if (unlikely(cm == 0))
202 return;
204 while (1) {
205 __u32 conn_id;
206 __u32 seqno;
208 __u8 code = pull_u8(skb);
210 switch (code) {
211 case KP_PADDING:
212 break;
213 case KP_ACK:
214 conn_id = pull_u32(skb, 0);
215 seqno = pull_u32(skb, 0);
216 ack_received(nb, conn_id, seqno, 0);
217 break;
218 case KP_NACK:
219 conn_id = pull_u32(skb, 0);
220 seqno = pull_u32(skb, 0);
221 ack_received(nb, conn_id, seqno, 1);
222 break;
223 case KP_SPEED:
224 conn_cmd(nb, skb, code, parse_speed);
225 break;
226 case KP_CONNECT:
227 parse_connect(nb, skb);
228 break;
229 case KP_CONNECT_SUCCESS:
230 conn_cmd(nb, skb, code, parse_conn_success);
231 break;
232 case KP_CONNECT_FAILED_TEMP:
233 case KP_CONNECT_FAILED_PERM:
234 conn_cmd(nb, skb, code, parse_conn_failed);
235 break;
236 case KP_CONN_DATA:
237 parse_conndata(nb, skb);
238 break;
240 #warning todo is next packet?
241 #warning todo move to next packet
242 packet_id = pull_u32(skb);
243 struc packet_id *pid = search_packet_id(packet_id);
244 if (code == KP_FLUSH_CONN_BUF)
245 #warning todo offset, timeout
246 flush(pid->conn, 0, 10);
247 else if (code == KP_CONN_DATA) {
248 #warning todo
249 //skb_trim(skb, len);
250 } else if (code == KP_CONN_EOF)
251 #warning todo timeout
252 eof(pid->conn, 10);
253 else if (code == KP_CONN_RESET_FW)
254 reset(pid->conn)
255 else
256 BUG();
257 break;*/
258 case KP_RESET_CONN:
259 conn_cmd(nb, skb, code, parse_reset);
260 break;
261 default:
262 BUG();
266 if (ack)
267 send_ack(cm, nb, 0, seqno);
268 else
269 free_control_msg(cm);
272 void kernel_packet(struct neighbor *nb, struct sk_buff *skb, __u32 seqno)
274 struct sk_buff *skb2 = skb_clone(skb, __GFP_DMA | GFP_KERNEL);
276 while (1) {
277 __u8 *codeptr = cor_pull_skb(skb2, 1);
278 __u8 code;
280 char *lengthptr;
281 __u32 length;
283 if (codeptr == 0)
284 goto discard;
285 code = *codeptr;
286 switch (code) {
287 case KP_PADDING:
288 break;
289 case KP_ACK:
290 case KP_NACK:
291 if (cor_pull_skb(skb2, 4) == 0)
292 goto discard;
293 break;
294 case KP_SPEED:
295 case KP_CONNECT:
296 if (cor_pull_skb(skb2, 5) == 0)
297 goto discard;
298 break;
299 case KP_CONNECT_SUCCESS:
300 if (cor_pull_skb(skb2, 4) == 0)
301 goto discard;
302 break;
303 case KP_CONNECT_FAILED_TEMP:
304 case KP_CONNECT_FAILED_PERM:
305 if (cor_pull_skb(skb2, 4) == 0)
306 goto discard;
307 break;
308 case KP_CONN_DATA:
309 if (cor_pull_skb(skb2, 4) == 0)
310 goto discard;
311 lengthptr = cor_pull_skb(skb2, 4);
312 if (lengthptr == 0)
313 goto discard;
314 length = ntohl(*((__u32 *)lengthptr));
315 if (cor_pull_skb(skb, length) == 0)
316 goto discard;
317 break;
318 case KP_RESET_CONN:
319 if (cor_pull_skb(skb2, 4) == 0)
320 goto discard;
321 break;
322 default:
323 goto discard;
326 kfree_skb(skb2);
327 kernel_packet2(nb, skb, seqno);
328 kfree_skb(skb);
329 return;
330 discard:
331 kfree_skb(skb2);
332 kfree_skb(skb);