calipso.c 38 KB

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