l3ni1.c 75.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/* $Id: l3ni1.c,v 2.8.2.3 2004/01/13 14:31:25 keil Exp $
 *
 * NI1 D-channel protocol
 *
 * Author       Matt Henderson & Guy Ellis
 * Copyright    by Traverse Technologies Pty Ltd, www.travers.com.au
7
 *
L
Linus Torvalds 已提交
8 9 10
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
11 12 13 14
 * 2000.6.6 Initial implementation of routines for US NI1
 * Layer 3 protocol based on the EURO/DSS1 D-channel protocol
 * driver written by Karsten Keil et al.
 * NI-1 Hall of Fame - Thanks to....
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24
 * Ragnar Paulson - for some handy code fragments
 * Will Scales - beta tester extraordinaire
 * Brett Whittacre - beta tester and remote devel system in Vegas
 *
 */

#include "hisax.h"
#include "isdnl3.h"
#include "l3ni1.h"
#include <linux/ctype.h>
25
#include <linux/slab.h>
L
Linus Torvalds 已提交
26 27

extern char *HiSax_getrev(const char *revision);
28
static const char *ni1_revision = "$Revision: 2.8.2.3 $";
L
Linus Torvalds 已提交
29 30 31

#define EXT_BEARER_CAPS 1

32 33 34 35 36 37 38 39
#define	MsgHead(ptr, cref, mty)			\
	*ptr++ = 0x8;				\
	if (cref == -1) {			\
		*ptr++ = 0x0;			\
	} else {				\
		*ptr++ = 0x1;			\
		*ptr++ = cref^0x80;		\
	}					\
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50
	*ptr++ = mty


/**********************************************/
/* get a new invoke id for remote operations. */
/* Only a return value != 0 is valid          */
/**********************************************/
static unsigned char new_invoke_id(struct PStack *p)
{
	unsigned char retval;
	int i;
51

L
Linus Torvalds 已提交
52 53 54 55 56 57
	i = 32; /* maximum search depth */

	retval = p->prot.ni1.last_invoke_id + 1; /* try new id */
	while ((i) && (p->prot.ni1.invoke_used[retval >> 3] == 0xFF)) {
		p->prot.ni1.last_invoke_id = (retval & 0xF8) + 8;
		i--;
58
	}
L
Linus Torvalds 已提交
59 60
	if (i) {
		while (p->prot.ni1.invoke_used[retval >> 3] & (1 << (retval & 7)))
61
			retval++;
L
Linus Torvalds 已提交
62 63 64 65
	} else
		retval = 0;
	p->prot.ni1.last_invoke_id = retval;
	p->prot.ni1.invoke_used[retval >> 3] |= (1 << (retval & 7));
66
	return (retval);
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74
} /* new_invoke_id */

/*************************/
/* free a used invoke id */
/*************************/
static void free_invoke_id(struct PStack *p, unsigned char id)
{

75
	if (!id) return; /* 0 = invalid value */
L
Linus Torvalds 已提交
76

77 78
	p->prot.ni1.invoke_used[id >> 3] &= ~(1 << (id & 7));
} /* free_invoke_id */
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87


/**********************************************************/
/* create a new l3 process and fill in ni1 specific data */
/**********************************************************/
static struct l3_process
*ni1_new_l3_process(struct PStack *st, int cr)
{  struct l3_process *proc;

88 89
	if (!(proc = new_l3_process(st, cr)))
		return (NULL);
L
Linus Torvalds 已提交
90

91 92 93 94 95
	proc->prot.ni1.invoke_id = 0;
	proc->prot.ni1.remote_operation = 0;
	proc->prot.ni1.uus1_data[0] = '\0';

	return (proc);
L
Linus Torvalds 已提交
96 97 98 99
} /* ni1_new_l3_process */

/************************************************/
/* free a l3 process and all ni1 specific data */
100
/************************************************/
L
Linus Torvalds 已提交
101 102 103
static void
ni1_release_l3_process(struct l3_process *p)
{
104 105
	free_invoke_id(p->st, p->prot.ni1.invoke_id);
	release_l3_process(p);
L
Linus Torvalds 已提交
106
} /* ni1_release_l3_process */
107

L
Linus Torvalds 已提交
108 109 110 111 112 113 114
/********************************************************/
/* search a process with invoke id id and dummy callref */
/********************************************************/
static struct l3_process *
l3ni1_search_dummy_proc(struct PStack *st, int id)
{ struct l3_process *pc = st->l3.proc; /* start of processes */

115
	if (!id) return (NULL);
L
Linus Torvalds 已提交
116

117 118 119 120 121 122
	while (pc)
	{ if ((pc->callref == -1) && (pc->prot.ni1.invoke_id == id))
			return (pc);
		pc = pc->next;
	}
	return (NULL);
L
Linus Torvalds 已提交
123 124 125 126 127
} /* l3ni1_search_dummy_proc */

/*******************************************************************/
/* called when a facility message with a dummy callref is received */
/* and a return result is delivered. id specifies the invoke id.   */
128 129
/*******************************************************************/
static void
L
Linus Torvalds 已提交
130 131
l3ni1_dummy_return_result(struct PStack *st, int id, u_char *p, u_char nlen)
{ isdn_ctrl ic;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	struct IsdnCardState *cs;
	struct l3_process *pc = NULL;

	if ((pc = l3ni1_search_dummy_proc(st, id)))
	{ L3DelTimer(&pc->timer); /* remove timer */

		cs = pc->st->l1.hardware;
		ic.driver = cs->myid;
		ic.command = ISDN_STAT_PROT;
		ic.arg = NI1_STAT_INVOKE_RES;
		ic.parm.ni1_io.hl_id = pc->prot.ni1.invoke_id;
		ic.parm.ni1_io.ll_id = pc->prot.ni1.ll_id;
		ic.parm.ni1_io.proc = pc->prot.ni1.proc;
		ic.parm.ni1_io.timeout = 0;
		ic.parm.ni1_io.datalen = nlen;
		ic.parm.ni1_io.data = p;
		free_invoke_id(pc->st, pc->prot.ni1.invoke_id);
		pc->prot.ni1.invoke_id = 0; /* reset id */

		cs->iif.statcallb(&ic);
		ni1_release_l3_process(pc);
	}
	else
		l3_debug(st, "dummy return result id=0x%x result len=%d", id, nlen);
L
Linus Torvalds 已提交
156 157 158 159 160
} /* l3ni1_dummy_return_result */

/*******************************************************************/
/* called when a facility message with a dummy callref is received */
/* and a return error is delivered. id specifies the invoke id.    */
161 162
/*******************************************************************/
static void
L
Linus Torvalds 已提交
163 164
l3ni1_dummy_error_return(struct PStack *st, int id, ulong error)
{ isdn_ctrl ic;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	struct IsdnCardState *cs;
	struct l3_process *pc = NULL;

	if ((pc = l3ni1_search_dummy_proc(st, id)))
	{ L3DelTimer(&pc->timer); /* remove timer */

		cs = pc->st->l1.hardware;
		ic.driver = cs->myid;
		ic.command = ISDN_STAT_PROT;
		ic.arg = NI1_STAT_INVOKE_ERR;
		ic.parm.ni1_io.hl_id = pc->prot.ni1.invoke_id;
		ic.parm.ni1_io.ll_id = pc->prot.ni1.ll_id;
		ic.parm.ni1_io.proc = pc->prot.ni1.proc;
		ic.parm.ni1_io.timeout = error;
		ic.parm.ni1_io.datalen = 0;
		ic.parm.ni1_io.data = NULL;
		free_invoke_id(pc->st, pc->prot.ni1.invoke_id);
		pc->prot.ni1.invoke_id = 0; /* reset id */

		cs->iif.statcallb(&ic);
		ni1_release_l3_process(pc);
	}
	else
		l3_debug(st, "dummy return error id=0x%x error=0x%lx", id, error);
L
Linus Torvalds 已提交
189 190 191 192 193
} /* l3ni1_error_return */

/*******************************************************************/
/* called when a facility message with a dummy callref is received */
/* and a invoke is delivered. id specifies the invoke id.          */
194 195 196 197
/*******************************************************************/
static void
l3ni1_dummy_invoke(struct PStack *st, int cr, int id,
		   int ident, u_char *p, u_char nlen)
L
Linus Torvalds 已提交
198
{ isdn_ctrl ic;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	struct IsdnCardState *cs;

	l3_debug(st, "dummy invoke %s id=0x%x ident=0x%x datalen=%d",
		 (cr == -1) ? "local" : "broadcast", id, ident, nlen);
	if (cr >= -1) return; /* ignore local data */

	cs = st->l1.hardware;
	ic.driver = cs->myid;
	ic.command = ISDN_STAT_PROT;
	ic.arg = NI1_STAT_INVOKE_BRD;
	ic.parm.ni1_io.hl_id = id;
	ic.parm.ni1_io.ll_id = 0;
	ic.parm.ni1_io.proc = ident;
	ic.parm.ni1_io.timeout = 0;
	ic.parm.ni1_io.datalen = nlen;
	ic.parm.ni1_io.data = p;

	cs->iif.statcallb(&ic);
L
Linus Torvalds 已提交
217 218 219 220
} /* l3ni1_dummy_invoke */

static void
l3ni1_parse_facility(struct PStack *st, struct l3_process *pc,
221
		     int cr, u_char *p)
L
Linus Torvalds 已提交
222 223 224 225 226 227
{
	int qd_len = 0;
	unsigned char nlen = 0, ilen, cp_tag;
	int ident, id;
	ulong err_ret;

228
	if (pc)
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		st = pc->st; /* valid Stack */
	else
		if ((!st) || (cr >= 0)) return; /* neither pc nor st specified */

	p++;
	qd_len = *p++;
	if (qd_len == 0) {
		l3_debug(st, "qd_len == 0");
		return;
	}
	if ((*p & 0x1F) != 0x11) {	/* Service discriminator, supplementary service */
		l3_debug(st, "supplementary service != 0x11");
		return;
	}
	while (qd_len > 0 && !(*p & 0x80)) {	/* extension ? */
		p++;
		qd_len--;
	}
	if (qd_len < 2) {
		l3_debug(st, "qd_len < 2");
		return;
	}
	p++;
	qd_len--;
	if ((*p & 0xE0) != 0xA0) {	/* class and form */
		l3_debug(st, "class and form != 0xA0");
		return;
	}

258 259 260
	cp_tag = *p & 0x1F; /* remember tag value */

	p++;
L
Linus Torvalds 已提交
261
	qd_len--;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	if (qd_len < 1)
	{ l3_debug(st, "qd_len < 1");
		return;
	}
	if (*p & 0x80)
	{ /* length format indefinite or limited */
		nlen = *p++ & 0x7F; /* number of len bytes or indefinite */
		if ((qd_len-- < ((!nlen) ? 3 : (1 + nlen))) ||
		    (nlen > 1))
		{ l3_debug(st, "length format error or not implemented");
			return;
		}
		if (nlen == 1)
		{ nlen = *p++; /* complete length */
			qd_len--;
		}
		else
		{ qd_len -= 2; /* trailing null bytes */
			if ((*(p + qd_len)) || (*(p + qd_len + 1)))
			{ l3_debug(st, "length format indefinite error");
				return;
			}
			nlen = qd_len;
		}
	}
	else
	{ nlen = *p++;
		qd_len--;
	}
	if (qd_len < nlen)
	{ l3_debug(st, "qd_len < nlen");
		return;
	}
L
Linus Torvalds 已提交
295 296
	qd_len -= nlen;

297 298 299 300 301 302 303 304 305
	if (nlen < 2)
	{ l3_debug(st, "nlen < 2");
		return;
	}
	if (*p != 0x02)
	{  /* invoke identifier tag */
		l3_debug(st, "invoke identifier tag !=0x02");
		return;
	}
L
Linus Torvalds 已提交
306 307
	p++;
	nlen--;
308 309 310 311 312
	if (*p & 0x80)
	{ /* length format */
		l3_debug(st, "invoke id length format 2");
		return;
	}
L
Linus Torvalds 已提交
313 314
	ilen = *p++;
	nlen--;
315 316 317 318
	if (ilen > nlen || ilen == 0)
	{ l3_debug(st, "ilen > nlen || ilen == 0");
		return;
	}
L
Linus Torvalds 已提交
319 320
	nlen -= ilen;
	id = 0;
321 322 323 324
	while (ilen > 0)
	{ id = (id << 8) | (*p++ & 0xFF);	/* invoke identifier */
		ilen--;
	}
L
Linus Torvalds 已提交
325 326

	switch (cp_tag) {	/* component tag */
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	case 1:	/* invoke */
		if (nlen < 2) {
			l3_debug(st, "nlen < 2 22");
			return;
		}
		if (*p != 0x02) {	/* operation value */
			l3_debug(st, "operation value !=0x02");
			return;
		}
		p++;
		nlen--;
		ilen = *p++;
		nlen--;
		if (ilen > nlen || ilen == 0) {
			l3_debug(st, "ilen > nlen || ilen == 0 22");
			return;
		}
		nlen -= ilen;
		ident = 0;
		while (ilen > 0) {
			ident = (ident << 8) | (*p++ & 0xFF);
			ilen--;
		}
L
Linus Torvalds 已提交
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
		if (!pc)
		{
			l3ni1_dummy_invoke(st, cr, id, ident, p, nlen);
			return;
		}
		l3_debug(st, "invoke break");
		break;
	case 2:	/* return result */
		/* if no process available handle separately */
		if (!pc)
		{ if (cr == -1)
				l3ni1_dummy_return_result(st, id, p, nlen);
			return;
		}
		if ((pc->prot.ni1.invoke_id) && (pc->prot.ni1.invoke_id == id))
		{ /* Diversion successful */
			free_invoke_id(st, pc->prot.ni1.invoke_id);
			pc->prot.ni1.remote_result = 0; /* success */
			pc->prot.ni1.invoke_id = 0;
			pc->redir_result = pc->prot.ni1.remote_result;
			st->l3.l3l4(st, CC_REDIR | INDICATION, pc); } /* Diversion successful */
		else
			l3_debug(st, "return error unknown identifier");
		break;
	case 3:	/* return error */
		err_ret = 0;
		if (nlen < 2)
		{ l3_debug(st, "return error nlen < 2");
			return;
		}
		if (*p != 0x02)
		{ /* result tag */
			l3_debug(st, "invoke error tag !=0x02");
			return;
		}
		p++;
		nlen--;
		if (*p > 4)
		{ /* length format */
			l3_debug(st, "invoke return errlen > 4 ");
			return;
		}
		ilen = *p++;
		nlen--;
		if (ilen > nlen || ilen == 0)
		{ l3_debug(st, "error return ilen > nlen || ilen == 0");
			return;
		}
		nlen -= ilen;
		while (ilen > 0)
		{ err_ret = (err_ret << 8) | (*p++ & 0xFF);	/* error value */
			ilen--;
		}
		/* if no process available handle separately */
		if (!pc)
		{ if (cr == -1)
				l3ni1_dummy_error_return(st, id, err_ret);
			return;
		}
		if ((pc->prot.ni1.invoke_id) && (pc->prot.ni1.invoke_id == id))
		{ /* Deflection error */
			free_invoke_id(st, pc->prot.ni1.invoke_id);
			pc->prot.ni1.remote_result = err_ret; /* result */
			pc->prot.ni1.invoke_id = 0;
			pc->redir_result = pc->prot.ni1.remote_result;
			st->l3.l3l4(st, CC_REDIR | INDICATION, pc);
		} /* Deflection error */
		else
			l3_debug(st, "return result unknown identifier");
		break;
	default:
		l3_debug(st, "facility default break tag=0x%02x", cp_tag);
		break;
L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	}
}

