blob: 694d11a2d2cf66b85880d917b18a0b6366e32a02 [file] [log] [blame]
Harald Welte96f71f22010-05-03 19:28:05 +02001/* GPRS SNDCP protocol implementation as per 3GPP TS 04.65 */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <errno.h>
24#include <stdint.h>
25
26#include <osmocore/msgb.h>
27#include <osmocore/linuxlist.h>
28#include <osmocore/timer.h>
29#include <osmocore/talloc.h>
30
31#include <openbsc/gsm_data.h>
32#include <openbsc/debug.h>
33#include <openbsc/gprs_bssgp.h>
34#include <openbsc/gprs_llc.h>
Harald Welteebabdea2010-06-01 18:28:10 +020035#include <openbsc/sgsn.h>
Harald Welte96f71f22010-05-03 19:28:05 +020036
37/* Chapter 7.2: SN-PDU Formats */
38struct sndcp_common_hdr {
39 /* octet 1 */
40 uint8_t nsapi:4;
41 uint8_t more:1;
42 uint8_t type:1;
43 uint8_t first:1;
44 uint8_t spare:1;
45 /* octet 2 */
46 uint8_t pcomp;
47 uint8_t dcomp;
Harald Welteebabdea2010-06-01 18:28:10 +020048} __attribute__((packed));
Harald Welte96f71f22010-05-03 19:28:05 +020049
50struct sndcp_udata_hdr {
51 /* octet 3 */
52 uint8_t npdu_high:4;
53 uint8_t seg_nr:4;
54 /* octet 4 */
55 uint8_t npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +020056} __attribute__((packed));
57
58/* See 6.7.1.2 Reassembly */
59enum sndcp_rx_state {
60 SNDCP_RX_S_FIRST,
61 SNDCP_RX_S_SUBSEQ,
62 SNDCP_RX_S_DISCARD,
63};
64
65
66static void *tall_sndcp_ctx;
67
68/* A fragment queue entry, containing one framgent of a N-PDU */
69struct frag_queue_entry {
70 struct llist_head list;
71 uint8_t seg_nr;
72 uint32_t data_len;
73 uint8_t data[0];
74};
75
76/* A fragment queue header, maintaining list of fragments for one N-PDU */
77struct frag_queue_head {
78 uint16_t npdu;
79
80 /* linked list of frag_queue_entry: one for each fragment */
81 struct llist_head frag_list;
82
83 struct timer_list timer;
Harald Welte96f71f22010-05-03 19:28:05 +020084};
85
Harald Welte2720e732010-05-17 00:44:57 +020086struct sndcp_entity {
Harald Welteebabdea2010-06-01 18:28:10 +020087 struct llist_head list;
88
89 struct gprs_llc_lle *lle;
90 uint8_t nsapi;
91
92 enum sndcp_rx_state rx_state;
93 struct frag_queue_head fqueue;
Harald Welte2720e732010-05-17 00:44:57 +020094};
95
Harald Welteebabdea2010-06-01 18:28:10 +020096LLIST_HEAD(sndcp_entities);
Harald Welte96f71f22010-05-03 19:28:05 +020097
Harald Welteebabdea2010-06-01 18:28:10 +020098#if 0
99static struct frag_queue_entry _find_fqe(struct freg_queue_head *fqh, uint8_t seg_nr)
100{
101
102}
103
104static struct frag_queue_head _find_fqh(struct sndcp_entity *sne, uint16_t npdu)
105{
106
107}
108
109static int ul_enqueue_fragment(struct sndcp_entity *sne, uint16_t npdu,
110 uint8_t seg_nr, uint32_t data_len, uint8_t *data)
111{
112
113}
114#endif
115
116static struct sndcp_entity *sndcp_entity_by_lle(const struct gprs_llc_lle *lle,
117 uint8_t nsapi)
118{
119 struct sndcp_entity *sne;
120
121 llist_for_each_entry(sne, &sndcp_entities, list) {
122 if (sne->lle == lle && sne->nsapi == nsapi)
123 return sne;
124 }
125 return NULL;
126}
127
128static struct sndcp_entity *sndcp_entity_alloc(struct gprs_llc_lle *lle,
129 uint8_t nsapi)
130{
131 struct sndcp_entity *sne;
132
133 sne = talloc_zero(tall_sndcp_ctx, struct sndcp_entity);
134 if (!sne)
135 return NULL;
136
137 sne->lle = lle;
138 sne->nsapi = nsapi;
139 sne->fqueue.timer.data = sne;
140 //sne->fqueue.timer.cb = FIXME;
141 sne->rx_state = SNDCP_RX_S_FIRST;
142
143 return sne;
144}
145
146/* Entry point for the SNSM-ACTIVATE.indication */
147int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi)
148{
Harald Welte16836a32010-06-02 10:25:40 +0200149 LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (TLLI=%08x, NSAPI=%u)\n",
150 lle->llme->tlli, nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200151
Harald Welte16836a32010-06-02 10:25:40 +0200152 if (sndcp_entity_by_lle(lle, nsapi)) {
153 LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE "
154 "already-existing entity (TLLI=%08x, NSAPI=%u)\n",
155 lle->llme->tlli, nsapi);
156 return -EEXIST;
157 }
158
159 if (!sndcp_entity_alloc(lle, nsapi)) {
160 LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n");
Harald Welteebabdea2010-06-01 18:28:10 +0200161 return -ENOMEM;
Harald Welte16836a32010-06-02 10:25:40 +0200162 }
Harald Welteebabdea2010-06-01 18:28:10 +0200163
164 return 0;
165}
166
167/* Section 5.1.2.17 LL-UNITDATA.ind */
168int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t *hdr, uint8_t len)
169{
170 struct sndcp_entity *sne;
171 struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr;
172 struct sndcp_udata_hdr *suh;
Harald Welte16836a32010-06-02 10:25:40 +0200173 uint8_t *npdu;
Harald Welteebabdea2010-06-01 18:28:10 +0200174 uint16_t npdu_num;
175 int npdu_len;
176
177 if (sch->type == 0) {
Harald Welte96f71f22010-05-03 19:28:05 +0200178 LOGP(DGPRS, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n");
179 return -EINVAL;
180 }
181
Harald Welte16836a32010-06-02 10:25:40 +0200182 if (len < sizeof(*sch) + sizeof(*suh)) {
Harald Welteebabdea2010-06-01 18:28:10 +0200183 LOGP(DGPRS, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len);
184 return -EIO;
185 }
186
187 sne = sndcp_entity_by_lle(lle, sch->nsapi);
188 if (!sne) {
Harald Welte16836a32010-06-02 10:25:40 +0200189 LOGP(DGPRS, LOGL_ERROR, "Message for non-existing SNDCP Entity "
190 "(TLLI=%08x, NSAPI=%u)\n", lle->llme->tlli, sch->nsapi);
Harald Welteebabdea2010-06-01 18:28:10 +0200191 return -EIO;
192 }
193
194 if (!sch->first || sch->more) {
195 /* FIXME: implement fragment re-assembly */
196 LOGP(DGPRS, LOGL_ERROR, "We don't support reassembly yet\n");
197 return -EIO;
198 }
199
Harald Welte16836a32010-06-02 10:25:40 +0200200 if (sch->pcomp || sch->dcomp) {
Harald Welteebabdea2010-06-01 18:28:10 +0200201 LOGP(DGPRS, LOGL_ERROR, "We don't support compression yet\n");
202 return -EIO;
203 }
Harald Welteebabdea2010-06-01 18:28:10 +0200204
Harald Welte16836a32010-06-02 10:25:40 +0200205 suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr));
206 npdu_num = (suh->npdu_high << 8) | suh->npdu_low;
Harald Welteebabdea2010-06-01 18:28:10 +0200207 npdu = (uint8_t *)suh + sizeof(*suh);
208 npdu_len = (msg->data + msg->len) - npdu;
209 if (npdu_len) {
210 LOGP(DGPRS, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len);
211 return -EIO;
212 }
213 /* actually send the N-PDU to the SGSN core code, which then
214 * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */
215 return sgsn_rx_sndcp_ud_ind(lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu);
Harald Welte96f71f22010-05-03 19:28:05 +0200216}
217
Harald Welte2720e732010-05-17 00:44:57 +0200218/* Section 5.1.2.1 LL-RESET.ind */
Harald Welteebabdea2010-06-01 18:28:10 +0200219static int sndcp_ll_reset_ind(struct sndcp_entity *se)
Harald Welte2720e732010-05-17 00:44:57 +0200220{
221 /* treat all outstanding SNDCP-LLC request type primitives as not sent */
222 /* reset all SNDCP XID parameters to default values */
223}
224
Harald Welte2720e732010-05-17 00:44:57 +0200225static int sndcp_ll_status_ind()
226{
227 /* inform the SM sub-layer by means of SNSM-STATUS.req */
228}
229
Harald Welteebabdea2010-06-01 18:28:10 +0200230#if 0
Harald Welte2720e732010-05-17 00:44:57 +0200231static struct sndcp_state_list {{
232 uint32_t states;
233 unsigned int type;
234 int (*rout)(struct sndcp_entity *se, struct msgb *msg);
235} sndcp_state_list[] = {
236 { ALL_STATES,
237 LL_RESET_IND, sndcp_ll_reset_ind },
238 { ALL_STATES,
239 LL_ESTABLISH_IND, sndcp_ll_est_ind },
240 { SBIT(SNDCP_S_EST_RQD),
241 LL_ESTABLISH_RESP, sndcp_ll_est_ind },
242 { SBIT(SNDCP_S_EST_RQD),
243 LL_ESTABLISH_CONF, sndcp_ll_est_conf },
244 { SBIT(SNDCP_S_
245};
246
247static int sndcp_rx_llc_prim()
248{
249 case LL_ESTABLISH_REQ:
250 case LL_RELEASE_REQ:
251 case LL_XID_REQ:
252 case LL_DATA_REQ:
253 LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */
254
255 switch (prim) {
256 case LL_RESET_IND:
257 case LL_ESTABLISH_IND:
258 case LL_ESTABLISH_RESP:
259 case LL_ESTABLISH_CONF:
260 case LL_RELEASE_IND:
261 case LL_RELEASE_CONF:
262 case LL_XID_IND:
263 case LL_XID_RESP:
264 case LL_XID_CONF:
265 case LL_DATA_IND:
266 case LL_DATA_CONF:
267 case LL_UNITDATA_IND:
268 case LL_STATUS_IND:
269}
Harald Welteebabdea2010-06-01 18:28:10 +0200270#endif