netlabel_unlabeled.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /*
  2. * NetLabel Unlabeled Support
  3. *
  4. * This file defines functions for dealing with unlabeled packets for the
  5. * NetLabel system. The NetLabel system manages static and dynamic label
  6. * mappings for network protocols such as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/socket.h>
  34. #include <linux/string.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/audit.h>
  37. #include <linux/in.h>
  38. #include <linux/in6.h>
  39. #include <linux/ip.h>
  40. #include <linux/ipv6.h>
  41. #include <linux/notifier.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/security.h>
  44. #include <linux/slab.h>
  45. #include <net/sock.h>
  46. #include <net/netlink.h>
  47. #include <net/genetlink.h>
  48. #include <net/ip.h>
  49. #include <net/ipv6.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netlabel.h>
  52. #include <asm/bug.h>
  53. #include <asm/atomic.h>
  54. #include "netlabel_user.h"
  55. #include "netlabel_addrlist.h"
  56. #include "netlabel_domainhash.h"
  57. #include "netlabel_unlabeled.h"
  58. #include "netlabel_mgmt.h"
  59. /* NOTE: at present we always use init's network namespace since we don't
  60. * presently support different namespaces even though the majority of
  61. * the functions in this file are "namespace safe" */
  62. /* The unlabeled connection hash table which we use to map network interfaces
  63. * and addresses of unlabeled packets to a user specified secid value for the
  64. * LSM. The hash table is used to lookup the network interface entry
  65. * (struct netlbl_unlhsh_iface) and then the interface entry is used to
  66. * lookup an IP address match from an ordered list. If a network interface
  67. * match can not be found in the hash table then the default entry
  68. * (netlbl_unlhsh_def) is used. The IP address entry list
  69. * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
  70. * larger netmask come first.
  71. */
  72. struct netlbl_unlhsh_tbl {
  73. struct list_head *tbl;
  74. u32 size;
  75. };
  76. #define netlbl_unlhsh_addr4_entry(iter) \
  77. container_of(iter, struct netlbl_unlhsh_addr4, list)
  78. struct netlbl_unlhsh_addr4 {
  79. u32 secid;
  80. struct netlbl_af4list list;
  81. struct rcu_head rcu;
  82. };
  83. #define netlbl_unlhsh_addr6_entry(iter) \
  84. container_of(iter, struct netlbl_unlhsh_addr6, list)
  85. struct netlbl_unlhsh_addr6 {
  86. u32 secid;
  87. struct netlbl_af6list list;
  88. struct rcu_head rcu;
  89. };
  90. struct netlbl_unlhsh_iface {
  91. int ifindex;
  92. struct list_head addr4_list;
  93. struct list_head addr6_list;
  94. u32 valid;
  95. struct list_head list;
  96. struct rcu_head rcu;
  97. };
  98. /* Argument struct for netlbl_unlhsh_walk() */
  99. struct netlbl_unlhsh_walk_arg {
  100. struct netlink_callback *nl_cb;
  101. struct sk_buff *skb;
  102. u32 seq;
  103. };
  104. /* Unlabeled connection hash table */
  105. /* updates should be so rare that having one spinlock for the entire
  106. * hash table should be okay */
  107. static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
  108. #define netlbl_unlhsh_rcu_deref(p) \
  109. rcu_dereference_check(p, rcu_read_lock_held() || \
  110. lockdep_is_held(&netlbl_unlhsh_lock))
  111. static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
  112. static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
  113. /* Accept unlabeled packets flag */
  114. static u8 netlabel_unlabel_acceptflg = 0;
  115. /* NetLabel Generic NETLINK unlabeled family */
  116. static struct genl_family netlbl_unlabel_gnl_family = {
  117. .id = GENL_ID_GENERATE,
  118. .hdrsize = 0,
  119. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  120. .version = NETLBL_PROTO_VERSION,
  121. .maxattr = NLBL_UNLABEL_A_MAX,
  122. };
  123. /* NetLabel Netlink attribute policy */
  124. static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
  125. [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
  126. [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
  127. .len = sizeof(struct in6_addr) },
  128. [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
  129. .len = sizeof(struct in6_addr) },
  130. [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
  131. .len = sizeof(struct in_addr) },
  132. [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
  133. .len = sizeof(struct in_addr) },
  134. [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
  135. .len = IFNAMSIZ - 1 },
  136. [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
  137. };
  138. /*
  139. * Unlabeled Connection Hash Table Functions
  140. */
  141. /**
  142. * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
  143. * @entry: the entry's RCU field
  144. *
  145. * Description:
  146. * This function is designed to be used as a callback to the call_rcu()
  147. * function so that memory allocated to a hash table address entry can be
  148. * released safely.
  149. *
  150. */
  151. static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
  152. {
  153. struct netlbl_unlhsh_addr4 *ptr;
  154. ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
  155. kfree(ptr);
  156. }
  157. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  158. /**
  159. * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
  160. * @entry: the entry's RCU field
  161. *
  162. * Description:
  163. * This function is designed to be used as a callback to the call_rcu()
  164. * function so that memory allocated to a hash table address entry can be
  165. * released safely.
  166. *
  167. */
  168. static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
  169. {
  170. struct netlbl_unlhsh_addr6 *ptr;
  171. ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
  172. kfree(ptr);
  173. }
  174. #endif /* IPv6 */
  175. /**
  176. * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
  177. * @entry: the entry's RCU field
  178. *
  179. * Description:
  180. * This function is designed to be used as a callback to the call_rcu()
  181. * function so that memory allocated to a hash table interface entry can be
  182. * released safely. It is important to note that this function does not free
  183. * the IPv4 and IPv6 address lists contained as part of an interface entry. It
  184. * is up to the rest of the code to make sure an interface entry is only freed
  185. * once it's address lists are empty.
  186. *
  187. */
  188. static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
  189. {
  190. struct netlbl_unlhsh_iface *iface;
  191. struct netlbl_af4list *iter4;
  192. struct netlbl_af4list *tmp4;
  193. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  194. struct netlbl_af6list *iter6;
  195. struct netlbl_af6list *tmp6;
  196. #endif /* IPv6 */
  197. iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
  198. /* no need for locks here since we are the only one with access to this
  199. * structure */
  200. netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
  201. netlbl_af4list_remove_entry(iter4);
  202. kfree(netlbl_unlhsh_addr4_entry(iter4));
  203. }
  204. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  205. netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
  206. netlbl_af6list_remove_entry(iter6);
  207. kfree(netlbl_unlhsh_addr6_entry(iter6));
  208. }
  209. #endif /* IPv6 */
  210. kfree(iface);
  211. }
  212. /**
  213. * netlbl_unlhsh_hash - Hashing function for the hash table
  214. * @ifindex: the network interface/device to hash
  215. *
  216. * Description:
  217. * This is the hashing function for the unlabeled hash table, it returns the
  218. * bucket number for the given device/interface. The caller is responsible for
  219. * ensuring that the hash table is protected with either a RCU read lock or
  220. * the hash table lock.
  221. *
  222. */
  223. static u32 netlbl_unlhsh_hash(int ifindex)
  224. {
  225. return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
  226. }
  227. /**
  228. * netlbl_unlhsh_search_iface - Search for a matching interface entry
  229. * @ifindex: the network interface
  230. *
  231. * Description:
  232. * Searches the unlabeled connection hash table and returns a pointer to the
  233. * interface entry which matches @ifindex, otherwise NULL is returned. The
  234. * caller is responsible for ensuring that the hash table is protected with
  235. * either a RCU read lock or the hash table lock.
  236. *
  237. */
  238. static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
  239. {
  240. u32 bkt;
  241. struct list_head *bkt_list;
  242. struct netlbl_unlhsh_iface *iter;
  243. bkt = netlbl_unlhsh_hash(ifindex);
  244. bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
  245. list_for_each_entry_rcu(iter, bkt_list, list)
  246. if (iter->valid && iter->ifindex == ifindex)
  247. return iter;
  248. return NULL;
  249. }
  250. /**
  251. * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
  252. * @iface: the associated interface entry
  253. * @addr: IPv4 address in network byte order
  254. * @mask: IPv4 address mask in network byte order
  255. * @secid: LSM secid value for entry
  256. *
  257. * Description:
  258. * Add a new address entry into the unlabeled connection hash table using the
  259. * interface entry specified by @iface. On success zero is returned, otherwise
  260. * a negative value is returned.
  261. *
  262. */
  263. static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
  264. const struct in_addr *addr,
  265. const struct in_addr *mask,
  266. u32 secid)
  267. {
  268. int ret_val;
  269. struct netlbl_unlhsh_addr4 *entry;
  270. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  271. if (entry == NULL)
  272. return -ENOMEM;
  273. entry->list.addr = addr->s_addr & mask->s_addr;
  274. entry->list.mask = mask->s_addr;
  275. entry->list.valid = 1;
  276. entry->secid = secid;
  277. spin_lock(&netlbl_unlhsh_lock);
  278. ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
  279. spin_unlock(&netlbl_unlhsh_lock);
  280. if (ret_val != 0)
  281. kfree(entry);
  282. return ret_val;
  283. }
  284. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  285. /**
  286. * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
  287. * @iface: the associated interface entry
  288. * @addr: IPv6 address in network byte order
  289. * @mask: IPv6 address mask in network byte order
  290. * @secid: LSM secid value for entry
  291. *
  292. * Description:
  293. * Add a new address entry into the unlabeled connection hash table using the
  294. * interface entry specified by @iface. On success zero is returned, otherwise
  295. * a negative value is returned.
  296. *
  297. */
  298. static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
  299. const struct in6_addr *addr,
  300. const struct in6_addr *mask,
  301. u32 secid)
  302. {
  303. int ret_val;
  304. struct netlbl_unlhsh_addr6 *entry;
  305. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  306. if (entry == NULL)
  307. return -ENOMEM;
  308. ipv6_addr_copy(&entry->list.addr, addr);
  309. entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  310. entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  311. entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  312. entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
  313. ipv6_addr_copy(&entry->list.mask, mask);
  314. entry->list.valid = 1;
  315. entry->secid = secid;
  316. spin_lock(&netlbl_unlhsh_lock);
  317. ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
  318. spin_unlock(&netlbl_unlhsh_lock);
  319. if (ret_val != 0)
  320. kfree(entry);
  321. return 0;
  322. }
  323. #endif /* IPv6 */
  324. /**
  325. * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
  326. * @ifindex: network interface
  327. *
  328. * Description:
  329. * Add a new, empty, interface entry into the unlabeled connection hash table.
  330. * On success a pointer to the new interface entry is returned, on failure NULL
  331. * is returned.
  332. *
  333. */
  334. static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
  335. {
  336. u32 bkt;
  337. struct netlbl_unlhsh_iface *iface;
  338. iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
  339. if (iface == NULL)
  340. return NULL;
  341. iface->ifindex = ifindex;
  342. INIT_LIST_HEAD(&iface->addr4_list);
  343. INIT_LIST_HEAD(&iface->addr6_list);
  344. iface->valid = 1;
  345. spin_lock(&netlbl_unlhsh_lock);
  346. if (ifindex > 0) {
  347. bkt = netlbl_unlhsh_hash(ifindex);
  348. if (netlbl_unlhsh_search_iface(ifindex) != NULL)
  349. goto add_iface_failure;
  350. list_add_tail_rcu(&iface->list,
  351. &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
  352. } else {
  353. INIT_LIST_HEAD(&iface->list);
  354. if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
  355. goto add_iface_failure;
  356. rcu_assign_pointer(netlbl_unlhsh_def, iface);
  357. }
  358. spin_unlock(&netlbl_unlhsh_lock);
  359. return iface;
  360. add_iface_failure:
  361. spin_unlock(&netlbl_unlhsh_lock);
  362. kfree(iface);
  363. return NULL;
  364. }
  365. /**
  366. * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
  367. * @net: network namespace
  368. * @dev_name: interface name
  369. * @addr: IP address in network byte order
  370. * @mask: address mask in network byte order
  371. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  372. * @secid: LSM secid value for the entry
  373. * @audit_info: NetLabel audit information
  374. *
  375. * Description:
  376. * Adds a new entry to the unlabeled connection hash table. Returns zero on
  377. * success, negative values on failure.
  378. *
  379. */
  380. int netlbl_unlhsh_add(struct net *net,
  381. const char *dev_name,
  382. const void *addr,
  383. const void *mask,
  384. u32 addr_len,
  385. u32 secid,
  386. struct netlbl_audit *audit_info)
  387. {
  388. int ret_val;
  389. int ifindex;
  390. struct net_device *dev;
  391. struct netlbl_unlhsh_iface *iface;
  392. struct audit_buffer *audit_buf = NULL;
  393. char *secctx = NULL;
  394. u32 secctx_len;
  395. if (addr_len != sizeof(struct in_addr) &&
  396. addr_len != sizeof(struct in6_addr))
  397. return -EINVAL;
  398. rcu_read_lock();
  399. if (dev_name != NULL) {
  400. dev = dev_get_by_name_rcu(net, dev_name);
  401. if (dev == NULL) {
  402. ret_val = -ENODEV;
  403. goto unlhsh_add_return;
  404. }
  405. ifindex = dev->ifindex;
  406. iface = netlbl_unlhsh_search_iface(ifindex);
  407. } else {
  408. ifindex = 0;
  409. iface = rcu_dereference(netlbl_unlhsh_def);
  410. }
  411. if (iface == NULL) {
  412. iface = netlbl_unlhsh_add_iface(ifindex);
  413. if (iface == NULL) {
  414. ret_val = -ENOMEM;
  415. goto unlhsh_add_return;
  416. }
  417. }
  418. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
  419. audit_info);
  420. switch (addr_len) {
  421. case sizeof(struct in_addr): {
  422. struct in_addr *addr4, *mask4;
  423. addr4 = (struct in_addr *)addr;
  424. mask4 = (struct in_addr *)mask;
  425. ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
  426. if (audit_buf != NULL)
  427. netlbl_af4list_audit_addr(audit_buf, 1,
  428. dev_name,
  429. addr4->s_addr,
  430. mask4->s_addr);
  431. break;
  432. }
  433. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  434. case sizeof(struct in6_addr): {
  435. struct in6_addr *addr6, *mask6;
  436. addr6 = (struct in6_addr *)addr;
  437. mask6 = (struct in6_addr *)mask;
  438. ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
  439. if (audit_buf != NULL)
  440. netlbl_af6list_audit_addr(audit_buf, 1,
  441. dev_name,
  442. addr6, mask6);
  443. break;
  444. }
  445. #endif /* IPv6 */
  446. default:
  447. ret_val = -EINVAL;
  448. }
  449. if (ret_val == 0)
  450. atomic_inc(&netlabel_mgmt_protocount);
  451. unlhsh_add_return:
  452. rcu_read_unlock();
  453. if (audit_buf != NULL) {
  454. if (security_secid_to_secctx(secid,
  455. &secctx,
  456. &secctx_len) == 0) {
  457. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  458. security_release_secctx(secctx, secctx_len);
  459. }
  460. audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
  461. audit_log_end(audit_buf);
  462. }
  463. return ret_val;
  464. }
  465. /**
  466. * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
  467. * @net: network namespace
  468. * @iface: interface entry
  469. * @addr: IP address
  470. * @mask: IP address mask
  471. * @audit_info: NetLabel audit information
  472. *
  473. * Description:
  474. * Remove an IP address entry from the unlabeled connection hash table.
  475. * Returns zero on success, negative values on failure.
  476. *
  477. */
  478. static int netlbl_unlhsh_remove_addr4(struct net *net,
  479. struct netlbl_unlhsh_iface *iface,
  480. const struct in_addr *addr,
  481. const struct in_addr *mask,
  482. struct netlbl_audit *audit_info)
  483. {
  484. struct netlbl_af4list *list_entry;
  485. struct netlbl_unlhsh_addr4 *entry;
  486. struct audit_buffer *audit_buf;
  487. struct net_device *dev;
  488. char *secctx;
  489. u32 secctx_len;
  490. spin_lock(&netlbl_unlhsh_lock);
  491. list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  492. &iface->addr4_list);
  493. spin_unlock(&netlbl_unlhsh_lock);
  494. if (list_entry != NULL)
  495. entry = netlbl_unlhsh_addr4_entry(list_entry);
  496. else
  497. entry = NULL;
  498. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  499. audit_info);
  500. if (audit_buf != NULL) {
  501. dev = dev_get_by_index(net, iface->ifindex);
  502. netlbl_af4list_audit_addr(audit_buf, 1,
  503. (dev != NULL ? dev->name : NULL),
  504. addr->s_addr, mask->s_addr);
  505. if (dev != NULL)
  506. dev_put(dev);
  507. if (entry != NULL &&
  508. security_secid_to_secctx(entry->secid,
  509. &secctx, &secctx_len) == 0) {
  510. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  511. security_release_secctx(secctx, secctx_len);
  512. }
  513. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  514. audit_log_end(audit_buf);
  515. }
  516. if (entry == NULL)
  517. return -ENOENT;
  518. call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
  519. return 0;
  520. }
  521. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  522. /**
  523. * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
  524. * @net: network namespace
  525. * @iface: interface entry
  526. * @addr: IP address
  527. * @mask: IP address mask
  528. * @audit_info: NetLabel audit information
  529. *
  530. * Description:
  531. * Remove an IP address entry from the unlabeled connection hash table.
  532. * Returns zero on success, negative values on failure.
  533. *
  534. */
  535. static int netlbl_unlhsh_remove_addr6(struct net *net,
  536. struct netlbl_unlhsh_iface *iface,
  537. const struct in6_addr *addr,
  538. const struct in6_addr *mask,
  539. struct netlbl_audit *audit_info)
  540. {
  541. struct netlbl_af6list *list_entry;
  542. struct netlbl_unlhsh_addr6 *entry;
  543. struct audit_buffer *audit_buf;
  544. struct net_device *dev;
  545. char *secctx;
  546. u32 secctx_len;
  547. spin_lock(&netlbl_unlhsh_lock);
  548. list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
  549. spin_unlock(&netlbl_unlhsh_lock);
  550. if (list_entry != NULL)
  551. entry = netlbl_unlhsh_addr6_entry(list_entry);
  552. else
  553. entry = NULL;
  554. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  555. audit_info);
  556. if (audit_buf != NULL) {
  557. dev = dev_get_by_index(net, iface->ifindex);
  558. netlbl_af6list_audit_addr(audit_buf, 1,
  559. (dev != NULL ? dev->name : NULL),
  560. addr, mask);
  561. if (dev != NULL)
  562. dev_put(dev);
  563. if (entry != NULL &&
  564. security_secid_to_secctx(entry->secid,
  565. &secctx, &secctx_len) == 0) {
  566. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  567. security_release_secctx(secctx, secctx_len);
  568. }
  569. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  570. audit_log_end(audit_buf);
  571. }
  572. if (entry == NULL)
  573. return -ENOENT;
  574. call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
  575. return 0;
  576. }
  577. #endif /* IPv6 */
  578. /**
  579. * netlbl_unlhsh_condremove_iface - Remove an interface entry
  580. * @iface: the interface entry
  581. *
  582. * Description:
  583. * Remove an interface entry from the unlabeled connection hash table if it is
  584. * empty. An interface entry is considered to be empty if there are no
  585. * address entries assigned to it.
  586. *
  587. */
  588. static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
  589. {
  590. struct netlbl_af4list *iter4;
  591. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  592. struct netlbl_af6list *iter6;
  593. #endif /* IPv6 */
  594. spin_lock(&netlbl_unlhsh_lock);
  595. netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
  596. goto unlhsh_condremove_failure;
  597. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  598. netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
  599. goto unlhsh_condremove_failure;
  600. #endif /* IPv6 */
  601. iface->valid = 0;
  602. if (iface->ifindex > 0)
  603. list_del_rcu(&iface->list);
  604. else
  605. rcu_assign_pointer(netlbl_unlhsh_def, NULL);
  606. spin_unlock(&netlbl_unlhsh_lock);
  607. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  608. return;
  609. unlhsh_condremove_failure:
  610. spin_unlock(&netlbl_unlhsh_lock);
  611. return;
  612. }
  613. /**
  614. * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
  615. * @net: network namespace
  616. * @dev_name: interface name
  617. * @addr: IP address in network byte order
  618. * @mask: address mask in network byte order
  619. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  620. * @audit_info: NetLabel audit information
  621. *
  622. * Description:
  623. * Removes and existing entry from the unlabeled connection hash table.
  624. * Returns zero on success, negative values on failure.
  625. *
  626. */
  627. int netlbl_unlhsh_remove(struct net *net,
  628. const char *dev_name,
  629. const void *addr,
  630. const void *mask,
  631. u32 addr_len,
  632. struct netlbl_audit *audit_info)
  633. {
  634. int ret_val;
  635. struct net_device *dev;
  636. struct netlbl_unlhsh_iface *iface;
  637. if (addr_len != sizeof(struct in_addr) &&
  638. addr_len != sizeof(struct in6_addr))
  639. return -EINVAL;
  640. rcu_read_lock();
  641. if (dev_name != NULL) {
  642. dev = dev_get_by_name_rcu(net, dev_name);
  643. if (dev == NULL) {
  644. ret_val = -ENODEV;
  645. goto unlhsh_remove_return;
  646. }
  647. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  648. } else
  649. iface = rcu_dereference(netlbl_unlhsh_def);
  650. if (iface == NULL) {
  651. ret_val = -ENOENT;
  652. goto unlhsh_remove_return;
  653. }
  654. switch (addr_len) {
  655. case sizeof(struct in_addr):
  656. ret_val = netlbl_unlhsh_remove_addr4(net,
  657. iface, addr, mask,
  658. audit_info);
  659. break;
  660. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  661. case sizeof(struct in6_addr):
  662. ret_val = netlbl_unlhsh_remove_addr6(net,
  663. iface, addr, mask,
  664. audit_info);
  665. break;
  666. #endif /* IPv6 */
  667. default:
  668. ret_val = -EINVAL;
  669. }
  670. if (ret_val == 0) {
  671. netlbl_unlhsh_condremove_iface(iface);
  672. atomic_dec(&netlabel_mgmt_protocount);
  673. }
  674. unlhsh_remove_return:
  675. rcu_read_unlock();
  676. return ret_val;
  677. }
  678. /*
  679. * General Helper Functions
  680. */
  681. /**
  682. * netlbl_unlhsh_netdev_handler - Network device notification handler
  683. * @this: notifier block
  684. * @event: the event
  685. * @ptr: the network device (cast to void)
  686. *
  687. * Description:
  688. * Handle network device events, although at present all we care about is a
  689. * network device going away. In the case of a device going away we clear any
  690. * related entries from the unlabeled connection hash table.
  691. *
  692. */
  693. static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
  694. unsigned long event,
  695. void *ptr)
  696. {
  697. struct net_device *dev = ptr;
  698. struct netlbl_unlhsh_iface *iface = NULL;
  699. if (!net_eq(dev_net(dev), &init_net))
  700. return NOTIFY_DONE;
  701. /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
  702. if (event == NETDEV_DOWN) {
  703. spin_lock(&netlbl_unlhsh_lock);
  704. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  705. if (iface != NULL && iface->valid) {
  706. iface->valid = 0;
  707. list_del_rcu(&iface->list);
  708. } else
  709. iface = NULL;
  710. spin_unlock(&netlbl_unlhsh_lock);
  711. }
  712. if (iface != NULL)
  713. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  714. return NOTIFY_DONE;
  715. }
  716. /**
  717. * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
  718. * @value: desired value
  719. * @audit_info: NetLabel audit information
  720. *
  721. * Description:
  722. * Set the value of the unlabeled accept flag to @value.
  723. *
  724. */
  725. static void netlbl_unlabel_acceptflg_set(u8 value,
  726. struct netlbl_audit *audit_info)
  727. {
  728. struct audit_buffer *audit_buf;
  729. u8 old_val;
  730. old_val = netlabel_unlabel_acceptflg;
  731. netlabel_unlabel_acceptflg = value;
  732. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
  733. audit_info);
  734. if (audit_buf != NULL) {
  735. audit_log_format(audit_buf,
  736. " unlbl_accept=%u old=%u", value, old_val);
  737. audit_log_end(audit_buf);
  738. }
  739. }
  740. /**
  741. * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
  742. * @info: the Generic NETLINK info block
  743. * @addr: the IP address
  744. * @mask: the IP address mask
  745. * @len: the address length
  746. *
  747. * Description:
  748. * Examine the Generic NETLINK message and extract the IP address information.
  749. * Returns zero on success, negative values on failure.
  750. *
  751. */
  752. static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
  753. void **addr,
  754. void **mask,
  755. u32 *len)
  756. {
  757. u32 addr_len;
  758. if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
  759. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  760. if (addr_len != sizeof(struct in_addr) &&
  761. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
  762. return -EINVAL;
  763. *len = addr_len;
  764. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  765. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
  766. return 0;
  767. } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
  768. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  769. if (addr_len != sizeof(struct in6_addr) &&
  770. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
  771. return -EINVAL;
  772. *len = addr_len;
  773. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  774. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
  775. return 0;
  776. }
  777. return -EINVAL;
  778. }
  779. /*
  780. * NetLabel Command Handlers
  781. */
  782. /**
  783. * netlbl_unlabel_accept - Handle an ACCEPT message
  784. * @skb: the NETLINK buffer
  785. * @info: the Generic NETLINK info block
  786. *
  787. * Description:
  788. * Process a user generated ACCEPT message and set the accept flag accordingly.
  789. * Returns zero on success, negative values on failure.
  790. *
  791. */
  792. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  793. {
  794. u8 value;
  795. struct netlbl_audit audit_info;
  796. if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  797. value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
  798. if (value == 1 || value == 0) {
  799. netlbl_netlink_auditinfo(skb, &audit_info);
  800. netlbl_unlabel_acceptflg_set(value, &audit_info);
  801. return 0;
  802. }
  803. }
  804. return -EINVAL;
  805. }
  806. /**
  807. * netlbl_unlabel_list - Handle a LIST message
  808. * @skb: the NETLINK buffer
  809. * @info: the Generic NETLINK info block
  810. *
  811. * Description:
  812. * Process a user generated LIST message and respond with the current status.
  813. * Returns zero on success, negative values on failure.
  814. *
  815. */
  816. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  817. {
  818. int ret_val = -EINVAL;
  819. struct sk_buff *ans_skb;
  820. void *data;
  821. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  822. if (ans_skb == NULL)
  823. goto list_failure;
  824. data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  825. 0, NLBL_UNLABEL_C_LIST);
  826. if (data == NULL) {
  827. ret_val = -ENOMEM;
  828. goto list_failure;
  829. }
  830. ret_val = nla_put_u8(ans_skb,
  831. NLBL_UNLABEL_A_ACPTFLG,
  832. netlabel_unlabel_acceptflg);
  833. if (ret_val != 0)
  834. goto list_failure;
  835. genlmsg_end(ans_skb, data);
  836. return genlmsg_reply(ans_skb, info);
  837. list_failure:
  838. kfree_skb(ans_skb);
  839. return ret_val;
  840. }
  841. /**
  842. * netlbl_unlabel_staticadd - Handle a STATICADD message
  843. * @skb: the NETLINK buffer
  844. * @info: the Generic NETLINK info block
  845. *
  846. * Description:
  847. * Process a user generated STATICADD message and add a new unlabeled
  848. * connection entry to the hash table. Returns zero on success, negative
  849. * values on failure.
  850. *
  851. */
  852. static int netlbl_unlabel_staticadd(struct sk_buff *skb,
  853. struct genl_info *info)
  854. {
  855. int ret_val;
  856. char *dev_name;
  857. void *addr;
  858. void *mask;
  859. u32 addr_len;
  860. u32 secid;
  861. struct netlbl_audit audit_info;
  862. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  863. * single entry. However, allow users to create two entries, one each
  864. * for IPv4 and IPv4, with the same LSM security context which should
  865. * achieve the same result. */
  866. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  867. !info->attrs[NLBL_UNLABEL_A_IFACE] ||
  868. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  869. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  870. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  871. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  872. return -EINVAL;
  873. netlbl_netlink_auditinfo(skb, &audit_info);
  874. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  875. if (ret_val != 0)
  876. return ret_val;
  877. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  878. ret_val = security_secctx_to_secid(
  879. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  880. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  881. &secid);
  882. if (ret_val != 0)
  883. return ret_val;
  884. return netlbl_unlhsh_add(&init_net,
  885. dev_name, addr, mask, addr_len, secid,
  886. &audit_info);
  887. }
  888. /**
  889. * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
  890. * @skb: the NETLINK buffer
  891. * @info: the Generic NETLINK info block
  892. *
  893. * Description:
  894. * Process a user generated STATICADDDEF message and add a new default
  895. * unlabeled connection entry. Returns zero on success, negative values on
  896. * failure.
  897. *
  898. */
  899. static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
  900. struct genl_info *info)
  901. {
  902. int ret_val;
  903. void *addr;
  904. void *mask;
  905. u32 addr_len;
  906. u32 secid;
  907. struct netlbl_audit audit_info;
  908. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  909. * single entry. However, allow users to create two entries, one each
  910. * for IPv4 and IPv6, with the same LSM security context which should
  911. * achieve the same result. */
  912. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  913. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  914. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  915. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  916. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  917. return -EINVAL;
  918. netlbl_netlink_auditinfo(skb, &audit_info);
  919. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  920. if (ret_val != 0)
  921. return ret_val;
  922. ret_val = security_secctx_to_secid(
  923. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  924. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  925. &secid);
  926. if (ret_val != 0)
  927. return ret_val;
  928. return netlbl_unlhsh_add(&init_net,
  929. NULL, addr, mask, addr_len, secid,
  930. &audit_info);
  931. }
  932. /**
  933. * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
  934. * @skb: the NETLINK buffer
  935. * @info: the Generic NETLINK info block
  936. *
  937. * Description:
  938. * Process a user generated STATICREMOVE message and remove the specified
  939. * unlabeled connection entry. Returns zero on success, negative values on
  940. * failure.
  941. *
  942. */
  943. static int netlbl_unlabel_staticremove(struct sk_buff *skb,
  944. struct genl_info *info)
  945. {
  946. int ret_val;
  947. char *dev_name;
  948. void *addr;
  949. void *mask;
  950. u32 addr_len;
  951. struct netlbl_audit audit_info;
  952. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  953. * IPv4 and IPv6 in the same entry. */
  954. if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
  955. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  956. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  957. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  958. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  959. return -EINVAL;
  960. netlbl_netlink_auditinfo(skb, &audit_info);
  961. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  962. if (ret_val != 0)
  963. return ret_val;
  964. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  965. return netlbl_unlhsh_remove(&init_net,
  966. dev_name, addr, mask, addr_len,
  967. &audit_info);
  968. }
  969. /**
  970. * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
  971. * @skb: the NETLINK buffer
  972. * @info: the Generic NETLINK info block
  973. *
  974. * Description:
  975. * Process a user generated STATICREMOVEDEF message and remove the default
  976. * unlabeled connection entry. Returns zero on success, negative values on
  977. * failure.
  978. *
  979. */
  980. static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
  981. struct genl_info *info)
  982. {
  983. int ret_val;
  984. void *addr;
  985. void *mask;
  986. u32 addr_len;
  987. struct netlbl_audit audit_info;
  988. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  989. * IPv4 and IPv6 in the same entry. */
  990. if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  991. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  992. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  993. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  994. return -EINVAL;
  995. netlbl_netlink_auditinfo(skb, &audit_info);
  996. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  997. if (ret_val != 0)
  998. return ret_val;
  999. return netlbl_unlhsh_remove(&init_net,
  1000. NULL, addr, mask, addr_len,
  1001. &audit_info);
  1002. }
  1003. /**
  1004. * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
  1005. * @cmd: command/message
  1006. * @iface: the interface entry
  1007. * @addr4: the IPv4 address entry
  1008. * @addr6: the IPv6 address entry
  1009. * @arg: the netlbl_unlhsh_walk_arg structure
  1010. *
  1011. * Description:
  1012. * This function is designed to be used to generate a response for a
  1013. * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
  1014. * can be specified, not both, the other unspecified entry should be set to
  1015. * NULL by the caller. Returns the size of the message on success, negative
  1016. * values on failure.
  1017. *
  1018. */
  1019. static int netlbl_unlabel_staticlist_gen(u32 cmd,
  1020. const struct netlbl_unlhsh_iface *iface,
  1021. const struct netlbl_unlhsh_addr4 *addr4,
  1022. const struct netlbl_unlhsh_addr6 *addr6,
  1023. void *arg)
  1024. {
  1025. int ret_val = -ENOMEM;
  1026. struct netlbl_unlhsh_walk_arg *cb_arg = arg;
  1027. struct net_device *dev;
  1028. void *data;
  1029. u32 secid;
  1030. char *secctx;
  1031. u32 secctx_len;
  1032. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  1033. cb_arg->seq, &netlbl_unlabel_gnl_family,
  1034. NLM_F_MULTI, cmd);
  1035. if (data == NULL)
  1036. goto list_cb_failure;
  1037. if (iface->ifindex > 0) {
  1038. dev = dev_get_by_index(&init_net, iface->ifindex);
  1039. if (!dev) {
  1040. ret_val = -ENODEV;
  1041. goto list_cb_failure;
  1042. }
  1043. ret_val = nla_put_string(cb_arg->skb,
  1044. NLBL_UNLABEL_A_IFACE, dev->name);
  1045. dev_put(dev);
  1046. if (ret_val != 0)
  1047. goto list_cb_failure;
  1048. }
  1049. if (addr4) {
  1050. struct in_addr addr_struct;
  1051. addr_struct.s_addr = addr4->list.addr;
  1052. ret_val = nla_put(cb_arg->skb,
  1053. NLBL_UNLABEL_A_IPV4ADDR,
  1054. sizeof(struct in_addr),
  1055. &addr_struct);
  1056. if (ret_val != 0)
  1057. goto list_cb_failure;
  1058. addr_struct.s_addr = addr4->list.mask;
  1059. ret_val = nla_put(cb_arg->skb,
  1060. NLBL_UNLABEL_A_IPV4MASK,
  1061. sizeof(struct in_addr),
  1062. &addr_struct);
  1063. if (ret_val != 0)
  1064. goto list_cb_failure;
  1065. secid = addr4->secid;
  1066. } else {
  1067. ret_val = nla_put(cb_arg->skb,
  1068. NLBL_UNLABEL_A_IPV6ADDR,
  1069. sizeof(struct in6_addr),
  1070. &addr6->list.addr);
  1071. if (ret_val != 0)
  1072. goto list_cb_failure;
  1073. ret_val = nla_put(cb_arg->skb,
  1074. NLBL_UNLABEL_A_IPV6MASK,
  1075. sizeof(struct in6_addr),
  1076. &addr6->list.mask);
  1077. if (ret_val != 0)
  1078. goto list_cb_failure;
  1079. secid = addr6->secid;
  1080. }
  1081. ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
  1082. if (ret_val != 0)
  1083. goto list_cb_failure;
  1084. ret_val = nla_put(cb_arg->skb,
  1085. NLBL_UNLABEL_A_SECCTX,
  1086. secctx_len,
  1087. secctx);
  1088. security_release_secctx(secctx, secctx_len);
  1089. if (ret_val != 0)
  1090. goto list_cb_failure;
  1091. cb_arg->seq++;
  1092. return genlmsg_end(cb_arg->skb, data);
  1093. list_cb_failure:
  1094. genlmsg_cancel(cb_arg->skb, data);
  1095. return ret_val;
  1096. }
  1097. /**
  1098. * netlbl_unlabel_staticlist - Handle a STATICLIST message
  1099. * @skb: the NETLINK buffer
  1100. * @cb: the NETLINK callback
  1101. *
  1102. * Description:
  1103. * Process a user generated STATICLIST message and dump the unlabeled
  1104. * connection hash table in a form suitable for use in a kernel generated
  1105. * STATICLIST message. Returns the length of @skb.
  1106. *
  1107. */
  1108. static int netlbl_unlabel_staticlist(struct sk_buff *skb,
  1109. struct netlink_callback *cb)
  1110. {
  1111. struct netlbl_unlhsh_walk_arg cb_arg;
  1112. u32 skip_bkt = cb->args[0];
  1113. u32 skip_chain = cb->args[1];
  1114. u32 skip_addr4 = cb->args[2];
  1115. u32 skip_addr6 = cb->args[3];
  1116. u32 iter_bkt;
  1117. u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
  1118. struct netlbl_unlhsh_iface *iface;
  1119. struct list_head *iter_list;
  1120. struct netlbl_af4list *addr4;
  1121. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1122. struct netlbl_af6list *addr6;
  1123. #endif
  1124. cb_arg.nl_cb = cb;
  1125. cb_arg.skb = skb;
  1126. cb_arg.seq = cb->nlh->nlmsg_seq;
  1127. rcu_read_lock();
  1128. for (iter_bkt = skip_bkt;
  1129. iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
  1130. iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
  1131. iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
  1132. list_for_each_entry_rcu(iface, iter_list, list) {
  1133. if (!iface->valid ||
  1134. iter_chain++ < skip_chain)
  1135. continue;
  1136. netlbl_af4list_foreach_rcu(addr4,
  1137. &iface->addr4_list) {
  1138. if (iter_addr4++ < skip_addr4)
  1139. continue;
  1140. if (netlbl_unlabel_staticlist_gen(
  1141. NLBL_UNLABEL_C_STATICLIST,
  1142. iface,
  1143. netlbl_unlhsh_addr4_entry(addr4),
  1144. NULL,
  1145. &cb_arg) < 0) {
  1146. iter_addr4--;
  1147. iter_chain--;
  1148. goto unlabel_staticlist_return;
  1149. }
  1150. }
  1151. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1152. netlbl_af6list_foreach_rcu(addr6,
  1153. &iface->addr6_list) {
  1154. if (iter_addr6++ < skip_addr6)
  1155. continue;
  1156. if (netlbl_unlabel_staticlist_gen(
  1157. NLBL_UNLABEL_C_STATICLIST,
  1158. iface,
  1159. NULL,
  1160. netlbl_unlhsh_addr6_entry(addr6),
  1161. &cb_arg) < 0) {
  1162. iter_addr6--;
  1163. iter_chain--;
  1164. goto unlabel_staticlist_return;
  1165. }
  1166. }
  1167. #endif /* IPv6 */
  1168. }
  1169. }
  1170. unlabel_staticlist_return:
  1171. rcu_read_unlock();
  1172. cb->args[0] = skip_bkt;
  1173. cb->args[1] = skip_chain;
  1174. cb->args[2] = skip_addr4;
  1175. cb->args[3] = skip_addr6;
  1176. return skb->len;
  1177. }
  1178. /**
  1179. * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
  1180. * @skb: the NETLINK buffer
  1181. * @cb: the NETLINK callback
  1182. *
  1183. * Description:
  1184. * Process a user generated STATICLISTDEF message and dump the default
  1185. * unlabeled connection entry in a form suitable for use in a kernel generated
  1186. * STATICLISTDEF message. Returns the length of @skb.
  1187. *
  1188. */
  1189. static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
  1190. struct netlink_callback *cb)
  1191. {
  1192. struct netlbl_unlhsh_walk_arg cb_arg;
  1193. struct netlbl_unlhsh_iface *iface;
  1194. u32 skip_addr4 = cb->args[0];
  1195. u32 skip_addr6 = cb->args[1];
  1196. u32 iter_addr4 = 0;
  1197. struct netlbl_af4list *addr4;
  1198. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1199. u32 iter_addr6 = 0;
  1200. struct netlbl_af6list *addr6;
  1201. #endif
  1202. cb_arg.nl_cb = cb;
  1203. cb_arg.skb = skb;
  1204. cb_arg.seq = cb->nlh->nlmsg_seq;
  1205. rcu_read_lock();
  1206. iface = rcu_dereference(netlbl_unlhsh_def);
  1207. if (iface == NULL || !iface->valid)
  1208. goto unlabel_staticlistdef_return;
  1209. netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
  1210. if (iter_addr4++ < skip_addr4)
  1211. continue;
  1212. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1213. iface,
  1214. netlbl_unlhsh_addr4_entry(addr4),
  1215. NULL,
  1216. &cb_arg) < 0) {
  1217. iter_addr4--;
  1218. goto unlabel_staticlistdef_return;
  1219. }
  1220. }
  1221. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1222. netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
  1223. if (iter_addr6++ < skip_addr6)
  1224. continue;
  1225. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1226. iface,
  1227. NULL,
  1228. netlbl_unlhsh_addr6_entry(addr6),
  1229. &cb_arg) < 0) {
  1230. iter_addr6--;
  1231. goto unlabel_staticlistdef_return;
  1232. }
  1233. }
  1234. #endif /* IPv6 */
  1235. unlabel_staticlistdef_return:
  1236. rcu_read_unlock();
  1237. cb->args[0] = skip_addr4;
  1238. cb->args[1] = skip_addr6;
  1239. return skb->len;
  1240. }
  1241. /*
  1242. * NetLabel Generic NETLINK Command Definitions
  1243. */
  1244. static struct genl_ops netlbl_unlabel_genl_ops[] = {
  1245. {
  1246. .cmd = NLBL_UNLABEL_C_STATICADD,
  1247. .flags = GENL_ADMIN_PERM,
  1248. .policy = netlbl_unlabel_genl_policy,
  1249. .doit = netlbl_unlabel_staticadd,
  1250. .dumpit = NULL,
  1251. },
  1252. {
  1253. .cmd = NLBL_UNLABEL_C_STATICREMOVE,
  1254. .flags = GENL_ADMIN_PERM,
  1255. .policy = netlbl_unlabel_genl_policy,
  1256. .doit = netlbl_unlabel_staticremove,
  1257. .dumpit = NULL,
  1258. },
  1259. {
  1260. .cmd = NLBL_UNLABEL_C_STATICLIST,
  1261. .flags = 0,
  1262. .policy = netlbl_unlabel_genl_policy,
  1263. .doit = NULL,
  1264. .dumpit = netlbl_unlabel_staticlist,
  1265. },
  1266. {
  1267. .cmd = NLBL_UNLABEL_C_STATICADDDEF,
  1268. .flags = GENL_ADMIN_PERM,
  1269. .policy = netlbl_unlabel_genl_policy,
  1270. .doit = netlbl_unlabel_staticadddef,
  1271. .dumpit = NULL,
  1272. },
  1273. {
  1274. .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
  1275. .flags = GENL_ADMIN_PERM,
  1276. .policy = netlbl_unlabel_genl_policy,
  1277. .doit = netlbl_unlabel_staticremovedef,
  1278. .dumpit = NULL,
  1279. },
  1280. {
  1281. .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
  1282. .flags = 0,
  1283. .policy = netlbl_unlabel_genl_policy,
  1284. .doit = NULL,
  1285. .dumpit = netlbl_unlabel_staticlistdef,
  1286. },
  1287. {
  1288. .cmd = NLBL_UNLABEL_C_ACCEPT,
  1289. .flags = GENL_ADMIN_PERM,
  1290. .policy = netlbl_unlabel_genl_policy,
  1291. .doit = netlbl_unlabel_accept,
  1292. .dumpit = NULL,
  1293. },
  1294. {
  1295. .cmd = NLBL_UNLABEL_C_LIST,
  1296. .flags = 0,
  1297. .policy = netlbl_unlabel_genl_policy,
  1298. .doit = netlbl_unlabel_list,
  1299. .dumpit = NULL,
  1300. },
  1301. };
  1302. /*
  1303. * NetLabel Generic NETLINK Protocol Functions
  1304. */
  1305. /**
  1306. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  1307. *
  1308. * Description:
  1309. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  1310. * mechanism. Returns zero on success, negative values on failure.
  1311. *
  1312. */
  1313. int __init netlbl_unlabel_genl_init(void)
  1314. {
  1315. return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
  1316. netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
  1317. }
  1318. /*
  1319. * NetLabel KAPI Hooks
  1320. */
  1321. static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  1322. .notifier_call = netlbl_unlhsh_netdev_handler,
  1323. };
  1324. /**
  1325. * netlbl_unlabel_init - Initialize the unlabeled connection hash table
  1326. * @size: the number of bits to use for the hash buckets
  1327. *
  1328. * Description:
  1329. * Initializes the unlabeled connection hash table and registers a network
  1330. * device notification handler. This function should only be called by the
  1331. * NetLabel subsystem itself during initialization. Returns zero on success,
  1332. * non-zero values on error.
  1333. *
  1334. */
  1335. int __init netlbl_unlabel_init(u32 size)
  1336. {
  1337. u32 iter;
  1338. struct netlbl_unlhsh_tbl *hsh_tbl;
  1339. if (size == 0)
  1340. return -EINVAL;
  1341. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  1342. if (hsh_tbl == NULL)
  1343. return -ENOMEM;
  1344. hsh_tbl->size = 1 << size;
  1345. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  1346. sizeof(struct list_head),
  1347. GFP_KERNEL);
  1348. if (hsh_tbl->tbl == NULL) {
  1349. kfree(hsh_tbl);
  1350. return -ENOMEM;
  1351. }
  1352. for (iter = 0; iter < hsh_tbl->size; iter++)
  1353. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  1354. rcu_read_lock();
  1355. spin_lock(&netlbl_unlhsh_lock);
  1356. rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
  1357. spin_unlock(&netlbl_unlhsh_lock);
  1358. rcu_read_unlock();
  1359. register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
  1360. return 0;
  1361. }
  1362. /**
  1363. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  1364. * @skb: the packet
  1365. * @family: protocol family
  1366. * @secattr: the security attributes
  1367. *
  1368. * Description:
  1369. * Determine the security attributes, if any, for an unlabled packet and return
  1370. * them in @secattr. Returns zero on success and negative values on failure.
  1371. *
  1372. */
  1373. int netlbl_unlabel_getattr(const struct sk_buff *skb,
  1374. u16 family,
  1375. struct netlbl_lsm_secattr *secattr)
  1376. {
  1377. struct netlbl_unlhsh_iface *iface;
  1378. rcu_read_lock();
  1379. iface = netlbl_unlhsh_search_iface(skb->skb_iif);
  1380. if (iface == NULL)
  1381. iface = rcu_dereference(netlbl_unlhsh_def);
  1382. if (iface == NULL || !iface->valid)
  1383. goto unlabel_getattr_nolabel;
  1384. switch (family) {
  1385. case PF_INET: {
  1386. struct iphdr *hdr4;
  1387. struct netlbl_af4list *addr4;
  1388. hdr4 = ip_hdr(skb);
  1389. addr4 = netlbl_af4list_search(hdr4->saddr,
  1390. &iface->addr4_list);
  1391. if (addr4 == NULL)
  1392. goto unlabel_getattr_nolabel;
  1393. secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
  1394. break;
  1395. }
  1396. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1397. case PF_INET6: {
  1398. struct ipv6hdr *hdr6;
  1399. struct netlbl_af6list *addr6;
  1400. hdr6 = ipv6_hdr(skb);
  1401. addr6 = netlbl_af6list_search(&hdr6->saddr,
  1402. &iface->addr6_list);
  1403. if (addr6 == NULL)
  1404. goto unlabel_getattr_nolabel;
  1405. secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
  1406. break;
  1407. }
  1408. #endif /* IPv6 */
  1409. default:
  1410. goto unlabel_getattr_nolabel;
  1411. }
  1412. rcu_read_unlock();
  1413. secattr->flags |= NETLBL_SECATTR_SECID;
  1414. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1415. return 0;
  1416. unlabel_getattr_nolabel:
  1417. rcu_read_unlock();
  1418. if (netlabel_unlabel_acceptflg == 0)
  1419. return -ENOMSG;
  1420. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1421. return 0;
  1422. }
  1423. /**
  1424. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  1425. *
  1426. * Description:
  1427. * Set the default NetLabel configuration to allow incoming unlabeled packets
  1428. * and to send unlabeled network traffic by default.
  1429. *
  1430. */
  1431. int __init netlbl_unlabel_defconf(void)
  1432. {
  1433. int ret_val;
  1434. struct netlbl_dom_map *entry;
  1435. struct netlbl_audit audit_info;
  1436. /* Only the kernel is allowed to call this function and the only time
  1437. * it is called is at bootup before the audit subsystem is reporting
  1438. * messages so don't worry to much about these values. */
  1439. security_task_getsecid(current, &audit_info.secid);
  1440. audit_info.loginuid = 0;
  1441. audit_info.sessionid = 0;
  1442. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1443. if (entry == NULL)
  1444. return -ENOMEM;
  1445. entry->type = NETLBL_NLTYPE_UNLABELED;
  1446. ret_val = netlbl_domhsh_add_default(entry, &audit_info);
  1447. if (ret_val != 0)
  1448. return ret_val;
  1449. netlbl_unlabel_acceptflg_set(1, &audit_info);
  1450. return 0;
  1451. }