static void
l3ni1_message(struct l3_process *pc, u_char mt)
{
	struct sk_buff *skb;
	u_char *p;

	if (!(skb = l3_alloc_skb(4)))
		return;
	p = skb_put(skb, 4);
	MsgHead(p, pc->callref, mt);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
}

static void
l3ni1_message_plus_chid(struct l3_process *pc, u_char mt)
/* sends an l3 messages plus channel id -  added GE 05/09/00 */
{
	struct sk_buff *skb;
	u_char tmp[16];
	u_char *p = tmp;
	u_char chid;

	chid = (u_char)(pc->para.bchannel & 0x03) | 0x88;
	MsgHead(p, pc->callref, mt);
	*p++ = IE_CHANNEL_ID;
	*p++ = 0x01;
	*p++ = chid;

	if (!(skb = l3_alloc_skb(7)))
		return;
	memcpy(skb_put(skb, 7), tmp, 7);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
}

static void
l3ni1_message_cause(struct l3_process *pc, u_char mt, u_char cause)
{
	struct sk_buff *skb;
	u_char tmp[16];
	u_char *p = tmp;
	int l;

	MsgHead(p, pc->callref, mt);
	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause | 0x80;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
}

static void
l3ni1_status_send(struct l3_process *pc, u_char pr, void *arg)
{
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	struct sk_buff *skb;

	MsgHead(p, pc->callref, MT_STATUS);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = pc->para.cause | 0x80;

	*p++ = IE_CALL_STATE;
	*p++ = 0x1;
	*p++ = pc->state & 0x3f;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
}

static void
l3ni1_msg_without_setup(struct l3_process *pc, u_char pr, void *arg)
{
	/* This routine is called if here was no SETUP made (checks in ni1up and in
	 * l3ni1_setup) and a RELEASE_COMPLETE have to be sent with an error code
	 * MT_STATUS_ENQUIRE in the NULL state is handled too
	 */
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	struct sk_buff *skb;

	switch (pc->para.cause) {
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	case 81:	/* invalid callreference */
	case 88:	/* incomp destination */
	case 96:	/* mandory IE missing */
	case 100:       /* invalid IE contents */
	case 101:	/* incompatible Callstate */
		MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);
		*p++ = IE_CAUSE;
		*p++ = 0x2;
		*p++ = 0x80;
		*p++ = pc->para.cause | 0x80;
		break;
	default:
		printk(KERN_ERR "HiSax l3ni1_msg_without_setup wrong cause %d\n",
		       pc->para.cause);
		return;
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545
	}
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
	ni1_release_l3_process(pc);
}

static int ie_ALERTING[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
546 547
			    IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, IE_HLC,
			    IE_USER_USER, -1};
L
Linus Torvalds 已提交
548
static int ie_CALL_PROCEEDING[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
549 550 551 552
				   IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_HLC, -1};
static int ie_CONNECT[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
			   IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_DATE, IE_SIGNAL,
			   IE_CONNECT_PN, IE_CONNECT_SUB, IE_LLC, IE_HLC, IE_USER_USER, -1};
L
Linus Torvalds 已提交
553 554
static int ie_CONNECT_ACKNOWLEDGE[] = {IE_CHANNEL_ID, IE_DISPLAY, IE_SIGNAL, -1};
static int ie_DISCONNECT[] = {IE_CAUSE | IE_MANDATORY, IE_FACILITY,
555
			      IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, IE_USER_USER, -1};
L
Linus Torvalds 已提交
556
static int ie_INFORMATION[] = {IE_COMPLETE, IE_DISPLAY, IE_KEYPAD, IE_SIGNAL,
557
			       IE_CALLED_PN, -1};
L
Linus Torvalds 已提交
558 559
static int ie_NOTIFY[] = {IE_BEARER, IE_NOTIFY | IE_MANDATORY, IE_DISPLAY, -1};
static int ie_PROGRESS[] = {IE_BEARER, IE_CAUSE, IE_FACILITY, IE_PROGRESS |
560
			    IE_MANDATORY, IE_DISPLAY, IE_HLC, IE_USER_USER, -1};
L
Linus Torvalds 已提交
561
static int ie_RELEASE[] = {IE_CAUSE | IE_MANDATORY_1, IE_FACILITY, IE_DISPLAY,
562 563 564
			   IE_SIGNAL, IE_USER_USER, -1};
/* a RELEASE_COMPLETE with errors don't require special actions
   static int ie_RELEASE_COMPLETE[] = {IE_CAUSE | IE_MANDATORY_1, IE_DISPLAY, IE_SIGNAL, IE_USER_USER, -1};
L
Linus Torvalds 已提交
565
*/
566 567
static int ie_RESUME_ACKNOWLEDGE[] = {IE_CHANNEL_ID | IE_MANDATORY, IE_FACILITY,
				      IE_DISPLAY, -1};
L
Linus Torvalds 已提交
568 569
static int ie_RESUME_REJECT[] = {IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
static int ie_SETUP[] = {IE_COMPLETE, IE_BEARER  | IE_MANDATORY,
570 571 572 573
			 IE_CHANNEL_ID | IE_MANDATORY, IE_FACILITY, IE_PROGRESS,
			 IE_NET_FAC, IE_DISPLAY, IE_KEYPAD, IE_SIGNAL, IE_CALLING_PN,
			 IE_CALLING_SUB, IE_CALLED_PN, IE_CALLED_SUB, IE_REDIR_NR,
			 IE_LLC, IE_HLC, IE_USER_USER, -1};
L
Linus Torvalds 已提交
574
static int ie_SETUP_ACKNOWLEDGE[] = {IE_CHANNEL_ID | IE_MANDATORY, IE_FACILITY,
575
				     IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, -1};
L
Linus Torvalds 已提交
576
static int ie_STATUS[] = {IE_CAUSE | IE_MANDATORY, IE_CALL_STATE |
577
			  IE_MANDATORY, IE_DISPLAY, -1};
L
Linus Torvalds 已提交
578 579 580
static int ie_STATUS_ENQUIRY[] = {IE_DISPLAY, -1};
static int ie_SUSPEND_ACKNOWLEDGE[] = {IE_DISPLAY, IE_FACILITY, -1};
static int ie_SUSPEND_REJECT[] = {IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
581
/* not used
L
Linus Torvalds 已提交
582 583 584 585 586 587 588
 * static int ie_CONGESTION_CONTROL[] = {IE_CONGESTION | IE_MANDATORY,
 *		IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
 * static int ie_USER_INFORMATION[] = {IE_MORE_DATA, IE_USER_USER | IE_MANDATORY, -1};
 * static int ie_RESTART[] = {IE_CHANNEL_ID, IE_DISPLAY, IE_RESTART_IND |
 *		IE_MANDATORY, -1};
 */
static int ie_FACILITY[] = {IE_FACILITY | IE_MANDATORY, IE_DISPLAY, -1};
589 590
static int comp_required[] = {1, 2, 3, 5, 6, 7, 9, 10, 11, 14, 15, -1};
static int l3_valid_states[] = {0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 15, 17, 19, 25, -1};
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

struct ie_len {
	int ie;
	int len;
};

static
struct ie_len max_ie_len[] = {
	{IE_SEGMENT, 4},
	{IE_BEARER, 12},
	{IE_CAUSE, 32},
	{IE_CALL_ID, 10},
	{IE_CALL_STATE, 3},
	{IE_CHANNEL_ID,	34},
	{IE_FACILITY, 255},
	{IE_PROGRESS, 4},
	{IE_NET_FAC, 255},
	{IE_NOTIFY, 3},
	{IE_DISPLAY, 82},
	{IE_DATE, 8},
	{IE_KEYPAD, 34},
	{IE_SIGNAL, 3},
	{IE_INFORATE, 6},
	{IE_E2E_TDELAY, 11},
	{IE_TDELAY_SEL, 5},
	{IE_PACK_BINPARA, 3},
	{IE_PACK_WINSIZE, 4},
	{IE_PACK_SIZE, 4},
	{IE_CUG, 7},
	{IE_REV_CHARGE, 3},
	{IE_CALLING_PN, 24},
	{IE_CALLING_SUB, 23},
	{IE_CALLED_PN, 24},
	{IE_CALLED_SUB, 23},
	{IE_REDIR_NR, 255},
	{IE_TRANS_SEL, 255},
	{IE_RESTART_IND, 3},
	{IE_LLC, 18},
	{IE_HLC, 5},
	{IE_USER_USER, 131},
631
	{-1, 0},
L
Linus Torvalds 已提交
632 633 634 635 636 637 638
};

static int
getmax_ie_len(u_char ie) {
	int i = 0;
	while (max_ie_len[i].ie != -1) {
		if (max_ie_len[i].ie == ie)
639
			return (max_ie_len[i].len);
L
Linus Torvalds 已提交
640 641
		i++;
	}
642
	return (255);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651
}

static int
ie_in_set(struct l3_process *pc, u_char ie, int *checklist) {
	int ret = 1;

	while (*checklist != -1) {
		if ((*checklist & 0xff) == ie) {
			if (ie & 0x80)
652
				return (-ret);
L
Linus Torvalds 已提交
653
			else
654
				return (ret);
L
Linus Torvalds 已提交
655 656 657 658
		}
		ret++;
		checklist++;
	}
659
	return (0);
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672
}

static int
check_infoelements(struct l3_process *pc, struct sk_buff *skb, int *checklist)
{
	int *cl = checklist;
	u_char mt;
	u_char *p, ie;
	int l, newpos, oldpos;
	int err_seq = 0, err_len = 0, err_compr = 0, err_ureg = 0;
	u_char codeset = 0;
	u_char old_codeset = 0;
	u_char codelock = 1;
673

L
Linus Torvalds 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
	p = skb->data;
	/* skip cr */
	p++;
	l = (*p++) & 0xf;
	p += l;
	mt = *p++;
	oldpos = 0;
	while ((p - skb->data) < skb->len) {
		if ((*p & 0xf0) == 0x90) { /* shift codeset */
			old_codeset = codeset;
			codeset = *p & 7;
			if (*p & 0x08)
				codelock = 0;
			else
				codelock = 1;
			if (pc->debug & L3_DEB_CHECK)
				l3_debug(pc->st, "check IE shift%scodeset %d->%d",
691
					 codelock ? " locking " : " ", old_codeset, codeset);
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
			p++;
			continue;
		}
		if (!codeset) { /* only codeset 0 */
			if ((newpos = ie_in_set(pc, *p, cl))) {
				if (newpos > 0) {
					if (newpos < oldpos)
						err_seq++;
					else
						oldpos = newpos;
				}
			} else {
				if (ie_in_set(pc, *p, comp_required))
					err_compr++;
				else
					err_ureg++;
			}
		}
		ie = *p++;
		if (ie & 0x80) {
			l = 1;
		} else {
			l = *p++;
			p += l;
			l += 2;
		}
		if (!codeset && (l > getmax_ie_len(ie)))
			err_len++;
		if (!codelock) {
			if (pc->debug & L3_DEB_CHECK)
				l3_debug(pc->st, "check IE shift back codeset %d->%d",
723
					 codeset, old_codeset);
L
Linus Torvalds 已提交
724 725 726 727 728 729 730
			codeset = old_codeset;
			codelock = 1;
		}
	}
	if (err_compr | err_ureg | err_len | err_seq) {
		if (pc->debug & L3_DEB_CHECK)
			l3_debug(pc->st, "check IE MT(%x) %d/%d/%d/%d",
731
				 mt, err_compr, err_ureg, err_len, err_seq);
L
Linus Torvalds 已提交
732
		if (err_compr)
733
			return (ERR_IE_COMPREHENSION);
L
Linus Torvalds 已提交
734
		if (err_ureg)
735
			return (ERR_IE_UNRECOGNIZED);
L
Linus Torvalds 已提交
736
		if (err_len)
737
			return (ERR_IE_LENGTH);
L
Linus Torvalds 已提交
738
		if (err_seq)
739 740 741
			return (ERR_IE_SEQUENCE);
	}
	return (0);
L
Linus Torvalds 已提交
742 743 744 745 746 747 748
}

/* verify if a message type exists and contain no IE error */
static int
l3ni1_check_messagetype_validity(struct l3_process *pc, int mt, void *arg)
{
	switch (mt) {
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	case MT_ALERTING:
	case MT_CALL_PROCEEDING:
	case MT_CONNECT:
	case MT_CONNECT_ACKNOWLEDGE:
	case MT_DISCONNECT:
	case MT_INFORMATION:
	case MT_FACILITY:
	case MT_NOTIFY:
	case MT_PROGRESS:
	case MT_RELEASE:
	case MT_RELEASE_COMPLETE:
	case MT_SETUP:
	case MT_SETUP_ACKNOWLEDGE:
	case MT_RESUME_ACKNOWLEDGE:
	case MT_RESUME_REJECT:
	case MT_SUSPEND_ACKNOWLEDGE:
	case MT_SUSPEND_REJECT:
	case MT_USER_INFORMATION:
	case MT_RESTART:
	case MT_RESTART_ACKNOWLEDGE:
	case MT_CONGESTION_CONTROL:
	case MT_STATUS:
	case MT_STATUS_ENQUIRY:
		if (pc->debug & L3_DEB_CHECK)
			l3_debug(pc->st, "l3ni1_check_messagetype_validity mt(%x) OK", mt);
		break;
	case MT_RESUME: /* RESUME only in user->net */
	case MT_SUSPEND: /* SUSPEND only in user->net */
	default:
		if (pc->debug & (L3_DEB_CHECK | L3_DEB_WARN))
			l3_debug(pc->st, "l3ni1_check_messagetype_validity mt(%x) fail", mt);
		pc->para.cause = 97;
		l3ni1_status_send(pc, 0, NULL);
		return (1);
L
Linus Torvalds 已提交
783
	}
784
	return (0);
L
Linus Torvalds 已提交
785 786 787 788 789 790 791
}

static void
l3ni1_std_ie_err(struct l3_process *pc, int ret) {

	if (pc->debug & L3_DEB_CHECK)
		l3_debug(pc->st, "check_infoelements ret %d", ret);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	switch (ret) {
	case 0:
		break;
	case ERR_IE_COMPREHENSION:
		pc->para.cause = 96;
		l3ni1_status_send(pc, 0, NULL);
		break;
	case ERR_IE_UNRECOGNIZED:
		pc->para.cause = 99;
		l3ni1_status_send(pc, 0, NULL);
		break;
	case ERR_IE_LENGTH:
		pc->para.cause = 100;
		l3ni1_status_send(pc, 0, NULL);
		break;
	case ERR_IE_SEQUENCE:
	default:
		break;
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	}
}

static int
l3ni1_get_channel_id(struct l3_process *pc, struct sk_buff *skb) {
	u_char *p;

	p = skb->data;
	if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
		p++;
		if (*p != 1) { /* len for BRI = 1 */
			if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "wrong chid len %d", *p);
			return (-2);
		}
		p++;
		if (*p & 0x60) { /* only base rate interface */
			if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "wrong chid %x", *p);
			return (-3);
		}
831
		return (*p & 0x3);
L
Linus Torvalds 已提交
832
	} else
