netlabel_unlabeled.c 45 KB

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