cipso_ipv4.c 62 KB

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