SGSN: fix FCS calculation for encrypted frames

Change-Id: I352bc9db0c17fff773788831c4389ec0a5a30af8
Related: OS#1582
diff --git a/openbsc/src/gprs/gprs_llc.c b/openbsc/src/gprs/gprs_llc.c
index 23fd2e3..7724ebb 100644
--- a/openbsc/src/gprs/gprs_llc.c
+++ b/openbsc/src/gprs/gprs_llc.c
@@ -357,7 +357,7 @@
 		return -EINVAL;
 
 	/* Compute the 'Input' Paraemeter */
-	uint32_t iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
+	uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
 							 nu, oc);
 	/* Compute gamma that we need to XOR with the data */
 	int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
@@ -371,6 +371,12 @@
 	}
 
 	if (fcs) {
+		/* Mark frame as encrypted and update FCS */
+		data[2] |= 0x02;
+		fcs_calc = gprs_llc_fcs(data, fcs - data);
+		fcs[0] = fcs_calc & 0xff;
+		fcs[1] = (fcs_calc >> 8) & 0xff;
+		fcs[2] = (fcs_calc >> 16) & 0xff;
 		data += 3;
 	}
 
@@ -452,9 +458,6 @@
 			msgb_free(msg);
 			return rc;
 		}
-
-		/* Mark frame as encrypted */
-		ctrl[1] |= 0x02;
 	}
 
 	/* Identifiers passed down: (BVCI, NSEI) */
@@ -640,6 +643,9 @@
 				       llhp.data);
 			if (rc < 0)
 				return rc;
+		llhp.fcs = *(llhp.data + llhp.data_len);
+		llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
+		llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
 		} else {
 			LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
 				"has no KC/Algo! Dropping.\n");