833
		return (-1);
L
Linus Torvalds 已提交
834 835 836 837
}

static int
l3ni1_get_cause(struct l3_process *pc, struct sk_buff *skb) {
838
	u_char l, i = 0;
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846
	u_char *p;

	p = skb->data;
	pc->para.cause = 31;
	pc->para.loc = 0;
	if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
		p++;
		l = *p++;
847 848
		if (l > 30)
			return (1);
L
Linus Torvalds 已提交
849 850 851 852
		if (l) {
			pc->para.loc = *p++;
			l--;
		} else {
853
			return (2);
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862
		}
		if (l && !(pc->para.loc & 0x80)) {
			l--;
			p++; /* skip recommendation */
		}
		if (l) {
			pc->para.cause = *p++;
			l--;
			if (!(pc->para.cause & 0x80))
863
				return (3);
L
Linus Torvalds 已提交
864
		} else
865 866
			return (4);
		while (l && (i < 6)) {
L
Linus Torvalds 已提交
867 868 869 870
			pc->para.diag[i++] = *p++;
			l--;
		}
	} else
871 872
		return (-1);
	return (0);
L
Linus Torvalds 已提交
873 874 875 876 877 878
}

static void
l3ni1_msg_with_uus(struct l3_process *pc, u_char cmd)
{
	struct sk_buff *skb;
879
	u_char tmp[16 + 40];
L
Linus Torvalds 已提交
880 881 882 883 884
	u_char *p = tmp;
	int l;

	MsgHead(p, pc->callref, cmd);

885 886 887 888 889 890 891 892
	if (pc->prot.ni1.uus1_data[0])
	{ *p++ = IE_USER_USER; /* UUS info element */
		*p++ = strlen(pc->prot.ni1.uus1_data) + 1;
		*p++ = 0x04; /* IA5 chars */
		strcpy(p, pc->prot.ni1.uus1_data);
		p += strlen(pc->prot.ni1.uus1_data);
		pc->prot.ni1.uus1_data[0] = '\0';
	}
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
} /* l3ni1_msg_with_uus */

static void
l3ni1_release_req(struct l3_process *pc, u_char pr, void *arg)
{
	StopAllL3Timer(pc);
	newl3state(pc, 19);
906
	if (!pc->prot.ni1.uus1_data[0])
L
Linus Torvalds 已提交
907 908 909 910 911 912 913 914 915 916 917 918
		l3ni1_message(pc, MT_RELEASE);
	else
		l3ni1_msg_with_uus(pc, MT_RELEASE);
	L3AddTimer(&pc->timer, T308, CC_T308_1);
}

static void
l3ni1_release_cmpl(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

919
	if ((ret = l3ni1_get_cause(pc, skb)) > 0) {
L
Linus Torvalds 已提交
920
		if (pc->debug & L3_DEB_WARN)
921
			l3_debug(pc->st, "RELCMPL get_cause ret(%d)", ret);
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930 931 932
	} else if (ret < 0)
		pc->para.cause = NO_CAUSE;
	StopAllL3Timer(pc);
	newl3state(pc, 0);
	pc->st->l3.l3l4(pc->st, CC_RELEASE | CONFIRM, pc);
	ni1_release_l3_process(pc);
}

#if EXT_BEARER_CAPS

static u_char *
933
EncodeASyncParams(u_char *p, u_char si2)
L
Linus Torvalds 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
{				// 7c 06 88  90 21 42 00 bb

	p[0] = 0;
	p[1] = 0x40;		// Intermediate rate: 16 kbit/s jj 2000.02.19
	p[2] = 0x80;
	if (si2 & 32)		// 7 data bits

		p[2] += 16;
	else			// 8 data bits

		p[2] += 24;

	if (si2 & 16)		// 2 stop bits

		p[2] += 96;
	else			// 1 stop bit

		p[2] += 32;

	if (si2 & 8)		// even parity

		p[2] += 2;
	else			// no parity

		p[2] += 3;

	switch (si2 & 0x07) {
961 962
	case 0:
		p[0] = 66;	// 1200 bit/s
L
Linus Torvalds 已提交
963

964 965 966
		break;
	case 1:
		p[0] = 88;	// 1200/75 bit/s
L
Linus Torvalds 已提交
967

968 969 970
		break;
	case 2:
		p[0] = 87;	// 75/1200 bit/s
L
Linus Torvalds 已提交
971

972 973 974
		break;
	case 3:
		p[0] = 67;	// 2400 bit/s
L
Linus Torvalds 已提交
975

976 977 978
		break;
	case 4:
		p[0] = 69;	// 4800 bit/s
L
Linus Torvalds 已提交
979

980 981 982
		break;
	case 5:
		p[0] = 72;	// 9600 bit/s
L
Linus Torvalds 已提交
983

984 985 986
		break;
	case 6:
		p[0] = 73;	// 14400 bit/s
L
Linus Torvalds 已提交
987

988 989 990
		break;
	case 7:
		p[0] = 75;	// 19200 bit/s
L
Linus Torvalds 已提交
991

992
		break;
L
Linus Torvalds 已提交
993 994 995 996 997 998 999 1000 1001
	}
	return p + 3;
}

static u_char
EncodeSyncParams(u_char si2, u_char ai)
{

	switch (si2) {
1002 1003
	case 0:
		return ai + 2;	// 1200 bit/s
L
Linus Torvalds 已提交
1004

1005 1006
	case 1:
		return ai + 24;		// 1200/75 bit/s
L
Linus Torvalds 已提交
1007

1008 1009
	case 2:
		return ai + 23;		// 75/1200 bit/s
L
Linus Torvalds 已提交
1010

1011 1012
	case 3:
		return ai + 3;	// 2400 bit/s
L
Linus Torvalds 已提交
1013

1014 1015
	case 4:
		return ai + 5;	// 4800 bit/s
L
Linus Torvalds 已提交
1016

1017 1018
	case 5:
		return ai + 8;	// 9600 bit/s
L
Linus Torvalds 已提交
1019

1020 1021
	case 6:
		return ai + 9;	// 14400 bit/s
L
Linus Torvalds 已提交
1022

1023 1024
	case 7:
		return ai + 11;		// 19200 bit/s
L
Linus Torvalds 已提交
1025

1026 1027
	case 8:
		return ai + 14;		// 48000 bit/s
L
Linus Torvalds 已提交
1028

1029 1030
	case 9:
		return ai + 15;		// 56000 bit/s
L
Linus Torvalds 已提交
1031

1032 1033
	case 15:
		return ai + 40;		// negotiate bit/s
L
Linus Torvalds 已提交
1034

1035 1036
	default:
		break;
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042
	}
	return ai;
}


static u_char
1043
DecodeASyncParams(u_char si2, u_char *p)
L
Linus Torvalds 已提交
1044 1045 1046 1047
{
	u_char info;

	switch (p[5]) {
1048
	case 66:	// 1200 bit/s
L
Linus Torvalds 已提交
1049

1050
		break;	// si2 don't change
L
Linus Torvalds 已提交
1051

1052
	case 88:	// 1200/75 bit/s
L
Linus Torvalds 已提交
1053

1054 1055 1056
		si2 += 1;
		break;
	case 87:	// 75/1200 bit/s
L
Linus Torvalds 已提交
1057

1058 1059 1060
		si2 += 2;
		break;
	case 67:	// 2400 bit/s
L
Linus Torvalds 已提交
1061

1062 1063 1064
		si2 += 3;
		break;
	case 69:	// 4800 bit/s
L
Linus Torvalds 已提交
1065

1066 1067 1068
		si2 += 4;
		break;
	case 72:	// 9600 bit/s
L
Linus Torvalds 已提交
1069

1070 1071 1072
		si2 += 5;
		break;
	case 73:	// 14400 bit/s
L
Linus Torvalds 已提交
1073

1074 1075 1076
		si2 += 6;
		break;
	case 75:	// 19200 bit/s
L
Linus Torvalds 已提交
1077

1078 1079
		si2 += 7;
		break;
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	}

	info = p[7] & 0x7f;
	if ((info & 16) && (!(info & 8)))	// 7 data bits

		si2 += 32;	// else 8 data bits

	if ((info & 96) == 96)	// 2 stop bits

		si2 += 16;	// else 1 stop bit

	if ((info & 2) && (!(info & 1)))	// even parity

		si2 += 8;	// else no parity

	return si2;
}


static u_char
DecodeSyncParams(u_char si2, u_char info)
{
	info &= 0x7f;
	switch (info) {
1104
	case 40:	// bit/s negotiation failed  ai := 165 not 175!
L
Linus Torvalds 已提交
1105

1106 1107
		return si2 + 15;
	case 15:	// 56000 bit/s failed, ai := 0 not 169 !
L
Linus Torvalds 已提交
1108

1109 1110
		return si2 + 9;
	case 14:	// 48000 bit/s
L
Linus Torvalds 已提交
1111

1112 1113
		return si2 + 8;
	case 11:	// 19200 bit/s
L
Linus Torvalds 已提交
1114

1115 1116
		return si2 + 7;
	case 9:	// 14400 bit/s
L
Linus Torvalds 已提交
1117

1118 1119
		return si2 + 6;
	case 8:	// 9600  bit/s
L
Linus Torvalds 已提交
1120

1121 1122
		return si2 + 5;
	case 5:	// 4800  bit/s
L
Linus Torvalds 已提交
1123

1124 1125
		return si2 + 4;
	case 3:	// 2400  bit/s
L
Linus Torvalds 已提交
1126

1127 1128
		return si2 + 3;
	case 23:	// 75/1200 bit/s
L
Linus Torvalds 已提交
1129

1130 1131
		return si2 + 2;
	case 24:	// 1200/75 bit/s
L
Linus Torvalds 已提交
1132

1133 1134
		return si2 + 1;
	default:	// 1200 bit/s
L
Linus Torvalds 已提交
1135

1136
		return si2;
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
	}
}

static u_char
DecodeSI2(struct sk_buff *skb)
{
	u_char *p;		//, *pend=skb->data + skb->len;

	if ((p = findie(skb->data, skb->len, 0x7c, 0))) {
		switch (p[4] & 0x0f) {
1147 1148
		case 0x01:
			if (p[1] == 0x04)	// sync. Bitratenadaption
L
Linus Torvalds 已提交
1149

1150
				return DecodeSyncParams(160, p[5]);	// V.110/X.30
L
Linus Torvalds 已提交
1151

1152
			else if (p[1] == 0x06)	// async. Bitratenadaption
L
Linus Torvalds 已提交
1153

1154
				return DecodeASyncParams(192, p);	// V.110/X.30
L
Linus Torvalds 已提交
1155

1156 1157 1158 1159 1160
			break;
		case 0x08:	// if (p[5] == 0x02) // sync. Bitratenadaption
			if (p[1] > 3)
				return DecodeSyncParams(176, p[5]);	// V.120
			break;
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
		}
	}
	return 0;
}

#endif


