netlabel_unlabeled.c 42 KB

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