netlabel_unlabeled.c 50 KB

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