static void
l3ni1_setup_req(struct l3_process *pc, u_char pr,
1171
		void *arg)
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
{
	struct sk_buff *skb;
	u_char tmp[128];
	u_char *p = tmp;

	u_char *teln;
	u_char *sub;
	u_char *sp;
	int l;

	MsgHead(p, pc->callref, MT_SETUP);

	teln = pc->para.setup.phone;

	*p++ = 0xa1;		/* complete indicator */
	/*
	 * Set Bearer Capability, Map info from 1TR6-convention to NI1
	 */
	switch (pc->para.setup.si1) {
	case 1:	                  /* Telephony                                */
		*p++ = IE_BEARER;
		*p++ = 0x3;	  /* Length                                   */
1194
		*p++ = 0x90;	  /* 3.1khz Audio			      */
L
Linus Torvalds 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		*p++ = 0x90;	  /* Circuit-Mode 64kbps                      */
		*p++ = 0xa2;	  /* u-Law Audio                              */
		break;
	case 5:	                  /* Datatransmission 64k, BTX                */
	case 7:	                  /* Datatransmission 64k                     */
	default:
		*p++ = IE_BEARER;
		*p++ = 0x2;	  /* Length                                   */
		*p++ = 0x88;	  /* Coding Std. CCITT, unrestr. dig. Inform. */
		*p++ = 0x90;	  /* Circuit-Mode 64kbps                      */
		break;
	}

	sub = NULL;
	sp = teln;
	while (*sp) {
		if ('.' == *sp) {
			sub = sp;
			*sp = 0;
		} else
			sp++;
	}
1217

L
Linus Torvalds 已提交
1218 1219 1220 1221 1222 1223 1224
	*p++ = IE_KEYPAD;
	*p++ = strlen(teln);
	while (*teln)
		*p++ = (*teln++) & 0x7F;

	if (sub)
		*sub++ = '.';
1225

L
Linus Torvalds 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
#if EXT_BEARER_CAPS
	if ((pc->para.setup.si2 >= 160) && (pc->para.setup.si2 <= 175)) {	// sync. Bitratenadaption, V.110/X.30

		*p++ = IE_LLC;
		*p++ = 0x04;
		*p++ = 0x88;
		*p++ = 0x90;
		*p++ = 0x21;
		*p++ = EncodeSyncParams(pc->para.setup.si2 - 160, 0x80);
	} else if ((pc->para.setup.si2 >= 176) && (pc->para.setup.si2 <= 191)) {	// sync. Bitratenadaption, V.120

		*p++ = IE_LLC;
		*p++ = 0x05;
		*p++ = 0x88;
		*p++ = 0x90;
		*p++ = 0x28;
		*p++ = EncodeSyncParams(pc->para.setup.si2 - 176, 0);
		*p++ = 0x82;
	} else if (pc->para.setup.si2 >= 192) {		// async. Bitratenadaption, V.110/X.30

		*p++ = IE_LLC;
		*p++ = 0x06;
		*p++ = 0x88;
		*p++ = 0x90;
		*p++ = 0x21;
		p = EncodeASyncParams(p, pc->para.setup.si2 - 192);
	} else {
1253
		switch (pc->para.setup.si1) {
L
Linus Torvalds 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
		case 1:	                /* Telephony                                */
			*p++ = IE_LLC;
			*p++ = 0x3;	/* Length                                   */
			*p++ = 0x90;	/* Coding Std. CCITT, 3.1 kHz audio         */
			*p++ = 0x90;	/* Circuit-Mode 64kbps                      */
			*p++ = 0xa2;	/* u-Law Audio                              */
			break;
		case 5:	                /* Datatransmission 64k, BTX                */
		case 7:	                /* Datatransmission 64k                     */
		default:
			*p++ = IE_LLC;
			*p++ = 0x2;	/* Length                                   */
			*p++ = 0x88;	/* Coding Std. CCITT, unrestr. dig. Inform. */
			*p++ = 0x90;	/* Circuit-Mode 64kbps                      */
			break;
1269
		}
L
Linus Torvalds 已提交
1270 1271 1272 1273
	}
#endif
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
1274
	{
L
Linus Torvalds 已提交
1275
		return;
1276
	}
L
Linus Torvalds 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	memcpy(skb_put(skb, l), tmp, l);
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, T303, CC_T303);
	newl3state(pc, 1);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
}

static void
l3ni1_call_proc(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int id, ret;

	if ((id = l3ni1_get_channel_id(pc, skb)) >= 0) {
		if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
			if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "setup answer with wrong chid %x", id);
			pc->para.cause = 100;
			l3ni1_status_send(pc, pr, NULL);
			return;
		}
		pc->para.bchannel = id;
	} else if (1 == pc->state) {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "setup answer wrong chid (ret %d)", id);
		if (id == -1)
			pc->para.cause = 96;
		else
			pc->para.cause = 100;
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	/* Now we are on none mandatory IEs */
	ret = check_infoelements(pc, skb, ie_CALL_PROCEEDING);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);
	newl3state(pc, 3);
	L3AddTimer(&pc->timer, T310, CC_T310);
	if (ret) /* STATUS for none mandatory IE errors after actions are taken */
		l3ni1_std_ie_err(pc, ret);
	pc->st->l3.l3l4(pc->st, CC_PROCEEDING | INDICATION, pc);
}

static void
l3ni1_setup_ack(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int id, ret;

	if ((id = l3ni1_get_channel_id(pc, skb)) >= 0) {
		if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
			if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "setup answer with wrong chid %x", id);
			pc->para.cause = 100;
			l3ni1_status_send(pc, pr, NULL);
			return;
		}
		pc->para.bchannel = id;
	} else {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "setup answer wrong chid (ret %d)", id);
		if (id == -1)
			pc->para.cause = 96;
		else
			pc->para.cause = 100;
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	/* Now we are on none mandatory IEs */
	ret = check_infoelements(pc, skb, ie_SETUP_ACKNOWLEDGE);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);
	newl3state(pc, 2);
	L3AddTimer(&pc->timer, T304, CC_T304);
	if (ret) /* STATUS for none mandatory IE errors after actions are taken */
		l3ni1_std_ie_err(pc, ret);
	pc->st->l3.l3l4(pc->st, CC_MORE_INFO | INDICATION, pc);
}

static void
l3ni1_disconnect(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	u_char *p;
	int ret;
	u_char cause = 0;

	StopAllL3Timer(pc);
	if ((ret = l3ni1_get_cause(pc, skb))) {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "DISC get_cause ret(%d)", ret);
		if (ret < 0)
			cause = 96;
		else if (ret > 0)
			cause = 100;
1378
	}
L
Linus Torvalds 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	if ((p = findie(skb->data, skb->len, IE_FACILITY, 0)))
		l3ni1_parse_facility(pc->st, pc, pc->callref, p);
	ret = check_infoelements(pc, skb, ie_DISCONNECT);
	if (ERR_IE_COMPREHENSION == ret)
		cause = 96;
	else if ((!cause) && (ERR_IE_UNRECOGNIZED == ret))
		cause = 99;
	ret = pc->state;
	newl3state(pc, 12);
	if (cause)
		newl3state(pc, 19);
1390
	if (11 != ret)
L
Linus Torvalds 已提交
1391
		pc->st->l3.l3l4(pc->st, CC_DISCONNECT | INDICATION, pc);
1392 1393
	else if (!cause)
		l3ni1_release_req(pc, pr, NULL);
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	if (cause) {
		l3ni1_message_cause(pc, MT_RELEASE, cause);
		L3AddTimer(&pc->timer, T308, CC_T308_1);
	}
}

static void
l3ni1_connect(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

	ret = check_infoelements(pc, skb, ie_CONNECT);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);	/* T310 */
	newl3state(pc, 10);
	pc->para.chargeinfo = 0;
	/* here should inserted COLP handling KKe */
	if (ret)
		l3ni1_std_ie_err(pc, ret);
	pc->st->l3.l3l4(pc->st, CC_SETUP | CONFIRM, pc);
}

static void
l3ni1_alerting(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

	ret = check_infoelements(pc, skb, ie_ALERTING);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);	/* T304 */
	newl3state(pc, 4);
	if (ret)
		l3ni1_std_ie_err(pc, ret);
	pc->st->l3.l3l4(pc->st, CC_ALERTING | INDICATION, pc);
}

static void
l3ni1_setup(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	int bcfound = 0;
	char tmp[80];
	struct sk_buff *skb = arg;
	int id;
	int err = 0;

	/*
	 * Bearer Capabilities
	 */
	p = skb->data;
L
Lucas De Marchi 已提交
1452
	/* only the first occurrence 'll be detected ! */
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458
	if ((p = findie(p, skb->len, 0x04, 0))) {
		if ((p[1] < 2) || (p[1] > 11))
			err = 1;
		else {
			pc->para.setup.si2 = 0;
			switch (p[2] & 0x7f) {
1459 1460 1461 1462 1463 1464
			case 0x00: /* Speech */
			case 0x10: /* 3.1 Khz audio */
				pc->para.setup.si1 = 1;
				break;
			case 0x08: /* Unrestricted digital information */
				pc->para.setup.si1 = 7;
L
Linus Torvalds 已提交
1465 1466
/* JIM, 05.11.97 I wanna set service indicator 2 */
#if EXT_BEARER_CAPS
1467
				pc->para.setup.si2 = DecodeSI2(skb);
L
Linus Torvalds 已提交
1468
#endif
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
				break;
			case 0x09: /* Restricted digital information */
				pc->para.setup.si1 = 2;
				break;
			case 0x11:
				/* Unrestr. digital information  with
				 * tones/announcements ( or 7 kHz audio
				 */
				pc->para.setup.si1 = 3;
				break;
			case 0x18: /* Video */
				pc->para.setup.si1 = 4;
				break;
			default:
				err = 2;
				break;
L
Linus Torvalds 已提交
1485 1486
			}
			switch (p[3] & 0x7f) {
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
			case 0x40: /* packed mode */
				pc->para.setup.si1 = 8;
				break;
			case 0x10: /* 64 kbit */
			case 0x11: /* 2*64 kbit */
			case 0x13: /* 384 kbit */
			case 0x15: /* 1536 kbit */
			case 0x17: /* 1920 kbit */
				pc->para.moderate = p[3] & 0x7f;
				break;
			default:
				err = 3;
				break;
L
Linus Torvalds 已提交
1500 1501 1502 1503
			}
		}
		if (pc->debug & L3_DEB_SI)
			l3_debug(pc->st, "SI=%d, AI=%d",
1504
				 pc->para.setup.si1, pc->para.setup.si2);
L
Linus Torvalds 已提交
1505 1506 1507
		if (err) {
			if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "setup with wrong bearer(l=%d:%x,%x)",
1508
					 p[1], p[2], p[3]);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
			pc->para.cause = 100;
			l3ni1_msg_without_setup(pc, pr, NULL);
			return;
		}
	} else {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "setup without bearer capabilities");
		/* ETS 300-104 1.3.3 */
		pc->para.cause = 96;
		l3ni1_msg_without_setup(pc, pr, NULL);
		return;
	}
	/*
	 * Channel Identification
	 */
	if ((id = l3ni1_get_channel_id(pc, skb)) >= 0) {
		if ((pc->para.bchannel = id)) {
			if ((3 == id) && (0x10 == pc->para.moderate)) {
				if (pc->debug & L3_DEB_WARN)
					l3_debug(pc->st, "setup with wrong chid %x",
1529
						 id);
L
Linus Torvalds 已提交
1530 1531 1532 1533 1534
				pc->para.cause = 100;
				l3ni1_msg_without_setup(pc, pr, NULL);
				return;
			}
			bcfound++;
1535 1536 1537 1538 1539
		} else
		{ if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "setup without bchannel, call waiting");
			bcfound++;
		}
L
Linus Torvalds 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
	} else {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "setup with wrong chid ret %d", id);
		if (id == -1)
			pc->para.cause = 96;
		else
			pc->para.cause = 100;
		l3ni1_msg_without_setup(pc, pr, NULL);
		return;
	}
	/* Now we are on none mandatory IEs */
	err = check_infoelements(pc, skb, ie_SETUP);
	if (ERR_IE_COMPREHENSION == err) {
		pc->para.cause = 96;
		l3ni1_msg_without_setup(pc, pr, NULL);
		return;
	}
	p = skb->data;
	if ((p = findie(p, skb->len, 0x70, 0)))
		iecpy(pc->para.setup.eazmsn, p, 1);
	else
		pc->para.setup.eazmsn[0] = 0;

	p = skb->data;
	if ((p = findie(p, skb->len, 0x71, 0))) {
		/* Called party subaddress */
		if ((p[1] >= 2) && (p[2] == 0x80) && (p[3] == 0x50)) {
			tmp[0] = '.';
			iecpy(&tmp[1], p, 2);
			strcat(pc->para.setup.eazmsn, tmp);
		} else if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "wrong called subaddress");
	}
	p = skb->data;
	if ((p = findie(p, skb->len, 0x6c, 0))) {
		pc->para.setup.plan = p[2];
		if (p[2] & 0x80) {
			iecpy(pc->para.setup.phone, p, 1);
			pc->para.setup.screen = 0;
		} else {
			iecpy(pc->para.setup.phone, p, 2);
			pc->para.setup.screen = p[3];
		}
	} else {
		pc->para.setup.phone[0] = 0;
		pc->para.setup.plan = 0;
		pc->para.setup.screen = 0;
	}
	p = skb->data;
	if ((p = findie(p, skb->len, 0x6d, 0))) {
		/* Calling party subaddress */
		if ((p[1] >= 2) && (p[2] == 0x80) && (p[3] == 0x50)) {
			tmp[0] = '.';
			iecpy(&tmp[1], p, 2);
			strcat(pc->para.setup.phone, tmp);
		} else if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "wrong calling subaddress");
	}
	newl3state(pc, 6);
	if (err) /* STATUS for none mandatory IE errors after actions are taken */
		l3ni1_std_ie_err(pc, err);
	pc->st->l3.l3l4(pc->st, CC_SETUP | INDICATION, pc);
}

static void
l3ni1_reset(struct l3_process *pc, u_char pr, void *arg)
{
	ni1_release_l3_process(pc);
}

static void
l3ni1_disconnect_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
1614
	u_char tmp[16 + 40];
L
Linus Torvalds 已提交
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
	u_char *p = tmp;
	int l;
	u_char cause = 16;

	if (pc->para.cause != NO_CAUSE)
		cause = pc->para.cause;

	StopAllL3Timer(pc);

	MsgHead(p, pc->callref, MT_DISCONNECT);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause | 0x80;

