96e4a84faacc57545ea90e3cb505425620dc0041
[sip-router] / modules / tm / t_cancel.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License 
25  * along with this program; if not, write to the Free Software 
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  *
28  * History:
29  * ----------
30  * 2003-04-14  checking if a reply sent before cancel is initiated
31  *             moved here (jiri)
32  * 2004-02-11  FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
33  * 2004-02-13  timer_link.payload removed (bogdan)
34  * 2006-10-10  cancel_uacs  & cancel_branch take more options now (andrei)
35  * 2007-03-15  TMCB_ONSEND hooks added (andrei)
36  * 2007-05-28: cancel_branch() constructs the CANCEL from the
37  *             outgoing INVITE instead of the incomming one.
38  *             (it can be disabled with reparse_invite=0) (Miklos)
39  * 2007-06-04  cancel_branch() can operate in lockless mode (with a lockless
40  *              should_cancel()) (andrei)
41  * 2008-03-07  cancel_branch() takes a new flag: F_CANCEL_B_FORCE_RETR (andrei)
42  * 2008-03-08  e2e_cancel handles non replied branches in 3 different ways,
43  *              selectable by the tm cancel_b_method parameter: fake reply,
44  *              retransmit request and send cancel on branch (andrei)
45  */
46
47 #include <stdio.h> /* for FILE* in fifo_uac_cancel */
48 #ifdef EXTRA_DEBUG
49 #include <assert.h>
50 #endif /* EXTRA_DEBUG */
51
52 #include "defs.h"
53 #include "config.h"
54
55 #include "t_funcs.h"
56 #include "../../dprint.h"
57 #include "../../ut.h"
58 #include "t_reply.h"
59 #include "t_cancel.h"
60 #include "t_msgbuilder.h"
61 #include "t_lookup.h" /* for t_lookup_callid in fifo_uac_cancel */
62 #include "t_hooks.h"
63
64
65 /* determine which branches should be canceled; can be called 
66    without REPLY_LOCK, since should_cancel_branch() is atomic now
67    -- andrei
68  WARNING: - has side effects, see should_cancel_branch()
69           - one _must_ call cancel_uacs(cancel_bm) if *cancel_bm!=0 or
70             you'll have some un-cancelable branches */
71 void which_cancel( struct cell *t, branch_bm_t *cancel_bm )
72 {
73         int i;
74         int branches_no;
75         
76         *cancel_bm=0;
77         branches_no=t->nr_of_outgoings;
78         membar_depends(); 
79         for( i=0 ; i<branches_no ; i++ ) {
80                 if (should_cancel_branch(t, i, 1)) 
81                         *cancel_bm |= 1<<i ;
82         }
83 }
84
85
86
87
88 /* cancel branches scheduled for deletion
89  * params: t          - transaction
90  *          cancel_bm - bitmap with the branches that are supposed to be 
91  *                       canceled 
92  *          flags     - how_to_cancel flags, see cancel_branch()
93  * returns: bitmap with the still active branches (on fr timer)
94  * WARNING: always fill cancel_bm using which_cancel(), supplying values
95  *          in any other way is a bug*/
96 int cancel_uacs( struct cell *t, branch_bm_t cancel_bm, int flags)
97 {
98         int i;
99         int ret;
100         int r;
101
102         ret=0;
103         /* cancel pending client transactions, if any */
104         for( i=0 ; i<t->nr_of_outgoings ; i++ ) 
105                 if (cancel_bm & (1<<i)){
106                         r=cancel_branch(
107                                 t,
108                                 i,
109                                 flags | ((t->uac[i].request.buffer==NULL)?
110                                         F_CANCEL_B_FAKE_REPLY:0) /* blind UAC? */
111                         );
112                         ret|=(r!=0)<<i;
113                 }
114         return ret;
115 }
116
117 int cancel_all_uacs(struct cell *trans, int how)
118 {
119         branch_bm_t cancel_bm;
120         int i,j;
121
122 #ifdef EXTRA_DEBUG
123         assert(trans);
124 #endif
125         DBG("Canceling T@%p [%u:%u]\n", trans, trans->hash_index, trans->label);
126         
127         cancel_bm=0;
128         which_cancel(trans, &cancel_bm);
129          /* tell tm to cancel the call */
130         i=cancel_uacs(trans, cancel_bm, how);
131         
132         if (how & F_CANCEL_UNREF)
133 #ifndef TM_DEL_UNREF
134         /* in case of 'too many' _buggy_ invocations, the ref count (a uint) might 
135          * actually wrap around, possibly leaving the T leaking. */
136 #warning "use of F_CANCEL_UNREF flag is unsafe without defining TM_DEL_UNREF"
137 #endif
138                 UNREF(trans);
139
140         /* count the still active branches */
141         if (! how) {
142                 j=0;
143                 while(i){
144                         j++;
145                         i&=i-1;
146                 }
147                 return j;
148         }
149         return 0;
150 }
151
152
153 /* should be called directly only if one of the condition bellow is true:
154  *  - should_cancel_branch or which_cancel returned true for this branch
155  *  - buffer value was 0 and then set to BUSY in an atomic op.:
156  *     if (atomic_cmpxchg_long(&buffer, 0, BUSY_BUFFER)==0).
157  *
158  * params:  t - transaction
159  *          branch - branch number to be canceled
160  *          flags - howto cancel: 
161  *                   F_CANCEL_B_KILL - will completely stop the 
162  *                     branch (stops the timers), use with care
163  *                   F_CANCEL_B_FAKE_REPLY - will send a fake 487
164  *                      to all branches that haven't received any response
165  *                      (>=100). It assumes the REPLY_LOCK is not held
166  *                      (if it is => deadlock)
167  *                  F_CANCEL_B_FORCE_C - will send a cancel (and create the 
168  *                       corresp. local cancel rb) even if no reply was 
169  *                       received; F_CANCEL_B_FAKE_REPLY will be ignored.
170  *                  F_CANCEL_B_FORCE_RETR - don't stop retransmission if no 
171  *                       reply was received on the branch; incompatible
172  *                       with F_CANCEL_B_FAKE_REPLY, F_CANCEL_B_FORCE_C and
173  *                       F_CANCEL_B_KILL (all of them take precedence) a
174  *                  default: stop only the retransmissions for the branch
175  *                      and leave it to timeout if it doesn't receive any
176  *                      response to the CANCEL
177  * returns: 0 - branch inactive after running cancel_branch() 
178  *          1 - branch still active  (fr_timer)
179  *         -1 - error
180  * WARNING:
181  *          - F_CANCEL_B_KILL should be used only if the transaction is killed
182  *            explicitly afterwards (since it might kill all the timers
183  *            the transaction won't be able to "kill" itself => if not
184  *            explicitly "put_on_wait" it might live forever)
185  *          - F_CANCEL_B_FAKE_REPLY must be used only if the REPLY_LOCK is not
186  *            held
187  *          - checking for buffer==0 under REPLY_LOCK is no enough, an 
188  *           atomic_cmpxhcg or atomic_get_and_set _must_ be used.
189  */
190 int cancel_branch( struct cell *t, int branch, int flags )
191 {
192         char *cancel;
193         unsigned int len;
194         struct retr_buf *crb, *irb;
195         int ret;
196         branch_bm_t tmp_bm;
197
198         crb=&t->uac[branch].local_cancel;
199         irb=&t->uac[branch].request;
200         irb->flags|=F_RB_CANCELED;
201         ret=1;
202
203 #       ifdef EXTRA_DEBUG
204         if (crb->buffer!=BUSY_BUFFER) {
205                 LOG(L_CRIT, "ERROR: attempt to rewrite cancel buffer: %p\n",
206                                 crb->buffer);
207                 abort();
208         }
209 #       endif
210
211         if (flags & F_CANCEL_B_KILL){
212                 stop_rb_timers( irb );
213                 ret=0;
214                 if ((t->uac[branch].last_received < 100) &&
215                                 !(flags & F_CANCEL_B_FORCE_C)) {
216                         DBG("DEBUG: cancel_branch: no response ever received: "
217                             "giving up on cancel\n");
218                         /* remove BUSY_BUFFER -- mark cancel buffer as not used */
219                         atomic_set_long((void*)&crb->buffer, 0);
220                         if (flags & F_CANCEL_B_FAKE_REPLY){
221                                 LOCK_REPLIES(t);
222                                 if (relay_reply(t, FAKED_REPLY, branch, 487, &tmp_bm, 1) == 
223                                                                                 RPS_ERROR){
224                                         return -1;
225                                 }
226                         }
227                         /* do nothing, hope that the caller will clean up */
228                         return ret;
229                 }
230         }else{
231                 if (t->uac[branch].last_received < 100){
232                         if (!(flags & F_CANCEL_B_FORCE_C)) {
233                                 /* no response received => don't send a cancel on this branch,
234                                  *  just drop it */
235                                 if (!(flags & F_CANCEL_B_FORCE_RETR))
236                                         stop_rb_retr(irb); /* stop retransmissions */
237                                 /* remove BUSY_BUFFER -- mark cancel buffer as not used */
238                                 atomic_set_long((void*)&crb->buffer, 0);
239                                 if (flags & F_CANCEL_B_FAKE_REPLY){
240                                         stop_rb_timers( irb ); /* stop even the fr timer */
241                                         LOCK_REPLIES(t);
242                                         if (relay_reply(t, FAKED_REPLY, branch, 487, &tmp_bm, 1)== 
243                                                                                         RPS_ERROR){
244                                                 return -1;
245                                         }
246                                         return 0; /* should be inactive after the 487 */
247                                 }
248                                 /* do nothing, just wait for the final timeout */
249                                 return 1;
250                         }
251                 }
252                 stop_rb_retr(irb); /* stop retransmissions */
253         }
254
255         if (cfg_get(tm, tm_cfg, reparse_invite)) {
256                 /* build the CANCEL from the INVITE which was sent out */
257                 cancel = build_local_reparse(t, branch, &len, CANCEL, CANCEL_LEN, &t->to);
258         } else {
259                 /* build the CANCEL from the reveived INVITE */
260                 cancel = build_local(t, branch, &len, CANCEL, CANCEL_LEN, &t->to);
261         }
262         if (!cancel) {
263                 LOG(L_ERR, "ERROR: attempt to build a CANCEL failed\n");
264                 /* remove BUSY_BUFFER -- mark cancel buffer as not used */
265                 atomic_set_long((void*)&crb->buffer, 0);
266                 return -1;
267         }
268         /* install cancel now */
269         crb->dst = irb->dst;
270         crb->branch = branch;
271         /* label it as cancel so that FR timer can better know how to
272            deal with it */
273         crb->activ_type = TYPE_LOCAL_CANCEL;
274         /* be extra carefully and check for bugs (the below if could be replaced
275          *  by an atomic_set((void*)&crb->buffer, cancel) */
276         if (unlikely(atomic_cmpxchg_long((void*)&crb->buffer, (long)BUSY_BUFFER,
277                                                         (long)cancel)!= (long)BUSY_BUFFER)){
278                 BUG("tm: cancel_branch: local_cancel buffer=%p != BUSY_BUFFER"
279                                 " (trying to continue)\n", crb->buffer);
280                 shm_free(cancel);
281                 return -1;
282         }
283         membar_write_atomic_op(); /* cancel retr. can be called from 
284                                                                  reply_received w/o the reply lock held => 
285                                                                  they check for buffer_len to 
286                                                                  see if a valid reply exists */
287         crb->buffer_len = len;
288
289         DBG("DEBUG: cancel_branch: sending cancel...\n");
290 #ifdef TMCB_ONSEND
291         if (SEND_BUFFER( crb )>=0){
292                 if (unlikely (has_tran_tmcbs(t, TMCB_REQUEST_SENT)))
293                         run_onsend_callbacks(TMCB_REQUEST_SENT, crb, 0, 0, TMCB_LOCAL_F);
294         }
295 #else
296         SEND_BUFFER( crb );
297 #endif
298         /*sets and starts the FINAL RESPONSE timer */
299         if (start_retr( crb )!=0)
300                 LOG(L_CRIT, "BUG: cancel_branch: failed to start retransmission"
301                                         " for %p\n", crb);
302         return ret;
303 }
304
305
306 const char* rpc_cancel_doc[2] = {
307         "Cancel a pending transaction",
308         0
309 };
310
311
312 /* fifo command to cancel a pending call (Uli)
313  * Syntax:
314  *
315  * ":uac_cancel:[response file]\n
316  * callid\n
317  * cseq\n
318  */
319 void rpc_cancel(rpc_t* rpc, void* c)
320 {
321         struct cell *trans;
322         static char cseq[128], callid[128];
323         branch_bm_t cancel_bm;
324         int i,j;
325
326         str cseq_s;   /* cseq */
327         str callid_s; /* callid */
328
329         cseq_s.s=cseq;
330         callid_s.s=callid;
331         cancel_bm=0;
332
333         if (rpc->scan(c, "SS", &callid_s, &cseq_s) < 2) {
334                 rpc->fault(c, 400, "Callid and CSeq expected as parameters");
335                 return;
336         }
337
338         if( t_lookup_callid(&trans, callid_s, cseq_s) < 0 ) {
339                 DBG("Lookup failed\n");
340                 rpc->fault(c, 400, "Transaction not found");
341                 return;
342         }
343         /*  find the branches that need cancel-ing */
344         LOCK_REPLIES(trans);
345                 which_cancel(trans, &cancel_bm);
346         UNLOCK_REPLIES(trans);
347          /* tell tm to cancel the call */
348         DBG("Now calling cancel_uacs\n");
349         i=cancel_uacs(trans, cancel_bm, 0); /* don't fake 487s, 
350                                                                                  just wait for timeout */
351         
352         /* t_lookup_callid REF`d the transaction for us, we must UNREF here! */
353         UNREF(trans);
354         j=0;
355         while(i){
356                 j++;
357                 i&=i-1;
358         }
359         rpc->add(c, "ds", j, "branches remaining (waiting for timeout)");
360 }
361
362
363
364 /* returns <0 on error */
365 int cancel_b_flags_get(unsigned int* f, int m)
366 {
367         int ret;
368         
369         ret=0;
370         switch(m){
371                 case 1:
372                         *f=F_CANCEL_B_FORCE_RETR;
373                         break;
374                 case 0:
375                         *f=F_CANCEL_B_FAKE_REPLY;
376                         break;
377                 case 2:
378                         *f=F_CANCEL_B_FORCE_C;
379                         break;
380                 default:
381                         *f=F_CANCEL_B_FAKE_REPLY;
382                         ret=-1;
383         }
384         return ret;
385 }
386
387
388
389 /* fixup function for the default cancel branch method/flags
390  * (called by the configuration framework) */
391 int cancel_b_flags_fixup(void* handle, str* name, void** val)
392 {
393         unsigned int m,f;
394         int ret;
395         
396         m=(unsigned int)(long)(*val);
397         ret=cancel_b_flags_get(&f, m);
398         if (ret<0)
399                 ERR("cancel_b_flags_fixup: invalid value for %.*s; %d\n",
400                                 name->len, name->s, m);
401         *val=(void*)(long)f;
402         return ret;
403 }
404
405
406
407