Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 1 | /* GPRS SNDCP protocol implementation as per 3GPP TS 04.65 */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #include <osmocore/msgb.h> |
| 28 | #include <osmocore/linuxlist.h> |
| 29 | #include <osmocore/timer.h> |
| 30 | #include <osmocore/talloc.h> |
| 31 | |
| 32 | #include <openbsc/gsm_data.h> |
| 33 | #include <openbsc/debug.h> |
| 34 | #include <openbsc/gprs_bssgp.h> |
| 35 | #include <openbsc/gprs_llc.h> |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 36 | #include <openbsc/sgsn.h> |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 37 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 38 | #include "gprs_sndcp.h" |
| 39 | |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 40 | /* Chapter 7.2: SN-PDU Formats */ |
| 41 | struct sndcp_common_hdr { |
| 42 | /* octet 1 */ |
| 43 | uint8_t nsapi:4; |
| 44 | uint8_t more:1; |
| 45 | uint8_t type:1; |
| 46 | uint8_t first:1; |
| 47 | uint8_t spare:1; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 48 | } __attribute__((packed)); |
| 49 | |
| 50 | /* PCOMP / DCOMP only exist in first fragment */ |
| 51 | struct sndcp_comp_hdr { |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 52 | /* octet 2 */ |
Harald Welte | 5cc2bc3 | 2010-06-02 23:17:05 +0200 | [diff] [blame] | 53 | uint8_t pcomp:4; |
| 54 | uint8_t dcomp:4; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 55 | } __attribute__((packed)); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 56 | |
| 57 | struct sndcp_udata_hdr { |
| 58 | /* octet 3 */ |
| 59 | uint8_t npdu_high:4; |
| 60 | uint8_t seg_nr:4; |
| 61 | /* octet 4 */ |
| 62 | uint8_t npdu_low; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 63 | } __attribute__((packed)); |
| 64 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 65 | |
| 66 | static void *tall_sndcp_ctx; |
| 67 | |
| 68 | /* A fragment queue entry, containing one framgent of a N-PDU */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 69 | struct defrag_queue_entry { |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 70 | struct llist_head list; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 71 | /* segment number of this fragment */ |
| 72 | uint32_t seg_nr; |
| 73 | /* length of the data area of this fragment */ |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 74 | uint32_t data_len; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 75 | /* pointer to the data of this fragment */ |
| 76 | uint8_t *data; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 79 | LLIST_HEAD(gprs_sndcp_entities); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 80 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 81 | /* Enqueue a fragment into the defragment queue */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 82 | static int defrag_enqueue(struct gprs_sndcp_entity *sne, uint8_t seg_nr, |
Harald Welte | 3d6815a | 2010-07-02 17:16:07 +0200 | [diff] [blame] | 83 | uint8_t *data, uint32_t data_len) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 84 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 85 | struct defrag_queue_entry *dqe; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 86 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 87 | dqe = talloc_zero(tall_sndcp_ctx, struct defrag_queue_entry); |
| 88 | if (!dqe) |
| 89 | return -ENOMEM; |
| 90 | dqe->data = talloc_zero_size(dqe, data_len); |
| 91 | if (!dqe->data) { |
| 92 | talloc_free(dqe); |
| 93 | return -ENOMEM; |
| 94 | } |
| 95 | dqe->seg_nr = seg_nr; |
| 96 | dqe->data_len = data_len; |
| 97 | |
| 98 | llist_add(&dqe->list, &sne->defrag.frag_list); |
| 99 | |
| 100 | if (seg_nr > sne->defrag.highest_seg) |
| 101 | sne->defrag.highest_seg = seg_nr; |
| 102 | |
| 103 | sne->defrag.seg_have |= (1 << seg_nr); |
| 104 | sne->defrag.tot_len += data_len; |
| 105 | |
Harald Welte | 8f0c0a3 | 2010-07-02 10:29:06 +0200 | [diff] [blame] | 106 | memcpy(dqe->data, data, data_len); |
| 107 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 108 | return 0; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 111 | /* return if we have all segments of this N-PDU */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 112 | static int defrag_have_all_segments(struct gprs_sndcp_entity *sne) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 113 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 114 | uint32_t seg_needed = 0; |
| 115 | unsigned int i; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 116 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 117 | /* create a bitmask of needed segments */ |
Harald Welte | 951a12c | 2010-07-01 15:09:45 +0200 | [diff] [blame] | 118 | for (i = 0; i <= sne->defrag.highest_seg; i++) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 119 | seg_needed |= (1 << i); |
| 120 | |
| 121 | if (seg_needed == sne->defrag.seg_have) |
| 122 | return 1; |
| 123 | |
| 124 | return 0; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 127 | static struct defrag_queue_entry *defrag_get_seg(struct gprs_sndcp_entity *sne, |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 128 | uint32_t seg_nr) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 129 | { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 130 | struct defrag_queue_entry *dqe; |
| 131 | |
| 132 | llist_for_each_entry(dqe, &sne->defrag.frag_list, list) { |
| 133 | if (dqe->seg_nr == seg_nr) { |
| 134 | llist_del(&dqe->list); |
| 135 | return dqe; |
| 136 | } |
| 137 | } |
| 138 | return NULL; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 139 | } |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 140 | |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 141 | /* Perform actual defragmentation and create an output packet */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 142 | static int defrag_segments(struct gprs_sndcp_entity *sne) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 143 | { |
| 144 | struct msgb *msg; |
| 145 | unsigned int seg_nr; |
| 146 | uint8_t *npdu; |
| 147 | |
Harald Welte | ab4094c | 2010-07-02 16:01:47 +0200 | [diff] [blame] | 148 | LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Defragment output PDU %u " |
| 149 | "num_seg=%u tot_len=%u\n", sne->lle->llme->tlli, sne->nsapi, |
| 150 | sne->defrag.npdu, sne->defrag.highest_seg, sne->defrag.tot_len); |
Sylvain Munaut | eda125c | 2010-06-09 20:56:52 +0200 | [diff] [blame] | 151 | msg = msgb_alloc_headroom(sne->defrag.tot_len+256, 128, "SNDCP Defrag"); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 152 | if (!msg) |
| 153 | return -ENOMEM; |
| 154 | |
| 155 | /* FIXME: message headers + identifiers */ |
| 156 | |
| 157 | npdu = msg->data; |
| 158 | |
Harald Welte | 993697c | 2010-07-02 10:11:42 +0200 | [diff] [blame] | 159 | for (seg_nr = 0; seg_nr <= sne->defrag.highest_seg; seg_nr++) { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 160 | struct defrag_queue_entry *dqe; |
| 161 | uint8_t *data; |
| 162 | |
| 163 | dqe = defrag_get_seg(sne, seg_nr); |
| 164 | if (!dqe) { |
| 165 | LOGP(DSNDCP, LOGL_ERROR, "Segment %u missing\n", seg_nr); |
| 166 | talloc_free(msg); |
| 167 | return -EIO; |
| 168 | } |
| 169 | /* actually append the segment to the N-PDU */ |
| 170 | data = msgb_put(msg, dqe->data_len); |
| 171 | memcpy(data, dqe->data, dqe->data_len); |
| 172 | |
| 173 | /* release memory for the fragment queue entry */ |
| 174 | talloc_free(dqe); |
| 175 | } |
| 176 | |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 177 | /* FIXME: cancel timer */ |
| 178 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 179 | /* actually send the N-PDU to the SGSN core code, which then |
| 180 | * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */ |
Harald Welte | 8911cef | 2010-07-01 19:56:19 +0200 | [diff] [blame] | 181 | return sgsn_rx_sndcp_ud_ind(&sne->ra_id, sne->lle->llme->tlli, |
| 182 | sne->nsapi, msg, sne->defrag.tot_len, npdu); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Harald Welte | 60da7d4 | 2010-07-02 15:45:12 +0200 | [diff] [blame] | 185 | static int defrag_input(struct gprs_sndcp_entity *sne, struct msgb *msg, uint8_t *hdr, |
| 186 | unsigned int len) |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 187 | { |
| 188 | struct sndcp_common_hdr *sch; |
| 189 | struct sndcp_comp_hdr *scomph = NULL; |
| 190 | struct sndcp_udata_hdr *suh; |
| 191 | uint16_t npdu_num; |
| 192 | uint8_t *data; |
| 193 | int rc; |
| 194 | |
| 195 | sch = (struct sndcp_common_hdr *) hdr; |
| 196 | if (sch->first) { |
| 197 | scomph = (struct sndcp_comp_hdr *) (hdr + 1); |
| 198 | suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr)); |
| 199 | } else |
| 200 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 201 | |
| 202 | data = (uint8_t *)suh + sizeof(struct sndcp_udata_hdr); |
| 203 | |
| 204 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
| 205 | |
Harald Welte | ab4094c | 2010-07-02 16:01:47 +0200 | [diff] [blame] | 206 | LOGP(DSNDCP, LOGL_DEBUG, "TLLI=0x%08x NSAPI=%u: Input PDU %u Segment %u " |
| 207 | "Length %u %s %s\n", sne->lle->llme->tlli, sne->nsapi, npdu_num, |
| 208 | suh->seg_nr, len, sch->first ? "F " : "", sch->more ? "M" : ""); |
Harald Welte | b87bc86 | 2010-07-01 20:29:20 +0200 | [diff] [blame] | 209 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 210 | if (sch->first) { |
| 211 | /* first segment of a new packet. Discard all leftover fragments of |
| 212 | * previous packet */ |
| 213 | if (!llist_empty(&sne->defrag.frag_list)) { |
Harald Welte | 65d9678 | 2010-07-01 12:19:02 +0200 | [diff] [blame] | 214 | struct defrag_queue_entry *dqe, *dqe2; |
Harald Welte | b87bc86 | 2010-07-01 20:29:20 +0200 | [diff] [blame] | 215 | LOGP(DSNDCP, LOGL_INFO, "TLLI=0x%08x NSAPI=%u: Dropping " |
| 216 | "SN-PDU %u due to insufficient segments (%04x)\n", |
| 217 | sne->lle->llme->tlli, sne->nsapi, sne->defrag.npdu, |
| 218 | sne->defrag.seg_have); |
Harald Welte | 65d9678 | 2010-07-01 12:19:02 +0200 | [diff] [blame] | 219 | llist_for_each_entry_safe(dqe, dqe2, &sne->defrag.frag_list, list) { |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 220 | llist_del(&dqe->list); |
| 221 | talloc_free(dqe); |
| 222 | } |
| 223 | } |
| 224 | /* store the currently de-fragmented PDU number */ |
| 225 | sne->defrag.npdu = npdu_num; |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 226 | |
| 227 | /* Re-set fragmentation state */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 228 | sne->defrag.no_more = sne->defrag.highest_seg = sne->defrag.seg_have = 0; |
Harald Welte | 8b705f2 | 2010-07-02 16:18:59 +0200 | [diff] [blame] | 229 | sne->defrag.tot_len = 0; |
| 230 | /* FIXME: (re)start timer */ |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (sne->defrag.npdu != npdu_num) { |
| 234 | LOGP(DSNDCP, LOGL_INFO, "Segment for different SN-PDU " |
| 235 | "(%u != %u)\n", npdu_num, sne->defrag.npdu); |
| 236 | /* FIXME */ |
| 237 | } |
| 238 | |
| 239 | /* FIXME: check if seg_nr already exists */ |
Harald Welte | 3d6815a | 2010-07-02 17:16:07 +0200 | [diff] [blame] | 240 | /* make sure to subtract length of SNDCP header from 'len' */ |
| 241 | rc = defrag_enqueue(sne, suh->seg_nr, data, len - (data - hdr)); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 242 | if (rc < 0) |
| 243 | return rc; |
| 244 | |
| 245 | if (!sch->more) { |
| 246 | /* this is suppsed to be the last segment of the N-PDU, but it |
| 247 | * might well be not the last to arrive */ |
| 248 | sne->defrag.no_more = 1; |
| 249 | } |
| 250 | |
| 251 | if (sne->defrag.no_more) { |
| 252 | /* we have already received the last segment before, let's check |
| 253 | * if all the previous segments exist */ |
| 254 | if (defrag_have_all_segments(sne)) |
| 255 | return defrag_segments(sne); |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 260 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 261 | static struct gprs_sndcp_entity *gprs_sndcp_entity_by_lle(const struct gprs_llc_lle *lle, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 262 | uint8_t nsapi) |
| 263 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 264 | struct gprs_sndcp_entity *sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 265 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 266 | llist_for_each_entry(sne, &gprs_sndcp_entities, list) { |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 267 | if (sne->lle == lle && sne->nsapi == nsapi) |
| 268 | return sne; |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 273 | static struct gprs_sndcp_entity *gprs_sndcp_entity_alloc(struct gprs_llc_lle *lle, |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 274 | uint8_t nsapi) |
| 275 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 276 | struct gprs_sndcp_entity *sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 277 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 278 | sne = talloc_zero(tall_sndcp_ctx, struct gprs_sndcp_entity); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 279 | if (!sne) |
| 280 | return NULL; |
| 281 | |
| 282 | sne->lle = lle; |
| 283 | sne->nsapi = nsapi; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 284 | sne->defrag.timer.data = sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 285 | //sne->fqueue.timer.cb = FIXME; |
| 286 | sne->rx_state = SNDCP_RX_S_FIRST; |
Harald Welte | 362aea0 | 2010-07-01 12:31:10 +0200 | [diff] [blame] | 287 | INIT_LLIST_HEAD(&sne->defrag.frag_list); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 288 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 289 | llist_add(&sne->list, &gprs_sndcp_entities); |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 290 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 291 | return sne; |
| 292 | } |
| 293 | |
| 294 | /* Entry point for the SNSM-ACTIVATE.indication */ |
| 295 | int sndcp_sm_activate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 296 | { |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 297 | LOGP(DSNDCP, LOGL_INFO, "SNSM-ACTIVATE.ind (lle=%p TLLI=%08x, " |
| 298 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 299 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 300 | if (gprs_sndcp_entity_by_lle(lle, nsapi)) { |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 301 | LOGP(DSNDCP, LOGL_ERROR, "Trying to ACTIVATE " |
| 302 | "already-existing entity (TLLI=%08x, NSAPI=%u)\n", |
| 303 | lle->llme->tlli, nsapi); |
| 304 | return -EEXIST; |
| 305 | } |
| 306 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 307 | if (!gprs_sndcp_entity_alloc(lle, nsapi)) { |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 308 | LOGP(DSNDCP, LOGL_ERROR, "Out of memory during ACTIVATE\n"); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 309 | return -ENOMEM; |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 310 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 315 | /* Entry point for the SNSM-DEACTIVATE.indication */ |
| 316 | int sndcp_sm_deactivate_ind(struct gprs_llc_lle *lle, uint8_t nsapi) |
| 317 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 318 | struct gprs_sndcp_entity *sne; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 319 | |
| 320 | LOGP(DSNDCP, LOGL_INFO, "SNSM-DEACTIVATE.ind (lle=%p, TLLI=%08x, " |
| 321 | "SAPI=%u, NSAPI=%u)\n", lle, lle->llme->tlli, lle->sapi, nsapi); |
| 322 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 323 | sne = gprs_sndcp_entity_by_lle(lle, nsapi); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 324 | if (!sne) { |
| 325 | LOGP(DSNDCP, LOGL_ERROR, "SNSM-DEACTIVATE.ind for non-" |
| 326 | "existing TLLI=%08x SAPI=%u NSAPI=%u\n", lle->llme->tlli, |
| 327 | lle->sapi, nsapi); |
| 328 | return -ENOENT; |
| 329 | } |
| 330 | llist_del(&sne->list); |
| 331 | /* frag queue entries are hierarchically allocated, so no need to |
| 332 | * free them explicitly here */ |
| 333 | talloc_free(sne); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /* Fragmenter state */ |
| 339 | struct sndcp_frag_state { |
| 340 | uint8_t frag_nr; |
| 341 | struct msgb *msg; /* original message */ |
| 342 | uint8_t *next_byte; /* first byte of next fragment */ |
| 343 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 344 | struct gprs_sndcp_entity *sne; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 345 | void *mmcontext; |
| 346 | }; |
| 347 | |
| 348 | /* returns '1' if there are more fragments to send, '0' if none */ |
| 349 | static int sndcp_send_ud_frag(struct sndcp_frag_state *fs) |
| 350 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 351 | struct gprs_sndcp_entity *sne = fs->sne; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 352 | struct gprs_llc_lle *lle = sne->lle; |
| 353 | struct sndcp_common_hdr *sch; |
| 354 | struct sndcp_comp_hdr *scomph; |
| 355 | struct sndcp_udata_hdr *suh; |
| 356 | struct msgb *fmsg; |
| 357 | unsigned int max_payload_len; |
| 358 | unsigned int len; |
| 359 | uint8_t *data; |
| 360 | int rc, more; |
| 361 | |
Sylvain Munaut | eda125c | 2010-06-09 20:56:52 +0200 | [diff] [blame] | 362 | fmsg = msgb_alloc_headroom(fs->sne->lle->params.n201_u+256, 128, |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 363 | "SNDCP Frag"); |
| 364 | if (!fmsg) |
| 365 | return -ENOMEM; |
| 366 | |
| 367 | /* make sure lower layers route the fragment like the original */ |
| 368 | msgb_tlli(fmsg) = msgb_tlli(fs->msg); |
| 369 | msgb_bvci(fmsg) = msgb_bvci(fs->msg); |
| 370 | msgb_nsei(fmsg) = msgb_nsei(fs->msg); |
| 371 | |
| 372 | /* prepend common SNDCP header */ |
| 373 | sch = (struct sndcp_common_hdr *) msgb_put(fmsg, sizeof(*sch)); |
| 374 | sch->nsapi = sne->nsapi; |
| 375 | /* Set FIRST bit if we are the first fragment in a series */ |
| 376 | if (fs->frag_nr == 0) |
| 377 | sch->first = 1; |
| 378 | sch->type = 1; |
| 379 | |
| 380 | /* append the compression header for first fragment */ |
| 381 | if (sch->first) { |
| 382 | scomph = (struct sndcp_comp_hdr *) |
| 383 | msgb_put(fmsg, sizeof(*scomph)); |
| 384 | scomph->pcomp = 0; |
| 385 | scomph->dcomp = 0; |
| 386 | } |
| 387 | |
| 388 | /* append the user-data header */ |
| 389 | suh = (struct sndcp_udata_hdr *) msgb_put(fmsg, sizeof(*suh)); |
| 390 | suh->npdu_low = sne->tx_npdu_nr & 0xff; |
| 391 | suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf; |
| 392 | suh->seg_nr = fs->frag_nr % 0xf; |
| 393 | |
| 394 | /* calculate remaining length to be sent */ |
| 395 | len = (fs->msg->data + fs->msg->len) - fs->next_byte; |
| 396 | /* how much payload can we actually send via LLC? */ |
| 397 | max_payload_len = lle->params.n201_u - (sizeof(*sch) + sizeof(*suh)); |
| 398 | if (sch->first) |
| 399 | max_payload_len -= sizeof(*scomph); |
| 400 | /* check if we're exceeding the max */ |
| 401 | if (len > max_payload_len) |
| 402 | len = max_payload_len; |
| 403 | |
| 404 | /* copy the actual fragment data into our fmsg */ |
| 405 | data = msgb_put(fmsg, len); |
| 406 | memcpy(data, fs->next_byte, len); |
| 407 | |
| 408 | /* Increment fragment number and data pointer to next fragment */ |
| 409 | fs->frag_nr++; |
| 410 | fs->next_byte += len; |
| 411 | |
| 412 | /* determine if we have more fragemnts to send */ |
| 413 | if ((fs->msg->data + fs->msg->len) <= fs->next_byte) |
| 414 | more = 0; |
| 415 | else |
| 416 | more = 1; |
| 417 | |
| 418 | /* set the MORE bit of the SNDCP header accordingly */ |
| 419 | sch->more = more; |
| 420 | |
| 421 | rc = gprs_llc_tx_ui(fmsg, lle->sapi, 0, fs->mmcontext); |
| 422 | if (rc < 0) { |
| 423 | /* abort in case of error, do not advance frag_nr / next_byte */ |
| 424 | msgb_free(fmsg); |
| 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | if (!more) { |
| 429 | /* we've sent all fragments */ |
| 430 | msgb_free(fs->msg); |
| 431 | memset(fs, 0, sizeof(*fs)); |
| 432 | /* increment NPDU number for next frame */ |
| 433 | sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff; |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /* default: more fragments to send */ |
| 438 | return 1; |
| 439 | } |
| 440 | |
Harald Welte | db2c39f | 2010-06-03 07:14:59 +0200 | [diff] [blame] | 441 | /* Request transmission of a SN-PDU over specified LLC Entity + SAPI */ |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 442 | int sndcp_unitdata_req(struct msgb *msg, struct gprs_llc_lle *lle, uint8_t nsapi, |
| 443 | void *mmcontext) |
| 444 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 445 | struct gprs_sndcp_entity *sne; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 446 | struct sndcp_common_hdr *sch; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 447 | struct sndcp_comp_hdr *scomph; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 448 | struct sndcp_udata_hdr *suh; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 449 | struct sndcp_frag_state fs; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 450 | |
| 451 | /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */ |
| 452 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 453 | sne = gprs_sndcp_entity_by_lle(lle, nsapi); |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 454 | if (!sne) { |
| 455 | LOGP(DSNDCP, LOGL_ERROR, "Cannot find SNDCP Entity\n"); |
| 456 | return -EIO; |
| 457 | } |
| 458 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 459 | /* Check if we need to fragment this N-PDU into multiple SN-PDUs */ |
| 460 | if (msg->len > lle->params.n201_u - |
| 461 | (sizeof(*sch) + sizeof(*suh) + sizeof(*scomph))) { |
| 462 | /* initialize the fragmenter state */ |
| 463 | fs.msg = msg; |
| 464 | fs.frag_nr = 0; |
| 465 | fs.next_byte = msg->data; |
| 466 | fs.sne = sne; |
| 467 | fs.mmcontext = mmcontext; |
| 468 | |
| 469 | /* call function to generate and send fragments until all |
| 470 | * of the N-PDU has been sent */ |
| 471 | while (1) { |
| 472 | int rc = sndcp_send_ud_frag(&fs); |
| 473 | if (rc == 0) |
| 474 | return 0; |
| 475 | if (rc < 0) |
| 476 | return rc; |
| 477 | } |
| 478 | /* not reached */ |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /* this is the non-fragmenting case where we only build 1 SN-PDU */ |
| 483 | |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 484 | /* prepend the user-data header */ |
| 485 | suh = (struct sndcp_udata_hdr *) msgb_push(msg, sizeof(*suh)); |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 486 | suh->npdu_low = sne->tx_npdu_nr & 0xff; |
| 487 | suh->npdu_high = (sne->tx_npdu_nr >> 8) & 0xf; |
| 488 | suh->seg_nr = 0; |
| 489 | sne->tx_npdu_nr = (sne->tx_npdu_nr + 1) % 0xfff; |
| 490 | |
| 491 | scomph = (struct sndcp_comp_hdr *) msgb_push(msg, sizeof(*scomph)); |
| 492 | scomph->pcomp = 0; |
| 493 | scomph->dcomp = 0; |
Harald Welte | bb1c805 | 2010-06-03 06:38:38 +0200 | [diff] [blame] | 494 | |
| 495 | /* prepend common SNDCP header */ |
| 496 | sch = (struct sndcp_common_hdr *) msgb_push(msg, sizeof(*sch)); |
| 497 | sch->first = 1; |
| 498 | sch->type = 1; |
| 499 | sch->nsapi = nsapi; |
| 500 | |
| 501 | return gprs_llc_tx_ui(msg, lle->sapi, 0, mmcontext); |
| 502 | } |
| 503 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 504 | /* Section 5.1.2.17 LL-UNITDATA.ind */ |
Harald Welte | 36f1217 | 2010-07-02 16:44:24 +0200 | [diff] [blame] | 505 | int sndcp_llunitdata_ind(struct msgb *msg, struct gprs_llc_lle *lle, |
| 506 | uint8_t *hdr, uint16_t len) |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 507 | { |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 508 | struct gprs_sndcp_entity *sne; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 509 | struct sndcp_common_hdr *sch = (struct sndcp_common_hdr *)hdr; |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 510 | struct sndcp_comp_hdr *scomph = NULL; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 511 | struct sndcp_udata_hdr *suh; |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 512 | uint8_t *npdu; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 513 | uint16_t npdu_num; |
| 514 | int npdu_len; |
| 515 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 516 | sch = (struct sndcp_common_hdr *) hdr; |
| 517 | if (sch->first) { |
| 518 | scomph = (struct sndcp_comp_hdr *) (hdr + 1); |
| 519 | suh = (struct sndcp_udata_hdr *) (hdr + 1 + sizeof(struct sndcp_common_hdr)); |
| 520 | } else |
| 521 | suh = (struct sndcp_udata_hdr *) (hdr + sizeof(struct sndcp_common_hdr)); |
| 522 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 523 | if (sch->type == 0) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 524 | LOGP(DSNDCP, LOGL_ERROR, "SN-DATA PDU at unitdata_ind() function\n"); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 525 | return -EINVAL; |
| 526 | } |
| 527 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 528 | if (len < sizeof(*sch) + sizeof(*suh)) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 529 | LOGP(DSNDCP, LOGL_ERROR, "SN-UNITDATA PDU too short (%u)\n", len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 530 | return -EIO; |
| 531 | } |
| 532 | |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 533 | sne = gprs_sndcp_entity_by_lle(lle, sch->nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 534 | if (!sne) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 535 | LOGP(DSNDCP, LOGL_ERROR, "Message for non-existing SNDCP Entity " |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 536 | "(lle=%p, TLLI=%08x, SAPI=%u, NSAPI=%u)\n", lle, |
| 537 | lle->llme->tlli, lle->sapi, sch->nsapi); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 538 | return -EIO; |
| 539 | } |
Harald Welte | 8911cef | 2010-07-01 19:56:19 +0200 | [diff] [blame] | 540 | /* FIXME: move this RA_ID up to the LLME or even higher */ |
| 541 | bssgp_parse_cell_id(&sne->ra_id, msgb_bcid(msg)); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 542 | |
Harald Welte | ab4094c | 2010-07-02 16:01:47 +0200 | [diff] [blame] | 543 | /* any non-first segment is by definition something to defragment |
| 544 | * as is any segment that tells us there are more segments */ |
| 545 | if (!sch->first || sch->more) |
Harald Welte | 60da7d4 | 2010-07-02 15:45:12 +0200 | [diff] [blame] | 546 | return defrag_input(sne, msg, hdr, len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 547 | |
Harald Welte | ce22f92 | 2010-06-03 21:21:21 +0200 | [diff] [blame] | 548 | if (scomph && (scomph->pcomp || scomph->dcomp)) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 549 | LOGP(DSNDCP, LOGL_ERROR, "We don't support compression yet\n"); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 550 | return -EIO; |
| 551 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 552 | |
Harald Welte | 16836a3 | 2010-06-02 10:25:40 +0200 | [diff] [blame] | 553 | npdu_num = (suh->npdu_high << 8) | suh->npdu_low; |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 554 | npdu = (uint8_t *)suh + sizeof(*suh); |
| 555 | npdu_len = (msg->data + msg->len) - npdu; |
Harald Welte | 6144452 | 2010-06-02 12:40:48 +0200 | [diff] [blame] | 556 | if (npdu_len <= 0) { |
Harald Welte | 69996cb | 2010-06-02 10:26:19 +0200 | [diff] [blame] | 557 | LOGP(DSNDCP, LOGL_ERROR, "Short SNDCP N-PDU: %d\n", npdu_len); |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 558 | return -EIO; |
| 559 | } |
| 560 | /* actually send the N-PDU to the SGSN core code, which then |
| 561 | * hands it off to the correct GTP tunnel + GGSN via gtp_data_req() */ |
Harald Welte | 8911cef | 2010-07-01 19:56:19 +0200 | [diff] [blame] | 562 | return sgsn_rx_sndcp_ud_ind(&sne->ra_id, lle->llme->tlli, sne->nsapi, msg, npdu_len, npdu); |
Harald Welte | 96f71f2 | 2010-05-03 19:28:05 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 565 | /* Section 5.1.2.1 LL-RESET.ind */ |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 566 | static int sndcp_ll_reset_ind(struct gprs_sndcp_entity *se) |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 567 | { |
| 568 | /* treat all outstanding SNDCP-LLC request type primitives as not sent */ |
| 569 | /* reset all SNDCP XID parameters to default values */ |
| 570 | } |
| 571 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 572 | static int sndcp_ll_status_ind() |
| 573 | { |
| 574 | /* inform the SM sub-layer by means of SNSM-STATUS.req */ |
| 575 | } |
| 576 | |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 577 | #if 0 |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 578 | static struct sndcp_state_list {{ |
| 579 | uint32_t states; |
| 580 | unsigned int type; |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 581 | int (*rout)(struct gprs_sndcp_entity *se, struct msgb *msg); |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 582 | } sndcp_state_list[] = { |
| 583 | { ALL_STATES, |
| 584 | LL_RESET_IND, sndcp_ll_reset_ind }, |
| 585 | { ALL_STATES, |
| 586 | LL_ESTABLISH_IND, sndcp_ll_est_ind }, |
| 587 | { SBIT(SNDCP_S_EST_RQD), |
| 588 | LL_ESTABLISH_RESP, sndcp_ll_est_ind }, |
| 589 | { SBIT(SNDCP_S_EST_RQD), |
| 590 | LL_ESTABLISH_CONF, sndcp_ll_est_conf }, |
| 591 | { SBIT(SNDCP_S_ |
| 592 | }; |
| 593 | |
| 594 | static int sndcp_rx_llc_prim() |
| 595 | { |
| 596 | case LL_ESTABLISH_REQ: |
| 597 | case LL_RELEASE_REQ: |
| 598 | case LL_XID_REQ: |
| 599 | case LL_DATA_REQ: |
| 600 | LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */ |
| 601 | |
| 602 | switch (prim) { |
| 603 | case LL_RESET_IND: |
| 604 | case LL_ESTABLISH_IND: |
| 605 | case LL_ESTABLISH_RESP: |
| 606 | case LL_ESTABLISH_CONF: |
| 607 | case LL_RELEASE_IND: |
| 608 | case LL_RELEASE_CONF: |
| 609 | case LL_XID_IND: |
| 610 | case LL_XID_RESP: |
| 611 | case LL_XID_CONF: |
| 612 | case LL_DATA_IND: |
| 613 | case LL_DATA_CONF: |
| 614 | case LL_UNITDATA_IND: |
| 615 | case LL_STATUS_IND: |
| 616 | } |
Harald Welte | ebabdea | 2010-06-01 18:28:10 +0200 | [diff] [blame] | 617 | #endif |