1631 1632 1633 1634 1635 1636 1637 1638
	if (pc->prot.ni1.uus1_data[0])
	{ *p++ = IE_USER_USER; /* UUS info element */
		*p++ = strlen(pc->prot.ni1.uus1_data) + 1;
		*p++ = 0x04; /* IA5 chars */
		strcpy(p, pc->prot.ni1.uus1_data);
		p += strlen(pc->prot.ni1.uus1_data);
		pc->prot.ni1.uus1_data[0] = '\0';
	}
L
Linus Torvalds 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	newl3state(pc, 11);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
	L3AddTimer(&pc->timer, T305, CC_T305);
}

static void
l3ni1_setup_rsp(struct l3_process *pc, u_char pr,
1651 1652 1653 1654 1655 1656 1657 1658
		void *arg)
{
	if (!pc->para.bchannel)
	{ if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "D-chan connect for waiting call");
		l3ni1_disconnect_req(pc, pr, arg);
		return;
	}
L
Linus Torvalds 已提交
1659 1660 1661
	newl3state(pc, 8);
	if (pc->debug & L3_DEB_WARN)
		l3_debug(pc->st, "D-chan connect for waiting call");
1662
	l3ni1_message_plus_chid(pc, MT_CONNECT); /* GE 05/09/00 */
L
Linus Torvalds 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, T313, CC_T313);
}

static void
l3ni1_connect_ack(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

	ret = check_infoelements(pc, skb, ie_CONNECT_ACKNOWLEDGE);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	newl3state(pc, 10);
	L3DelTimer(&pc->timer);
	if (ret)
		l3ni1_std_ie_err(pc, ret);
	pc->st->l3.l3l4(pc->st, CC_SETUP_COMPL | INDICATION, pc);
}

static void
l3ni1_reject_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	u_char cause = 21;

	if (pc->para.cause != NO_CAUSE)
		cause = pc->para.cause;

	MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause | 0x80;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
	pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
	newl3state(pc, 0);
	ni1_release_l3_process(pc);
}

static void
l3ni1_release(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	u_char *p;
1719
	int ret, cause = 0;
L
Linus Torvalds 已提交
1720 1721

	StopAllL3Timer(pc);
1722
	if ((ret = l3ni1_get_cause(pc, skb)) > 0) {
L
Linus Torvalds 已提交
1723 1724
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "REL get_cause ret(%d)", ret);
1725
	} else if (ret < 0)
L
Linus Torvalds 已提交
1726 1727 1728 1729
		pc->para.cause = NO_CAUSE;
	if ((p = findie(skb->data, skb->len, IE_FACILITY, 0))) {
		l3ni1_parse_facility(pc->st, pc, pc->callref, p);
	}
1730
	if ((ret < 0) && (pc->state != 11))
L
Linus Torvalds 已提交
1731
		cause = 96;
1732
	else if (ret > 0)
L
Linus Torvalds 已提交
1733 1734 1735 1736 1737
		cause = 100;
	ret = check_infoelements(pc, skb, ie_RELEASE);
	if (ERR_IE_COMPREHENSION == ret)
		cause = 96;
	else if ((ERR_IE_UNRECOGNIZED == ret) && (!cause))
1738
		cause = 99;
L
Linus Torvalds 已提交
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
	if (cause)
		l3ni1_message_cause(pc, MT_RELEASE_COMPLETE, cause);
	else
		l3ni1_message(pc, MT_RELEASE_COMPLETE);
	pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
	newl3state(pc, 0);
	ni1_release_l3_process(pc);
}

static void
l3ni1_alert_req(struct l3_process *pc, u_char pr,
1750
		void *arg)
L
Linus Torvalds 已提交
1751 1752
{
	newl3state(pc, 7);
1753
	if (!pc->prot.ni1.uus1_data[0])
L
Linus Torvalds 已提交
1754 1755
		l3ni1_message(pc, MT_ALERTING);
	else
1756
		l3ni1_msg_with_uus(pc, MT_ALERTING);
L
Linus Torvalds 已提交
1757 1758 1759 1760
}

static void
l3ni1_proceed_req(struct l3_process *pc, u_char pr,
1761
		  void *arg)
L
Linus Torvalds 已提交
1762 1763 1764
{
	newl3state(pc, 9);
	l3ni1_message(pc, MT_CALL_PROCEEDING);
1765
	pc->st->l3.l3l4(pc->st, CC_PROCEED_SEND | INDICATION, pc);
L
Linus Torvalds 已提交
1766 1767 1768 1769
}

static void
l3ni1_setup_ack_req(struct l3_process *pc, u_char pr,
1770
		    void *arg)
L
Linus Torvalds 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
{
	newl3state(pc, 25);
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, T302, CC_T302);
	l3ni1_message(pc, MT_SETUP_ACKNOWLEDGE);
}

/********************************************/
/* deliver a incoming display message to HL */
/********************************************/
static void
l3ni1_deliver_display(struct l3_process *pc, int pr, u_char *infp)
{       u_char len;
1784
	isdn_ctrl ic;
L
Linus Torvalds 已提交
1785
	struct IsdnCardState *cs;
1786
	char *p;
L
Linus Torvalds 已提交
1787

1788 1789
	if (*infp++ != IE_DISPLAY) return;
	if ((len = *infp++) > 80) return; /* total length <= 82 */
L
Linus Torvalds 已提交
1790 1791
	if (!pc->chan) return;

1792 1793 1794
	p = ic.parm.display;
	while (len--)
		*p++ = *infp++;
L
Linus Torvalds 已提交
1795 1796 1797 1798
	*p = '\0';
	ic.command = ISDN_STAT_DISPLAY;
	cs = pc->st->l1.hardware;
	ic.driver = cs->myid;
1799
	ic.arg = pc->chan->chan;
L
Linus Torvalds 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	cs->iif.statcallb(&ic);
} /* l3ni1_deliver_display */


static void
l3ni1_progress(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int err = 0;
	u_char *p;

	if ((p = findie(skb->data, skb->len, IE_PROGRESS, 0))) {
		if (p[1] != 2) {
			err = 1;
			pc->para.cause = 100;
		} else if (!(p[2] & 0x70)) {
			switch (p[2]) {
1817 1818 1819 1820 1821 1822 1823 1824
			case 0x80:
			case 0x81:
			case 0x82:
			case 0x84:
			case 0x85:
			case 0x87:
			case 0x8a:
				switch (p[3]) {
L
Linus Torvalds 已提交
1825 1826
				case 0x81:
				case 0x82:
1827
				case 0x83:
L
Linus Torvalds 已提交
1828
				case 0x84:
1829
				case 0x88:
L
Linus Torvalds 已提交
1830 1831
					break;
				default:
1832
					err = 2;
L
Linus Torvalds 已提交
1833 1834
					pc->para.cause = 100;
					break;
1835 1836 1837 1838 1839 1840
				}
				break;
			default:
				err = 3;
				pc->para.cause = 100;
				break;
L
Linus Torvalds 已提交
1841 1842 1843 1844 1845 1846
			}
		}
	} else {
		pc->para.cause = 96;
		err = 4;
	}
1847
	if (err) {
L
Linus Torvalds 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "progress error %d", err);
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	/* Now we are on none mandatory IEs */
	err = check_infoelements(pc, skb, ie_PROGRESS);
	if (err)
		l3ni1_std_ie_err(pc, err);
	if (ERR_IE_COMPREHENSION != err)
		pc->st->l3.l3l4(pc->st, CC_PROGRESS | INDICATION, pc);
}

static void
l3ni1_notify(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int err = 0;
	u_char *p;

	if ((p = findie(skb->data, skb->len, IE_NOTIFY, 0))) {
		if (p[1] != 1) {
			err = 1;
			pc->para.cause = 100;
		} else {
			switch (p[2]) {
1874 1875 1876 1877 1878 1879 1880 1881
			case 0x80:
			case 0x81:
			case 0x82:
				break;
			default:
				pc->para.cause = 100;
				err = 2;
				break;
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886 1887
			}
		}
	} else {
		pc->para.cause = 96;
		err = 3;
	}
1888
	if (err) {
L
Linus Torvalds 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "notify error %d", err);
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	/* Now we are on none mandatory IEs */
	err = check_infoelements(pc, skb, ie_NOTIFY);
	if (err)
		l3ni1_std_ie_err(pc, err);
	if (ERR_IE_COMPREHENSION != err)
		pc->st->l3.l3l4(pc->st, CC_NOTIFY | INDICATION, pc);
}

static void
l3ni1_status_enq(struct l3_process *pc, u_char pr, void *arg)
{
	int ret;
	struct sk_buff *skb = arg;

	ret = check_infoelements(pc, skb, ie_STATUS_ENQUIRY);
	l3ni1_std_ie_err(pc, ret);
	pc->para.cause = 30; /* response to STATUS_ENQUIRY */
1911
	l3ni1_status_send(pc, pr, NULL);
L
Linus Torvalds 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
}

static void
l3ni1_information(struct l3_process *pc, u_char pr, void *arg)
{
	int ret;
	struct sk_buff *skb = arg;
	u_char *p;
	char tmp[32];

	ret = check_infoelements(pc, skb, ie_INFORMATION);
	if (ret)
		l3ni1_std_ie_err(pc, ret);
	if (pc->state == 25) { /* overlap receiving */
		L3DelTimer(&pc->timer);
		p = skb->data;
		if ((p = findie(p, skb->len, 0x70, 0))) {
			iecpy(tmp, p, 1);
			strcat(pc->para.setup.eazmsn, tmp);
			pc->st->l3.l3l4(pc->st, CC_MORE_INFO | INDICATION, pc);
		}
		L3AddTimer(&pc->timer, T302, CC_T302);
	}
}

/******************************/
/* handle deflection requests */
/******************************/
static void l3ni1_redir_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
	u_char tmp[128];
	u_char *p = tmp;
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
	u_char *subp;
	u_char len_phone = 0;
	u_char len_sub = 0;
	int l;


	strcpy(pc->prot.ni1.uus1_data, pc->chan->setup.eazmsn); /* copy uus element if available */
	if (!pc->chan->setup.phone[0])
	{ pc->para.cause = -1;
		l3ni1_disconnect_req(pc, pr, arg); /* disconnect immediately */
		return;
	} /* only uus */

	if (pc->prot.ni1.invoke_id)
		free_invoke_id(pc->st, pc->prot.ni1.invoke_id);

	if (!(pc->prot.ni1.invoke_id = new_invoke_id(pc->st)))
		return;

	MsgHead(p, pc->callref, MT_FACILITY);

	for (subp = pc->chan->setup.phone; (*subp) && (*subp != '.'); subp++) len_phone++; /* len of phone number */
	if (*subp++ == '.') len_sub = strlen(subp) + 2; /* length including info subaddress element */
L
Linus Torvalds 已提交
1968 1969

	*p++ = 0x1c;   /* Facility info element */
1970 1971 1972 1973 1974 1975
	*p++ = len_phone + len_sub + 2 + 2 + 8 + 3 + 3; /* length of element */
	*p++ = 0x91;  /* remote operations protocol */
	*p++ = 0xa1;  /* invoke component */

	*p++ = len_phone + len_sub + 2 + 2 + 8 + 3; /* length of data */
	*p++ = 0x02;  /* invoke id tag, integer */
L
Linus Torvalds 已提交
1976
	*p++ = 0x01;  /* length */
1977 1978
	*p++ = pc->prot.ni1.invoke_id;  /* invoke id */
	*p++ = 0x02;  /* operation value tag, integer */
L
Linus Torvalds 已提交
1979
	*p++ = 0x01;  /* length */
1980 1981 1982 1983 1984 1985 1986 1987
	*p++ = 0x0D;  /* Call Deflect */

	*p++ = 0x30;  /* sequence phone number */
	*p++ = len_phone + 2 + 2 + 3 + len_sub; /* length */

	*p++ = 0x30;  /* Deflected to UserNumber */
	*p++ = len_phone + 2 + len_sub; /* length */
	*p++ = 0x80; /* NumberDigits */
L
Linus Torvalds 已提交
1988
	*p++ = len_phone; /* length */
1989 1990
	for (l = 0; l < len_phone; l++)
		*p++ = pc->chan->setup.phone[l];
L
Linus Torvalds 已提交
1991

1992 1993 1994 1995 1996
	if (len_sub)
	{ *p++ = 0x04; /* called party subaddress */
		*p++ = len_sub - 2;
		while (*subp) *p++ = *subp++;
	}
L
Linus Torvalds 已提交
1997

1998 1999 2000
	*p++ = 0x01; /* screening identifier */
	*p++ = 0x01;
	*p++ = pc->chan->setup.screen;
L
Linus Torvalds 已提交
2001 2002 2003 2004 2005

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l))) return;
	memcpy(skb_put(skb, l), tmp, l);

2006
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
L
Linus Torvalds 已提交
2007 2008 2009 2010 2011 2012 2013
} /* l3ni1_redir_req */

/********************************************/
/* handle deflection request in early state */
/********************************************/
static void l3ni1_redir_req_early(struct l3_process *pc, u_char pr, void *arg)
{
2014 2015
	l3ni1_proceed_req(pc, pr, arg);
	l3ni1_redir_req(pc, pr, arg);
L
Linus Torvalds 已提交
2016 2017 2018 2019
} /* l3ni1_redir_req_early */

