root/openbsc/include/openbsc/gsm_data.h @ dfe6c7d910edbba238751d0cfddbb627bb08ef54

Revision dfe6c7d910edbba238751d0cfddbb627bb08ef54, 16.3 kB (checked in by Harald Welte <laforge@netfilter.org>, 7 months ago)

split 'libosmocore' from openbsc codebase

This library is intended to collect all generic/common funcitionality
of all Osmocom.org projects, including OpenBSC but also OsmocomBB

The library currently includes the following modules:

bitvec, comp128, gsm_utils, msgb, select, signal, statistics, talloc, timer,
tlv_parse, linuxlist

msgb allocation error debugging had to be temporarily disabled as it depends on
'debug.c' functionality which at the moment remains in OpenBSC

  • Property mode set to 100644
Line 
1#ifndef _GSM_DATA_H
2#define _GSM_DATA_H
3
4#include <sys/types.h>
5
6struct value_string {
7        unsigned int value;
8        const char *str;
9};
10
11const char *get_value_string(const struct value_string *vs, u_int32_t val);
12int get_string_value(const struct value_string *vs, const char *str);
13
14enum gsm_phys_chan_config {
15        GSM_PCHAN_NONE,
16        GSM_PCHAN_CCCH,
17        GSM_PCHAN_CCCH_SDCCH4,
18        GSM_PCHAN_TCH_F,
19        GSM_PCHAN_TCH_H,
20        GSM_PCHAN_SDCCH8_SACCH8C,
21        GSM_PCHAN_PDCH,         /* GPRS PDCH */
22        GSM_PCHAN_TCH_F_PDCH,   /* TCH/F if used, PDCH otherwise */
23        GSM_PCHAN_UNKNOWN,
24};
25
26enum gsm_chan_t {
27        GSM_LCHAN_NONE,
28        GSM_LCHAN_SDCCH,
29        GSM_LCHAN_TCH_F,
30        GSM_LCHAN_TCH_H,
31        GSM_LCHAN_UNKNOWN,
32};
33
34/* RRLP mode of operation */
35enum rrlp_mode {
36        RRLP_MODE_NONE,
37        RRLP_MODE_MS_BASED,
38        RRLP_MODE_MS_PREF,
39        RRLP_MODE_ASS_PREF,
40};
41
42/* Channel Request reason */
43enum gsm_chreq_reason_t {
44        GSM_CHREQ_REASON_EMERG,
45        GSM_CHREQ_REASON_PAG,
46        GSM_CHREQ_REASON_CALL,
47        GSM_CHREQ_REASON_LOCATION_UPD,
48        GSM_CHREQ_REASON_OTHER,
49};
50
51#include <osmocore/timer.h>
52#include <openbsc/gsm_04_08.h>
53#include <openbsc/abis_rsl.h>
54#include <openbsc/mncc.h>
55#include <osmocore/tlv.h>
56#include <osmocore/bitvec.h>
57#include <osmocore/statistics.h>
58#include <osmocore/gsm_utils.h>
59#include <osmocore/utils.h>
60
61#define TRX_NR_TS       8
62#define TS_MAX_LCHAN    8
63
64#define HARDCODED_ARFCN 123
65#define HARDCODED_TSC   7
66#define HARDCODED_BSIC  0x3f    /* NCC = 7 / BCC = 7 */
67
68/* for multi-drop config */
69#define HARDCODED_BTS0_TS       1
70#define HARDCODED_BTS1_TS       6
71#define HARDCODED_BTS2_TS       11
72
73enum gsm_hooks {
74        GSM_HOOK_NM_SWLOAD,
75        GSM_HOOK_RR_PAGING,
76};
77
78enum gsm_paging_event {
79        GSM_PAGING_SUCCEEDED,
80        GSM_PAGING_EXPIRED,
81        GSM_PAGING_OOM,
82};
83
84struct msgb;
85typedef int gsm_cbfn(unsigned int hooknum,
86                     unsigned int event,
87                     struct msgb *msg,
88                     void *data, void *param);
89
90/*
91 * Use the channel. As side effect the lchannel recycle timer
92 * will be started.
93 */
94#define LCHAN_RELEASE_TIMEOUT 20, 0
95#define use_lchan(lchan) \
96        do {    lchan->use_count++; \
97                DEBUGP(DREF, "lchan (bts=%d,trx=%d,ts=%d,ch=%d) increases usage to: %d\n", \
98                        lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, \
99                        lchan->nr, lchan->use_count); \
100                bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT); } while(0);
101
102#define put_lchan(lchan) \
103        do { lchan->use_count--; \
104                DEBUGP(DREF, "lchan (bts=%d,trx=%d,ts=%d,ch=%d) decreases usage to: %d\n", \
105                        lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr, \
106                        lchan->nr, lchan->use_count); \
107        } while(0);
108
109
110/* communications link with a BTS */
111struct gsm_bts_link {
112        struct gsm_bts *bts;
113};
114
115/* Real authentication information containing Ki */
116enum gsm_auth_algo {
117        AUTH_ALGO_NONE,
118        AUTH_ALGO_XOR,
119        AUTH_ALGO_COMP128v1,
120};
121
122struct gsm_auth_info {
123        enum gsm_auth_algo auth_algo;
124        unsigned int a3a8_ki_len;
125        u_int8_t a3a8_ki[16];
126};
127
128struct gsm_auth_tuple {
129        int use_count;
130        int key_seq;
131        u_int8_t rand[16];
132        u_int8_t sres[4];
133        u_int8_t kc[8];
134};
135
136
137struct gsm_lchan;
138struct gsm_subscriber;
139struct gsm_mncc;
140struct rtp_socket;
141
142/* Network Management State */
143struct gsm_nm_state {
144        u_int8_t operational;
145        u_int8_t administrative;
146        u_int8_t availability;
147};
148
149/*
150 * LOCATION UPDATING REQUEST state
151 *
152 * Our current operation is:
153 *      - Get imei/tmsi
154 *      - Accept/Reject according to global policy
155 */
156struct gsm_loc_updating_operation {
157        struct timer_list updating_timer;
158        unsigned int waiting_for_imsi : 1;
159        unsigned int waiting_for_imei : 1;
160};
161
162/* Maximum number of neighbor cells whose average we track */
163#define MAX_NEIGH_MEAS          10
164/* Maximum size of the averaging window for neighbor cells */
165#define MAX_WIN_NEIGH_AVG       10
166
167/* processed neighbor measurements for one cell */
168struct neigh_meas_proc {
169        u_int16_t arfcn;
170        u_int8_t bsic;
171        u_int8_t rxlev[MAX_WIN_NEIGH_AVG];
172        unsigned int rxlev_cnt;
173        u_int8_t last_seen_nr;
174};
175
176#define MAX_A5_KEY_LEN  (128/8)
177#define RSL_ENC_ALG_A5(x)       (x+1)
178
179/* is the data link established? who established it? */
180#define LCHAN_SAPI_UNUSED       0
181#define LCHAN_SAPI_MS           1
182#define LCHAN_SAPI_NET          2
183
184/* state of a logical channel */
185enum gsm_lchan_state {
186        LCHAN_S_NONE,           /* channel is not active */
187        LCHAN_S_ACT_REQ,        /* channel activatin requested */
188        LCHAN_S_ACTIVE,         /* channel is active and operational */
189        LCHAN_S_REL_REQ,        /* channel release has been requested */
190        LCHAN_S_INACTIVE,       /* channel is set inactive */
191};
192
193struct gsm_lchan {
194        /* The TS that we're part of */
195        struct gsm_bts_trx_ts *ts;
196        /* The logical subslot number in the TS */
197        u_int8_t nr;
198        /* The logical channel type */
199        enum gsm_chan_t type;
200        /* RSL channel mode */
201        enum rsl_cmod_spd rsl_cmode;
202        /* If TCH, traffic channel mode */
203        enum gsm48_chan_mode tch_mode;
204        /* State */
205        enum gsm_lchan_state state;
206        /* Power levels for MS and BTS */
207        u_int8_t bs_power;
208        u_int8_t ms_power;
209        /* Encryption information */
210        struct {
211                u_int8_t alg_id;
212                u_int8_t key_len;
213                u_int8_t key[MAX_A5_KEY_LEN];
214        } encr;
215        /* Are we part of a special "silent" call */
216        int silent_call;
217
218        /* AMR bits */
219        struct gsm48_multi_rate_conf mr_conf;
220       
221        /* To whom we are allocated at the moment */
222        struct gsm_subscriber *subscr;
223
224        /* Timer started to release the channel */
225        struct timer_list release_timer;
226
227        struct timer_list T3101;
228
229        /* Established data link layer services */
230        u_int8_t sapis[8];
231
232        /*
233         * Operations that have a state and might be pending
234         */
235        struct gsm_loc_updating_operation *loc_operation;
236
237        /* use count. how many users use this channel */
238        unsigned int use_count;
239
240        /* cache of last measurement reports on this lchan */
241        struct gsm_meas_rep meas_rep[6];
242        int meas_rep_idx;
243
244        /* table of neighbor cell measurements */
245        struct neigh_meas_proc neigh_meas[MAX_NEIGH_MEAS];
246
247        struct {
248                u_int32_t bound_ip;
249                u_int32_t connect_ip;
250                u_int16_t bound_port;
251                u_int16_t connect_port;
252                u_int16_t conn_id;
253                u_int8_t rtp_payload2;
254                u_int8_t speech_mode;
255                struct rtp_socket *rtp_socket;
256        } abis_ip;
257};
258
259struct gsm_e1_subslot {
260        /* Number of E1 link */
261        u_int8_t        e1_nr;
262        /* Number of E1 TS inside E1 link */
263        u_int8_t        e1_ts;
264        /* Sub-slot within the E1 TS, 0xff if full TS */
265        u_int8_t        e1_ts_ss;
266};
267
268#define BTS_TRX_F_ACTIVATED     0x0001
269/* One Timeslot in a TRX */
270struct gsm_bts_trx_ts {
271        struct gsm_bts_trx *trx;
272        /* number of this timeslot at the TRX */
273        u_int8_t nr;
274
275        enum gsm_phys_chan_config pchan;
276
277        unsigned int flags;
278        struct gsm_nm_state nm_state;
279        struct tlv_parsed nm_attr;
280        u_int8_t nm_chan_comb;
281
282        /* To which E1 subslot are we connected */
283        struct gsm_e1_subslot e1_link;
284
285        struct gsm_lchan lchan[TS_MAX_LCHAN];
286};
287
288/* One TRX in a BTS */
289struct gsm_bts_trx {
290        /* list header in bts->trx_list */
291        struct llist_head list;
292
293        struct gsm_bts *bts;
294        /* number of this TRX in the BTS */
295        u_int8_t nr;
296        /* how do we talk RSL with this TRX? */
297        struct gsm_e1_subslot rsl_e1_link;
298        u_int8_t rsl_tei;
299        struct e1inp_sign_link *rsl_link;
300
301        struct gsm_nm_state nm_state;
302        struct tlv_parsed nm_attr;
303        struct {
304                struct gsm_nm_state nm_state;
305        } bb_transc;
306
307        u_int16_t arfcn;
308        int nominal_power;              /* in dBm */
309        unsigned int max_power_red;     /* in actual dB */
310
311        union {
312                struct {
313                        struct {
314                                struct gsm_nm_state nm_state;
315                        } bbsig;
316                        struct {
317                                struct gsm_nm_state nm_state;
318                        } pa;
319                } bs11;
320        };
321        struct gsm_bts_trx_ts ts[TRX_NR_TS];
322};
323
324enum gsm_bts_type {
325        GSM_BTS_TYPE_UNKNOWN,
326        GSM_BTS_TYPE_BS11,
327        GSM_BTS_TYPE_NANOBTS,
328};
329
330struct gsm_bts_model {
331        struct llist_head list;
332
333        enum gsm_bts_type type;
334        const char *name;
335
336        struct tlv_definition nm_att_tlvdef;
337};
338
339/**
340 * A pending paging request
341 */
342struct gsm_paging_request {
343        /* list_head for list of all paging requests */
344        struct llist_head entry;
345        /* the subscriber which we're paging. Later gsm_paging_request
346         * should probably become a part of the gsm_subscriber struct? */
347        struct gsm_subscriber *subscr;
348        /* back-pointer to the BTS on which we are paging */
349        struct gsm_bts *bts;
350        /* what kind of channel type do we ask the MS to establish */
351        int chan_type;
352
353        /* Timer 3113: how long do we try to page? */
354        struct timer_list T3113;
355
356        /* callback to be called in case paging completes */
357        gsm_cbfn *cbfn;
358        void *cbfn_param;
359};
360
361/*
362 * This keeps track of the paging status of one BTS. It
363 * includes a number of pending requests, a back pointer
364 * to the gsm_bts, a timer and some more state.
365 */
366struct gsm_bts_paging_state {
367        /* pending requests */
368        struct llist_head pending_requests;
369        struct gsm_paging_request *last_request;
370        struct gsm_bts *bts;
371
372        struct timer_list work_timer;
373
374        /* load */
375        u_int16_t available_slots;
376};
377
378struct gsm_envabtse {
379        struct gsm_nm_state nm_state;
380};
381
382struct gsm_bts_gprs_nsvc {
383        struct gsm_bts *bts;
384        int id;
385        struct gsm_nm_state nm_state;
386};
387
388/* One BTS */
389struct gsm_bts {
390        /* list header in net->bts_list */
391        struct llist_head list;
392
393        struct gsm_network *network;
394        /* number of ths BTS in network */
395        u_int8_t nr;
396        /* Cell Identity */
397        u_int16_t cell_identity;
398        /* location area code of this BTS */
399        u_int16_t location_area_code;
400        /* Training Sequence Code */
401        u_int8_t tsc;
402        /* Base Station Identification Code (BSIC) */
403        u_int8_t bsic;
404        /* type of BTS */
405        enum gsm_bts_type type;
406        struct gsm_bts_model *model;
407        enum gsm_band band;
408        /* should the channel allocator allocate channels from high TRX to TRX0,
409         * rather than starting from TRX0 and go upwards? */
410        int chan_alloc_reverse;
411        /* maximum Tx power that the MS is permitted to use in this cell */
412        int ms_max_power;
413
414        /* how do we talk OML with this TRX? */
415        struct gsm_e1_subslot oml_e1_link;
416        u_int8_t oml_tei;
417        struct e1inp_sign_link *oml_link;
418
419        /* Abis network management O&M handle */
420        struct abis_nm_h *nmh;
421        struct gsm_nm_state nm_state;
422        struct tlv_parsed nm_attr;
423
424        /* number of this BTS on given E1 link */
425        u_int8_t bts_nr;
426
427        /* paging state and control */
428        struct gsm_bts_paging_state paging;
429
430        /* CCCH is on C0 */
431        struct gsm_bts_trx *c0;
432
433        struct {
434                struct gsm_nm_state nm_state;
435        } site_mgr;
436
437        /* parameters from which we build SYSTEM INFORMATION */
438        struct {
439                struct gsm48_rach_control rach_control;
440                u_int8_t ncc_permitted;
441                struct gsm48_cell_sel_par cell_sel_par;
442                struct gsm48_cell_options cell_options;
443                struct gsm48_control_channel_descr chan_desc;
444                struct bitvec neigh_list;
445                struct bitvec cell_alloc;
446                struct {
447                        /* bitmask large enough for all possible ARFCN's */
448                        u_int8_t neigh_list[1024/8];
449                        u_int8_t cell_alloc[1024/8];
450                } data;
451        } si_common;
452
453        /* ip.accesss Unit ID's have Site/BTS/TRX layout */
454        union {
455                struct {
456                        u_int16_t site_id;
457                        u_int16_t bts_id;
458                        u_int32_t flags;
459                } ip_access;
460                struct {
461                        struct {
462                                struct gsm_nm_state nm_state;
463                        } cclk;
464                        struct {
465                                struct gsm_nm_state nm_state;
466                        } rack;
467                        struct gsm_envabtse envabtse[4];
468                } bs11;
469        };
470
471        /* Not entirely sure how ip.access specific this is */
472        struct {
473                struct {
474                        struct gsm_nm_state nm_state;
475                } nse;
476                struct {
477                        struct gsm_nm_state nm_state;
478                } cell;
479                struct gsm_bts_gprs_nsvc nsvc[2];
480        } gprs;
481       
482        /* transceivers */
483        int num_trx;
484        struct llist_head trx_list;
485};
486
487/* Some statistics of our network */
488struct gsmnet_stats {
489        struct {
490                struct counter *total;
491                struct counter *no_channel;
492        } chreq;
493        struct {
494                struct counter *attempted;
495                struct counter *no_channel;     /* no channel available */
496                struct counter *timeout;                /* T3103 timeout */
497                struct counter *completed;      /* HO COMPL received */
498                struct counter *failed;         /* HO FAIL received */
499        } handover;
500        struct {
501                struct counter *attach;
502                struct counter *normal;
503                struct counter *periodic;
504                struct counter *detach;
505        } loc_upd_type;
506        struct {
507                struct counter *reject;
508                struct counter *accept;
509        } loc_upd_resp;
510        struct {
511                struct counter *attempted;
512                struct counter *detached;
513                struct counter *completed;
514                struct counter *expired;
515        } paging;
516        struct {
517                struct counter *submitted; /* MO SMS submissions */
518                struct counter *no_receiver;
519                struct counter *delivered; /* MT SMS deliveries */
520                struct counter *rp_err_mem;
521                struct counter *rp_err_other;
522        } sms;
523        struct {
524                struct counter *dialled;        /* total number of dialled calls */
525                struct counter *alerted;        /* we alerted the other end */
526                struct counter *connected;/* how many calls were accepted */
527        } call;
528};
529
530enum gsm_auth_policy {
531        GSM_AUTH_POLICY_CLOSED, /* only subscribers authorized in DB */
532        GSM_AUTH_POLICY_ACCEPT_ALL, /* accept everyone, even if not authorized in DB */
533        GSM_AUTH_POLICY_TOKEN, /* accept first, send token per sms, then revoke authorization */
534};
535
536#define GSM_T3101_DEFAULT 10
537#define GSM_T3113_DEFAULT 60
538
539struct gsm_network {
540        /* global parameters */
541        u_int16_t country_code;
542        u_int16_t network_code;
543        char *name_long;
544        char *name_short;
545        enum gsm_auth_policy auth_policy;
546        enum gsm48_reject_value reject_cause;
547        int a5_encryption;
548        int neci;
549        int send_mm_info;
550        struct {
551                int active;
552                /* Window RXLEV averaging */
553                unsigned int win_rxlev_avg;     /* number of SACCH frames */
554                /* Window RXQUAL averaging */
555                unsigned int win_rxqual_avg;    /* number of SACCH frames */
556                /* Window RXLEV neighbouring cells averaging */
557                unsigned int win_rxlev_avg_neigh; /* number of SACCH frames */
558
559                /* how often should we check for power budget HO */
560                unsigned int pwr_interval;      /* SACCH frames */
561                /* how much better does a neighbor cell have to be ? */
562                unsigned int pwr_hysteresis;    /* dBm */
563                /* maximum distacne before we try a handover */
564                unsigned int max_distance;      /* TA values */
565        } handover;
566
567        struct gsmnet_stats stats;
568
569        /* layer 4 */
570        int (*mncc_recv) (struct gsm_network *net, int msg_type, void *arg);
571        struct llist_head upqueue;
572        struct llist_head trans_list;
573
574        unsigned int num_bts;
575        struct llist_head bts_list;
576
577        /* timer values */
578        int T3101;
579        int T3103;
580        int T3105;
581        int T3107;
582        int T3109;
583        int T3111;
584        int T3113;
585        int T3115;
586        int T3117;
587        int T3119;
588        int T3141;
589
590        /* Radio Resource Location Protocol (TS 04.31) */
591        struct {
592                enum rrlp_mode mode;
593        } rrlp;
594};
595
596#define SMS_HDR_SIZE    128
597#define SMS_TEXT_SIZE   256
598struct gsm_sms {
599        unsigned long long id;
600        struct gsm_subscriber *sender;
601        struct gsm_subscriber *receiver;
602
603        unsigned long validity_minutes;
604        u_int8_t reply_path_req;
605        u_int8_t status_rep_req;
606        u_int8_t ud_hdr_ind;
607        u_int8_t protocol_id;
608        u_int8_t data_coding_scheme;
609        u_int8_t msg_ref;
610        char dest_addr[20+1];   /* DA LV is 12 bytes max, i.e. 10 bytes
611                                 * BCD == 20 bytes string */
612        u_int8_t user_data_len;
613        u_int8_t user_data[SMS_TEXT_SIZE];
614
615        char text[SMS_TEXT_SIZE];
616};
617
618
619struct gsm_network *gsm_network_init(u_int16_t country_code, u_int16_t network_code,
620                                     int (*mncc_recv)(struct gsm_network *, int, void *));
621struct gsm_bts *gsm_bts_alloc(struct gsm_network *net, enum gsm_bts_type type,
622                              u_int8_t tsc, u_int8_t bsic);
623struct gsm_bts_trx *gsm_bts_trx_alloc(struct gsm_bts *bts);
624int gsm_set_bts_type(struct gsm_bts *bts, enum gsm_bts_type type);
625
626struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num);
627
628/* Get reference to a neighbor cell on a given BCCH ARFCN */
629struct gsm_bts *gsm_bts_neighbor(const struct gsm_bts *bts,
630                                 u_int16_t arfcn, u_int8_t bsic);
631
632struct gsm_bts_trx *gsm_bts_trx_num(struct gsm_bts *bts, int num);
633
634const char *gsm_pchan_name(enum gsm_phys_chan_config c);
635enum gsm_phys_chan_config gsm_pchan_parse(const char *name);
636const char *gsm_lchant_name(enum gsm_chan_t c);
637const char *gsm_chreq_name(enum gsm_chreq_reason_t c);
638char *gsm_trx_name(struct gsm_bts_trx *trx);
639char *gsm_ts_name(struct gsm_bts_trx_ts *ts);
640char *gsm_lchan_name(struct gsm_lchan *lchan);
641const char *gsm_lchans_name(enum gsm_lchan_state s);
642
643enum gsm_e1_event {
644        EVT_E1_NONE,
645        EVT_E1_TEI_UP,
646        EVT_E1_TEI_DN,
647};
648
649void set_ts_e1link(struct gsm_bts_trx_ts *ts, u_int8_t e1_nr,
650                   u_int8_t e1_ts, u_int8_t e1_ts_ss);
651enum gsm_bts_type parse_btstype(const char *arg);
652const char *btstype2str(enum gsm_bts_type type);
653struct gsm_bts_trx *gsm_bts_trx_by_nr(struct gsm_bts *bts, int nr);
654struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
655                                struct gsm_bts *start_bts);
656
657char *gsm_band_name(enum gsm_band band);
658enum gsm_band gsm_band_parse(const char *mhz);
659
660extern void *tall_bsc_ctx;
661extern int ipacc_rtp_direct;
662
663static inline int is_ipaccess_bts(struct gsm_bts *bts)
664{
665        switch (bts->type) {
666        case GSM_BTS_TYPE_NANOBTS:
667                return 1;
668        default:
669                break;
670        }
671        return 0;
672}
673
674static inline int is_siemens_bts(struct gsm_bts *bts)
675{
676        switch (bts->type) {
677        case GSM_BTS_TYPE_BS11:
678                return 1;
679        default:
680                break;
681        }
682
683        return 0;
684}
685
686
687enum gsm_auth_policy gsm_auth_policy_parse(const char *arg);
688const char *gsm_auth_policy_name(enum gsm_auth_policy policy);
689
690enum rrlp_mode rrlp_mode_parse(const char *arg);
691const char *rrlp_mode_name(enum rrlp_mode mode);
692
693void gsm_trx_lock_rf(struct gsm_bts_trx *trx, int locked);
694
695struct gsm_meas_rep *lchan_next_meas_rep(struct gsm_lchan *lchan);
696
697#endif
Note: See TracBrowser for help on using the browser.