calipso.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. atomic_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. atomic_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 && atomic_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. atomic_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 (!atomic_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 (!atomic_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 (!atomic_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 (atomic_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_kern(sk, old, IPV6_HOPOPTS,
  717. hop, hop ? ipv6_optlen(hop) : 0);
  718. txopt_put(old);
  719. if (IS_ERR(txopts))
  720. return PTR_ERR(txopts);
  721. txopts = ipv6_update_options(sk, txopts);
  722. if (txopts) {
  723. atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
  724. txopt_put(txopts);
  725. }
  726. return 0;
  727. }
  728. /**
  729. * calipso_tlv_len - Returns the length of the TLV
  730. * @opt: the option header
  731. * @offset: offset of the TLV within the header
  732. *
  733. * Description:
  734. * Returns the length of the TLV option at offset @offset within
  735. * the option header @opt. Checks that the entire TLV fits inside
  736. * the option header, returns a negative value if this is not the case.
  737. */
  738. static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
  739. {
  740. unsigned char *tlv = (unsigned char *)opt;
  741. unsigned int opt_len = ipv6_optlen(opt), tlv_len;
  742. if (offset < sizeof(*opt) || offset >= opt_len)
  743. return -EINVAL;
  744. if (tlv[offset] == IPV6_TLV_PAD1)
  745. return 1;
  746. if (offset + 1 >= opt_len)
  747. return -EINVAL;
  748. tlv_len = tlv[offset + 1] + 2;
  749. if (offset + tlv_len > opt_len)
  750. return -EINVAL;
  751. return tlv_len;
  752. }
  753. /**
  754. * calipso_opt_find - Finds the CALIPSO option in an IPv6 hop options header
  755. * @hop: the hop options header
  756. * @start: on return holds the offset of any leading padding
  757. * @end: on return holds the offset of the first non-pad TLV after CALIPSO
  758. *
  759. * Description:
  760. * Finds the space occupied by a CALIPSO option (including any leading and
  761. * trailing padding).
  762. *
  763. * If a CALIPSO option exists set @start and @end to the
  764. * offsets within @hop of the start of padding before the first
  765. * CALIPSO option and the end of padding after the first CALIPSO
  766. * option. In this case the function returns 0.
  767. *
  768. * In the absence of a CALIPSO option, @start and @end will be
  769. * set to the start and end of any trailing padding in the header.
  770. * This is useful when appending a new option, as the caller may want
  771. * to overwrite some of this padding. In this case the function will
  772. * return -ENOENT.
  773. */
  774. static int calipso_opt_find(struct ipv6_opt_hdr *hop, unsigned int *start,
  775. unsigned int *end)
  776. {
  777. int ret_val = -ENOENT, tlv_len;
  778. unsigned int opt_len, offset, offset_s = 0, offset_e = 0;
  779. unsigned char *opt = (unsigned char *)hop;
  780. opt_len = ipv6_optlen(hop);
  781. offset = sizeof(*hop);
  782. while (offset < opt_len) {
  783. tlv_len = calipso_tlv_len(hop, offset);
  784. if (tlv_len < 0)
  785. return tlv_len;
  786. switch (opt[offset]) {
  787. case IPV6_TLV_PAD1:
  788. case IPV6_TLV_PADN:
  789. if (offset_e)
  790. offset_e = offset;
  791. break;
  792. case IPV6_TLV_CALIPSO:
  793. ret_val = 0;
  794. offset_e = offset;
  795. break;
  796. default:
  797. if (offset_e == 0)
  798. offset_s = offset;
  799. else
  800. goto out;
  801. }
  802. offset += tlv_len;
  803. }
  804. out:
  805. if (offset_s)
  806. *start = offset_s + calipso_tlv_len(hop, offset_s);
  807. else
  808. *start = sizeof(*hop);
  809. if (offset_e)
  810. *end = offset_e + calipso_tlv_len(hop, offset_e);
  811. else
  812. *end = opt_len;
  813. return ret_val;
  814. }
  815. /**
  816. * calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr
  817. * @hop: the original hop options header
  818. * @doi_def: the CALIPSO DOI to use
  819. * @secattr: the specific security attributes of the socket
  820. *
  821. * Description:
  822. * Creates a new hop options header based on @hop with a
  823. * CALIPSO option added to it. If @hop already contains a CALIPSO
  824. * option this is overwritten, otherwise the new option is appended
  825. * after any existing options. If @hop is NULL then the new header
  826. * will contain just the CALIPSO option and any needed padding.
  827. *
  828. */
  829. static struct ipv6_opt_hdr *
  830. calipso_opt_insert(struct ipv6_opt_hdr *hop,
  831. const struct calipso_doi *doi_def,
  832. const struct netlbl_lsm_secattr *secattr)
  833. {
  834. unsigned int start, end, buf_len, pad, hop_len;
  835. struct ipv6_opt_hdr *new;
  836. int ret_val;
  837. if (hop) {
  838. hop_len = ipv6_optlen(hop);
  839. ret_val = calipso_opt_find(hop, &start, &end);
  840. if (ret_val && ret_val != -ENOENT)
  841. return ERR_PTR(ret_val);
  842. } else {
  843. hop_len = 0;
  844. start = sizeof(*hop);
  845. end = 0;
  846. }
  847. buf_len = hop_len + start - end + CALIPSO_OPT_LEN_MAX_WITH_PAD;
  848. new = kzalloc(buf_len, GFP_ATOMIC);
  849. if (!new)
  850. return ERR_PTR(-ENOMEM);
  851. if (start > sizeof(*hop))
  852. memcpy(new, hop, start);
  853. ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
  854. secattr);
  855. if (ret_val < 0) {
  856. kfree(new);
  857. return ERR_PTR(ret_val);
  858. }
  859. buf_len = start + ret_val;
  860. /* At this point buf_len aligns to 4n, so (buf_len & 4) pads to 8n */
  861. pad = ((buf_len & 4) + (end & 7)) & 7;
  862. calipso_pad_write((unsigned char *)new, buf_len, pad);
  863. buf_len += pad;
  864. if (end != hop_len) {
  865. memcpy((char *)new + buf_len, (char *)hop + end, hop_len - end);
  866. buf_len += hop_len - end;
  867. }
  868. new->nexthdr = 0;
  869. new->hdrlen = buf_len / 8 - 1;
  870. return new;
  871. }
  872. /**
  873. * calipso_opt_del - Removes the CALIPSO option from an option header
  874. * @hop: the original header
  875. * @new: the new header
  876. *
  877. * Description:
  878. * Creates a new header based on @hop without any CALIPSO option. If @hop
  879. * doesn't contain a CALIPSO option it returns -ENOENT. If @hop contains
  880. * no other non-padding options, it returns zero with @new set to NULL.
  881. * Otherwise it returns zero, creates a new header without the CALIPSO
  882. * option (and removing as much padding as possible) and returns with
  883. * @new set to that header.
  884. *
  885. */
  886. static int calipso_opt_del(struct ipv6_opt_hdr *hop,
  887. struct ipv6_opt_hdr **new)
  888. {
  889. int ret_val;
  890. unsigned int start, end, delta, pad, hop_len;
  891. ret_val = calipso_opt_find(hop, &start, &end);
  892. if (ret_val)
  893. return ret_val;
  894. hop_len = ipv6_optlen(hop);
  895. if (start == sizeof(*hop) && end == hop_len) {
  896. /* There's no other option in the header so return NULL */
  897. *new = NULL;
  898. return 0;
  899. }
  900. delta = (end - start) & ~7;
  901. *new = kzalloc(hop_len - delta, GFP_ATOMIC);
  902. if (!*new)
  903. return -ENOMEM;
  904. memcpy(*new, hop, start);
  905. (*new)->hdrlen -= delta / 8;
  906. pad = (end - start) & 7;
  907. calipso_pad_write((unsigned char *)*new, start, pad);
  908. if (end != hop_len)
  909. memcpy((char *)*new + start + pad, (char *)hop + end,
  910. hop_len - end);
  911. return 0;
  912. }
  913. /**
  914. * calipso_opt_getattr - Get the security attributes from a memory block
  915. * @calipso: the CALIPSO option
  916. * @secattr: the security attributes
  917. *
  918. * Description:
  919. * Inspect @calipso and return the security attributes in @secattr.
  920. * Returns zero on success and negative values on failure.
  921. *
  922. */
  923. static int calipso_opt_getattr(const unsigned char *calipso,
  924. struct netlbl_lsm_secattr *secattr)
  925. {
  926. int ret_val = -ENOMSG;
  927. u32 doi, len = calipso[1], cat_len = calipso[6] * 4;
  928. struct calipso_doi *doi_def;
  929. if (cat_len + 8 > len)
  930. return -EINVAL;
  931. if (calipso_cache_check(calipso + 2, calipso[1], secattr) == 0)
  932. return 0;
  933. doi = get_unaligned_be32(calipso + 2);
  934. rcu_read_lock();
  935. doi_def = calipso_doi_search(doi);
  936. if (!doi_def)
  937. goto getattr_return;
  938. secattr->attr.mls.lvl = calipso[7];
  939. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  940. if (cat_len) {
  941. ret_val = calipso_map_cat_ntoh(doi_def,
  942. calipso + 10,
  943. cat_len,
  944. secattr);
  945. if (ret_val != 0) {
  946. netlbl_catmap_free(secattr->attr.mls.cat);
  947. goto getattr_return;
  948. }
  949. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  950. }
  951. secattr->type = NETLBL_NLTYPE_CALIPSO;
  952. getattr_return:
  953. rcu_read_unlock();
  954. return ret_val;
  955. }
  956. /* sock functions.
  957. */
  958. /**
  959. * calipso_sock_getattr - Get the security attributes from a sock
  960. * @sk: the sock
  961. * @secattr: the security attributes
  962. *
  963. * Description:
  964. * Query @sk to see if there is a CALIPSO option attached to the sock and if
  965. * there is return the CALIPSO security attributes in @secattr. This function
  966. * requires that @sk be locked, or privately held, but it does not do any
  967. * locking itself. Returns zero on success and negative values on failure.
  968. *
  969. */
  970. static int calipso_sock_getattr(struct sock *sk,
  971. struct netlbl_lsm_secattr *secattr)
  972. {
  973. struct ipv6_opt_hdr *hop;
  974. int opt_len, len, ret_val = -ENOMSG, offset;
  975. unsigned char *opt;
  976. struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
  977. if (!txopts || !txopts->hopopt)
  978. goto done;
  979. hop = txopts->hopopt;
  980. opt = (unsigned char *)hop;
  981. opt_len = ipv6_optlen(hop);
  982. offset = sizeof(*hop);
  983. while (offset < opt_len) {
  984. len = calipso_tlv_len(hop, offset);
  985. if (len < 0) {
  986. ret_val = len;
  987. goto done;
  988. }
  989. switch (opt[offset]) {
  990. case IPV6_TLV_CALIPSO:
  991. if (len < CALIPSO_HDR_LEN)
  992. ret_val = -EINVAL;
  993. else
  994. ret_val = calipso_opt_getattr(&opt[offset],
  995. secattr);
  996. goto done;
  997. default:
  998. offset += len;
  999. break;
  1000. }
  1001. }
  1002. done:
  1003. txopt_put(txopts);
  1004. return ret_val;
  1005. }
  1006. /**
  1007. * calipso_sock_setattr - Add a CALIPSO option to a socket
  1008. * @sk: the socket
  1009. * @doi_def: the CALIPSO DOI to use
  1010. * @secattr: the specific security attributes of the socket
  1011. *
  1012. * Description:
  1013. * Set the CALIPSO option on the given socket using the DOI definition and
  1014. * security attributes passed to the function. This function requires
  1015. * exclusive access to @sk, which means it either needs to be in the
  1016. * process of being created or locked. Returns zero on success and negative
  1017. * values on failure.
  1018. *
  1019. */
  1020. static int calipso_sock_setattr(struct sock *sk,
  1021. const struct calipso_doi *doi_def,
  1022. const struct netlbl_lsm_secattr *secattr)
  1023. {
  1024. int ret_val;
  1025. struct ipv6_opt_hdr *old, *new;
  1026. struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
  1027. old = NULL;
  1028. if (txopts)
  1029. old = txopts->hopopt;
  1030. new = calipso_opt_insert(old, doi_def, secattr);
  1031. txopt_put(txopts);
  1032. if (IS_ERR(new))
  1033. return PTR_ERR(new);
  1034. ret_val = calipso_opt_update(sk, new);
  1035. kfree(new);
  1036. return ret_val;
  1037. }
  1038. /**
  1039. * calipso_sock_delattr - Delete the CALIPSO option from a socket
  1040. * @sk: the socket
  1041. *
  1042. * Description:
  1043. * Removes the CALIPSO option from a socket, if present.
  1044. *
  1045. */
  1046. static void calipso_sock_delattr(struct sock *sk)
  1047. {
  1048. struct ipv6_opt_hdr *new_hop;
  1049. struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
  1050. if (!txopts || !txopts->hopopt)
  1051. goto done;
  1052. if (calipso_opt_del(txopts->hopopt, &new_hop))
  1053. goto done;
  1054. calipso_opt_update(sk, new_hop);
  1055. kfree(new_hop);
  1056. done:
  1057. txopt_put(txopts);
  1058. }
  1059. /* request sock functions.
  1060. */
  1061. /**
  1062. * calipso_req_setattr - Add a CALIPSO option to a connection request socket
  1063. * @req: the connection request socket
  1064. * @doi_def: the CALIPSO DOI to use
  1065. * @secattr: the specific security attributes of the socket
  1066. *
  1067. * Description:
  1068. * Set the CALIPSO option on the given socket using the DOI definition and
  1069. * security attributes passed to the function. Returns zero on success and
  1070. * negative values on failure.
  1071. *
  1072. */
  1073. static int calipso_req_setattr(struct request_sock *req,
  1074. const struct calipso_doi *doi_def,
  1075. const struct netlbl_lsm_secattr *secattr)
  1076. {
  1077. struct ipv6_txoptions *txopts;
  1078. struct inet_request_sock *req_inet = inet_rsk(req);
  1079. struct ipv6_opt_hdr *old, *new;
  1080. struct sock *sk = sk_to_full_sk(req_to_sk(req));
  1081. if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
  1082. old = req_inet->ipv6_opt->hopopt;
  1083. else
  1084. old = NULL;
  1085. new = calipso_opt_insert(old, doi_def, secattr);
  1086. if (IS_ERR(new))
  1087. return PTR_ERR(new);
  1088. txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
  1089. new, new ? ipv6_optlen(new) : 0);
  1090. kfree(new);
  1091. if (IS_ERR(txopts))
  1092. return PTR_ERR(txopts);
  1093. txopts = xchg(&req_inet->ipv6_opt, txopts);
  1094. if (txopts) {
  1095. atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
  1096. txopt_put(txopts);
  1097. }
  1098. return 0;
  1099. }
  1100. /**
  1101. * calipso_req_delattr - Delete the CALIPSO option from a request socket
  1102. * @reg: the request socket
  1103. *
  1104. * Description:
  1105. * Removes the CALIPSO option from a request socket, if present.
  1106. *
  1107. */
  1108. static void calipso_req_delattr(struct request_sock *req)
  1109. {
  1110. struct inet_request_sock *req_inet = inet_rsk(req);
  1111. struct ipv6_opt_hdr *new;
  1112. struct ipv6_txoptions *txopts;
  1113. struct sock *sk = sk_to_full_sk(req_to_sk(req));
  1114. if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
  1115. return;
  1116. if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
  1117. return; /* Nothing to do */
  1118. txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
  1119. new, new ? ipv6_optlen(new) : 0);
  1120. if (!IS_ERR(txopts)) {
  1121. txopts = xchg(&req_inet->ipv6_opt, txopts);
  1122. if (txopts) {
  1123. atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
  1124. txopt_put(txopts);
  1125. }
  1126. }
  1127. kfree(new);
  1128. }
  1129. /* skbuff functions.
  1130. */
  1131. /**
  1132. * calipso_skbuff_optptr - Find the CALIPSO option in the packet
  1133. * @skb: the packet
  1134. *
  1135. * Description:
  1136. * Parse the packet's IP header looking for a CALIPSO option. Returns a pointer
  1137. * to the start of the CALIPSO option on success, NULL if one if not found.
  1138. *
  1139. */
  1140. static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
  1141. {
  1142. const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
  1143. int offset;
  1144. if (ip6_hdr->nexthdr != NEXTHDR_HOP)
  1145. return NULL;
  1146. offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
  1147. if (offset >= 0)
  1148. return (unsigned char *)ip6_hdr + offset;
  1149. return NULL;
  1150. }
  1151. /**
  1152. * calipso_skbuff_setattr - Set the CALIPSO option on a packet
  1153. * @skb: the packet
  1154. * @doi_def: the CALIPSO DOI to use
  1155. * @secattr: the security attributes
  1156. *
  1157. * Description:
  1158. * Set the CALIPSO option on the given packet based on the security attributes.
  1159. * Returns a pointer to the IP header on success and NULL on failure.
  1160. *
  1161. */
  1162. static int calipso_skbuff_setattr(struct sk_buff *skb,
  1163. const struct calipso_doi *doi_def,
  1164. const struct netlbl_lsm_secattr *secattr)
  1165. {
  1166. int ret_val;
  1167. struct ipv6hdr *ip6_hdr;
  1168. struct ipv6_opt_hdr *hop;
  1169. unsigned char buf[CALIPSO_MAX_BUFFER];
  1170. int len_delta, new_end, pad, payload;
  1171. unsigned int start, end;
  1172. ip6_hdr = ipv6_hdr(skb);
  1173. if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
  1174. hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
  1175. ret_val = calipso_opt_find(hop, &start, &end);
  1176. if (ret_val && ret_val != -ENOENT)
  1177. return ret_val;
  1178. } else {
  1179. start = 0;
  1180. end = 0;
  1181. }
  1182. memset(buf, 0, sizeof(buf));
  1183. ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
  1184. if (ret_val < 0)
  1185. return ret_val;
  1186. new_end = start + ret_val;
  1187. /* At this point new_end aligns to 4n, so (new_end & 4) pads to 8n */
  1188. pad = ((new_end & 4) + (end & 7)) & 7;
  1189. len_delta = new_end - (int)end + pad;
  1190. ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
  1191. if (ret_val < 0)
  1192. return ret_val;
  1193. ip6_hdr = ipv6_hdr(skb); /* Reset as skb_cow() may have moved it */
  1194. if (len_delta) {
  1195. if (len_delta > 0)
  1196. skb_push(skb, len_delta);
  1197. else
  1198. skb_pull(skb, -len_delta);
  1199. memmove((char *)ip6_hdr - len_delta, ip6_hdr,
  1200. sizeof(*ip6_hdr) + start);
  1201. skb_reset_network_header(skb);
  1202. ip6_hdr = ipv6_hdr(skb);
  1203. payload = ntohs(ip6_hdr->payload_len);
  1204. ip6_hdr->payload_len = htons(payload + len_delta);
  1205. }
  1206. hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
  1207. if (start == 0) {
  1208. struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
  1209. new_hop->nexthdr = ip6_hdr->nexthdr;
  1210. new_hop->hdrlen = len_delta / 8 - 1;
  1211. ip6_hdr->nexthdr = NEXTHDR_HOP;
  1212. } else {
  1213. hop->hdrlen += len_delta / 8;
  1214. }
  1215. memcpy((char *)hop + start, buf + (start & 3), new_end - start);
  1216. calipso_pad_write((unsigned char *)hop, new_end, pad);
  1217. return 0;
  1218. }
  1219. /**
  1220. * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
  1221. * @skb: the packet
  1222. *
  1223. * Description:
  1224. * Removes any and all CALIPSO options from the given packet. Returns zero on
  1225. * success, negative values on failure.
  1226. *
  1227. */
  1228. static int calipso_skbuff_delattr(struct sk_buff *skb)
  1229. {
  1230. int ret_val;
  1231. struct ipv6hdr *ip6_hdr;
  1232. struct ipv6_opt_hdr *old_hop;
  1233. u32 old_hop_len, start = 0, end = 0, delta, size, pad;
  1234. if (!calipso_skbuff_optptr(skb))
  1235. return 0;
  1236. /* since we are changing the packet we should make a copy */
  1237. ret_val = skb_cow(skb, skb_headroom(skb));
  1238. if (ret_val < 0)
  1239. return ret_val;
  1240. ip6_hdr = ipv6_hdr(skb);
  1241. old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
  1242. old_hop_len = ipv6_optlen(old_hop);
  1243. ret_val = calipso_opt_find(old_hop, &start, &end);
  1244. if (ret_val)
  1245. return ret_val;
  1246. if (start == sizeof(*old_hop) && end == old_hop_len) {
  1247. /* There's no other option in the header so we delete
  1248. * the whole thing. */
  1249. delta = old_hop_len;
  1250. size = sizeof(*ip6_hdr);
  1251. ip6_hdr->nexthdr = old_hop->nexthdr;
  1252. } else {
  1253. delta = (end - start) & ~7;
  1254. if (delta)
  1255. old_hop->hdrlen -= delta / 8;
  1256. pad = (end - start) & 7;
  1257. size = sizeof(*ip6_hdr) + start + pad;
  1258. calipso_pad_write((unsigned char *)old_hop, start, pad);
  1259. }
  1260. if (delta) {
  1261. skb_pull(skb, delta);
  1262. memmove((char *)ip6_hdr + delta, ip6_hdr, size);
  1263. skb_reset_network_header(skb);
  1264. }
  1265. return 0;
  1266. }
  1267. static const struct netlbl_calipso_ops ops = {
  1268. .doi_add = calipso_doi_add,
  1269. .doi_free = calipso_doi_free,
  1270. .doi_remove = calipso_doi_remove,
  1271. .doi_getdef = calipso_doi_getdef,
  1272. .doi_putdef = calipso_doi_putdef,
  1273. .doi_walk = calipso_doi_walk,
  1274. .sock_getattr = calipso_sock_getattr,
  1275. .sock_setattr = calipso_sock_setattr,
  1276. .sock_delattr = calipso_sock_delattr,
  1277. .req_setattr = calipso_req_setattr,
  1278. .req_delattr = calipso_req_delattr,
  1279. .opt_getattr = calipso_opt_getattr,
  1280. .skbuff_optptr = calipso_skbuff_optptr,
  1281. .skbuff_setattr = calipso_skbuff_setattr,
  1282. .skbuff_delattr = calipso_skbuff_delattr,
  1283. .cache_invalidate = calipso_cache_invalidate,
  1284. .cache_add = calipso_cache_add
  1285. };
  1286. /**
  1287. * calipso_init - Initialize the CALIPSO module
  1288. *
  1289. * Description:
  1290. * Initialize the CALIPSO module and prepare it for use. Returns zero on
  1291. * success and negative values on failure.
  1292. *
  1293. */
  1294. int __init calipso_init(void)
  1295. {
  1296. int ret_val;
  1297. ret_val = calipso_cache_init();
  1298. if (!ret_val)
  1299. netlbl_calipso_ops_register(&ops);
  1300. return ret_val;
  1301. }
  1302. void calipso_exit(void)
  1303. {
  1304. netlbl_calipso_ops_register(NULL);
  1305. calipso_cache_invalidate();
  1306. kfree(calipso_cache);
  1307. }