netlabel_unlabeled.c 45 KB

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