calipso.c 38 KB

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