/***********************************************/
/* handle special commands for this protocol.  */
L
Lucas De Marchi 已提交
2020
/* Examples are call independent services like */
L
Linus Torvalds 已提交
2021 2022 2023 2024
/* remote operations with dummy  callref.      */
/***********************************************/
static int l3ni1_cmd_global(struct PStack *st, isdn_ctrl *ic)
{ u_char id;
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
	u_char temp[265];
	u_char *p = temp;
	int i, l, proc_len;
	struct sk_buff *skb;
	struct l3_process *pc = NULL;

	switch (ic->arg)
	{ case NI1_CMD_INVOKE:
			if (ic->parm.ni1_io.datalen < 0) return (-2); /* invalid parameter */

			for (proc_len = 1, i = ic->parm.ni1_io.proc >> 8; i; i++)
				i = i >> 8; /* add one byte */
			l = ic->parm.ni1_io.datalen + proc_len + 8; /* length excluding ie header */
			if (l > 255)
				return (-2); /* too long */

			if (!(id = new_invoke_id(st)))
				return (0); /* first get a invoke id -> return if no available */

			i = -1;
			MsgHead(p, i, MT_FACILITY); /* build message head */
			*p++ = 0x1C; /* Facility IE */
			*p++ = l; /* length of ie */
			*p++ = 0x91; /* remote operations */
			*p++ = 0xA1; /* invoke */
			*p++ = l - 3; /* length of invoke */
			*p++ = 0x02; /* invoke id tag */
			*p++ = 0x01; /* length is 1 */
			*p++ = id; /* invoke id */
			*p++ = 0x02; /* operation */
			*p++ = proc_len; /* length of operation */

			for (i = proc_len; i; i--)
				*p++ = (ic->parm.ni1_io.proc >> (i - 1)) & 0xFF;
			memcpy(p, ic->parm.ni1_io.data, ic->parm.ni1_io.datalen); /* copy data */
			l = (p - temp) + ic->parm.ni1_io.datalen; /* total length */

2062 2063 2064 2065
			if (ic->parm.ni1_io.timeout > 0) {
				pc = ni1_new_l3_process(st, -1);
				if (!pc) {
					free_invoke_id(st, id);
2066 2067
					return (-2);
				}
2068 2069 2070 2071 2072
				/* remember id */
				pc->prot.ni1.ll_id = ic->parm.ni1_io.ll_id;
				/* and procedure */
				pc->prot.ni1.proc = ic->parm.ni1_io.proc;
			}
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

			if (!(skb = l3_alloc_skb(l)))
			{ free_invoke_id(st, id);
				if (pc) ni1_release_l3_process(pc);
				return (-2);
			}
			memcpy(skb_put(skb, l), temp, l);

			if (pc)
			{ pc->prot.ni1.invoke_id = id; /* remember id */
				L3AddTimer(&pc->timer, ic->parm.ni1_io.timeout, CC_TNI1_IO | REQUEST);
			}

			l3_msg(st, DL_DATA | REQUEST, skb);
			ic->parm.ni1_io.hl_id = id; /* return id */
			return (0);

	case NI1_CMD_INVOKE_ABORT:
		if ((pc = l3ni1_search_dummy_proc(st, ic->parm.ni1_io.hl_id)))
		{ L3DelTimer(&pc->timer); /* remove timer */
			ni1_release_l3_process(pc);
			return (0);
		}
		else
		{ l3_debug(st, "l3ni1_cmd_global abort unknown id");
			return (-2);
		}
		break;

	default:
		l3_debug(st, "l3ni1_cmd_global unknown cmd 0x%lx", ic->arg);
		return (-1);
	} /* switch ic-> arg */
	return (-1);
L
Linus Torvalds 已提交
2107 2108
} /* l3ni1_cmd_global */

2109
static void
L
Linus Torvalds 已提交
2110 2111
l3ni1_io_timer(struct l3_process *pc)
{ isdn_ctrl ic;
2112
	struct IsdnCardState *cs = pc->st->l1.hardware;
L
Linus Torvalds 已提交
2113

2114
	L3DelTimer(&pc->timer); /* remove timer */
L
Linus Torvalds 已提交
2115

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
	ic.driver = cs->myid;
	ic.command = ISDN_STAT_PROT;
	ic.arg = NI1_STAT_INVOKE_ERR;
	ic.parm.ni1_io.hl_id = pc->prot.ni1.invoke_id;
	ic.parm.ni1_io.ll_id = pc->prot.ni1.ll_id;
	ic.parm.ni1_io.proc = pc->prot.ni1.proc;
	ic.parm.ni1_io.timeout = -1;
	ic.parm.ni1_io.datalen = 0;
	ic.parm.ni1_io.data = NULL;
	free_invoke_id(pc->st, pc->prot.ni1.invoke_id);
	pc->prot.ni1.invoke_id = 0; /* reset id */
L
Linus Torvalds 已提交
2127

2128
	cs->iif.statcallb(&ic);
L
Linus Torvalds 已提交
2129

2130
	ni1_release_l3_process(pc);
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
} /* l3ni1_io_timer */

static void
l3ni1_release_ind(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;
	int callState = 0;
	p = skb->data;

	if ((p = findie(p, skb->len, IE_CALL_STATE, 0))) {
		p++;
		if (1 == *p++)
			callState = *p;
	}
	if (callState == 0) {
		/* ETS 300-104 7.6.1, 8.6.1, 10.6.1... and 16.1
		 * set down layer 3 without sending any message
		 */
		pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
		newl3state(pc, 0);
		ni1_release_l3_process(pc);
	} else {
		pc->st->l3.l3l4(pc->st, CC_IGNORE | INDICATION, pc);
	}
}

static void
l3ni1_dummy(struct l3_process *pc, u_char pr, void *arg)
{
}

static void
l3ni1_t302(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.loc = 0;
	pc->para.cause = 28; /* invalid number */
	l3ni1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
}

static void
l3ni1_t303(struct l3_process *pc, u_char pr, void *arg)
{
	if (pc->N303 > 0) {
		pc->N303--;
		L3DelTimer(&pc->timer);
		l3ni1_setup_req(pc, pr, arg);
	} else {
		L3DelTimer(&pc->timer);
		l3ni1_message_cause(pc, MT_RELEASE_COMPLETE, 102);
		pc->st->l3.l3l4(pc->st, CC_NOSETUP_RSP, pc);
		ni1_release_l3_process(pc);
	}
}

static void
l3ni1_t304(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.loc = 0;
	pc->para.cause = 102;
	l3ni1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);

}

static void
l3ni1_t305(struct l3_process *pc, u_char pr, void *arg)
{
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	struct sk_buff *skb;
	u_char cause = 16;

	L3DelTimer(&pc->timer);
	if (pc->para.cause != NO_CAUSE)
		cause = pc->para.cause;

	MsgHead(p, pc->callref, MT_RELEASE);

	*p++ = IE_CAUSE;
	*p++ = 0x2;
	*p++ = 0x80;
	*p++ = cause | 0x80;

	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	newl3state(pc, 19);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
	L3AddTimer(&pc->timer, T308, CC_T308_1);
}

static void
l3ni1_t310(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.loc = 0;
	pc->para.cause = 102;
	l3ni1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
}

static void
l3ni1_t313(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.loc = 0;
	pc->para.cause = 102;
	l3ni1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc->st, CC_CONNECT_ERR, pc);
}

static void
l3ni1_t308_1(struct l3_process *pc, u_char pr, void *arg)
{
	newl3state(pc, 19);
	L3DelTimer(&pc->timer);
	l3ni1_message(pc, MT_RELEASE);
	L3AddTimer(&pc->timer, T308, CC_T308_2);
}

static void
l3ni1_t308_2(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc->st, CC_RELEASE_ERR, pc);
	ni1_release_l3_process(pc);
}

static void
l3ni1_t318(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.cause = 102;	/* Timer expiry */
	pc->para.loc = 0;	/* local */
	pc->st->l3.l3l4(pc->st, CC_RESUME_ERR, pc);
	newl3state(pc, 19);
	l3ni1_message(pc, MT_RELEASE);
	L3AddTimer(&pc->timer, T308, CC_T308_1);
}

static void
l3ni1_t319(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->para.cause = 102;	/* Timer expiry */
	pc->para.loc = 0;	/* local */
	pc->st->l3.l3l4(pc->st, CC_SUSPEND_ERR, pc);
	newl3state(pc, 10);
}

static void
l3ni1_restart(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
	ni1_release_l3_process(pc);
}

static void
l3ni1_status(struct l3_process *pc, u_char pr, void *arg)
{
	u_char *p;
	struct sk_buff *skb = arg;
2300
	int ret;
L
Linus Torvalds 已提交
2301
	u_char cause = 0, callState = 0;
2302

L
Linus Torvalds 已提交
2303 2304
	if ((ret = l3ni1_get_cause(pc, skb))) {
		if (pc->debug & L3_DEB_WARN)
2305
			l3_debug(pc->st, "STATUS get_cause ret(%d)", ret);
L
Linus Torvalds 已提交
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
		if (ret < 0)
			cause = 96;
		else if (ret > 0)
			cause = 100;
	}
	if ((p = findie(skb->data, skb->len, IE_CALL_STATE, 0))) {
		p++;
		if (1 == *p++) {
			callState = *p;
			if (!ie_in_set(pc, *p, l3_valid_states))
				cause = 100;
		} else
			cause = 100;
	} else
		cause = 96;
	if (!cause) { /*  no error before */
		ret = check_infoelements(pc, skb, ie_STATUS);
		if (ERR_IE_COMPREHENSION == ret)
			cause = 96;
		else if (ERR_IE_UNRECOGNIZED == ret)
			cause = 99;
	}
	if (cause) {
		u_char tmp;
2330

L
Linus Torvalds 已提交
2331
		if (pc->debug & L3_DEB_WARN)
2332
			l3_debug(pc->st, "STATUS error(%d/%d)", ret, cause);
L
Linus Torvalds 已提交
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
		tmp = pc->para.cause;
		pc->para.cause = cause;
		l3ni1_status_send(pc, 0, NULL);
		if (cause == 99)
			pc->para.cause = tmp;
		else
			return;
	}
	cause = pc->para.cause;
	if (((cause & 0x7f) == 111) && (callState == 0)) {
		/* ETS 300-104 7.6.1, 8.6.1, 10.6.1...
		 * if received MT_STATUS with cause == 111 and call
		 * state == 0, then we must set down layer 3
		 */
		pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
		newl3state(pc, 0);
		ni1_release_l3_process(pc);
	}
}

static void
l3ni1_facility(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;
2358

L
Linus Torvalds 已提交
2359 2360
	ret = check_infoelements(pc, skb, ie_FACILITY);
	l3ni1_std_ie_err(pc, ret);
2361
	{
L
Linus Torvalds 已提交
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
		u_char *p;
		if ((p = findie(skb->data, skb->len, IE_FACILITY, 0)))
			l3ni1_parse_facility(pc->st, pc, pc->callref, p);
	}
}

static void
l3ni1_suspend_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
	u_char tmp[32];
	u_char *p = tmp;
	u_char i, l;
	u_char *msg = pc->chan->setup.phone;

	MsgHead(p, pc->callref, MT_SUSPEND);
	l = *msg++;
	if (l && (l <= 10)) {	/* Max length 10 octets */
		*p++ = IE_CALL_ID;
		*p++ = l;
		for (i = 0; i < l; i++)
			*p++ = *msg++;
	} else if (l) {
		l3_debug(pc->st, "SUS wrong CALL_ID len %d", l);
		return;
	}
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
	newl3state(pc, 15);
	L3AddTimer(&pc->timer, T319, CC_T319);
}

static void
l3ni1_suspend_ack(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

	L3DelTimer(&pc->timer);
	newl3state(pc, 0);
	pc->para.cause = NO_CAUSE;
	pc->st->l3.l3l4(pc->st, CC_SUSPEND | CONFIRM, pc);
	/* We don't handle suspend_ack for IE errors now */
	if ((ret = check_infoelements(pc, skb, ie_SUSPEND_ACKNOWLEDGE)))
		if (pc->debug & L3_DEB_WARN)
2410
			l3_debug(pc->st, "SUSPACK check ie(%d)", ret);
L
Linus Torvalds 已提交
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
	ni1_release_l3_process(pc);
}

static void
l3ni1_suspend_rej(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

	if ((ret = l3ni1_get_cause(pc, skb))) {
		if (pc->debug & L3_DEB_WARN)
2422 2423
			l3_debug(pc->st, "SUSP_REJ get_cause ret(%d)", ret);
		if (ret < 0)
L
Linus Torvalds 已提交
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
			pc->para.cause = 96;
		else
			pc->para.cause = 100;
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	ret = check_infoelements(pc, skb, ie_SUSPEND_REJECT);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc->st, CC_SUSPEND_ERR, pc);
	newl3state(pc, 10);
	if (ret) /* STATUS for none mandatory IE errors after actions are taken */
		l3ni1_std_ie_err(pc, ret);
}

static void
l3ni1_resume_req(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb;
	u_char tmp[32];
	u_char *p = tmp;
	u_char i, l;
	u_char *msg = pc->para.setup.phone;

	MsgHead(p, pc->callref, MT_RESUME);

	l = *msg++;
	if (l && (l <= 10)) {	/* Max length 10 octets */
		*p++ = IE_CALL_ID;
		*p++ = l;
		for (i = 0; i < l; i++)
			*p++ = *msg++;
	} else if (l) {
		l3_debug(pc->st, "RES wrong CALL_ID len %d", l);
		return;
	}
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
	newl3state(pc, 17);
	L3AddTimer(&pc->timer, T318, CC_T318);
}

static void
l3ni1_resume_ack(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int id, ret;

	if ((id = l3ni1_get_channel_id(pc, skb)) > 0) {
		if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
			if (pc->debug & L3_DEB_WARN)
				l3_debug(pc->st, "resume ack with wrong chid %x", id);
			pc->para.cause = 100;
			l3ni1_status_send(pc, pr, NULL);
			return;
		}
		pc->para.bchannel = id;
	} else if (1 == pc->state) {
		if (pc->debug & L3_DEB_WARN)
			l3_debug(pc->st, "resume ack without chid (ret %d)", id);
		pc->para.cause = 96;
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	ret = check_infoelements(pc, skb, ie_RESUME_ACKNOWLEDGE);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc->st, CC_RESUME | CONFIRM, pc);
	newl3state(pc, 10);
	if (ret) /* STATUS for none mandatory IE errors after actions are taken */
		l3ni1_std_ie_err(pc, ret);
}

