cipso_ipv4.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CIPSO - Commercial IP Security Option
  4. *
  5. * This is an implementation of the CIPSO 2.2 protocol as specified in
  6. * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
  7. * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors
  8. * have chosen to adopt the protocol and over the years it has become a
  9. * de-facto standard for labeled networking.
  10. *
  11. * The CIPSO draft specification can be found in the kernel's Documentation
  12. * directory as well as the following URL:
  13. * https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt
  14. * The FIPS-188 specification can be found at the following URL:
  15. * https://www.itl.nist.gov/fipspubs/fip188.htm
  16. *
  17. * Author: Paul Moore <[email protected]>
  18. */
  19. /*
  20. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  21. */
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/list.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/string.h>
  28. #include <linux/jhash.h>
  29. #include <linux/audit.h>
  30. #include <linux/slab.h>
  31. #include <net/ip.h>
  32. #include <net/icmp.h>
  33. #include <net/tcp.h>
  34. #include <net/netlabel.h>
  35. #include <net/cipso_ipv4.h>
  36. #include <linux/atomic.h>
  37. #include <linux/bug.h>
  38. #include <asm/unaligned.h>
  39. /* List of available DOI definitions */
  40. /* XXX - This currently assumes a minimal number of different DOIs in use,
  41. * if in practice there are a lot of different DOIs this list should
  42. * probably be turned into a hash table or something similar so we
  43. * can do quick lookups. */
  44. static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
  45. static LIST_HEAD(cipso_v4_doi_list);
  46. /* Label mapping cache */
  47. int cipso_v4_cache_enabled = 1;
  48. int cipso_v4_cache_bucketsize = 10;
  49. #define CIPSO_V4_CACHE_BUCKETBITS 7
  50. #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
  51. #define CIPSO_V4_CACHE_REORDERLIMIT 10
  52. struct cipso_v4_map_cache_bkt {
  53. spinlock_t lock;
  54. u32 size;
  55. struct list_head list;
  56. };
  57. struct cipso_v4_map_cache_entry {
  58. u32 hash;
  59. unsigned char *key;
  60. size_t key_len;
  61. struct netlbl_lsm_cache *lsm_data;
  62. u32 activity;
  63. struct list_head list;
  64. };
  65. static struct cipso_v4_map_cache_bkt *cipso_v4_cache;
  66. /* Restricted bitmap (tag #1) flags */
  67. int cipso_v4_rbm_optfmt;
  68. int cipso_v4_rbm_strictvalid = 1;
  69. /*
  70. * Protocol Constants
  71. */
  72. /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
  73. * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
  74. #define CIPSO_V4_OPT_LEN_MAX 40
  75. /* Length of the base CIPSO option, this includes the option type (1 byte), the
  76. * option length (1 byte), and the DOI (4 bytes). */
  77. #define CIPSO_V4_HDR_LEN 6
  78. /* Base length of the restrictive category bitmap tag (tag #1). */
  79. #define CIPSO_V4_TAG_RBM_BLEN 4
  80. /* Base length of the enumerated category tag (tag #2). */
  81. #define CIPSO_V4_TAG_ENUM_BLEN 4
  82. /* Base length of the ranged categories bitmap tag (tag #5). */
  83. #define CIPSO_V4_TAG_RNG_BLEN 4
  84. /* The maximum number of category ranges permitted in the ranged category tag
  85. * (tag #5). You may note that the IETF draft states that the maximum number
  86. * of category ranges is 7, but if the low end of the last category range is
  87. * zero then it is possible to fit 8 category ranges because the zero should
  88. * be omitted. */
  89. #define CIPSO_V4_TAG_RNG_CAT_MAX 8
  90. /* Base length of the local tag (non-standard tag).
  91. * Tag definition (may change between kernel versions)
  92. *
  93. * 0 8 16 24 32
  94. * +----------+----------+----------+----------+
  95. * | 10000000 | 00000110 | 32-bit secid value |
  96. * +----------+----------+----------+----------+
  97. * | in (host byte order)|
  98. * +----------+----------+
  99. *
  100. */
  101. #define CIPSO_V4_TAG_LOC_BLEN 6
  102. /*
  103. * Helper Functions
  104. */
  105. /**
  106. * cipso_v4_cache_entry_free - Frees a cache entry
  107. * @entry: the entry to free
  108. *
  109. * Description:
  110. * This function frees the memory associated with a cache entry including the
  111. * LSM cache data if there are no longer any users, i.e. reference count == 0.
  112. *
  113. */
  114. static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
  115. {
  116. if (entry->lsm_data)
  117. netlbl_secattr_cache_free(entry->lsm_data);
  118. kfree(entry->key);
  119. kfree(entry);
  120. }
  121. /**
  122. * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
  123. * @key: the hash key
  124. * @key_len: the length of the key in bytes
  125. *
  126. * Description:
  127. * The CIPSO tag hashing function. Returns a 32-bit hash value.
  128. *
  129. */
  130. static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
  131. {
  132. return jhash(key, key_len, 0);
  133. }
  134. /*
  135. * Label Mapping Cache Functions
  136. */
  137. /**
  138. * cipso_v4_cache_init - Initialize the CIPSO cache
  139. *
  140. * Description:
  141. * Initializes the CIPSO label mapping cache, this function should be called
  142. * before any of the other functions defined in this file. Returns zero on
  143. * success, negative values on error.
  144. *
  145. */
  146. static int __init cipso_v4_cache_init(void)
  147. {
  148. u32 iter;
  149. cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
  150. sizeof(struct cipso_v4_map_cache_bkt),
  151. GFP_KERNEL);
  152. if (!cipso_v4_cache)
  153. return -ENOMEM;
  154. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  155. spin_lock_init(&cipso_v4_cache[iter].lock);
  156. cipso_v4_cache[iter].size = 0;
  157. INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
  158. }
  159. return 0;
  160. }
  161. /**
  162. * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
  163. *
  164. * Description:
  165. * Invalidates and frees any entries in the CIPSO cache.
  166. *
  167. */
  168. void cipso_v4_cache_invalidate(void)
  169. {
  170. struct cipso_v4_map_cache_entry *entry, *tmp_entry;
  171. u32 iter;
  172. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  173. spin_lock_bh(&cipso_v4_cache[iter].lock);
  174. list_for_each_entry_safe(entry,
  175. tmp_entry,
  176. &cipso_v4_cache[iter].list, list) {
  177. list_del(&entry->list);
  178. cipso_v4_cache_entry_free(entry);
  179. }
  180. cipso_v4_cache[iter].size = 0;
  181. spin_unlock_bh(&cipso_v4_cache[iter].lock);
  182. }
  183. }
  184. /**
  185. * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
  186. * @key: the buffer to check
  187. * @key_len: buffer length in bytes
  188. * @secattr: the security attribute struct to use
  189. *
  190. * Description:
  191. * This function checks the cache to see if a label mapping already exists for
  192. * the given key. If there is a match then the cache is adjusted and the
  193. * @secattr struct is populated with the correct LSM security attributes. The
  194. * cache is adjusted in the following manner if the entry is not already the
  195. * first in the cache bucket:
  196. *
  197. * 1. The cache entry's activity counter is incremented
  198. * 2. The previous (higher ranking) entry's activity counter is decremented
  199. * 3. If the difference between the two activity counters is geater than
  200. * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
  201. *
  202. * Returns zero on success, -ENOENT for a cache miss, and other negative values
  203. * on error.
  204. *
  205. */
  206. static int cipso_v4_cache_check(const unsigned char *key,
  207. u32 key_len,
  208. struct netlbl_lsm_secattr *secattr)
  209. {
  210. u32 bkt;
  211. struct cipso_v4_map_cache_entry *entry;
  212. struct cipso_v4_map_cache_entry *prev_entry = NULL;
  213. u32 hash;
  214. if (!READ_ONCE(cipso_v4_cache_enabled))
  215. return -ENOENT;
  216. hash = cipso_v4_map_cache_hash(key, key_len);
  217. bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
  218. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  219. list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
  220. if (entry->hash == hash &&
  221. entry->key_len == key_len &&
  222. memcmp(entry->key, key, key_len) == 0) {
  223. entry->activity += 1;
  224. refcount_inc(&entry->lsm_data->refcount);
  225. secattr->cache = entry->lsm_data;
  226. secattr->flags |= NETLBL_SECATTR_CACHE;
  227. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  228. if (!prev_entry) {
  229. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  230. return 0;
  231. }
  232. if (prev_entry->activity > 0)
  233. prev_entry->activity -= 1;
  234. if (entry->activity > prev_entry->activity &&
  235. entry->activity - prev_entry->activity >
  236. CIPSO_V4_CACHE_REORDERLIMIT) {
  237. __list_del(entry->list.prev, entry->list.next);
  238. __list_add(&entry->list,
  239. prev_entry->list.prev,
  240. &prev_entry->list);
  241. }
  242. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  243. return 0;
  244. }
  245. prev_entry = entry;
  246. }
  247. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  248. return -ENOENT;
  249. }
  250. /**
  251. * cipso_v4_cache_add - Add an entry to the CIPSO cache
  252. * @cipso_ptr: pointer to CIPSO IP option
  253. * @secattr: the packet's security attributes
  254. *
  255. * Description:
  256. * Add a new entry into the CIPSO label mapping cache. Add the new entry to
  257. * head of the cache bucket's list, if the cache bucket is out of room remove
  258. * the last entry in the list first. It is important to note that there is
  259. * currently no checking for duplicate keys. Returns zero on success,
  260. * negative values on failure.
  261. *
  262. */
  263. int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  264. const struct netlbl_lsm_secattr *secattr)
  265. {
  266. int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize);
  267. int ret_val = -EPERM;
  268. u32 bkt;
  269. struct cipso_v4_map_cache_entry *entry = NULL;
  270. struct cipso_v4_map_cache_entry *old_entry = NULL;
  271. u32 cipso_ptr_len;
  272. if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0)
  273. return 0;
  274. cipso_ptr_len = cipso_ptr[1];
  275. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  276. if (!entry)
  277. return -ENOMEM;
  278. entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
  279. if (!entry->key) {
  280. ret_val = -ENOMEM;
  281. goto cache_add_failure;
  282. }
  283. entry->key_len = cipso_ptr_len;
  284. entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
  285. refcount_inc(&secattr->cache->refcount);
  286. entry->lsm_data = secattr->cache;
  287. bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
  288. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  289. if (cipso_v4_cache[bkt].size < bkt_size) {
  290. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  291. cipso_v4_cache[bkt].size += 1;
  292. } else {
  293. old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
  294. struct cipso_v4_map_cache_entry, list);
  295. list_del(&old_entry->list);
  296. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  297. cipso_v4_cache_entry_free(old_entry);
  298. }
  299. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  300. return 0;
  301. cache_add_failure:
  302. if (entry)
  303. cipso_v4_cache_entry_free(entry);
  304. return ret_val;
  305. }
  306. /*
  307. * DOI List Functions
  308. */
  309. /**
  310. * cipso_v4_doi_search - Searches for a DOI definition
  311. * @doi: the DOI to search for
  312. *
  313. * Description:
  314. * Search the DOI definition list for a DOI definition with a DOI value that
  315. * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
  316. * Returns a pointer to the DOI definition on success and NULL on failure.
  317. */
  318. static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
  319. {
  320. struct cipso_v4_doi *iter;
  321. list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
  322. if (iter->doi == doi && refcount_read(&iter->refcount))
  323. return iter;
  324. return NULL;
  325. }
  326. /**
  327. * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
  328. * @doi_def: the DOI structure
  329. * @audit_info: NetLabel audit information
  330. *
  331. * Description:
  332. * The caller defines a new DOI for use by the CIPSO engine and calls this
  333. * function to add it to the list of acceptable domains. The caller must
  334. * ensure that the mapping table specified in @doi_def->map meets all of the
  335. * requirements of the mapping type (see cipso_ipv4.h for details). Returns
  336. * zero on success and non-zero on failure.
  337. *
  338. */
  339. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  340. struct netlbl_audit *audit_info)
  341. {
  342. int ret_val = -EINVAL;
  343. u32 iter;
  344. u32 doi;
  345. u32 doi_type;
  346. struct audit_buffer *audit_buf;
  347. doi = doi_def->doi;
  348. doi_type = doi_def->type;
  349. if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
  350. goto doi_add_return;
  351. for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
  352. switch (doi_def->tags[iter]) {
  353. case CIPSO_V4_TAG_RBITMAP:
  354. break;
  355. case CIPSO_V4_TAG_RANGE:
  356. case CIPSO_V4_TAG_ENUM:
  357. if (doi_def->type != CIPSO_V4_MAP_PASS)
  358. goto doi_add_return;
  359. break;
  360. case CIPSO_V4_TAG_LOCAL:
  361. if (doi_def->type != CIPSO_V4_MAP_LOCAL)
  362. goto doi_add_return;
  363. break;
  364. case CIPSO_V4_TAG_INVALID:
  365. if (iter == 0)
  366. goto doi_add_return;
  367. break;
  368. default:
  369. goto doi_add_return;
  370. }
  371. }
  372. refcount_set(&doi_def->refcount, 1);
  373. spin_lock(&cipso_v4_doi_list_lock);
  374. if (cipso_v4_doi_search(doi_def->doi)) {
  375. spin_unlock(&cipso_v4_doi_list_lock);
  376. ret_val = -EEXIST;
  377. goto doi_add_return;
  378. }
  379. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  380. spin_unlock(&cipso_v4_doi_list_lock);
  381. ret_val = 0;
  382. doi_add_return:
  383. audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
  384. if (audit_buf) {
  385. const char *type_str;
  386. switch (doi_type) {
  387. case CIPSO_V4_MAP_TRANS:
  388. type_str = "trans";
  389. break;
  390. case CIPSO_V4_MAP_PASS:
  391. type_str = "pass";
  392. break;
  393. case CIPSO_V4_MAP_LOCAL:
  394. type_str = "local";
  395. break;
  396. default:
  397. type_str = "(unknown)";
  398. }
  399. audit_log_format(audit_buf,
  400. " cipso_doi=%u cipso_type=%s res=%u",
  401. doi, type_str, ret_val == 0 ? 1 : 0);
  402. audit_log_end(audit_buf);
  403. }
  404. return ret_val;
  405. }
  406. /**
  407. * cipso_v4_doi_free - Frees a DOI definition
  408. * @doi_def: the DOI definition
  409. *
  410. * Description:
  411. * This function frees all of the memory associated with a DOI definition.
  412. *
  413. */
  414. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  415. {
  416. if (!doi_def)
  417. return;
  418. switch (doi_def->type) {
  419. case CIPSO_V4_MAP_TRANS:
  420. kfree(doi_def->map.std->lvl.cipso);
  421. kfree(doi_def->map.std->lvl.local);
  422. kfree(doi_def->map.std->cat.cipso);
  423. kfree(doi_def->map.std->cat.local);
  424. kfree(doi_def->map.std);
  425. break;
  426. }
  427. kfree(doi_def);
  428. }
  429. /**
  430. * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
  431. * @entry: the entry's RCU field
  432. *
  433. * Description:
  434. * This function is designed to be used as a callback to the call_rcu()
  435. * function so that the memory allocated to the DOI definition can be released
  436. * safely.
  437. *
  438. */
  439. static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
  440. {
  441. struct cipso_v4_doi *doi_def;
  442. doi_def = container_of(entry, struct cipso_v4_doi, rcu);
  443. cipso_v4_doi_free(doi_def);
  444. }
  445. /**
  446. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  447. * @doi: the DOI value
  448. * @audit_info: NetLabel audit information
  449. *
  450. * Description:
  451. * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
  452. * be called to release their own LSM domain mappings as well as our own
  453. * domain list. Returns zero on success and negative values on failure.
  454. *
  455. */
  456. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
  457. {
  458. int ret_val;
  459. struct cipso_v4_doi *doi_def;
  460. struct audit_buffer *audit_buf;
  461. spin_lock(&cipso_v4_doi_list_lock);
  462. doi_def = cipso_v4_doi_search(doi);
  463. if (!doi_def) {
  464. spin_unlock(&cipso_v4_doi_list_lock);
  465. ret_val = -ENOENT;
  466. goto doi_remove_return;
  467. }
  468. list_del_rcu(&doi_def->list);
  469. spin_unlock(&cipso_v4_doi_list_lock);
  470. cipso_v4_doi_putdef(doi_def);
  471. ret_val = 0;
  472. doi_remove_return:
  473. audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
  474. if (audit_buf) {
  475. audit_log_format(audit_buf,
  476. " cipso_doi=%u res=%u",
  477. doi, ret_val == 0 ? 1 : 0);
  478. audit_log_end(audit_buf);
  479. }
  480. return ret_val;
  481. }
  482. /**
  483. * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
  484. * @doi: the DOI value
  485. *
  486. * Description:
  487. * Searches for a valid DOI definition and if one is found it is returned to
  488. * the caller. Otherwise NULL is returned. The caller must ensure that
  489. * rcu_read_lock() is held while accessing the returned definition and the DOI
  490. * definition reference count is decremented when the caller is done.
  491. *
  492. */
  493. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  494. {
  495. struct cipso_v4_doi *doi_def;
  496. rcu_read_lock();
  497. doi_def = cipso_v4_doi_search(doi);
  498. if (!doi_def)
  499. goto doi_getdef_return;
  500. if (!refcount_inc_not_zero(&doi_def->refcount))
  501. doi_def = NULL;
  502. doi_getdef_return:
  503. rcu_read_unlock();
  504. return doi_def;
  505. }
  506. /**
  507. * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
  508. * @doi_def: the DOI definition
  509. *
  510. * Description:
  511. * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
  512. *
  513. */
  514. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
  515. {
  516. if (!doi_def)
  517. return;
  518. if (!refcount_dec_and_test(&doi_def->refcount))
  519. return;
  520. cipso_v4_cache_invalidate();
  521. call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
  522. }
  523. /**
  524. * cipso_v4_doi_walk - Iterate through the DOI definitions
  525. * @skip_cnt: skip past this number of DOI definitions, updated
  526. * @callback: callback for each DOI definition
  527. * @cb_arg: argument for the callback function
  528. *
  529. * Description:
  530. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  531. * For each entry call @callback, if @callback returns a negative value stop
  532. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  533. * return. Returns zero on success, negative values on failure.
  534. *
  535. */
  536. int cipso_v4_doi_walk(u32 *skip_cnt,
  537. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  538. void *cb_arg)
  539. {
  540. int ret_val = -ENOENT;
  541. u32 doi_cnt = 0;
  542. struct cipso_v4_doi *iter_doi;
  543. rcu_read_lock();
  544. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  545. if (refcount_read(&iter_doi->refcount) > 0) {
  546. if (doi_cnt++ < *skip_cnt)
  547. continue;
  548. ret_val = callback(iter_doi, cb_arg);
  549. if (ret_val < 0) {
  550. doi_cnt--;
  551. goto doi_walk_return;
  552. }
  553. }
  554. doi_walk_return:
  555. rcu_read_unlock();
  556. *skip_cnt = doi_cnt;
  557. return ret_val;
  558. }
  559. /*
  560. * Label Mapping Functions
  561. */
  562. /**
  563. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  564. * @doi_def: the DOI definition
  565. * @level: the level to check
  566. *
  567. * Description:
  568. * Checks the given level against the given DOI definition and returns a
  569. * negative value if the level does not have a valid mapping and a zero value
  570. * if the level is defined by the DOI.
  571. *
  572. */
  573. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  574. {
  575. switch (doi_def->type) {
  576. case CIPSO_V4_MAP_PASS:
  577. return 0;
  578. case CIPSO_V4_MAP_TRANS:
  579. if ((level < doi_def->map.std->lvl.cipso_size) &&
  580. (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL))
  581. return 0;
  582. break;
  583. }
  584. return -EFAULT;
  585. }
  586. /**
  587. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  588. * @doi_def: the DOI definition
  589. * @host_lvl: the host MLS level
  590. * @net_lvl: the network/CIPSO MLS level
  591. *
  592. * Description:
  593. * Perform a label mapping to translate a local MLS level to the correct
  594. * CIPSO level using the given DOI definition. Returns zero on success,
  595. * negative values otherwise.
  596. *
  597. */
  598. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  599. u32 host_lvl,
  600. u32 *net_lvl)
  601. {
  602. switch (doi_def->type) {
  603. case CIPSO_V4_MAP_PASS:
  604. *net_lvl = host_lvl;
  605. return 0;
  606. case CIPSO_V4_MAP_TRANS:
  607. if (host_lvl < doi_def->map.std->lvl.local_size &&
  608. doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
  609. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  610. return 0;
  611. }
  612. return -EPERM;
  613. }
  614. return -EINVAL;
  615. }
  616. /**
  617. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  618. * @doi_def: the DOI definition
  619. * @net_lvl: the network/CIPSO MLS level
  620. * @host_lvl: the host MLS level
  621. *
  622. * Description:
  623. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  624. * level using the given DOI definition. Returns zero on success, negative
  625. * values otherwise.
  626. *
  627. */
  628. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  629. u32 net_lvl,
  630. u32 *host_lvl)
  631. {
  632. struct cipso_v4_std_map_tbl *map_tbl;
  633. switch (doi_def->type) {
  634. case CIPSO_V4_MAP_PASS:
  635. *host_lvl = net_lvl;
  636. return 0;
  637. case CIPSO_V4_MAP_TRANS:
  638. map_tbl = doi_def->map.std;
  639. if (net_lvl < map_tbl->lvl.cipso_size &&
  640. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  641. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  642. return 0;
  643. }
  644. return -EPERM;
  645. }
  646. return -EINVAL;
  647. }
  648. /**
  649. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  650. * @doi_def: the DOI definition
  651. * @bitmap: category bitmap
  652. * @bitmap_len: bitmap length in bytes
  653. *
  654. * Description:
  655. * Checks the given category bitmap against the given DOI definition and
  656. * returns a negative value if any of the categories in the bitmap do not have
  657. * a valid mapping and a zero value if all of the categories are valid.
  658. *
  659. */
  660. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  661. const unsigned char *bitmap,
  662. u32 bitmap_len)
  663. {
  664. int cat = -1;
  665. u32 bitmap_len_bits = bitmap_len * 8;
  666. u32 cipso_cat_size;
  667. u32 *cipso_array;
  668. switch (doi_def->type) {
  669. case CIPSO_V4_MAP_PASS:
  670. return 0;
  671. case CIPSO_V4_MAP_TRANS:
  672. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  673. cipso_array = doi_def->map.std->cat.cipso;
  674. for (;;) {
  675. cat = netlbl_bitmap_walk(bitmap,
  676. bitmap_len_bits,
  677. cat + 1,
  678. 1);
  679. if (cat < 0)
  680. break;
  681. if (cat >= cipso_cat_size ||
  682. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  683. return -EFAULT;
  684. }
  685. if (cat == -1)
  686. return 0;
  687. break;
  688. }
  689. return -EFAULT;
  690. }
  691. /**
  692. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  693. * @doi_def: the DOI definition
  694. * @secattr: the security attributes
  695. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  696. * @net_cat_len: the length of the CIPSO bitmap in bytes
  697. *
  698. * Description:
  699. * Perform a label mapping to translate a local MLS category bitmap to the
  700. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  701. * size in bytes of the network bitmap on success, negative values otherwise.
  702. *
  703. */
  704. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  705. const struct netlbl_lsm_secattr *secattr,
  706. unsigned char *net_cat,
  707. u32 net_cat_len)
  708. {
  709. int host_spot = -1;
  710. u32 net_spot = CIPSO_V4_INV_CAT;
  711. u32 net_spot_max = 0;
  712. u32 net_clen_bits = net_cat_len * 8;
  713. u32 host_cat_size = 0;
  714. u32 *host_cat_array = NULL;
  715. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  716. host_cat_size = doi_def->map.std->cat.local_size;
  717. host_cat_array = doi_def->map.std->cat.local;
  718. }
  719. for (;;) {
  720. host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,
  721. host_spot + 1);
  722. if (host_spot < 0)
  723. break;
  724. switch (doi_def->type) {
  725. case CIPSO_V4_MAP_PASS:
  726. net_spot = host_spot;
  727. break;
  728. case CIPSO_V4_MAP_TRANS:
  729. if (host_spot >= host_cat_size)
  730. return -EPERM;
  731. net_spot = host_cat_array[host_spot];
  732. if (net_spot >= CIPSO_V4_INV_CAT)
  733. return -EPERM;
  734. break;
  735. }
  736. if (net_spot >= net_clen_bits)
  737. return -ENOSPC;
  738. netlbl_bitmap_setbit(net_cat, net_spot, 1);
  739. if (net_spot > net_spot_max)
  740. net_spot_max = net_spot;
  741. }
  742. if (++net_spot_max % 8)
  743. return net_spot_max / 8 + 1;
  744. return net_spot_max / 8;
  745. }
  746. /**
  747. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  748. * @doi_def: the DOI definition
  749. * @net_cat: the category bitmap in network/CIPSO format
  750. * @net_cat_len: the length of the CIPSO bitmap in bytes
  751. * @secattr: the security attributes
  752. *
  753. * Description:
  754. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  755. * MLS category bitmap using the given DOI definition. Returns zero on
  756. * success, negative values on failure.
  757. *
  758. */
  759. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  760. const unsigned char *net_cat,
  761. u32 net_cat_len,
  762. struct netlbl_lsm_secattr *secattr)
  763. {
  764. int ret_val;
  765. int net_spot = -1;
  766. u32 host_spot = CIPSO_V4_INV_CAT;
  767. u32 net_clen_bits = net_cat_len * 8;
  768. u32 net_cat_size = 0;
  769. u32 *net_cat_array = NULL;
  770. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  771. net_cat_size = doi_def->map.std->cat.cipso_size;
  772. net_cat_array = doi_def->map.std->cat.cipso;
  773. }
  774. for (;;) {
  775. net_spot = netlbl_bitmap_walk(net_cat,
  776. net_clen_bits,
  777. net_spot + 1,
  778. 1);
  779. if (net_spot < 0) {
  780. if (net_spot == -2)
  781. return -EFAULT;
  782. return 0;
  783. }
  784. switch (doi_def->type) {
  785. case CIPSO_V4_MAP_PASS:
  786. host_spot = net_spot;
  787. break;
  788. case CIPSO_V4_MAP_TRANS:
  789. if (net_spot >= net_cat_size)
  790. return -EPERM;
  791. host_spot = net_cat_array[net_spot];
  792. if (host_spot >= CIPSO_V4_INV_CAT)
  793. return -EPERM;
  794. break;
  795. }
  796. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  797. host_spot,
  798. GFP_ATOMIC);
  799. if (ret_val != 0)
  800. return ret_val;
  801. }
  802. return -EINVAL;
  803. }
  804. /**
  805. * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
  806. * @doi_def: the DOI definition
  807. * @enumcat: category list
  808. * @enumcat_len: length of the category list in bytes
  809. *
  810. * Description:
  811. * Checks the given categories against the given DOI definition and returns a
  812. * negative value if any of the categories do not have a valid mapping and a
  813. * zero value if all of the categories are valid.
  814. *
  815. */
  816. static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
  817. const unsigned char *enumcat,
  818. u32 enumcat_len)
  819. {
  820. u16 cat;
  821. int cat_prev = -1;
  822. u32 iter;
  823. if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
  824. return -EFAULT;
  825. for (iter = 0; iter < enumcat_len; iter += 2) {
  826. cat = get_unaligned_be16(&enumcat[iter]);
  827. if (cat <= cat_prev)
  828. return -EFAULT;
  829. cat_prev = cat;
  830. }
  831. return 0;
  832. }
  833. /**
  834. * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
  835. * @doi_def: the DOI definition
  836. * @secattr: the security attributes
  837. * @net_cat: the zero'd out category list in network/CIPSO format
  838. * @net_cat_len: the length of the CIPSO category list in bytes
  839. *
  840. * Description:
  841. * Perform a label mapping to translate a local MLS category bitmap to the
  842. * correct CIPSO category list using the given DOI definition. Returns the
  843. * size in bytes of the network category bitmap on success, negative values
  844. * otherwise.
  845. *
  846. */
  847. static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
  848. const struct netlbl_lsm_secattr *secattr,
  849. unsigned char *net_cat,
  850. u32 net_cat_len)
  851. {
  852. int cat = -1;
  853. u32 cat_iter = 0;
  854. for (;;) {
  855. cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);
  856. if (cat < 0)
  857. break;
  858. if ((cat_iter + 2) > net_cat_len)
  859. return -ENOSPC;
  860. *((__be16 *)&net_cat[cat_iter]) = htons(cat);
  861. cat_iter += 2;
  862. }
  863. return cat_iter;
  864. }
  865. /**
  866. * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
  867. * @doi_def: the DOI definition
  868. * @net_cat: the category list in network/CIPSO format
  869. * @net_cat_len: the length of the CIPSO bitmap in bytes
  870. * @secattr: the security attributes
  871. *
  872. * Description:
  873. * Perform a label mapping to translate a CIPSO category list to the correct
  874. * local MLS category bitmap using the given DOI definition. Returns zero on
  875. * success, negative values on failure.
  876. *
  877. */
  878. static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
  879. const unsigned char *net_cat,
  880. u32 net_cat_len,
  881. struct netlbl_lsm_secattr *secattr)
  882. {
  883. int ret_val;
  884. u32 iter;
  885. for (iter = 0; iter < net_cat_len; iter += 2) {
  886. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  887. get_unaligned_be16(&net_cat[iter]),
  888. GFP_ATOMIC);
  889. if (ret_val != 0)
  890. return ret_val;
  891. }
  892. return 0;
  893. }
  894. /**
  895. * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
  896. * @doi_def: the DOI definition
  897. * @rngcat: category list
  898. * @rngcat_len: length of the category list in bytes
  899. *
  900. * Description:
  901. * Checks the given categories against the given DOI definition and returns a
  902. * negative value if any of the categories do not have a valid mapping and a
  903. * zero value if all of the categories are valid.
  904. *
  905. */
  906. static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
  907. const unsigned char *rngcat,
  908. u32 rngcat_len)
  909. {
  910. u16 cat_high;
  911. u16 cat_low;
  912. u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
  913. u32 iter;
  914. if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
  915. return -EFAULT;
  916. for (iter = 0; iter < rngcat_len; iter += 4) {
  917. cat_high = get_unaligned_be16(&rngcat[iter]);
  918. if ((iter + 4) <= rngcat_len)
  919. cat_low = get_unaligned_be16(&rngcat[iter + 2]);
  920. else
  921. cat_low = 0;
  922. if (cat_high > cat_prev)
  923. return -EFAULT;
  924. cat_prev = cat_low;
  925. }
  926. return 0;
  927. }
  928. /**
  929. * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
  930. * @doi_def: the DOI definition
  931. * @secattr: the security attributes
  932. * @net_cat: the zero'd out category list in network/CIPSO format
  933. * @net_cat_len: the length of the CIPSO category list in bytes
  934. *
  935. * Description:
  936. * Perform a label mapping to translate a local MLS category bitmap to the
  937. * correct CIPSO category list using the given DOI definition. Returns the
  938. * size in bytes of the network category bitmap on success, negative values
  939. * otherwise.
  940. *
  941. */
  942. static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
  943. const struct netlbl_lsm_secattr *secattr,
  944. unsigned char *net_cat,
  945. u32 net_cat_len)
  946. {
  947. int iter = -1;
  948. u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
  949. u32 array_cnt = 0;
  950. u32 cat_size = 0;
  951. /* make sure we don't overflow the 'array[]' variable */
  952. if (net_cat_len >
  953. (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
  954. return -ENOSPC;
  955. for (;;) {
  956. iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);
  957. if (iter < 0)
  958. break;
  959. cat_size += (iter == 0 ? 0 : sizeof(u16));
  960. if (cat_size > net_cat_len)
  961. return -ENOSPC;
  962. array[array_cnt++] = iter;
  963. iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);
  964. if (iter < 0)
  965. return -EFAULT;
  966. cat_size += sizeof(u16);
  967. if (cat_size > net_cat_len)
  968. return -ENOSPC;
  969. array[array_cnt++] = iter;
  970. }
  971. for (iter = 0; array_cnt > 0;) {
  972. *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
  973. iter += 2;
  974. array_cnt--;
  975. if (array[array_cnt] != 0) {
  976. *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
  977. iter += 2;
  978. }
  979. }
  980. return cat_size;
  981. }
  982. /**
  983. * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
  984. * @doi_def: the DOI definition
  985. * @net_cat: the category list in network/CIPSO format
  986. * @net_cat_len: the length of the CIPSO bitmap in bytes
  987. * @secattr: the security attributes
  988. *
  989. * Description:
  990. * Perform a label mapping to translate a CIPSO category list to the correct
  991. * local MLS category bitmap using the given DOI definition. Returns zero on
  992. * success, negative values on failure.
  993. *
  994. */
  995. static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
  996. const unsigned char *net_cat,
  997. u32 net_cat_len,
  998. struct netlbl_lsm_secattr *secattr)
  999. {
  1000. int ret_val;
  1001. u32 net_iter;
  1002. u16 cat_low;
  1003. u16 cat_high;
  1004. for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
  1005. cat_high = get_unaligned_be16(&net_cat[net_iter]);
  1006. if ((net_iter + 4) <= net_cat_len)
  1007. cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
  1008. else
  1009. cat_low = 0;
  1010. ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,
  1011. cat_low,
  1012. cat_high,
  1013. GFP_ATOMIC);
  1014. if (ret_val != 0)
  1015. return ret_val;
  1016. }
  1017. return 0;
  1018. }
  1019. /*
  1020. * Protocol Handling Functions
  1021. */
  1022. /**
  1023. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  1024. * @doi_def: the DOI definition
  1025. * @len: the total tag length in bytes, not including this header
  1026. * @buf: the CIPSO option buffer
  1027. *
  1028. * Description:
  1029. * Write a CIPSO header into the beginning of @buffer.
  1030. *
  1031. */
  1032. static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  1033. unsigned char *buf,
  1034. u32 len)
  1035. {
  1036. buf[0] = IPOPT_CIPSO;
  1037. buf[1] = CIPSO_V4_HDR_LEN + len;
  1038. put_unaligned_be32(doi_def->doi, &buf[2]);
  1039. }
  1040. /**
  1041. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  1042. * @doi_def: the DOI definition
  1043. * @secattr: the security attributes
  1044. * @buffer: the option buffer
  1045. * @buffer_len: length of buffer in bytes
  1046. *
  1047. * Description:
  1048. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  1049. * actual buffer length may be larger than the indicated size due to
  1050. * translation between host and network category bitmaps. Returns the size of
  1051. * the tag on success, negative values on failure.
  1052. *
  1053. */
  1054. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  1055. const struct netlbl_lsm_secattr *secattr,
  1056. unsigned char *buffer,
  1057. u32 buffer_len)
  1058. {
  1059. int ret_val;
  1060. u32 tag_len;
  1061. u32 level;
  1062. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  1063. return -EPERM;
  1064. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1065. secattr->attr.mls.lvl,
  1066. &level);
  1067. if (ret_val != 0)
  1068. return ret_val;
  1069. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1070. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  1071. secattr,
  1072. &buffer[4],
  1073. buffer_len - 4);
  1074. if (ret_val < 0)
  1075. return ret_val;
  1076. /* This will send packets using the "optimized" format when
  1077. * possible as specified in section 3.4.2.6 of the
  1078. * CIPSO draft. */
  1079. if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 &&
  1080. ret_val <= 10)
  1081. tag_len = 14;
  1082. else
  1083. tag_len = 4 + ret_val;
  1084. } else
  1085. tag_len = 4;
  1086. buffer[0] = CIPSO_V4_TAG_RBITMAP;
  1087. buffer[1] = tag_len;
  1088. buffer[3] = level;
  1089. return tag_len;
  1090. }
  1091. /**
  1092. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  1093. * @doi_def: the DOI definition
  1094. * @tag: the CIPSO tag
  1095. * @secattr: the security attributes
  1096. *
  1097. * Description:
  1098. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  1099. * attributes in @secattr. Return zero on success, negatives values on
  1100. * failure.
  1101. *
  1102. */
  1103. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  1104. const unsigned char *tag,
  1105. struct netlbl_lsm_secattr *secattr)
  1106. {
  1107. int ret_val;
  1108. u8 tag_len = tag[1];
  1109. u32 level;
  1110. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1111. if (ret_val != 0)
  1112. return ret_val;
  1113. secattr->attr.mls.lvl = level;
  1114. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1115. if (tag_len > 4) {
  1116. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  1117. &tag[4],
  1118. tag_len - 4,
  1119. secattr);
  1120. if (ret_val != 0) {
  1121. netlbl_catmap_free(secattr->attr.mls.cat);
  1122. return ret_val;
  1123. }
  1124. if (secattr->attr.mls.cat)
  1125. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1126. }
  1127. return 0;
  1128. }
  1129. /**
  1130. * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
  1131. * @doi_def: the DOI definition
  1132. * @secattr: the security attributes
  1133. * @buffer: the option buffer
  1134. * @buffer_len: length of buffer in bytes
  1135. *
  1136. * Description:
  1137. * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
  1138. * size of the tag on success, negative values on failure.
  1139. *
  1140. */
  1141. static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
  1142. const struct netlbl_lsm_secattr *secattr,
  1143. unsigned char *buffer,
  1144. u32 buffer_len)
  1145. {
  1146. int ret_val;
  1147. u32 tag_len;
  1148. u32 level;
  1149. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1150. return -EPERM;
  1151. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1152. secattr->attr.mls.lvl,
  1153. &level);
  1154. if (ret_val != 0)
  1155. return ret_val;
  1156. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1157. ret_val = cipso_v4_map_cat_enum_hton(doi_def,
  1158. secattr,
  1159. &buffer[4],
  1160. buffer_len - 4);
  1161. if (ret_val < 0)
  1162. return ret_val;
  1163. tag_len = 4 + ret_val;
  1164. } else
  1165. tag_len = 4;
  1166. buffer[0] = CIPSO_V4_TAG_ENUM;
  1167. buffer[1] = tag_len;
  1168. buffer[3] = level;
  1169. return tag_len;
  1170. }
  1171. /**
  1172. * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
  1173. * @doi_def: the DOI definition
  1174. * @tag: the CIPSO tag
  1175. * @secattr: the security attributes
  1176. *
  1177. * Description:
  1178. * Parse a CIPSO enumerated tag (tag type #2) and return the security
  1179. * attributes in @secattr. Return zero on success, negatives values on
  1180. * failure.
  1181. *
  1182. */
  1183. static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
  1184. const unsigned char *tag,
  1185. struct netlbl_lsm_secattr *secattr)
  1186. {
  1187. int ret_val;
  1188. u8 tag_len = tag[1];
  1189. u32 level;
  1190. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1191. if (ret_val != 0)
  1192. return ret_val;
  1193. secattr->attr.mls.lvl = level;
  1194. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1195. if (tag_len > 4) {
  1196. ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
  1197. &tag[4],
  1198. tag_len - 4,
  1199. secattr);
  1200. if (ret_val != 0) {
  1201. netlbl_catmap_free(secattr->attr.mls.cat);
  1202. return ret_val;
  1203. }
  1204. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1205. }
  1206. return 0;
  1207. }
  1208. /**
  1209. * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
  1210. * @doi_def: the DOI definition
  1211. * @secattr: the security attributes
  1212. * @buffer: the option buffer
  1213. * @buffer_len: length of buffer in bytes
  1214. *
  1215. * Description:
  1216. * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
  1217. * size of the tag on success, negative values on failure.
  1218. *
  1219. */
  1220. static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
  1221. const struct netlbl_lsm_secattr *secattr,
  1222. unsigned char *buffer,
  1223. u32 buffer_len)
  1224. {
  1225. int ret_val;
  1226. u32 tag_len;
  1227. u32 level;
  1228. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1229. return -EPERM;
  1230. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1231. secattr->attr.mls.lvl,
  1232. &level);
  1233. if (ret_val != 0)
  1234. return ret_val;
  1235. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1236. ret_val = cipso_v4_map_cat_rng_hton(doi_def,
  1237. secattr,
  1238. &buffer[4],
  1239. buffer_len - 4);
  1240. if (ret_val < 0)
  1241. return ret_val;
  1242. tag_len = 4 + ret_val;
  1243. } else
  1244. tag_len = 4;
  1245. buffer[0] = CIPSO_V4_TAG_RANGE;
  1246. buffer[1] = tag_len;
  1247. buffer[3] = level;
  1248. return tag_len;
  1249. }
  1250. /**
  1251. * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
  1252. * @doi_def: the DOI definition
  1253. * @tag: the CIPSO tag
  1254. * @secattr: the security attributes
  1255. *
  1256. * Description:
  1257. * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
  1258. * in @secattr. Return zero on success, negatives values on failure.
  1259. *
  1260. */
  1261. static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
  1262. const unsigned char *tag,
  1263. struct netlbl_lsm_secattr *secattr)
  1264. {
  1265. int ret_val;
  1266. u8 tag_len = tag[1];
  1267. u32 level;
  1268. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1269. if (ret_val != 0)
  1270. return ret_val;
  1271. secattr->attr.mls.lvl = level;
  1272. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1273. if (tag_len > 4) {
  1274. ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
  1275. &tag[4],
  1276. tag_len - 4,
  1277. secattr);
  1278. if (ret_val != 0) {
  1279. netlbl_catmap_free(secattr->attr.mls.cat);
  1280. return ret_val;
  1281. }
  1282. if (secattr->attr.mls.cat)
  1283. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1284. }
  1285. return 0;
  1286. }
  1287. /**
  1288. * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
  1289. * @doi_def: the DOI definition
  1290. * @secattr: the security attributes
  1291. * @buffer: the option buffer
  1292. * @buffer_len: length of buffer in bytes
  1293. *
  1294. * Description:
  1295. * Generate a CIPSO option using the local tag. Returns the size of the tag
  1296. * on success, negative values on failure.
  1297. *
  1298. */
  1299. static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
  1300. const struct netlbl_lsm_secattr *secattr,
  1301. unsigned char *buffer,
  1302. u32 buffer_len)
  1303. {
  1304. if (!(secattr->flags & NETLBL_SECATTR_SECID))
  1305. return -EPERM;
  1306. buffer[0] = CIPSO_V4_TAG_LOCAL;
  1307. buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
  1308. *(u32 *)&buffer[2] = secattr->attr.secid;
  1309. return CIPSO_V4_TAG_LOC_BLEN;
  1310. }
  1311. /**
  1312. * cipso_v4_parsetag_loc - Parse a CIPSO local tag
  1313. * @doi_def: the DOI definition
  1314. * @tag: the CIPSO tag
  1315. * @secattr: the security attributes
  1316. *
  1317. * Description:
  1318. * Parse a CIPSO local tag and return the security attributes in @secattr.
  1319. * Return zero on success, negatives values on failure.
  1320. *
  1321. */
  1322. static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
  1323. const unsigned char *tag,
  1324. struct netlbl_lsm_secattr *secattr)
  1325. {
  1326. secattr->attr.secid = *(u32 *)&tag[2];
  1327. secattr->flags |= NETLBL_SECATTR_SECID;
  1328. return 0;
  1329. }
  1330. /**
  1331. * cipso_v4_optptr - Find the CIPSO option in the packet
  1332. * @skb: the packet
  1333. *
  1334. * Description:
  1335. * Parse the packet's IP header looking for a CIPSO option. Returns a pointer
  1336. * to the start of the CIPSO option on success, NULL if one is not found.
  1337. *
  1338. */
  1339. unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
  1340. {
  1341. const struct iphdr *iph = ip_hdr(skb);
  1342. unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]);
  1343. int optlen;
  1344. int taglen;
  1345. for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) {
  1346. switch (optptr[0]) {
  1347. case IPOPT_END:
  1348. return NULL;
  1349. case IPOPT_NOOP:
  1350. taglen = 1;
  1351. break;
  1352. default:
  1353. taglen = optptr[1];
  1354. }
  1355. if (!taglen || taglen > optlen)
  1356. return NULL;
  1357. if (optptr[0] == IPOPT_CIPSO)
  1358. return optptr;
  1359. optlen -= taglen;
  1360. optptr += taglen;
  1361. }
  1362. return NULL;
  1363. }
  1364. /**
  1365. * cipso_v4_validate - Validate a CIPSO option
  1366. * @skb: the packet
  1367. * @option: the start of the option, on error it is set to point to the error
  1368. *
  1369. * Description:
  1370. * This routine is called to validate a CIPSO option, it checks all of the
  1371. * fields to ensure that they are at least valid, see the draft snippet below
  1372. * for details. If the option is valid then a zero value is returned and
  1373. * the value of @option is unchanged. If the option is invalid then a
  1374. * non-zero value is returned and @option is adjusted to point to the
  1375. * offending portion of the option. From the IETF draft ...
  1376. *
  1377. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1378. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1379. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1380. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1381. * that is unrecognized."
  1382. *
  1383. */
  1384. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
  1385. {
  1386. unsigned char *opt = *option;
  1387. unsigned char *tag;
  1388. unsigned char opt_iter;
  1389. unsigned char err_offset = 0;
  1390. u8 opt_len;
  1391. u8 tag_len;
  1392. struct cipso_v4_doi *doi_def = NULL;
  1393. u32 tag_iter;
  1394. /* caller already checks for length values that are too large */
  1395. opt_len = opt[1];
  1396. if (opt_len < 8) {
  1397. err_offset = 1;
  1398. goto validate_return;
  1399. }
  1400. rcu_read_lock();
  1401. doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
  1402. if (!doi_def) {
  1403. err_offset = 2;
  1404. goto validate_return_locked;
  1405. }
  1406. opt_iter = CIPSO_V4_HDR_LEN;
  1407. tag = opt + opt_iter;
  1408. while (opt_iter < opt_len) {
  1409. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1410. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1411. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1412. err_offset = opt_iter;
  1413. goto validate_return_locked;
  1414. }
  1415. if (opt_iter + 1 == opt_len) {
  1416. err_offset = opt_iter;
  1417. goto validate_return_locked;
  1418. }
  1419. tag_len = tag[1];
  1420. if (tag_len > (opt_len - opt_iter)) {
  1421. err_offset = opt_iter + 1;
  1422. goto validate_return_locked;
  1423. }
  1424. switch (tag[0]) {
  1425. case CIPSO_V4_TAG_RBITMAP:
  1426. if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
  1427. err_offset = opt_iter + 1;
  1428. goto validate_return_locked;
  1429. }
  1430. /* We are already going to do all the verification
  1431. * necessary at the socket layer so from our point of
  1432. * view it is safe to turn these checks off (and less
  1433. * work), however, the CIPSO draft says we should do
  1434. * all the CIPSO validations here but it doesn't
  1435. * really specify _exactly_ what we need to validate
  1436. * ... so, just make it a sysctl tunable. */
  1437. if (READ_ONCE(cipso_v4_rbm_strictvalid)) {
  1438. if (cipso_v4_map_lvl_valid(doi_def,
  1439. tag[3]) < 0) {
  1440. err_offset = opt_iter + 3;
  1441. goto validate_return_locked;
  1442. }
  1443. if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
  1444. cipso_v4_map_cat_rbm_valid(doi_def,
  1445. &tag[4],
  1446. tag_len - 4) < 0) {
  1447. err_offset = opt_iter + 4;
  1448. goto validate_return_locked;
  1449. }
  1450. }
  1451. break;
  1452. case CIPSO_V4_TAG_ENUM:
  1453. if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
  1454. err_offset = opt_iter + 1;
  1455. goto validate_return_locked;
  1456. }
  1457. if (cipso_v4_map_lvl_valid(doi_def,
  1458. tag[3]) < 0) {
  1459. err_offset = opt_iter + 3;
  1460. goto validate_return_locked;
  1461. }
  1462. if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
  1463. cipso_v4_map_cat_enum_valid(doi_def,
  1464. &tag[4],
  1465. tag_len - 4) < 0) {
  1466. err_offset = opt_iter + 4;
  1467. goto validate_return_locked;
  1468. }
  1469. break;
  1470. case CIPSO_V4_TAG_RANGE:
  1471. if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
  1472. err_offset = opt_iter + 1;
  1473. goto validate_return_locked;
  1474. }
  1475. if (cipso_v4_map_lvl_valid(doi_def,
  1476. tag[3]) < 0) {
  1477. err_offset = opt_iter + 3;
  1478. goto validate_return_locked;
  1479. }
  1480. if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
  1481. cipso_v4_map_cat_rng_valid(doi_def,
  1482. &tag[4],
  1483. tag_len - 4) < 0) {
  1484. err_offset = opt_iter + 4;
  1485. goto validate_return_locked;
  1486. }
  1487. break;
  1488. case CIPSO_V4_TAG_LOCAL:
  1489. /* This is a non-standard tag that we only allow for
  1490. * local connections, so if the incoming interface is
  1491. * not the loopback device drop the packet. Further,
  1492. * there is no legitimate reason for setting this from
  1493. * userspace so reject it if skb is NULL. */
  1494. if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {
  1495. err_offset = opt_iter;
  1496. goto validate_return_locked;
  1497. }
  1498. if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
  1499. err_offset = opt_iter + 1;
  1500. goto validate_return_locked;
  1501. }
  1502. break;
  1503. default:
  1504. err_offset = opt_iter;
  1505. goto validate_return_locked;
  1506. }
  1507. tag += tag_len;
  1508. opt_iter += tag_len;
  1509. }
  1510. validate_return_locked:
  1511. rcu_read_unlock();
  1512. validate_return:
  1513. *option = opt + err_offset;
  1514. return err_offset;
  1515. }
  1516. /**
  1517. * cipso_v4_error - Send the correct response for a bad packet
  1518. * @skb: the packet
  1519. * @error: the error code
  1520. * @gateway: CIPSO gateway flag
  1521. *
  1522. * Description:
  1523. * Based on the error code given in @error, send an ICMP error message back to
  1524. * the originating host. From the IETF draft ...
  1525. *
  1526. * "If the contents of the CIPSO [option] are valid but the security label is
  1527. * outside of the configured host or port label range, the datagram is
  1528. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1529. * returned. The code field of the ICMP is set to 'communication with
  1530. * destination network administratively prohibited' (code 9) or to
  1531. * 'communication with destination host administratively prohibited'
  1532. * (code 10). The value of the code is dependent on whether the originator
  1533. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1534. * recipient of the ICMP message MUST be able to handle either value. The
  1535. * same procedure is performed if a CIPSO [option] can not be added to an
  1536. * IP packet because it is too large to fit in the IP options area."
  1537. *
  1538. * "If the error is triggered by receipt of an ICMP message, the message is
  1539. * discarded and no response is permitted (consistent with general ICMP
  1540. * processing rules)."
  1541. *
  1542. */
  1543. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1544. {
  1545. unsigned char optbuf[sizeof(struct ip_options) + 40];
  1546. struct ip_options *opt = (struct ip_options *)optbuf;
  1547. int res;
  1548. if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
  1549. return;
  1550. /*
  1551. * We might be called above the IP layer,
  1552. * so we can not use icmp_send and IPCB here.
  1553. */
  1554. memset(opt, 0, sizeof(struct ip_options));
  1555. opt->optlen = ip_hdr(skb)->ihl*4 - sizeof(struct iphdr);
  1556. rcu_read_lock();
  1557. res = __ip_options_compile(dev_net(skb->dev), opt, skb, NULL);
  1558. rcu_read_unlock();
  1559. if (res)
  1560. return;
  1561. if (gateway)
  1562. __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, opt);
  1563. else
  1564. __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, opt);
  1565. }
  1566. /**
  1567. * cipso_v4_genopt - Generate a CIPSO option
  1568. * @buf: the option buffer
  1569. * @buf_len: the size of opt_buf
  1570. * @doi_def: the CIPSO DOI to use
  1571. * @secattr: the security attributes
  1572. *
  1573. * Description:
  1574. * Generate a CIPSO option using the DOI definition and security attributes
  1575. * passed to the function. Returns the length of the option on success and
  1576. * negative values on failure.
  1577. *
  1578. */
  1579. static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
  1580. const struct cipso_v4_doi *doi_def,
  1581. const struct netlbl_lsm_secattr *secattr)
  1582. {
  1583. int ret_val;
  1584. u32 iter;
  1585. if (buf_len <= CIPSO_V4_HDR_LEN)
  1586. return -ENOSPC;
  1587. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1588. * really a good assumption to make but since we only support the MAC
  1589. * tags right now it is a safe assumption. */
  1590. iter = 0;
  1591. do {
  1592. memset(buf, 0, buf_len);
  1593. switch (doi_def->tags[iter]) {
  1594. case CIPSO_V4_TAG_RBITMAP:
  1595. ret_val = cipso_v4_gentag_rbm(doi_def,
  1596. secattr,
  1597. &buf[CIPSO_V4_HDR_LEN],
  1598. buf_len - CIPSO_V4_HDR_LEN);
  1599. break;
  1600. case CIPSO_V4_TAG_ENUM:
  1601. ret_val = cipso_v4_gentag_enum(doi_def,
  1602. secattr,
  1603. &buf[CIPSO_V4_HDR_LEN],
  1604. buf_len - CIPSO_V4_HDR_LEN);
  1605. break;
  1606. case CIPSO_V4_TAG_RANGE:
  1607. ret_val = cipso_v4_gentag_rng(doi_def,
  1608. secattr,
  1609. &buf[CIPSO_V4_HDR_LEN],
  1610. buf_len - CIPSO_V4_HDR_LEN);
  1611. break;
  1612. case CIPSO_V4_TAG_LOCAL:
  1613. ret_val = cipso_v4_gentag_loc(doi_def,
  1614. secattr,
  1615. &buf[CIPSO_V4_HDR_LEN],
  1616. buf_len - CIPSO_V4_HDR_LEN);
  1617. break;
  1618. default:
  1619. return -EPERM;
  1620. }
  1621. iter++;
  1622. } while (ret_val < 0 &&
  1623. iter < CIPSO_V4_TAG_MAXCNT &&
  1624. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1625. if (ret_val < 0)
  1626. return ret_val;
  1627. cipso_v4_gentag_hdr(doi_def, buf, ret_val);
  1628. return CIPSO_V4_HDR_LEN + ret_val;
  1629. }
  1630. /**
  1631. * cipso_v4_sock_setattr - Add a CIPSO option to a socket
  1632. * @sk: the socket
  1633. * @doi_def: the CIPSO DOI to use
  1634. * @secattr: the specific security attributes of the socket
  1635. *
  1636. * Description:
  1637. * Set the CIPSO option on the given socket using the DOI definition and
  1638. * security attributes passed to the function. This function requires
  1639. * exclusive access to @sk, which means it either needs to be in the
  1640. * process of being created or locked. Returns zero on success and negative
  1641. * values on failure.
  1642. *
  1643. */
  1644. int cipso_v4_sock_setattr(struct sock *sk,
  1645. const struct cipso_v4_doi *doi_def,
  1646. const struct netlbl_lsm_secattr *secattr)
  1647. {
  1648. int ret_val = -EPERM;
  1649. unsigned char *buf = NULL;
  1650. u32 buf_len;
  1651. u32 opt_len;
  1652. struct ip_options_rcu *old, *opt = NULL;
  1653. struct inet_sock *sk_inet;
  1654. struct inet_connection_sock *sk_conn;
  1655. /* In the case of sock_create_lite(), the sock->sk field is not
  1656. * defined yet but it is not a problem as the only users of these
  1657. * "lite" PF_INET sockets are functions which do an accept() call
  1658. * afterwards so we will label the socket as part of the accept(). */
  1659. if (!sk)
  1660. return 0;
  1661. /* We allocate the maximum CIPSO option size here so we are probably
  1662. * being a little wasteful, but it makes our life _much_ easier later
  1663. * on and after all we are only talking about 40 bytes. */
  1664. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1665. buf = kmalloc(buf_len, GFP_ATOMIC);
  1666. if (!buf) {
  1667. ret_val = -ENOMEM;
  1668. goto socket_setattr_failure;
  1669. }
  1670. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1671. if (ret_val < 0)
  1672. goto socket_setattr_failure;
  1673. buf_len = ret_val;
  1674. /* We can't use ip_options_get() directly because it makes a call to
  1675. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1676. * we won't always have CAP_NET_RAW even though we _always_ want to
  1677. * set the IPOPT_CIPSO option. */
  1678. opt_len = (buf_len + 3) & ~3;
  1679. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1680. if (!opt) {
  1681. ret_val = -ENOMEM;
  1682. goto socket_setattr_failure;
  1683. }
  1684. memcpy(opt->opt.__data, buf, buf_len);
  1685. opt->opt.optlen = opt_len;
  1686. opt->opt.cipso = sizeof(struct iphdr);
  1687. kfree(buf);
  1688. buf = NULL;
  1689. sk_inet = inet_sk(sk);
  1690. old = rcu_dereference_protected(sk_inet->inet_opt,
  1691. lockdep_sock_is_held(sk));
  1692. if (sk_inet->is_icsk) {
  1693. sk_conn = inet_csk(sk);
  1694. if (old)
  1695. sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
  1696. sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
  1697. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1698. }
  1699. rcu_assign_pointer(sk_inet->inet_opt, opt);
  1700. if (old)
  1701. kfree_rcu(old, rcu);
  1702. return 0;
  1703. socket_setattr_failure:
  1704. kfree(buf);
  1705. kfree(opt);
  1706. return ret_val;
  1707. }
  1708. /**
  1709. * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
  1710. * @req: the connection request socket
  1711. * @doi_def: the CIPSO DOI to use
  1712. * @secattr: the specific security attributes of the socket
  1713. *
  1714. * Description:
  1715. * Set the CIPSO option on the given socket using the DOI definition and
  1716. * security attributes passed to the function. Returns zero on success and
  1717. * negative values on failure.
  1718. *
  1719. */
  1720. int cipso_v4_req_setattr(struct request_sock *req,
  1721. const struct cipso_v4_doi *doi_def,
  1722. const struct netlbl_lsm_secattr *secattr)
  1723. {
  1724. int ret_val = -EPERM;
  1725. unsigned char *buf = NULL;
  1726. u32 buf_len;
  1727. u32 opt_len;
  1728. struct ip_options_rcu *opt = NULL;
  1729. struct inet_request_sock *req_inet;
  1730. /* We allocate the maximum CIPSO option size here so we are probably
  1731. * being a little wasteful, but it makes our life _much_ easier later
  1732. * on and after all we are only talking about 40 bytes. */
  1733. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1734. buf = kmalloc(buf_len, GFP_ATOMIC);
  1735. if (!buf) {
  1736. ret_val = -ENOMEM;
  1737. goto req_setattr_failure;
  1738. }
  1739. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1740. if (ret_val < 0)
  1741. goto req_setattr_failure;
  1742. buf_len = ret_val;
  1743. /* We can't use ip_options_get() directly because it makes a call to
  1744. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1745. * we won't always have CAP_NET_RAW even though we _always_ want to
  1746. * set the IPOPT_CIPSO option. */
  1747. opt_len = (buf_len + 3) & ~3;
  1748. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1749. if (!opt) {
  1750. ret_val = -ENOMEM;
  1751. goto req_setattr_failure;
  1752. }
  1753. memcpy(opt->opt.__data, buf, buf_len);
  1754. opt->opt.optlen = opt_len;
  1755. opt->opt.cipso = sizeof(struct iphdr);
  1756. kfree(buf);
  1757. buf = NULL;
  1758. req_inet = inet_rsk(req);
  1759. opt = xchg((__force struct ip_options_rcu **)&req_inet->ireq_opt, opt);
  1760. if (opt)
  1761. kfree_rcu(opt, rcu);
  1762. return 0;
  1763. req_setattr_failure:
  1764. kfree(buf);
  1765. kfree(opt);
  1766. return ret_val;
  1767. }
  1768. /**
  1769. * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
  1770. * @opt_ptr: IP option pointer
  1771. *
  1772. * Description:
  1773. * Deletes the CIPSO IP option from a set of IP options and makes the necessary
  1774. * adjustments to the IP option structure. Returns zero on success, negative
  1775. * values on failure.
  1776. *
  1777. */
  1778. static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr)
  1779. {
  1780. struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1);
  1781. int hdr_delta = 0;
  1782. if (!opt || opt->opt.cipso == 0)
  1783. return 0;
  1784. if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
  1785. u8 cipso_len;
  1786. u8 cipso_off;
  1787. unsigned char *cipso_ptr;
  1788. int iter;
  1789. int optlen_new;
  1790. cipso_off = opt->opt.cipso - sizeof(struct iphdr);
  1791. cipso_ptr = &opt->opt.__data[cipso_off];
  1792. cipso_len = cipso_ptr[1];
  1793. if (opt->opt.srr > opt->opt.cipso)
  1794. opt->opt.srr -= cipso_len;
  1795. if (opt->opt.rr > opt->opt.cipso)
  1796. opt->opt.rr -= cipso_len;
  1797. if (opt->opt.ts > opt->opt.cipso)
  1798. opt->opt.ts -= cipso_len;
  1799. if (opt->opt.router_alert > opt->opt.cipso)
  1800. opt->opt.router_alert -= cipso_len;
  1801. opt->opt.cipso = 0;
  1802. memmove(cipso_ptr, cipso_ptr + cipso_len,
  1803. opt->opt.optlen - cipso_off - cipso_len);
  1804. /* determining the new total option length is tricky because of
  1805. * the padding necessary, the only thing i can think to do at
  1806. * this point is walk the options one-by-one, skipping the
  1807. * padding at the end to determine the actual option size and
  1808. * from there we can determine the new total option length */
  1809. iter = 0;
  1810. optlen_new = 0;
  1811. while (iter < opt->opt.optlen)
  1812. if (opt->opt.__data[iter] != IPOPT_NOP) {
  1813. iter += opt->opt.__data[iter + 1];
  1814. optlen_new = iter;
  1815. } else
  1816. iter++;
  1817. hdr_delta = opt->opt.optlen;
  1818. opt->opt.optlen = (optlen_new + 3) & ~3;
  1819. hdr_delta -= opt->opt.optlen;
  1820. } else {
  1821. /* only the cipso option was present on the socket so we can
  1822. * remove the entire option struct */
  1823. *opt_ptr = NULL;
  1824. hdr_delta = opt->opt.optlen;
  1825. kfree_rcu(opt, rcu);
  1826. }
  1827. return hdr_delta;
  1828. }
  1829. /**
  1830. * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
  1831. * @sk: the socket
  1832. *
  1833. * Description:
  1834. * Removes the CIPSO option from a socket, if present.
  1835. *
  1836. */
  1837. void cipso_v4_sock_delattr(struct sock *sk)
  1838. {
  1839. struct inet_sock *sk_inet;
  1840. int hdr_delta;
  1841. sk_inet = inet_sk(sk);
  1842. hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
  1843. if (sk_inet->is_icsk && hdr_delta > 0) {
  1844. struct inet_connection_sock *sk_conn = inet_csk(sk);
  1845. sk_conn->icsk_ext_hdr_len -= hdr_delta;
  1846. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1847. }
  1848. }
  1849. /**
  1850. * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
  1851. * @req: the request socket
  1852. *
  1853. * Description:
  1854. * Removes the CIPSO option from a request socket, if present.
  1855. *
  1856. */
  1857. void cipso_v4_req_delattr(struct request_sock *req)
  1858. {
  1859. cipso_v4_delopt(&inet_rsk(req)->ireq_opt);
  1860. }
  1861. /**
  1862. * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
  1863. * @cipso: the CIPSO v4 option
  1864. * @secattr: the security attributes
  1865. *
  1866. * Description:
  1867. * Inspect @cipso and return the security attributes in @secattr. Returns zero
  1868. * on success and negative values on failure.
  1869. *
  1870. */
  1871. int cipso_v4_getattr(const unsigned char *cipso,
  1872. struct netlbl_lsm_secattr *secattr)
  1873. {
  1874. int ret_val = -ENOMSG;
  1875. u32 doi;
  1876. struct cipso_v4_doi *doi_def;
  1877. if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
  1878. return 0;
  1879. doi = get_unaligned_be32(&cipso[2]);
  1880. rcu_read_lock();
  1881. doi_def = cipso_v4_doi_search(doi);
  1882. if (!doi_def)
  1883. goto getattr_return;
  1884. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1885. * really a good assumption to make but since we only support the MAC
  1886. * tags right now it is a safe assumption. */
  1887. switch (cipso[6]) {
  1888. case CIPSO_V4_TAG_RBITMAP:
  1889. ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
  1890. break;
  1891. case CIPSO_V4_TAG_ENUM:
  1892. ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
  1893. break;
  1894. case CIPSO_V4_TAG_RANGE:
  1895. ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
  1896. break;
  1897. case CIPSO_V4_TAG_LOCAL:
  1898. ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
  1899. break;
  1900. }
  1901. if (ret_val == 0)
  1902. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  1903. getattr_return:
  1904. rcu_read_unlock();
  1905. return ret_val;
  1906. }
  1907. /**
  1908. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1909. * @sk: the sock
  1910. * @secattr: the security attributes
  1911. *
  1912. * Description:
  1913. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1914. * there is return the CIPSO security attributes in @secattr. This function
  1915. * requires that @sk be locked, or privately held, but it does not do any
  1916. * locking itself. Returns zero on success and negative values on failure.
  1917. *
  1918. */
  1919. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1920. {
  1921. struct ip_options_rcu *opt;
  1922. int res = -ENOMSG;
  1923. rcu_read_lock();
  1924. opt = rcu_dereference(inet_sk(sk)->inet_opt);
  1925. if (opt && opt->opt.cipso)
  1926. res = cipso_v4_getattr(opt->opt.__data +
  1927. opt->opt.cipso -
  1928. sizeof(struct iphdr),
  1929. secattr);
  1930. rcu_read_unlock();
  1931. return res;
  1932. }
  1933. /**
  1934. * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
  1935. * @skb: the packet
  1936. * @doi_def: the DOI structure
  1937. * @secattr: the security attributes
  1938. *
  1939. * Description:
  1940. * Set the CIPSO option on the given packet based on the security attributes.
  1941. * Returns a pointer to the IP header on success and NULL on failure.
  1942. *
  1943. */
  1944. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  1945. const struct cipso_v4_doi *doi_def,
  1946. const struct netlbl_lsm_secattr *secattr)
  1947. {
  1948. int ret_val;
  1949. struct iphdr *iph;
  1950. struct ip_options *opt = &IPCB(skb)->opt;
  1951. unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
  1952. u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
  1953. u32 opt_len;
  1954. int len_delta;
  1955. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1956. if (ret_val < 0)
  1957. return ret_val;
  1958. buf_len = ret_val;
  1959. opt_len = (buf_len + 3) & ~3;
  1960. /* we overwrite any existing options to ensure that we have enough
  1961. * room for the CIPSO option, the reason is that we _need_ to guarantee
  1962. * that the security label is applied to the packet - we do the same
  1963. * thing when using the socket options and it hasn't caused a problem,
  1964. * if we need to we can always revisit this choice later */
  1965. len_delta = opt_len - opt->optlen;
  1966. /* if we don't ensure enough headroom we could panic on the skb_push()
  1967. * call below so make sure we have enough, we are also "mangling" the
  1968. * packet so we should probably do a copy-on-write call anyway */
  1969. ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
  1970. if (ret_val < 0)
  1971. return ret_val;
  1972. if (len_delta > 0) {
  1973. /* we assume that the header + opt->optlen have already been
  1974. * "pushed" in ip_options_build() or similar */
  1975. iph = ip_hdr(skb);
  1976. skb_push(skb, len_delta);
  1977. memmove((char *)iph - len_delta, iph, iph->ihl << 2);
  1978. skb_reset_network_header(skb);
  1979. iph = ip_hdr(skb);
  1980. } else if (len_delta < 0) {
  1981. iph = ip_hdr(skb);
  1982. memset(iph + 1, IPOPT_NOP, opt->optlen);
  1983. } else
  1984. iph = ip_hdr(skb);
  1985. if (opt->optlen > 0)
  1986. memset(opt, 0, sizeof(*opt));
  1987. opt->optlen = opt_len;
  1988. opt->cipso = sizeof(struct iphdr);
  1989. opt->is_changed = 1;
  1990. /* we have to do the following because we are being called from a
  1991. * netfilter hook which means the packet already has had the header
  1992. * fields populated and the checksum calculated - yes this means we
  1993. * are doing more work than needed but we do it to keep the core
  1994. * stack clean and tidy */
  1995. memcpy(iph + 1, buf, buf_len);
  1996. if (opt_len > buf_len)
  1997. memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
  1998. if (len_delta != 0) {
  1999. iph->ihl = 5 + (opt_len >> 2);
  2000. iph->tot_len = htons(skb->len);
  2001. }
  2002. ip_send_check(iph);
  2003. return 0;
  2004. }
  2005. /**
  2006. * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
  2007. * @skb: the packet
  2008. *
  2009. * Description:
  2010. * Removes any and all CIPSO options from the given packet. Returns zero on
  2011. * success, negative values on failure.
  2012. *
  2013. */
  2014. int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  2015. {
  2016. int ret_val;
  2017. struct iphdr *iph;
  2018. struct ip_options *opt = &IPCB(skb)->opt;
  2019. unsigned char *cipso_ptr;
  2020. if (opt->cipso == 0)
  2021. return 0;
  2022. /* since we are changing the packet we should make a copy */
  2023. ret_val = skb_cow(skb, skb_headroom(skb));
  2024. if (ret_val < 0)
  2025. return ret_val;
  2026. /* the easiest thing to do is just replace the cipso option with noop
  2027. * options since we don't change the size of the packet, although we
  2028. * still need to recalculate the checksum */
  2029. iph = ip_hdr(skb);
  2030. cipso_ptr = (unsigned char *)iph + opt->cipso;
  2031. memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
  2032. opt->cipso = 0;
  2033. opt->is_changed = 1;
  2034. ip_send_check(iph);
  2035. return 0;
  2036. }
  2037. /*
  2038. * Setup Functions
  2039. */
  2040. /**
  2041. * cipso_v4_init - Initialize the CIPSO module
  2042. *
  2043. * Description:
  2044. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  2045. * and negative values on failure.
  2046. *
  2047. */
  2048. static int __init cipso_v4_init(void)
  2049. {
  2050. int ret_val;
  2051. ret_val = cipso_v4_cache_init();
  2052. if (ret_val != 0)
  2053. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  2054. ret_val);
  2055. return 0;
  2056. }
  2057. subsys_initcall(cipso_v4_init);