netlabel_unlabeled.c 42 KB

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