1 /*
2  * libpri: An implementation of Primary Rate ISDN
3  *
4  * Written by Matthew Fredrickson <creslin@digium.com>
5  *
6  * Copyright (C) 2004-2005, Digium, Inc.
7  * All Rights Reserved.
8  */
9 
10 /*
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2 as published by the
19  * Free Software Foundation. See the LICENSE file included with
20  * this program for more details.
21  *
22  * In addition, when this program is distributed with Asterisk in
23  * any form that would qualify as a 'combined work' or as a
24  * 'derivative work' (but not mere aggregation), you can redistribute
25  * and/or modify the combination under the terms of the license
26  * provided with that copy of Asterisk, instead of the license
27  * terms granted here.
28  */
29 
30 #include "compat.h"
31 #include "libpri.h"
32 #include "pri_internal.h"
33 #include "pri_facility.h"
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <limits.h>
39 
pri_facility_error2str(int facility_error_code)40 const char *pri_facility_error2str(int facility_error_code)
41 {
42 	return rose_error2str(facility_error_code);
43 }
44 
pri_facility_reject2str(int facility_reject_code)45 const char *pri_facility_reject2str(int facility_reject_code)
46 {
47 	return rose_reject2str(facility_reject_code);
48 }
49 
redirectingreason_from_q931(struct pri * ctrl,int redirectingreason)50 static int redirectingreason_from_q931(struct pri *ctrl, int redirectingreason)
51 {
52 	int value;
53 
54 	switch (ctrl->switchtype) {
55 	case PRI_SWITCH_QSIG:
56 		switch (redirectingreason) {
57 		case PRI_REDIR_UNKNOWN:
58 			value = QSIG_DIVERT_REASON_UNKNOWN;
59 			break;
60 		case PRI_REDIR_FORWARD_ON_BUSY:
61 			value = QSIG_DIVERT_REASON_CFB;
62 			break;
63 		case PRI_REDIR_FORWARD_ON_NO_REPLY:
64 			value = QSIG_DIVERT_REASON_CFNR;
65 			break;
66 		case PRI_REDIR_UNCONDITIONAL:
67 			value = QSIG_DIVERT_REASON_CFU;
68 			break;
69 		case PRI_REDIR_DEFLECTION:
70 		case PRI_REDIR_DTE_OUT_OF_ORDER:
71 		case PRI_REDIR_FORWARDED_BY_DTE:
72 			pri_message(ctrl,
73 				"!! Don't know how to convert Q.931 redirection reason %d to Q.SIG\n",
74 				redirectingreason);
75 			/* Fall through */
76 		default:
77 			value = QSIG_DIVERT_REASON_UNKNOWN;
78 			break;
79 		}
80 		break;
81 	default:
82 		switch (redirectingreason) {
83 		case PRI_REDIR_UNKNOWN:
84 			value = Q952_DIVERT_REASON_UNKNOWN;
85 			break;
86 		case PRI_REDIR_FORWARD_ON_BUSY:
87 			value = Q952_DIVERT_REASON_CFB;
88 			break;
89 		case PRI_REDIR_FORWARD_ON_NO_REPLY:
90 			value = Q952_DIVERT_REASON_CFNR;
91 			break;
92 		case PRI_REDIR_DEFLECTION:
93 			value = Q952_DIVERT_REASON_CD;
94 			break;
95 		case PRI_REDIR_UNCONDITIONAL:
96 			value = Q952_DIVERT_REASON_CFU;
97 			break;
98 		case PRI_REDIR_DTE_OUT_OF_ORDER:
99 		case PRI_REDIR_FORWARDED_BY_DTE:
100 			pri_message(ctrl,
101 				"!! Don't know how to convert Q.931 redirection reason %d to Q.952\n",
102 				redirectingreason);
103 			/* Fall through */
104 		default:
105 			value = Q952_DIVERT_REASON_UNKNOWN;
106 			break;
107 		}
108 		break;
109 	}
110 
111 	return value;
112 }
113 
redirectingreason_for_q931(struct pri * ctrl,int redirectingreason)114 static int redirectingreason_for_q931(struct pri *ctrl, int redirectingreason)
115 {
116 	int value;
117 
118 	switch (ctrl->switchtype) {
119 	case PRI_SWITCH_QSIG:
120 		switch (redirectingreason) {
121 		case QSIG_DIVERT_REASON_UNKNOWN:
122 			value = PRI_REDIR_UNKNOWN;
123 			break;
124 		case QSIG_DIVERT_REASON_CFU:
125 			value = PRI_REDIR_UNCONDITIONAL;
126 			break;
127 		case QSIG_DIVERT_REASON_CFB:
128 			value = PRI_REDIR_FORWARD_ON_BUSY;
129 			break;
130 		case QSIG_DIVERT_REASON_CFNR:
131 			value = PRI_REDIR_FORWARD_ON_NO_REPLY;
132 			break;
133 		default:
134 			pri_message(ctrl, "!! Unknown Q.SIG diversion reason %d\n",
135 				redirectingreason);
136 			value = PRI_REDIR_UNKNOWN;
137 			break;
138 		}
139 		break;
140 	default:
141 		switch (redirectingreason) {
142 		case Q952_DIVERT_REASON_UNKNOWN:
143 			value = PRI_REDIR_UNKNOWN;
144 			break;
145 		case Q952_DIVERT_REASON_CFU:
146 			value = PRI_REDIR_UNCONDITIONAL;
147 			break;
148 		case Q952_DIVERT_REASON_CFB:
149 			value = PRI_REDIR_FORWARD_ON_BUSY;
150 			break;
151 		case Q952_DIVERT_REASON_CFNR:
152 			value = PRI_REDIR_FORWARD_ON_NO_REPLY;
153 			break;
154 		case Q952_DIVERT_REASON_CD:
155 			value = PRI_REDIR_DEFLECTION;
156 			break;
157 		case Q952_DIVERT_REASON_IMMEDIATE:
158 			pri_message(ctrl,
159 				"!! Dont' know how to convert Q.952 diversion reason IMMEDIATE to PRI analog\n");
160 			value = PRI_REDIR_UNKNOWN;	/* ??? */
161 			break;
162 		default:
163 			pri_message(ctrl, "!! Unknown Q.952 diversion reason %d\n",
164 				redirectingreason);
165 			value = PRI_REDIR_UNKNOWN;
166 			break;
167 		}
168 		break;
169 	}
170 
171 	return value;
172 }
173 
174 /*!
175  * \internal
176  * \brief Convert the Q.931 type-of-number field to facility.
177  *
178  * \param ctrl D channel controller for diagnostic messages or global options.
179  * \param ton Q.931 ton/plan octet.
180  *
181  * \return PartyNumber enumeration value.
182  */
typeofnumber_from_q931(struct pri * ctrl,int ton)183 static int typeofnumber_from_q931(struct pri *ctrl, int ton)
184 {
185 	int value;
186 
187 	switch ((ton >> 4) & 0x03) {
188 	default:
189 		pri_message(ctrl, "!! Unsupported Q.931 TypeOfNumber value (%d)\n", ton);
190 		/* fall through */
191 	case PRI_TON_UNKNOWN:
192 		value = Q932_TON_UNKNOWN;
193 		break;
194 	case PRI_TON_INTERNATIONAL:
195 		value = Q932_TON_INTERNATIONAL;
196 		break;
197 	case PRI_TON_NATIONAL:
198 		value = Q932_TON_NATIONAL;
199 		break;
200 	case PRI_TON_NET_SPECIFIC:
201 		value = Q932_TON_NET_SPECIFIC;
202 		break;
203 	case PRI_TON_SUBSCRIBER:
204 		value = Q932_TON_SUBSCRIBER;
205 		break;
206 	case PRI_TON_ABBREVIATED:
207 		value = Q932_TON_ABBREVIATED;
208 		break;
209 	}
210 
211 	return value;
212 }
213 
typeofnumber_for_q931(struct pri * ctrl,int ton)214 static int typeofnumber_for_q931(struct pri *ctrl, int ton)
215 {
216 	int value;
217 
218 	switch (ton) {
219 	default:
220 		pri_message(ctrl, "!! Invalid TypeOfNumber %d\n", ton);
221 		/* fall through */
222 	case Q932_TON_UNKNOWN:
223 		value = PRI_TON_UNKNOWN;
224 		break;
225 	case Q932_TON_INTERNATIONAL:
226 		value = PRI_TON_INTERNATIONAL;
227 		break;
228 	case Q932_TON_NATIONAL:
229 		value = PRI_TON_NATIONAL;
230 		break;
231 	case Q932_TON_NET_SPECIFIC:
232 		value = PRI_TON_NET_SPECIFIC;
233 		break;
234 	case Q932_TON_SUBSCRIBER:
235 		value = PRI_TON_SUBSCRIBER;
236 		break;
237 	case Q932_TON_ABBREVIATED:
238 		value = PRI_TON_ABBREVIATED;
239 		break;
240 	}
241 
242 	return value << 4;
243 }
244 
245 /*!
246  * \internal
247  * \brief Convert the Q.931 numbering plan field to facility.
248  *
249  * \param ctrl D channel controller for diagnostic messages or global options.
250  * \param plan Q.931 ton/plan octet.
251  *
252  * \return PartyNumber enumeration value.
253  */
numbering_plan_from_q931(struct pri * ctrl,int plan)254 static int numbering_plan_from_q931(struct pri *ctrl, int plan)
255 {
256 	int value;
257 
258 	switch (plan & 0x0F) {
259 	default:
260 		pri_message(ctrl, "!! Unsupported Q.931 numbering plan value (%d)\n", plan);
261 		/* fall through */
262 	case PRI_NPI_UNKNOWN:
263 		value = 0;	/* unknown */
264 		break;
265 	case PRI_NPI_E163_E164:
266 		value = 1;	/* public */
267 		break;
268 	case PRI_NPI_X121:
269 		value = 3;	/* data */
270 		break;
271 	case PRI_NPI_F69:
272 		value = 4;	/* telex */
273 		break;
274 	case PRI_NPI_NATIONAL:
275 		value = 8;	/* nationalStandard */
276 		break;
277 	case PRI_NPI_PRIVATE:
278 		value = 5;	/* private */
279 		break;
280 	}
281 
282 	return value;
283 }
284 
285 /*!
286  * \internal
287  * \brief Convert the PartyNumber numbering plan to Q.931 plan field value.
288  *
289  * \param ctrl D channel controller for diagnostic messages or global options.
290  * \param plan PartyNumber enumeration value.
291  *
292  * \return Q.931 plan field value.
293  */
numbering_plan_for_q931(struct pri * ctrl,int plan)294 static int numbering_plan_for_q931(struct pri *ctrl, int plan)
295 {
296 	int value;
297 
298 	switch (plan) {
299 	default:
300 		pri_message(ctrl,
301 			"!! Unsupported PartyNumber to Q.931 numbering plan value (%d)\n", plan);
302 		/* fall through */
303 	case 0:	/* unknown */
304 		value = PRI_NPI_UNKNOWN;
305 		break;
306 	case 1:	/* public */
307 		value = PRI_NPI_E163_E164;
308 		break;
309 	case 3:	/* data */
310 		value = PRI_NPI_X121;
311 		break;
312 	case 4:	/* telex */
313 		value = PRI_NPI_F69;
314 		break;
315 	case 5:	/* private */
316 		value = PRI_NPI_PRIVATE;
317 		break;
318 	case 8:	/* nationalStandard */
319 		value = PRI_NPI_NATIONAL;
320 		break;
321 	}
322 
323 	return value;
324 }
325 
326 /*!
327  * \internal
328  * \brief Convert the Q.931 number presentation field to facility.
329  *
330  * \param ctrl D channel controller for diagnostic messages or global options.
331  * \param presentation Q.931 presentation/screening octet.
332  * \param number_present Non-zero if the number is available.
333  *
334  * \return Presented<Number/Address><Screened/Unscreened> enumeration value.
335  */
presentation_from_q931(struct pri * ctrl,int presentation,int number_present)336 static int presentation_from_q931(struct pri *ctrl, int presentation, int number_present)
337 {
338 	int value;
339 
340 	switch (presentation & PRI_PRES_RESTRICTION) {
341 	case PRI_PRES_ALLOWED:
342 		value = 0;	/* presentationAllowed<Number/Address> */
343 		break;
344 	default:
345 		pri_message(ctrl, "!! Unsupported Q.931 number presentation value (%d)\n",
346 			presentation);
347 		/* fall through */
348 	case PRI_PRES_RESTRICTED:
349 		if (number_present) {
350 			value = 3;	/* presentationRestricted<Number/Address> */
351 		} else {
352 			value = 1;	/* presentationRestricted */
353 		}
354 		break;
355 	case PRI_PRES_UNAVAILABLE:
356 		value = 2;	/* numberNotAvailableDueToInterworking */
357 		break;
358 	}
359 
360 	return value;
361 }
362 
363 /*!
364  * \internal
365  * \brief Convert the Presented<Number/Address><Screened/Unscreened> presentation
366  * to Q.931 presentation field value.
367  *
368  * \param ctrl D channel controller for diagnostic messages or global options.
369  * \param presentation Presented<Number/Address><Screened/Unscreened> value.
370  *
371  * \return Q.931 presentation field value.
372  */
presentation_for_q931(struct pri * ctrl,int presentation)373 static int presentation_for_q931(struct pri *ctrl, int presentation)
374 {
375 	int value;
376 
377 	switch (presentation) {
378 	case 0:	/* presentationAllowed<Number/Address> */
379 		value = PRI_PRES_ALLOWED;
380 		break;
381 	default:
382 		pri_message(ctrl,
383 			"!! Unsupported Presented<Number/Address><Screened/Unscreened> to Q.931 value (%d)\n",
384 			presentation);
385 		/* fall through */
386 	case 1:	/* presentationRestricted */
387 	case 3:	/* presentationRestricted<Number/Address> */
388 		value = PRI_PRES_RESTRICTED;
389 		break;
390 	case 2:	/* numberNotAvailableDueToInterworking */
391 		value = PRI_PRES_UNAVAILABLE;
392 		break;
393 	}
394 
395 	return value;
396 }
397 
398 /*!
399  * \internal
400  * \brief Convert the Q.931 number presentation field to Q.SIG name presentation.
401  *
402  * \param ctrl D channel controller for diagnostic messages or global options.
403  * \param presentation Q.931 presentation/screening octet.
404  * \param name_present Non-zero if the name is available.
405  *
406  * \return Name presentation enumeration value.
407  */
qsig_name_presentation_from_q931(struct pri * ctrl,int presentation,int name_present)408 static int qsig_name_presentation_from_q931(struct pri *ctrl, int presentation, int name_present)
409 {
410 	int value;
411 
412 	switch (presentation & PRI_PRES_RESTRICTION) {
413 	case PRI_PRES_ALLOWED:
414 		if (name_present) {
415 			value = 1;	/* presentation_allowed */
416 		} else {
417 			value = 4;	/* name_not_available */
418 		}
419 		break;
420 	default:
421 		pri_message(ctrl, "!! Unsupported Q.931 number presentation value (%d)\n",
422 			presentation);
423 		/* fall through */
424 	case PRI_PRES_RESTRICTED:
425 		if (name_present) {
426 			value = 2;	/* presentation_restricted */
427 		} else {
428 			value = 3;	/* presentation_restricted_null */
429 		}
430 		break;
431 	case PRI_PRES_UNAVAILABLE:
432 		value = 4;	/* name_not_available */
433 		break;
434 	}
435 
436 	return value;
437 }
438 
439 /*!
440  * \internal
441  * \brief Convert the Q.SIG name presentation to Q.931 presentation field value.
442  *
443  * \param ctrl D channel controller for diagnostic messages or global options.
444  * \param presentation Q.SIG name presentation value.
445  *
446  * \return Q.931 presentation field value.
447  */
qsig_name_presentation_for_q931(struct pri * ctrl,int presentation)448 static int qsig_name_presentation_for_q931(struct pri *ctrl, int presentation)
449 {
450 	int value;
451 
452 	switch (presentation) {
453 	case 1:	/* presentation_allowed */
454 		value = PRI_PRES_ALLOWED;
455 		break;
456 	default:
457 		pri_message(ctrl,
458 			"!! Unsupported Q.SIG name presentation to Q.931 value (%d)\n",
459 			presentation);
460 		/* fall through */
461 	case 2:	/* presentation_restricted */
462 	case 3:	/* presentation_restricted_null */
463 		value = PRI_PRES_RESTRICTED;
464 		break;
465 	case 0:	/* optional_name_not_present */
466 	case 4:	/* name_not_available */
467 		value = PRI_PRES_UNAVAILABLE;
468 		break;
469 	}
470 
471 	return value;
472 }
473 
474 /*!
475  * \internal
476  * \brief Convert number presentation to Q.SIG diversion subscription notification.
477  *
478  * \param ctrl D channel controller for diagnostic messages or global options.
479  * \param presentation Number presentation value.
480  *
481  * \return Q.SIG diversion subscription notification value.
482  */
presentation_to_subscription(struct pri * ctrl,int presentation)483 static int presentation_to_subscription(struct pri *ctrl, int presentation)
484 {
485 	/* derive subscription value from presentation value */
486 
487 	switch (presentation & PRI_PRES_RESTRICTION) {
488 	case PRI_PRES_ALLOWED:
489 		return QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR;
490 	case PRI_PRES_RESTRICTED:
491 		return QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR;
492 	case PRI_PRES_UNAVAILABLE:	/* Number not available due to interworking */
493 		return QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR;	/* ?? QSIG_NO_NOTIFICATION */
494 	default:
495 		pri_message(ctrl, "!! Unknown Q.SIG presentationIndicator 0x%02x\n",
496 			presentation);
497 		return QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR;
498 	}
499 }
500 
501 /*!
502  * \brief Copy the given rose party number to the q931_party_number
503  *
504  * \param ctrl D channel controller for diagnostic messages or global options.
505  * \param q931_number Q.931 party number structure
506  * \param rose_number ROSE party number structure
507  *
508  * \note It is assumed that the q931_number has been initialized before calling.
509  *
510  * \return Nothing
511  */
rose_copy_number_to_q931(struct pri * ctrl,struct q931_party_number * q931_number,const struct rosePartyNumber * rose_number)512 void rose_copy_number_to_q931(struct pri *ctrl, struct q931_party_number *q931_number,
513 	const struct rosePartyNumber *rose_number)
514 {
515 	//q931_party_number_init(q931_number);
516 	libpri_copy_string(q931_number->str, (char *) rose_number->str,
517 		sizeof(q931_number->str));
518 	q931_number->plan = numbering_plan_for_q931(ctrl, rose_number->plan)
519 		| typeofnumber_for_q931(ctrl, rose_number->ton);
520 	q931_number->valid = 1;
521 }
522 
523 /*!
524  * \brief Copy the given rose subaddress to the q931_party_subaddress.
525  *
526  * \param ctrl D channel controller for diagnostic messages or global options.
527  * \param q931_subaddress Q.931 party subaddress structure
528  * \param rose_subaddress ROSE subaddress structure
529  *
530  * \note It is assumed that the q931_subaddress has been initialized before calling.
531  *
532  * \return Nothing
533  */
rose_copy_subaddress_to_q931(struct pri * ctrl,struct q931_party_subaddress * q931_subaddress,const struct rosePartySubaddress * rose_subaddress)534 void rose_copy_subaddress_to_q931(struct pri *ctrl,
535 	struct q931_party_subaddress *q931_subaddress,
536 	const struct rosePartySubaddress *rose_subaddress)
537 {
538 	//q931_party_subaddress_init(q931_subaddress);
539 	if (!rose_subaddress->length) {
540 		/* Subaddress is not present. */
541 		return;
542 	}
543 
544 	switch (rose_subaddress->type) {
545 	case 0:/* UserSpecified */
546 		q931_subaddress->type = 2;/* user_specified */
547 		q931_subaddress->valid = 1;
548 		q931_subaddress->length = rose_subaddress->length;
549 		if (sizeof(q931_subaddress->data) <= q931_subaddress->length) {
550 			q931_subaddress->length = sizeof(q931_subaddress->data) - 1;
551 		}
552 		memcpy(q931_subaddress->data, rose_subaddress->u.user_specified.information,
553 			q931_subaddress->length);
554 		q931_subaddress->data[q931_subaddress->length] = '\0';
555 		if (rose_subaddress->u.user_specified.odd_count_present) {
556 			q931_subaddress->odd_even_indicator =
557 				rose_subaddress->u.user_specified.odd_count;
558 		}
559 		break;
560 	case 1:/* NSAP */
561 		q931_subaddress->type = 0;/* nsap */
562 		q931_subaddress->valid = 1;
563 		libpri_copy_string((char *) q931_subaddress->data,
564 			(char *) rose_subaddress->u.nsap, sizeof(q931_subaddress->data));
565 		q931_subaddress->length = strlen((char *) q931_subaddress->data);
566 		break;
567 	default:
568 		/* Don't know how to encode so assume it is not present. */
569 		break;
570 	}
571 }
572 
573 /*!
574  * \brief Copy the given rose address to the q931_party_address.
575  *
576  * \param ctrl D channel controller for diagnostic messages or global options.
577  * \param q931_address Q.931 party address structure
578  * \param rose_address ROSE address structure
579  *
580  * \note It is assumed that the q931_address has been initialized before calling.
581  *
582  * \return Nothing
583  */
rose_copy_address_to_q931(struct pri * ctrl,struct q931_party_address * q931_address,const struct roseAddress * rose_address)584 void rose_copy_address_to_q931(struct pri *ctrl, struct q931_party_address *q931_address,
585 	const struct roseAddress *rose_address)
586 {
587 	rose_copy_number_to_q931(ctrl, &q931_address->number, &rose_address->number);
588 	rose_copy_subaddress_to_q931(ctrl, &q931_address->subaddress,
589 		&rose_address->subaddress);
590 }
591 
592 /*!
593  * \brief Copy the given rose address to the q931_party_id address.
594  *
595  * \param ctrl D channel controller for diagnostic messages or global options.
596  * \param q931_address Q.931 party id structure to fill address
597  * \param rose_address ROSE address structure
598  *
599  * \note It is assumed that the q931_address has been initialized before calling.
600  *
601  * \return Nothing
602  */
rose_copy_address_to_id_q931(struct pri * ctrl,struct q931_party_id * q931_address,const struct roseAddress * rose_address)603 void rose_copy_address_to_id_q931(struct pri *ctrl, struct q931_party_id *q931_address,
604 	const struct roseAddress *rose_address)
605 {
606 	rose_copy_number_to_q931(ctrl, &q931_address->number, &rose_address->number);
607 	rose_copy_subaddress_to_q931(ctrl, &q931_address->subaddress,
608 		&rose_address->subaddress);
609 }
610 
611 /*!
612  * \brief Copy the given rose presented screened party number to the q931_party_number
613  *
614  * \param ctrl D channel controller for diagnostic messages or global options.
615  * \param q931_number Q.931 party number structure
616  * \param rose_presented ROSE presented screened party number structure
617  *
618  * \return Nothing
619  */
rose_copy_presented_number_screened_to_q931(struct pri * ctrl,struct q931_party_number * q931_number,const struct rosePresentedNumberScreened * rose_presented)620 void rose_copy_presented_number_screened_to_q931(struct pri *ctrl,
621 	struct q931_party_number *q931_number,
622 	const struct rosePresentedNumberScreened *rose_presented)
623 {
624 	q931_party_number_init(q931_number);
625 	q931_number->valid = 1;
626 	q931_number->presentation = presentation_for_q931(ctrl, rose_presented->presentation);
627 	switch (rose_presented->presentation) {
628 	case 0:	/* presentationAllowedNumber */
629 	case 3:	/* presentationRestrictedNumber */
630 		q931_number->presentation |=
631 			(rose_presented->screened.screening_indicator & PRI_PRES_NUMBER_TYPE);
632 		rose_copy_number_to_q931(ctrl, q931_number,
633 			&rose_presented->screened.number);
634 		break;
635 	case 2:	/* numberNotAvailableDueToInterworking */
636 		q931_number->presentation = PRES_NUMBER_NOT_AVAILABLE;
637 		break;
638 	default:
639 		q931_number->presentation |= PRI_PRES_USER_NUMBER_UNSCREENED;
640 		break;
641 	}
642 }
643 
644 /*!
645  * \brief Copy the given rose presented unscreened party number to the q931_party_number
646  *
647  * \param ctrl D channel controller for diagnostic messages or global options.
648  * \param q931_number Q.931 party number structure
649  * \param rose_presented ROSE presented unscreened party number structure
650  *
651  * \return Nothing
652  */
rose_copy_presented_number_unscreened_to_q931(struct pri * ctrl,struct q931_party_number * q931_number,const struct rosePresentedNumberUnscreened * rose_presented)653 void rose_copy_presented_number_unscreened_to_q931(struct pri *ctrl,
654 	struct q931_party_number *q931_number,
655 	const struct rosePresentedNumberUnscreened *rose_presented)
656 {
657 	q931_party_number_init(q931_number);
658 	q931_number->valid = 1;
659 	q931_number->presentation = presentation_for_q931(ctrl,
660 		rose_presented->presentation) | PRI_PRES_USER_NUMBER_UNSCREENED;
661 	switch (rose_presented->presentation) {
662 	case 0:	/* presentationAllowedNumber */
663 	case 3:	/* presentationRestrictedNumber */
664 		rose_copy_number_to_q931(ctrl, q931_number, &rose_presented->number);
665 		break;
666 	case 2:	/* numberNotAvailableDueToInterworking */
667 		q931_number->presentation = PRES_NUMBER_NOT_AVAILABLE;
668 		break;
669 	default:
670 		break;
671 	}
672 }
673 
674 /*!
675  * \brief Copy the given rose presented screened party address to the q931_party_number
676  *
677  * \param ctrl D channel controller for diagnostic messages or global options.
678  * \param q931_address Q.931 party id structure to fill the address
679  * \param rose_presented ROSE presented screened party address structure
680  *
681  * \return Nothing
682  */
rose_copy_presented_address_screened_to_id_q931(struct pri * ctrl,struct q931_party_id * q931_address,const struct rosePresentedAddressScreened * rose_presented)683 void rose_copy_presented_address_screened_to_id_q931(struct pri *ctrl,
684 	struct q931_party_id *q931_address,
685 	const struct rosePresentedAddressScreened *rose_presented)
686 {
687 	q931_party_number_init(&q931_address->number);
688 	q931_party_subaddress_init(&q931_address->subaddress);
689 	q931_address->number.valid = 1;
690 	q931_address->number.presentation = presentation_for_q931(ctrl,
691 		rose_presented->presentation);
692 	switch (rose_presented->presentation) {
693 	case 0:	/* presentationAllowedAddress */
694 	case 3:	/* presentationRestrictedAddress */
695 		q931_address->number.presentation |=
696 			(rose_presented->screened.screening_indicator & PRI_PRES_NUMBER_TYPE);
697 		rose_copy_number_to_q931(ctrl, &q931_address->number,
698 			&rose_presented->screened.number);
699 		rose_copy_subaddress_to_q931(ctrl, &q931_address->subaddress,
700 			&rose_presented->screened.subaddress);
701 		break;
702 	case 2:	/* numberNotAvailableDueToInterworking */
703 		q931_address->number.presentation = PRES_NUMBER_NOT_AVAILABLE;
704 		break;
705 	default:
706 		q931_address->number.presentation |= PRI_PRES_USER_NUMBER_UNSCREENED;
707 		break;
708 	}
709 }
710 
711 /*!
712  * \brief Copy the given rose party name to the q931_party_name
713  *
714  * \param ctrl D channel controller for diagnostic messages or global options.
715  * \param qsig_name Q.SIG party name structure
716  * \param rose_name Q.SIG ROSE party name structure
717  *
718  * \return Nothing
719  */
rose_copy_name_to_q931(struct pri * ctrl,struct q931_party_name * qsig_name,const struct roseQsigName * rose_name)720 void rose_copy_name_to_q931(struct pri *ctrl, struct q931_party_name *qsig_name,
721 	const struct roseQsigName *rose_name)
722 {
723 	//q931_party_name_init(qsig_name);
724 	qsig_name->valid = 1;
725 	qsig_name->presentation = qsig_name_presentation_for_q931(ctrl,
726 		rose_name->presentation);
727 	qsig_name->char_set = rose_name->char_set;
728 	libpri_copy_string(qsig_name->str, (char *) rose_name->data, sizeof(qsig_name->str));
729 }
730 
731 /*!
732  * \brief Copy the given q931_party_number to the rose party number
733  *
734  * \param ctrl D channel controller for diagnostic messages or global options.
735  * \param rose_number ROSE party number structure
736  * \param q931_number Q.931 party number structure
737  *
738  * \return Nothing
739  */
q931_copy_number_to_rose(struct pri * ctrl,struct rosePartyNumber * rose_number,const struct q931_party_number * q931_number)740 void q931_copy_number_to_rose(struct pri *ctrl, struct rosePartyNumber *rose_number,
741 	const struct q931_party_number *q931_number)
742 {
743 	rose_number->plan = numbering_plan_from_q931(ctrl, q931_number->plan);
744 	rose_number->ton = typeofnumber_from_q931(ctrl, q931_number->plan);
745 	/* Truncate the q931_number->str if necessary. */
746 	libpri_copy_string((char *) rose_number->str, q931_number->str,
747 		sizeof(rose_number->str));
748 	rose_number->length = strlen((char *) rose_number->str);
749 }
750 
751 /*!
752  * \brief Copy the given q931_party_subaddress to the rose subaddress.
753  *
754  * \param ctrl D channel controller for diagnostic messages or global options.
755  * \param rose_subaddress ROSE subaddress structure
756  * \param q931_subaddress Q.931 party subaddress structure
757  *
758  * \return Nothing
759  */
q931_copy_subaddress_to_rose(struct pri * ctrl,struct rosePartySubaddress * rose_subaddress,const struct q931_party_subaddress * q931_subaddress)760 void q931_copy_subaddress_to_rose(struct pri *ctrl,
761 	struct rosePartySubaddress *rose_subaddress,
762 	const struct q931_party_subaddress *q931_subaddress)
763 {
764 	if (!q931_subaddress->valid) {
765 		/* Subaddress is not present. */
766 		rose_subaddress->length = 0;
767 		return;
768 	}
769 
770 	switch (q931_subaddress->type) {
771 	case 0:	/* NSAP */
772 		rose_subaddress->type = 1;/* NSAP */
773 		libpri_copy_string((char *) rose_subaddress->u.nsap,
774 			(char *) q931_subaddress->data, sizeof(rose_subaddress->u.nsap));
775 		rose_subaddress->length = strlen((char *) rose_subaddress->u.nsap);
776 		break;
777 	case 2:	/* user_specified */
778 		rose_subaddress->type = 0;/* UserSpecified */
779 		rose_subaddress->length = q931_subaddress->length;
780 		if (sizeof(rose_subaddress->u.user_specified.information)
781 			<= rose_subaddress->length) {
782 			rose_subaddress->length =
783 				sizeof(rose_subaddress->u.user_specified.information) - 1;
784 		} else {
785 			if (q931_subaddress->odd_even_indicator) {
786 				rose_subaddress->u.user_specified.odd_count_present = 1;
787 				rose_subaddress->u.user_specified.odd_count = 1;
788 			}
789 		}
790 		memcpy(rose_subaddress->u.user_specified.information, q931_subaddress->data,
791 			rose_subaddress->length);
792 		rose_subaddress->u.user_specified.information[rose_subaddress->length] = '\0';
793 		break;
794 	default:
795 		/* Don't know how to encode so assume it is not present. */
796 		rose_subaddress->length = 0;
797 		break;
798 	}
799 }
800 
801 /*!
802  * \brief Copy the given q931_party_address to the rose address.
803  *
804  * \param ctrl D channel controller for diagnostic messages or global options.
805  * \param rose_address ROSE address structure
806  * \param q931_address Q.931 party address structure
807  *
808  * \return Nothing
809  */
q931_copy_address_to_rose(struct pri * ctrl,struct roseAddress * rose_address,const struct q931_party_address * q931_address)810 void q931_copy_address_to_rose(struct pri *ctrl, struct roseAddress *rose_address,
811 	const struct q931_party_address *q931_address)
812 {
813 	q931_copy_number_to_rose(ctrl, &rose_address->number, &q931_address->number);
814 	q931_copy_subaddress_to_rose(ctrl, &rose_address->subaddress,
815 		&q931_address->subaddress);
816 }
817 
818 /*!
819  * \brief Copy the given q931_party_id address to the rose address.
820  *
821  * \param ctrl D channel controller for diagnostic messages or global options.
822  * \param rose_address ROSE address structure
823  * \param q931_address Q.931 party id structure to give address
824  *
825  * \return Nothing
826  */
q931_copy_id_address_to_rose(struct pri * ctrl,struct roseAddress * rose_address,const struct q931_party_id * q931_address)827 void q931_copy_id_address_to_rose(struct pri *ctrl, struct roseAddress *rose_address,
828 	const struct q931_party_id *q931_address)
829 {
830 	q931_copy_number_to_rose(ctrl, &rose_address->number, &q931_address->number);
831 	q931_copy_subaddress_to_rose(ctrl, &rose_address->subaddress,
832 		&q931_address->subaddress);
833 }
834 
835 /*!
836  * \brief Copy the given q931_party_number to the rose presented screened party number
837  *
838  * \param ctrl D channel controller for diagnostic messages or global options.
839  * \param rose_presented ROSE presented screened party number structure
840  * \param q931_number Q.931 party number structure
841  *
842  * \return Nothing
843  */
q931_copy_presented_number_screened_to_rose(struct pri * ctrl,struct rosePresentedNumberScreened * rose_presented,const struct q931_party_number * q931_number)844 void q931_copy_presented_number_screened_to_rose(struct pri *ctrl,
845 	struct rosePresentedNumberScreened *rose_presented,
846 	const struct q931_party_number *q931_number)
847 {
848 	if (q931_number->valid) {
849 		rose_presented->presentation =
850 			presentation_from_q931(ctrl, q931_number->presentation, q931_number->str[0]);
851 		rose_presented->screened.screening_indicator =
852 			q931_number->presentation & PRI_PRES_NUMBER_TYPE;
853 		q931_copy_number_to_rose(ctrl, &rose_presented->screened.number, q931_number);
854 	} else {
855 		rose_presented->presentation = 2;/* numberNotAvailableDueToInterworking */
856 	}
857 }
858 
859 /*!
860  * \brief Copy the given q931_party_number to the rose presented unscreened party number
861  *
862  * \param ctrl D channel controller for diagnostic messages or global options.
863  * \param rose_presented ROSE presented unscreened party number structure
864  * \param q931_number Q.931 party number structure
865  *
866  * \return Nothing
867  */
q931_copy_presented_number_unscreened_to_rose(struct pri * ctrl,struct rosePresentedNumberUnscreened * rose_presented,const struct q931_party_number * q931_number)868 void q931_copy_presented_number_unscreened_to_rose(struct pri *ctrl,
869 	struct rosePresentedNumberUnscreened *rose_presented,
870 	const struct q931_party_number *q931_number)
871 {
872 	if (q931_number->valid) {
873 		rose_presented->presentation =
874 			presentation_from_q931(ctrl, q931_number->presentation, q931_number->str[0]);
875 		q931_copy_number_to_rose(ctrl, &rose_presented->number, q931_number);
876 	} else {
877 		rose_presented->presentation = 2;/* numberNotAvailableDueToInterworking */
878 	}
879 }
880 
881 /*!
882  * \brief Copy the given q931_party_number to the rose presented screened party address
883  *
884  * \param ctrl D channel controller for diagnostic messages or global options.
885  * \param rose_presented ROSE presented screened party address structure
886  * \param q931_address Q.931 party id structure to get the address
887  *
888  * \return Nothing
889  */
q931_copy_presented_id_address_screened_to_rose(struct pri * ctrl,struct rosePresentedAddressScreened * rose_presented,const struct q931_party_id * q931_address)890 void q931_copy_presented_id_address_screened_to_rose(struct pri *ctrl,
891 	struct rosePresentedAddressScreened *rose_presented,
892 	const struct q931_party_id *q931_address)
893 {
894 	if (q931_address->number.valid) {
895 		rose_presented->presentation =
896 			presentation_from_q931(ctrl, q931_address->number.presentation,
897 				q931_address->number.str[0]);
898 		rose_presented->screened.screening_indicator =
899 			q931_address->number.presentation & PRI_PRES_NUMBER_TYPE;
900 		q931_copy_number_to_rose(ctrl, &rose_presented->screened.number,
901 			&q931_address->number);
902 		q931_copy_subaddress_to_rose(ctrl, &rose_presented->screened.subaddress,
903 			&q931_address->subaddress);
904 	} else {
905 		rose_presented->presentation = 2;/* numberNotAvailableDueToInterworking */
906 	}
907 }
908 
909 /*!
910  * \brief Copy the given q931_party_name to the rose party name
911  *
912  * \param ctrl D channel controller for diagnostic messages or global options.
913  * \param rose_name Q.SIG ROSE party name structure
914  * \param qsig_name Q.SIG party name structure
915  *
916  * \return Nothing
917  */
q931_copy_name_to_rose(struct pri * ctrl,struct roseQsigName * rose_name,const struct q931_party_name * qsig_name)918 void q931_copy_name_to_rose(struct pri *ctrl, struct roseQsigName *rose_name,
919 	const struct q931_party_name *qsig_name)
920 {
921 	if (qsig_name->valid) {
922 		rose_name->presentation = qsig_name_presentation_from_q931(ctrl,
923 			qsig_name->presentation, qsig_name->str[0]);
924 		rose_name->char_set = qsig_name->char_set;
925 		/* Truncate the qsig_name->str if necessary. */
926 		libpri_copy_string((char *) rose_name->data, qsig_name->str, sizeof(rose_name->data));
927 		rose_name->length = strlen((char *) rose_name->data);
928 	} else {
929 		rose_name->presentation = 4;/* name_not_available */
930 	}
931 }
932 
933 /*!
934  * \internal
935  * \brief Encode the Q.SIG DivertingLegInformation1 invoke message.
936  *
937  * \param ctrl D channel controller for diagnostic messages or global options.
938  * \param pos Starting position to encode the facility ie contents.
939  * \param end End of facility ie contents encoding data buffer.
940  * \param call Call leg from which to encode diversion leg 1.
941  *
942  * \retval Start of the next ASN.1 component to encode on success.
943  * \retval NULL on error.
944  */
enc_qsig_diverting_leg_information1(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)945 static unsigned char *enc_qsig_diverting_leg_information1(struct pri *ctrl,
946 	unsigned char *pos, unsigned char *end, q931_call *call)
947 {
948 	struct fac_extension_header header;
949 	struct rose_msg_invoke msg;
950 
951 	memset(&header, 0, sizeof(header));
952 	header.nfe_present = 1;
953 	header.nfe.source_entity = 0;	/* endPINX */
954 	header.nfe.destination_entity = 0;	/* endPINX */
955 	header.interpretation_present = 1;
956 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
957 	pos = facility_encode_header(ctrl, pos, end, &header);
958 	if (!pos) {
959 		return NULL;
960 	}
961 
962 	memset(&msg, 0, sizeof(msg));
963 	msg.operation = ROSE_QSIG_DivertingLegInformation1;
964 	msg.invoke_id = get_invokeid(ctrl);
965 	msg.args.qsig.DivertingLegInformation1.diversion_reason =
966 		redirectingreason_from_q931(ctrl, call->redirecting.reason);
967 
968 	/* subscriptionOption is the redirecting.to.number.presentation */
969 	msg.args.qsig.DivertingLegInformation1.subscription_option =
970 		presentation_to_subscription(ctrl, call->redirecting.to.number.presentation);
971 
972 	/* nominatedNr is the redirecting.to.number */
973 	q931_copy_number_to_rose(ctrl,
974 		&msg.args.qsig.DivertingLegInformation1.nominated_number,
975 		&call->redirecting.to.number);
976 
977 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
978 
979 	return pos;
980 }
981 
982 /*!
983  * \internal
984  * \brief Encode the ETSI DivertingLegInformation1 invoke message.
985  *
986  * \param ctrl D channel controller for diagnostic messages or global options.
987  * \param pos Starting position to encode the facility ie contents.
988  * \param end End of facility ie contents encoding data buffer.
989  * \param call Call leg from which to encode diversion leg 1.
990  *
991  * \retval Start of the next ASN.1 component to encode on success.
992  * \retval NULL on error.
993  */
enc_etsi_diverting_leg_information1(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)994 static unsigned char *enc_etsi_diverting_leg_information1(struct pri *ctrl,
995 	unsigned char *pos, unsigned char *end, q931_call *call)
996 {
997 	struct rose_msg_invoke msg;
998 
999 	pos = facility_encode_header(ctrl, pos, end, NULL);
1000 	if (!pos) {
1001 		return NULL;
1002 	}
1003 
1004 	memset(&msg, 0, sizeof(msg));
1005 	msg.operation = ROSE_ETSI_DivertingLegInformation1;
1006 	msg.invoke_id = get_invokeid(ctrl);
1007 	msg.args.etsi.DivertingLegInformation1.diversion_reason =
1008 		redirectingreason_from_q931(ctrl, call->redirecting.reason);
1009 
1010 	if (call->redirecting.to.number.valid) {
1011 		msg.args.etsi.DivertingLegInformation1.subscription_option = 2;
1012 
1013 		/* divertedToNumber is the redirecting.to.number */
1014 		msg.args.etsi.DivertingLegInformation1.diverted_to_present = 1;
1015 		q931_copy_presented_number_unscreened_to_rose(ctrl,
1016 			&msg.args.etsi.DivertingLegInformation1.diverted_to,
1017 			&call->redirecting.to.number);
1018 	} else {
1019 		msg.args.etsi.DivertingLegInformation1.subscription_option = 1;
1020 	}
1021 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1022 
1023 	return pos;
1024 }
1025 
1026 /*!
1027  * \brief Encode and queue the DivertingLegInformation1 invoke message.
1028  *
1029  * \param ctrl D channel controller for diagnostic messages or global options.
1030  * \param call Call leg from which to encode diversion leg 1.
1031  *
1032  * \retval 0 on success.
1033  * \retval -1 on error.
1034  */
rose_diverting_leg_information1_encode(struct pri * ctrl,q931_call * call)1035 int rose_diverting_leg_information1_encode(struct pri *ctrl, q931_call *call)
1036 {
1037 	unsigned char buffer[256];
1038 	unsigned char *end;
1039 
1040 	switch (ctrl->switchtype) {
1041 	case PRI_SWITCH_EUROISDN_E1:
1042 	case PRI_SWITCH_EUROISDN_T1:
1043 		end = enc_etsi_diverting_leg_information1(ctrl, buffer, buffer + sizeof(buffer),
1044 			call);
1045 		break;
1046 	case PRI_SWITCH_QSIG:
1047 		end = enc_qsig_diverting_leg_information1(ctrl, buffer, buffer + sizeof(buffer),
1048 			call);
1049 		break;
1050 	default:
1051 		return -1;
1052 	}
1053 	if (!end) {
1054 		return -1;
1055 	}
1056 
1057 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL);
1058 }
1059 
1060 /*!
1061  * \internal
1062  * \brief Encode the Q.SIG DivertingLegInformation2 invoke message.
1063  *
1064  * \param ctrl D channel controller for diagnostic messages or global options.
1065  * \param pos Starting position to encode the facility ie contents.
1066  * \param end End of facility ie contents encoding data buffer.
1067  * \param call Call leg from which to encode diversion leg 2.
1068  *
1069  * \retval Start of the next ASN.1 component to encode on success.
1070  * \retval NULL on error.
1071  */
enc_qsig_diverting_leg_information2(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)1072 static unsigned char *enc_qsig_diverting_leg_information2(struct pri *ctrl,
1073 	unsigned char *pos, unsigned char *end, q931_call *call)
1074 {
1075 	struct fac_extension_header header;
1076 	struct rose_msg_invoke msg;
1077 
1078 	memset(&header, 0, sizeof(header));
1079 	header.nfe_present = 1;
1080 	header.nfe.source_entity = 0;	/* endPINX */
1081 	header.nfe.destination_entity = 0;	/* endPINX */
1082 	header.interpretation_present = 1;
1083 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
1084 	pos = facility_encode_header(ctrl, pos, end, &header);
1085 	if (!pos) {
1086 		return NULL;
1087 	}
1088 
1089 	memset(&msg, 0, sizeof(msg));
1090 	msg.operation = ROSE_QSIG_DivertingLegInformation2;
1091 	msg.invoke_id = get_invokeid(ctrl);
1092 
1093 	/* diversionCounter is the redirecting.count */
1094 	msg.args.qsig.DivertingLegInformation2.diversion_counter = call->redirecting.count;
1095 
1096 	msg.args.qsig.DivertingLegInformation2.diversion_reason =
1097 		redirectingreason_from_q931(ctrl, call->redirecting.reason);
1098 
1099 	/* divertingNr is the redirecting.from.number */
1100 	msg.args.qsig.DivertingLegInformation2.diverting_present = 1;
1101 	q931_copy_presented_number_unscreened_to_rose(ctrl,
1102 		&msg.args.qsig.DivertingLegInformation2.diverting,
1103 		&call->redirecting.from.number);
1104 
1105 	/* redirectingName is the redirecting.from.name */
1106 	if (call->redirecting.from.name.valid) {
1107 		msg.args.qsig.DivertingLegInformation2.redirecting_name_present = 1;
1108 		q931_copy_name_to_rose(ctrl,
1109 			&msg.args.qsig.DivertingLegInformation2.redirecting_name,
1110 			&call->redirecting.from.name);
1111 	}
1112 
1113 	if (1 < call->redirecting.count) {
1114 		/* originalCalledNr is the redirecting.orig_called.number */
1115 		msg.args.qsig.DivertingLegInformation2.original_called_present = 1;
1116 		q931_copy_presented_number_unscreened_to_rose(ctrl,
1117 			&msg.args.qsig.DivertingLegInformation2.original_called,
1118 			&call->redirecting.orig_called.number);
1119 
1120 		msg.args.qsig.DivertingLegInformation2.original_diversion_reason_present = 1;
1121 		if (call->redirecting.orig_called.number.valid) {
1122 			msg.args.qsig.DivertingLegInformation2.original_diversion_reason =
1123 				redirectingreason_from_q931(ctrl, call->redirecting.orig_reason);
1124 		} else {
1125 			msg.args.qsig.DivertingLegInformation2.original_diversion_reason =
1126 				QSIG_DIVERT_REASON_UNKNOWN;
1127 		}
1128 
1129 		/* originalCalledName is the redirecting.orig_called.name */
1130 		if (call->redirecting.orig_called.name.valid) {
1131 			msg.args.qsig.DivertingLegInformation2.original_called_name_present = 1;
1132 			q931_copy_name_to_rose(ctrl,
1133 				&msg.args.qsig.DivertingLegInformation2.original_called_name,
1134 				&call->redirecting.orig_called.name);
1135 		}
1136 	}
1137 
1138 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1139 
1140 	return pos;
1141 }
1142 
1143 /*!
1144  * \internal
1145  * \brief Encode the ETSI DivertingLegInformation2 invoke message.
1146  *
1147  * \param ctrl D channel controller for diagnostic messages or global options.
1148  * \param pos Starting position to encode the facility ie contents.
1149  * \param end End of facility ie contents encoding data buffer.
1150  * \param call Call leg from which to encode diversion leg 2.
1151  *
1152  * \retval Start of the next ASN.1 component to encode on success.
1153  * \retval NULL on error.
1154  */
enc_etsi_diverting_leg_information2(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)1155 static unsigned char *enc_etsi_diverting_leg_information2(struct pri *ctrl,
1156 	unsigned char *pos, unsigned char *end, q931_call *call)
1157 {
1158 	struct rose_msg_invoke msg;
1159 
1160 	pos = facility_encode_header(ctrl, pos, end, NULL);
1161 	if (!pos) {
1162 		return NULL;
1163 	}
1164 
1165 	memset(&msg, 0, sizeof(msg));
1166 	msg.operation = ROSE_ETSI_DivertingLegInformation2;
1167 	msg.invoke_id = get_invokeid(ctrl);
1168 
1169 	/* diversionCounter is the redirecting.count */
1170 	msg.args.etsi.DivertingLegInformation2.diversion_counter = call->redirecting.count;
1171 
1172 	msg.args.etsi.DivertingLegInformation2.diversion_reason =
1173 		redirectingreason_from_q931(ctrl, call->redirecting.reason);
1174 
1175 	/* divertingNr is the redirecting.from.number */
1176 	msg.args.etsi.DivertingLegInformation2.diverting_present = 1;
1177 	q931_copy_presented_number_unscreened_to_rose(ctrl,
1178 		&msg.args.etsi.DivertingLegInformation2.diverting,
1179 		&call->redirecting.from.number);
1180 
1181 	if (1 < call->redirecting.count) {
1182 		/* originalCalledNr is the redirecting.orig_called.number */
1183 		msg.args.etsi.DivertingLegInformation2.original_called_present = 1;
1184 		q931_copy_presented_number_unscreened_to_rose(ctrl,
1185 			&msg.args.etsi.DivertingLegInformation2.original_called,
1186 			&call->redirecting.orig_called.number);
1187 	}
1188 
1189 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1190 
1191 	return pos;
1192 }
1193 
1194 /*!
1195  * \internal
1196  * \brief Encode and queue the DivertingLegInformation2 invoke message.
1197  *
1198  * \param ctrl D channel controller for diagnostic messages or global options.
1199  * \param call Call leg from which to encode diversion leg 2.
1200  *
1201  * \retval 0 on success.
1202  * \retval -1 on error.
1203  */
rose_diverting_leg_information2_encode(struct pri * ctrl,q931_call * call)1204 static int rose_diverting_leg_information2_encode(struct pri *ctrl, q931_call *call)
1205 {
1206 	unsigned char buffer[256];
1207 	unsigned char *end;
1208 
1209 	switch (ctrl->switchtype) {
1210 	case PRI_SWITCH_EUROISDN_E1:
1211 	case PRI_SWITCH_EUROISDN_T1:
1212 		end = enc_etsi_diverting_leg_information2(ctrl, buffer, buffer + sizeof(buffer),
1213 			call);
1214 		break;
1215 	case PRI_SWITCH_QSIG:
1216 		end = enc_qsig_diverting_leg_information2(ctrl, buffer, buffer + sizeof(buffer),
1217 			call);
1218 		break;
1219 	default:
1220 		return -1;
1221 	}
1222 	if (!end) {
1223 		return -1;
1224 	}
1225 
1226 	return pri_call_apdu_queue(call, Q931_SETUP, buffer, end - buffer, NULL);
1227 }
1228 
1229 /*!
1230  * \internal
1231  * \brief Encode the Q.SIG DivertingLegInformation3 invoke message.
1232  *
1233  * \param ctrl D channel controller for diagnostic messages or global options.
1234  * \param pos Starting position to encode the facility ie contents.
1235  * \param end End of facility ie contents encoding data buffer.
1236  * \param call Call leg from which to encode diversion leg 3.
1237  *
1238  * \retval Start of the next ASN.1 component to encode on success.
1239  * \retval NULL on error.
1240  */
enc_qsig_diverting_leg_information3(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)1241 static unsigned char *enc_qsig_diverting_leg_information3(struct pri *ctrl,
1242 	unsigned char *pos, unsigned char *end, q931_call *call)
1243 {
1244 	struct fac_extension_header header;
1245 	struct rose_msg_invoke msg;
1246 
1247 	memset(&header, 0, sizeof(header));
1248 	header.nfe_present = 1;
1249 	header.nfe.source_entity = 0;	/* endPINX */
1250 	header.nfe.destination_entity = 0;	/* endPINX */
1251 	header.interpretation_present = 1;
1252 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
1253 	pos = facility_encode_header(ctrl, pos, end, &header);
1254 	if (!pos) {
1255 		return NULL;
1256 	}
1257 
1258 	memset(&msg, 0, sizeof(msg));
1259 	msg.operation = ROSE_QSIG_DivertingLegInformation3;
1260 	msg.invoke_id = get_invokeid(ctrl);
1261 
1262 	/* redirecting.to.number.presentation also indicates if name presentation is allowed */
1263 	if ((call->redirecting.to.number.presentation & PRI_PRES_RESTRICTION) == PRI_PRES_ALLOWED) {
1264 		msg.args.qsig.DivertingLegInformation3.presentation_allowed_indicator = 1;	/* TRUE */
1265 
1266 		/* redirectionName is the redirecting.to.name */
1267 		if (call->redirecting.to.name.valid) {
1268 			msg.args.qsig.DivertingLegInformation3.redirection_name_present = 1;
1269 			q931_copy_name_to_rose(ctrl,
1270 				&msg.args.qsig.DivertingLegInformation3.redirection_name,
1271 				&call->redirecting.to.name);
1272 		}
1273 	}
1274 
1275 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1276 
1277 	return pos;
1278 }
1279 
1280 /*!
1281  * \internal
1282  * \brief Encode the ETSI DivertingLegInformation3 invoke message.
1283  *
1284  * \param ctrl D channel controller for diagnostic messages or global options.
1285  * \param pos Starting position to encode the facility ie contents.
1286  * \param end End of facility ie contents encoding data buffer.
1287  * \param call Call leg from which to encode diversion leg 3.
1288  *
1289  * \retval Start of the next ASN.1 component to encode on success.
1290  * \retval NULL on error.
1291  */
enc_etsi_diverting_leg_information3(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)1292 static unsigned char *enc_etsi_diverting_leg_information3(struct pri *ctrl,
1293 	unsigned char *pos, unsigned char *end, q931_call *call)
1294 {
1295 	struct rose_msg_invoke msg;
1296 
1297 	pos = facility_encode_header(ctrl, pos, end, NULL);
1298 	if (!pos) {
1299 		return NULL;
1300 	}
1301 
1302 	memset(&msg, 0, sizeof(msg));
1303 	msg.operation = ROSE_ETSI_DivertingLegInformation3;
1304 	msg.invoke_id = get_invokeid(ctrl);
1305 
1306 	if ((call->redirecting.to.number.presentation & PRI_PRES_RESTRICTION) == PRI_PRES_ALLOWED) {
1307 		msg.args.etsi.DivertingLegInformation3.presentation_allowed_indicator = 1;	/* TRUE */
1308 	}
1309 
1310 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1311 
1312 	return pos;
1313 }
1314 
1315 /*!
1316  * \brief Encode and queue the DivertingLegInformation3 invoke message.
1317  *
1318  * \param ctrl D channel controller for diagnostic messages or global options.
1319  * \param call Call leg from which to encode diversion leg 3.
1320  * \param messagetype Q.931 message type to add facility ie to.
1321  *
1322  * \retval 0 on success.
1323  * \retval -1 on error.
1324  */
rose_diverting_leg_information3_encode(struct pri * ctrl,q931_call * call,int messagetype)1325 int rose_diverting_leg_information3_encode(struct pri *ctrl, q931_call *call,
1326 	int messagetype)
1327 {
1328 	unsigned char buffer[256];
1329 	unsigned char *end;
1330 
1331 	switch (ctrl->switchtype) {
1332 	case PRI_SWITCH_EUROISDN_E1:
1333 	case PRI_SWITCH_EUROISDN_T1:
1334 		end = enc_etsi_diverting_leg_information3(ctrl, buffer, buffer + sizeof(buffer),
1335 			call);
1336 		break;
1337 	case PRI_SWITCH_QSIG:
1338 		end = enc_qsig_diverting_leg_information3(ctrl, buffer, buffer + sizeof(buffer),
1339 			call);
1340 		break;
1341 	default:
1342 		return -1;
1343 	}
1344 	if (!end) {
1345 		return -1;
1346 	}
1347 
1348 	return pri_call_apdu_queue(call, messagetype, buffer, end - buffer, NULL);
1349 }
1350 
1351 /*!
1352  * \internal
1353  * \brief Encode the rltThirdParty invoke message.
1354  *
1355  * \param ctrl D channel controller for diagnostic messages or global options.
1356  * \param pos Starting position to encode the facility ie contents.
1357  * \param end End of facility ie contents encoding data buffer.
1358  * \param callwithid Call-ID information to encode.
1359  *
1360  * \retval Start of the next ASN.1 component to encode on success.
1361  * \retval NULL on error.
1362  */
enc_dms100_rlt_initiate_transfer(struct pri * ctrl,unsigned char * pos,unsigned char * end,const q931_call * callwithid)1363 static unsigned char *enc_dms100_rlt_initiate_transfer(struct pri *ctrl,
1364 	unsigned char *pos, unsigned char *end, const q931_call *callwithid)
1365 {
1366 	struct rose_msg_invoke msg;
1367 
1368 	pos = facility_encode_header(ctrl, pos, end, NULL);
1369 	if (!pos) {
1370 		return NULL;
1371 	}
1372 
1373 	memset(&msg, 0, sizeof(msg));
1374 	msg.operation = ROSE_DMS100_RLT_ThirdParty;
1375 	msg.invoke_id = ROSE_DMS100_RLT_THIRD_PARTY;
1376 	msg.args.dms100.RLT_ThirdParty.call_id = callwithid->rlt_call_id & 0xFFFFFF;
1377 	msg.args.dms100.RLT_ThirdParty.reason = 0;	/* unused, set to 129 */
1378 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1379 
1380 	return pos;
1381 }
1382 
1383 /*!
1384  * \brief Send the rltThirdParty: Invoke.
1385  *
1386  * \note For PRI_SWITCH_DMS100 only.
1387  *
1388  * \param ctrl D channel controller for diagnostic messages or global options.
1389  * \param c1 Q.931 call leg 1
1390  * \param c2 Q.931 call leg 2
1391  *
1392  * \retval 0 on success.
1393  * \retval -1 on error.
1394  */
rlt_initiate_transfer(struct pri * ctrl,q931_call * c1,q931_call * c2)1395 int rlt_initiate_transfer(struct pri *ctrl, q931_call *c1, q931_call *c2)
1396 {
1397 	unsigned char buffer[256];
1398 	unsigned char *end;
1399 	q931_call *apdubearer;
1400 	q931_call *callwithid;
1401 
1402 	if (c2->transferable) {
1403 		apdubearer = c1;
1404 		callwithid = c2;
1405 	} else if (c1->transferable) {
1406 		apdubearer = c2;
1407 		callwithid = c1;
1408 	} else {
1409 		return -1;
1410 	}
1411 
1412 	end =
1413 		enc_dms100_rlt_initiate_transfer(ctrl, buffer, buffer + sizeof(buffer),
1414 		callwithid);
1415 	if (!end) {
1416 		return -1;
1417 	}
1418 
1419 	if (pri_call_apdu_queue(apdubearer, Q931_FACILITY, buffer, end - buffer, NULL)) {
1420 		return -1;
1421 	}
1422 
1423 	if (q931_facility(apdubearer->pri, apdubearer)) {
1424 		pri_message(ctrl, "Could not schedule facility message for call %d\n",
1425 			apdubearer->cr);
1426 		return -1;
1427 	}
1428 	return 0;
1429 }
1430 
1431 /*!
1432  * \internal
1433  * \brief Encode the rltOperationInd invoke message.
1434  *
1435  * \param ctrl D channel controller for diagnostic messages or global options.
1436  * \param pos Starting position to encode the facility ie contents.
1437  * \param end End of facility ie contents encoding data buffer.
1438  *
1439  * \retval Start of the next ASN.1 component to encode on success.
1440  * \retval NULL on error.
1441  */
enc_dms100_rlt_transfer_ability(struct pri * ctrl,unsigned char * pos,unsigned char * end)1442 static unsigned char *enc_dms100_rlt_transfer_ability(struct pri *ctrl,
1443 	unsigned char *pos, unsigned char *end)
1444 {
1445 	struct rose_msg_invoke msg;
1446 
1447 	pos = facility_encode_header(ctrl, pos, end, NULL);
1448 	if (!pos) {
1449 		return NULL;
1450 	}
1451 
1452 	memset(&msg, 0, sizeof(msg));
1453 	msg.operation = ROSE_DMS100_RLT_OperationInd;
1454 	msg.invoke_id = ROSE_DMS100_RLT_OPERATION_IND;
1455 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1456 
1457 	return pos;
1458 }
1459 
1460 /*!
1461  * \internal
1462  * \brief Send the rltOperationInd: Invoke.
1463  *
1464  * \note For PRI_SWITCH_DMS100 only.
1465  *
1466  * \param ctrl D channel controller for diagnostic messages or global options.
1467  * \param call Q.931 call leg
1468  *
1469  * \retval 0 on success.
1470  * \retval -1 on error.
1471  */
add_dms100_transfer_ability_apdu(struct pri * ctrl,q931_call * call)1472 static int add_dms100_transfer_ability_apdu(struct pri *ctrl, q931_call *call)
1473 {
1474 	unsigned char buffer[256];
1475 	unsigned char *end;
1476 
1477 	end = enc_dms100_rlt_transfer_ability(ctrl, buffer, buffer + sizeof(buffer));
1478 	if (!end) {
1479 		return -1;
1480 	}
1481 
1482 	return pri_call_apdu_queue(call, Q931_SETUP, buffer, end - buffer, NULL);
1483 }
1484 
1485 /*!
1486  * \internal
1487  * \brief Encode the NI2 InformationFollowing invoke message.
1488  *
1489  * \param ctrl D channel controller for diagnostic messages or global options.
1490  * \param pos Starting position to encode the facility ie contents.
1491  * \param end End of facility ie contents encoding data buffer.
1492  *
1493  * \retval Start of the next ASN.1 component to encode on success.
1494  * \retval NULL on error.
1495  */
enc_ni2_information_following(struct pri * ctrl,unsigned char * pos,unsigned char * end)1496 static unsigned char *enc_ni2_information_following(struct pri *ctrl, unsigned char *pos,
1497 	unsigned char *end)
1498 {
1499 	struct fac_extension_header header;
1500 	struct rose_msg_invoke msg;
1501 
1502 	memset(&header, 0, sizeof(header));
1503 	header.interpretation_present = 1;
1504 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
1505 	pos = facility_encode_header(ctrl, pos, end, &header);
1506 	if (!pos) {
1507 		return NULL;
1508 	}
1509 
1510 	memset(&msg, 0, sizeof(msg));
1511 	msg.operation = ROSE_NI2_InformationFollowing;
1512 	msg.invoke_id = get_invokeid(ctrl);
1513 	msg.args.ni2.InformationFollowing.value = 0;
1514 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1515 
1516 	return pos;
1517 }
1518 
1519 /*!
1520  * \internal
1521  * \brief Encode the Q.SIG CallingName invoke message.
1522  *
1523  * \param ctrl D channel controller for diagnostic messages or global options.
1524  * \param pos Starting position to encode the facility ie contents.
1525  * \param end End of facility ie contents encoding data buffer.
1526  * \param name Name data which to encode name.
1527  *
1528  * \retval Start of the next ASN.1 component to encode on success.
1529  * \retval NULL on error.
1530  */
enc_qsig_calling_name(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct q931_party_name * name)1531 static unsigned char *enc_qsig_calling_name(struct pri *ctrl, unsigned char *pos,
1532 	unsigned char *end, const struct q931_party_name *name)
1533 {
1534 	struct fac_extension_header header;
1535 	struct rose_msg_invoke msg;
1536 
1537 	memset(&header, 0, sizeof(header));
1538 	if (ctrl->switchtype == PRI_SWITCH_QSIG) {
1539 		header.nfe_present = 1;
1540 		header.nfe.source_entity = 0;	/* endPINX */
1541 		header.nfe.destination_entity = 0;	/* endPINX */
1542 	}
1543 	header.interpretation_present = 1;
1544 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
1545 	pos = facility_encode_header(ctrl, pos, end, &header);
1546 	if (!pos) {
1547 		return NULL;
1548 	}
1549 
1550 	memset(&msg, 0, sizeof(msg));
1551 	msg.operation = ROSE_QSIG_CallingName;
1552 	msg.invoke_id = get_invokeid(ctrl);
1553 
1554 	/* CallingName */
1555 	q931_copy_name_to_rose(ctrl, &msg.args.qsig.CallingName.name, name);
1556 
1557 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1558 
1559 	return pos;
1560 }
1561 
1562 /*!
1563  * \internal
1564  * \brief Send caller name information.
1565  *
1566  * \note For PRI_SWITCH_NI2 and PRI_SWITCH_QSIG.
1567  *
1568  * \param ctrl D channel controller for diagnostic messages or global options.
1569  * \param call Call leg from which to encode name.
1570  * \param cpe TRUE if we are the CPE side.
1571  *
1572  * \retval 0 on success.
1573  * \retval -1 on error.
1574  */
add_callername_facility_ies(struct pri * ctrl,q931_call * call,int cpe)1575 static int add_callername_facility_ies(struct pri *ctrl, q931_call *call, int cpe)
1576 {
1577 	unsigned char buffer[256];
1578 	unsigned char *end;
1579 	int mymessage;
1580 
1581 	if (!call->local_id.name.valid) {
1582 		return 0;
1583 	}
1584 
1585 	if (ctrl->switchtype == PRI_SWITCH_NI2 && !cpe) {
1586 		end = enc_ni2_information_following(ctrl, buffer, buffer + sizeof(buffer));
1587 		if (!end) {
1588 			return -1;
1589 		}
1590 
1591 		if (pri_call_apdu_queue(call, Q931_SETUP, buffer, end - buffer, NULL)) {
1592 			return -1;
1593 		}
1594 
1595 		/*
1596 		 * We can reuse the buffer since the queue function doesn't
1597 		 * need it.
1598 		 */
1599 	}
1600 
1601 	/* CallingName is the local_id.name */
1602 	end = enc_qsig_calling_name(ctrl, buffer, buffer + sizeof(buffer),
1603 		&call->local_id.name);
1604 	if (!end) {
1605 		return -1;
1606 	}
1607 
1608 	if (cpe) {
1609 		mymessage = Q931_SETUP;
1610 	} else {
1611 		mymessage = Q931_FACILITY;
1612 	}
1613 
1614 	return pri_call_apdu_queue(call, mymessage, buffer, end - buffer, NULL);
1615 }
1616 /* End Callername */
1617 
1618 /* MWI related encode and decode functions */
1619 
1620 /*!
1621  * \internal
1622  * \brief Encode the Q.SIG MWIActivate invoke message.
1623  *
1624  * \param ctrl D channel controller for diagnostic messages or global options.
1625  * \param pos Starting position to encode the facility ie contents.
1626  * \param end End of facility ie contents encoding data buffer.
1627  * \param req Served user setup request information.
1628  *
1629  * \retval Start of the next ASN.1 component to encode on success.
1630  * \retval NULL on error.
1631  */
enc_qsig_mwi_activate_message(struct pri * ctrl,unsigned char * pos,unsigned char * end,struct pri_sr * req)1632 static unsigned char *enc_qsig_mwi_activate_message(struct pri *ctrl, unsigned char *pos,
1633 	unsigned char *end, struct pri_sr *req)
1634 {
1635 	struct fac_extension_header header;
1636 	struct rose_msg_invoke msg;
1637 
1638 	memset(&header, 0, sizeof(header));
1639 	header.nfe_present = 1;
1640 	header.nfe.source_entity = 0;	/* endPINX */
1641 	header.nfe.destination_entity = 0;	/* endPINX */
1642 	header.interpretation_present = 1;
1643 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
1644 	pos = facility_encode_header(ctrl, pos, end, &header);
1645 	if (!pos) {
1646 		return NULL;
1647 	}
1648 
1649 	memset(&msg, 0, sizeof(msg));
1650 	msg.operation = ROSE_QSIG_MWIActivate;
1651 	msg.invoke_id = get_invokeid(ctrl);
1652 
1653 	/* The called.number is the served user */
1654 	q931_copy_number_to_rose(ctrl, &msg.args.qsig.MWIActivate.served_user_number,
1655 		&req->called.number);
1656 	/*
1657 	 * For now, we will just force the numbering plan to unknown to preserve
1658 	 * the original behaviour.
1659 	 */
1660 	msg.args.qsig.MWIActivate.served_user_number.plan = 0;	/* unknown */
1661 
1662 	msg.args.qsig.MWIActivate.basic_service = 1;	/* speech */
1663 
1664 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1665 
1666 	return pos;
1667 }
1668 
1669 /*!
1670  * \internal
1671  * \brief Encode the Q.SIG MWIDeactivate invoke message.
1672  *
1673  * \param ctrl D channel controller for diagnostic messages or global options.
1674  * \param pos Starting position to encode the facility ie contents.
1675  * \param end End of facility ie contents encoding data buffer.
1676  * \param req Served user setup request information.
1677  *
1678  * \retval Start of the next ASN.1 component to encode on success.
1679  * \retval NULL on error.
1680  */
enc_qsig_mwi_deactivate_message(struct pri * ctrl,unsigned char * pos,unsigned char * end,struct pri_sr * req)1681 static unsigned char *enc_qsig_mwi_deactivate_message(struct pri *ctrl,
1682 	unsigned char *pos, unsigned char *end, struct pri_sr *req)
1683 {
1684 	struct fac_extension_header header;
1685 	struct rose_msg_invoke msg;
1686 
1687 	memset(&header, 0, sizeof(header));
1688 	header.nfe_present = 1;
1689 	header.nfe.source_entity = 0;	/* endPINX */
1690 	header.nfe.destination_entity = 0;	/* endPINX */
1691 	header.interpretation_present = 1;
1692 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
1693 	pos = facility_encode_header(ctrl, pos, end, &header);
1694 	if (!pos) {
1695 		return NULL;
1696 	}
1697 
1698 	memset(&msg, 0, sizeof(msg));
1699 	msg.operation = ROSE_QSIG_MWIDeactivate;
1700 	msg.invoke_id = get_invokeid(ctrl);
1701 
1702 	/* The called.number is the served user */
1703 	q931_copy_number_to_rose(ctrl, &msg.args.qsig.MWIDeactivate.served_user_number,
1704 		&req->called.number);
1705 	/*
1706 	 * For now, we will just force the numbering plan to unknown to preserve
1707 	 * the original behaviour.
1708 	 */
1709 	msg.args.qsig.MWIDeactivate.served_user_number.plan = 0;	/* unknown */
1710 
1711 	msg.args.qsig.MWIDeactivate.basic_service = 1;	/* speech */
1712 
1713 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1714 
1715 	return pos;
1716 }
1717 
1718 /*!
1719  * \brief Encode and queue the Q.SIG MWIActivate/MWIDeactivate invoke message.
1720  *
1721  * \param ctrl D channel controller for diagnostic messages or global options.
1722  * \param call Call leg to queue message.
1723  * \param req Served user setup request information.
1724  * \param activate Nonzero to do the activate message.
1725  *
1726  * \retval 0 on success.
1727  * \retval -1 on error.
1728  */
mwi_message_send(struct pri * ctrl,q931_call * call,struct pri_sr * req,int activate)1729 int mwi_message_send(struct pri *ctrl, q931_call *call, struct pri_sr *req, int activate)
1730 {
1731 	unsigned char buffer[255];
1732 	unsigned char *end;
1733 
1734 	if (!req->called.number.valid || !req->called.number.str[0]) {
1735 		return -1;
1736 	}
1737 
1738 	if (activate) {
1739 		end = enc_qsig_mwi_activate_message(ctrl, buffer, buffer + sizeof(buffer), req);
1740 	} else {
1741 		end =
1742 			enc_qsig_mwi_deactivate_message(ctrl, buffer, buffer + sizeof(buffer), req);
1743 	}
1744 	if (!end) {
1745 		return -1;
1746 	}
1747 
1748 	return pri_call_apdu_queue(call, Q931_SETUP, buffer, end - buffer, NULL);
1749 }
1750 
1751 /*!
1752  * \internal
1753  * \brief Encode a MWI indication.
1754  *
1755  * \param ctrl D channel controller for diagnostic messages or global options.
1756  * \param pos Starting position to encode the facility ie contents.
1757  * \param end End of facility ie contents encoding data buffer.
1758  * \param vm_id Controlling party number (NULL if not present).
1759  * \param basic_service Basic service enum (-1 if not present).
1760  * \param num_messages NumberOfMessages (-1 if not present).
1761  * \param caller_id Controlling party privided number (NULL if not present).
1762  * \param timestamp When message left. (Generalized Time format, NULL if not present)
1763  * \param message_reference Message reference number (-1 if not present).
1764  * \param message_status Message status: added(0), removed(1).
1765  *
1766  * \retval Start of the next ASN.1 component to encode on success.
1767  * \retval NULL on error.
1768  */
enc_etsi_mwi_indicate_message(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct pri_party_id * vm_id,int basic_service,int num_messages,const struct pri_party_id * caller_id,const char * timestamp,int message_reference,int message_status)1769 static unsigned char *enc_etsi_mwi_indicate_message(struct pri *ctrl, unsigned char *pos,
1770 	unsigned char *end, const struct pri_party_id *vm_id, int basic_service,
1771 	int num_messages, const struct pri_party_id *caller_id, const char *timestamp,
1772 	int message_reference, int message_status)
1773 {
1774 	struct rose_msg_invoke msg;
1775 	struct q931_party_number number;
1776 
1777 	pos = facility_encode_header(ctrl, pos, end, NULL);
1778 	if (!pos) {
1779 		return NULL;
1780 	}
1781 
1782 	memset(&msg, 0, sizeof(msg));
1783 	msg.operation = ROSE_ETSI_MWIIndicate;
1784 	msg.invoke_id = get_invokeid(ctrl);
1785 
1786 	if (vm_id && vm_id->number.valid) {
1787 		pri_copy_party_number_to_q931(&number, &vm_id->number);
1788 		q931_copy_number_to_rose(ctrl, &msg.args.etsi.MWIIndicate.controlling_user_number,
1789 			&number);
1790 	}
1791 	if (-1 < basic_service) {
1792 		msg.args.etsi.MWIIndicate.basic_service_present = 1;
1793 		msg.args.etsi.MWIIndicate.basic_service = basic_service;
1794 	}
1795 	if (-1 < num_messages) {
1796 		msg.args.etsi.MWIIndicate.number_of_messages_present = 1;
1797 		msg.args.etsi.MWIIndicate.number_of_messages = num_messages;
1798 	}
1799 	if (caller_id && caller_id->number.valid) {
1800 		pri_copy_party_number_to_q931(&number, &caller_id->number);
1801 		q931_copy_number_to_rose(ctrl,
1802 			&msg.args.etsi.MWIIndicate.controlling_user_provided_number, &number);
1803 	}
1804 	if (timestamp && timestamp[0]) {
1805 		msg.args.etsi.MWIIndicate.time_present = 1;
1806 		libpri_copy_string((char *) msg.args.etsi.MWIIndicate.time.str, timestamp,
1807 			sizeof(msg.args.etsi.MWIIndicate.time.str));
1808 	}
1809 	if (-1 < message_reference) {
1810 		msg.args.etsi.MWIIndicate.message_id_present = 1;
1811 		msg.args.etsi.MWIIndicate.message_id.reference_number = message_reference;
1812 		msg.args.etsi.MWIIndicate.message_id.status = message_status;
1813 	}
1814 
1815 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1816 
1817 	return pos;
1818 }
1819 
1820 /*!
1821  * \internal
1822  * \brief Encode and queue a MWI indication.
1823  *
1824  * \param ctrl D channel controller.
1825  * \param call Call leg to queue message.
1826  * \param vm_id Voicemail system number (NULL if not present).
1827  * \param basic_service Basic service enum (-1 if not present).
1828  * \param num_messages NumberOfMessages (-1 if not present).
1829  * \param caller_id Party leaving message (NULL if not present).
1830  * \param timestamp When message left. (Generalized Time format, NULL if not present)
1831  * \param message_reference Message reference number (-1 if not present).
1832  * \param message_status Message status: added(0), removed(1).
1833  *
1834  * \retval 0 on success.
1835  * \retval -1 on error.
1836  */
rose_mwi_indicate_encode(struct pri * ctrl,struct q931_call * call,const struct pri_party_id * vm_id,int basic_service,int num_messages,const struct pri_party_id * caller_id,const char * timestamp,int message_reference,int message_status)1837 static int rose_mwi_indicate_encode(struct pri *ctrl, struct q931_call *call,
1838 	const struct pri_party_id *vm_id, int basic_service, int num_messages,
1839 	const struct pri_party_id *caller_id, const char *timestamp, int message_reference,
1840 	int message_status)
1841 {
1842 	unsigned char buffer[255];
1843 	unsigned char *end;
1844 
1845 	end = enc_etsi_mwi_indicate_message(ctrl, buffer, buffer + sizeof(buffer), vm_id,
1846 		basic_service, num_messages, caller_id, timestamp, message_reference,
1847 		message_status);
1848 	if (!end) {
1849 		return -1;
1850 	}
1851 
1852 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL);
1853 }
1854 
pri_mwi_indicate_v2(struct pri * ctrl,const struct pri_party_id * mailbox,const struct pri_party_id * vm_id,int basic_service,int num_messages,const struct pri_party_id * caller_id,const char * timestamp,int message_reference,int message_status)1855 int pri_mwi_indicate_v2(struct pri *ctrl, const struct pri_party_id *mailbox,
1856 	const struct pri_party_id *vm_id, int basic_service, int num_messages,
1857 	const struct pri_party_id *caller_id, const char *timestamp, int message_reference,
1858 	int message_status)
1859 {
1860 	struct q931_call *call;
1861 	struct q931_party_id called;
1862 
1863 	if (!ctrl) {
1864 		return -1;
1865 	}
1866 
1867 	switch (ctrl->switchtype) {
1868 	case PRI_SWITCH_EUROISDN_E1:
1869 	case PRI_SWITCH_EUROISDN_T1:
1870 		if (!BRI_NT_PTMP(ctrl)) {
1871 			return -1;
1872 		}
1873 		call = ctrl->link.dummy_call;
1874 		if (!call) {
1875 			return -1;
1876 		}
1877 		break;
1878 	default:
1879 		return -1;
1880 	}
1881 
1882 	pri_copy_party_id_to_q931(&called, mailbox);
1883 	q931_party_id_fixup(ctrl, &called);
1884 	if (rose_mwi_indicate_encode(ctrl, call, vm_id, basic_service, num_messages,
1885 		caller_id, timestamp, message_reference, message_status)
1886 		|| q931_facility_called(ctrl, call, &called)) {
1887 		pri_message(ctrl,
1888 			"Could not schedule facility message for MWI indicate message.\n");
1889 		return -1;
1890 	}
1891 
1892 	return 0;
1893 }
1894 
pri_mwi_indicate(struct pri * ctrl,const struct pri_party_id * mailbox,int basic_service,int num_messages,const struct pri_party_id * caller_id,const char * timestamp,int message_reference,int message_status)1895 int pri_mwi_indicate(struct pri *ctrl, const struct pri_party_id *mailbox,
1896 	int basic_service, int num_messages, const struct pri_party_id *caller_id,
1897 	const char *timestamp, int message_reference, int message_status)
1898 {
1899 	return pri_mwi_indicate_v2(ctrl, mailbox, mailbox, basic_service, num_messages,
1900 	caller_id, timestamp, message_reference, message_status);
1901 }
1902 /* End MWI */
1903 
1904 /* EECT functions */
1905 /*!
1906  * \internal
1907  * \brief Encode the NI2 InitiateTransfer invoke message.
1908  *
1909  * \param ctrl D channel controller for diagnostic messages or global options.
1910  * \param pos Starting position to encode the facility ie contents.
1911  * \param end End of facility ie contents encoding data buffer.
1912  * \param call Call leg from which to encode transfer information.
1913  *
1914  * \retval Start of the next ASN.1 component to encode on success.
1915  * \retval NULL on error.
1916  */
enc_ni2_initiate_transfer(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)1917 static unsigned char *enc_ni2_initiate_transfer(struct pri *ctrl, unsigned char *pos,
1918 	unsigned char *end, q931_call *call)
1919 {
1920 	struct rose_msg_invoke msg;
1921 
1922 	pos = facility_encode_header(ctrl, pos, end, NULL);
1923 	if (!pos) {
1924 		return NULL;
1925 	}
1926 
1927 	memset(&msg, 0, sizeof(msg));
1928 	msg.operation = ROSE_NI2_InitiateTransfer;
1929 	msg.invoke_id = get_invokeid(ctrl);
1930 	/* Let's do the trickery to make sure the flag is correct */
1931 	msg.args.ni2.InitiateTransfer.call_reference = call->cr ^ Q931_CALL_REFERENCE_FLAG;
1932 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
1933 
1934 	return pos;
1935 }
1936 
1937 /*!
1938  * \brief Start a 2BCT
1939  *
1940  * \note Called for PRI_SWITCH_NI2, PRI_SWITCH_LUCENT5E, and PRI_SWITCH_ATT4ESS
1941  *
1942  * \param ctrl D channel controller for diagnostic messages or global options.
1943  * \param c1 Q.931 call leg 1
1944  * \param c2 Q.931 call leg 2
1945  *
1946  * \retval 0 on success.
1947  * \retval -1 on error.
1948  */
eect_initiate_transfer(struct pri * ctrl,q931_call * c1,q931_call * c2)1949 int eect_initiate_transfer(struct pri *ctrl, q931_call *c1, q931_call *c2)
1950 {
1951 	unsigned char buffer[255];
1952 	unsigned char *end;
1953 
1954 	end = enc_ni2_initiate_transfer(ctrl, buffer, buffer + sizeof(buffer), c2);
1955 	if (!end) {
1956 		return -1;
1957 	}
1958 
1959 	if (pri_call_apdu_queue(c1, Q931_FACILITY, buffer, end - buffer, NULL)) {
1960 		pri_message(ctrl, "Could not queue APDU in facility message\n");
1961 		return -1;
1962 	}
1963 
1964 	/* Remember that if we queue a facility IE for a facility message we
1965 	 * have to explicitly send the facility message ourselves */
1966 
1967 	if (q931_facility(c1->pri, c1)) {
1968 		pri_message(ctrl, "Could not schedule facility message for call %d\n", c1->cr);
1969 		return -1;
1970 	}
1971 
1972 	return 0;
1973 }
1974 /* End EECT */
1975 
1976 /* QSIG CF CallRerouting */
1977 /*!
1978  * \internal
1979  * \brief Encode the Q.SIG CallRerouting invoke message.
1980  *
1981  * \param ctrl D channel controller for diagnostic messages or global options.
1982  * \param pos Starting position to encode the facility ie contents.
1983  * \param end End of facility ie contents encoding data buffer.
1984  * \param call Q.931 call leg.
1985  * \param calling Call rerouting/deflecting updated caller data.
1986  * \param deflection Call rerouting/deflecting redirection data.
1987  * \param subscription_option Diverting user subscription option to specify if caller is notified.
1988  *
1989  * \note
1990  * deflection->to is the new called number and must always be present.
1991  * \note
1992  * subscription option:
1993  * noNotification(0),
1994  * notificationWithoutDivertedToNr(1),
1995  * notificationWithDivertedToNr(2)
1996  *
1997  * \retval Start of the next ASN.1 component to encode on success.
1998  * \retval NULL on error.
1999  */
enc_qsig_call_rerouting(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,const struct q931_party_id * calling,const struct q931_party_redirecting * deflection,int subscription_option)2000 static unsigned char *enc_qsig_call_rerouting(struct pri *ctrl, unsigned char *pos,
2001 	unsigned char *end, q931_call *call, const struct q931_party_id *calling,
2002 	const struct q931_party_redirecting *deflection, int subscription_option)
2003 {
2004 	struct fac_extension_header header;
2005 	struct rose_msg_invoke msg;
2006 	unsigned char *q931ie_pos;
2007 
2008 	memset(&header, 0, sizeof(header));
2009 	header.nfe_present = 1;
2010 	header.nfe.source_entity = 0;	/* endPINX */
2011 	header.nfe.destination_entity = 0;	/* endPINX */
2012 	header.interpretation_present = 1;
2013 	header.interpretation = 2;	/* rejectAnyUnrecognisedInvokePdu */
2014 	pos = facility_encode_header(ctrl, pos, end, &header);
2015 	if (!pos) {
2016 		return NULL;
2017 	}
2018 
2019 	memset(&msg, 0, sizeof(msg));
2020 	msg.operation = ROSE_QSIG_CallRerouting;
2021 	msg.invoke_id = get_invokeid(ctrl);
2022 
2023 	msg.args.qsig.CallRerouting.rerouting_reason =
2024 		redirectingreason_from_q931(ctrl, deflection->reason);
2025 
2026 	/* calledAddress is the passed in deflection->to address */
2027 	q931_copy_id_address_to_rose(ctrl, &msg.args.qsig.CallRerouting.called, &deflection->to);
2028 
2029 	msg.args.qsig.CallRerouting.diversion_counter = deflection->count;
2030 
2031 	/* pSS1InfoElement */
2032 	q931ie_pos = msg.args.qsig.CallRerouting.q931ie_contents;
2033 	*q931ie_pos++ = 0x04;	/* Bearer Capability IE */
2034 	*q931ie_pos++ = 0x03;	/* len */
2035 	*q931ie_pos++ = 0x80 | call->bc.transcapability;	/* Rxed transfer capability. */
2036 	*q931ie_pos++ = 0x90;	/* circuit mode, 64kbit/s */
2037 	*q931ie_pos++ = 0xa3;	/* level1 protocol, a-law */
2038 	*q931ie_pos++ = 0x95;	/* locking shift to codeset 5 (national use) */
2039 	*q931ie_pos++ = 0x32;	/* Unknown ie */
2040 	*q931ie_pos++ = 0x01;	/* Unknown ie len */
2041 	*q931ie_pos++ = 0x81;	/* Unknown ie body */
2042 	msg.args.qsig.CallRerouting.q931ie.length = q931ie_pos
2043 		- msg.args.qsig.CallRerouting.q931ie_contents;
2044 
2045 	/* lastReroutingNr is the passed in deflection->from.number */
2046 	q931_copy_presented_number_unscreened_to_rose(ctrl,
2047 		&msg.args.qsig.CallRerouting.last_rerouting, &deflection->from.number);
2048 
2049 	msg.args.qsig.CallRerouting.subscription_option = subscription_option;
2050 
2051 	/* callingNumber is the passed in calling->number */
2052 	q931_copy_presented_number_screened_to_rose(ctrl,
2053 		&msg.args.qsig.CallRerouting.calling, &calling->number);
2054 
2055 	/* callingPartySubaddress is the passed in calling->subaddress if valid */
2056 	q931_copy_subaddress_to_rose(ctrl, &msg.args.qsig.CallRerouting.calling_subaddress,
2057 		&calling->subaddress);
2058 
2059 	/* callingName is the passed in calling->name if valid */
2060 	if (calling->name.valid) {
2061 		msg.args.qsig.CallRerouting.calling_name_present = 1;
2062 		q931_copy_name_to_rose(ctrl, &msg.args.qsig.CallRerouting.calling_name,
2063 			&calling->name);
2064 	}
2065 
2066 	if (1 < deflection->count) {
2067 		/* originalCalledNr is the deflection->orig_called.number */
2068 		msg.args.qsig.CallRerouting.original_called_present = 1;
2069 		q931_copy_presented_number_unscreened_to_rose(ctrl,
2070 			&msg.args.qsig.CallRerouting.original_called,
2071 			&deflection->orig_called.number);
2072 
2073 		msg.args.qsig.CallRerouting.original_rerouting_reason_present = 1;
2074 		if (deflection->orig_called.number.valid) {
2075 			msg.args.qsig.CallRerouting.original_rerouting_reason =
2076 				redirectingreason_from_q931(ctrl, deflection->orig_reason);
2077 		} else {
2078 			msg.args.qsig.CallRerouting.original_rerouting_reason =
2079 				QSIG_DIVERT_REASON_UNKNOWN;
2080 		}
2081 
2082 		/* originalCalledName is the deflection->orig_called.name */
2083 		if (deflection->orig_called.name.valid) {
2084 			msg.args.qsig.CallRerouting.original_called_name_present = 1;
2085 			q931_copy_name_to_rose(ctrl,
2086 				&msg.args.qsig.CallRerouting.original_called_name,
2087 				&deflection->orig_called.name);
2088 		}
2089 	}
2090 
2091 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2092 
2093 	return pos;
2094 }
2095 
2096 /*!
2097  * \internal
2098  * \brief Encode the ETSI CallRerouting invoke message.
2099  *
2100  * \param ctrl D channel controller for diagnostic messages or global options.
2101  * \param pos Starting position to encode the facility ie contents.
2102  * \param end End of facility ie contents encoding data buffer.
2103  * \param call Q.931 call leg.
2104  * \param calling Call rerouting/deflecting updated caller data.
2105  * \param deflection Call rerouting/deflecting redirection data.
2106  * \param subscription_option Diverting user subscription option to specify if caller is notified.
2107  *
2108  * \note
2109  * deflection->to is the new called number and must always be present.
2110  * \note
2111  * subscription option:
2112  * noNotification(0),
2113  * notificationWithoutDivertedToNr(1),
2114  * notificationWithDivertedToNr(2)
2115  *
2116  * \retval Start of the next ASN.1 component to encode on success.
2117  * \retval NULL on error.
2118  */
enc_etsi_call_rerouting(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,const struct q931_party_id * calling,const struct q931_party_redirecting * deflection,int subscription_option)2119 static unsigned char *enc_etsi_call_rerouting(struct pri *ctrl, unsigned char *pos,
2120 	unsigned char *end, q931_call *call, const struct q931_party_id *calling,
2121 	const struct q931_party_redirecting *deflection, int subscription_option)
2122 {
2123 	struct rose_msg_invoke msg;
2124 	unsigned char *q931ie_pos;
2125 
2126 	pos = facility_encode_header(ctrl, pos, end, NULL);
2127 	if (!pos) {
2128 		return NULL;
2129 	}
2130 
2131 	memset(&msg, 0, sizeof(msg));
2132 	msg.operation = ROSE_ETSI_CallRerouting;
2133 	msg.invoke_id = get_invokeid(ctrl);
2134 
2135 	msg.args.etsi.CallRerouting.rerouting_reason =
2136 		redirectingreason_from_q931(ctrl, deflection->reason);
2137 
2138 	/* calledAddress is the passed in deflection->to address */
2139 	q931_copy_id_address_to_rose(ctrl, &msg.args.etsi.CallRerouting.called_address,
2140 		&deflection->to);
2141 
2142 	msg.args.etsi.CallRerouting.rerouting_counter = deflection->count;
2143 
2144 	/* q931InfoElement */
2145 	q931ie_pos = msg.args.etsi.CallRerouting.q931ie_contents;
2146 	*q931ie_pos++ = 0x04;	/* Bearer Capability IE */
2147 	*q931ie_pos++ = 0x03;	/* len */
2148 	*q931ie_pos++ = 0x80 | call->bc.transcapability;	/* Rxed transfer capability. */
2149 	*q931ie_pos++ = 0x90;	/* circuit mode, 64kbit/s */
2150 	*q931ie_pos++ = 0xa3;	/* level1 protocol, a-law */
2151 	msg.args.etsi.CallRerouting.q931ie.length = q931ie_pos
2152 		- msg.args.etsi.CallRerouting.q931ie_contents;
2153 
2154 	/* lastReroutingNr is the passed in deflection->from.number */
2155 	q931_copy_presented_number_unscreened_to_rose(ctrl,
2156 		&msg.args.etsi.CallRerouting.last_rerouting, &deflection->from.number);
2157 
2158 	msg.args.etsi.CallRerouting.subscription_option = subscription_option;
2159 
2160 	/* callingPartySubaddress is the passed in calling->subaddress if valid */
2161 	q931_copy_subaddress_to_rose(ctrl, &msg.args.etsi.CallRerouting.calling_subaddress,
2162 		&calling->subaddress);
2163 
2164 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2165 
2166 	return pos;
2167 }
2168 
2169 /*!
2170  * \internal
2171  * \brief Encode the ETSI CallDeflection invoke message.
2172  *
2173  * \param ctrl D channel controller for diagnostic messages or global options.
2174  * \param pos Starting position to encode the facility ie contents.
2175  * \param end End of facility ie contents encoding data buffer.
2176  * \param call Q.931 call leg.
2177  * \param deflection Call deflection address.
2178  *
2179  * \note
2180  * deflection is the new called number and must always be present.
2181  *
2182  * \retval Start of the next ASN.1 component to encode on success.
2183  * \retval NULL on error.
2184  */
enc_etsi_call_deflection(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,const struct q931_party_id * deflection)2185 static unsigned char *enc_etsi_call_deflection(struct pri *ctrl, unsigned char *pos,
2186 	unsigned char *end, q931_call *call, const struct q931_party_id *deflection)
2187 {
2188 	struct rose_msg_invoke msg;
2189 
2190 	pos = facility_encode_header(ctrl, pos, end, NULL);
2191 	if (!pos) {
2192 		return NULL;
2193 	}
2194 
2195 	memset(&msg, 0, sizeof(msg));
2196 	msg.operation = ROSE_ETSI_CallDeflection;
2197 	msg.invoke_id = get_invokeid(ctrl);
2198 
2199 	/* deflectionAddress is the passed in deflection->to address */
2200 	q931_copy_id_address_to_rose(ctrl, &msg.args.etsi.CallDeflection.deflection,
2201 		deflection);
2202 
2203 	msg.args.etsi.CallDeflection.presentation_allowed_to_diverted_to_user_present = 1;
2204 	switch (deflection->number.presentation & PRI_PRES_RESTRICTION) {
2205 	case PRI_PRES_ALLOWED:
2206 		msg.args.etsi.CallDeflection.presentation_allowed_to_diverted_to_user = 1;
2207 		break;
2208 	default:
2209 	case PRI_PRES_UNAVAILABLE:
2210 	case PRI_PRES_RESTRICTED:
2211 		break;
2212 	}
2213 
2214 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2215 
2216 	return pos;
2217 }
2218 
2219 /*!
2220  * \internal
2221  * \brief Encode and queue the CallRerouting/CallDeflection message.
2222  *
2223  * \param ctrl D channel controller.
2224  * \param call Q.931 call leg.
2225  * \param caller Call rerouting/deflecting updated caller data. (NULL if data not updated.)
2226  * \param deflection Call rerouting/deflecting redirection data.
2227  * \param subscription_option Diverting user subscription option to specify if caller is notified.
2228  *
2229  * \note
2230  * deflection->to is the new called number and must always be present.
2231  * \note
2232  * subscription option:
2233  * noNotification(0),
2234  * notificationWithoutDivertedToNr(1),
2235  * notificationWithDivertedToNr(2)
2236  *
2237  * \retval 0 on success.
2238  * \retval -1 on error.
2239  */
rose_reroute_request_encode(struct pri * ctrl,q931_call * call,const struct q931_party_id * caller,const struct q931_party_redirecting * deflection,int subscription_option)2240 static int rose_reroute_request_encode(struct pri *ctrl, q931_call *call,
2241 	const struct q931_party_id *caller, const struct q931_party_redirecting *deflection,
2242 	int subscription_option)
2243 {
2244 	unsigned char buffer[256];
2245 	unsigned char *end;
2246 
2247 	if (!caller) {
2248 		/*
2249 		 * We are deflecting an incoming call back to the network.
2250 		 * Therefore, the Caller-ID is the remote party.
2251 		 */
2252 		caller = &call->remote_id;
2253 	}
2254 
2255 	switch (ctrl->switchtype) {
2256 	case PRI_SWITCH_EUROISDN_E1:
2257 	case PRI_SWITCH_EUROISDN_T1:
2258 		if (PTMP_MODE(ctrl)) {
2259 			end =
2260 				enc_etsi_call_deflection(ctrl, buffer, buffer + sizeof(buffer), call,
2261 					&deflection->to);
2262 		} else {
2263 			end =
2264 				enc_etsi_call_rerouting(ctrl, buffer, buffer + sizeof(buffer), call,
2265 					caller, deflection, subscription_option);
2266 		}
2267 		break;
2268 	case PRI_SWITCH_QSIG:
2269 		end =
2270 			enc_qsig_call_rerouting(ctrl, buffer, buffer + sizeof(buffer), call, caller,
2271 				deflection, subscription_option);
2272 		break;
2273 	default:
2274 		return -1;
2275 	}
2276 	if (!end) {
2277 		return -1;
2278 	}
2279 
2280 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL);
2281 }
2282 
2283 /*!
2284  * \brief Send the CallRerouting/CallDeflection message.
2285  *
2286  * \param ctrl D channel controller.
2287  * \param call Q.931 call leg.
2288  * \param caller Call rerouting/deflecting updated caller data. (NULL if data not updated.)
2289  * \param deflection Call rerouting/deflecting redirection data.
2290  * \param subscription_option Diverting user subscription option to specify if caller is notified.
2291  *
2292  * \note
2293  * deflection->to is the new called number and must always be present.
2294  * \note
2295  * subscription option:
2296  * noNotification(0),
2297  * notificationWithoutDivertedToNr(1),
2298  * notificationWithDivertedToNr(2)
2299  *
2300  * \retval 0 on success.
2301  * \retval -1 on error.
2302  */
send_reroute_request(struct pri * ctrl,q931_call * call,const struct q931_party_id * caller,const struct q931_party_redirecting * deflection,int subscription_option)2303 int send_reroute_request(struct pri *ctrl, q931_call *call,
2304 	const struct q931_party_id *caller, const struct q931_party_redirecting *deflection,
2305 	int subscription_option)
2306 {
2307 	if (!deflection->to.number.str[0]) {
2308 		/* Must have a deflect to number.  That is the point of deflection. */
2309 		return -1;
2310 	}
2311 	if (rose_reroute_request_encode(ctrl, call, caller, deflection, subscription_option)
2312 		|| q931_facility(ctrl, call)) {
2313 		pri_message(ctrl,
2314 			"Could not schedule facility message for CallRerouting/CallDeflection message.\n");
2315 		return -1;
2316 	}
2317 
2318 	return 0;
2319 }
2320 
2321 /*!
2322  * \brief Send the Q.SIG CallRerouting invoke message.
2323  *
2324  * \param ctrl D channel controller for diagnostic messages or global options.
2325  * \param call Q.931 call leg.
2326  * \param dest Destination number.
2327  * \param original Original called number.
2328  * \param reason Rerouting reason: cfu, cfb, cfnr
2329  *
2330  * \retval 0 on success.
2331  * \retval -1 on error.
2332  */
qsig_cf_callrerouting(struct pri * ctrl,q931_call * call,const char * dest,const char * original,const char * reason)2333 int qsig_cf_callrerouting(struct pri *ctrl, q931_call *call, const char *dest,
2334 	const char *original, const char *reason)
2335 {
2336 	struct q931_party_redirecting reroute;
2337 
2338 	q931_party_redirecting_init(&reroute);
2339 
2340 	/* Rerouting to the dest number. */
2341 	reroute.to.number.valid = 1;
2342 	reroute.to.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
2343 	reroute.to.number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2344 	libpri_copy_string(reroute.to.number.str, dest, sizeof(reroute.to.number.str));
2345 
2346 	/* Rerouting from the original number. */
2347 	if (original) {
2348 		reroute.from.number.valid = 1;
2349 		reroute.from.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
2350 		libpri_copy_string(reroute.from.number.str, original, sizeof(reroute.from.number.str));
2351 	} else {
2352 		q931_party_address_to_id(&reroute.from, &call->called);
2353 	}
2354 	reroute.from.number.presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
2355 
2356 	/* Decode the rerouting reason. */
2357 	reroute.reason = PRI_REDIR_UNKNOWN;
2358 	if (!reason) {
2359 		/* No reason for rerouting given. */
2360 	} else if (!strcasecmp(reason, "cfu")) {
2361 		reroute.reason = PRI_REDIR_UNCONDITIONAL;
2362 	} else if (!strcasecmp(reason, "cfb")) {
2363 		reroute.reason = PRI_REDIR_FORWARD_ON_BUSY;
2364 	} else if (!strcasecmp(reason, "cfnr")) {
2365 		reroute.reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
2366 	}
2367 
2368 	reroute.count = (call->redirecting.count < PRI_MAX_REDIRECTS)
2369 		? call->redirecting.count + 1 : PRI_MAX_REDIRECTS;
2370 
2371 	if (!call->redirecting.orig_called.number.valid) {
2372 		/*
2373 		 * Since we do not already have an originally called party, we
2374 		 * must either be the first redirected to party or this call
2375 		 * has not been redirected before.
2376 		 *
2377 		 * Preserve who redirected to us as the originally called party.
2378 		 */
2379 		reroute.orig_called = call->redirecting.from;
2380 		reroute.orig_reason = call->redirecting.reason;
2381 	} else {
2382 		reroute.orig_called = call->redirecting.orig_called;
2383 		reroute.orig_reason = call->redirecting.orig_reason;
2384 	}
2385 
2386 	return send_reroute_request(ctrl, call, NULL, &reroute, 0 /* noNotification */);
2387 }
2388 /* End QSIG CC-CallRerouting */
2389 
2390 /*
2391  * From Mantis issue 7778 description: (ETS 300 258, ISO 13863)
2392  * After both legs of the call are setup and Asterisk has a successful "tromboned" or bridged call ...
2393  * Asterisk sees both 'B' channels (from trombone) are on same PRI/technology and initiates "Path Replacement" events
2394  * a. Asterisk sends "Transfer Complete" messages to both call legs
2395  * b. QSIG Switch sends "PathReplacement" message on one of the legs (random 1-10sec timer expires - 1st leg to send is it!)
2396  * c. Asterisk rebroadcasts "PathReplacement" message to other call leg
2397  * d. QSIG Switch sends "Disconnect" message on one of the legs (same random timer sequence as above)
2398  * e. Asterisk rebroadcasts "Disconnect" message to other call leg
2399  * f. QSIG Switch disconnects Asterisk call legs - callers are now within QSIG switch
2400  *
2401  * Just need to resend the message to the other tromboned leg of the call.
2402  */
anfpr_pathreplacement_respond(struct pri * ctrl,q931_call * call,q931_ie * ie)2403 static int anfpr_pathreplacement_respond(struct pri *ctrl, q931_call *call, q931_ie *ie)
2404 {
2405 	int res;
2406 
2407 	pri_call_apdu_queue_cleanup(call->bridged_call);
2408 
2409 	/* Send message */
2410 	res = pri_call_apdu_queue(call->bridged_call, Q931_FACILITY, ie->data, ie->len, NULL);
2411 	if (res) {
2412 		pri_message(ctrl, "Could not queue ADPU in facility message\n");
2413 		return -1;
2414 	}
2415 
2416 	/* Remember that if we queue a facility IE for a facility message we
2417 	 * have to explicitly send the facility message ourselves */
2418 
2419 	res = q931_facility(call->bridged_call->pri, call->bridged_call);
2420 	if (res) {
2421 		pri_message(ctrl, "Could not schedule facility message for call %d\n",
2422 			call->bridged_call->cr);
2423 		return -1;
2424 	}
2425 
2426 	return 0;
2427 }
2428 
2429 /* AFN-PR */
2430 /*!
2431  * \brief Start a Q.SIG path replacement.
2432  *
2433  * \note Called for PRI_SWITCH_QSIG
2434  *
2435  * \note Did all the tests to see if we're on the same PRI and
2436  * are on a compatible switchtype.
2437  *
2438  * \param ctrl D channel controller for diagnostic messages or global options.
2439  * \param c1 Q.931 call leg 1
2440  * \param c2 Q.931 call leg 2
2441  *
2442  * \retval 0 on success.
2443  * \retval -1 on error.
2444  */
anfpr_initiate_transfer(struct pri * ctrl,q931_call * c1,q931_call * c2)2445 int anfpr_initiate_transfer(struct pri *ctrl, q931_call *c1, q931_call *c2)
2446 {
2447 	unsigned char buffer[255];
2448 	unsigned char *pos;
2449 	unsigned char *end;
2450 	int res;
2451 	struct fac_extension_header header;
2452 	struct rose_msg_invoke msg;
2453 
2454 	end = buffer + sizeof(buffer);
2455 
2456 	memset(&header, 0, sizeof(header));
2457 	header.nfe_present = 1;
2458 	header.nfe.source_entity = 0;	/* endPINX */
2459 	header.nfe.destination_entity = 0;	/* endPINX */
2460 	header.interpretation_present = 1;
2461 	header.interpretation = 2;	/* rejectAnyUnrecognisedInvokePdu */
2462 	pos = facility_encode_header(ctrl, buffer, end, &header);
2463 	if (!pos) {
2464 		return -1;
2465 	}
2466 
2467 	memset(&msg, 0, sizeof(msg));
2468 	msg.operation = ROSE_QSIG_CallTransferComplete;
2469 	msg.invoke_id = get_invokeid(ctrl);
2470 	msg.args.qsig.CallTransferComplete.end_designation = 0;	/* primaryEnd */
2471 	msg.args.qsig.CallTransferComplete.redirection.presentation = 1;	/* presentationRestricted */
2472 	msg.args.qsig.CallTransferComplete.call_status = 1;	/* alerting */
2473 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2474 	if (!pos) {
2475 		return -1;
2476 	}
2477 
2478 	res = pri_call_apdu_queue(c1, Q931_FACILITY, buffer, pos - buffer, NULL);
2479 	if (res) {
2480 		pri_message(ctrl, "Could not queue ADPU in facility message\n");
2481 		return -1;
2482 	}
2483 
2484 	/* Remember that if we queue a facility IE for a facility message we
2485 	 * have to explicitly send the facility message ourselves */
2486 
2487 	res = q931_facility(c1->pri, c1);
2488 	if (res) {
2489 		pri_message(ctrl, "Could not schedule facility message for call %d\n", c1->cr);
2490 		return -1;
2491 	}
2492 
2493 	/* Reuse the previous message header */
2494 	pos = facility_encode_header(ctrl, buffer, end, &header);
2495 	if (!pos) {
2496 		return -1;
2497 	}
2498 
2499 	/* Update the previous message */
2500 	msg.invoke_id = get_invokeid(ctrl);
2501 	msg.args.qsig.CallTransferComplete.end_designation = 1;	/* secondaryEnd */
2502 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2503 	if (!pos) {
2504 		return -1;
2505 	}
2506 
2507 	res = pri_call_apdu_queue(c2, Q931_FACILITY, buffer, pos - buffer, NULL);
2508 	if (res) {
2509 		pri_message(ctrl, "Could not queue ADPU in facility message\n");
2510 		return -1;
2511 	}
2512 
2513 	/* Remember that if we queue a facility IE for a facility message we
2514 	 * have to explicitly send the facility message ourselves */
2515 
2516 	res = q931_facility(c2->pri, c2);
2517 	if (res) {
2518 		pri_message(ctrl, "Could not schedule facility message for call %d\n", c2->cr);
2519 		return -1;
2520 	}
2521 
2522 	return 0;
2523 }
2524 /* End AFN-PR */
2525 
2526 /*!
2527  * \internal
2528  * \brief Encode ETSI ExplicitEctExecute message.
2529  *
2530  * \param ctrl D channel controller for diagnostic messages or global options.
2531  * \param pos Starting position to encode the facility ie contents.
2532  * \param end End of facility ie contents encoding data buffer.
2533  * \param link_id Identifier of other call involved in transfer.
2534  *
2535  * \retval Start of the next ASN.1 component to encode on success.
2536  * \retval NULL on error.
2537  */
enc_etsi_ect_explicit_execute(struct pri * ctrl,unsigned char * pos,unsigned char * end,int link_id)2538 static unsigned char *enc_etsi_ect_explicit_execute(struct pri *ctrl, unsigned char *pos,
2539 	unsigned char *end, int link_id)
2540 {
2541 	struct rose_msg_invoke msg;
2542 
2543 	pos = facility_encode_header(ctrl, pos, end, NULL);
2544 	if (!pos) {
2545 		return NULL;
2546 	}
2547 
2548 	memset(&msg, 0, sizeof(msg));
2549 	msg.invoke_id = get_invokeid(ctrl);
2550 	msg.operation = ROSE_ETSI_ExplicitEctExecute;
2551 
2552 	msg.args.etsi.ExplicitEctExecute.link_id = link_id;
2553 
2554 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2555 
2556 	return pos;
2557 }
2558 
2559 /*!
2560  * \internal
2561  * \brief ECT LinkId response callback function.
2562  *
2563  * \param reason Reason callback is called.
2564  * \param ctrl D channel controller.
2565  * \param call Q.931 call leg.
2566  * \param apdu APDU queued entry.  Do not change!
2567  * \param msg APDU response message data.  (NULL if was not the reason called.)
2568  *
2569  * \return TRUE if no more responses are expected.
2570  */
etsi_ect_link_id_rsp(enum APDU_CALLBACK_REASON reason,struct pri * ctrl,struct q931_call * call,struct apdu_event * apdu,const struct apdu_msg_data * msg)2571 static int etsi_ect_link_id_rsp(enum APDU_CALLBACK_REASON reason, struct pri *ctrl,
2572 	struct q931_call *call, struct apdu_event *apdu, const struct apdu_msg_data *msg)
2573 {
2574 	unsigned char buffer[256];
2575 	unsigned char *end;
2576 	q931_call *call_2;
2577 
2578 	switch (reason) {
2579 	case APDU_CALLBACK_REASON_MSG_RESULT:
2580 		call_2 = apdu->response.user.ptr;
2581 		if (!q931_is_call_valid(ctrl, call_2)) {
2582 			/* Call is no longer present. */
2583 			break;
2584 		}
2585 
2586 		end = enc_etsi_ect_explicit_execute(ctrl, buffer, buffer + sizeof(buffer),
2587 			msg->response.result->args.etsi.EctLinkIdRequest.link_id);
2588 		if (!end) {
2589 			break;
2590 		}
2591 
2592 		/* Remember that if we queue a facility IE for a facility message we
2593 		 * have to explicitly send the facility message ourselves */
2594 		if (pri_call_apdu_queue(call_2, Q931_FACILITY, buffer, end - buffer, NULL)
2595 			|| q931_facility(call_2->pri, call_2)) {
2596 			pri_message(ctrl, "Could not schedule facility message for call %d\n",
2597 				call_2->cr);
2598 		}
2599 		break;
2600 	default:
2601 		break;
2602 	}
2603 	return 1;
2604 }
2605 
2606 /*!
2607  * \internal
2608  * \brief Encode ETSI ECT LinkId request message.
2609  *
2610  * \param ctrl D channel controller for diagnostic messages or global options.
2611  * \param pos Starting position to encode the facility ie contents.
2612  * \param end End of facility ie contents encoding data buffer.
2613  *
2614  * \retval Start of the next ASN.1 component to encode on success.
2615  * \retval NULL on error.
2616  */
enc_etsi_ect_link_id_req(struct pri * ctrl,unsigned char * pos,unsigned char * end)2617 static unsigned char *enc_etsi_ect_link_id_req(struct pri *ctrl, unsigned char *pos,
2618 	unsigned char *end)
2619 {
2620 	struct rose_msg_invoke msg;
2621 
2622 	pos = facility_encode_header(ctrl, pos, end, NULL);
2623 	if (!pos) {
2624 		return NULL;
2625 	}
2626 
2627 	memset(&msg, 0, sizeof(msg));
2628 	msg.invoke_id = get_invokeid(ctrl);
2629 	msg.operation = ROSE_ETSI_EctLinkIdRequest;
2630 
2631 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2632 
2633 	return pos;
2634 }
2635 
2636 /*!
2637  * \brief Start an Explicit Call Transfer (ECT) sequence between the two calls.
2638  *
2639  * \param ctrl D channel controller for diagnostic messages or global options.
2640  * \param call_1 Q.931 call leg 1
2641  * \param call_2 Q.931 call leg 2
2642  *
2643  * \retval 0 on success.
2644  * \retval -1 on error.
2645  */
etsi_initiate_transfer(struct pri * ctrl,q931_call * call_1,q931_call * call_2)2646 int etsi_initiate_transfer(struct pri *ctrl, q931_call *call_1, q931_call *call_2)
2647 {
2648 	unsigned char buffer[256];
2649 	unsigned char *end;
2650 	struct apdu_callback_data response;
2651 
2652 	end = enc_etsi_ect_link_id_req(ctrl, buffer, buffer + sizeof(buffer));
2653 	if (!end) {
2654 		return -1;
2655 	}
2656 
2657 	memset(&response, 0, sizeof(response));
2658 	response.invoke_id = ctrl->last_invoke;
2659 	response.timeout_time = ctrl->timers[PRI_TIMER_T_RESPONSE];
2660 	response.callback = etsi_ect_link_id_rsp;
2661 	response.user.ptr = call_2;
2662 
2663 	/* Remember that if we queue a facility IE for a facility message we
2664 	 * have to explicitly send the facility message ourselves */
2665 	if (pri_call_apdu_queue(call_1, Q931_FACILITY, buffer, end - buffer, &response)
2666 		|| q931_facility(call_1->pri, call_1)) {
2667 		pri_message(ctrl, "Could not schedule facility message for call %d\n",
2668 			call_1->cr);
2669 		return -1;
2670 	}
2671 
2672 	return 0;
2673 }
2674 
2675 /*!
2676  * \internal
2677  * \brief Encode ETSI ECT LinkId result respnose message.
2678  *
2679  * \param ctrl D channel controller for diagnostic messages or global options.
2680  * \param pos Starting position to encode the facility ie contents.
2681  * \param end End of facility ie contents encoding data buffer.
2682  * \param invoke_id Invoke id to put in result message.
2683  * \param link_id Requested link id to put in result message.
2684  *
2685  * \retval Start of the next ASN.1 component to encode on success.
2686  * \retval NULL on error.
2687  */
enc_etsi_ect_link_id_rsp(struct pri * ctrl,unsigned char * pos,unsigned char * end,int invoke_id,int link_id)2688 static unsigned char *enc_etsi_ect_link_id_rsp(struct pri *ctrl, unsigned char *pos,
2689 	unsigned char *end, int invoke_id, int link_id)
2690 {
2691 	struct rose_msg_result msg;
2692 
2693 	pos = facility_encode_header(ctrl, pos, end, NULL);
2694 	if (!pos) {
2695 		return NULL;
2696 	}
2697 
2698 	memset(&msg, 0, sizeof(msg));
2699 	msg.invoke_id = invoke_id;
2700 	msg.operation = ROSE_ETSI_EctLinkIdRequest;
2701 
2702 	msg.args.etsi.EctLinkIdRequest.link_id = link_id;
2703 
2704 	pos = rose_encode_result(ctrl, pos, end, &msg);
2705 
2706 	return pos;
2707 }
2708 
2709 /*!
2710  * \internal
2711  * \brief Send EctLinkIdRequest result response message.
2712  *
2713  * \param ctrl D channel controller.
2714  * \param call Q.931 call leg.
2715  * \param invoke_id Invoke id to put in result message.
2716  *
2717  * \retval 0 on success.
2718  * \retval -1 on error.
2719  */
send_ect_link_id_rsp(struct pri * ctrl,q931_call * call,int invoke_id)2720 static int send_ect_link_id_rsp(struct pri *ctrl, q931_call *call, int invoke_id)
2721 {
2722 	unsigned char buffer[256];
2723 	unsigned char *end;
2724 
2725 	end = enc_etsi_ect_link_id_rsp(ctrl, buffer, buffer + sizeof(buffer), invoke_id,
2726 		call->link_id);
2727 	if (!end) {
2728 		return -1;
2729 	}
2730 
2731 	/* Remember that if we queue a facility IE for a facility message we
2732 	 * have to explicitly send the facility message ourselves */
2733 	if (pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL)
2734 		|| q931_facility(call->pri, call)) {
2735 		pri_message(ctrl, "Could not schedule facility message for call %d\n", call->cr);
2736 		return -1;
2737 	}
2738 
2739 	return 0;
2740 }
2741 
2742 /*!
2743  * \internal
2744  * \brief Process the received ETSI EctExecute message.
2745  *
2746  * \param ctrl D channel controller.
2747  * \param call Q.931 call leg.
2748  * \param invoke_id Invoke id to put in response message.
2749  *
2750  * \details
2751  * 1) Find the active call implied by the transfer request.
2752  * 2) Create the PRI_SUBCMD_TRANSFER_CALL event.
2753  *
2754  * \retval ROSE_ERROR_None on success.
2755  * \retval error_code on error.
2756  */
etsi_ect_execute_transfer(struct pri * ctrl,q931_call * call,int invoke_id)2757 static enum rose_error_code etsi_ect_execute_transfer(struct pri *ctrl, q931_call *call, int invoke_id)
2758 {
2759 	enum rose_error_code error_code;
2760 	struct pri_subcommand *subcmd;
2761 	q931_call *call_active;
2762 
2763 	switch (call->ourcallstate) {
2764 	case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
2765 	case Q931_CALL_STATE_CALL_DELIVERED:
2766 	case Q931_CALL_STATE_CALL_RECEIVED:
2767 	case Q931_CALL_STATE_CONNECT_REQUEST:
2768 	case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
2769 	case Q931_CALL_STATE_ACTIVE:
2770 		if (call->master_call->hold_state != Q931_HOLD_STATE_CALL_HELD) {
2771 			/* EctExecute must be sent on the held call. */
2772 			error_code = ROSE_ERROR_Gen_InvalidCallState;
2773 			break;
2774 		}
2775 		/* Held call is being transferred. */
2776 		call_active = q931_find_held_active_call(ctrl, call);
2777 		if (!call_active) {
2778 			error_code = ROSE_ERROR_Gen_NotAvailable;
2779 			break;
2780 		}
2781 
2782 		/* Setup transfer subcommand */
2783 		subcmd = q931_alloc_subcommand(ctrl);
2784 		if (!subcmd) {
2785 			error_code = ROSE_ERROR_Gen_NotAvailable;
2786 			break;
2787 		}
2788 		subcmd->cmd = PRI_SUBCMD_TRANSFER_CALL;
2789 		subcmd->u.transfer.call_1 = call->master_call;
2790 		subcmd->u.transfer.call_2 = call_active;
2791 		subcmd->u.transfer.is_call_1_held = 1;
2792 		subcmd->u.transfer.is_call_2_held = 0;
2793 		subcmd->u.transfer.invoke_id = invoke_id;
2794 
2795 		error_code = ROSE_ERROR_None;
2796 		break;
2797 	default:
2798 		error_code = ROSE_ERROR_Gen_InvalidCallState;
2799 		break;
2800 	}
2801 
2802 	return error_code;
2803 }
2804 
2805 /*!
2806  * \internal
2807  * \brief Process the received ETSI ExplicitEctExecute message.
2808  *
2809  * \param ctrl D channel controller.
2810  * \param call Q.931 call leg.
2811  * \param invoke_id Invoke id to put in response message.
2812  * \param link_id Link id of the other call involved in the transfer.
2813  *
2814  * \details
2815  * 1) Find the other call specified by the link_id in transfer request.
2816  * 2) Create the PRI_SUBCMD_TRANSFER_CALL event.
2817  *
2818  * \retval ROSE_ERROR_None on success.
2819  * \retval error_code on error.
2820  */
etsi_explicit_ect_execute_transfer(struct pri * ctrl,q931_call * call,int invoke_id,int link_id)2821 static enum rose_error_code etsi_explicit_ect_execute_transfer(struct pri *ctrl, q931_call *call, int invoke_id, int link_id)
2822 {
2823 	enum rose_error_code error_code;
2824 	struct pri_subcommand *subcmd;
2825 	q931_call *call_2;
2826 
2827 	switch (call->ourcallstate) {
2828 	case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
2829 	case Q931_CALL_STATE_CALL_DELIVERED:
2830 	case Q931_CALL_STATE_CALL_RECEIVED:
2831 	case Q931_CALL_STATE_CONNECT_REQUEST:
2832 	case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
2833 	case Q931_CALL_STATE_ACTIVE:
2834 		call_2 = q931_find_link_id_call(ctrl, link_id);
2835 		if (!call_2 || call_2 == call->master_call) {
2836 			error_code = ROSE_ERROR_Gen_NotAvailable;
2837 			break;
2838 		}
2839 
2840 		/* Setup transfer subcommand */
2841 		subcmd = q931_alloc_subcommand(ctrl);
2842 		if (!subcmd) {
2843 			error_code = ROSE_ERROR_Gen_NotAvailable;
2844 			break;
2845 		}
2846 		subcmd->cmd = PRI_SUBCMD_TRANSFER_CALL;
2847 		subcmd->u.transfer.call_1 = call->master_call;
2848 		subcmd->u.transfer.call_2 = call_2;
2849 		subcmd->u.transfer.is_call_1_held =
2850 			(call->master_call->hold_state == Q931_HOLD_STATE_CALL_HELD) ? 1 : 0;
2851 		subcmd->u.transfer.is_call_2_held =
2852 			(call_2->hold_state == Q931_HOLD_STATE_CALL_HELD) ? 1 : 0;
2853 		subcmd->u.transfer.invoke_id = invoke_id;
2854 
2855 		error_code = ROSE_ERROR_None;
2856 		break;
2857 	default:
2858 		error_code = ROSE_ERROR_Gen_InvalidCallState;
2859 		break;
2860 	}
2861 
2862 	return error_code;
2863 }
2864 
2865 /* ===== Call Transfer Supplementary Service (ECMA-178) ===== */
2866 
2867 /*!
2868  * \internal
2869  * \brief Encode the Q.SIG CallTransferComplete invoke message.
2870  *
2871  * \param ctrl D channel controller for diagnostic messages or global options.
2872  * \param pos Starting position to encode the facility ie contents.
2873  * \param end End of facility ie contents encoding data buffer.
2874  * \param call Call leg from which to encode call transfer.
2875  * \param call_status TRUE if call is alerting.
2876  *
2877  * \retval Start of the next ASN.1 component to encode on success.
2878  * \retval NULL on error.
2879  */
enc_qsig_call_transfer_complete(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,int call_status)2880 static unsigned char *enc_qsig_call_transfer_complete(struct pri *ctrl,
2881 	unsigned char *pos, unsigned char *end, q931_call *call, int call_status)
2882 {
2883 	struct fac_extension_header header;
2884 	struct rose_msg_invoke msg;
2885 
2886 	memset(&header, 0, sizeof(header));
2887 	header.nfe_present = 1;
2888 	header.nfe.source_entity = 0;	/* endPINX */
2889 	header.nfe.destination_entity = 0;	/* endPINX */
2890 	header.interpretation_present = 1;
2891 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
2892 	pos = facility_encode_header(ctrl, pos, end, &header);
2893 	if (!pos) {
2894 		return NULL;
2895 	}
2896 
2897 	memset(&msg, 0, sizeof(msg));
2898 	msg.operation = ROSE_QSIG_CallTransferComplete;
2899 	msg.invoke_id = get_invokeid(ctrl);
2900 	msg.args.qsig.CallTransferComplete.end_designation = 0;	/* primaryEnd */
2901 
2902 	/* redirectionNumber is the local_id.number */
2903 	q931_copy_presented_number_screened_to_rose(ctrl,
2904 		&msg.args.qsig.CallTransferComplete.redirection, &call->local_id.number);
2905 
2906 	/* redirectionName is the local_id.name */
2907 	if (call->local_id.name.valid) {
2908 		msg.args.qsig.CallTransferComplete.redirection_name_present = 1;
2909 		q931_copy_name_to_rose(ctrl,
2910 			&msg.args.qsig.CallTransferComplete.redirection_name,
2911 			&call->local_id.name);
2912 	}
2913 
2914 	if (call_status) {
2915 		msg.args.qsig.CallTransferComplete.call_status = 1;	/* alerting */
2916 	}
2917 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2918 
2919 	return pos;
2920 }
2921 
2922 /*!
2923  * \internal
2924  * \brief Encode the ETSI EctInform invoke message.
2925  *
2926  * \param ctrl D channel controller for diagnostic messages or global options.
2927  * \param pos Starting position to encode the facility ie contents.
2928  * \param end End of facility ie contents encoding data buffer.
2929  * \param call Call leg from which to encode inform message.
2930  * \param call_status TRUE if call is alerting.
2931  *
2932  * \retval Start of the next ASN.1 component to encode on success.
2933  * \retval NULL on error.
2934  */
enc_etsi_ect_inform(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,int call_status)2935 static unsigned char *enc_etsi_ect_inform(struct pri *ctrl, unsigned char *pos,
2936 	unsigned char *end, q931_call *call, int call_status)
2937 {
2938 	struct rose_msg_invoke msg;
2939 
2940 	pos = facility_encode_header(ctrl, pos, end, NULL);
2941 	if (!pos) {
2942 		return NULL;
2943 	}
2944 
2945 	memset(&msg, 0, sizeof(msg));
2946 	msg.operation = ROSE_ETSI_EctInform;
2947 	msg.invoke_id = get_invokeid(ctrl);
2948 
2949 	if (!call_status) {
2950 		msg.args.etsi.EctInform.status = 1;/* active */
2951 
2952 		/*
2953 		 * EctInform(active) contains the redirectionNumber
2954 		 * redirectionNumber is the local_id.number
2955 		 */
2956 		msg.args.etsi.EctInform.redirection_present = 1;
2957 		q931_copy_presented_number_unscreened_to_rose(ctrl,
2958 			&msg.args.etsi.EctInform.redirection, &call->local_id.number);
2959 	}
2960 
2961 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
2962 
2963 	return pos;
2964 }
2965 
2966 /*!
2967  * \internal
2968  * \brief Encode and queue the CallTransferComplete/EctInform invoke message.
2969  *
2970  * \param ctrl D channel controller for diagnostic messages or global options.
2971  * \param call Call leg from which to encode call transfer.
2972  * \param call_status TRUE if call is alerting.
2973  *
2974  * \retval 0 on success.
2975  * \retval -1 on error.
2976  */
rose_call_transfer_complete_encode(struct pri * ctrl,q931_call * call,int call_status)2977 static int rose_call_transfer_complete_encode(struct pri *ctrl, q931_call *call,
2978 	int call_status)
2979 {
2980 	unsigned char buffer[256];
2981 	unsigned char *end;
2982 
2983 	switch (ctrl->switchtype) {
2984 	case PRI_SWITCH_EUROISDN_E1:
2985 	case PRI_SWITCH_EUROISDN_T1:
2986 		end =
2987 			enc_etsi_ect_inform(ctrl, buffer, buffer + sizeof(buffer), call, call_status);
2988 		break;
2989 	case PRI_SWITCH_QSIG:
2990 		end =
2991 			enc_qsig_call_transfer_complete(ctrl, buffer, buffer + sizeof(buffer), call,
2992 			call_status);
2993 		break;
2994 	default:
2995 		return -1;
2996 	}
2997 	if (!end) {
2998 		return -1;
2999 	}
3000 
3001 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL);
3002 }
3003 
3004 /* ===== End Call Transfer Supplementary Service (ECMA-178) ===== */
3005 
3006 /*!
3007  * \internal
3008  * \brief Encode the Q.SIG CalledName invoke message.
3009  *
3010  * \param ctrl D channel controller for diagnostic messages or global options.
3011  * \param pos Starting position to encode the facility ie contents.
3012  * \param end End of facility ie contents encoding data buffer.
3013  * \param name Name data which to encode name.
3014  *
3015  * \retval Start of the next ASN.1 component to encode on success.
3016  * \retval NULL on error.
3017  */
enc_qsig_called_name(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct q931_party_name * name)3018 static unsigned char *enc_qsig_called_name(struct pri *ctrl, unsigned char *pos,
3019 	unsigned char *end, const struct q931_party_name *name)
3020 {
3021 	struct fac_extension_header header;
3022 	struct rose_msg_invoke msg;
3023 
3024 	memset(&header, 0, sizeof(header));
3025 	header.nfe_present = 1;
3026 	header.nfe.source_entity = 0;	/* endPINX */
3027 	header.nfe.destination_entity = 0;	/* endPINX */
3028 	header.interpretation_present = 1;
3029 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
3030 	pos = facility_encode_header(ctrl, pos, end, &header);
3031 	if (!pos) {
3032 		return NULL;
3033 	}
3034 
3035 	memset(&msg, 0, sizeof(msg));
3036 	msg.operation = ROSE_QSIG_CalledName;
3037 	msg.invoke_id = get_invokeid(ctrl);
3038 
3039 	/* CalledName */
3040 	q931_copy_name_to_rose(ctrl, &msg.args.qsig.CalledName.name, name);
3041 
3042 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
3043 
3044 	return pos;
3045 }
3046 
3047 /*!
3048  * \internal
3049  * \brief Encode and queue the Q.SIG CalledName invoke message.
3050  *
3051  * \param ctrl D channel controller for diagnostic messages or global options.
3052  * \param call Call leg from which to encode name.
3053  *
3054  * \retval 0 on success.
3055  * \retval -1 on error.
3056  */
rose_called_name_encode(struct pri * ctrl,q931_call * call,int messagetype)3057 int rose_called_name_encode(struct pri *ctrl, q931_call *call, int messagetype)
3058 {
3059 	unsigned char buffer[256];
3060 	unsigned char *end;
3061 
3062 	/* CalledName is the local_id.name */
3063 	end = enc_qsig_called_name(ctrl, buffer, buffer + sizeof(buffer),
3064 		&call->local_id.name);
3065 	if (!end) {
3066 		return -1;
3067 	}
3068 
3069 	return pri_call_apdu_queue(call, messagetype, buffer, end - buffer, NULL);
3070 }
3071 
3072 /*!
3073  * \internal
3074  * \brief Encode the Q.SIG ConnectedName invoke message.
3075  *
3076  * \param ctrl D channel controller for diagnostic messages or global options.
3077  * \param pos Starting position to encode the facility ie contents.
3078  * \param end End of facility ie contents encoding data buffer.
3079  * \param name Name data which to encode name.
3080  *
3081  * \retval Start of the next ASN.1 component to encode on success.
3082  * \retval NULL on error.
3083  */
enc_qsig_connected_name(struct pri * ctrl,unsigned char * pos,unsigned char * end,const struct q931_party_name * name)3084 static unsigned char *enc_qsig_connected_name(struct pri *ctrl, unsigned char *pos,
3085 	unsigned char *end, const struct q931_party_name *name)
3086 {
3087 	struct fac_extension_header header;
3088 	struct rose_msg_invoke msg;
3089 
3090 	memset(&header, 0, sizeof(header));
3091 	header.nfe_present = 1;
3092 	header.nfe.source_entity = 0;	/* endPINX */
3093 	header.nfe.destination_entity = 0;	/* endPINX */
3094 	header.interpretation_present = 1;
3095 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
3096 	pos = facility_encode_header(ctrl, pos, end, &header);
3097 	if (!pos) {
3098 		return NULL;
3099 	}
3100 
3101 	memset(&msg, 0, sizeof(msg));
3102 	msg.operation = ROSE_QSIG_ConnectedName;
3103 	msg.invoke_id = get_invokeid(ctrl);
3104 
3105 	/* ConnectedName */
3106 	q931_copy_name_to_rose(ctrl, &msg.args.qsig.ConnectedName.name, name);
3107 
3108 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
3109 
3110 	return pos;
3111 }
3112 
3113 /*!
3114  * \internal
3115  * \brief Encode and queue the Q.SIG ConnectedName invoke message.
3116  *
3117  * \param ctrl D channel controller for diagnostic messages or global options.
3118  * \param call Call leg from which to encode name.
3119  *
3120  * \retval 0 on success.
3121  * \retval -1 on error.
3122  */
rose_connected_name_encode(struct pri * ctrl,q931_call * call,int messagetype)3123 int rose_connected_name_encode(struct pri *ctrl, q931_call *call, int messagetype)
3124 {
3125 	unsigned char buffer[256];
3126 	unsigned char *end;
3127 
3128 	/* ConnectedName is the local_id.name */
3129 	end = enc_qsig_connected_name(ctrl, buffer, buffer + sizeof(buffer),
3130 		&call->local_id.name);
3131 	if (!end) {
3132 		return -1;
3133 	}
3134 
3135 	return pri_call_apdu_queue(call, messagetype, buffer, end - buffer, NULL);
3136 }
3137 
3138 /*!
3139  * \brief Put the APDU on the call queue.
3140  *
3141  * \param call Call to enqueue message.
3142  * \param messagetype Q.931 message type.
3143  * \param apdu Facility ie contents buffer.
3144  * \param apdu_len Length of the contents buffer.
3145  * \param response Sender supplied information to handle APDU response messages.
3146  *        NULL if don't care about responses.
3147  *
3148  * \note
3149  * Only APDU messages with an invoke component can supply a response pointer.
3150  * If any other APDU messages supply a response pointer then aliasing of the
3151  * invoke_id can occur.
3152  *
3153  * \retval 0 on success.
3154  * \retval -1 on error.
3155  */
pri_call_apdu_queue(q931_call * call,int messagetype,const unsigned char * apdu,int apdu_len,struct apdu_callback_data * response)3156 int pri_call_apdu_queue(q931_call *call, int messagetype, const unsigned char *apdu, int apdu_len, struct apdu_callback_data *response)
3157 {
3158 	struct apdu_event *cur = NULL;
3159 	struct apdu_event *new_event = NULL;
3160 
3161 	if (!call || !messagetype || !apdu
3162 		|| apdu_len < 1 || sizeof(new_event->apdu) < apdu_len) {
3163 		return -1;
3164 	}
3165 	switch (messagetype) {
3166 	case Q931_FACILITY:
3167 		break;
3168 	default:
3169 		if (q931_is_dummy_call(call)) {
3170 			pri_error(call->pri, "!! Cannot send %s message on dummy call reference.\n",
3171 				msg2str(messagetype));
3172 			return -1;
3173 		}
3174 		break;
3175 	}
3176 
3177 	new_event = calloc(1, sizeof(*new_event));
3178 	if (!new_event) {
3179 		pri_error(call->pri, "!! Malloc failed!\n");
3180 		return -1;
3181 	}
3182 
3183 	/* Fill in the APDU event */
3184 	new_event->message = messagetype;
3185 	if (response) {
3186 		new_event->response = *response;
3187 	}
3188 	new_event->call = call;
3189 	new_event->apdu_len = apdu_len;
3190 	memcpy(new_event->apdu, apdu, apdu_len);
3191 
3192 	/* Append APDU event to the end of the list. */
3193 	if (call->apdus) {
3194 		for (cur = call->apdus; cur->next; cur = cur->next) {
3195 		}
3196 		cur->next = new_event;
3197 	} else {
3198 		call->apdus = new_event;
3199 	}
3200 
3201 	return 0;
3202 }
3203 
3204 /* Used by q931.c to cleanup the apdu queue upon destruction of a call */
pri_call_apdu_queue_cleanup(q931_call * call)3205 void pri_call_apdu_queue_cleanup(q931_call *call)
3206 {
3207 	struct apdu_event *cur_event;
3208 	struct apdu_event *free_event;
3209 
3210 	if (call) {
3211 		cur_event = call->apdus;
3212 		call->apdus = NULL;
3213 		while (cur_event) {
3214 			if (cur_event->response.callback) {
3215 				/* Stop any response timeout. */
3216 				pri_schedule_del(call->pri, cur_event->timer);
3217 				cur_event->timer = 0;
3218 
3219 				/* Indicate to callback that the APDU is being cleaned up. */
3220 				cur_event->response.callback(APDU_CALLBACK_REASON_CLEANUP, call->pri,
3221 					call, cur_event, NULL);
3222 			}
3223 
3224 			free_event = cur_event;
3225 			cur_event = cur_event->next;
3226 			free(free_event);
3227 		}
3228 	}
3229 }
3230 
3231 /*!
3232  * \brief Find an outstanding APDU with the given invoke id.
3233  *
3234  * \param call Call to find APDU.
3235  * \param invoke_id Invoke id to match outstanding APDUs in queue.
3236  *
3237  * \retval apdu_event if found.
3238  * \retval NULL if not found.
3239  */
pri_call_apdu_find(struct q931_call * call,int invoke_id)3240 struct apdu_event *pri_call_apdu_find(struct q931_call *call, int invoke_id)
3241 {
3242 	struct apdu_event *apdu;
3243 
3244 	if (invoke_id == APDU_INVALID_INVOKE_ID) {
3245 		/* No need to search the list since it cannot be in there. */
3246 		return NULL;
3247 	}
3248 	for (apdu = call->apdus; apdu; apdu = apdu->next) {
3249 		/*
3250 		 * Note: The APDU cannot be sent and still in the queue without a
3251 		 * callback and timeout timer active.  Therefore, an invoke_id of
3252 		 * zero is valid and not just the result of a memset().
3253 		 */
3254 		if (apdu->response.invoke_id == invoke_id && apdu->sent) {
3255 			break;
3256 		}
3257 	}
3258 	return apdu;
3259 }
3260 
3261 /*!
3262  * \brief Extract the given APDU event from the given call.
3263  *
3264  * \param call Call to remove the APDU.
3265  * \param extract APDU event to extract.
3266  *
3267  * \retval TRUE on success.
3268  * \retval FALSE on error.
3269  */
pri_call_apdu_extract(struct q931_call * call,struct apdu_event * extract)3270 int pri_call_apdu_extract(struct q931_call *call, struct apdu_event *extract)
3271 {
3272 	struct apdu_event **prev;
3273 	struct apdu_event *cur;
3274 
3275 	/* Find APDU in list. */
3276 	for (prev = &call->apdus, cur = call->apdus;
3277 		cur;
3278 		prev = &cur->next, cur = cur->next) {
3279 		if (cur == extract) {
3280 			/* Stop any response timeout. */
3281 			pri_schedule_del(call->pri, cur->timer);
3282 			cur->timer = 0;
3283 
3284 			/* Remove APDU from list. */
3285 			*prev = cur->next;
3286 
3287 			/* Found and extracted APDU from list. */
3288 			return 1;
3289 		}
3290 	}
3291 
3292 	/* Did not find the APDU in the list. */
3293 	return 0;
3294 }
3295 
3296 /*!
3297  * \brief Delete the given APDU event from the given call.
3298  *
3299  * \param call Call to remove the APDU.
3300  * \param doomed APDU event to delete.
3301  *
3302  * \return Nothing
3303  */
pri_call_apdu_delete(struct q931_call * call,struct apdu_event * doomed)3304 void pri_call_apdu_delete(struct q931_call *call, struct apdu_event *doomed)
3305 {
3306 	if (pri_call_apdu_extract(call, doomed)) {
3307 		free(doomed);
3308 	}
3309 }
3310 
3311 /*! \note Only called when sending the SETUP message. */
pri_call_add_standard_apdus(struct pri * ctrl,q931_call * call)3312 int pri_call_add_standard_apdus(struct pri *ctrl, q931_call *call)
3313 {
3314 	if (!ctrl->sendfacility) {
3315 		return 0;
3316 	}
3317 
3318 	switch (ctrl->switchtype) {
3319 	case PRI_SWITCH_EUROISDN_E1:
3320 	case PRI_SWITCH_EUROISDN_T1:
3321 		if (call->aoc_charging_request) {
3322 			aoc_charging_request_send(ctrl, call, call->aoc_charging_request);
3323 		}
3324 		if (PTMP_MODE(ctrl)) {
3325 			/* PTMP mode */
3326 			break;
3327 		}
3328 		/* PTP mode */
3329 		if (call->redirecting.count) {
3330 			rose_diverting_leg_information2_encode(ctrl, call);
3331 
3332 			/*
3333 			 * Expect a DivertingLegInformation3 to update the COLR of the
3334 			 * redirecting-to party we are attempting to call now.
3335 			 */
3336 			call->redirecting.state = Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3;
3337 		}
3338 		break;
3339 	case PRI_SWITCH_QSIG:
3340 		/* For Q.SIG it does network and cpe operations */
3341 		if (call->redirecting.count) {
3342 			rose_diverting_leg_information2_encode(ctrl, call);
3343 
3344 			/*
3345 			 * Expect a DivertingLegInformation3 to update the COLR of the
3346 			 * redirecting-to party we are attempting to call now.
3347 			 */
3348 			call->redirecting.state = Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3;
3349 		}
3350 		add_callername_facility_ies(ctrl, call, 1);
3351 		break;
3352 	case PRI_SWITCH_NI2:
3353 		add_callername_facility_ies(ctrl, call, (ctrl->localtype == PRI_CPE));
3354 		break;
3355 	case PRI_SWITCH_DMS100:
3356 		if (ctrl->localtype == PRI_CPE) {
3357 			add_dms100_transfer_ability_apdu(ctrl, call);
3358 		}
3359 		break;
3360 	default:
3361 		break;
3362 	}
3363 
3364 	return 0;
3365 }
3366 
3367 /*!
3368  * \brief Send the CallTransferComplete/EctInform invoke message.
3369  *
3370  * \param ctrl D channel controller for diagnostic messages or global options.
3371  * \param call Call leg from which to encode call transfer.
3372  * \param call_status TRUE if call is alerting.
3373  *
3374  * \retval 0 on success.
3375  * \retval -1 on error.
3376  */
send_call_transfer_complete(struct pri * ctrl,q931_call * call,int call_status)3377 int send_call_transfer_complete(struct pri *ctrl, q931_call *call, int call_status)
3378 {
3379 	int status;
3380 
3381 	status = rose_call_transfer_complete_encode(ctrl, call, call_status);
3382 	if (!status) {
3383 		if (!call_status && call->local_id.number.valid
3384 			&& (ctrl->display_flags.send & PRI_DISPLAY_OPTION_NAME_UPDATE)) {
3385 			status = q931_facility_display_name(ctrl, call, &call->local_id.name);
3386 		} else {
3387 			status = q931_facility(ctrl, call);
3388 		}
3389 	}
3390 	if (status) {
3391 		pri_message(ctrl,
3392 			"Could not schedule facility message for call transfer completed.\n");
3393 		return -1;
3394 	}
3395 
3396 	return 0;
3397 }
3398 
3399 /*!
3400  * \internal
3401  * \brief Encode the ETSI RequestSubaddress invoke message.
3402  *
3403  * \param ctrl D channel controller for diagnostic messages or global options.
3404  * \param pos Starting position to encode the facility ie contents.
3405  * \param end End of facility ie contents encoding data buffer.
3406  *
3407  * \retval Start of the next ASN.1 component to encode on success.
3408  * \retval NULL on error.
3409  */
enc_etsi_request_subaddress(struct pri * ctrl,unsigned char * pos,unsigned char * end)3410 static unsigned char *enc_etsi_request_subaddress(struct pri *ctrl, unsigned char *pos,
3411 	unsigned char *end)
3412 {
3413 	struct rose_msg_invoke msg;
3414 
3415 	pos = facility_encode_header(ctrl, pos, end, NULL);
3416 	if (!pos) {
3417 		return NULL;
3418 	}
3419 
3420 	memset(&msg, 0, sizeof(msg));
3421 	msg.operation = ROSE_ETSI_RequestSubaddress;
3422 	msg.invoke_id = get_invokeid(ctrl);
3423 
3424 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
3425 
3426 	return pos;
3427 }
3428 
3429 /*!
3430  * \brief Encode and queue the RequestSubaddress invoke message.
3431  *
3432  * \param ctrl D channel controller for diagnostic messages or global options.
3433  * \param call Call leg from which to encode message.
3434  *
3435  * \retval 0 on success.
3436  * \retval -1 on error.
3437  */
rose_request_subaddress_encode(struct pri * ctrl,struct q931_call * call)3438 int rose_request_subaddress_encode(struct pri *ctrl, struct q931_call *call)
3439 {
3440 	unsigned char buffer[256];
3441 	unsigned char *end;
3442 
3443 	switch (ctrl->switchtype) {
3444 	case PRI_SWITCH_EUROISDN_E1:
3445 	case PRI_SWITCH_EUROISDN_T1:
3446 		end = enc_etsi_request_subaddress(ctrl, buffer, buffer + sizeof(buffer));
3447 		break;
3448 	case PRI_SWITCH_QSIG:
3449 	default:
3450 		return -1;
3451 	}
3452 	if (!end) {
3453 		return -1;
3454 	}
3455 
3456 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL);
3457 }
3458 
3459 /*!
3460  * \internal
3461  * \brief Encode the ETSI SubaddressTransfer invoke message.
3462  *
3463  * \param ctrl D channel controller for diagnostic messages or global options.
3464  * \param pos Starting position to encode the facility ie contents.
3465  * \param end End of facility ie contents encoding data buffer.
3466  * \param call Call leg from which to encode message.
3467  *
3468  * \retval Start of the next ASN.1 component to encode on success.
3469  * \retval NULL on error.
3470  */
enc_etsi_subaddress_transfer(struct pri * ctrl,unsigned char * pos,unsigned char * end,struct q931_call * call)3471 static unsigned char *enc_etsi_subaddress_transfer(struct pri *ctrl, unsigned char *pos,
3472 	unsigned char *end, struct q931_call *call)
3473 {
3474 	struct rose_msg_invoke msg;
3475 
3476 	pos = facility_encode_header(ctrl, pos, end, NULL);
3477 	if (!pos) {
3478 		return NULL;
3479 	}
3480 
3481 	memset(&msg, 0, sizeof(msg));
3482 	msg.operation = ROSE_ETSI_SubaddressTransfer;
3483 	msg.invoke_id = get_invokeid(ctrl);
3484 
3485 	if (!call->local_id.subaddress.valid) {
3486 		return NULL;
3487 	}
3488 	q931_copy_subaddress_to_rose(ctrl, &msg.args.etsi.SubaddressTransfer.subaddress,
3489 		&call->local_id.subaddress);
3490 
3491 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
3492 
3493 	return pos;
3494 }
3495 
3496 /*!
3497  * \internal
3498  * \brief Encode the Q.SIG SubaddressTransfer invoke message.
3499  *
3500  * \param ctrl D channel controller for diagnostic messages or global options.
3501  * \param pos Starting position to encode the facility ie contents.
3502  * \param end End of facility ie contents encoding data buffer.
3503  * \param call Call leg from which to encode message.
3504  *
3505  * \retval Start of the next ASN.1 component to encode on success.
3506  * \retval NULL on error.
3507  */
enc_qsig_subaddress_transfer(struct pri * ctrl,unsigned char * pos,unsigned char * end,struct q931_call * call)3508 static unsigned char *enc_qsig_subaddress_transfer(struct pri *ctrl,
3509 	unsigned char *pos, unsigned char *end, struct q931_call *call)
3510 {
3511 	struct fac_extension_header header;
3512 	struct rose_msg_invoke msg;
3513 
3514 	memset(&header, 0, sizeof(header));
3515 	header.nfe_present = 1;
3516 	header.nfe.source_entity = 0;	/* endPINX */
3517 	header.nfe.destination_entity = 0;	/* endPINX */
3518 	header.interpretation_present = 1;
3519 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
3520 	pos = facility_encode_header(ctrl, pos, end, &header);
3521 	if (!pos) {
3522 		return NULL;
3523 	}
3524 
3525 	memset(&msg, 0, sizeof(msg));
3526 	msg.operation = ROSE_QSIG_SubaddressTransfer;
3527 	msg.invoke_id = get_invokeid(ctrl);
3528 
3529 	if (!call->local_id.subaddress.valid) {
3530 		return NULL;
3531 	}
3532 	q931_copy_subaddress_to_rose(ctrl,
3533 		&msg.args.qsig.SubaddressTransfer.redirection_subaddress,
3534 		&call->local_id.subaddress);
3535 
3536 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
3537 
3538 	return pos;
3539 }
3540 
3541 /*!
3542  * \internal
3543  * \brief Encode and queue the SubaddressTransfer invoke message.
3544  *
3545  * \param ctrl D channel controller for diagnostic messages or global options.
3546  * \param call Call leg from which to encode message.
3547  *
3548  * \retval 0 on success.
3549  * \retval -1 on error.
3550  */
rose_subaddress_transfer_encode(struct pri * ctrl,struct q931_call * call)3551 static int rose_subaddress_transfer_encode(struct pri *ctrl, struct q931_call *call)
3552 {
3553 	unsigned char buffer[256];
3554 	unsigned char *end;
3555 
3556 	switch (ctrl->switchtype) {
3557 	case PRI_SWITCH_EUROISDN_E1:
3558 	case PRI_SWITCH_EUROISDN_T1:
3559 		end =
3560 			enc_etsi_subaddress_transfer(ctrl, buffer, buffer + sizeof(buffer), call);
3561 		break;
3562 	case PRI_SWITCH_QSIG:
3563 		end =
3564 			enc_qsig_subaddress_transfer(ctrl, buffer, buffer + sizeof(buffer), call);
3565 		break;
3566 	default:
3567 		return -1;
3568 	}
3569 	if (!end) {
3570 		return -1;
3571 	}
3572 
3573 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, NULL);
3574 }
3575 
3576 /*!
3577  * \brief Send a FACILITY SubaddressTransfer.
3578  *
3579  * \param ctrl D channel controller.
3580  * \param call Q.931 call leg
3581  *
3582  * \retval 0 on success.
3583  * \retval -1 on error.
3584  */
send_subaddress_transfer(struct pri * ctrl,struct q931_call * call)3585 int send_subaddress_transfer(struct pri *ctrl, struct q931_call *call)
3586 {
3587 	if (rose_subaddress_transfer_encode(ctrl, call)
3588 		|| q931_facility(ctrl, call)) {
3589 		pri_message(ctrl,
3590 			"Could not schedule facility message for subaddress transfer.\n");
3591 		return -1;
3592 	}
3593 
3594 	return 0;
3595 }
3596 
3597 /*!
3598  * \internal
3599  * \brief Handle the received RequestSubaddress facility.
3600  *
3601  * \param ctrl D channel controller.
3602  * \param call Q.931 call leg
3603  *
3604  * \return Nothing
3605  */
etsi_request_subaddress(struct pri * ctrl,struct q931_call * call)3606 static void etsi_request_subaddress(struct pri *ctrl, struct q931_call *call)
3607 {
3608 	struct q931_party_name name;
3609 	int changed = 0;
3610 
3611 	switch (call->notify) {
3612 	case PRI_NOTIFY_TRANSFER_ACTIVE:
3613 		if (q931_party_number_cmp(&call->remote_id.number, &call->redirection_number)) {
3614 			/* The remote party number information changed. */
3615 			call->remote_id.number = call->redirection_number;
3616 			changed = 1;
3617 		}
3618 		/* Fall through */
3619 	case PRI_NOTIFY_TRANSFER_ALERTING:
3620 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
3621 			if (q931_display_name_get(call, &name)) {
3622 				if (q931_party_name_cmp(&call->remote_id.name, &name)) {
3623 					/* The remote party name information changed. */
3624 					call->remote_id.name = name;
3625 					changed = 1;
3626 				}
3627 			}
3628 		}
3629 		if (call->redirection_number.valid
3630 			&& q931_party_number_cmp(&call->remote_id.number, &call->redirection_number)) {
3631 			/* The remote party number information changed. */
3632 			call->remote_id.number = call->redirection_number;
3633 			changed = 1;
3634 		}
3635 		if (call->remote_id.subaddress.valid) {
3636 			/*
3637 			 * Clear the subaddress as the remote party has been changed.
3638 			 * Any new subaddress will arrive later.
3639 			 */
3640 			q931_party_subaddress_init(&call->remote_id.subaddress);
3641 			changed = 1;
3642 		}
3643 		if (changed) {
3644 			call->incoming_ct_state = INCOMING_CT_STATE_POST_CONNECTED_LINE;
3645 		}
3646 		break;
3647 	default:
3648 		break;
3649 	}
3650 
3651 	/* Send our subaddress back if we have one. */
3652 	if (call->local_id.subaddress.valid) {
3653 		send_subaddress_transfer(ctrl, call);
3654 	}
3655 }
3656 
3657 /*!
3658  * \internal
3659  * \brief Handle the received SubaddressTransfer facility subaddress.
3660  *
3661  * \param ctrl D channel controller.
3662  * \param call Q.931 call leg
3663  * \param subaddr Received subaddress of remote party.
3664  *
3665  * \return Nothing
3666  */
handle_subaddress_transfer(struct pri * ctrl,struct q931_call * call,const struct rosePartySubaddress * subaddr)3667 static void handle_subaddress_transfer(struct pri *ctrl, struct q931_call *call, const struct rosePartySubaddress *subaddr)
3668 {
3669 	int changed = 0;
3670 	struct q931_party_name name;
3671 	struct q931_party_subaddress q931_subaddress;
3672 
3673 	q931_party_subaddress_init(&q931_subaddress);
3674 	rose_copy_subaddress_to_q931(ctrl, &q931_subaddress, subaddr);
3675 	if (q931_party_subaddress_cmp(&call->remote_id.subaddress, &q931_subaddress)) {
3676 		call->remote_id.subaddress = q931_subaddress;
3677 		changed = 1;
3678 	}
3679 	if (call->redirection_number.valid
3680 		&& q931_party_number_cmp(&call->remote_id.number, &call->redirection_number)) {
3681 		/* The remote party number information changed. */
3682 		call->remote_id.number = call->redirection_number;
3683 		changed = 1;
3684 	}
3685 	if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
3686 		if (q931_display_name_get(call, &name)) {
3687 			if (q931_party_name_cmp(&call->remote_id.name, &name)) {
3688 				/* The remote party name information changed. */
3689 				call->remote_id.name = name;
3690 				changed = 1;
3691 			}
3692 		}
3693 	}
3694 	if (changed) {
3695 		call->incoming_ct_state = INCOMING_CT_STATE_POST_CONNECTED_LINE;
3696 	}
3697 }
3698 
3699 /*!
3700  * \internal
3701  * \brief Encode a plain facility ETSI error code.
3702  *
3703  * \param ctrl D channel controller for diagnostic messages or global options.
3704  * \param pos Starting position to encode the facility ie contents.
3705  * \param end End of facility ie contents encoding data buffer.
3706  * \param call Call leg from which to encode error message response.
3707  * \param invoke_id Invoke id to put in error message response.
3708  * \param code Error code to put in error message response.
3709  *
3710  * \retval Start of the next ASN.1 component to encode on success.
3711  * \retval NULL on error.
3712  */
enc_etsi_error(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,int invoke_id,enum rose_error_code code)3713 static unsigned char *enc_etsi_error(struct pri *ctrl, unsigned char *pos,
3714 	unsigned char *end, q931_call *call, int invoke_id, enum rose_error_code code)
3715 {
3716 	struct rose_msg_error msg;
3717 
3718 	pos = facility_encode_header(ctrl, pos, end, NULL);
3719 	if (!pos) {
3720 		return NULL;
3721 	}
3722 
3723 	memset(&msg, 0, sizeof(msg));
3724 	msg.invoke_id = invoke_id;
3725 	msg.code = code;
3726 
3727 	pos = rose_encode_error(ctrl, pos, end, &msg);
3728 
3729 	return pos;
3730 }
3731 
3732 /*!
3733  * \internal
3734  * \brief Encode a plain facility Q.SIG error code.
3735  *
3736  * \param ctrl D channel controller for diagnostic messages or global options.
3737  * \param pos Starting position to encode the facility ie contents.
3738  * \param end End of facility ie contents encoding data buffer.
3739  * \param call Call leg from which to encode error message response.
3740  * \param invoke_id Invoke id to put in error message response.
3741  * \param code Error code to put in error message response.
3742  *
3743  * \retval Start of the next ASN.1 component to encode on success.
3744  * \retval NULL on error.
3745  */
enc_qsig_error(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,int invoke_id,enum rose_error_code code)3746 static unsigned char *enc_qsig_error(struct pri *ctrl, unsigned char *pos,
3747 	unsigned char *end, q931_call *call, int invoke_id, enum rose_error_code code)
3748 {
3749 	struct fac_extension_header header;
3750 	struct rose_msg_error msg;
3751 
3752 	memset(&header, 0, sizeof(header));
3753 	header.nfe_present = 1;
3754 	header.nfe.source_entity = 0;	/* endPINX */
3755 	header.nfe.destination_entity = 0;	/* endPINX */
3756 	header.interpretation_present = 1;
3757 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
3758 	pos = facility_encode_header(ctrl, pos, end, &header);
3759 	if (!pos) {
3760 		return NULL;
3761 	}
3762 
3763 	memset(&msg, 0, sizeof(msg));
3764 	msg.invoke_id = invoke_id;
3765 	msg.code = code;
3766 
3767 	pos = rose_encode_error(ctrl, pos, end, &msg);
3768 
3769 	return pos;
3770 }
3771 
3772 /*!
3773  * \brief Encode and queue a plain facility error code.
3774  *
3775  * \param ctrl D channel controller for diagnostic messages or global options.
3776  * \param call Call leg from which to encode error message response.
3777  * \param msgtype Q.931 message type to put facility ie in.
3778  * \param invoke_id Invoke id to put in error message response.
3779  * \param code Error code to put in error message response.
3780  *
3781  * \retval 0 on success.
3782  * \retval -1 on error.
3783  */
rose_error_msg_encode(struct pri * ctrl,q931_call * call,int msgtype,int invoke_id,enum rose_error_code code)3784 int rose_error_msg_encode(struct pri *ctrl, q931_call *call, int msgtype, int invoke_id,
3785 	enum rose_error_code code)
3786 {
3787 	unsigned char buffer[256];
3788 	unsigned char *end;
3789 
3790 	switch (ctrl->switchtype) {
3791 	case PRI_SWITCH_EUROISDN_E1:
3792 	case PRI_SWITCH_EUROISDN_T1:
3793 		end =
3794 			enc_etsi_error(ctrl, buffer, buffer + sizeof(buffer), call, invoke_id, code);
3795 		break;
3796 	case PRI_SWITCH_QSIG:
3797 		end =
3798 			enc_qsig_error(ctrl, buffer, buffer + sizeof(buffer), call, invoke_id, code);
3799 		break;
3800 	default:
3801 		return -1;
3802 	}
3803 	if (!end) {
3804 		return -1;
3805 	}
3806 
3807 	return pri_call_apdu_queue(call, msgtype, buffer, end - buffer, NULL);
3808 }
3809 
3810 /*!
3811  * \brief Encode and send a plain facility error code.
3812  *
3813  * \param ctrl D channel controller for diagnostic messages or global options.
3814  * \param call Call leg from which to encode error message response.
3815  * \param invoke_id Invoke id to put in error message response.
3816  * \param code Error code to put in error message response.
3817  *
3818  * \retval 0 on success.
3819  * \retval -1 on error.
3820  */
send_facility_error(struct pri * ctrl,q931_call * call,int invoke_id,enum rose_error_code code)3821 int send_facility_error(struct pri *ctrl, q931_call *call, int invoke_id,
3822 	enum rose_error_code code)
3823 {
3824 	if (rose_error_msg_encode(ctrl, call, Q931_FACILITY, invoke_id, code)
3825 		|| q931_facility(ctrl, call)) {
3826 		pri_message(ctrl,
3827 			"Could not schedule facility message for error message.\n");
3828 		return -1;
3829 	}
3830 
3831 	return 0;
3832 }
3833 
3834 /*!
3835  * \internal
3836  * \brief Encode a plain facility ETSI result ok.
3837  *
3838  * \param ctrl D channel controller for diagnostic messages or global options.
3839  * \param pos Starting position to encode the facility ie contents.
3840  * \param end End of facility ie contents encoding data buffer.
3841  * \param call Call leg from which to encode result ok message response.
3842  * \param invoke_id Invoke id to put in result ok message response.
3843  *
3844  * \retval Start of the next ASN.1 component to encode on success.
3845  * \retval NULL on error.
3846  */
enc_etsi_result_ok(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,int invoke_id)3847 static unsigned char *enc_etsi_result_ok(struct pri *ctrl, unsigned char *pos,
3848 	unsigned char *end, q931_call *call, int invoke_id)
3849 {
3850 	struct rose_msg_result msg;
3851 
3852 	pos = facility_encode_header(ctrl, pos, end, NULL);
3853 	if (!pos) {
3854 		return NULL;
3855 	}
3856 
3857 	memset(&msg, 0, sizeof(msg));
3858 	msg.invoke_id = invoke_id;
3859 	msg.operation = ROSE_None;
3860 
3861 	pos = rose_encode_result(ctrl, pos, end, &msg);
3862 
3863 	return pos;
3864 }
3865 
3866 /*!
3867  * \internal
3868  * \brief Encode a plain facility Q.SIG result ok.
3869  *
3870  * \param ctrl D channel controller for diagnostic messages or global options.
3871  * \param pos Starting position to encode the facility ie contents.
3872  * \param end End of facility ie contents encoding data buffer.
3873  * \param call Call leg from which to encode result ok message response.
3874  * \param invoke_id Invoke id to put in result ok message response.
3875  *
3876  * \retval Start of the next ASN.1 component to encode on success.
3877  * \retval NULL on error.
3878  */
enc_qsig_result_ok(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call,int invoke_id)3879 static unsigned char *enc_qsig_result_ok(struct pri *ctrl, unsigned char *pos,
3880 	unsigned char *end, q931_call *call, int invoke_id)
3881 {
3882 	struct fac_extension_header header;
3883 	struct rose_msg_result msg;
3884 
3885 	memset(&header, 0, sizeof(header));
3886 	header.nfe_present = 1;
3887 	header.nfe.source_entity = 0;	/* endPINX */
3888 	header.nfe.destination_entity = 0;	/* endPINX */
3889 	header.interpretation_present = 1;
3890 	header.interpretation = 0;	/* discardAnyUnrecognisedInvokePdu */
3891 	pos = facility_encode_header(ctrl, pos, end, &header);
3892 	if (!pos) {
3893 		return NULL;
3894 	}
3895 
3896 	memset(&msg, 0, sizeof(msg));
3897 	msg.invoke_id = invoke_id;
3898 	msg.operation = ROSE_None;
3899 
3900 	pos = rose_encode_result(ctrl, pos, end, &msg);
3901 
3902 	return pos;
3903 }
3904 
3905 /*!
3906  * \brief Encode and queue a plain ROSE result ok.
3907  *
3908  * \param ctrl D channel controller for diagnostic messages or global options.
3909  * \param call Call leg from which to encode result ok message response.
3910  * \param msgtype Q.931 message type to put facility ie in.
3911  * \param invoke_id Invoke id to put in result ok message response.
3912  *
3913  * \retval 0 on success.
3914  * \retval -1 on error.
3915  */
rose_result_ok_encode(struct pri * ctrl,q931_call * call,int msgtype,int invoke_id)3916 int rose_result_ok_encode(struct pri *ctrl, q931_call *call, int msgtype, int invoke_id)
3917 {
3918 	unsigned char buffer[256];
3919 	unsigned char *end;
3920 
3921 	switch (ctrl->switchtype) {
3922 	case PRI_SWITCH_EUROISDN_E1:
3923 	case PRI_SWITCH_EUROISDN_T1:
3924 		end =
3925 			enc_etsi_result_ok(ctrl, buffer, buffer + sizeof(buffer), call, invoke_id);
3926 		break;
3927 	case PRI_SWITCH_QSIG:
3928 		end =
3929 			enc_qsig_result_ok(ctrl, buffer, buffer + sizeof(buffer), call, invoke_id);
3930 		break;
3931 	default:
3932 		return -1;
3933 	}
3934 	if (!end) {
3935 		return -1;
3936 	}
3937 
3938 	return pri_call_apdu_queue(call, msgtype, buffer, end - buffer, NULL);
3939 }
3940 
3941 /*!
3942  * \brief Encode and send a FACILITY message with a plain ROSE result ok.
3943  *
3944  * \param ctrl D channel controller for diagnostic messages or global options.
3945  * \param call Call leg from which to encode result ok message response.
3946  * \param invoke_id Invoke id to put in result ok message response.
3947  *
3948  * \retval 0 on success.
3949  * \retval -1 on error.
3950  */
send_facility_result_ok(struct pri * ctrl,q931_call * call,int invoke_id)3951 int send_facility_result_ok(struct pri *ctrl, q931_call *call, int invoke_id)
3952 {
3953 	if (rose_result_ok_encode(ctrl, call, Q931_FACILITY, invoke_id)
3954 		|| q931_facility(ctrl, call)) {
3955 		pri_message(ctrl,
3956 			"Could not schedule facility message for result OK message.\n");
3957 		return -1;
3958 	}
3959 
3960 	return 0;
3961 }
3962 
pri_rerouting_rsp(struct pri * ctrl,q931_call * call,int invoke_id,enum PRI_REROUTING_RSP_CODE code)3963 int pri_rerouting_rsp(struct pri *ctrl, q931_call *call, int invoke_id, enum PRI_REROUTING_RSP_CODE code)
3964 {
3965 	enum rose_error_code rose_err;
3966 
3967 	if (!ctrl || !pri_is_call_valid(ctrl, call)) {
3968 		return -1;
3969 	}
3970 
3971 	/* Convert the public rerouting response code to an error code or result ok. */
3972 	rose_err = ROSE_ERROR_Gen_ResourceUnavailable;
3973 	switch (code) {
3974 	case PRI_REROUTING_RSP_OK_CLEAR:
3975 		/*
3976 		 * Send the response out on the next message which should be
3977 		 * either Q931_DISCONNECT or Q931_RELEASE depending upon who
3978 		 * initiates the disconnect first.
3979 		 */
3980 		return rose_result_ok_encode(ctrl, call, Q931_ANY_MESSAGE, invoke_id);
3981 	case PRI_REROUTING_RSP_OK_RETAIN:
3982 		return send_facility_result_ok(ctrl, call, invoke_id);
3983 	case PRI_REROUTING_RSP_NOT_SUBSCRIBED:
3984 		rose_err = ROSE_ERROR_Gen_NotSubscribed;
3985 		break;
3986 	case PRI_REROUTING_RSP_NOT_AVAILABLE:
3987 		rose_err = ROSE_ERROR_Gen_NotAvailable;
3988 		break;
3989 	case PRI_REROUTING_RSP_NOT_ALLOWED:
3990 		rose_err = ROSE_ERROR_Gen_SupplementaryServiceInteractionNotAllowed;
3991 		break;
3992 	case PRI_REROUTING_RSP_INVALID_NUMBER:
3993 		rose_err = ROSE_ERROR_Div_InvalidDivertedToNr;
3994 		break;
3995 	case PRI_REROUTING_RSP_SPECIAL_SERVICE_NUMBER:
3996 		rose_err = ROSE_ERROR_Div_SpecialServiceNr;
3997 		break;
3998 	case PRI_REROUTING_RSP_DIVERSION_TO_SELF:
3999 		rose_err = ROSE_ERROR_Div_DiversionToServedUserNr;
4000 		break;
4001 	case PRI_REROUTING_RSP_MAX_DIVERSIONS_EXCEEDED:
4002 		rose_err = ROSE_ERROR_Div_NumberOfDiversionsExceeded;
4003 		break;
4004 	case PRI_REROUTING_RSP_RESOURCE_UNAVAILABLE:
4005 		rose_err = ROSE_ERROR_Gen_ResourceUnavailable;
4006 		break;
4007 	}
4008 	return send_facility_error(ctrl, call, invoke_id, rose_err);
4009 }
4010 
pri_transfer_rsp(struct pri * ctrl,q931_call * call,int invoke_id,int is_successful)4011 int pri_transfer_rsp(struct pri *ctrl, q931_call *call, int invoke_id, int is_successful)
4012 {
4013 	if (!ctrl || !pri_is_call_valid(ctrl, call)) {
4014 		return -1;
4015 	}
4016 
4017 	if (is_successful) {
4018 		return rose_result_ok_encode(ctrl, call, Q931_DISCONNECT, invoke_id);
4019 	} else {
4020 		return send_facility_error(ctrl, call, invoke_id, ROSE_ERROR_Gen_NotAvailable);
4021 	}
4022 }
4023 
4024 /*!
4025  * \internal
4026  * \brief MCIDRequest response callback function.
4027  *
4028  * \param reason Reason callback is called.
4029  * \param ctrl D channel controller.
4030  * \param call Q.931 call leg.
4031  * \param apdu APDU queued entry.  Do not change!
4032  * \param msg APDU response message data.  (NULL if was not the reason called.)
4033  *
4034  * \return TRUE if no more responses are expected.
4035  */
mcid_req_response(enum APDU_CALLBACK_REASON reason,struct pri * ctrl,struct q931_call * call,struct apdu_event * apdu,const struct apdu_msg_data * msg)4036 static int mcid_req_response(enum APDU_CALLBACK_REASON reason, struct pri *ctrl, struct q931_call *call, struct apdu_event *apdu, const struct apdu_msg_data *msg)
4037 {
4038 	struct pri_subcommand *subcmd;
4039 	int status;
4040 	int fail_code;
4041 
4042 	switch (reason) {
4043 	case APDU_CALLBACK_REASON_TIMEOUT:
4044 		status = 1;/* timeout */
4045 		fail_code = 0;
4046 		break;
4047 	case APDU_CALLBACK_REASON_MSG_RESULT:
4048 		status = 0;/* success */
4049 		fail_code = 0;
4050 		break;
4051 	case APDU_CALLBACK_REASON_MSG_ERROR:
4052 		status = 2;/* error */
4053 		fail_code = msg->response.error->code;
4054 		break;
4055 	case APDU_CALLBACK_REASON_MSG_REJECT:
4056 		status = 3;/* reject */
4057 		fail_code = 0;
4058 		fail_code = msg->response.reject->code;
4059 		break;
4060 	default:
4061 		return 1;
4062 	}
4063 	subcmd = q931_alloc_subcommand(ctrl);
4064 	if (subcmd) {
4065 		/* Indicate that our MCID request has completed. */
4066 		subcmd->cmd = PRI_SUBCMD_MCID_RSP;
4067 		subcmd->u.mcid_rsp.status = status;
4068 		subcmd->u.mcid_rsp.fail_code = fail_code;
4069 	} else {
4070 		/* Oh, well. */
4071 	}
4072 	return 1;
4073 }
4074 
4075 /*!
4076  * \internal
4077  * \brief Encode a MCIDRequest message.
4078  *
4079  * \param ctrl D channel controller for diagnostic messages or global options.
4080  * \param pos Starting position to encode the facility ie contents.
4081  * \param end End of facility ie contents encoding data buffer.
4082  * \param call Call leg from which to encode message.
4083  *
4084  * \retval Start of the next ASN.1 component to encode on success.
4085  * \retval NULL on error.
4086  */
enc_etsi_mcid_req(struct pri * ctrl,unsigned char * pos,unsigned char * end,q931_call * call)4087 static unsigned char *enc_etsi_mcid_req(struct pri *ctrl, unsigned char *pos,
4088 	unsigned char *end, q931_call *call)
4089 {
4090 	struct rose_msg_invoke msg;
4091 
4092 	pos = facility_encode_header(ctrl, pos, end, NULL);
4093 	if (!pos) {
4094 		return NULL;
4095 	}
4096 
4097 	memset(&msg, 0, sizeof(msg));
4098 	msg.invoke_id = get_invokeid(ctrl);
4099 	msg.operation = ROSE_ETSI_MCIDRequest;
4100 
4101 	pos = rose_encode_invoke(ctrl, pos, end, &msg);
4102 
4103 	return pos;
4104 }
4105 
4106 /*!
4107  * \internal
4108  * \brief Encode and queue a MCID request message.
4109  *
4110  * \param ctrl D channel controller for diagnostic messages or global options.
4111  * \param call Call leg from which to encode message.
4112  *
4113  * \retval 0 on success.
4114  * \retval -1 on error.
4115  */
rose_mcid_req_encode(struct pri * ctrl,q931_call * call)4116 static int rose_mcid_req_encode(struct pri *ctrl, q931_call *call)
4117 {
4118 	unsigned char buffer[256];
4119 	unsigned char *end;
4120 	struct apdu_callback_data response;
4121 
4122 	switch (ctrl->switchtype) {
4123 	case PRI_SWITCH_EUROISDN_E1:
4124 	case PRI_SWITCH_EUROISDN_T1:
4125 		end = enc_etsi_mcid_req(ctrl, buffer, buffer + sizeof(buffer), call);
4126 		break;
4127 	default:
4128 		return -1;
4129 	}
4130 	if (!end) {
4131 		return -1;
4132 	}
4133 
4134 	memset(&response, 0, sizeof(response));
4135 	response.invoke_id = ctrl->last_invoke;
4136 	response.timeout_time = ctrl->timers[PRI_TIMER_T_RESPONSE];
4137 	response.callback = mcid_req_response;
4138 
4139 	return pri_call_apdu_queue(call, Q931_FACILITY, buffer, end - buffer, &response);
4140 }
4141 
pri_mcid_req_send(struct pri * ctrl,q931_call * call)4142 int pri_mcid_req_send(struct pri *ctrl, q931_call *call)
4143 {
4144 	if (!ctrl || !pri_is_call_valid(ctrl, call)) {
4145 		return -1;
4146 	}
4147 	if (call->cc.originated) {
4148 		/* We can only send MCID if we answered the call. */
4149 		return -1;
4150 	}
4151 
4152 	if (rose_mcid_req_encode(ctrl, call) || q931_facility(ctrl, call)) {
4153 		pri_message(ctrl,
4154 			"Could not schedule facility message for MCID request message.\n");
4155 		return -1;
4156 	}
4157 
4158 	return 0;
4159 }
4160 
pri_mcid_enable(struct pri * ctrl,int enable)4161 void pri_mcid_enable(struct pri *ctrl, int enable)
4162 {
4163 	if (ctrl) {
4164 		ctrl->mcid_support = enable ? 1 : 0;
4165 	}
4166 }
4167 
4168 /*!
4169  * \brief Handle the ROSE reject message.
4170  *
4171  * \param ctrl D channel controller for diagnostic messages or global options.
4172  * \param call Call leg from which the message came.
4173  * \param msgtype Q.931 message type ie is in.
4174  * \param ie Raw ie contents.
4175  * \param header Decoded facility header before ROSE.
4176  * \param reject Decoded ROSE reject message contents.
4177  *
4178  * \return Nothing
4179  */
rose_handle_reject(struct pri * ctrl,q931_call * call,int msgtype,q931_ie * ie,const struct fac_extension_header * header,const struct rose_msg_reject * reject)4180 void rose_handle_reject(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie,
4181 	const struct fac_extension_header *header, const struct rose_msg_reject *reject)
4182 {
4183 	q931_call *orig_call;
4184 	struct apdu_event *apdu;
4185 	struct apdu_msg_data msg;
4186 
4187 	if (ctrl->debug & PRI_DEBUG_APDU) {
4188 		/* Gripe to the user about getting rejected. */
4189 		pri_message(ctrl, "ROSE REJECT:\n");
4190 		if (reject->invoke_id_present) {
4191 			pri_message(ctrl, "\tINVOKE ID: %d\n", reject->invoke_id);
4192 		}
4193 		pri_message(ctrl, "\tPROBLEM: %s\n", rose_reject2str(reject->code));
4194 	}
4195 
4196 	switch (ctrl->switchtype) {
4197 	case PRI_SWITCH_DMS100:
4198 		/* The DMS-100 switch apparently handles invoke_id as an invoke operation. */
4199 		return;
4200 	default:
4201 		break;
4202 	}
4203 
4204 	if (!reject->invoke_id_present) {
4205 		/*
4206 		 * No invoke id to look up so we cannot match it to any outstanding APDUs.
4207 		 * This REJECT is apparently meant for someone monitoring the link.
4208 		 */
4209 		return;
4210 	}
4211 	orig_call = NULL;/* To make some compilers happy. */
4212 	apdu = NULL;
4213 	if (q931_is_dummy_call(call)) {
4214 		/*
4215 		 * The message was likely sent on the broadcast dummy call reference call
4216 		 * and the reject came in on a specific dummy call reference call.
4217 		 * Look for the original invocation message on the
4218 		 * broadcast dummy call reference call first.
4219 		 */
4220 		orig_call = ctrl->link.dummy_call;
4221 		if (orig_call) {
4222 			apdu = pri_call_apdu_find(orig_call, reject->invoke_id);
4223 		}
4224 	}
4225 	if (!apdu) {
4226 		apdu = pri_call_apdu_find(call, reject->invoke_id);
4227 		if (!apdu) {
4228 			return;
4229 		}
4230 		orig_call = call;
4231 	}
4232 	msg.response.reject = reject;
4233 	msg.type = msgtype;
4234 	if (apdu->response.callback(APDU_CALLBACK_REASON_MSG_REJECT, ctrl, call, apdu, &msg)) {
4235 		pri_call_apdu_delete(orig_call, apdu);
4236 	}
4237 }
4238 
4239 /*!
4240  * \brief Handle the ROSE error message.
4241  *
4242  * \param ctrl D channel controller for diagnostic messages or global options.
4243  * \param call Call leg from which the message came.
4244  * \param msgtype Q.931 message type ie is in.
4245  * \param ie Raw ie contents.
4246  * \param header Decoded facility header before ROSE.
4247  * \param error Decoded ROSE error message contents.
4248  *
4249  * \return Nothing
4250  */
rose_handle_error(struct pri * ctrl,q931_call * call,int msgtype,q931_ie * ie,const struct fac_extension_header * header,const struct rose_msg_error * error)4251 void rose_handle_error(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie,
4252 	const struct fac_extension_header *header, const struct rose_msg_error *error)
4253 {
4254 	const char *dms100_operation;
4255 	q931_call *orig_call;
4256 	struct apdu_event *apdu;
4257 	struct apdu_msg_data msg;
4258 
4259 	if (ctrl->debug & PRI_DEBUG_APDU) {
4260 		/* Gripe to the user about getting an error. */
4261 		pri_message(ctrl, "ROSE RETURN ERROR:\n");
4262 		switch (ctrl->switchtype) {
4263 		case PRI_SWITCH_DMS100:
4264 			switch (error->invoke_id) {
4265 			case ROSE_DMS100_RLT_OPERATION_IND:
4266 				dms100_operation = "RLT_OPERATION_IND";
4267 				break;
4268 			case ROSE_DMS100_RLT_THIRD_PARTY:
4269 				dms100_operation = "RLT_THIRD_PARTY";
4270 				break;
4271 			default:
4272 				dms100_operation = NULL;
4273 				break;
4274 			}
4275 			if (dms100_operation) {
4276 				pri_message(ctrl, "\tOPERATION: %s\n", dms100_operation);
4277 				break;
4278 			}
4279 			/* fall through */
4280 		default:
4281 			pri_message(ctrl, "\tINVOKE ID: %d\n", error->invoke_id);
4282 			break;
4283 		}
4284 		pri_message(ctrl, "\tERROR: %s\n", rose_error2str(error->code));
4285 	}
4286 
4287 	switch (ctrl->switchtype) {
4288 	case PRI_SWITCH_DMS100:
4289 		/* The DMS-100 switch apparently handles invoke_id as an invoke operation. */
4290 		return;
4291 	default:
4292 		break;
4293 	}
4294 
4295 	orig_call = NULL;/* To make some compilers happy. */
4296 	apdu = NULL;
4297 	if (q931_is_dummy_call(call)) {
4298 		/*
4299 		 * The message was likely sent on the broadcast dummy call reference call
4300 		 * and the error came in on a specific dummy call reference call.
4301 		 * Look for the original invocation message on the
4302 		 * broadcast dummy call reference call first.
4303 		 */
4304 		orig_call = ctrl->link.dummy_call;
4305 		if (orig_call) {
4306 			apdu = pri_call_apdu_find(orig_call, error->invoke_id);
4307 		}
4308 	}
4309 	if (!apdu) {
4310 		apdu = pri_call_apdu_find(call, error->invoke_id);
4311 		if (!apdu) {
4312 			return;
4313 		}
4314 		orig_call = call;
4315 	}
4316 	msg.response.error = error;
4317 	msg.type = msgtype;
4318 	if (apdu->response.callback(APDU_CALLBACK_REASON_MSG_ERROR, ctrl, call, apdu, &msg)) {
4319 		pri_call_apdu_delete(orig_call, apdu);
4320 	}
4321 }
4322 
4323 /*!
4324  * \brief Handle the ROSE result message.
4325  *
4326  * \param ctrl D channel controller for diagnostic messages or global options.
4327  * \param call Call leg from which the message came.
4328  * \param msgtype Q.931 message type ie is in.
4329  * \param ie Raw ie contents.
4330  * \param header Decoded facility header before ROSE.
4331  * \param result Decoded ROSE result message contents.
4332  *
4333  * \return Nothing
4334  */
rose_handle_result(struct pri * ctrl,q931_call * call,int msgtype,q931_ie * ie,const struct fac_extension_header * header,const struct rose_msg_result * result)4335 void rose_handle_result(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie,
4336 	const struct fac_extension_header *header, const struct rose_msg_result *result)
4337 {
4338 	q931_call *orig_call;
4339 	struct apdu_event *apdu;
4340 	struct apdu_msg_data msg;
4341 
4342 	switch (ctrl->switchtype) {
4343 	case PRI_SWITCH_DMS100:
4344 		/* The DMS-100 switch apparently handles invoke_id as an invoke operation. */
4345 		switch (result->invoke_id) {
4346 		case ROSE_DMS100_RLT_OPERATION_IND:
4347 			if (result->operation != ROSE_DMS100_RLT_OperationInd) {
4348 				pri_message(ctrl, "Invalid Operation value in return result! %s\n",
4349 					rose_operation2str(result->operation));
4350 				break;
4351 			}
4352 
4353 			/* We have enough data to transfer the call */
4354 			call->rlt_call_id = result->args.dms100.RLT_OperationInd.call_id;
4355 			call->transferable = 1;
4356 			break;
4357 		case ROSE_DMS100_RLT_THIRD_PARTY:
4358 			if (ctrl->debug & PRI_DEBUG_APDU) {
4359 				pri_message(ctrl, "Successfully completed RLT transfer!\n");
4360 			}
4361 			break;
4362 		default:
4363 			pri_message(ctrl, "Could not parse invoke of type %d!\n", result->invoke_id);
4364 			break;
4365 		}
4366 		return;
4367 	default:
4368 		break;
4369 	}
4370 
4371 	orig_call = NULL;/* To make some compilers happy. */
4372 	apdu = NULL;
4373 	if (q931_is_dummy_call(call)) {
4374 		/*
4375 		 * The message was likely sent on the broadcast dummy call reference call
4376 		 * and the result came in on a specific dummy call reference call.
4377 		 * Look for the original invocation message on the
4378 		 * broadcast dummy call reference call first.
4379 		 */
4380 		orig_call = ctrl->link.dummy_call;
4381 		if (orig_call) {
4382 			apdu = pri_call_apdu_find(orig_call, result->invoke_id);
4383 		}
4384 	}
4385 	if (!apdu) {
4386 		apdu = pri_call_apdu_find(call, result->invoke_id);
4387 		if (!apdu) {
4388 			return;
4389 		}
4390 		orig_call = call;
4391 	}
4392 	msg.response.result = result;
4393 	msg.type = msgtype;
4394 	if (apdu->response.callback(APDU_CALLBACK_REASON_MSG_RESULT, ctrl, call, apdu, &msg)) {
4395 		pri_call_apdu_delete(orig_call, apdu);
4396 	}
4397 }
4398 
4399 /*!
4400  * \brief Handle the ROSE invoke message.
4401  *
4402  * \param ctrl D channel controller for diagnostic messages or global options.
4403  * \param call Call leg from which the message came.
4404  * \param msgtype Q.931 message type ie is in.
4405  * \param ie Raw ie contents.
4406  * \param header Decoded facility header before ROSE.
4407  * \param invoke Decoded ROSE invoke message contents.
4408  *
4409  * \return Nothing
4410  */
rose_handle_invoke(struct pri * ctrl,q931_call * call,int msgtype,q931_ie * ie,const struct fac_extension_header * header,const struct rose_msg_invoke * invoke)4411 void rose_handle_invoke(struct pri *ctrl, q931_call *call, int msgtype, q931_ie *ie,
4412 	const struct fac_extension_header *header, const struct rose_msg_invoke *invoke)
4413 {
4414 	struct pri_cc_record *cc_record;
4415 	struct pri_subcommand *subcmd;
4416 	struct q931_party_id party_id;
4417 	struct q931_party_address party_address;
4418 	struct q931_party_redirecting deflection;
4419 	enum rose_error_code error_code;
4420 
4421 	switch (invoke->operation) {
4422 #if 0	/* Not handled yet */
4423 	case ROSE_ETSI_ActivationDiversion:
4424 		break;
4425 	case ROSE_ETSI_DeactivationDiversion:
4426 		break;
4427 	case ROSE_ETSI_ActivationStatusNotificationDiv:
4428 		break;
4429 	case ROSE_ETSI_DeactivationStatusNotificationDiv:
4430 		break;
4431 	case ROSE_ETSI_InterrogationDiversion:
4432 		break;
4433 	case ROSE_ETSI_DiversionInformation:
4434 		break;
4435 #endif	/* Not handled yet */
4436 	case ROSE_ETSI_CallDeflection:
4437 		if (!ctrl->deflection_support) {
4438 			send_facility_error(ctrl, call, invoke->invoke_id,
4439 				ROSE_ERROR_Gen_NotSubscribed);
4440 			break;
4441 		}
4442 		if (!q931_master_pass_event(ctrl, call, msgtype)) {
4443 			/* Some other user is further along to connecting than this call. */
4444 			send_facility_error(ctrl, call, invoke->invoke_id,
4445 				ROSE_ERROR_Div_IncomingCallAccepted);
4446 			break;
4447 		}
4448 		if (call->master_call->deflection_in_progress) {
4449 			/* Someone else is already doing a call deflection. */
4450 			send_facility_error(ctrl, call, invoke->invoke_id,
4451 				ROSE_ERROR_Div_RequestAlreadyAccepted);
4452 			break;
4453 		}
4454 		subcmd = q931_alloc_subcommand(ctrl);
4455 		if (!subcmd) {
4456 			/*
4457 			 * ROSE_ERROR_Gen_ResourceUnavailable was not in the list of allowed codes,
4458 			 * but we will send it anyway.
4459 			 */
4460 			send_facility_error(ctrl, call, invoke->invoke_id,
4461 				ROSE_ERROR_Gen_ResourceUnavailable);
4462 			break;
4463 		}
4464 
4465 		call->master_call->deflection_in_progress = 1;
4466 
4467 		q931_party_redirecting_init(&deflection);
4468 
4469 		/* Deflecting from the called address. */
4470 		q931_party_address_to_id(&deflection.from, &call->called);
4471 		if (invoke->args.etsi.CallDeflection.presentation_allowed_to_diverted_to_user_present) {
4472 			deflection.from.number.presentation =
4473 				invoke->args.etsi.CallDeflection.presentation_allowed_to_diverted_to_user
4474 				? PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED
4475 				: PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4476 		} else {
4477 			deflection.from.number.presentation =
4478 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4479 		}
4480 
4481 		/* Deflecting to the new address. */
4482 		rose_copy_address_to_id_q931(ctrl, &deflection.to,
4483 			&invoke->args.etsi.CallDeflection.deflection);
4484 		deflection.to.number.presentation = deflection.from.number.presentation;
4485 
4486 		deflection.count = (call->redirecting.count < PRI_MAX_REDIRECTS)
4487 			? call->redirecting.count + 1 : PRI_MAX_REDIRECTS;
4488 		deflection.reason = PRI_REDIR_DEFLECTION;
4489 		if (deflection.count == 1) {
4490 			deflection.orig_called = deflection.from;
4491 			deflection.orig_reason = deflection.reason;
4492 		} else {
4493 			deflection.orig_called = call->redirecting.orig_called;
4494 			deflection.orig_reason = call->redirecting.orig_reason;
4495 		}
4496 
4497 		subcmd->cmd = PRI_SUBCMD_REROUTING;
4498 		subcmd->u.rerouting.invoke_id = invoke->invoke_id;
4499 		subcmd->u.rerouting.subscription_option = 3;/* notApplicable */
4500 		q931_party_id_copy_to_pri(&subcmd->u.rerouting.caller, &call->local_id);
4501 		q931_party_redirecting_copy_to_pri(&subcmd->u.rerouting.deflection,
4502 			&deflection);
4503 		break;
4504 	case ROSE_ETSI_CallRerouting:
4505 		if (!ctrl->deflection_support) {
4506 			send_facility_error(ctrl, call, invoke->invoke_id,
4507 				ROSE_ERROR_Gen_NotSubscribed);
4508 			break;
4509 		}
4510 		subcmd = q931_alloc_subcommand(ctrl);
4511 		if (!subcmd) {
4512 			send_facility_error(ctrl, call, invoke->invoke_id,
4513 				ROSE_ERROR_Gen_ResourceUnavailable);
4514 			break;
4515 		}
4516 
4517 		q931_party_redirecting_init(&deflection);
4518 
4519 		/* Rerouting from the last address. */
4520 		rose_copy_presented_number_unscreened_to_q931(ctrl, &deflection.from.number,
4521 			&invoke->args.etsi.CallRerouting.last_rerouting);
4522 
4523 		/* Rerouting to the new address. */
4524 		rose_copy_address_to_id_q931(ctrl, &deflection.to,
4525 			&invoke->args.etsi.CallRerouting.called_address);
4526 		switch (invoke->args.etsi.CallRerouting.subscription_option) {
4527 		default:
4528 		case 0: /* noNotification */
4529 		case 1: /* notificationWithoutDivertedToNr */
4530 			deflection.to.number.presentation =
4531 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4532 			break;
4533 		case 2: /* notificationWithDivertedToNr */
4534 			deflection.to.number.presentation =
4535 				PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
4536 			break;
4537 		}
4538 
4539 		/* Calling party subaddress update. */
4540 		party_id = call->local_id;
4541 
4542 		deflection.count = invoke->args.etsi.CallRerouting.rerouting_counter;
4543 		deflection.reason = redirectingreason_for_q931(ctrl,
4544 			invoke->args.etsi.CallRerouting.rerouting_reason);
4545 		if (deflection.count == 1) {
4546 			deflection.orig_called = deflection.from;
4547 			deflection.orig_reason = deflection.reason;
4548 		} else {
4549 			deflection.orig_called = call->redirecting.orig_called;
4550 			deflection.orig_reason = call->redirecting.orig_reason;
4551 		}
4552 
4553 		subcmd->cmd = PRI_SUBCMD_REROUTING;
4554 		subcmd->u.rerouting.invoke_id = invoke->invoke_id;
4555 		subcmd->u.rerouting.subscription_option =
4556 			invoke->args.etsi.CallRerouting.subscription_option;
4557 		q931_party_id_copy_to_pri(&subcmd->u.rerouting.caller, &party_id);
4558 		q931_party_redirecting_copy_to_pri(&subcmd->u.rerouting.deflection,
4559 			&deflection);
4560 		break;
4561 #if 0	/* Not handled yet */
4562 	case ROSE_ETSI_InterrogateServedUserNumbers:
4563 		break;
4564 #endif	/* Not handled yet */
4565 	case ROSE_ETSI_DivertingLegInformation1:
4566 		if (invoke->args.etsi.DivertingLegInformation1.diverted_to_present) {
4567 			rose_copy_presented_number_unscreened_to_q931(ctrl, &party_id.number,
4568 				&invoke->args.etsi.DivertingLegInformation1.diverted_to);
4569 			/*
4570 			 * We set the presentation value since the sender cannot know the
4571 			 * presentation value preference of the destination party.
4572 			 */
4573 			if (party_id.number.str[0]) {
4574 				party_id.number.presentation =
4575 					PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
4576 			} else {
4577 				party_id.number.presentation = PRES_NUMBER_NOT_AVAILABLE;
4578 			}
4579 		} else {
4580 			q931_party_number_init(&party_id.number);
4581 			party_id.number.valid = 1;
4582 		}
4583 
4584 		/*
4585 		 * Unless otherwise indicated by CONNECT, the divertedToNumber will be
4586 		 * the remote_id.number.
4587 		 */
4588 		if (!call->connected_number_in_message) {
4589 			call->remote_id.number = party_id.number;
4590 		}
4591 
4592 		/* divertedToNumber is put in redirecting.to.number */
4593 		switch (invoke->args.etsi.DivertingLegInformation1.subscription_option) {
4594 		default:
4595 		case 0:	/* noNotification */
4596 		case 1:	/* notificationWithoutDivertedToNr */
4597 			q931_party_number_init(&call->redirecting.to.number);
4598 			call->redirecting.to.number.valid = 1;
4599 			call->redirecting.to.number.presentation =
4600 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4601 			break;
4602 		case 2:	/* notificationWithDivertedToNr */
4603 			call->redirecting.to.number = party_id.number;
4604 			break;
4605 		}
4606 
4607 		call->redirecting.reason = redirectingreason_for_q931(ctrl,
4608 			invoke->args.etsi.DivertingLegInformation1.diversion_reason);
4609 		if (call->redirecting.count < PRI_MAX_REDIRECTS) {
4610 			++call->redirecting.count;
4611 		}
4612 		call->redirecting.state = Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3;
4613 		break;
4614 	case ROSE_ETSI_DivertingLegInformation2:
4615 		call->redirecting.state = Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3;
4616 		call->redirecting.count =
4617 			invoke->args.etsi.DivertingLegInformation2.diversion_counter;
4618 		if (!call->redirecting.count) {
4619 			/* To be safe, make sure that the count is non-zero. */
4620 			call->redirecting.count = 1;
4621 		}
4622 		call->redirecting.reason = redirectingreason_for_q931(ctrl,
4623 			invoke->args.etsi.DivertingLegInformation2.diversion_reason);
4624 
4625 		/* divertingNr is put in redirecting.from.number */
4626 		if (invoke->args.etsi.DivertingLegInformation2.diverting_present) {
4627 			rose_copy_presented_number_unscreened_to_q931(ctrl,
4628 				&call->redirecting.from.number,
4629 				&invoke->args.etsi.DivertingLegInformation2.diverting);
4630 		} else if (!call->redirecting_number_in_message) {
4631 			q931_party_number_init(&call->redirecting.from.number);
4632 			call->redirecting.from.number.valid = 1;
4633 		}
4634 
4635 		call->redirecting.orig_reason = PRI_REDIR_UNKNOWN;
4636 
4637 		/* originalCalledNr is put in redirecting.orig_called.number */
4638 		if (invoke->args.etsi.DivertingLegInformation2.original_called_present) {
4639 			rose_copy_presented_number_unscreened_to_q931(ctrl,
4640 				&call->redirecting.orig_called.number,
4641 				&invoke->args.etsi.DivertingLegInformation2.original_called);
4642 		} else {
4643 			q931_party_number_init(&call->redirecting.orig_called.number);
4644 		}
4645 		break;
4646 	case ROSE_ETSI_DivertingLegInformation3:
4647 		/*
4648 		 * Unless otherwise indicated by CONNECT, this will be the
4649 		 * remote_id.number.presentation.
4650 		 */
4651 		if (!invoke->args.etsi.DivertingLegInformation3.presentation_allowed_indicator) {
4652 			call->redirecting.to.number.presentation =
4653 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4654 			if (!call->connected_number_in_message) {
4655 				call->remote_id.number.presentation =
4656 					PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4657 			}
4658 		}
4659 
4660 		switch (call->redirecting.state) {
4661 		case Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3:
4662 			call->redirecting.state = Q931_REDIRECTING_STATE_IDLE;
4663 			subcmd = q931_alloc_subcommand(ctrl);
4664 			if (!subcmd) {
4665 				break;
4666 			}
4667 			/* Setup redirecting subcommand */
4668 			subcmd->cmd = PRI_SUBCMD_REDIRECTING;
4669 			q931_party_redirecting_copy_to_pri(&subcmd->u.redirecting,
4670 				&call->redirecting);
4671 			break;
4672 		default:
4673 			break;
4674 		}
4675 		break;
4676 	case ROSE_ETSI_ChargingRequest:
4677 		aoc_etsi_aoc_request(ctrl, call, invoke);
4678 		break;
4679 	case ROSE_ETSI_AOCSCurrency:
4680 		aoc_etsi_aoc_s_currency(ctrl, invoke);
4681 		break;
4682 	case ROSE_ETSI_AOCSSpecialArr:
4683 		aoc_etsi_aoc_s_special_arrangement(ctrl, invoke);
4684 		break;
4685 	case ROSE_ETSI_AOCDCurrency:
4686 		aoc_etsi_aoc_d_currency(ctrl, invoke);
4687 		break;
4688 	case ROSE_ETSI_AOCDChargingUnit:
4689 		aoc_etsi_aoc_d_charging_unit(ctrl, invoke);
4690 		break;
4691 	case ROSE_ETSI_AOCECurrency:
4692 		aoc_etsi_aoc_e_currency(ctrl, call, invoke);
4693 		break;
4694 	case ROSE_ETSI_AOCEChargingUnit:
4695 		aoc_etsi_aoc_e_charging_unit(ctrl, call, invoke);
4696 		break;
4697 #if 0	/* Not handled yet */
4698 	case ROSE_ITU_IdentificationOfCharge:
4699 		break;
4700 #endif	/* Not handled yet */
4701 	case ROSE_ETSI_EctExecute:
4702 		if (!ctrl->transfer_support) {
4703 			send_facility_error(ctrl, call, invoke->invoke_id,
4704 				ROSE_ERROR_Gen_NotSubscribed);
4705 			break;
4706 		}
4707 		error_code = etsi_ect_execute_transfer(ctrl, call, invoke->invoke_id);
4708 		if (error_code != ROSE_ERROR_None) {
4709 			send_facility_error(ctrl, call, invoke->invoke_id, error_code);
4710 		}
4711 		break;
4712 	case ROSE_ETSI_ExplicitEctExecute:
4713 		error_code = etsi_explicit_ect_execute_transfer(ctrl, call, invoke->invoke_id,
4714 			invoke->args.etsi.ExplicitEctExecute.link_id);
4715 		if (error_code != ROSE_ERROR_None) {
4716 			send_facility_error(ctrl, call, invoke->invoke_id, error_code);
4717 		}
4718 		break;
4719 	case ROSE_ETSI_RequestSubaddress:
4720 		etsi_request_subaddress(ctrl, call);
4721 		break;
4722 	case ROSE_ETSI_SubaddressTransfer:
4723 		handle_subaddress_transfer(ctrl, call,
4724 			&invoke->args.etsi.SubaddressTransfer.subaddress);
4725 		break;
4726 	case ROSE_ETSI_EctLinkIdRequest:
4727 		if (!ctrl->transfer_support) {
4728 			send_facility_error(ctrl, call, invoke->invoke_id,
4729 				ROSE_ERROR_Gen_ResourceUnavailable);
4730 			break;
4731 		}
4732 		/*
4733 		 * Use the invoke_id sequence number as a link_id.
4734 		 * It should be safe enough to do this.  If not then we will have to search
4735 		 * the call pool to ensure that the link_id is not already in use.
4736 		 */
4737 		call->master_call->link_id = get_invokeid(ctrl);
4738 		call->master_call->is_link_id_valid = 1;
4739 		send_ect_link_id_rsp(ctrl, call, invoke->invoke_id);
4740 		break;
4741 	case ROSE_ETSI_EctInform:
4742 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
4743 			q931_display_name_get(call, &call->remote_id.name);
4744 		}
4745 
4746 		/* redirectionNumber is put in remote_id.number */
4747 		if (invoke->args.etsi.EctInform.redirection_present) {
4748 			rose_copy_presented_number_unscreened_to_q931(ctrl,
4749 				&call->remote_id.number, &invoke->args.etsi.EctInform.redirection);
4750 		}
4751 
4752 		/*
4753 		 * Clear the subaddress as the remote party has been changed.
4754 		 * Any new subaddress will arrive later.
4755 		 */
4756 		q931_party_subaddress_init(&call->remote_id.subaddress);
4757 
4758 		if (!invoke->args.etsi.EctInform.status) {
4759 			/* The remote party for the transfer has not answered yet. */
4760 			call->incoming_ct_state = INCOMING_CT_STATE_EXPECT_CT_ACTIVE;
4761 		} else {
4762 			call->incoming_ct_state = INCOMING_CT_STATE_POST_CONNECTED_LINE;
4763 		}
4764 
4765 		/* Send our subaddress back if we have one. */
4766 		if (call->local_id.subaddress.valid) {
4767 			send_subaddress_transfer(ctrl, call);
4768 		}
4769 		break;
4770 	case ROSE_ETSI_EctLoopTest:
4771 		/*
4772 		 * The ETS 300 369 specification does a very poor job describing
4773 		 * how this message is used to detect loops.
4774 		 */
4775 		send_facility_error(ctrl, call, invoke->invoke_id, ROSE_ERROR_Gen_NotAvailable);
4776 		break;
4777 #if defined(STATUS_REQUEST_PLACE_HOLDER)
4778 	case ROSE_ETSI_StatusRequest:
4779 		/* Not handled yet */
4780 		break;
4781 #endif	/* defined(STATUS_REQUEST_PLACE_HOLDER) */
4782 	case ROSE_ETSI_CallInfoRetain:
4783 		if (!ctrl->cc_support) {
4784 			/*
4785 			 * Blocking the cc-available event effectively
4786 			 * disables call completion for outgoing calls.
4787 			 */
4788 			break;
4789 		}
4790 		if (call->cc.record) {
4791 			/* Duplicate message!  Should not happen. */
4792 			break;
4793 		}
4794 		cc_record = pri_cc_new_record(ctrl, call);
4795 		if (!cc_record) {
4796 			break;
4797 		}
4798 		cc_record->signaling = ctrl->link.dummy_call;
4799 		/*
4800 		 * Since we received this facility, we will not be allocating any
4801 		 * reference and linkage id's.
4802 		 */
4803 		cc_record->call_linkage_id =
4804 			invoke->args.etsi.CallInfoRetain.call_linkage_id & 0x7F;
4805 		cc_record->original_call = call;
4806 		call->cc.record = cc_record;
4807 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_AVAILABLE);
4808 		break;
4809 	case ROSE_ETSI_CCBSRequest:
4810 		pri_cc_ptmp_request(ctrl, call, invoke);
4811 		break;
4812 	case ROSE_ETSI_CCNRRequest:
4813 		pri_cc_ptmp_request(ctrl, call, invoke);
4814 		break;
4815 	case ROSE_ETSI_CCBSDeactivate:
4816 		cc_record = pri_cc_find_by_reference(ctrl,
4817 			invoke->args.etsi.CCBSDeactivate.ccbs_reference);
4818 		if (!cc_record) {
4819 			send_facility_error(ctrl, call, invoke->invoke_id,
4820 				ROSE_ERROR_CCBS_InvalidCCBSReference);
4821 			break;
4822 		}
4823 		send_facility_result_ok(ctrl, call, invoke->invoke_id);
4824 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_LINK_CANCEL);
4825 		break;
4826 	case ROSE_ETSI_CCBSInterrogate:
4827 		pri_cc_interrogate_rsp(ctrl, call, invoke);
4828 		break;
4829 	case ROSE_ETSI_CCNRInterrogate:
4830 		pri_cc_interrogate_rsp(ctrl, call, invoke);
4831 		break;
4832 	case ROSE_ETSI_CCBSErase:
4833 		cc_record = pri_cc_find_by_reference(ctrl,
4834 			invoke->args.etsi.CCBSErase.ccbs_reference);
4835 		if (!cc_record) {
4836 			/*
4837 			 * Ignore any status requests that we do not have a record.
4838 			 * We will not participate in any CC requests that we did
4839 			 * not initiate.
4840 			 */
4841 			break;
4842 		}
4843 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_LINK_CANCEL);
4844 		break;
4845 	case ROSE_ETSI_CCBSRemoteUserFree:
4846 		cc_record = pri_cc_find_by_reference(ctrl,
4847 			invoke->args.etsi.CCBSRemoteUserFree.ccbs_reference);
4848 		if (!cc_record) {
4849 			/*
4850 			 * Ignore any status requests that we do not have a record.
4851 			 * We will not participate in any CC requests that we did
4852 			 * not initiate.
4853 			 */
4854 			break;
4855 		}
4856 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_REMOTE_USER_FREE);
4857 		break;
4858 	case ROSE_ETSI_CCBSCall:
4859 		cc_record = pri_cc_find_by_reference(ctrl,
4860 			invoke->args.etsi.CCBSCall.ccbs_reference);
4861 		if (!cc_record) {
4862 			rose_error_msg_encode(ctrl, call, Q931_ANY_MESSAGE, invoke->invoke_id,
4863 				ROSE_ERROR_CCBS_InvalidCCBSReference);
4864 			call->cc.hangup_call = 1;
4865 			break;
4866 		}
4867 
4868 		/* Save off data to know how to send back any response. */
4869 		cc_record->response.signaling = call;
4870 		cc_record->response.invoke_operation = invoke->operation;
4871 		cc_record->response.invoke_id = invoke->invoke_id;
4872 
4873 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_RECALL);
4874 		break;
4875 	case ROSE_ETSI_CCBSStatusRequest:
4876 		cc_record = pri_cc_find_by_reference(ctrl,
4877 			invoke->args.etsi.CCBSStatusRequest.ccbs_reference);
4878 		if (!cc_record) {
4879 			/*
4880 			 * Ignore any status requests that we do not have a record.
4881 			 * We will not participate in any CC requests that we did
4882 			 * not initiate.
4883 			 */
4884 			break;
4885 		}
4886 
4887 		/* Save off data to know how to send back any response. */
4888 		//cc_record->response.signaling = call;
4889 		cc_record->response.invoke_operation = invoke->operation;
4890 		cc_record->response.invoke_id = invoke->invoke_id;
4891 
4892 		subcmd = q931_alloc_subcommand(ctrl);
4893 		if (!subcmd) {
4894 			break;
4895 		}
4896 
4897 		subcmd->cmd = PRI_SUBCMD_CC_STATUS_REQ;
4898 		subcmd->u.cc_status_req.cc_id = cc_record->record_id;
4899 		break;
4900 	case ROSE_ETSI_CCBSBFree:
4901 		cc_record = pri_cc_find_by_reference(ctrl,
4902 			invoke->args.etsi.CCBSBFree.ccbs_reference);
4903 		if (!cc_record) {
4904 			/*
4905 			 * Ignore any status requests that we do not have a record.
4906 			 * We will not participate in any CC requests that we did
4907 			 * not initiate.
4908 			 */
4909 			break;
4910 		}
4911 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_B_FREE);
4912 		break;
4913 	case ROSE_ETSI_EraseCallLinkageID:
4914 		cc_record = pri_cc_find_by_linkage(ctrl,
4915 			invoke->args.etsi.EraseCallLinkageID.call_linkage_id);
4916 		if (!cc_record) {
4917 			/*
4918 			 * Ignore any status requests that we do not have a record.
4919 			 * We will not participate in any CC requests that we did
4920 			 * not initiate.
4921 			 */
4922 			break;
4923 		}
4924 		/*
4925 		 * T_RETENTION expired on the network side so we will pretend
4926 		 * that it expired on our side.
4927 		 */
4928 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_TIMEOUT_T_RETENTION);
4929 		break;
4930 	case ROSE_ETSI_CCBSStopAlerting:
4931 		cc_record = pri_cc_find_by_reference(ctrl,
4932 			invoke->args.etsi.CCBSStopAlerting.ccbs_reference);
4933 		if (!cc_record) {
4934 			/*
4935 			 * Ignore any status requests that we do not have a record.
4936 			 * We will not participate in any CC requests that we did
4937 			 * not initiate.
4938 			 */
4939 			break;
4940 		}
4941 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_STOP_ALERTING);
4942 		break;
4943 	case ROSE_ETSI_CCBS_T_Request:
4944 		pri_cc_ptp_request(ctrl, call, msgtype, invoke);
4945 		break;
4946 	case ROSE_ETSI_CCNR_T_Request:
4947 		pri_cc_ptp_request(ctrl, call, msgtype, invoke);
4948 		break;
4949 	case ROSE_ETSI_CCBS_T_Call:
4950 		if (msgtype != Q931_SETUP) {
4951 			/* Ignore since it did not come in on the correct message. */
4952 			break;
4953 		}
4954 
4955 		/*
4956 		 * If we cannot find the cc_record we should still pass up the
4957 		 * CC call indication but with a -1 for the cc_id.
4958 		 * The upper layer would then need to search its records for a
4959 		 * matching CC.  The call may have come in on a different interface.
4960 		 */
4961 		q931_party_id_to_address(&party_address, &call->remote_id);
4962 		cc_record = pri_cc_find_by_addressing(ctrl, &party_address, &call->called,
4963 			call->cc.saved_ie_contents.length, call->cc.saved_ie_contents.data);
4964 		if (cc_record) {
4965 			pri_cc_event(ctrl, call, cc_record, CC_EVENT_RECALL);
4966 		} else {
4967 			subcmd = q931_alloc_subcommand(ctrl);
4968 			if (!subcmd) {
4969 				break;
4970 			}
4971 
4972 			subcmd->cmd = PRI_SUBCMD_CC_CALL;
4973 			subcmd->u.cc_call.cc_id = -1;
4974 		}
4975 		break;
4976 	case ROSE_ETSI_CCBS_T_Suspend:
4977 		cc_record = call->cc.record;
4978 		if (!cc_record) {
4979 			break;
4980 		}
4981 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_SUSPEND);
4982 		break;
4983 	case ROSE_ETSI_CCBS_T_Resume:
4984 		cc_record = call->cc.record;
4985 		if (!cc_record) {
4986 			break;
4987 		}
4988 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_RESUME);
4989 		break;
4990 	case ROSE_ETSI_CCBS_T_RemoteUserFree:
4991 		cc_record = call->cc.record;
4992 		if (!cc_record) {
4993 			break;
4994 		}
4995 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_REMOTE_USER_FREE);
4996 		break;
4997 	case ROSE_ETSI_CCBS_T_Available:
4998 		if (!ctrl->cc_support) {
4999 			/*
5000 			 * Blocking the cc-available event effectively
5001 			 * disables call completion for outgoing calls.
5002 			 */
5003 			break;
5004 		}
5005 		if (call->cc.record) {
5006 			/* Duplicate message!  Should not happen. */
5007 			break;
5008 		}
5009 		cc_record = pri_cc_new_record(ctrl, call);
5010 		if (!cc_record) {
5011 			break;
5012 		}
5013 		cc_record->original_call = call;
5014 		call->cc.record = cc_record;
5015 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_AVAILABLE);
5016 		break;
5017 	case ROSE_ETSI_MCIDRequest:
5018 		if (q931_is_dummy_call(call)) {
5019 			/* Don't even dignify this with a response. */
5020 			break;
5021 		}
5022 		if (!ctrl->mcid_support) {
5023 			send_facility_error(ctrl, call, invoke->invoke_id,
5024 				ROSE_ERROR_Gen_NotSubscribed);
5025 			break;
5026 		}
5027 		if (!call->cc.originated) {
5028 			send_facility_error(ctrl, call, invoke->invoke_id,
5029 				ROSE_ERROR_Gen_NotIncomingCall);
5030 			break;
5031 		}
5032 		switch (call->ourcallstate) {
5033 		case Q931_CALL_STATE_ACTIVE:
5034 		case Q931_CALL_STATE_DISCONNECT_INDICATION:
5035 		case Q931_CALL_STATE_DISCONNECT_REQUEST:/* XXX We are really in the wrong state for this mode. */
5036 			subcmd = q931_alloc_subcommand(ctrl);
5037 			if (!subcmd) {
5038 				send_facility_error(ctrl, call, invoke->invoke_id,
5039 					ROSE_ERROR_Gen_NotAvailable);
5040 				break;
5041 			}
5042 
5043 			subcmd->cmd = PRI_SUBCMD_MCID_REQ;
5044 			q931_party_id_copy_to_pri(&subcmd->u.mcid_req.originator, &call->local_id);
5045 			q931_party_id_copy_to_pri(&subcmd->u.mcid_req.answerer, &call->remote_id);
5046 
5047 			send_facility_result_ok(ctrl, call, invoke->invoke_id);
5048 			break;
5049 		default:
5050 			send_facility_error(ctrl, call, invoke->invoke_id,
5051 				ROSE_ERROR_Gen_InvalidCallState);
5052 			break;
5053 		}
5054 		break;
5055 #if 0	/* Not handled yet */
5056 	case ROSE_ETSI_MWIActivate:
5057 		break;
5058 	case ROSE_ETSI_MWIDeactivate:
5059 		break;
5060 	case ROSE_ETSI_MWIIndicate:
5061 		break;
5062 #endif	/* Not handled yet */
5063 	case ROSE_QSIG_CallingName:
5064 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
5065 			q931_display_name_get(call, &call->remote_id.name);
5066 		}
5067 
5068 		/* CallingName is put in remote_id.name */
5069 		rose_copy_name_to_q931(ctrl, &call->remote_id.name,
5070 			&invoke->args.qsig.CallingName.name);
5071 
5072 		switch (msgtype) {
5073 		case Q931_SETUP:
5074 		case Q931_CONNECT:
5075 			/* The caller name will automatically be reported. */
5076 			break;
5077 		default:
5078 			/* Setup connected line subcommand */
5079 			subcmd = q931_alloc_subcommand(ctrl);
5080 			if (!subcmd) {
5081 				break;
5082 			}
5083 			subcmd->cmd = PRI_SUBCMD_CONNECTED_LINE;
5084 			q931_party_id_copy_to_pri(&subcmd->u.connected_line.id, &call->remote_id);
5085 			break;
5086 		}
5087 		break;
5088 	case ROSE_QSIG_CalledName:
5089 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
5090 			q931_display_name_get(call, &call->remote_id.name);
5091 		}
5092 
5093 		/* CalledName is put in remote_id.name */
5094 		rose_copy_name_to_q931(ctrl, &call->remote_id.name,
5095 			&invoke->args.qsig.CalledName.name);
5096 
5097 		switch (msgtype) {
5098 		case Q931_SETUP:
5099 		case Q931_CONNECT:
5100 			/* The called name will automatically be reported. */
5101 			break;
5102 		default:
5103 			/* Setup connected line subcommand */
5104 			subcmd = q931_alloc_subcommand(ctrl);
5105 			if (!subcmd) {
5106 				break;
5107 			}
5108 			subcmd->cmd = PRI_SUBCMD_CONNECTED_LINE;
5109 			q931_party_id_copy_to_pri(&subcmd->u.connected_line.id, &call->remote_id);
5110 			break;
5111 		}
5112 		break;
5113 	case ROSE_QSIG_ConnectedName:
5114 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
5115 			q931_display_name_get(call, &call->remote_id.name);
5116 		}
5117 
5118 		/* ConnectedName is put in remote_id.name */
5119 		rose_copy_name_to_q931(ctrl, &call->remote_id.name,
5120 			&invoke->args.qsig.ConnectedName.name);
5121 
5122 		switch (msgtype) {
5123 		case Q931_SETUP:
5124 		case Q931_CONNECT:
5125 			/* The connected line name will automatically be reported. */
5126 			break;
5127 		default:
5128 			/* Setup connected line subcommand */
5129 			subcmd = q931_alloc_subcommand(ctrl);
5130 			if (!subcmd) {
5131 				break;
5132 			}
5133 			subcmd->cmd = PRI_SUBCMD_CONNECTED_LINE;
5134 			q931_party_id_copy_to_pri(&subcmd->u.connected_line.id, &call->remote_id);
5135 			break;
5136 		}
5137 		break;
5138 #if 0	/* Not handled yet */
5139 	case ROSE_QSIG_BusyName:
5140 		break;
5141 #endif	/* Not handled yet */
5142 #if 0	/* Not handled yet */
5143 	case ROSE_QSIG_ChargeRequest:
5144 		break;
5145 	case ROSE_QSIG_GetFinalCharge:
5146 		break;
5147 	case ROSE_QSIG_AocFinal:
5148 		break;
5149 	case ROSE_QSIG_AocInterim:
5150 		break;
5151 	case ROSE_QSIG_AocRate:
5152 		break;
5153 	case ROSE_QSIG_AocComplete:
5154 		break;
5155 	case ROSE_QSIG_AocDivChargeReq:
5156 		break;
5157 #endif	/* Not handled yet */
5158 #if 0	/* Not handled yet */
5159 	case ROSE_QSIG_CallTransferIdentify:
5160 		break;
5161 	case ROSE_QSIG_CallTransferAbandon:
5162 		break;
5163 	case ROSE_QSIG_CallTransferInitiate:
5164 		break;
5165 	case ROSE_QSIG_CallTransferSetup:
5166 		break;
5167 #endif	/* Not handled yet */
5168 	case ROSE_QSIG_CallTransferActive:
5169 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
5170 			q931_display_name_get(call, &call->remote_id.name);
5171 		}
5172 
5173 		call->incoming_ct_state = INCOMING_CT_STATE_POST_CONNECTED_LINE;
5174 
5175 		/* connectedAddress is put in remote_id */
5176 		rose_copy_presented_address_screened_to_id_q931(ctrl, &call->remote_id,
5177 			&invoke->args.qsig.CallTransferActive.connected);
5178 
5179 		/* connectedName is put in remote_id.name */
5180 		if (invoke->args.qsig.CallTransferActive.connected_name_present) {
5181 			rose_copy_name_to_q931(ctrl, &call->remote_id.name,
5182 				&invoke->args.qsig.CallTransferActive.connected_name);
5183 		}
5184 		break;
5185 	case ROSE_QSIG_CallTransferComplete:
5186 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
5187 			q931_display_name_get(call, &call->remote_id.name);
5188 		}
5189 
5190 		/* redirectionNumber is put in remote_id.number */
5191 		rose_copy_presented_number_screened_to_q931(ctrl, &call->remote_id.number,
5192 			&invoke->args.qsig.CallTransferComplete.redirection);
5193 
5194 		/* redirectionName is put in remote_id.name */
5195 		if (invoke->args.qsig.CallTransferComplete.redirection_name_present) {
5196 			rose_copy_name_to_q931(ctrl, &call->remote_id.name,
5197 				&invoke->args.qsig.CallTransferComplete.redirection_name);
5198 		}
5199 
5200 		/*
5201 		 * Clear the subaddress as the remote party has been changed.
5202 		 * Any new subaddress will arrive later.
5203 		 */
5204 		q931_party_subaddress_init(&call->remote_id.subaddress);
5205 
5206 		if (invoke->args.qsig.CallTransferComplete.call_status == 1) {
5207 			/* The remote party for the transfer has not answered yet. */
5208 			call->incoming_ct_state = INCOMING_CT_STATE_EXPECT_CT_ACTIVE;
5209 		} else {
5210 			call->incoming_ct_state = INCOMING_CT_STATE_POST_CONNECTED_LINE;
5211 		}
5212 
5213 		/* Send our subaddress back if we have one. */
5214 		if (call->local_id.subaddress.valid) {
5215 			send_subaddress_transfer(ctrl, call);
5216 		}
5217 		break;
5218 	case ROSE_QSIG_CallTransferUpdate:
5219 		party_id = call->remote_id;
5220 
5221 		if (ctrl->display_flags.receive & PRI_DISPLAY_OPTION_NAME_UPDATE) {
5222 			q931_display_name_get(call, &party_id.name);
5223 		}
5224 
5225 		/* redirectionNumber is put in party_id.number */
5226 		rose_copy_presented_number_screened_to_q931(ctrl, &party_id.number,
5227 			&invoke->args.qsig.CallTransferUpdate.redirection);
5228 
5229 		/* redirectionName is put in party_id.name */
5230 		if (invoke->args.qsig.CallTransferUpdate.redirection_name_present) {
5231 			rose_copy_name_to_q931(ctrl, &party_id.name,
5232 				&invoke->args.qsig.CallTransferUpdate.redirection_name);
5233 		}
5234 
5235 		if (q931_party_id_cmp(&party_id, &call->remote_id)) {
5236 			/* The remote_id data has changed. */
5237 			call->remote_id = party_id;
5238 			switch (call->incoming_ct_state) {
5239 			case INCOMING_CT_STATE_IDLE:
5240 				call->incoming_ct_state = INCOMING_CT_STATE_POST_CONNECTED_LINE;
5241 				break;
5242 			default:
5243 				break;
5244 			}
5245 		}
5246 		break;
5247 	case ROSE_QSIG_SubaddressTransfer:
5248 		handle_subaddress_transfer(ctrl, call,
5249 			&invoke->args.qsig.SubaddressTransfer.redirection_subaddress);
5250 		break;
5251 	case ROSE_QSIG_PathReplacement:
5252 		anfpr_pathreplacement_respond(ctrl, call, ie);
5253 		break;
5254 #if 0	/* Not handled yet */
5255 	case ROSE_QSIG_ActivateDiversionQ:
5256 		break;
5257 	case ROSE_QSIG_DeactivateDiversionQ:
5258 		break;
5259 	case ROSE_QSIG_InterrogateDiversionQ:
5260 		break;
5261 	case ROSE_QSIG_CheckRestriction:
5262 		break;
5263 #endif	/* Not handled yet */
5264 	case ROSE_QSIG_CallRerouting:
5265 		if (!ctrl->deflection_support) {
5266 			send_facility_error(ctrl, call, invoke->invoke_id,
5267 				ROSE_ERROR_Gen_NotSubscribed);
5268 			break;
5269 		}
5270 		subcmd = q931_alloc_subcommand(ctrl);
5271 		if (!subcmd) {
5272 			send_facility_error(ctrl, call, invoke->invoke_id,
5273 				ROSE_ERROR_Gen_ResourceUnavailable);
5274 			break;
5275 		}
5276 
5277 		q931_party_redirecting_init(&deflection);
5278 
5279 		/* Rerouting from the last address. */
5280 		rose_copy_presented_number_unscreened_to_q931(ctrl, &deflection.from.number,
5281 			&invoke->args.qsig.CallRerouting.last_rerouting);
5282 		if (invoke->args.qsig.CallRerouting.redirecting_name_present) {
5283 			rose_copy_name_to_q931(ctrl, &deflection.from.name,
5284 				&invoke->args.qsig.CallRerouting.redirecting_name);
5285 		}
5286 
5287 		/* Rerouting to the new address. */
5288 		rose_copy_address_to_id_q931(ctrl, &deflection.to,
5289 			&invoke->args.qsig.CallRerouting.called);
5290 		switch (invoke->args.qsig.CallRerouting.subscription_option) {
5291 		default:
5292 		case 0: /* noNotification */
5293 		case 1: /* notificationWithoutDivertedToNr */
5294 			deflection.to.number.presentation =
5295 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
5296 			break;
5297 		case 2: /* notificationWithDivertedToNr */
5298 			deflection.to.number.presentation =
5299 				PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
5300 			break;
5301 		}
5302 
5303 		/* Calling party update. */
5304 		party_id = call->local_id;
5305 		rose_copy_presented_number_screened_to_q931(ctrl, &party_id.number,
5306 			&invoke->args.qsig.CallRerouting.calling);
5307 		if (invoke->args.qsig.CallRerouting.calling_name_present) {
5308 			rose_copy_name_to_q931(ctrl, &party_id.name,
5309 				&invoke->args.qsig.CallRerouting.calling_name);
5310 		}
5311 
5312 		deflection.count = invoke->args.qsig.CallRerouting.diversion_counter;
5313 		deflection.reason = redirectingreason_for_q931(ctrl,
5314 			invoke->args.qsig.CallRerouting.rerouting_reason);
5315 
5316 		/* Original called party update. */
5317 		if (deflection.count == 1) {
5318 			deflection.orig_called = deflection.from;
5319 			deflection.orig_reason = deflection.reason;
5320 		} else {
5321 			deflection.orig_called = call->redirecting.orig_called;
5322 			deflection.orig_reason = call->redirecting.orig_reason;
5323 		}
5324 		if (invoke->args.qsig.CallRerouting.original_called_present) {
5325 			rose_copy_presented_number_unscreened_to_q931(ctrl,
5326 				&deflection.orig_called.number,
5327 				&invoke->args.qsig.CallRerouting.original_called);
5328 		}
5329 		if (invoke->args.qsig.CallRerouting.original_called_name_present) {
5330 			rose_copy_name_to_q931(ctrl, &deflection.orig_called.name,
5331 				&invoke->args.qsig.CallRerouting.original_called_name);
5332 		}
5333 		if (invoke->args.qsig.CallRerouting.original_rerouting_reason_present) {
5334 			deflection.orig_reason = redirectingreason_for_q931(ctrl,
5335 				invoke->args.qsig.CallRerouting.original_rerouting_reason);
5336 		}
5337 
5338 		subcmd->cmd = PRI_SUBCMD_REROUTING;
5339 		subcmd->u.rerouting.invoke_id = invoke->invoke_id;
5340 		subcmd->u.rerouting.subscription_option =
5341 			invoke->args.qsig.CallRerouting.subscription_option;
5342 		q931_party_id_copy_to_pri(&subcmd->u.rerouting.caller, &party_id);
5343 		q931_party_redirecting_copy_to_pri(&subcmd->u.rerouting.deflection,
5344 			&deflection);
5345 		break;
5346 	case ROSE_QSIG_DivertingLegInformation1:
5347 		q931_party_number_init(&party_id.number);
5348 		rose_copy_number_to_q931(ctrl, &party_id.number,
5349 			&invoke->args.qsig.DivertingLegInformation1.nominated_number);
5350 		if (party_id.number.str[0]) {
5351 			party_id.number.presentation =
5352 				PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
5353 		}
5354 
5355 		/*
5356 		 * Unless otherwise indicated by CONNECT, the nominatedNr will be
5357 		 * the remote_id.number.
5358 		 */
5359 		if (!call->connected_number_in_message) {
5360 			call->remote_id.number = party_id.number;
5361 		}
5362 
5363 		/* nominatedNr is put in redirecting.to.number */
5364 		switch (invoke->args.qsig.DivertingLegInformation1.subscription_option) {
5365 		default:
5366 		case QSIG_NO_NOTIFICATION:
5367 		case QSIG_NOTIFICATION_WITHOUT_DIVERTED_TO_NR:
5368 			q931_party_number_init(&call->redirecting.to.number);
5369 			call->redirecting.to.number.valid = 1;
5370 			call->redirecting.to.number.presentation =
5371 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
5372 			break;
5373 		case QSIG_NOTIFICATION_WITH_DIVERTED_TO_NR:
5374 			call->redirecting.to.number = party_id.number;
5375 			break;
5376 		}
5377 
5378 		call->redirecting.reason = redirectingreason_for_q931(ctrl,
5379 			invoke->args.qsig.DivertingLegInformation1.diversion_reason);
5380 		if (call->redirecting.count < PRI_MAX_REDIRECTS) {
5381 			++call->redirecting.count;
5382 		}
5383 		call->redirecting.state = Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3;
5384 		break;
5385 	case ROSE_QSIG_DivertingLegInformation2:
5386 		call->redirecting.state = Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3;
5387 		call->redirecting.count =
5388 			invoke->args.qsig.DivertingLegInformation2.diversion_counter;
5389 		if (!call->redirecting.count) {
5390 			/* To be safe, make sure that the count is non-zero. */
5391 			call->redirecting.count = 1;
5392 		}
5393 		call->redirecting.reason = redirectingreason_for_q931(ctrl,
5394 			invoke->args.qsig.DivertingLegInformation2.diversion_reason);
5395 
5396 		/* divertingNr is put in redirecting.from.number */
5397 		if (invoke->args.qsig.DivertingLegInformation2.diverting_present) {
5398 			rose_copy_presented_number_unscreened_to_q931(ctrl,
5399 				&call->redirecting.from.number,
5400 				&invoke->args.qsig.DivertingLegInformation2.diverting);
5401 		} else if (!call->redirecting_number_in_message) {
5402 			q931_party_number_init(&call->redirecting.from.number);
5403 			call->redirecting.from.number.valid = 1;
5404 		}
5405 
5406 		/* redirectingName is put in redirecting.from.name */
5407 		if (invoke->args.qsig.DivertingLegInformation2.redirecting_name_present) {
5408 			rose_copy_name_to_q931(ctrl, &call->redirecting.from.name,
5409 				&invoke->args.qsig.DivertingLegInformation2.redirecting_name);
5410 		} else {
5411 			q931_party_name_init(&call->redirecting.from.name);
5412 		}
5413 
5414 		call->redirecting.orig_reason = PRI_REDIR_UNKNOWN;
5415 		if (invoke->args.qsig.DivertingLegInformation2.original_diversion_reason_present) {
5416 			call->redirecting.orig_reason = redirectingreason_for_q931(ctrl,
5417 				invoke->args.qsig.DivertingLegInformation2.original_diversion_reason);
5418 		}
5419 
5420 		/* originalCalledNr is put in redirecting.orig_called.number */
5421 		if (invoke->args.qsig.DivertingLegInformation2.original_called_present) {
5422 			rose_copy_presented_number_unscreened_to_q931(ctrl,
5423 				&call->redirecting.orig_called.number,
5424 				&invoke->args.qsig.DivertingLegInformation2.original_called);
5425 		} else {
5426 			q931_party_number_init(&call->redirecting.orig_called.number);
5427 		}
5428 
5429 		/* originalCalledName is put in redirecting.orig_called.name */
5430 		if (invoke->args.qsig.DivertingLegInformation2.original_called_name_present) {
5431 			rose_copy_name_to_q931(ctrl, &call->redirecting.orig_called.name,
5432 				&invoke->args.qsig.DivertingLegInformation2.original_called_name);
5433 		} else {
5434 			q931_party_name_init(&call->redirecting.orig_called.name);
5435 		}
5436 		break;
5437 	case ROSE_QSIG_DivertingLegInformation3:
5438 		/*
5439 		 * Unless otherwise indicated by CONNECT, this will be the
5440 		 * remote_id.number.presentation.
5441 		 */
5442 		if (!invoke->args.qsig.DivertingLegInformation3.presentation_allowed_indicator) {
5443 			call->redirecting.to.number.presentation =
5444 				PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
5445 			if (!call->connected_number_in_message) {
5446 				call->remote_id.number.presentation =
5447 					PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
5448 			}
5449 		}
5450 
5451 		/* redirectionName is put in redirecting.to.name */
5452 		if (invoke->args.qsig.DivertingLegInformation3.redirection_name_present) {
5453 			rose_copy_name_to_q931(ctrl, &call->redirecting.to.name,
5454 				&invoke->args.qsig.DivertingLegInformation3.redirection_name);
5455 			if (!invoke->args.qsig.DivertingLegInformation3.presentation_allowed_indicator) {
5456 				call->redirecting.to.name.presentation = PRI_PRES_RESTRICTED;
5457 			}
5458 		} else {
5459 			q931_party_name_init(&call->redirecting.to.name);
5460 		}
5461 
5462 		switch (call->redirecting.state) {
5463 		case Q931_REDIRECTING_STATE_EXPECTING_RX_DIV_LEG_3:
5464 			call->redirecting.state = Q931_REDIRECTING_STATE_IDLE;
5465 			subcmd = q931_alloc_subcommand(ctrl);
5466 			if (!subcmd) {
5467 				break;
5468 			}
5469 			/* Setup redirecting subcommand */
5470 			subcmd->cmd = PRI_SUBCMD_REDIRECTING;
5471 			q931_party_redirecting_copy_to_pri(&subcmd->u.redirecting,
5472 				&call->redirecting);
5473 			break;
5474 		default:
5475 			break;
5476 		}
5477 		break;
5478 #if 0	/* Not handled yet */
5479 	case ROSE_QSIG_CfnrDivertedLegFailed:
5480 		break;
5481 #endif	/* Not handled yet */
5482 	case ROSE_QSIG_CcbsRequest:
5483 		pri_cc_qsig_request(ctrl, call, msgtype, invoke);
5484 		break;
5485 	case ROSE_QSIG_CcnrRequest:
5486 		pri_cc_qsig_request(ctrl, call, msgtype, invoke);
5487 		break;
5488 	case ROSE_QSIG_CcCancel:
5489 		pri_cc_qsig_cancel(ctrl, call, msgtype, invoke);
5490 		break;
5491 	case ROSE_QSIG_CcExecPossible:
5492 		pri_cc_qsig_exec_possible(ctrl, call, msgtype, invoke);
5493 		break;
5494 	case ROSE_QSIG_CcPathReserve:
5495 		/*!
5496 		 * \todo It may be possible for us to accept the ccPathReserve call.
5497 		 * We could certainly never initiate it.
5498 		 */
5499 		rose_error_msg_encode(ctrl, call, Q931_ANY_MESSAGE, invoke->invoke_id,
5500 			ROSE_ERROR_QSIG_FailedDueToInterworking);
5501 		call->cc.hangup_call = 1;
5502 		break;
5503 	case ROSE_QSIG_CcRingout:
5504 		if (msgtype != Q931_SETUP) {
5505 			/*
5506 			 * Ignore since it did not come in on the correct message.
5507 			 *
5508 			 * It could come in on a FACILITY message if we supported
5509 			 * incoming ccPathReserve calls.
5510 			 */
5511 			break;
5512 		}
5513 
5514 		q931_party_id_to_address(&party_address, &call->remote_id);
5515 		cc_record = pri_cc_find_by_addressing(ctrl, &party_address, &call->called,
5516 			call->cc.saved_ie_contents.length, call->cc.saved_ie_contents.data);
5517 		if (cc_record) {
5518 			/* Save off data to know how to send back any response. */
5519 			cc_record->response.signaling = call;
5520 			cc_record->response.invoke_operation = invoke->operation;
5521 			cc_record->response.invoke_id = invoke->invoke_id;
5522 
5523 			pri_cc_event(ctrl, call, cc_record, CC_EVENT_RECALL);
5524 		} else {
5525 			rose_error_msg_encode(ctrl, call, Q931_ANY_MESSAGE, invoke->invoke_id,
5526 				ROSE_ERROR_QSIG_FailureToMatch);
5527 			call->cc.hangup_call = 1;
5528 		}
5529 		break;
5530 	case ROSE_QSIG_CcSuspend:
5531 		cc_record = call->cc.record;
5532 		if (!cc_record) {
5533 			break;
5534 		}
5535 		cc_record->fsm.qsig.msgtype = msgtype;
5536 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_SUSPEND);
5537 		break;
5538 	case ROSE_QSIG_CcResume:
5539 		cc_record = call->cc.record;
5540 		if (!cc_record) {
5541 			break;
5542 		}
5543 		cc_record->fsm.qsig.msgtype = msgtype;
5544 		pri_cc_event(ctrl, call, cc_record, CC_EVENT_RESUME);
5545 		break;
5546 #if 0	/* Not handled yet */
5547 	case ROSE_QSIG_MWIActivate:
5548 		break;
5549 	case ROSE_QSIG_MWIDeactivate:
5550 		break;
5551 	case ROSE_QSIG_MWIInterrogate:
5552 		break;
5553 #endif	/* Not handled yet */
5554 	default:
5555 		if (ctrl->debug & PRI_DEBUG_APDU) {
5556 			pri_message(ctrl,
5557 				"!! ROSE invoke operation not handled on switchtype:%s! %s\n",
5558 				pri_switch2str(ctrl->switchtype), rose_operation2str(invoke->operation));
5559 		}
5560 		break;
5561 	}
5562 }
5563