cipso_ipv4.c 62 KB

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