calipso.c 38 KB

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