static void
l3ni1_resume_rej(struct l3_process *pc, u_char pr, void *arg)
{
	struct sk_buff *skb = arg;
	int ret;

	if ((ret = l3ni1_get_cause(pc, skb))) {
		if (pc->debug & L3_DEB_WARN)
2514 2515
			l3_debug(pc->st, "RES_REJ get_cause ret(%d)", ret);
		if (ret < 0)
L
Linus Torvalds 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
			pc->para.cause = 96;
		else
			pc->para.cause = 100;
		l3ni1_status_send(pc, pr, NULL);
		return;
	}
	ret = check_infoelements(pc, skb, ie_RESUME_REJECT);
	if (ERR_IE_COMPREHENSION == ret) {
		l3ni1_std_ie_err(pc, ret);
		return;
	}
	L3DelTimer(&pc->timer);
	pc->st->l3.l3l4(pc->st, CC_RESUME_ERR, pc);
	newl3state(pc, 0);
	if (ret) /* STATUS for none mandatory IE errors after actions are taken */
		l3ni1_std_ie_err(pc, ret);
	ni1_release_l3_process(pc);
}

static void
l3ni1_global_restart(struct l3_process *pc, u_char pr, void *arg)
{
	u_char tmp[32];
	u_char *p;
	u_char ri, ch = 0, chan = 0;
	int l;
	struct sk_buff *skb = arg;
	struct l3_process *up;

	newl3state(pc, 2);
	L3DelTimer(&pc->timer);
	p = skb->data;
	if ((p = findie(p, skb->len, IE_RESTART_IND, 0))) {
		ri = p[2];
		l3_debug(pc->st, "Restart %x", ri);
	} else {
		l3_debug(pc->st, "Restart without restart IE");
		ri = 0x86;
	}
	p = skb->data;
	if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
		chan = p[2] & 3;
		ch = p[2];
		if (pc->st->l3.debug)
			l3_debug(pc->st, "Restart for channel %d", chan);
	}
	newl3state(pc, 2);
	up = pc->st->l3.proc;
	while (up) {
		if ((ri & 7) == 7)
			up->st->lli.l4l3(up->st, CC_RESTART | REQUEST, up);
		else if (up->para.bchannel == chan)
			up->st->lli.l4l3(up->st, CC_RESTART | REQUEST, up);
2569

L
Linus Torvalds 已提交
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
		up = up->next;
	}
	p = tmp;
	MsgHead(p, pc->callref, MT_RESTART_ACKNOWLEDGE);
	if (chan) {
		*p++ = IE_CHANNEL_ID;
		*p++ = 1;
		*p++ = ch | 0x80;
	}
	*p++ = 0x79;		/* RESTART Ind */
	*p++ = 1;
	*p++ = ri;
	l = p - tmp;
	if (!(skb = l3_alloc_skb(l)))
		return;
	memcpy(skb_put(skb, l), tmp, l);
	newl3state(pc, 0);
	l3_msg(pc->st, DL_DATA | REQUEST, skb);
}

static void
l3ni1_dl_reset(struct l3_process *pc, u_char pr, void *arg)
{
2593 2594 2595 2596
	pc->para.cause = 0x29;          /* Temporary failure */
	pc->para.loc = 0;
	l3ni1_disconnect_req(pc, pr, NULL);
	pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601
}

static void
l3ni1_dl_release(struct l3_process *pc, u_char pr, void *arg)
{
2602 2603 2604 2605 2606
	newl3state(pc, 0);
	pc->para.cause = 0x1b;          /* Destination out of order */
	pc->para.loc = 0;
	pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
	release_l3_process(pc);
L
Linus Torvalds 已提交
2607 2608 2609 2610 2611
}

static void
l3ni1_dl_reestablish(struct l3_process *pc, u_char pr, void *arg)
{
2612 2613 2614
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, T309, CC_T309);
	l3_msg(pc->st, DL_ESTABLISH | REQUEST, NULL);
L
Linus Torvalds 已提交
2615
}
2616

L
Linus Torvalds 已提交
2617 2618 2619 2620
static void
l3ni1_dl_reest_status(struct l3_process *pc, u_char pr, void *arg)
{
	L3DelTimer(&pc->timer);
2621 2622

	pc->para.cause = 0x1F; /* normal, unspecified */
L
Linus Torvalds 已提交
2623 2624 2625
	l3ni1_status_send(pc, 0, NULL);
}

2626
static void l3ni1_SendSpid(struct l3_process *pc, u_char pr, struct sk_buff *skb, int iNewState)
L
Linus Torvalds 已提交
2627
{
2628 2629 2630 2631
	u_char *p;
	char *pSPID;
	struct Channel *pChan = pc->st->lli.userdata;
	int l;
L
Linus Torvalds 已提交
2632

2633 2634
	if (skb)
		dev_kfree_skb(skb);
L
Linus Torvalds 已提交
2635

2636
	if (!(pSPID = strchr(pChan->setup.eazmsn, ':')))
L
Linus Torvalds 已提交
2637
	{
2638 2639 2640
		printk(KERN_ERR "SPID not supplied in EAZMSN %s\n", pChan->setup.eazmsn);
		newl3state(pc, 0);
		pc->st->l3.l3l2(pc->st, DL_RELEASE | REQUEST, NULL);
L
Linus Torvalds 已提交
2641 2642 2643
		return;
	}

2644 2645
	l = strlen(++pSPID);
	if (!(skb = l3_alloc_skb(5 + l)))
L
Linus Torvalds 已提交
2646
	{
2647
		printk(KERN_ERR "HiSax can't get memory to send SPID\n");
L
Linus Torvalds 已提交
2648 2649 2650
		return;
	}

2651
	p = skb_put(skb, 5);
L
Linus Torvalds 已提交
2652 2653 2654 2655 2656 2657
	*p++ = PROTO_DIS_EURO;
	*p++ = 0;
	*p++ = MT_INFORMATION;
	*p++ = IE_SPID;
	*p++ = l;

2658
	memcpy(skb_put(skb, l), pSPID, l);
L
Linus Torvalds 已提交
2659

2660
	newl3state(pc, iNewState);
L
Linus Torvalds 已提交
2661

2662 2663
	L3DelTimer(&pc->timer);
	L3AddTimer(&pc->timer, TSPID, CC_TSPID);
L
Linus Torvalds 已提交
2664

2665
	pc->st->l3.l3l2(pc->st, DL_DATA | REQUEST, skb);
L
Linus Torvalds 已提交
2666 2667
}

2668
static void l3ni1_spid_send(struct l3_process *pc, u_char pr, void *arg)
L
Linus Torvalds 已提交
2669
{
2670
	l3ni1_SendSpid(pc, pr, arg, 20);
L
Linus Torvalds 已提交
2671 2672
}

2673
static void l3ni1_spid_epid(struct l3_process *pc, u_char pr, void *arg)
L
Linus Torvalds 已提交
2674 2675 2676
{
	struct sk_buff *skb = arg;

2677 2678
	if (skb->data[1] == 0)
		if (skb->data[3] == IE_ENDPOINT_ID)
L
Linus Torvalds 已提交
2679
		{
2680 2681 2682
			L3DelTimer(&pc->timer);
			newl3state(pc, 0);
			l3_msg(pc->st, DL_ESTABLISH | CONFIRM, NULL);
L
Linus Torvalds 已提交
2683
		}
2684
	dev_kfree_skb(skb);
L
Linus Torvalds 已提交
2685 2686
}

2687
static void l3ni1_spid_tout(struct l3_process *pc, u_char pr, void *arg)
L
Linus Torvalds 已提交
2688
{
2689 2690
	if (pc->state < 22)
		l3ni1_SendSpid(pc, pr, arg, pc->state + 1);
L
Linus Torvalds 已提交
2691 2692
	else
	{
2693 2694
		L3DelTimer(&pc->timer);
		dev_kfree_skb(arg);
L
Linus Torvalds 已提交
2695

2696 2697 2698
		printk(KERN_ERR "SPID not accepted\n");
		newl3state(pc, 0);
		pc->st->l3.l3l2(pc->st, DL_RELEASE | REQUEST, NULL);
L
Linus Torvalds 已提交
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
	}
}

/* *INDENT-OFF* */
static struct stateentry downstatelist[] =
{
	{SBIT(0),
	 CC_SETUP | REQUEST, l3ni1_setup_req},
	{SBIT(0),
	 CC_RESUME | REQUEST, l3ni1_resume_req},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(6) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(25),
	 CC_DISCONNECT | REQUEST, l3ni1_disconnect_req},
	{SBIT(12),
	 CC_RELEASE | REQUEST, l3ni1_release_req},
	{ALL_STATES,
	 CC_RESTART | REQUEST, l3ni1_restart},
	{SBIT(6) | SBIT(25),
	 CC_IGNORE | REQUEST, l3ni1_reset},
	{SBIT(6) | SBIT(25),
	 CC_REJECT | REQUEST, l3ni1_reject_req},
	{SBIT(6) | SBIT(25),
	 CC_PROCEED_SEND | REQUEST, l3ni1_proceed_req},
	{SBIT(6),
	 CC_MORE_INFO | REQUEST, l3ni1_setup_ack_req},
	{SBIT(25),
	 CC_MORE_INFO | REQUEST, l3ni1_dummy},
	{SBIT(6) | SBIT(9) | SBIT(25),
	 CC_ALERTING | REQUEST, l3ni1_alert_req},
	{SBIT(6) | SBIT(7) | SBIT(9) | SBIT(25),
	 CC_SETUP | RESPONSE, l3ni1_setup_rsp},
	{SBIT(10),
	 CC_SUSPEND | REQUEST, l3ni1_suspend_req},
2731 2732 2733 2734 2735 2736
	{SBIT(7) | SBIT(9) | SBIT(25),
	 CC_REDIR | REQUEST, l3ni1_redir_req},
	{SBIT(6),
	 CC_REDIR | REQUEST, l3ni1_redir_req_early},
	{SBIT(9) | SBIT(25),
	 CC_DISCONNECT | REQUEST, l3ni1_disconnect_req},
L
Linus Torvalds 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
	{SBIT(25),
	 CC_T302, l3ni1_t302},
	{SBIT(1),
	 CC_T303, l3ni1_t303},
	{SBIT(2),
	 CC_T304, l3ni1_t304},
	{SBIT(3),
	 CC_T310, l3ni1_t310},
	{SBIT(8),
	 CC_T313, l3ni1_t313},
	{SBIT(11),
	 CC_T305, l3ni1_t305},
	{SBIT(15),
	 CC_T319, l3ni1_t319},
	{SBIT(17),
	 CC_T318, l3ni1_t318},
	{SBIT(19),
	 CC_T308_1, l3ni1_t308_1},
	{SBIT(19),
	 CC_T308_2, l3ni1_t308_2},
	{SBIT(10),
	 CC_T309, l3ni1_dl_release},
2759 2760
	{ SBIT(20) | SBIT(21) | SBIT(22),
	  CC_TSPID, l3ni1_spid_tout },
L
Linus Torvalds 已提交
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
};

static struct stateentry datastatelist[] =
{
	{ALL_STATES,
	 MT_STATUS_ENQUIRY, l3ni1_status_enq},
	{ALL_STATES,
	 MT_FACILITY, l3ni1_facility},
	{SBIT(19),
	 MT_STATUS, l3ni1_release_ind},
	{ALL_STATES,
	 MT_STATUS, l3ni1_status},
	{SBIT(0),
	 MT_SETUP, l3ni1_setup},
	{SBIT(6) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(11) | SBIT(12) |
	 SBIT(15) | SBIT(17) | SBIT(19) | SBIT(25),
	 MT_SETUP, l3ni1_dummy},
	{SBIT(1) | SBIT(2),
	 MT_CALL_PROCEEDING, l3ni1_call_proc},
	{SBIT(1),
	 MT_SETUP_ACKNOWLEDGE, l3ni1_setup_ack},
	{SBIT(2) | SBIT(3),
	 MT_ALERTING, l3ni1_alerting},
	{SBIT(2) | SBIT(3),
	 MT_PROGRESS, l3ni1_progress},
	{SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) |
	 SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(19) | SBIT(25),
	 MT_INFORMATION, l3ni1_information},
	{SBIT(10) | SBIT(11) | SBIT(15),
	 MT_NOTIFY, l3ni1_notify},
	{SBIT(0) | SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(10) |
	 SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(19) | SBIT(25),
	 MT_RELEASE_COMPLETE, l3ni1_release_cmpl},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(25),
	 MT_RELEASE, l3ni1_release},
	{SBIT(19),  MT_RELEASE, l3ni1_release_ind},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(11) | SBIT(15) | SBIT(17) | SBIT(25),
	 MT_DISCONNECT, l3ni1_disconnect},
	{SBIT(19),
	 MT_DISCONNECT, l3ni1_dummy},
	{SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4),
	 MT_CONNECT, l3ni1_connect},
	{SBIT(8),
	 MT_CONNECT_ACKNOWLEDGE, l3ni1_connect_ack},
	{SBIT(15),
	 MT_SUSPEND_ACKNOWLEDGE, l3ni1_suspend_ack},
	{SBIT(15),
	 MT_SUSPEND_REJECT, l3ni1_suspend_rej},
	{SBIT(17),
	 MT_RESUME_ACKNOWLEDGE, l3ni1_resume_ack},
	{SBIT(17),
	 MT_RESUME_REJECT, l3ni1_resume_rej},
};

static struct stateentry globalmes_list[] =
{
	{ALL_STATES,
	 MT_STATUS, l3ni1_status},
	{SBIT(0),
	 MT_RESTART, l3ni1_global_restart},
/*	{SBIT(1),
2822
	MT_RESTART_ACKNOWLEDGE, l3ni1_restart_ack},
L
Linus Torvalds 已提交
2823
*/
2824 2825
	{ SBIT(0), MT_DL_ESTABLISHED, l3ni1_spid_send },
	{ SBIT(20) | SBIT(21) | SBIT(22), MT_INFORMATION, l3ni1_spid_epid },
L
Linus Torvalds 已提交
2826 2827 2828 2829
};

static struct stateentry manstatelist[] =
{
2830 2831 2832 2833 2834 2835 2836 2837
	{SBIT(2),
	 DL_ESTABLISH | INDICATION, l3ni1_dl_reset},
	{SBIT(10),
	 DL_ESTABLISH | CONFIRM, l3ni1_dl_reest_status},
	{SBIT(10),
	 DL_RELEASE | INDICATION, l3ni1_dl_reestablish},
	{ALL_STATES,
	 DL_RELEASE | INDICATION, l3ni1_dl_release},
L
Linus Torvalds 已提交
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
};

