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