Merge commit '4d3a7b124e08a597d5f01fb2a71f3a4677a360a9'
[osmocom-bb.git] / src / host / layer23 / src / misc / app_cbch_sniff.c
1 /* CBCH passive sniffer */
2
3 /* (C) 2010 by Holger Hans Peter Freyther
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  * (C) 2010 by Alex Badea <vamposdecampos@gmail.com>
6  *
7  * All Rights Reserved
8  *
9  * This program 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  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  */
24
25 #include <osmocom/bb/common/osmocom_data.h>
26 #include <osmocom/bb/common/l1ctl.h>
27 #include <osmocom/bb/common/lapdm.h>
28 #include <osmocom/bb/common/logging.h>
29 #include <osmocom/bb/common/l23_app.h>
30 #include <osmocom/bb/misc/layer3.h>
31
32 #include <osmocom/core/msgb.h>
33 #include <osmocom/core/talloc.h>
34 #include <osmocom/core/select.h>
35 #include <osmocom/core/signal.h>
36 #include <osmocom/gsm/rsl.h>
37
38 struct osmocom_ms *g_ms;
39 struct gsm48_sysinfo g_sysinfo = {};
40
41 static int try_cbch(struct osmocom_ms *ms, struct gsm48_sysinfo *s)
42 {
43         if (!s->si1 || !s->si4)
44                 return 0;
45         if (!s->chan_nr) {
46                 LOGP(DRR, LOGL_INFO, "no CBCH chan_nr found\n");
47                 return 0;
48         }
49
50         if (s->h) {
51                 LOGP(DRR, LOGL_INFO, "chan_nr = 0x%02x TSC = %d  MAIO = %d  "
52                         "HSN = %d  hseq (%d): %s\n",
53                         s->chan_nr, s->tsc, s->maio, s->hsn,
54                         s->hopp_len,
55                         osmo_hexdump((unsigned char *) s->hopping, s->hopp_len * 2));
56                 return l1ctl_tx_dm_est_req_h1(ms,
57                         s->maio, s->hsn, s->hopping, s->hopp_len,
58                         s->chan_nr, s->tsc,
59                         GSM48_CMODE_SIGN);
60         } else {
61                 LOGP(DRR, LOGL_INFO, "chan_nr = 0x%02x TSC = %d  ARFCN = %d\n",
62                         s->chan_nr, s->tsc, s->arfcn);
63                 return l1ctl_tx_dm_est_req_h0(ms, s->arfcn,
64                         s->chan_nr, s->tsc, GSM48_CMODE_SIGN);
65         }
66 }
67
68
69 /* receive BCCH at RR layer */
70 static int bcch(struct osmocom_ms *ms, struct msgb *msg)
71 {
72         struct gsm48_system_information_type_header *sih = msgb_l3(msg);
73         struct gsm48_sysinfo *s = &g_sysinfo;
74
75         if (msgb_l3len(msg) != 23) {
76                 LOGP(DRR, LOGL_NOTICE, "Invalid BCCH message length\n");
77                 return -EINVAL;
78         }
79         switch (sih->system_information) {
80         case GSM48_MT_RR_SYSINFO_1:
81                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 1\n");
82                 gsm48_decode_sysinfo1(s,
83                         (struct gsm48_system_information_type_1 *) sih,
84                         msgb_l3len(msg));
85                 return try_cbch(ms, s);
86         case GSM48_MT_RR_SYSINFO_4:
87                 LOGP(DRR, LOGL_INFO, "New SYSTEM INFORMATION 4\n");
88                 gsm48_decode_sysinfo4(s,
89                         (struct gsm48_system_information_type_4 *) sih,
90                         msgb_l3len(msg));
91                 return try_cbch(ms, s);
92         default:
93                 return 0;
94         }
95 }
96
97 static int unit_data_ind(struct osmocom_ms *ms, struct msgb *msg)
98 {
99         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
100         struct tlv_parsed tv;
101         uint8_t ch_type, ch_subch, ch_ts;
102
103         DEBUGP(DRSL, "RSLms UNIT DATA IND chan_nr=0x%02x link_id=0x%02x\n",
104                 rllh->chan_nr, rllh->link_id);
105
106         rsl_tlv_parse(&tv, rllh->data, msgb_l2len(msg)-sizeof(*rllh));
107         if (!TLVP_PRESENT(&tv, RSL_IE_L3_INFO)) {
108                 DEBUGP(DRSL, "UNIT_DATA_IND without L3 INFO ?!?\n");
109                 return -EIO;
110         }
111         msg->l3h = (uint8_t *) TLVP_VAL(&tv, RSL_IE_L3_INFO);
112
113         rsl_dec_chan_nr(rllh->chan_nr, &ch_type, &ch_subch, &ch_ts);
114         switch (ch_type) {
115         case RSL_CHAN_BCCH:
116                 return bcch(ms, msg);
117         default:
118                 return 0;
119         }
120 }
121
122 static int rcv_rll(struct osmocom_ms *ms, struct msgb *msg)
123 {
124         struct abis_rsl_rll_hdr *rllh = msgb_l2(msg);
125         int msg_type = rllh->c.msg_type;
126
127         if (msg_type == RSL_MT_UNIT_DATA_IND) {
128                 unit_data_ind(ms, msg);
129         } else
130                 LOGP(DRSL, LOGL_NOTICE, "RSLms message unhandled\n");
131
132         msgb_free(msg);
133
134         return 0;
135 }
136
137 static int rcv_rsl(struct msgb *msg, struct osmocom_ms *ms)
138 {
139         struct abis_rsl_common_hdr *rslh = msgb_l2(msg);
140         int rc = 0;
141
142         switch (rslh->msg_discr & 0xfe) {
143         case ABIS_RSL_MDISC_RLL:
144                 rc = rcv_rll(ms, msg);
145                 break;
146         default:
147                 LOGP(DRSL, LOGL_NOTICE, "unknown RSLms msg_discr 0x%02x\n",
148                         rslh->msg_discr);
149                 msgb_free(msg);
150                 rc = -EINVAL;
151                 break;
152         }
153
154         return rc;
155 }
156
157 static int signal_cb(unsigned int subsys, unsigned int signal,
158                      void *handler_data, void *signal_data)
159 {
160         struct osmocom_ms *ms;
161
162         if (subsys != SS_L1CTL)
163                 return 0;
164
165         switch (signal) {
166         case S_L1CTL_RESET:
167         case S_L1CTL_FBSB_ERR:
168                 ms = g_ms;
169                 return l1ctl_tx_fbsb_req(ms, ms->test_arfcn,
170                         L1CTL_FBSB_F_FB01SB, 100, 0, CCCH_MODE_COMBINED);
171         case S_L1CTL_FBSB_RESP:
172                 return 0;
173         }
174         return 0;
175 }
176
177 int l23_app_init(struct osmocom_ms *ms)
178 {
179         /* don't do layer3_init() as we don't want an actualy L3 */
180
181         g_ms = ms;
182         osmol2_register_handler(ms, &rcv_rsl);
183
184         l1ctl_tx_reset_req(ms, L1CTL_RES_T_FULL);
185         /* FIXME: L1CTL_RES_T_FULL doesn't reset dedicated mode
186          * (if previously set), so we release it here. */
187         l1ctl_tx_dm_rel_req(ms);
188         return osmo_signal_register_handler(SS_L1CTL, &signal_cb, NULL);
189 }
190
191 static struct l23_app_info info = {
192         .copyright      = "Copyright (C) 2010 Harald Welte <laforge@gnumonks.org>\n",
193         .contribution   = "Contributions by Holger Hans Peter Freyther\n",
194 };
195
196 struct l23_app_info *l23_app_info()
197 {
198         return &info;
199 }