calipso.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * CALIPSO - Common Architecture Label IPv6 Security Option
  3. *
  4. * This is an implementation of the CALIPSO protocol as specified in
  5. * RFC 5570.
  6. *
  7. * Authors: Paul Moore <paul.moore@hp.com>
  8. * Huw Davies <huw@codeweavers.com>
  9. *
  10. */
  11. /* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  12. * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/string.h>
  34. #include <linux/jhash.h>
  35. #include <linux/audit.h>
  36. #include <linux/slab.h>
  37. #include <net/ip.h>
  38. #include <net/icmp.h>
  39. #include <net/tcp.h>
  40. #include <net/netlabel.h>
  41. #include <net/calipso.h>
  42. #include <linux/atomic.h>
  43. #include <linux/bug.h>
  44. #include <asm/unaligned.h>
  45. #include <linux/crc-ccitt.h>
  46. /* Maximium size of the calipso option including
  47. * the two-byte TLV header.
  48. */
  49. #define CALIPSO_OPT_LEN_MAX (2 + 252)
  50. /* Size of the minimum calipso option including
  51. * the two-byte TLV header.
  52. */
  53. #define CALIPSO_HDR_LEN (2 + 8)
  54. /* Maximium size of the calipso option including
  55. * the two-byte TLV header and upto 3 bytes of
  56. * leading pad and 7 bytes of trailing pad.
  57. */
  58. #define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
  59. /* Maximium size of u32 aligned buffer required to hold calipso
  60. * option. Max of 3 initial pad bytes starting from buffer + 3.
  61. * i.e. the worst case is when the previous tlv finishes on 4n + 3.
  62. */
  63. #define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
  64. /* List of available DOI definitions */
  65. static DEFINE_SPINLOCK(calipso_doi_list_lock);
  66. static LIST_HEAD(calipso_doi_list);
  67. /* Label mapping cache */
  68. int calipso_cache_enabled = 1;
  69. int calipso_cache_bucketsize = 10;
  70. #define CALIPSO_CACHE_BUCKETBITS 7
  71. #define CALIPSO_CACHE_BUCKETS BIT(CALIPSO_CACHE_BUCKETBITS)
  72. #define CALIPSO_CACHE_REORDERLIMIT 10
  73. struct calipso_map_cache_bkt {
  74. spinlock_t lock;
  75. u32 size;
  76. struct list_head list;
  77. };
  78. struct calipso_map_cache_entry {
  79. u32 hash;
  80. unsigned char *key;
  81. size_t key_len;
  82. struct netlbl_lsm_cache *lsm_data;
  83. u32 activity;
  84. struct list_head list;
  85. };
  86. static struct calipso_map_cache_bkt *calipso_cache;
  87. /* Label Mapping Cache Functions
  88. */
  89. /**
  90. * calipso_cache_entry_free - Frees a cache entry
  91. * @entry: the entry to free
  92. *
  93. * Description:
  94. * This function frees the memory associated with a cache entry including the
  95. * LSM cache data if there are no longer any users, i.e. reference count == 0.
  96. *
  97. */
  98. static void calipso_cache_entry_free(struct calipso_map_cache_entry *entry)
  99. {
  100. if (entry->lsm_data)
  101. netlbl_secattr_cache_free(entry->lsm_data);
  102. kfree(entry->key);
  103. kfree(entry);
  104. }
  105. /**
  106. * calipso_map_cache_hash - Hashing function for the CALIPSO cache
  107. * @key: the hash key
  108. * @key_len: the length of the key in bytes
  109. *
  110. * Description:
  111. * The CALIPSO tag hashing function. Returns a 32-bit hash value.
  112. *
  113. */
  114. static u32 calipso_map_cache_hash(const unsigned char *key, u32 key_len)
  115. {
  116. return jhash(key, key_len, 0);
  117. }
  118. /**
  119. * calipso_cache_init - Initialize the CALIPSO cache
  120. *
  121. * Description:
  122. * Initializes the CALIPSO label mapping cache, this function should be called
  123. * before any of the other functions defined in this file. Returns zero on
  124. * success, negative values on error.
  125. *
  126. */
  127. static int __init calipso_cache_init(void)
  128. {
  129. u32 iter;
  130. calipso_cache = kcalloc(CALIPSO_CACHE_BUCKETS,
  131. sizeof(struct calipso_map_cache_bkt),
  132. GFP_KERNEL);
  133. if (!calipso_cache)
  134. return -ENOMEM;
  135. for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
  136. spin_lock_init(&calipso_cache[iter].lock);
  137. calipso_cache[iter].size = 0;
  138. INIT_LIST_HEAD(&calipso_cache[iter].list);
  139. }
  140. return 0;
  141. }
  142. /**
  143. * calipso_cache_invalidate - Invalidates the current CALIPSO cache
  144. *
  145. * Description:
  146. * Invalidates and frees any entries in the CALIPSO cache. Returns zero on
  147. * success and negative values on failure.
  148. *
  149. */
  150. static void calipso_cache_invalidate(void)
  151. {
  152. struct calipso_map_cache_entry *entry, *tmp_entry;
  153. u32 iter;
  154. for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
  155. spin_lock_bh(&calipso_cache[iter].lock);
  156. list_for_each_entry_safe(entry,
  157. tmp_entry,
  158. &calipso_cache[iter].list, list) {
  159. list_del(&entry->list);
  160. calipso_cache_entry_free(entry);
  161. }
  162. calipso_cache[iter].size = 0;
  163. spin_unlock_bh(&calipso_cache[iter].lock);
  164. }
  165. }
  166. /**
  167. * calipso_cache_check - Check the CALIPSO cache for a label mapping
  168. * @key: the buffer to check
  169. * @key_len: buffer length in bytes
  170. * @secattr: the security attribute struct to use
  171. *
  172. * Description:
  173. * This function checks the cache to see if a label mapping already exists for
  174. * the given key. If there is a match then the cache is adjusted and the
  175. * @secattr struct is populated with the correct LSM security attributes. The
  176. * cache is adjusted in the following manner if the entry is not already the
  177. * first in the cache bucket:
  178. *
  179. * 1. The cache entry's activity counter is incremented
  180. * 2. The previous (higher ranking) entry's activity counter is decremented
  181. * 3. If the difference between the two activity counters is geater than
  182. * CALIPSO_CACHE_REORDERLIMIT the two entries are swapped
  183. *
  184. * Returns zero on success, -ENOENT for a cache miss, and other negative values
  185. * on error.
  186. *
  187. */
  188. static int calipso_cache_check(const unsigned char *key,
  189. u32 key_len,
  190. struct netlbl_lsm_secattr *secattr)
  191. {
  192. u32 bkt;
  193. struct calipso_map_cache_entry *entry;
  194. struct calipso_map_cache_entry *prev_entry = NULL;
  195. u32 hash;
  196. if (!calipso_cache_enabled)
  197. return -ENOENT;
  198. hash = calipso_map_cache_hash(key, key_len);
  199. bkt = hash & (CALIPSO_CACHE_BUCKETS - 1);
  200. spin_lock_bh(&calipso_cache[bkt].lock);
  201. list_for_each_entry(entry, &calipso_cache[bkt].list, list) {
  202. if (entry->hash == hash &&
  203. entry->key_len == key_len &&
  204. memcmp(entry->key, key, key_len) == 0) {
  205. entry->activity += 1;
  206. refcount_inc(&entry->lsm_data->refcount);
  207. secattr->cache = entry->lsm_data;
  208. secattr->flags |= NETLBL_SECATTR_CACHE;
  209. secattr->type = NETLBL_NLTYPE_CALIPSO;
  210. if (!prev_entry) {
  211. spin_unlock_bh(&calipso_cache[bkt].lock);
  212. return 0;
  213. }
  214. if (prev_entry->activity > 0)
  215. prev_entry->activity -= 1;
  216. if (entry->activity > prev_entry->activity &&
  217. entry->activity - prev_entry->activity >
  218. CALIPSO_CACHE_REORDERLIMIT) {
  219. __list_del(entry->list.prev, entry->list.next);
  220. __list_add(&entry->list,
  221. prev_entry->list.prev,
  222. &prev_entry->list);
  223. }
  224. spin_unlock_bh(&calipso_cache[bkt].lock);
  225. return 0;
  226. }
  227. prev_entry = entry;
  228. }
  229. spin_unlock_bh(&calipso_cache[bkt].lock);
  230. return -ENOENT;
  231. }
  232. /**
  233. * calipso_cache_add - Add an entry to the CALIPSO cache
  234. * @calipso_ptr: the CALIPSO option
  235. * @secattr: the packet's security attributes
  236. *
  237. * Description:
  238. * Add a new entry into the CALIPSO label mapping cache. Add the new entry to
  239. * head of the cache bucket's list, if the cache bucket is out of room remove
  240. * the last entry in the list first. It is important to note that there is
  241. * currently no checking for duplicate keys. Returns zero on success,
  242. * negative values on failure. The key stored starts at calipso_ptr + 2,
  243. * i.e. the type and length bytes are not stored, this corresponds to
  244. * calipso_ptr[1] bytes of data.
  245. *
  246. */
  247. static int calipso_cache_add(const unsigned char *calipso_ptr,
  248. const struct netlbl_lsm_secattr *secattr)
  249. {
  250. int ret_val = -EPERM;
  251. u32 bkt;
  252. struct calipso_map_cache_entry *entry = NULL;
  253. struct calipso_map_cache_entry *old_entry = NULL;
  254. u32 calipso_ptr_len;
  255. if (!calipso_cache_enabled || calipso_cache_bucketsize <= 0)
  256. return 0;
  257. calipso_ptr_len = calipso_ptr[1];
  258. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  259. if (!entry)
  260. return -ENOMEM;
  261. entry->key = kmemdup(calipso_ptr + 2, calipso_ptr_len, GFP_ATOMIC);
  262. if (!entry->key) {
  263. ret_val = -ENOMEM;
  264. goto cache_add_failure;
  265. }
  266. entry->key_len = calipso_ptr_len;
  267. entry->hash = calipso_map_cache_hash(calipso_ptr, calipso_ptr_len);
  268. refcount_inc(&secattr->cache->refcount);
  269. entry->lsm_data = secattr->cache;
  270. bkt = entry->hash & (CALIPSO_CACHE_BUCKETS - 1);
  271. spin_lock_bh(&calipso_cache[bkt].lock);
  272. if (calipso_cache[bkt].size < calipso_cache_bucketsize) {
  273. list_add(&entry->list, &calipso_cache[bkt].list);
  274. calipso_cache[bkt].size += 1;
  275. } else {
  276. old_entry = list_entry(calipso_cache[bkt].list.prev,
  277. struct calipso_map_cache_entry, list);
  278. list_del(&old_entry->list);
  279. list_add(&entry->list, &calipso_cache[bkt].list);
  280. calipso_cache_entry_free(old_entry);
  281. }
  282. spin_unlock_bh(&calipso_cache[bkt].lock);
  283. return 0;
  284. cache_add_failure:
  285. if (entry)
  286. calipso_cache_entry_free(entry);
  287. return ret_val;
  288. }
  289. /* DOI List Functions
  290. */
  291. /**
  292. * calipso_doi_search - Searches for a DOI definition
  293. * @doi: the DOI to search for
  294. *
  295. * Description:
  296. * Search the DOI definition list for a DOI definition with a DOI value that
  297. * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
  298. * Returns a pointer to the DOI definition on success and NULL on failure.
  299. */
  300. static struct calipso_doi *calipso_doi_search(u32 doi)
  301. {
  302. struct calipso_doi *iter;
  303. list_for_each_entry_rcu(iter, &calipso_doi_list, list)
  304. if (iter->doi == doi && refcount_read(&iter->refcount))
  305. return iter;
  306. return NULL;
  307. }
  308. /**
  309. * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
  310. * @doi_def: the DOI structure
  311. * @audit_info: NetLabel audit information
  312. *
  313. * Description:
  314. * The caller defines a new DOI for use by the CALIPSO engine and calls this
  315. * function to add it to the list of acceptable domains. The caller must
  316. * ensure that the mapping table specified in @doi_def->map meets all of the
  317. * requirements of the mapping type (see calipso.h for details). Returns
  318. * zero on success and non-zero on failure.
  319. *
  320. */
  321. static int calipso_doi_add(struct calipso_doi *doi_def,
  322. struct netlbl_audit *audit_info)
  323. {
  324. int ret_val = -EINVAL;
  325. u32 doi;
  326. u32 doi_type;
  327. struct audit_buffer *audit_buf;
  328. doi = doi_def->doi;
  329. doi_type = doi_def->type;
  330. if (doi_def->doi == CALIPSO_DOI_UNKNOWN)
  331. goto doi_add_return;
  332. refcount_set(&doi_def->refcount, 1);
  333. spin_lock(&calipso_doi_list_lock);
  334. if (calipso_doi_search(doi_def->doi)) {
  335. spin_unlock(&calipso_doi_list_lock);
  336. ret_val = -EEXIST;
  337. goto doi_add_return;
  338. }
  339. list_add_tail_rcu(&doi_def->list, &calipso_doi_list);
  340. spin_unlock(&calipso_doi_list_lock);
  341. ret_val = 0;
  342. doi_add_return:
  343. audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD, audit_info);
  344. if (audit_buf) {
  345. const char *type_str;
  346. switch (doi_type) {
  347. case CALIPSO_MAP_PASS:
  348. type_str = "pass";
  349. break;
  350. default:
  351. type_str = "(unknown)";
  352. }
  353. audit_log_format(audit_buf,
  354. " calipso_doi=%u calipso_type=%s res=%u",
  355. doi, type_str, ret_val == 0 ? 1 : 0);
  356. audit_log_end(audit_buf);
  357. }
  358. return ret_val;
  359. }
  360. /**
  361. * calipso_doi_free - Frees a DOI definition
  362. * @doi_def: the DOI definition
  363. *
  364. * Description:
  365. * This function frees all of the memory associated with a DOI definition.
  366. *
  367. */
  368. static void calipso_doi_free(struct calipso_doi *doi_def)
  369. {
  370. kfree(doi_def);
  371. }
  372. /**
  373. * calipso_doi_free_rcu - Frees a DOI definition via the RCU pointer
  374. * @entry: the entry's RCU field
  375. *
  376. * Description:
  377. * This function is designed to be used as a callback to the call_rcu()
  378. * function so that the memory allocated to the DOI definition can be released
  379. * safely.
  380. *
  381. */
  382. static void calipso_doi_free_rcu(struct rcu_head *entry)
  383. {
  384. struct calipso_doi *doi_def;
  385. doi_def = container_of(entry, struct calipso_doi, rcu);
  386. calipso_doi_free(doi_def);
  387. }
  388. /**
  389. * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
  390. * @doi: the DOI value
  391. * @audit_secid: the LSM secid to use in the audit message
  392. *
  393. * Description:
  394. * Removes a DOI definition from the CALIPSO engine. The NetLabel routines will
  395. * be called to release their own LSM domain mappings as well as our own
  396. * domain list. Returns zero on success and negative values on failure.
  397. *
  398. */
  399. static int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
  400. {
  401. int ret_val;
  402. struct calipso_doi *doi_def;
  403. struct audit_buffer *audit_buf;
  404. spin_lock(&calipso_doi_list_lock);
  405. doi_def = calipso_doi_search(doi);
  406. if (!doi_def) {
  407. spin_unlock(&calipso_doi_list_lock);
  408. ret_val = -ENOENT;
  409. goto doi_remove_return;
  410. }
  411. if (!refcount_dec_and_test(&doi_def->refcount)) {
  412. spin_unlock(&calipso_doi_list_lock);
  413. ret_val = -EBUSY;
  414. goto doi_remove_return;
  415. }
  416. list_del_rcu(&doi_def->list);
  417. spin_unlock(&calipso_doi_list_lock);
  418. call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
  419. ret_val = 0;
  420. doi_remove_return:
  421. audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_DEL, audit_info);
  422. if (audit_buf) {
  423. audit_log_format(audit_buf,
  424. " calipso_doi=%u res=%u",
  425. doi, ret_val == 0 ? 1 : 0);
  426. audit_log_end(audit_buf);
  427. }
  428. return ret_val;
  429. }
  430. /**
  431. * calipso_doi_getdef - Returns a reference to a valid DOI definition
  432. * @doi: the DOI value
  433. *
  434. * Description:
  435. * Searches for a valid DOI definition and if one is found it is returned to
  436. * the caller. Otherwise NULL is returned. The caller must ensure that
  437. * calipso_doi_putdef() is called when the caller is done.
  438. *
  439. */
  440. static struct calipso_doi *calipso_doi_getdef(u32 doi)
  441. {
  442. struct calipso_doi *doi_def;
  443. rcu_read_lock();
  444. doi_def = calipso_doi_search(doi);
  445. if (!doi_def)
  446. goto doi_getdef_return;
  447. if (!refcount_inc_not_zero(&doi_def->refcount))
  448. doi_def = NULL;
  449. doi_getdef_return:
  450. rcu_read_unlock();
  451. return doi_def;
  452. }
  453. /**
  454. * calipso_doi_putdef - Releases a reference for the given DOI definition
  455. * @doi_def: the DOI definition
  456. *
  457. * Description:
  458. * Releases a DOI definition reference obtained from calipso_doi_getdef().
  459. *
  460. */
  461. static void calipso_doi_putdef(struct calipso_doi *doi_def)
  462. {
  463. if (!doi_def)
  464. return;
  465. if (!refcount_dec_and_test(&doi_def->refcount))
  466. return;
  467. spin_lock(&calipso_doi_list_lock);
  468. list_del_rcu(&doi_def->list);
  469. spin_unlock(&calipso_doi_list_lock);
  470. call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
  471. }
  472. /**
  473. * calipso_doi_walk - Iterate through the DOI definitions
  474. * @skip_cnt: skip past this number of DOI definitions, updated
  475. * @callback: callback for each DOI definition
  476. * @cb_arg: argument for the callback function
  477. *
  478. * Description:
  479. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  480. * For each entry call @callback, if @callback returns a negative value stop
  481. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  482. * return. Returns zero on success, negative values on failure.
  483. *
  484. */
  485. static int calipso_doi_walk(u32 *skip_cnt,
  486. int (*callback)(struct calipso_doi *doi_def,
  487. void *arg),
  488. void *cb_arg)
  489. {
  490. int ret_val = -ENOENT;
  491. u32 doi_cnt = 0;
  492. struct calipso_doi *iter_doi;
  493. rcu_read_lock();
  494. list_for_each_entry_rcu(iter_doi, &calipso_doi_list, list)
  495. if (refcount_read(&iter_doi->refcount) > 0) {
  496. if (doi_cnt++ < *skip_cnt)
  497. continue;
  498. ret_val = callback(iter_doi, cb_arg);
  499. if (ret_val < 0) {
  500. doi_cnt--;
  501. goto doi_walk_return;
  502. }
  503. }
  504. doi_walk_return:
  505. rcu_read_unlock();
  506. *skip_cnt = doi_cnt;
  507. return ret_val;
  508. }
  509. /**
  510. * calipso_validate - Validate a CALIPSO option
  511. * @skb: the packet
  512. * @option: the start of the option
  513. *
  514. * Description:
  515. * This routine is called to validate a CALIPSO option.
  516. * If the option is valid then %true is returned, otherwise
  517. * %false is returned.
  518. *
  519. * The caller should have already checked that the length of the
  520. * option (including the TLV header) is >= 10 and that the catmap
  521. * length is consistent with the option length.
  522. *
  523. * We leave checks on the level and categories to the socket layer.
  524. */
  525. bool calipso_validate(const struct sk_buff *skb, const unsigned char *option)
  526. {
  527. struct calipso_doi *doi_def;
  528. bool ret_val;
  529. u16 crc, len = option[1] + 2;
  530. static const u8 zero[2];
  531. /* The original CRC runs over the option including the TLV header
  532. * with the CRC-16 field (at offset 8) zeroed out. */
  533. crc = crc_ccitt(0xffff, option, 8);
  534. crc = crc_ccitt(crc, zero, sizeof(zero));
  535. if (len > 10)
  536. crc = crc_ccitt(crc, option + 10, len - 10);
  537. crc = ~crc;
  538. if (option[8] != (crc & 0xff) || option[9] != ((crc >> 8) & 0xff))
  539. return false;
  540. rcu_read_lock();
  541. doi_def = calipso_doi_search(get_unaligned_be32(option + 2));
  542. ret_val = !!doi_def;
  543. rcu_read_unlock();
  544. return ret_val;
  545. }
  546. /**
  547. * calipso_map_cat_hton - Perform a category mapping from host to network
  548. * @doi_def: the DOI definition
  549. * @secattr: the security attributes
  550. * @net_cat: the zero'd out category bitmap in network/CALIPSO format
  551. * @net_cat_len: the length of the CALIPSO bitmap in bytes
  552. *
  553. * Description:
  554. * Perform a label mapping to translate a local MLS category bitmap to the
  555. * correct CALIPSO bitmap using the given DOI definition. Returns the minimum
  556. * size in bytes of the network bitmap on success, negative values otherwise.
  557. *
  558. */
  559. static int calipso_map_cat_hton(const struct calipso_doi *doi_def,
  560. const struct netlbl_lsm_secattr *secattr,
  561. unsigned char *net_cat,
  562. u32 net_cat_len)
  563. {
  564. int spot = -1;
  565. u32 net_spot_max = 0;
  566. u32 net_clen_bits = net_cat_len * 8;
  567. for (;;) {
  568. spot = netlbl_catmap_walk(secattr->attr.mls.cat,
  569. spot + 1);
  570. if (spot < 0)
  571. break;
  572. if (spot >= net_clen_bits)
  573. return -ENOSPC;
  574. netlbl_bitmap_setbit(net_cat, spot, 1);
  575. if (spot > net_spot_max)
  576. net_spot_max = spot;
  577. }
  578. return (net_spot_max / 32 + 1) * 4;
  579. }
  580. /**
  581. * calipso_map_cat_ntoh - Perform a category mapping from network to host
  582. * @doi_def: the DOI definition
  583. * @net_cat: the category bitmap in network/CALIPSO format
  584. * @net_cat_len: the length of the CALIPSO bitmap in bytes
  585. * @secattr: the security attributes
  586. *
  587. * Description:
  588. * Perform a label mapping to translate a CALIPSO bitmap to the correct local
  589. * MLS category bitmap using the given DOI definition. Returns zero on
  590. * success, negative values on failure.
  591. *
  592. */
  593. static int calipso_map_cat_ntoh(const struct calipso_doi *doi_def,
  594. const unsigned char *net_cat,
  595. u32 net_cat_len,
  596. struct netlbl_lsm_secattr *secattr)
  597. {
  598. int ret_val;
  599. int spot = -1;
  600. u32 net_clen_bits = net_cat_len * 8;
  601. for (;;) {
  602. spot = netlbl_bitmap_walk(net_cat,
  603. net_clen_bits,
  604. spot + 1,
  605. 1);
  606. if (spot < 0) {
  607. if (spot == -2)
  608. return -EFAULT;
  609. return 0;
  610. }
  611. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  612. spot,
  613. GFP_ATOMIC);
  614. if (ret_val != 0)
  615. return ret_val;
  616. }
  617. return -EINVAL;
  618. }
  619. /**
  620. * calipso_pad_write - Writes pad bytes in TLV format
  621. * @buf: the buffer
  622. * @offset: offset from start of buffer to write padding
  623. * @count: number of pad bytes to write
  624. *
  625. * Description:
  626. * Write @count bytes of TLV padding into @buffer starting at offset @offset.
  627. * @count should be less than 8 - see RFC 4942.
  628. *
  629. */
  630. static int calipso_pad_write(unsigned char *buf, unsigned int offset,
  631. unsigned int count)
  632. {
  633. if (WARN_ON_ONCE(count >= 8))
  634. return -EINVAL;
  635. switch (count) {
  636. case 0:
  637. break;
  638. case 1:
  639. buf[offset] = IPV6_TLV_PAD1;
  640. break;
  641. default:
  642. buf[offset] = IPV6_TLV_PADN;
  643. buf[offset + 1] = count - 2;
  644. if (count > 2)
  645. memset(buf + offset + 2, 0, count - 2);
  646. break;
  647. }
  648. return 0;
  649. }
  650. /**
  651. * calipso_genopt - Generate a CALIPSO option
  652. * @buf: the option buffer
  653. * @start: offset from which to write
  654. * @buf_len: the size of opt_buf
  655. * @doi_def: the CALIPSO DOI to use
  656. * @secattr: the security attributes
  657. *
  658. * Description:
  659. * Generate a CALIPSO option using the DOI definition and security attributes
  660. * passed to the function. This also generates upto three bytes of leading
  661. * padding that ensures that the option is 4n + 2 aligned. It returns the
  662. * number of bytes written (including any initial padding).
  663. */
  664. static int calipso_genopt(unsigned char *buf, u32 start, u32 buf_len,
  665. const struct calipso_doi *doi_def,
  666. const struct netlbl_lsm_secattr *secattr)
  667. {
  668. int ret_val;
  669. u32 len, pad;
  670. u16 crc;
  671. static const unsigned char padding[4] = {2, 1, 0, 3};
  672. unsigned char *calipso;
  673. /* CALIPSO has 4n + 2 alignment */
  674. pad = padding[start & 3];
  675. if (buf_len <= start + pad + CALIPSO_HDR_LEN)
  676. return -ENOSPC;
  677. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  678. return -EPERM;
  679. len = CALIPSO_HDR_LEN;
  680. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  681. ret_val = calipso_map_cat_hton(doi_def,
  682. secattr,
  683. buf + start + pad + len,
  684. buf_len - start - pad - len);
  685. if (ret_val < 0)
  686. return ret_val;
  687. len += ret_val;
  688. }
  689. calipso_pad_write(buf, start, pad);
  690. calipso = buf + start + pad;
  691. calipso[0] = IPV6_TLV_CALIPSO;
  692. calipso[1] = len - 2;
  693. *(__be32 *)(calipso + 2) = htonl(doi_def->doi);
  694. calipso[6] = (len - CALIPSO_HDR_LEN) / 4;
  695. calipso[7] = secattr->attr.mls.lvl,
  696. crc = ~crc_ccitt(0xffff, calipso, len);
  697. calipso[8] = crc & 0xff;
  698. calipso[9] = (crc >> 8) & 0xff;
  699. return pad + len;
  700. }
  701. /* Hop-by-hop hdr helper functions
  702. */
  703. /**
  704. * calipso_opt_update - Replaces socket's hop options with a new set
  705. * @sk: the socket
  706. * @hop: new hop options
  707. *
  708. * Description:
  709. * Replaces @sk's hop options with @hop. @hop may be NULL to leave
  710. * the socket with no hop options.
  711. *
  712. */
  713. static int calipso_opt_update(struct sock *sk, struct ipv6_opt_hdr *hop)
  714. {
  715. struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)), *txopts;
  716. txopts = ipv6_renew_options(sk, old, IPV6_HOPOPTS, hop);
  717. txopt_put(old);
  718. if (IS_ERR(txopts))
  719. return PTR_ERR(txopts);
  720. txopts = ipv6_update_options(sk, txopts);
  721. if (txopts) {
  722. atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
  723. txopt_put(txopts);
  724. }
  725. return 0;
  726. }
  727. /**
  728. * calipso_tlv_len - Returns the length of the TLV
  729. * @opt: the option header
  730. * @offset: offset of the TLV within the header
  731. *
  732. * Description:
  733. * Returns the length of the TLV option at offset @offset within
  734. * the option header @opt. Checks that the entire TLV fits inside
  735. * the option header, returns a negative value if this is not the case.
  736. */
  737. static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
  738. {
  739. unsigned char *tlv = (unsigned char *)opt;
  740. unsigned int opt_len = ipv6_optlen(opt), tlv_len;
  741. if (offset < sizeof(*opt) || offset >= opt_len)
  742. return -EINVAL;
  743. if (tlv[offset] == IPV6_TLV_PAD1)
  744. return 1;
  745. if (offset + 1 >= opt_len)
  746. return -EINVAL;
  747. tlv_len = tlv[offset + 1] + 2;
  748. if (offset + tlv_len > opt_len)
  749. return -EINVAL;
  750. return tlv_len;
  751. }
  752. /**
  753. * calipso_opt_find - Finds the CALIPSO option in an IPv6 hop options header
  754. * @hop: the hop options header
  755. * @start: on return holds the offset of any leading padding
  756. * @end: on return holds the offset of the first non-pad TLV after CALIPSO
  757. *
  758. * Description:
  759. * Finds the space occupied by a CALIPSO option (including any leading and
  760. * trailing padding).
  761. *
  762. * If a CALIPSO option exists set @start and @end to the
  763. * offsets within @hop of the start of padding before the first
  764. * CALIPSO option and the end of padding after the first CALIPSO
  765. * option. In this case the function returns 0.
  766. *
  767. * In the absence of a CALIPSO option, @start and @end will be
  768. * set to the start and end of any trailing padding in the header.
  769. * This is useful when appending a new option, as the caller may want
  770. * to overwrite some of this padding. In this case the function will
  771. * return -ENOENT.
  772. */
  773. static int calipso_opt_find(struct ipv6_opt_hdr *hop, unsigned int *start,
  774. unsigned int *end)
  775. {
  776. int ret_val = -ENOENT, tlv_len;
  777. unsigned int opt_len, offset, offset_s = 0, offset_e = 0;
  778. unsigned char *opt = (unsigned char *)hop;
  779. opt_len = ipv6_optlen(hop);
  780. offset = sizeof(*hop);
  781. while (offset < opt_len) {
  782. tlv_len = calipso_tlv_len(hop, offset);
  783. if (tlv_len < 0)
  784. return tlv_len;
  785. switch (opt[offset]) {
  786. case IPV6_TLV_PAD1:
  787. case IPV6_TLV_PADN:
  788. if (offset_e)
  789. offset_e = offset;
  790. break;
  791. case IPV6_TLV_CALIPSO:
  792. ret_val = 0;
  793. offset_e = offset;
  794. break;
  795. default:
  796. if (offset_e == 0)
  797. offset_s = offset;
  798. else
  799. goto out;
  800. }
  801. offset += tlv_len;
  802. }
  803. out:
  804. if (offset_s)
  805. *start = offset_s + calipso_tlv_len(hop, offset_s);
  806. else
  807. *start = sizeof(*hop);
  808. if (offset_e)
  809. *end = offset_e + calipso_tlv_len(hop, offset_e);
  810. else
  811. *end = opt_len;
  812. return ret_val;
  813. }
  814. /**
  815. * calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr
  816. * @hop: the original hop options header
  817. * @doi_def: the CALIPSO DOI to use
  818. * @secattr: the specific security attributes of the socket
  819. *
  820. * Description:
  821. * Creates a new hop options header based on @hop with a
  822. * CALIPSO option added to it. If @hop already contains a CALIPSO
  823. * option this is overwritten, otherwise the new option is appended
  824. * after any existing options. If @hop is NULL then the new header
  825. * will contain just the CALIPSO option and any needed padding.
  826. *
  827. */
  828. static struct ipv6_opt_hdr *
  829. calipso_opt_insert(struct ipv6_opt_hdr *hop,
  830. const struct calipso_doi *doi_def,
  831. const struct netlbl_lsm_secattr *secattr)
  832. {
  833. unsigned int start, end, buf_len, pad, hop_len;
  834. struct ipv6_opt_hdr *new;
  835. int ret_val;
  836. if (hop) {
  837. hop_len = ipv6_optlen(hop);
  838. ret_val = calipso_opt_find(hop, &start, &end);
  839. if (ret_val && ret_val != -ENOENT)
  840. return ERR_PTR(ret_val);
  841. } else {
  842. hop_len = 0;
  843. start = sizeof(*hop);
  844. end = 0;
  845. }
  846. buf_len = hop_len + start - end + CALIPSO_OPT_LEN_MAX_WITH_PAD;
  847. new = kzalloc(buf_len, GFP_ATOMIC);
  848. if (!new)
  849. return ERR_PTR(-ENOMEM);
  850. if (start > sizeof(*hop))
  851. memcpy(new, hop, start);
  852. ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
  853. secattr);
  854. if (ret_val < 0) {
  855. kfree(new);
  856. return ERR_PTR(ret_val);
  857. }
  858. buf_len = start + ret_val;
  859. /* At this point buf_len aligns to 4n, so (buf_len & 4) pads to 8n */
  860. pad = ((buf_len & 4) + (end & 7)) & 7;
  861. calipso_pad_write((unsigned char *)new, buf_len, pad);
  862. buf_len += pad;
  863. if (end != hop_len) {
  864. memcpy((char *)new + buf_len, (char *)hop + end, hop_len - end);
  865. buf_len += hop_len - end;
  866. }
  867. new->nexthdr = 0;
  868. new->hdrlen = buf_len / 8 - 1;
  869. return new;
  870. }
  871. /**
  872. * calipso_opt_del - Removes the CALIPSO option from an option header
  873. * @hop: the original header
  874. * @new: the new header
  875. *
  876. * Description:
  877. * Creates a new header based on @hop without any CALIPSO option. If @hop
  878. * doesn't contain a CALIPSO option it returns -ENOENT. If @hop contains
  879. * no other non-padding options, it returns zero with @new set to NULL.
  880. * Otherwise it returns zero, creates a new header without the CALIPSO
  881. * option (and removing as much padding as possible) and returns with
  882. * @new set to that header.
  883. *
  884. */
  885. static int calipso_opt_del(struct ipv6_opt_hdr *hop,
  886. struct ipv6_opt_hdr **new)
  887. {
  888. int ret_val;
  889. unsigned int start, end, delta, pad, hop_len;
  890. ret_val = calipso_opt_find(hop, &start, &end);
  891. if (ret_val)
  892. return ret_val;
  893. hop_len = ipv6_optlen(hop);
  894. if (start == sizeof(*hop) && end == hop_len) {
  895. /* There's no other option in the header so return NULL */
  896. *new = NULL;
  897. return 0;
  898. }
  899. delta = (end - start) & ~7;
  900. *new = kzalloc(hop_len - delta, GFP_ATOMIC);
  901. if (!*new)
  902. return -ENOMEM;
  903. memcpy(*new, hop, start);
  904. (*new)->hdrlen -= delta / 8;
  905. pad = (end - start) & 7;
  906. calipso_pad_write((unsigned char *)*new, start, pad);
  907. if (end != hop_len)
  908. memcpy((char *)*new + start + pad, (char *)hop + end,
  909. hop_len - end);
  910. return 0;
  911. }
  912. /**
  913. * calipso_opt_getattr - Get the security attributes from a memory block
  914. * @calipso: the CALIPSO option
  915. * @secattr: the security attributes
  916. *
  917. * Description:
  918. * Inspect @calipso and return the security attributes in @secattr.
  919. * Returns zero on success and negative values on failure.
  920. *
  921. */
  922. static int calipso_opt_getattr(const unsigned char *calipso,
  923. struct netlbl_lsm_secattr *secattr)
  924. {
  925. int ret_val = -ENOMSG;
  926. u32 doi, len = calipso[1], cat_len = calipso[6] * 4;
  927. struct calipso_doi *doi_def;
  928. if (cat_len + 8 > len)
  929. return -EINVAL;
  930. if (calipso_cache_check(calipso + 2, calipso[1], secattr) == 0)
  931. return 0;
  932. doi = get_unaligned_be32(calipso + 2);
  933. rcu_read_lock();
  934. doi_def = calipso_doi_search(doi);
  935. if (!doi_def)
  936. goto getattr_return;
  937. secattr->attr.mls.lvl = calipso[7];
  938. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  939. if (cat_len) {
  940. ret_val = calipso_map_cat_ntoh(doi_def,
  941. calipso + 10,
  942. cat_len,
  943. secattr);
  944. if (ret_val != 0) {
  945. netlbl_catmap_free(secattr->attr.mls.cat);
  946. goto getattr_return;
  947. }
  948. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  949. }
  950. secattr->type = NETLBL_NLTYPE_CALIPSO;
  951. getattr_return:
  952. rcu_read_unlock();
  953. return ret_val;
  954. }
  955. /* sock functions.
  956. */
  957. /**
  958. * calipso_sock_getattr - Get the security attributes from a sock
  959. * @sk: the sock
  960. * @secattr: the security attributes
  961. *
  962. * Description:
  963. * Query @sk to see if there is a CALIPSO option attached to the sock and if
  964. * there is return the CALIPSO security attributes in @secattr. This function
  965. * requires that @sk be locked, or privately held, but it does not do any
  966. * locking itself. Returns zero on success and negative values on failure.
  967. *
  968. */
  969. static int calipso_sock_getattr(struct sock *sk,
  970. struct netlbl_lsm_secattr *secattr)
  971. {
  972. struct ipv6_opt_hdr *hop;
  973. int opt_len, len, ret_val = -ENOMSG, offset;
  974. unsigned char *opt;
  975. struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
  976. if (!txopts || !txopts->hopopt)
  977. goto done;
  978. hop = txopts->hopopt;
  979. opt = (unsigned char *)hop;
  980. opt_len = ipv6_optlen(hop);
  981. offset = sizeof(*hop);
  982. while (offset < opt_len) {
  983. len = calipso_tlv_len(hop, offset);
  984. if (len < 0) {
  985. ret_val = len;
  986. goto done;
  987. }
  988. switch (opt[offset]) {
  989. case IPV6_TLV_CALIPSO:
  990. if (len < CALIPSO_HDR_LEN)
  991. ret_val = -EINVAL;
  992. else
  993. ret_val = calipso_opt_getattr(&opt[offset],
  994. secattr);
  995. goto done;
  996. default:
  997. offset += len;
  998. break;
  999. }
  1000. }
  1001. done:
  1002. txopt_put(txopts);
  1003. return ret_val;
  1004. }
  1005. /**
  1006. * calipso_sock_setattr - Add a CALIPSO option to a socket
  1007. * @sk: the socket
  1008. * @doi_def: the CALIPSO DOI to use
  1009. * @secattr: the specific security attributes of the socket
  1010. *
  1011. * Description:
  1012. * Set the CALIPSO option on the given socket using the DOI definition and
  1013. * security attributes passed to the function. This function requires
  1014. * exclusive access to @sk, which means it either needs to be in the
  1015. * process of being created or locked. Returns zero on success and negative
  1016. * values on failure.
  1017. *
  1018. */
  1019. static int calipso_sock_setattr(struct sock *sk,
  1020. const struct calipso_doi *doi_def,
  1021. const struct netlbl_lsm_secattr *secattr)
  1022. {
  1023. int ret_val;
  1024. struct ipv6_opt_hdr *old, *new;
  1025. struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
  1026. old = NULL;
  1027. if (txopts)
  1028. old = txopts->hopopt;
  1029. new = calipso_opt_insert(old, doi_def, secattr);
  1030. txopt_put(txopts);
  1031. if (IS_ERR(new))
  1032. return PTR_ERR(new);
  1033. ret_val = calipso_opt_update(sk, new);
  1034. kfree(new);
  1035. return ret_val;
  1036. }
  1037. /**
  1038. * calipso_sock_delattr - Delete the CALIPSO option from a socket
  1039. * @sk: the socket
  1040. *
  1041. * Description:
  1042. * Removes the CALIPSO option from a socket, if present.
  1043. *
  1044. */
  1045. static void calipso_sock_delattr(struct sock *sk)
  1046. {
  1047. struct ipv6_opt_hdr *new_hop;
  1048. struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
  1049. if (!txopts || !txopts->hopopt)
  1050. goto done;
  1051. if (calipso_opt_del(txopts->hopopt, &new_hop))
  1052. goto done;
  1053. calipso_opt_update(sk, new_hop);
  1054. kfree(new_hop);
  1055. done:
  1056. txopt_put(txopts);
  1057. }
  1058. /* request sock functions.
  1059. */
  1060. /**
  1061. * calipso_req_setattr - Add a CALIPSO option to a connection request socket
  1062. * @req: the connection request socket
  1063. * @doi_def: the CALIPSO DOI to use
  1064. * @secattr: the specific security attributes of the socket
  1065. *
  1066. * Description:
  1067. * Set the CALIPSO option on the given socket using the DOI definition and
  1068. * security attributes passed to the function. Returns zero on success and
  1069. * negative values on failure.
  1070. *
  1071. */
  1072. static int calipso_req_setattr(struct request_sock *req,
  1073. const struct calipso_doi *doi_def,
  1074. const struct netlbl_lsm_secattr *secattr)
  1075. {
  1076. struct ipv6_txoptions *txopts;
  1077. struct inet_request_sock *req_inet = inet_rsk(req);
  1078. struct ipv6_opt_hdr *old, *new;
  1079. struct sock *sk = sk_to_full_sk(req_to_sk(req));
  1080. if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
  1081. old = req_inet->ipv6_opt->hopopt;
  1082. else
  1083. old = NULL;
  1084. new = calipso_opt_insert(old, doi_def, secattr);
  1085. if (IS_ERR(new))
  1086. return PTR_ERR(new);
  1087. txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
  1088. kfree(new);
  1089. if (IS_ERR(txopts))
  1090. return PTR_ERR(txopts);
  1091. txopts = xchg(&req_inet->ipv6_opt, txopts);
  1092. if (txopts) {
  1093. atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
  1094. txopt_put(txopts);
  1095. }
  1096. return 0;
  1097. }
  1098. /**
  1099. * calipso_req_delattr - Delete the CALIPSO option from a request socket
  1100. * @reg: the request socket
  1101. *
  1102. * Description:
  1103. * Removes the CALIPSO option from a request socket, if present.
  1104. *
  1105. */
  1106. static void calipso_req_delattr(struct request_sock *req)
  1107. {
  1108. struct inet_request_sock *req_inet = inet_rsk(req);
  1109. struct ipv6_opt_hdr *new;
  1110. struct ipv6_txoptions *txopts;
  1111. struct sock *sk = sk_to_full_sk(req_to_sk(req));
  1112. if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
  1113. return;
  1114. if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
  1115. return; /* Nothing to do */
  1116. txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
  1117. if (!IS_ERR(txopts)) {
  1118. txopts = xchg(&req_inet->ipv6_opt, txopts);
  1119. if (txopts) {
  1120. atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
  1121. txopt_put(txopts);
  1122. }
  1123. }
  1124. kfree(new);
  1125. }
  1126. /* skbuff functions.
  1127. */
  1128. /**
  1129. * calipso_skbuff_optptr - Find the CALIPSO option in the packet
  1130. * @skb: the packet
  1131. *
  1132. * Description:
  1133. * Parse the packet's IP header looking for a CALIPSO option. Returns a pointer
  1134. * to the start of the CALIPSO option on success, NULL if one if not found.
  1135. *
  1136. */
  1137. static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
  1138. {
  1139. const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
  1140. int offset;
  1141. if (ip6_hdr->nexthdr != NEXTHDR_HOP)
  1142. return NULL;
  1143. offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
  1144. if (offset >= 0)
  1145. return (unsigned char *)ip6_hdr + offset;
  1146. return NULL;
  1147. }
  1148. /**
  1149. * calipso_skbuff_setattr - Set the CALIPSO option on a packet
  1150. * @skb: the packet
  1151. * @doi_def: the CALIPSO DOI to use
  1152. * @secattr: the security attributes
  1153. *
  1154. * Description:
  1155. * Set the CALIPSO option on the given packet based on the security attributes.
  1156. * Returns a pointer to the IP header on success and NULL on failure.
  1157. *
  1158. */
  1159. static int calipso_skbuff_setattr(struct sk_buff *skb,
  1160. const struct calipso_doi *doi_def,
  1161. const struct netlbl_lsm_secattr *secattr)
  1162. {
  1163. int ret_val;
  1164. struct ipv6hdr *ip6_hdr;
  1165. struct ipv6_opt_hdr *hop;
  1166. unsigned char buf[CALIPSO_MAX_BUFFER];
  1167. int len_delta, new_end, pad, payload;
  1168. unsigned int start, end;
  1169. ip6_hdr = ipv6_hdr(skb);
  1170. if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
  1171. hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
  1172. ret_val = calipso_opt_find(hop, &start, &end);
  1173. if (ret_val && ret_val != -ENOENT)
  1174. return ret_val;
  1175. } else {
  1176. start = 0;
  1177. end = 0;
  1178. }
  1179. memset(buf, 0, sizeof(buf));
  1180. ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
  1181. if (ret_val < 0)
  1182. return ret_val;
  1183. new_end = start + ret_val;
  1184. /* At this point new_end aligns to 4n, so (new_end & 4) pads to 8n */
  1185. pad = ((new_end & 4) + (end & 7)) & 7;
  1186. len_delta = new_end - (int)end + pad;
  1187. ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
  1188. if (ret_val < 0)
  1189. return ret_val;
  1190. ip6_hdr = ipv6_hdr(skb); /* Reset as skb_cow() may have moved it */
  1191. if (len_delta) {
  1192. if (len_delta > 0)
  1193. skb_push(skb, len_delta);
  1194. else
  1195. skb_pull(skb, -len_delta);
  1196. memmove((char *)ip6_hdr - len_delta, ip6_hdr,
  1197. sizeof(*ip6_hdr) + start);
  1198. skb_reset_network_header(skb);
  1199. ip6_hdr = ipv6_hdr(skb);
  1200. payload = ntohs(ip6_hdr->payload_len);
  1201. ip6_hdr->payload_len = htons(payload + len_delta);
  1202. }
  1203. hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
  1204. if (start == 0) {
  1205. struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
  1206. new_hop->nexthdr = ip6_hdr->nexthdr;
  1207. new_hop->hdrlen = len_delta / 8 - 1;
  1208. ip6_hdr->nexthdr = NEXTHDR_HOP;
  1209. } else {
  1210. hop->hdrlen += len_delta / 8;
  1211. }
  1212. memcpy((char *)hop + start, buf + (start & 3), new_end - start);
  1213. calipso_pad_write((unsigned char *)hop, new_end, pad);
  1214. return 0;
  1215. }
  1216. /**
  1217. * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
  1218. * @skb: the packet
  1219. *
  1220. * Description:
  1221. * Removes any and all CALIPSO options from the given packet. Returns zero on
  1222. * success, negative values on failure.
  1223. *
  1224. */
  1225. static int calipso_skbuff_delattr(struct sk_buff *skb)
  1226. {
  1227. int ret_val;
  1228. struct ipv6hdr *ip6_hdr;
  1229. struct ipv6_opt_hdr *old_hop;
  1230. u32 old_hop_len, start = 0, end = 0, delta, size, pad;
  1231. if (!calipso_skbuff_optptr(skb))
  1232. return 0;
  1233. /* since we are changing the packet we should make a copy */
  1234. ret_val = skb_cow(skb, skb_headroom(skb));
  1235. if (ret_val < 0)
  1236. return ret_val;
  1237. ip6_hdr = ipv6_hdr(skb);
  1238. old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
  1239. old_hop_len = ipv6_optlen(old_hop);
  1240. ret_val = calipso_opt_find(old_hop, &start, &end);
  1241. if (ret_val)
  1242. return ret_val;
  1243. if (start == sizeof(*old_hop) && end == old_hop_len) {
  1244. /* There's no other option in the header so we delete
  1245. * the whole thing. */
  1246. delta = old_hop_len;
  1247. size = sizeof(*ip6_hdr);
  1248. ip6_hdr->nexthdr = old_hop->nexthdr;
  1249. } else {
  1250. delta = (end - start) & ~7;
  1251. if (delta)
  1252. old_hop->hdrlen -= delta / 8;
  1253. pad = (end - start) & 7;
  1254. size = sizeof(*ip6_hdr) + start + pad;
  1255. calipso_pad_write((unsigned char *)old_hop, start, pad);
  1256. }
  1257. if (delta) {
  1258. skb_pull(skb, delta);
  1259. memmove((char *)ip6_hdr + delta, ip6_hdr, size);
  1260. skb_reset_network_header(skb);
  1261. }
  1262. return 0;
  1263. }
  1264. static const struct netlbl_calipso_ops ops = {
  1265. .doi_add = calipso_doi_add,
  1266. .doi_free = calipso_doi_free,
  1267. .doi_remove = calipso_doi_remove,
  1268. .doi_getdef = calipso_doi_getdef,
  1269. .doi_putdef = calipso_doi_putdef,
  1270. .doi_walk = calipso_doi_walk,
  1271. .sock_getattr = calipso_sock_getattr,
  1272. .sock_setattr = calipso_sock_setattr,
  1273. .sock_delattr = calipso_sock_delattr,
  1274. .req_setattr = calipso_req_setattr,
  1275. .req_delattr = calipso_req_delattr,
  1276. .opt_getattr = calipso_opt_getattr,
  1277. .skbuff_optptr = calipso_skbuff_optptr,
  1278. .skbuff_setattr = calipso_skbuff_setattr,
  1279. .skbuff_delattr = calipso_skbuff_delattr,
  1280. .cache_invalidate = calipso_cache_invalidate,
  1281. .cache_add = calipso_cache_add
  1282. };
  1283. /**
  1284. * calipso_init - Initialize the CALIPSO module
  1285. *
  1286. * Description:
  1287. * Initialize the CALIPSO module and prepare it for use. Returns zero on
  1288. * success and negative values on failure.
  1289. *
  1290. */
  1291. int __init calipso_init(void)
  1292. {
  1293. int ret_val;
  1294. ret_val = calipso_cache_init();
  1295. if (!ret_val)
  1296. netlbl_calipso_ops_register(&ops);
  1297. return ret_val;
  1298. }
  1299. void calipso_exit(void)
  1300. {
  1301. netlbl_calipso_ops_register(NULL);
  1302. calipso_cache_invalidate();
  1303. kfree(calipso_cache);
  1304. }