1*d15c345fSPaul Moore /* 2*d15c345fSPaul Moore * NetLabel NETLINK Interface 3*d15c345fSPaul Moore * 4*d15c345fSPaul Moore * This file defines the NETLINK interface for the NetLabel system. The 5*d15c345fSPaul Moore * NetLabel system manages static and dynamic label mappings for network 6*d15c345fSPaul Moore * protocols such as CIPSO and RIPSO. 7*d15c345fSPaul Moore * 8*d15c345fSPaul Moore * Author: Paul Moore <paul.moore@hp.com> 9*d15c345fSPaul Moore * 10*d15c345fSPaul Moore */ 11*d15c345fSPaul Moore 12*d15c345fSPaul Moore /* 13*d15c345fSPaul Moore * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 14*d15c345fSPaul Moore * 15*d15c345fSPaul Moore * This program is free software; you can redistribute it and/or modify 16*d15c345fSPaul Moore * it under the terms of the GNU General Public License as published by 17*d15c345fSPaul Moore * the Free Software Foundation; either version 2 of the License, or 18*d15c345fSPaul Moore * (at your option) any later version. 19*d15c345fSPaul Moore * 20*d15c345fSPaul Moore * This program is distributed in the hope that it will be useful, 21*d15c345fSPaul Moore * but WITHOUT ANY WARRANTY; without even the implied warranty of 22*d15c345fSPaul Moore * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 23*d15c345fSPaul Moore * the GNU General Public License for more details. 24*d15c345fSPaul Moore * 25*d15c345fSPaul Moore * You should have received a copy of the GNU General Public License 26*d15c345fSPaul Moore * along with this program; if not, write to the Free Software 27*d15c345fSPaul Moore * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 28*d15c345fSPaul Moore * 29*d15c345fSPaul Moore */ 30*d15c345fSPaul Moore 31*d15c345fSPaul Moore #ifndef _NETLABEL_USER_H 32*d15c345fSPaul Moore #define _NETLABEL_USER_H 33*d15c345fSPaul Moore 34*d15c345fSPaul Moore #include <linux/skbuff.h> 35*d15c345fSPaul Moore #include <linux/capability.h> 36*d15c345fSPaul Moore #include <linux/genetlink.h> 37*d15c345fSPaul Moore #include <net/netlabel.h> 38*d15c345fSPaul Moore #include <net/genetlink.h> 39*d15c345fSPaul Moore 40*d15c345fSPaul Moore /* NetLabel NETLINK helper functions */ 41*d15c345fSPaul Moore 42*d15c345fSPaul Moore /** 43*d15c345fSPaul Moore * netlbl_netlink_cap_check - Check the NETLINK msg capabilities 44*d15c345fSPaul Moore * @skb: the NETLINK buffer 45*d15c345fSPaul Moore * @req_cap: the required capability 46*d15c345fSPaul Moore * 47*d15c345fSPaul Moore * Description: 48*d15c345fSPaul Moore * Check the NETLINK buffer's capabilities against the required capabilities. 49*d15c345fSPaul Moore * Returns zero on success, negative values on failure. 50*d15c345fSPaul Moore * 51*d15c345fSPaul Moore */ 52*d15c345fSPaul Moore static inline int netlbl_netlink_cap_check(const struct sk_buff *skb, 53*d15c345fSPaul Moore kernel_cap_t req_cap) 54*d15c345fSPaul Moore { 55*d15c345fSPaul Moore if (cap_raised(NETLINK_CB(skb).eff_cap, req_cap)) 56*d15c345fSPaul Moore return 0; 57*d15c345fSPaul Moore return -EPERM; 58*d15c345fSPaul Moore } 59*d15c345fSPaul Moore 60*d15c345fSPaul Moore /** 61*d15c345fSPaul Moore * netlbl_getinc_u8 - Read a u8 value from a nlattr stream and move on 62*d15c345fSPaul Moore * @nla: the attribute 63*d15c345fSPaul Moore * @rem_len: remaining length 64*d15c345fSPaul Moore * 65*d15c345fSPaul Moore * Description: 66*d15c345fSPaul Moore * Return a u8 value pointed to by @nla and advance it to the next attribute. 67*d15c345fSPaul Moore * 68*d15c345fSPaul Moore */ 69*d15c345fSPaul Moore static inline u8 netlbl_getinc_u8(struct nlattr **nla, int *rem_len) 70*d15c345fSPaul Moore { 71*d15c345fSPaul Moore u8 val = nla_get_u8(*nla); 72*d15c345fSPaul Moore *nla = nla_next(*nla, rem_len); 73*d15c345fSPaul Moore return val; 74*d15c345fSPaul Moore } 75*d15c345fSPaul Moore 76*d15c345fSPaul Moore /** 77*d15c345fSPaul Moore * netlbl_getinc_u16 - Read a u16 value from a nlattr stream and move on 78*d15c345fSPaul Moore * @nla: the attribute 79*d15c345fSPaul Moore * @rem_len: remaining length 80*d15c345fSPaul Moore * 81*d15c345fSPaul Moore * Description: 82*d15c345fSPaul Moore * Return a u16 value pointed to by @nla and advance it to the next attribute. 83*d15c345fSPaul Moore * 84*d15c345fSPaul Moore */ 85*d15c345fSPaul Moore static inline u16 netlbl_getinc_u16(struct nlattr **nla, int *rem_len) 86*d15c345fSPaul Moore { 87*d15c345fSPaul Moore u16 val = nla_get_u16(*nla); 88*d15c345fSPaul Moore *nla = nla_next(*nla, rem_len); 89*d15c345fSPaul Moore return val; 90*d15c345fSPaul Moore } 91*d15c345fSPaul Moore 92*d15c345fSPaul Moore /** 93*d15c345fSPaul Moore * netlbl_getinc_u32 - Read a u32 value from a nlattr stream and move on 94*d15c345fSPaul Moore * @nla: the attribute 95*d15c345fSPaul Moore * @rem_len: remaining length 96*d15c345fSPaul Moore * 97*d15c345fSPaul Moore * Description: 98*d15c345fSPaul Moore * Return a u32 value pointed to by @nla and advance it to the next attribute. 99*d15c345fSPaul Moore * 100*d15c345fSPaul Moore */ 101*d15c345fSPaul Moore static inline u32 netlbl_getinc_u32(struct nlattr **nla, int *rem_len) 102*d15c345fSPaul Moore { 103*d15c345fSPaul Moore u32 val = nla_get_u32(*nla); 104*d15c345fSPaul Moore *nla = nla_next(*nla, rem_len); 105*d15c345fSPaul Moore return val; 106*d15c345fSPaul Moore } 107*d15c345fSPaul Moore 108*d15c345fSPaul Moore /** 109*d15c345fSPaul Moore * netlbl_netlink_hdr_put - Write the NETLINK buffers into a sk_buff 110*d15c345fSPaul Moore * @skb: the packet 111*d15c345fSPaul Moore * @pid: the PID of the receipient 112*d15c345fSPaul Moore * @seq: the sequence number 113*d15c345fSPaul Moore * @type: the generic NETLINK message family type 114*d15c345fSPaul Moore * @cmd: command 115*d15c345fSPaul Moore * 116*d15c345fSPaul Moore * Description: 117*d15c345fSPaul Moore * Write both a NETLINK nlmsghdr structure and a Generic NETLINK genlmsghdr 118*d15c345fSPaul Moore * struct to the packet. Returns a pointer to the start of the payload buffer 119*d15c345fSPaul Moore * on success or NULL on failure. 120*d15c345fSPaul Moore * 121*d15c345fSPaul Moore */ 122*d15c345fSPaul Moore static inline void *netlbl_netlink_hdr_put(struct sk_buff *skb, 123*d15c345fSPaul Moore u32 pid, 124*d15c345fSPaul Moore u32 seq, 125*d15c345fSPaul Moore int type, 126*d15c345fSPaul Moore u8 cmd) 127*d15c345fSPaul Moore { 128*d15c345fSPaul Moore return genlmsg_put(skb, 129*d15c345fSPaul Moore pid, 130*d15c345fSPaul Moore seq, 131*d15c345fSPaul Moore type, 132*d15c345fSPaul Moore 0, 133*d15c345fSPaul Moore 0, 134*d15c345fSPaul Moore cmd, 135*d15c345fSPaul Moore NETLBL_PROTO_VERSION); 136*d15c345fSPaul Moore } 137*d15c345fSPaul Moore 138*d15c345fSPaul Moore /** 139*d15c345fSPaul Moore * netlbl_netlink_hdr_push - Write the NETLINK buffers into a sk_buff 140*d15c345fSPaul Moore * @skb: the packet 141*d15c345fSPaul Moore * @pid: the PID of the receipient 142*d15c345fSPaul Moore * @seq: the sequence number 143*d15c345fSPaul Moore * @type: the generic NETLINK message family type 144*d15c345fSPaul Moore * @cmd: command 145*d15c345fSPaul Moore * 146*d15c345fSPaul Moore * Description: 147*d15c345fSPaul Moore * Write both a NETLINK nlmsghdr structure and a Generic NETLINK genlmsghdr 148*d15c345fSPaul Moore * struct to the packet. 149*d15c345fSPaul Moore * 150*d15c345fSPaul Moore */ 151*d15c345fSPaul Moore static inline void netlbl_netlink_hdr_push(struct sk_buff *skb, 152*d15c345fSPaul Moore u32 pid, 153*d15c345fSPaul Moore u32 seq, 154*d15c345fSPaul Moore int type, 155*d15c345fSPaul Moore u8 cmd) 156*d15c345fSPaul Moore 157*d15c345fSPaul Moore { 158*d15c345fSPaul Moore struct nlmsghdr *nlh; 159*d15c345fSPaul Moore struct genlmsghdr *hdr; 160*d15c345fSPaul Moore 161*d15c345fSPaul Moore nlh = (struct nlmsghdr *)skb_push(skb, NLMSG_SPACE(GENL_HDRLEN)); 162*d15c345fSPaul Moore nlh->nlmsg_type = type; 163*d15c345fSPaul Moore nlh->nlmsg_len = skb->len; 164*d15c345fSPaul Moore nlh->nlmsg_flags = 0; 165*d15c345fSPaul Moore nlh->nlmsg_pid = pid; 166*d15c345fSPaul Moore nlh->nlmsg_seq = seq; 167*d15c345fSPaul Moore 168*d15c345fSPaul Moore hdr = nlmsg_data(nlh); 169*d15c345fSPaul Moore hdr->cmd = cmd; 170*d15c345fSPaul Moore hdr->version = NETLBL_PROTO_VERSION; 171*d15c345fSPaul Moore hdr->reserved = 0; 172*d15c345fSPaul Moore } 173*d15c345fSPaul Moore 174*d15c345fSPaul Moore /** 175*d15c345fSPaul Moore * netlbl_netlink_payload_len - Return the length of the payload 176*d15c345fSPaul Moore * @skb: the NETLINK buffer 177*d15c345fSPaul Moore * 178*d15c345fSPaul Moore * Description: 179*d15c345fSPaul Moore * This function returns the length of the NetLabel payload. 180*d15c345fSPaul Moore * 181*d15c345fSPaul Moore */ 182*d15c345fSPaul Moore static inline u32 netlbl_netlink_payload_len(const struct sk_buff *skb) 183*d15c345fSPaul Moore { 184*d15c345fSPaul Moore return nlmsg_len((struct nlmsghdr *)skb->data) - GENL_HDRLEN; 185*d15c345fSPaul Moore } 186*d15c345fSPaul Moore 187*d15c345fSPaul Moore /** 188*d15c345fSPaul Moore * netlbl_netlink_payload_data - Returns a pointer to the start of the payload 189*d15c345fSPaul Moore * @skb: the NETLINK buffer 190*d15c345fSPaul Moore * 191*d15c345fSPaul Moore * Description: 192*d15c345fSPaul Moore * This function returns a pointer to the start of the NetLabel payload. 193*d15c345fSPaul Moore * 194*d15c345fSPaul Moore */ 195*d15c345fSPaul Moore static inline void *netlbl_netlink_payload_data(const struct sk_buff *skb) 196*d15c345fSPaul Moore { 197*d15c345fSPaul Moore return (unsigned char *)nlmsg_data((struct nlmsghdr *)skb->data) + 198*d15c345fSPaul Moore GENL_HDRLEN; 199*d15c345fSPaul Moore } 200*d15c345fSPaul Moore 201*d15c345fSPaul Moore /* NetLabel common protocol functions */ 202*d15c345fSPaul Moore 203*d15c345fSPaul Moore void netlbl_netlink_send_ack(const struct genl_info *info, 204*d15c345fSPaul Moore u32 genl_family, 205*d15c345fSPaul Moore u8 ack_cmd, 206*d15c345fSPaul Moore u32 ret_code); 207*d15c345fSPaul Moore 208*d15c345fSPaul Moore /* NetLabel NETLINK I/O functions */ 209*d15c345fSPaul Moore 210*d15c345fSPaul Moore int netlbl_netlink_init(void); 211*d15c345fSPaul Moore int netlbl_netlink_snd(struct sk_buff *skb, u32 pid); 212*d15c345fSPaul Moore int netlbl_netlink_snd_multicast(struct sk_buff *skb, u32 pid, u32 group); 213*d15c345fSPaul Moore 214*d15c345fSPaul Moore #endif 215