/* *INDENT-ON* */


static void
global_handler(struct PStack *st, int mt, struct sk_buff *skb)
{
	u_char tmp[16];
	u_char *p = tmp;
	int l;
	int i;
	struct l3_process *proc = st->l3.global;

2852
	if (skb)
L
Linus Torvalds 已提交
2853 2854 2855
		proc->callref = skb->data[2]; /* cr flag */
	else
		proc->callref = 0;
K
Karsten Keil 已提交
2856
	for (i = 0; i < ARRAY_SIZE(globalmes_list); i++)
L
Linus Torvalds 已提交
2857 2858 2859
		if ((mt == globalmes_list[i].primitive) &&
		    ((1 << proc->state) & globalmes_list[i].state))
			break;
K
Karsten Keil 已提交
2860
	if (i == ARRAY_SIZE(globalmes_list)) {
L
Linus Torvalds 已提交
2861 2862
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "ni1 global state %d mt %x unhandled",
2863
				 proc->state, mt);
L
Linus Torvalds 已提交
2864 2865 2866 2867 2868
		}
		MsgHead(p, proc->callref, MT_STATUS);
		*p++ = IE_CAUSE;
		*p++ = 0x2;
		*p++ = 0x80;
2869
		*p++ = 81 | 0x80;	/* invalid cr */
L
Linus Torvalds 已提交
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
		*p++ = 0x14;		/* CallState */
		*p++ = 0x1;
		*p++ = proc->state & 0x3f;
		l = p - tmp;
		if (!(skb = l3_alloc_skb(l)))
			return;
		memcpy(skb_put(skb, l), tmp, l);
		l3_msg(proc->st, DL_DATA | REQUEST, skb);
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "ni1 global %d mt %x",
2881
				 proc->state, mt);
L
Linus Torvalds 已提交
2882 2883 2884 2885 2886 2887 2888 2889
		}
		globalmes_list[i].rout(proc, mt, skb);
	}
}

static void
ni1up(struct PStack *st, int pr, void *arg)
{
2890
	int i, mt, cr, callState;
L
Linus Torvalds 已提交
2891 2892 2893 2894 2895 2896
	char *ptr;
	u_char *p;
	struct sk_buff *skb = arg;
	struct l3_process *proc;

	switch (pr) {
2897 2898 2899 2900 2901 2902 2903 2904 2905
	case (DL_DATA | INDICATION):
	case (DL_UNIT_DATA | INDICATION):
		break;
	case (DL_ESTABLISH | INDICATION):
	case (DL_RELEASE | INDICATION):
	case (DL_RELEASE | CONFIRM):
		l3_msg(st, pr, arg);
		return;
		break;
L
Linus Torvalds 已提交
2906

2907 2908 2909
	case (DL_ESTABLISH | CONFIRM):
		global_handler(st, MT_DL_ESTABLISHED, NULL);
		return;
L
Linus Torvalds 已提交
2910

2911 2912 2913
	default:
		printk(KERN_ERR "HiSax ni1up unknown pr=%04x\n", pr);
		return;
L
Linus Torvalds 已提交
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
	}
	if (skb->len < 3) {
		l3_debug(st, "ni1up frame too short(%d)", skb->len);
		dev_kfree_skb(skb);
		return;
	}

	if (skb->data[0] != PROTO_DIS_EURO) {
		if (st->l3.debug & L3_DEB_PROTERR) {
			l3_debug(st, "ni1up%sunexpected discriminator %x message len %d",
				 (pr == (DL_DATA | INDICATION)) ? " " : "(broadcast) ",
				 skb->data[0], skb->len);
		}
		dev_kfree_skb(skb);
		return;
	}
	cr = getcallref(skb->data);
	if (skb->len < ((skb->data[1] & 0x0f) + 3)) {
		l3_debug(st, "ni1up frame too short(%d)", skb->len);
		dev_kfree_skb(skb);
		return;
	}
	mt = skb->data[skb->data[1] + 2];
	if (st->l3.debug & L3_DEB_STATE)
		l3_debug(st, "ni1up cr %d", cr);
	if (cr == -2) {  /* wrong Callref */
		if (st->l3.debug & L3_DEB_WARN)
			l3_debug(st, "ni1up wrong Callref");
		dev_kfree_skb(skb);
		return;
	} else if (cr == -1) {	/* Dummy Callref */
		if (mt == MT_FACILITY)
		{
			if ((p = findie(skb->data, skb->len, IE_FACILITY, 0))) {
2948 2949
				l3ni1_parse_facility(st, NULL,
						     (pr == (DL_DATA | INDICATION)) ? -1 : -2, p);
L
Linus Torvalds 已提交
2950
				dev_kfree_skb(skb);
2951
				return;
L
Linus Torvalds 已提交
2952 2953 2954 2955 2956 2957 2958
			}
		}
		else
		{
			global_handler(st, mt, skb);
			return;
		}
2959

L
Linus Torvalds 已提交
2960 2961 2962 2963
		if (st->l3.debug & L3_DEB_WARN)
			l3_debug(st, "ni1up dummy Callref (no facility msg or ie)");
		dev_kfree_skb(skb);
		return;
2964 2965
	} else if ((((skb->data[1] & 0x0f) == 1) && (0 == (cr & 0x7f))) ||
		   (((skb->data[1] & 0x0f) == 2) && (0 == (cr & 0x7fff)))) {	/* Global CallRef */
L
Linus Torvalds 已提交
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
		if (st->l3.debug & L3_DEB_STATE)
			l3_debug(st, "ni1up Global CallRef");
		global_handler(st, mt, skb);
		dev_kfree_skb(skb);
		return;
	} else if (!(proc = getl3proc(st, cr))) {
		/* No transaction process exist, that means no call with
		 * this callreference is active
		 */
		if (mt == MT_SETUP) {
			/* Setup creates a new transaction process */
			if (skb->data[2] & 0x80) {
				/* Setup with wrong CREF flag */
				if (st->l3.debug & L3_DEB_STATE)
					l3_debug(st, "ni1up wrong CRef flag");
				dev_kfree_skb(skb);
				return;
			}
			if (!(proc = ni1_new_l3_process(st, cr))) {
				/* May be to answer with RELEASE_COMPLETE and
				 * CAUSE 0x2f "Resource unavailable", but this
				 * need a new_l3_process too ... arghh
				 */
				dev_kfree_skb(skb);
				return;
			}
		} else if (mt == MT_STATUS) {
			if ((ptr = findie(skb->data, skb->len, IE_CAUSE, 0)) != NULL) {
				ptr++;
				if (*ptr++ == 2)
					ptr++;
			}
			callState = 0;
			if ((ptr = findie(skb->data, skb->len, IE_CALL_STATE, 0)) != NULL) {
				ptr++;
				if (*ptr++ == 2)
					ptr++;
				callState = *ptr;
			}
			/* ETS 300-104 part 2.4.1
			 * if setup has not been made and a message type
			 * MT_STATUS is received with call state == 0,
			 * we must send nothing
			 */
			if (callState != 0) {
				/* ETS 300-104 part 2.4.2
				 * if setup has not been made and a message type
				 * MT_STATUS is received with call state != 0,
				 * we must send MT_RELEASE_COMPLETE cause 101
				 */
				if ((proc = ni1_new_l3_process(st, cr))) {
					proc->para.cause = 101;
					l3ni1_msg_without_setup(proc, 0, NULL);
				}
			}
			dev_kfree_skb(skb);
			return;
		} else if (mt == MT_RELEASE_COMPLETE) {
			dev_kfree_skb(skb);
			return;
		} else {
			/* ETS 300-104 part 2
			 * if setup has not been made and a message type
			 * (except MT_SETUP and RELEASE_COMPLETE) is received,
			 * we must send MT_RELEASE_COMPLETE cause 81 */
			dev_kfree_skb(skb);
			if ((proc = ni1_new_l3_process(st, cr))) {
				proc->para.cause = 81;
				l3ni1_msg_without_setup(proc, 0, NULL);
			}
			return;
		}
	}
	if (l3ni1_check_messagetype_validity(proc, mt, skb)) {
		dev_kfree_skb(skb);
		return;
	}
3043 3044
	if ((p = findie(skb->data, skb->len, IE_DISPLAY, 0)) != NULL)
		l3ni1_deliver_display(proc, pr, p); /* Display IE included */
K
Karsten Keil 已提交
3045
	for (i = 0; i < ARRAY_SIZE(datastatelist); i++)
L
Linus Torvalds 已提交
3046 3047 3048
		if ((mt == datastatelist[i].primitive) &&
		    ((1 << proc->state) & datastatelist[i].state))
			break;
K
Karsten Keil 已提交
3049
	if (i == ARRAY_SIZE(datastatelist)) {
L
Linus Torvalds 已提交
3050 3051
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "ni1up%sstate %d mt %#x unhandled",
3052 3053
				 (pr == (DL_DATA | INDICATION)) ? " " : "(broadcast) ",
				 proc->state, mt);
L
Linus Torvalds 已提交
3054 3055 3056 3057 3058 3059 3060 3061
		}
		if ((MT_RELEASE_COMPLETE != mt) && (MT_RELEASE != mt)) {
			proc->para.cause = 101;
			l3ni1_status_send(proc, pr, skb);
		}
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "ni1up%sstate %d mt %x",
3062 3063
				 (pr == (DL_DATA | INDICATION)) ? " " : "(broadcast) ",
				 proc->state, mt);
L
Linus Torvalds 已提交
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
		}
		datastatelist[i].rout(proc, pr, skb);
	}
	dev_kfree_skb(skb);
	return;
}

static void
ni1down(struct PStack *st, int pr, void *arg)
{
	int i, cr;
	struct l3_process *proc;
	struct Channel *chan;

	if ((DL_ESTABLISH | REQUEST) == pr) {
		l3_msg(st, pr, NULL);
		return;
	} else if (((CC_SETUP | REQUEST) == pr) || ((CC_RESUME | REQUEST) == pr)) {
		chan = arg;
		cr = newcallref();
		cr |= 0x80;
		if ((proc = ni1_new_l3_process(st, cr))) {
			proc->chan = chan;
			chan->proc = proc;
			memcpy(&proc->para.setup, &chan->setup, sizeof(setup_parm));
			proc->callref = cr;
		}
	} else {
		proc = arg;
	}
	if (!proc) {
		printk(KERN_ERR "HiSax ni1down without proc pr=%04x\n", pr);
		return;
	}

3099 3100
	if (pr == (CC_TNI1_IO | REQUEST)) {
		l3ni1_io_timer(proc); /* timer expires */
L
Linus Torvalds 已提交
3101
		return;
3102
	}
L
Linus Torvalds 已提交
3103

K
Karsten Keil 已提交
3104
	for (i = 0; i < ARRAY_SIZE(downstatelist); i++)
L
Linus Torvalds 已提交
3105 3106 3107
		if ((pr == downstatelist[i].primitive) &&
		    ((1 << proc->state) & downstatelist[i].state))
			break;
K
Karsten Keil 已提交
3108
	if (i == ARRAY_SIZE(downstatelist)) {
L
Linus Torvalds 已提交
3109 3110
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "ni1down state %d prim %#x unhandled",
3111
				 proc->state, pr);
L
Linus Torvalds 已提交
3112 3113 3114 3115
		}
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "ni1down state %d prim %#x",
3116
				 proc->state, pr);
L
Linus Torvalds 已提交
3117 3118 3119 3120 3121 3122 3123 3124
		}
		downstatelist[i].rout(proc, pr, arg);
	}
}

static void
ni1man(struct PStack *st, int pr, void *arg)
{
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
	int i;
	struct l3_process *proc = arg;

	if (!proc) {
		printk(KERN_ERR "HiSax ni1man without proc pr=%04x\n", pr);
		return;
	}
	for (i = 0; i < ARRAY_SIZE(manstatelist); i++)
		if ((pr == manstatelist[i].primitive) &&
		    ((1 << proc->state) & manstatelist[i].state))
			break;
	if (i == ARRAY_SIZE(manstatelist)) {
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "cr %d ni1man state %d prim %#x unhandled",
				 proc->callref & 0x7f, proc->state, pr);
		}
	} else {
		if (st->l3.debug & L3_DEB_STATE) {
			l3_debug(st, "cr %d ni1man state %d prim %#x",
				 proc->callref & 0x7f, proc->state, pr);
		}
		manstatelist[i].rout(proc, pr, arg);
	}
}

L
Linus Torvalds 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
void
setstack_ni1(struct PStack *st)
{
	char tmp[64];
	int i;

	st->lli.l4l3 = ni1down;
	st->lli.l4l3_proto = l3ni1_cmd_global;
	st->l2.l2l3 = ni1up;
	st->l3.l3ml3 = ni1man;
	st->l3.N303 = 1;
	st->prot.ni1.last_invoke_id = 0;
	st->prot.ni1.invoke_used[0] = 1; /* Bit 0 must always be set to 1 */
	i = 1;
3164 3165
	while (i < 32)
		st->prot.ni1.invoke_used[i++] = 0;
L
Linus Torvalds 已提交
3166 3167 3168 3169 3170 3171 3172 3173 3174 3175

	if (!(st->l3.global = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
		printk(KERN_ERR "HiSax can't get memory for ni1 global CR\n");
	} else {
		st->l3.global->state = 0;
		st->l3.global->callref = 0;
		st->l3.global->next = NULL;
		st->l3.global->debug = L3_DEB_WARN;
		st->l3.global->st = st;
		st->l3.global->N303 = 1;
3176
		st->l3.global->prot.ni1.invoke_id = 0;
L
Linus Torvalds 已提交
3177 3178 3179 3180 3181 3182

		L3InitTimer(st->l3.global, &st->l3.global->timer);
	}
	strcpy(tmp, ni1_revision);
	printk(KERN_INFO "HiSax: National ISDN-1 Rev. %s\n", HiSax_getrev(tmp));
}